Re: [Tutor] Python debugger under Tiger?
Great recommendation, thanks.-MHOn May 18, 2005, at 8:12 PM, Lee Cullens wrote:Mike,You may not be looking for a commercial IDE, but I am very happy with WingIDE and using it with Tiger.Lee COn May 18, 2005, at 6:54 PM, Mike Hall wrote: I should of specified that I'm looking for an IDE with fulldebugging. Basically something like Xcode, but with Python support.On May 18, 2005, at 3:10 PM, [EMAIL PROTECTED] wrote: Quoting Mike Hall <[EMAIL PROTECTED]>: Does anyone know of a Python debugger that will run under OSX 10.4?The Eric debugger was looked at, but it's highly unstable underTiger. Thanks. pdb should work :-)-- John.___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor ___Tutor maillist - Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python debugger under Tiger?
I should of specified that I'm looking for an IDE with full debugging. Basically something like Xcode, but with Python support. On May 18, 2005, at 3:10 PM, [EMAIL PROTECTED] wrote: > Quoting Mike Hall <[EMAIL PROTECTED]>: > > >> Does anyone know of a Python debugger that will run under OSX 10.4? >> The Eric debugger was looked at, but it's highly unstable under >> Tiger. Thanks. >> > > pdb should work :-) > > -- > John. > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python debugger under Tiger?
Does anyone know of a Python debugger that will run under OSX 10.4? The Eric debugger was looked at, but it's highly unstable under Tiger. Thanks.-MH___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] building strings of specific length
You can chop off anything past 72 characters with: s2 = s[:72] On Apr 4, 2005, at 7:04 AM, Vines, John (Civ, ARL/CISD) wrote: Hello. I have a question regarding strings. How do I format a string to be a specific length? For example I need 'string1' to be 72 characters long. Thanks for your time, John ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Launching a file browser
On Apr 1, 2005, at 4:12 PM, Jeff Shannon wrote: At the OS level, these two actions are *completely* different. The webbrowser module launches an entirely separate program in its own independent process, where the "file browser" is opening a standard dialog inside of the current process and dependent upon the current process' message loop. (AFAIK, every GUI environment uses some sort of message/event loop...) I don't know Macs, but on Windows, the closest "file browser" parallel to what the webbrowser module is doing would be os.system("explorer.exe"), which launches a separate program in an independent process. However, if you're trying to get the results of the file selection back into your own app, you need to do the file browsing within your own process (or explicitly use some form of inter-process communication). In order to use a GUI file-browsing dialog, you need to follow all the rules for a GUI program. Thanks Jeff. This further clarifies it for me. -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Launching a file browser
Ah, so it has to do with access to the window manager. That answers a lot, thanks. On Mar 31, 2005, at 4:09 PM, Max Noel wrote: On Apr 1, 2005, at 00:14, Mike Hall wrote: On Mar 31, 2005, at 12:21 AM, Max Noel wrote: It's been too long since I used Python on MacOSX, but IIRC you can't just run a Python GUI program from the shell. Or something like that...you should ask this one on the python-mac SIG mailing list: http://www.python.org/sigs/pythonmac-sig/ Kent You have to launch your script with pythonw, not with python. I'm unclear on why a command like webbrowser.open() will comfortably launch your default web browser (in my case Safari), but something as ubiquitous to an OS as a file browser has special needs to launch. Perhaps each application has custom written their file browser, and I'm assuming they are each essentially doing system calls to the same thing...? No, the reason for that, IIRC, is that for the program to be able to interact with the window manager, it has to be launched with pythonw. When the program starts to display stuff elsewhere than in STDOUT or STDERR, an application launch is somehow triggered (icon appears in the Dock), which for some reason enables the user to interact with the program. Launching a web browser requires no interaction whatsoever with the WM, and can therefore be done with python. Yes, the python/pythonw distinction in Mac OS X is stupid, I'll give you that. I don't even know why it exists in the first place. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 "Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine?" -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Launching a file browser
On Mar 31, 2005, at 12:21 AM, Max Noel wrote: It's been too long since I used Python on MacOSX, but IIRC you can't just run a Python GUI program from the shell. Or something like that...you should ask this one on the python-mac SIG mailing list: http://www.python.org/sigs/pythonmac-sig/ Kent You have to launch your script with pythonw, not with python. I'm unclear on why a command like webbrowser.open() will comfortably launch your default web browser (in my case Safari), but something as ubiquitous to an OS as a file browser has special needs to launch. Perhaps each application has custom written their file browser, and I'm assuming they are each essentially doing system calls to the same thing...? -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Launching a file browser
On Mar 28, 2005, at 4:24 PM, [EMAIL PROTECTED] wrote: So, you are writing a GUI app and you want some kind of open file dialog? Won't this depend on what toolkit you are using for your GUI? If you are using Tkinter (which should work on OS X, I think), try: import tkFileDialog f = tkFileDialog.askopenfilename() Check dir(tkFileDialog) for other functions. But other GUI toolkits will have their own functions. I my case the gui will be comprised of html and javascript, talking to python through system calls. I basically want to know if there's an equivalent of the "webbrowser()" module (which launches my browser) for file dialogs. This is what EasyDialogs should do, but does not. -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Launching a file browser
I looked over the global module index and the closest thing I could find relating to my os (osx) was EasyDialogs, which has a few functions pertaining to this, "AskFileForOpen()" being one. Calling any function within EasyDialogs however yields an Apple Event error: AE.AEInteractWithUser(5000) MacOS.Error: (-1713, 'no user interaction is allowed') I get the impression some of these "Mac" modules are more relevant to os 9 than 10(which is Unix), so maybe EasyDialogs is not the right choice here. Any suggestions are appreciated. -MH___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Javascript
On Mar 25, 2005, at 4:53 PM, Alan Gauld wrote: intrigued by Dashboard, which will be in the next OSX release. It allows you to create "widgets" which are essentially little html pages There is an API for Dashboard and I'm pretty sure MacPython will support it - it covers most of the cocoa type stuff. You might be better checking out the Apple developer site for the Dashboard hooks and loooking at MacPythons options. Alan G. Alan, thanks for pointing me towards a few good approaches to look at. Going through some of the developer information I've come across mention of JS extensions which allow for system calls within a JS function, which should pretty much do what I want. Thanks, -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Javascript
Danny, great reply. I have looked a bit at pyObjC, and it does indeed look cool. I was however hoping to bypass that route altogether and go for the simplicity (I thought) that came with the html/js route. Perhaps a cocoa bundle is the only way to get what I'm after. Thanks, -MH On Mar 25, 2005, at 1:40 PM, Danny Yoo wrote: Yikes, that sounds pretty hairy. Maybe this kind of thing is not as straight forward as anticipated. Why HTML you say? Well I've been intrigued by Dashboard, which will be in the next OSX release. It allows you to create "widgets" which are essentially little html pages that do things. This got me thinking how I'd like to tie a small Python script I wrote into an html front end (ideally becoming a widget). It's looking like this may be trickier than anticipated. In any case, thanks. Hi Mike, Interesting! You probably know about this already, but PyObjC allows you to write Mac OS X Cocoa applications in Python: http://pyobjc.sourceforge.net/ and this is a well tested bridge to make Python classes integrate into Cocoa applications. For example, http://www.pycs.net/bbum/2004/12/10/#200412101 mentions the use of PyObjC to make a Mac OS X screensaver. So it appears to embed very well. According to the documentation from Apple's Dashboard developer site, we can embed Cocoa bundles into Javascript (there's a brief mention of it under "Custom Code Plug-ins": http://developer.apple.com/macosx/tiger/dashboard.html So in theory, we should be able to inject a Pythonified Cocoa bundle into Dashboard, but then again, I've never tried this before. *grin* I haven't dived into doing Mac OS X development yet, but perhaps someone on the PyObjC list might be able to cook up a quick-and-dirty example of this for you. Try asking on their list and see if you get some useful responses: http://lists.sourceforge.net/lists/listinfo/pyobjc-dev Best of wishes to you! -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Javascript
On Mar 25, 2005, at 1:00 PM, Ryan Davis wrote: Ok, that explains a lot, but I don't know of any easy way to do have javascript talk to python. I can think of some horrible ways to do it, though. 1. Make a python web service running locally, and build up SOAP calls or HTTP posts to it. (same as I suggested earlier) 2. Use XUL and pyXPCOM to make a firefox extension that talks to python. This is probably much more of a pain in the ass than you want to do, but that's the only way I know of to directly call python functions from javascript. 3. Look into web framework Zope, that might have some of this plumbing done already. 4. Check out Sajax, http://www.modernmethod.com/sajax/, a framework to automate javascript calling your server-side functions. It was made for PHP, but looks to have a python version as well. All of those but #2 require you to set up some kind of server. Is there a reason it has to be an HTML page? If not, making a GUI might be an alternative that sidesteps this altogether. Yikes, that sounds pretty hairy. Maybe this kind of thing is not as straight forward as anticipated. Why HTML you say? Well I've been intrigued by Dashboard, which will be in the next OSX release. It allows you to create "widgets" which are essentially little html pages that do things. This got me thinking how I'd like to tie a small Python script I wrote into an html front end (ideally becoming a widget). It's looking like this may be trickier than anticipated. In any case, thanks. -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Javascript
On Mar 25, 2005, at 12:41 PM, Alan Gauld wrote: If you are using WSH on Windows and have the Python active scripting installed then yes. Similarly if you use IE as web browser then it can be done in a web page too. I'm on OSX, and would be doing this through Safari most likely. -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Javascript
Ryan, I should clarify that what I'd like to do here is unrelated to the web. I'm actually just interested in using a local html page as a simple gui to launch python calls. So a JS event handler, say a button click, would then call a JS function which inside of it would call a Python function while handing it arguments (say a path that the JS queried from a field in the html page.) That kind of thing. It seems like it should be possible, and hopefully easy, but I have no experience in calling Python functions from other languages so I'm just looking for some input on that. Thanks, -MH On Mar 25, 2005, at 12:01 PM, Ryan Davis wrote: Depends on your environment. If your js is on a webpage, you can have it make http calls to a python web service. Look for articles on XMLHttpRequest in javascript to see some examples. I don't know how else that could be done, but I imagine there are other ways. Thanks, Ryan -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Mike Hall Sent: Friday, March 25, 2005 2:18 PM To: tutor@python.org Subject: [Tutor] Python and Javascript I'm curious on whether or not JavaScript and Python can talk to each other. Specifically, can a python function be called from within a JS function? Admittedly this is probably more of a JavaScript than Python question, but I'd love to know if anyone can at least point me in a direction to research this. -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python and Javascript
I'm curious on whether or not JavaScript and Python can talk to each other. Specifically, can a python function be called from within a JS function? Admittedly this is probably more of a JavaScript than Python question, but I'd love to know if anyone can at least point me in a direction to research this. -MH ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
On Mar 23, 2005, at 3:17 AM, Kent Johnson wrote: Anyway, Mike, it seems clear that your file has line endings in it which are not consistent with the default for your OS. If reading with universal newlines doesn't solve the problem, please let us know what OS you are running under and give more details about the data. Kent, reading universal did indeed solve my problem, but for the record I'm on OSX, and was reading from a standard plain text file. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
On Mar 23, 2005, at 12:53 AM, Alan Gauld wrote: Typically what happens is you view the file in an application that autrowraps long lines so it looks like multiple lines on screen but in fact it is one long line in the file. In that case Python will only see the single long line. I'm using subEthaEdit, which will autowrap long lines, but it also displays line numbers, so there's no doubt where one begins and ends :) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] .readlines() condensing multiple lines
Liam, "rU" worked like a charm. My previous syntax where the lines were condensing was: fOpen = file(f, "r") fRead = fTmp.readlines() In this instance the size of fRead would not correspond to my line numbers. With fOpen = file(f, "rU") it now does. Thanks :) On Mar 22, 2005, at 7:15 PM, Liam Clarke wrote: From the docs - In addition to the standard fopen() values mode may be 'U' or 'rU'. If Python is built with universal newline support (the default) the file is opened as a text file, but lines may be terminated by any of '\n', the Unix end-of-line convention, '\r', the Macintosh convention or '\r\n', the Windows convention. All of these external representations are seen as '\n' by the Python program. If Python is built without universal newline support mode 'U' is the same as normal text mode. Note that file objects so opened also have an attribute called newlines which has a value of None (if no newlines have yet been seen), '\n', '\r', '\r\n', or a tuple containing all the newline types seen. So, try x = file(myFile, 'rU').readlines() Or try: x = file(myFile, 'rU') for line in x: #do stuff Let us know how that goes. Regards, Liam Clarke PS Worse come to worse, you could always do - x = file(myFile, 'r').read() listX = x.split('\r') On Tue, 22 Mar 2005 17:10:43 -0800, Mike Hall <[EMAIL PROTECTED]> wrote: Unless I'm mistaken .readlines() is supposed to return a list, where each index is a line from the file that was handed to it. Well I'm finding that it's putting more than one line of my file into a single list entry, and separating them with \r. Surely there's a way to have a one to one correlation between len(list) and the lines in the file the list was derived from...? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] .readlines() condensing multiple lines
Unless I'm mistaken .readlines() is supposed to return a list, where each index is a line from the file that was handed to it. Well I'm finding that it's putting more than one line of my file into a single list entry, and separating them with \r. Surely there's a way to have a one to one correlation between len(list) and the lines in the file the list was derived from...? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stopping greedy matches
On Mar 18, 2005, at 1:02 PM, Christopher Weimann wrote: On 03/18/2005-10:35AM, Mike Hall wrote: A caret as the first charachter in a class is a negation. So this [^\s]+ means match one or more of any char that isn't whitespace. Ok, so the context of metas change within a class. That makes sense, but I'm unclear on the discrepancy below. The ^ means begining of line EXCEPT inside a charachter class. There it means NOT for the entire class and it only means that if it is the very first charachter. I suppose you could consider that the there are two separate types of char classes. One is started with [ and the other is started with [^. Got it, thanks. That would be \ Here's where I'm confused. From the Python docs: Special characters are not active inside sets. For example, [akm$] will match any of the characters "a", "k", "m", or "$" And the next paragraphs says... You can match the characters not within a range by complementing the set. This is indicated by including a "^" as the first character of the class; "^" elsewhere will simply match the "^" character. For example, [^5] will match any character except "5". The sad thing is I have read that paragraph before (but obviously hadn't absorbed the significance). I'm new to this, it'll sink in. Thanks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stopping greedy matches
On Mar 18, 2005, at 9:27 AM, Christopher Weimann wrote: On 03/17/2005-10:15AM, Mike Hall wrote: Very nice sir. I'm interested in what you're doing here with the caret metacharacter. For one thing, why enclose it and the whitespace flag within a character class? A caret as the first charachter in a class is a negation. So this [^\s]+ means match one or more of any char that isn't whitespace. Ok, so the context of metas change within a class. That makes sense, but I'm unclear on the discrepancy below. Does this not traditionally mean you want to strip a metacharacter of it's special meaning? That would be \ Here's where I'm confused. From the Python docs: Special characters are not active inside sets. For example, [akm$] will match any of the characters "a", "k", "m", or "$"___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stopping greedy matches
On Mar 17, 2005, at 11:11 AM, Kent Johnson wrote: The first one matches the space after 'in'. Without it the .+? will match the single space, then \b matches the *start* of the next word. I think I understand. Basically the first dot advances the pattern forward in order to perform a non-greedy match on the following word.(?) Very nice. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stopping greedy matches
I don't have that script on my system, but I may put pythoncard on here and run it through that: http://pythoncard.sourceforge.net/samples/redemo.html Although regexPlor looks like it has the same functionality, so I may just go with that. Thanks. On Mar 17, 2005, at 1:31 AM, Michael Dunn wrote: As Kent said, redemo.py is a script that you run (e.g. from the command line), rather than something to import into the python interpretor. On my OSX machine it's located in the directory: /Applications/MacPython-2.3/Extras/Tools/scripts Cheers, Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stopping greedy matches
On Mar 16, 2005, at 8:32 PM, Kent Johnson wrote: "in (.*?)\b" will match against "in " because you use .* which will match an empty string. Try "in (.+?)\b" (or "(?<=\bin)..+?\b" )to require one character after the space. Another working example, excellent. I'm not too clear on why the back to back ".." in "(?<=\bin)..+?\b" )" makes the regex work, but it does. You can't import it, you have to run it from the command line. I don't know if it is installed under Mac OSX though. You might be interested in RegexPlor: http://python.net/~gherman/RegexPlor.html RegexPlor looks fantastic, will be downloading. Thanks. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stopping greedy matches
Very nice sir. I'm interested in what you're doing here with the caret metacharacter. For one thing, why enclose it and the whitespace flag within a character class? Does this not traditionally mean you want to strip a metacharacter of it's special meaning? On Mar 16, 2005, at 8:00 PM, Christopher Weimann wrote: On 03/16/2005-12:12PM, Mike Hall wrote: I'm having trouble getting re to stop matching after it's consumed what I want it to. Using this string as an example, the goal is to match "CAPS": s = "only the word in CAPS should be matched" jet% python Python 2.4 (#2, Jan 5 2005, 15:59:52) [GCC 2.95.4 20020320 [FreeBSD]] on freebsd4 Type "help", "copyright", "credits" or "license" for more information. import re s = "only the word in CAPS should be matched" x=re.compile(r"\bin ([^\s]+)") x.findall(s) ['CAPS'] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stopping greedy matches
On Mar 16, 2005, at 5:32 PM, Sean Perry wrote: I know this does not directly help, but I have never successfully used \b in my regexs. I always end up writing something like foo\s+bar or something more intense. I've had luck with the boundary flag in relation to lookbehinds. For example, if I wanted to only match after "int" (and not "print") (?<=\bint) seems to work fine. I'm a bit frustrated at not being able to find a simple way to have a search stop after eating up one word. You'd think the \b would do it, but nope. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] stopping greedy matches
Liam, "re.compile("in (.*?)\b")" will not find any match in the example string I provided. I have had little luck with these non-greedy matchers. I don't appear to have redemo.py on my system (on OSX), as an import returns an error. I will look into finding this module, thanks for pointing me towards it :) On Mar 16, 2005, at 2:36 PM, Liam Clarke wrote: x=re.compile(r"(?<=\bin).+\b") Try x = re.compile("in (.*?)\b") .*? is a non-greedy matcher I believe. Are you using python24/tools/scripts/redemo.py? Use that to test regexes. Regards, Liam Clarke On Wed, 16 Mar 2005 12:12:32 -0800, Mike Hall <[EMAIL PROTECTED]> wrote: I'm having trouble getting re to stop matching after it's consumed what I want it to. Using this string as an example, the goal is to match "CAPS": s = "only the word in CAPS should be matched" So let's say I want to specify when to begin my pattern by using a lookbehind: x = re.compile(r"(?<=\bin)") #this will simply match the spot in front of "in" So that's straight forward, but let's say I don't want to use a lookahead to specify the end of my pattern, I simply want it to stop after it has combed over the word following "in". I would expect this to work, but it doesn't: x=re.compile(r"(?<=\bin).+\b") #this will consume everything past "in" all the way to the end of the string In the above example I would think that the word boundary flag "\b" would indicate a stopping point. Is ".+\b" not saying, "keep matching characters until a word boundary has been reached"? Even stranger are the results I get from: x=re.compile(r"(?<=\bin).+\s") #keep matching characters until a whitespace has been reached(?) r = x.sub("[EMAIL PROTECTED]", s) print r only the word [EMAIL PROTECTED] For some reason there it's decided to consume three words instead of one. My question is simply this: after specifying a start point, how do I make a match stop after it has found one word, and one word only? As always, all help is appreciated. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] stopping greedy matches
I'm having trouble getting re to stop matching after it's consumed what I want it to. Using this string as an example, the goal is to match "CAPS": >>> s = "only the word in CAPS should be matched" So let's say I want to specify when to begin my pattern by using a lookbehind: >>> x = re.compile(r"(?<=\bin)") #this will simply match the spot in front of "in" So that's straight forward, but let's say I don't want to use a lookahead to specify the end of my pattern, I simply want it to stop after it has combed over the word following "in". I would expect this to work, but it doesn't: >>> x=re.compile(r"(?<=\bin).+\b") #this will consume everything past "in" all the way to the end of the string In the above example I would think that the word boundary flag "\b" would indicate a stopping point. Is ".+\b" not saying, "keep matching characters until a word boundary has been reached"? Even stranger are the results I get from: >>> x=re.compile(r"(?<=\bin).+\s") #keep matching characters until a whitespace has been reached(?) >>> r = x.sub("[EMAIL PROTECTED]", s) >>> print r only the word [EMAIL PROTECTED] For some reason there it's decided to consume three words instead of one. My question is simply this: after specifying a start point, how do I make a match stop after it has found one word, and one word only? As always, all help is appreciated. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression question
but yeah, it seems you're expecting it to examine the string as a whole. I guess I was, good point. On Mar 9, 2005, at 12:28 PM, Liam Clarke wrote: Actually, you should get that anyway... """ | Alternation, or the ``or'' operator. If A and B are regular expressions, A|B will match any string that matches either "A" or "B". | has very low precedence in order to make it work reasonably when you're alternating multi-character strings. Crow|Servo will match either "Crow" or "Servo", not "Cro", a "w" or an "S", and "ervo". """ So, for each letter in that string, it's checking to see if any letter matches 'A' or 'B' ... the engine steps through one character at a time. sorta like - for letter in s: if letter == 'A': #Do some string stuff elif letter == 'B': #do some string stuff i.e. k = ['A','B', 'C', 'B'] for i in range(len(k)): if k[i] == 'A' or k[i]=='B': k[i]==13 print k [13, 13, 'C', 13] You can limit substitutions using an optional argument, but yeah, it seems you're expecting it to examine the string as a whole. Check out the example here - http://www.amk.ca/python/howto/regex/ regex.html#SECTION00032 Also http://www.regular-expressions.info/alternation.html Regards, Liam Clarke On Thu, 10 Mar 2005 09:09:13 +1300, Liam Clarke <[EMAIL PROTECTED]> wrote: Hi Mike, Do you get the same results for a search pattern of 'A|B'? On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall <[EMAIL PROTECTED]> wrote: I'm having some strange results using the "or" operator. In every test I do I'm matching both sides of the "|" metacharacter, not one or the other as all documentation says it should be (the parser supposedly scans left to right, using the first match it finds and ignoring the rest). It should only go beyond the "|" if there was no match found before it, no? Correct me if I'm wrong, but your regex is saying "match dog, unless it's followed by cat. if it is followed by cat there is no match on this side of the "|" at which point we advance past it and look at the alternative expression which says to match in front of cat." However, if I run a .sub using your regex on a string contain both dog and cat, both will be replaced. A simple example will show what I mean: import re x = re.compile(r"(A) | (B)") s = "X R A Y B E" r = x.sub("13", s) print r X R 13Y13 E ...so unless I'm understanding it wrong, "B" is supposed to be ignored if "A" is matched, yet I get both matched. I get the same result if I put "A" and "B" within the same group. On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote: Regular expressions are a little evil at times; here's what I think you're thinking of: ### import re pattern = re.compile(r"""dog(?!cat) ...| (?<=dogcat)""", re.VERBOSE) pattern.match('dogman').start() 0 pattern.search('dogcatcher').start() Hi Mike, Gaaah, bad copy-and-paste. The example with 'dogcatcher' actually does come up with a result: ### pattern.search('dogcatcher').start() 6 ### Sorry about that! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression question
But I only want to ignore "B" if "A" is a match. If "A" is not a match, I'd like it to advance on to "B". On Mar 9, 2005, at 12:07 PM, Marcos Mendonça wrote: Hi Not and regexp expert. But it seems to me that if you want to ignora "B" then it should be (A) | (^B) Hope it helps! On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall <[EMAIL PROTECTED]> wrote: I'm having some strange results using the "or" operator. In every test I do I'm matching both sides of the "|" metacharacter, not one or the other as all documentation says it should be (the parser supposedly scans left to right, using the first match it finds and ignoring the rest). It should only go beyond the "|" if there was no match found before it, no? Correct me if I'm wrong, but your regex is saying "match dog, unless it's followed by cat. if it is followed by cat there is no match on this side of the "|" at which point we advance past it and look at the alternative expression which says to match in front of cat." However, if I run a .sub using your regex on a string contain both dog and cat, both will be replaced. A simple example will show what I mean: import re x = re.compile(r"(A) | (B)") s = "X R A Y B E" r = x.sub("13", s) print r X R 13Y13 E ...so unless I'm understanding it wrong, "B" is supposed to be ignored if "A" is matched, yet I get both matched. I get the same result if I put "A" and "B" within the same group. On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote: Regular expressions are a little evil at times; here's what I think you're thinking of: ### import re pattern = re.compile(r"""dog(?!cat) ...| (?<=dogcat)""", re.VERBOSE) pattern.match('dogman').start() 0 pattern.search('dogcatcher').start() Hi Mike, Gaaah, bad copy-and-paste. The example with 'dogcatcher' actually does come up with a result: ### pattern.search('dogcatcher').start() 6 ### Sorry about that! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression question
Indeed I do: >>> import re >>> x = re.compile('A|B') >>> s = " Q A R B C" >>> r = x.sub("13", s) >>> print r Q 13 R 13 C On Mar 9, 2005, at 12:09 PM, Liam Clarke wrote: Hi Mike, Do you get the same results for a search pattern of 'A|B'? On Wed, 9 Mar 2005 11:11:57 -0800, Mike Hall <[EMAIL PROTECTED]> wrote: I'm having some strange results using the "or" operator. In every test I do I'm matching both sides of the "|" metacharacter, not one or the other as all documentation says it should be (the parser supposedly scans left to right, using the first match it finds and ignoring the rest). It should only go beyond the "|" if there was no match found before it, no? Correct me if I'm wrong, but your regex is saying "match dog, unless it's followed by cat. if it is followed by cat there is no match on this side of the "|" at which point we advance past it and look at the alternative expression which says to match in front of cat." However, if I run a .sub using your regex on a string contain both dog and cat, both will be replaced. A simple example will show what I mean: import re x = re.compile(r"(A) | (B)") s = "X R A Y B E" r = x.sub("13", s) print r X R 13Y13 E ...so unless I'm understanding it wrong, "B" is supposed to be ignored if "A" is matched, yet I get both matched. I get the same result if I put "A" and "B" within the same group. On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote: Regular expressions are a little evil at times; here's what I think you're thinking of: ### import re pattern = re.compile(r"""dog(?!cat) ...| (?<=dogcat)""", re.VERBOSE) pattern.match('dogman').start() 0 pattern.search('dogcatcher').start() Hi Mike, Gaaah, bad copy-and-paste. The example with 'dogcatcher' actually does come up with a result: ### pattern.search('dogcatcher').start() 6 ### Sorry about that! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression question
I'm having some strange results using the "or" operator. In every test I do I'm matching both sides of the "|" metacharacter, not one or the other as all documentation says it should be (the parser supposedly scans left to right, using the first match it finds and ignoring the rest). It should only go beyond the "|" if there was no match found before it, no? Correct me if I'm wrong, but your regex is saying "match dog, unless it's followed by cat. if it is followed by cat there is no match on this side of the "|" at which point we advance past it and look at the alternative expression which says to match in front of cat." However, if I run a .sub using your regex on a string contain both dog and cat, both will be replaced. A simple example will show what I mean: >>> import re >>> x = re.compile(r"(A) | (B)") >>> s = "X R A Y B E" >>> r = x.sub("13", s) >>> print r X R 13Y13 E ...so unless I'm understanding it wrong, "B" is supposed to be ignored if "A" is matched, yet I get both matched. I get the same result if I put "A" and "B" within the same group. On Mar 8, 2005, at 6:47 PM, Danny Yoo wrote: Regular expressions are a little evil at times; here's what I think you're thinking of: ### import re pattern = re.compile(r"""dog(?!cat) ...| (?<=dogcat)""", re.VERBOSE) pattern.match('dogman').start() 0 pattern.search('dogcatcher').start() Hi Mike, Gaaah, bad copy-and-paste. The example with 'dogcatcher' actually does come up with a result: ### pattern.search('dogcatcher').start() 6 ### Sorry about that! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression question
Sorry, my last reply crossed this one (and yes, I forgot again to CC the list). I'm experimenting now with your use of the "or" operator( "|") between two expressions, thanks. On Mar 8, 2005, at 6:42 PM, Danny Yoo wrote: On Tue, 8 Mar 2005, Mike Hall wrote: Yes, my existing regex is using a look behind assertion: (?<=dog) ...it's also checking the existence of "Cat": (?!Cat) ...what I'm stuck on is how to essentially use a lookbehind on "Cat", but only if it exists. Hi Mike, [Note: Please do a reply-to-all next time, so that everyone can help you.] Regular expressions are a little evil at times; here's what I think you're thinking of: ### import re pattern = re.compile(r"""dog(?!cat) ...| (?<=dogcat)""", re.VERBOSE) pattern.match('dogman').start() 0 pattern.search('dogcatcher').start() pattern.search('dogman').start() 0 pattern.search('catwoman') ### but I can't be sure without seeing some of the examples you'd like the regular expression to match against. Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression question
This will match the position in front of "dog": (?<=dog) This will match the position in front of "cat": (?<=cat) This will not match in front of "dog" if "dog" is followed by "cat": (?<=dog)\b (?!cat) Now my question is how to get this: (?<=cat) ...but ONLY if "cat" is following "dog." If "dog" does not have "cat" following it, then I simply want this: (?<=dog) ...if that makes sense :) thanks. On Mar 8, 2005, at 6:05 PM, Danny Yoo wrote: On Tue, 8 Mar 2005, Mike Hall wrote: I'd like to get a match for a position in a string preceded by a specified word (let's call it "Dog"), unless that spot in the string (after "Dog") is directly followed by a specific word(let's say "Cat"), in which case I want my match to occur directly after "Cat", and not "Dog." Hi Mike, You may want to look at "lookahead" assertions. These are patterns of the form '(?=...)' or '(?!...). The documentation mentions them here: http://www.python.org/doc/lib/re-syntax.html and AMK's excellent "Regular Expression HOWTO" covers how one might use them: http://www.amk.ca/python/howto/regex/ regex.html#SECTION00054 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regular expression question
First, thanks for the response. Using your re: my_re = re.compile(r'(dog)(cat)?') ...I seem to simply be matching the pattern "Dog". Example: >>> str1 = "The dog chased the car" >>> str2 = "The dog cat parade was under way" >>> x1 = re.compile(r'(dog)(cat)?') >>> rep1 = x1.sub("REPLACE", str1) >>> rep2 = x2.sub("REPLACE", str2) >>> print rep1 The REPLACE chased the car >>> print rep2 The REPLACE cat parade was under way ...what I'm looking for is a match for the position in front of "Cat", should it exist. On Mar 8, 2005, at 5:54 PM, Sean Perry wrote: Mike Hall wrote: I'd like to get a match for a position in a string preceded by a specified word (let's call it "Dog"), unless that spot in the string (after "Dog") is directly followed by a specific word(let's say "Cat"), in which case I want my match to occur directly after "Cat", and not "Dog." I can easily get the spot after "Dog," and I can also get it to ignore this spot if "Dog" is followed by "Cat." But what I'm having trouble with is how to match the spot after "Cat" if this word does indeed exist in the string. . >>> import re . >>> my_re = re.compile(r'(dog)(cat)?') # the ? means "find one or zero of these, in other words cat is optional. . >>> m = my_re.search("This is a nice dog is it not?") . >>> dir(m) ['__copy__', '__deepcopy__', 'end', 'expand', 'group', 'groupdict', 'groups', 'span', 'start'] . >>> m.span() (15, 18) . >>> m = my_re.search("This is a nice dogcat is it not?") . >>> m.span() (15, 21) If m is None then no match was found. span returns the locations in the string where the match occured. So in the dogcat sentence the last char is 21. . >>> "This is a nice dogcat is it not?"[21:] ' is it not?' Hope that helps. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] regular expression question
I'd like to get a match for a position in a string preceded by a specified word (let's call it "Dog"), unless that spot in the string (after "Dog") is directly followed by a specific word(let's say "Cat"), in which case I want my match to occur directly after "Cat", and not "Dog." I can easily get the spot after "Dog," and I can also get it to ignore this spot if "Dog" is followed by "Cat." But what I'm having trouble with is how to match the spot after "Cat" if this word does indeed exist in the string.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] gensuitemodule?
I'm seeing it used in a Python/Applescript tutorial, though am unclear on it's exact purpose or usage. Can someone fill me in? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newbie OSX module path question
Ok, I've got it working. The environment.plist file wants a path beginning with /Users, not /Local_HD. So simple! Thanks everyone. On Feb 14, 2005, at 6:26 PM, David Rock wrote: * Mike Hall <[EMAIL PROTECTED]> [2005-02-14 18:22]: Hm, so if I import glob, and then execute this line: print glob.glob('/Local_HD/Users/mike/Documents/pythonModules/*.py') I simply get brackets returned: [] ...not sure what this means. Thanks again. It means it didn't find anything that matches that pattern, which suggests that the directory does not contain *.py files. That might be a problem. ;-) -- David Rock [EMAIL PROTECTED] ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newbie OSX module path question
Hm, so if I import glob, and then execute this line: print glob.glob('/Local_HD/Users/mike/Documents/pythonModules/*.py') I simply get brackets returned: [] ...not sure what this means. Thanks again. On Feb 14, 2005, at 5:41 PM, Danny Yoo wrote: On Mon, 14 Feb 2005, Mike Hall wrote: Can you show us what your sys.path looks like? Just do a cut-and-paste so we can quickly validate it for you. Thanks for the response. Here's a paste of what sys.path returns. The first listing is the path inside of environment.plist: ['', '/Local_HD/Users/mike/Documents/pythonModules', '/Users/tempmike/Documents/pythonModules', '/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python23.zip', '/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3', '/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ python2.3/site-packages'] Can you show us the exact thing you're typing, as well as the literal error that Python shows? I will attempt to import using 'import' followed by file name. Example: import module1 The error returned will be: ImportError: No module named module1 [Meta: Please keep python-tutor in CC, so that all of us on the mailing list can help you.] Hi Mike, Ok, can you do this at the Python prompt? ### import glob print glob.glob('/Local_HD/Users/mike/Documents/pythonModules/*.py') ### Copy and paste the output you see. If things go wrong, then we will have a good focus point to debug the problem. But if things go right --- if you see a bunch of Python module files --- then I will be stuck and will have to think of something else. *grin* Do you have problems doing an import if your modules's directory is the current working directory? Funny you should mention that. After posting to this list, I tried cd'ing over to the dir I created for modules, and then launched Python. My modules can indeed be imported using this method. But I'm still curious as to why I cannot get a successful import (when I'm not within my work dir) when the path is visibly defined within the sys.path variable? Thanks very much. Ok, so there appears to be nothing wrong with the modules themselves or with importing them when they're in the current working directory. We should then focus on sys.path itself, since that's the mechanism Python uses to lookup modules that aren't in the current directory. For the moment, I'll assume that there's something funky with the pathname. As mentioned earlier, it could be as subtle as a case-sensitivity issue. The glob statement above will help us check to see if Python can see those files, at least. Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] newbie OSX module path question
I'm on OS X, and I cannot get Python to import modules I've saved. I have created the the environment.plist file and appended it with my desired module path. If I print sys.path from the interpreter, my new path does indeed show up as the first listing, yet any attempt at importing modules from this directory fails with ImportError. What am I doing wrong? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor