Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to combine these 2 simple python files without using class?

So I have 2 python files. I wanted to know if I can do it without the use of class.

from bson import json_util
from pymongo import MongoClient
import json

class DBClient:
    def __init__(self):
        self.client = MongoClient('localhost', 27017)
        self.db = self.client.requests.requests_data

    def get_data(self, request_id, email):
        request = self.db.find_one({"requestId": request_id, "email": email})
        if request:
            request = json.loads(json_util.dumps(request))
            for k, v in list(request.items()):
                if v == 'Failed':
                    del request[k]
        return request

    def update_status(self, request_id, tc, old_status, new_status):
        self.db.update_one({"requestId": request_id, tc: old_status}, {"$set": {tc:new_status}})

from flask import Flask, request, jsonify
from modules import db

app = Flask(__name__)

db = db.DBClient()

@app.route('/fetch_data', methods=['GET'])
def fetch_data():
    request_id = request.args.get('requestId')
    email = request.args.get('email')
    if request_id is None or email is None:
        return jsonify({"status": 400, "data": [], "info": "Invalid email or requestId"})
    request_data = db.get_data(request_id, email)
    return jsonify({"status": 200, "data": request_data, "info": "OK"})

if __name__ == '__main__':
    app.run()

Also I wanted to know if it is possible to make this into one single python file instead of 2 separate ones.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

While having your modules separate improves readability and is more in line with software engineering practices. You can combine the two files into a single one without issue.

from bson import json_util
from pymongo import MongoClient
from flask import Flask, request, jsonify
import json
app = Flask(__name__)
class DBClient:
    def __init__(self):
        self.client = MongoClient('localhost', 27017)
        self.db = self.client.requests.requests_data

    def get_data(self, request_id, email):
        request = self.db.find_one({"requestId": request_id, "email": email})
        if request:
            request = json.loads(json_util.dumps(request))
            for k, v in list(request.items()):
                if v == 'Failed':
                    del request[k]
        return request

    def update_status(self, request_id, tc, old_status, new_status):
        self.db.update_one({"requestId": request_id, tc: old_status}, {"$set": {tc:new_status}})

db = DBClient()

@app.route('/fetch_data', methods=['GET'])
def fetch_data():
    request_id = request.args.get('requestId')
    email = request.args.get('email')
    if request_id is None or email is None:
        return jsonify({"status": 400, "data": [], "info": "Invalid email or requestId"})
    request_data = db.get_data(request_id, email)
    return jsonify({"status": 200, "data": request_data, "info": "OK"})

if __name__ == '__main__':
    app.run()

Non Class Approach

from pymongo import MongoClient
from flask import Flask, request, jsonify
import json

app = Flask(__name__)

client = MongoClient("localhost", 27017)
db = client.requests.requests_data


def get_data(db, request_id, email):
    request = db.find_one({"requestId": request_id, "email": email})
    if request:
        request = json.loads(json_util.dumps(request))
        for k, v in list(request.items()):
            if v == "Failed":
                del request[k]
    return request


def update_status(db, request_id, tc, old_status, new_status):
    db.update_one(
        {"requestId": request_id, tc: old_status}, {"$set": {tc: new_status}}
    )


@app.route("/fetch_data", methods=["GET"])
def fetch_data():
    request_id = request.args.get("requestId")
    email = request.args.get("email")
    if request_id is None or email is None:
        return jsonify(
            {"status": 400, "data": [], "info": "Invalid email or requestId"}
        )
    request_data = get_data(db, request_id, email)
    return jsonify({"status": 200, "data": request_data, "info": "OK"})


if __name__ == "__main__":
    app.run()


Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading