-
Notifications
You must be signed in to change notification settings - Fork 0
/
python_notes.txt
94 lines (67 loc) · 2.75 KB
/
python_notes.txt
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
# Keyword help:
help(keyword)
# Short version (in IPython):
?keyword
# Find methods available in a given object:
# http://stackoverflow.com/questions/34439/finding-what-methods-an-object-has
dir(object)
# List attributes of an object:
# https://stackoverflow.com/questions/2675028/list-attributes-of-an-object
>>> a.__dict__
{'multi': 4, 'str': '2'}
>>> a.__dict__.keys()
dict_keys(['multi', 'str'])
# This is equivalent to a.__dict__:
vars(a)
# Also try this:
pprint.pprint(a.__dict__)
# Common gotchas:
http://docs.python-guide.org/en/latest/writing/gotchas/
# Append to module search path:
# http://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path
sys.path.append('/path/')
# Reload a module that has changed:
import importlib
importlib.reload(some_module)
# Compare values:
a == b
# Compare identities (location in memory):
a is b
# Usually, identity (is) implies equality (==). NaN is a counterexample.
# The converse is not true: Two distinct objects can have the same value.
# http://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs
# String literals are interned in Python, meaning only one copy of each
# distinct string value is stored. This requires that strings must be
# immutable. Hence, if str1 = 'test' and str2 = 'test', str1 is str2 is True
# (as is str1 == str2).
# http://stackoverflow.com/questions/1504717/why-does-comparing-strings-in-python-using-either-or-is-sometimes-produce
# If you've accidentally bound a necessary function name (such as 'str') to a value:
# http://stackoverflow.com/questions/24637266/typeerror-list-object-is-not-callable-when-trying-to-convert-an-object-to-a-stri
# http://stackoverflow.com/questions/8237647/clear-variable-in-python
del str
# Expand a tuple/list into function input parameters:
# http://stackoverflow.com/questions/1993727/expanding-tuples-into-arguments
function(*tuple)
# If IPython kernel crashes when re-running an application that initializes PyQt:
# http://stackoverflow.com/questions/24041259/python-kernel-crashes-after-closing-an-pyqt4-gui-application
app = 0
app = QtGui.QApplication([])
# Get Python path (from which modules can be imported):
print(sys.path)
# Install virtualenv:
# https://gist.github.com/Geoyi/d9fab4f609e9f75941946be45000632b
sudo apt-get install python3-pip
pip3 install virtualenv
# Set up a virtualenv:
virtualenv venv
source venv/bin/activate
# Using alternate Python interpreter:
virtualenv -p /usr/bin/python2.7 venv
# To deactivate:
deactivate
# Alternate command for python3:
python3 -m venv myenv
# Set breakpoint for debugging in multithreaded environment (such as with flask):
# https://stackoverflow.com/questions/7617066/pdb-wont-stop-on-breakpoint
# Add this to the code at the desired breakpoint:
import pdb; pdb.set_trace()