Re: [Tutor] validation

2007-08-28 Thread Michael
Thanks Everybody

I now have several methods in my arsenal and they all look quite simple, 
now that I know. Just wondering, when would you use isInstance()?

Thanks

Alan Gauld wrote:
> "Michael" <[EMAIL PROTECTED]> wrote
>
>   
>> to check and make sure that an integer is entered and the program 
>> not
>> crashing when a naughty user enters a character instead.
>> 
>
> John F has already pointed you to the use of try/except for this,
> however...
>
>   
>> trying to use the Type() function but I cannot work out how to check 
>> the
>> return value? Caomparing it to 'int' or 'str' isn't working,
>> 
>
> The easiest way is to compare to another type:
>
> x = 42
> if type(x) == type(int()):
>
> or even
>
> if type(x) == type(2):
>
> Or you can use the types module:
>
> if type(x) == types.IntType
>
> But for your purposes the Python idiom of its 'better to ask
> forgiveness than permission' applies.
>
>
>   

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


Re: [Tutor] Filemaker interactions

2007-08-28 Thread Ian Witham
Hello,

I thought I'd update on my Filemaker Pro 6 situation.

The PyFileMaker module (CGI based access) is meeting my requirements at
present, although record searches with a large number of results are a
little slow.

If my project grows much larger in scope I will certainly look into the
mxODBC extension.

Thanks for the suggestions.


On 8/26/07, M.-A. Lemburg <[EMAIL PROTECTED]> wrote:
>
> On 2007-08-01 23:41, Ian Witham wrote:
> > Hello,
> >
> > I'm hoping someone here can put me on the right track with some broad
> > concepts here.
> >
> > What I am hoping to achieve is a simple HTML page to be served over
> > our company LAN, into which the users (Real Estate Agents) can enter a
> > property address or reference number.
> >
> > My next thought was to have a Python CGI script query our filemaker
> > database of property listings, construct a PDF from the relevant
> > information, and finally return this PDF to the user.
> >
> > At no stage do I want the user to have unfettered access to the
> > database or the ability to alter/delete records.
> >
> > My question is: what is the most appropriate way for my script to
> > interact with Filemaker? Can this be done with Filemaker Pro 6?
> >
> > According to the Filemaker Help, the "Local Data Access Companion"
> > shares the FileMaker Pro database with ODBC-compliant applications on
> > the same computer. Is this the right option?
> >
> > Can my CGI script be an ODBC client? How? Would it need to be
> > Filemaker specific code or does ODBC have a standardised format?
> >
> > I'm grateful for any advice and a nudge in the right direction.
>
> You could try our mxODBC extension for Python which will
> likely just work out of the box:
>
> https://www.egenix.com/products/python/mxODBC/
>
> with the Filemaker ODBC driver.
>
> Or give this module a try (if you have more time at hand
> and can do without a DB-API interface):
>
> http://www.lfd.uci.edu/~gohlke/code/filemaker.py.html
>
> It uses Filemaker's XML interface.
>
> --
> Marc-Andre Lemburg
> eGenix.com
>
> Professional Python Services directly from the Source  (#1, Aug 25 2007)
> >>> Python/Zope Consulting and Support ...http://www.egenix.com/
> >>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
> >>> mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/
> 
>
>  Try mxODBC.Zope.DA for Windows,Linux,Solaris,MacOSX for free ! 
>
>
>eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
> D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
>Registered at Amtsgericht Duesseldorf: HRB 46611
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Shared Class Attribute

2007-08-28 Thread Ricardo Aráoz
Hi,

if I have this class :

class myClass :
ClassCount = 0
def __init__(self) :
(here I want to increment ClassCount for the whole class)
self.InstanceAttr = 1

How would I increment the shared class attribute (ClassCount) from
INSIDE the class?
I can do it from the outside by stating myClass.ClassCount = 22 and it
will change for all instances. But haven't found a way to do it from inside.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Shared Class Attribute

2007-08-28 Thread Eric Brunson
Ricardo Aráoz wrote:
> Hi,
>   

Hi Ricardo,

In the future, please start a new thread with a new email and not a 
reply to an existing thread.  Compliant mail clients thread based on 
headers you may or may not see in your client, and this email is part of 
the thread you replied to called "[Tutor] PyCon 2008 - Call for Tutorial 
Ideas".  In addition, you send a copy of your message to 
[EMAIL PROTECTED] and many who reply using a "Reply All" 
function in their mailer will spam that list, too.

> if I have this class :
>
> class myClass :
> ClassCount = 0
> def __init__(self) :
> (here I want to increment ClassCount for the whole class)
>   self.InstanceAttr = 1
>   

You always reference a class variable by the class name:

 >>> class myclass:
... count = 0
... def __init__( self ):
... myclass.count += 1
...
 >>> c = myclass()
 >>> print c.count
1
 >>> d = myclass()
 >>> print d.count
2
 >>> print c.count
2
 >>> e = myclass()
 >>> print e.count, myclass.count
3 3

Hope that helps.

Sincerely,
e.

> How would I increment the shared class attribute (ClassCount) from
> INSIDE the class?
> I can do it from the outside by stating myClass.ClassCount = 22 and it
> will change for all instances. But haven't found a way to do it from inside.
> ___
> 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] validation

