Commit a35c373a by ajil.k

added

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 (databases)" 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/databases.iml" filepath="$PROJECT_DIR$/.idea/databases.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
# importing library
from scripts.services.crud_operations import start_crud_operation
# calling function to perform crud operations
start_crud_operation()
[DbConnection]
uri = mongodb://localhost:27017/
\ No newline at end of file
import configparser
config = configparser.ConfigParser()
config.read("conf/db.conf")
uri = config.get('DbConnection', 'uri')
def delete_one_document(company_collection):
try:
# delete one document
deleted_one = company_collection.delete_one({"company_name": "hjdf"})
if deleted_one.acknowledged:
print("One document deleted")
else:
print("Document deletion failed!")
except Exception as e:
print("Error ", e)
def delete_many_document(company_collection):
try:
# delete more than one document
deleted_many = company_collection.delete_many({"pat_change": 33.3})
if deleted_many.acknowledged:
print("Documents deleted successfully")
else:
print("Documents deletion failed!")
except Exception as e:
print("Error ", e)
def data_from_user():
company_license_no = int(input("Enter the company license no: "))
company_name = input("Enter the company name: ")
market_cap = int(input("Enter the market capital: "))
revenue_change = float(input("Enter the revenue change: "))
pat_change = float(input("Enter the pat change: "))
company_document = {
"company_license_no": company_license_no,
"company_name": company_name,
"market_cap": market_cap,
"revenue_change": revenue_change,
"pat_change": pat_change
}
return company_document
from scripts.core.handlers.get_data import data_from_user
def inserting_one(company_collection):
try:
# insert one document
print("------Inserting using insert_one------")
# get data from user
data = data_from_user()
# inserting one document
inserted_one = company_collection.insert_one(data)
if inserted_one.acknowledged:
print("One document inserted successfully!")
else:
print("One document insertion failed!")
except Exception as e:
print("Error ", e)
def inserting_many(company_collection):
try:
# insert more than one document
print("------Inserting using insert_many------")
number_of_documents = int(input("Enter the number of documents to be inserted: "))
document_list = []
for i in range(0, number_of_documents):
print("----- Enter the details of company " + str(i + 1) + " -----\n")
document_list.append(data_from_user())
# inserting more than one document
inserted_many = company_collection.insert_many(document_list)
if inserted_many.acknowledged:
print(str(number_of_documents) + " documents inserted successfully!")
except Exception as e:
print("Error ", e)
# importing pymongo
import pymongo
def db_connect():
# create a connection
try:
connection = pymongo.MongoClient("mongodb://localhost:27017/")
print("Connected Successfully !")
# create a database
db = connection['company_db']
except Exception as e:
print("Error ", e)
return db
def finding_one(company_collection):
try:
# find one document
print(list(company_collection.find({}, {"company_name": 1, "_id": 0, "company_license_no": 1})))
except Exception as e:
print("Error ", e)
def finding_many(company_collection):
try:
# find one document
print(company_collection.find_one())
except Exception as e:
print("Error ", e)
def update_one_document(company_collection):
try:
# update one
updated_one = company_collection.update_one({'company_name': "Reliance Industries Ltd."},
{"$set": {"pat_change": 33.4}})
if updated_one.matched_count == 1:
print("One document updated!")
else:
print("Updating one document failed!")
except Exception as e:
print("Error ", e)
def update_many_document(company_collection):
try:
# update many
updated_many = company_collection.update_many({}, {"$set": {"pat_change": 3.6}})
if updated_many.matched_count != 0:
print("Documents updated Successfully!")
else:
print("Updating documents failed!")
except Exception as e:
print("Error ", e)
from scripts.core.handlers.mongodb_connect import db_connect
from scripts.core.handlers.insert import inserting_one, inserting_many
from scripts.core.handlers.read_documents import finding_one, finding_many
from scripts.core.handlers.delete import delete_one_document, delete_many_document
from scripts.core.handlers.update import update_one_document, update_many_document
def start_crud_operation():
try:
# create a db connection
db = db_connect()
# create a collection
company_collection = db['company']
# inserting one document
inserting_one(company_collection)
# finding more than one document
inserting_many(company_collection)
# finding one document
finding_one(company_collection)
# inserting more than one document
finding_many(company_collection)
# updating one document
update_one_document(company_collection)
# updating many documents
update_many_document(company_collection)
# deleting one document
delete_one_document(company_collection)
# deleting many documents
delete_many_document(company_collection)
except Exception as e:
print("Error-", e)
\ No newline at end of file
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