Arguments in Python

This is the place for queries that don't fit in any of the other categories.

Arguments in Python

Postby alabview » Tue Nov 26, 2013 3:00 pm

Hi there,

I wish to implement multiple arugments in Python but no idea how to do it.

For example,

python DCSOURCE.py "CONFIGURE{DCSOURCE1:RESET}" "CONFIGURE{SWITCH1|CH1|OPEN}"

Where,
Argument1 = "CONFIGURE{DCSOURCE1:RESET}"
Argument2 = "CONFIGURE{SWITCH1|CH1|OPEN}"



Please advice and Thanks.
alabview
 
Posts: 22
Joined: Mon Sep 16, 2013 1:48 pm

Re: Arguments in Python

Postby Marbelous » Tue Nov 26, 2013 3:21 pm

You can pass in a list (*my_list) or a dictionary (**my_dict) as arguments to python functions. Check out the Defining Functions chapter 4.7 in the Tutorial.

From the Python Tutorial ch. 4.7.2:
Code: Select all
def cheeseshop(kind, *arguments, **keywords):
    print "-- Do you have any", kind, "?"
    print "-- I'm sorry, we're all out of", kind
    for arg in arguments: print arg
    print "-" * 40
    keys = keywords.keys()
    keys.sort()
    for kw in keys: print kw, ":", keywords[kw]
Marbelous
 
Posts: 128
Joined: Fri May 31, 2013 8:12 pm

Re: Arguments in Python

Postby mckryall » Tue Nov 26, 2013 3:50 pm

I don't think he's trying to use a function. He said "python hjbhjbjh.py", so that means he's talking about arguments in terminal/command prompt. For starters, alabview, you need to import argv and unpack argv(essentially defining variables in one line). Then, they can be used as variables. Note that you don't need to use quotation marks, and that spaces mark the beginning and end of a variable in the command prompt. Try this lesson http://learnpythonthehardway.org/book/ex13.html and try to run this code:
Code: Select all
from sys import argv

script, val1, string1, val2, string2 = argv

print "This script is called %s" % script
print "Value 1 is %s" % val1
print "String 1 is %s" % string1
print "Value 2 is %s" % val2
print "String 2 is %s" % string2

You should see something like this:
Code: Select all
MYMAC:python MYACCOUNT$ python playground.py 99 bottles 98 glasses
This script is called playground.py
Value 1 is 99
String 1 is bottles
Value 2 is 98
String 2 is glasses
Last edited by Mekire on Sat Nov 30, 2013 7:34 am, edited 1 time in total.
Reason: Locked
Python 2.7
Mac Mini
Laptop w/ Ubuntu 14.04
Steam; PSN
mckryall
 
Posts: 94
Joined: Sat Nov 23, 2013 6:13 pm

Re: Arguments in Python

Postby alabview » Tue Nov 26, 2013 4:15 pm

Mckryall,

Yes, you are correct.

I have tested an example you showed to me just now and I have an error message appears as below,

Code: Select all
script,val1,string1,val2,string2 = argv
ValueError: need more than 1 value to unpack


Please advice.
alabview
 
Posts: 22
Joined: Mon Sep 16, 2013 1:48 pm

Re: Arguments in Python

Postby Marbelous » Tue Nov 26, 2013 4:19 pm

I believe you're right, I completely missed the obvious command-line syntax. :oops:
Marbelous
 
Posts: 128
Joined: Fri May 31, 2013 8:12 pm

Re: Arguments in Python

Postby Kebap » Wed Nov 27, 2013 11:32 am

alabview wrote:I have tested an example you showed to me just now and I have an error message

You need to execute the example exactly like mckryall did, with the correct number of arguments.

Of course, you could also modify the code to check the number of arguments provided, then react accordingly.
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 390
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests

cron