Procházet zdrojové kódy

datetimeex: format; refactor; added test

Fabian Peter Hammerle před 8 roky
rodič
revize
88871425e4
2 změnil soubory, kde provedl 16 přidání a 10 odebrání
  1. 15 10
      ioex/datetimeex.py
  2. 1 0
      tests/datetimeex/test_duration.py

+ 15 - 10
ioex/datetimeex.py

@@ -51,7 +51,12 @@ class Duration(object):
 
     @property
     def isoformat(self):
-        return 'P%dY' % self.years
+        iso_str = re.sub(
+            r'(?<!\d)0.',
+            '',
+            'P{}Y'.format(self.years),
+        )
+        return 'P0Y' if iso_str == 'P' else iso_str
 
     def __eq__(self, other):
         return (type(self) == type(other)
@@ -145,8 +150,8 @@ class Period(object):
 
     def __eq__(self, other):
         return (type(self) == type(other)
-            and self.start == other.start
-            and self.end == other.end)
+                and self.start == other.start
+                and self.end == other.end)
 
     @classmethod
     def from_yaml(cls, loader, node):
@@ -155,23 +160,23 @@ class Period(object):
     @classmethod
     def to_yaml(cls, dumper, period):
         return dumper.represent_mapping(
-            tag = cls.yaml_tag,
-            mapping = {
+            tag=cls.yaml_tag,
+            mapping={
                 'start': period.start,
                 'end': period.end,
-                },
+            },
             # represent datetime objects with !timestamp tag
-            flow_style = False,
-            )
+            flow_style=False,
+        )
 
     def __repr__(self):
         return '%s(%s)' % (self.__class__.__name__, ', '.join([
             'start = %r' % self.start,
             'end = %r' % self.end,
-            ]))
+        ]))
 
     @classmethod
-    def register_yaml_constructor(cls, loader, tag = yaml_tag):
+    def register_yaml_constructor(cls, loader, tag=yaml_tag):
         register_yaml_timestamp_constructor(loader)
         loader.add_constructor(tag, cls.from_yaml)
 

+ 1 - 0
tests/datetimeex/test_duration.py

@@ -45,6 +45,7 @@ def test_set_years_fail(years, exception_type):
 @pytest.mark.parametrize(('init_params', 'iso'), [
     [{'years': 0}, 'P0Y'],
     [{'years': 3}, 'P3Y'],
+    [{'years': 30}, 'P30Y'],
     ])
 def test_get_isoformat(init_params, iso):
     d = Duration(**init_params)