Package babel :: Module support :: Class LazyProxy

# Class LazyProxy

    object --+
             |
            LazyProxy

Class for proxy objects that delegate to a specified function to evaluate the actual object.

    >>> def greeting(name='world'):
    ...     return 'Hello, %s!' % name
    >>> lazy_greeting = LazyProxy(greeting, name='Joe')
    >>> print lazy_greeting
    Hello, Joe!
    >>> u'  ' + lazy_greeting
    u'  Hello, Joe!'
    >>> u'(%s)' % lazy_greeting
    u'(Hello, Joe!)'

This can be used, for example, to implement lazy translation functions that delay the actual translation until the string is actually used. The rationale for such behavior is that the locale of the user may not always be available. In web applications, you only know the locale when processing a request.

The proxy implementation attempts to be as complete as possible, so that the lazy objects should mostly work as expected, for example for sorting:

    >>> greetings = [
    ...     LazyProxy(greeting, 'world'),
    ...     LazyProxy(greeting, 'Joe'),
    ...     LazyProxy(greeting, 'universe'),
    ... ]
    >>> greetings.sort()
    >>> for greeting in greetings:
    ...     print greeting
    Hello, Joe!
    Hello, universe!
    Hello, world!

Instance Methods

__init__(self, func, *args, **kwargs) x.__init__(...) initializes x; see x.__class__.__doc__ for signature |

__contains__(self, key) |

__nonzero__(self) |

__dir__(self) |

__iter__(self) |

__len__(self) |

__str__(self) str(x) |

__unicode__(self) |

__add__(self, other) |

__radd__(self, other) |

__mod__(self, other) |

__rmod__(self, other) |

__mul__(self, other) |

__rmul__(self, other) |

__call__(self, *args, **kwargs) |

__lt__(self, other) |

__le__(self, other) |

__eq__(self, other) |

__ne__(self, other) |

__gt__(self, other) |

__ge__(self, other) |

__delattr__(self, name) x.__delattr__('name') <==> del x.name |

__getattr__(self, name) |

__setattr__(self, name, value) x.__setattr__('name', value) <==> x.name = value |

__delitem__(self, key) |

__getitem__(self, key) |

__setitem__(self, key, value) |

Inherited from `object`: `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`

Properties
| value
Inherited from `object`: `__class__`

Method Details

### __init__(self, func, *args, **kwargs)
(Constructor)

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
  (inherited documentation)

### __str__(self)
(Informal representation operator)

str(x)

Overrides: object.__str__
  (inherited documentation)

### __delattr__(self, name)

x.__delattr__('name') <==> del x.name

Overrides: object.__delattr__
  (inherited documentation)

### __setattr__(self, name, value)

x.__setattr__('name', value) <==> x.name = value

Overrides: object.__setattr__
  (inherited documentation)

Property Details

### value

Get Method:
  unreachable.value(self)
Set Method:

      {'territory_zones': {'001': ['Etc/GMT',
                                   'Etc/GMT-1',
                                   'Etc/GMT-2',
                                   'Etc/GMT-3',
                                   'Etc/GMT-4',
                                   'Etc/GMT-5',
                                   'Etc/GMT-6',
                                   'Etc/GMT-7',
      ...

Delete Method:

      {'territory_zones': {'001': ['Etc/GMT',
                                   'Etc/GMT-1',
                                   'Etc/GMT-2',
                                   'Etc/GMT-3',
                                   'Etc/GMT-4',
                                   'Etc/GMT-5',
                                   'Etc/GMT-6',
                                   'Etc/GMT-7',
      ...