-
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.
Merge pull request #2 from ramsunvtech/main
tech(base): add logging, typing, packaging cython, garbage collection, docker run command
- Loading branch information
Showing
9 changed files
with
117 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,34 @@ | ||
# CobraPy | ||
Suggests agility and power, suitable for a versatile Python project. | ||
|
||
## Installation Command | ||
|
||
``` | ||
pip install -r requirements.txt | ||
``` | ||
|
||
## Development Installation Command | ||
|
||
``` | ||
pip install -r dev-requirements.txt | ||
``` | ||
|
||
## Build and Run | ||
|
||
``` | ||
docker build -t myapp . | ||
docker run -p 8000:8000 myapp | ||
docker build -t math_api . | ||
docker run -p 8000:8000 math_api | ||
``` | ||
|
||
## Static Binaries with PyInstaller | ||
|
||
``` | ||
pyinstaller --onefile main.py --name math_api | ||
mv dist/math_api static/ | ||
``` | ||
|
||
## Packaging | ||
|
||
``` | ||
python setup.py build_ext --inplace | ||
``` |
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,13 @@ | ||
import asyncio | ||
from math_operations import add | ||
|
||
async def perform_addition(a, b): | ||
result = add(a, b) | ||
print(f"Addition Result: {result}") | ||
|
||
async def main(): | ||
tasks = [perform_addition(i, i+1) for i in range(10)] | ||
await asyncio.gather(*tasks) | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(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,3 @@ | ||
pyinstaller | ||
flake8 | ||
pytest |
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,13 @@ | ||
import gc | ||
|
||
def create_garbage(): | ||
garbage = [{} for _ in range(100000)] | ||
return garbage | ||
|
||
def clean_up(): | ||
collected = gc.collect() | ||
print(f"Garbage collector: collected {collected} objects.") | ||
|
||
if __name__ == "__main__": | ||
create_garbage() | ||
clean_up() |
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,27 @@ | ||
from fastapi import FastAPI, HTTPException | ||
from math_operations import add, subtract, multiply, divide | ||
|
||
app = FastAPI() | ||
|
||
@app.get("/") | ||
def root(): | ||
return {"message": "Welcome to the Math API"} | ||
|
||
@app.post("/add") | ||
def api_add(a: float, b: float): | ||
return {"result": add(a, b)} | ||
|
||
@app.post("/subtract") | ||
def api_subtract(a: float, b: float): | ||
return {"result": subtract(a, b)} | ||
|
||
@app.post("/multiply") | ||
def api_multiply(a: float, b: float): | ||
return {"result": multiply(a, b)} | ||
|
||
@app.post("/divide") | ||
def api_divide(a: float, b: float): | ||
try: | ||
return {"result": divide(a, b)} | ||
except ValueError as e: | ||
raise HTTPException(status_code=400, detail=str(e)) |
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,22 @@ | ||
import logging | ||
|
||
logging.basicConfig(filename="logs/app.log", level=logging.INFO) | ||
|
||
def add(a: float, b: float) -> float: | ||
logging.info(f"Adding {a} + {b}") | ||
return a + b | ||
|
||
def subtract(a: float, b: float) -> float: | ||
logging.info(f"Subtracting {a} - {b}") | ||
return a - b | ||
|
||
def multiply(a: float, b: float) -> float: | ||
logging.info(f"Multiplying {a} * {b}") | ||
return a * b | ||
|
||
def divide(a: float, b: float) -> float: | ||
if b == 0: | ||
logging.error("Attempted division by zero") | ||
raise ValueError("Cannot divide by zero.") | ||
logging.info(f"Dividing {a} / {b}") | ||
return a / b |
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,5 @@ | ||
fastapi | ||
uvicorn | ||
cython | ||
memory_profiler | ||
psutil |
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,6 @@ | ||
from setuptools import setup | ||
from Cython.Build import cythonize | ||
|
||
setup( | ||
ext_modules=cythonize("math_operations.py"), | ||
) |