Skip to content

Make the landmark manager an attr dict#1

Open
patricksnape wants to merge 1 commit intolabelled_pointgraphfrom
attr_landmark_manager
Open

Make the landmark manager an attr dict#1
patricksnape wants to merge 1 commit intolabelled_pointgraphfrom
attr_landmark_manager

Conversation

@patricksnape
Copy link
Owner

@patricksnape patricksnape commented Feb 6, 2017

This is a special kind of mutable mapping that exposes it's
keys as attributes. Uses the correct overriding of
__getattr__ and __setattr__ in order to not override the default
behaviour of the attributes actually on the class (inspired
by Bunch and used under the MIT license:
https://github.com/dsc/bunch/blob/master/LICENSE.txt).

This also makes a stronger enforcement of ASCII style string
identifiers only for Python 2 and Unicode identifiers for
Python 3.

An example usage is:

import menpo.io as mio

lena = mio.import_builtin_asset.lenna_png()
# Access the landmarks
lena.landmarks.LJSON
# And getitem style access style works
lena.landmarks['LJSON']
# And setting works sensibly too
lena.landmarks.new_key = lena.landmarks.LJSON
lena.landmarks['another_key'] = lena.landmarks.LJSON
# And blocks bad keys
lena.landmarks['3'] = lena.landmarks.LJSON  # Fails due to invalid Python identifier

@jabooth @nontas @grigorisg9gr What do you think? Obviously requires menpo#675 first

This is a special kind of mutable mapping that exposes it's
keys as attributes. Uses the correct overriding of
__getattr__ and __setattr_ in order to not override the default
behaviour of the attributes actually on the class (inspired
by Bunch and used under the MIT license:
https://github.com/dsc/bunch/blob/master/LICENSE.txt).

This also makes a stronger enforcement of ascii style string
identifiers only for Python 2 and Unicode identifiers for
Python 3.
man = LandmarkManager()
man['test_set'] = pcloud.copy()
man['abc'] = pcloud.copy()
man['def'] = pcloud.copy()
Copy link
Owner Author

@patricksnape patricksnape Feb 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jabooth Hilariously this fails as a key because def is a reserved keyword

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting! So this seems to match exactly what we would expect from duck typing onto a class...

screenshot 2017-02-07 09 13 47

Copy link
Collaborator

@jabooth jabooth Feb 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@patricksnape (I'll be honest, before trying that I actually thought the first two might succeed, as I'd seen the latter example work before. But, (obviously, in retrospect) __builtin__ != keyword)

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.

2 participants