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.

>>> s = AliasSpace(
>>>     {
>>>         "The Netherlands": ["NL", "Netherlands", "Holland"],
>>>         "The Hague": ["Den Haag", "'s-Gravenhage"],
>>>         "Amsterdam": ["Adam"],
>>>     },
>>>     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, when you have pandas installed, the aliases package will register accessors for series and dataframes. This allows you to easily enforce aliases in your pandas DataFrame. The following example was the original motivation for building this package:

>>> import pandas as pd
>>> df = pd.DataFrame(
>>>     {
>>>         "Country": ["NL", "Netherlands", "Belgium"],
>>>         "City": ["Den Haag", "amsterdam", "Brussel"],
>>>         "SomeData": [10, 11, 12],
>>>     }
>>> )
       Country       City  SomeData
0           NL   Den Haag        10
1  Netherlands  amsterdam        11
2      Belgium    Brussel        12
>>>
>>> df.Country.alias.representative(space=s)
0    The Netherlands
1    The Netherlands
2            Belgium
Name: Country, dtype: object
>>>
>>> df.alias.representative(space=s, missing=pd.NA)
           Country       City  SomeData
0  The Netherlands  The Hague        10
1  The Netherlands  Amsterdam        11
2             <NA>       <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.5.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

aliases-0.5.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aliases-0.5.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.15

File hashes

Hashes for aliases-0.5.0.tar.gz
Algorithm Hash digest
SHA256 9df4fb8ff841f1803479f994ce9f8dcf1932fe97cd3509d65bbbbc44cbb703bc
MD5 a764a9f5e94d2c1c4fe7e2df2c2e8680
BLAKE2b-256 3e427d845e61dc5847bfa6e5b95467426cf6b97ad2b4299eca2fea6900b04109

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for aliases-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 110ad63cee36cfd5879a85e3441696730ef0958ebf4bd08dd1dfcd7f4b034ec0
MD5 94ebebc25b0a1f944fa176f84a6d5bac
BLAKE2b-256 612e51baa9e111718961d61a5e3fb9791b95ee9fa18e0ad93f7d3951618638c4

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