Re: [Tutor] best approach to db-api imports and cursor calls

2009-08-13 Thread Alan Gauld

"Serdar Tumgoren"  wrote


Is there any reason why you can't reuse the same cursor object? I know
when you're writing to a database, you have to be sure to commit your
changes. But if I'm just issuing execute statements and then fetching
data, is it okay to reuse the same cursor?


No, there is no reason. You can reuse a single cursor as often as 
you like provided you are happy to take responsibility for keeping 
the transactions clean - eg not trying to read data from one while 
writing at the same time, or combining two different selects, and 
committing between each change transaction.


But most folks find multiple cursors easier to manage in the same 
way that they find multiple variables easier than reusing a minimal 
number.


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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


Re: [Tutor] To write data in two different fonts?

2009-08-13 Thread Dave Angel

Nick Raptis wrote:
Dave 
Angel wrote:
As I said, you'd probably get in trouble if any of the lines had '&' 
or '<' characters in them.  The following function from the standard 
library can be used to escape the line directly, or of course you 
could use the function Nick supplied.


xml.sax.saxutils.escape(/data/[, /entities/])

   Escape '&', '<', and '>' in a string of data.

   You can escape other strings of data by passing a dictionary as the
   optional /entities/ parameter. The keys and values must all be
   strings; each key will be replaced with its corresponding value. The
   characters '&', '<' and '>' are always escaped, even if /entities/
   is provided.

Let us know if that doesn't do the trick.

DaveA


Thanks Dave for the info on xml.sax.saxutils.escape
Didn't know about this one.

For the rest:
It is sometimes
This is the source code of the xml.sax.saxutils.escape function:

---
def __dict_replace(s, d):
   """Replace substrings of a string using a dictionary."""
   for key, value in d.items():
   s = s.replace(key, value)
   return s

def escape(data, entities={}):
   """Escape &, <, and > in a string of data.

   You can escape other strings of data by passing a dictionary as
   the optional entities parameter.  The keys and values must all be
   strings; each key will be replaced with its corresponding value.
   """

   # must do ampersand first
   data = data.replace("&", "&")
   data = data.replace(">", ">")
   data = data.replace("<", "<")
   if entities:
   data = __dict_replace(data, entities)
   return data
-

As you can see, it too uses string.replace to do the job.
However, using a built-in function that works for what you want to do 
is preferable.

It's tested and might also be optimized to be faster.
It's easy and fun to look into the source though and know exactly what 
something does.

It's also one of the ways for a begginer (me too) to progress.

