0

I'm trying to get this script to take the contents of the tuple and cycle through using a for loop (I'm not sure where to put it in my code) and put the contents of the tuple in a command. For this example I've used find as the command. Depending on which option the executor uses sp1 or sp2 will determine how much of the tuple will be used.

import sys, subprocess, os, string

cmd = '/bin/find '

tuple = ('apple', 'banana', 'cat', 'dog')

sp1 = tuple[0:1]
sp2 = tuple[2:3]

def find():
    find_cmd = subprocess.Popen(cmd + " * -name {}".format(type)),
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE, shell=True)
    output, err = find_cmd.communicate()
    find = output
    find_temp = []
    find_temp = string.split(find)
    if find_temp[0] == ' ':
        print("Found nothing")
    else:
        print("Found {}".format(find_temp))

type_input = input("Are you looking for fruit or animals? ")
if type_input  == "fruit":
    type = sp1
elif type_input == "animals":
    type = sp2
    print("syntax error")
    exit()

find()
martineau
  • 119,623
  • 25
  • 170
  • 301
clarkus978
  • 574
  • 3
  • 10
  • 22

1 Answers1

0

You're close, but you don't do what you're trying to do, that's just silly. You could do better.

Rather than doing this weird tuple slicing thing, just give them a real name:

import sys, subprocess, os, string

# No need for the trailing space here
FIND = '/bin/find'

fruit = ('apple', 'banana')
animals = ('cat', 'dog')

Alternatively, you could use a dictionary:

find_params = {
    'fruit': ['apple', 'banana'],
    'animals': ['cat', 'dog'],
}

In your comment you mentioned:

my tuple is a bit larger and both variables use some of the same values... This would keep me from typing many of the same values into two separate lists.

You can still take a nice approach:

cheeses = ('Wensleydale', 'Edam', 'Cheddar', 'Gouda')
spam = ('Spam with eggs', 'Spam on eggs', 'Spam')
confectionaries = ('crunchy frog', 'spring surprise', 'cockroach cluster',
                   'anthrax ripple', 'cherry fondue')
food = cheeses + spam + confectionaries

Even if you just need a subset, you can still do something like:

food = cheeses + spam[:2] + confectionaries[-1:]

You should take parameter(s) for your find command instead. Also, no need to concatenate and then use a format string. Just use a format string for all the things:

def find(what, cmd=FIND):
    find_cmd = subprocess.Popen('{cmd} * -name {what}'.format(cmd=cmd, what=what),
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE, shell=True)
    output, err = find_cmd.communicate()
    find = output
    find_temp = []
    find_temp = string.split(find)
    if find_temp[0] == ' ':
        print("Found nothing")
    else:
        print("Found {}".format(find_temp))

Now you can either use the variables, or what they asked for:

type_input = input("Are you looking for fruit or animals? ")
try:
    find(find_params[type_input])
except KeyError:
    sys.exit('Unknown parameter {!r}'.format(type_input))

# Or use the variables
if type_input  == "fruit":
    find(fruit)
elif type_input == "animals":
    find(animals)
else:
    sys.exit('Unknown parameter {!r}'.format(type_input))
Wayne Werner
  • 49,299
  • 29
  • 200
  • 290
  • Brilliant! Thank you for sharing. Now, it's my fault for not clearly explaining my issue, but in my real world issue my tuple is a bit larger and both variables use some of the same values. For example `sp1 = tuple[0:10]` and `sp2 = tuple[0:20]`. This would keep me from typing many of the same values into two separate lists. – clarkus978 Dec 07 '16 at 20:12
  • I'd still give them more appropriate names than `sp1` and `sp2`. I'll update my question with some examples. – Wayne Werner Dec 07 '16 at 21:22
  • Thank you for updating your answer. This really helps. – clarkus978 Dec 07 '16 at 21:40