Mergable range object for use in Pydantic classes
Project description
Arranges
Range string fields for Pydantic BaseModels
I needed a way to parse batches of byte, row and line and other object ranges
in my merge-files
app, in a way that I can just drop it in as a string field
type. The reason for this is so the machine-generated command line help is
flat and readable by humans.
It it kinda grew into a monster so I've split it out into this separate package. The main feature is a pair of classes that can represent ranges:
Segment
is a class that can be treated like aset
and its constructor is compatible withrange
andslice
. It is derived fromstr
so serializes without custom JSON encoders. It is immutable, hashable and has a stable string representation.Ranges
is an orderedtuple
ofSegment
s. It is also immutable and derived fromstr
so serializes without custom JSON encoders like the above. It can be constructed from comma-separated Python-style slice notation strings (e.g."1:10, 20:"
,"0x00:0xff
and":"
), integers,slice
s,range
s, integers and (nested) iterables of the above.- An
inf
singleton that is afloat
with a value ofmath.inf
but has an__index__
that returnssys.maxsize
and compares equal to infinity andmaxsize
, and its string representation is"inf"
.
The range classes are designed to be used as fields in Pydantic BaseModel
s,
but they can be used anywhere you need a range. They are not designed with
speed in mind, and comparisons usually use the canonical string form by
converting other things into Ranges
objects.
Constraints
I made it to select lines or bytes in a stream of data, so it:
- only supports
int
s; - does not allow negative indices, the minimum is 0 and the maximum is unbounded;
- it's compatible with
range
andslice
, butstep
is fixed to1
. If you pass something with a step into its constructor it'll be converted to a list ofint
s (range(0, 10, 2)
becomes"0,2,4,6,8"
); - does not support duplicate ranges. Ranges are merged together as they are
added to the
Ranges
object; - it is unpydantic in that its constructors are duck-typed, which is what I need; and
- it violates the Zen of Python by having multiple ways to do the same thing, but it's also useful.
- Currently the interface is unstable, so lock the exact version in if you don't want breaking changes.
Installation
pip install arranges
if you want to use it. You'll need Python 3.10 or
above.
Dev setup
To add features etc you'll ideally need git
, make
, bash
and something
with a debugger. Config for Visual Studio Code is included.
Clone the repo and make dev
to make the venv, install dependencies, then
code .
to open the project up in the venv with tests and debugging and all
that jazz.
Type make help
to see the other options, or run the one-liner scripts in the
./build
dir if you want to run steps without all that fancy caching nonsense.
Usage
- RTFM
- Read the tests, which have full coverage.
- Read the pydocs
License
Free as in freedom from legalese; the WTFPL with a warranty clause.
Political note: I don't want to live in a world where lawyers tell me how to speak. If you don't trust me enough to use the WTFPL then you shouldn't be running my code in the first place.
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
Built Distribution
File details
Details for the file arranges-0.1.0.tar.gz
.
File metadata
- Download URL: arranges-0.1.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37716ee53a003aa7758f1066e0345a7940aacfc63af6d906f4206058c52b0f1e |
|
MD5 | 5bbc902efebefbab669051cf9dddb314 |
|
BLAKE2b-256 | f7319e8fe4168a9c42c0efa13a4e1bbe40cfe54f74c430cdc1677a3f82d27c8c |
File details
Details for the file arranges-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: arranges-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 657dcefab4a13c0faef16367c217f9748432060743b7aae74e7989e7a1aff4c0 |
|
MD5 | 3d2e8a74d5c639d0e7a8b7c9cbe0a26b |
|
BLAKE2b-256 | 86f8275c45c7970a799aa1a99ffde552bfc5be33198fe01c46be6891e0ee0444 |