A mechanism for subclass tree indexation.
Project description
INDEXED CLASS
Maintainer: aachn3 n45t31@protonmail.com
Site: https://gitlab.com/pyutil/indexed_class
Version: 1.0.1
About
This package provides some metaclasses/class hierarchy roots capable of storing and retrieving subclasses using arbitrary keys and registry types.
Table of Contents
Project Structure
-NoRootClassError(TypeError)
-NoMatchingSubclassError(KeyError)
-SubclassValidationError(ValueError)
-IndexedClassMeta(type)
-keys
-operator[]
-IndexedClass(meta=IndexedClassMeta)
-AbstractIndexedClassMeta(IndexedClassMeta, ABCMeta)
-AbstractIndexedClass(meta=AbstractIndexedClassMeta)
Usage
(Abstract)IndexedClassMeta exposes following parameters:
- key (optional, any type): the key under which the class is to be stored in the registry;
if the
__registry__
attribute is not defined anywhere in parent hierarchy, a NoRootClassError is raised - root (optional, boolean): if set to True, the class defines a
__registry__
attribute as a new instance of the ``@registry_class
parameter - registry_class (optional, type exposing
__getitem__
,__delitem__
and__setitem__
): class used to initialise empty__registry__
in root classes
Methods exposed by classes derived from (Abstract)IndexedClassMeta:
- keys (property): returns a set of all keys defined in
__registry__
; if this attribute is not defined anywhere in parent hierarchy, a NoRootClassError is raised; by default uses.__registry__.keys()
- override if yourregistry_class
does not implementkeys()
method - __getitem__(key): returns a class stored in
__registry__
under@key
; if the__registry__
attribute is not defined anywhere in parent hierarchy, a NoRootClassError is raised; if@key
is not found in__registry__
, a NoMatchingSubclassError is raised - __setitem__(key, value): stores
@value
under@key
in__registry__
; if the__registry__
attribute is not defined anywhere in parent hierarchy, a NoRootClassError is raised; if@value
is not a valid subclass of calling class, a SubclassValidationError is raised - __delitem__(key): deletes
@key
from__registry__
; if the__registry__
attribute is not defined anywhere in parent hierarchy, a NoRootClassError is raised; if@key
is not found in__registry__
, a NoMatchingSubclassError is raised
Code samples
Basic usecase
from indexed_class import AbstractIndexedClass, NoMatchingSubclassError
from abc import abstractmethod
import json
import pickle
class FileParser(AbstractIndexedClass, root=True):
def __init__(self, filename: str):
self.filename = filename
@abstractmethod
def fetch_content(self)->dict:
raise NotImplementedError("abstract method requires override")
class JsonFileParser(FileParser, key="json"):
def fetch_content(self)->dict:
with open(self.filename, "r") as content:
return json.load(content)
class PickleFileParser(FileParser, key="pickle"):
def fetch_content(self)->dict:
with open(self.filename, "rb") as content:
return pickle.load(content)
### MAIN ###
# example settings:
# filename: /some/path/data.json
# filetype: json
with open("settings.json", "r") as file:
settings = json.load(file)
try:
data = FileParser[settings["filetype"]](settings["filename"]).fetch_content()
except NoMatchingSubclassError:
data = None
...
Support
Prerequisites
- python >= 3.8.0
Installation
pip3 install
Testing package functionality
Unit tests
Format: pytest
Integration tests
Format: None
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
indexed_class-1.0.1.tar.gz
(4.7 kB
view hashes)
Built Distribution
Close
Hashes for indexed_class-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c65dc85fd901196261d6d15060fc00b623f9fc9eb1f869448902b15d1992fc28 |
|
MD5 | 574525d26dc037995d4b11c1a7fc2364 |
|
BLAKE2b-256 | 9a863a3fcd727eafb2f373cd4ce5ce8869606775bf5bfd7492a53b1076fd6621 |