Writing Crunch scripts in Python » History » Version 7
Tom Clegg, 04/08/2015 07:40 PM
| 1 | 4 | Tom Clegg | {{>toc}} |
|---|---|---|---|
| 2 | |||
| 3 | 1 | Bryan Cosca | h1. Writing Crunch scripts in Python |
| 4 | |||
| 5 | 6 | Tom Clegg | ... |
| 6 | |||
| 7 | 2 | Bryan Cosca | h3. How to read your input file from the json template |
| 8 | 1 | Bryan Cosca | |
| 9 | 6 | Tom Clegg | ... |
| 10 | |||
| 11 | 1 | Bryan Cosca | h3. How to write your file to a collection |
| 12 | |||
| 13 | 6 | Tom Clegg | ... |
| 14 | |||
| 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 | |||
| 19 | <pre> |
||
| 20 | import subprocess |
||
| 21 | foo = subprocess.check_output(['echo','foo']) |
||
| 22 | </pre> |
||
| 23 | |||
| 24 | If the output is big, redirect it to a file: |
||
| 25 | |||
| 26 | <pre> |
||
| 27 | import subprocess |
||
| 28 | with open('/tmp/foo', 'w') as outfile: |
||
| 29 | subprocess.check_call(['head', '-c', '1234567', '/dev/urandom'], stdout=outfile) |
||
| 30 | </pre> |
||
| 31 | |||
| 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 | |||
| 37 | 5 | Tom Clegg | h3. Where to put temporary files |
| 38 | 4 | Tom Clegg | |
| 39 | 6 | Tom Clegg | ... |
| 40 | |||
| 41 | 5 | Tom Clegg | h3. How to write data directly to Keep with native Python code |
| 42 | 6 | Tom Clegg | |
| 43 | ... |