HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Simple greeting program based with timezone offset

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simpletimezonewithoffsetprogrambasedgreeting

Problem

I made a simple set of functions that returns a greeting based on the current time, the user's timezone offset, and the user's level. For context I wrote this as a part of a larger chat bot application. I am not sure if there is a better way to handle the if/elif/else blocks in both functions.

Please let me know of places of improvement.

``
"""A module for creating greeting strings based on a user's timezone
offset and user's level."""

import time

def get_part_of_day(user_tz_offset, time_now):
"""Return part of day depending on time_now and the user's timzone
offset value.

user_tz_offset - integer of user's time zone offset in hours
time_now - UTC time in seconds

From - To => part of day
---------------------------
00:00 - 04:59 => midnight
05:00 - 06:59 => dawn
07:00 - 10:59 => morning
11:00 - 12:59 => noon
13:00 - 16:59 => afternoon
17:00 - 18:59 => dusk
19:00 - 20:59 => evening
21:00 - 23:59 => night
"""
user_time = time_now + (user_tz_offset6060)
# gmtime[3] is tm_hour
user_hour = time.gmtime(user_time)[3]

if 0 <= user_hour < 5:
return 'midnight'
elif 5 <= user_hour < 7:
return 'dawn'
elif 7 <= user_hour < 11:
return 'morning'
elif 11 <= user_hour < 13:
return 'noon'
elif 13 <= user_hour < 17:
return 'afternoon'
elif 17 <= user_hour < 19:
return 'dusk'
elif 19 <= user_hour < 21:
return 'evening'
else:
return 'night'

def choose_greeting(username, level, part_of_day):
"""Return greeting string based on user's level and part of day.

username - username string
level - integer of user's level
part_of_day - string from function
get_part_of_day`
"""

greetings = {
'dawn': 'Good early morning',
'morning': 'Good morning',
'afternoon': 'Good afternoon',
'dusk': 'Good afternoon',
'evening': 'Good evenin

Solution

You could use a dictionary for your case conditions:

def midnight():
    print "It's midnight!"

def dawn():
    print "Tis dawn"

def morning():
    print 'good morning'

TOD = {00 : midnight,
        01: midnight,
        02: midnight,
        03: midnight,
        04: midnight,
        05: dawn,
        06: dawn,
        07: morning
        }

TEST = 05
TOD[TEST]()


It should find the key in the dictionary and perform the method defined. This would also avoid you from having to have multiple returns in your function which may be frowned upon.

I'm not sure if this is more readable than your code, but it may be a little more OO friendly. I fear this is not any better but would be great to get other peer reviews.

Code Snippets

def midnight():
    print "It's midnight!"

def dawn():
    print "Tis dawn"

def morning():
    print 'good morning'

TOD = {00 : midnight,
        01: midnight,
        02: midnight,
        03: midnight,
        04: midnight,
        05: dawn,
        06: dawn,
        07: morning
        }

TEST = 05
TOD[TEST]()

Context

StackExchange Code Review Q#87970, answer score: 4

Revisions (0)

No revisions yet.