Skip to content
This repository has been archived by the owner on May 30, 2022. It is now read-only.

Commit

Permalink
Support ocr data format
Browse files Browse the repository at this point in the history
  • Loading branch information
jiajunsu committed Aug 30, 2018
1 parent 8621545 commit 1191481
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
6 changes: 3 additions & 3 deletions calculator_of_Onmyoji/cal_and_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def filter_mitama_by_type(mitama, desired_type):
def filter_loc_prop(data_list, prop_type, prop_min_value):
def prop_value_le_min(mitama):
mitama_info = mitama.values()[0]
if (mitama_info[prop_type] and
if (mitama_info.get(prop_type, 0) and
mitama_info[prop_type] >= prop_min_value):
return True
else:
Expand Down Expand Up @@ -179,7 +179,7 @@ def fit_prop_value(mitama_sum_data, prop_type, min_value, max_value):

for mitama in mitama_comb:
mitama_info = mitama.values()[0]
if mitama_info[prop_type]:
if mitama_info.get(prop_type, 0):
prop_value += mitama_info[prop_type]

for m_type, m_count in mitama_type_count.items():
Expand Down Expand Up @@ -279,7 +279,7 @@ def sum_prop(mitama_comb, mitama_type_count):

# 计算除套装外的总属性
for prop_type in prop_type_list:
if mitama_info[prop_type]:
if mitama_info.get(prop_type, 0):
sum_result[prop_type] += mitama_info[prop_type]

for m_type, m_count in mitama_type_count.items():
Expand Down
39 changes: 31 additions & 8 deletions calculator_of_Onmyoji/load_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,34 @@ def _get_sheet_rows(filename, sheet_name):
print(traceback.format_exc())
raise


def get_mitama_data_json(filename, ignore_serial):
with open(filename) as f:
data = json.load(f)

if isinstance(data, dict):
return load_json_from_editor(data, ignore_serial)
elif isinstance(data, list):
return load_json_from_ocr_editor(data, ignore_serial)
else:
print('Unsupport format')
raise TypeError


def load_json_from_ocr_editor(data, ignore_serial):
'''从OCR录入器读取数据'''
mitama_data = dict()
serial = 1

for d in data:
mitama_data[serial] = d
serial += 1

return mitama_data


def load_json_from_editor(data, ignore_serial):
'''从网页版御魂编辑器读取数据'''
def mitama_json_to_dict(json_obj):
MITAMA_COL_MAP = {u'御魂序号': u'id', u'御魂类型': u'name', u'位置': u'pos'}
serial = json_obj[u'id']
Expand All @@ -40,14 +67,9 @@ def mitama_json_to_dict(json_obj):

return (serial, mitama)

try:
with open(filename) as f:
data = json.load(f)
mitama_list = map(mitama_json_to_dict, data['data'])
return dict(filter(lambda x: x, mitama_list))
except Exception as e:
print("Error loading json file!")
return None
mitama_list = map(mitama_json_to_dict, data['data'])
return dict(filter(lambda x: x, mitama_list))


def get_mitama_data_xls(filename, ignore_serial):
rows_data = _get_sheet_rows(filename, sheet_name=u'御魂')
Expand All @@ -71,6 +93,7 @@ def get_mitama_data_xls(filename, ignore_serial):

return mitama_data


def get_mitama_data(filename, ignore_serial):
ext_name = os.path.splitext(filename)[1]
if ext_name == '.xls':
Expand Down
Binary file modified example/data_Template.xls
Binary file not shown.

0 comments on commit 1191481

Please sign in to comment.