nice-duration 0.7.0__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Tom Koelman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,111 @@
1
+ Metadata-Version: 2.4
2
+ Name: nice_duration
3
+ Version: 0.7.0
4
+ Summary: Nice formatting of timedelta instances
5
+ Author: Tom Koelman
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 Tom Koelman
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+ Project-URL: Homepage, https://github.com/tomkoelman/nice_duration
28
+ Project-URL: Issues, https://github.com/tomkoelman/nice_duration/issues
29
+ Classifier: Programming Language :: Python :: 3
30
+ Classifier: Operating System :: OS Independent
31
+ Classifier: Development Status :: 5 - Production/Stable
32
+ Classifier: Intended Audience :: Developers
33
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
34
+ Requires-Python: >=3.13
35
+ Description-Content-Type: text/markdown
36
+ License-File: LICENSE
37
+ Dynamic: license-file
38
+
39
+ # nice_duration
40
+
41
+ This is a Python module that introduces a way of formatting a
42
+ `timedelta` instance like so: `3h12m`. It can also format an `int`
43
+ representing a number of seconds in the same way.
44
+
45
+ The `separator` parameter, which defaults to the empty string,
46
+ determines what the individual components are concatenated with.
47
+ Setting `separator` to a single space changes the above example to
48
+ `3h 12m`.
49
+
50
+ Components with value 0 are left out. That can be changed by setting
51
+ - `keep_leading_zeroes`
52
+ - `keep_trailing_zeroes`
53
+ - `keep_infix_zeroes`
54
+
55
+ For example, by setting `keep_trailing_zeroes` to `True`, the example `duration_string` is
56
+ changed to `3h12m0s`.
57
+
58
+ For convenience, setting `keep_zeroes` to `True` will set all three
59
+ `keep_*_zeroes` to `True`.
60
+
61
+ ** Examples
62
+ ```python
63
+ >>> from datetime import timedelta
64
+ >>> from nice_duration import duration_string
65
+ >>> delta = timedelta(hours=3, minutes=20)
66
+ >>> duration_string(delta)
67
+ '3h20m'
68
+ ```
69
+
70
+ ```python
71
+ >>> duration_string(200) # When an int is given, it is interpreted as a number of seconds
72
+ '3h20m'
73
+ ```
74
+
75
+ Setting `separator`:
76
+
77
+ ```python
78
+ >>> duration_string(delta, separator=" ")
79
+ '3h 20m'
80
+ ```
81
+
82
+ Setting `keep_zeroes`:
83
+
84
+ ```python
85
+ >>> duration_string(delta, keep_zeroes=True)
86
+ '0w0d3h20m0s'
87
+ ```
88
+
89
+ Setting `keep_trailing_zeroes`:
90
+ ```python
91
+ >>> duration_string(delta, keep_trailing_zeroes=True)
92
+ '3h20m0s'
93
+ ```
94
+
95
+ Setting `keep_leading_zeroes`:
96
+ ```python
97
+ >>> duration_string(delta, keep_leading_zeroes=True)
98
+ '0w0d3h20m'
99
+ ```
100
+
101
+ Setting `keep_infix_zeroes`:
102
+ ```python
103
+ >>> duration_string(timedelta(hours=3, seconds=11), keep_infix_zeroes=True)
104
+ '3h0m11s'
105
+ ```
106
+
107
+ Return value when `timedelta` is 0:
108
+ ```python
109
+ >>> duration_string(timedelta())
110
+ '0s'
111
+ ```
@@ -0,0 +1,73 @@
1
+ # nice_duration
2
+
3
+ This is a Python module that introduces a way of formatting a
4
+ `timedelta` instance like so: `3h12m`. It can also format an `int`
5
+ representing a number of seconds in the same way.
6
+
7
+ The `separator` parameter, which defaults to the empty string,
8
+ determines what the individual components are concatenated with.
9
+ Setting `separator` to a single space changes the above example to
10
+ `3h 12m`.
11
+
12
+ Components with value 0 are left out. That can be changed by setting
13
+ - `keep_leading_zeroes`
14
+ - `keep_trailing_zeroes`
15
+ - `keep_infix_zeroes`
16
+
17
+ For example, by setting `keep_trailing_zeroes` to `True`, the example `duration_string` is
18
+ changed to `3h12m0s`.
19
+
20
+ For convenience, setting `keep_zeroes` to `True` will set all three
21
+ `keep_*_zeroes` to `True`.
22
+
23
+ ** Examples
24
+ ```python
25
+ >>> from datetime import timedelta
26
+ >>> from nice_duration import duration_string
27
+ >>> delta = timedelta(hours=3, minutes=20)
28
+ >>> duration_string(delta)
29
+ '3h20m'
30
+ ```
31
+
32
+ ```python
33
+ >>> duration_string(200) # When an int is given, it is interpreted as a number of seconds
34
+ '3h20m'
35
+ ```
36
+
37
+ Setting `separator`:
38
+
39
+ ```python
40
+ >>> duration_string(delta, separator=" ")
41
+ '3h 20m'
42
+ ```
43
+
44
+ Setting `keep_zeroes`:
45
+
46
+ ```python
47
+ >>> duration_string(delta, keep_zeroes=True)
48
+ '0w0d3h20m0s'
49
+ ```
50
+
51
+ Setting `keep_trailing_zeroes`:
52
+ ```python
53
+ >>> duration_string(delta, keep_trailing_zeroes=True)
54
+ '3h20m0s'
55
+ ```
56
+
57
+ Setting `keep_leading_zeroes`:
58
+ ```python
59
+ >>> duration_string(delta, keep_leading_zeroes=True)
60
+ '0w0d3h20m'
61
+ ```
62
+
63
+ Setting `keep_infix_zeroes`:
64
+ ```python
65
+ >>> duration_string(timedelta(hours=3, seconds=11), keep_infix_zeroes=True)
66
+ '3h0m11s'
67
+ ```
68
+
69
+ Return value when `timedelta` is 0:
70
+ ```python
71
+ >>> duration_string(timedelta())
72
+ '0s'
73
+ ```
@@ -0,0 +1,34 @@
1
+ [project]
2
+ name = "nice_duration"
3
+ version = "0.7.0"
4
+ authors = [
5
+ { name="Tom Koelman" },
6
+ ]
7
+ description = "Nice formatting of timedelta instances"
8
+ readme = "README.md"
9
+ requires-python = ">=3.13"
10
+ classifiers = [
11
+ "Programming Language :: Python :: 3",
12
+ "Operating System :: OS Independent",
13
+ "Development Status :: 5 - Production/Stable",
14
+ "Intended Audience :: Developers",
15
+ "Topic :: Software Development :: Libraries :: Python Modules",
16
+ ]
17
+ license = {file = "LICENSE"}
18
+ dependencies = []
19
+
20
+
21
+ [project.urls]
22
+ Homepage = "https://github.com/tomkoelman/nice_duration"
23
+ Issues = "https://github.com/tomkoelman/nice_duration/issues"
24
+
25
+ [dependency-groups]
26
+ dev = [
27
+ "pytest>=8.3.5",
28
+ "twine>=6.1.0",
29
+ ]
30
+
31
+ [tool.pytest.ini_options]
32
+ pythonpath = [
33
+ "src"
34
+ ]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
File without changes
@@ -0,0 +1,111 @@
1
+ Metadata-Version: 2.4
2
+ Name: nice_duration
3
+ Version: 0.7.0
4
+ Summary: Nice formatting of timedelta instances
5
+ Author: Tom Koelman
6
+ License: MIT License
7
+
8
+ Copyright (c) 2025 Tom Koelman
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+ Project-URL: Homepage, https://github.com/tomkoelman/nice_duration
28
+ Project-URL: Issues, https://github.com/tomkoelman/nice_duration/issues
29
+ Classifier: Programming Language :: Python :: 3
30
+ Classifier: Operating System :: OS Independent
31
+ Classifier: Development Status :: 5 - Production/Stable
32
+ Classifier: Intended Audience :: Developers
33
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
34
+ Requires-Python: >=3.13
35
+ Description-Content-Type: text/markdown
36
+ License-File: LICENSE
37
+ Dynamic: license-file
38
+
39
+ # nice_duration
40
+
41
+ This is a Python module that introduces a way of formatting a
42
+ `timedelta` instance like so: `3h12m`. It can also format an `int`
43
+ representing a number of seconds in the same way.
44
+
45
+ The `separator` parameter, which defaults to the empty string,
46
+ determines what the individual components are concatenated with.
47
+ Setting `separator` to a single space changes the above example to
48
+ `3h 12m`.
49
+
50
+ Components with value 0 are left out. That can be changed by setting
51
+ - `keep_leading_zeroes`
52
+ - `keep_trailing_zeroes`
53
+ - `keep_infix_zeroes`
54
+
55
+ For example, by setting `keep_trailing_zeroes` to `True`, the example `duration_string` is
56
+ changed to `3h12m0s`.
57
+
58
+ For convenience, setting `keep_zeroes` to `True` will set all three
59
+ `keep_*_zeroes` to `True`.
60
+
61
+ ** Examples
62
+ ```python
63
+ >>> from datetime import timedelta
64
+ >>> from nice_duration import duration_string
65
+ >>> delta = timedelta(hours=3, minutes=20)
66
+ >>> duration_string(delta)
67
+ '3h20m'
68
+ ```
69
+
70
+ ```python
71
+ >>> duration_string(200) # When an int is given, it is interpreted as a number of seconds
72
+ '3h20m'
73
+ ```
74
+
75
+ Setting `separator`:
76
+
77
+ ```python
78
+ >>> duration_string(delta, separator=" ")
79
+ '3h 20m'
80
+ ```
81
+
82
+ Setting `keep_zeroes`:
83
+
84
+ ```python
85
+ >>> duration_string(delta, keep_zeroes=True)
86
+ '0w0d3h20m0s'
87
+ ```
88
+
89
+ Setting `keep_trailing_zeroes`:
90
+ ```python
91
+ >>> duration_string(delta, keep_trailing_zeroes=True)
92
+ '3h20m0s'
93
+ ```
94
+
95
+ Setting `keep_leading_zeroes`:
96
+ ```python
97
+ >>> duration_string(delta, keep_leading_zeroes=True)
98
+ '0w0d3h20m'
99
+ ```
100
+
101
+ Setting `keep_infix_zeroes`:
102
+ ```python
103
+ >>> duration_string(timedelta(hours=3, seconds=11), keep_infix_zeroes=True)
104
+ '3h0m11s'
105
+ ```
106
+
107
+ Return value when `timedelta` is 0:
108
+ ```python
109
+ >>> duration_string(timedelta())
110
+ '0s'
111
+ ```
@@ -0,0 +1,9 @@
1
+ LICENSE
2
+ README.md
3
+ pyproject.toml
4
+ src/__init__.py
5
+ src/nice_duration.py
6
+ src/nice_duration.egg-info/PKG-INFO
7
+ src/nice_duration.egg-info/SOURCES.txt
8
+ src/nice_duration.egg-info/dependency_links.txt
9
+ src/nice_duration.egg-info/top_level.txt
@@ -0,0 +1,2 @@
1
+ __init__
2
+ nice_duration
@@ -0,0 +1,108 @@
1
+ from datetime import timedelta
2
+
3
+ amount_of_seconds = {
4
+ "week": 604800,
5
+ "day": 86400,
6
+ "hour": 3600,
7
+ "minute": 60,
8
+ "second": 1,
9
+ }
10
+ unit_names = {"week": "w", "day": "d", "hour": "h", "minute": "m", "second": "s"}
11
+
12
+
13
+ def duration_string(
14
+ duration: timedelta | int,
15
+ separator="",
16
+ keep_leading_zeroes=False,
17
+ keep_trailing_zeroes=False,
18
+ keep_infix_zeroes=False,
19
+ keep_zeroes=False,
20
+ ) -> str:
21
+ """Convert a timedelta object to a string.
22
+
23
+ Examples:
24
+ duration_string(timedelta(hours=3, minutes=20)) = "3h20m"
25
+ duration_string(timedelta(hours=3, minutes=20), separator=" ") = "3h 20m"
26
+ duration_string(timedelta(hours=3, minutes=20), keep_zeroes=True) = "0w0d3h20m0s"
27
+ """
28
+
29
+ if keep_zeroes:
30
+ keep_leading_zeroes = True
31
+ keep_infix_zeroes = True
32
+ keep_trailing_zeroes = True
33
+
34
+ if type(duration) is int:
35
+ remainder = duration
36
+ else:
37
+ remainder = int(duration.total_seconds())
38
+
39
+ values = {}
40
+
41
+ for field in amount_of_seconds.keys():
42
+ value, remainder = divmod(remainder, amount_of_seconds[field])
43
+ values[field] = value
44
+
45
+ if not keep_leading_zeroes:
46
+ for field in values.copy():
47
+ if values[field]:
48
+ break
49
+ else:
50
+ del values[field]
51
+
52
+ if not keep_trailing_zeroes:
53
+ for field in reversed(values.copy()):
54
+ if values[field]:
55
+ break
56
+ else:
57
+ del values[field]
58
+
59
+ if not keep_infix_zeroes and len(values) > 2:
60
+ # We transform the values dictionary to a list of pairs
61
+ values_list = [[k, v] for k, v in values.items()]
62
+
63
+ # We split off potential leading zeroes
64
+ leading_zeroes = []
65
+ for e in values_list:
66
+ if e[1]:
67
+ break
68
+ else:
69
+ leading_zeroes.append(e)
70
+
71
+ # We split off potential trailing zeroes
72
+ trailing_zeroes = []
73
+ for e in reversed(values_list):
74
+ if e[1]:
75
+ break
76
+ else:
77
+ trailing_zeroes.insert(0, e)
78
+
79
+ # Check whether there are enough elements left between leading
80
+ # zeroes and trailing zeroes. If there are less than 3
81
+ # elements beteen leading and trailing zeroes, we know for
82
+ # sure there are no in between zeroes.
83
+ if len(values_list) - len(leading_zeroes) - len(trailing_zeroes) > 2:
84
+ # Remove leading and trailing zeroes from values_list
85
+ values_list = values_list[
86
+ len(leading_zeroes) : len(values_list) - len(trailing_zeroes)
87
+ ]
88
+
89
+ # In this remaining list we can remove all zero values
90
+ values_list = [e for e in values_list if e[1]]
91
+
92
+ # Now zeroes are removed, we re-attach leading and trailing zeroes
93
+ new_list = leading_zeroes
94
+ new_list.extend(values_list)
95
+ new_list.extend(trailing_zeroes)
96
+
97
+ # We convert the list of pairs back to a dictionary
98
+ values = {e[0]: e[1] for e in new_list}
99
+
100
+ if not values:
101
+ values["second"] = 0
102
+
103
+ string = ""
104
+ for field in values:
105
+ string += separator + str(values[field]) + unit_names[field]
106
+
107
+ string = string[len(separator) :]
108
+ return string