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:



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!



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=1291819247219837219837129&goto=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] RegEx query

2005-12-19 Thread Kent Johnson
Liam Clarke wrote:
> 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=1291819247219837219837129&goto=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.

This seems like a good solution to me. The patterns you want to accept and 
reject are 
pretty similar so your regex has to be very specific to discriminate them.
> 
> 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.

I think you may misunderstand how * works. It will match as much as possible 
but it will 
backtrack and match less if that makes the whole match work.

For example the regex ab*d will match abbd with b* matching bb. If I change the 
regex to 
ab*(?!d) then it will still match abbd but the b* will just match one b and the 
d doesn't 
participate in the match.

So b* doesn't mean "match all the b's no matter what" it means "match as many 
b's as you 
can and still have the rest of the match succeed". In the case of ab*(?!d) this 
means, 
match an 'a', then a sequence of 'b', then something that is not 'd'. By 
shortening the 
match for b*, the 'not d' can match against the last 'b'.

> 
> 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.

Which XPath program is that? I haven't found one I really like.

Kent

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


[Tutor] List-question

2005-12-19 Thread Øyvind
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?


-- 
This email has been scanned for viruses & spam by Decna as - www.decna.no
Denne e-posten er sjekket for virus & spam av Decna as - www.decna.no

___
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] List-question

2005-12-19 Thread Ed Singleton
On 19/12/05, Ed Singleton <[EMAIL PROTECTED]> wrote:
> 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[:]

Sorry, you need to:

from copy import copy

before you can use copy.

Also, you shouldn't use "list" as a variable name. as it is a built-in
function that converts things to lists.  Use "list1".

That way you can also use list2 = list(list1).

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


Re: [Tutor] Name tables questions

2005-12-19 Thread Kent Johnson
Bernard Lebel wrote:
> Hello,
> 
> I have a few theoric questions regarding name tables. I wish to better
> understand a few things about this aspect of Python, in particular how
> module names and the import statements fit into the picture of name
> tables.
> 
> - First of all, I understand each scope has its "local" name table,
> containing all the names defined in this scope, no matter the origin
> of the name. So, if you import a module in a scope, its name is added
> to the local scope name table, correct?

Yes, with a couple of notes:
- The correct term is "namespace", not "name table".
- Instead of talking about defining a name, a better term is "binding". Binding 
a name in 
a namespace means associating the name with a value in the context of the 
namespace. This 
is usually implemented by making an entry in some dictionary that implements 
the namespace.
- "All the names defined in this scope" could be interpreted to mean "all the 
names 
accessible in this scope." With this interpretation your statement is *not* 
correct. The 
local namespace includes all names that are actually bound in the local scope, 
not all 
names accessible in the local scope.

For example:

  >>> import sys
  >>> a = 1
  >>> def f():
  ...   import os
  ...   b = 2
  ...   print 'locals:', locals()
  ...   print 'globals:', globals()
  ...
  >>> f()
locals: {'b': 2, 'os': }
globals: {'a': 1, 'f': , '__builtins__': , '__file__': 'C:\\Documents and Settings\\ktjohns
on\\pythonstartup.py', 'sys': , '__name__': 
'__main__', 
'__doc__': None}


The local namespace of f() includes os and b, which are bound in f(). The 
global namespace 
includes sys, a and f, all names bound at global scope, and several other names 
inserted 
automatically by the Python runtime.

> 
> - When you import a module, I understand the module name is appended
> to Python dictionary of imported modules. I can see the name of the
> module if I print out the sys.modules attribute. If the module name is
> also added to the local scope name table, it does mean that the module
> is added in two tables: the local scope name table and the imported
> modules table. Is that correct?

Yes. The module object itself is cached in sys.modules. sys.modules is a dict 
whose keys 
are module names and values are the actual module objects. Subsequent imports 
will find 
the cached module and bind a name to it.
> 
> - I *think* I have read in Learning Python (sorry I don't have the
> book near me) that during execution, when Python doesn't find a name
> in the current scope name table, then it will look up every
> encompassing name table one after another, until it can find it, and
> if it can't, will raise an error.

