commit c672eb00c9f7193bcf6eade3f44f326bff0df94e Author: RussellBallestrini Date: Fri Jun 29 19:25:55 2012 -0400 first commit, packaged on pypi, easy_install ago! diff --git a/.hgignore b/.hgignore new file mode 100644 index 0000000..cef8f3e --- /dev/null +++ b/.hgignore @@ -0,0 +1,9 @@ +# use glob syntax. +syntax: glob + +*.pyc +*.swp +*.orig +*.egg* +*.gz +build/* diff --git a/ago.py b/ago.py new file mode 100644 index 0000000..13b1054 --- /dev/null +++ b/ago.py @@ -0,0 +1,48 @@ +def delta2dict( delta ): + """return dictionary of delta""" + return { + 'year' : delta.days / 365 , + 'day' : delta.days % 365 , + 'hour' : delta.seconds / 3600 , + 'minute' : (delta.seconds / 60) % 60 , + 'second' : delta.seconds % 60 , + 'microsecond' : delta.microseconds + } + +def human( delta, precision = 1 ): + """return human readable delta string""" + units = ( 'year', 'day', 'hour', 'minute', 'second', 'microsecond' ) + d = delta2dict( delta ) + hlist = [] + count = 0 + for unit in units: + if count >= precision: break # met precision + if d[ unit ] == 0: continue # skip 0's + s = '' if d[ unit ] == 1 else 's' # handle plurals + hlist.append( '%s %s%s' % ( d[unit], unit, s ) ) + count += 1 + return ', '.join( hlist ) + +def test(): + """Test and example usage""" + from datetime import datetime + + d1 = datetime( year = 2009, month=5, day=4, hour=6, minute=54, second=33, microsecond=4000 ) + d2 = datetime( year = 2011, month=10, day=6, hour=8, minute=55, second=36 ) + + delta = d2 - d1 + + print delta2dict( delta ) + print 'Created ' + human( delta ) + ' ago.' + print 'Created ' + human( delta, 3 ) + ' ago.' + print 'Created ' + human( delta, 6 ) + ' ago.' + + d3 = datetime.now() + delta = d3 - d2 + + print 'Posted ' + human( delta ) + ' ago' + print 'Commented ' + human( delta, 3 ) + ' ago' + print 'Edited ' + human( delta, 6 ) + ' ago' + +if __name__ == "__main__": + test() diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..a98ec56 --- /dev/null +++ b/setup.py @@ -0,0 +1,27 @@ +# installation: easy_install ago + +from setuptools import setup + +setup( + name='ago', + version='0.0.1', + description='ago: Human readable timedeltas', + + author = 'Russell Ballestrini', + author_email = 'russell@ballestrini.net', + keywords = 'ago human readable time deltas timedelta', + url = 'https://bitbucket.org/russellballestrini/ago/src', + + platforms=['All'], + license='Public Domain', + + #package_dir = {'': ''}, + py_modules = ['ago'], + include_package_data=True, +) + +# setup keyword args: http://peak.telecommunity.com/DevCenter/setuptools + +# built and uploaded to pypi with this: +# python setup.py sdist bdist_egg register upload +