Skip to main content

A mechanism for subclass tree indexation.

Project description

INDEXED CLASS

Maintainer: aachn3 n45t31@protonmail.com

Site: https://gitlab.com/pyutil/indexed_class

Version: 1.1.0

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)
-defaultregistry(dict)
-IndexedClassMeta(type)
 -keys
 -operator[]
-IndexedClass(meta=IndexedClassMeta)
-DefaultIndexedClass(IndexedClass)
-AbstractIndexedClassMeta(IndexedClassMeta, ABCMeta)
-AbstractIndexedClass(meta=AbstractIndexedClassMeta)
-DefaultAbstractIndexedClass(AbstractIndexedClass)

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, defaults to super().__registry_class__, sets __registry_class__

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 your registry_class does not implement keys() 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


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.1.0.tar.gz (5.0 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page