[Tutor] Creating an object which is an extension of a dictionary

Ian Winstanley ianwinstanley8 at gmail.com
Sat Mar 26 07:35:13 EDT 2016


I'm attempting to create an object which can have elements which can be
referenced like a dictionary but can also have normal methods and
attributes like a conventional object but can also be referenced with key
items using d[key]

The dictionary I have is below:

UPPERCASE = "ucase"
LOWERCASE = "lcase"

DEFAULT_TYPING_SETTINGS = {"allow letters" : True,
                           "default case" : LOWERCASE,
                           "allow uppercase" : True,
                           "allow lowercase" : True,
                           "allow shift case change" : True,
                           "allow caps lock uppercase" : True,
                           "allow numbers" : True,
                           "allow standard numbers" : True,
                           "allow keypad numbers" : True,
                           "allow spaces" : True,
                           "space keys" : K_SPACE,
                           "allow backspace" : True,
                           "backspace keys" : K_BACKSPACE,
                           "allow newline" : False,
                           "newline keys" : K_RETURN,
                           "allow tab" : False,
                           "tab keys" : K_TAB,
                           "tab stop" : 4,
                           "allow punctuation" : True,
                           "allow standard punctuation" : True,
                           "allow shift punctuation" : True,
                           "allow number key punctuation" : True,
                           "allow keypad punctuation" : True,
                           "punctuation allowed" : None,
                           "punctuation disallowed" : None,
                           "show cursor" : False,
                           "allow navigation" : False,
                           "allow horizontal navigation" : False,
                           "allow vertical navigation" : False,
                           "allow arrow key navigation" : False,
                           "allow keypad navigation" : False}

I want to set an attribute for an object's typing settings which I can
change any of these individually but I also want to be able to reference
methods which can be called to perform operations such as allow all letters.

Ideally I want a typing settings object where I can get and set attributes
using for example:
typing_settings["allow uppercase"] = True
but I also want to create methods I can all using dot notation so I need
something which is a cross between a dictionary and a conventional object.

It isn't absolutely necessary but I would also like to validate when
setting the dictionary attributes such as converting necessary values to
boolean when setting and ensure the default case attribute is either UPPER
or LOWER

Thanks for any suggestions

Ian


More information about the Tutor mailing list