2007-08-28 Thread Terry Carroll
On Tue, 28 Aug 2007, Michael wrote:

> I now have several methods in my arsenal and they all look quite simple, 
> now that I know. Just wondering, when would you use isInstance()?

Well, as this thread has shown, I'm no expert, but let me take a stab on 
it.  If nothing else, we'll all learn something as the experts come in to 
correct me!

We have three (or four, depending on how you count) possible approaches:

1) Use duck-typing.  Don't worry about the type of operand you're being
fed.  You don't really care, for example, whether it is actually a file,
or an int, or a float, or a string, or whatever, as long as it acts like
one for the purpose for which you're using it.  For example, if you're 
writing an increment function:

def incr(n):
  "returns an incremented value of n"
  return n + 1

It will work whether you get an int or a float.  It won't work with a file 
or a string, but rather than spend your time checking that, just let 
Python generate the exception.  In the mean time, you code will work with 
ints, floats, complex numbers, or anything that subclassed from them, with 
no special work from you.

This is called "duck-typing" from the phrase, "if it looks like a duck, 
walks like a duck and quacks like a duck, it's a duck."  If it looks 
enough like a number that it lets itself have one added to it, I'm calling 
it a number.

1A) a variation of option 1 (which is why I said "depending on how you
count" above):  duck-typing with error recovery.

def incr(n):
  "returns an incremented value of n, or None if n is not incrementable"
  try:
return n + 1
  except:
return None

This will try to return a value equal to N + 1, but if n does not quack 
like a duck, it just returns None (and it's up to the caller to deal with 
it).

Options 2 & 3 are non-duck-typing approaches, usually called 
Look-Before-You-Leap (LBYL), where the code is checking to see if it was 
passed the right type, and behaving accordingly.  These are:

2) Using type() to obtain the type of the operand, and making a decision 
based on the type;

3) using isinstance() to see if the operand is an instance of a particular 
type, and then making a decision based on whether or not it is.

These are subtly different, because of subclassing.  An object has only 
one type -- the type of its class.  But it is an instance, not only of its 
class, but of that class's superclass(es), if any; and the 
superclass(es)'s superclass(s), if any, etc. all the way back to the 
"object" class.

Here's an example.

I'm going to create a mutant list class that subclasses list.  It produces
objects that are the same as an ordinary list with one difference: if the
list is sorted, the contents are sorted without regard to case; that is,
both "B"  and "b" sort after both "A" and "a", and before "C" and "c".  
Here's the class definition:

>>> class caseindependentlist(list):
... def __init__(self, *args):
... list.__init__(self, *args)
... def sort(self):
... return list.sort(self, key=str.upper)

It subclasses list, and overrides the usual sort method with a replacement 
method that sorts based on what the value would be if converted to all 
upper-case (which means the sort is case-independent).

Let's create a plain old ordinary list and a mutant caseindependentlist to 
see how they compare.  (I'm using a long form of the list(0 constructor 
just to be consistent here)

>>> ordinary_list = list(["Zebra", "alligator", "Duck", "penguin"])
>>> mutant_list = caseindependentlist(["Zebra", "alligator", "Duck", "penguin"])

Let's look at each before and after sorting to show how they work...

>>> ordinary_list
['Zebra', 'alligator', 'Duck', 'penguin']
>>> ordinary_list.sort()
>>> ordinary_list
['Duck', 'Zebra', 'alligator', 'penguin']

That worked as expected, but the upper-case ones are sorted first.

But...

>>> mutant_list
['Zebra', 'alligator', 'Duck', 'penguin']
>>> mutant_list.sort()
>>> mutant_list
['alligator', 'Duck', 'penguin', 'Zebra']

