Created
April 10, 2018 00:32
-
-
Save jtratner/ff2eab3973fea0d8d07a1403a1b948ce to your computer and use it in GitHub Desktop.
Example of auto generating type stubs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
STRING_DEFAULT = 'string' | |
DATETIME_DEFAULT = datetime(2015, 1, 1) | |
INT_DEFAULT = 123 | |
def generate_typed_dicts(name, nested_dict): | |
full_context = [] | |
final_dict = [f"class {name}(TypedDict):"] | |
for k, v in nested_dict.items(): | |
if v == STRING_DEFAULT: | |
v = str | |
if v == DATETIME_DEFAULT: | |
v = datetime | |
if v == INT_DEFAULT: | |
v = int | |
if isinstance(v, dict): | |
klass = k[0].upper() + k[1:] | |
full_context.append(generate_typed_dicts(klass, v)) | |
final_dict.append(f" {k}: {klass}") | |
elif isinstance(v, str): | |
final_dict.append(f" {k}: str") | |
elif isinstance(v, list): | |
full_context.append(generate_typed_dicts(k, v[0])) | |
final_dict.append(f" {k}: List[{k}]") | |
elif isinstance(v, (int, float)): | |
final_dict.append(f" {k}: {type(v).__name__}") | |
# elif isinstance(v, str): # forward reference | |
# final_dict.append(f" {k}: '{k}'") | |
else: # class | |
final_dict.append(f" {k}: {v.__name__}") | |
full_context.append('\n'.join(final_dict)) | |
return '\n\n\n'.join(full_context) | |
print( | |
generate_typed_dicts( | |
'S3PutEvent', { | |
"Records": [{ | |
"eventVersion": "2.0", | |
"eventTime": "1970-01-01T00:00:00.000Z", | |
"requestParameters": { | |
"sourceIPAddress": "127.0.0.1" | |
}, | |
"s3": { | |
"configurationId": "testConfigRule", | |
"object": { | |
"eTag": "0123456789abcdef0123456789abcdef", | |
"sequencer": "0A1B2C3D4E5F678901", | |
"key": "HappyFace.jpg", | |
"size": 1024 | |
}, | |
"bucket": { | |
"arn": "", | |
"name": "sourcebucket", | |
"ownerIdentity": { | |
"principalId": "EXAMPLE" | |
} | |
}, | |
"s3SchemaVersion": "1.0" | |
}, | |
"responseElements": { | |
"x-amz-id-2": | |
"EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH", | |
"x-amz-request-id": | |
"EXAMPLE123456789" | |
}, | |
"awsRegion": "us-east-1", | |
"eventName": "ObjectCreated:Put", | |
"userIdentity": { | |
"principalId": "EXAMPLE" | |
}, | |
"eventSource": "aws:s3" | |
}] | |
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RequestParameters(TypedDict): | |
sourceIPAddress: str | |
class Object(TypedDict): | |
eTag: str | |
sequencer: str | |
key: str | |
size: int | |
class OwnerIdentity(TypedDict): | |
principalId: str | |
class Bucket(TypedDict): | |
arn: str | |
name: str | |
ownerIdentity: OwnerIdentity | |
class S3(TypedDict): | |
configurationId: str | |
object: Object | |
bucket: Bucket | |
s3SchemaVersion: str | |
# note this requires manual fix :( | |
class ResponseElements(TypedDict): | |
x-amz-id-2: str | |
x-amz-request-id: str | |
class UserIdentity(TypedDict): | |
principalId: str | |
class Records(TypedDict): | |
eventVersion: str | |
eventTime: str | |
requestParameters: RequestParameters | |
s3: S3 | |
responseElements: ResponseElements | |
awsRegion: str | |
eventName: str | |
userIdentity: UserIdentity | |
eventSource: str | |
class S3PutEvent(TypedDict): | |
Records: List[Records] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment