Newbie: Why doesn't this work

2007-12-31 Thread ct60
Hi Python Community:

Despite my new-ness to Python  I have alreadhy been able to do some (I
think) amazing things.  It is a truly elegant and smart language.

Yet, I can not seem to get a handle on something simple.

I would like to make a class which has private varaiables fName and
lName.  It should have a property "name" which can get or set a name.
Something like as follows:

class Person:
def __init__(self, fName="", lName=""):
self.__fName = fName
self.__lName = lName

def __getattr__(self, attr):
if attr == "name":
return self.__fName + " " + self.__lName

def __setattr__(self, attr, value):
# this assumes that value is a tuple of first and last name
if attr == "name":
self.__fName, self.__lName = value


P = Person()

P.name = ("Joe", "Smith")

print P.name

This fails with the following note:

>>>
Traceback (most recent call last):
  File "C:\Python\testObject.py", line 20, in 
print P.name
  File "C:\Python\testObject.py", line 8, in __getattr__
return self.__fName + " " + self.__lName
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'

I don't understand why this fails.  I thought perhaps I need to make
the __getattr__ function like this

def __getattr__(self, attr):
if attr == "name":
return self.__fName + " " + self.__lName
elif attr == "__fName":
return self.__fName
elif attr == "__lName":
return self.__lName

But that still fails.

Can someone please tell me what I am doing wrong?

Thansk in advance,

Chris ([EMAIL PROTECTED])
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie: Why doesn't this work

2007-12-31 Thread ct60
Thanks you Gabriel and Timm for your thoughtful responses.  I am very
appreciative.

I had heard about the properties function, but wanted to understand
the old syntax first before I tried that.  Thanks to your responses, I
was able to see what the problem was.

Here is a solution I came up with:

class Person():
def __init__(self, fName="", lName=""):
self.__fName = fName
self.__lName = lName

def __getattr__(self, attr):
if attr == "name":
return self.__fName + " " + self.__lName
else:
return self.__dict__[attr]

def __setattr__(self, attr, value):
# this assumes that value is a tuple of first and last name
if attr == "name":
self.__fName, self.__lName = value
else:
self.__dict__[attr] = value


P = Person("Joe", "Smith")

print P.name

P.name = ("Jane", "Doe")

print P.name

This works as expected printing "Joe Smith" and then "Jane Doe".

To be honest, I think the above old syle (I guess) method is pretty
cool and elegant.

Thanks again and have a GREAT NEW YEAR!!

Chris ([EMAIL PROTECTED])
-- 
http://mail.python.org/mailman/listinfo/python-list


Using Regular Expressions to Parse SQL

2008-02-05 Thread ct60
Hello again -

I do not seem to be able to get a handle on non-greedy pattern
matching.

I am trying to parse the following - note that there are no line
breaks in the string:

" FROM ((qry_Scores_Lookup1 INNER JOIN CSS_Rpt1 ON
(qry_Scores_Lookup1.desc = CSS_Rpt1.desc) AND
(qry_Scores_Lookup1.lastcdu = CSS_Rpt1.lastcdu))"

What I would like to do is be able to split on the "AND" and the "="
to come up with the following:
qry_Scores_Lookup1.desc
CSS_Rpt1.desc
qry_Scores_Lookup1.lastcdu
CSS_Rpt1.lastcdu

The following is one of my many attempts to do this:

import re

s= " FROM ((qry_Scores_Lookup1 INNER JOIN CSS_Rpt1 ON
(qry_Scores_Lookup1.desc = CSS_Rpt1.desc) AND
(qry_Scores_Lookup1.lastcdu = CSS_Rpt1.lastcdu))"

pat = " FROM .+ (?:INNER|LEFT|RIGHT) JOIN .+ ON (?:AND)*?((.+)=(.+))"

m = re.match(pat, s)

if m is None:
print "No Match"
else:
for mat in m.groups():
print mat

My pattern does not even come close.

Any help would be greatly appreciated.  My goal is to analyse a large
number of SQL querys to try to identify the join field and see where
indexing might make sense.

While I am mostly interested in understanding regular expressions, I
would also be interested in knowing about any Python SQL parsers out
there.

Thanks in advance.

Chris ([EMAIL PROTECTED])


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using Regular Expressions to Parse SQL

2008-02-05 Thread ct60
Firstly, thanks to those who posted.

I just do not understand how the non-greedy operator works.

Using the following code:

import re

s = "qry_Lookup.desc = CSS_Rpt1.desc AND qry_Lookup.lcdu1 =
CSS_Rpt1.lcdu"

pat = "(.+=)+?(.+)"

m = re.match(pat, s)

if m is None:
print "No Match"
else:
for mat in m.groups():
print mat

I would expect that the first group would match one or more times with
the fewest amount of text.  However the result is:

>qry_Lookup.desc = CSS_Rpt1.desc AND qry_Lookup.lcdu1 =
> CSS_Rpt1.lcdu

The first match of the "=" char is still greedy.  I would have
expected:
qry_Lookup.desc =
> CSS_Rpt1.desc AND qry_Lookup.lcdu1
> =
> CSS_Rpt1.lcdu

I'm obviously missing something because the non-greedy match seems to
not be acting as expected.

Any insights would be greatly appreciated.

Thanks in advance,

Chris ([EMAIL PROTECTED])



On Feb 5, 9:31 am, [EMAIL PROTECTED] wrote:
> Hello again -
>
> I do not seem to be able to get a handle on non-greedy pattern
> matching.
>
> I am trying to parse the following - note that there are no line
> breaks in the string:
>
> " FROM ((qry_Scores_Lookup1 INNER JOIN CSS_Rpt1 ON
> (qry_Scores_Lookup1.desc = CSS_Rpt1.desc) AND
> (qry_Scores_Lookup1.lastcdu = CSS_Rpt1.lastcdu))"
>
> What I would like to do is be able to split on the "AND" and the "="
> to come up with the following:
> qry_Scores_Lookup1.desc
> CSS_Rpt1.desc
> qry_Scores_Lookup1.lastcdu
> CSS_Rpt1.lastcdu
>
> The following is one of my many attempts to do this:
>
> import re
>
> s= " FROM ((qry_Scores_Lookup1 INNER JOIN CSS_Rpt1 ON
> (qry_Scores_Lookup1.desc = CSS_Rpt1.desc) AND
> (qry_Scores_Lookup1.lastcdu = CSS_Rpt1.lastcdu))"
>
> pat = " FROM .+ (?:INNER|LEFT|RIGHT) JOIN .+ ON (?:AND)*?((.+)=(.+))"
>
> m = re.match(pat, s)
>
> if m is None:
>     print "No Match"
> else:
>     for mat in m.groups():
>         print mat
>
> My pattern does not even come close.
>
> Any help would be greatly appreciated.  My goal is to analyse a large
> number of SQL querys to try to identify the join field and see where
> indexing might make sense.
>
> While I am mostly interested in understanding regular expressions, I
> would also be interested in knowing about any Python SQL parsers out
> there.
>
> Thanks in advance.
>
> Chris ([EMAIL PROTECTED])

-- 
http://mail.python.org/mailman/listinfo/python-list