2014-11-25 10:26:10 +00:00
|
|
|
from datetime import datetime
|
2014-12-02 07:08:20 +00:00
|
|
|
import xlrd
|
|
|
|
import xmltodict
|
2014-11-03 11:22:48 +00:00
|
|
|
|
2014-11-25 10:26:10 +00:00
|
|
|
|
|
|
|
DAYS={
|
|
|
|
'th_TH': ['จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์', 'อาทิตย์'],
|
|
|
|
'th_TH2': ['จันทร์', 'อังคาร', 'พุธ', 'พฤหัสบดี', 'ศุกร์', 'เสาร์', 'อาทิตย์'],
|
|
|
|
'en_US': ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
|
|
|
|
}
|
|
|
|
|
|
|
|
MONTHS={
|
|
|
|
'th_TH': [None, 'มกราคม', 'กุมภาพันธ์', 'มีนาคม', 'เมษายน', 'พฤษภาคม', 'มิถุนายน', 'กรกฎาคม', 'สิงหาคม', 'กันยายน', 'ตุลาคม', 'พฤศจิกายน', 'ธันวาคม'],
|
|
|
|
'th_TH2': [None, 'ม.ค.', 'ก.พ.', 'มี.ค.', 'เม.ย.', 'พ.ค.', 'มิ.ย.', 'ก.ค.', 'ส.ค.', 'ก.ย.', 'ต.ค.', 'พ.ย.', 'ธ.ค.'],
|
|
|
|
'en_US': [None, 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
|
|
|
|
}
|
2014-11-04 16:10:23 +00:00
|
|
|
|
|
|
|
PATIENT_TYPE={
|
|
|
|
"sc":"ปกส.",
|
2014-11-26 11:22:17 +00:00
|
|
|
"uc":"UC.",
|
2014-11-25 10:26:10 +00:00
|
|
|
"others": "จ่ายเอง",
|
2014-11-04 16:10:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 11:39:53 +00:00
|
|
|
STAFF_TYPE={
|
2014-11-24 09:04:10 +00:00
|
|
|
'doctor': 'แพทย์',
|
|
|
|
'nurse': 'พยาบาล',
|
2014-11-25 11:39:53 +00:00
|
|
|
'staff': 'ทั่วไป',
|
2014-11-24 08:50:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-03 11:22:48 +00:00
|
|
|
TOPICS={
|
|
|
|
'topic1': {'name': 'จำนวนครั้งการทำ Hemodialysis', 'unit': 'ครั้ง'},
|
|
|
|
'topic2': {'name': 'จำนวนผู้ป่วยยกมาจากเดือน', 'unit': 'คน'},
|
|
|
|
'topic3': {'name': 'จำนวนผู้ป่วยรับใหม่เดือน', 'unit': 'คน'},
|
|
|
|
'topic4': {'name': 'จำนวนผู้ป่วยจำหน่ายเดือน', 'unit': 'คน'},
|
|
|
|
'topic5': {'name': 'จำนวนผู้ป่วยยกไปเดือน', 'unit': 'คน'},
|
|
|
|
'topic6': {'name': 'จำนวนผู้ป่วยเบิก ปกส.', 'unit': 'คน'},
|
|
|
|
'topic7': {'name': 'จำนวนผู้ป่วยเบิก สปกส.', 'unit': 'คน'},
|
|
|
|
'topic8': {'name': 'จำนวนผู้ป่วยจ่ายเอง', 'unit': 'คน'},
|
|
|
|
}
|
2014-11-25 10:26:10 +00:00
|
|
|
|
2014-12-02 07:08:20 +00:00
|
|
|
def read_excel(fpath=None,show_datetime=False):
|
|
|
|
data={}
|
|
|
|
if fpath:
|
|
|
|
suffix=fpath.split(".")[-1]
|
|
|
|
if suffix not in ('xls', 'xlsx'):
|
|
|
|
raise Exception("ERROR : please should file xls or xlsx")
|
|
|
|
wb=xlrd.open_workbook(fpath)
|
|
|
|
sheet=wb.sheet_by_name("Sheet1")
|
|
|
|
keys = [sheet.cell(0, col_index).value for col_index in range(sheet.ncols)]
|
|
|
|
data=[]
|
|
|
|
for row_index in range(1, sheet.nrows):
|
|
|
|
d={}
|
|
|
|
for col_index in range(sheet.ncols):
|
|
|
|
ctype=sheet.cell(row_index,col_index).ctype
|
|
|
|
if ctype==3:
|
|
|
|
value=sheet.cell(row_index, col_index).value
|
|
|
|
year, month, day, hour, minute, second = xlrd.xldate_as_tuple(value,wb.datemode)
|
2014-12-02 08:34:28 +00:00
|
|
|
value=datetime(year, month, day, hour, minute,second)
|
2014-12-02 07:08:20 +00:00
|
|
|
if show_datetime:
|
|
|
|
value=value.strftime("%Y-%m-%d %H:%M:%S")
|
|
|
|
else:
|
|
|
|
value=value.strftime("%Y-%m-%d")
|
|
|
|
else:
|
|
|
|
value=sheet.cell(row_index, col_index).value
|
|
|
|
d.update({(keys[col_index] or "").lower():value})
|
|
|
|
data.append(d)
|
|
|
|
return data
|
|
|
|
|
2014-12-03 17:46:50 +00:00
|
|
|
def read_xml(fpath=None,node="HDBills"):
|
|
|
|
root_name="STMSTM"
|
|
|
|
child_name=node
|
2014-12-02 07:08:20 +00:00
|
|
|
data={}
|
2014-12-03 17:46:50 +00:00
|
|
|
if not child_name:
|
2014-12-02 07:08:20 +00:00
|
|
|
return data
|
|
|
|
if fpath:
|
|
|
|
suffix=fpath.split(".")[-1]
|
|
|
|
if suffix not in ('xml'):
|
|
|
|
raise Exception("ERROR : please should file xml")
|
|
|
|
data=xmltodict.parse(open(fpath,"r").read())
|
2014-12-03 17:46:50 +00:00
|
|
|
root=data.get(root_name)
|
|
|
|
lines=[]
|
|
|
|
if root:
|
|
|
|
child=root.get(child_name)
|
|
|
|
if not child:
|
2014-12-02 07:08:20 +00:00
|
|
|
return {}
|
2014-12-03 17:46:50 +00:00
|
|
|
for k, v in child.items():
|
2014-12-02 07:08:20 +00:00
|
|
|
collections=v
|
|
|
|
for collection in collections:
|
|
|
|
if isinstance(collection,dict):
|
|
|
|
line={}
|
|
|
|
for i, j in collection.items():
|
|
|
|
key=(i or "").lower()
|
|
|
|
line[key]=j
|
|
|
|
lines.append(line)
|
|
|
|
return lines
|
|
|
|
|
2014-11-25 10:26:10 +00:00
|
|
|
def date2thai(date, format='%(BY)s-%(m)s-%(d)s', lang='th_TH'):
|
|
|
|
'''
|
|
|
|
>>> date2thai('2011-12-31', lang='th_TH')
|
|
|
|
'2554-12-31'
|
|
|
|
>>> date2thai('2011-12-31', format='%(Td)s %(d)s %(Tm)s, %(By)s', lang='en_US')
|
|
|
|
'Saturday 31 December, 54'
|
|
|
|
>>> print date2thai('2011-12-31', format='%(Td)s %(d)s %(Tm)s, %(By)s', lang='th_TH')
|
|
|
|
เสาร์ 31 ธันวาคม, 54
|
|
|
|
>>> date2thai('2000-06-08', lang='th_TH')
|
|
|
|
'2543-06-08'
|
|
|
|
>>> date2thai('2000-06-08', format='%(Td)s %(d)s %(Tm)s, %(By)s', lang='en_US')
|
|
|
|
'Thursday 08 June, 43'
|
|
|
|
>>> print date2thai('2000-06-08', format='%(Td)s %(d)s %(Tm)s, %(By)s', lang='th_TH')
|
|
|
|
พฤหัสบดี 08 มิถุนายน, 43
|
|
|
|
'''
|
|
|
|
|
|
|
|
if not date or not date.count('-') == 2:
|
|
|
|
return ''
|
|
|
|
|
|
|
|
year, month, day = date.split('-')
|
|
|
|
|
|
|
|
#dow = DateTime.Date(int(year), int(month), int(day)).day_of_week
|
|
|
|
dow = datetime(int(year),int(month),int(day)).weekday()
|
|
|
|
return format % { 'BY': int(year) + 543
|
|
|
|
, 'By': int(year[2:]) + 43
|
|
|
|
, 'Tm': MONTHS[lang][int(month)]
|
|
|
|
, 'Td': DAYS[lang][dow]
|
|
|
|
, 'm': month
|
|
|
|
, 'd': int(day) # XXX remove zero
|
|
|
|
}
|
2014-12-02 07:08:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|