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 / 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 / custom_json_encoder.py
Last active April 10, 2016 12:13
Custom JSON Encoder for own type
import json
from json import JSONEncoder
class Custom(object):
def serialize(self):
return 42
class CustomEncoder(JSONEncoder):
@bmwant
bmwant / letters_distance.py
Created January 25, 2016 16:54
Find distance between strings based on non-equal characters count
from collections import Counter
def find_letter_distance(string, other):
string_counter = Counter(string)
other_counter = Counter(other)
unique_symbols = set(string + other)
distance = 0
for char in unique_symbols:
@bmwant
bmwant / try_boto.py
Created December 14, 2015 09:37
AWS boto3 some calls
# Setup docs: http://boto3.readthedocs.org/en/latest/guide/quickstart.html
import boto3
# List all regions
client = boto3.client('ec2')
regions = client.describe_regions()['Regions']
for region in regions:
print('Name: %s. Endpoint: %s' % (region['RegionName'], region['Endpoint']))
@bmwant
bmwant / distance.py
Created December 8, 2015 13:19
Find least distance between two words in text
import math
from collections import defaultdict
words = ('the', 'quisck', 'brown', 'fox', 'quick')
class WordDistanceFinder(object):
def __init__(self, words):
@bmwant
bmwant / timetable.py
Created December 8, 2015 13:18
Rooms and timetable problem
timetable = (
('10:30', '11:30'),
('10:00', '11:00'),
('11:00', '12:00'),
('09:33', '12:00'),
('17:00', '19:00'),
('17:00', '19:00'),
('17:00', '19:00'),
('17:00', '19:00'),
@bmwant
bmwant / os_info.py
Last active November 15, 2015 12:28
Get some OS info with psutils
import os
import time
import psutil
import humanfriendly
now = time.time()
pid = os.getgid()
ppid = os.getppid()
@bmwant
bmwant / reverse_linked_list.py
Created November 11, 2015 10:36
Reverse linked list both iteratively and recursively
class LinkedList(object):
def __init__(self):
self.head = None
self.tail = None
def add(self, node):
if self.tail is not None:
self.tail.next = node
self.tail = node
else:
@bmwant
bmwant / naive_deployer.py
Created September 29, 2015 12:53
Fabric copyfiles
# -*- coding: utf-8 -*-
import os
from fabric.api import env, run, put, cd, local
env.user = ''
env.password = ''
env.hosts = ['127.0.0.1']
env.shell = "bash -c"
@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')