@@ -58,6 +58,31 @@ format.
5858Examples
5959------------------------------------------------------------------------------
6060
61+ Automatically converting a generator to a list, dict or other collections
62+ using a decorator:
63+
64+ .. code-block :: pycon
65+
66+ >>> @decorators.listify()
67+ ... def generate_list():
68+ ... yield 1
69+ ... yield 2
70+ ... yield 3
71+ ...
72+ >>> generate_list()
73+ [1, 2, 3]
74+
75+ >>> @listify(collection=dict)
76+ ... def dict_generator():
77+ ... yield 'a', 1
78+ ... yield 'b', 2
79+
80+ >>> dict_generator()
81+ {'a': 1, 'b': 2}
82+
83+ Retrying until timeout
84+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
85+
6186To easily retry a block of code with a configurable timeout, you can use the
6287`time.timeout_generator `:
6388
@@ -69,6 +94,9 @@ To easily retry a block of code with a configurable timeout, you can use the
6994 ... except Exception as e:
7095 ... # Handle the exception
7196
97+ Formatting of timestamps, dates and times
98+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99+
72100Easy formatting of timestamps and calculating the time since:
73101
74102.. code-block :: pycon
@@ -98,12 +126,15 @@ Easy formatting of timestamps and calculating the time since:
98126 '1 minute ago'
99127
100128 Converting your test from camel-case to underscores:
129+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
101130
102131.. code-block :: pycon
103132
104133 >>> camel_to_underscore('SpamEggsAndBacon')
105134 'spam_eggs_and_bacon'
106135
136+ Attribute setting decorator. Very useful for the Django admin
137+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107138A convenient decorator to set function attributes using a decorator:
108139
109140.. code-block :: pycon
@@ -119,7 +150,11 @@ A convenient decorator to set function attributes using a decorator:
119150
120151 >>> upper_case_name.short_description = 'Name'
121152
122- Or to scale numbers:
153+ This can be very useful for the Django admin as it allows you to have all
154+ metadata in one place.
155+
156+ Scaling numbers between ranges
157+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
123158
124159.. code-block :: pycon
125160
@@ -130,7 +165,8 @@ Or to scale numbers:
130165 >>> remap(decimal.Decimal('250.0'), 0.0, 1000.0, 0.0, 100.0)
131166 Decimal('25.0')
132167
133- To get the screen/window/terminal size in characters:
168+ Get the screen/window/terminal size in characters:
169+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
134170
135171.. code-block :: pycon
136172
@@ -140,7 +176,8 @@ To get the screen/window/terminal size in characters:
140176 That method supports IPython and Jupyter as well as regular shells, using
141177`blessings ` and other modules depending on what is available.
142178
143- To extract a number from nearly every string:
179+ Extracting numbers from nearly every string:
180+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
144181
145182.. code-block :: pycon
146183
@@ -151,6 +188,9 @@ To extract a number from nearly every string:
151188 >>> number = converters.to_int('spam', default=1)
152189 1
153190
191+ Doing a global import of all the modules in a package programmatically:
192+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
193+
154194To do a global import programmatically you can use the `import_global `
155195function. This effectively emulates a `from ... import * `
156196
@@ -161,6 +201,9 @@ function. This effectively emulates a `from ... import *`
161201 # The following is the equivalent of `from some_module import *`
162202 import_global(' some_module' )
163203
204+ Automatically named logger for classes:
205+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
206+
164207Or add a correclty named logger to your classes which can be easily accessed:
165208
166209.. code-block :: python
@@ -183,3 +226,23 @@ Or add a correclty named logger to your classes which can be easily accessed:
183226 import logging
184227 my_class.log(logging.ERROR , ' log' )
185228
229+
230+ Convenient type aliases and some commonly used types:
231+
232+ .. code-block :: python
233+
234+ # For type hinting scopes such as locals/globals/vars
235+ Scope = Dict[str , Any]
236+ OptionalScope = O[Scope]
237+
238+ # Note that Number is only useful for extra clarity since float
239+ # will work for both int and float in practice.
240+ Number = U[int , float ]
241+ DecimalNumber = U[Number, decimal.Decimal]
242+
243+ # To accept an exception or list of exceptions
244+ ExceptionType = Type[Exception ]
245+ ExceptionsType = U[Tuple[ExceptionType, ... ], ExceptionType]
246+
247+ # Matching string/bytes types:
248+ StringTypes = U[str , bytes ]
0 commit comments