Skip to content

Commit 4197f88

Browse files
committed
Merge branch 'release/2.7.1'
2 parents 8197991 + e6de8e1 commit 4197f88

File tree

3 files changed

+89
-17
lines changed

3 files changed

+89
-17
lines changed

README.rst

Lines changed: 88 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Links
2828
Requirements for installing:
2929
------------------------------------------------------------------------------
3030

31-
- `six` any recent version
31+
For the Python 3+ release (i.e. v3.0.0 or higher) there are no requirements.
32+
For the Python 2 compatible version (v2.x.x) the `six` package is needed.
3233

3334
Installation:
3435
------------------------------------------------------------------------------
@@ -57,22 +58,98 @@ format.
5758
Examples
5859
------------------------------------------------------------------------------
5960

60-
To extract a number from nearly every string:
61+
To easily retry a block of code with a configurable timeout, you can use the
62+
`time.timeout_generator`:
6163

62-
.. code-block:: python
64+
.. code-block:: pycon
65+
66+
>>> for i in time.timeout_generator(10):
67+
... try:
68+
... # Run your code here
69+
... except Exception as e:
70+
... # Handle the exception
71+
72+
Easy formatting of timestamps and calculating the time since:
73+
74+
.. code-block:: pycon
75+
76+
>>> time.format_time('1')
77+
'0:00:01'
78+
>>> time.format_time(1.234)
79+
'0:00:01'
80+
>>> time.format_time(1)
81+
'0:00:01'
82+
>>> time.format_time(datetime.datetime(2000, 1, 2, 3, 4, 5, 6))
83+
'2000-01-02 03:04:05'
84+
>>> time.format_time(datetime.date(2000, 1, 2))
85+
'2000-01-02'
86+
>>> time.format_time(datetime.timedelta(seconds=3661))
87+
'1:01:01'
88+
>>> time.format_time(None)
89+
'--:--:--'
90+
91+
>>> formatters.timesince(now)
92+
'just now'
93+
>>> formatters.timesince(now - datetime.timedelta(seconds=1))
94+
'1 second ago'
95+
>>> formatters.timesince(now - datetime.timedelta(seconds=2))
96+
'2 seconds ago'
97+
>>> formatters.timesince(now - datetime.timedelta(seconds=60))
98+
'1 minute ago'
99+
100+
Converting your test from camel-case to underscores:
101+
102+
.. code-block:: pycon
103+
104+
>>> camel_to_underscore('SpamEggsAndBacon')
105+
'spam_eggs_and_bacon'
106+
107+
A convenient decorator to set function attributes using a decorator:
63108

64-
from python_utils import converters
109+
.. code-block:: pycon
65110
66-
number = converters.to_int('spam15eggs')
67-
assert number == 15
111+
You can use:
112+
>>> @decorators.set_attributes(short_description='Name')
113+
... def upper_case_name(self, obj):
114+
... return ("%s %s" % (obj.first_name, obj.last_name)).upper()
68115
69-
number = converters.to_int('spam')
70-
assert number == 0
116+
Instead of:
117+
>>> def upper_case_name(obj):
118+
... return ("%s %s" % (obj.first_name, obj.last_name)).upper()
119+
120+
>>> upper_case_name.short_description = 'Name'
121+
122+
Or to scale numbers:
123+
124+
.. code-block:: pycon
125+
126+
>>> converters.remap(500, old_min=0, old_max=1000, new_min=0, new_max=100)
127+
50
128+
129+
# Or with decimals:
130+
>>> remap(decimal.Decimal('250.0'), 0.0, 1000.0, 0.0, 100.0)
131+
Decimal('25.0')
132+
133+
To get the screen/window/terminal size in characters:
134+
135+
.. code-block:: pycon
136+
137+
>>> terminal.get_terminal_size()
138+
(80, 24)
139+
140+
That method supports IPython and Jupyter as well as regular shells, using
141+
`blessings` and other modules depending on what is available.
142+
143+
To extract a number from nearly every string:
71144

72-
number = converters.to_int('spam', default=1)
73-
assert number == 1
145+
.. code-block:: pycon
74146
75-
number = converters.to_float('spam1.234')
147+
>>> converters.to_int('spam15eggs')
148+
15
149+
>>> converters.to_int('spam')
150+
0
151+
>>> number = converters.to_int('spam', default=1)
152+
1
76153
77154
To do a global import programmatically you can use the `import_global`
78155
function. This effectively emulates a `from ... import *`

python_utils/__about__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__package_name__ = 'python-utils'
2-
__version__ = '2.7.0'
2+
__version__ = '2.7.1'
33
__author__ = 'Rick van Hattem'
44
__author_email__ = '[email protected]'
55
__description__ = (

python_utils/time.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,6 @@ def timeout_generator(
110110
Generator that walks through the given iterable (a counter by default)
111111
until the timeout is reached with a configurable interval between items
112112
113-
The interval_exponent automatically increases the timeout with each run.
114-
Note that if the interval is less than 1, 1/interval_exponent will be used
115-
so the interval is always growing. To double the interval with each run,
116-
specify 2.
117-
118113
>>> for i in timeout_generator(0.1, 0.06):
119114
... print(i)
120115
0

0 commit comments

Comments
 (0)