def getbalance(): # {"apiKey": "0000", "params": {"account":""}} result_data = "{'code':'%(code)s','data':%(data)s,'msg':'%(msg)s'}" rpc_connection = AuthServiceProxy("http://%s:%[email protected]:18332" % (rpc_user, rpc_password)) try: form_data = request.args param = getParams(form_data, 'get') if param == None: raise Exception('入参异常') address = param['address'] amount, txs = getBalanceByAddress(address, rpc_connection) # account = rpc_connection.getaccount(address) # amount = rpc_connection.getbalance(account) result_data = result_data % { "code": "0000", "data": str({'amount': float(amount)}), "msg": '0' } result_data = demjson.decode(result_data) except BaseException as err: err = str(err).replace("'", "") result_data = result_data % ({ "code": "0001", "data": "0", "msg": str(err) }) # print(result_data) result_data = demjson.decode(result_data) # batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips: return jsonify(result_data)
def sendrawtransaction(): # {"apiKey": "0000", "params": {"account":""}} result_data = "{'code':'%(code)s','data':%(data)s,'msg':'%(msg)s'}" rpc_connection = AuthServiceProxy("http://%s:%[email protected]:18332" % (rpc_user, rpc_password)) try: form_data = request.json param = getParams(form_data, 'post') if param == None: raise Exception('入参异常') # from_addr, from_prikey, to_addr, amount, fee_amount from_addr = param['from_addr'] from_prikey = param['from_prikey'] to_addr = param['to_addr'] amount = param['amount'] fee_amount = param['fee_amount'] tx_data = None if 'tx_data' in param.keys(): tx_data = param['tx_data'] txhash = sendTx(rpc_connection, from_addr, from_prikey, to_addr, amount, fee_amount, tx_data) # print(txhash) result_data = result_data % { "code": "0000", "data": str({'txhash': txhash}), "msg": '0' } result_data = demjson.decode(result_data) except Exception as err: print(type(err)) err = str(err).replace("'", "") result_data = result_data % ({ "code": "0001", "data": "0", "msg": str(err) }) result_data = demjson.decode(result_data) # batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips: return jsonify(result_data)
def listtransactionsByaddr(): rpc_connection = AuthServiceProxy("http://%s:%[email protected]:18332" % (rpc_user, rpc_password)) # {"apiKey": "0000", "params": {"account":""}} result_data = "{'code':'%(code)s','data':%(data)s,'msg':%(msg)s}" try: form_data = request.args param = getParams(form_data, 'get') if param == None: raise Exception('入参异常') address = param['address'] to_address = None from_time = None to_time = None if 'to_address' in param.keys(): to_address = param['to_address'] if 'from_time' in param.keys(): from_time = param['from_time'] if 'to_time' in param.keys(): to_time = param['to_time'] to_add_list = listTransactions(rpc_connection, address, to_address, from_time, to_time) result_data = result_data % { "code": "0000", "data": str({'tx_list': to_add_list}), "msg": '0' } result_data = demjson.decode(result_data) except Exception as err: result_data = result_data % ({ "code": "0001", "data": "0", "msg": str(err) }) result_data = demjson.decode(result_data) return jsonify(result_data)
def newaddress(): # {"apiKey": "0000", "params": {"account":""}} result_data = "{'code':'%(code)s','data':%(data)s,'msg':'%(msg)s'}" rpc_connection = AuthServiceProxy("http://%s:%[email protected]:18332" % (rpc_user, rpc_password)) try: form_data = request.json param = getParams(form_data, 'post') if param == None: raise Exception('入参异常') account = param['account'] address_type = 'legacy' address = rpc_connection.getnewaddress(account, address_type) a = rpc_connection.walletpassphrase('admin', 30) address_pkey = rpc_connection.dumpprivkey(address) result_data = result_data % { "code": "0000", "data": str({ 'address': address, 'address_pkey': address_pkey }), "msg": '0' } result_data = demjson.decode(result_data) except BaseException as err: err = str(err).replace("'", "") result_data = result_data % ({ "code": "0001", "data": "0", "msg": str(err) }) # print(result_data) result_data = demjson.decode(result_data) # batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips: return jsonify(result_data)
def gettransaction(): result_data = "{'code':'%(code)s','data':%(data)s,'msg':'%(msg)s'}" rpc_connection = AuthServiceProxy("http://%s:%[email protected]:18332" % (rpc_user, rpc_password)) try: form_data = request.json param = getParams(form_data, 'post') if param == None: raise Exception('入参异常') txid = param['txid'] print(txid) txinfo = conv_dict(rpc_connection.gettransaction(txid)) result_data = result_data % { "code": "0000", "data": str({'txinfo': txinfo}), "msg": '0' } print(result_data) result_data = demjson.decode(result_data) # e15b43f44f039a90fe0d15a5e62d721da9a98233a9f57fb932e966bc5f26c42d except BaseException as err: err = str(err).replace("'", "") result_data = result_data % ({ "code": "0001", "data": "0", "msg": str(err) }) result_data = demjson.decode(result_data) print(result_data) # batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips: return jsonify(result_data)
def listtransactions(): rpc_connection = AuthServiceProxy("http://%s:%[email protected]:18332" % (rpc_user, rpc_password)) # {"apiKey": "0000", "params": {"account":""}} result_data = "{'code':'%(code)s','data':%(data)s,'msg':'%(msg)s'}" try: form_data = request.args param = getParams(form_data, 'get') if param == None: raise Exception('入参异常') # from_addr, from_prikey, to_addr, amount, fee_amount address = param['address'] tx_list_result = [] tx_list = rpc_connection.listtransactions() tx_list_result_temp = [] for tx in tx_list: # print(tx) if ('address' in tx.keys() and address == tx['address']): tx['amount'] = float(tx['amount']) if 'fee' in tx.keys(): tx['fee'] = float(tx['fee']) if 'identifier' in tx.keys(): tx['identifier'] = 'true' if tx[ 'identifier'] == True else 'false' if 'abandoned' in tx.keys(): tx['abandoned'] = 'true' if tx[ 'abandoned'] == True else 'false' if 'trusted' in tx.keys(): tx['trusted'] = 'true' if tx['trusted'] == True else 'false' if 'blocktime' in tx.keys(): tx['blocktime_conv'] = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime(tx['blocktime'])) if 'time' in tx.keys(): tx['time_conv'] = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(tx['time'])) if 'timereceived' in tx.keys(): tx['timereceived_conv'] = time.strftime( "%Y-%m-%d %H:%M:%S", time.localtime(tx['timereceived'])) to_add = getToAddr(tx['txid'], address, rpc_connection) if to_add != None: tx['to_add'] = to_add tx_list_result_temp.append(tx) result_data = result_data % { "code": "0000", "data": str({'tx_list': tx_list_result_temp}), "msg": '0' } result_data = demjson.decode(result_data) except Exception as err: err = str(err).replace("'", "") result_data = result_data % ({"code": "0001", "data": "0", "msg": err}) # print('result_data===',result_data) result_data = demjson.decode(result_data) # batch support : print timestamps of blocks 0 to 99 in 2 RPC round-trips: return jsonify(result_data)
print(samples) inputs = [row[:2] for row in samples] targets = [row[2:] for row in samples] percepter = mlp(inputs, targets, 2) percepter.mlptrain(inputs, targets, params['learningRate'], params['maxIterations']) def generateARFF(fileName, samples): #open file for writing fullDestFilePath = f'./project2/data/{fileName}.arff' shutil.copy2('./project2/starter.arff', fullDestFilePath) with open(fullDestFilePath, 'a') as arff: for sample in samples: arff.write(f'{sample[0]},{sample[1]},{sample[2]}\n') #write general information (attributes etc.) #write sample data if __name__ == "__main__": params = util.getParams('./project2/params/params.yaml') print(params) xDomain = [1, 100] yDomain = [1, 100] objective = Objective(xDomain, yDomain) plotActual(xDomain, yDomain, objective.calculate, params['resultsPath']) numTrainingSamples = int(params['numSamples'] * params['trainingPercentage']) numTestingSamples = params['numSamples'] - numTrainingSamples inputs = objective.getSampleInputs(numTrainingSamples) #get random (x,y) coords from input domain (how many? build training/testing sets) #split these up into training and testing inputs trainingSamples = objective.getSamples(numTrainingSamples) #runMyMLP(params, inputs, objective) #runBookMLP(params, trainingSamples, objective) generateARFF('samples', objective.getSamples(params['numSamples']))
def sortChromosomes(self, chromosomes: ChromList, descending=True) -> ChromList: chromosomes.sort(key=attrgetter('fitness'), reverse=descending) return chromosomes def getMostFitChromosome(self, chromosomes: ChromList) -> Chrom: mostFitChrom = chromosomes[0] for chrom in chromosomes: if chrom.fitness > mostFitChrom.fitness: mostFitChrom = copy.deepcopy(chrom) return mostFitChrom if __name__ == "__main__": if (len(sys.argv) != 2): print(f'Proper use:\n\tpython3 {sys.argv[0]} PARAM_FILENAME.yaml') params = util.getParams(sys.argv[1]) ga = SinLn(**params) plotter.plot3d(ga.xDomain, ga.yDomain, ga.f) bestChromsByGen, avgFitnessByGen = ga.run() plotter.plot2dTimeSeries( [chrom.fitness for chrom in bestChromsByGen], 'Fitness of Best Chromosome By Generation', 'generation', 'z-value' ) plotter.plot2dTimeSeries( avgFitnessByGen, 'Average Fitness By Generation', 'generation', 'avg. fitness' )
def setUp(self): unittest.TestCase.setUp(self) self.params = SimpleNamespace(**util.getParams('params.yaml')) self.ga = SinLn(**util.getParams('params.yaml'))
targets = [] with open(fileName, 'r') as csvFile: reader = csv.reader(csvFile, delimiter=',') currInputsRow = [] for row in reader: if len(row) == 1: targets.append(row[0]) inputs.append(currInputsRow) currInputsRow = [] else: currInputsRow.extend(list(map(int, row))) return inputs, targets if __name__ == "__main__": if len(sys.argv) != 2: print(f'Proper use of this program looks like: \ \n\tpython3 {sys.argv[0]} params*.yaml \ \n\nExiting gracefully...') sys.exit() # 1. get user-defined parameters params = getParams(sys.argv[1]) # 2. create and preprocess the inputs, inputs, targets = preprocessInputsandTargetsFrom(params['inputFile']) print(f'Number of data samples: {len(targets):d}') # 3. pass inputs to the Perceptron for learning percepter = SingleLayerPerceptron(params) # 4. train and test the Perceptron print(f'Training and testing based on {params["inputFile"]} data') percepter.trainAndTest(inputs, targets)