Question About Command line arguments

2011-06-10 Thread Mark Phillips
I have a script that processes command line arguments

def main(argv=None):
syslog.syslog("Sparkler stared processing")
if argv is None:
argv = sys.argv
if len(argv) != 2:
syslog.syslog(usage())
else:
r = parseMsg(sys.argv[1])
syslog.syslog(r)
return 0

if __name__ == "__main__":
sys.exit(main())

When I run "python myscript fred" it works as expected - the argument fred
is processed in parseMsg as sys.arv[1]

When I run "echo fred | python myscript" the script thinks there are no
arguments, so it prints out the usage statement.

Is the problem with the echo command, or how I wrote my script?

Thanks!

Mark
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question About Command line arguments

2011-06-10 Thread Mark Phillips
On Fri, Jun 10, 2011 at 10:41 AM, MRAB  wrote:

> On 10/06/2011 18:21, Mark Phillips wrote:
>
>> I have a script that processes command line arguments
>>
>> def main(argv=None):
>> syslog.syslog("Sparkler stared processing")
>> if argv is None:
>> argv = sys.argv
>> if len(argv) != 2:
>> syslog.syslog(usage())
>> else:
>> r = parseMsg(sys.argv[1])
>> syslog.syslog(r)
>> return 0
>>
>> if __name__ == "__main__":
>> sys.exit(main())
>>
>> When I run "python myscript fred" it works as expected - the argument
>> fred is processed in parseMsg as sys.arv[1]
>>
>> When I run "echo fred | python myscript" the script thinks there are no
>> arguments, so it prints out the usage statement.
>>
>> Is the problem with the echo command, or how I wrote my script?
>>
>>  In the second case, there aren't any arguments. The echo command is
> writing "fred" to its standard output, which is attached to your
> script's standard input.
>
> How do I write my script so it picks up argument from the output of
commands that pipe input into my script?

Mark
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question About Command line arguments

2011-06-10 Thread Mark Phillips
On Fri, Jun 10, 2011 at 11:03 AM, Kurt Smith  wrote:

> On Fri, Jun 10, 2011 at 12:58 PM, Mark Phillips
>  wrote:
> > How do I write my script so it picks up argument from the output of
> commands
> > that pipe input into my script?
>
> def main():
>import sys
>print sys.stdin.read()
>
> if __name__ == '__main__':
>main()
>
> $ echo "fred" | python script.py
> fred
> $
>
Kurt,

How does one write a main method to handle both command line args and stdin
args?

Thanks,

Mark
-- 
http://mail.python.org/mailman/listinfo/python-list


How to convert a string into a list

2010-10-04 Thread Mark Phillips
I have the following string - "['1', '2']" that I need to convert into a
list of integers - [1,2]. The string can contain from 1 to many integers. Eg
"['1', '7', '4',..,'n']" (values are not sequential)

What would be the best way to do this? I don't want to use eval, as the
string is coming from an untrusted source.

Thanks!

Mark
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to convert a string into a list

2010-10-05 Thread Mark Phillips
Thanks to everyone for their suggestions. I learned a lot from them!

Mark

On Mon, Oct 4, 2010 at 11:54 PM, Chris Rebert  wrote:

> On Mon, Oct 4, 2010 at 10:33 PM, Arnaud Delobelle 
> wrote:
> > MRAB  writes:
> >> On 05/10/2010 02:10, Mark Phillips wrote:
> >>> I have the following string - "['1', '2']" that I need to convert into
> a
> >>> list of integers - [1,2]. The string can contain from 1 to many
> >>> integers. Eg "['1', '7', '4',..,'n']" (values are not sequential)
> >>>
> >>> What would be the best way to do this? I don't want to use eval, as the
> >>> string is coming from an untrusted source.
> >>>
> >> I'd probably use a regex, although others might think it's overkill. :-)
> >>
> >>>>> import re
> >>>>> s = "['1', '2']"
> >>>>> [int(n) for n in re.findall(r'-?\d+', s)]
> >> [1, 2]
> >>
> >> An alternative is:
> >>
> >>>>> s = "['1', '2']"
> >>>>> [int(n.strip("'[] ")) for n in s.split(",")]
> >> [1, 2]
> >
> > I'll add:
> >
> >>>> s = ['1', '2', '42']
> >>>> [int(x) for x in s.split("'")[1::2]]
> > [1, 2, 42]
>
> There's also:
> >>> s = "['1', '2']"
> >>> from ast import literal_eval
> >>> [int(n) for n in literal_eval(s)]
> [1, 2]
>
> Which is safe, but less strict.
>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list