From the source code I can see this for example:
*Don' t pass the entity dictionary I proposed earlier to this function:*
entities = {'&' : '&',
  '<' : '<',
  '>' : '>',
  '"' : '"',
  "'" : '''}
If you pass an entity for '&' into escape(), it will escape it in the 
already partially escaped string, resulting in chaos.


Think of it, this function not checking for a '&' entity passed to it 
might worth qualifying as a bug :)


Nick


Yes, duplicating the & entitity would be a bug in the caller's code 
in this case.  (see my posted improvements to the OP code, which removed 
the variable entities entirely) The question is whether this function's 
doc should have such a warning, or whether the function should make sure 
double-substitution does not happen.


The & entity is the only predefined entity in the S3 standard that 
has this problem.  For example, there's no entity that replaces the 
letter 'a' or the semicolon.  And a quote sign is never used within an 
encoded entity.


I think perhaps an improved version would either ignore a & key in the 
supplied dictionary, or throw an exception if one is encountered.  The 
question that must always be answered is whether this could break 
existing code.


There are legitimate reasons for a string to be escaped twice.  Think 
what happens when a website wants to quote some html  source code.  Or a 
little less recursively, suppose you have a website teaching xml.  The 
examples posted would need to be double-escaped.  However, if someone 
had tried to do that in a single call to the current function, their 
code would already be broken because the dictionary doesn't preserve 
order, so the & substitution might not happen first.  Such a user must 
call the escape function twice, without passing & at all.


DaveA

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


Re: [Tutor] best approach to db-api imports and cursor calls

2009-08-13 Thread Serdar Tumgoren
> But most folks find multiple cursors easier to manage in the same way that
> they find multiple variables easier than reusing a minimal number.
>
That makes sense. I think I'll try using a global import/connect, and
then create cursors inside each method.

Thanks to you both for the advice!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] best approach to db-api imports and cursor calls

2009-08-13 Thread Christian Witts

Serdar Tumgoren wrote:

But most folks find multiple cursors easier to manage in the same way that
they find multiple variables easier than reusing a minimal number.



That makes sense. I think I'll try using a global import/connect, and
then create cursors inside each method.

Thanks to you both for the advice!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

  
You can also pass the connection object around from your __main__ so you 
can call .commit() and .rollback() when you need without polluting globals.


--
Kind Regards,
Christian Witts


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


Re: [Tutor] To write data in two different fonts?

2009-08-13 Thread Nick Raptis

Dave Angel wrote:


 However, if someone had tried to do that in a single call to the 
current function, their code would already be broken because the 
dictionary doesn't preserve order, so the & substitution might not 
happen first. 

Wow, I never thought about the dictionary not being sorted messing 
things up. Thanks for the insight.


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


[Tutor] Web framework: looking for python-tutor's angle.

2009-08-13 Thread Mac Ryan
A couple of months ago I took the time to read a few articles on python
web application frameworks and I got the impression that the two most
mature and active projects are Zope and Django.

Zope vs. Django hits 879.000 pages on google but much of the debate - or
at least this is my impression - falls into the class "vi vs. emacs" or
"gtk vs. qt" with many people singling out a single characteristics that
for them is THE characteristic making one framework better than the
other.

This [1] graph seems to corroborate my final impression (i.e. that
django is the way to go). Yet, I would be very interested in hearing
what the members of this list think, as I particularly enjoy the
"learner centered" approach that most of the people seems to have here.

I believe my needs are quite ordinary: my customers are typically small
businesses needing to process their data on a single server, sometime
exposing part of the application as front-end to the customers (hence
easy and flexible theming is important). I would definitively be happy
to sacrifice some functionality in exchange for a leaner and cleaner
design (i.e. more modular, elegant and intuitive), though.

Thank you in advance for your time,
Mac.

[1] http://www.google.com/trends?q=python+zope%2C+python+django

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


Re: [Tutor] Web framework: looking for python-tutor's angle.

2009-08-13 Thread Tino Dai
On Thu, Aug 13, 2009 at 2:09 PM, Mac Ryan  wrote:

> A couple of months ago I took the time to read a few articles on python
> web application frameworks and I got the impression that the two most
> mature and active projects are Zope and Django.
>
> Zope vs. Django hits 879.000 pages on google but much of the debate - or
> at least this is my impression - falls into the class "vi vs. emacs" or
> "gtk vs. qt" with many people singling out a single characteristics that
> for them is THE characteristic making one framework better than the
> other.
>

 I have worked with both Zope and Django. When I worked with
Zope, it had a lot of powerful features like their version of interfaces and
zodb (zope object database - think of a high performance dictionary) but the

learning curve was very high. After a couple of weeks of messing with it, my

partner and I drop it for Django. Django allows you to get up and doing
productive
work in a couple of hours instead of a couple of days or weeks. If you are
under any time constraints and do not have experience with either one, go
with Django.

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


Re: [Tutor] Web framework: looking for python-tutor's angle.

2009-08-13 Thread Serdar Tumgoren
I've never used Zope so I can't speak to its strengths or weaknesses.
But I can say that Django is quite natural if you already know Python.
It certainly is modular, with a ton of apps that you can plug in to
gain added functionality. And there's also a project called Pinax that
provides layers of CMS-type functionality (e.g. authentication,
blogging, wiki, etc.) that can save you from reinventing the wheel.

Here are a few places to explore:

http://djangoplugables.com/
http://pinaxproject.com/

Search Google Code for django

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


[Tutor] Dynamic Function Calls

2009-08-13 Thread Megan Land

Hi,

I'm trying to call a function from a dictionary.  I did some googling and
from what I can tell my code should work, but doesn't.  Here's an example:

def myFunc(self, inputList):
dict={0: func0, 1: func1, 2:func2}
for element in inputList:
   dict[element]()

When I go to run this I get an error saying func0 is not defined.  Does
anyone have any ideas as to why this won't work?  I'm using Python 2.6 if
that makes any difference.

Thanks!

Megan Land
FVT Blade EMET Test Engineer
ml...@us.ibm.com___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Dynamic Function Calls

2009-08-13 Thread Kent Johnson
On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote:
> Hi,
>
> I'm trying to call a function from a dictionary. I did some googling and
> from what I can tell my code should work, but doesn't. Here's an example:
>
> def myFunc(self, inputList):
> dict={0: func0, 1: func1, 2:func2}
> for element in inputList:
> dict[element]()
>
> When I go to run this I get an error saying func0 is not defined. Does
> anyone have any ideas as to why this won't work? I'm using Python 2.6 if
> that makes any difference.

You don't show any definition for func0 in the above snippet. Where is
it defined?

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


Re: [Tutor] Dynamic Function Calls

2009-08-13 Thread Alan Gauld

"Megan Land"  wrote


I'm trying to call a function from a dictionary.  I did some googling and
from what I can tell my code should work, but doesn't.  Here's an 
example:


def myFunc(self, inputList):


The fact you have a self in there suggests that this is a method
of some class? Is it? If not remove the self.


   dict={0: func0, 1: func1, 2:func2}


What do you think this is doing? What are func0, func1 etc?
Where are they defined?


   for element in inputList:
  dict[element]()


This will work provided element exists in dict and dict[element]
is a callable object, eg a function. But I recommend putting
some error handling in for the cases where either of those
two conditions is not true


When I go to run this I get an error saying func0 is not defined.  Does
anyone have any ideas as to why this won't work?


Because you haven't defined func0!

try adding

def func0(): print 'func0'

def func1(): print 'func1'

def func2(): print 'func2'

above your function.
Then it might work if you call you function like this:

myFunc([func2,func0,func1])

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] Web framework: looking for python-tutor's angle.

2009-08-13 Thread Alan Gauld
"Mac Ryan"  wrote 


A couple of months ago I took the time to read a few articles on python
web application frameworks and I got the impression that the two most
mature and active projects are Zope and Django.


They are both mature and widely used, but for quite different markets.
TurboGears is a more direct competitor to Django and there is no 
competitor (in the Python world) to Zope



This [1] graph seems to corroborate my final impression (i.e. that
django is the way to go). 


I believe my needs are quite ordinary: my customers are typically small
businesses needing to process their data on a single server, sometime
exposing part of the application as front-end to the customers 


That sounds like Django to me. Zope is better suited to large scale 
corporate type scenarios with high volumes. If Zope can't cope 
you really need to move to the big guns like BEA Weblogic etc.. 
But for SMEs Django (et al) is ideal.


HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/

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