From 2c64cd271852b950078deff497306bb53c2ace46 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Thu, 7 Jan 2016 10:08:36 +0100 Subject: [PATCH 1/2] Adjust code style according to PEP8 and add OS Independent classifier --- chronyk/chronyk.py | 19 +++--- setup.py | 30 +++++----- test_chronyk.py | 141 +++++++++++++++++++++++++++++++++++++-------- 3 files changed, 146 insertions(+), 44 deletions(-) diff --git a/chronyk/chronyk.py b/chronyk/chronyk.py index 0b560c5..cbc7605 100644 --- a/chronyk/chronyk.py +++ b/chronyk/chronyk.py @@ -67,7 +67,8 @@ def _gmtime(timestamp): try: return time.gmtime(timestamp) except OSError: - dt = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=timestamp) + dt = (datetime.datetime(1970, 1, 1) + + datetime.timedelta(seconds=timestamp)) dst = int(_isdst(dt)) return time.struct_time(dt.timetuple()[:8] + tuple([dst])) @@ -79,10 +80,12 @@ def _dtfromtimestamp(timestamp): return datetime.datetime.fromtimestamp(timestamp) except OSError: timestamp -= time.timezone - dt = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=timestamp) + dt = (datetime.datetime(1970, 1, 1) + + datetime.timedelta(seconds=timestamp)) if _isdst(dt): timestamp += 3600 - dt = datetime.datetime(1970, 1, 1) + datetime.timedelta(seconds=timestamp) + dt = (datetime.datetime(1970, 1, 1) + + datetime.timedelta(seconds=timestamp)) return dt @@ -96,7 +99,8 @@ def _dfromtimestamp(timestamp): d = datetime.date(1970, 1, 1) + datetime.timedelta(seconds=timestamp) if _isdst(d): timestamp += 3600 - d = datetime.date(1970, 1, 1) + datetime.timedelta(seconds=timestamp) + d = (datetime.date(1970, 1, 1) + + datetime.timedelta(seconds=timestamp)) return d @@ -310,7 +314,8 @@ def __fromrelative__(self, timestr): match = re.match(r".*?([0-9]+?) month", timestr) assert match is not None months = int(match.group(1)) - newyear = dati.year + int(((dati.month - 1) + months * coef) / 12) + newyear = (dati.year + + int(((dati.month - 1) + months * coef) / 12)) newmonth = (((dati.month - 1) + months * coef) % 12) + 1 newday = dati.day while newday > calendar.monthrange(newyear, newmonth)[1]: @@ -510,11 +515,11 @@ def datetime(self, timezone=None): if timezone is None: timezone = self.timezone return _dtfromtimestamp(self.__timestamp__ - timezone) - + def date(self, timezone=None): """Returns a datetime.date object. This object retains all information, including timezones. - + :param timezone = self.timezone The timezone (in seconds west of UTC) to return the value in. By default, the timezone used when constructing the class is used diff --git a/setup.py b/setup.py index 7bd37ca..17d4a4a 100644 --- a/setup.py +++ b/setup.py @@ -3,27 +3,29 @@ import os from setuptools import setup + def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read() setup( - name = "Chronyk", - version = "1.0.1", - - packages = ["chronyk"], - install_requires = [], - - author = 'Felix "KoffeinFlummi" Wiegand', - author_email = "koffeinflummi@gmail.com", - description = "A library for parsing human-written times and dates.", - long_description = read("README.rst"), - license = "MIT", - keywords = "time date clock human parser timezone", - url = "https://github.com/KoffeinFlummi/Chronyk", - classifiers = [ + name="Chronyk", + version="1.0.1", + + packages=["chronyk"], + install_requires=[], + + author='Felix "KoffeinFlummi" Wiegand', + author_email="koffeinflummi@gmail.com", + description="A library for parsing human-written times and dates.", + long_description=read("README.rst"), + license="MIT", + keywords="time date clock human parser timezone", + url="https://github.com/KoffeinFlummi/Chronyk", + classifiers=[ "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Natural Language :: English", + "Operating System :: OS Independent", "Programming Language :: Python :: 3.2", "Programming Language :: Python :: 3.3", "Programming Language :: Python :: 3.4" diff --git a/test_chronyk.py b/test_chronyk.py index 633b13f..96683da 100644 --- a/test_chronyk.py +++ b/test_chronyk.py @@ -7,26 +7,35 @@ import calendar import datetime -from chronyk import LOCALTZ, Chronyk, ChronykDelta, currentutc, guesstype, DateRangeError +from chronyk import (LOCALTZ, Chronyk, ChronykDelta, currentutc, guesstype, + DateRangeError) -def isEqual(time1, time2): + +def is_equal(time1, time2): return abs(time1 - time2) < 0.1 + ############################################################################### def test_currentutc(): currentutc() + def test_guesstype(): assert guesstype(9001) == Chronyk(9001) - assert guesstype("2 hours ago").relativestring() == Chronyk("2 hours ago").relativestring() + assert (guesstype("2 hours ago").relativestring() == + Chronyk("2 hours ago").relativestring()) assert guesstype("2 hours") == ChronykDelta("2 hours") + def test_empty_con(): - assert isEqual(Chronyk().timestamp(), time.time()) + assert is_equal(Chronyk().timestamp(), time.time()) + def test_none_con(): - assert isEqual(Chronyk(None).timestamp(), Chronyk(time.time()).timestamp()) + assert is_equal(Chronyk(None).timestamp(), + Chronyk(time.time()).timestamp()) + def test_common_strings(): c1 = Chronyk("today").relativestring() @@ -34,21 +43,32 @@ def test_common_strings(): c3 = Chronyk("this week").relativestring() c4 = Chronyk("this month").relativestring() c5 = Chronyk("this day").relativestring() - assert c1 == c2 and c2 == c3 and c3 == c4 and c4 == c5 + assert c1 == c2 + assert c2 == c3 + assert c3 == c4 + assert c4 == c5 + def test_yesterday(): assert Chronyk("yesterday").relativestring() == "yesterday" + def test_yesteryear(): - assert Chronyk("yesteryear").relativestring() == Chronyk("1 year ago").relativestring() + assert (Chronyk("yesteryear").relativestring() == + Chronyk("1 year ago").relativestring()) # TEST PRE-EPOCH DATES + def test_pre_epoch_1(): - assert Chronyk(datetime.datetime(1950, 1, 1)).datetime() == datetime.datetime(1950, 1, 1) + assert (Chronyk(datetime.datetime(1950, 1, 1)).datetime() == + datetime.datetime(1950, 1, 1)) + def test_pre_epoch_2(): - assert Chronyk(time.strptime("1950 01 01", "%Y %m %d")).timestring("%Y %m %d") == "1950 01 01" + assert (Chronyk(time.strptime("1950 01 01", "%Y %m %d")) + .timestring("%Y %m %d") == "1950 01 01") + # ABSOLUTE STRINGS @@ -56,82 +76,108 @@ def test_absolute_iso(): t = Chronyk("2014-09-18 11:24:47") assert t.ctime() == "Thu Sep 18 11:24:47 2014" + def test_absolute_iso_date(): t = Chronyk("2014-09-18") assert t.ctime() == "Thu Sep 18 00:00:00 2014" + def test_absolute_written_1(): t = Chronyk("May 2nd, 2015") assert t.ctime() == "Sat May 2 00:00:00 2015" + def test_absolute_written_2(): t = Chronyk("2. August 2010") assert t.ctime() == "Mon Aug 2 00:00:00 2010" + def test_absolute_12hr(): t = Chronyk("11:14 am") assert t.ctime()[11:-5] == "11:14:00" + def test_absolute_12hr_seconds(): t = Chronyk("11:14:32 am") assert t.ctime()[11:-5] == "11:14:32" + def test_absolute_24hr(): t = Chronyk("17:14") assert t.ctime()[11:-5] == "17:14:00" + def test_absolute_24hr_seconds(): t = Chronyk("17:14:32") assert t.ctime()[11:-5] == "17:14:32" + def test_absolute_value(): with pytest.raises(ValueError): Chronyk("warglblargl") + # RELATIVE STRINGS def test_relative_now(): assert Chronyk().relativestring() == "just now" + def test_relative_seconds_1(): assert Chronyk("2 seconds ago").relativestring() == "just now" + def test_relative_seconds_2(): assert Chronyk("in 5 seconds").relativestring() == "just now" + def test_relative_seconds_3(): timest = time.time() - assert Chronyk(timest - 5).relativestring(now=timest, minimum=0) == "5 seconds ago" + assert (Chronyk(timest - 5).relativestring(now=timest, minimum=0) == + "5 seconds ago") + def test_relative_minutes_1(): assert Chronyk("1 minute ago").relativestring() == "1 minute ago" + def test_relative_minutes_2(): assert Chronyk("in 2 minutes").relativestring() == "in 2 minutes" + def test_relative_hours_1(): assert Chronyk("1 hour ago").relativestring() == "1 hour ago" + def test_relative_hours_2(): assert Chronyk("in 2 hours").relativestring() == "in 2 hours" + def test_relative_days_1(): assert Chronyk("10 days ago").relativestring() == "10 days ago" + def test_relative_days_2(): assert Chronyk("in 20 days").relativestring() == "in 20 days" + def test_relative_weeks_1(): assert Chronyk("1 week ago").relativestring() == "7 days ago" + def test_relative_weeks_2(): assert Chronyk("in 2 weeks").relativestring() == "in 14 days" + def test_relative_weeks_3(): - assert Chronyk("in blurgh weeks and 2 days").relativestring() == "in 2 days" + assert (Chronyk("in blurgh weeks and 2 days").relativestring() + == "in 2 days") + def test_relative_months_1(): - assert Chronyk("overninethousand months and 2 days ago").relativestring() == "2 days ago" + assert (Chronyk("overninethousand months and 2 days ago").relativestring() + == "2 days ago") + def test_relative_months_2(): dati = datetime.datetime.utcnow() @@ -144,8 +190,11 @@ def test_relative_months_2(): timestr = time.strftime("%Y-%m-%d", dati.timetuple()) assert Chronyk("in 4 months", timezone=0).relativestring() == timestr + def test_relative_years_1(): - assert Chronyk("something years and 2 days ago").relativestring() == "2 days ago" + assert (Chronyk("something years and 2 days ago").relativestring() == + "2 days ago") + def test_relative_years_2(): dati = datetime.datetime.utcnow() @@ -153,73 +202,98 @@ def test_relative_years_2(): timestr = time.strftime("%Y-%m-%d", dati.timetuple()) assert Chronyk("2 years ago").relativestring() == timestr + def test_struct(): timestr = time.localtime() assert Chronyk(timestr).timestamp() == time.mktime(timestr) + def test_valid_1(): Chronyk("2 hours ago", allowfuture=False, allowpast=True) + def test_valid_2(): Chronyk("in 2 hours", allowfuture=True, allowpast=False) + def test_valid_3(): with pytest.raises(DateRangeError): Chronyk("2 hours ago", allowpast=False) + def test_valid_4(): with pytest.raises(DateRangeError): Chronyk("in 2 hours", allowfuture=False) + def test_typeerror(): with pytest.raises(TypeError): Chronyk(["this", "should", "throw", "TypeError"]) + def test_datetime(): timest = currentutc() - assert Chronyk(timest, timezone=0).datetime() == datetime.datetime.fromtimestamp(timest) - + assert (Chronyk(timest, timezone=0).datetime() == + datetime.datetime.fromtimestamp(timest)) + + def test_date(): timest = currentutc() - assert Chronyk(timest, timezone=0).date() == datetime.date.fromtimestamp(timest) + assert (Chronyk(timest, timezone=0).date() == + datetime.date.fromtimestamp(timest)) + def test_timest_1(): timest = time.time() assert Chronyk(timest).timestamp() == timest + def test_timest_2(): timest = time.time() - assert Chronyk(timest, timezone=0).timestamp(timezone=-7200) == timest + 7200 + assert (Chronyk(timest, timezone=0).timestamp(timezone=-7200) == + timest + 7200) + def test_timest_3(): timest = time.time() - assert Chronyk(timest, timezone=-7200).timestamp(timezone=0) == timest - 7200 + assert (Chronyk(timest, timezone=-7200).timestamp(timezone=0) == + timest - 7200) + def test_timestring_1(): timest = time.time() - assert Chronyk(timest).timestring() == time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(timest - LOCALTZ)) + assert (Chronyk(timest).timestring() == + time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(timest - LOCALTZ))) + def test_timestring_2(): timest = time.time() - assert Chronyk(timest).timestring("%Y-%m-%d") == time.strftime("%Y-%m-%d", time.gmtime(timest - LOCALTZ)) + assert (Chronyk(timest).timestring("%Y-%m-%d") == + time.strftime("%Y-%m-%d", time.gmtime(timest - LOCALTZ))) + def test_operators_eq(): timest = time.time() - assert Chronyk(timest) - 5 == Chronyk(timest - 5) and Chronyk(timest, timezone=0) == timest + assert (Chronyk(timest) - 5 == + Chronyk(timest - 5) and Chronyk(timest, timezone=0) == timest) + def test_operators_str(): t = Chronyk() assert str(t) == t.timestring() + def test_operators_num(): timest = time.time() t = Chronyk(timest, timezone=0) assert int(timest) == int(t) and int(t) == int(float(t)) + def test_operators_ne(): timest = time.time() assert Chronyk(timest) != Chronyk(timest - 2) + def test_operators_ltgt(): timest = time.time() assert Chronyk(timest) > Chronyk(timest - 5) @@ -231,74 +305,95 @@ def test_operators_ltgt(): assert Chronyk(timest) >= Chronyk(timest - 5) assert Chronyk(timest, timezone=0) >= timest - 5 + def test_operators_add(): timest = time.time() assert Chronyk(timest) + ChronykDelta(5) == Chronyk(timest + 5) assert Chronyk(timest) + 5 == Chronyk(timest + 5) + def test_operators_sub(): timest = time.time() assert Chronyk(timest) - Chronyk(timest - 5) == ChronykDelta(5) assert Chronyk(timest) - ChronykDelta(5) == Chronyk(timest - 5) assert Chronyk(timest, timezone=0) - 5 == timest - 5 + def test_delta_type(): with pytest.raises(TypeError): ChronykDelta(["WEEE", "EEEEE", "EEEEEE"]) + def test_delta_timestring_1(): assert ChronykDelta("5 hours").timestring() == "5 hours" + def test_delta_timestring_2(): assert ChronykDelta("1 week").timestring() == "7 days" + def test_delta_timestring_3(): assert ChronykDelta("1 hour").timestring() == "1 hour" + def test_delta_timestring_4(): - assert ChronykDelta("1 day and 12 hours").timestring() == "1 day and 12 hours" + assert (ChronykDelta("1 day and 12 hours").timestring() == + "1 day and 12 hours") + def test_delta_timestring_5(): assert ChronykDelta("1 day and 12 hours").timestring(maxunits=1) == "1 day" + def test_delta_timestring_6(): with pytest.raises(ValueError): ChronykDelta("1 day ago").timestring(maxunits=0) + def test_delta_timestring_7(): assert ChronykDelta(0).timestring() == "" + def test_delta_operators_str(): assert ChronykDelta(5).timestring() == str(ChronykDelta(5)) + def test_delta_operators_num(): - assert 5 == int(ChronykDelta(5)) and int(ChronykDelta(5)) == float(ChronykDelta(5)) + assert 5 == int(ChronykDelta(5)) + assert int(ChronykDelta(5)) == float(ChronykDelta(5)) + def test_delta_operators_eq(): assert ChronykDelta(5) == ChronykDelta(5) and ChronykDelta(5) == 5 + def test_delta_operators_neq(): assert ChronykDelta(5) != ChronykDelta(6) and ChronykDelta(5) != 3 + def test_delta_operators_ltgt(): assert ChronykDelta(5) > ChronykDelta(4) and ChronykDelta(5) > 3 assert ChronykDelta(5) < ChronykDelta(7) and ChronykDelta(5) < 9 assert ChronykDelta(5) >= ChronykDelta(5) and ChronykDelta(5) >= 3 assert ChronykDelta(5) <= 5 and ChronykDelta(5) <= ChronykDelta(6) + def test_delta_operators_add(): timest = time.time() assert ChronykDelta(5) + ChronykDelta(-5) == 0 assert ChronykDelta(5) + Chronyk(timest) == Chronyk(timest + 5) assert ChronykDelta(5) + 10 == 15 + def test_delta_operators_sub(): assert ChronykDelta(5) - 5 == 0 assert ChronykDelta(5) - ChronykDelta(1) == 4 + def test_delta_operators_mul(): assert ChronykDelta(12) * 2 == 24 + def test_delta_operators_div(): assert ChronykDelta(10) / 2 == 5 From d04603da783a0273307d2f84bcba809af5a06fd2 Mon Sep 17 00:00:00 2001 From: Martin Thoma Date: Thu, 7 Jan 2016 10:13:30 +0100 Subject: [PATCH 2/2] Split assert statements with 'and' This makes it easier to see what actually failed --- test_chronyk.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/test_chronyk.py b/test_chronyk.py index 96683da..17839dc 100644 --- a/test_chronyk.py +++ b/test_chronyk.py @@ -274,8 +274,8 @@ def test_timestring_2(): def test_operators_eq(): timest = time.time() - assert (Chronyk(timest) - 5 == - Chronyk(timest - 5) and Chronyk(timest, timezone=0) == timest) + assert Chronyk(timest) - 5 == Chronyk(timest - 5) + assert Chronyk(timest, timezone=0) == timest def test_operators_str(): @@ -286,7 +286,8 @@ def test_operators_str(): def test_operators_num(): timest = time.time() t = Chronyk(timest, timezone=0) - assert int(timest) == int(t) and int(t) == int(float(t)) + assert int(timest) == int(t) + assert int(t) == int(float(t)) def test_operators_ne(): @@ -364,18 +365,27 @@ def test_delta_operators_num(): def test_delta_operators_eq(): - assert ChronykDelta(5) == ChronykDelta(5) and ChronykDelta(5) == 5 + assert ChronykDelta(5) == ChronykDelta(5) + assert ChronykDelta(5) == 5 def test_delta_operators_neq(): - assert ChronykDelta(5) != ChronykDelta(6) and ChronykDelta(5) != 3 + assert ChronykDelta(5) != ChronykDelta(6) + assert ChronykDelta(5) != 3 def test_delta_operators_ltgt(): - assert ChronykDelta(5) > ChronykDelta(4) and ChronykDelta(5) > 3 - assert ChronykDelta(5) < ChronykDelta(7) and ChronykDelta(5) < 9 - assert ChronykDelta(5) >= ChronykDelta(5) and ChronykDelta(5) >= 3 - assert ChronykDelta(5) <= 5 and ChronykDelta(5) <= ChronykDelta(6) + assert ChronykDelta(5) > ChronykDelta(4) + assert ChronykDelta(5) > 3 + + assert ChronykDelta(5) < ChronykDelta(7) + assert ChronykDelta(5) < 9 + + assert ChronykDelta(5) >= ChronykDelta(5) + assert ChronykDelta(5) >= 3 + + assert ChronykDelta(5) <= 5 + assert ChronykDelta(5) <= ChronykDelta(6) def test_delta_operators_add():