Skip to content

Instantly share code, notes, and snippets.

View britonad's full-sized avatar
🪖
Fighting for the bright future of 🇺🇦

Vladyslav Krylasov britonad

🪖
Fighting for the bright future of 🇺🇦
View GitHub Profile
def convert_size(size_bytes):
if size_bytes == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(size_bytes, 1024)))
p = math.pow(1024, i)
s = round(size_bytes / p, 2)
return "%s %s" % (s, size_name[i])
@britonad
britonad / context.py
Created August 22, 2017 12:59
Render template without flask context
def render_without_context(template_name, **context):
env = jinja2.Environment(
loader=jinja2.PackageLoader('ui')
)
template = env.get_template(template_name)
return template.render(**context)
@britonad
britonad / wrap.py
Created November 10, 2017 12:04
Wrap Flask app context to all the class methods
class Wrapper:
def __init__(self, wrapped_class):
self.wrapped_class = wrapped_class()
def __getattr__(self, item):
attr = self.wrapped_class.__getattribute__(item)
if callable(attr):
@wraps(attr)
def wrap(*args, **kwargs):
@britonad
britonad / roles.py
Created November 20, 2017 15:46
Simple Flask MongoDB roles, permissions and decorator for views
import datetime
from functools import wraps
from werkzeug.security import (
generate_password_hash,
check_password_hash
)
from flask import abort
@britonad
britonad / cleanup
Last active December 20, 2018 12:08
A simple clean up helper of *.swp and *.pyc files.
#!/bin/bash
echo "### Cleaning *.pyc, *.swp and *.DS_Store files."
# Get a path from the first argument.
if [[ $* ]]; then
LOCATION=$*
else
LOCATION=$(pwd)
fi
@britonad
britonad / signal.py
Created June 14, 2018 14:56
An example of the creation of a full-fledged signal.
from blinker import Namespace
_signals = Namespace()
pre_save = _signals.signal('pre_save')
class Sender:
def __init__(self, name):
self.name = name
version: '2'
services:
backend:
build: .
command: >
gunicorn -b 0.0.0.0:5005
--worker-class eventlet
--access-logfile -
--reload
import remote_pdb
def main():
"""
A dummy function.
"""
remote_pdb.set_trace(host='0.0.0.0', port=4444)
return True
[bumpversion]
current_version = 3.0.0.a0
commit = True
tag = False
files = setup.py docs/conf.py wtforms/__init__.py
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(\.(?P<release>[a-z]+)(?P<n>\d+))?
serialize =
{major}.{minor}.{patch}.{release}{n}
{major}.{minor}.{patch}
@britonad
britonad / plus.cpp
Last active June 24, 2020 22:27
Pre-increment vs post-increment in CPP.
#include <iostream>
using namespace std;
void pre_increment() {
// Pre-increment.
// i is: 1
// j is: 1
// ++i
int i(0);