Utilities

philo.utils.fattr(*args, **kwargs)

Returns a wrapper which takes a function as its only argument and sets the key/value pairs passed in with kwargs as attributes on that function. This can be used as a decorator.

Example:

>>> from philo.utils import fattr
>>> @fattr(short_description="Hello World!")
... def x():
...     pass
... 
>>> x.short_description
'Hello World!'
class philo.utils.ContentTypeRegistryLimiter

Can be used to limit the choices for a ForeignKey or ManyToManyField to the ContentTypes which have been registered with this limiter.

register_class(cls)

Registers a model class with this limiter.

unregister_class(cls)

Unregisters a model class from this limiter.

class philo.utils.ContentTypeSubclassLimiter(cls, inclusive=False)

Can be used to limit the choices for a ForeignKey or ManyToManyField to the ContentTypes for all non-abstract models which subclass the class passed in on instantiation.

Parameters:
  • cls – The class whose non-abstract subclasses will be valid choices.
  • inclusive – Whether cls should also be considered a valid choice (if it is a non-abstract subclass of models.Model)
philo.utils.paginate(objects, per_page=None, page_number=1)

Given a list of objects, return a (paginator, page, objects) tuple.

Parameters:
  • objects – The list of objects to be paginated.
  • per_page – The number of objects per page.
  • page_number – The number of the current page.
Returns tuple:

(paginator, page, objects) where paginator is a django.core.paginator.Paginator instance, page is the result of calling Paginator.page() with page_number, and objects is page.objects. Any of the return values which can’t be calculated will be returned as None.

AttributeMappers

class philo.utils.entities.AttributeMapper(entity)

Given an Entity subclass instance, this class allows dictionary-style access to the Entity‘s Attributes. In order to prevent unnecessary queries, the AttributeMapper will cache all Attributes and the associated python values when it is first accessed.

Parameters:entity – The Entity subclass instance whose Attributes will be made accessible.
get_attributes()

Returns an iterable of all of the Entity‘s Attributes.

get_attribute(key, default=None)

Returns the Attribute instance with the given key from the cache, populating the cache if necessary, or default if no such attribute is found.

keys()

Returns the keys from the cache, first populating the cache if necessary.

items()

Returns the items from the cache, first populating the cache if necessary.

values()

Returns the values from the cache, first populating the cache if necessary.

clear_cache()

Clears the cache.

class philo.utils.entities.TreeAttributeMapper(entity)

Bases: philo.utils.entities.AttributeMapper

The TreeEntity class allows the inheritance of Attributes down the tree. This mapper will return the most recently declared Attribute among the TreeEntity‘s ancestors or set an attribute on the Entity it is attached to.

get_attributes()

Returns a list of Attributes sorted by increasing parent level. When used to populate the cache, this will cause Attributes on the root to be overwritten by those on its children, etc.

class philo.utils.entities.PassthroughAttributeMapper(entities)

Bases: philo.utils.entities.AttributeMapper

Given an iterable of Entities, this mapper will fetch an AttributeMapper for each one. Lookups will return the value from the first AttributeMapper which has an entry for a given key. Assignments will be made to the first Entity in the iterable.

Parameters:entities – An iterable of Entity subclass instances.

LazyAttributeMappers

class philo.utils.entities.LazyAttributeMapperMixin

In some cases, it may be that only one attribute value needs to be fetched. In this case, it is more efficient to avoid populating the cache whenever possible. This mixin overrides the __getitem__() and get_attribute() methods to prevent their populating the cache. If the cache has been populated (i.e. through keys(), values(), etc.), then the value or attribute will simply be returned from the cache.

class philo.utils.entities.LazyAttributeMapper(entity)

Bases: philo.utils.entities.LazyAttributeMapperMixin, philo.utils.entities.AttributeMapper

class philo.utils.entities.LazyTreeAttributeMapper(entity)

Bases: philo.utils.entities.LazyAttributeMapperMixin, philo.utils.entities.TreeAttributeMapper

class philo.utils.entities.LazyPassthroughAttributeMapper(entities)

Bases: philo.utils.entities.LazyAttributeMapperMixin, philo.utils.entities.PassthroughAttributeMapper

The LazyPassthroughAttributeMapper is lazy in that it tries to avoid accessing the AttributeMappers that it uses for lookups. However, those AttributeMappers may or may not be lazy themselves.

Project Versions

Table Of Contents

Previous topic

Validators

Next topic

Template Tags

This Page