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 ability to access dict entries #8

Closed
wants to merge 1 commit into from

Conversation

twidi
Copy link
Owner

@twidi twidi commented Jun 30, 2015

This works this way:

The solver starts by trying to access the name as an attribute.
If an AttributeError exception is raised, then it tries to access it
via a dict-like approach.
If it still fail via a KeyError exception, then a
AttributeNotFound excetption is raised.

The key must of course be one of the attribute allowed for the class.

Their is no way to specify keys to retrieve for a simple dict. If the
dict class is added as a source to the registry allowing all
attributes, then all keys will be allowed for all dicts (if the dict class is not registered as a source, no key will be available for simple dicts)

It still possible, though, to use a function at the upper level that
will act as an attribute and will return only some keys we want.

If we have this:

class Klass:
    def func(self):
        return {'foo': 0, 'bar': 1, 'baz': 2}

registry.register(dict)
registry.register(Klass)

Then the user will be able to access func, then foo bar and baz
from the dict.

If we don't want the user to access baz without modifying the class,
we can do this:

def func(obj):
    d = obj.func()
    allowed = {'foo', 'bar'}
    return {k: v for k, v in d.items() if k in allowed}

registry.register(dict)
registry.register(klass, ('func', func))

Then, when solving the func attribute, it's the func function what
will be called, itself calling the method then returning a dict with
only the wanted keys

Another way to do it is:

class FuncDict(dict): pass

def func(obj):
    return FuncDict(obj.func())

registry.register(FuncDict, ['foo', 'bar'])
registry.register(klass, ('func', func))

This works this way:

The solver starts by trying to access the name as an attribute.
If an `AttributeError` exception is raised, then it tries to access it
via a dict-like approach.
If it still fail via a `KeyError` exception, then a
`AttributeNotFound` excetption is raised.

The key must of course be one of the attribute allowed for the class.

Their is no way to specify keys to retrieve for a simple dict. If the
`dict` class is added as a source to the registry allowing all
attributes, then all keys will be allowed for all dicts (if the dict
class is not registered as a source, no key will be available for simple
dicts)

It still possible, though, to use a function at the upper level that
will act as an attribute and will return only some keys we want.

If we have this:

```python
class Klass:
    def func(self):
        return {'foo': 0, 'bar': 1, 'baz': 2}

registry.register(dict)
registry.register(Klass)
```

Then the user will be able to access `func`, then `foo` `bar` and `baz`
from the dict.

If we don't want the user to access `baz` without modifying the class,
we can do this:

```python
def func(obj):
    d = obj.func()
    allowed = {'foo', 'bar'}
    return {k: v for k, v in d.items() if k in allowed}

registry.register(dict)
registry.register(klass, ('func', func))
```

Then, when solving the ``func`` attribute, it's the `func` function what
will be called, itself calling the method then returning a dict with
only the wanted keys

Another way to do it is:

```python
class FuncDict(dict): pass

def func(obj):
    return FuncDict(obj.func())

registry.register(FuncDict, ['foo', 'bar'])
registry.register(klass, ('func', func))
```
@twidi twidi force-pushed the feature/twidi/access-dict-items branch from 1cea8cd to dff79ba Compare June 30, 2015 10:22
@twidi twidi closed this Jun 30, 2015
@twidi twidi deleted the feature/twidi/access-dict-items branch June 30, 2015 10:26
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.

None yet

1 participant