KeySet with 4 classes to represent concepts of All, None, Some, and AllExceptSome
Project description
key_set
Requires Python 3.12+
Installation
pip install key-set
Overview
Lightweight, zero-dependency library implementing 4 KeySet types representing set theory concepts:
- All (𝕌): Universal set - contains everything
- None (∅): Empty set
- Some: Finite subset of elements
- AllExceptSome: Complement of a finite set
Operations like intersect, union, difference are algebraically correct. You can also check inclusion with includes(element) or use element in keyset.
Python port of KeySet in TypeScript and KeySet in Ruby
Limitations
- for now, only KeySet of strings
- no ComposedKeySet yet (see KeySet in TypeScript)
KeySetType enum
Enum that represents the 4 types of KeySets:
ALLrepresents the entirety of possible keys (𝕌)NONErepresents an empty set (∅)SOMErepresents a concrete set (A ⊂ 𝕌)ALL_EXCEPT_SOMErepresents the complementary of a set, all the elements except the given ones (A' = {x ∈ 𝕌 | x ∉ A}) _(see Complement in Wikipedia)*
Creation
Build your KeySets using the build functions
from key_set import build_all, build_none, build_some_or_none, build_all_except_some_or_all
build_all() # => returns a new instance of KeySetAll
build_none() # => returns a new instance of KeySetNone
build_some_or_none([]) # returns a new instance of KeySetNone
# returns a new instance of KeySetSome with keys {'a', 'b', 'c'} (in a unique set)
build_some_or_none({'a', 'c', 'b'})
build_some_or_none(['a', 'c', 'b', 'c'])
build_all_except_some_or_all([]) # returns a new instance of KeySetAll
# returns a new instance of KeySetAllExceptSome with keys {'a', 'b', 'c'} (in a unique set)
build_all_except_some_or_all({'a', 'c', 'b'})
build_all_except_some_or_all(['a', 'c', 'b', 'c'])
KeySet classes
Methods exposed:
key_set_type()
returns the KeySetType enum
elements()
returns the set with the elements. It will be blank for All and None.
represents_xxx() methods
represents_all: returns True if the KeySet is ALLrepresents_none: returns True if the KeySet is NONErepresents_some: returns True if the KeySet is SOMErepresents_all_except_some: returns True if the KeySet is ALL_EXCEPT_SOME
invert()
Returns a new KeySet that represents the inverse Set of this one.
ALL<->NONESOME<->ALL_EXCEPT_SOME
intersect(other)
Returns a new KeySet with the intersection (A ∩ B) of both Sets: a set that contains the elements included in both sets.
union(other)
Returns a new KeySet with the union (A ∪ B) of both Sets: a set that contains the elements in any of the sets.
difference(other)
Returns a new KeySet with the difference (A - B) of the Sets: a set that contains the elements of A that are not in B.
includes(element)
Returns True if the set that this KeySet represents contains the given element.
clone()
Returns a new KeySet with the same elements.
Hashability
All KeySet classes are hashable and can be used as dictionary keys or in sets:
from key_set import KeySetSome, KeySetAll
cache = {
KeySetAll(): "all data",
KeySetSome({'a', 'b'}): "partial data",
}
unique_keysets = {KeySetSome({'a'}), KeySetSome({'a'})} # contains 1 element
len() support
KeySetNone, KeySetSome, and KeySetAllExceptSome support len():
len(KeySetNone()) # 0
len(KeySetSome({'a', 'b'})) # 2
len(KeySetAllExceptSome({'a', 'b'})) # 2 (number of excluded elements)
KeySetAll represents an infinite set, so by default len() raises TypeError:
len(KeySetAll()) # raises TypeError
Compatibility mode for KeySetAll
If you need len() to work on all KeySet types (e.g., for code that expects all objects to support len()), you can enable compatibility mode:
from key_set import KeySetAll
KeySetAll.enable_compat_len(True)
len(KeySetAll()) # returns sys.maxsize
KeySetAll.enable_compat_len(False) # restore default behavior
len(KeySetAll()) # raises TypeError again
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file key_set-2.0.0.tar.gz.
File metadata
- Download URL: key_set-2.0.0.tar.gz
- Upload date:
- Size: 68.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61a4ec07ad43246e7dcf6c2f5a97e81b761c28340b51308f04d1c2f4931b749a
|
|
| MD5 |
4dd47eccb78d3cbe42282b91cdbcfe16
|
|
| BLAKE2b-256 |
28deecaec05ec81ab17ffa0d45de5d4a33e7205fb809f7633d411d5e90aeec5f
|
Provenance
The following attestation bundles were made for key_set-2.0.0.tar.gz:
Publisher:
main.yml on eturino/key_set.py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
key_set-2.0.0.tar.gz -
Subject digest:
61a4ec07ad43246e7dcf6c2f5a97e81b761c28340b51308f04d1c2f4931b749a - Sigstore transparency entry: 831611140
- Sigstore integration time:
-
Permalink:
eturino/key_set.py@9ac8021944d4a10b1625441018126073efe44f05 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/eturino
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@9ac8021944d4a10b1625441018126073efe44f05 -
Trigger Event:
push
-
Statement type:
File details
Details for the file key_set-2.0.0-py3-none-any.whl.
File metadata
- Download URL: key_set-2.0.0-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4894c85b0e19c344d3b23623877391cc040518c7f9d12493127280b2c88eccfb
|
|
| MD5 |
166b0efdd0b16aee2c40d2de30f1c173
|
|
| BLAKE2b-256 |
88b3f33494d5a44a8d623b584825bf9d51026e9ed246d0054992edae4e432024
|
Provenance
The following attestation bundles were made for key_set-2.0.0-py3-none-any.whl:
Publisher:
main.yml on eturino/key_set.py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
key_set-2.0.0-py3-none-any.whl -
Subject digest:
4894c85b0e19c344d3b23623877391cc040518c7f9d12493127280b2c88eccfb - Sigstore transparency entry: 831611158
- Sigstore integration time:
-
Permalink:
eturino/key_set.py@9ac8021944d4a10b1625441018126073efe44f05 -
Branch / Tag:
refs/tags/v2.0.0 - Owner: https://github.com/eturino
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@9ac8021944d4a10b1625441018126073efe44f05 -
Trigger Event:
push
-
Statement type: