-
Notifications
You must be signed in to change notification settings - Fork 76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NewFeature]列医院信息+按关键字搜索医院 #32
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
# -*- coding: utf-8 -*- | ||
from flask import Blueprint | ||
from flask import request | ||
import codecs | ||
import os | ||
import json | ||
import datetime | ||
|
@@ -40,12 +42,35 @@ | |
""" | ||
def csv_helper(fpath, headers): | ||
result = [] | ||
with open(fpath) as f: | ||
with codecs.open(fpath, encoding='utf-8') as f: | ||
for line in f.readlines()[1:]: | ||
csv_data = line.strip().split(",") | ||
result.append(dict(zip(headers,csv_data))) | ||
return result | ||
|
||
def filter_entity(entity_list, search_fields, keywords): | ||
recall_set = [] | ||
for entity in entity_list: | ||
if is_valid_entity(entity, search_fields, keywords): | ||
recall_set.append(entity) | ||
return recall_set | ||
|
||
def is_valid_entity(entity, search_fields, keywords): | ||
field_values = [] | ||
for search_field in search_fields: | ||
if search_field in entity: | ||
field_values.append(entity[search_field]) | ||
field_val_concat = ' '.join(field_values).lower() | ||
is_valid = True | ||
for keyword in keywords.split(' '): | ||
try: | ||
if keyword.lower() not in field_val_concat: | ||
is_valid = False | ||
break | ||
except Exception as ex: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be great to not catch all exceptions, but only a handful of expected errors. |
||
is_valid = False | ||
return is_valid | ||
|
||
def yaml_helper(fpath): | ||
result = [] | ||
with open(fpath, 'r') as f: | ||
|
@@ -61,15 +86,33 @@ def hospital_list(): | |
'msg': '', | ||
} | ||
try: | ||
resp_data = csv_helper(HOSPITAL_PATH,HOTEL_HEADERS) | ||
resp_data = csv_helper(HOSPITAL_PATH,HOSPICAL_HEADERS) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo: |
||
resp['success'] = True | ||
resp['data'] = resp_data | ||
except Exception as e: | ||
resp['msg'] = str(e) | ||
return json.dumps(resp, ensure_ascii=False) | ||
|
||
|
||
|
||
@data.route('/hospital_search') | ||
def hospital_search(): | ||
keyword = request.args.get('keyword') | ||
print('keyword: %s' % keyword) | ||
resp = { | ||
'success': False, | ||
'data': [], | ||
'msg': '', | ||
} | ||
try: | ||
hospital_all = csv_helper(HOSPITAL_PATH,HOSPICAL_HEADERS) | ||
search_fields = ['hospital_name', 'hospital_addr', 'hospital_requirement'] | ||
print('search_fields created: %s' % search_fields) | ||
search_result = filter_entity(hospital_all, search_fields, keyword) | ||
resp['success'] = True | ||
resp['data'] = search_result | ||
except Exception as e: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to https://github.com/wuhan2020/api-server/pull/38/files#r372081013, this is trying to catch any exceptions, it also swallows the status codes. |
||
resp['msg'] = str(e) | ||
return json.dumps(resp, ensure_ascii=False) | ||
|
||
@data.route('/hotel_list') | ||
def hotel_list(): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo:
HOSPITAL_HEADERS