Giter Site home page Giter Site logo

ultralightweight / qtils Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 0.0 122 KB

Qtils is a syntactic sugar library to make sweet Python coding even sweeter.

Home Page: http://www.ultralightweight.io/

License: GNU Lesser General Public License v3.0

Makefile 12.75% Python 87.25%
python utility-library sweet lightweight ultralightweight

qtils's Introduction

https://qtils.readthedocs.io/en/latest/_images/qtils-logo.png


GitHub tag (latest SemVer) Travis CI build status Code Coverage Documentation Status PyPI GitHub issues

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.

Features

Resources

Getting Started

Installation

Installing the latest release from PyPI using pip:

$ pip install qtils

Installing the latest release from PyPI and saving it to requirements.txt using pip:

$ pip install -s requirements.txt qtils

Installing the latest pre-release from GitHub:

$ pip install -e https://github.com/ultralightweight/qtils.git#master

Examples

Attribute dictionary

>>> from qtils import *

>>> d = qdict(hello = "world")
>>> d.hello
'world'
>>> d.answer = 42
>>> d['answer']
42

See more examples in the qdict tutorial, see the API reference here.

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>

See more examples in the PrettyObject tutorial, see the API reference here

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

See more examples in the cachedproperty tutorial, see the API reference here.

Weak reference property

>>> from qtils import weakproperty

>>> class Foo(object):
...     @weakproperty
...     def bar(self, value): pass
>>>

# The code above is the functional equivalent of writing:

>>> import weakref
>>> class Foo(object):
...     @property
...     def bar(self, value):
...         return self._bar() if self._bar is not None else None
...     @bar.setter
...     def bar(self, value):
...         if value is not None:
...             value = weakref.ref(value)
...         self._bar = value
>>>

See more examples in the weakproperty tutorial, see the API reference here.

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'

See more examples in the formatting module tutorial, see the API reference here.

Dynamic module exports

>>> from qtils import qlist

>>> __all__ = qlist()

>>> @__all__.register
... class Foo(object):
...     pass

See more examples in the qlist tutorial, see the API reference here.

Adding a class-private logger

>>> @logged
... class LoggedFoo():
...     def __init__(self):
...         self.__logger.info("Hello World from Foo!")
...

See more examples in the logging module tutorial, see the API reference here.

Contributing

Pull requests are always welcome! Please see the Developer's Guide on getting started with qtils development.

Licence

This library is available under GNU Lesser General Public Licence v3.

qtils's People

Contributors

neonihil avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.