I have the following code for making a RESTful call to a server:
我有以下代码用于对服务器进行RESTful调用:
def request(self, request, account_id, user):
if request is 'get_id':
#Get user from id
result = requests.get(api_root + "/accounts/" + account_id + "/users/" + user, headers=self.headers)
elif request is 'get_username':
#Get user from username
result = requests.get(api_root + "/accounts/" + account_id + "/users?username=" + user, headers=self.headers)
elif request is 'get_email':
#Get user from username
result = requests.get(api_root + "/accounts/" + account_id + "/users?email=" + user, headers=self.headers)
elif request is 'post':
#Add user to new account
result = requests.post(api_root + '/accounts/' + account_id + '/users', data=json.dumps(user), headers=self.headers)
elif request is 'delete':
#Delete user from account
result = requests.delete(api_root + "/accounts/" + account_id + "/users/" + user, headers=self.headers)
#Throw exception if non-200 response
result.raise_for_status()
#Print request result / status
print "\nRequest " + request + " Result: " + result.text + "\nStatus: " + str(result.status_code)
return result
I know this is ugly and I wanted to change it into a dictionary, something like:
我知道这很难看,我想把它改成字典,比如:
def request(self, request, account_id, user):
url = api_root + "/accounts/" + account_id
function_dictionary = {}
function_dictionary['get_id'] = requests.get(url + "/users/" + user, headers=self.headers)
function_dictionary['get_username'] = requests.get(api_root + "/accounts/" + account_id + "/users?username=" + user, headers=self.headers)
function_dictionary['get_email'] = requests.get(api_root + "/accounts/" + account_id + "/users?email=" + user, headers=self.headers)
function_dictionary['delete'] = requests.delete(url + "/users/" + user, headers=self.headers)
function_dictionary['post'] = requests.post(url + '/users', data=json.dumps(user), headers=self.headers)
result = function_dictionary.get(request)
#Throw exception if non-200 response
result.raise_for_status()
return result
I still have a feeling I am going about it the wrong way. Can anybody tell me what the proper method to approach if / elseif statements in Python is?
我仍然有一种感觉,我会以错误的方式去做。任何人都可以告诉我,如果Python中的/ elseif语句是正确的方法吗?
Thanks!
谢谢!
1 个解决方案
#1
8
Using a dict
to replace if: elif:
loops is certainly Pythonic, but note that in your example you are calling requests.get
etc for every case you store in the dictionary, i.e. the dictionary values are the results of those calls.
使用dict替换if:elif:loops肯定是Pythonic,但请注意,在您的示例中,您为字典中存储的每个案例调用requests.get等,即字典值是这些调用的结果。
An alternative would be to store function and arguments separately in the dictionary:
另一种方法是在字典中单独存储函数和参数:
function_dict = {'get_id': (requests.get, # function
(url + "/users/" + user,), # tuple of arguments
{'headers': self.headers}), # dict of keyword args
...}
Now you can use
现在你可以使用了
func, args, kwargs = function_dict[request]
result = func(*args, **kwargs)
Also, note that comparing strings using is
is a bad idea (although it sometimes works); it is better to use ==
:
另外,请注意使用is比较字符串是一个坏主意(虽然它有时可行);最好使用==:
if request == 'get_id':
#1
8
Using a dict
to replace if: elif:
loops is certainly Pythonic, but note that in your example you are calling requests.get
etc for every case you store in the dictionary, i.e. the dictionary values are the results of those calls.
使用dict替换if:elif:loops肯定是Pythonic,但请注意,在您的示例中,您为字典中存储的每个案例调用requests.get等,即字典值是这些调用的结果。
An alternative would be to store function and arguments separately in the dictionary:
另一种方法是在字典中单独存储函数和参数:
function_dict = {'get_id': (requests.get, # function
(url + "/users/" + user,), # tuple of arguments
{'headers': self.headers}), # dict of keyword args
...}
Now you can use
现在你可以使用了
func, args, kwargs = function_dict[request]
result = func(*args, **kwargs)
Also, note that comparing strings using is
is a bad idea (although it sometimes works); it is better to use ==
:
另外,请注意使用is比较字符串是一个坏主意(虽然它有时可行);最好使用==:
if request == 'get_id':