Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
KOLANICH committed Sep 30, 2023
0 parents commit 6ff688f
Show file tree
Hide file tree
Showing 18 changed files with 2,583 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
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
1 change: 1 addition & 0 deletions .github/.templateMarker
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
KOLANICH/python_project_boilerplate.py
8 changes: 8 additions & 0 deletions .github/dependabot.yml
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"
15 changes: 15 additions & 0 deletions .github/workflows/CI.yml
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 }}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
__pycache__
*.pyc
*.pyo
/*.egg-info
/build
/dist
/.eggs
/monkeytype.sqlite3
/.coverage
*.py,cover
50 changes: 50 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
image: registry.gitlab.com/kolanich-subgroups/docker-images/fixed_python:latest
stages:
- build
- trigger
- test

variables:
DOCKER_DRIVER: overlay2
SAST_ANALYZER_IMAGE_TAG: latest
SAST_DISABLE_DIND: "true"

include:
- template: SAST.gitlab-ci.yml
#- template: DAST.gitlab-ci.yml
#- template: License-Management.gitlab-ci.yml
#- template: Container-Scanning.gitlab-ci.yml
#- template: Dependency-Scanning.gitlab-ci.yml
- template: Code-Quality.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`

cache:
paths:
- $PYTHONUSERBASE

script:
- python3 setup.py bdist_wheel
- mkdir wheels
- mv ./dist/*.whl ./wheels/rangeslicetools-0.CI-py3-none-any.whl
- pip3 install --user --upgrade ./wheels/rangeslicetools-0.CI-py3-none-any.whl
- coverage run --branch --source=rangeslicetools -m pytest --junitxml=./rspec.xml ./tests/tests.py
- coverage report -m || true
- coverage xml

artifacts:
paths:
- wheels
reports:
junit: rspec.xml
cobertura: ./coverage.xml
1 change: 1 addition & 0 deletions Code_Of_Conduct.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No codes of conduct!
4 changes: 4 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include UNLICENSE
include *.md
include tests
include .editorconfig
89 changes: 89 additions & 0 deletions ReadMe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
rangeslicetools.py [![Unlicensed work](https://raw.githubusercontent.com/unlicense/unlicense.org/master/static/favicon.png)](https://unlicense.org/)
==================
[wheel (GHA via nightly.link)](https://nightly.link/KOLANICH-libs/rangeslicetools.py/workflows/CI/master/rangeslicetools-0.CI-py3-none-any.whl)
[wheel (GitLab)](https://gitlab.com/KOLANICH/rangeslicetools.py/-/jobs/artifacts/master/raw/dist/rangeslicetools-0.CI-py3-none-any.whl?job=build)
[![GitLab Build Status](https://gitlab.com/KOLANICH/rangeslicetools.py/badges/master/pipeline.svg)](https://gitlab.com/KOLANICH/rangeslicetools.py/pipelines/master/latest)
![GitLab Coverage](https://gitlab.com/KOLANICH/rangeslicetools.py/badges/master/coverage.svg)
[![GitHub Actions CI](https://github.com/KOLANICH-libs/rangeslicetools.py/workflows/CI/badge.svg)](https://github.com/KOLANICH-libs/rangeslicetools.py/actions/)
[![Coveralls Coverage](https://img.shields.io/coveralls/KOLANICH/rangeslicetools.py.svg)](https://coveralls.io/r/KOLANICH/rangeslicetools.py)
![N∅ dependencies](https://shields.io/badge/-N%E2%88%85_deps!-0F0)
[![Code style: antiflash](https://img.shields.io/badge/code%20style-antiflash-FFF.svg)](https://github.com/KOLANICH-tools/antiflash.py)

This is a library to manipulate python `range` and `slice` objects. The objects of these classes have the same internal structure but a bit different semantics and set of available methods. Unfortunately these objects include no methods to manipulate them and unfortunately they cannot be subclassed.

So I have implemented a set of functions to manipulate these objects. Their names follow the following conventions:

* All these **functions** names begin from `s` which stands there for `slice`, even though they will work for ranges too.

* If a name ends with `_`, it is a generator, otherwise it returns a `list`.

**WARNING: FOR NEGATIVE-DIRECTED `slice`s/`range`s `step` is MANDATORY. It is BY DESIGN of python and we follow this convention too. Always set `step` for all the ranges if you may deal with negative-directed ones.**

For the info on usage see the docstrings and tests. And READ the source code, it is SMALL ENOUGH.

Features
--------
Notation and terms:

* For briefness we `r = range` and `s = slice`
* When we say `range`, it also works for a `slice` and in the opposite direction too.

Conventions:

* When we say `range` or `slice`, it usually works also for a sequence of them. See type annotations to check if a specific function supports sequences of ranges.
* There may be undefined behavior (UB):

* negative-directed ranges without negative `step` is always UB;
* non-integer numbers usage is always UB;;
* operations on the ranges having different `abs(step)`;
* empty ranges (ranges of zero length) produced **may** (but not guaranteed) be eliminated;
* operations on the ranges having opposite direction may be UB. It should be stated in docstrings if it is the case.

* basic operations

* type conversion:
* `sAny2Type(s(1, 10), r) -> r(1, 10)` `sAny2Type(r(1, 10), s) -> s(1, 10)`
* `range2slice` and `slice2range` do the same.

* get a length of a range `slen(s(2, 4)) -> 2`. For usual ranges just `len` works, but our func works also for slices and seqs.
* get direction (a director vector in fact) of a slice `sdir(r(10, 1, -2)) -> -1`
* reverse direction of a slice: `srev(r(0, 10)) -> r(9, -1, -1)`
* make 2 `slice`s of the same direction: `sdirect(r(25, 5, -5), s(1, 10)) -> slice(9, 0, -1)`
* make a slice positive-directed: `snormalize(r(25, 5, -5)) -> range(10, 30, 5)`

* checking conditions about ranges:

* check if one range is fully within another range `swithin(r(0, 10), r(1, 5)) -> true`
* check if one range is overlaps another range `soverlaps(r(0, 10), r(2, -6, -1)) -> true`

* splitting

* sprit a range at certain points: `ssplit(r(5, 13), (7, 8, 12)) -> [r(5, 7), r(7, 8), r(8, 12), r(12, 13)]`
* split a range into pieces of certain lengths `soffset_split(r(5, 13), (2, 3, 7)) -> [r(5, 7), r(7, 8), r(8, 12), r(12, 13)]`
* split a range into pieces of a certain length `schunks(r(5, 13), 3) -> [r(5, 8), r(8, 11), r(11, 13)]`
* split multiple sequences of ranges of the same total length into the chunks of equal length, in other words - align split points of all the sequence - see the docs for `salign` function.

* join/merge **adjacent** (non-overlapping!) ranges into one: `sjoin([r(0, 8), r(8, 9), r(9, 10), r(12, 15)]) -> [r(0, 10), r(12, 15)]`

* set operations

* compute a diff of 2 ranges: `sdiff`
* subtract 2 ranges: `ssub(r(1, 10), r(5, -10, -1)) -> [r(6, 10)]`
* union 2 ranges: `sunion(r(1, 10), r(7, 20)) -> [r(1, 20)]`

* intersections querying via a [range tree](https://en.wikipedia.org/wiki/Range_tree)
* remapping via a `SliceSequence`
* visualization


Examples
--------
* https://github.com/KOLANICH-libs/Endianness.py
* tests


Similar projects
----------------

* [intervaltree](https://github.com/chaimleib/intervaltree)
* [rangetree](https://github.com/nanobit/rangetree)
24 changes: 24 additions & 0 deletions UNLICENSE
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/>
39 changes: 39 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[build-system]
requires = ["setuptools>=61.2.0", "wheel", "setuptools_scm[toml]>=3.4.3"]
build-backend = "setuptools.build_meta"

[project]
name = "rangeslicetools"
authors = [{name = "KOLANICH"}]
description = "A library to manipulate ranges and slices"
keywords = ["range", "slice"]
license = {text = "Unlicense"}
classifiers = [
"Programming Language :: Python",
"Programming Language :: Python :: 3",
"Development Status :: 4 - Beta",
"Environment :: Other Environment",
"Intended Audience :: Developers",
"License :: Public Domain",
"Operating System :: OS Independent",
"Topic :: Software Development :: Libraries :: Python Modules",
]
requires-python = ">=3.4"
dynamic = ["version"]

[project.readme]
file = "ReadMe.md"
content-type = "text/markdown"

[project.urls]
Homepage = "https://github.com/KOLANICH-libs/rangeslicetools.py"
Download = "https://github.com/KOLANICH-libs/rangeslicetools.py/-/jobs/artifacts/master/raw/wheels/rangeslicetools-0.CI-py3-none-any.whl?job=build"
"Bug Tracker" = "https://github.com/KOLANICH-libs/rangeslicetools.py/issues"
"CI Pipeline & static analysis" = "https://github.com/KOLANICH-libs/rangeslicetools.py/pipelines/master/latest"
"libraries.io" = "https://libraries.io/github/KOLANICH-libs/rangeslicetools.py"

[tool.setuptools]
zip-safe = true
packages = ["rangeslicetools"]

[tool.setuptools_scm]
61 changes: 61 additions & 0 deletions rangeslicetools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import sys
import types
import typing
from functools import wraps

from . import diff, utils


def _createWrapped(f: typing.Callable) -> typing.Callable:
@wraps(f)
def f1(*args, **kwargs):
return tuple(f(*args, **kwargs))

f1.__annotations__["return"] = utils.SliceRangeListT
return f1


def _wrapModuleProp(module, k, v, _all_) -> None:
if k[0] != "_":
_all_.append(k)

if k[0] == "s" and k[-1] == "_":
if "return" not in v.__annotations__:
raise ValueError("Annotate the return type in " + v.__qualname__ + "!")

modName = k[:-1]
if v.__annotations__["return"] is module.SliceRangeSeqT and modName not in module.__dict__:
module.__dict__[modName] = _createWrapped(v)
_all_.append(modName)

module.__dict__[k] = v


def _wrap(module) -> None:
_all_ = getattr(module, "__all__", None)
if _all_ is None:
_all_ = []
for k, v in tuple(module.__dict__.items()):
_wrapModuleProp(module, k, v, _all_)
_all_.append(k)
else:
_all_ = list(_all_)
for k in list(_all_):
v = getattr(module, k)
_wrapModuleProp(module, k, v, _all_)
_all_ = tuple(sorted(_all_))

module.__all__ = tuple(_all_)

sys.modules[module.__name__] = module


_wrap(utils)
_wrap(diff)


# pylint: disable=wrong-import-position
from .utils import * # noqa
from .diff import * # noqa
from .tree import * # noqa
from .viz import * # noqa
Loading

0 comments on commit 6ff688f

Please sign in to comment.