|
1 |
| -"""Package helper functions.""" |
| 1 | +"""Package helper functions and classes. |
| 2 | +
|
| 3 | +Functions: |
| 4 | + utf8: Returns the input string as a UTF-8 unicode encoded string. |
| 5 | + generator_container: Function decorator that containerizes calls to |
| 6 | + generator functions. |
| 7 | +
|
| 8 | +Classes: |
| 9 | + GeneratorContainer: Container for storing a function call to a generator |
| 10 | + function. |
| 11 | +
|
| 12 | +""" |
| 13 | + |
| 14 | + |
| 15 | +import functools |
2 | 16 |
|
3 | 17 |
|
4 | 18 | def utf8(string):
|
5 |
| - """Return the 'string' as a UTF-8 unicode encoded string.""" |
| 19 | + """Return the 'string' as a UTF-8 unicode encoded string. |
| 20 | +
|
| 21 | + Ensure that 'string' is a unicode encoded string; converting if necessary. |
| 22 | +
|
| 23 | + Args: |
| 24 | + string(unicode, str): The input string. |
| 25 | +
|
| 26 | + Returns: |
| 27 | + unicode: The input string encoded as a unicode string. |
| 28 | +
|
| 29 | + Raises: |
| 30 | + AssertionError: If the parameter types are incorrect. |
| 31 | +
|
| 32 | + """ |
6 | 33 | assert isinstance(string, basestring)
|
7 | 34 | if isinstance(string, unicode):
|
8 | 35 | return string
|
9 | 36 | elif isinstance(string, str):
|
10 | 37 | return unicode(string, encoding='utf-8')
|
| 38 | + |
| 39 | + |
| 40 | +class GeneratorContainer(object): |
| 41 | + """Container for storing a function call to a generator function. |
| 42 | +
|
| 43 | + Return a fresh iterator every time __iter__() is called on the container |
| 44 | + object. |
| 45 | +
|
| 46 | + Attributes: |
| 47 | + generator(func): The generator function. |
| 48 | + args(list): The arguments passed to the generator function. |
| 49 | + kwargs(dict): The keyword arguments passed to the generator function. |
| 50 | +
|
| 51 | + """ |
| 52 | + |
| 53 | + def __init__(self, generator, *args, **kwargs): |
| 54 | + """Inits a new GeneratorContainer. |
| 55 | +
|
| 56 | + Args: |
| 57 | + generator(func): The generator function. |
| 58 | + *args: The arguments passed to the generator function. |
| 59 | + **kwargs: The keyword arguments passed to the generator function. |
| 60 | +
|
| 61 | + """ |
| 62 | + self.generator = generator |
| 63 | + self.args = args |
| 64 | + self.kwargs = kwargs |
| 65 | + |
| 66 | + def __iter__(self): |
| 67 | + """Return a fresh iterator.""" |
| 68 | + return self.generator(*self.args, **self.kwargs) |
| 69 | + |
| 70 | + |
| 71 | +def generator_container(generator): |
| 72 | + """Function Decorator: Containerize calls to a generator function. |
| 73 | +
|
| 74 | + Args: |
| 75 | + generator(func): The generator function being containerized. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + func: A wrapper function that containerizes the calls to the generator. |
| 79 | +
|
| 80 | + """ |
| 81 | + |
| 82 | + @functools.wraps(generator) |
| 83 | + def generator_container_wrapper(*args, **kwargs): |
| 84 | + """Store a generator call in a container and return the container. |
| 85 | +
|
| 86 | + Args: |
| 87 | + *args: The arguments passed to the generator function. |
| 88 | + **kwargs: The keyword arguments passed to the generator function. |
| 89 | +
|
| 90 | + Returns: |
| 91 | + GeneratorContainer: A container wrapping the call to the generator. |
| 92 | +
|
| 93 | + """ |
| 94 | + return GeneratorContainer(generator, *args, **kwargs) |
| 95 | + |
| 96 | + return generator_container_wrapper |
0 commit comments