def base_round_zero(self, val, n):
if val is not None:
val = Decimal(val, getcontext())
else:
val = Decimal(0, getcontext())
return val.__round__(n)
python类getcontext()的实例源码
def get_rpc_request_bytes(connection, procname, params=[]):
buf = _int_to_4bytes(22)
buf += _int_to_4bytes(18)
buf += _int_to_2bytes(2)
buf += connection.transaction_id
buf += _int_to_4bytes(1) # request count
buf += _int_to_2bytes(len(procname))
buf += _str_to_bytes(procname)
buf += bytes([0x00, 0x00]) # OptionFlags
for p in params:
buf += bytes([0, 0]) # name="", StatusFlags
if p is None:
buf += bytes([INTNTYPE, 2])
buf += bytes([0])
elif isinstance(p, int):
buf += bytes([INTNTYPE, 4])
buf += bytes([4]) + p.to_bytes(4, byteorder='little')
elif isinstance(p, str):
ln = len(p) * 2
buf += bytes([NCHARTYPE]) + ln.to_bytes(2, byteorder='little')
buf += _int_to_2bytes(connection.lcid) + bytes([0, 0, 0])
buf += ln.to_bytes(2, byteorder='little') + _str_to_bytes(p)
elif isinstance(p, decimal.Decimal):
sign, digits, disponent = p.as_tuple()
if disponent > 0:
exp = 256 - disponent
else:
exp = -disponent
v = 0
ln = len(digits)
for i in range(ln):
v += digits[i] * (10 ** (ln - i - 1))
buf += bytes([DECIMALNTYPE, 9])
buf += bytes([decimal.getcontext().prec, exp])
buf += bytes([9, bool(not sign)]) + _int_to_8bytes(v)
else:
# another type. pack as string parameter
s = str(p)
ln = len(s) * 2
buf += bytes([NCHARTYPE]) + ln.to_bytes(2, byteorder='little')
buf += _int_to_2bytes(connection.lcid) + bytes([0, 0, 0])
buf += ln.to_bytes(2, byteorder='little') + _str_to_bytes(s)
return buf
def __init__(self):
decimal.getcontext().prec = 5
self.config_file = ConfigurationFile.ConfigurationFile()
self.port = 8989
self.listen_address = "localhost"
self.server_socket = None
self.wallets = []
self.authority_wallet = None
self.clients = []
self.ca_name = "unnamed"
self.transactions = []
self.coins_per_challenge = 0
self.minutes_per_challenge = 0
self.ssl_on = True
self.ssl_cert = ""
self.available_challenges = []
self.prefix_length = 4
self.challenge_thread = None
self.max_requests_per_minutes = 30
self.initial_cooldown_length = 60
self.invalid_submission_allowed = 5 # within 5 minutes
self.supervisor_key = ''
self.emit_coins = False
self.min_transaction_amount = 0
self.submissions_allowed_ips = []
self.statistic = ServerStatistic.ServerStatistic()
self.read_vars_from_config()
self.database = ServerDatabase.ServerDatabase(
self.config_file.get_string(
"db_user", "cacoins"), self.config_file.get_string(
"db_password", ""), self.config_file.get_string(
"db_name", "cacoins"))
# commands handler
self.commands_handler = []
self.fill_commands_handler()
self.ca_private_key = None
self.ca_public_key = None
self.ca_wallet_id = None
self.wallet_keys = {}