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.3rc2.tar.gz (11.0 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.3rc2-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for class-pool-0.3rc2.tar.gz
Algorithm Hash digest
SHA256 6f278121171d42e7ac0ed910d3b698468ac69c8633a83d8d2f4973cd48494e7f
MD5 14d53b5faf328f197a18c833f0d8552d
BLAKE2b-256 e9ff19397239d2205914fe79bd8df16eb0d7fa05f56155aa3c7e93bc2e8d51c8

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for class_pool-0.3rc2-py3-none-any.whl
Algorithm Hash digest
SHA256 03a84f8b9c920b69cfcdbf6d171d939741fd6d66e2cee46ae1461ab095650240
MD5 f2f23c1dd41903c08f988d4c3facfc84
BLAKE2b-256 0248ccc0e83e51f852e8d47ba91255d1a90ab5503b7035c200152b10789860ee

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