Keeping track of aliases
Project description
A very small Python package for keeping track of aliases.
Installation
$ pip install aliases
Getting Started
Keeping track of aliases in your data can be annoying. This small packages provides three small classes than can help you in the bookkeeping associated to the occurrences of aliases in your data.
The AliasSpace objects keeps track of existing aliases. As input is accepts a dictionary where a string (the “preferred” form) points to a list of all its aliases. Using the str method on the space, we can transform regular strings into AliasAwareString objects.
>>> from aliases import AliasSpace
>>>
>>> s = AliasSpace(
>>> {"The Netherlands": ["NL", "Netherlands", "Holland"]},
>>> case_sensitive=False
>>> )
>>>
>>> s.str("nl")
<'nl' in AliasSpace>
The preferred form of an AliasAwareString is called its _representative_ (because it represents the equivalance class of the string under the equivalance relation of being aliases).
>>> s.str("nl").representative
'The Netherlands'
AliasAwareString objects with the same representative are considered equal and have the same hash.
>>> s.str("holland") == s.str("NL")
True
>>> data = {s.str("holland"): 12345}
>>> data[s.str("nl")]
12345
The example above already shows how alias aware strings can be used to store data without worrying to much about the different aliases around. However, it is still annoying to cast to an AliasAwareString every time manually. To solve this you can use the AliasAwareDict.
>>> data = s.dict(holland=12345)
>>> data['nl']
12345
Finally, the AliasAwareSpace object has a map method which can be used to find the representatives of a list of strings easily. The following example was the original motivation for building this package:
>>> import pandas as pd
>>> df = pd.DataFrame(
>>> {"Country": ["NL", "Netherlands", "Belgium"], "SomeData": [10, 11, 12]}
>>> )
>>> df
Country SomeData
0 NL 10
1 Netherlands 11
2 Belgium 12
>>> df.assign(Country=s.map(df.Country, return_list=True))
Country SomeData
0 The Netherlands 10
1 The Netherlands 11
2 Belgium 12
>>> df.assign(Country=s.map(df.Country, return_list=True, missing=pd.NA))
Country SomeData
0 The Netherlands 10
1 The Netherlands 11
2 <NA> 12
Documentation
Coming soon…
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 aliases-0.1.0.tar.gz
.
File metadata
- Download URL: aliases-0.1.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3139ea29430615d759649f8cfa5f12a1ad696156b0bee353d3786e766b6fcb3 |
|
MD5 | 58c36112ddb9e848125dc4a8881db15d |
|
BLAKE2b-256 | 21975feb3b035ed6910344e0a9604a713fb61cdde6f0a50b7256eef37b37088e |
File details
Details for the file aliases-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: aliases-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.14
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c8de3b5995337363253d9ec470a0c02885dc056d4c18d50cfabee41752718b4 |
|
MD5 | 415cde92be8ffdc56f2e5f8dd198748d |
|
BLAKE2b-256 | b7b056de5ba6cb799d5319db9a41cf665a9eaa64f2c77144fa19dc144ae8bf2f |