Skip to content

Instantly share code, notes, and snippets.

@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!)
@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 / 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 / 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 / argparse_example.py
Created May 31, 2016 13:31
An example of how to use argparse module for Python
import argparse
parser = argparse.ArgumentParser()
# add a positional argument. THey are treated as strings by default
parser.add_argument("echo", help="echo the string you use here")
# tell it to treat the argument as an integer
parser.add_argument("square", help="display a square of a given number",
type=int
@Highstaker
Highstaker / array_operations.py
Last active June 11, 2016 10:12
Studies of numpy library
import numpy as np
a = np.array([20, 30, 40, 50])
b = np.arange(4)
# mathematical operationscan be done with arrays. Arrays should be of same size and dimensions
print(a+b) # [20 31 42 53]
print(a-b) # [20 29 38 47]
print(a*b) # [0 30 80 150]
print(a/b) # [inf 30 20 16.6666667]
# https://www.reddit.com/r/dailyprogrammer/comments/4m3ddb/20160601_challenge_269_intermediate_mirror/
import numpy as np
def decrypt(inp):
mirrors = np.array(list(map(list ,MIRRORS.split('\n'))))
TOP = [chr(i) for i in range(97,110)]
RIGHT = [chr(i) for i in range(110,123)]
LEFT = [chr(i) for i in range(65,78)]
BOTTOM = [chr(i) for i in range(78,91)]
result = ""
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
int ndigits[10] = {0};
memset(ndigits, 0, 10 * sizeof(int));//init with zeroes
int n_spaces = 0;
int n_others = 0;
@Highstaker
Highstaker / password_breaker.py
Last active June 27, 2016 11:22
A function that generates a set of variations of a string based on replacers. Good for coming up with passwords with popular replacers (1 for i, 0 for o, etc.)
INPUT = "terminator"
REPLACERS = {
"o" : {"0", "Q"},
"i" : {"1",},
"a" : {"@",},
"e" : {"3",},
}
# Add capitals
@Highstaker
Highstaker / telegram_bot_webhook_example.py
Created August 25, 2016 04:45
A simple bot working on webhooks.
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
def msg_process(bot, update):
bot.sendMessage(131711493,update.message.text + " hello")
updater = Updater("176032236:AAF6h2YjwtUfsLP5K5O5wD8xV2dg3VadL5k")
disp = updater.dispatcher
updater.start_webhook(listen='0.0.0.0',