Skip to main content

Assorted decorator functions.

Function cached(*da, **dkw)

Decorator to cache the result of a method and keep a revision counter for changes. The revision supports the @revised decorator.

This decorator may be used in 2 modes. Directly:

@cached
def method(self, ...)

or indirectly:

@cached(poll_delay=0.25)
def method(self, ...)

Optional keyword arguments:

  • attr_name: the basis name for the supporting attributes. Default: the name of the method.
  • poll_delay: minimum time between polls; after the first access, subsequent accesses before the poll_delay has elapsed will return the cached value. Default: None, meaning no poll delay.
  • sig_func: a signature function, which should be significantly cheaper than the method. If the signature is unchanged, the cached value will be returned. The signature function expects the instance (self) as its first parameter. Default: None, meaning no signature function; the first computed value will be kept and never updated.
  • unset_value: the value to return before the method has been called successfully. Default: None.

If the method raises an exception, this will be logged and the method will return the previously cached value.

An example use of this decorator might be to keep a "live" configuration data structure, parsed from a configuration file which might be modified after the program starts. One might provide a signature function which called os.stat() on the file to check for changes before invoking a full read and parse of the file.

Function decorator(deco)

Wrapper for decorator functions to support optional keyword arguments.

Examples:

@decorator
def dec(func, **dkw):
  ...
@dec
def func1(...):
  ...
@dec(foo='bah')
def func2(...):
  ...

Function strable(*da, **dkw)

Decorator for functions which may accept a str instead of their core type.

Parameters:

  • func: the function to decorate
  • open_func: the "open" factory to produce the core type form the string if a string is provided; the default is the builtin "open" function

The usual (and default) example is a function to process an open file, designed to be handed a file object but which may be called with a filename. If the first argument is a str then that file is opened and the function called with the open file.

Examples:

@strable
def count_lines(f):
  return len(line for line in f)

class Recording:
  "Class representing a video recording."
  ...
@strable
def process_video(r, open_func=Recording):
  ... do stuff with `r` as a Recording instance ...

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cs.deco-20190307.2.tar.gz (4.5 kB view details)

Uploaded Source

File details

Details for the file cs.deco-20190307.2.tar.gz.

File metadata

  • Download URL: cs.deco-20190307.2.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.8

File hashes

Hashes for cs.deco-20190307.2.tar.gz
Algorithm Hash digest
SHA256 afac1ef14b7de5c48b80fea313d09f7972556825512096c2833e1a6e3a4d94f9
MD5 6b890b0a13de726430179f1cd102abfc
BLAKE2b-256 bb90e1c50b8cf3e71a89d1087a897e63f7e75c96fa6a1166c3e7485fa2736b2e

See more details on using hashes here.

Release history Release notifications | RSS feed

20260719

2 files

20260622

2 files

20260525

2 files

20251230

2 files

20250914

2 files

20250724

2 files

20250601

2 files

20250531

2 files

20250513

2 files

20250428

2 files

20250306

2 files

20250103

2 files

20241206

2 files

20241109

2 files

20241007

2 files

20241003

2 files

20240709

2 files

20240630

2 files

20240412

2 files

20240326

2 files

20240316

2 files

20240314.1

2 files

20240314

2 files

20240303

2 files

20240211

2 files

20231129

2 files

20230331

2 files

20230212

2 files

20230210

2 files

20221214

2 files

20221207

2 files

20221106.1

2 files

20221106

2 files

20220918.1

2 files

20220918

2 files

20220905

2 files

20220805

2 files

20220327

2 files

20220311

1 file

20220227

1 file

20210823

1 file

20210123

1 file

20201202

1 file

20201025

1 file

20201020

1 file

20200725

1 file

20200517.2

1 file

20200517.1

1 file

20200517

1 file

20200417

1 file

20200318.1

1 file

20200318

1 file

20191012

1 file

20191006

1 file

20191004

1 file

20190905

1 file

20190830.2

1 file

20190830.1

1 file

20190830

1 file

20190729

1 file

20190601.1

1 file

20190601

1 file

20190526

1 file

20190512

1 file

20190404

1 file

20190403

1 file

20190322.1

1 file

20190322

1 file

20190309

1 file

This release

20190307.2 This release

1 file

20190307.1

1 file

20190307

1 file

20190220

1 file

20181227

1 file

20171231

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page