Last active
May 9, 2016 23:36
-
-
Save sousatg/283d124d91bb17e1fcd713d312d23df8 to your computer and use it in GitHub Desktop.
https://ideone.com/hfJSvp object refactor
This file contains 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 | |
# -*- coding: utf-8 -*- | |
class Reff: | |
def __init__(self, ent_id, subent_id, order_id, order_value): | |
self.ent_id = ent_id | |
self.subent_id = subent_id | |
self.order_id = '0000' + order_id | |
self.order_value = order_value | |
array = [3, 30, 9, 90, 27, 76, 81, 34, 49, 5, 50, 15, 53, 45, 62, 38, 89, 17, 73, 51] | |
self.check() | |
self.order_value = '%01.2f' % self.order_value | |
self.generate_chk_str() | |
self.generate_chk_val( array ) | |
self.generate_chk_digits() | |
def check(self): | |
if len(self.ent_id) != 5: | |
print 'Lamentamos mas tem de indicar uma entidade válida' | |
exit() | |
if len(self.subent_id) == 0: | |
print 'Lamentamos mas tem de indicar uma subentidade válida' | |
exit() | |
if self.order_value < -1: | |
print 'Lamentamos mas não é possível gerar uma referência MB para valores inferiores a 1 Euro' | |
exit() | |
def generate_chk_str(self): | |
index = (lambda x: -(7-x) if x in [1,2] else -4) (len(self.subent_id)) | |
self.chk_str = self.ent_id + self.subent_id + self.order_id[index:] + str('%08.0f' % int(round(float(self.order_value) * 100))) | |
def generate_chk_val(self, chk_array): | |
chk_tuple = zip( chk_array, reversed( self.chk_str )) | |
self.chk_val = sum( [ (lambda (item, x) : (int(x) % 10) * item)(t) for t in chk_tuple ] ) % 97 | |
def generate_chk_digits(self): | |
self.chk_digits = '%02.0f' % (98-self.chk_val) | |
if __name__ == '__main__': | |
c = Reff('93999', '99', '0404', 14.5) | |
print c.ent_id | |
print c.order_value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment