def pi(P): from decimal import Decimal # attention, nombre pair obligatoire (voir commentaire) C = Decimal(4) / 1 I = 1 for i in range(P, 0, -1): z = Decimal(4) / (I + 2) if i % 2 == 0: z = Decimal.copy_negate(z) C += z I += 2
def setup() : P = 10000000 C = Decimal(4) / 1 I = 1 for i in range(P,0,-1): z = Decimal(4) / (I+2) if i%2 == 0: z = Decimal.copy_negate(z) C = C + z I = I + 2 print(C)
def full_hydrate(self, bundle): bundle = super(SplitTransactionResource, self).full_hydrate(bundle) # this must happen after all hydrations because order isn't garanteed value = bundle.data.get('total_value') if value: # casting value to str to avoid repeating decimals value = Decimal(str(value)).copy_abs() if bundle.obj.category.is_negative: value = value.copy_negate() bundle.data['total_value'] = value return bundle
def test_add_lots(self): """ Simulates a synchronisation of a large amount of transactions """ a = Account.objects.get(pk=1) p = Payee.objects.get(pk=1) total = a.balance orig = total.copy_abs() count = Transaction.objects.all().count() # Turn the signal listening off so we can update the account balance afterwards Transaction.listen_off() for i in range(1, 100): amount = Decimal('%.2f' % i) if i > 50: amount = amount.copy_negate() t = Transaction(account=a, mobile=False, payee=p, amount=amount, date=datetime.date.today()) t.save() total += amount count += 1 # Turn signal listening back on Transaction.listen_on() a = Account.objects.get(pk=1) # Make sure the balance hasn't been updated self.assertEqual(a.balance, orig) # Make sure the right number of transactions has been added self.assertEqual(Transaction.objects.all().count(), count)
def parse_invoice(self, invoice_element): invoice = {} invoice['id'] = invoice_element.attrib['id'] invoice['number'] = invoice_element.findtext('number') invoice['type'] = invoice_element.findtext('type') invoice['vat_rate'] = invoice_element.findtext('vatRate') invoice['billing_datetime'] = self.parse_datetime(invoice_element.findtext('billingDatetime')) invoice['paid_transaction_id'] = invoice_element.findtext('paidTransactionId') invoice['created_datetime'] = self.parse_datetime(invoice_element.findtext('createdDatetime')) invoice['charges'] = self.parse_charges(invoice_element.find('charges')) invoice['paid_datetime'] = '' if invoice['paid_transaction_id']: transactions = invoice_element.find('transactions') for transaction in transactions: if transaction.attrib['id'] == invoice['paid_transaction_id']: invoice['paid_datetime'] = self.parse_datetime(transaction.findtext('transactedDatetime')) break invoice['refunded'] = False if invoice['paid_transaction_id']: paid_transaction_amount = Decimal('0') childeren_of_paid_transaction_sum = Decimal('0') transactions = invoice_element.find('transactions') for transaction in transactions: if transaction.findtext('parentId') == invoice['paid_transaction_id']: amount = self.parse_decimal(transaction.findtext('amount')) childeren_of_paid_transaction_sum += amount elif transaction.attrib['id'] == invoice['paid_transaction_id']: paid_transaction_amount = self.parse_decimal(transaction.findtext('amount')) refunded_equals_received = (childeren_of_paid_transaction_sum.copy_negate() == paid_transaction_amount) if paid_transaction_amount != 0 and refunded_equals_received: invoice['refunded'] = True return invoice
def test_add_lots(self): """ Simulates a synchronisation of a large amount of transactions """ a = Account.objects.get(pk=1) p = Payee.objects.get(pk=1) total = a.balance orig = total.copy_abs() count = Transaction.objects.all().count() # Turn the signal listening off so we can update the account balance afterwards Transaction.listen_off() for i in range(1, 100): amount = Decimal("%.2f" % i) if i > 50: amount = amount.copy_negate() t = Transaction(account=a, mobile=False, payee=p, amount=amount, date=datetime.date.today()) t.save() total += amount count += 1 # Turn signal listening back on Transaction.listen_on() a = Account.objects.get(pk=1) # Make sure the balance hasn't been updated self.assertEqual(a.balance, orig) # Make sure the right number of transactions has been added self.assertEqual(Transaction.objects.all().count(), count)
def _bill_to_rows(bill, cancel=False): """Map bills to Procountor CSV format http://support.procountor.com/fi/aineiston-sisaanluku/laskuaineiston-siirtotiedosto.html """ rows = [] c = bill.billingcycle if c.membership.type in ['H']: return rows bill_delivery = ProcountorBillDelivery.NO_DELIVERY if c.membership.get_billing_contact(): billing_address = '%s\%s\%s\%s\%s' % ( c.membership.name(), c.membership.get_billing_contact().street_address, c.membership.get_billing_contact().postal_code, c.membership.get_billing_contact().post_office, 'FI') billing_email = c.membership.get_billing_contact().email else: billing_email = "" billing_address = "" if bill_delivery == ProcountorBillDelivery.POST: logger.critical( "No billing contact found for member {member}".format( member=str(c.membership))) return [] else: logger.warn("No billing contact found for member {member}".format( member=str(c.membership))) rows.append([ 'M', # laskutyyppi 'EUR', # valuuttakoodi c.reference_number, # viitenumero settings.IBAN_ACCOUNT_NUMBER, # pankkitili '', # Y-tunnus/HETU/ALV-tunnus 'tilisiirto', # Maksutapa c.membership.name(), # Liikekumppanin nimi '', # Toimitustapa '0', # Laskun alennus % 't', # Sis. alv koodi 'f' if cancel else 't', # Hyvityslaskukoodi '0.0', # Viivästyskorko % ft(bill.created), # Laskun päivä ft(bill.created), # Toimituspäivämäärä ft(bill.created + timedelta(days=settings.BILL_DAYS_TO_DUE)), # Eräpäivämäärä '', # Liikekumppanin osoite billing_address, # Laskutusosoite '', # Toimitusosoite '', # Laskun lisätiedot '%s %d sikteerissä, tuotu %s, jäsen %d' % ('Hyvityslasku' if cancel else 'Lasku', bill.id, ft( datetime.now()), c.membership.id), # Muistiinpanot billing_email, # Sähköpostiosoite '', # Maksupäivämäärä '', # Valuuttakurssi "%.2f" % Decimal.copy_negate(c.get_fee()) if cancel else c.get_fee(), # Laskun loppusumma "%d" % c.get_vat_percentage(), # ALV-% '%d' % bill_delivery, # Laskukanava '', # Verkkolaskutunnus '%d' % bill.id, # Tilausviite 't', # Kirjanpito riveittäin -koodi) '', # Finvoice-osoite 1(ei enää käytössä) '', # Finvoice-osoite 2(ei enää käytössä) '%d' % c.membership.id, # Asiakasnumero 'X', # Automaattinen lähetys tai maksettu muualla tieto '', # Liitetiedoston nimi ZIP-paketissa '', # Yhteyshenkilö '', # Liikekumppanin pankin SWIFT-tunnus '', # Verkkolaskuoperaattori '', # Liikekumppanin OVT-tunnus "%s" % bill.id, # Laskuttajan laskunumero '', # Faktoring-rahoitussopimuksen numero '', # ALV-käsittelyn maakoodi '', # Kielikoodi '0', # Käteisalennuksen päivien lukumäärä '0' # Käteisalennuksen prosentti ]) member_type = settings.BILLING_ACCOUNTING_MAP[c.membership.type] r2 = [ '', # TYHJÄ '', # Tuotteen kuvaus '%s%s' % (member_type[0], c.start.strftime("%y")), # Tuotteen koodi '-1' if cancel else '1', # Määrä '', # Yksikkö '%.2f' % c.get_fee(), # Yksikköhinta '0', # Rivin alennusprosentti "%d" % c.get_vat_percentage(), # Rivin ALV-% '', # Rivikommentti '', # Tilausviite '', # Asiakkaan ostotilausnumero '', # Tilausvahvistusnumero '', # Lähetysluettelonumero '%s' % member_type[1] # Kirjanpitotili ] r2 += [''] * (len(rows[0]) - len(r2)) rows.append(r2) return rows
def _bill_to_rows(bill, cancel=False): """Map bills to Procountor CSV format http://support.procountor.com/fi/aineiston-sisaanluku/laskuaineiston-siirtotiedosto.html """ rows = [] c = bill.billingcycle if c.membership.type in ['H']: return rows bill_delivery = ProcountorBillDelivery.NO_DELIVERY if c.membership.get_billing_contact(): billing_address = '%s\%s\%s\%s\%s' % (c.membership.name(), c.membership.get_billing_contact().street_address, c.membership.get_billing_contact().postal_code, c.membership.get_billing_contact().post_office, 'FI') billing_email = c.membership.get_billing_contact().email else: billing_email = "" billing_address = "" if bill_delivery == ProcountorBillDelivery.POST: logger.critical("No billing contact found for member {member}".format(member=str(c.membership))) return [] else: logger.warn("No billing contact found for member {member}".format(member=str(c.membership))) rows.append([ 'M', # laskutyyppi 'EUR', # valuuttakoodi c.reference_number, # viitenumero settings.IBAN_ACCOUNT_NUMBER, # pankkitili '', # Y-tunnus/HETU/ALV-tunnus 'tilisiirto', # Maksutapa c.membership.name(), # Liikekumppanin nimi '', # Toimitustapa '0', # Laskun alennus % 't', # Sis. alv koodi 'f' if cancel else 't', # Hyvityslaskukoodi '0.0', # Viivästyskorko % ft(bill.created), # Laskun päivä ft(bill.created), # Toimituspäivämäärä ft(bill.created + timedelta(days=settings.BILL_DAYS_TO_DUE)), # Eräpäivämäärä '', # Liikekumppanin osoite billing_address, # Laskutusosoite '', # Toimitusosoite '', # Laskun lisätiedot '%s %d sikteerissä, tuotu %s, jäsen %d' % ('Hyvityslasku' if cancel else 'Lasku', bill.id, ft(datetime.now()), c.membership.id), # Muistiinpanot billing_email, # Sähköpostiosoite '', # Maksupäivämäärä '', # Valuuttakurssi "%.2f" % Decimal.copy_negate(c.get_fee()) if cancel else c.get_fee(), # Laskun loppusumma "%d" % c.get_vat_percentage(), # ALV-% '%d' % bill_delivery, # Laskukanava '', # Verkkolaskutunnus '%d' % bill.id, # Tilausviite 't', # Kirjanpito riveittäin -koodi) '', # Finvoice-osoite 1(ei enää käytössä) '', # Finvoice-osoite 2(ei enää käytössä) '%d' % c.membership.id, # Asiakasnumero 'X', # Automaattinen lähetys tai maksettu muualla tieto '', # Liitetiedoston nimi ZIP-paketissa '', # Yhteyshenkilö '', # Liikekumppanin pankin SWIFT-tunnus '', # Verkkolaskuoperaattori '', # Liikekumppanin OVT-tunnus "%s" % bill.id, # Laskuttajan laskunumero '', # Faktoring-rahoitussopimuksen numero '', # ALV-käsittelyn maakoodi '', # Kielikoodi '0', # Käteisalennuksen päivien lukumäärä '0' # Käteisalennuksen prosentti ]) member_type = settings.BILLING_ACCOUNTING_MAP[c.membership.type] r2 = ['', # TYHJÄ '', # Tuotteen kuvaus '%s%s' % (member_type[0], c.start.strftime("%y")), # Tuotteen koodi '-1' if cancel else '1', # Määrä '', # Yksikkö '%.2f' % c.get_fee(), # Yksikköhinta '0', # Rivin alennusprosentti "%d" % c.get_vat_percentage(), # Rivin ALV-% '', # Rivikommentti '', # Tilausviite '', # Asiakkaan ostotilausnumero '', # Tilausvahvistusnumero '', # Lähetysluettelonumero '%s' % member_type[1] # Kirjanpitotili ] r2 += [''] * (len(rows[0]) - len(r2)) rows.append(r2) return rows
from decimal import Decimal def setup() : P = 10000000 C = Decimal(4) / 1 I = 1 for i in range(P,0,-1): z = Decimal(4) / (I+2) if i%2 == 0: z = Decimal.copy_negate(z) C = C + z I = I + 2 print(C) def draw() : P = 10000000 C = Decimal(4) / 1 I = 1 for i in range(P,0,-1): z = Decimal(4) / (I+2) if i%2 == 0: z = Decimal.copy_negate(z) C = C + z I = I + 2 print(C)
class Qualean(object): ''' Qualean(x) -> Imaginary Number ranging from -1 to 1. Convert a real state of either -1, 0 or 1 into an imaginary state of any number ranging between -1 to 1. Qualean = input * random.uniform(-1,1) Methods defined here: __abs__(self) abs(self.num) __add__(self, other) Return self.num + other.num. __and__(self, other) Return self.num & other.num. __bool__(self) self != 0 __eq__(self, other) Return self.num == other.num. __float__(self) float(self.num) __ge__(self, other) Return self.num >= other.nun. __gt__(self, other) Return self.num > other.nun. __invertsign__(self) Return -self __le__(self, other) Return self.num <= other.nun. __lt__(self, other) Return Return self.num < other.nun. __mul__(self, other) Return self.num * other.num. __ne__(self, value, /) Return self!=value. __or__(self, value, /) Return self|value. __repr__(self, /) Return repr(self). __round__(self, prec, rounding) Arguments: prec: Precision to be rounded rounding: Type of Rounding Rounding options include ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP Return Quantized Rounding. __str__(self, /) Return str(self). __sub__(self, other) Return self-other. sizeof(self) Returns size in memory, in bytes. conjugate(self) Returns the complex conjugate. ''' def __init__(self, input: int): if not isinstance(input, (int, float)): raise TypeError( f"unsupported type {type(input)}. Please enter one among (-1, 0, 1)") if input not in (1, -1, 0, -1.0, -0.0, 1.0): raise ValueError( f'Invalid INput, Input must be 1 or 0 or -1, you entered {input}') self.input = input self.num = Decimal(self.input * random.uniform(-1, 1)) self.num = self.num.quantize( Decimal('0.0000000001'), rounding='ROUND_HALF_EVEN') def __abs__(self): return abs(self.num) def __and__(self, other): return bool(self.num and other.num) def __or__(self, other): return bool(self.num or other.num) def __add__(self, other): if isinstance(other, (int, float, Decimal)): return self.num + other if not isinstance(other, Qualean): raise TypeError( f"unsupported operand type(s) for +: Can not add 'Qualean' and {type(other)}") return self.num + other.num def __sub__(self, other): if isinstance(other, (int, float, Decimal)): return self.num - other if not isinstance(other, Qualean): raise TypeError( f"unsupported operand type(s) for -: Can not subtract 'Qualean' and {type(other)}") return self.num - other.num def __eq__(self, other): if not isinstance(other, Qualean): raise TypeError( f"unsupported operand type(s) for ==: Can not equate 'Qualean' and {type(other)}") return self.num == other.num def __ne__(self, other): if not isinstance(other, Qualean): raise TypeError( f"unsupported operand type(s) for !=: Can not equate 'Qualean' and {type(other)}") return self.num != other.num def __float__(self): return float(self.num) def __Decimal__(self): return self.num def __ge__(self, other): if not isinstance(other, Qualean): raise TypeError( f"unsupported operand type(s) for >=: Can not compare 'Qualean' and {type(other)}") return self.num >= other.num def __gt__(self, other): if not isinstance(other, Qualean): raise TypeError( f"unsupported operand type(s) for >: Can not compare 'Qualean' and {type(other)}") return self.num > other.num def __le__(self, other): if not isinstance(other, Qualean): raise TypeError( f"unsupported operand type(s) for <=: Can not compare 'Qualean' and {type(other)}") return self.num <= other.num def __lt__(self, other): if not isinstance(other, Qualean): raise TypeError( f"unsupported operand type(s) for <: Can not compare 'Qualean' and {type(other)}") return self.num < other.num def __invertsign__(self): return self.num.copy_negate() def __neg__(self): neg_q = Qualean(0) neg_q.num = -self.num return neg_q def __mul__(self, other): if isinstance(other, (int, float, Decimal)): return self.num * other if not isinstance(other, Qualean): raise TypeError( f"unsupported operand type(s) for *: Can not multiply 'Qualean' and {type(other)}") mul_q = Qualean(0) mul_q.num = self.num * other.num return mul_q def __round__(self, prec=1e-10, rounding='ROUND_HALF_EVEN'): return self.num.quantize(Decimal(str(prec)), rounding=rounding) def __sqrt__(self): if self.num >= 0: return self.num.sqrt() return complex(0, (-1*self.num).sqrt()) def __bool__(self): return self.num != 0 def __repr__(self): return f'Hey!, I am a Qualean with value {self.num} and I am very VOLATILE, so every time you call me I CHANGE' def __str__(self): return str(self.num) def sizeof(self): return sys.getsizeof(self.num) def conjugate(self): return complex(0, self.num)