Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5843477
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:09:36+00:00 2026-05-22T12:09:36+00:00

I know, title is hard to understand. So, i have this class: class ConfigDict(dict):

  • 0

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 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:09:37+00:00Added an answer on May 22, 2026 at 12:09 pm

    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:

    class CallWrapper(object):
        def __init__(self, fn):
            self.fn = fn
    
        def __call__(self, *args, **kwargs):
            self.args   = args
            self.kwargs = kwargs
            return self
    
        def invoke_with_value(self, value):
            return self.fn(value, *self.args, **self.kwargs)
    
    class ConfigDict(dict):
        def __init__(self, ldic):
            myvars = globals()
            for name in dir(self):
                attr = getattr(self, name)
                if callable(attr) and not name.startswith('_'):
                    myvars[name] = CallWrapper(attr)
    
            wrapped_dic = eval(ldic.func_code, myvars)
    
            dic = {}
            for key, value in wrapped_dic.iteritems():
                # Check if value is a tuple with call wrappers
                if isinstance(value, tuple) and len(value) > 1 and \
                            isinstance(value[1], CallWrapper):
                    wrappers = value[1:]
                    value = value[0]
                    for wrapper in wrappers:
                        # Run wrappers
                        result = wrapper.invoke_with_value(value)
                        if result:
                            value = result # Wrapper modified value
    
                dic[key] = value # No wrappers
    
            super(ConfigDict, self).__init__(dic)
    
        def prefix(self, value, pref):
            print 'prefix called for value: ', value
            return pref + '_' + value
    
        def required(self, value, keys_array):
            print 'required called for value: ', value
            print 'with keys: ', keys_array
    
        def valid(self, value):
            print 'valid called for value: ', value
    
    cfg = ConfigDict(lambda: {'A': ('a', required(['test']), valid()),
                              'B': ('b', prefix('hello')),
                              'C': 'c'})
    print cfg
    

    Output:

    required called for value:  a
    with keys:  ['test']
    valid called for value:  a
    prefix called for value:  b
    {'A': 'a', 'C': 'c', 'B': 'hello_b'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't know which title I should use for this question. I have a
i don't know good title but heres the explain i have this link <a
hey, had no idea what title this post should have, hard to describe :)
I know the title is probably really hard to understand, it was hard to
I know the title is a little off, but it's hard to explain the
i didn't really know how to title this question, but here's a thing that
I know that title didn't make sense, Im sorry! Its hard to word what
Had a hard time coming up with a concise title for this. I'm sure
I know the title makes little sense, mostly because it's hard to explain in
I know, a vague title but it's hard to describe what I want in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.