Re: executing list of methods (and collecting results)

2007-09-20 Thread gherzig
> Gerardo Herzig wrote:
>
>> I want the collect_validators() method is to execute any of the
>> above methods, and collect their names as items of a list (wich
>> will be the collect_validators() return value).
>
> (inside class definition -- untested)
> validators = {"is a number": is_really_a_number,
>   "is even": is_even,
>   "is greater than zero": is_greater_than_zero}
>
> def collect_validators(self):
>return [desc for desc, func in self.validators.items() if func()]
>
Excelent!!!
>> My first approach is:
>
> ... no method, but a generator. Executing it will give you a
> generator object instead of a result list.
>
>> [code]
>> def collect_validators(self):
>> v_dict = { 'is_really_a_number': is_really_a_number,
>>   'is_even': is_even,
>>   'is_greater_than_zero', is_greater_than_zero
>>}
>>
>>for name, meth in v_dict.items():
>>   result = meth()
>>   if result: yield name
>> [/code]
>>
>> I wondering if is this a good pattern to apply, i like the way it
>> looks like, at least to me it looks `natural',
>
> IMHO, it doesn't look natural. It depends on what you want to
> achieve. This generator will need to be iterated over until it
> is "exhausted".
Im having some fun doing a mail filter. A master thread will fire several
threads (each one returning a list with the matched validators) and
collect the results of each one of them.
>
>> but...im calling every method twice here?
>
> No. Methods are only called if you apply the function call
> operator, "()".
>
> BTW, I hope you don't really want to test a number to be greater
> than zero, or even, by using an own method, respectively, just to
> test this.
Haha, no, the actual methods do other kind of things.
Thanks Björn!!!

Cheers.
Gerardo


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


Re: migrating to packages

2007-10-03 Thread gherzig
>
> On Oct 3, 2007, at 11:42 AM, Gerardo Herzig wrote:
>
>> Hi all. I have a single file with several classes, wich i want to
>> separate into several packages.
>> The big file is named, say MYCLASES, and contains a class named
>> A(object), and B(A).
>>
>> We have been using this MYCLASES in the
>> from MYCLASES import B syntax, but i cant reproduce this syntax using
>> packages. Im forced to write from PACKAGE.B import *, and if that
>> means
>> i have to touch so many files, that would really be a problem to me.
>> There is (i cant find it yet) a place where i can read about that kind
>> of package 'migration' situation?
>>
>> Because i *really* want to keep the FROM PACKAGE import B syntax.
>
> http://www.python.org/doc/current/tut/
> node8.html#SECTION00840
I have already read those files (and i just read it again), but still cant
find the solution. Maybe i just do not explain myself in the correct way
(or  i am very very stupid :)
I will expose my case quicly.
The MYCLASES.py file contains the A class, so i can use
from MYCLASES import A
a = ()

Using the "package mode" (wich looks fine BTW), having the simple
MYCLASES/
 __init__.py
 A.py

forces my (i guess) to use the
from MYCLASES.A import A

which is not what i want, because the big amount of files i will have to
modify (and im not alones, there is a group of dudes who use this
MYCLASES)

Im i missing something in that docs you post?
Thanks!!

Gerardo

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


Re: multimethods decorator

2007-10-10 Thread gherzig
> Gerardo Herzig a écrit :
>> Hi all. Im reading the Gido's aproach using decorators at
>> http://www.artima.com/weblogs/viewpost.jsp?thread=101605
>>
>> It looks good to me, but the examples shows the functionality using
>> functions.
>> Now, when i try to give this decorator into a method, if i try the
>>
>> class test(object):
>>@multimethod(...)
>>def met(self, ...):
>>
>> The multimethod decorator needs the types of the arguments, and, if the
>> met method requires self as the first argument, the multimethod should
>> look like
>> @multimethod(self.__class__, bla, ble) or some like that...
>>
>> Now i know that im wrong, because i have this error
>>  >@multimethod(self.__class__)
>>  >NameError: name 'self' is not defined
>
> Indeed. Neither self (which will only be known at method call time) nor
> even the 'test' class (which is not yet defined when the decorator is
> executed) are availables.
Doh!
>
>> So what would be the first argument to @multimethod??
>
> A string ?-)
Ah? And what will that string contains?
>
> FWIW, there's already an implementation of multiple dispacth by Mr. Eby...
Oh yes, i found the dispatch version of multimethods, but i have not tried
it yet. Do you think is better this version than Guido's?

Thanks!
Gerardo

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


Re: sharing objects between classes

2008-01-28 Thread gherzig
> Diez B. Roggisch a écrit :
>> Gerardo Herzig wrote:
>>
>>> Hi all. Im wondering the way to share a database connection between
>>> some
>>> classes:
>>>
>>> So far, i came up with a simple class schema, where each class means
>>> each different relation, i mean i have the follow classes
>>>
>>> class Database(object):
>>>   ## make the connection
>>>   self.conn = make_conn()
>>>
>>> class Table(object):
>>>   def get_fields:
>>> 
>>>
> (snip)
>>
>> Take a look at the sources of e.g. SQLObject and how they do it (in SO,
>> the
>> concept is called "HUB")
>>
> And while you're at it, take a look at SQLAlchemy too, and ask yourself
> if you really need to roll your own solution !-)

Yes, i dont need to reinvent the wheel, i know, it just seems like a
pattern i will have to deal with, not just in this case. SQLObject seems
like a big peace of code to read. At least to me (not that good
programmer).

I will take a look at SQLAlchemy, and see a little more.
Thanks!

Gerardo


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


Re: Weird cgi error

2008-02-25 Thread gherzig
>
>> This is some kind of crooked game, right? Your code works fine on a
>> local server, and there's no reason why it shouldn't work just fine on
>> yours either. All you are changing is the standard input to the process.
>>
>> Since you claim to have spotted this specific error, perhaps you'd like
>> to explain just exactly how you came across it. I mean that's a pretty
>> specific input to test with ...
>>
>> Frankly I am not sure you are telling the truth about the code behind
>> that page. If you *are* then you'd better provide specifics: Python
>> version, Apache version, httpd.conf file, and so on. April 1 is still
>> over a month away.
>>
>> regards
>>   Steve
>>
>> PS: consider closing the  tag on the same line as the opening
>> tag to avoid spurious spaces in your pristine form.
>> --
>> Steve Holden+1 571 484 6266   +1 800 494 3119
>> Holden Web LLC  http://www.holdenweb.com/
>
> Thanks for the reply.
>
> No, it's not a game, crookedgames.com is a mostly defunct games site
> that I was working on for a while.  I'm just hosting the script
> there.  What I am actually working on is a tool used to compare
> various things.  Check it out here:
> http://crookedgames.com/cgi-bin/Language_Comparison.py
> Here's some input you can use to test with:
>
> Cats
>   +2 Fuzzy
>   -1 Medium Maintenance
>
> Fish
>   +1 Low Maintenance
>   -1 Stupid
>
> Dogs
>   +2 Fuzzy
>   -2 High Maintenance
>
> (note that there's supposed to be two spaces before the +/- symbols --
> in case my formatting doesn't go through)
>
> I originally created that tool because I wanted to compare programming
> languages, python among them, thus leading me discover this issue.
>
> Now, I'm very new to this web development stuff (this is my first real
> app), so it's quite likely that I'm just doing something stupid, but I
> can't figure out what.
>
> I'm using LunarPages.  CPanel reports my Apache version as: 1.3.37
> (Unix)
>
> I added the line "print sys.version" to the test script, and that
> spits out: "2.3.4 (#1, Dec 11 2007, 05:27:57) [GCC 3.4.6 20060404 (Red
> Hat 3.4.6-9)]"
>
> I can't find any file called httpd.conf.  It would be in /etc, right?
> I guess I don't have one.
>
> Still having the same problem.
>
> Here's the new contents of test.py:
>
> #!/usr/bin/python
> import cgitb, sys
> cgitb.enable()
>
> print "Content-Type: text/html\n"
> print sys.version
> print """
> 
> 
>   
>  
> 
>   
> 
> 
> """
>
> It's not a joke, honest :)
> --
It just doesnt make sense to me. I guess we all agree that is not a python
problem, because that code does actually nothing but showing the form.

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