This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You can get the name of | |
# an object's class as a | |
# string: | |
>>> class MyClass: pass | |
>>> obj = MyClass() | |
>>> obj.__class__.__name__ | |
'MyClass' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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/ |
NewerOlder