Skip to content

Commit ae19a70

Browse files
committed
Update Generator Container interfaces, variables and docstrings
Update the Generator Container interfaces and docstrings to improve clarity. The parameters and internal variable names should clearly illustrate what data types are accepted as parameters, contained, and returned. These containers are taking on a an extra degree of complexity in the ciscosparksdk package where the containers are being extended to provide caching capabilities. Clarity of operations is key to understanding the proper operation of these containers and their caching enabled sub-classes.
1 parent efabff5 commit ae19a70

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

ciscosparkapi/utils.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -211,48 +211,47 @@ def extract_and_parse_json(response):
211211

212212

213213
class GeneratorContainer(object):
214-
"""Container for storing a function call to a generator function.
214+
"""Store a generator function call, making it for safe reuse.
215215
216-
Return a fresh iterator every time __iter__() is called on the container
216+
Return a fresh generator every time __iter__() is called on the container
217217
object.
218218
219-
Attributes:
220-
generator(func): The generator function.
221-
args(list): The arguments passed to the generator function.
222-
kwargs(dict): The keyword arguments passed to the generator function.
223-
224219
"""
225220

226-
def __init__(self, generator, *args, **kwargs):
221+
def __init__(self, generator_function, *args, **kwargs):
227222
"""Init a new GeneratorContainer.
228223
229224
Args:
230-
generator(func): The generator function.
225+
generator_function(func): The generator function.
231226
*args: The arguments passed to the generator function.
232227
**kwargs: The keyword arguments passed to the generator function.
233228
234229
"""
235-
self.generator = generator
230+
self.generator_function = generator_function
236231
self.args = args
237232
self.kwargs = kwargs
238233

234+
def new_generator(self):
235+
"""Create a new generator object."""
236+
return self.generator_function(*self.args, **self.kwargs)
237+
239238
def __iter__(self):
240239
"""Return a fresh iterator."""
241-
return self.generator(*self.args, **self.kwargs)
242-
240+
return self.new_generator()
243241

244-
def generator_container(generator):
242+
def generator_container(generator_function):
245243
"""Function Decorator: Containerize calls to a generator function.
246244
247245
Args:
248-
generator(func): The generator function being containerized.
246+
generator_function(func): The generator function being containerized.
249247
250248
Returns:
251-
func: A wrapper function that containerizes the calls to the generator.
249+
func: A wrapper function that containerizes the calls to the generator
250+
function.
252251
253252
"""
254253

255-
@functools.wraps(generator)
254+
@functools.wraps(generator_function)
256255
def generator_container_wrapper(*args, **kwargs):
257256
"""Store a generator call in a container and return the container.
258257
@@ -264,6 +263,6 @@ def generator_container_wrapper(*args, **kwargs):
264263
GeneratorContainer: A container wrapping the call to the generator.
265264
266265
"""
267-
return GeneratorContainer(generator, *args, **kwargs)
266+
return GeneratorContainer(generator_function, *args, **kwargs)
268267

269268
return generator_container_wrapper

0 commit comments

Comments
 (0)