Re: [Tutor] design advice for function

2005-12-19 Thread Brian van den Broek
Danny Yoo said unto the world upon 2005-12-18 15:19:
  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.

Right.
 
 
 Hello!
 
 Just wanted to clarify the situation: argsList ends up being the empty
 list, which is a perfectly good value:

snip

Danny is, of course, perfectly correct. Apologies to the op for the 
imprecision.

Once again, I vow never to post after 3:00. Oh, wait, it is 3:23!

disappears in a puff of logic

Brian vdB

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


Re: [Tutor] RegEx query

2005-12-19 Thread Liam Clarke
Hi Kent,

I apologise for the not overly helpful initial post.

I had six possible uris to deal with -

/thread/28742/
/thread/28742/?s=1291819247219837219837129
/thread/28742/5/
/thread/28742/5/?s=1291819247219837219837129
/thread/28742/?goto=lastpost
/thread/28742/?s=1291819247219837219837129goto=lastpost

The only one I wanted to match was the first two.

My initial pattern /thread/[0-9]*?/(\?s\=.*)?(?!lastpost)$

matched the first two and the last in redemo.py (which I've got
stashed as a py2exe bundle, should I ever find myself sans Python but
having to use regexes).

I managed to sort it by using

/thread
/[0-9]*?/
(\?s\=\w*)?$

The s avoids the fourth possibility, and the \w precludes the  in the last uri.

But, circumventing the problem irks me no end, as I haven't fixed what
I was doing wrong, which means I'll probably do it again, and avoiding
problems instead of resolving them feels too much like programming for
the Win32 api to me.
(Where removing a service from the service database doesn't actually
remove the service from the service database until you open and close
a handle to the service database a second time...)

So yes, any advice on how to use negative lookaheads would be great. I
get the feeling it was the .* before it.

As for my problem with BeautifulSoup, I'm not sure what was happening
there. It was happening in interactive console only, and I can't
replicate it today, which suggests to me that I've engaged email
before brain again.

I do like BeautifulSoup, however. Although people keep telling about
some XPath programme that's better, apparently, I like BeautifulSoup,
it works.

Regards,

Liam Clarke

On 12/18/05, Kent Johnson [EMAIL PROTECTED] wrote:
 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 maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Accessing next and previous items during iteration

2005-12-19 Thread Ed Singleton
On 18/12/05, Kent Johnson [EMAIL PROTECTED] wrote:
 Ed Singleton wrote:
  Is it possible to access the next and previous items during an iteration?

 This just came up on c.l.python. Bengt Richter has a nice generator-based 
 solution.
 http://groups.google.com/group/comp.lang.python/browse_thread/thread/2e4533f108fbf172/90d87c91dac844d3?hl=en#90d87c91dac844d3

That's perfect!

It's a lovely piece of code as well.  Obvious once you've seen it, but
you wouldn't have thought of it before hand.

Thanks

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


Re: [Tutor] List-question

2005-12-19 Thread Ed Singleton
On 19/12/05, Øyvind [EMAIL PROTECTED] wrote:
 I have one function that finds some values. Then I want that function to
 find new values based on the values it found first. However, by just
 looping, it starts on an eternal job.

 As illustrated in:
  list = [1,2,3]
  list2 = list
  list2
 [1, 2, 3]
  for i in list:
 ... print i
 ... list2.append(4)
 ...
 1
 2
 3
 4
 4
 4 and it will forever continue with 4's.

 Why would list be expanded with the values of list2? How can I copy the
 result from one list, and do things with the list without getting it to
 expand?

Because they point to the same thing.

Type list2 is list after your other code and see.

You want list2 to be a COPY of list not a pointer to it.  Do this by using

list2 = list.copy()

Slices create a copy, so a shortcut is:

list2 = list[:]


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


Re: [Tutor] bnf

2005-12-19 Thread Hugo González Monteverde
  mystring = 'laLA'
  mystring.upper()
'LALA'
  mystring.lower()
'lala'
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] question !

2005-12-19 Thread Krava Magare
How can I remove and add record ( dictionary  type) to a file. This is the program that I'm working on: the program  should create a text file, print the contents of the text file, read  the file after it's been created, add a record and print the contents  of the file, remove a record(s) from the specified file, write it  again, read it again, print the contens of the new file.  Here is what I have so far: import cPickle, shelve  def  write_file(): CIT101 = ["Academic Computer Skills"]   CIT111 = ["Database Management"]   CIT115 = ["Intro to Computer scince"]   CIT127 = ["ACCESS"]   CIT211 = !
 ["Systems
 Analysis and Design"]   CIT216 = ["Visual Basic"]   CIT218 = ["Intermediate Visual Basic"]   CIT234 = ["Decision Support Using Excel"]   pickle_file = open("pickles1.dat","w") cPickle.dump(CIT101, pickle_file)   cPickle.dump(CIT111, pickle_file)   cPickle.dump(CIT115, pickle_file)   cPickle.dump(CIT127, pickle_file)   cPickle.dump(CIT211, pickle_file)   cPickle.dump(CIT216, pickle_file)   cPickle.dump(CIT218, pickle_file)   cPickle.dump(CIT234, pickle_file)   print "A file has been created and the required specifications have been added"   pickle_file.closedef read_file():   pickle_file = open("pickles1.dat","r") 
  CIT101 = cPickle.load(pickle_file)   CIT111 = cPickle.load(pickle_file)   CIT115 = cPickle.load(pickle_file)   CIT127 = cPickle.load(pickle_file)   CIT211 = cPickle.load(pickle_file)   CIT216 = cPickle.load(pickle_file)   CIT218 = cPickle.load(pickle_file)   CIT234 = cPickle.load(pickle_file)   pickle_file.close()   pickles = shelve.open("pickles2.dat")   pickles["CIT101"] = ["Academic Computer Skills"]   pickles["CIT111"] = ["Database Management"]   pickles["CIT115"] = ["Intro to Computer scince"]   pickles["CIT127"] = ["ACCESS"]   pickles["CIT211"] = ["Systems Analysis and Design"]   pickles["CIT216"] = ["Visual Basic!
 "] 
  pickles["CIT218"] = ["Intermediate Visual Basic"]   pickles["CIT234"] = ["Decision Support Using Excel"] pickles.sync() for key in pickles.keys():   print key, "-", pickles[key]def dele_file():   word_dele = raw_input("Which record do u want to delete?: ") if word_dele in picles.keys():   del word_dele else:   print "There is no such record in file pickles2.dat"  pickles.close()  def add_record():   CIT236 = ["SQL Programming"]   CIT240 = ["Database programming"]   pickle_file = open("pickles1.dat","a")   
  cPickle.dump(CIT236, pickle_file)   cPickle.dump(CIT240, pickle_file)   print "New data was added to the file"   pickle_file.close   def display_instructions():   """Display the Main menue"""   print \   """   Main Manue:  1. Exit   2. Create a new file and add specifications   3. (not working)Add more courses to the file   4. Read the file   5. (not working)Delete f!
 ile 
""" # exit the  program   1   def over_program():   """Exit the program"""   print "Good Bye!"   def main():   choice = None   display_instructions() while choice != 1:   choice = raw_input("\nChoice: ")   if choice == "1":   over_program()   break 
 elif choice == "2":   write_file() elif choice == "3":   add_to_file() elif choice == "4":   read_file() elif choice == "5":   delete_file()  else:   print "\nSorry, but", choice, "isn!
 't a
 valid choice."main()  raw_input("Press Enter Key to Exit.")   __Do You Yahoo!?Tired of spam?  Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] float question

2005-12-19 Thread linda.s
what does 2 mean in %2.4f ?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor