-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f578817
commit 465f862
Showing
6 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
# Pydantic | ||
|
||
[]() | ||
[Pydantic](https://docs.pydantic.dev/latest/) is a data validation library for Python, schema validation and serialization are controlled by type annotations. | ||
|
||
Interesting constructs: | ||
|
||
* [BaseSettings](https://docs.pydantic.dev/latest/api/pydantic_settings/#pydantic_settings.BaseSettings) Base class for settings, allowing values to be overridden by environment variables. Attention import pydantic_settings and install pydantic-settings | ||
* [YamlConfigSettingsSource](https://docs.pydantic.dev/latest/api/pydantic_settings/#pydantic_settings.YamlConfigSettingsSource) get configuration from Yaml file |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import unittest | ||
from fct_to_tests import parse_event | ||
|
||
class TestFct(unittest.TestCase): | ||
|
||
def test_validate_event_as_dict(self): | ||
event = {} | ||
validate, evt = parse_event(event,"event",["purpose"]) | ||
assert validate == True | ||
assert "Please ensure you have purpose in the event" in str(evt) | ||
|
||
def test_validate_event_as_str(self): | ||
event = "" | ||
validate, evt = parse_event(event,"event",["purpose"]) | ||
assert validate == True | ||
assert "Please ensure you have purpose in the event" in str(evt) | ||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import json | ||
|
||
def parse_event(event, entity_name, requirements=[]): | ||
if isinstance(event, dict): | ||
# Event is already a dictionary | ||
#check if all requirements are met: | ||
for item in requirements: | ||
# logger.info(f"Requirement {item} in body passed is complete:: ") | ||
try: | ||
exists = event[str(item)] | ||
except: | ||
# logger.info(f"Found an exception on {item}:: ") | ||
return True, str(f"Please ensure you have {item} in the {entity_name}!") | ||
return True, event | ||
elif isinstance(event, str): | ||
try: | ||
# Attempt to parse string as JSON | ||
for item in requirements: | ||
try: | ||
exists = event[item] | ||
except: | ||
return True, f"Please ensure you have {item} in the {entity_name}!" | ||
event = json.loads(event) | ||
return True, event | ||
except json.JSONDecodeError: | ||
# Not a valid JSON string | ||
return False, None | ||
else: | ||
# Unsupported format | ||
return False, None |