break-out-nested


Namebreak-out-nested JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/break_out_nested
SummaryBreak out of nested loops with max. 4 lines of code, no matter how deeply they are nested
upload_time2023-02-05 09:00:13
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords itertools nested loops break break out
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
# Break out of nested loops with max. 4 lines of code, no matter how deeply they are nested 





## The problem: breaking out of nested loops 



#### pip install break-out-nested 



```python

# Break out of nested loops - a pain in the ***

# How it is usually done:

done = False

for i in range(1, 6, 1):  # 1st loop

    print('i:', i)

    for j in range(1, 11, 2):  # 2nd loop

        print('   i, j:', i, j)

        for k in range(1, 21, 4):  # 3rd loop

            print('      i,j,k:', i, j, k)

            if i % 3 == 0 and j % 3 == 0 and k % 3 == 0:

                done = True

                break  # breaking from 3rd loop

        if done: break  # breaking from 2nd loop

    if done: break  # breaking from 1st loop

```







## The solution



```python

# Way easier

from break_out_nested import it, bol



# you need to create new variables as attributes of it,

# because break_out_nested has only access to these variables

it.i, it.j, it.k = 1, 1, 1





# the break condition

def cond(): return it.i % 3 == 0 and it.j % 3 == 0 and it.k % 3 == 0





# The condition will be checked in each loop

# The function that checks the condition has to be passed as the last argument. 

# You can pass as many iterables as you want to the function

for it.i, it.j, it.k in bol(range(1, 6, 1), range(1, 11, 2), range(1, 21, 4), cond):

    print(it.i, it.j, it.k)

```







## More examples



```python

# More examples

def cond(): return it.i + it.j + it.k == 777





it.i, it.j, it.k = 0, 0, 0

for it.i, it.j, it.k in bol(range(100), range(1000), range(10000), cond):

    print(it.i, it.j, it.k)





def cond(): return it.i + it.j + it.k >= 100000





it.i, it.j, it.k = 0, 0, 0

# you don't have to use it.i, it.j, it.k as the loop variables, you can

# use anything you want, but you have to update the variables somewhere

for i, j, k in bol(range(100), range(1000), range(10000), cond):

    it.i, it.j, it.k = i * 10, j * 100, k * 100

    print(it.i, it.j, it.k)

```




            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/break_out_nested",
    "name": "break-out-nested",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "itertools,nested,loops,break,break out",
    "author": "Johannes Fischer",
    "author_email": "<aulasparticularesdealemaosp@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/fb/68/9367b189244380d51c260033e94143bc0b9344cdb862c7c2fdb1ece6903d/break_out_nested-0.10.tar.gz",
    "platform": null,
    "description": "\n# Break out of nested loops with max. 4 lines of code, no matter how deeply they are nested \n\n\n\n\n\n## The problem: breaking out of nested loops \n\n\n\n#### pip install break-out-nested \n\n\n\n```python\n\n# Break out of nested loops - a pain in the ***\n\n# How it is usually done:\n\ndone = False\n\nfor i in range(1, 6, 1):  # 1st loop\n\n    print('i:', i)\n\n    for j in range(1, 11, 2):  # 2nd loop\n\n        print('   i, j:', i, j)\n\n        for k in range(1, 21, 4):  # 3rd loop\n\n            print('      i,j,k:', i, j, k)\n\n            if i % 3 == 0 and j % 3 == 0 and k % 3 == 0:\n\n                done = True\n\n                break  # breaking from 3rd loop\n\n        if done: break  # breaking from 2nd loop\n\n    if done: break  # breaking from 1st loop\n\n```\n\n\n\n\n\n\n\n## The solution\n\n\n\n```python\n\n# Way easier\n\nfrom break_out_nested import it, bol\n\n\n\n# you need to create new variables as attributes of it,\n\n# because break_out_nested has only access to these variables\n\nit.i, it.j, it.k = 1, 1, 1\n\n\n\n\n\n# the break condition\n\ndef cond(): return it.i % 3 == 0 and it.j % 3 == 0 and it.k % 3 == 0\n\n\n\n\n\n# The condition will be checked in each loop\n\n# The function that checks the condition has to be passed as the last argument. \n\n# You can pass as many iterables as you want to the function\n\nfor it.i, it.j, it.k in bol(range(1, 6, 1), range(1, 11, 2), range(1, 21, 4), cond):\n\n    print(it.i, it.j, it.k)\n\n```\n\n\n\n\n\n\n\n## More examples\n\n\n\n```python\n\n# More examples\n\ndef cond(): return it.i + it.j + it.k == 777\n\n\n\n\n\nit.i, it.j, it.k = 0, 0, 0\n\nfor it.i, it.j, it.k in bol(range(100), range(1000), range(10000), cond):\n\n    print(it.i, it.j, it.k)\n\n\n\n\n\ndef cond(): return it.i + it.j + it.k >= 100000\n\n\n\n\n\nit.i, it.j, it.k = 0, 0, 0\n\n# you don't have to use it.i, it.j, it.k as the loop variables, you can\n\n# use anything you want, but you have to update the variables somewhere\n\nfor i, j, k in bol(range(100), range(1000), range(10000), cond):\n\n    it.i, it.j, it.k = i * 10, j * 100, k * 100\n\n    print(it.i, it.j, it.k)\n\n```\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Break out of nested loops with max. 4 lines of code, no matter how deeply they are nested",
    "version": "0.10",
    "split_keywords": [
        "itertools",
        "nested",
        "loops",
        "break",
        "break out"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d60464747c9ec50e53eedcdeef5426499de920ed5afcdb6986a862e1d1767fff",
                "md5": "d11c145316280f1dce4e90e89d9fe0b4",
                "sha256": "f792522a23c37c465c7adf4228178e4c866f427c65fd26868bc4f891862af873"
            },
            "downloads": -1,
            "filename": "break_out_nested-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d11c145316280f1dce4e90e89d9fe0b4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 5369,
            "upload_time": "2023-02-05T09:00:12",
            "upload_time_iso_8601": "2023-02-05T09:00:12.268972Z",
            "url": "https://files.pythonhosted.org/packages/d6/04/64747c9ec50e53eedcdeef5426499de920ed5afcdb6986a862e1d1767fff/break_out_nested-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "fb689367b189244380d51c260033e94143bc0b9344cdb862c7c2fdb1ece6903d",
                "md5": "77489c320b160a1cc5ab35913796ec41",
                "sha256": "5da8cd628c2325f20e84b57df8f1913312d05fd748c4b933e69bae4849e91bf1"
            },
            "downloads": -1,
            "filename": "break_out_nested-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "77489c320b160a1cc5ab35913796ec41",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 3731,
            "upload_time": "2023-02-05T09:00:13",
            "upload_time_iso_8601": "2023-02-05T09:00:13.952678Z",
            "url": "https://files.pythonhosted.org/packages/fb/68/9367b189244380d51c260033e94143bc0b9344cdb862c7c2fdb1ece6903d/break_out_nested-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-02-05 09:00:13",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "hansalemaos",
    "github_project": "break_out_nested",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "break-out-nested"
}
        
Elapsed time: 0.06203s