Python type code generator
Project description
py2ts
Mapping of Scalar Types
assert python_type_to_typescript(str) == "string"
assert python_type_to_typescript(int) == "number"
assert python_type_to_typescript(bool) == "boolean"
assert python_type_to_typescript(float) == "number"
assert python_type_to_typescript(datetime) == "string"
assert python_type_to_typescript(date) == "string"
assert python_type_to_typescript(Decimal) == "string"
assert python_type_to_typescript(dict) == "{}"
assert python_type_to_typescript(list) == "Array<any>"
Usage
Create a schema with Python dataclass:
class EnumFruit(Enum):
APPLE = "APPLE"
ORANGE = "ORANGE"
@dataclass
class NestedSchema:
string_field: str
nullable_datetime_field: Optional[datetime]
recursively_nested_field: Optional["NestedSchema"]
@dataclass
class ComplexSchema:
nullable_int_field: Optional[int]
nullable_decimal_field: Optional[int]
nullable_enum_field: Optional[EnumFruit]
nullable_nested_field: Optional[NestedSchema]
nested_list_field: List[NestedSchema]
blocks = []
for path, node in schemas2typescript([ComplexSchema]).items():
blocks.append(node.to_typescript())
print("\n".join(blocks))
The output is:
export enum EnumFruit {
APPLE = 'APPLE',
ORANGE = 'ORANGE'
}
export interface NestedSchema {
string_field: string;
nullable_datetime_field: string | null;
recursively_nested_field: NestedSchema | null;
}
export interface ComplexSchema {
nullable_int_field: number | null;
nullable_decimal_field: number | null;
nullable_enum_field: EnumFruit | null;
nullable_nested_field: NestedSchema | null;
nested_list_field: Array<NestedSchema>;
}
Generate service registry
Dependency: ripgrep
Generate boilerplate service registry code.
Usage
python -m py2ts.generate_service_registry > service_registery.py
How it works
The command assumes that any classes named XXXService should be included in the service registry. For example:
class TestService:
pass
will generate the following code:
# Generated code. DO NOT EDIT!
from dataclasses import dataclass
from tests.test_generate_service_registr import TestService
@dataclass
class ServiceRegistry:
test_service: TestService = TestService()
service_registry = ServiceRegistry()
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
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 py2ts-0.0.6.tar.gz.
File metadata
- Download URL: py2ts-0.0.6.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.1.0 requests-toolbelt/0.8.0 tqdm/4.46.0 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
24ca30c8950ea378bdd98ee523d72988c3bae8435dca8b9dd8eb0bd0670e9827
|
|
| MD5 |
44b54aa1e61e956cb420b074a0a3bb2e
|
|
| BLAKE2b-256 |
aa3879a2f62608ef9ce1b970d8fcaab3ca5fd34f8370886e2232d375a793f248
|
File details
Details for the file py2ts-0.0.6-py3-none-any.whl.
File metadata
- Download URL: py2ts-0.0.6-py3-none-any.whl
- Upload date:
- Size: 7.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.1.0 requests-toolbelt/0.8.0 tqdm/4.46.0 CPython/3.8.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb035f008f54edfc5343970f24f6fc83586d3c112e707acd25969045d7535019
|
|
| MD5 |
49bc65544eb27934d0ff1fd138f502b7
|
|
| BLAKE2b-256 |
5d80d9740128060db31694c342699531a63446e796e41da0b5e4c4d45a36c09c
|