Skip to content

Commit 91c9f5b

Browse files
Update standard library lesson (#731)
1 parent 424c4f2 commit 91c9f5b

File tree

1 file changed

+95
-132
lines changed

1 file changed

+95
-132
lines changed

intro/language/standard_library.rst

Lines changed: 95 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,59 @@ Current directory:
2020

2121
.. ipython::
2222

23-
In [17]: os.getcwd()
24-
Out[17]: '/Users/cburns/src/scipy2009/scipy_2009_tutorial/source'
23+
In [1]: import os
24+
25+
In [2]: os.getcwd()
26+
Out[2]: '/home/jarrod/src/scientific-python-lectures/intro'
2527

2628
List a directory:
2729

2830
.. ipython::
2931

30-
In [31]: os.listdir(os.curdir)
31-
Out[31]:
32-
['.index.rst.swo',
33-
'.python_language.rst.swp',
34-
'.view_array.py.swp',
35-
'_static',
36-
'_templates',
37-
'basic_types.rst',
38-
'conf.py',
39-
'control_flow.rst',
40-
'debugging.rst',
41-
...
32+
In [3]: os.listdir(os.curdir)
33+
Out[3]: ['intro.rst', 'scipy', 'language', 'matplotlib', 'index.rst', 'numpy', 'help']
4234

4335
Make a directory:
4436

4537
.. ipython::
4638

47-
In [32]: os.mkdir('junkdir')
39+
In [4]: os.mkdir('junkdir')
4840

49-
In [33]: 'junkdir' in os.listdir(os.curdir)
50-
Out[33]: True
41+
In [5]: 'junkdir' in os.listdir(os.curdir)
42+
Out[5]: True
5143

5244
Rename the directory:
5345

5446
.. ipython::
5547

56-
In [36]: os.rename('junkdir', 'foodir')
48+
In [6]: os.rename('junkdir', 'foodir')
5749

58-
In [37]: 'junkdir' in os.listdir(os.curdir)
59-
Out[37]: False
50+
In [7]: 'junkdir' in os.listdir(os.curdir)
51+
Out[7]: False
6052

61-
In [38]: 'foodir' in os.listdir(os.curdir)
62-
Out[38]: True
53+
In [8]: 'foodir' in os.listdir(os.curdir)
54+
Out[8]: True
6355

64-
In [41]: os.rmdir('foodir')
56+
In [9]: os.rmdir('foodir')
6557

66-
In [42]: 'foodir' in os.listdir(os.curdir)
67-
Out[42]: False
58+
In [10]: 'foodir' in os.listdir(os.curdir)
59+
Out[10]: False
6860

6961
Delete a file:
7062

7163
.. ipython::
7264

73-
In [44]: fp = open('junk.txt', 'w')
65+
In [11]: fp = open('junk.txt', 'w')
7466

75-
In [45]: fp.close()
67+
In [12]: fp.close()
7668

77-
In [46]: 'junk.txt' in os.listdir(os.curdir)
78-
Out[46]: True
69+
In [13]: 'junk.txt' in os.listdir(os.curdir)
70+
Out[13]: True
7971

80-
In [47]: os.remove('junk.txt')
72+
In [14]: os.remove('junk.txt')
8173

82-
In [48]: 'junk.txt' in os.listdir(os.curdir)
83-
Out[48]: False
74+
In [15]: 'junk.txt' in os.listdir(os.curdir)
75+
Out[15]: False
8476

8577
``os.path``: path manipulations
8678
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -89,52 +81,50 @@ Delete a file:
8981

9082
.. ipython::
9183

92-
In [70]: fp = open('junk.txt', 'w')
84+
In [16]: fp = open('junk.txt', 'w')
9385

94-
In [71]: fp.close()
86+
In [17]: fp.close()
9587

96-
In [72]: a = os.path.abspath('junk.txt')
88+
In [18]: a = os.path.abspath('junk.txt')
9789

98-
In [73]: a
99-
Out[73]: '/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/junk.txt'
90+
In [19]: a
91+
Out[19]: '/home/jarrod/src/scientific-python-lectures/intro/junk.txt'
10092

101-
In [74]: os.path.split(a)
102-
Out[74]: ('/Users/cburns/src/scipy2009/scipy_2009_tutorial/source',
103-
'junk.txt')
93+
In [20]: os.path.split(a)
94+
Out[20]: ('/home/jarrod/src/scientific-python-lectures/intro', 'junk.txt')
10495

105-
In [78]: os.path.dirname(a)
106-
Out[78]: '/Users/cburns/src/scipy2009/scipy_2009_tutorial/source'
96+
In [21]: os.path.dirname(a)
97+
Out[21]: '/home/jarrod/src/scientific-python-lectures/intro'
10798

108-
In [79]: os.path.basename(a)
109-
Out[79]: 'junk.txt'
99+
In [22]: os.path.basename(a)
100+
Out[22]: 'junk.txt'
110101

111-
In [80]: os.path.splitext(os.path.basename(a))
112-
Out[80]: ('junk', '.txt')
102+
In [23]: os.path.splitext(os.path.basename(a))
103+
Out[23]: ('junk', '.txt')
113104

114-
In [84]: os.path.exists('junk.txt')
115-
Out[84]: True
105+
In [24]: os.path.exists('junk.txt')
106+
Out[24]: True
116107

117-
In [86]: os.path.isfile('junk.txt')
118-
Out[86]: True
108+
In [25]: os.path.isfile('junk.txt')
109+
Out[25]: True
119110

120-
In [87]: os.path.isdir('junk.txt')
121-
Out[87]: False
111+
In [26]: os.path.isdir('junk.txt')
112+
Out[26]: False
122113

123-
In [88]: os.path.expanduser('~/local')
124-
Out[88]: '/Users/cburns/local'
114+
In [27]: os.path.expanduser('~/local')
115+
Out[27]: '/home/jarrod/local'
125116

126-
In [92]: os.path.join(os.path.expanduser('~'), 'local', 'bin')
127-
Out[92]: '/Users/cburns/local/bin'
117+
In [28]: os.path.join(os.path.expanduser('~'), 'local', 'bin')
118+
Out[28]: '/home/jarrod/local/bin'
128119

129120
Running an external command
130121
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131122

132123
.. ipython::
133124

134-
In [8]: os.system('ls')
135-
basic_types.rst demo.py functions.rst python_language.rst standard_library.rst
136-
control_flow.rst exceptions.rst io.rst python-logo.png
137-
demo2.py first_steps.rst oop.rst reusing_code.rst
125+
In [29]: os.system('ls')
126+
help index.rst intro.rst junk.txt language matplotlib numpy scipy
127+
Out[29]: 0
138128

139129
.. note:: Alternative to ``os.system``
140130

@@ -145,20 +135,17 @@ Running an external command
145135
.. ipython::
146136
:verbatim:
147137

148-
In [20]: import sh
149-
In [20]: com = sh.ls()
138+
In [30]: import sh
139+
In [31]: com = sh.ls()
150140

151-
In [21]: print(com)
141+
In [32]: print(com)
152142
basic_types.rst exceptions.rst oop.rst standard_library.rst
153143
control_flow.rst first_steps.rst python_language.rst
154144
demo2.py functions.rst python-logo.png
155145
demo.py io.rst reusing_code.rst
156146

157-
In [22]: print(com.exit_code)
158-
0
159-
In [23]: type(com)
160-
Out[23]: sh.RunningCommand
161-
147+
In [33]: type(com)
148+
Out[33]: str
162149

163150
Walking a directory
164151
~~~~~~~~~~~~~~~~~~~~
@@ -172,11 +159,11 @@ Walking a directory
172159
....: print(os.path.abspath(fp))
173160
....:
174161
....:
175-
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/.index.rst.swo
176-
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/.view_array.py.swp
177-
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/basic_types.rst
178-
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/conf.py
179-
/Users/cburns/src/scipy2009/scipy_2009_tutorial/source/control_flow.rst
162+
/home/jarrod/src/scientific-python-lectures/intro/language/basic_types.rst
163+
/home/jarrod/src/scientific-python-lectures/intro/language/control_flow.rst
164+
/home/jarrod/src/scientific-python-lectures/intro/language/python_language.rst
165+
/home/jarrod/src/scientific-python-lectures/intro/language/reusing_code.rst
166+
/home/jarrod/src/scientific-python-lectures/intro/language/standard_library.rst
180167
...
181168

182169
Environment variables:
@@ -185,35 +172,12 @@ Environment variables:
185172
.. ipython::
186173
:verbatim:
187174

188-
In [9]: import os
189-
190-
In [11]: os.environ.keys()
191-
Out[11]:
192-
['_',
193-
'FSLDIR',
194-
'TERM_PROGRAM_VERSION',
195-
'FSLREMOTECALL',
196-
'USER',
197-
'HOME',
198-
'PATH',
199-
'PS1',
200-
'SHELL',
201-
'EDITOR',
202-
'WORKON_HOME',
203-
'PYTHONPATH',
204-
...
205-
206-
In [12]: os.environ['PYTHONPATH']
207-
Out[12]: '.:/Users/cburns/src/utils:/Users/cburns/src/nitools:
208-
/Users/cburns/local/lib/python2.5/site-packages/:
209-
/usr/local/lib/python2.5/site-packages/:
210-
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5'
211-
212-
In [16]: os.getenv('PYTHONPATH')
213-
Out[16]: '.:/Users/cburns/src/utils:/Users/cburns/src/nitools:
214-
/Users/cburns/local/lib/python2.5/site-packages/:
215-
/usr/local/lib/python2.5/site-packages/:
216-
/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5'
175+
In [32]: os.environ.keys()
176+
Out[32]: KeysView(environ({'SHELL': '/bin/bash', 'COLORTERM': 'truecolor', ...}))
177+
178+
179+
In [34]: os.environ['SHELL']
180+
Out[34]: '/bin/bash'
217181

218182

219183
``shutil``: high-level file operations
@@ -234,12 +198,10 @@ Find all files ending in ``.txt``:
234198

235199
.. ipython::
236200

237-
In [18]: import glob
238-
239-
In [19]: glob.glob('*.txt')
240-
Out[19]: ['holy_grail.txt', 'junk.txt', 'newfile.txt']
241-
201+
In [36]: import glob
242202

203+
In [37]: glob.glob('*.txt')
204+
Out[37]: ['junk.txt']
243205
244206
``sys`` module: system-specific information
245207
--------------------------------------------
@@ -248,41 +210,41 @@ System-specific information related to the Python interpreter.
248210

249211
* Which version of python are you running and where is it installed:
250212

251-
.. ipython::
213+
.. ipython::
252214

253-
In [117]: sys.platform
254-
Out[117]: 'darwin'
255215

256-
In [118]: sys.version
257-
Out[118]: '2.5.2 (r252:60911, Feb 22 2008, 07:57:53) \n
258-
[GCC 4.0.1 (Apple Computer, Inc. build 5363)]'
216+
In [39]: import sys
259217

260-
In [119]: sys.prefix
261-
Out[119]: '/Library/Frameworks/Python.framework/Versions/2.5'
218+
In [40]: sys.platform
219+
Out[40]: 'linux'
262220

263-
* List of command line arguments passed to a Python script:
221+
In [41]: sys.version
222+
Out[41]: '3.11.8 (main, Feb 28 2024, 00:00:00) [GCC 13.2.1 20231011 (Red Hat 13.2.1-4)]'
264223

265-
.. ipython::
224+
In [42]: sys.prefix
225+
Out[42]: '/home/jarrod/.venv/nx'
266226

267-
In [100]: sys.argv
268-
Out[100]: ['/Users/cburns/local/bin/ipython']
227+
* List of command line arguments passed to a Python script:
228+
229+
.. ipython::
269230

231+
In [43]: sys.argv
232+
Out[43]: ['/home/jarrod/.venv/nx/bin/ipython']
270233

271234
``sys.path`` is a list of strings that specifies the search path for
272235
modules. Initialized from PYTHONPATH:
273236

274237
.. ipython::
275238

276-
In [121]: sys.path
277-
Out[121]:
278-
['',
279-
'/Users/cburns/local/bin',
280-
'/Users/cburns/local/lib/python2.5/site-packages/grin-1.1-py2.5.egg',
281-
'/Users/cburns/local/lib/python2.5/site-packages/argparse-0.8.0-py2.5.egg',
282-
'/Users/cburns/local/lib/python2.5/site-packages/urwid-0.9.7.1-py2.5.egg',
283-
'/Users/cburns/local/lib/python2.5/site-packages/yolk-0.4.1-py2.5.egg',
284-
'/Users/cburns/local/lib/python2.5/site-packages/virtualenv-1.2-py2.5.egg',
285-
...
239+
In [44]: sys.path
240+
Out[44]:
241+
['/home/jarrod/.venv/nx/bin',
242+
'/usr/lib64/python311.zip',
243+
'/usr/lib64/python3.11',
244+
'/usr/lib64/python3.11/lib-dynload',
245+
'',
246+
'/home/jarrod/.venv/nx/lib64/python3.11/site-packages',
247+
'/home/jarrod/.venv/nx/lib/python3.11/site-packages']
286248

287249
``pickle``: easy persistence
288250
-------------------------------
@@ -291,9 +253,9 @@ Useful to store arbitrary objects to a file. Not safe or fast!
291253

292254
.. ipython::
293255

294-
In [1]: import pickle
256+
In [45]: import pickle
295257

296-
In [2]: l = [1, None, 'Stan']
258+
In [46]: l = [1, None, 'Stan']
297259

298260
In [3]: with open('test.pkl', 'wb') as file:
299261
...: pickle.dump(l, file)
@@ -303,7 +265,8 @@ Useful to store arbitrary objects to a file. Not safe or fast!
303265
...: out = pickle.load(file)
304266
...:
305267

306-
In [5]: out
268+
In [49]: out
269+
Out[49]: [1, None, 'Stan']
307270

308271

309272
.. topic:: Exercise

0 commit comments

Comments
 (0)