Skip to main content

Python3 module to simulate recursion with iteration.

Project description

iterativerecursion

Python3 module to simulate recursion with iteration.

This is a module I've done because I need something like this and in part as a little experiment.

Installation

Install with pip

pip3 install -U iterativerecursion

Usage

from iterativerecursion import FunctionReturn
from iterativerecursion import IterativeRecursionEngine


def func_1_to_test(a: int) -> FunctionReturn:
    """
    Print a number.
    :param a: int: Number to print.

    """
    print(f"a: {a}")
    return dict(
        call_arg_n_func=dict(b="global_a"),
        next_function_to_call="func_2_to_test",
        returned_values=dict(global_a=a + 1)
        )


def func_2_to_test(b: int) -> FunctionReturn:
    """
    Print a number.
    :param b: int: Number to print.

    """
    print("b", b)
    return dict(
        call_arg_n_func=dict(),
        next_function_to_call=None,
        returned_values=dict()
        )


executor = IterativeRecursionEngine()
executor.add_function(func_1_to_test)
executor.add_function(func_2_to_test)
executor.start_function_caller(
    next_function_to_call="func_1_to_test",
    enviroment_variables=dict(test_var=2),
    call_arg_n_func=dict(a="test_var")
    )

Output:

a: 2
b 3

Be careful with infinite loops:

from iterativerecursion import FunctionReturn
from iterativerecursion import IterativeRecursionEngine


def func_1_to_test(a: int) -> FunctionReturn:
    """
    Print a number.
    :param a: int: Number to print.

    """
    print(f"a: {a}")
    return dict(
        call_arg_n_func=dict(a="global_a"),
        next_function_to_call="func_1_to_test",
        returned_values=dict(global_a=a + 1)
        )


executor = IterativeRecursionEngine()
executor.add_function(func_1_to_test)
executor.start_function_caller(
    next_function_to_call="func_1_to_test",
    enviroment_variables=dict(test_var=0),
    call_arg_n_func=dict(a="test_var")
    )

Output:

a: 0
a: 1
a: 2
...

Import types used on this module:

from iterativerecursion import FunctionReturn, VarsDict

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

iterativerecursion-0.2.tar.gz (2.8 kB view details)

Uploaded Source

File details

Details for the file iterativerecursion-0.2.tar.gz.

File metadata

  • Download URL: iterativerecursion-0.2.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.6.7

File hashes

Hashes for iterativerecursion-0.2.tar.gz
Algorithm Hash digest
SHA256 3768747dc49650b6cc432be8aec2912cba2746ac4a1247a9217982913676d8dd
MD5 23ff0e54f6a26fbb0336abf5ab6f2014
BLAKE2b-256 c47fdfe83d0da1a2656a1a506d85d93f32ee80016ff614d2f7fc8b54ea2ab7d0

See more details on using hashes here.

Supported by

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