Skip to main content

This module lets you inject a function call between every iteration without doing it manually. Created mainly for fun :)

Project description

InjectInLoop

This module that I created for fun, lets you inject function calls inside the loop itself, between every iteration.

Important! currently, it only supports one-line for loop (only the "signature"), i.e.

for XYZ().xyz in obj([], [], []):

will work, and

for XYZ().xyz in obj(
        [],
        [],
        []
):

Will not!

Usage:

  • import inject_in_loop.Base and inherit it with your own class.
  • The inheriting class must define an attribute/method named function that 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 item will be available.
  • In the second scenario, the variables item1 and item2 will 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 regular for a,b in array:...
  • In the third scenario, the variables item1 and item2 will be available, just like the second example. However, In this scenario YourClass.function will be called two times per each iteration, since there are two objects in that example.
  • In the fourth scenario, the variables item1 and item2 will 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:

from inject_in_loop import Base
from time import sleep


class Sleep(Base):
    function = sleep


for Sleep(0.3).range1.range2 in zip(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:

class PrintNewLine(Base):
    function = print


for PrintNewLine().item in ("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:

from inject_in_loop import Base
from time import sleep

class Mixin(Base):
    functions = [sleep, print]

for Mixin(f_params={"sleep": (1,),"print": ()}).element in range(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:

from inject_in_loop import Base
from time import sleep

class Sleep(Base):
    function = sleep
    
    
for Sleep(0.3, unpack="b").a.b.c in [["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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

InjectInLoop-1.0.2.tar.gz (3.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

InjectInLoop-1.0.2-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file InjectInLoop-1.0.2.tar.gz.

File metadata

  • Download URL: InjectInLoop-1.0.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.0

File hashes

Hashes for InjectInLoop-1.0.2.tar.gz
Algorithm Hash digest
SHA256 8141ece8256530ae99cf10bdcf05e69812e50a58d0e5d5678ed3bcfb44054b1c
MD5 c2c79fa71aa02bccb657b8d815cc29b3
BLAKE2b-256 5a475888d049454f1d2ede9916548854ac62c66f88a82847bdc002ee92636b21

See more details on using hashes here.

File details

Details for the file InjectInLoop-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: InjectInLoop-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.0

File hashes

Hashes for InjectInLoop-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cd71e2e48bcde2f6c782b4ef70b4993d9f925fab7478f2f912e7423d367f45a6
MD5 fdb4c0aa3868d6ff013f6ecc25157daa
BLAKE2b-256 9414ceac7070e63f8fba816adabf4b7e894773a5bad0d895517b561514c56b34

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page