Skip to main content

Lazy attributes for Python objects

Project description

Package Contents

@lazy

A decorator to create lazy attributes.

Overview

Lazy attributes are computed attributes that are evaluated only once, the first time they are used. Subsequent uses return the results of the first call. They come handy when code should run

  • late, i.e. just before it is needed, and

  • once, i.e. not twice, in the lifetime of an object.

You can think of it as deferred initialization. The possibilities are endless.

Typing

The decorator is fully typed. Type checkers can infer the type of a lazy attribute from the return value of the decorated method.

Examples

The class below creates its store resource lazily:

from lazy import lazy

class FileUploadTmpStore(object):

    @lazy
    def store(self):
        location = settings.get('fs.filestore')
        return FileSystemStore(location)

    def put(self, uid, fp):
        self.store.put(uid, fp)
        fp.seek(0)

    def get(self, uid, default=None):
        return self.store.get(uid, default)

Another application area is caching:

class PersonView(View):

    @lazy
    def person_id(self):
        return self.request.get('person_id', -1)

    @lazy
    def person_data(self):
        return self.session.query(Person).get(self.person_id)

Documentation

For further details please refer to the API Documentation.

Changelog

1.5 - 2022-09-18

  • Allow type checkers to infer the type of a lazy attribute. Thanks to Elias Keis and Palpatineli for their contributions. [elKei24] [Palpatineli]

  • Add Python 3.8-3.11 to tox.ini. Remove old Python versions. [stefan]

  • Replace deprecated python setup.py test in tox.ini. [stefan]

  • Remove deprecated test_suite from setup.py. [stefan]

  • Move metadata to setup.cfg and add a pyproject.toml file. [stefan]

  • Include tests in sdist but not in wheel. [stefan]

1.4 - 2019-01-28

  • Add MANIFEST.in. [stefan]

  • Release as universal wheel. [stefan]

1.3 - 2017-02-05

  • Support Python 2.6-3.6 without 2to3. [stefan]

  • Add a LICENSE file. [stefan]

1.2 - 2014-04-19

  • Remove setuptools from install_requires because it isn’t. [stefan]

1.1 - 2012-10-12

  • Use functools.wraps() properly; the list of attributes changes with every version of Python 3. [stefan]

1.0 - 2011-03-24

  • Initial release.

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

lazy-1.5.tar.gz (9.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lazy-1.5-py2.py3-none-any.whl (5.0 kB view details)

Uploaded Python 2Python 3

File details

Details for the file lazy-1.5.tar.gz.

File metadata

  • Download URL: lazy-1.5.tar.gz
  • Upload date:
  • Size: 9.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.2

File hashes

Hashes for lazy-1.5.tar.gz
Algorithm Hash digest
SHA256 cb3d8612aa895a48afe8f08860573ba8ef5ee4fdbe1b3cd606c5f50a16152186
MD5 1f41272aa28fd79e2c9060ce1fc73967
BLAKE2b-256 2900ba9688438904f0cd446e1986deb826610385c042776dd939ecb2b360b3bd

See more details on using hashes here.

File details

Details for the file lazy-1.5-py2.py3-none-any.whl.

File metadata

  • Download URL: lazy-1.5-py2.py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.2

File hashes

Hashes for lazy-1.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7b8bc7e0136f96722bf9e41eec7472495059526bfa1195db61d43e8da6bbe604
MD5 ff63b5562db7aaecdffa47c3eefbcbd3
BLAKE2b-256 83948797264d5697a1643b6f33537d88fee98bfd016f40daa1cc3a59f2ed3468

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page