@@ -83,7 +83,8 @@ import typing
8383
8484
8585class Animal (abc .ABC ):
86- is_wild: typing.Optional[bool ] = None
86+ slug: str
87+ is_wild: bool
8788
8889 def walk (self ):
8990 pass
@@ -93,6 +94,7 @@ class Animal(abc.ABC):
9394animal_registry = registerer.Registerer(
9495 parent_class = Animal,
9596 max_size = 5 , # only 5 items can register
97+ slug_attr = " slug" , # set the slug of item as attribute on it
9698 validators = [
9799 registerer.RegistryValidator(
98100 lambda item : item.is_wild is False , # check passed if returns True
@@ -134,12 +136,13 @@ assert animal_registry._registry_dict == {"Sheep": Sheep, "kitty": Cat}
134136assert animal_registry[" Sheep" ]().walk() == " sheep walks"
135137assert animal_registry[" kitty" ]().walk() == " cat walks"
136138```
137- The ` register ` method will also set an attribute on the registered item as ` registry_slug ` .
139+ The ` register ` method will also set an attribute on the registered item as ` registry_slug ` .
140+ You can change the attribute name when creating the Registerer object.
138141So, in last example we have:
139142
140143``` python
141- assert Cat.registry_slug == " kitty"
142- assert animal_registry[" kitty" ].registry_slug == " kitty"
144+ assert Cat.slug == " kitty"
145+ assert animal_registry[" kitty" ].slug == " kitty"
143146
144147```
145148if you need to add attributes on the registered item on registration (it's optional), you can pass kwargs to the ` register ` method.
@@ -236,7 +239,7 @@ A utility that can be used to create a registry object to register class or func
236239``` python
237240__init__ (
238241 parent_class: Optional[Type[~ T]] = None ,
239- slug_attr = ' registry_slug ' ,
242+ slug_attr: Optional[ str ] = None ,
240243 max_size: Optional[int ] = None ,
241244 validators: Optional[List[registerer.validators.RegistryValidator]] = None
242245)
@@ -247,6 +250,7 @@ __init__(
247250** Args:**
248251
249252 - <b >` parent_class ` </b >: The class of parent. If you set this, the registered class should be subclass of the this, If it's not the register method going to raise RegistrationError. Also by setting this you'll be benefit from type hints in your IDE.
253+ - <b >` slug_attr ` </b >: Pass the attribute name of registered item that you want to set registry slug to or read from registered item.
250254 - <b >` max_size ` </b >: allowed size of registered items. Defaults to None which means there is no limit.
251255 - <b >` validators ` </b >: custom validation for on registering items.
252256
@@ -267,7 +271,7 @@ get actual registered items as list (classes or functions)
267271
268272---
269273
270- <a href =" https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L58 " ><img align =" right " style =" float :right ;" src =" https://img.shields.io/badge/-source-cccccc?style=flat-square " ></a >
274+ <a href =" https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L60 " ><img align =" right " style =" float :right ;" src =" https://img.shields.io/badge/-source-cccccc?style=flat-square " ></a >
271275
272276#### <kbd >method</kbd > ` Registerer.is_registered `
273277
@@ -279,7 +283,7 @@ is the slug registered?
279283
280284---
281285
282- <a href =" https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L94 " ><img align =" right " style =" float :right ;" src =" https://img.shields.io/badge/-source-cccccc?style=flat-square " ></a >
286+ <a href =" https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L96 " ><img align =" right " style =" float :right ;" src =" https://img.shields.io/badge/-source-cccccc?style=flat-square " ></a >
283287
284288#### <kbd >method</kbd > ` Registerer.register `
285289
@@ -335,7 +339,7 @@ assert postgresql_connection.env == "prod"
335339
336340---
337341
338- <a href =" https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L73 " ><img align =" right " style =" float :right ;" src =" https://img.shields.io/badge/-source-cccccc?style=flat-square " ></a >
342+ <a href =" https://github.com/danialkeimasi/python-registerer/tree/main/registerer/registry.py#L75 " ><img align =" right " style =" float :right ;" src =" https://img.shields.io/badge/-source-cccccc?style=flat-square " ></a >
339343
340344#### <kbd >method</kbd > ` Registerer.validate `
341345
0 commit comments