Skip to main content

The DType-Decorate module defines two different decorators at the current state. These decorators can be used to constrain the attributes of the decorated function to specific data types. This can help to keep functions clean especially when they are written for a specific context. This is usually the case for scientific applications, where functionality is often more important than clean code.

The basic structure of this module was heavily inspired / extended on the basis of: https://stackoverflow.com/questions/15299878/how-to-use-python-decorators-to-check-function-arguments

Installation

You can either use pip to install the version from PyPI or git to install the probably more recent version from github.

git clone http://github.com/mmaelicke/dtype-decorate.git
cd dtype-decorate
pip install -r requirements.txt
python setup.py install
pip install dtype-decorate

Usage

There are two decorators so far: accept and enforce. accept will restrict the attribute data types to the the defined ones, while enforce will try to convert the given attribute to a desired data type. Both can also be used together, where accept does only make sense to be used after enforce.

Define a function that does only accept an int and a float.

import ddec
@ddec.accept(a=int, b=float)
def f(a, b):
  pass

You can also specify more than one data type allowed. Any attribute not given in the decorator will just be ignored.

@ddec.accept(a=(int, float))
def f(a, be_any_type)
  pass

f(5, 'mystr')   # will run fine
f('mystr', 5)   # will raise a TypeError

The accept decorator can also handle None type and callables like functions or lambda. These have to be specified as a string.

@ddec.accept(a='None', b=('None', 'callable'))
def f(a, b):
  pass

f(None, None)           # will run fine
f(None, lambda x: x)    # will run fine
f(5, None)              # will raise a TypeError

Metadata

Release files for dtype-decorate 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dtype-decorate 0.1.2
File Size Uploaded Provenance Actions
dtype-decorate-0.1.2.tar.gz 5.7 kB Without provenance Details for dtype-decorate-0.1.2.tar.gz

Release files / dtype-decorate-0.1.2.tar.gz

Download URL dtype-decorate-0.1.2.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
aa93f9213ab2548ea138482b3c48fcda10cd91dd70f1f485d3cdfd2c38907057
BLAKE2b-256 checksum
How to use checksums
f7afa851e97d825299c114039306899b391d5179de70f33c58eed0a2036d126e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for dtype-decorate 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.2 This release

1 release file No provenance

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