Skip to content

Commit

Permalink
Completed version 0.6.4
Browse files Browse the repository at this point in the history
- Modified initialization test cases.
- Modified Time constructor
- Fixed error in documentation.
- Upgraded version number in setup.py
  • Loading branch information
Francesco Ricciardi committed Nov 27, 2015
1 parent d1d2c82 commit 6a53a17
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
11 changes: 7 additions & 4 deletions datetime2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,15 +190,18 @@ def __get__(self, instance, owner):

@total_ordering
class Time:
def __init__(self, day_frac, denominator=1, *, correction=None):
def __init__(self, day_frac, *, correction=None):
self.correction = correction # for the time being
try:
if denominator != 1:
self._day_frac = Fraction(day_frac, denominator)
if type(day_frac) == tuple:
if len(day_frac) == 2:
self._day_frac = Fraction(*day_frac)
else:
raise TypeError('Time argument tuple is invalid')
else:
self._day_frac = Fraction(day_frac)
except ZeroDivisionError:
raise ZeroDivisionError("Time denominator cannot be <ero.".format(str(day_frac)))
raise ZeroDivisionError("Time denominator cannot be zero.".format(str(day_frac)))
if self.day_frac < 0 or self.day_frac >= 1:
raise ValueError("resulting fraction is outside range valid for Time instances (0 <= value < 1): {}".format(float(self.day_frac)))

Expand Down
7 changes: 3 additions & 4 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,9 @@ There are two ways of creating a :class:`Time` instance:
fraction that will be passed to the :class:`fractions.Fraction`
constructor. In any case, the resulting value for ``day_frac`` must be
equal or greater than 0 and less than 1. A :exc:`TypeError` exception
is raised if the argument type is not one of the accepted types. A
:exc:`ZeroDivisionError` exception is raised if denominator is 0. A
:exc:`ValueError` exception is raised in the argument value is outside
the accepted range.
is raised if the argument type is not one of the accepted types or the
tuple argument does not have two values. A :exc:`ZeroDivisionError`
exception is raised if denominator is 0.

.. classmethod:: Time.now(correction = None)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='datetime2',
version='0.6.2',
version='0.6.4',

description='New date and time classes',
long_description=long_description,
Expand Down
27 changes: 12 additions & 15 deletions tests/test_datetime2.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,12 @@ def newmeth(self, start):
# Time tests
#
time_test_data = [
[Fraction(0, 1), [0, Fraction(0), Decimal('0'), 0.0, '0', '0/33']],
[Fraction(1, 4), [Fraction(1, 4), Decimal('0.25'), 0.25, '0.25', '1/4']]]
[Fraction(0, 1), [0, Fraction(0), Decimal('0'), 0.0, '0', '0/33', (0, 5)]],
[Fraction(1, 4), [Fraction(1, 4), Decimal('0.25'), 0.25, '0.25', '1/4', (2, 8)]]]
# we are not going to test more values, because we don't want to test the Fraction constructor :-)

# but we want to test with a few strange values
time_strange_test_data = (Fraction(123, 4567), 0.999999, '0.999999', '0.0000001', '5/456789')
time_strange_test_data = (Fraction(123, 4567), 0.999999, '0.999999', '0.0000001', '5/456789', (123, 4567))

class TestTime:
def test_000_valid_argument_types(self):
Expand All @@ -331,27 +331,20 @@ def test_000_valid_argument_types(self):
for input_value in input_values:
assert Time(input_value).day_frac == day_frac

def test_003_valid_argument_types_strange(self):
def test_005_valid_argument_types_strange(self):
"The day_frac argument can be anything that can be passed to the fractions.Fraction constructor."
for input_value in time_strange_test_data:
Time(input_value)

def test_006_valid_fractional_argument(self):
"The day_frac argument can be anything that can be passed to the fractions.Fraction constructor."
for numerator in (2, Fraction(4, 3)):
Time(numerator, 4)
for denominator in (2, Fraction(4, 3)):
Time(1, denominator)

def test_010_invalid_argument_types(self):
"A TypeError exception is raised if the argument type is not one of the accepted types."
# exception with no or two parameters
with pytest.raises(TypeError):
Time()
with pytest.raises(TypeError):
Time(1, 2, 3)
Time(1, 2)
# exception with non-numeric types
for par in (1j, (1,), [1], {1:1}, (), [], {}, None):
for par in (1j, (1,), [1], {1:1}, [], {}, None, (1,2,3)):
with pytest.raises(TypeError):
Time(par)

Expand All @@ -360,11 +353,15 @@ def test_015_invalid_argument_values(self):
for par in (-0.00001, 1, 1.00000001, 10000, -10000):
with pytest.raises(ValueError):
Time(par)
# same for tuple argument
for par in ( (1000, 1), (4, 2), (2, 2), (-1, -1), (-1, 1000000), (-3, 3), (1000000, -2)):
with pytest.raises(ValueError):
Time(par)
for den in (2, -2, -10000):
with pytest.raises(ValueError):
Time(2, den)
Time((2, den))
with pytest.raises(ZeroDivisionError):
Time(2, 0)
Time((2, 0))

def test_020_now(self):
"Return an object that represents the current moment in the day."
Expand Down

0 comments on commit 6a53a17

Please sign in to comment.