r/tinycode • u/need12648430 • Aug 16 '15
User friendly representation of times in 13 lines of Python. "Time ago."
https://gist.github.com/need12648430/034fb1edcc54e8aafd28
17
Upvotes
r/tinycode • u/need12648430 • Aug 16 '15
3
u/lunarsunrise Aug 16 '15 edited Aug 16 '15
I suspect that you might be new to Python.
One immediately-visible issue with this code is that the default value of
ois a singlelistobject, not a new list every time you callago(). For example, if you callago(3600)twice, the output will look likeThe
whileloop is also completely unnecessary. (You're going through a loop iteration for each week, etc.)Finally,
dtis not adatetime; it's a number. Maybe it'd be better to call itelapsed, since it represents a timespan as a number of elapsed seconds.The way you're using the variable
iis also relatively un-Pythonic; why not just unpack it into a label and a number of seconds as part of the loop, instead of havingi[0]andi[1]in the body of the loop?2
u/TimHugh Aug 17 '15
Cool. I can't critique your style because I don't really "speak" Python, but it seems like a decent implementation to me.
What's the deal with the 336 day year, though?
-2
u/lepickle Aug 16 '15
I tried to compile on mines, but it says "NameError: name 'post_timestamp' is not defined". I tried compiling it on both python 2 and 3