Skip to content

Instantly share code, notes, and snippets.

View kgantsov's full-sized avatar
🇺🇦

Kostiantyn Hantsov kgantsov

🇺🇦
  • iconik.io
  • Stockholm, Sweden
View GitHub Profile
@kgantsov
kgantsov / gist:6269126d012254387aa0
Last active August 29, 2015 14:05
Sends morse code messages by Raspberry Pi
#!/usr/bin/python
# coding: utf-8
import argparse
import RPi.GPIO as GPIO
import time
import sys
MORSE_CODE = {
u'A': '.-', u'a': '.-', u'А': '.-', u'а': '.-',
@kgantsov
kgantsov / gist:cbdfabf992cf4ee12841
Created August 24, 2014 12:56
Gets temperature from USB Temper, show it by LED lighting of BerryClip on Raspberry Pi and log it into a file.
#!/usr/bin/python
# coding: utf-8
import RPi.GPIO as GPIO
import datetime
import logging
import usb
from temperusb import TemperDevice
@kgantsov
kgantsov / gist:2926510a3848283e8e90
Created August 24, 2014 13:05
Gets log file of temperatures and shows it on a plot (using matplotlib)
#!/usr/bin/python
# coding: utf-8
import csv
from datetime import datetime
import matplotlib.pyplot as plt
from matplotlib import rcParams
def get_data(file_name):
@kgantsov
kgantsov / tree
Created March 24, 2016 10:32 — forked from ryanbaldwin/tree
Outputs the directory tree in your terminal, all pretty like.
find . -print | sed -e 's;[^/]*/;|____;g;s;____|; |;g'
@kgantsov
kgantsov / beautiful_idiomatic_python.md
Created April 2, 2016 13:18 — forked from JeffPaine/beautiful_idiomatic_python.md
Transforming Code into Beautiful, Idiomatic Python: notes from Raymond Hettinger's talk at pycon US 2013. The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Transforming Code into Beautiful, Idiomatic Python

Notes from Raymond Hettinger's talk at pycon US 2013 video, slides.

The code examples and direct quotes are all from Raymond's talk. I've reproduced them here for my own edification and the hopes that others will find them as handy as I have!

Looping over a range of numbers

for i in [0, 1, 2, 3, 4, 5]:
@kgantsov
kgantsov / genrate_random_string.py
Created August 29, 2016 10:55
Generate random string in Python
import string
from random import choice
def generate_random_string(n, lowercase=True, uppercase=True, digits=True, punctuation=True):
assert lowercase or uppercase or digits or punctuation, 'One of boolean params must be True'
seq = ''.join([
string.ascii_lowercase if lowercase else '',
string.ascii_uppercase if uppercase else '',
@kgantsov
kgantsov / id3_fix.py
Created March 28, 2017 17:57
Simple script that I wrote to fix encoding of id3 tags in my mp3 collection
import os
import click
from mutagen.easyid3 import EasyID3
from mutagen.id3._util import ID3NoHeaderError
@click.group()
def cli():
pass
@kgantsov
kgantsov / large_file_server.py
Created April 14, 2017 11:18
Example of serving large files using aiohttp server without reading entire file into a memory
import asyncio
import os
from aiohttp import web
from aiohttp import streamer
@streamer
async def file_sender(writer, file_path=None):
"""
@kgantsov
kgantsov / tcpdump_host.sh
Created July 13, 2017 13:42
Simple bash script to do tcpdump that will listen specified host
#!/bin/bash
if [ $# -eq 1 ]
then
tcpdump -nnvXSs 0 host $1
else
echo "No arguments supplied"
fi
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = sock.connect_ex(('127.0.0.1', 8000))
sock.close()
if result == 0:
print("Port: 8000 is already in use")
else:
print("Port is ready")