Okay, that works as expected.

Now here's the thing: mutant_list, for almost every purpose, is just a 
list.  You can do anything with it that you can do with a plain old list.  
the only thing different about it is that it sorts without regard to case.

But mutant_list and ordinary_list have differing types:

>>> type(ordinary_list)

>>> type(mutant_list)

>>>

If you were to write a program that checked "to make sure" it was being 
passed a list, and were checking with type(), it would wrongly reject 
mutant_list.

isinstance does a better job here.  isinstance doesn't care whether the 
object in question is a list directly (as in the case of ordinary_list) or 
indirectly (as in the case of mutant_list):

>>> isinstance(ordinary_list, list)
True
>>> isinstance(mutant_list, list)
True

So, when to use what?

I would say:

1) where possible, use duck-typing, either of the type 1 or type 1A 
variety.  That's the easiest way to have your program just work if it's 
possible to work.

2) 

Re: [Tutor] Shared Class Attribute

2007-08-28 Thread Ricardo Aráoz
Eric Brunson wrote:
> Ricardo Aráoz wrote:
>> Hi,
>>   
> 
> Hi Ricardo,
> 
> In the future, please start a new thread with a new email and not a 
> reply to an existing thread.  Compliant mail clients thread based on 
> headers you may or may not see in your client, and this email is part of 
> the thread you replied to called "[Tutor] PyCon 2008 - Call for Tutorial 
> Ideas".  In addition, you send a copy of your message to 
> [EMAIL PROTECTED] and many who reply using a "Reply All" 
> function in their mailer will spam that list, too.

Hi Eric.

Sorry, thought that by changing the subject I was changing the thread,
won't happen again.
BTW, I did a 'reply all', is that bad?

Thanks for the info on the class attribute.

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


Re: [Tutor] Shared Class Attribute

2007-08-28 Thread Eric Brunson
Ricardo Aráoz wrote:
> Eric Brunson wrote:
>   
>> Ricardo Aráoz wrote:
>> 
>>> Hi,
>>>   
>>>   
>> Hi Ricardo,
>>
>> In the future, please start a new thread with a new email and not a 
>> reply to an existing thread.  Compliant mail clients thread based on 
>> headers you may or may not see in your client, and this email is part of 
>> the thread you replied to called "[Tutor] PyCon 2008 - Call for Tutorial 
>> Ideas".  In addition, you send a copy of your message to 
>> [EMAIL PROTECTED] and many who reply using a "Reply All" 
>> function in their mailer will spam that list, too.
>> 
>
> Hi Eric.
>
> Sorry, thought that by changing the subject I was changing the thread,
> won't happen again.
>   

No worries, everyone has to learn sometime.

> BTW, I did a 'reply all', is that bad?
>   

No, that's preferred.  :-)

> Thanks for the info on the class attribute.
>
>   
No problem, good luck.

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


Re: [Tutor] validation

2007-08-28 Thread Kent Johnson
Terry Carroll wrote:

