def test_str_to_str_to_str(self): @contract(str, contract(str, str)) def prepender(s): @contract(str, str) def wrapper(s2): return s + s2 return wrapper # this is ok self.assertEqual(prepender('hello, ')('dave'), 'hello, dave') # these are not ok self.assertRaisesRegexp(FailedContract, r'^expected a str, got a int$', prepender, 5) self.assertRaisesRegexp(FailedContract, r'^expected a str, got a int$', prepender('hello, '), 5) @contract(str, contract(str, str)) def prepender(s): def wrapper(s2): return s + s2 return wrapper self.assertRaisesRegexp(FailedContract, r'^expected a contract, got a function$', prepender, 'hello, ') @contract(str, contract(str, str)) def prepender(s): @contract(str, int) def wrapper(s2): return ord(s) + ord(s2) return wrapper self.assertRaisesRegexp(FailedContract, r'^expected contract\(str, str\), got contract\(str, int\)$', prepender, 'hello, ')
def test_argle_bargle(self): @contract(contract(str, str), str) def i_give_you_happy(f): return f('happy') @contract(str, str) def joy_joy(s): return s + ' ' + s + ' joy joy' # this is ok self.assertEqual(i_give_you_happy(joy_joy), 'happy happy joy joy') # these are not ok self.assertRaisesRegexp(TypeError, r'^i_give_you_happy\(\) takes exactly 1 argument \(0 given\)$', i_give_you_happy) self.assertRaisesRegexp(FailedContract, r'^expected a contract, got a str$', i_give_you_happy, 'joy joy') self.assertRaisesRegexp(FailedContract, r'^expected a contract, got a function$', i_give_you_happy, lambda s: s)
from contract import args, self, contract, old, isinstance_ def some_func(x, y, z=3): pass c = contract(some_func) c.require(isinstance_(args.x, (int, float)), isinstance_(args.y, (int, float)), (args.x**2 + args.y**2) < 25) c(2, 4.) # ## (2.) Contract decorator @contract def some_func(x, y, z=3): pass some_func.require(args.x > 2, args.y > 4, isinstance_(args.x, int)) some_func(3, y=5) # ## Post conditions class Stateful: counter = 0 @contract def increment(self, delta): self.counter += delta
def call_contract(self, args): sc = contract.contract(self.w3, args.compiled_path) return sc.call_contract(args)
def main(): for i in range(1): M = 1000 number_of_agents = 200 number_of_simulation_per_lanbda = 100 generator_price_multiply = 1 gamma = [1.0, 1.166, 1.333, 1.5, 1.666, 1.833, 2] df_columns = [ 'actuel expanse', 'gamma', 'M', 'actuel kWh reduced', 'Met the demand' ] row_data_df = pd.DataFrame(columns=df_columns) Fixed_cont_avg_cost = [] Fixed_cont_avg_reliability = [] Fixed_single_cont_avg_cost = [] Fixed_single_cont_avg_reliability = [] T_F_List_Fixed_cont_Met_the_demand = [] Fixed_cont_Total_expense = [] gamma_used = [] for lb in gamma: Fixed_cont_reduce_list = [] for i in range(number_of_simulation_per_lanbda): start = time.time() print('iteration:', i) Grid = grid.grid(M, lb) Grid.introduce_self() Agents = [] for num in range(number_of_agents): ag = agent.agent(num) Agents.append(ag) Contracts = [] for i in range(10, M + 1, 10): Contracts.append(contract.contract(i, 0.3, 0.5)) single_contract = [] single_contract.append(contract.contract(50, 0.3, 0.5)) Grid.set_single_contract(single_contract) Grid.set_contract(Contracts) Grid.set_agents(Agents) Grid.send_contrects_to_agents() Grid.send_single_contrects_to_agents() for ag in Agents: ag.Fixed_cont_bid_on_contract() for ag in Agents: ag.Fixed_single_cont_bid_on_contract() Grid.Fixed_cont_get_bids_from_agent() Grid.Fixed_cont_generator_bids( price_multiply=generator_price_multiply) Grid.Fixed_cont_get_q_from_agent() Fixed_cont_sum_of_bids = Grid.knapsack(bids_type='Fixed_cont') Grid.Fixed_cont_pay_to_agents(Fixed_cont_sum_of_bids) Grid.Fixed_cont_reliability() Grid.Fixed_single_cont_get_bids_from_agent() Grid.Fixed_single_cont_get_q_from_agent() Fixed_cont_Total_expense.append( Grid.Fixed_cont_Total_expense_sum) Fixed_cont_reduce_list.append( Grid.Fixed_cont_reliability_sum_q) if Grid.Fixed_cont_reliability_sum_q >= Grid.M: met_the_demand = 1 else: met_the_demand = 0 T_F_List_Fixed_cont_Met_the_demand.append(met_the_demand) print('Fixed_cont- Met_the_demand: ', T_F_List_Fixed_cont_Met_the_demand) print('Fixed_cont- Total_expense: ', Fixed_cont_Total_expense) gamma_used.append(lb) row_data_df = row_data_df.append( pd.DataFrame({ 'actuel expanse': [Grid.Fixed_cont_Total_expense_sum], 'gamma': [lb], 'M': [M], 'actuel kWh reduced': [Grid.Fixed_cont_reliability_sum_q], 'Met the demand': [met_the_demand] })) end = time.time() print('iteration took:', (end - start), 'sec') print('-' * 200) Fixed_cont_avg_cost.append( statistics.mean(Fixed_cont_Total_expense)) if len(T_F_List_Fixed_cont_Met_the_demand) > 0: Fixed_cont_avg_reliability.append( T_F_List_Fixed_cont_Met_the_demand.count(True) / len(T_F_List_Fixed_cont_Met_the_demand)) else: Fixed_cont_avg_reliability.append(0.0) filename = datetime.now().strftime( 'data/energy_demamd_row_data-%Y-%m-%d-%H-%M-%S.csv') row_data_df.to_csv(filename, index=False) graph_it(Fixed_cont_avg_reliability, Fixed_single_cont_avg_reliability, Fixed_cont_avg_cost, Fixed_single_cont_avg_cost)
import pickle import codecs from contract import contract contractkey = '0x6003284f23a3c3ef0234232dfa32' filename = 'contract.dat' candidateList = ['Rama', 'Ken', 'Rose'] module = contract() module.appendCandidateNames(candidateList) dumpmodule = pickle.dumps(module, protocol=pickle.HIGHEST_PROTOCOL) encodemodule = codecs.encode(dumpmodule, 'hex_codec') with open(filename, "a") as contractfile: contractfile.write(str(contractkey) + ":" + str(encodemodule)[2:-1] + "\n") print(encodemodule)