add to function

This commit is contained in:
2024-03-09 20:28:55 -05:00
parent 66ea127f23
commit 039ecde461
7 changed files with 103 additions and 2 deletions

View File

@@ -2,7 +2,7 @@
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="jdk" jdkName="Remote Python 3.11.8 Docker Compose (app)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

5
.idea/misc.xml generated
View File

@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
<component name="Black">
<option name="sdkName" value="Remote Python 3.11.8 Docker Compose (app)" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Remote Python 3.11.8 Docker Compose (app)" project-jdk-type="Python SDK" />
</project>

5
Dockerfile Normal file
View File

@@ -0,0 +1,5 @@
FROM python:3.11.8-alpine3.18
COPY ./app /app
WORKDIR /app
RUN pip3 install -r requirements.txt
CMD ["python", "/app/main.py"]

59
app/AnimalShelter.py Normal file
View File

@@ -0,0 +1,59 @@
from pymongo import MongoClient
from bson.objectid import ObjectId
class AnimalShelter(object):
""" CRUD operations for Animal collection in MongoDB """
def __init__(self):
# Initializing the MongoClient. This helps to
# access the MongoDB databases and collections.
# This is hard-wired to use the aac database, the
# animals collection, and the aac user.
# Definitions of the connection string variables are
# unique to the individual Apporto environment.
#
# You must edit the connection variables below to reflect
# your own instance of MongoDB!
#
# Connection Variables
#
USER = 'aacuser'
PASS = 'Ex9Ai3Y2eP'
HOST = 'db'
PORT = 27017
DB = 'AAC'
COL = 'animals'
#
# Initialize Connection
#
self.client = MongoClient('mongodb://%s:%s@%s:%d' % (USER,PASS,HOST,PORT))
self.database = self.client['%s' % (DB)]
self.collection = self.database['%s' % (COL)]
# Complete this create method to implement the C in CRUD.
def create(self, *args, **kwargs):
data = kwargs.get('data', None)
if data is not None:
try:
self.database.animals.insert_one(data) # data should be dictionary
except Exception as e:
raise Exception(f'Error: {e}')
else:
raise Exception("Nothing to save, because data parameter is empty")
# Create method to implement the R in CRUD.
def read(self, *args, **kwargs):
criteria = kwargs.get('criteria', None)
find_one = kwargs.get('once', False)
if find_one:
try:
data = self.collection.find_one(criteria)
return data
except Exception as e:
raise Exception(f"Error: {e}")
else:
try:
data = self.collection.find(criteria)
return data
except Exception as e:
raise Exception(f"Error: {e}")

29
app/main.py Normal file
View File

@@ -0,0 +1,29 @@
from AnimalShelter import AnimalShelter
test = AnimalShelter()
test_criteria = {"breed": "Pit Bull Mix"}
test_data = test.read(criteria=test_criteria)
for object in test_data:
print(object)
test_data = {
'rec_num': 10001,
'age_upon_outcome': '3 years',
'animal_id': 'FA69420',
'animal_type': 'Furry',
'breed': 'Protogen',
'color': 'Purple',
'date_of_birth': '1999-12-25',
'datetime': '2019-10-11 10:03:00',
'monthyear': '2019-10-11T10:03:00',
'name': '*Brex',
'outcome_subtype': 'Foster',
'outcome_type': 'Adoption',
'sex_upon_outcome': 'Male i think',
'location_lat': 30.7118766251975,
'location_long': -97.2999365773623,
'age_upon_outcome_in_weeks': 558.059821428571
}
test.create(test_data)

1
app/requirements.txt Normal file
View File

@@ -0,0 +1 @@
pymongo

View File

@@ -1,4 +1,8 @@
services:
app:
build:
context: .
dockerfile: Dockerfile
db:
image: mongo
restart: unless-stopped