From 163aa824f66c4c33aabd81494e219e57ef6e9f7d Mon Sep 17 00:00:00 2001 From: inso <insomniak.fr@gmaiL.com> Date: Wed, 19 Oct 2016 22:19:04 +0200 Subject: [PATCH] Money referentials --- src/sakia/app.py | 11 ++++---- src/sakia/money/base_referential.py | 17 +++++++----- src/sakia/money/dividend_per_day.py | 31 ++++++++++----------- src/sakia/money/quant_zerosum.py | 26 ++++++++++-------- src/sakia/money/quantitative.py | 17 +++++++----- src/sakia/money/relative.py | 42 ++++++++++++++++++----------- src/sakia/money/relative_to_past.py | 17 +++++++----- src/sakia/money/relative_zerosum.py | 25 +++++++++-------- src/sakia/money/udd_to_past.py | 7 +++-- 9 files changed, 113 insertions(+), 80 deletions(-) diff --git a/src/sakia/app.py b/src/sakia/app.py index 44894873..a49bb355 100644 --- a/src/sakia/app.py +++ b/src/sakia/app.py @@ -5,10 +5,7 @@ Created on 1 févr. 2014 """ import datetime -import json import logging -import os -import tarfile import aiohttp from PyQt5.QtCore import QObject, pyqtSignal, QTranslator, QCoreApplication, QLocale @@ -19,11 +16,11 @@ from . import __version__ from .options import SakiaOptions from sakia.data.connectors import BmaConnector from sakia.services import NetworkService, BlockchainService, IdentitiesService -from sakia.data.repositories import SakiaDatabase, BlockchainsRepo, CertificationsRepo, \ - IdentitiesRepo, NodesRepo, TransactionsRepo, ConnectionsRepo +from sakia.data.repositories import SakiaDatabase from sakia.data.processors import BlockchainProcessor, NodesProcessor, IdentitiesProcessor, CertificationsProcessor from sakia.data.files import AppDataFile, UserParametersFile from sakia.decorators import asyncify +from sakia.money import Relative class Application(QObject): @@ -177,3 +174,7 @@ class Application(QObject): logging.debug("Could not connect to github : {0}".format(str(e))) except Exception as e: pass + + @property + def current_ref(self): + return Relative \ No newline at end of file diff --git a/src/sakia/money/base_referential.py b/src/sakia/money/base_referential.py index 57adf671..3682c235 100644 --- a/src/sakia/money/base_referential.py +++ b/src/sakia/money/base_referential.py @@ -1,20 +1,25 @@ -from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QObject, QLocale -import asyncio class BaseReferential: """ Interface to all referentials """ - def __init__(self, amount, community, app, block_number=None): + def __init__(self, amount, currency, app, block_number=None): + """ + + :param int amount: + :param str currency: + :param sakia.app.Application app: + :param int block_number: + """ self.amount = amount - self.community = community self.app = app + self.currency = currency self._block_number = block_number @classmethod - def instance(cls, amount, community, app, block_number=None): - return cls(amount, community, app, block_number) + def instance(cls, amount, currency, app, block_number=None): + return cls(amount, currency, app, block_number) @classmethod def translated_name(self): diff --git a/src/sakia/money/dividend_per_day.py b/src/sakia/money/dividend_per_day.py index 7528c867..1b2749bb 100644 --- a/src/sakia/money/dividend_per_day.py +++ b/src/sakia/money/dividend_per_day.py @@ -1,6 +1,7 @@ -from PyQt5.QtCore import QObject, QCoreApplication, QT_TRANSLATE_NOOP, QLocale from .base_referential import BaseReferential from .udd_to_past import UDDToPast +from .currency import shortened +from ..data.processors import BlockchainProcessor from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale @@ -26,15 +27,16 @@ class DividendPerDay(BaseReferential): 100 UDD equal the Universal Dividend created per day. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod - def instance(cls, amount, community, app, block_number=None): + def instance(cls, amount, currency, app, block_number=None): if app.preferences['forgetfulness']: - return cls(amount, community, app, block_number) + return cls(amount, currency, app, block_number) else: - return UDDToPast(amount, community, app, block_number) + return UDDToPast(amount, currency, app, block_number) @classmethod def translated_name(cls): @@ -42,7 +44,7 @@ class DividendPerDay(BaseReferential): @property def units(self): - return QCoreApplication.translate("DividendPerDay", DividendPerDay._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("DividendPerDay", DividendPerDay._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -65,14 +67,13 @@ class DividendPerDay(BaseReferential): R = UD(t) of one day t = last UD block time - :param int amount: Value :param sakia.core.community.Community community: Community instance :return: float """ - dividend = await self.community.dividend() - params = await self.community.parameters() + dividend, base = self._blockchain_processor.last_ud(self.currency) + params = await self._blockchain_processor.parameters(self.currency) if dividend > 0: - return (self.amount * 100) / (float(dividend) / (params['dt'] / 86400)) + return (self.amount * 100) / (float(dividend * (10**base)) / (params.dt / 86400)) else: return self.amount @@ -82,25 +83,25 @@ class DividendPerDay(BaseReferential): async def localized(self, units=False, international_system=False): value = await self.value() prefix = "" - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", DividendPerDay._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value async def diff_localized(self, units=False, international_system=False): value = await self.differential() prefix = "" - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", DividendPerDay._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/quant_zerosum.py b/src/sakia/money/quant_zerosum.py index 767257c8..61b48668 100644 --- a/src/sakia/money/quant_zerosum.py +++ b/src/sakia/money/quant_zerosum.py @@ -1,6 +1,8 @@ from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale from . import Quantitative from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class QuantitativeZSum(BaseReferential): @@ -26,8 +28,9 @@ class QuantitativeZSum(BaseReferential): the value is under the average value. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls): @@ -35,7 +38,7 @@ class QuantitativeZSum(BaseReferential): @property def units(self): - return QCoreApplication.translate("QuantitativeZSum", QuantitativeZSum._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("QuantitativeZSum", QuantitativeZSum._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -47,7 +50,7 @@ class QuantitativeZSum(BaseReferential): @property def diff_units(self): - return QCoreApplication.translate("Quantitative", Quantitative._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("Quantitative", Quantitative._UNITS_STR_).format(shortened(self.currency)) async def value(self): """ @@ -66,16 +69,16 @@ class QuantitativeZSum(BaseReferential): :param sakia.core.community.Community community: Community instance :return: int """ - ud_block = await self.community.get_ud_block() - if ud_block and ud_block['membersCount'] > 0: - monetary_mass = await self.community.monetary_mass() - average = int(monetary_mass / ud_block['membersCount']) + last_members_count = self._blockchain_processor.last_members_count(self.currency) + monetary_mass = self._blockchain_processor.monetary_mass(self.currency) + if last_members_count != 0: + average = int(monetary_mass / last_members_count) else: average = 0 return self.amount - average async def differential(self): - return await Quantitative(self.amount, self.community, self.app).value() + return await Quantitative(self.amount, self.currency, self.app).value() async def localized(self, units=False, international_system=False): value = await self.value() @@ -91,10 +94,11 @@ class QuantitativeZSum(BaseReferential): QuantitativeZSum._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value async def diff_localized(self, units=False, international_system=False): - localized = await Quantitative(self.amount, self.community, self.app).localized(units, international_system) + localized = await Quantitative(self.amount, shortened(self.currency), self.app).localized(units, + international_system) return localized diff --git a/src/sakia/money/quantitative.py b/src/sakia/money/quantitative.py index aa4808e3..7ce9ecf7 100644 --- a/src/sakia/money/quantitative.py +++ b/src/sakia/money/quantitative.py @@ -1,5 +1,7 @@ from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class Quantitative(BaseReferential): @@ -16,8 +18,9 @@ class Quantitative(BaseReferential): ) _DESCRIPTION_STR_ = QT_TRANSLATE_NOOP('Quantitative', "Base referential of the money. Units values are used here.") - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls): @@ -25,7 +28,7 @@ class Quantitative(BaseReferential): @property def units(self): - return QCoreApplication.translate("Quantitative", Quantitative._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("Quantitative", Quantitative._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -81,7 +84,7 @@ class Quantitative(BaseReferential): value = await self.value() prefix = "" if international_system: - localized_value, prefix = Quantitative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Quantitative.to_si(value, self.app.parameters.digits_after_comma) else: localized_value = QLocale().toString(float(value), 'f', 0) @@ -90,7 +93,7 @@ class Quantitative(BaseReferential): Quantitative._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value @@ -98,7 +101,7 @@ class Quantitative(BaseReferential): value = await self.differential() prefix = "" if international_system: - localized_value, prefix = Quantitative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Quantitative.to_si(value, self.app.parameters.digits_after_comma) else: localized_value = QLocale().toString(float(value), 'f', 0) @@ -107,6 +110,6 @@ class Quantitative(BaseReferential): Quantitative._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/relative.py b/src/sakia/money/relative.py index 004deb6c..0c1c5ee0 100644 --- a/src/sakia/money/relative.py +++ b/src/sakia/money/relative.py @@ -1,6 +1,7 @@ -from PyQt5.QtCore import QObject, QCoreApplication, QT_TRANSLATE_NOOP, QLocale from .base_referential import BaseReferential from .relative_to_past import RelativeToPast +from .currency import shortened +from ..data.processors import BlockchainProcessor from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale @@ -28,15 +29,24 @@ class Relative(BaseReferential): the average. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod - def instance(cls, amount, community, app, block_number=None): - if app.preferences['forgetfulness']: - return cls(amount, community, app, block_number) + def instance(cls, amount, currency, app, block_number=None): + """ + + :param int amount: + :param str currency: + :param sakia.app.Application app: + :param int block_number: + :return: + """ + if app.parameters.forgetfulness: + return cls(amount, currency, app, block_number) else: - return RelativeToPast(amount, community, app, block_number) + return RelativeToPast(amount, currency, app, block_number) @classmethod def translated_name(cls): @@ -44,7 +54,7 @@ class Relative(BaseReferential): @property def units(self): - return QCoreApplication.translate("Relative", Relative._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("Relative", Relative._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -68,9 +78,9 @@ class Relative(BaseReferential): :param sakia.core.community.Community community: Community instance :return: float """ - dividend = await self.community.dividend() + dividend, base = self._blockchain_processor.last_ud(self.currency) if dividend > 0: - return self.amount / float(dividend) + return self.amount / float(dividend * (10**base)) else: return self.amount @@ -105,15 +115,15 @@ class Relative(BaseReferential): value = await self.value() prefix = "" if international_system: - localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) else: - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", Relative._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value @@ -121,14 +131,14 @@ class Relative(BaseReferential): value = await self.differential() prefix = "" if international_system and value != 0: - localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) else: - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", Relative._REF_STR_) \ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/relative_to_past.py b/src/sakia/money/relative_to_past.py index b92e5dc1..5da07200 100644 --- a/src/sakia/money/relative_to_past.py +++ b/src/sakia/money/relative_to_past.py @@ -1,5 +1,7 @@ from PyQt5.QtCore import QObject, QCoreApplication, QT_TRANSLATE_NOOP, QLocale, QDateTime from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class RelativeToPast(BaseReferential): @@ -24,8 +26,9 @@ class RelativeToPast(BaseReferential): This referential is practical to remember what was the value at the Time. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls): @@ -34,7 +37,7 @@ class RelativeToPast(BaseReferential): @property def units(self): return QCoreApplication.translate("RelativeToPast", RelativeToPast._UNITS_STR_).format('t', - self.community.short_currency) + shortened(self.currency)) @property def formula(self): return QCoreApplication.translate('RelativeToPast', RelativeToPast._FORMULA_STR_) @@ -72,7 +75,7 @@ class RelativeToPast(BaseReferential): async def localized(self, units=False, international_system=False): from . import Relative value = await self.value() - block = await self.community.get_ud_block() + last_ud_time = self._blockchain_processor.last_ud_time(self.currency) prefix = "" if international_system: localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) @@ -85,10 +88,10 @@ class RelativeToPast(BaseReferential): prefix, QLocale.toString( QLocale(), - QDateTime.fromTime_t(block['medianTime']).date(), + QDateTime.fromTime_t(last_ud_time).date(), QLocale.dateFormat(QLocale(), QLocale.ShortFormat) ), - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value @@ -111,6 +114,6 @@ class RelativeToPast(BaseReferential): QDateTime.fromTime_t(block['medianTime']).date(), QLocale.dateFormat(QLocale(), QLocale.ShortFormat) ), - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/relative_zerosum.py b/src/sakia/money/relative_zerosum.py index e6c8ea5a..c01c57de 100644 --- a/src/sakia/money/relative_zerosum.py +++ b/src/sakia/money/relative_zerosum.py @@ -1,6 +1,8 @@ from PyQt5.QtCore import QCoreApplication, QT_TRANSLATE_NOOP, QLocale from .relative import Relative from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class RelativeZSum(BaseReferential): @@ -25,8 +27,9 @@ class RelativeZSum(BaseReferential): the value is under the average value. """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls): @@ -34,7 +37,7 @@ class RelativeZSum(BaseReferential): @property def units(self): - return QCoreApplication.translate("RelativeZSum", RelativeZSum._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("RelativeZSum", RelativeZSum._UNITS_STR_).format(shortened(self.currency)) @property def formula(self): @@ -46,7 +49,7 @@ class RelativeZSum(BaseReferential): @property def diff_units(self): - return QCoreApplication.translate("Relative", Relative._UNITS_STR_).format(self.community.short_currency) + return QCoreApplication.translate("Relative", Relative._UNITS_STR_).format(shortened(self.currency)) async def value(self): """ @@ -75,22 +78,22 @@ class RelativeZSum(BaseReferential): return relative_value - relative_median async def differential(self): - return await Relative(self.amount, self.community, self.app).value() + return await Relative(self.amount, self.currency, self.app).value() async def localized(self, units=False, international_system=False): value = await self.value() prefix = "" if international_system: - localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) else: - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("RelativeZSum", RelativeZSum._REF_STR_)\ .format(localized_value, prefix, - self.community.short_currency if units else "") + shortened(self.currency) if units else "") else: return localized_value @@ -99,12 +102,12 @@ class RelativeZSum(BaseReferential): prefix = "" if international_system and value != 0: - localized_value, prefix = Relative.to_si(value, self.app.preferences['digits_after_comma']) + localized_value, prefix = Relative.to_si(value, self.app.parameters.digits_after_comma) else: - localized_value = QLocale().toString(float(value), 'f', self.app.preferences['digits_after_comma']) + localized_value = QLocale().toString(float(value), 'f', self.app.parameters.digits_after_comma) if units or international_system: return QCoreApplication.translate("Relative", Relative._REF_STR_)\ - .format(localized_value, prefix, self.community.short_currency if units else "") + .format(localized_value, prefix, shortened(self.currency) if units else "") else: return localized_value diff --git a/src/sakia/money/udd_to_past.py b/src/sakia/money/udd_to_past.py index fc85821f..38667963 100644 --- a/src/sakia/money/udd_to_past.py +++ b/src/sakia/money/udd_to_past.py @@ -1,5 +1,7 @@ from PyQt5.QtCore import QObject, QCoreApplication, QT_TRANSLATE_NOOP, QLocale, QDateTime from .base_referential import BaseReferential +from .currency import shortened +from ..data.processors import BlockchainProcessor class UDDToPast(BaseReferential): @@ -27,8 +29,9 @@ class UDDToPast(BaseReferential): Relative value R is calculated by dividing the quantitative value Q by the """.replace('\n', '<br >')) - def __init__(self, amount, community, app, block_number=None): - super().__init__(amount, community, app, block_number) + def __init__(self, amount, currency, app, block_number=None): + super().__init__(amount, currency, app, block_number) + self._blockchain_processor = BlockchainProcessor.instanciate(self.app) @classmethod def translated_name(cls): -- GitLab