Re: [Tutor] Strings
Thank you guys On Thu, Nov 6, 2014 at 3:39 AM, Dave Angel wrote: > William Becerra Wrote in message: > > > > have the following code: > names = "John, Cindy, Peter" > def find(str, ch, s): > index = 0 > while index < len(str): > if s==1: > for char in names[:4]: > if str[index] == ch: > return index + 1 > index = index + 1 > if s==2: > for char in names[6:11]: > if str[index] == ch: > return index + 1 > index = index + 1 > if s==3: > for char in names[13:]: > if str[index] == ch: > return index + 1 > index = index + 1 > return -1 > print find(names,"n", 2) > > > > and my problem is: > I intend for the parameter s to tell the interpreter which name to > look at > so that i get the index return value related to that name. > for example: > John and Cindy both have a letter 'n' but when I call the function > with an s value of 2 I want it to return the index value of the > letter n in Cindy and not in John. > > Your most immediate problem is that you're using index to fetch > characters from the original string, and that's only reasonable > for John. You could make a slice of the original, and search that > slice. Or you could change the comparison to: >if char == ch: > > The loop could also be cleaner with enumerate. > > Your second big problem is that you've hard coded the sizes of the > first two names in the slice expressions. The magic [6:11] for > example certainly doesn't belong inside the function. > > > Third is your while loop makes no sense. Each of the if clauses > won't finish till you're done with the selected substring, so > you might as well return right away. Drop that line entirely. > > > Anyway, I say you're trying to do too much in the one function. If > possible change the data structure of names to eliminate one of > the two tasks, or write two functions, a few lines > each. > > I'd make > names = ["John", "Cindy", "Peter"] > And start the function with > name = names [s-1] > > And now the task of the remainder of the function is to search a > name for a particular character. > > -- > DaveA > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Subprocess how to use?
- On Thu, Nov 6, 2014 12:57 AM CET Cameron Simpson wrote: >On 05Nov2014 14:05, jarod...@libero.it wrote: >> I need to use external program from my scirpt. >> code = "intersectBed -a %s -b /database/Refseq_Gene2.bed -wa -wb|cut -f >> 4,8|uniq > tmp.tmp1"%("gene5.tmp.bed") >>code2 = "intersectBed -a %s -b /database/Refseq_Gene2.bed -wa -wb|cut -f >> 4,8|uniq > tmp.tmp2"%("gene3.tmp.bed") >>proc = [] >>p = subprocess.Popen(code,shell=True) >>proc.append(p) >>time.sleep(1) # seconds >>p = subprocess.Popen(code2,shell=True) >>proc.append(p) >>time.sleep(1) >>print >> >sys.stderr,'-' >>print >sys.stderr,"Waiting for Star Fusion Annotate to finish running..." >>for p in proc: >>p.communicate() > >First remark: as written above, your shell pipelines do not read any input and >all their output goes to your temp files. Therefore using p.communicate() is a >waste of time. Just use p.wait(); there is no I/O to do from the point of view >of your Python code. > >Second remark: it is generally a bad idea to use Python's % operator to embed >strings in shell commands (or other "parsed by someone else" text, such as >SQL) because unless you are always very careful about the inserted string (eg >"gene5.tmp.bed") you may insert punctuation, leading to bad behaviour. In your >example code above you are sort of ok, and we can address this as a separate >issue after you have things working. > >>What append I don't habve any error however the file are truncated. > >You need to find out why. > >The shell code _will_ truncate "tmp.tmp1", but then we expect the output of >"uniq" to appear there. > >Start by stripping back the shell pipeline. > >If you remove the "|uniq", is there anything in "tmp.tmp1"? > >If there is not, strip off the "|cut -f 4,8" as well and check again. At that >point we're asking: does intersectBed actually emit any output? > >You can test that on the command line directly, and then build up the shell >pipeline to what you have above by hand. When working, _then_ put it into your >Python program. > >>So I try to use subprocess.call >> >> p = subprocess.call(shlex.split(code),stdout = subprocess.PIPE, stderr = >> subprocess.STDOUT, shell = False) >> but with p.comunicate I have this error: >> >> ('\n* ERROR: Unrecognized parameter: -wb|cut *\n* ERROR: >> Unrecognized parameter: > >[...] > >This is a standard mistake. shlex is _not_ a full shell syntax parser. It is >a simple parser that understands basic shell string syntax (quotes etc) but >NOT redirections. It is really a tool for using with your own minilanguages, >as you might if you were writing an interactive program that accepts user >commands. > >You can't use shlex for shell pipelines or shell redirections. > >What is happening above is that all the "words" are being gathered up and >passed to the "intersectBed" command as arguments; no pipelines! And >"intersectBed" complains bitterly as you show. > >You can see this in the example you post below: > >> shlex.split(code) >> Out[102]: >> ['intersectBed', >> '-a', >> 'gene5.tmp.bed', >> '-b', >> '/home/maurizio/database/Refseq_Gene2.bed', >> '-wa', >> '-wb|cut', >> '-f', >> '4,8|uniq', >> '>', >> 'tmp.tmp1'] >> >> So what can I do? which system do you suggest to my Problem? > >First, forget about shlex. It does not do what you want. > >Then there are three approaches: > >1: Fix up your shell pipeline. You original code should essentially work: >there is just something wrong with your pipeline. Strip it back until you can >see the error. > >2: Construct the pipeline using Popen. This is complicated for someone new to >Popen and subprocess. Essentially you would make each pipeline as 3 distinct >Popen calls, using stdout=PIPE and attached that to the stdin of the next >Popen. Look for "connecting segments" on http://pymotw.com/2/subprocess/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Subprocess how to use?
Dear All thanks so much for the suggestion !!! One thing is not clear to me: How can write more safe string to send on subprocess.Popen() without %s? What is the best way to do this? thanks so much! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Subprocess how to use?
On 06Nov2014 22:18, jarod...@libero.it wrote: Dear All thanks so much for the suggestion !!! One thing is not clear to me: How can write more safe string to send on subprocess.Popen() without %s? What is the best way to do this? The safest way is to use shell=False and pass a python list with the command line strings in it. If you absolutely must generate a shell command string, you need to use some intermediate function that knows how to quote a string for the shell. Eg: def shell_quote(s): return "'" + s.replace("'", r"'\''") + "'" That's untested, but it puts a string in single quotes and correctly escapes any single quotes in the string itself. Then you'd go: shcmd = "cat %s %s" % (shell_quote(filename1), shell_quote(filename2)) P = Popen(shcmd, shell=True) You will see the same kind of thing in most database interfaces, but presented more conveniently. As with the shell, it is always bad to go: sqlcmd = "INSERT into Table1 values(%s,%s)" % (value1, value2) because value1 or value2 might have SQL punctuation in it. Eg: http://xkcd.com/327/ Instead you will usually use a call like this: db_handle.execute("INSERT into Table1 values(?,?)", value1, value2) and the .execute function will itself call the right SQL quoting function and replace the "?" for you. Cheers, Cameron Simpson ... It beeped and said "Countdown initiated." Is that bad? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor