Answer the question
In order to leave comments, you need to log in
What are the nuances of python flask syntax?
So I started learning python and ran into the following problem: when I try to process my "wonderful" code, a syntax error occurs constantly in different places.
I will be very grateful if you help me understand the details of the python syntax that I did not take into account. Thank you. Actually, the code is:
from flask import Flask
from flask_restful import Api, Resourse, reqparse
app = Flask (__name__)
api = Api(app)
users = [
{
"name": "Nikita",
"age": "27",
"profession": "Ment"
},
{
"name": "Roma",
"age": "26",
"profession": "Debil"
},
{
"name": "Evgeniy",
"age": "27",
"profession": "DevOps"
}
]
class User(Resourse):
def get(self, name):
def post(self, name):
def put(self, name):
def delete(self, name):
def get(self, name):
for user in users:
if(name==user["name"]):
return user, 200
return "User not found", 404
def post(self, name):
parser = reqparse.RequestParser()
parser.add_argument("age")
parser.add_argument("profession")
args = parser.parse_args()
for user in users:
if(name == user["name"]):
return "User with name {} already exists".format(name), 400
user = {
"name": name,
"age": args["age"],
"profession": args["profession"]
}
users.append(user)
return user, 201
def put(self, name):
parser = reqparse.RequestParser()
parser.add_argument("age")
parser.add_argument("profession")
args = parser.parse_args()
for user in users:
if(name==user["name"]):
user["age"] = args["age"]
user["profession"] = args["profession"]
return user, 200
user = {
"name": name,
"age": args["age"],
"profession": args["profession"]
}
users.append(user)
return user, 201
def delete(self, name):
global users
users = [user for user in users if user["name"] != name]
return "{} is deleted.".format(name), 200
api.add_resourse(User, "/user/<string:name>")
app.run(debug=True)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question