[Tutor] RegEx query

2005-12-17 Thread Liam Clarke
Hi all,

Using Beautiful Soup and regexes.. I've noticed that all the examples
used regexes like so - anchors = parseTree.fetch("a",
{"href":re.compile("pattern")} )  instead of precompiling the pattern.

Myself, I have the following code -
>>> z = []
>>> x = q.findNext("a", {"href":re.compile(".*?thread/[0-9]*?/.*",
re.IGNORECASE)})

>>> while x:
... num = x.findNext("td", "tableColA")
... h = (x.contents[0],x.attrMap["href"],num.contents[0])
... z.append(h)
... x = x.findNext("a",{"href":re.compile(".*?thread/[0-9]*?/.*",
re.IGNORECASE)})
...

This gives me a correct set of results. However, using the following -

>>> z = []
>>> pattern = re.compile(".*?thread/[0-9]*?/.*", re.IGNORECASE)
>>> x = q.findNext("a", {"href":pattern)})

>>> while x:
... num = x.findNext("td", "tableColA")
... h = (x.contents[0],x.attrMap["href"],num.contents[0])
... z.append(h)
... x = x.findNext("a",{"href":pattern} )

will only return the first found tag.

Is the regex only evaluated once or similar?

(Also any pointers on how to get negative lookahead matching working
would be great.
the regex (/thread/[0-9]*)(?!\/) still matches "/thread/28606/" and
I'd assumed it wouldn't.

Regards,

Liam Clarke
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] RegEx query

2005-12-17 Thread Kent Johnson
Liam Clarke wrote:
> Hi all,
> 
> Using Beautiful Soup and regexes.. I've noticed that all the examples
> used regexes like so - anchors = parseTree.fetch("a",
> {"href":re.compile("pattern")} )  instead of precompiling the pattern.
> 
> Myself, I have the following code -
> 
z = []
x = q.findNext("a", {"href":re.compile(".*?thread/[0-9]*?/.*",
> 
> re.IGNORECASE)})
> 
> 
while x:
> 
> ...   num = x.findNext("td", "tableColA")
> ...   h = (x.contents[0],x.attrMap["href"],num.contents[0])
> ...   z.append(h)
> ...   x = x.findNext("a",{"href":re.compile(".*?thread/[0-9]*?/.*",
> re.IGNORECASE)})
> ...
> 
> This gives me a correct set of results. However, using the following -
> 
> 
z = []
pattern = re.compile(".*?thread/[0-9]*?/.*", re.IGNORECASE)
x = q.findNext("a", {"href":pattern)})
> 
> 
while x:
> 
> ...   num = x.findNext("td", "tableColA")
> ...   h = (x.contents[0],x.attrMap["href"],num.contents[0])
> ...   z.append(h)
> ...   x = x.findNext("a",{"href":pattern} )
> 
> will only return the first found tag.
> 
> Is the regex only evaluated once or similar?

I don't know why there should be any difference unless BS modifies the compiled 
regex 
object and for some reason needs a fresh one each time. That would be odd and I 
don't see 
it in the source code.

The code above has a syntax error (extra paren in the first findNext() call) - 
can you 
post the exact non-working code?
> 
> (Also any pointers on how to get negative lookahead matching working
> would be great.
> the regex (/thread/[0-9]*)(?!\/) still matches "/thread/28606/" and
> I'd assumed it wouldn't.

Putting these expressions into Regex Demo is enlightening - the regex matches 
against 
"/thread/2860" - in other words the "not /" is matching against the 6.

You don't give an example of what you do want to match so it's hard to know 
what a better 
solution is. Some possibilities
- match anything except a digit or a slash - [^0-9/]
- match the end of the string - $
- both of the above - ([^0-9/]|$)

Kent

> 
> Regards,
> 
> Liam Clarke
> ___
> 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] How do I fix this Invalid Mode?

2005-12-17 Thread Nathan Pinno



Here is the latest 
error:
The Currency Exchange 
ProgramBy Nathan Pinno
 
Traceback (most recent 
call last):  File "D:\Python24\exchange.py", line 27, in 
-toplevel-    store = open('exch.txt', 'b')#loadIOError: 
invalid mode: b
 
and the latest 
code:
import picklerates 
= {'can_us' : 0.80276, 
'us_can' : 1.245702, 
'can_euro' : 1.488707, 
'euro_can' : 0.671724}
 
def 
menu():    print "1. Change Canadian currency into 
American."    print "2. Change American currency into 
Canadian."    print "3. Change Canadian currency into 
Euros."    print "4. Change Euros into Canadian 
currency."    print "5. Update exchange 
rates."    print "9. Save and Exit"
 
def 
exchange_update():    print "1. Update Canadian to US 
rate."    print "2. Update US to Canadian 
rate."    print "3. Update Canadian to Euro 
rate."    print "4. Update Euro to Canadian 
update."    print "5. Main menu"
 
