durationpy 0.4__tar.gz → 0.7__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
durationpy-0.7/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright 2017 Ilia Choly
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -1,11 +1,10 @@
1
- Metadata-Version: 1.1
1
+ Metadata-Version: 2.1
2
2
  Name: durationpy
3
- Version: 0.4
3
+ Version: 0.7
4
4
  Summary: Module for converting between datetime.timedelta and Go's Duration strings.
5
5
  Home-page: https://github.com/icholy/durationpy
6
+ Download-URL: https://github.com/icholy/durationpy/tarball/0.7
6
7
  Author: Ilia Choly
7
8
  Author-email: ilia.choly@gmail.com
8
9
  License: MIT
9
- Download-URL: https://github.com/icholy/durationpy/tarball/0.3
10
- Description: UNKNOWN
11
- Platform: UNKNOWN
10
+ License-File: LICENSE
@@ -0,0 +1,29 @@
1
+ # durationpy
2
+
3
+ > Module for converting between `datetime.timedelta` and Go's Duration strings.
4
+
5
+ ### Install
6
+
7
+ ``` sh
8
+ $ pip install durationpy
9
+ ```
10
+
11
+
12
+ ### Parse
13
+
14
+ * `ns` - nanoseconds
15
+ * `us` - microseconds
16
+ * `ms` - millisecond
17
+ * `s` - second
18
+ * `m` - minute
19
+ * `h` - hour
20
+
21
+ ``` py
22
+ # parse
23
+ td = durationpy.from_str("4h3m2s1ms")
24
+
25
+ # format
26
+ durationpy.to_str(td)
27
+ ```
28
+
29
+ **Note:** nanosecond precision is lost because `datetime.timedelta` uses microsecond resolution.
@@ -0,0 +1,2 @@
1
+ # -*- coding: UTF-8 -*-
2
+ from .duration import to_str, from_str, DurationError
@@ -29,30 +29,45 @@ units = {
29
29
  "y": _year_size,
30
30
  }
31
31
 
32
+ _duration_re = re.compile(r'([\d\.]+)([a-zµμ]+)')
33
+
34
+
35
+ class DurationError(ValueError):
36
+ """duration error"""
37
+
32
38
 
33
39
  def from_str(duration):
34
40
  """Parse a duration string to a datetime.timedelta"""
35
41
 
42
+ original = duration
43
+
36
44
  if duration in ("0", "+0", "-0"):
37
45
  return datetime.timedelta()
38
46
 
39
- pattern = re.compile('([\d\.]+)([a-zµμ]+)')
40
- total = 0
41
- sign = -1 if duration[0] == '-' else 1
42
- matches = pattern.findall(duration)
47
+ sign = 1
48
+ if duration and duration[0] in '+-':
49
+ if duration[0] == '-':
50
+ sign = -1
51
+ duration = duration[1:]
43
52
 
44
- if not len(matches):
45
- raise Exception("Invalid duration {}".format(duration))
53
+ matches = list(_duration_re.finditer(duration))
54
+ if not matches:
55
+ raise DurationError("Invalid duration {}".format(original))
56
+ if matches[0].start() != 0 or matches[-1].end() != len(duration):
57
+ raise DurationError(
58
+ 'Extra chars at start or end of duration {}'.format(original))
46
59
 
47
- for (value, unit) in matches:
60
+ total = 0
61
+ for match in matches:
62
+ value, unit = match.groups()
48
63
  if unit not in units:
49
- raise Exception(
50
- "Unknown unit {} in duration {}".format(unit, duration))
64
+ raise DurationError(
65
+ "Unknown unit {} in duration {}".format(unit, original))
51
66
  try:
52
67
  total += float(value) * units[unit]
53
- except:
54
- raise Exception(
55
- "Invalid value {} in duration {}".format(value, duration))
68
+ except Exception:
69
+ raise DurationError(
70
+ "Invalid value {} in duration {}".format(value, original))
56
71
 
57
72
  microseconds = total / _microsecond_size
58
73
  return datetime.timedelta(microseconds=sign * microseconds)
@@ -62,9 +77,9 @@ def to_str(delta, extended=False):
62
77
 
63
78
  total_seconds = delta.total_seconds()
64
79
  sign = "-" if total_seconds < 0 else ""
65
- nanoseconds = abs(total_seconds * _second_size)
80
+ nanoseconds = round(abs(total_seconds * _second_size), 0)
66
81
 
67
- if total_seconds < 1:
82
+ if abs(total_seconds) < 1:
68
83
  result_str = _to_str_small(nanoseconds, extended)
69
84
  else:
70
85
  result_str = _to_str_large(nanoseconds, extended)
@@ -109,12 +124,12 @@ def _to_str_large(nanoseconds, extended):
109
124
  months = int(nanoseconds / _month_size)
110
125
  if months:
111
126
  nanoseconds -= _month_size * months
112
- result_str += "{:g}mm"
127
+ result_str += "{:g}mm".format(months)
113
128
 
114
129
  days = int(nanoseconds / _day_size)
115
130
  if days:
116
131
  nanoseconds -= _day_size * days
117
- result_str += "{:g}d"
132
+ result_str += "{:g}d".format(days)
118
133
 
119
134
  hours = int(nanoseconds / _hour_size)
120
135
  if hours:
@@ -132,5 +147,3 @@ def _to_str_large(nanoseconds, extended):
132
147
  result_str += "{:g}s".format(seconds)
133
148
 
134
149
  return result_str
135
-
136
-
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.1
2
+ Name: durationpy
3
+ Version: 0.7
4
+ Summary: Module for converting between datetime.timedelta and Go's Duration strings.
5
+ Home-page: https://github.com/icholy/durationpy
6
+ Download-URL: https://github.com/icholy/durationpy/tarball/0.7
7
+ Author: Ilia Choly
8
+ Author-email: ilia.choly@gmail.com
9
+ License: MIT
10
+ License-File: LICENSE
@@ -0,0 +1,10 @@
1
+ LICENSE
2
+ README.md
3
+ setup.cfg
4
+ setup.py
5
+ durationpy/__init__.py
6
+ durationpy/duration.py
7
+ durationpy.egg-info/PKG-INFO
8
+ durationpy.egg-info/SOURCES.txt
9
+ durationpy.egg-info/dependency_links.txt
10
+ durationpy.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ durationpy
@@ -1,2 +1,7 @@
1
1
  [metadata]
2
2
  description-file = README.md
3
+
4
+ [egg_info]
5
+ tag_build =
6
+ tag_date = 0
7
+
@@ -1,4 +1,4 @@
1
- from distutils.core import setup
1
+ from setuptools import setup
2
2
 
3
3
  setup(
4
4
  name = 'durationpy',
@@ -6,8 +6,8 @@ setup(
6
6
  url = 'https://github.com/icholy/durationpy',
7
7
  author = 'Ilia Choly',
8
8
  author_email = 'ilia.choly@gmail.com',
9
- download_url = 'https://github.com/icholy/durationpy/tarball/0.3',
10
- version = '0.4',
9
+ download_url = 'https://github.com/icholy/durationpy/tarball/0.7',
10
+ version = '0.7',
11
11
  packages = ['durationpy'],
12
12
  license = 'MIT'
13
13
  )
@@ -1,3 +0,0 @@
1
- # -*- coding: UTF-8 -*-
2
- from .duration import to_str, from_str
3
-