This module that I created for fun, lets you inject function calls inside the loop itself, between every iteration.
forXYZ().xyzinobj([], [], []):will work, and
forXYZ().xyzinobj(
[],
[],
[]
):Will not!
- import
inject_in_loop.Baseand inherit it with your own class. - The inheriting class must define an attribute/method named
functionthat will be called between each iteration. - use the following syntax:
for YourClass().item in Array:...
for YourClass().item1.item2 in Array:...
for YourClass().item1, YourClass().item2 in Array:...
for YourClass().item1, item2 in Array:...
- In the first scenario, the variable
itemwill be available. - In the second scenario, the variables
item1anditem2will be available, It will only work in case that each element in the array can be unpacked into a two-items tuple, just like the regularfor a,b in array:... - In the third scenario, the variables
item1anditem2will be available, just like the second example. However, In this scenarioYourClass.functionwill be called two times per each iteration, since there are two objects in that example. - In the fourth scenario, the variables
item1anditem2will be available, like the second example, in fact it is the same as the second scenario.
You can also pass arguments to the class which will be sent into function:
an example from inject_in_loop/main.py:
frominject_in_loopimportBasefromtimeimportsleepclassSleep(Base):
function=sleepforSleep(0.3).range1.range2inzip(range(10), range(10)):
print(range1+range2, "[obj.a.b] a + b")It will sleep 0.3 seconds between each iteration, and the output will be:
0 [obj.a.b] a + b
2 [obj.a.b] a + b
4 [obj.a.b] a + b
6 [obj.a.b] a + b
8 [obj.a.b] a + b
10 [obj.a.b] a + b
12 [obj.a.b] a + b
14 [obj.a.b] a + b
16 [obj.a.b] a + b
18 [obj.a.b] a + b
or:
classPrintNewLine(Base):
function=printforPrintNewLine().itemin ("a", "b", "c"):
print(item)The output will be
a
b
c
You can see a couple of examples in inject_in_loop/main.py.
In order to run those examples you need to run that file directly (not by importing it.).
You can also create mixins, which will call several functions with their corresponding arguments, as in the ex example:
frominject_in_loopimportBasefromtimeimportsleepclassMixin(Base):
functions= [sleep, print]
forMixin(f_params={"sleep": (1,),"print": ()}).elementinrange(10):
print(element)You must specify the parameter f_params (must be a keyword argument) which will hold a dict with the corresponding tuple of arguments for each function.
the functions in a mixin will be held under an attribute named functions in the class.
You can also use unpacking in the for loop, as in the example:
frominject_in_loopimportBasefromtimeimportsleepclassSleep(Base):
function=sleepforSleep(0.3, unpack="b").a.b.cin [["a", "b", "c", "d"]]*5:
print(a, b, c)The result will be:
a ['b', 'c'] d
a ['b', 'c'] d
a ['b', 'c'] d
a ['b', 'c'] d
a ['b', 'c'] d
b is the "starred" argument, as in the expression a, *b, c = ["a", "b", "c", "d"].
You can't have more than one starred argument.