|
28 | 28 | Requirements for installing: |
29 | 29 | ------------------------------------------------------------------------------ |
30 | 30 |
|
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. |
32 | 33 |
|
33 | 34 | Installation: |
34 | 35 | ------------------------------------------------------------------------------ |
@@ -57,22 +58,98 @@ format. |
57 | 58 | Examples |
58 | 59 | ------------------------------------------------------------------------------ |
59 | 60 |
|
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`: |
61 | 63 |
|
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: |
63 | 108 |
|
64 | | - from python_utils import converters |
| 109 | +.. code-block:: pycon |
65 | 110 |
|
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() |
68 | 115 |
|
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: |
71 | 144 |
|
72 | | - number = converters.to_int('spam', default=1) |
73 | | - assert number == 1 |
| 145 | +.. code-block:: pycon |
74 | 146 |
|
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 |
76 | 153 |
|
77 | 154 | To do a global import programmatically you can use the `import_global` |
78 | 155 | function. This effectively emulates a `from ... import *` |
|
0 commit comments