A __getattr__ for class methods?

2006-02-08 Thread Dylan Moreland
I'm trying to implement a bunch of class methods in an ORM object in
order to provide functionality similar to Rails' ActiveRecord. This
means that if I have an SQL table mapped to the class "Person" with
columns name, city, and email, I can have class methods such as:

Person.find_by_name
Person.find_by_city_and_name
Person.find_by_name_and_city_and_email

I have a metaclass generating basic properties such as .name and .city,
but I don't want to generate a class method for every permutation of
the attributes. I'd like to have something much like __getattr__ for
instance attributes, so that if a method like
Person.find_by_city_and_email cannot be found, I can construct a call
to the basic find method that hides the SQL. Is there any way of doing
this, or am I trying to mirror a functionality that Python simply does
not have?

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


Re: A __getattr__ for class methods?

2006-02-08 Thread Dylan Moreland
Michael Spencer wrote:
> Dylan Moreland wrote:
> > I'm trying to implement a bunch of class methods in an ORM object in
> > order to provide functionality similar to Rails' ActiveRecord. This
> > means that if I have an SQL table mapped to the class "Person" with
> > columns name, city, and email, I can have class methods such as:
> >
> > Person.find_by_name
> > Person.find_by_city_and_name
> > Person.find_by_name_and_city_and_email
> >
> > I have a metaclass generating basic properties such as .name and .city,
> > but I don't want to generate a class method for every permutation of
> > the attributes. I'd like to have something much like __getattr__ for
> > instance attributes, so that if a method like
> > Person.find_by_city_and_email cannot be found, I can construct a call
> > to the basic find method that hides the SQL. Is there any way of doing
> > this, ...
>
> Sure, define __getattr__ on the type of the class i.e., the metaclass, just as
> you define it on a class to provide default-attribute-lookup to its instances:
>
>   >>> class A(object):
>   ... class __metaclass__(type):
>   ... def __getattr__(cls, attr):
>   ... return "%s.%s" % (cls.__name__, attr)
>   ...
>   >>> A.somefunc
>   'A.somefunc'
>   >>> A.someotherfunc
>   'A.someotherfunc'
>   >>>
>
> HTH
>
> Michael

Thanks! I only recently realized that I would have to learn metaclasses
in order to make this work, and I'm still a bit unclear on their
properties.

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


Re: What editor shall I use?

2006-02-08 Thread Dylan Moreland

Radek Kubicek wrote:
> > What editor shall I use if my Python script must contain utf-8
> > characters?
> > I use XP
>
> vim :-)
>
> > Thank you for reply
> > l.b.
>
> not for all :-)

I myself have just begun using vim. Does anyone have any
tips/convenient tweaks for python programming with it?

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


Re: how to remove using replace function?

2006-02-08 Thread Dylan Moreland
I think you want to use the replace method of the string instance.
Something like this will work:

# See http://docs.python.org/lib/string-methods.html#l2h-196
txt = "an unfortunate  in the middle"
txt = txt.replace("", "")

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


Re: how to remove using replace function?

2006-02-08 Thread Dylan Moreland

[EMAIL PROTECTED] wrote:
> nope didn't work

Could you be more specific about the error? Both my example and yours
work perfectly on my box.

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


Re: Newbie Q: dynamically assigning object attribute

2006-02-09 Thread Dylan Moreland
Take a look at the built-in function setattr:

http://ftp.python.org/doc/lib/built-in-funcs.html#l2h-64

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


Re: Newbie Q: dynamically assigning object attribute

2006-02-09 Thread Dylan Moreland
No problem. There are, in fact, ugly class-based methods of doing this.
You could assign to an instance's __dict__ dictionary, or -- if the
class is a new-style class -- you can call the __setattr__(self, name,
value) method.

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


Re: Splitting a string

2006-02-14 Thread Dylan Moreland
Take a look at:

http://docs.python.org/lib/node115.html#l2h-878

So I would try something like:

pat = re.compile(r" (?:AND|OR|AND NOT) ")
pat.split(string)

Compile the regular expression with re.IGNORECASE if you like.

Nico Grubert wrote:
> Dear Python users,
>
> I'd like to split a string where 'and', 'or', 'and not' occurs.
>
> Example string:
> s = 'Smith, R. OR White OR Blue, T. AND Black AND Red AND NOT Green'
>
> I need to split s in order to get this list:
> ['Smith, R.', 'White', 'Blue, T.', 'Back', 'Red', 'Green']
>
> Any idea, how I can split a string where 'and', 'or', 'and not' occurs?
> 
> 
> Thank you very much in advance,
> Nico

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


Re: Splitting a string

2006-02-14 Thread Dylan Moreland
Woops! Thanks for the correction. I was assuming greediness for some
reason.

Fredrik Lundh wrote:
> Dylan Moreland wrote:
>
> > So I would try something like:
> >
> > pat = re.compile(r" (?:AND|OR|AND NOT) ")
> > pat.split(string)
>
> footnote: this yields:
>
> ['Smith, R.', 'White', 'Blue, T.', 'Black', 'Red', 'NOT Green']
>
> (the | operator picks the first (leftmost) alternative that results in an
> overall match.)
> 
> 

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