Yes.

> If I remember correctly about my reading, this lookup into an
> encompassing name table has a performance cost. The author suggests
> that, without regard to the practical considerations, it would not be
> such a bad idea to pass down all possible names to functions and
> encapsulated functions so name lookup into other tables could be
> avoided.

Yes, there is a performance cost. No, it doesn't make sense to "pass down" all 
possible 
names. If a name is not used in a function, then you have incurred the cost of 
looking it 
up and binding it in the local namespace without any benefit. If the name is 
used once in 
the function, it is still probably cheaper to just look it up once globally and 
skip the 
local rebinding and lookup.

Where this makes sense is in a loop that is a known performance bottleneck that 
you are 
trying to optimize. Then there is a benefit to making local copies of any 
global names 
that are in the loop. It is also beneficial to make local copies of attributes 
that are 
accessed in the loop such as methods.

> Now, let say I would do something like that, and be zealous at that,
> to the point where I would write import statements for the same
> modules in every possible function to define the module name there.
> Since the module name is already in the module names dictionary but
> not in the global name table, would there be any advantage, or
> disadvantage in doing so?

Again the only advantage of this would be if the module is used repeatedly in a 
loop. But 
in that case you are better off making a local reference to the module 
attribute you 
actually use, rather than the module itself. For example something like this to 
generate 
the paths to all files in a directory:

import os
def allFiles(path):
   join = os.path.join
   isfile = os.path.isfile
   for f in os.listdir(path):
 p = join(path, f)
 if isfile(p):
   yield p

This is not a very good example because the performance of this fn is likely to 
be 
dominated by the I/O, not the name lookup, but it gives the idea.

You might be interested in this recipe which automatically performs this kind 
of optimization:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/277940

But I should stress that this is an *optimization*, which you should use on

Re: [Tutor] List-question

2005-12-19 Thread Terry Carroll
On Mon, 19 Dec 2005, Ed Singleton wrote:

> On 19/12/05, Ed Singleton <[EMAIL PROTECTED]> wrote:
> >
> > list2 = list.copy()
> >
> > Slices create a copy, so a shortcut is:
> >
> > list2 = list[:]
> 
> Sorry, you need to:
> 
> from copy import copy
> 
> before you can use copy.

It should also be, after the import:

  list2=copy.copy(list1)

rather than 

  list2=list1.copy()# or
  list2=list.copy()


___
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


Re: [Tutor] synchronized enumerate

2005-12-19 Thread Chris or Leslie Smith
Kent wrote:
| Take a look at this thread on c.l.py for some discussion and
| possibilities. The part you 
| are interested in starts around message 14.
| 
http://groups.google.com/group/comp.lang.python/browse_frm/thread/ab1658dca4023e2b?hl=en&;
|
After looking at that I found something on python-dev that showed how to create 
an object that is sliceable but returns a slice object: sslice[::-1] --> 
slice(None, None, -1). It would be interesting if a single object could be 
sliced or accessed as a function, but I'm not sure that is possible. If it 
were, you could do:

sslice(3) --> slice(None, 3, None)
sslice[:3]--> slice(None, 3, None)

Here's the code from python-dev:

class MetaSlice(object):
def __getitem__(cls, item):
return item
def __init__(self, *args, **kw):
return super(MetaSlice,self).__init__(self, *args, **kw)
class sslice(slice):
__metaclass__=MetaSlice

Anway, after seeing that, I decided to re-write the enumerate so it takes a 
slice argument and then I get the start and step values from that rather than 
having those passed in as arguments. I also initiated the discussion on 
python-dev (and they said that comp.lang.python was a better place for it) and 
c.l.p. The updated version of the enuerate can be found on compl.lang.python 
under "synchronized enumerate" at  

http://tinyurl.com/e35mh 

Thanks for the pointers and advice, Kent.
/c

 

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