I know, title is hard to understand.
So, i have this class:
class ConfigDict(dict):
def __init__(self, dic):
super(ConfigDict, self).__init__(dic)
def required(self, keys_array, function):
print 'required'
def valid(self, function = None):
print 'valid'
And what i want – i create instance of this class with dict as parameter:
ConfigDict({'a' : 'b'})
It’s working, thats good. But i want pass function as argument in dict from ConfigDict class without importing methods from ConfigDict.
For example, i want do this:
ConfigDict({'a' : ('b', required(['test'], valid))})
I know that required in ConfigDict do nothing now. Expected result is:
>> ConfigDict({'a' : ('b', required(['test'], valid()))})
required called with ['test'], valid for {a : b}
valid called from required with None for {a : b}
So, after creating instance of ConfigDict with {'a' : ('b', required['test'], valid())} dict, i want that this instance in __init__ method make loop in all dict elements, and if founds tuple in value, execute founded nested function in itselfs.
Is here any way to do this without importing all methods from ConfigDict?
EDIT:
As i expected i must better explain what i need.
Ok, we take this fragment:
ConfigDict({'a' : ('b', required(['test'], valid))})
This made us new instance of ConfigDict. These functions in touple is used to validate value, in this case it is 'b'. I made few changes waiting for response, so calling this class is now look that:
cd = ConfigDict()
cd.feed({'a' : 'b'})
I can call functions like that:
cd.feed({'a' : ('b', cg.required(['test']))})
What is work’s very well, but there’s one thing – it not pass to required function the value. ConfigDicts.required should get an array and value, in this case 'b'. I don’t expect to find way to do this myself, except using everywhere lambda’s what i like to avoid.
So, my question been bit changed – is there a any way to get 'b' value from inside required function? I can pass 'b' as argument directly into required, but i expect to have many functions call in tuple, passing value for each make a little mess in code.
Also, anyone please, edit title of my post, i lack of words to describe this problem 🙂
Your English is a bit hard to understand, and your question contains some errors (or just inconsistencies), so I’m not sure what exactly you want, but here’s one thing the may work for you:
(Edit) Try this:
Output: