2

I made the below class.

class Message:

    def __init__(self, message):
        self.message = message
        self.__dict__.update(message)

    def dict_value_finder(self, field, partial_match=False):
        """It Takes a dict with nested lists and dicts,
        and searches all dicts for a key of the field
        provided and return the value(s) as a list.
        set partial_match = True to get partial matches.
        """
        fields_found = []

        for key, value in self.message.items():

            if field in key if partial_match else field == key:
                fields_found.append(value)
                print(key, value)

            elif isinstance(value, dict):
                results = dict_value_finder(value, field, partial_match)
                fields_found.extend(results)

            elif isinstance(value, list):
                for item in value:
                    if isinstance(item, dict):
                        more_results = dict_value_finder(item, field,
                                                         partial_match)
                        fields_found.extend(more_results)

        return fields_found

The function dict_value_finder would work outside the class as in:

def dict_value_finder(search_dict, field, partial_match=False):
    """Takes a dict with nested lists and dicts,
    and searches all dicts for a key of the field
    provided and return the value(s) as a list.
    set partial_match = True to get partial matches.
    """
    fields_found = []

    for key, value in search_dict.items():

        if field in key if partial_match else field == key:
            fields_found.append(value)
            print(key, value)

        elif isinstance(value, dict):
            results = dict_value_finder(value, field, partial_match)
            fields_found.extend(results)

        elif isinstance(value, list):
            for item in value:
                if isinstance(item, dict):
                    more_results = dict_value_finder(item, field,
                                                     partial_match)
                    fields_found.extend(more_results)

    return fields_found

But when I put it inside the class I get the error:

  File "<ipython-input-42-76ab838299bc>", line 23, in dict_value_finder
    results = dict_value_finder(value, field, partial_match)

NameError: name 'dict_value_finder' is not defined

I'm not sure How to add this function to a class given that it needs recursion.

1
  • You used self when referring to message but not when referring to dict_value_finder. Why? Commented Dec 5, 2018 at 11:10

1 Answer 1

3

Change results = dict_value_finder(value, field, partial_match) to this:

results = self.dict_value_finder(value, field, partial_match)

and more_results = dict_value_finder(item, field,partial_match) to:

more_results = self.dict_value_finder(item, field,partial_match)

For accessing attributes of an instance of class we should use self.

For solving the question in comments:

def dict_value_finder(self, field, partial_match=False, search=None):

    fields_found = []

    search =  search or self.message

    for key, value in search.items():

        if field in key if partial_match else field == key:
            fields_found.append(value)
            print(key, value)

        elif isinstance(value, dict):
            results = self.dict_value_finder(field, partial_match, value)
            fields_found.extend(results)

        elif isinstance(value, list):
            for item in value:
                if isinstance(item, dict):
                    more_results = self.dict_value_finder(field,partial_match, item)
                    fields_found.extend(more_results)

    return fields_found
Sign up to request clarification or add additional context in comments.

4 Comments

TypeError: dict_value_finder() takes from 2 to 3 positional arguments but 4 were given
it should not take self as first argument but either value or item
This comes from the definition of your method def dict_value_finder(self, field, partial_match=False): you didn't define item or value there.
item and value should replace self.message. How can I do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.