Skip to content

Instantly share code, notes, and snippets.

@espdev
Last active January 31, 2016 11:51
Show Gist options
  • Save espdev/c098680e884c5a4ac922 to your computer and use it in GitHub Desktop.
Save espdev/c098680e884c5a4ac922 to your computer and use it in GitHub Desktop.
The outgoing calls information for the "Megafon" mobile operator
# coding=utf-8
"""
The outgoing calls information for the "Megafon" mobile operator
"""
import xml.etree.ElementTree as ElementTree
import re
import numpy as np
ALL_CALLS = re.compile(r'Исходящие (местные|междугородные|международные) вызовы')
LOCAL_CALLS = re.compile(r'Исходящие местные вызовы')
LONG_DIST_CALLS = re.compile(r'Исходящие междугородные вызовы')
INTERNATIONAL_CALLS = re.compile(r'Исходящие международные вызовы')
def get_calls_info(file_name, call_type=ALL_CALLS):
tree = ElementTree.parse(file_name)
root = tree.getroot()
prefix = '{urn:schemas-microsoft-com:office:spreadsheet}'
worksheet = root.find(prefix + 'Worksheet')
table = worksheet.find(prefix + 'Table')
calls_info = {
'date': [],
'time': [],
'number': [],
'type': [],
'minutes': [],
'cost': [],
}
for row in table.findall(prefix + 'Row'):
cells = list(row.findall(prefix + 'Cell'))
for cell in cells:
if cell.attrib[prefix + 'Index'] == '11':
data = cell.find(prefix + 'Data')
if data is None:
continue
if call_type.search(data.text):
calls_info['type'].append(data.text)
for cell_ in cells:
if cell_.attrib[prefix + 'Index'] == '1':
date = cell_.find(prefix + 'Data').text
calls_info['date'].append(date)
if cell_.attrib[prefix + 'Index'] == '5':
time = cell_.find(prefix + 'Data').text
calls_info['time'].append(time)
if cell_.attrib[prefix + 'Index'] == '7':
number = cell_.find(prefix + 'Data').text
calls_info['number'].append(number)
if cell_.attrib[prefix + 'Index'] == '8':
minutes = cell_.find(prefix + 'Data').text
calls_info['minutes'].append(int(minutes))
if cell_.attrib[prefix + 'Index'] == '14':
cost = cell_.find(prefix + 'Data').text
calls_info['cost'].append(float(cost))
return calls_info
def filter_by(calls_info, key='number', value=None):
filtered_calls_info = {}
def _filter(info):
if not key or not value:
return info
filtered = list(filter(lambda x: calls_info[key][x[0]] == value,
enumerate(info)))
if not filtered:
return []
return list(zip(*filtered))[1]
for k, v in calls_info.items():
filtered_calls_info[k] = _filter(v)
return filtered_calls_info
def print_report(calls_info):
cumsum_minutes = np.cumsum(calls_info['minutes']).tolist()
cumsum_cost = np.cumsum(calls_info['cost']).tolist()
for date, time, call_type, number, minutes, cost, cum_minutes, cum_cost in zip(
calls_info['date'], calls_info['time'], calls_info['type'],
calls_info['number'], calls_info['minutes'], calls_info['cost'],
cumsum_minutes, cumsum_cost):
print('{}, {}, {}, {}, {} ({} минут, {} рублей)'.format(
date, time, number, minutes, cost, cum_minutes, round(cum_cost, 2)))
if __name__ == '__main__':
calls_info = get_calls_info('megafon_report.xls', ALL_CALLS)
print_report(filter_by(calls_info, 'number', '79212223344'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment