Tools for routine tasks
Project description
Python utils and configuration
Configuration
Constants and variables
export CONFIG='/path/to/base_config.yaml;/path/to/config.toml'
from ttutils import Config
CFG = Config()
CFG.PUBLIC_URL # get from config files
CFG.ENV.CONFIG # get from os env
CFG.SECRET.KEY # get from os env and clean
Logging configuration
export CONFIG='/path/to/base_log_config.yaml;/path/to/logging.toml'
from ttutils import LoggingConfig
CFG = LoggingConfig({
'loggers': {
'aiohttp.access': { # local overriding
'level': 'ERROR',
}
}
})
Safe type convertors
from ttutils import try_int, as_bool, to_string, safe_text, text_crop, int_list, int_set
try_int('123') == 123
try_int('asd') is None
as_bool('t') is True
as_bool(1) is True
as_bool('false') is False
to_string(AClass) == '<AClass>'
to_string('text') == 'text'
to_string(b'text') == 'text'
to_bytes('text') == b'text'
to_bytes(b'text') == b'text'
to_bytes(1234567890) == b'I\x96\x02\xd2'
safe_text('<b>text</b>') == '<b>text</b>'
safe_text('text') == 'text'
text_crop('text', 5) == 'text'
text_crop('sometext', 6) == 'some …'
int_list(['1', '2', 'a', 'b', None]) == [1, 2]
int_set(['1', '2', 'a', 'b', None]) == {1, 2}
Compress
Integer, dict integers, list integers compression/decompression functions
from ttutils import compress
compress.encode(11232423) # 'GSiD'
compress.decode('GSi') # 175506
compress.encode_list([12312, 34535, 12323]) # '30o-8rD-30z'
compress.decode_list('30o-8rD-30z--30C') # [12312, 34535, 12323, 12324, 12325, 12326]
compress.encode_dict({12: [234, 453], 789: [12, 98, 99, 100, 101]}) # 'c-3G-75/cl-c-1y--1B'
compress.decode_dict('c-3G-75/cl-c-1y--1B') # {12: [234, 453], 789: [12, 98, 99, 100, 101]}
DateTime
Datetime parse and serialize utils
from ttutils import (utcnow, utcnow_ms, utcnow_sec, parsedt, parsedt_ms,
parsedt_sec, try_parsedt, isoformat, safe_isoformat)
utcnow() # datetime(2022, 2, 22, 14, 28, 10, 158164, tzinfo=datetime.timezone.utc)
utcnow_ms() # datetime(2022, 2, 22, 14, 28, 20, 824000, tzinfo=datetime.timezone.utc)
utcnow_sec() # datetime(2022, 2, 22, 14, 28, 24, tzinfo=datetime.timezone.utc)
parsedt('2022-02-22T11:22:33.123456Z') # datetime(2022, 2, 22, 11, 22, 33, 123456, tzinfo=datetime.timezone.utc)
parsedt_ms('2022-02-22T11:22:33.123456Z') # datetime(2022, 2, 22, 11, 22, 33, 123000, tzinfo=datetime.timezone.utc)
parsedt_sec('2022-02-22T11:22:33.123456Z') # datetime(2022, 2, 22, 11, 22, 33, tzinfo=datetime.timezone.utc)
try_parsedt('2022-02-22T11:22:33.123456Z') # datetime(2022, 2, 22, 11, 22, 33, 123456, tzinfo=datetime.timezone.utc)
try_parsedt(None) # None
isoformat(utcnow()) # '2022-02-22T14:33:51.381164Z'
try_isoformat(utcnow()) # '2022-02-22T14:33:51.381164Z'
try_isoformat(None) # None
Concurrency
Tools for asyncio
To limit the parallelism of an asynchronous function, install a decorator
from ttutils import concurrency_limit
@concurrency_limit(2)
async def my_task(...) -> None:
... # there are only 2 concurrent executions
# the queue length will be recorded in the log when the function is overloaded
log = logging.getLogger('concurrency_logger')
@concurrency_limit(2, logger=log)
async def my_task(...) -> None:
... # there are only 2 concurrent executions
Stats collector
Collector предназначен для:
- сбора данных о длительности выполнения функций, методов и блоков кода,
- формированни периодических отчетов о статистике времени выполнения,
- ведении лога медленных запросов.
from ttutils.stats import Collector
stats = Collector()
@stats.atimer('k1')
async def func():
...
class A:
@stats.atimer('k2')
async def func(self):
...
with stats.timer('k3'):
sync_func()
await async_func()
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ttutils-0.10.tar.gz
(21.8 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
ttutils-0.10-py3-none-any.whl
(15.9 kB
view details)
File details
Details for the file ttutils-0.10.tar.gz.
File metadata
- Download URL: ttutils-0.10.tar.gz
- Upload date:
- Size: 21.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
72f0f89cb51134c2425004d1591b45f49fd41e30864e0fb99392291d96d72f2e
|
|
| MD5 |
4f177fea142df2eb7f864b8e1bb576ff
|
|
| BLAKE2b-256 |
008e75d140c59067de3e9a76c5878eab0a59cefaef3ac77e2db0c7e8bc226794
|
File details
Details for the file ttutils-0.10-py3-none-any.whl.
File metadata
- Download URL: ttutils-0.10-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ff2efdfdb584f859ad556b6424f0bb877d876c4bb8e35651605d8b5e15a37c8
|
|
| MD5 |
067a1626e537804e5b0d37f2f280746a
|
|
| BLAKE2b-256 |
c7df197ed18c5daef52cb2512eac3ee57af82a4d6d57562a473a3ec003183d61
|