Commit ec2d6c28 by mohammed.shibili

first commit

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (FastAPIwithCSV)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/FastAPIwithCSV.iml" filepath="$PROJECT_DIR$/.idea/FastAPIwithCSV.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
import uvicorn
from script.core.handlers.reading_csv import csv_read
from script.util.mongo_util import mongo_connection, insert_many
if __name__ == "__main__":
uvicorn.run("script.service.main:app", port=8000, reload=True)
# getting collection name by establishing connection
collection_name = mongo_connection()
dict_from_file = csv_read()
insert_many(collection_name, dict_from_file)
[path]
csv_path=temp/api_data.csv
[mongo]
server=mongodb://localhost:27017
database =my_db
collection =table_1
\ No newline at end of file
import configparser
config = configparser.RawConfigParser()
config.read("conf/congiguration.conf")
# paths
csv_path = config.get("path", "csv_path")
# for mongodb
server_name = config.get("mongo", "server")
db_name = config.get("mongo", "database")
collection_name = config.get("mongo", "collection")
from mongoengine import Document, IntField, StringField
class table_1(Document):
_id = StringField()
id = IntField()
first_name = StringField(max_length=20)
last_name = StringField(max_length=20)
gender = StringField(max_length=10)
\ No newline at end of file
import pandas as pd
from script.config.appconfig import csv_path
def csv_read():
dataset = pd.read_csv(csv_path)
dict_dataset = dataset.to_dict(orient='records')
return dict_dataset
from fastapi import FastAPI
from script.config.appconfig import db_name
from script.core.engine.models import table_1
from mongoengine import connect
import json
connect(db=db_name, host="localhost", port=27017)
app = FastAPI()
# read --> get
@app.get("/get_all")
async def get_element():
datas = table_1.objects.to_json()
datas_list = json.loads(datas)
return datas_list
#
#
# insert --> post
@app.post("/new", tags=['FIRST'])
async def post_element(body: dict):
data = table_1.insert_one(body).to_json()
new_data = json.loads(data)
return new_data
#
#
# # update --> put
# @app.put("/new", tags=['FIRST'])
# async def put_element(id: int, body: dict):
# for values in value:
# if int(values['id']) == body['id']:
# values['name'] = body['name']
# return {
# "data": f'name of {values["name"]} is updated with {body["name"]}'
# }
# return {
# "data": f'id {id} not found'
# }
#
#
# # delete
# @app.delete("/new/{id}", tags=['FIRST'])
# async def delete_element(id: int):
# for values in value:
# if int((values['id'])) == id:
# value.remove(values)
# return {"data": f'details of {id} deleted'}
#
#
# value = [
# {"id": 5, "name": "ajil", "age": 23},
# {"id": 6, "name": "arun", "age": 25}
#
# ]
#
# new_val = {"id": 7, "name": "arjun", "age": 26}
from pymongo import MongoClient
from script.config.appconfig import server_name, db_name, collection_name
from script.core.handlers.reading_csv import csv_read
def mongo_connection():
try:
client = MongoClient(server_name)
database = client[db_name]
collection = database[collection_name]
print("connection success")
return collection
except Exception as e:
print("db connection failed", e)
def insert_many(coll_name, dict_from_file):
try:
coll_name.insert_many(dict_from_file)
# coll_name.update_many({}, {"$unset": {"_id": ""}})
print("data inserted")
except Exception as e:
print("data insertion error", e)
id,first_name,last_name,gender
1,Francisco,Inder,Male
2,Marnie,Brewers,Female
3,Lazare,Stoney,Male
4,Deidre,Le Breton De La Vieuville,Female
5,Jeannette,Margetson,Female
6,Chere,Hamber,Female
7,Nicolais,Temperton,Male
8,Alli,Pettet,Female
9,Florinda,Purches,Female
10,Vassily,Vasyukov,Non-binary
11,Haley,Monteaux,Male
12,Dolores,Doone,Female
13,Jemmy,Diggles,Female
14,Leonora,Biasioli,Female
15,Gabbi,Mashro,Female
16,Cliff,Werner,Male
17,Cherrita,O'Breen,Female
18,Ambrosio,Clapp,Male
19,Chane,Yakov,Male
20,Madella,Jeanesson,Female
21,Nona,Bointon,Female
22,Annemarie,O'Shavlan,Genderfluid
23,Tallulah,Cutill,Female
24,Ty,Goode,Male
25,Fiorenze,Kermon,Female
26,Jacki,Cocci,Female
27,Erick,Vasser,Male
28,Crysta,Mordy,Female
29,Beltran,Mash,Male
30,Fidole,Detoile,Male
31,Barney,Zanni,Male
32,Alexander,Livingston,Male
33,Eugenio,Hartzogs,Male
34,Homer,Stinton,Male
35,Hasheem,Briggs,Bigender
36,Vasilis,Gwynn,Male
37,Wilhelmina,Cordell,Female
38,Shepard,Yves,Non-binary
39,Nehemiah,Heisman,Male
40,Kasey,Shapter,Female
41,Paolina,Spellsworth,Female
42,Nealy,Slaght,Male
43,Griffith,Vears,Genderqueer
44,Carlota,Huard,Female
45,Jackqueline,Maciocia,Female
46,Peggi,Yvens,Female
47,Charissa,Flipek,Female
48,Dougy,Fortnon,Male
49,Gail,Gawke,Female
50,Merry,Korlat,Male
51,Lily,Cutforth,Bigender
52,Tam,Bart,Male
53,Margaretta,Todd,Bigender
54,Rosamund,Szymon,Female
55,Caesar,Walls,Genderqueer
56,Denys,Bjerkan,Male
57,Hedy,Castard,Bigender
58,Vick,Bergen,Male
59,Germayne,Crossland,Male
60,Rand,Dorsey,Male
61,Scot,Wimmers,Male
62,Ced,Garrity,Male
63,Christian,Prazer,Male
64,Olimpia,Hofner,Female
65,Chevalier,Niven,Genderfluid
66,Benedikt,Wandrich,Genderqueer
67,Kalil,Harris,Male
68,Olav,Hun,Male
69,Kiersten,Normanvill,Female
70,Dill,Kunes,Male
71,Hilly,Mattingley,Male
72,Shirleen,Korneichik,Genderfluid
73,Roland,McNuff,Male
74,Amara,Bouzan,Female
75,Edin,Abbe,Female
76,Humfrid,Loidl,Male
77,Palmer,Bloomfield,Male
78,Berkly,Moran,Male
79,Davita,Paulet,Genderqueer
80,Katherina,Henaughan,Female
81,Raimundo,Congdon,Male
82,Ellswerth,Eastope,Male
83,Bern,Skerman,Genderqueer
84,Donal,Wetherhead,Male
85,Zolly,Schoenfisch,Male
86,Noelyn,Crammy,Female
87,Karlotta,Castiello,Female
88,Edee,Casperri,Female
89,Aleece,Carlan,Bigender
90,Nari,Redolfi,Female
91,Revkah,Clements,Female
92,Terrijo,Reimer,Female
93,Royall,Aldersea,Male
94,Erwin,Rogge,Male
95,Royce,Barnhill,Male
96,Ave,Stoggell,Male
97,Torrin,Emlen,Male
98,Regan,MacDuff,Male
99,Nonah,Clissett,Female
100,Eadith,Keating,Polygender
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment