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.1.tar.gz (19.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.1-py3-none-any.whl (21.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: coppyr-0.9.1.tar.gz
  • Upload date:
  • Size: 19.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.1.tar.gz
Algorithm Hash digest
SHA256 98e905189cbaa13527a06a9fdd50b56e95f6f95a1ceae5c3caf0078285f686d3
MD5 05f88d3ec62f9eee1e496ad801afdcd0
BLAKE2b-256 baf5b5c62c8035150a589aab04e0f52d9b072a97659f60f3fad826fc39dd66da

See more details on using hashes here.

File details

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

File metadata

  • Download URL: coppyr-0.9.1-py3-none-any.whl
  • Upload date:
  • Size: 21.8 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e7a3b8320a0b56482a5892b3de9cab3e9703ab4d97441fc8c0231688def3323a
MD5 91ce62d852923f82d28d1adb6f0f262f
BLAKE2b-256 9a62446b426ec64d3a1581beec0fb6fad69d8bce75b7d418710292f549bf0545

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