brocksam / pyproprop

Write classes with lots of similar simple defensive properties without the boilerplate
MIT License
3 stars 1 forks source link

A utility designed to work with processed properties with options. #40

Closed brocksam closed 4 years ago

brocksam commented 4 years ago

Processed properties allow the specification of a group of options that a user can then choose from. If an option from this group is chosen then an error is raised. Processed property options also allow for a default option to be specified as well as for options to be specified as unsupported. Additionally dispatchers can be built using these options so that a specific function or class handle can be linked to the option identifiers. This module implements a framework to provide all of these things in a clean and easy-to-use way that is designed with use alongside processed properties in mind.

To do this currently is something like:

from pyproprop import processed_property

KEYWORD_IDENTIFIER_1 = "keyword_1"
KEYWORD_IDENTIFIER_2 = "keyword_2"
KEYWORD_IDENTIFIER_3 = "keyword_3"
OPTIONS = (KEYWORD_IDENTIFIER_1, KEYWORD_IDENTIFIER_2, KEYWORD_IDENTIFIER_3)
DEFAULT_OPTION = KEYWORD_IDENTIFIER_1
UNSUPPORTED_OPTIONS = KEYWORD_IDENTIFIER_3

class MyClassWithOptionsProperty:

    my_property = processed_property(
        "my_property",
        type=str,
        options=OPTIONS,
        default=KEYWORD_IDENTIFIER_1
        unsupported_options=KEYWORD_IDENTIFIER_3,
    )

    def __init__(self, my_property):
        self.my_property = my_property

    def my_dispatcher(self):
        return {
            KEYWORD_IDENTIFIER_1: SomeClass1,
            KEYWORD_IDENTIFIER_2: SomeClass2,
            KEYWORD_IDENTIFIER_3: SomeClass3,
        }

Ideally this would be done something like:

from pyproprop import Options, processed_property

my_options = Options(
    ["keyword_1", "keyword_2", "keyword_3"],
    unsupported=2,
    callables=[SomeClass1, SomeClass2, SomeClass3]
)

class MyClassWithOptionsProperty:

    my_property = processed_property(
        "my_property",
        type=str,
        options=OPTIONS,
    )

    def __init__(self, my_property):
        self.my_property = my_property
brocksam commented 4 years ago

Addressed in 271aa6c46ec15ca9510145029744a0a65f601ffe.