Prev: Employee motivation in the software industry
Next: Pickling an extension type subclasses problems
From: Jean-Michel Pichavant on 5 Feb 2010 10:28 mk wrote: > Jean-Michel Pichavant wrote: >> >> What is worrying me the most in your code sample is that self.cmd can >> hold diferrent types (str, and something else). That is usually a bad >> thing to do (putting None aside). >> However, my remark could be totally irrelevant of course, that >> depends on the context. > > That's a valid criticism - but I do not know how to handle this > otherwise really, because the program can be called with "cmd" to run, > or a script to run (or a directory to copy) and in those cases cmd is > None. > > I guess I could use > > if cmd: > self.cmd = ... > > > But. Suppose that under some circumstances cmd is not string. What then? > > I know that isinstance is typically not recommended, but I don't see > better solution here. > > > Regards, > mk > > If you can change your program interface, then do it, if not then you're right you don't have much choice as you are suffering from the program poor interface. You can fix this problem by explicitly asking for the thing you want to do, instead of guessing by inspecting the argument nature. myProg --help usage : myProg command [args] command list: - cmd: execute the given <arg1> command line - exec: execute the given script file named <arg1> - copy: copy <arg1> to <arg2> example: >myProg cmd "echo that's cool" >myProg exec /etc/init.d/myDaemon >myProg copy /tmp /tmp2 JM
From: John Posner on 5 Feb 2010 10:42 On 2/5/2010 9:21 AM, mk wrote: > > if isinstance(cmd, str): > self.cmd = cmd.replace(r'${ADDR}',ip) > else: > self.cmd = cmd > > or > > self.cmd = cmd > if isinstance(cmd, str): > self.cmd = cmd.replace(r'${ADDR}',ip) > > (lunatic fringe?) Last August [1], I offered this alternative: self.cmd = (cmd.replace(r'${ADDR}',ip) if isinstance(cmd, str) else cmd) But it didn't get much love in this forum! [1] http://groups.google.com/group/comp.lang.python/browse_thread/thread/6876917a4d579d59/1f700586f4c4614d?lnk=gst&q=Posner#1f700586f4c4614d
From: Peter Otten on 5 Feb 2010 10:49 mk wrote: > if isinstance(cmd, str): > self.cmd = cmd.replace(r'${ADDR}',ip) > else: > self.cmd = cmd > > or > > self.cmd = cmd > if isinstance(cmd, str): > self.cmd = cmd.replace(r'${ADDR}',ip) Neither. self.cmd = cmd self.ip = ip .... subprocess.call(self.cmd, shell=True, env=dict(ADDR=self.ip)) Please spend a bit more energy on a descriptive subject and an unambiguous exposition of your problem (in English rather than code) next time. Peter
From: mk on 5 Feb 2010 10:52 Jean-Michel Pichavant wrote: > If you can change your program interface, then do it, if not then you're > right you don't have much choice as you are suffering from the program > poor interface. > You can fix this problem by explicitly asking for the thing you want to > do, instead of guessing by inspecting the argument nature. > > myProg --help > > usage : myProg command [args] > command list: > - cmd: execute the given <arg1> command line > - exec: execute the given script file named <arg1> > - copy: copy <arg1> to <arg2> > > example: > >myProg cmd "echo that's cool" > >myProg exec /etc/init.d/myDaemon > >myProg copy /tmp /tmp2 > I sure can change the interface since I'm the author of the entire program. But I don't see how I can arrange program in a different way: the program is supposed to be called with -c parameter (command to run), -s script to run, or -y file_or_dir_to_copy. Then, I start instances of SSHThread class to do precisely that, separately for each ip/hostname: class SSHThread(threading.Thread): def __init__(self, lock, cmd, ip, username, sshprivkey=None, passw=None, port=22, script=None, remotedir=None): threading.Thread.__init__(self) self.lock = lock if isinstance(cmd, str): self.cmd = cmd.replace(r'${ADDR}',ip) else: self.cmd = cmd self.ip = ip self.username = username self.sshprivkey = sshprivkey self.passw = passw self.port = port self.conobj = None self.conerror = '' self.msgstr = '' self.confailed = True if script: self.setpathinfo(script, remotedir=remotedir) self.sentbytes = 0 self.finished = False self.abort = False It gets called like this: th = SSHThread(lock, opts.cmd, ip, username=username, sshprivkey=opts.key, passw=passw, port=port, script=opts.script, remotedir=opts.remotedir) ...where all the options are parsed by ConfigParser.OptionParser(). So they are either strings, or Nones. So in this context this is fine. But I wanted to make the class more robust. Perhaps I should do smth like this before setting self.cmd? assert isinstance(cmd, basestring) or cmd is None, "cmd should be string or None" and then: if cmd: self.cmd = cmd.replace.. ? Entire source code is here: http://python.domeny.com/cssh.py regards, mk
From: Jean-Michel Pichavant on 5 Feb 2010 10:54 John Posner wrote: > On 2/5/2010 9:21 AM, mk wrote: >> >> if isinstance(cmd, str): >> self.cmd = cmd.replace(r'${ADDR}',ip) >> else: >> self.cmd = cmd >> >> or >> >> self.cmd = cmd >> if isinstance(cmd, str): >> self.cmd = cmd.replace(r'${ADDR}',ip) >> >> > > (lunatic fringe?) > > Last August [1], I offered this alternative: > > self.cmd = (cmd.replace(r'${ADDR}',ip) > if isinstance(cmd, str) else > cmd) > > But it didn't get much love in this forum! > > > [1] > http://groups.google.com/group/comp.lang.python/browse_thread/thread/6876917a4d579d59/1f700586f4c4614d?lnk=gst&q=Posner#1f700586f4c4614d > Heresy ! JM
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: Employee motivation in the software industry Next: Pickling an extension type subclasses problems |