Skip to content

Instantly share code, notes, and snippets.

@Highstaker
Highstaker / traceback_printer.py
Last active May 23, 2016 17:07
A function that prints the full traceback
#!/usr/bin/python3 -u
# -*- coding: utf-8 -*-
import sys, traceback
def full_traceback():
"""
Returns a full traceback to an exception
:return:
"""
exc_type, exc_value, exc_traceback = sys.exc_info()
@Highstaker
Highstaker / simple_timer_decorator.py
Last active May 18, 2016 18:03
Simple decorator examples
import time
import functools
def timer(func):
#Decorator function takes the decorated function as parameter
@functools.wraps(func)
def wrapper(*args, **kwargs):
#the replacement function to be used
@Highstaker
Highstaker / secondsToText.py
Last active January 5, 2021 02:51
secondsToText with pluralization (days, hours, minutes, seconds) in several languages: English, German, Spanish and Russian.
def pluralizeRussian(number, nom_sing, gen_sing, gen_pl):
s_last_digit = str(number)[-1]
if int(str(number)[-2:]) in range(11,20):
#11-19
return gen_pl
elif s_last_digit == '1':
#1
return nom_sing
elif int(s_last_digit) in range(2,5):
@Highstaker
Highstaker / hole-punch-introductor.py
Created April 25, 2016 05:18
A simple hole-punching system with sender, receiver and introductor. Launch introductor first, then sender, then receiver. Presumably, may work with full-cone NAT, but not the other type.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from socket import AF_INET, SOCK_DGRAM
import socket
PORT = 9091
sock = socket.socket(AF_INET, SOCK_DGRAM)
# assign socket to host (empty for any) and port (as tuple, not as two params!)