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 details)

Uploaded Source

File details

Details for the file indexed_class-1.1.0.tar.gz.

File metadata

  • Download URL: indexed_class-1.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for indexed_class-1.1.0.tar.gz
Algorithm Hash digest
SHA256 5bfdd598d70a420cb79af8e9f027e52c5cf6ba5bd40e0bd49312e65c09f21591
MD5 197f0f1ea0e039d4662c829f321f41e8
BLAKE2b-256 a4da175666637752f2df03c38ddf24e7022a4c61bab08a9e51cca04122890284

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