Skip to content

Instantly share code, notes, and snippets.

# zipping and unzipping of two lists
list1 = ['a','b','c']
list2 = [20,100,2000]
# zip the two lists together
pairs = zip(list1,list2)
print pairs
# [('a', 20), ('b', 100), ('c', 2000)]
# unzipping the list
@technofriends
technofriends / faulthandler.py
Created September 9, 2017 12:12
Python3.3+ has a standard lib module for displaying tracebacks even when Python "dies".
# Python 3.3+ has a std
# lib module for displaying
# tracebacks even when Python
# "dies", e.g with a segfault:
import faulthandler
faulthandler.enable()
# Can also be enabled with
# "python -X faulthandler"
@technofriends
technofriends / name-of-class.txt
Created August 20, 2017 12:36
Get name of object's class as a string at runtime in Python.
# You can get the name of
# an object's class as a
# string:
>>> class MyClass: pass
>>> obj = MyClass()
>>> obj.__class__.__name__
'MyClass'
kp = 1.2
ki = 0.9
kd = 0.3
set_point = 50
prev_error = 0
cumulative_moving_average = 0
iteration = 0
def make_iteration(measured):
@technofriends
technofriends / multiple_flags.py
Created July 17, 2017 05:52
testing multiple python flags at once in Python
# Different ways to test multiple
# flags at once in Python
x, y, z = 0, 1, 0
if x == 1 or y == 1 or z == 1:
print('passed')
if 1 in (x, y, z):
print('passed')
@technofriends
technofriends / python-http-server.txt
Last active July 17, 2017 05:50
Python's built in HTTP Server
# Python has a HTTP server built into the
# standard library. This is super handy for
# previewing websites.
# Python 3.x
$ python3 -m http.server
# Python 2.x
$ python -m SimpleHTTPServer 8000
@technofriends
technofriends / is_non_zero_file.py
Created July 17, 2017 05:40
check if a file is of zero size or not.
def is_non_zero_file(base_filename):
wk_dir = os.path.dirname(os.path.realpath('__file__'))
fpath = os.path.join(wk_dir, base_filename)
print fpath
return os.path.isfile(fpath) and os.path.getsize(fpath) > 0
import math
PI = math.pi
def compute_bearing(p, q):
x1, y1 = p
x2, y2 = q
dx = x2 - x1
dy = y2 - y1
return truncate_angle(math.atan2(dy, dx))
import math
def compute_distance(p, q):
x1, y1 = p
x2, y2 = q
dx = x2 - x1
dy = y2 - y1
return math.sqrt(dx**2 + dy**2)
@technofriends
technofriends / ab-usage
Last active May 5, 2017 08:35
ab usage gist
# ab -n <number-of-requests> -h "User-Agent: <user-agent>" <url>
# send 100 requests to example.com and change the User Agent to foobar/1.0
ab -n 100 -H "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
" http://example.com/somepath/somefile.xyz
# send 10 concurrent requests to example.com at the specific IP
ab -n 10 -c 10 -H "Host: example.com" http://192.168.1.1/