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

Convert Context class to dictionary #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions freshen/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,24 @@
__all__ = ['glc', 'ftc', 'scc']

# Contexts
class Context(object):
class Context(dict):
"""
A javascript/lua like dictionary whose items can be accessed as attributes
"""

def __init__(self):
self.__dict__['d'] = {}
def __init__(self, *args, **kwargs):
super(Context, self).__init__(*args, **kwargs)

def __getattr__(self, name):
if name in self.d:
return self.d[name]
else:
return None
__getitem__ = __getattr__
return self.__getitem__(name)

def __setattr__(self, name, value):
self.d[name] = value
__setitem__ = __setattr__
self.__setitem__(name, value)

def __delattr__(self, name):
if name in self.d:
del self.d[name]
__delitem__ = __delattr__

def clear(self):
self.__dict__['d'] = {}
self.__delitem__(name)

def __missing__(self, name):
return None


glc = Context() # Global context - never cleared
Expand Down