Skip to content

Commit fe15fc1

Browse files
committed
Update for sh 2.x
1 parent b3c336c commit fe15fc1

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

hands-on/shell_interaction.ipynb

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,13 @@
5151
"sh.ls('-l')"
5252
]
5353
},
54-
{
55-
"cell_type": "markdown",
56-
"metadata": {},
57-
"source": [
58-
"The output can be used by assigning the command to a variable, and using the result's `stdout` attribute. Note that the latter is a sequence of bytes, so it has to be decoded into a UTF-8 string for further processing."
59-
]
60-
},
6154
{
6255
"cell_type": "code",
6356
"execution_count": null,
6457
"metadata": {},
6558
"outputs": [],
6659
"source": [
67-
"cmd = sh.ls('-l', '-a', _encoding='UTF-8')"
60+
"result = sh.ls('-l', '-a')"
6861
]
6962
},
7063
{
@@ -73,7 +66,7 @@
7366
"metadata": {},
7467
"outputs": [],
7568
"source": [
76-
"lines = cmd.stdout.decode(encoding='utf8').split('\\n')"
69+
"lines = result.split('\\n')"
7770
]
7871
},
7972
{
@@ -109,7 +102,8 @@
109102
"metadata": {},
110103
"outputs": [],
111104
"source": [
112-
"sh.ls()"
105+
"for file in (line.strip() for line in sh.ls().split()):\n",
106+
" print(file)"
113107
]
114108
},
115109
{
@@ -179,7 +173,7 @@
179173
"metadata": {},
180174
"outputs": [],
181175
"source": [
182-
"sh.cat('tmp/date_file.txt')"
176+
"print(sh.cat('tmp/date_file.txt'))"
183177
]
184178
},
185179
{
@@ -228,14 +222,14 @@
228222
},
229223
"outputs": [],
230224
"source": [
231-
"sh.grep(sh.ls('-l'), r'\\.ipynb$')"
225+
"print(sh.grep('-e', r'\\.ipynb$', _in=sh.ls('-l')))"
232226
]
233227
},
234228
{
235229
"cell_type": "markdown",
236230
"metadata": {},
237231
"source": [
238-
"Pipe the output of `cut` into `sort`."
232+
"Pipe the output of `cut` into `sort`. Also use the `_iter` argument to create a generator over standard output."
239233
]
240234
},
241235
{
@@ -246,7 +240,8 @@
246240
},
247241
"outputs": [],
248242
"source": [
249-
"sh.sort(sh.cut('-d', ' ', '-f', '5', 'tmp/date_file.txt'), '-r')"
243+
"for line in sh.sort('-r', _in=sh.cut('-d', ' ', '-f', '5', 'tmp/date_file.txt'), _iter=True):\n",
244+
" print(line.strip())"
250245
]
251246
},
252247
{
@@ -315,10 +310,12 @@
315310
},
316311
"outputs": [],
317312
"source": [
313+
"process = sh.sleep(10, _bg=True, _bg_exc=False, _timeout=3)\n",
318314
"try:\n",
319-
" process = sh.sleep(10, _bg=True, _timeout=3)\n",
320-
"except TimeoutError as error:\n",
321-
" print(error)"
315+
" process.wait()\n",
316+
"except sh.TimeoutException as error:\n",
317+
" print('process timed out')\n",
318+
" print(error.exit_code)"
322319
]
323320
},
324321
{
@@ -578,7 +575,7 @@
578575
"metadata": {},
579576
"outputs": [],
580577
"source": [
581-
"process = subprocess.run('ls *.py', stdout=subprocess.PIPE, encoding='utf8', shell=True)"
578+
"process = subprocess.run('ls *.ipynb', stdout=subprocess.PIPE, encoding='utf8', shell=True)"
582579
]
583580
},
584581
{
@@ -653,7 +650,9 @@
653650
{
654651
"cell_type": "code",
655652
"execution_count": null,
656-
"metadata": {},
653+
"metadata": {
654+
"scrolled": true
655+
},
657656
"outputs": [],
658657
"source": [
659658
"process.returncode"
@@ -676,7 +675,7 @@
676675
"name": "python",
677676
"nbconvert_exporter": "python",
678677
"pygments_lexer": "ipython3",
679-
"version": "3.11.3"
678+
"version": "3.11.6"
680679
}
681680
},
682681
"nbformat": 4,

0 commit comments

Comments
 (0)