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
import pandas as pd | |
from datetime import datetime | |
from pytz import UTC, timezone | |
ET = timezone('US/Eastern') | |
s = pd.Series([datetime.now(UTC)]) # This is the same as a column in your dataframe. | |
def convert_datetime(dt: datetime) -> datetime: | |
"""Convert a localized datetime to Eastern time.""" |
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
{ | |
"$schema": "http://json-schema.org/draft-07/schema#", | |
"$id": "http://foo.qwerty/some/schema#", | |
"title": "Foo Schema", | |
"type": "object", | |
"properties": { | |
"title": { | |
"type": "string" | |
}, | |
"awesome": { |
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 APIv4(API): | |
"""JSON Schema draft 4.""" | |
... | |
def object(self, ctx: AnalyzeTypeContext, schema: Dict[str, Any], | |
outer: bool = False, **kwargs) -> Type: | |
"""Generate an annotation for an object, usually a TypedDict.""" | |
properties = schema.get('properties') |
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
import os | |
import json | |
class JSONSchemaPlugin(Plugin): | |
"""Provides support for the JSON Schema as TypedDict.""" | |
JSONSchema = 'jsonschema_typed.types.JSONSchema' | |
JSONSchemaBase = 'jsonschema_typed.types.JSONSchemaBase' |
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 JSONSchemaPlugin(Plugin): | |
"""Provides support for the JSON Schema as TypedDict.""" | |
# I put the placeholder class in a package called `jsonschema_typed`. | |
JSONSchema = 'jsonschema_typed.types.JSONSchema' | |
def get_type_analyze_hook(self, fullname: str) -> Optional[Callable]: | |
"""Produce an analyzer callback if a JSONSchema annotation is found.""" | |
if fullname == self.JSONSchema: | |
def callback(ctx: AnalyzeTypeContext) -> TypedDictType: |
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 JSONSchema(dict): | |
"""Placeholder for JSON schema TypedDict.""" |
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 ThePlugin: | |
"""My custom plugin for mypy.""" | |
def get_type_analyze_hook(self, fullname: str) -> Optional[Callable[[AnalyzeTypeContext], Type]]: | |
"""Customize analysis of specific types.""" | |
if fullname == 'somemodule.SomeName': | |
return analyze_some_name | |
return None # mypy will just move on without us. | |
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
data: JSONSchema['path/to/schema.json'] = json.loads(raw_data) | |
data['apropertyintheschema'] # OK | |
data['notintheschema'] # error: TypedDict "FooSchema" has no key 'notintheschema' |
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
{ | |
"$schema": "http://json-schema.org/draft-07/schema#", | |
"$id": "http://foo.qwerty/some/schema#", | |
"title": "A Resource", | |
"type": "object", | |
"properties": { | |
"thiskeyhere": { | |
"type": "string" | |
}, | |
"aninteger": { |
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 astr(str): | |
def __or__(self, other): | |
return astr(f'({self} | {other})') | |
def __and__(self, other): | |
return astr(f'({self} & {other})') | |
def __invert__(self): | |
return astr(f'~{self}') | |
def _grouped_terms_to_q(term_pair: tuple): |
NewerOlder