Skip to content

Instantly share code, notes, and snippets.

View bmwant's full-sized avatar
🇺🇦
Hold strong Ukraine

Misha Behersky bmwant

🇺🇦
Hold strong Ukraine
View GitHub Profile
@bmwant
bmwant / shareme.py
Created April 3, 2018 08:39
Just share some code
from html.parser import HTMLParser
class MyBase(object):
def __init__(self):
print('I want this called')
@bmwant
bmwant / telegraph_article.py
Created February 15, 2018 13:28
Get article from Telegra.ph and transform resulting html
import re
import os
import argparse
from urllib import request
from urllib.parse import urljoin
from html.parser import HTMLParser
class ArticleParser(HTMLParser):
@bmwant
bmwant / logging_setup.py
Last active February 8, 2018 16:00
Logging setup
import logging
def setup_logging():
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
log.propagate = False
formatter = logging.Formatter('%(asctime)s :: line %(lineno)d, %(module)s [%(levelname)s] %(message)s')
formatter.datefmt = '%H:%M:%S %d/%m/%y'
handler = logging.StreamHandler()
file_handler = logging.FileHandler('file.log')
@bmwant
bmwant / config_overrides.py
Last active January 30, 2018 09:59
Override config values from environment
import os
KEY1 = 'VALUE1'
KEY2 = 'VALUE2'
# Override values from config_local.py if exists
try:
import config_local
for key, value in config_local.__dict__.items():
if key.isupper() and key in globals():
@bmwant
bmwant / logging_adapter.py
Created November 16, 2017 12:22
Logging adapter with extra info
import logging
class Runner(object):
def __init__(self):
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
adapter = CustomAdapter(logger, {'runner': self})
# adapter.debug('Some debug message')
@bmwant
bmwant / mongo_queries.py
Created August 15, 2017 07:03
Mongo queries
# Get generation time from ObjectId
>>> k['_id']
ObjectId('5991ba6cd241bf1001790c26')
>>> k['_id'].generation_time
datetime.datetime(2017, 8, 14, 14, 57, 48, tzinfo=<bson.tz_util.FixedOffset object at 0x7f1beb8d9550>)
>>> k['_id'].generation_time.strftime('%H:%M')
'14:57'
>>> k['_id'].generation_time.strftime('%H:%M:%S %d/%b/%Y')
'14:57:48 14/Aug/2017'
@bmwant
bmwant / os_path_usage.py
Last active July 6, 2017 21:09
Usage of common methods of os.path module
import os
import glob
# Get current directory
dir_path = os.path.dirname(os.path.realpath(__file__))
# Walk match files by a pattern within directory
glob.glob('your-directory/*')
# Recursively walk through all the files/directories within a folder
@bmwant
bmwant / stateMock.js
Last active June 8, 2017 13:29
Angular mock for the state
angular.module('stateMock', []);
angular.module('stateMock').service("$state", function($q) {
this.expectedTransitions = [];
this.transitionTo = function(stateName) {
if(this.expectedTransitions.length > 0) {
var expectedState = this.expectedTransitions.shift();
if(expectedState !== stateName) {
throw Error("Expected transition to state: " + expectedState + " but transitioned to " + stateName );
}
} else {
@bmwant
bmwant / render_template.py
Created March 8, 2017 23:19
Jinja2 render template, click, datetime format
import click
from datetime import datetime
from jinja2 import Environment, PackageLoader, StrictUndefined
@click.command()
@click.option('--version')
def do_it(version):
click.secho('yellow text', fg='yellow')
env = Environment(
@bmwant
bmwant / regex01.py
Created September 29, 2016 20:07
Regex #1
import term
exp = '(+|-|e)dd*'
class InvalidInput(ValueError):
pass