def label_offline_data(pklfilename=offline_train_data_pklfilename): """ delete the irrelevant data and label the effective data convert the distance_str into distance_int # Date : 消费日期:如果Date=null & Coupon_id != null,该记录表示领取优惠券但没有使用,即负样本; 如果Date!=null & Coupon_id = null,则表示普通消费日期;如果Date!=null & Coupon_id != null,则表示用优惠券消费日期,即正样本; :param pklfilename: :return: """ offline_data = pickle.load(open(pklfilename, "rb")) labels = [] for i in range(len(offline_data)): if offline_data['Coupon_id'][i] == "null": labels.append(0) else: if offline_data['Date'][i] != "null": labels.append(1) else: labels.append(-1) offline_data['Label'] = np.array(labels) pickle._dump(offline_data, open("labeled_offline_train_data.pkl", "wb")) washed_labeled_data = pd.DataFrame( offline_data[offline_data['Label'] != 0]) washed_labeled_data.to_csv("tem.csv", header=True, index=False) washed_labeled_data = pd.read_csv("tem.csv") # print(washed_labeled_data) # Warning: the index of dataframe cannot be modified,and the slices of the dataframe is not the copy of the dataframe # though we delete a row of the dataframe, we cannot change the indices pickle._dump(washed_labeled_data, open("washed_labeled_data.pkl", "wb"))
def getCh(): count = 0 char_vocab = [] shape_vocab = [] char_shape = {} def getShapes(filename, char_vocab, shape_vocab, char_shape, count): for line in open(filename).readlines(): line = line.split() if len(line)<2: continue text = line[1][0] im = Image.new("1", (28,28), 0) dr = ImageDraw.Draw(im) font = ImageFont.truetype("/usr/share/fonts/opentype/noto/NotoSansCJK-Light.ttc", 28) dr.text((0, -7), text, font=font, fill=1) im.save("ch_shape_jpg/"+text+".jpg") img = np.array(im, dtype="int32") char_vocab.append(text) shape_vocab.append(img) char_shape[text] = img count += 0 getShapes("../cjkvi-tables/zibiao2009-1.txt", char_vocab, shape_vocab, char_shape, count) getShapes("../cjkvi-tables/zibiao2009-2.txt", char_vocab, shape_vocab, char_shape, count) print("Read {number} character shapes.".format(number=len(char_vocab))) shape_vocab_data = { "chars": char_vocab, "shapes": shape_vocab, "char_shape": char_shape } with open("ch_shape_vocab.pickle", "wb") as f: pickle._dump(shape_vocab_data, f)
def load_offline_data(filename=offline_filename): """ load the data_offline convert string_time into datetime and make the data persistent as a *.pkl """ header = [ 'User_id', 'Merchant_id', 'Coupon_id', 'Discount_rate', 'Distance', 'Date_received', 'Date' ] data_offline = pd.read_csv(filename, names=header) rdate_list = [] udate_list = [] for i in range(len(data_offline['Date_received'])): if data_offline['Date_received'][i] != "null": rdate_list.append( datetime.datetime.strptime(data_offline['Date_received'][i], "%Y%m%d")) else: rdate_list.append('null') if data_offline['Date'][i] != "null": udate_list.append( datetime.datetime.strptime(data_offline['Date'][i], "%Y%m%d")) else: udate_list.append("null") data_offline['Date_received'] = np.array(rdate_list) data_offline['Date'] = np.array(udate_list) pickle._dump(data_offline, open("offline_train_data.pkl", "wb")) return data_offline
def getJp(): count = 0 char_vocab = [] shape_vocab = [] char_shape = {} for line in open("joyo2010.txt").readlines(): if line[0] == "#": continue text = line[0] im = Image.new("1", (28, 28), 0) dr = ImageDraw.Draw(im) font = ImageFont.truetype( "/usr/share/fonts/truetype/takao-mincho/TakaoPMincho.ttf", 28) dr.text((0, 0), text, font=font, fill=1) im.save("1.jpg") img = np.array(im, dtype="int32") char_vocab.append(text) shape_vocab.append(img) char_shape[text] = img count += 0 shape_vocab_data = { "chars": char_vocab, "shapes": shape_vocab, "char_shape": char_shape } with open("jp_shape_vocab.pickle", "wb") as f: pickle._dump(shape_vocab_data, f)
def register(self): while True: address = 'stu_info/%s.pk' % self.name real_name = input('your real name:\n-->') acc_new_password = input('your account password:\n-->') aacc_new_password = input('again account password:\n-->') if len(real_name) == 0 or len(acc_new_password) ==0: print('somewhere is none!\ntry again!') continue elif aacc_new_password != acc_new_password: print('first password not same as second!') continue #还可以加是否账号名已被占用 else: data = { 'name': real_name, 'account_name' : self.name, 'account_pass' : acc_new_password } try: with open(address,'wb') as f: pickle._dump(data,f) print('well!anway!\nwelcome join oldboy') # global Student_log不行,全局变量不能这样用 # Student_log = True # print(Student_log) return True except: print('somewhere wrong!') return False
def voter(choice): voterkeys['sk'], voterkeys['pk'] = enc.rsakeys() v1 = vote(invisiblevoter, int(choice), voterkeys['pk']) vote.inc_votecount() #--votedata digitally signed and encrypted and sent to the temporary pool with open('temp/votefile.csv', 'a', newline="") as votefile: writer = csv.writer(votefile) encvotedata = v1.encryptvote() writer.writerow(encvotedata) #--and broadcasted to other peers on the network #pp.send_votedata_to_peer('127.0.0.1',9999,encvotedata) """ This method mines new blocks after generation of every 2 votes Uncomment this method and comment the 'mineblocktimer()' method to switch to 'vote count block mining' method - where block will be mined after 2 votes are generated and not on regular time intervals. """ if vote.count % 2 == 0: blockx = Block().mineblock() with open('temp/blockchain.dat', 'ab') as blockfile: pickle._dump(blockx, blockfile) print("block added") pass """ Now the QR code containing the information about your PIN and private key is printed on the thank you page. """ print("thanks")
def persistdataondisk(self, obj): print( " -------------------Persisting Data on Disk using Pickle-----------------------", '\n') output = open('myfile.pkl', 'wb') pc._dump(obj, output) output.close()
def _index(self, data, type): def _apply_index(data, type): if type == "han": indexed = [[[unqVoc_LookUp[char] for char in seq] for seq in doc] for doc in data] else: indexed = [[unqVoc_LookUp[char] for char in seq] for seq in data] return indexed # end x_train, x_test = data unqVoc = set(list(more_itertools.collapse(x_train[:] + x_test[:]))) unqVoc_LookUp = {k: v + 1 for v, k in enumerate(unqVoc)} self.vocab_size = len(list(unqVoc_LookUp)) # save lookup table pickle._dump( unqVoc_LookUp, open("{}/{}_unqVoc_Lookup.p".format(self.paths.LIB_DIR, type), "wb")) x_train = _apply_index(data=x_train, type=type) x_test = _apply_index(data=x_test, type=type) return [x_train, x_test]
def label_data(pklfilename=disc_dispose_filename): """ Date : 消费日期:如果Date=null & Coupon_id != null,该记录表示领取优惠券但没有使用,即负样本; 如果Date!=null & Coupon_id = null,则表示普通消费日期;如果Date!=null & Coupon_id != null,则表示用优惠券消费日期,即正样本; label: 3: positive sample 0: negative sample 1: normal consumption sample 2: more than 15 days sample :param pklfilename: :return: """ train_data = pickle.load(open(pklfilename, "rb")) data_label = [] for i in range(len(train_data)): if train_data['Coupon_id'][i] != "null" and train_data['Date'][ i] == 'null': data_label.append('-1') else: if train_data['Coupon_id'][ i] == "null" and train_data['Date'][i] != 'null': data_label.append('3') else: if (train_data['Date'][i] - train_data['Date_received'][i] ) > datetime.timedelta(15): data_label.append('2') else: data_label.append('1') train_data['Label'] = pd.Series(data_label, dtype="category") print(train_data['Label']) pickle._dump(train_data, open("labeled_data.pkl", "wb"))
def menu_fun(): info = '''(0)save (1)cost (2)show (3)quit make your choice(0/1/2/3): ''' fname = 'account.data' init = [['2020-01-10', 0, 0, 10000, 'init']] if not os.path.exists(fname): with open(fname, 'wb') as fobj: pickle._dump(init, fobj) mon = {'0': save_fun, '1': cost_fun, '2': show_fun} while 1: try: choice = input(info).strip() except (EOFError, KeyboardInterrupt, KeyError): print('\nByebye~') break if choice not in '0123': print("the choice must in '0/1/2/3'") continue elif choice == '3': print('\nByebye~') break else: mon[choice](fname)
def convert_discount_num(pklfilename=washed_labeled_data_pklfilename, targert_filename=washed_labeled_data_pklfilename): """ conver the '*:*' into float number create two feature: one is total money ,the other is discount money for example '50:10',total money is 50 , discount money is 10 :param pklfilename: :return: """ upper_limit = [] allowance = [] discount_rate = [] washed_data = pickle.load(open(pklfilename, "rb")) for i in range(len(washed_data)): if washed_data['Discount_rate'][i].find(':') != -1: tem = washed_data['Discount_rate'][i].split(':') upper_limit.append(int(tem[0])) allowance.append(int(tem[1])) discount_rate.append((1 - int(tem[1]) * 1.0 / int(tem[0]))) else: upper_limit.append(100) rate = 1 - float(washed_data['Discount_rate'][i]) allowance.append(int(100 * 1.0 * rate)) discount_rate.append(float(washed_data['Discount_rate'][i])) washed_data['Discount_rate'] = np.array(discount_rate) washed_data['Upper_limit'] = np.array(upper_limit) washed_data['Allowance'] = np.array(allowance) # print(washed_data) pickle._dump(washed_data, open(targert_filename, "wb"))
def saveSnapshot(self, path='', log=''): if type(log) is not type(''): log.emit('Saving snapshot...') if path not in '': self.path = path output_filen = self.path + self.id + "_participant.pkl" with open(output_filen, 'wb') as output: pickle._dump(self.__dict__, output, pickle.HIGHEST_PROTOCOL)
def save_model(model, model_filepath): """ saves model as pickle file to given filepath :param model: model that should be saved :param model_filepath: path where the file should be saved :return: """ pickle._dump(model, open(model_filepath, "wb"))
def make_rss_pickle(url, file_name="rss.pickle"): rss = feedparser.parse(url) # file_name = "rss.pickle" script_dir = os.path.dirname(__file__) file_path = os.path.join(script_dir, file_name) file = open(file_path, "wb") pickle._dump(rss, file) file.close()
def build_k_fold_models(self, num_fold=13): for i in range(num_fold): train_dataMat, data_label = self.load_train_data(i) adaBoostModel = self.adaBoostTrainDecisionStump( train_dataMat, data_label) pickle._dump( adaBoostModel, open("./adaboost_result/adaboost_model%d.pkl" % i, "wb"))
def save(self): ''' 用pickle将用户对象保存到文件 :return: ''' id = str(self.id) file_path = os.path.join(self.db_path, id) pickle._dump(self, open(file_path, 'wb'))
def inlinetimer(): while True: sleep(BLOCK_TIME_LIMIT) #--global variable #--sleep for 15 seconds --> mine a block --> repeat blockx = Block().mineblock() with open('temp/blockchain.dat', 'ab') as blockfile: pickle._dump(blockx, blockfile) print("block added")
def storeTree(inputTree, filename): """存储决策树""" import pickle # open(filename,'w')报如下错误: # TypeError: write() argument must be str, not bytes # pickle 默认是用二进制的形式存储数据 # 解决办法:open(filename,'wb') with open(filename, 'wb') as file: pickle._dump(inputTree, file)
def syncer_pickle_dump(*args): # second argument is file handle! if len(args) > 1: file_sync_storage.append(args[1].name) else: logger.warning( "Pickle dump called with less than two arguments... shouldn't happen." ) pickle._dump(*args)
def on_save(self, event): global cells dlg = wx.FileDialog(self.frame, "Choose a file", self.dirname, "", "*.*", wx.FD_SAVE) if dlg.ShowModal() == wx.ID_OK: self.filename = dlg.GetFilename() self.dirname = dlg.GetDirectory() with open(os.path.join(self.dirname, self.filename), 'wb') as f: pickle._dump(cells, f, pickle.HIGHEST_PROTOCOL) dlg.Destroy()
def save(self): w_file_name = self.folder_name + "\Weights" weights_f = open(w_file_name, 'wb') pickle._dump(self.weights, weights_f) weights_f.close() b_file_name = self.folder_name + "\Biases" biases_f = open(b_file_name, 'wb') pickle.dump(self.biases, biases_f) biases_f.close()
def report(aps, items): try: if os.path.exists(aps): os.remove(aps) with open(aps, 'w') as mysavedata: pickle._dump(items, mysavedata) except IOError as err: print('File error:' + str(err)) except pickle.PickleError as perr: print('Pickling error:' + str(perr))
def create_linear(data_set): model = train_linear(data_set) fn = '{}/_model/lin_{}'.format(ROOT_FOLDER, data_set.name) with open(fn, 'wb') as file: pickle._dump(model, file) print('Model saved in {}'.format(fn)) return model
def convert_distance_int(pklfilename=washed_labeled_data_pklfilename, targert_filename=washed_labeled_data_pklfilename): washed_labeled_data = pickle.load(open(pklfilename, "rb")) dis_list = [] for i in range(len(washed_labeled_data)): if str(washed_labeled_data['Distance'][i]) != "null": dis_list.append(int(washed_labeled_data['Distance'][i])) else: dis_list.append(12) washed_labeled_data['Distance'] = np.array(dis_list) pickle._dump(washed_labeled_data, open(targert_filename, "wb"))
def pageToSentences(self, useCache=True): """ 把每个页面数据看成一个段落,并将这些页面句子拆分开来 同时会去掉空格以及所有的标点符号 :return: """ # 判断是否使用缓存 if useCache: # 如果文件存在 if os.path.exists(self.sentences_path): file = open(self.sentences_path, 'rb') sentences = pickle.load(file) file.close() return sentences # 缓存文件不存在,乖乖算一次吧 pattern1 = re.compile(r"。|?|!|!|\?") pattern2 = re.compile(r"[\s+-::\.\!\/_,$%^*()【】\[\]\"\']+|[+—!{}<>《》/\\,。?\?、~@#¥%…&*()]+") sentences = [] for i in range(len(self.data)): # 把最后的换行符去掉 page = self.data[i][:-1] # 分成句子 print("{}、original data:{}".format(i, page)) strs = re.split(pattern1, page) for j in range(len(strs)): str = strs[j] # 去掉所有的标点符号 # print("{}、original sentence:{}".format(i, str)) str = re.sub(pattern2, "", str) # 排除过短的句子 if len(str) > 12: print("{}、processed sentence:{}".format(i, str)) sentence = Sentence(str, i) sentences.append(sentence) # 经过分析,发现合并句子比较合适。。。。 sentences = self.merge_sentences(sentences) print("句子总数:{}".format(len(sentences))) # 如果使用缓存,缓存一下 if(useCache): file = open(sentences_path, 'wb') pickle._dump(sentences, file) file.close() del self.data return sentences
def close_file(self): """ 关闭文件流 最后一定要加上(重要!!!) :return: """ self.f.close() # hash_id_offset 持久化 到磁盘 f_dict = open(self.doc_raw_offset_file_dir, 'wb') pickle._dump(self.hash_id_offset, f_dict) f_dict.close()
def convert_distance_feature(pklfilename=offline_data_pklfilename, target_filename=offline_data_pklfilename): train_data = pickle.load(open(pklfilename, "rb")) dis_list = [] for i in range(len(train_data)): if train_data['Distance'][i] == 'null': dis_list.append('12') else: dis_list.append(train_data['Distance'][i]) train_data['Distance'] = np.array(dis_list) # print(train_data) pickle._dump(train_data, open(target_filename, "wb"))
def save_file(boy, girl, count): boy_file_name = 'boy_' + str(count) + '.txt' girl_file_name = 'girl_' + str(count) + '.txt' boy_file = open(boy_file_name, 'wb') girl_file = open(girl_file_name, 'wb') pickle._dump(boy, boy_file) #除了这一步,其他的都和分割文件(2)一样,这里采用(接下行) pickle._dump(girl, girl_file) #pickle模块将文件boy,girl数组写入文件中 boy_file.close() girl_file.close()
def count_coupon_day(pklfiname=offline_train_data_pklfilename): offline_data = pickle.load(open(pklfiname, "rb")) day_beforeUsed = [] for i in range(len(offline_data)): if offline_data['Date_received'][i] != "null" and offline_data['Date'][ i] != "null": day_beforeUsed.append(offline_data['Date'][i] - offline_data['Date_received'][i]) day_sorted = np.array(sorted(day_beforeUsed, reverse=True)) pickle._dump(day_sorted, open("offline_coupon_using_day.pkl", "wb")) below15_days = len(day_sorted[day_sorted <= datetime.timedelta(15)]) print(below15_days)
def addGenesis(): genesisblock = Blockchain.genesis() #--find the proof of work for genesis block genesisblock.nonce = genesisblock.pow() genesisblock.hash = genesisblock.calcHash() Blockchain.chain.append(genesisblock) #--information of genesis block written to the blockchain data file with open('temp/Blockchain.dat', 'ab') as genfile: pickle._dump(genesisblock, genfile) print("Genesis block added")
def load_test_data(filename=test_filename): """ load the test data and make the data persistent as a *.pkl :param filename: :return: data_test """ header = [ 'User_id', 'Merchant_id', 'Coupon_id', 'Discount_rate', 'Distance', 'Date_received' ] test_data = pd.read_csv(filename, names=header) pickle._dump(test_data, open("./pretreat_test_data/test_data.pkl", "wb")) print(test_data)
def make_filter_pickle(trivia, file_name="filter.pickle", min_categories=1): categories = trivia.get_categories(filtered=False) # file_name = "test.pickle" script_dir = os.path.dirname(__file__) file_path = os.path.join(script_dir, file_name) file = open(file_path, "wb") category_filter = set() for category in categories: facts = trivia.get_facts_by_category(category.ID) if len(facts) < min_categories: category_filter.add(category.ID) pickle._dump(category_filter, file) file.close()
def prepare(): with open('dataCreep.txt', 'r') as f: lines = f.readlines() data = [] for line in lines: data.append(line.strip().split()) bookNum = len(data) print('bookNum:', bookNum) d = {} bid = 0 for book in data: for word in book: if word in d.keys(): d[word][0].add(bid) d[word][1][bid] += 1 else: d[word] = (set([bid]), [0]*bookNum) d[word][1][bid] = 1 bid += 1 idf = {} for word in d.keys(): idf[word] = math.log10(bookNum/len(d[word][0])) dic = {} for word in d.keys(): print(word, ': ', d[word]) dic[word] = (d[word][0], list(map(lambda x: (1+math.log10(x))*idf[word] if x>0 else 0, d[word][1])), idf[word]) print(word, ': ', dic[word]) norm = [0]*bookNum for word in d.keys(): norm = [x+y*y for x, y in zip(norm, dic[word][1])] for word in d.keys(): dic[word] = (d[word][0], [x/math.sqrt(y) for x, y in zip(dic[word][1], norm)], idf[word]) with open('data.pkl', 'wb') as pkl_file: pickle._dump(dic, pkl_file) mat = np.array([value[1] for value in dic.values()]) # for word in d.keys(): # mat = np.vstack([mat, d[word][1]]) print('mat:') print(mat) sim = np.dot(mat.T, mat) top = [[None]*bookNum for i in range(3)] print('sim:') print(sim) for i in range(bookNum): for j in range(3): top[i][j] = sim[i].argmax() sim[i, top[i][j]] = 0 print('top:') print(top) with open('similarity.pkl', 'wb') as pkl_file: pickle._dump(top, pkl_file)
class SimpleBalclass(object): def __init__(self, text): self.text = text def as_dict(self): return dict(text=self.text) class pickeltest: def __init__(self, text, *NOlisting): self.server_text = text self.sever_list = NOlisting # print(i.as_dict for i in self.sever_list) def as_dict(self): return dict(text=self.server_text, server_list=[i.as_dict() for i in self.sever_list]) if __name__ == "__main__": p = pickeltest("complex bal", SimpleBalclass("Simple"), SimpleBalclass("Simple1")) print(p.as_dict()) # The pickel object Load will produce the same as normal object with open("Balcomplex.p", "wb") as target: pickle._dump(p, target) with open("Balcomplex.p", "rb") as source: copy = pickle.load(source) print(copy.as_dict())
classifier = random.choice(classifiers) classifierParameter = 0 if isinstance(classifier,skes.RandomForestClassifier) or isinstance(classifier,sktr.DecisionTreeClassifier) or isinstance(classifier,skes.GradientBoostingClassifier) : classifier.min_samples_leaf = random.randint(10,100) classifierParameter = classifier.min_samples_leaf if isinstance(classifier, sklin.RidgeClassifier): classifier.alpha = random.random() classifierParameter = classifier.alpha if isinstance(classifier, skneib.KNeighborsClassifier): classifier.leaf_size = random.randint(10,100) classifierParameter = classifier.leaf_size alphaCutoff = 0.10*random.random() try: featureSet = getEstimationForMultipleFeatureSets(classifier,alphaCutoff,randomSeed=randomSeed,verbose=False) result = [classifier,[alphaCutoff,classifierParameter],featureSet] print(tries, result) with open("Temp/multipleModels_performance.p","ab") as file: pickle._dump(result,file) tries += 1 except: print("Error on something!")
if parent in geneModelIndex or id in geneModelIndex: i = geneModelIndex.index(re.search('ID=(.+?);', elementsCL[8]).group(1)) geneModels[i].geneModelParts(elementsCL) print('line 182') geneModels[i].print() # dump geneModels to output file file = open(os.path.expanduser('~/PycharmProjects/data_files/TAIR10_gene_models.pkl'),'wb') pickle._dump(geneModels,file) file.close() # test reading back in variantList file = open(os.path.expanduser('~/PycharmProjects/data_files/TAIR10_gene_models.pkl'),'rb') reconstituted = pickle.load(file) print('Now printing reconstituted gene models read from file.') for i in range(geneModelCounter): reconstituted[i].print()
def main(): pygame.init() screen = pygame.display.set_mode((WINDOWX,WINDOWY), FULLSCREEN) pygame.display.set_caption('Wrath of the Wrecktangles') pygame.mouse.set_visible(False) clock = pygame.time.Clock() stageCounter = 0 score = 0 #Load Stages (Shelve) d = shelve.open('score.txt') stagesUnlocked = d['stagesUnlocked'] running = True while running: #Set Up Groups backgroundSprites = pygame.sprite.Group() foregroundSprites = pygame.sprite.Group() playerSprite = pygame.sprite.Group() haloSprite = pygame.sprite.Group() coinsSprite = pygame.sprite.Group() healthBoxes = pygame.sprite.Group() #Battle Room Groups tinyFloaters = pygame.sprite.Group() normalFloaters = pygame.sprite.Group() largeFloaters = pygame.sprite.Group() hugeFloaters = pygame.sprite.Group() shooters = pygame.sprite.Group() bottomFeeders = pygame.sprite.Group() brothers = pygame.sprite.Group() def worldShiftBattleRoom(x, direction): if direction == 'x': for floater in normalFloaters: floater.rect.x += x for floater in largeFloaters: floater.rect.x += x for floater in hugeFloaters: floater.rect.x += x for floater in shooters: floater.rect.x += x for bullet in floater.bullets: bullet.rect.x += x for floater in bottomFeeders: floater.rect.x += x if direction == 'y': for floater in normalFloaters: floater.rect.y += x for floater in largeFloaters: floater.rect.y += x for floater in hugeFloaters: floater.rect.y += x for floater in shooters: floater.rect.y += x for bullet in floater.bullets: bullet.rect.y += x for floater in bottomFeeders: floater.rect.y += x #player and level player = Player.Players(350,40) player.stage = stageCounter if player.stagesUnlocked > stagesUnlocked: stagesUnlocked = player.stagesUnlocked else: player.stagesUnlocked = stagesUnlocked player.score = score player.highScores = highScores player.CPUs.empty() player.itsMe.empty() player.bottomFeeders.empty() player.targetedCPUs.empty() thisLevel = Level.Level_One(player) player.level = thisLevel player.rect.center = (Constants.width * 0.25, Constants.height * 0.73) #add all to groups foregroundSprites.add(thisLevel.platforms) backgroundSprites.add(thisLevel.clouds) playerSprite.add(player) #add CPUS to Player and ForeGround foregroundSprites.add(thisLevel.floaters) player.targetedCPUs.add(thisLevel.targetingFloaters) player.CPUs.add(thisLevel.floaters) player.bottomFeeders.add(thisLevel.bottomFeeders) player.itsMe.add(thisLevel.itsMe) for i in range(len(thisLevel.coins)): thisLevel.coins[i].changeLocation() foregroundSprites.add(thisLevel.coins[i]) coinsSprite.add(thisLevel.coins[i]) for i in range(len(thisLevel.healthBoxes)): thisLevel.healthBoxes[i].changeLocation() foregroundSprites.add(thisLevel.healthBoxes[i]) healthBoxes.add(thisLevel.healthBoxes[i]) for i in range(1): halo = CPUs.myHalo(player) haloSprite.add(halo) #text displayText = TextOnScreen.TextOnScreen(player, thisLevel.floaters, coinsSprite, screen, thisLevel) #Set All Values Back to Normal counter = 0 player.health = 5 playerAlive = True timeLeft = True coinsLeft = True paused = False levelChosen = False HealthGet = False Fullscreen = True #Main Game Loop while playerAlive and timeLeft and coinsLeft and not levelChosen: #Quit for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE): running = playerAlive = False #KeyDown if event.type == KEYDOWN: if event.key == K_LEFT: player.goLeft() player.leftKey = True if event.key == K_RIGHT: player.goRight() player.rightKey = True if event.key == K_SPACE: player.jump() #KeyUp if event.type == KEYUP: if event.key == K_LEFT: player.stopLeft() player.leftKey = False if event.key == K_RIGHT: player.stopRight() player.rightKey = False if event.key == K_SPACE: player.stopJump() if event.key == K_p: paused = not paused player.paused = paused #FullScreen if event.key == K_f: if Fullscreen: screen = pygame.display.set_mode((WINDOWX,WINDOWY)) Fullscreen = False else: screen = pygame.display.set_mode((WINDOWX,WINDOWY), FULLSCREEN) Fullscreen = True #Return Key if event.key == K_RETURN: if paused: #Return Back to Stage Select / Main Menu stageCounter = 0 levelChosen = True continue elif stageCounter != 0 and stageCounter != 11: if player.health < 1: #Player Dead playerAlive = False elif displayText.timer < 1: #Out of Time timeLeft = False elif len(coinsSprite) < 1: #Out of Coins coinsLeft = False elif stageCounter == 0: stageCounter = player.chooseLevel() #Determines if the Player is standing on a Stage Select Box if stageCounter != 0: levelChosen = True continue displayText.helpResetCredits = player.chooseLevelText() #Determines if Player is on a Help / Reset / Credits box if displayText.helpResetCredits == 'reset': #Reset All of the Information levelChosen = True player.stagesUnlocked = stagesUnlocked = d['stagesUnlocked'] = 1 for i in player.highScores: player.highScores[i] = 0 elif stageCounter == 11: if player.health < 1: #Player Dead playerAlive = False addCPU = player.chooseCPUs() if addCPU == '+Tiny Floaters+': i = CPUs.floater(randint(thisLevel.left, thisLevel.right),randint(thisLevel.top, thisLevel.bottom), 5, 5, 3.5, 0.2) i.player = player i.walls = thisLevel.platforms foregroundSprites.add(i) player.level.platforms.add(i) tinyFloaters.add(i) if addCPU == '-Tiny Floaters-': chosenOne = None for i in tinyFloaters: chosenOne = i if chosenOne: chosenOne.kill() if addCPU == '+Floaters+': i = CPUs.floater(randint(thisLevel.left, thisLevel.right),randint(thisLevel.top, thisLevel.bottom), 15, 15, 4, 0.2) i.player = player i.walls = thisLevel.platforms foregroundSprites.add(i) player.CPUs.add(i) normalFloaters.add(i) if addCPU == '-Floaters-': chosenOne = None for i in normalFloaters: chosenOne = i if chosenOne: chosenOne.kill() if addCPU == '+Large Floaters+': i = CPUs.floater(randint(thisLevel.left, thisLevel.right),randint(thisLevel.top, thisLevel.bottom), 30, 30, 2.5, 0.12) i.player = player i.walls = thisLevel.platforms foregroundSprites.add(i) player.CPUs.add(i) largeFloaters.add(i) if addCPU == '-Large Floaters-': chosenOne = None for i in largeFloaters: chosenOne = i if chosenOne: chosenOne.kill() if addCPU == '+Huge Floaters+': i = CPUs.floater(randint(thisLevel.left, thisLevel.right),randint(thisLevel.top, thisLevel.bottom), 60, 60, 1.0, 0.1) i.player = player i.walls = thisLevel.platforms foregroundSprites.add(i) player.CPUs.add(i) hugeFloaters.add(i) if addCPU == '-Huge Floaters-': chosenOne = None for i in hugeFloaters: chosenOne = i if chosenOne: chosenOne.kill() if addCPU == '+Bottom Feeders+': i = CPUs.BottomFeeder(randint(thisLevel.left, thisLevel.right)) i.rect.bottom = thisLevel.bottom i.player = player i.walls = thisLevel.platforms foregroundSprites.add(i) player.bottomFeeders.add(i) bottomFeeders.add(i) if addCPU == '-Bottom Feeders-': chosenOne = None for i in bottomFeeders: chosenOne = i if chosenOne: chosenOne.kill() if addCPU == '+Brothers+': i = CPUs.ItsMe(randint(thisLevel.left, thisLevel.right),randint(thisLevel.top, thisLevel.bottom)) i.player = player thisLevel.platforms.add(i) i.walls = thisLevel.platforms foregroundSprites.add(i) player.itsMe.add(i) brothers.add(i) if addCPU == '-Brothers-': chosenOne = None for i in brothers: chosenOne = i if chosenOne: chosenOne.kill() if addCPU == '+Shooters+' or (event.type == KEYUP and event.key == K_s): i = CPUs.floater(randint(thisLevel.left, thisLevel.right),randint(thisLevel.top, thisLevel.bottom), 5, 5, 3.5, 0.2) i.player = player i.walls = thisLevel.platforms foregroundSprites.add(i) player.targetedCPUs.add(i) player.CPUs.add(i) shooters.add(i) if addCPU == '-Shooters-': chosenOne = None for i in shooters: chosenOne = i if chosenOne: chosenOne.kill() if addCPU == '+Health+': HealthGet = True #WorldShift if player.rect.x >= WINDOWX - 300: #Right Side of Screen x = player.rect.x - (WINDOWX - 300) player.rect.x = WINDOWX - 300 thisLevel.worldShift(-x, 'x') displayText.textWorldShift(-x, 'x') worldShiftBattleRoom(-x, 'x') if player.rect.x <= 300: #Left Side of Screen x = 300 - player.rect.x player.rect.x = 300 thisLevel.worldShift(x, 'x') displayText.textWorldShift(x, 'x') worldShiftBattleRoom(x, 'x') if player.rect.y >= WINDOWY - 150: #Bottom of Screen y = player.rect.y - (WINDOWY - 150) player.rect.y = WINDOWY - 150 thisLevel.worldShift(-y, 'y') displayText.textWorldShift(-y, 'y') worldShiftBattleRoom(-y, 'y') if player.rect.y <= 150: #Top of Screen y = 150 - player.rect.y player.rect.y = 150 thisLevel.worldShift(y, 'y') displayText.textWorldShift(y, 'y') worldShiftBattleRoom(y, 'y') #Add Bullets for floater in thisLevel.floaters: foregroundSprites.add(floater.bullets) for shooter in shooters: foregroundSprites.add(shooter.bullets) #Health / Coins currentHealth = player.health numHealth = len(healthBoxes) numCoins = len(coinsSprite) displayText.counter += 0.25 counter += 0.25 #If Player is Alive, Player Can Move if player.health > 0 and not paused: playerSprite.update() #Is Player Outside the Box?? if player.rect.centerx < thisLevel.left or\ player.rect.centerx > thisLevel.right or\ player.rect.centery < thisLevel.top or\ player.rect.centery > thisLevel.bottom: if counter % 5 == 0: player.health -= 1 #If Time and Coins Left, and Not Paused, (Or if Player is Dead) Everything Else Can Move if (counter > 50 and displayText.timer > 1 and len(coinsSprite) > 0 and not paused) or player.health < 1 or stageCounter == 11: displayText.timer -= 0.02 backgroundSprites.update() foregroundSprites.update() for i in range(len(thisLevel.coins)): if thisLevel.coins[i].playerGrabbed(): thisLevel.coins[i].kill() for i in range(len(thisLevel.healthBoxes)): if thisLevel.healthBoxes[i].playerGrabbed(): thisLevel.healthBoxes[i].kill() #Player Halo haloSprite.update() #Draw To Screen screen.fill(bPURPLE) backgroundSprites.draw(screen) displayText.updateBack() foregroundSprites.draw(screen) haloSprite.draw(screen) playerSprite.draw(screen) displayText.updateFront() player.newShowLines(screen) #Flashes Display on Screen if player.health != currentHealth: #Flash White if Player Hurt screen.fill((255,255,255)) if numHealth != len(healthBoxes) or HealthGet: #Flash Turquoise if Health Box, and Give Player Health screen.fill((0,255,255)) player.health += 3 if player.health > 5: player.health = 5 HealthGet = False if numCoins != len(coinsSprite): #Flash Yellow if Coin, Give Player a Point screen.fill((175, 175, 20)) score += 1 player.score += 1 pygame.display.flip() #Score Reset To Zero When Player Goes to Main Menu if stageCounter == 0: score = 0 #Player Died, Back to Main Menu elif not playerAlive: stageCounter = 0 score = 0 #All Coins, Display Scores, Send On to Next Level elif not coinsLeft: player.score += int(displayText.timer) if player.score > player.highScores[stageCounter]: player.highScores[stageCounter] = player.score stageCounter += 1 if stageCounter > stagesUnlocked: stagesUnlocked = stageCounter #No Time Left elif not timeLeft: if len(coinsSprite) > 5: #Player Didn't Get Enough Coins, Back to Menu score = 0 else: if player.score > player.highScores[stageCounter]: #Player Sent One to Next Level player.highScores[stageCounter] = player.score player.score = 0 stageCounter += 1 if stageCounter > stagesUnlocked: stagesUnlocked = stageCounter score = 0 #Save Stages and High Scores d['stagesUnlocked'] = stagesUnlocked d.close() scores_file = open('scores_file', 'wb') pickle._dump(player.highScores, scores_file) scores_file.close()
def save(dataset: SharedDataset, file_path: str): with open(file_path, 'wb') as file: pickle._dump(dataset, file)
__author__ = 'Ingrid Marie' import copy import keyword import sys import pickle print(keyword.iskeyword('if')) print(keyword.kwlist) print(sys.version) mySaveDOC = ['william','james','peter'] save_file = open('pickle.pdf' , 'wb') pickle._dump(mySaveDOC, save_file) save_file.close()
def save(model: Model, target_path: str): with open(target_path, 'wb') as file: pickle._dump(model, file)
def write_file(filename,obj): with open(r'C:\Users\liuyunhai\Desktop\Python\tmp'+filename,'wb') as f: pickle._dump(obj,f)
my_list = [] for k, v in trie.items(): if k != '_': for el in self.list(v): my_list.append(k+el) else: my_list.append('') return my_list def get_trie(self): return self.trie # if __name__ == '__main__': mt = Trie() lines = mt.get_file() # mt.__init__() words = [] trie = mt.get_trie() for word in lines: if word != None: mt.add(trie, word) pickle._dump(trie, open( "pickletree", "wb" )) trie = None trie = pickle.load( open( "pickletree", "rb" ) ) # print(mt.list(trie)) print(trie) # print(mt.in_trie(trie, 'bar')) # print(mt.in_trie(trie, 'bab')) # print(mt.in_trie(trie, 'zzz')) # print(mt.in_trie(trie, 'bax')) # print(mt.in_trie(trie, 'baz'))