A core utility suite written in Python
Project description
Python Core Utilities
A Fully-typed Core Utility Suite Written in Python
There should be one -- and preferably only one -- obvious way to do it.
Collection
An abstract container intended to store a collection of items in an arbitrary data structure.
If you were a list, a Collection would be her ex.
DictCollection
A dictionary-based collection utility class with integrated support for efficient key lookups.
from core.collection import DictCollection
class Country:
"""A class that represents countries of the world"""
def __init__(self, name: str, iso2: str, iso3: str):
"""Init Method"""
self.name = name
self.iso2 = iso2
self.iso3 = iso3
def __str__(self) -> str:
"""String Method"""
return self.name
# Initialize a countries DictCollection
countries = DictCollection[Country](keys=("iso2", "iso3"))
# Iterate over countries to create
for name, iso2, iso3 in (
("Cambodia", "KH", "KHM"),
("China", "CN", "CHN"),
("Fiji", "FJ", "FJI"),
("Guam", "GU", "GUM"),
("Singapore", "SG", "SGP"),
("Thailand", "TH", "THA"),
("United States", "US", "USA"),
):
# Initialize country instance
country = Country(name=name, iso2=iso2, iso3=iso3)
# Add country to countries collection
countries.add(country)
# Print countries
print(countries)
# <DictCollection: 7 [Cambodia, China, Fiji, Guam, Singapore, Thailand, United States]>
Retrieve items by any of their keys:
# Look up Thailand by ISO2
print(countries["TH"], repr(countries["TH"]))
# Thailand <__main__.Country object at 0x7f8d085e2560>
# Look up Thailand by ISO3
print(countries["THA"], repr(countries["THA"]))
# Thailand <__main__.Country object at 0x7f8d085e2560>
Keys are treated as synonymous with the items to which they are associated:
# Retrieve Thailand country instance
thailand = countries["THA"]
# Show that she is present in the countries collection
print(repr(thailand), "in countries:", thailand in countries)
# <__main__.Country object at 0x7f8d085e2560> in countries: True
# Show that this works in the same way with with just her ISO3 key
print("'THA' in countries:", "THA" in countries)
# 'THA' in countries: True
Naturally then, you may also remove items by any of their keys:
# Remove Thailand by ISO2
countries.remove("TH")
# Print countries
print(countries)
# <DictCollection: 6 [Cambodia, China, Fiji, Guam, Singapore, United States]>
# Remove Singapore by ISO3
countries.remove("SGP")
# Print countries
print(countries)
# <DictCollection: 5 [Cambodia, China, Fiji, Guam, United States]>
Don't be a dict, use a DictCollection.
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 Distribution
Built Distribution
File details
Details for the file python_core_utilities-0.2.3.tar.gz
.
File metadata
- Download URL: python_core_utilities-0.2.3.tar.gz
- Upload date:
- Size: 37.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.5.0-21-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b7b5e7ca7fc94d72fe451a41b8ae1577c2a4ad2db62d464408343f24499e657f |
|
MD5 | 425b642351ff0656b2f31b8b4e852bc9 |
|
BLAKE2b-256 | 0cebdcd50a4d960b9ad4deea5cd39acea4b4a9aed7aae22b37ba95ae99dcb2e0 |
File details
Details for the file python_core_utilities-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: python_core_utilities-0.2.3-py3-none-any.whl
- Upload date:
- Size: 60.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.6.1 CPython/3.10.12 Linux/6.5.0-21-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 652f7b38db5a5361ac543437e232a7048ecc97b038603c11c998c66ce89a029c |
|
MD5 | bc2ce7d2fe50155be8427fe6f585157e |
|
BLAKE2b-256 | 9e999417f556a01af7ddb8a7063e220e1dc105f095c237e854fea3e3352ea74d |