incase
A word case management library with too many features (just in case).
Table of Contents
About The Project
Incase is a library to help manage word case. It includes a class for abstracting away from case for easy comparison of words or conversion to any case. Incase also includes a flexible decorator for managing the case of keywords, inputs, and outputs from functions.
Currently, incase supports the following cases: caseless, camel, dromedary, medial, snake, pascal, initial_capitals, kebab, dash, upper_snake, uppercase, upper, lowercase, lower, title, alternating, sarcasm, original, word
If you are aware of another case, please open an issue so we can add it!
Getting Started
To get a local copy up and running follow these simple steps.
Installing with pip
pip install incase
For information about cloning and dev setup see: Contributing
Usage
CLI
You can leverage incase in shell scripts or other non-python contexts by calling it from the cli. Example:
The default output is snake case.
$ incase someCamel
some_camel
Use the --case option to set output case
$ incase someCamel --case lower
some camel
Caseless
Here is an example showing basic usage of the Caseless class.
from incase import Case, Caseless
# Instances of Caseless are strings
example = Caseless("example string")
print(isinstance(example, str))
# True
# By property
print(example.snake)
# example_string
# Or by subscript (string or Case)
print(example["camel"])
# exampleString
print(example[Case.UPPER_SNAKE])
# EXAMPLE_STRING
# Caseless ignore case when comparing to str
print(Caseless("some name") == "SOME_NAME")
# True
# Caseless hashes ignore case also
a_dict = {Caseless("This is a Key"): "this"}
print(a_dict[Caseless("thisIsAKey")])
# Caseless can also generate case coercion functions
make_camel = Caseless.factory("camel")
print(make_camel("snake_case"))
# snakeCase
ASGI Middleware Usage
This ASGI middleware, JSONCaseTranslatorMiddleware, translates JSON keys' case between snake_case and camelCase, easing communication between Python backends and JavaScript frontends. It handles JSON key case conversion for incoming and outgoing HTTP requests and responses.
Example:
from incase.middleware import JSONCaseTranslatorMiddleware
# Setup your FastAPI or Starlette application and then:
app.add_middleware(JSONCaseTranslatorMiddleware)
The middleware is ASGI compliant and should work as middleware with other frameworks but this has not been tested.
Updated Usage Example:
To effectively use JSONCaseTranslatorMiddleware in your FastAPI or Starlette application, it's recommended to use a custom response class (CamelJsonResponse) for outgoing data. This approach avoids the performance overhead associated with deserializing and re-serializing JSON in middleware.
Here is how you can set it up to use the custom response with FastAPI. Note the argument to FastAPI to set the default_response_class and also the argument to add_middleware to disable response handling.
from fastapi import FastAPI
from incase.middleware import JSONCaseTranslatorMiddleware, Request, CamelJsonResponse
app = FastAPI(default_response_class=CamelJsonResponse)
app.add_middleware(JSONCaseTranslatorMiddleware, handle_response=False)
@app.get("/")
def read_root():
return {"hello_there": "general kenobi"}
@app.post("/")
async def receive_data(request: Request):
json_data = await request.json()
print(json_data)
return {"Received": json_data}
The above app will return {"helloThere": "general kenobi"} at the root because the default_response_class will automatically change json keys to camel case. If you post the following json to the root ("/"): {"thisThing": 1} you will see {"Recieved": {"this_thing": 1}} because the middleware will intercept the incoming json and convert the keys to snake_case.
See also:
- https://www.starlette.io/middleware/#using-middleware
- https://fastapi.tiangolo.com/advanced/middleware/#advanced-middleware
Helper functions
case_modifier is a function for altering other functions. It can change the incoming case of parameter values, the case of the keywords provided, or the case of the function output.
from incase import Case, case_modifier
# Some functions you don't control
def external_function(expectsCamel, iterationCount):
# expects camelCase parameter names
for i in range(iterationCount):
print(expectsCamel)
# We'll use case_modifier. Now any keyword will be turned to camelCase
f = case_modifier(keywords_case=Case.CAMEL)(external_function)
f(expects_camel="this", iteration_count=1)
# this
# Here, we'll use case modifier as a function decorator
# to give a sarcastic twist to our output
@case_modifier(args_case="sarcasm")
def say_words(*args) -> None:
[print(word) for word in args]
say_words("It's all about", "the he said", "SHE SAID")
# It's aLl aBoUt
# ThE He sAi
# ShE SaId
Finally, incase is a powerful case coercion function.
from incase import incase
# It covers the basic case
print(incase("snake", "example text"))
# example_text
# But can also handle sequences
print(incase("upper", ["three", "word", "list"]))
# ['THREE', 'WORD', 'LIST']
print(incase("upper", ("three", "word", "tuple")))
# ('THREE', 'WORD', 'TUPLE')
# Even generators
generator = (word for word in ["some", "list"])
incased = incase("upper", generator)
print(incased)
# <generator object _incase_single.<locals>.<genexpr> at ...
print(list(incased))
# ['SOME', 'LIST']
# Or nested objects
nested = {
"first_key": ["some", "list"],
"second_key": {"another": "dict"},
"third_key": 1,
}
print(incase("upper", nested))
# {"first_key": ["SOME", "LIST"], "second_key": {"another": "DICT"}, "third_key": 1}
# Finally, it is possible to map case to objects
print(incase(["upper", "lower", "snake", "camel"], ["some_word"] * 4))
# ['SOME WORD', 'some word', 'some_word', 'someWord']
# By key
print(
incase(
{"first_key": "alternating", "second_key": "title"},
{"first_key": "some example", "second_key": "another example"},
)
)
# {'first_key': 'SoMe eXaMpLe', 'second_key': 'Another Example'}
print(incase({"bob": "upper"}, "bob"))
Planetary Defense Shield
Note that this is probably not a good idea. However, if you want to be able to easily access objects in globals() by a case other then the one they have, you can use the planetary defense shield.
from incase import planetary_defense_shield
THIS_IS_A_THING = "Some Value"
def badCamelCaseFunction():
print("hi")
# If you pass a case, instead of a dictionary, this will grab most of globals.
# Also note that the objects that will be cloned are whatever is in globals()
# at the time you pass it as the second argument, so call accordingly.
planetary_defense_shield(
{"THIS_IS_A_THING": "snake", "badCamelCaseFunction": "snake"}, globals()
)
print(this_is_a_thing)
# Some Value
bad_camel_case_function()
# hi
Contributing
Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature) - Add tests, we aim for 100% test coverage Using Coverage
- Commit your Changes (
git commit -m 'Add some AmazingFeature') - Push to the Branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Cloning / Development setup
- Clone the repo and install
git clone https://github.com/kajuberdut/incase.git cd incase pipenv install --dev
- Run tests
pipenv shell ward
For more about pipenv see: Pipenv Github
License
Distributed under the MIT License. See LICENSE for more information.
Contact
Patrick Shechet - patrick.shechet@gmail.com
Project Link: https://github.com/kajuberdut/incase
Metadata
Release files for incase 0.0.12
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| incase-0.0.12.tar.gz | 16.6 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| incase-0.0.12-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.6 kB
Release files / incase-0.0.12.tar.gz
| Download URL | incase-0.0.12.tar.gz |
|---|---|
| Size | 16.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
aef873f9ac6bb8f2af16f774296cf1fbf068f71083882285db148cb81bcb96cc
|
|
BLAKE2b-256 checksum How to use checksums |
bf98f8649b8314bd0783f824440b96f5f4fdb5dbce0f7554586ceef17e6482a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.11.1 CPython/3.10.12
|
Release files / incase-0.0.12-py3-none-any.whl
| Download URL | incase-0.0.12-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bcb2285e08f96adca4378cee839b201eefe9a61b3b6275ac42a813496efd6217
|
|
BLAKE2b-256 checksum How to use checksums |
7acbe178199d2bff7a3be4889e3747b1cf361c5deabaef047f627cc1cbc16871
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
pdm/2.11.1 CPython/3.10.12
|
Security for incase 0.0.12
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.