0

I am beginner in Python programing, I want to pass a complex object (Python dictionary or class object) as an argument to a python script using command line (cmd). I see that sys.argv gets only string parameters.

Here is an example of what I want:

class point(object):
     def __init__(self,x,y):
         self.__x=x
         self.__y=y
p=point(4,8)
import os
os.system('""xxxxxx.exe" "-s" "../create_scenario.py" "'+ p +'""')

The xxxxxx.exe is a program which accept to run a python script with the -s option. Thanks for all!!!

jamylak
  • 128,818
  • 30
  • 231
  • 230
user2294884
  • 1
  • 1
  • 2
  • 7
    Sorry, we don't do *urgent* here. For time-critical answers, please hire a consultant. – Martijn Pieters Apr 18 '13 at 11:58
  • 1
    `os.system` is really bad. – aldeb Apr 18 '13 at 11:58
  • 1
    Your only option is to save it to a file. On a side note: using `__x` is for name mangling which I'm pretty sure you don't need in the `point` class. You might have wanted a private variable `_x` if you have getter and setter functions/property but otherwise just leave it as `self.x` – jamylak Apr 18 '13 at 11:59
  • segfolt >> What to use so if os.system is really bad ? – user2294884 Apr 18 '13 at 12:02
  • See this post:http://stackoverflow.com/questions/3479728/is-it-good-style-to-call-bash-commands-within-a-python-script-using-os-systemb – aldeb Apr 18 '13 at 12:13

2 Answers2

1

You could try serialize\deserialize to\from a string using a pickle module. Look for dumps and loads

Odomontois
  • 15,918
  • 2
  • 36
  • 71
0

You could use eval:

my_dict = eval(sys.argv[1])
print(my_dict)

Comand prompt:

$ my_script.py {1:2}
$ {1: 2}

But what you want to do is not recommended (see this post). You should avoid this and store your data in a file instead (using JSON for example).

Community
  • 1
  • 1
aldeb
  • 6,588
  • 5
  • 25
  • 48