Re: how to write a C-style for loop?

2006-02-14 Thread Dylan Moreland

John Salerno wrote:
> I assume this is the way for loops are written in C, but if it helps to
> be specific, I'm referring to C# for loops. The Python for loop seems to
> be the same (or similar) to C#'s foreach loop:
>
> foreach int i in X
>
> But how would you write a C# for loop in Python? Do you rework a while
> loop, or use the range() function?
>
> Here's an example:
>
> for (int i = 0; i < 50; i += 5)
>
> How would that go in Python, in the simplest and most efficient way?
>
> Thanks.

Take a look at the range() builtin. That should give you what you need.

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


Re: looping over more than one list

2006-02-16 Thread Dylan Moreland
> def lowest(s1,s2):
> s = ""
> for c1,c2 in [x for x in zip(s1,s2)]:
> s += lowerChar(c1,c2)
> return s
>
> but it's hardly any more elegant than using a loop counter, and I'm
> guessing it's performance is a lot worse - I assume that the zip
> operation is extra work?
>
> Iain

Always look in itertools for stuff like this:

http://docs.python.org/lib/itertools-functions.html#l2h-1392

for c1, c2 in itertools.izip(s1, s2):
...

I haven't profiled it, but it makes sense that this would be fast.

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


Re: general coding issues - coding style...

2006-02-18 Thread Dylan Moreland

calmar wrote:
> On 2006-02-18, Diez B. Roggisch <[EMAIL PROTECTED]> wrote:
> >   - why are these {{{ thingies there?
>
> markers for folding for vim
> http://www.calmar.ws/tmp/sc.png

I would look into one of the many Vim scripts which automatically fold
most large blocks without the ugly {{{.

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


Re: Getting a list of all classes derived from a base class

2006-04-02 Thread Dylan Moreland
Vijairaj  R wrote:
> Hi,
> I have a requirement to findout all the classes that are derived from a
> single base class.
>
> This is how I do it currently.
>
> class Test:
> case = []
>
> class Test1(Test):
> Test.case.append("Test1")
>
> class Test2(Test):
> Test.case.append("Test2")
>
> 1. Is there a better way of doing this.
> 2. Is there a way to generalize the Test.case.append("TestN")
> statements to something like
> Test.case.append(__myclass__)
>
> --
> Warm Regards,
> Vijairaj

If you're willing to use metaclass madness:

class TestMeta(type):
def __init__(cls, name, bases, dct):
if bases == (object,): return # Prevent the appending of Test
cls.case.append(cls)

class Test(object):
__metaclass__ = TestMeta
case = []

class Test1(Test): pass
class Test2(Test): pass

print Test.case

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


Re: Getting a list of all classes derived from a base class

2006-04-03 Thread Dylan Moreland
Alex Martelli wrote:
> Vijairaj  R <[EMAIL PROTECTED]> wrote:
>...
> > class Test:
>
> do not use old-style classes: they exist ONLY for backwards
> compatibility.
>
> Make you class Test new-style:
>
> class Test(object):
>...
>
>
> and you can call Test.__subclasses__() to get a list of all extant
> subclassed of Test at any time.
>
>
> Alex

Thanks Alex. That's a new one to me -- new-style classes have so many
strange properties.

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


Re: - Removing Need For Repeated Definition of __str__

2006-04-03 Thread Dylan Moreland
Ilias Lazaridis wrote:
> I have some python code which looks similar to this:
>
> class Car(BaseClass) :
>manufacturer = factory.string()
>model = factory.string()
>modelYear = factory.integer()
>
>def __str__(self):
>return '%s %s %s' % (self.modelYear, self.manufacturer,
> self.model)
>
> def factory.string(self)
>   s = String() # creates a string object
>   #... # does several things
>   return s # returns the string object
>
> -
>
> I would like to simplify it in this way:
>
> class Car(BaseClass):
>manufacturer = factory.string(2)  # 2 = position number...
>model = factory.string(3) # ...withinn __str__
>modelYear = factory.integer(1)
>
> def factory.string(self, position)
>   s = String() # creates a string object
>   ...  # does several things
>
   # creates somehow the __str__ functionality...
>
>   return s # returns the string object
>
> -
>
> How could I achieve this?
>
> .
>

I'm slightly confused about your use of factory functions for making
instance variables (perhaps you could explain that?). Without knowing
more about that, here's a mixin solution I've used in the past (note
that __strdef__ is something I just made up):

class SmartStr(object):

def __str__(self):
return "<%s %s>" % (self.__class__.__name__,
", ".join(attrname + "=" + str(getattr(self, attrname))
  for attrname in self.__strdef__))

class Car(SmartStr):

__strdef__ = ["model_year", "manufacturer", "model"]

def __init__(self, manufacturer, model, model_year):
self.manufacturer = manufacturer
self.model = model
self.model_year = model_year

c = Car("Toyota", "Camry", 1990)
print c # => 

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