Package: qtils
OverviewQtils - pronounced as cutieels - is a syntactic sugar library to make sweet Python coding even sweeter.DedicationThis library is dedicated toPá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.FeaturesConvenient collectionsqdict,qlistandQEnumSelf-formatting object inPrettyObjectTwo-way formatter/parser for file sizes, for example ‘5.4 GB’, inDataSizeWeak reference property decoratorweakpropertyCached property decoratorcachedpropertyClass logger decoratorloggedCommon string transformations inqtils.string_utilsResourcesSources are available onGitHubInstaller is available onPyPIUsage guide and more examples are available in thetutorialsDocumentation isavailable online on ReadTheDocsMigrating fromsutils? See thesutils migration guide here.Contributions are always welcome. Please see theDeveloper’s guideon getting started.Getting StartedInstallationInstalling the latest release from PyPI usingpip:$pipinstallqtilsInstalling the latest release from PyPI and saving it torequirements.txtusingpip:$pipinstall-srequirements.txtqtilsInstalling the latest pre-release from GitHub:$pipinstall-ehttps://github.com/ultralightweight/qtils.git#masterExamplesAttribute dictionary>>>fromqtilsimport*>>>d=qdict(hello="world")>>>d.hello'world'>>>d.answer=42>>>d['answer']42See more examples in theqdict tutorial, see the API referencehere.Objects with self-formatting capability>>>classMyObject(PrettyObject):...__pretty_fields__=[...'hello',...'answer',...]...def__init__(self,hello,answer):...self.hello=hello...self.answer=answer>>>obj=MyObject('world',42)>>>print(obj)<__main__.MyObjectobjectat...hello='world',answer=42>See more examples in thePrettyObject tutorial, see the API referencehereCached property>>>classDeepThought(object):...@cachedproperty...defanswer_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...return42...>>>deep_thougth=DeepThought()>>>deep_thougth.answer_to_life_the_universe_and_everything# first call, getter is calledDeepThoughtisthinking42>>>deep_thougth.answer_to_life_the_universe_and_everything# second call, getter is not called42>>>deldeep_thougth.answer_to_life_the_universe_and_everything# removing cached value>>>deep_thougth.answer_to_life_the_universe_and_everything# getter is called againDeepThoughtisthinking42See more examples in thecachedproperty tutorial, see the API referencehere.Weak reference property>>>fromqtilsimportweakproperty>>>classFoo(object):...@weakproperty...defbar(self,value):pass>>># The code above is the functional equivalent of writing:>>>importweakref>>>classFoo(object):...@property...defbar(self,value):...returnself._bar()ifself._barisnotNoneelseNone...@bar.setter...defbar(self,value):...ifvalueisnotNone:...value=weakref.ref(value)...self._bar=value>>>See more examples in theweakproperty tutorial, see the API referencehere.Formatting and parsing file sizes>>>print(DataSize(123000))123k>>>DataSize('1.45 megabytes')1450000>>>DataSize('1T').format(unit="k",number_format="{:,.0f}{}")'1,000,000,000 k'See more examples in theformatting module tutorial, see the API referencehere.Dynamic module exports>>>fromqtilsimportqlist>>>__all__=qlist()>>>@__all__.register...classFoo(object):...passSee more examples in theqlist tutorial, see the API referencehere.Adding a class-private logger>>>@logged...classLoggedFoo():...def__init__(self):...self.__logger.info("Hello World from Foo!")...See more examples in thelogging module tutorial, see the API referencehere.ContributingPull requests are always welcome! Please see theDeveloper’s Guideon getting started with qtils development.LicenceThis library is available underGNU Lesser General Public Licence v3.