A Python library that uses ordinal numbers to work with lists.
Project description
Ordinal List
Ordinal List is a Python library that extends the functionality of Python's built-in list to use ordinal numbers for indexing. This allows users to access list elements using ordinal notations like '1st', '2nd', '3rd', etc., instead of zero-based indexing.
Features
- Easy to use interface similar to Python's native list.
- Supports negative indexing similar to Python lists.
- Raises clear and descriptive errors for invalid inputs.
Installation
To install the Ordinal List library, run the following command in your terminal:
pip install ordinal-list
This will download and install the latest version of Ordinal List from the Python Package Index (PyPI). After installation, you can import and use the library in your Python projects.
Examples
Here's how to get started with the Ordinal List library:
>>> from ordinal_list import OrdinalList
Create an OrdinalList with some fruits
>>> ordinal_list = OrdinalList(["apple", "banana", "cherry"])
Access elements using ordinal indices
>>> print(ordinal_list["1st"])
apple
>>> print(ordinal_list["2nd"])
banana
>>> print(ordinal_list["3rd"])
cherry
>>> print(ordinal_list["-1st"])
cherry
Negative ordinals are also supported, as shown in the example above.
If you try to access an element with an invalid ordinal, the library will raise a descriptive error:
>>> try:
>>> print(ordinal_list["0th"]) # Invalid ordinal, will raise an error
>>> except Exception as e:
>>> print(e)
You can also set items using ordinal indices:
>>> ordinal_list["1st"] = "strawberry"
>>> print(ordinal_list["1st"])
strawberry
And you can extend the list with more items:
>>> ordinal_list.extend(["kiwi", "mango"])
>>> print(ordinal_list["5th"])
mango
There is also a great helper method for programatically iterating an OrdinalList:
>>> from ordinal_list import ordinal_range
>>> ordinal_list = OrdinalList(["apple", "banana", "cherry", "date", "elderberry"])
>>> for ordinal in ordinal_range("1st", "5th"):
>>> print(ordinal_list[ordinal])
apple
banana
cherry
date
elderberry
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file ordinal_list-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: ordinal_list-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f8e0ffb4bbd9f6d6a78e9053a379b78ef3d6c4d189af4d158636d8556e3da36e |
|
MD5 | 14df387469676e869b54942b4b1440ae |
|
BLAKE2b-256 | baf53a691a3bb5b71c7678398887b12813213e05679d2d3291e94c6ba6bc25a2 |