Skip to main content

A collection of boilerplate for Python applications.

Project description

Coppyr (Copp-er) is a collecton of useful Python boilerplate that I find myself reusing frequently between projects.

subp

subp.call

Convenience wrapper around subprocess.run that abstracts many of the common options and features (such as subprocess.PIPE passing).

>>> from coppyr import subp
>>> retcode, stdout, stederr = subp.call("lsb_release -a")
>>> print retcode
0
>>> print stdout
['Distributor ID:\tUbuntu', 'Description:\tUbuntu 18.04.3 LTS',
'Release:\t18.04', 'Codename:\tbionic', '']
>>> print stderr
['No LSB modules are available.', '']

Note: STDOUT and STDERR are returned as List objects with each line as a String. This includes empty lines which become empty strings.

singleton

singleton.Singleton

Base object that implements the Singleton pattern pythonically. Future inits of this object will return previously the first created object.

>>> from coppyr.types import Singleton
>>> first = Singleton()
>>> first
<singleton.Singleton object at 0x7fa72df4cd30>
>>> second = Singleton()
>>> second
<singleton.Singleton object at 0x7fa72df4cd30>

This object can be used as a base class or mixin to add Singleton behavior to custom objects.

Warning: When inheriting from Singleton it is neccessary to override the _instance class attribute to ensure that you don’t inadvertantly store your subclass instance in the parent class variable (types.Singleton._instance). For the same reason, you should also override _init as well.

class MySingletonClass(Singleton):
    _instance = None
    _init = False
singleton.Namespace

Simple Singleton object that stores KV pairs.

>>> from coppyr.singleton import Namespace
>>> ns = Namespace()
>>> ns.foo = "bar"
>>> ns.foo
'bar'
>>> ns2 = Namespace()
>>> ns2.foo
'bar'

Returns None if the key is not in the namespace.

>>> ns.baz
>>>

Warning: Just like Singleton, child objects should override the class _instance and _init attributes.

__getattr__(self, attr)

When an attribute does not exist, a Namespace will return None instead of raising an AttributeError.

Context

Context

This is intended as an interpreter local object that can store common state between executing threads/coroutines. It’s a convenient tool to provide access to shared utilities such as logging, environment information, and other shared utilities for an application.

>>> from coppyr import Context
>>> context = Context()
>>> context.action_id
'15_100000'
>>> context.inc_action_id()
>>> context.action_id
'15_100001'

lazyproperty

lazyproperty

This is a decorator that will turn a class method into a property that is evaluated once. This is a useful performance optimization for class elements that require computation but do not change overtime.

>>> from coppyr import lazyproperty
>>> class Foo:
...     def __init__(self):
...         self.a = 5
...         self.b = 6
...
...     @lazyproperty
...     def c(self):
...         return self.a + self.b
...
>>> x = Foo()
>>> x.c
11
>>> x.a = 6
>>> x.c
11  # c remains 11

CoppyrError

CoppyrError

Simple boilerplate for readable, consistent, custom error messages. Adds a dict representation that can be used for easy(ish) conversion to JSON for web use cases.

>>> from coppyr import CoppyrError
>>> class MyError(CoppyrError):
...     description = "Doom 2: Hell on earth."
...
>>> err = MyError()
>>> raise err
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
__main__.MyError: Doom 2: Hell on earth.
>>> err
MyError(message=Doom 2: Hell on earth., payload={})
>>> err.to_dict()
{'error': 'MyError', 'message': 'Doom 2: Hell on earth.', 'payload': {}}

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

coppyr-0.9.3.tar.gz (21.4 kB view details)

Uploaded Source

Built Distribution

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

coppyr-0.9.3-py3-none-any.whl (24.7 kB view details)

Uploaded Python 3

File details

Details for the file coppyr-0.9.3.tar.gz.

File metadata

  • Download URL: coppyr-0.9.3.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for coppyr-0.9.3.tar.gz
Algorithm Hash digest
SHA256 7236f92e810eecf7f150e8f036ae25c07d36947334aef4903484d8b62a9be034
MD5 622ce92458a0aec54bc492e4a5ab8d2b
BLAKE2b-256 1094feb475e6136ed1cc64e564b064da563f2cbfe1e5d687c57a0cedf271aaa7

See more details on using hashes here.

File details

Details for the file coppyr-0.9.3-py3-none-any.whl.

File metadata

  • Download URL: coppyr-0.9.3-py3-none-any.whl
  • Upload date:
  • Size: 24.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for coppyr-0.9.3-py3-none-any.whl
Algorithm Hash digest
SHA256 849becffcf3de07d93e3e536bd1220b679cf4cf3f43bdd77ec320b99e7c2824a
MD5 0d894939efe7c9ca693d27bbd5602233
BLAKE2b-256 564102c6eb7268253ac9b4e67f8c3a3dfbbfa5d4379bdb6e1d8a372e159bbd34

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