-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f890322
Showing
13 changed files
with
277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
indent_style = tab | ||
indent_size = 4 | ||
insert_final_newline = true | ||
end_of_line = lf | ||
|
||
[*.{yml,yaml}] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
KOLANICH/python_project_boilerplate.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: 2 | ||
updates: | ||
- package-ecosystem: "pip" | ||
directory: "/" | ||
schedule: | ||
interval: "daily" | ||
allow: | ||
- dependency-type: "all" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: CI | ||
on: | ||
push: | ||
branches: [master] | ||
pull_request: | ||
branches: [master] | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-22.04 | ||
steps: | ||
- name: typical python workflow | ||
uses: KOLANICH-GHActions/typical-python-workflow@master | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
__pycache__ | ||
*.py[co] | ||
/*.egg-info | ||
*.srctrlbm | ||
*.srctrldb | ||
build | ||
dist | ||
.eggs | ||
monkeytype.sqlite3 | ||
/.ipynb_checkpoints |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
image: registry.gitlab.com/kolanich-subgroups/docker-images/fixed_python:latest | ||
|
||
variables: | ||
DOCKER_DRIVER: overlay2 | ||
SAST_ANALYZER_IMAGE_TAG: latest | ||
SAST_DISABLE_DIND: "true" | ||
SAST_CONFIDENCE_LEVEL: 5 | ||
CODECLIMATE_VERSION: latest | ||
|
||
include: | ||
- template: SAST.gitlab-ci.yml | ||
- template: Code-Quality.gitlab-ci.yml | ||
- template: License-Management.gitlab-ci.yml | ||
|
||
build: | ||
tags: | ||
- shared | ||
- linux | ||
stage: build | ||
variables: | ||
GIT_DEPTH: "1" | ||
PYTHONUSERBASE: ${CI_PROJECT_DIR}/python_user_packages | ||
|
||
before_script: | ||
- export PATH="$PATH:$PYTHONUSERBASE/bin" # don't move into `variables` | ||
- apt-get update | ||
# todo: | ||
#- apt-get -y install | ||
#- pip3 install --upgrade | ||
#- python3 ./fix_python_modules_paths.py | ||
|
||
script: | ||
- python3 -m build -nw bdist_wheel | ||
- mv ./dist/*.whl ./dist/RecursionSafe-0.CI-py3-none-any.whl | ||
- pip3 install --upgrade ./dist/*.whl | ||
- coverage run --source=RecursionSafe -m --branch pytest --junitxml=./rspec.xml ./tests/test.py | ||
- coverage report -m | ||
- coverage xml | ||
|
||
coverage: "/^TOTAL(?:\\s+\\d+){4}\\s+(\\d+%).+/" | ||
|
||
cache: | ||
paths: | ||
- $PYTHONUSERBASE | ||
|
||
artifacts: | ||
paths: | ||
- dist | ||
reports: | ||
junit: ./rspec.xml | ||
cobertura: ./coverage.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
No codes of conduct! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
include UNLICENSE | ||
include *.md | ||
include tests | ||
include .editorconfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
RecursionSafe.py [![Unlicensed work](https://raw.githubusercontent.com/unlicense/unlicense.org/master/static/favicon.png)](https://unlicense.org/) | ||
================================ | ||
~~[wheel (GitLab)](https://gitlab.com/KOLANICH/RecursionSafe.py/-/jobs/artifacts/master/raw/dist/RecursionSafe-0.CI-py3-none-any.whl?job=build)~~ | ||
~~[wheel (GHA via `nightly.link`)](https://nightly.link/KOLANICH-libs/RecursionSafe.py/workflows/CI/master/RecursionSafe-0.CI-py3-none-any.whl)~~ | ||
~~![GitLab Build Status](https://gitlab.com/KOLANICH/RecursionSafe.py/badges/master/pipeline.svg)~~ | ||
~~![GitLab Coverage](https://gitlab.com/KOLANICH/RecursionSafe.py/badges/master/coverage.svg)~~ | ||
~~[![GitHub Actions](https://github.com/KOLANICH-libs/RecursionSafe.py/workflows/CI/badge.svg)](https://github.com/KOLANICH-libs/RecursionSafe.py/actions/)~~ | ||
![N∅ hard dependencies](https://shields.io/badge/-N∅_Ъ_deps!-0F0) | ||
[![Libraries.io Status](https://img.shields.io/librariesio/github/KOLANICH-libs/RecursionSafe.py.svg)](https://libraries.io/github/KOLANICH-libs/RecursionSafe.py) | ||
[![Code style: antiflash](https://img.shields.io/badge/code%20style-antiflash-FFF.svg)](https://codeberg.org/KOLANICH-tools/antiflash.py) | ||
|
||
A context manager to protect you from infinite recursion when walking collections. | ||
|
||
Just wrap a recursive function with it. If you visit any object you are already in you will get ellipsis instead of the object, be ready for it. | ||
|
||
Example | ||
------- | ||
```python | ||
recSafe = RecursionSafe() | ||
|
||
objs = { | ||
"a": {}, | ||
"a.b": {}, | ||
"a.b.c": {}, | ||
"a.b.d": {}, | ||
"a.b.c.d": 14, | ||
"a.e": 10, | ||
"a.f": {}, | ||
} | ||
|
||
objs["a"]["b"] = objs["a.b"] | ||
objs["a.b"]["c"] = objs["a"] | ||
objs["a.b"]["d"] = objs["a.b.d"] | ||
objs["a.b.c"]["d"] = objs["a.b.c.d"] | ||
objs["a"]["e"] = objs["a.e"] | ||
objs["a"]["f"] = objs["a.f"] | ||
objs["a.f"]["g"] = objs["a"] | ||
a = objs["a"] | ||
|
||
idsToNames = {id(objs[k]): k for k in objs} | ||
|
||
|
||
@recSafe.wrap | ||
def ownRepr(a): | ||
if isinstance(a, dict): | ||
return "{" + ", ".join((ownRepr(k) + ":" + ownRepr(v) for k, v in a.items())) + "}" | ||
else: | ||
return repr(a) | ||
|
||
|
||
print("repr", repr(a)) # even non-recursive objects are ellipsed | ||
print("ownRepr", ownRepr(a)) # feel the difference - non-self-referencing objects are not ellipsed | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from functools import wraps | ||
|
||
|
||
class RecursionSafe: | ||
__slots__ = ("visiting",) | ||
|
||
class Lock: | ||
__slots__ = ("obj", "safe") | ||
|
||
def __init__(self, safe, obj): | ||
self.safe = safe | ||
self.obj = obj | ||
|
||
def __enter__(self): | ||
i = id(self.obj) | ||
if i not in self.safe.visiting: | ||
self.safe.visiting.add(i) | ||
return self.obj | ||
else: | ||
return ... | ||
|
||
def __exit__(self, *args, **kwargs): | ||
i = id(self.obj) | ||
if i in self.safe.visiting: | ||
self.safe.visiting.remove(i) | ||
|
||
def __init__(self): | ||
self.visiting = set() | ||
|
||
def __call__(self, obj): | ||
return self.__class__.Lock(self, obj) | ||
|
||
def wrap(self, f): | ||
@wraps(f) | ||
def wrapped(firstArg, *args, **kwargs): | ||
with self(firstArg) as firstArg: | ||
return f(firstArg, *args, **kwargs) | ||
|
||
return wrapped |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
This is free and unencumbered software released into the public domain. | ||
|
||
Anyone is free to copy, modify, publish, use, compile, sell, or | ||
distribute this software, either in source code form or as a compiled | ||
binary, for any purpose, commercial or non-commercial, and by any | ||
means. | ||
|
||
In jurisdictions that recognize copyright laws, the author or authors | ||
of this software dedicate any and all copyright interest in the | ||
software to the public domain. We make this dedication for the benefit | ||
of the public at large and to the detriment of our heirs and | ||
successors. We intend this dedication to be an overt act of | ||
relinquishment in perpetuity of all present and future rights to this | ||
software under copyright law. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
OTHER DEALINGS IN THE SOFTWARE. | ||
|
||
For more information, please refer to <https://unlicense.org/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
[build-system] | ||
requires = ["setuptools>=61.2.0", "setuptools_scm[toml]>=3.4.3"] | ||
build-backend = "setuptools.build_meta" | ||
|
||
[project] | ||
name = "RecursionSafe" | ||
readme = "ReadMe.md" | ||
description = "Prevents recursive functions walking collections from infinite looping." | ||
authors = [{name = "KOLANICH"}] | ||
classifiers = [ | ||
"Development Status :: 4 - Beta", | ||
"Environment :: Other Environment", | ||
"Intended Audience :: Developers", | ||
"License :: Public Domain", | ||
"Operating System :: OS Independent", | ||
"Programming Language :: Python", | ||
"Programming Language :: Python :: 3", | ||
"Programming Language :: Python :: 3 :: Only", | ||
"Topic :: Software Development :: Libraries :: Python Modules", | ||
] | ||
keywords = ["collection", "looping", "recursion"] | ||
license = {text = "Unlicense"} | ||
requires-python = ">=3.4" | ||
dynamic = ["version"] | ||
|
||
[project.urls] | ||
Homepage = "https://codeberg.org/KOLANICH-libs/RecursionSafe.py" | ||
|
||
[tool.setuptools] | ||
zip-safe = true | ||
include-package-data = true | ||
py-modules = ["RecursionSafe"] | ||
|
||
[tool.setuptools_scm] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env python3 | ||
import sys | ||
from pathlib import Path | ||
import unittest | ||
import itertools, re | ||
|
||
sys.path.insert(0, str(Path(__file__).parent.parent)) | ||
|
||
from collections import OrderedDict | ||
|
||
dict = OrderedDict | ||
|
||
import RecursionSafe | ||
from RecursionSafe import * | ||
|
||
|
||
class Tests(unittest.TestCase): | ||
|
||
def testSimple(self): | ||
raise NotImplementedError | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |