oca.decorators package

Submodules

oca.decorators.foreach module

oca.decorators.foreach.foreach(container=None)[source]

Loop the decorated method and return the results in a new container.

Example

@helpers.foreach(list)
def method(self):
    return self.id

If the above method is called on a Record Singleton with the id 1 it would return:

[1]

If the method is called on a RecordSet containing Records with id 1, 2, and 3 it would return:

[1, 2, 3]

If no container is provided as an argument to the decorator, and a value is returned from the decorated method, a ValueError will be raised.

@helpers.foreach()
def method(self):
    # Raises ValueError
    return 1+2

@helpers.foreach()
def method(self):
    # No error raised
    self.math = 1+2
Parameters:

container (callable) – It will be called with an iterable of the decorated method results, then returned.

Raises:
  • AssertionError – If no container type is provided, and a value is returned from the decorated method. This will only be raised if PYTHONOPTIMIZE is less than 2.
  • TypeError – If an incorrect data type is provided as container. Valid arguments are None or any callable.
Returns:

A new container, instantiated with the results of the

decorated method - iterated so that self is a singleton.

Return type:

mixed

Module contents

oca.decorators.foreach(container=None)[source]

Loop the decorated method and return the results in a new container.

Example

@helpers.foreach(list)
def method(self):
    return self.id

If the above method is called on a Record Singleton with the id 1 it would return:

[1]

If the method is called on a RecordSet containing Records with id 1, 2, and 3 it would return:

[1, 2, 3]

If no container is provided as an argument to the decorator, and a value is returned from the decorated method, a ValueError will be raised.

@helpers.foreach()
def method(self):
    # Raises ValueError
    return 1+2

@helpers.foreach()
def method(self):
    # No error raised
    self.math = 1+2
Parameters:

container (callable) – It will be called with an iterable of the decorated method results, then returned.

Raises:
  • AssertionError – If no container type is provided, and a value is returned from the decorated method. This will only be raised if PYTHONOPTIMIZE is less than 2.
  • TypeError – If an incorrect data type is provided as container. Valid arguments are None or any callable.
Returns:

A new container, instantiated with the results of the

decorated method - iterated so that self is a singleton.

Return type:

mixed