Writing Crunch scripts in Python » History » Version 7

Tom Clegg, 04/08/2015 07:40 PM

1 4 Tom Clegg
{{>toc}}
2 4 Tom Clegg
3 1 Bryan Cosca
h1. Writing Crunch scripts in Python
4 1 Bryan Cosca
5 6 Tom Clegg
...
6 6 Tom Clegg
7 2 Bryan Cosca
h3. How to read your input file from the json template
8 1 Bryan Cosca
9 6 Tom Clegg
...
10 6 Tom Clegg
11 1 Bryan Cosca
h3. How to write your file to a collection
12 1 Bryan Cosca
13 6 Tom Clegg
...
14 6 Tom Clegg
15 4 Tom Clegg
h3. How to run an external command
16 3 Bryan Cosca
17 7 Tom Clegg
Usually this is most convenient:
18 7 Tom Clegg
19 7 Tom Clegg
<pre>
20 7 Tom Clegg
import subprocess
21 7 Tom Clegg
foo = subprocess.check_output(['echo','foo'])
22 7 Tom Clegg
</pre>
23 7 Tom Clegg
24 7 Tom Clegg
If the output is big, redirect it to a file:
25 7 Tom Clegg
26 7 Tom Clegg
<pre>
27 7 Tom Clegg
import subprocess
28 7 Tom Clegg
with open('/tmp/foo', 'w') as outfile:
29 7 Tom Clegg
    subprocess.check_call(['head', '-c', '1234567', '/dev/urandom'], stdout=outfile)
30 7 Tom Clegg
</pre>
31 7 Tom Clegg
32 6 Tom Clegg
33 1 Bryan Cosca
h3. How to create new tasks per input file in a collection
34 4 Tom Clegg
35 6 Tom Clegg
...
36 6 Tom Clegg
37 5 Tom Clegg
h3. Where to put temporary files
38 4 Tom Clegg
39 6 Tom Clegg
...
40 6 Tom Clegg
41 5 Tom Clegg
h3. How to write data directly to Keep with native Python code
42 6 Tom Clegg
43 6 Tom Clegg
...