def positivity(address): ################# read canopy output ################# positivity_scores = BTree() with open(address,'r') as f: for line in f: canopy_members = line.strip().split() total = float(len(canopy_members)) number_of_positive = 0 for member in canopy_members: if membertag_to_class(member) == 1: number_of_positive = number_of_positive + 1 p_score = float(number_of_positive)/total for member in canopy_members: try: score_list = positivity_scores[member] score_list.append(p_score) positivity_scores[member] = score_list except: positivity_scores[member] = [p_score] ############### take average score ################### for key in positivity_scores.keys(): score_list = positivity_scores[key] sum = float(0) for item in score_list: sum = sum + item final_score = sum/float(len(score_list)) positivity_scores[key] = final_score ################ prepare output #################### output = '' for key,value in positivity_scores.items(): output = output + key + ' ' + str(value) + '\n' with open(positivity_outputs, 'w') as f: f.write(output)
def modifier(inq, outq): while True: item=inq.get() if item is None: break else: sequence_number=item[0] sequence=item[1].strip().lower() sequence = ''.join([i for i in sequence if i in allowed]) local_features = BTree() for i in range(len(sequence)-k+1): kmer = sequence[i:i+k] feature_index = features[kmer] index = feature_index try: current_count = local_features[index] local_features[index] = current_count + 1 except KeyError: local_features[index] = 1 #normalization sum = 0 for value in local_features.values(): sum = sum + (value*value) sum = math.sqrt(sum) for key in local_features.keys(): value = local_features[key] local_features[key] = float(value)/sum out_string = str(sequence_number) for key,val in local_features.iteritems(): out_string = out_string + ' ' + str(key) + '-' + str(val) out_string = out_string + '\n' outq.put(out_string) del local_features
def __init__(self, id, name, class_name, score): self.id = id self.name = name self.class_name = class_name self.score = score csv_filename = 'data.csv' db_filename = 'data/data.fs' conn = ZODB.connection(db_filename) students = BTree() root = conn.root root.students = students with open(csv_filename, mode="r", encoding="GBK") as file: students.clear() reader = csv.reader(file) for row in reader: id = row[0] name = row[1] class_name = row[2] score = float(row[3]) if id in students.keys(): print(f"载入失败:学号{id}已存在!") student = Student(id, name, class_name, score) students[id] = student transaction.commit() conn.close()
class ContractStorage(persistent.Persistent): def __init__(self): self.contracts = BTree() self.instance_lists = BTree() self.last_block = 0 def get_contract_by_hash(self, contract_hash): return self.contracts[contract_hash] def initialize(self, rpchost, rpcport, rpctls, sync_all, ipc): if ipc: eth = EthIpc() else: eth = EthJsonRpc(rpchost, rpcport, rpctls) if self.last_block: blockNum = self.last_block print("Resuming synchronization from block " + str(blockNum)) else: blockNum = eth.eth_blockNumber() print("Starting synchronization from latest block: " + str(blockNum)) while (blockNum > 0): if not blockNum % 1000: print("Processing block " + str(blockNum) + ", " + str(len(self.contracts.keys())) + " unique contracts in database") block = eth.eth_getBlockByNumber(blockNum) for tx in block['transactions']: if not tx['to']: receipt = eth.eth_getTransactionReceipt(tx['hash']) if receipt is not None: contract_address = receipt['contractAddress'] contract_code = eth.eth_getCode(contract_address) contract_balance = eth.eth_getBalance(contract_address) if not contract_balance and not sync_all: # skip contracts with zero balance (disable with --sync-all) continue code = ETHContract(contract_code, tx['input']) m = hashlib.md5() m.update(contract_code.encode('UTF-8')) contract_hash = m.digest() try: self.contracts[contract_hash] except KeyError: self.contracts[contract_hash] = code m = InstanceList() self.instance_lists[contract_hash] = m self.instance_lists[contract_hash].add( contract_address, contract_balance) transaction.commit() self.last_block = blockNum blockNum -= 1 # If we've finished initializing the database, start over from the end of the chain if we want to initialize again self.last_block = 0 transaction.commit() def search(self, expression, callback_func): all_keys = list(self.contracts) for k in all_keys: if self.contracts[k].matches_expression(expression): m = self.instance_lists[k] callback_func(k.hex(), self.contracts[k], m.addresses, m.balances)