Skip to content

Commit

Permalink
Merge pull request #2 from ramsunvtech/main
Browse files Browse the repository at this point in the history
tech(base): add logging, typing, packaging cython, garbage collection, docker run command
  • Loading branch information
ramsunvtech authored Dec 3, 2024
2 parents 905d963 + 53b92a9 commit 17fdef6
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ RUN pip install -r requirements.txt

COPY . .

CMD ["python", "main.py"]
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
29 changes: 27 additions & 2 deletions README.md
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
```
13 changes: 13 additions & 0 deletions concurrency_example.py
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())
3 changes: 3 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pyinstaller
flake8
pytest
13 changes: 13 additions & 0 deletions garbage_collection.py
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()
27 changes: 27 additions & 0 deletions main.py
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))
22 changes: 22 additions & 0 deletions math_operations.py
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
5 changes: 5 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fastapi
uvicorn
cython
memory_profiler
psutil
6 changes: 6 additions & 0 deletions setup.py
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"),
)

0 comments on commit 17fdef6

Please sign in to comment.