Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add check to __contains__(k) #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

zsandrus
Copy link

Fixes #16

Adds a check to see if the first level of the key exists before trying to retrieve the value.

Add a check to see if the first level of the key exists before trying to retrieve the value.
@edwardselby
Copy link

edwardselby commented Oct 19, 2020

not sure on the progress of this so i have made a work around for the time being, use at your own risk:

from dotted.collection import (
    DottedDict as BaseDottedDict,
    DottedList as BaseDottedList,
    DottedCollection as BaseDottedCollection
)


class DottedCollection(BaseDottedCollection):

    def __getitem__(self, name): return super().__getitem__(name)
    def __setitem__(self, name, value): super().__setitem__(name, value)
    def __delitem__(self, name): super().__delitem__(name)
    def to_python(self): return super().to_python()

    @classmethod
    def factory(cls, initial=None):
        if isinstance(initial, list):
            return DottedList(initial)
        elif isinstance(initial, dict):
            return DottedDict(initial)
        else:
            return initial

class DottedList(BaseDottedList, DottedCollection): pass

class DottedDict(BaseDottedDict, DottedCollection):
    def __contains__(self, k):
        try:
            return super().__contains__(k)
        except KeyError:
            return False

# proof of concept.
data = DottedCollection.factory({"calculated": {"attribute": 123}})
if "calculated.attribute" in data: print('test')

data = DottedCollection.factory([{"calculated": {"attribute": 123}}])
    for item in data:
        if "calculated.attribute" in data: print('test')

Please let us know when you fix this buddy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

__contains__(k) does not handle dotted checks properly
2 participants