@@ -35,7 +35,7 @@ import hashlib
3535def md5_digest(s):
3636 """Calculates an MD5 digest of a string."""
3737 m = hashlib.md5()
38- m.update("s" )
38+ m.update(s )
3939 return m.hexdigest()
4040----
4141
@@ -54,7 +54,7 @@ def install(package):
5454install('tornado')
5555----
5656
57- === Install numpy and scipy in virtualenv
57+ === Installing numpy and scipy in virtualenv
5858
5959For Ubuntu/Debian:
6060
@@ -64,6 +64,179 @@ sudo apt-get install -y gfortran libopenblas-dev liblapack-dev
6464pip install numpy scipy
6565----
6666
67+ === Installing PIL in virtualenv
68+
69+ Source: http://goo.gl/NOZtUu
70+
71+ [source,python,linenums]
72+ ----
73+ sudo aptitude install libjpeg-dev
74+
75+ sudo ln -s /usr/lib/x86_64-linux-gnu/libjpeg.so /usr/lib
76+ sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so /usr/lib
77+ sudo ln -s /usr/lib/x86_64-linux-gnu/libz.so /usr/lib
78+
79+ pip install -I PIL
80+ ----
81+
82+ === Using temporary file
83+
84+ [source,python,linenums]
85+ ----
86+ from tempfile import mkstemp
87+ from os import fdopen
88+
89+
90+ def write_temp_file(content):
91+ """
92+ Writes content to a temporary file and returns the file path.
93+ """
94+ fd, path = mkstemp()
95+
96+ with fdopen(fd, 'w') as out:
97+ out.write(content)
98+ out.write("\n")
99+
100+ return path
101+ ----
102+
103+ === Calling method or function by name
104+
105+ * Calling method:
106+ +
107+ [source,python,linenums]
108+ ----
109+ bar = getattr(foo, 'bar')
110+ result = method(arg_1, arg_2)
111+ ----
112+
113+ * Calling function:
114+ +
115+ [source,python,linenums]
116+ ----
117+ locals()["myfunction"]()
118+ # or
119+ globals()["myfunction"]()
120+ ----
121+ +
122+ `locals` returns a dictionary containing local symbol table. `globals`
123+ returns a dictionary with global symbol table.
124+
125+ === Trapping `Ctrl-C` but not SIGTERM
126+
127+ By using `KeyboardInterrupt` exception:
128+
129+ [source,python,linenums]
130+ ----
131+ import time, sys
132+
133+
134+ x = 1
135+ while True:
136+ try:
137+ print(x)
138+ time.sleep(.3)
139+ x += 1
140+ except KeyboardInterrupt:
141+ print "Bye"
142+ sys.exit()
143+ ----
144+
145+ === Dropping into IPython PDB
146+
147+ Install `ipdb`
148+
149+ [source,bash,linenums]
150+ ----
151+ pip install ipdb
152+ ----
153+
154+
155+ Then in your code:
156+
157+ [source,python,linenums]
158+ ----
159+
160+ import ipdb; ipdb.set_trace()
161+ ----
162+
163+ === Getting site-package path
164+
165+ [source,python,linenums]
166+ ----
167+ from distutils.sysconfig import get_python_lib
168+
169+
170+ print(get_python_lib())
171+ ----
172+
173+ === Generating SSH key pairs on file system
174+
175+ Requirement: Pycrypto.
176+
177+ [source,python,linenums]
178+ ----
179+ from Crypto.PublicKey import RSA
180+ from tempfile import mkstemp
181+ from os import fdopen, chmod
182+
183+ import stat
184+
185+
186+ def generate_RSA(bits=4096):
187+ """
188+ Generates an RSA keypair with an exponent of 65537.
189+ """
190+ new_key = RSA.generate(bits, e=65537)
191+ public_key = new_key.publickey().exportKey("OpenSSH")
192+ private_key = new_key.exportKey("PEM")
193+ return public_key, private_key
194+
195+
196+ def write_temp_file(content):
197+ """
198+ Writes content to a temporary file and returns the file path.
199+ """
200+ fd, path = mkstemp()
201+
202+ with fdopen(fd, 'w') as out:
203+ out.write(content)
204+ out.write("\n")
205+
206+ return path
207+
208+
209+ def gen_keys_write_files():
210+ """
211+ Generates an RSA key pair and writes them into 2 temporary files: one for
212+ public key and the other one for private key.
213+ """
214+ pub, priv = generate_RSA()
215+ pub_path, priv_path = write_temp_file(pub), write_temp_file(priv)
216+
217+ # 644 for public key
218+ chmod(pub_path, stat.S_IRUSR \
219+ | stat.S_IWUSR \
220+ | stat.S_IRGRP \
221+ | stat.S_IROTH)
222+
223+ # 600 for private key
224+ chmod(priv_path, stat.S_IRUSR | stat.S_IWUSR)
225+
226+ return pub_path, priv_path
227+ ----
228+
229+ === Generating cookie secret for web framework
230+
231+ [source,python,linenums]
232+ ----
233+ import base64
234+ import uuid
235+
236+
237+ print(base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes))
238+ ----
239+
67240== FAQs
68241
69242=== Why global interpreter lock (GIL) in CPython?
@@ -113,3 +286,11 @@ the GIL. Therefore it is only in multithreaded programs that spend a lot of
113286time inside the GIL, interpreting CPython bytecode, that the GIL becomes a
114287bottleneck.
115288____
289+
290+ === IPython startup script directory
291+
292+ Reference: http://ipython.org/ipython-doc/stable/development/config.html
293+
294+ `$HOME/.ipython/profile_default/startup/`
295+
296+ Just put a Python file like `00-run-something.py`.
0 commit comments