Skip to main content

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 with 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 too 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. This object can be created using the dict method on the space.

>>> 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aliases-0.3.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

aliases-0.3.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file aliases-0.3.0.tar.gz.

File metadata

  • Download URL: aliases-0.3.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

Hashes for aliases-0.3.0.tar.gz
Algorithm Hash digest
SHA256 3137b3b6b2e75fe1d6002a6a68a4ca863a056a3015f8de29f672977ff896b906
MD5 10a82a9c5fb14e1e915ed8aa5deaa0b9
BLAKE2b-256 06f8d95bf464d813dc86e0ec26429bd6afa79e83f82efa629d8835ace60dbec8

See more details on using hashes here.

File details

Details for the file aliases-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: aliases-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.14

File hashes

Hashes for aliases-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2bff3eafd89ceef520ac3bdf59590ef6ab96a13d4a08e901e7a02e37db149b21
MD5 bf377c8eb4ff0255d44c9d8f9c2066c2
BLAKE2b-256 e21f9b881acc6bd5145217a103c39d7b5049d9d48aaf5c3ff1825ea0e63ac57b

See more details on using hashes here.

Supported by

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