Qtils - pronounces as `cuteels` - is a syntactic sugar library to make sweet Python coding even sweeter.
Project description
Overview
Qtils - pronounced as cutieels - is a syntactic sugar library to make sweet Python coding even sweeter.
Dedication
This library is dedicated to Pál Hubai, Surfy, my programming Master who spent countless hours answering my questions, providing code examples, and guiding me towards the right approach when I was learning programming as a child.
Documentation
Documentation is available at https://qtils.readthedocs.io/en/latest/
See examples and usage in examples and tutorials
Features
Self-formatting object in PrettyObject
Two-way formatter/parser for file sizes, for example ‘5.4 GB’, in DataSize
Weak reference property decorator weakproperty
Cached property decorator cachedproperty
Class logger decorator logged
Common string transformations in qtils.string_utils
Installation
Qtils is available in PyPI:
$ pip install qtils
Examples
Attribute dictionary
>>> from qtils import *
>>> d = qdict(hello = "world")
>>> d.hello
'world'
>>> d.answer = 42
>>> d['answer']
42
Objects with self-formatting capability
>>> class MyObject(PrettyObject):
... __pretty_fields__ = [
... 'hello',
... 'answer',
... ]
... def __init__(self, hello, answer):
... self.hello = hello
... self.answer = answer
>>> obj = MyObject('world', 42)
>>> print(obj)
<__main__.MyObject object at ... hello='world', answer=42>
Cached property
>>> class DeepThought(object):
... @cachedproperty
... def answer_to_life_the_universe_and_everything(self):
... print('Deep Thought is thinking')
... # Deep Thought: Spends a period of 7.5 million years
... # calculating the answer
... return 42
...
>>> deep_thougth = DeepThought()
>>> deep_thougth.answer_to_life_the_universe_and_everything # first call, getter is called
Deep Thought is thinking
42
>>> deep_thougth.answer_to_life_the_universe_and_everything # second call, getter is not called
42
>>> del deep_thougth.answer_to_life_the_universe_and_everything # removing cached value
>>> deep_thougth.answer_to_life_the_universe_and_everything # getter is called again
Deep Thought is thinking
42
Formatting and parsing file sizes
>>> print(DataSize(123000))
123 k
>>> DataSize('1.45 megabytes')
1450000
>>> DataSize('1T').format(unit="k", number_format="{:,.0f} {}")
'1,000,000,000 k'
Adding a class-private logger
>>> @logged
... class LoggedFoo():
... def __init__(self):
... self.__logger.info("Hello World from Foo!")
...
See more examples and usage in examples and tutorials.
Contribution
Pull requests are always welcome.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.