Skip to main content

Package for registering types and creating systems based on config files

Project description

Python Type Registry

Build Status

The type-registry library provides the ability to easily register factory methods (or classes) that can be used in a configuration file to quickly construct complex objects (or models).

Example

Lets demonstrate how to easily use this library. There are a functions and decorators, but the most important is the register decorator. By registering a type, we are telling the system that when a configuration section has a special key __factory__ to construct that section into an object.

We start by registering our PytorchExample and give it a more usable name of pytorch-model.

import torch.nn as nn

from type_registry import register

@register('pytorch-model', input_size=1024, output_size=8)
def PytorchExample(nn.Module):
    def __init__(self, input_size, hidden_size, output_size):
      ...

Now that we have the type registered with the system, in the configuration we can supplied the details around its construction (in this case we are just providing the value of the hidden_size)

model:
  __factory__: pytorch-model
  hidden_size: ...

Constructing from config

Once we have our types registered and a config defined, we need to correctly load the configuration so that it will construct the types. This is accomplished using the load_yaml and load_yaml_str functions.

from type_registery import load_yaml_str

yaml_str = get_config()
result = load_yaml_str(yaml_str)

You can also have environment variables (or custom variables) automatically expanded in the same configuration file.

model:
  __factory__: simple-model
  use_aws: $USE_AWS

Viewing the Registery

To make working with the library a little easier, there is a function that can be called which will print out the details of the registery (at the time the call takes place).

from type_registery import print_registry

if __name__ == '__main__':
  print_registery(color=True)

Setting the color value to True will have the system print out the results using shell coloring (termcolor library). You can also have the output of the print_registry call target a file instead of stdout.

from type_registery import print_registry

if __name__ == '__main__':
  with open('registry.txt', 'w') as f:
    print_registry(file=f)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

type_registry-1.2-py3-none-any.whl (14.2 kB view details)

Uploaded Python 3

File details

Details for the file type_registry-1.2-py3-none-any.whl.

File metadata

  • Download URL: type_registry-1.2-py3-none-any.whl
  • Upload date:
  • Size: 14.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.9

File hashes

Hashes for type_registry-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 77b74c091d93c6e657e7a829d51578fad45cc508f02367de7ab75e204bf0f0f5
MD5 99a7943d26e4fba2281fa0ca41afd223
BLAKE2b-256 766f93564b85c2ec4d794dabcdb0630fe1965d6c4afc372f4b41a689d9af9a80

See more details on using hashes here.

Supported by

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