The Artima Developer Community
Sponsored Link

Python Answers Forum
to call class function from command line

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
abc def

Posts: 2
Nickname: mareeinaa
Registered: Jul, 2006

to call class function from command line Posted: Jul 17, 2006 5:27 AM
Reply to this message Reply
Advertisement
Hi,

I have written following code to call class function from command line

###

##### This is a script for calling function from command line #####

import getopt, sys
from sys import argv

class fileOperations:

def writeFile():
fw = open("abc.txt",'w')
fw.write('Simple File Operations')
fw.close()

def writeSpFile(*args):
fw = open(args[0],'w')
fw.write('Simple File Operations')
fw.close()

"""Our dispatch table has function names as the keys, with (description, function) tuples as values."""

x = fileOperations()
DISPATCH_TABLE = { 'writeFile' : x.writeFile,
'writeSpFile' : x.writeSpFile }


##### This is a script for calling function from command line #####

def callFunction(function_name,arguments):
the_func = DISPATCH_TABLE[function_name]
count = len(arguments)
print count

x.writeFile()
#the_func(*arguments)

if __name__ == "__main__" :

print "Unknown command '%s'" % sys.argv[1]

if DISPATCH_TABLE.has_key( sys.argv[1] ) :
callFunction(sys.argv[1],sys.argv[2:])

sys.exit( 1 )
###

when i am running this command
>>> python class_fn_Call1.py writeFile
I am getting the following error

Traceback (most recent call last):
File "class_fn_Call1.py", line 39, in ?
callFunction(sys.argv[1],sys.argv[2:])
File "class_fn_Call1.py", line 33, in callFunction
x.writeFile()
TypeError: writeFile() takes no arguments (1 given)

Same program i am able to run without using class functions. i.e if i write writeFile and writeSpFile functions as normal functions program will run properly. But when i am making it as class functions then it is giving errors.
So can anybody help me to reslove this problem? Is this a proper way to call a class function?
Please help me.

Thanks & Regards,

Topic: problem in calling web service Previous Topic   Next Topic Topic: apache + mod_python + coverage.py

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use