diff --git a/.vscode/launch.json b/.vscode/launch.json index 306f58e..0058d2f 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,7 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { "name": "Python: Current File", "type": "python", diff --git a/docs/fastapi/index.md b/docs/fastapi/index.md index 473529c..2cf1d0f 100644 --- a/docs/fastapi/index.md +++ b/docs/fastapi/index.md @@ -2,8 +2,13 @@ [FastAPI](https://fastapi.tiangolo.com/) helps to build backend REST api in python. + ## Boilerplate +* Create a main with health and metrics routes +* Add any specific routes for a business entities in its own router +* Add dependencies to initialize singletons, reference as part of the dependencies + ```python ``` diff --git a/docs/python/faq.md b/docs/python/faq.md index c320281..7997e2d 100644 --- a/docs/python/faq.md +++ b/docs/python/faq.md @@ -2,7 +2,8 @@ ## Why pipenv -pipenv resolves the problem of dependencies management, that is not perfectly done in the requirements.txt, which leads to underterministic build process. Given the same input (the requirements.txt file), pip doesn’t always produce the same environment. `pip freeze` helps to freeze the dependencies and update your requirements.txt. But any dependency change needs to be done manually, and you need to track the dependent package version, for bug fix, or mandatory security fixes. +pipenv resolves the problem of dependencies management, that is not perfectly done in the requirements.txt, which leads to under deterministic build process. Given the same input (the requirements.txt file), pip doesn’t always produce the same environment. `pip freeze` helps to freeze the dependencies and update the + requirements.txt. But any dependency change needs to be done manually, and you need to track the dependent package version, for bug fix, or mandatory security fixes. A second problem is the system wide repository used by pip. When developing multiple different projects in parallele that could become a real issue. `pipenv` use a per project environment. pipenv acts as pip + virtual environment. It uses Pipfile to replace requirements.txt and pipfile.lock for determnistic build. See [this guide](https://realpython.com/pipenv-guide/) for command examples. @@ -66,6 +67,11 @@ AWS_ACCESS_KEY_ID=os.environ.get("AWS_ACCESS_KEY_ID") AWS_SECRET_ACCESS_KEY=os.environ.get("AWS_SECRET_ACCESS_KEY") ``` +???- question "How to assess the type of an object?" + '''python + type(x) == str + ''' + ## List content of folder ```python diff --git a/docs/python/pydantic.md b/docs/python/pydantic.md index 8a4e571..738f405 100644 --- a/docs/python/pydantic.md +++ b/docs/python/pydantic.md @@ -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 \ No newline at end of file diff --git a/testing/Test_fct_to_test.py b/testing/Test_fct_to_test.py new file mode 100644 index 0000000..5a56d7a --- /dev/null +++ b/testing/Test_fct_to_test.py @@ -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() \ No newline at end of file diff --git a/testing/fct_to_tests.py b/testing/fct_to_tests.py new file mode 100644 index 0000000..e78129c --- /dev/null +++ b/testing/fct_to_tests.py @@ -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 \ No newline at end of file