Python Forum

Full Version: some ideas for intelligent list splitting?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have the following list ->

['SYSTEM=LDAP', 'registry=LDAP']
in need the elements as commandline argument for a system command
chsec -f filename -s stanza -a element1 -a element2 -a and_so_on

how to do this?
some kind of argument counting would also be nice, because the argument count is
flexible.

rc, package_result, err = module.run_command("%s -f %s -s %s -a %s" % (chsec_command, filename, stanza, options ))
(Nov-20-2018, 11:39 AM)wardancer84 Wrote: [ -> ]in need the elements as commandline argument for a system command
your looking for the argparse module. Which is is in the standard library. nargs='*' is used for variable number of arguments.
There is more than one way. I think a generator is the easiest approach.
The generator yields fist -a, then a element, then -a then the next element.
If there are no elements, the for-loop won't be executed, so you also don't get -a back.

def gen_options(elements):
    for element in elements:
        yield '-a'
        yield element

filename = 'a_file'
command = ['chsec', '-f', filename, '-s', 'stanza']
elements = ['file_a', 'file_b', 'file_x']
options = list(gen_options(elements))

command += options
# concatenation of commad and options. It mutates command.
print(command)
Output:
['chsec', '-f', 'a_file', '-s', 'stanza', '-a', 'file_a', '-a', 'file_b', '-a', 'file_x']
Is this the output you want?
yes, this looks very promising..i will try this out and see how far it goes.

ok...one little thing.

the command need to run as follows.

chsec -f filename -s stanza -a SYSTEM=LDAP -a registry=LDAP

how to remove the commas from the output.

actual out:

MODULE_STDOUT:

['-a', 'SYSTEM=LDAP', '-a', 'registry=LDAP']
desired out:
MODULE_STDOUT:

['-a' 'SYSTEM=LDAP'  '-a' 'registry=LDAP']
You don't need to remove the commas.
When you enter following into your shell:
ls -l /proc
The shell splits the command into it's parts:
[
    'ls',
    '-l,',
    '/proc'
]
The main function a a C program, get usually all command line arguments as an array, where the first argument is the program itself.

In Python it's sys.argv.
Just try following code:
import sys
print(sys.argv)
Call it test.py for example.

Then run it:
python test.py
python test.py Hello World
python test.py "Hello World" # Attentin, Quoting!
python test.py a b c d e f g
Output:
andre@andre-GP70-2PE:~$ python test.py Hello World ['test.py', 'Hello', 'World'] andre@andre-GP70-2PE:~$ python test.py "Hello World" # Attentin, Quoting! ['test.py', 'Hello World'] andre@andre-GP70-2PE:~$ python test.py a b c d e f g ['test.py', 'a', 'b', 'c', 'd', 'e', 'f', 'g'] andre@andre-GP70-2PE:~$
From subprocess the functions Popen, call, check_output, etc. need as first argument a list/tuple/iterable. The first element of the list is the program binary itself. The following elements are the arguments.

You have it already in the right form.
If you have for example a long string as you type it into your shell, you need to split it before you are able to run it (i won't give tips how to do this whithout splitting). To split the command safe with paying attention about quoutes, you should look for shlex.split.