def shutdown(): """Save all data to a file - one for books, one for the current counter value, for persistent storage""" output_data = make_output_data() file.write(DATA_DIR, BOOKS_FILE_NAME, output_data) file.write(DATA_DIR, COUNTER_FILE_NAME, counter)
async def on_ready(): t = time.localtime() current_time = time.strftime("[%H:%M]", t) print(f"{current_time}:login successful as {bot.user}") servers = list_servers() while True: place = 1 for i in servers: print(f"{place}:{await bot.fetch_guild(i)}") place += 1 server_select = input(">>>") try: data = files.read(f"data/{servers[int(server_select)-1]}.pkl") except (IndexError, ValueError): print("You have to select a valid server") continue while True: while True: action_counter = 1 for i in data: if type(i) == str: print(f"{action_counter}:{i} = {data[i]}") else: print( f"{action_counter}:{await bot.fetch_user(i)} = {data[i]}" ) action_counter += 1 action_select = input(">>>") try: int(action_select) break except ValueError: print("Not Valid number") action_counter = 1 for i in data: if action_counter == int(action_select): action = i break else: pass action_counter += 1 try: confirmation = input( f"Do you want to edit {await bot.fetch_user(action)}? \n>>>" ) except discord.errors.HTTPException: confirmation = input(f"Do you want to edit {action}? \n>>>") if confirmation.lower() == "y": pass else: continue new_value = input("Set new value \n>>>") try: data[action] = int(new_value) except ValueError: data[action] = new_value files.write(f"data/{servers[int(server_select)-1]}.pkl", data)
def SaveLog(self): name = QtWidgets.QFileDialog.getSaveFileName(self, 'Сохранить LOG') print(name) file = open(name[0], 'w') for i in range(0, len(self.listWidget)): text = self.listWidget.item(i).text() print(text) file.write(str(text) + "\n") file.close()
def write_comments(job): """ Write content of comment file to errors file. :param job: job object :type job: :py:obj:`object` """ comments = read(job.comment_file) if comments: buf = \ '------- ' 'Contents of output stream forwarded by the LRMS ' '---------\n' buf += ''.join(comments) buf += \ '------------------------- ' 'End of output ' '-------------------------' write(job.errors_file, buf, 0644, True)
def update_diag(job): """ Filters out WallTime from the diag file if present and replaces it with output from the batch system. It also adds StartTime and EndTime for accounting. :param job: job object :type job: :py:obj:`object` """ content = read(job.diag_file) diag_dict = {} for line in content: line = line.strip(' \n#') if not line: continue key, value = line.split('=', 1) if key[:9] == 'frontend_': continue if key not in diag_dict: diag_dict[key] = [] diag_dict[key].append(value) # Do not save the 'frontend_*' and 'ExecutionUnits' keys, # they are set on the font-end. Not to be overwritten diag_dict.pop('ExecutionUnits', None) keys = ['nodename', 'WallTime', 'UserTime', 'KernelTime', 'AverageTotalMemory', 'AverageResidentMemory', 'exitcode', 'LRMSStartTime', 'LRMSEndTime', 'LRMSExitcode', 'LRMSMessage'] for key in keys: if key in diag_dict: diag_dict[key] = diag_dict[key][-1:] if hasattr(job, 'Processors'): diag_dict['Processors'] = [job.Processors] if hasattr(job, 'LRMSStartTime'): diag_dict['LRMSStartTime'] = [job.LRMSStartTime.str(arc.common.MDSTime)] if hasattr(job, 'LRMSEndTime'): diag_dict['LRMSEndTime'] = [job.LRMSEndTime.str(arc.common.MDSTime)] if hasattr(job, 'WallTime'): diag_dict['WallTime'] = ['%ds' % (job.WallTime.GetPeriod())] if not diag_dict.has_key('exitcode') and hasattr(job, 'exitcode'): diag_dict['exitcode'] = [job.exitcode] buf = '' for k, vs in diag_dict.iteritems(): buf += '\n'.join('%s=%s' % (k, v) for v in vs) + '\n' if write(job.diag_file, buf, 0644): # Set job user as owner os.chown(job.diag_file, job.uid, job.gid)
def create(userid, journal, friends_only=False, tags=None): # Check invalid arguments if not journal.title: raise WeasylError("titleInvalid") elif not journal.content: raise WeasylError("contentInvalid") elif not journal.rating: raise WeasylError("ratingInvalid") profile.check_user_rating_allowed(userid, journal.rating) # Assign settings settings = "f" if friends_only else "" # Create journal jo = d.meta.tables["journal"] journalid = d.engine.execute(jo.insert().returning(jo.c.journalid), { "userid": userid, "title": journal.title, "rating": journal.rating.code, "unixtime": arrow.now(), "settings": settings, }).scalar() # Write journal file files.make_path(journalid, "journal") files.write(files.make_resource(userid, journalid, "journal/submit"), journal.content) # Assign search tags searchtag.associate(userid, tags, journalid=journalid) # Create notifications if "m" not in settings: welcome.journal_insert(userid, journalid, rating=journal.rating.code, settings=settings) d.metric('increment', 'journals') return journalid
def edit(userid, journal, friends_only=False): if not journal.title: raise WeasylError("titleInvalid") elif not journal.content: raise WeasylError("contentInvalid") elif not journal.rating: raise WeasylError("ratingInvalid") profile.check_user_rating_allowed(userid, journal.rating) query = d.execute("SELECT userid, settings FROM journal WHERE journalid = %i", [journal.journalid], options="single") if not query or "h" in query[1]: raise WeasylError("Unexpected") elif userid != query[0] and userid not in staff.MODS: raise WeasylError("InsufficientPermissions") settings = [query[1].replace("f", "")] settings.append("f" if friends_only else "") settings = "".join(settings) if "f" in settings: welcome.journal_remove(journal.journalid) # TODO(kailys): use ORM d.execute("UPDATE journal SET (title, rating, settings) = ('%s', %i, '%s') WHERE journalid = %i", [journal.title, journal.rating.code, settings, journal.journalid]) # Write journal file files.write(files.make_resource(userid, journal.journalid, "journal/submit"), journal.content) if userid != query[0]: from weasyl import moderation moderation.note_about( userid, query[0], 'The following journal was edited:', '- ' + text.markdown_link(journal.title, '/journal/%s?anyway=true' % (journal.journalid,)))
import time import os bot = discord.Client() # load the Token into a variable / ask for it try: token = files.read("data/token.pkl") except FileNotFoundError: token = input("INPUT TOKEN:") # the writing of the token.pkl file does not automatically generate a data folder # this creates it if it is missing try: files.write("data/token.pkl", token) except FileNotFoundError: os.system("mkdir data") files.write("data/token.pkl", token) # TODO # nothing rn @bot.event async def on_ready(): t = time.localtime() current_time = time.strftime("[%H:%M]", t) print(f"{current_time}:login successful as {bot.user}") # downloads the
def put(): try: r = files.write() except Exception, e: r = 'System Error:%s' % e
file_path = os.path.dirname(os.path.abspath(__file__)) if not (file_path in sys.path): sys.path.append(file_path) import paths admin_file = paths.admin_file_path if __name__ == "__main__": from encryption import cryption from files import write from accounts import account account_list = [] auth_list = ["master_login", "master_manager"] authorizations = "" for auth in auth_list: authorizations = authorizations + auth + "\t" authorizations = authorizations[:-1] account_list.append( account(cryption("admin"), cryption("admin"), cryption(authorizations))) account_list.append( account(cryption("admin1"), cryption("admin1"), cryption(authorizations))) buffer = "" for account in account_list: buffer = buffer + str(account) + "\n" buffer = buffer[:-1] write(admin_file, buffer)
def files_list_view(self): if not 'password' in self.request.POST and self.check(): print self.request.POST return HTTPFound(location='/login') url = USB_LOCATION + get_url(self.request.matchdict['list']) url_parsed = '/' for i in range(len(url.split('/')) - 3): url_parsed += url.split('/')[i + 1] + '/' action = url.split('/')[-2] filename = str(url_parsed.split('/')[-2]) if 'password' in self.request.POST: if self.request.POST['password'] != '': password = self.request.POST['password'] # print password content = encrypt(self.request.POST['notecontent'], password) write('/'.join(url.split('/')[:-2]), content) return HTTPFound(location='/files' + url[:-1]) # return Response() elif 'file' in self.request.POST: filename = self.request.POST['file'].filename print filename input_file = self.request.POST['file'].file upload(input_file, url, filename) print '/files' + url return HTTPFound(location='/files' + url) elif 'dir_name' in self.request.POST: dirname = self.request.POST['dir_name'] make_dir(dirname, url) return HTTPFound(location='/files' + url) elif 'note_name' in self.request.POST: write(url + self.request.POST['note_name'], '') return HTTPFound(location='/files' + url) elif 'notecontent' in self.request.POST: content = encrypt(self.request.POST['notecontent'], decrypt(self.request.session['enpass'], self.request.cookies['r'])) write('/'.join(url.split('/')[:-2]), content) return HTTPFound(location='/files' + url) elif action == 'edit': content = decrypt(read(url_parsed[:-1]), decrypt(self.request.session['enpass'], self.request.cookies['r'])) re = { 'page_title' : 'Notes', 'edit' : True, 'contents' : content, 'url' : url } re.update(self.get_header()) return re elif action == 'rename': # file_old = str(url_parsed.split('/')[-3]) file_old = '/'.join(url_parsed.split('/')[:-2]) if not is_file('/'.join(url_parsed.split('/')[:-2])) and not is_dir('/'.join(url_parsed.split('/')[:-2])): print 'not filename' return HTTPFound(location='/files') rename(file_old, filename) return HTTPFound('/files' + '/'.join(url.split('/')[:-4])) elif is_file(url_parsed[:-1]): if action == 'download': re = FileResponse(url_parsed[:-1]) re.headers['Content-Disposition'] = 'attachment; filename="' + filename + '"' re.headers['Content-Type'] = 'application/force-download' # re.headers['filename'] = filename re.headers['Accept-Ranges'] = 'bytes' return re elif action == 'delete': delete(url_parsed[:-1]) return HTTPFound(self.request.url[:-7-len(filename.replace(' ', '%20'))]) elif is_dir(url[:-7]) and action == 'delete': delete(url[:-7]) return HTTPFound(self.request.url[:-7-len(filename)]) elif not is_dir(url) or len(url.split('/')) < 5: return HTTPFound(location='/files') temp = [str(part) for part in list_dir(url)] temp.sort(lambda x, y: cmp(x.lower(),y.lower())) item_list = [ { 'url' : '/files/' + url[1:] + part if is_dir(url + part) else '/files/' + url[1:] + part + '/download', 'url_ren' : '/files/' + url[1:] + part, 'url_del' : '/files/' + url[1:] + part + '/delete', 'name' : part, 'is_file' : is_file(url + part), 'size' : size(url + part) } for part in temp ] re = {'page_title': LINKS['files_list']['title'], 'list' : item_list, 'up_dir' : '/files/' + url_parsed[1:], 'url' : url, 'edit' : False } re.update(self.get_header()) return re
generated += ' '.join(gen_sentence) print('----- Generating with seed: "' + str(gen_sentence) + '"') sys.stdout.write(generated) for i in range(400): x_prediction = np.zeros((1, max_len, len(chars))) for t, char in enumerate(gen_sentence): x_prediction[0, t, char_indices[char]] = 1. preds = model.predict(x_prediction, verbose=0)[0] next_index = sample(preds, diversity) next_char = indices_char[next_index] generated += next_char gen_sentence = gen_sentence[1:] gen_sentence.append(next_char) add(next_char + ' ', 'out_soundex.txt') print() model.fit(x, y, batch_size=128, epochs=60, callbacks=[LambdaCallback(on_epoch_end=on_epoch_end)]) out_soundex = read('out_soundex.txt', False) text = to_text(out_soundex, content_soundex_dictionary) write(text, 'out_text.txt')
async def on_message(message): t = time.localtime() current_time = time.strftime("[%H:%M]", t) # Ignore own messages unless they are leaderboard messages, if so save them to the guild's file if message.author == bot.user: data = files.read(f"data/{message.guild.id}.pkl") # Write the channel id and message id (dependent on channel) into the guild file try: if message.channel.id == data["leaderboard_message_channel_id"]: data["leaderboard_message_id"] = message.id files.write(f"data/{message.guild.id}.pkl", data) return except KeyError: if str(message.channel).lower() == "leaderboard": data["leaderboard_message_id"] = message.id data["leaderboard_message_channel_id"] = message.channel.id files.write(f"data/{message.guild.id}.pkl", data) if message.author.bot: forbidden = ["leaderboard", "counting"] data = files.read(f"data/{message.guild.id}.pkl") if (str(message.channel) in forbidden or message.channel.id == data["leaderboard_message_channel_id"] ) and message.author != bot.user: await message.delete() return # Put server's data into var data for efficiency try: data = files.read(f"data/{message.guild.id}.pkl") except FileNotFoundError: # if the file is not found intitialize the guild with counter on 1 files.write(f"data/{message.guild.id}.pkl", {"count": 1}) data = files.read(f"data/{message.guild.id}.pkl") except AttributeError: # This only occurs in a private chat await message.channel.send("Sorry you have to be on a server to count") return try: expected_number = int(data["count"]) except ValueError: # This prevents breaking of functionality when the Value is a string as a result of corrections.py expected_number = 1 # dont count if messages are not send in the counting channel if str(message.channel) == "counting": # Check if message is a number # If not disregard the message and delete it try: message_number = int(message.content) except ValueError: print( f"{current_time} on {message.guild}: was looking for {expected_number} got {message.content} instead" ) await message.delete() return # ignore if the author of the message was the last one counting try: if message.author.id == data["last_counter"]: await message.channel.send( f"Sie sind der letzte der Zählte, **{message.author}**!") await message.delete() return except KeyError: pass # delete message if the number isn't the one we're searching for if message_number == expected_number: # Update Values for counting data["count"] = expected_number + 1 data["last_counter"] = message.author.id try: # Read participation data add 1 to user's value participation_user = data[message.author.id] participation_user = participation_user + 1 data[message.author.id] = participation_user except KeyError: # If participation is not there yet set it to one data[message.author.id] = 1 # this is used for updating the leaderboard try: data["till_update"] = data["till_update"] - 1 except KeyError: data["till_update"] = 99 # <= because of caution IF something goes wrong that decreases the till_update key to a negative # or stupid / not intended use of corrections.py if data["till_update"] <= 0: data["till_update"] = 100 # Jup i have 0 clues on why attribute errors are excepted here # will test later it's 1am so no not now try: # Ignore if leaderboard messages haven't been configured yet, is normal on first run try: channel = await bot.fetch_channel( data["leaderboard_message_channel_id"]) msg = await channel.fetch_message( data["leaderboard_message_id"]) await msg.delete() # delete old Message so you dont have multiple leaderboards, done first bc even when something # goes wrong, there shall be now indication of that on the discord users end only logs except KeyError: print( f"{current_time} on {message.guild}: No old leaderboard msg found" ) for channel in message.guild.channels: # this could potentionally reconfigure the leaderboard channel without will, # however in order for this to happen, the KeyError has to be raised by the "leaderboard_message_id" key # this key is always defined except on first run # aka when no leaderboard channel is defined anyways # the only exception being someone working with corrections.py # TODO # make this specific to "leaderboard_message_channel_id" key if str(channel) == "leaderboard": if type(channel ) == discord.channel.CategoryChannel: pass else: print( f"{current_time} on {message.guild}: setup for leaderboard channel successful" ) data[ "leaderboard_message_channel_id"] = channel.id try: # This is only undefined when the setup above fails to locate any channels named "leaderboard" data["leaderboard_message_channel_id"] except KeyError: print( f"{current_time} on {message.guild}: FATAL no leaderboards channel found" ) # user counts are deleted to ensure that no Errors will hinder the count from being valid # will look into this, maybe making a leaderboardless version idk await message.delete() return pass except discord.errors.NotFound: print( f"{current_time} on {message.guild}: FATAL leaderboard msg not found!" ) pass # List all Counters counters = [] for user in data: if type(user) == int: counters.append([data[user], user]) else: # Sort output counters.sort() # we dont wan't the lowest on the leaderboard counters.reverse() board = "---------Leaderboard-----------------------------\n" for place in range(10): try: name = await bot.fetch_user(counters[place][1]) board += f"{place + 1}:{name}:{counters[place][0]}\n" except IndexError: pass # I should probably move this check to the others try: channel = await bot.fetch_channel( data["leaderboard_message_channel_id"]) except discord.errors.NotFound: print( f"{current_time} on {message.guild}: FATAL leaderboard channel deleted" ) data = files.read(f"data/{message.guild.id}.pkl") try: del data["leaderboard_message_channel_id"] except KeyError: pass files.write(f"data/{message.guild.id}.pkl", data) await message.delete() return await channel.send(board) print( f"{current_time} on {message.guild}: updated leaderboard" ) except AttributeError: pass # updating the guild file # this prevents overwriting the leader_message_id key as the updating is handled async and is faster # yes i know this is not good, I will look to improve this try: data_with_id = files.read(f'data/{message.guild.id}.pkl') data["leaderboard_message_id"] = data_with_id[ "leaderboard_message_id"] except KeyError: pass if (data["count"] - 1) % 100 == 0: print( f"{current_time} on {message.guild}: Server reached {message.content}" ) files.write(f"data/{message.guild.id}.pkl", data) else: await message.delete() if str(message.channel) == "leaderboard": # As all bot actions quit the function before so this is safe await message.delete() return
import parse, cache, itertools, files link = parse.base + 'classical/sort=0,start=' prev = cache.load_prev() codes_old = [] for i in itertools.count(start=prev + 1): src = parse.get_source(link + str(i*50)) codes = parse.get_codes(src) if codes == codes_old: break codes_old = codes names, probs, users = cache.load() for p, code in enumerate(codes): parse.print_progress(i, p + 1) parse.problem(code, names, probs, users) if len(codes) == 50: cache.dump(i, names, probs, users) for code, name in names.items(): names[code] = name.encode('utf-8') files.clear() files.write(names, probs, users)
elif infos == 'noextension' : print(Fore.RED + "Could not detect that file type.") files.createEmpty(fileName) else : #Our file has an extension name, extension = infos[0], infos[1] if not files.canWrite(fileName) : #If file already exists, don't take risk of overwriting it, quit print(Fore.RED + "Will not be able to create file {}, file already exists.".format(fileName)) sys.exit(-1) if not templates.exists(extension) : #If extension is unknown print(Fore.RED + "No template found for " + Style.BRIGHT + ".{}".format(extension) + Style.RESET_ALL + Fore.RED + " files.") files.createEmpty(fileName) else : #If it is a known extension template = 0 if '-c' in params : template = templates.askToChoose(extension) files.write(fileName, extension, template) print(Fore.GREEN + "File was successfully created.") if not '-d' in params : #Opening os.system('{} {}'.format(files.getConfig('editor'), fileName))