def test_get_config_deployment(self): self.assertEqual( main.get_config( open( os.path.join(os.path.dirname(os.path.dirname(__file__)), "examples", "deployment.yml"), "r")), dicts.exp_get_config_deployment)
def test_get_config_concourse(self): self.assertEqual( main.get_config( open( os.path.join(os.path.dirname(__file__), "samples", "concourse.yml"), "r")), dicts.exp_get_config_concourse)
def upload_image(): error = None if request.method == "POST": description = request.form.get('description', None) if request.form.get("imgur-img"): image = request.files["image"] if not image: error = "No image chosen" return render_template("upload_image.html", error = error) user_id = get_config().imgur_id extension = split_filename(image.filename, True) if extension not in app.config["ALLOWED_FILENAMES"]: error = "Allowed extensions are %r" % (", ".join(app.config["ALLOWED_FILENAMES"])) return render_template("upload_image.html", error = error) filename = secure_filename(image.filename.strip()) user = Users.get_user_by_username(session["user"]) config = dict( image = image, name = filename, description = description ) response = ImgurHandler(user_id, config).send_image() if not response["success"]: error = "Error uploading to imgur" return render_template("upload_image.html", error = error) response_data = response["data"] image_link = response_data["link"] is_vertical = response_data["width"] + 10 < response_data["height"] delete_hash = response_data["deletehash"] try: UserImages.add_image(image_link = image_link,\ description = description,\ delete_hash = delete_hash,\ is_vertical = is_vertical,\ imgur_img = True,\ owner = user) return redirect(url_for("user_images", username = user.username)) except: error = "Error writing to database" return render_template("upload_image.html", error = error) return render_template("upload_image.html", error = response) elif request.form.get('save-link'): link = request.form.get('image-link', None) if not link: error = "No link given" return render_template("upload_image.html", error = error) user = Users.get_user_by_username(session["user"]) try: UserImages.add_image(image_link = link,\ description = description,\ is_vertical = True,\ imgur_img = False,\ owner = user) return redirect(url_for("user_images", username = user.username)) except Exception as e: error = "Error writing to database" return render_template("upload_image.html", error = error) else: return render_template("upload_image.html")
async def delskip(self, ctx, *args): if not ctx.guild: return await ctx.send('You can\'t do that here!') config = get_config(ctx) p = config.get('prefix') # checks if not config.get('channel'): return await ctx.send( f'Please finish setting up the skip environment first with `{p}help skip`! (Missing whitelist)' ) elif not config.get('default'): return await ctx.send( f'Please finish setting up the skip environment first with `{p}help skip`! (Missing default)' ) elif not ctx.channel.id == config.get('channel'): return elif len(args) != 1: return await ctx.send('Please enter a valid username.') elif args[0].lower == 'skipbot': await ctx.send('hey thats me') name = args[0].lower().replace('\\', '') # format text # skip file processing with pymongo.MongoClient(os.environ.get('MONGO')) as client: db = client['skips'] skips = db['slist'] skipData = skips.find_one({'name': args[0], 'guild': ctx.guild.id}) if skipData is None: await ctx.send(f'`{name}` has not skipped yet.') return else: skipsUsed = skipData.get('skips') skipAmount = skipData.get('amount') #send skip messge message = await ctx.send( f'`{name}` has used {skipsUsed}/{skipAmount} skips. React ⏪ to undo a skip, react ➖ to allow less.' ) emojis = ['⏪', '➖'] for emoji in emojis: await message.add_reaction(emoji) self.messid.update({message.id: time.time()})
def main(): if os.path.isfile(DB_FILENAME): print "remove existing DB" os.remove(DB_FILENAME) config = get_config('secrets.json') twitter = TestTwitter(**config) hackernews_frontpage = HackerNewsFrontPage(DB_FILENAME, twitter) firebase = FirebaseStreamingEvents(hackernews_frontpage) try: firebase.run() except KeyboardInterrupt: print "Shutting down"
def delete_image(id): image = UserImages.get_image(id) if not image: abort(404) if image.owner.username != session["user"]: flash("Don't try to delete other\'s dude\'s pictures...dude") return redirect(url_for("index")) else: try: UserImages.delete_image(image) except: error = "Error occured when writing to database" flash(error) return redirect(url_for("index")) if image.imgur_img: resp = ImgurHandler(get_config().imgur_id).delete_image(image.delete_hash) if not resp["success"]: handle_errors(resp) return redirect(url_for("user_images", username = session["user"]))
def gallery(page): if not get_config().gallery: return redirect(url_for("index")) per_page = settings.get("images_per_page", 10) images = UserImages.get_gallery_images(page = page,\ per_page = per_page,\ gallery = True) if not tuple(images) and page != 1: abort(404) count = images.wrapped_count() show_pagination = count > per_page thumbnail_size = settings.get("thumbnail_size", "l") images_uploaded = count > 0 pagination = Pagination(page, per_page, count) return render_template("gallery.html",\ images_uploaded = images_uploaded,\ pagination = pagination,\ thumbnail_size = thumbnail_size,\ images = images,\ show_pagination = show_pagination)
def config(): return get_config(testing=True)
stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() retcode = p.poll() result = {} result["url"] = url result["path"] = output_path if retcode != 0 or os.path.getsize(output_path) < 5000: result["stdout"] = stdout result["stderr"] = stderr result["retcode"] = retcode result["status"] = "failed" else: im = Image.open(output_path) if im.size[0] < 100 or im.size[1] < 100: result["status"] = "failed" result["message"] = "Image too small" else: soup = BeautifulSoup(urllib2.urlopen(url)) result["title"] = soup.title.string result["status"] = "succeeded" return result if __name__ == "__main__": from main import get_config import sys config = get_config() print create_local_webshot(config, sys.argv[1])
async def on_reaction_add(self, reaction, user): message = reaction.message emoji = reaction.emoji if user.bot or not message.id in self.messid: return name = message.content.split(' ')[0][1:-1] default = get_config(message).get('default') with pymongo.MongoClient(os.environ.get('MONGO')) as client: db = client['skips'] skips = db['slist'] skipData = skips.find_one({ 'name': name, 'guild': message.guild.id }) # skip user if emoji == '⏪': skipsUsed = skipData.get('skips') - 1 with pymongo.MongoClient(os.environ.get('MONGO')) as client: db = client['skips'] skips = db['slist'] if skipData.get('skips') == 1 and skipData.get( 'amount') == default: skips.delete_one(skipData) else: query = {'name': name, 'guild': message.guild.id} skips.update(query, {'$set': {'skips': skipsUsed}}) await message.channel.send(f'Skipped {name}. ({skipsUsed})') del self.messid[message.id] # allow skip elif emoji == '➖': skipAmount = skipData.get('amount') - 1 with pymongo.MongoClient(os.environ.get('MONGO')) as client: db = client['skips'] skips = db['slist'] if skipData.get('skips') == 0 and skipData.get( 'amount') == default + 1: skips.delete_one(skipData) else: query = {'name': name, 'guild': message.guild.id} skips.update(query, {'$set': {'amount': skipAmount}}) await message.channel.send( f'Allowed {skipAmount} skips for `{name}`.') del self.messid[message.id] return
async def help(self, ctx, *command): if command in (('skip',), ('skips',)): prefix = get_config(ctx).get('prefix') embed = discord.Embed( title="Skipping", color=discord.Color.gold()) embed.add_field( name='Setup', value=""" `{0}whitelist`: Choose a channel in which skips can be performed. Make sure guests and res cannot send messages in the channel. (Admin) `{0}default` [num]: Sets the amount of skips people start out with. [try to only set this once] (Server Owner) """.format(prefix), inline=True ) embed.add_field( name='Skipping', value=""" `{0}skip` or `{0}s` [name]: Brings up the skip menu. In the menu, react with ⏩ to skip the player, or react ➕ to allow one more skip for the player. `{0}delskip` or `{0}d` [name]: Brings up a menu similar to the skip menu, except for deleting skips instead of adding them. """.format(prefix), inline=False ) embed.add_field( name='Other', value=""" `{0}reset`: Deletes ALL skips from the database. Cannot undo. (Server Owner) `{0}export`: Export all the skips into a text file. (Admin) `{0}import`: Export skips from a text file. Deletes current skips in the process. (Server Owner) For other users to use owner only commands, create a role called "Trusted" and assign it to them. Make sure it is near the top so that others may not assign it to themselves. """.format(prefix), inline=False ) embed.set_footer(text="https://repl.it/@nochef/skipBot") await ctx.send(embed=embed) return prefix = get_config(ctx).get('prefix') embed = discord.Embed( title="skipBot Help", color=discord.Color.gold() ) embed.add_field( name='Skip Commands', value=""" `{0}skip` or `{0}s` [name]: Brings up the skip menu (say `{0}help skip` for more info) `{0}delskip` or `{0}d` [name]: Brings up the skip deletion menu. `{0}export`: Exports skips into a file. `{0}import`: Imports skips from a file. (Deletes current skips in the database) `{0}whitelist`: Changes the channel in which the skip command works `{0}default`: Sets the default amount of skips players will recieve. """.format(prefix), inline=True ) embed.add_field( name='Other Commands', value=""" `{0}prefix` or `{0}skipPrefix` [new prefix]: Changes the prefix `{0}help`: Brings up this menu `{0}ping`: Shows bot ping `{0}joke`: Says a Club Penguin joke `{0}invite`: Sends the bot invite """.format(prefix), inline=False ) embed.set_footer(text="https://repl.it/@nochef/skipBot") await ctx.send(embed=embed)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() retcode = p.poll() result = {} result["url"] = url result["path"] = output_path if retcode != 0 or os.path.getsize(output_path) < 5000: result["stdout"] = stdout result["stderr"] = stderr result["retcode"] = retcode result["status"] = "failed" else: im = Image.open(output_path) if im.size[0] < 100 or im.size[1] < 100: result["status"] = "failed" result["message"] = "Image too small" else: soup = BeautifulSoup(urllib2.urlopen(url)) result["title"] = soup.title.string result["status"] = "succeeded" return result if __name__ == "__main__": from main import get_config import sys config = get_config() print create_local_webshot(config, sys.argv[1])
async def on_reaction_add(self, reaction, user): message = reaction.message emoji = reaction.emoji if user.bot or not message.id in self.messid: return name = message.content.split(' ')[0][1:-1] defaultSkips = get_config(message).get('default') with pymongo.MongoClient(os.environ.get('MONGO')) as client: db = client['skips'] skips = db['slist'] skipData = skips.find_one({ 'name': name, 'guild': message.guild.id }) print(skipData) # debug # skip user if emoji == '⏩': if skipData is None: skipsUsed = 1 with pymongo.MongoClient(os.environ.get('MONGO')) as client: db = client['skips'] skips = db['slist'] skips.insert_one({ 'name': name, 'skips': 1, 'amount': defaultSkips, 'guild': message.guild.id }) else: skipsUsed = skipData.get('skips') + 1 with pymongo.MongoClient(os.environ.get('MONGO')) as client: db = client['skips'] skips = db['slist'] query = {'name': name, 'guild': message.guild.id} skips.update(query, {'$set': {'skips': skipsUsed}}) await message.channel.send(f'Skipped `{name}`. ({skipsUsed})') del self.messid[message.id] # allow skip elif emoji == "➕": if skipData is None: skipAmount = defaultSkips + 1 with pymongo.MongoClient(os.environ.get('MONGO')) as client: db = client['skips'] skips = db['slist'] skips.insert_one({ 'name': name, 'skips': 0, 'amount': skipAmount, 'guild': message.guild.id }) else: skipAmount = skipData.get('amount') + 1 with pymongo.MongoClient(os.environ.get('MONGO')) as client: db = client['skips'] skips = db['slist'] query = {'name': name, 'guild': message.guild.id} skips.update(query, {'$set': {'amount': skipAmount}}) await message.channel.send( f'Allowed {skipAmount} skips for `{name}`.') del self.messid[message.id] return
def run_models(config, subset_names, subset_indices): models = config['models'] folder = config['dataset']['save_folder'] for name, indices in zip(subset_names, subset_indices): total_mse = [0 for i in range(len(models))] total_rmse = [0 for i in range(len(models))] total_r2 = [0 for i in range(len(models))] total_adj_r2 = [0 for i in range(len(models))] total_accuracy = [0 for i in range(len(models))] total_balanced_accuracy = [0 for i in range(len(models))] for i, model in enumerate(models): temp_config = get_config({**config, 'model': model}) temp_config['data_path'] = folder + '/subdata_' + name + '.pkl' temp_config['print'] = config['print'] if model in ['NN', 'LSTM', 'BiLSTM']: temp_config['in_dim'] = indices[1] if model == "NN": if name in set(['pr_su_bf_ma_tsfp', 'pr_su_bf_ma_tsfp_tsfd']): temp_config['lr'] = 0.0001 all_folds, all_folds_baseline = get_folds(temp_config) for index, (fold, fold_base) in enumerate( zip(all_folds, all_folds_baseline)): if model == 'baseline': mse, rmse, r2, adj_r2, accuracy, balanced_accuracy = run_model( temp_config, fold, fold_base) else: mse, rmse, r2, adj_r2, accuracy, balanced_accuracy = run_model( temp_config, fold) total_mse[i] += mse total_rmse[i] += rmse total_r2[i] += r2 total_adj_r2[i] += adj_r2 total_accuracy[i] += accuracy total_balanced_accuracy[i] += balanced_accuracy # Calculate the average over all runs mses = [mse / len(all_folds) for mse in total_mse] rmses = [rmse / len(all_folds) for rmse in total_rmse] r2s = [r2 / len(all_folds) for r2 in total_r2] adj_r2s = [adj_r2 / len(all_folds) for adj_r2 in total_adj_r2] accuracies = [accuracy / len(all_folds) for accuracy in total_accuracy] balanced_accuracies = [ balanced_accuracy / len(all_folds) for balanced_accuracy in total_balanced_accuracy ] # Print the results in a table table = [['mse'] + mses, ['root_mse'] + rmses, ['r2_score'] + r2s, ['adj_r2_score'] + adj_r2s, ['accuracy'] + accuracies, ['bal_accuracy'] + balanced_accuracies] if not os.path.exists("results"): os.makedirs("results") pd.DataFrame(table, columns=["metrics"] + models).to_csv("results/results_" + name + ".csv") #oke nice, dan kan ik nu helemaal overnieuw alles gaan runnen? Ja idd, en moet ook ff die wijziging van net terug draaien print('dataset: ' + name) print( tabulate(table, headers=['metrics'] + models, tablefmt="fancy_grid")) # plain
def config(): return get_config()