Last active
August 29, 2015 14:13
-
-
Save ap-Codkelden/9a40bb7a5a4e9628c122 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import argparse | |
import sys | |
import datetime | |
from xml.etree.ElementTree import ElementTree | |
from xml.etree.ElementTree import ParseError | |
tree = ElementTree() | |
parser = argparse.ArgumentParser() | |
parser.add_argument("okpo", help="OKPO", type=str) | |
parser.add_argument("pk", help="sum to search", type=str) | |
parser.add_argument("-a", "--agents", default=0, action="store", type=int, help="agents quantity") | |
parser.add_argument("-m", "--month", default=0, action="store", type=int, help="report month (M)") | |
parser.add_argument("-y", "--year", default=datetime.datetime.now().year, action="store", type=int, help="report year (YYYY)") | |
def parse(filename): | |
try: | |
tree.parse(filename) | |
pk_ipn = [ x.text for x in list(tree.iter('T2RXXXXG2')) ] | |
pk_month = [ x.text for x in list(tree.iter('T2RXXXXG3A')) ] | |
pk_year = [ x.text for x in list(tree.iter('T2RXXXXG3B')) ] | |
pk_sum = [ round(float(x.text.replace(',','.'))*100) for x in list(tree.iter('T2RXXXXG5')) ] | |
pk_date = [ ''.join([x[0],'.',x[1]]) for x in list(zip(pk_month,pk_year)) ] | |
pk = list(zip(pk_ipn,pk_date,pk_sum)) | |
return pk | |
except FileNotFoundError as e: | |
print("Cant found file '{0}'".format(e.filename)) | |
sys.exit() | |
except ParseError: | |
print('Incorrect XML file. Provide a correct file and try again.') | |
sys.exit() | |
def evaluate(l): | |
k = len(l) | |
r = 2**k - 1 | |
found = False | |
print('Options count: ',r) | |
print('Evaluating options...\n') | |
succsess_bin = [] | |
succsess_numbers = [] | |
for x in range(r)[1:]: | |
n = bin(x)[2:].rstrip('0') | |
if len(n)<k: | |
n = n.zfill(k) | |
s = 0 | |
p = [] | |
if n in succsess_bin: | |
continue | |
binary_list = [ i for i in enumerate(list(n)) ] | |
for j in binary_list: | |
if int(j[1]) == 1: | |
s = s + l[j[0]][2] | |
p.append( tuple([l[j[0]][0], l[j[0]][1], l[j[0]][2]/100]) ) # список чисел в комбинации | |
if s == f: | |
found = True | |
succsess_numbers.append(p) | |
succsess_bin.append(n) | |
print('Match at option ', n) | |
#print("Binary option view: ",n) | |
print('Matching numbers: ',p) | |
#print(10*'-') | |
if not found: | |
print('No match found') | |
else: | |
print("Matching options: ",succsess_bin) | |
print("Matching values: ",succsess_numbers) | |
if __name__ == '__main__': | |
try: | |
args = parser.parse_args() | |
print(args) | |
f = (args.pk).replace(',','.') | |
f = round(float(f.replace(',','.'))*100) | |
pk = parse(''.join([args.okpo,'.xml'])) | |
#pk_sum = [ x[2] for x in pk if x[2] <= f] | |
pk_sum_test = [ x for x in pk if x[2] <= f] | |
if sum([x[2] for x in pk_sum_test]) < f: | |
print('Overall sum of PK is smaller than you try to found.') | |
exit() | |
else: | |
print(pk_sum_test) | |
if args.month > 0: | |
pk_sum_test = [ x for x in pk_sum_test \ | |
if ((int(x[1].split('.')[0]) < args.month and int(x[1].split('.')[1]) == args.year) \ | |
or (int(x[1].split('.')[1]) < args.year)) ] | |
print(pk_sum_test) | |
evaluate(pk_sum_test) | |
except KeyboardInterrupt: | |
print('User abort.') | |
sys.exit() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment