Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jbcodeforce committed Jun 3, 2024
2 parents e4dcfb6 + 465f862 commit 7eea271
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 5 additions & 0 deletions docs/fastapi/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
8 changes: 7 additions & 1 deletion docs/python/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion docs/python/pydantic.md
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
19 changes: 19 additions & 0 deletions testing/Test_fct_to_test.py
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()
30 changes: 30 additions & 0 deletions testing/fct_to_tests.py
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

0 comments on commit 7eea271

Please sign in to comment.