annotate rhodecode/lib/vcs/utils/lazy.py @ 3057:79c5967a1e5c beta

whitespace and formatting
author Marcin Kuzminski <marcin@python-works.com>
date Mon, 03 Dec 2012 02:56:57 +0100
parents 7ae9939409ab
children f134d125fb67
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
1 class LazyProperty(object):
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
2 """
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
3 Decorator for easier creation of ``property`` from potentially expensive to
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
4 calculate attribute of the class.
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
5
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
6 Usage::
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
7
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
8 class Foo(object):
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
9 @LazyProperty
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
10 def bar(self):
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
11 print 'Calculating self._bar'
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
12 return 42
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
13
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
14 Taken from http://blog.pythonisito.com/2008/08/lazy-descriptors.html and
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
15 used widely.
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
16 """
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
17
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
18 def __init__(self, func):
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
19 self._func = func
2494
c4d418b440d1 small fix for lazy property
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
20 self.__module__ = func.__module__
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
21 self.__name__ = func.__name__
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
22 self.__doc__ = func.__doc__
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
23
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
24 def __get__(self, obj, klass=None):
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
25 if obj is None:
2494
c4d418b440d1 small fix for lazy property
Marcin Kuzminski <marcin@python-works.com>
parents: 2007
diff changeset
26 return self
2007
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
27 result = obj.__dict__[self.__name__] = self._func(obj)
324ac367a4da Added VCS into rhodecode core for faster and easier deployments of new versions
Marcin Kuzminski <marcin@python-works.com>
parents:
diff changeset
28 return result
3050
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
29
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
30 import threading
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
31
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
32
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
33 class ThreadLocalLazyProperty(LazyProperty):
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
34 """
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
35 Same as above but uses thread local dict for cache storage.
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
36 """
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
37
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
38 def __get__(self, obj, klass=None):
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
39 if obj is None:
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
40 return self
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
41 if not hasattr(obj, '__tl_dict__'):
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
42 obj.__tl_dict__ = threading.local().__dict__
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
43
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
44 result = obj.__tl_dict__[self.__name__] = self._func(obj)
7ae9939409ab Use ThreadLocal storage for dulwich cached repos, finally fixes issues on concurent opening git pack files via dulwich
Marcin Kuzminski <marcin@python-works.com>
parents: 2494
diff changeset
45 return result