> 1A) a variation of option 1 (which is why I said "depending on how you
> count" above):  duck-typing with error recovery.
> 
> def incr(n):
>   "returns an incremented value of n, or None if n is not incrementable"
>   try:
> return n + 1
>   except:
> return None
> 
> This will try to return a value equal to N + 1, but if n does not quack 
> like a duck, it just returns None (and it's up to the caller to deal with 
> it).

This is generally a bad idea. The problem is that you depend on the 
caller checking the return value for None. If it doesn't, it will 
probably get some other exception later which will be hard to track down 
because the source will not be clear. Depending on the caller to check 
for errors is unreliable. Raising an exception forces the issue - the 
caller has to catch the exception or propagate it, it can't just ignore it.

IIRC exceptions were introduced in C++ to give an alternative to 
checking for errors.

On the other hand, if the caller *does* check for errors, the code can 
quickly get very cluttered with error checking. Long ago and far away I 
wrote a lot of MacOS code. At that time, almost every system call 
returned an error code that had to be checked. Functions tended to look like
err = DoSomethingWithX(x);
if (err != noErr)
   return err;

err = DoSomethingElse(y);
if (err != noErr)
   return err;

etc. With exceptions this can be written much more simply, compactly and 
readably as
DoSomethingWithX(x);
DoSomethingElse(y);

> In a program I'm writing now, I would like to use duck-typing, but I don't
> think it works well.  In my particular case, to simplify it a bit, I'm
> writing a method that can either take a string that contains SQL
> statements; or a list or tuple that contains strings of SQL statements.  
> Duck-typing doesn't work too well here, because a string of characters
> looks an awful lot like a list or tuple of one-character strings for most
> duck-typing tests.  I'd have to put together a weird duck-typing approach 
> that would ultimately be a type-checking approach disguised as 
> duck-typing; so I opted to just be up-front about it and check the type 
> (using isinstance, of course).

This is one of the common cases where there is no simple alternative to 
LBYL - a function that can take a string or a list of strings as a 
parameter.

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


Re: [Tutor] validation

2007-08-28 Thread Terry Carroll
On Tue, 28 Aug 2007, Kent Johnson wrote:

> Terry Carroll wrote:
> 
> > 1A) a variation of option 1 (which is why I said "depending on how you
> > count" above):  duck-typing with error recovery.
> > 
> > def incr(n):
> >   "returns an incremented value of n, or None if n is not incrementable"
> >   try:
> > return n + 1
> >   except:
> > return None
> > 
> > This will try to return a value equal to N + 1, but if n does not quack 
> > like a duck, it just returns None (and it's up to the caller to deal with 
> > it).
> 
> This is generally a bad idea. The problem is that you depend on the 
> caller checking the return value for None. 

Agreed.  I was just trying to come up with a short example of catching the
exception and doing something different because of it.  Admittedly, 
returning None is not much of an "error recovery."


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


[Tutor] A replacement for a "for" loop

2007-08-28 Thread Trey Keown
Hello everybody.
I'm using a couple of "for" loops to help me in xml parsing using expat.
Unfortunately, though, I've found that using more than one of these in a
row, at least in my case, causes a redundancy error.
a snippet of my code (note that this is not the way I set the dictionaries)-

attrs={u'title': u'example window title', u'name': u'SELF', u'icon':
u'e.ico'}
keys = ['name','title','icon']
for (tag, val) in attrs.iteritems():
for key in keys:
print val

the first "for" tag causes the dictionary (attrs) to have its keys called
"tag" and its value called "val". The second "for" loop causes the
dictionary keys to be read in a certain order. How could I take away the
first "for" loop and replace it with something else to do the same general
function?
thanks for any help.
Trey

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


Re: [Tutor] A replacement for a "for" loop

2007-08-28 Thread John Fouhy
On 29/08/07, Trey Keown <[EMAIL PROTECTED]> wrote:
> attrs={u'title': u'example window title', u'name': u'SELF', u'icon':
> u'e.ico'}
> keys = ['name','title','icon']
> for (tag, val) in attrs.iteritems():
> for key in keys:
> print val
>
> the first "for" tag causes the dictionary (attrs) to have its keys called
> "tag" and its value called "val". The second "for" loop causes the
> dictionary keys to be read in a certain order. How could I take away the
> first "for" loop and replace it with something else to do the same general
> function?

for key in keys:
  print 'Attribute %s has value %s' % (key, attrs[key])

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


[Tutor] Newbie

2007-08-28 Thread Toby Holland
HI all,

I was wondering if any of you had any advice as to where I should start in
regards to learning using and programing with Python.  I have wanted to
learn a program language for some time and just felt that now was good and I
have heard some great things about Python so any suggestions would be great.

Thank you all in advance!

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


Re: [Tutor] Newbie

2007-08-28 Thread Scott
Toby Holland wrote:
> I was wondering if any of you had any advice as to where I should start 
> in regards to learning using and programing with Python.  I have wanted 
> to learn a program language for some time and just felt that now was 
> good and I have heard some great things about Python so any suggestions 
> would be great.
I myself am a newbie and I found starting with "How to Think Like a 
Computer Scientist Learning with Python" as a great starter.  You can 
find it at http://ibiblio.org/obp/thinkCS/python.php.

It was very easy to read, the writer is great, I believe a high school 
teacher.  The writer does not expect you to know any prior programming. 
also, best of all, it is completely free.

-- 
Your friend,
Scott

Sent to you from a Linux computer using Ubuntu Version 7.04 (Feisty Fawn)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie

2007-08-28 Thread Lutz Horn
Hi,

On Tue, 28 Aug 2007 22:25:11 -0400, "Toby Holland"
<[EMAIL PROTECTED]> said:
> I was wondering if any of you had any advice as to where I should
> start in regards to learning using and programing with Python.

Take a look at:

* "Byte of Python" 

* The "Python Tutorial" by GvR 

* "Dive into Python"  (advanced)

Lutz
-- 


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