本文整理汇总了Python中src.transaction.Transaction类的典型用法代码示例。如果您正苦于以下问题:Python Transaction类的具体用法?Python Transaction怎么用?Python Transaction使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Transaction类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: endow_labour
def endow_labour(self, environment, time):
# We make sure household get their labour endowment per step
for household in environment.households:
# First, we set a control variable that makes sure we have exactly
# one transaction with "manhours", though this should in general
# be the case, this should always run through the second if
check = 0
for tranx in household.accounts:
if tranx.type_ == "manhours":
check = check + 1 # We check how many transactions with manhours are there for the household
# If there are no "manhours" transactions then we create one and add it to the household's accounts
if check == 0:
# The amount is equal to the parameter read from the config of the household
amount = household.labour
# We create the transaction
transaction = Transaction()
# We add the appropriate values to the transaction
transaction.this_transaction("manhours", "", household.identifier, household.identifier, amount, 0, 0, -1)
# It's important to add the transaction using the method
# from Transaction class and not manually
transaction.add_transaction(environment)
else:
# If we have more than one "mahhours" transaction we raise an error
raise LookupError("Labour transactions for a household haven't been properly removed.")
logging.info(" labour endowed on step: %s", time)
示例2: add_transaction
def add_transaction(self, type_, asset, from_id, to_id, amount, interest, maturity, time_of_default):
from src.transaction import Transaction
transaction = Transaction()
transaction.this_transaction(type_, asset, from_id, to_id, amount, interest, maturity, time_of_default)
self.accounts.append(transaction)
del transaction # append() above does make a copy so we may delete for garbage collection
示例3: make_deposits
def make_deposits(self, environment, time):
for household in environment.households:
cash = 0.0 # total of cash available for the household
control_deposits = 0 # checking if the household already has deposits
# We calculate the available cash
for tranx in household.accounts:
if tranx.type_ == "cash":
cash = cash + tranx.amount
tranx.amount = 0
# And the number of existing deposits
if tranx.type_ == "deposits":
control_deposits = control_deposits + 1
# And move all available cash to deposits at the end of the step
# If there are no deposits we create one in a bank
# The bank is chosen randomly
if control_deposits == 0:
# We choose a bank randomly
random_bank = random.choice(environment.banks)
# Create a transaction
transaction = Transaction()
# Add the appropriate values to the transaction
transaction.this_transaction("deposits", "", household.identifier, random_bank,
amount, random_bank.interest_rate_deposits, 0, -1)
# And add the transaction to the books (do it through function/not manually)
transaction.add_transaction(environment)
# If there are previous deposits we expand them linearly
else:
for tranx in household.accounts:
if tranx.type_ == "deposits":
# We add the remaining cash to the existing deposits
# in equal proportions
# Perhaps this can be done proportionate with regards
# to the value of these deposits, but it's minor at this point
tranx.amount = tranx.amount + (cash/control_deposits)
logging.info(" deposits made on step: %s", time)
示例4: transaction__get_time_of_default
def transaction__get_time_of_default(self, args):
import os
from src.bank import Bank
from src.household import Household
from src.firm import Firm
from src.environment import Environment
from src.transaction import Transaction
text = "This test checks transaction.get_time_of_default \n"
self.print_info(text)
#
# INITIALIZATION
#
environment_directory = str(args[0])
identifier = str(args[1])
log_directory = str(args[2])
# Configure logging parameters so we get output while the program runs
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
filename=log_directory + identifier + ".log", level=logging.INFO)
logging.info('START logging for test transaction__get_time_of_default in run: %s',
environment_directory + identifier + ".xml")
# Construct household filename
environment = Environment(environment_directory, identifier)
#
# TESTING
#
print("Creating a transaction")
transaction = Transaction()
transaction.time_of_default = 1
print("Time of default: ")
print(transaction.get_time_of_default())
示例5: transaction__add_transaction
def transaction__add_transaction(self, args):
import os
from src.bank import Bank
from src.household import Household
from src.firm import Firm
from src.environment import Environment
from src.transaction import Transaction
text = "This test checks transaction.add_transaction \n"
self.print_info(text)
#
# INITIALIZATION
#
environment_directory = str(args[0])
identifier = str(args[1])
log_directory = str(args[2])
# Configure logging parameters so we get output while the program runs
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %H:%M:%S',
filename=log_directory + identifier + ".log", level=logging.INFO)
logging.info('START logging for test transaction__add_transaction in run: %s',
environment_directory + identifier + ".xml")
# Construct household filename
environment = Environment(environment_directory, identifier)
# generate a bank
bank = Bank()
bank.identifier = "test_bank"
environment.banks.append(bank)
# generate a firm
firm = Firm()
firm.identifier = "test_firm"
environment.firms.append(firm)
# generate a household
household = Household()
household.identifier = "test_household"
environment.households.append(household)
#
# TESTING
#
print("Creating a transaction")
transaction = Transaction()
print("Assigning values")
transaction.this_transaction("type", "asset", "test_household", "test_firm", 1, 2, 3, 4)
print("Adding the transaction to the books")
transaction.add_transaction(environment)
print("The transaction:")
print(transaction)
print("The firm:")
print(environment.get_agent_by_id("test_firm"))
print("The household:")
print(environment.get_agent_by_id("test_household"))
示例6: transfer_required_deposits
def transfer_required_deposits(self):
from src.transaction import Transaction
transaction = Transaction()
value = round(float(self.parameters["r"]*self.get_account("D")), 4)
transaction.this_transaction("rD", self.identifier, -3, value, self.parameters["rb"], 0, -1)
self.accounts.append(transaction)
return -1.0*value
示例7: initialize_standard_firm
def initialize_standard_firm(self, firm, environment):
from src.transaction import Transaction
firm.identifier = "standard_firm_id" # identifier
firm.parameters["productivity"] = 1.20 # how much goods do we get from 1 unit of labour
# loans - we get the first bank from the list of banks
# if there are no banks it will be a blank which is fine for testing
amount = 250.0
transaction = Transaction()
transaction.this_transaction("loans", "", environment.banks[0:1][0], firm.identifier,
amount, environment.banks[0:1][0].interest_rate_loans, 0, -1)
# environment.banks[0:1][0] is only for testing purposes DO NOT USE IN PRODUCTION
# what it does is is takes the first bank in environment, but if there are no
# banks (which happens in testing) it doesn't break down
firm.accounts.append(transaction)
# money - cash and equivalents
amount = 200.0
transaction = Transaction()
transaction.this_transaction("cash", "", firm.identifier, firm.identifier, amount, 0, 0, -1)
firm.accounts.append(transaction)
# goods - unique production
amount = 50.0
transaction = Transaction()
transaction.this_transaction("goods", "", firm.identifier, firm.identifier, amount, 0, 0, -1)
firm.accounts.append(transaction)
示例8: initialize_standard_bank
def initialize_standard_bank(self, bank, environment):
from src.transaction import Transaction
bank.identifier = "standard_bank_id"
# deposits - we get the first household from the list of households
# if there are no households it will be a blank which is fine for testing
amount = 250.0
transaction = Transaction()
transaction.this_transaction("deposits", "", environment.households[0:1][0], bank.identifier,
amount, bank.interest_rate_deposits, 0, -1)
# environment.households[0:1][0] is only for testing purposes DO NOT USE IN PRODUCTION
# what it does is is takes the first household in environment, but if there are no
# households (which happens in testing) it doesn't break down
bank.accounts.append(transaction)
# money - cash and equivalents
amount = 100.0
transaction = Transaction()
transaction.this_transaction("cash", "", bank.identifier, bank.identifier,
amount, 0, 0, -1)
bank.accounts.append(transaction)
# loans - we get the first firm from the list of firms
# if there are no firms it will be a blank which is fine for testing
amount = 150.0
transaction = Transaction()
transaction.this_transaction("loans", "", bank.identifier, environment.firms[0:1][0],
amount, bank.interest_rate_loans, 0, -1)
# environment.firms[0:1][0] is only for testing purposes DO NOT USE IN PRODUCTION
# what it does is is takes the first firm in environment, but if there are no
# firms (which happens in testing) it doesn't break down
bank.accounts.append(transaction)
示例9: initialize_standard_household
def initialize_standard_household(self, household, environment):
from src.transaction import Transaction
household.identifier = "standard_household_id" # identifier
household.parameters["labour"] = 24.00 # labour to sell per step
household.parameters["propensity_to_save"] = 0.40 # propensity to save
# percentage of income household wants to save as deposits
# deposits - we get the first bank from the list of banks
# if there are no banks it will be a blank which is fine for testing
amount = 200.0
transaction = Transaction()
transaction.this_transaction("deposits", "", household.identifier, environment.banks[0:1][0],
amount, environment.banks[0:1][0].interest_rate_deposits, 0, -1)
# environment.banks[0:1][0] is only for testing purposes DO NOT USE IN PRODUCTION
# what it does is is takes the first bank in environment, but if there are no
# banks (which happens in testing) it doesn't break down
household.accounts.append(transaction)
# money - cash and equivalents
amount = 50.0
transaction = Transaction()
transaction.this_transaction("cash", "", household.identifier, household.identifier, amount, 0, 0, -1)
household.accounts.append(transaction)
# manhours - labour to sell
amount = 250.0
transaction = Transaction()
transaction.this_transaction("manhours", "", household.identifier, household.identifier, amount, 0, 0, -1)
household.accounts.append(transaction)
示例10: transfer_excess_reserves
def transfer_excess_reserves(self):
from src.transaction import Transaction
availableVolume = self.parameters["Q"]
plannedVolume = self.parameters["gamma"]*(1.0-self.parameters["lamb"])*self.parameters["V"]
transactionVolume = round(min(plannedVolume, availableVolume), 4)
self.parameters["Q"] = round(self.parameters["Q"] - transactionVolume, 4)
if (self.parameters["Q"] < 0.0):
logging.info("ERROR: Q negative in transfer_excess_reserves")
transaction = Transaction()
transaction.this_transaction("E", self.identifier, -3, transactionVolume, self.parameters["rb"], 0, -1)
self.accounts.append(transaction)
del transaction
示例11: do_update
def do_update(self, environment, time):
# As a first step, we accrue all interest over the transactions
# Thus, important to notice to keep 0 as interest by default
# Unless transaction should carry interest
# DON'T DO INTERESTS SO FAR, DO ONCE THE REST WORKS
self.accrue_interests(environment, time)
# Then agents get their labour endowment for the step (e.g. work hours to spend)
# For now we don't need to keep track of labour left as there is no queue
# self.endow_labour(environment, time)
# The households sell labour to firms
self.sell_labour(environment, time)
# The firms sell goods to households
self.consume_rationed(environment, time)
# We net deposits and loans
self.net_loans_deposits(environment, time)
# We remove goods and labour (perishable) and are left with capital
self.net_labour_goods(environment, time)
# Purging accounts at every step just in case
transaction = Transaction()
transaction.purge_accounts(environment)
示例12: do_update
def do_update(self, environment, time):
# As a first step, we accrue all interest over the transactions
# Thus, important to notice to keep 0 as interest by default
# Unless transaction should carry interest
self.accrue_interests(environment, time)
# The households sell labour to firms
self.sell_labour(environment, time)
# The firms sell goods to households
self.consume_rationed(environment, time)
# We net deposits and loans
self.net_loans_deposits(environment, time)
# We remove the perishable transactions
self.remove_perishable(environment, time)
# And add capital to balance the books
self.capitalise(environment, time)
# Investing of the banks
self.invest(environment, time)
# Purging accounts at every step just in case
transaction = Transaction()
transaction.purge_accounts(environment)
示例13: find_incoming_utxos
def find_incoming_utxos(block_hash, transactions, isGenesis=False):
"""
Iterates through all the outputs and looks for any directed to user's wallet.
If found, save to the utxo pool
:return:
"""
myAddress = SHA256.new(get_public_key("string").encode()).hexdigest()
conf = Configuration()
for tnx_id, tnx_info in transactions.items():
# deserialize transaction
tnx_payload = tnx_info
tnx_payload["transaction_id"] = tnx_id
tnx = Transaction(payload=tnx_payload)
for index in range(len(tnx.outputs)):
if tnx.outputs[index]["address"] == myAddress and not isGenesis:
save_utxo(tnx.get_transaction_id(), index, block_hash, tnx.outputs[index]["amount"])
conf.add_balance(tnx.outputs[index]["amount"])
elif tnx.outputs[index]["address"] == myAddress and isGenesis:
save_utxo(tnx.get_transaction_id(), -1, block_hash, tnx.outputs[index]["amount"])
conf.add_balance(tnx.outputs[index]["amount"])
示例14: produce
def produce(self, environment, time):
# We take all the labour and turn it into goods
for firm in environment.firms: # We do it for every firm
production_factors = 0 # Here, we count how much labour the firm has
for tranx in firm.accounts:
if tranx.type_ == "manhours":
# We move the labour to production as a production factor
# First, we move it to production factors used below
# Then we will remove it from the books
production_factors = production_factors + tranx.amount
# Amount produced is labour * productivity in this simple model
amount = production_factors * firm.productivity
# Create a transaction
transaction = Transaction()
# Add the appropriate values to the transaction
transaction.this_transaction("goods", "", firm.identifier, firm.identifier,
amount, 0, 0, -1)
# And add the transaction to the books (do it through function/not manually)
transaction.add_transaction(environment)
# Finally, we remove all the labour that was used in production
# from the books of the firms
self.remove_labour_firms(environment, time)
logging.info(" goods produced on step: %s", time)
示例15: create_transaction
def create_transaction(recipient, amount):
"""
creates a new transaction and add it to verified_transactions.json
:param recipient: public address of the recipient
:param amount: the amount of abc to be sent
:return: None
"""
# TODO: Send a success message to client
conf = Configuration()
try:
tx = Transaction()
tx.add_output(recipient, amount)
tx.unlock_inputs(get_private_key(), get_public_key("string"))
save_verified_transaction(tx.get_transaction_id(), tx.get_data())
conf.subtract_balance(tx.sum_of_outputs())
except ValueError as e:
# Will raise if insufficient utxos are found
raise ValueError("INSUFFICIENT FUNDS")