public
Description: Web-based translation and translation management tool
Homepage: http://translate.sf.net/
Clone URL: git://github.com/julen/pootle.git
julen (author)
Sun Nov 23 02:11:08 -0800 2008
commit  30bd9d6f354be50672069ad8c1402788dffc0138
tree    eb843bf98f3294fa2a218dadb1faaa938a771a00
parent  8469e2a795e2e63eb6c6cf14eb4fbb5e27eec063
pootle / util.py
100644 63 lines (54 sloc) 2.03 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2004-2007 Zuza Software Foundation
#
# This file is part of translate.
#
# translate is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# translate is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with translate; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
undefined = lambda: None
 
def lazy(result_name):
    """This is used to create an attribute whose value is
lazily computed. The parameter names an object variable that
will be used to hold the lazily computed value. At the start,
this variable should hold the value undefined.
TODO: Replace this with a nice Python descriptor.
class Person(object):
def __init__(self):
self.name = 'John'
self.surname = 'Doe'
self._fullname = undefined
@lazy('_fullname')
def _get_fullname(self):
return self.name + ' ' + self.surname
"""
    
    def lazify(f):
        def evaluator(self):
          result = getattr(self, result_name)
          if result is not undefined:
            return result
          else:
            setattr(self, result_name, f(self))
            return getattr(self, result_name)
        return evaluator
    return lazify
 
class Cache(object):
    def __init__(self, getter):
        self.data = {}
        self._getter = getter
  
    def __getitem__(self, key):
        if key not in self.data:
            self.data[key] = self._getter(key)
        return self.data[key]