Python "know thyself"

Shell commands can be quick, but one of the main reasons to use python is that the code is more likely to be understood at some later date and it promotes reuse. They also combine more easily in a module. This is an extract from some python code that locates , selects files that end in .py , then cuts them up. The cutting really does nothing in this case, but is included as a NOP when I extend the code to combine more sequences. Command line syntax can be difficult to understand due to the syntactic shorthand that includes many codes that look like redacted swear words.

def findMyPythons():
 loff = ['start']
 located = subprocess.Popen(['locate', 'pyx'], 
    stdout=subprocess.PIPE,
    )

 grep = subprocess.Popen(['grep', '\.py$'],
    stdin=located.stdout,
    stdout=subprocess.PIPE,
    )

 cut = subprocess.Popen(['cut', '-f', '1', '-d:'],
    stdin=grep.stdout,
    stdout=subprocess.PIPE,
    )

 end_of_pipe = cut.stdout

This code requires that the python module "subprocess" be imported.

0 comments:

Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen