I have a very simple class that defines properties like:
class Person:
fields = set()
@property
def id(self):
self.fields.add('id')
return 'person.id'
The idea is for this class to record which properties have been accessed. Now the problem comes when I need to start supporting: person.metadata.key
where metadata is basically an HStore object or 1 level JSON, the key is arbitrary and the idea is for the class Person so record access to any of the keys in metadata
. I tried something like this:
class CustomerBulkContext:
fields = set()
class PersonMetadata:
def __getitem__(self, attr):
fields.add(f'metadata.{attr}')
return f'person.metadata.{attr}'
metadata = CustomerMetadataContext()
Now obviously the problem is that fields
inside PersonMetadata is not a known variable at this point. How can I overcome this issue, I don't know if it's possible to do in Python without too much code.