Skip to main content

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 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 a set and its constructor is compatible with range and slice. It is derived from str so is easy to compare and serializes nicely. It is immutable, hashable and has a stable string representation.
  • 🏔️ Ranges is an ordered tuple of Segments. It is also immutable and derived from str like the above. It can be constructed from comma-separated Python-style slice notation strings (e.g. "1:10, 20:", "0x00:0xff and ":"), integers, slices, ranges, integers and (nested) iterables of the above.
  • ♾️ An inf singleton that is a float with a value of math.inf but has an __index__ that returns sys.maxsize and compares equal to infinity and maxsize, and its string representation is "inf".
  • 📕 A Dict that is keyed by Ranges and holds one per unique value.

The Ranges class is designed to be used as fields in Pydantic BaseModels, but 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. Their preferred pronoun is they/them.

📦 Installation

pip install arranges if you want to use them. You'll need Python 3.10 or above.

📖 Docs

See the tests for executable documentation

🔗 Links

⚠️ Constraints

I made them to select lines or bytes in a stream of data, so they:

  • 🔢 only support ints;
  • ≮ do not allow negative indices, the minimum is 0 and the maximum is unbounded;
  • ✅ are compatible with range and slice, but step is fixed to 1. If you pass something with a step into its constructor it'll be converted to a list of ints (range(0, 10, 2) becomes "0,2,4,6,8");
  • ∪ do not support duplicate ranges. Ranges are merged together as they are added to the Ranges object;
  • 🐍 they are unpydantic in that its constructors are duck-typed, which is what I need;
  • ☣️ they violate the Zen of Python by having multiple ways to do the same thing, but I found that useful; and
  • ⚠️ Currently the interface is unstable, so lock the exact version in if you don't want breaking changes.

👨‍💻 Hacking

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.

⚖️ License

Free as in freedom from legalese; the WTFPL with a warranty clause.

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

arranges-0.2.2.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

arranges-0.2.2-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file arranges-0.2.2.tar.gz.

File metadata

  • Download URL: arranges-0.2.2.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for arranges-0.2.2.tar.gz
Algorithm Hash digest
SHA256 5ec936749c64e50068728d7d2f3d6dffb18c3c541e2e78f58648773d5c249a5e
MD5 0ee607574e6f8a1e07f2b81c06846670
BLAKE2b-256 09016c3a68f527e72e3a03ef99c286d933844a34064cebf8f7c2c65dca9bedbb

See more details on using hashes here.

File details

Details for the file arranges-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: arranges-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.11

File hashes

Hashes for arranges-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 734fdeeae0b328a4855ec619a8596bbd94540e25909550ca2e0c021de52ee8d5
MD5 7c481d28829ffcd6dd64ef7f0b5413ff
BLAKE2b-256 87fec843174272561de2c49a6d7707311316c001e23860634cae95e3426d03bd

See more details on using hashes here.

Supported by

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