diff options
author | Jason Kirtland <jek@discorporate.us> | 2008-02-22 19:03:44 +0000 |
---|---|---|
committer | Jason Kirtland <jek@discorporate.us> | 2008-02-22 19:03:44 +0000 |
commit | e203b4dd4e2ce24e18fbeb9db515b2bdbbb56bc5 (patch) | |
tree | 5e8127379e1ca1d72f4e4077d601a399bf6dd276 /lib/sqlalchemy/util.py | |
parent | b1c9082c9486887eaff3ccf95887a2618b939642 (diff) | |
download | sqlalchemy-e203b4dd4e2ce24e18fbeb9db515b2bdbbb56bc5.tar.gz |
- Converted MAGICCOOKIE=object() to a little symbol implementation to ease object inspection and debugging
Diffstat (limited to 'lib/sqlalchemy/util.py')
-rw-r--r-- | lib/sqlalchemy/util.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/sqlalchemy/util.py b/lib/sqlalchemy/util.py index 14c4c9f55..4e8a837c9 100644 --- a/lib/sqlalchemy/util.py +++ b/lib/sqlalchemy/util.py @@ -952,6 +952,42 @@ class ScopedRegistry(object): def _get_key(self): return self.scopefunc() +class symbol(object): + """A constant symbol. + + >>> symbol('foo') is symbol('foo') + True + >>> symbol('foo') + <symbol 'foo> + + A slight refinement of the MAGICCOOKIE=object() pattern. The primary + advantage of symbol() is its repr(). They are also singletons. + """ + + symbols = {} + _lock = threading.Lock() + + def __new__(cls, name): + try: + symbol._lock.acquire() + sym = cls.symbols.get(name) + if sym is None: + cls.symbols[name] = sym = object.__new__(cls, name) + return sym + finally: + symbol._lock.release() + + def __init__(self, name): + """Construct a new named symbol. + + Repeated calls of symbol('name') will all return the same instance. + """ + + assert isinstance(name, str) + self.name = name + def __repr__(self): + return "<symbol '%s>" % self.name + def warn(msg): if isinstance(msg, basestring): warnings.warn(msg, exceptions.SAWarning, stacklevel=3) |