Functionality
tocase leverages the regex library to convert your strings into any case.
Setup
To install the package run the following command:
pip install tocase
Once installed, import the for_strings if you want to use the basic API to recase strings. Import the for_pandas modules to use the pandas API to recase column names and column values.
import tocase.tocase.for_strings.ToCase as ToCase
import tocase.tocase.for_pandas
Usage
Camel
It is a naming convention where the first letter in compound words is capitalized, except for the first one.
# Example with simple string
Tocase("camel-case").camel() # ==> camelCase
Tocase("camel case").camel() # ==> camelCase
Constant
It is a naming convention where all letters in compound words are capitalized. Words are joined with an underscore.
# Example with simple string
Tocase("Constant-case").constant() # ==> CONSTANT_CASE
Tocase("constant Case").constant() # ==> CONSTANT_CASE
Dot
It is a naming convention where all letters in compound words are lowercased. Words are joined with a dot.
# Example with simple string
Tocase("Dot-case").dot() # ==> dot.case
Tocase("dot Case").dot() # ==> dot.case
Header
It is a naming convention where the first letter in compound words is capitalized. Words are joined by a dash.
# Example with simple string
Tocase("Header-case").header() # ==> Header-Case
Tocase("header Case").header() # ==> Header-Case
Kebab
It is a naming convention where all letters in compound words are lowercased. Words are joined by a dash.
# Example with simple string
Tocase("Kebab-case").kebab() # ==> kebab-case
Tocase("kebab Case").kebab() # ==> kebab-case
Pascal
It is a naming convention where the first letter in compound words is capitalized.
# Example with simple string
Tocase("Pascal-case").pascal() # ==> PascalCase
Tocase("pascal Case").pascal() # ==> PascalCase
Snake
It is a naming convention where all letters in compound words are lowercased. Words are joined by an underscore.
# Example with simple string
Tocase("Snake-case").snake() # ==> snake_case
Tocase("snake Case").snake() # ==> snake_case
Title
It is a naming convention where the first letter in compound words is capitalized. Words are separated by a space.
# Example with simple string
Tocase("Title-case").title() # ==> "Title Case"
Tocase("title Case").title() # ==> "Title Case"
With pandas DataFrames
You can work with pandas DataFrame to recase columns names or column values. See the following examples with fake data.
columns = ["first name", "last name", "age", "family doctor"]
values = [
["Jules", "Otti", 35, "Dr James Porter"],
["Marie", "Curie", 22, "Dr Vicky Laporte"],
["Marc", "El Bichon", 35, "Dr Hyde Frank"]
]
data = pd.DataFrame(data=values, columns=columns)
To recase columns names, use the col DataFrame accessor and the appropriate recasing function described above.
print(data)
"""
first name last name age family doctor
0 Jules Otti 35 DrJamesPorter
1 Marie Curie 22 DrVickyLaporte
2 Marc ElBichon 35 DrHydeFrank
"""
print(data.col.constant())
"""
FIRST_NAME LAST_NAME AGE FAMILY_DOCTOR
0 Jules Otti 35 DrJamesPorter
1 Marie Curie 22 DrVickyLaporte
2 Marc ElBichon 35 DrHydeFrank
"""
To recase columns values, use the val DataFrame accessor, the appropriate recasing function described above with a list of the columns to be processed.
columns_to_process = ["first name", "last name", "family doctor"]
print(data.val.constant(columns_to_process))
"""
first name last name age family doctor
0 JULES OTTI 35 DR_JAMES_PORTER
1 MARIE CURIE 22 DR_VICKY_LAPORTE
2 MARC EL_BICHON 35 DR_HYDE_FRANK
"""
For developers
Clone or download the repository on your machine. If you have poetry installed just run the following command to restore the working environment:
poetry install
If you don't have poetry you can use pip and the requirements.txt file:
pip install -r requirements.txt
To run tests, stay at the root of the directory and run:
pytest -v
All contributions are more than welcome. So feel free to to make a PR.
Author
Faouzi Braza
Metadata
Release files for tocase 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tocase-1.0.0.tar.gz | 6.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tocase-1.0.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.0 kB
Release files / tocase-1.0.0.tar.gz
| Download URL | tocase-1.0.0.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4b4b8e93ed55a830f05574ed9c6cb5b626d037129c2fa3548f47e78ffb316272
|
|
BLAKE2b-256 checksum How to use checksums |
0c877fe5fab77047993f77c31e35fb5836933af7d435fe128fde6fd1c0c15fe9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.8 CPython/3.7.10 Linux/5.11.0-7620-generic
|
Release files / tocase-1.0.0-py3-none-any.whl
| Download URL | tocase-1.0.0-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
61d62e17ac47e73b52dc76ddd6a5cf6f59a71d39e45e0ad3331d5f71a5055976
|
|
BLAKE2b-256 checksum How to use checksums |
cc73187395a742afb50f2fdfbf0690f13df4a9f1d165e357d692aeb9ce3d57ec
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.8 CPython/3.7.10 Linux/5.11.0-7620-generic
|