Python Forum
some ideas for intelligent list splitting?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
some ideas for intelligent list splitting?
#1
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 ))
Reply
#2
(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.
Recommended Tutorials:
Reply
#3
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?
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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']
Reply
#5
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Pandas Syntax problem? Wrong Output, any ideas? Gbuoy 2 931 Jan-18-2023, 10:02 PM
Last Post: snippsat
  Splitting strings in list of strings jesse68 3 1,773 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Help with coding/ideas natalie 6 1,852 Feb-12-2022, 03:16 PM
Last Post: deanhystad
  Looking for some ideas - Radius Log moralear27 1 1,960 Sep-10-2020, 09:32 PM
Last Post: micseydel
  A more intelligent .sort()? Pedroski55 4 2,692 Jul-08-2020, 07:52 AM
Last Post: DeaD_EyE
  Dealing with a .json nightmare... ideas? t4keheart 10 4,412 Jan-28-2020, 10:12 PM
Last Post: t4keheart
  Splitting String into 2d list cclark135 2 2,805 Aug-26-2019, 01:46 PM
Last Post: ThomasL
  splitting numeric list based on condition python_newbie09 7 9,412 May-27-2019, 03:58 PM
Last Post: python_newbie09
  Need help | splitting list into list Vinci141 3 2,597 Mar-13-2019, 09:09 PM
Last Post: Vinci141
  Ideas?? gullidog 3 3,739 Jul-20-2017, 09:06 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020