Strast is a versatile Python tool that transforms a string representation of a Python literal into the corresponding Python object, building upon the powerful ast module. The word "strast" also denotes "passion", reflecting our enthusiasm for data transformation.
- Transform string literals to Python objects seamlessly with the power of the
astmodule. - Flexible type checking to ensure data integrity.
- Supports extensibility through factory and class-based approaches.
To install Strast, use pip:
pip install strastimport straststrasts core function:
result = strast.c("{'a': 1, 'b': 2}", dict) # or strast.core.strast
print(result)
print(strast("{'a': 1, 'b': 2}", list))Output:
{'a': 1, 'b': 2}
TypeError: Expected <class 'list'>, got <class 'dict'> instead.
strast as a closure factory function:
strast = strast.f(dict, list, tuple) # or strast.factory.strast
print(strast("{'a': 1, 'b': 2}"))
print(strast("[1, 2, 3]"))
print(strast("('a', 'b', 'c')"))Output:
{'a': 1, 'b': 2}
[1, 2, 3]
('a', 'b', 'c')
strast as a class:
strast = strast.S(dict) # or strast.class_based.Strast
print(strast.transform("{'a': 1, 'b': 2}"))Output:
{'a': 1, 'b': 2}
See also documentation
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Strast is licensed under the MIT license.