Convert JSON keys to callable Python methods
Project description
JSON to Func
A Python library that converts JSON keys to callable methods on a class.
Installation
pip install json-to-func
Usage
Basic Usage
from json_to_func import JsonToFunc, load
# Load from a JSON file
translator = JsonToFunc("messages.json")
# Or use the convenience function
translator = load("messages.json")
# Or load from a dictionary
translator = JsonToFunc({
"hello": "Hello, World!",
"greeting": "Hello, %s!"
})
Calling Methods
JSON keys are automatically transformed into valid Python method names:
# Simple key
translator.hello() # Returns: "Hello, World!"
# With placeholder
translator.greeting("John") # Returns: "Hello, John!"
Key Transformation Rules
| Original Key | Method Name |
|---|---|
tomans |
tomans() |
field-required |
field_required() |
hint:title |
hint__title() |
hint-bar:text:message |
hint_bar__text__message() |
Rules:
- Dashes (
-) become single underscores (_) - Colons (
:) become double underscores (__) - Other invalid characters become underscores
- Keys starting with numbers get a leading underscore
Format String Support
The library supports %s placeholders in values:
data = {
"field-required": "%s is required",
"range-error": "Value must be between %s and %s"
}
translator = JsonToFunc(data)
translator.field_required("Email") # "Email is required"
translator.range_error("1", "10") # "Value must be between 1 and 10"
Alternative Access Methods
# Get by original key name
translator.get("field-required", "Email")
# Get by transformed function name
translator.get_by_func_name("field_required", "Email")
# With default value
translator.get("nonexistent", default="fallback")
Utility Methods
# List all original keys
translator.keys() # ["hello", "field-required", ...]
# List all method names
translator.func_names() # ["hello", "field_required", ...]
# Check if key exists
"hello" in translator # True
"field_required" in translator # True
# Get count
len(translator) # Number of entries
Example with i18n/Translation Files
{
"tomans": "تومان",
"field-required": "%s الزامی است",
"entity-not-exists": "چنین %s وجود ندارد",
"hint-bar:title:welcome": "خوش آمدید"
}
from json_to_func import JsonToFunc
messages = JsonToFunc("fa.json")
print(messages.tomans()) # تومان
print(messages.field_required("ایمیل")) # ایمیل الزامی است
print(messages.entity_not_exists("کاربر")) # چنین کاربر وجود ندارد
print(messages.hint_bar__title__welcome()) # خوش آمدید
License
MIT
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
json_to_func-0.1.0.tar.gz
(6.7 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file json_to_func-0.1.0.tar.gz.
File metadata
- Download URL: json_to_func-0.1.0.tar.gz
- Upload date:
- Size: 6.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8056432e97709771338184712e78d02c602f699356194dd3cf898b33e3fe1d4
|
|
| MD5 |
d9d9b96651a2bd9f4bdbb9e75ea531cd
|
|
| BLAKE2b-256 |
a3e75f54047fc1a1c657d9fa6a7b3c14862ad8f3b38fc1333c0c83caba32bde8
|
File details
Details for the file json_to_func-0.1.0-py3-none-any.whl.
File metadata
- Download URL: json_to_func-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11c4452c3da50bfb55d0b9b0f315fad63a6a3adb8e3c70accfce87397daa0465
|
|
| MD5 |
d9d460613206822dd22363ac911ee03c
|
|
| BLAKE2b-256 |
412512ec71dac9c6589260a58e7d32d9ba7a88836378feff56f820792bced4b8
|