On Wed, Nov 2, 2016 at 7:36 AM, <tutor-requ...@python.org> wrote: > Send Tutor mailing list submissions to > tutor@python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-requ...@python.org > > You can reach the person managing the list at > tutor-ow...@python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > Today's Topics: > > 1. Re: Python code (Danny Yoo) > 2. Re: implementing sed - termination error (c...@zip.com.au) > 3. Module webbrowser.os (Palanikumar Gopalakrishnan) > 4. Re: implementing sed - termination error (Peter Otten) > 5. Re: Module webbrowser.os (Alan Gauld) > 6. Re: Module webbrowser.os (Steven D'Aprano) > > > ---------- Forwarded message ---------- > From: Danny Yoo <d...@hashcollision.org> > To: Haley Sandherr <haleysandh...@gmail.com> > Cc: Tutor@python.org > Date: Tue, 1 Nov 2016 21:58:08 -0700 > Subject: Re: [Tutor] Python code > On Nov 1, 2016 4:57 PM, "Haley Sandherr" <haleysandh...@gmail.com> wrote: > > > > Hello, I am new to python and need help with this question: > > > > Compose a function odd ( ) that takes three bool arguments and returns > True if an odd number of arguments are True and False otherwise. > > Do you understand all of the terms in the question? Are there terms in the > question that you don't know? > > Try a simpler, related problem if you are getting stuck: it may help point > the way forward. > > For example, your original question has three arguments. Can you do the > problem variation that just has one boolean argument? Call this odd1(). > You can see that it's like the original problem. > > If you can do this, try writing odd2(): a function that can deal with two > arguments. Can you do this? > > What other similar functions have you seen so far? > > These questions are intended to help us calibrate our mental model of what > you currently understand. Please tell us more do we can give you > appropriate answers. > > > > ---------- Forwarded message ---------- > From: c...@zip.com.au > To: bruce <badoug...@gmail.com> > Cc: Python Tutor Mailing List <tutor@python.org> > Date: Wed, 2 Nov 2016 16:22:21 +1100 > Subject: Re: [Tutor] implementing sed - termination error > On 01Nov2016 20:18, bruce <badoug...@gmail.com> wrote: > >> Running a test on a linux box, with python. >> Trying to do a search/replace over a file, for a given string, and >> replacing the string with a chunk of text that has multiple lines. >> >> From the cmdline, using sed, no prob. however, implementing sed, runs >> into issues, that result in a "termination error" >> > > Just terminology: you're not "implementing sed", which is a nontrivial > task that would involve writing a python program that could do everything > sed does. You're writing a small python program to call sed to do the work. > > Further discussion below. > > The error gets thrown, due to the "\" of the newline. SO, and other >> sites have plenty to say about this, but haven't run across any soln. >> >> The test file contains 6K lines, but, the process requires doing lots >> of search/replace operations, so I'm interested in testing this method >> to see how "fast" the overall process is. >> >> The following psuedo code is what I've used to test. The key point >> being changing the "\n" portion to try to resolved the termination >> error. >> >> import subprocess >> >> ll_="ffdfdfdfghhhh" >> ll2_="12112121212121212" >> hash="aaaaa" >> >> data_=ll_+"\n"+ll2_+"\n"+qq22_ >> print data_ >> > > Presuming qq22_ is not shown. > > cc='sed -i "s/'+hash+'/'+data_+'/g" '+dname >> print cc >> proc=subprocess.Popen(cc, shell=True,stdout=subprocess.PIPE) >> res=proc.communicate()[0].strip() >> > > There are two fairly large problems with this program. The first is your > need to embed newlines in the replacement pattern. You have genuine > newlines in your string, but a sed command would look like this: > > sed 's/aaaaa/ffdfdfdfghhhh\ > 12112121212121212\ > qqqqq/g' > > so you need to replace the newlines with "backslash and newline". > > Fortunately strings have a .replace() method which you can use for this > purpose. Look it up: > > https://docs.python.org/3/library/stdtypes.html#str.replace > > You can use it to make data_ how you want it to be for the command. > > The second problem is that you're then trying to invoke sed by > constructing a shell command string and handing that to Popen. This means > that you need to embed shell syntax in that string to quote things like the > sed command. All very messy. > > It is better to _bypass_ the shell and invoke sed directory by leaving out > the "shell=True" parameter. All the command line (which is the shell) is > doing is honouring the shell quoting and constructing a sed invocation as > distinct strings: > > sed > -i > s/this/that/g > filename > > You want to do the equivalent in python, something like this: > > sed_argv = [ 'sed', '-i', 's/'+hash+'/'+data_+'/g', dname ] > proc=subprocess.Popen(sed_argv, stdout=subprocess.PIPE) > > See how you're now unconcerned by any difficulties around shell quoting? > You're now dealing directly in strings. > > There are a few other questions, such as: if you're using sed's -i option, > why is stdout a pipe? And what if hash or data_ contain slashes, which you > are using in sed to delimit them? > > Hoping this will help you move forward. > > Cheers, > Cameron Simpson <c...@zip.com.au> > > > > ---------- Forwarded message ---------- > From: Palanikumar Gopalakrishnan <pal...@vahaitech.com> > To: tutor@python.org > Cc: > Date: Wed, 2 Nov 2016 12:14:50 +0530 > Subject: [Tutor] Module webbrowser.os > Hi Guys, > I recently tested with some code , which open browser > > import webbrowser > webbrowser.open("https://www.google.com") > > After that i want to experiment with webbrowser.os module, But dont know > how to execute it.So I use dir(webbrowser.os) to find some details. Then i > tried the following > > webbrowser.os(umask) > > But It retruns the following error > > > > *Traceback (most recent call last): File "<stdin>", line 1, in > <module>NameError: name 'umask' is not defined* > > > > > > -- > > *Warm Regards,* > > *Palanikumar Gopalakrishnan *[image: ✌] > *Developer* > > > > ---------- Forwarded message ---------- > From: Peter Otten <__pete...@web.de> > To: tutor@python.org > Cc: > Date: Wed, 02 Nov 2016 09:21:45 +0100 > Subject: Re: [Tutor] implementing sed - termination error > bruce wrote: > > > Hi > > > > Running a test on a linux box, with python. > > > > Trying to do a search/replace over a file, for a given string, and > > replacing the string with a chunk of text that has multiple lines. > > > > From the cmdline, using sed, no prob. however, implementing sed, runs > > into issues, that result in a "termination error" > > > > The error gets thrown, due to the "\" of the newline. SO, and other > > sites have plenty to say about this, but haven't run across any soln. > > > > The test file contains 6K lines, but, the process requires doing lots > > of search/replace operations, so I'm interested in testing this method > > to see how "fast" the overall process is. > > > > The following psuedo code is what I've used to test. The key point > > being changing the "\n" portion to try to resolved the termination > > error. > > Here's a self-contained example that demonstrates that the key change is to > avoid shell=True. > > $ cat input.txt > foo > alpha > beta foo gamma > epsilon > foo zeta > $ sed s/foo/bar\\nbaz/g input.txt > bar > baz > alpha > beta bar > baz gamma > epsilon > bar > baz zeta > $ python3 > Python 3.4.3 (default, Sep 14 2016, 12:36:27) > [GCC 4.8.4] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import subprocess > >>> subprocess.call(["sed", "s/foo/bar\\nbaz/g", "input.txt"]) > bar > baz > alpha > beta bar > baz gamma > epsilon > bar > baz zeta > 0 > > Both the shell and Python require you to escape, so if you use one after > the > other you have to escape the escapes; but with only one level of escapes > and > a little luck you need not make any changes between Python and the shell. > > > > > > > ---------- Forwarded message ---------- > From: Alan Gauld <alan.ga...@yahoo.co.uk> > To: tutor@python.org > Cc: > Date: Wed, 2 Nov 2016 09:16:56 +0000 > Subject: Re: [Tutor] Module webbrowser.os > On 02/11/16 06:44, Palanikumar Gopalakrishnan wrote: > > After that i want to experiment with webbrowser.os module, But dont know > > how to execute it. > > webbrowser.os seems to just be a link to the standard os module. > So you should read the docs for os...and use the os module directly. > > > So I use dir(webbrowser.os) to find some details. Then i > > tried the following > > > > webbrowser.os(umask) > > > > But It retruns the following error > > > > *Traceback (most recent call last): File "<stdin>", line 1, in > > <module>NameError: name 'umask' is not defined* > > I'm not sure why because it works for me in both Python 2.7 and 3.4. > Which OS are you using? And which Python version? > > But note that umask is a function so > > webbrowser.os(umask) > > returns a function reference. To get the umask value you must > supply one: > > webbrowser.os(umask(0x777)) > > And the returned value will be the current umask() (Which you > should store and restore when finished with the new umask) > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > > > > ---------- Forwarded message ---------- > From: "Steven D'Aprano" <st...@pearwood.info> > To: tutor@python.org > Cc: > Date: Wed, 2 Nov 2016 22:36:40 +1100 > Subject: Re: [Tutor] Module webbrowser.os > On Wed, Nov 02, 2016 at 12:14:50PM +0530, Palanikumar Gopalakrishnan wrote: > > Hi Guys, > > I recently tested with some code , which open browser > > > > import webbrowser > > webbrowser.open("https://www.google.com") > > > > After that i want to experiment with webbrowser.os module, > > webbrowser.os is just the os module. > > https://docs.python.org/2/library/os.html > https://docs.python.org/3/library/os.html > > When a module imports another module, say: > > import os > > then "os" becomes a top level name, available as webbrowser.os. But you > shouldn't use it. Just do "import os" and then use "os" on its own. > > > > -- > Steve > > > _______________________________________________ > Tutor maillist - Tutor@python.org > https://mail.python.org/mailman/listinfo/tutor > >
Hi Alan, My python version is *2.7. *I tried the following but it not working , Returns same error message webbrowser.os(umask) webbrowser.os(umask(0x777)) -- *Warm Regards,* *Palanikumar Gopalakrishnan *[image: ✌] *Developer* _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor