Tuesday, 10 December 2019

How to split function in two different class with inheritance? Beginner

I have a json like below. I have program also below.Need to divide the program in to two class. first get the request in first class and then do the operation in second class. Basically how to convert my function in to two separate classes where on class is general and other class do the operations

Postman accepts the json POST request as below http://127.0.0.1:5000/out

{
    "filters": [{
        "id": 1,
        "name": "pool",
        "type": "str",
        "date": ["2019-12-01", "2019-12-03"]
    }]
}

Flask Code is below

from flask import Flask
app = Flask(__name__)
@app.route('/out/', methods=[ 'POST'])   
def values_extract():
    json_request = request.get_json()
    start_date = ''
    end_date = ''
    data = json.loads(json.dumps(json_request))

   #if valid json then do the below program in separate `**class**`

    for i in data['filters']:
    if i['type'] == 'pool':
       start_date =  i['date'][0]
       end_date = i['date'][1]
    return  (start_date, end_date)

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

My flask is perfectly running with above function

My psuedo code with class

class jsonrequest():
        def getrequest(self,json_request,data):
                self.json_request = request.get_json()
                self.data = json.loads(json.dumps(json_request))
                start_date = ''
                end_date = ''

class valuesextract(jsonrequest):
        def filtervalues(self,json_request,data):    
            for i in data['filters']:
                if i['type'] == 'pool':
                        start_date =  i['date'][0]
                        end_date = i['date'][1]
                return  (start_date, end_date)


from How to split function in two different class with inheritance? Beginner

No comments:

Post a Comment