Skip to main content

Simple implementation of class pools for Python. Optional support for Django and DRF (if available)

Project description

# class-pool

This tool bases on django's models pool.

*Features*
- thread-safe classes pool
- ad-hoc class registering
- auto class registering from all application
- custom class identifiers in a pool
- filtering classes during registration by base class
- default class for unregistered identifiers
- pool classes choice field for Django models, forms and Django Rest Framework serializers

## Requirements
* django >= 1.7
* Python 3 (probably works as well on 2.7, not tested)

## Usage


**Filtering**

Create your own class from `Pool` class. You can overload `base_class` attribute to allow registration
only subclasses of given class. By default all new-style Python classes can be registered.


**Autodiscovery**

Create your own class from `Pool` class and overload `module_lookup` attribute. When instantiated pool will import
all applications and try to load module which match name in `module_lookup`. If found, all classes will be imported
and registered in the pool (if `base_class` is given limited only to classes which match base).


**Default class**

Create your own class from `Pool` class and overload `default` attribute. When unregistered class is requested then
default will be returned. `default` can be callable and return class on demand, requested ID is passed as an
argument.


**Custom identifiers**

By default all classes in the pool are distinguished by its names. This can be changed by overloading `get_class_id()`
function in `Pool` implementation, which takes as an argument class to register and should return ID as a string.

**Registering classes**

Instantiate `Pool` class (or derivative). If autodiscovery is configured, pool will scan all applications listed
in `INSTALLED_APPS`. To add new class to the registry call `register()` function which takes one argument - class
to register. Note! Your class has to be compatible with identifier getter defined in the pool (by default class
name is used as ID).


**Getting classes**

To get class from the pool call `get()` method, which takes identifier as an argument. If class is not found and
default class is not specified `None` is returned.

`Pool` supports also `__getitem__` call, but in that case `KeyError` exception is thrown if class of given ID
is not registered, __even if default class is specified__.


**Field**

This tool offers simple model, form and django rest framework fields which allow to add a choice field with
dynamically built choices basing on all classes registered in a pool. These fields takes pool class instance
as a `pool` argument in the constructor.

Example:
```
class FooModel(models.Model):
subtype = PoolChoiceField(pool=types_pool)
```

## Roadmap to 1.0
- add tests
- add an ability to pass `pool` argument in fields as a string and then import it lazily (simplify imports)
- add django forms field (currently model field is represented as any other CharField with choices)

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

class-pool-0.4rc2.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

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

class_pool-0.4rc2-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file class-pool-0.4rc2.tar.gz.

File metadata

  • Download URL: class-pool-0.4rc2.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for class-pool-0.4rc2.tar.gz
Algorithm Hash digest
SHA256 34c2c3486a0e48f9da7c39a765f81383d9663c6e7bd5a40f1e0fb707c06f9b8e
MD5 e7bc957ba0083abf4262136ade379323
BLAKE2b-256 1cb40abd758ef3133d8b949d932445c3312c747ad4ba62ecd739928eedc354c9

See more details on using hashes here.

File details

Details for the file class_pool-0.4rc2-py3-none-any.whl.

File metadata

File hashes

Hashes for class_pool-0.4rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 a180ded75e4ec59994a8a91c286e9e92611a837fd52f6e099cba2d3062c9c9f1
MD5 5d78ea7b00805b3b72a0dc53e4bdf02a
BLAKE2b-256 6f8866542221f24b1343b9d4e7250a083a8ad2a78777570f9cc7021b4c8dd2fc

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