-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpython.txt
More file actions
151 lines (76 loc) · 8.96 KB
/
python.txt
File metadata and controls
151 lines (76 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Type reference a symbol before it's defined (like in a class definition)?;;Wrap the symbol in a string, it'll be eval'd later.<br>"Forward reference";
Display the differences between an environment and Django's default settings;;;manage.py diffsettings
Roll back a migration?;;;server/manage.py migrate seminar 0073
What are Django's five log levels?;;DEBUG, INFO, WARN(ING), ERROR, CRITICAL;
What are the four parts of Django's logging system?;;Loggers, Handlers, Filters, Formatters;
How do you create a timezone-aware datetime?;;;from django.utils import timezone<br>timezone.now()
Inspect Python's import search path?;;;import sys<br/>sys.path
Step one line in ipdb;;;s(tep)
IPython: list all variables + info;;;%whos
IPython: display environment vars;;;%env
What is the difference between a Python module and a package?;;Module: python file with functions (file) <br/>Package: directory with __init__.py, contians packages & modules (dir);
Read all of the functions implemented by module `url`?;;;dir(url)
How many layers of nesting does a decorator that takes arguments require?;;3;def outer_layer(takes_arguments):<br>....def second_layer(old_function):<br>........def inner_layer(*args, **kwds):<br>............new_function_work<br>........return inner_layer<br>....return second_layer
/ vs //?;;Float division (py3 only) vs integer floor division.;
np: Create a new array of 2*2 integers?;;;np.zeros([2,2], int)
np: Create a 3x3 matrix with ones on the diagonal and zeros elsewhere.;;;np.identity(3)
np: Create a new array of 2*5 uints, filled with 6.;;;np.full((2, 5), 6, dtype=np.uint)
np: What are the 4 _like methods, and what do they do?;;Copy shape and datatype. zeros, ones, full, empty;
np: create a 3x5 matrix of random data;;;np.random.random((3, 5))
np: Create an array of 2, 4, 6, 8, ..., 100.;;;np.arange(2, 101, 2)
np: Create a 1-D array of 50 linearly (or logarithmically) spaced elements between 3. and 10., inclusive.;;;np.linspace(3., 10, 50)<br>np.logspace
np: zero out the bottom triangle of an array?;;;np.tril(X, -1) # -1 chooses the diagonal
np: reshape the second dimension to be 75, let the first dimension be inferred?;;;x.reshape((-1, 150))
np: flatten a 2D array by iterating through in column order?; [[1, 2, 3], [4, 5, 6]] => [1 4 2 5 3 6];Use "Fortran" order. Default is "C", for C-style order (row-major). Fortran used column-major ordering.;np.asarray([np.arange(1, 4), np.arange(4, 7)]).flatten(order='F')
np: get the 10th element in a matrix?;;;x.flat[10]
np: get transpose of matrix?;;;x.T
np: expand dimensionality of matrix? Make each element of a 3, 4 array sit inside its own array.;;Shape will go from (3, 4) to (3, 4, 1);np.expand_dims(axis=2)
np: remove single-dimensional axes?;;;ndarray.squeeze()
np: Let x be an array <br/>[[ 1 2 3]<br/>[ 4 5 6].<br/><br/>and y be an array <br/>[[ 7 8 9]<br/>[10 11 12]].<br/>Concatenate x and y so that a new array looks like <br/>[[1, 2, 3, 7, 8, 9], <br/>[4,5, 6, 10, 11, 12]].;;;np.concatenate((a1, a2), axis=1)
np: Let x be an array [1, 2, 3, ..., 9]. Split x into 3 arrays, each of which has 4, 2, and 3 elements in the original order.;;;np.split(x, [4, 6, 9])
np: separate or join arrays across various dimensions?;;vsplit (row-wise), hsplit (column-wise), dsplit (depth-wise)<br>vstack, hstack, dstack<br>split, concatenate, stack;
np: concatenate vs stack?;;Concatenate adds matrices together along existing axes, stack inserts new ones.;
np: Compare two arrays elementwise?;;;np.allclose(a, b)
np: Let x be an array [0, 1, 2]. Convert it to <br/>[[0, 1, 2, 0, 1, 2],<br/>[0, 1, 2, 0, 1, 2]].;;A, reps=times to repeat array along each axis;np.tile(x, [2, 2])
np: Let x be an array [0, 1, 2]. Convert it to <br/>[0, 0, 1.1, 1, 2, 2].;;;np.repeat([0, 1, 2], 2)
np: Let x be an array [2, 2, 1, 5, 4, 5, 1, 2, 3]. Get two arrays of unique elements and their counts.;;return_index=True: indices of input that give first unique val<br>return_inverse=False: return indices that you can use to reconstruct original<br>return_counts=False: return counts<br>axis=None: if None, flatten first, otherwise only get unique per axis.;np.unique(x, return_counts=True)
np: Let x be an array <br/>[[ 1 2]<br/>[ 3 4].<br/>Rotate x 90 degrees counter-clockwise.;;m, # times to rotate clockwise, rotate through plane defined by these axes=(0, 1);np.rot90(x, -1)
np: Let x be an array <br/>[[ 1 2 3 4]<br/>[ 5 6 7 8].<br/>Shift elements one step to right along the second axis.;;a, shift, axis;np.roll(x, 0, axis=1)
np: Count the lowest index of "l" in x, element-wise.;;All sorts of string ops live in np.char.;np.char.find(x, 'l')
np: keyword search on docstrings?;;;np.lookfor(keyword)
np: customize how NP objects are displayed?;;precision=8: digits for floating points<br>threshold=1000: points at which np summarizes<br>suppress=False: show small floats?;np.set_printoptions
Show binary representation of decimal number?;;digit, width;np.binary_repr(12)<br>np.base_rep(12, 2)
np: what happens when you matmul a vector and 2x2 matrix? Isn't 2x1 * 2x2 multiplication impossible?;@;It promotes the vector to a matrix by PREPENDING a 1 to the dimensions;
enumerate through an array and its indices?;;;for index, value in np.ndenumerate(x):
When you index by an array or a matrix, what happens?;;Each element of matrix is interpreted as a coordinate (for that dimension) and mapped to corresponding value.;
What are the three parts of an index for a numpy matrix?;;slice start, slice end, stride;
What happens when you index one dimension with tensors and not the others?;;The others are broadcast to match dimensions, if possible, then mapped.;
Indexing: select in full from unspecified dimensions?;;Use the Ellipsis object literal;y[..., :2, :2]
How are tuples and indexes treated differently when slicing?;;Arrays are mapped to entire dimensions of values, tuples are coordinates;y[(1, 1, 2)] => scalar, y[[1, 1, 2]] => the matrix at depth 1 twice, then the matrix at depth 2
Render a matrix as an image?;;;import matplotlib.pyplot as plt<br>plt.imshow(X, cmap='bone')<br>plt.show()
imshow: what dimensions does it accept?;;MxN: color mappable<br>MxNx3: RGB<br>MxNx4: RGBA;
What types of colormaps are there?;;Sequential: lightness increases<br>Diverging: neutral at 0<br>Qualitative: pretty<br>Miscellaneous: rainbow, terrain, etc;
get a copy of a slice?;;;B = A.copy()
Select matrix values above 10?;;;B = A > 10
matplotlib.pyplot.scatter: args?;;x, y: input data<br>s: size or sequence of sizes<br>c: color or sequence of colors (RGB);
compute e^x of a tensor?;;;np.exp(x)
Make a new virtualenv?;;;pyenv virtualenv 3.6.2 new-env
List virtualenvs?;;;pyenv virtualenvs
Automatically switch to a virtualenv?;;;pyenv activate new-env<br>cat 'new-env' >> .python-version
Uninstall a virtualenv?;;;pyenv uninstall new-env
Difference between list and collections.deque (double-ended queue)?;;lists are arrays (O(1) for random access and approx O(1) for appending, O(N) for prepending)<br>deques are doubly-linked lists (O(1) push and pop from both ends).<br>deque also has maxlen, which shifts items off the front when a push would exceed it;
What's the problem with mutable default arguments?;def uh_oh(arg={}):<br> pass;They're instantiated once, not every time you invoke the function;
Constrain memory by Python;;;resource.setrlimit()
Why is Django delete() taking too much mem?;;It does SELECT, loads in Python, then sends signals around while deleting. Use _raw_delete to do it in SQL.;
Recover OOM EB instance?;;Make sure no connection to DB, reboot in EC2, deploy.;
Physical location of postgres record?;;;ctid
psql pretty print;;;\x auto
Postgres activity table?;;;pg_stat_activity
What are common reasons for a segfaults in Python?;;Binary module & library version mismatch<br>C extension (or Cython) is broke;
How does IPython's %debug magic work?;;IPython keeps a process hanging around for the last exception, and %debug can connect to it later.;
How are Python debuggers implemented?;;sys.settrace is used to set the system's trace function for the current thread, this function is then invoked with every line and exception. A debugger process runs in the background with breakpoints in mind, when those lines are hit, it opens a REPL.;
What is PyDevd?;;A high-performance debugger that handles settrace calls and decides what to do. Pydev is fast because it disables tracing entirely for contexts with no breakpoints!;
Why is Pandas chained indexing bad?;df[df.C <= 0].ix[:,'B':'E'] = 2;Because the get indexer will often (but not always!) return a view, and the set indexer will operate in place.;df[df.C <= 0, 'B':'E'] = 2
When does Pandas copy?;;(subsequent rules override).<br>1. Always.<br>2. Except if inplace=True.<br>3. Get/set indexers have their own rules;
When do Pandas indexers return copies, and when do they return views?;;Set indexers (.loc[], .iloc[], .at[]): views (in-place).<br>Get indexers on single-dtyped frames: views (mostly).<br>Get indexers on multi-dtyped frames: copies.;
GROUP BY multiple columns in Django?;;House.objects.values('price', 'rooms').annotate(Count('rooms'));