def 
menu_choice():    return int(raw_input("Which option? 
"))
 
print "The Currency 
Exchange Program"print "By Nathan Pinno"store = open('exch.txt', 
'b')#loadexch = pickle.load(store)store.close()while 
1:    menu()    menu_option = 
menu_choice()    if menu_option == 
1:    can = float(raw_input("Canadian 
$"))    print "US 
$",can*rates['can_us']    elif menu_option == 
2:    us = float(raw_input("US 
$"))    print "CAN 
$",us*rates['us_can']    elif menu_option == 
3:    can = float(raw_input("CAN 
$"))    print 
"Euros",can*rates['can_euro']    elif menu_option == 
4:    euro = 
float(raw_input("Euros"))    print 
"CAN $",euro*rates['euro_can']    elif menu_option == 
5:    while 
1:    
exchange_update()    
sub = 
menu_choice()    
if sub == 
1:    
new_can = float(raw_input("New CAN-US Exchange rate: 
"))    
rates['can_us'] = 
new_can    
print "Exchange rate successfully 
updated!"    
elif sub == 
2:    
new_us = float(raw_input("New US-CAN Exchange rate: 
"))    
rates['us_can'] = 
new_us    
print "Exchange rate successfully 
updated!"    
elif sub == 
3:    
new_cxr = float(raw_input("New CAN-Euro Exchange rate: 
"))    
rates['can_euro'] = 
new_cxr    
print "Exchange rate successfully 
updated!"    
elif sub == 
4:    
new_euro = float(raw_input("New Euro-CAN Exchange rate: 
"))    
rates['euro_can'] = 
new_euro    
print "Exchange rate successfully 
updated!"    
elif sub == 
5:    
break    elif menu_option == 
9:    store = open("exch.txt", 'wb') 
#save    pickle.dump(exch, 
store)    
store.close()    breakprint 
"Goodbye."
 
How do I fix it this 
time?
 
Thanks,
Nathan Pinno,

MSN Messenger: [EMAIL PROTECTED]
Yahoo! Messenger: spam_swatter31
AIM: f3mighty
ICQ: 199020705  
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How do I fix this Invalid Mode?

2005-12-17 Thread Dan Lowe
On Dec 17, 2005, at 2:00 PM, Nathan Pinno wrote: Here is the latest error: The Currency Exchange ProgramBy Nathan Pinno   Traceback (most recent call last):  File "D:\Python24\exchange.py", line 27, in -toplevel-    store = open('exch.txt', 'b')#loadIOError: invalid mode: b[snip...] store = open('exch.txt', 'b')#loadexch = pickle.load(store)store.close()It looks like what you are trying to do is read exch.txt as a binary file. The problem is that 'b' by itself is not a valid mode; it's a modifier to one of the other modes (r, w or a).So to read a file in binary mode, what you want to do is:	store = open('exch.txt', 'rb')Using your own code as an example, see how you implemented the save feature (option 9). That open() correctly uses the 'wb' flag to write the file in binary mode.    elif menu_option == 9:    store = open("exch.txt", 'wb') #save    pickle.dump(exch, store)    store.close()    break -dan-- Black holes are where God divided by zero.  -Steven Wright ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] mysterious object

2005-12-17 Thread Christopher Spears
I'm working on Exercise 4 from Part 4 from Learning
Python.  I'm trying to write a function using **args. 
I want to create a function that adds its arguments
together.  Here is what I have written:

def adder(**args):
for x in args.keys():
print x

print adder()
print "---"
print adder(a=5)
print "---"
print adder(a=5,b=6)
print "---"
print adder(a=5,b=6,c=7)
print "---"
print adder(ugly=7, good=6, bad=5)
print "---"


Here is my output:

None
---
a
None
---
a
b
None
---
a
c
b
None
---
ugly
bad
good
None
---

Where do the None objects come from?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mysterious object

2005-12-17 Thread Brian van den Broek
Christopher Spears said unto the world upon 2005-12-17 17:42:
> I'm working on Exercise 4 from Part 4 from Learning
> Python.  I'm trying to write a function using **args. 
> I want to create a function that adds its arguments
> together.  Here is what I have written:
> 
> def adder(**args):
> for x in args.keys():
> print x
> 
> print adder()
> print "---"
> print adder(a=5)
> print "---"



> 
> 
> Here is my output:
> 
> None
> ---
> a
> None
> ---

Hi Christopher,

all functions that terminate return a value, even if they don't have 
an explicit return statement. If they either have a bare "return" or 
no return at all, then they return None. You've put prints both in the 
functions and at the function calls. So, each time you've written 
print adder(), you've asked Python to print the return value of adder 
-- which is None.

 >>> def Noner():
return

 >>> print Noner()
None
 >>>

Does that clear it up?

Best,

Brian vdB

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] mysterious object

2005-12-17 Thread Alan Gauld
> def adder(**args):
>for x in args.keys():
>print x

A function which doesn't return a value returns None by default.

> print adder()

You are asking Python to print the return value of the function,
which is None.

As a general rule its better to do the printing outside of the function and
just do the calculation bit inside, this makes the function much easier to
reuse in future.

HTH

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] design advice for function

2005-12-17 Thread Christopher Spears
I got my function to work!  It takes arguments and
adds them:

def adder(**args):
argsList = args.values()
sum = argsList[0]
for x in argsList[1:]:
sum = sum + x
return sum

print adder()
print "---"
print adder(a=5)
print "---"
print adder(a=5,b=6)
print "---"
print adder(a=5,b=6,c=7)
print "---"
print adder(ugly=7, good=6, bad=5)
print "---"

However, if I run the above code.  I get an error:

Traceback (most recent call last):
  File "C:\Documents and Settings\Christopher
Spears\My
Documents\programming\PythonScripts\Part4\Ex04\adder.py",
line 8, in -toplevel-
print adder()
  File "C:\Documents and Settings\Christopher
Spears\My
Documents\programming\PythonScripts\Part4\Ex04\adder.py",
line 3, in adder
sum = argsList[0]
IndexError: list index out of range

This is caused by the line: print adder().  Obviously
if adder() doesn't receive any arguments, it can't
build the lists resulting in an IndexError.  What is
the best way to solve this?  Should I write some
syntax into the function to check for arguments? 
Should I just write a seperate function to check for arguments?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor