Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ast
- class AnyOf(list):
- pass
- class ObjectSchema(dict):
- pass
- def is_valid(schema, obj):
- if isinstance(schema, ObjectSchema):
- if not isinstance(obj, schema.get("type", object)):
- return False
- for key, value in schema.items():
- if key == "type": continue
- if not (hasattr(obj, key) and is_valid(value, getattr(obj, key))):
- return False
- return True
- elif isinstance(schema, AnyOf):
- return any(is_valid(item, obj) for item in schema)
- elif isinstance(schema, list):
- return isinstance(obj, list) and (is_valid(item_schema, item) for item_schema, item in zip(schema, obj))
- elif isinstance(schema, str):
- return schema == obj
- else:
- raise Exception(f"Not implemented yet for schema {repr(schema)} of type {type(schema)}")
- valid_values = [
- "df.isna()",
- "df.notna()",
- "df.str.contains(ANY_STRING_LITERAL, na=False)"
- ]
- schema = ObjectSchema(
- type=ast.Expression,
- body=ObjectSchema(
- type=ast.Call,
- args = [],
- keywords = [],
- func = ObjectSchema(
- type=ast.Attribute,
- value = ObjectSchema(
- type=ast.Name,
- id="df"
- ),
- attr=AnyOf(["isna", "notna"])
- )
- )
- )
- test_cases = [
- "df.isna()",
- "df.notna()",
- "df.str.contains(ANY_STRING_LITERAL, na=False)"
- ]
- for i, s in enumerate(test_cases, 1):
- print(f"Test case {i}: {repr(s)}")
- node = ast.parse(s, mode="eval")
- print(f" valid: {is_valid(schema, node)}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement