Re: Handling test data that depends on temporal data (that might change while the test runs)

2008-05-15 Thread Erik Jones
On Thu, May 15, 2008 at 12:21 PM, Marcelo de Moraes Serpa <
[EMAIL PROTECTED]> wrote:

> Hello,
>
> So, I have this particular method, generate_chat_dir_string, which should
> generate a string in the following format:
>  "md5hexstring-day-month-year-hour-minute"
>
> This string will be used to create a directory in the filesystem.
>
> I'm trying to adopt the TDD approach, so, I'm starting by testing all the
> methods of the Chat class, including this one. However, an issue just popped
> up, this is the test code:
>
>
>1.   def test_generate_chat_dir_string(self):
>2. import md5
>3. import random
>4. import datetime
>5. md5_handler = md5.new()
>6. users = [{"user":"pedro","atendente":False},{
>"user":"joão","atendente":True}]
>7. #salt = random.random().to_s()
>8. #md5_handler.update(salt)
>9. now = datetime.datetime.now()
>10. ts = now.strftime("%d-%m-%Y-%H-%M")
>11. for user in users:
>12. md5_handler.update(user["user"])
>13.
>14.
>15. seed = md5_handler.hexdigest()
>16.
>17. final_string = seed + "-" + ts
>18.
>19. method_generated_string =
>self.chat.generated_chat_dir_string(users,seed)
>20.
>21. self.assertEquals(final_string,method_generated_string)
>
> As you can see, it generates a specific set of data and assembles a
> final_string. It then feeds the same data to generate_chat_dir_string and
> compare to see if the final_string is equal to the string generated by the
> tested method.
>
> However, they might not be equal becouse of the temporal data. In this case
> I'm not using seconds, but in a bizarre situation a minute could have passed
> and the test would faile becouse the two strings would have a different
> minute portion. Something like this:
>
> "b2ef9c7b10eb0985365f913420ccb84a-30-10-2008-10-31"
> "b2ef9c7b10eb0985365f913420ccb84a-30-10-2008-10-32"
>
> How could I handle this issue?
>
> Thanks in advance,


Add another parameter, with a default, to the method's interface:

def test_generate_chat_dir_string(self, now=None):
if not now:
import datetime
now = datetime.datetime.now()
.


That way you can feed it values when testing to validate the calculations
but leave it up to the datetime module to fill in the used value in
production.



-- 
Erik Jones
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list

Re: fork/exec with input redirection

2007-11-26 Thread Erik Jones
On Nov 26, 2007, at 3:58 PM, Dan Upton wrote:

> I have a Python script that does a fork/exec, so the parent process
> can get the child's PID and monitor /proc/PID/stat (on a CentOS
> system).  Most of my processes' command lines are straightforward
> enough to do this with, but I have a handful that use < on the command
> line, eg
>
> ./gobmk_base.linux_x86 --quiet --mode gtp < 13x13.tst
>
> The only thing I could really think of to try was
>
>os.execv("./gobmk_base.linux_x86", ["./gobmk_base.linux_x86",
> "--quiet", "--mode", "gtp", "<", "13x13.tst"])
>
> but this apparently doesn't work.  Is there some other way to
> accomplish what I'm going for?

Read up on the docs for the subprocess module.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: basic if stuff- testing ranges

2007-11-26 Thread Erik Jones
On Nov 26, 2007, at 2:29 AM, Peter Otten wrote:

> Donn Ingle wrote:
>
>>> x in range(1,20) ?
>> Sure, that's okay, but it has clarity issues, and is calling a func.
>
> and it requires that x is integral (1.0 is in the range, 1.001 is  
> not),
> and becomes dog slow when the range gets larger. Not a good idea.

That is because range() is not a range in the abstract sense (i.e.  
simply defining bounds that can be tested for set membership) but are  
used to create lists (or, in the case of xrange(), successive values)  
between the bounds given in the params.  So, saying x in range(1,20)  
is not asking if x is between 1 and 20 but, rather, if x is a member  
of the values genereated by the range function with params 1 and 20.   
So, yes, using range()

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: basic if stuff- testing ranges

2007-11-26 Thread Erik Jones

On Nov 25, 2007, at 9:49 PM, Donn Ingle wrote:

>>> if 0 > x < 20: print "within"
>> That means "if x LESS THAN 0 and x < 20".
> Oh, bugger. It's tricky.
>> So try
>> if 0 < x < 20:
> Thanks. I was flipping signs in my tests, but I guess I flipped  
> both and got
> myself all confused.
>
>> Likely manuals: Tutorial & Reference
>> Tutorial: check contents, "if statement" looks possible, but no luck
> Yes, I got that far.
>> Reference: check contents, "comparisons" looks possible, and
> Thanks again. I find the reference is laid-out in a way that I  
> don't find
> intuitive and every time I look for something I fail. I even grep  
> through
> the folder to get a clue, which shows how poor the index is (to me)!

Then use one of the quick references here: http://rgruet.free.fr/.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: a simple tcp server sample

2007-11-07 Thread Erik Jones
On Nov 7, 2007, at 12:54 PM, Tzury Bar Yochay wrote:

> hi, the following sample (from docs.python.org) is a server that can
> actually serve only single client at a time.
>
> In my case I need a simple server that can serve more than one client.
> I couldn't find an example on how to do that and be glad to get a
> hint.
>
> Thanks in advance

That's when it stops being simple.  You'll need to spawn threads or  
fork off separate processes to do that.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: pygresql

2007-11-04 Thread Erik Jones
On Nov 4, 2007, at 9:45 AM, JD wrote:

> Hi there.
>
> I'm trying to use python with postgresql. I decided to use psycopg to
> interact with the postgresql server.  When installing psycopg it
> appeared that I needed mxDateTime. So I decided to install the mxbase
> package.
>
> I received the following error message (the interesting bit seems to
> be at the end):
>
>
> [EMAIL PROTECTED]:/var/lib/postgresql/mxbase$ sudo python setup.py
> install
> running install
> running build
> running mx_autoconf
> gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O2 -Wall -Wstrict-
> prototypes -fPIC -D_GNU_SOURCE=1 -I/usr/local/include -I/usr/include -
> c _configtest.c -o _configtest.o
> _configtest.c: In function 'main':
> _configtest.c:4: warning: statement with no effect
> gcc -pthread _configtest.o -L/usr/local/lib -o _configtest
> success!
> removing: _configtest.c _configtest.o _configtest
> gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O2 -Wall -Wstrict-
> prototypes -fPIC -D_GNU_SOURCE=1 -I/usr/include/python2.5 -I/usr/ 
> local/
> include -I/usr/include -c _configtest.c -o _configtest.o
> success!
> removing: _configtest.c _configtest.o
> macros to define: [('HAVE_STRPTIME', '1')]
> macros to undefine: []
> running build_ext
>
> building extension "mx.DateTime.mxDateTime.mxDateTime" (required)
> building 'mx.DateTime.mxDateTime.mxDateTime' extension
> gcc -pthread -fno-strict-aliasing -DNDEBUG -g -O2 -Wall -Wstrict-
> prototypes -fPIC -DUSE_FAST_GETCURRENTTIME -DHAVE_STRPTIME=1 -Imx/
> DateTime/mxDateTime -I/usr/include/python2.5 -I/usr/local/include -I/
> usr/include -c mx/DateTime/mxDateTime/mxDateTime.c -o build/ 
> temp.linux-
> i686-2.5_ucs4/mx-DateTime-mxDateTime-mxDateTime/mx/DateTime/ 
> mxDateTime/
> mxDateTime.o
> gcc: mx/DateTime/mxDateTime/mxDateTime.c: No such file or directory
> gcc: no input files
> error: command 'gcc' failed with exit status 1
>
>
> I googled "error: command 'gcc' failed with exit status 1" and
> interestingly a lot of the results seemed to be linked with python. I
> can confirm that I do have gcc installed. One post seemed to suggest
> that I may be using too new a version of gcc. Do you think this is the
> problem or am I going astray somewhere else?
>
> Thank you very much in advance for any assistance,
> James.

You shouldn't be using psycopg, it's not supported anymore.  Use  
psycopg2 which is in active development and has no dependecies on any  
of the mx libraries.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Can local function access local variables in main program?

2007-11-03 Thread Erik Jones
On Nov 3, 2007, at 5:32 PM, Pawel wrote:

> Stargaming wrote:
>> You can make this work using the `global` statement::
>>
>>>>> def foo():
>> ... global x
>> ... print x
>> ... x = 0
>
> Is there any way to disable global for x? Something like that:
>>>> x = 12345
>>>> def foo():
> ... global x
> ... print x
> ... noglobal(x)  # ???
> ... x = 0# now this is local x
>>>> foo()
> 12345
>>>> print x
> 12345

Why would you need to do that?  It would be confusing.  Just use a  
different variable name for the local.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Copy database with python..

2007-11-03 Thread Erik Jones
On Nov 2, 2007, at 11:49 AM, Diez B. Roggisch wrote:

> Abandoned wrote:
>
>> On Nov 2, 4:19 pm, Paul McNett <[EMAIL PROTECTED]> wrote:
>>> Abandoned wrote:
>>>> Hi.
>>>> I want to copy my database but python give me error when i use this
>>>> command.
>>>> cursor.execute("pg_dump mydata > old.dump")
>>>> What is the problem ? And how can i copy the database with python ?
>>>
>>> You are just going to have to give us more to go on. Please post the
>>> entire traceback of the error that you see (copy/paste it from your
>>> terminal).
>>>
>>> You can't issue system commands using the cursor's execute()  
>>> method and
>>> expect that to work. execute() is for executing SQL, DML, or DDL,  
>>> not
>>> for doing shell stuff.
>>>
>>> Try:
>>>
>>> import os
>>> os.system("pg_dump mydata > /tmp/old.dump")
>>>
>>> but I'm not versed in postgressql, so this probably won't work  
>>> exactly
>>> as written. You'd need to run that code from the server hosting the
>>> postgresql database.

Not true, you can run the dump from any server that can connect to  
the database server (based on network connectivity and the connection  
permissions in your pg_hba.conf file).  Just be sure to use the -h  
(host) -p (port) and -U (username) flags when execute pg_dump.

>>>
>>>> Note: The database's size is 200 GB
>>>
>>> Well, then you may want to make sure you have enough room on the  
>>> target
>>> volume before trying to dump the file! Note that the dump file could
>>> conceivably be much larger (or much smaller) than the database  
>>> itself.

If you use the -F c (pg_dump -F -c -f your_dmp.file your_db) option  
you'll get a compressed dump file which can be as little as 10% (or  
less) the size of your database.
>>>
>>> --
>>> pkm ~http://paulmcnett.com
>>
>> Are there any way to copy database without dump or any temp files ?
>> (If there is a temp my harddisk not enough for this operation :( )

If you can stop the database then you can just do a manual copy  
(using cp, rsync, or whatever) of the entire pg data directory but  
that will require the same amount of space as the original database.   
If you're goal is to make a backup, pg_dump is the way to go.  Make  
note, though, pg_dump will only dump one database so if you have more  
than one database in your postgres "cluster" then you'll need to use  
pg_dumpall in which case you don't have the compression (-F c)  
option.  There are other caveats between the two as well.  Just be  
sure to read the documentation.  Also, for other postgres questions,  
you should join one of the postgres mailing lists.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


OT Re: Parallel insert to postgresql with thread

2007-10-25 Thread Erik Jones
If you're not Scott Daniels, beware that this conversation has gone  
horribly off topic and, unless you have an interest in PostreSQL, you  
may not want to bother reading on...

On Oct 25, 2007, at 9:46 PM, Scott David Daniels wrote:

> Erik Jones wrote:
>>
>> On Oct 25, 2007, at 7:28 AM, Scott David Daniels wrote:
>>> Diez B. Roggisch wrote:
>>>> Abandoned wrote:
>>>>> Hi..
>>>>> I use the threading module for the fast operation. But 
>>> [in each thread]
>>>>> def save(a,b,c):
>>>>> cursor.execute("INSERT INTO ...
>>>>> conn.commit()
>>>>> cursor.execute(...)
>>>>> How can i insert data to postgresql the same moment ?...
>>>>
>>>> DB modules aren't necessarily thread-safe. Most of the times, a
>>>> connection (and ... cursor) can't be shared between threads.
>>>> So open a connection for each thread.
>>>
>>> Note that your DB server will have to "serialize" your inserts, so
>>> ... a single thread through a single connection to the DB is the way
>>> to go.  Of course it (the DB server) may be clever enough to behave
>>> "as if" they are serialized, but most of your work parallelizing at
>>> your end simply creates new work at the DB server end.
>>
>> Fortunately, in his case, that's not necessarily true  If he
>> goes the recommended route with a separate connection for each  
>> thread,
>> then Postgres will not serialize multiple inserts coming from  
>> separate
>> connections unless there is something like and ALTER TABLE or REINDEX
>> concurrently happening on the table.
>> The whole serialized inserts thing is strictly something popularized
>> by MySQL and is by no means necessary or standard (as with a lot of
>> MySQL).
>
> But he commits after every insert, which _does_ force serialization  
> (if
> only to provide safe transaction boundaries).  I understand you can  
> get
> clever at how to do it, _but_ preserving ACID properties is exactly  
> what
> I mean by "serialize,"

First, bad idea to work with your own definition of a very domain  
specific and standardized term.  Especially when Postgres's Multi- 
Version Concurrency Control mechanisms are designed specifically for  
the purpose of preserve ACID compliance without forcing serialized  
transactions on the user.

Second, unless he specifically sets his transaction level to  
serializable, he will be working in read-committed mode.  What this  
specifically means is that two (or more) transactions writing to the  
same table will not block any of the others.  Let's say the user has  
two concurrent inserts to run on the same table that, for whatever  
reason, take a while to run (for example, they insert the results of  
some horribly complex or inefficient select), if either is run in  
serializable mode then which ever one starts a fraction of a second  
sooner will run until completion before the second is even allowed to  
begin. In (the default) read-committed mode they will both begin  
executing as soon as they are called and will write their data  
regardless of conflicts.  At commit time (which may be sometime later  
for transactions with multiple statements are used) is when conflicts  
are resolved.  So, if between the two example transactions there does  
turn out to be a conflict betwen their results, whichever commits  
second will roll back and, since the data written by the second  
transaction will not be marked as committed, it will never be visible  
to any other transactions and the space will remain available for  
future transactions.

Here's the relevant portion of the Postgres docs on all of this:  
http://www.postgresql.org/docs/8.2/interactive/mvcc.html

> and while I like to bash MySQL as well as the
> next person, I most certainly am not under the evil sway of the vile
> MySQL cabal.

Good to hear ;)

>
> The server will have to be able to abort each transaction
> _independently_ of the others, and so must serialize any index
> updates that share a page by, for example, landing in the same node
> of a B-Tree.

There is nothing inherent in B-Trees that prevents identical datum  
from being written in them.  If there was the only they'd be good for  
would be unique indexes.  Even if you do use a unique index, as noted  
above, constraints and conflicts are only enforced at commit time.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Parallel insert to postgresql with thread

2007-10-25 Thread Erik Jones
On Oct 25, 2007, at 10:12 AM, Jean-Paul Calderone wrote:

> On Thu, 25 Oct 2007 09:46:54 -0500, Erik Jones <[EMAIL PROTECTED]>  
> wrote:
>>
>> [snip]
>>
>> Fortunately, in his case, that's not necessarily true.  If they do
>> all their work with the same connection then, yes, but there are
>> other problems with that as mention wrt thread safety and psycopg2.
>> If he goes the recommended route with a separate connection for each
>> thread, then Postgres will not serialize multiple inserts coming from
>> separate connections unless there is something like and ALTER TABLE
>> or REINDEX concurrently happening on the table.  The whole serialized
>> inserts thing is strictly something popularized by MySQL and is by no
>> means necessary or standard (as with a lot of MySQL).
>
> PostgreSQL won't serialize inserts to the same table with a sequence
> column?  Wow. :)

No, because sequences are separate relations that are transaction- 
safe, which is Postgres multi-process/session version of thread  
safe.  To be more specific, there's really no such thing as a  
"sequence column" in Postgres.  I wan't go into more detail here  
since this is a Python list, but if you want more info check out the  
Postges site and manual, the mailing lists, or respond directly to me  
if you just want to know how this particular bit works better.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Parallel insert to postgresql with thread

2007-10-25 Thread Erik Jones

On Oct 25, 2007, at 7:28 AM, Scott David Daniels wrote:

> Diez B. Roggisch wrote:
>> Abandoned wrote:
>>
>>> Hi..
>>> I use the threading module for the fast operation. But 
> [in each thread]
>>> def save(a,b,c):
>>> cursor.execute("INSERT INTO ...
>>> conn.commit()
>>> cursor.execute(...)
>>> How can i insert data to postgresql the same moment ?...
>>
>> DB modules aren't necessarily thread-safe. Most of the times, a  
>> connection
>> (and of course their cursor) can't be shared between threads.
>>
>> So open a connection for each thread.
>
> Note that your DB server will have to "serialize" your inserts, so
> unless there is some other reason for the threads, a single thread
> through a single connection to the DB is the way to go.  Of course
> it may be clever enough to behave "as if" they are serialized, but
> mostly of your work parallelizing at your end simply creates new
> work at the DB server end.

Fortunately, in his case, that's not necessarily true.  If they do  
all their work with the same connection then, yes, but there are  
other problems with that as mention wrt thread safety and psycopg2.   
If he goes the recommended route with a separate connection for each  
thread, then Postgres will not serialize multiple inserts coming from  
separate connections unless there is something like and ALTER TABLE  
or REINDEX concurrently happening on the table.  The whole serialized  
inserts thing is strictly something popularized by MySQL and is by no  
means necessary or standard (as with a lot of MySQL).

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: greatest and least of these...

2007-10-23 Thread Erik Jones
On Oct 23, 2007, at 10:56 AM, Shawn Minisall wrote:

> I just wrote a program to let the user input a series of whole numbers
> and tell them which is least and which is greatest based off of a  
> menu.
> However, the menu isn't kicking in after they pick a number.  I  
> included
> a while statement for a loop just for the menu and compared it to my
> other programs that have a similar setup and are working, but I'm
> stumped.   Here's the program...
>
> def main():
>
> #define and initialize variables
> #choice as int
> choice = 0
> #number as int
> number = 0
>
> #intro
> print "WELCOME TO THE GREATEST AND LEAST NUMBER PROGRAM!"
> print
>
> #Menu loop
> while choice != 2:
> #display menu
> print "Please choose from the following menu: "
> print "1. Enter a number"
> print "2. Exit"
> print
>
> #prompt user for their menu choice
> choice = input("Enter your choice here: ")
>
> #if statements to determine which choice
> if choice == 1:
> nums = []
> while number >=0:
> nums.append(number)
> number = input("Please enter a number.")

You're telling the script to continue asking for numbers until a non- 
positive number is entered.  Enter a negative number and you'll see  
your menu again.  What you probably want is to change this.

>
>
> elif choice == 2:
> print "Have a great day!"
>
> if len(nums) > 0:
> print "The smallest number that you entered  
> was:",min(nums)
> print "The largest number that you entered was:",max 
> (nums)
>
> else:
> #invalid
> print "Invalid selection.  Enter either one or two."
> print
>
> Also, if they quit the program with choice #2 and entered numbers, it
> should display the greatest and least of them.  If they just  
> started and
> didn't enter anything and want to quit, I get an error message saying
> UnboundLocalError: local variable 'nums' referenced before assignment.
> Isn't the if statement supposed to keep python from going there  
> since if
> they didn't enter any input, the length of the list should just be  
> zero.

The exception is because you're not ensure that nums is ever  
initialized.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Can't Write to PostGIS PostGreSQL database via psycopg2

2007-10-22 Thread Erik Jones

On Oct 22, 2007, at 8:19 PM, David Michael Schruth, wrote:

> Hi,
>
> I am sort of in a jam here.  I am using the PsycoPG2 library to read
> data out of a windows XP based PostGIS / PostGreSQL database but I am
> apparently unable to write (update or insert) even though I am able to
> read (select)
>
> I am using PsycoPG2 2.0.6 (psycopg2-2.0.6.win32-py2.5-pg8.2.4-
> release.exe )
> with Python 2.5 (python-2.5.msi)
> and PostGres 8.2.5.-1   (postgresql-8.2.5-1.zip)
> and PostGIS 8.2 (postgis-pg82-setup-1.3.1-1.exe)
>
> I can use PGadminIII to paste the same SQL queries and execute just
> fine, but when I try to execute them via python code like
>
> import psycopg2
> conn = psycopg2.connect("dbname='postgis' user='postgres'
> host='localhost' password='12345'")
> c=conn.cursor()
> c.execute("""INSERT INTO thetable (name) VALUES ('asdf');""")
> c.execute("""UPDATE thetable SET name = 'somename' WHERE id = 321;""")
> print(c.statusmessage)
> #returns "INSERT 0 1" and "UPDATE 0" respectively
>
> It gives me very specialized table specific error messages if the
> query is wrong, but when it's correct, it does nothing and doesn't
> update.   The only way I can update is pasting the query into the
> PgAdminIII query window.
>
> This is a problem on two separate machines (XP and Windows 2003
> server) with all of the above components installed by two different
> people.''

The return value of the insert of 'INSERT 0 1' indicates that one row  
was inserted so the insert certainly worked.  If you're not seeing  
the results when you look at the database after the script has run  
it's probably because you need to call conn.commit() after your  
execute statements in order to commit your transaction as psycopg  
does not, by default, run in "autocommit" mode.  The update's return  
value of 'UPDATE 0' indicates that the where condition of your update  
query did not, in fact, match any existing rows in your table.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: web.py & postgresql error

2007-10-22 Thread Erik Jones
On Oct 22, 2007, at 8:06 PM, [EMAIL PROTECTED] wrote:

> hi everyone, i'm very new to python and to this forum.  i'm actually
> just trying to work through the tutorial on webpy.org.  so far, so
> good, but as i tried to incorporate a postgresql database into the
> demo web app i'm receiving this error print out:
>
> Traceback (most recent call last):
>   File "/usr/lib/python2.5/site-packages/web/wsgiserver/__init__.py",
> line 624, in communicate
> req.respond()
>   File "/usr/lib/python2.5/site-packages/web/wsgiserver/__init__.py",
> line 357, in respond
> response = self.wsgi_app(self.environ, self.start_response)
>   File "/usr/lib/python2.5/site-packages/web/httpserver.py", line 200,
> in __call__
> return self.app(environ, xstart_response)
>   File "/usr/lib/python2.5/site-packages/web/http.py", line 255, in
> __call__
> return self.func(e, o)
>   File "/usr/lib/python2.5/site-packages/web/webapi.py", line 302, in
> wsgifunc
> _load(env)
>   File "/usr/lib/python2.5/site-packages/web/webapi.py", line 258, in
> _load
> load()
>   File "/usr/lib/python2.5/site-packages/web/webapi.py", line 253, in
> load
> db.connect(**config.db_parameters)
>   File "/usr/lib/python2.5/site-packages/web/db.py", line 265, in
> connect
> import pgdb as db
> ImportError: No module named pgdb
>
> any thoughts would be greatly appreciated.
> thanks,
> doug

Apparently you don't have pgdb installed or it's path isn't in your  
PYTHONPATH environment variable.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Order of tuples in dict.items()

2007-10-15 Thread Erik Jones

On Oct 15, 2007, at 6:07 PM, Steven D'Aprano wrote:

> On Mon, 15 Oct 2007 14:11:27 -0700, John Machin wrote:
>
>> On Oct 16, 12:47 am, Erik Jones <[EMAIL PROTECTED]> wrote:
>>
>>> Not between two consecutive reads, no.  However, after any  
>>> resizing of
>>> a dict the result of Python's hash function for any given newly
>>> inserted key is extremely likely to be different than it would have
>>> been before the resizing, i.e. the method may be the same, but the
>>> result is different.
>>
>> Could you please supply the basis for the above assertion? My  
>> reading of
>> the docs for the built-in hash function, the docs for an object's
>> __hash__ method, and the source (dictobject.c, intobject.c,
>> stringobject.c) indicate (as I would have expected) that the hash  
>> of an
>> object is determined solely by the object itself, not by the  
>> history of
>> insertion into a dict (or multiple dicts!?).
>>
>> Note that position_in dict = some_function(hash(obj),  
>> size_of_dict) ...
>> perhaps you are conflating two different concepts.
>
>
> The hash() function doesn't even take a dictionary as an argument  
> -- it
> simply can't be dependent on the history of insertions into the
> dictionary, because it can't know what dictionary to look at!
>
> But as you say, the position in the dictionary itself depends on the
> result of the hash function, and the size of the dictionary, and  
> what's
> already in the dict (that is to say, the history of insertions and
> deletions). That's how hash tables work.

John, sorry, I never saw your reply to my initial posting.  What I  
was referring to was indeed the some_function in your example, not  
the actual hash() function available in the standard library.  I was  
in no way conflating the two, the confusing is just one over the  
terminology as some_function IS a hash function.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Order of tuples in dict.items()

2007-10-15 Thread Erik Jones
On Oct 14, 2007, at 5:27 PM, Steven D'Aprano wrote:

> On Sun, 14 Oct 2007 13:26:27 -0700, Erik Max Francis wrote:
>
>> Will McGugan wrote:
>>
>>> If I have two dictionaries containing identical values, can I be  
>>> sure
>>> that the items() method will return tuples in the same order?
> [...]
>>> Can I rely on this behavior?
>>
>> Probably not.
>
> Definitely not. See Paul Hankin's earlier post in this thread.
>
>
>> Dictionaries do not have an ordering that you should
>> count on.  In practice, the order in which you get the items if you
>> iterate over a dictionary is dependent on the hashing function, which
>> can potentially change over time.
>
> Well, I suppose it *is* possible for Python to change its hash  
> function
> in some future version, but Python hashing is highly optimized,  
> very fast
> and not likely to change.
>
> It's especially not likely to change in the middle of a run of your
> program, say between calling a.items() and calling b.items().

Not between two consecutive reads, no.  However, after any resizing  
of a dict the result of Python's hash function for any given newly  
inserted key is extremely likely to be different than it would have  
been before the resizing, i.e. the method may be the same, but the  
result is different.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Python on imac

2007-10-14 Thread Erik Jones

On Oct 13, 2007, at 8:23 PM, Adam Atlas wrote:

> On Oct 13, 7:21 pm, John Velman <[EMAIL PROTECTED]> wrote:
>> I'm considering moving from Linux to imac.   I've recently  
>> returned to
>> Python (was never very expert) to develop a small gui  
>> application.  At
>> present I plan to use PyGTK with Pango and Cairo.
>>
>> What surprises may I be in for :-)
>>
>> (Currently using slackware 11.0 on an old (8 years) slow (400mhz)  
>> machine.)
>>
>> Thanks,
>>
>> John Velman
>
> Well... I think OS X currently comes with Python 2.3 or 2.4. I
> recommend getting Fink (fink.sourceforge.net), which is basically a
> DPKG/APT repository for OS X plus an added system for automatically
> building packages from source. It'll keep the system up to date with
> the latest 2.5 release (and future versions).
>
> If you're using PyGTK, you will need to install Apple X11 (can't
> remember if it's installed by default yet), since OS X's window
> manager is not X11-based. Fink can also install GTK+, etc. for you.
> Other than that, most things should work as on Linux, more or less.

He doesn't need Fink for up to date Python version as os x binaries  
are available from the www.python.org.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


stderr is a lame hack?

2007-10-12 Thread Erik Jones
So, I was just taking a look at doctest.py and saw this:

Then running the module as a script will cause the examples in the
docstrings to get executed and verified:

python M.py

This won't display anything unless an example fails, in which case the
failing example(s) and the cause(s) of the failure(s) are printed to  
stdout
(why not stderr? because stderr is a lame hack <0.2 wink>), and the  
final
line of output is "Test failed.".


What does he mean by stderr being a lame hack?


Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: PEP idea: Instrumented Python

2007-10-12 Thread Erik Jones
On Oct 12, 2007, at 12:10 PM, Jean-Paul Calderone wrote:

> On Fri, 12 Oct 2007 11:33:11 -0500, Erik Jones <[EMAIL PROTECTED]>  
> wrote:
>> [snip]
>>
>> This got me thinking about building a module that could be included
>> by projects that creates a socket and responds to messages on that
>> socket in a separate thread from the main app so that you can connect
>> to the app and explore its current state.  Take a look at the
>> evalexception module in Paste to see what he does.
>>
>
> Or manhole in Twisted.

Thanks for the tip!

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: PEP idea: Instrumented Python

2007-10-12 Thread Erik Jones
On Oct 12, 2007, at 10:09 AM, Christopher Nelson wrote:

> I was looking at adding dtrace-like dynamic tracing to Python.   
> Note that this isn't dtrace itself.  The basic rationale:
>
> 1. A lot of enterprise-level software is written in Python.  It is  
> difficult to impossible to reproduce the customer environment in a  
> test lab.  Sometimes applications hang mysteriously, and we are  
> unable to duplicate the problems in our environment.
> 2. dtrace is a good design.  It has little to no overhead for  
> collection points that are not collecting data.  Emulating that  
> behavior allows you to test what you fly and fly what you test.
> 3. Having the ability to only get trace data you want (or need)  
> instead of trace data for every instruction executed is very  
> useful.  It also reduces bandwidth requirements when archiving the  
> traces, or shipping them out over a network connection.
> 4. The ability to dynamically expand and contract the tracing scope  
> during a trace is vital to getting the right data.
>
> The general plan would be to provide collectors in all of the  
> public python API functions.  The filtering code would also be in  
> the local system, for speed and bandwidth-conserving purposes.  The  
> filters should generally be native code.  The data should be  
> writeable to a file or to a network socket.  In addition, there  
> should be a network socket listening for commands from a tracing  
> client.  The client would be able to upload additional filter  
> scripts and start or stop tracing transparently to the running source.
>
> For security there should be an authentication mechanism that  
> allows the application to decide whether or not a connection to the  
> trace facility should be allowed, and a way to disable the trace  
> listening port on startup.  (Or perhaps it should be disabled on  
> startup by default and enabled if so desired.)
>
> Finally, there should, of course, be Pythonic access to the trace  
> system.
>
> The essential differences between the current tracing system and  
> the proposal involve the selectable granularity, the transparent  
> remote access, and automatic triggering of collection points.
>
> Comments? Please make sure to reply to all, I am not subscribed to  
> the list.

I've recently starting thinking of something along these lines  
although I don't the need to alter existing libraries or even mimic  
Dtrace given Python's extremely dynamic nature and reflection  
capabilities.  Ian Bicking wrote something along these lines for  
Paste that is used in Pylons wherein exceptions raised by the  
application during debug mode drop you into an interactive debugger  
with a web interface -- very handy for developing webapps.

This got me thinking about building a module that could be included  
by projects that creates a socket and responds to messages on that  
socket in a separate thread from the main app so that you can connect  
to the app and explore its current state.  Take a look at the  
evalexception module in Paste to see what he does.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: storing meta data on dictionary keys

2007-10-11 Thread Erik Jones
On Oct 11, 2007, at 2:25 PM, Andreas Kraemer wrote:

> On Oct 11, 10:17 am, Erik Jones <[EMAIL PROTECTED]> wrote:
>
>> No, duck typing and inheritance are two different things.  Duck
>> typing is when you implement the same operations as another object or
>> class, whereas with inheritance you get the same implementation as
>> that of the parent class.
>
> Except when you override a method ...

Right.  But, that's specialization which is still part of the  
inheritance model, and not duck typing, since you're also inheriting  
state.  Duck typing is about implementing an interface and has  
nothing to do with state.  Put another way, with inheritance you're  
getting both behaviour and state, with duck typing you're mimicking  
behaviour.

>
>> With duck typing, an object "acts like"
>> another object in specific contexts.
>
> That's exactly what Str(str) does in the context dictionary look-
> up ...

No, in that case Str "is-a" str, it doesn't have to act like a str  
because it is one, whereas with my Node example, Node "acts-like" a  
str (or int or any other hashable).  Read the first sentence of the  
Wikipedia article on duck typing: http://en.wikipedia.org/wiki/ 
Duck_typing

>> With inheritance, an object is
>> the same general type as another object.  You don't want string
>> objects, you want objects that, in a specific case, act like strings
>> but in others, I'm sure, not.  How does a concept like string
>> substitution fit with your inherited objects?
>
> Since Str(str) inherits all methods of str, those methods that return
> other strings (always new objects since str is immutable) like e.g.
> replace() will return str and not Str(str), and don't have the meta
> data copied. Which may be a bug or a feature  ...:-)
>
> I think the subtle difference here lies between the definition of
> inheritance and duck-typing, and how both are typically used in
> practice ...

Right, here you're wanting to implement your Node as a specialization  
of str in order to get only some of it's behaviour.  But the  
abstractions of a graph node and string don't mix with some of str's  
other behaviour.  To me this is obtuse:

 >>> class Node(str): pass
...
 >>> n = Node('funky %s')
 >>> n.children = [Node('child1'), Node('child2')]
 >>> print n % 'yes
funky yes

Duck typing is what you're looking for and is specifically not  
inheritance.

I'd like to note that my entire argument has been based on the  
semantics of inheritance v. duck typing.  I'm not saying what you're  
doing won't work, just that when I see someone inherit from str I  
expect a str-like object in intention, it being hashable should be  
incidental to that.  This example is sufficiently small and simple  
that if you do choose inheritance you're not likely to get bitten.   
But, once you get in the habit of breaking abstractions like this you  
open yourself up for a world of hurt in other scenarios.

Another thing to think about is that the only thing you have to gain  
with piggy backing str like this is saving three lines of typing, 4  
lines to implement __hash__ and __eq__ v. 1 line to inherit from str.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: storing meta data on dictionary keys

2007-10-11 Thread Erik Jones

On Oct 11, 2007, at 1:36 AM, Andreas Kraemer wrote:

> On Oct 10, 9:00 pm, Erik Jones <[EMAIL PROTECTED]> wrote:
>> If you're sure that 1.  this use case won't grow and 2. that you'll
>> only be the only person ever using code, then it's your choice of
>> preference.  Both of those points are equally important.  1 is a
>> manageability issue in that you are complicating what could be an
>> easy solution for a small gain in convenience -- one that could just
>> as easily be had by encapsulating the management of both dicts in an
>> explicit class or function/method.  Let me quote lines 2 through 5 of
>> The Zen of Python (PEP20):
>>
>> Explicit is better than implicit.
>> Simple is better than complex.
>> Complex is better than complicated.
>> Flat is better than nested.
>>
>> Using two dicts would be explicit, simple and flat.  Your's is none
>> of those.
>>
>> For point 2, if anyone else needs to work with your code they're most
>> likely going to be confused as the implementation is overly complex.
>> I know when I initially read through it I had to read it a couple
>> times to assure myself of what it was doing because I was thinking
>> that it seems that there's more going on than there is, so there must
>> be.  Remember, the best solutions are those that not only you can
>> understand, but the next guy/girl/?.
>
> You are right, and these are all very valid points. One of the reasons
> I like Python so much is the philosophy behind it. Nevertheless,
> everything is relative, so a pattern that seems complex (or unusual)
> at first may become simple when people get used to it ... I hope, I
> may be excused. In the organization where I work I am mainly
> prototyping (more on the "scientific/algorithm" side) and not required
> to write maintainable code. The engineers will do that (in Java
> anyway, sadly ..).

Excellent.  I'm actually love "hackish" approaches to things when you  
can afford to use them and it seems like you're in that situation.
>
>>> The trivial "class Str(str): pass" in the OP (that already inherits
>>> the correct __hash__ and
>>> __eq__) serves the same purpose as your Node(object) below, ...
>>
>> True, but you're breaking the intended abstraction of inheriting from
>> str, i.e. the reason you can inherit from str is so that you can
>> specialize strings, not get __has__ and __eq__ for free.  You can
>> define those on your own for a reason.
>
> Who says that that's the intended abstraction of inheriting from str?
> I thought that one purpose of inheritance was the reuse of behavior
> (i.e. methods) (and maybe state) of the superclass. So, my class
> Str(str): pass behaves like a string in all aspects, except that you
> can attach some meta data that won't affect that behavior. In fact, I
> use that pattern quite frequently also for other built-in immutable
> types, e.g. floats or ints, which allows me to very easily "tag"
> numerical data (e.g. as an outlier in a stats application or whatever)
> without affecting any subsequent code operating on it as long as it
> only uses its built-in "flavor". This is the power of Python's duck
> typing!

No, duck typing and inheritance are two different things.  Duck  
typing is when you implement the same operations as another object or  
class, whereas with inheritance you get the same implementation as  
that of the parent class.  With duck typing, an object "acts like"  
another object in specific contexts.  With inheritance, an object is  
the same general type as another object.  You don't want string  
objects, you want objects that, in a specific case, act like strings  
but in others, I'm sure, not.  How does a concept like string  
substitution fit with your inherited objects?

>> So, (explicitly:) you want the built-in dict to be a dictionary that
>> also maintains a dictionary of its own key
>
> Without knowing the internals, so I may be wrong. But dict must
> somehow maintain a dictionary of the key objects already. How else
> could it detect collisions of hash values, when it must use __eq__ to
> determine equality of the "query" key object to one of the inserted
> keys having the same hash value? It also needs to do the same check
> when overwriting an existing key.

If you can, get a copy of Beautiful Code (easily had from Amazon or  
the likes).  It contains an chapter on Python's dictionary  
implementation.

>> Not likely to happen.
>> Again, you're asking to have one dictionary act as two and that's
>> just confusing.  I'm not saying th

Re: Keeping track of subclasses and instances?

2007-10-11 Thread Erik Jones

On Oct 11, 2007, at 12:49 AM, Andreas Kraemer wrote:

> On Oct 10, 6:19 pm, Karlo Lozovina <[EMAIL PROTECTED]> wrote:
>> Larry Bates wrote:
>>> I'm not completely sure I understand the question but here goes.
>>> Instances of
>>> classes are classes can be stored in lists or dictionaries.  In  
>>> lists you
>>> reference them via their index (or iterate over them) and in  
>>> dictionaries
>>> you can give them a name that is used as a key.
>>
>> I wish if it were that simple :).
>>
>> Here is a longer description - I have a function that given input  
>> creates a
>> custom class and returns it back. The user is free to subclass  
>> that (even
>> more, he should do that), and of course he will make instances of  
>> those
>> subclasses. Now, my question is how to keep track of subclasses  
>> and their
>> instances, without the need for user interaction (appending them  
>> to a list,
>> or adding to dictionary)?
>>
>> Thanks,
>>
>> --
>> Karlo Lozovina - Mosorclass Meta(type):
>
> What about the following solution?
>
> class Meta(type):
>   def __new__(mcl,*args,**kw):
> class_ = super(Meta,mcl).__new__(mcl,*args,**kw)
> mcl._classes.append(class_)
> class_._instances = []
> return class_
>   _classes = []
>
> def factory():
>   class Class(object):
> __metaclass__ = Meta
> def __new__(cls,*args,**kw):
>   instance = super(Class,cls).__new__(cls,*args,**kw)
>   cls._instances.append(instance)
>   return instance
>   return Class
>
>>>> A = factory()
>>>> class B(A): pass
> ...
>>>> a = A()
>>>> b = B()
>>>> Meta._classes
> [, ]
>>>> A._instances
> []
>>>> B._instances
> [<__main__.B object at 0xb7dbb0ec>]
>
> So, you see that you can access all classes, their subclasses, and
> instances from Meta.
>
> Of course in a more elaborate version _classes and _instances should
> store weak references, so that classes and instances can actually be
> deleted. I also haven't explored under which circumstances this can
> break ...
>
> I can imagine different use cases for this, (though they certainly are
> non-standard :-)). I once contemplated the (toy-) implementation of a
> frame-based knowledge representation system using Python's object
> model, where one definitely needs classes to keep track of their
> instances ...

Another use case that I've run across is when defining model classes  
using an ORM against a database that uses table inheritance  
extensively with the result that the database has way more tables  
than you'd actually want to manually maintain model classes/files for.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: storing meta data on dictionary keys

2007-10-10 Thread Erik Jones

On Oct 10, 2007, at 6:40 PM, Andreas Kraemer wrote:

> On Oct 9, 9:18 pm, Erik Jones <[EMAIL PROTECTED]> wrote:
>> So, do you not keep references to your nodes anywhere but the actual
>> graph dict?  I kind of agree with Chris here in that two dicts will
>> work. One for the nodes, indexed by their strings.
>
> Yes, I guess that's exactly what I want. To keep things as simple as
> possible and not having to keep track of another dictionary. If you
> look at class Dict(dict) in the OP, simulating the behavior I'd liked
> to have seen for built-in dict itself, the second dictionary is
> actually hidden so I don't have to bother with it any more ...

If you're sure that 1.  this use case won't grow and 2. that you'll  
only be the only person ever using code, then it's your choice of  
preference.  Both of those points are equally important.  1 is a  
manageability issue in that you are complicating what could be an  
easy solution for a small gain in convenience -- one that could just  
as easily be had by encapsulating the management of both dicts in an  
explicit class or function/method.  Let me quote lines 2 through 5 of  
The Zen of Python (PEP20):

Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.

Using two dicts would be explicit, simple and flat.  Your's is none  
of those.

For point 2, if anyone else needs to work with your code they're most  
likely going to be confused as the implementation is overly complex.   
I know when I initially read through it I had to read it a couple  
times to assure myself of what it was doing because I was thinking  
that it seems that there's more going on than there is, so there must  
be.  Remember, the best solutions are those that not only you can  
understand, but the next guy/girl/?.

>
>> And, to use the
>> actual nodes as keys simply override __hash__ in your Node object
>> classes.
>
> The trivial "class Str(str): pass" in the OP (that already inherits
> the correct __hash__ and
> __eq__) serves the same purpose as your Node(object) below, except
> that self.name is stored
> in the str built-in, and there is no flashy initializer.

True, but you're breaking the intended abstraction of inheriting from  
str, i.e. the reason you can inherit from str is so that you can  
specialize strings, not get __has__ and __eq__ for free.  You can  
define those on your own for a reason.

>
>>
>>>>> class Node(object):
>> ... def __init__(self, name, **kwargs):
>> ... self.name = name
>> ... for k, v in kwargs.items():
>> ... self.__dict__[k] = v
>> ...
>> ... def __hash__(self):
>> ... return hash(self.name)
>> ...
>>>>> nodes = {}
>>>>> graph = {}
>>>>>
>>>>> n = Node('foo')
>>>>> m = Node('blah', baz=5)
>>>>>
>>>>> nodes[n.name] = n
>>>>> nodes[m.name] = m
>>>>>
>>>>> for name, node in nodes.items():
>> ... graph[node] = "whatever for node %s" % name
>> ...
>>>>> nodes{'blah': <__main__.Node object at 0x76c50>, 'foo':
>> <__main__.Node object at 0x76d30>}
>>>>> graph{<__main__.Node object at 0x76c50>: 'whatever for node
>> blah', <__main__.Node object at 0x76d30>: 'whatever for node foo'}
>>>>> graph[nodes['foo']]'whatever for node foo'
>
> I still believe that it would be a nice-to-have, and probably only a
> small effort to equip
> the built-in dict with a get_key() method. The whole mechanism of
> dictionary look-up in Python,
> based on "duck typing", and only caring about the __hash__ and __eq__
> methods supplied by
> the key object, is constructed in a way that allows objects inserted
> into the dictionary as keys
> and objects used for look-up to be (non-trivially) distinct.
> Therefore, it would actually be
> nice to be able to retrieve keys from the dict in a direct way (other
> than via .keys()).
> The same may apply to sets and frozensets as well ...;-)

So, (explicitly:) you want the built-in dict to be a dictionary that  
also maintains a dictionary of its own keys?  Not likely to happen.   
Again, you're asking to have one dictionary act as two and that's  
just confusing.  I'm not saying that get_key is a bad idea for  
(possibly) some use cases, just that what you're actually doing is  
creating a dictionary wherein the same key can return two different  
object depending on the retrieval method used and that is NOT what  
the built-in dict is for.  In fact, now that I think of it, get_key  
is probably a bad name for it, get_other_object_with_this_same_key is  
probably more apt :)

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: I'm starting to think like a Pythonista

2007-10-10 Thread Erik Jones

On Oct 10, 2007, at 4:16 PM, Marc 'BlackJack' Rintsch wrote:

> On Wed, 10 Oct 2007 17:03:41 -0400, brad wrote:
>
>> Bjoern Schliessmann wrote:
>>> brad wrote:
>>>> low_odds = [1,3,5,7,9]
>>>> # make a list containing 10 - 98 evens only
>>>> big_evens =  big_evens = [x for x in list(xrange(99)) if x % 2 ==
>>>> 0 and x >8]
>>>
>>> Why use xrange if you convert it to a full list in place? No
>>> advantage there.
>>
>> What is the dis-advantage of using xrange over range in this  
>> circumstance?
>
> It's an unnecessary intermediate step.

Exactly, there's no disadvantage, but the use case for xrange is for  
when there is an advantage.  xrange is a lazy generator, which means  
that the values in the range are generated one by one, i.e. the  
entire range is never present in memory all at once.  If all your  
doing is building that list then there's no advantage.  In fact, [x  
for x in xrange(99)] can be though of as longhand for range(99).  Put  
another way, xrange returns a generator of successive, individual  
numbers over a range (optionally with a step) while range returns a  
list of numbers over a range (optionally with a step).

 >>> l = xrange(99)
 >>> m = range(99)
 >>> type(l)

 >>> type(m)



Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: I'm starting to think like a Pythonista

2007-10-10 Thread Erik Jones

On Oct 10, 2007, at 2:51 PM, brad wrote:

> I was looking at a way to implement Ruby's upto method in python. I  
> came
> up with the code below... three years ago, I would never have  
> thought of
> list comprehension, today it seems second nature. This may be totally
> un-Pythonic, but I thought it was kind of clever. Man, for some  
> reason,
> I feel like being rude and lofty acting :)
>
> low_odds = [1,3,5,7,9]
> # make a list containing 10 - 98 evens only
> big_evens =  big_evens = [x for x in list(xrange(99)) if x % 2 == 0  
> and
> x >8]

big_evens = range(10, 100, 2)

>
> low_evens = [2,4,6,8]
> # make a list containing 11 - 99 odds only
> big_odds = [x for x in list(xrange(100)) if x % 2 != 0 and x >9]

big_odds = range(11, 100, 2)


Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: mutable objects as dictionary keys

2007-10-09 Thread Erik Jones
On Oct 9, 2007, at 12:44 PM, Andreas Kraemer wrote:

> Hi everyone,
>
> I know that the subject of mutable objects as dictionary keys has  
> been discussed a number of times in this forum (see for instance  
> "freezing" of classes), but I would love to hear the thoughts of  
> the experts on the approach below.
>
> The use case that I encounter frequently is the classification of  
> objects according to certain rules or properties: Say, I have  
> objects A, B, C, ... (e.g. instances of a class, dict, etc), I can  
> write
>
> d = {}
> d.setdefault(A,[]).append(A)
> d.setdefault(B,[]).append(B)
> ...
>
> so objects that map to the same hash key will end up in the same  
> bucket. (A "real world" example I had to deal with recently was for  
> instance the construction of a directed acyclic graph from many  
> directed trees where identical subtrees needed to be identified.)
>
> The easiest way is of course to define custom __hash__() and __eq__ 
> () methods, but this breaks if objects are mutated (accidentally)  
> after having been inserted into the dictionary. The "best" working  
> approach I came up with so far is to generate an "immutable view" V  
> of a mutable object O according to my classification rule, delegate  
> O.__hash__ and O.__eq__ to V, and make sure that the V is memoized  
> and cannot (easily) be altered later, even when O is mutated:
>
> def hashable_mutable_factory(mutable,rule):
>   class _mutable(mutable):
> def __init__(self,*args,**kw):
>   self._view_cache = {}
>   super(_mutable,self).__init__(*args,**kw)
> def _view(self):
>   id_ = id(self)
>   if not self._view_cache.has_key(id_):
> self._view_cache[id_] = rule(self)
>   return self._view_cache[id_]
> def __hash__(self):
>   return hash(self._view())
> def __eq__(self,other):
>   return self._view() == other._view()
>   return _mutable
>
> E.g.:
>
> >>> hashable_dict = hashable_mutable_factory(dict,lambda obj:  
> frozenset(obj.iteritems()))
> >>> h = hashable_dict(a=1,b=2)
> >>> d = {}
> >>> d[h] = 'foo'
> >>> d
> {{'a': 1, 'b': 2}: 'foo'}
> >>> h['c'] = 'bar'
> >>> d
> {{'a': 1, 'c': 'bar', 'b': 2}: 'foo'}
> >>> g = hashable_dict(a=1,b=2)
> >>> h
> {'a': 1, 'c': 'bar', 'b': 2}
> >>> g
> {'a': 1, 'b': 2}
> >>> id(g) == id(h)
> False
> >>> g == h
> True
>
> I slightly favor the factory function idiom above over defining the  
> rule in a super class (this would have to be done for each mutable  
> type and rule function separately), especially since I read that  
> future versions of python (2.6 ?, 3.0 ?) will contain class  
> decorators and allow syntax like class A(*bases): pass
>
> Is there a better approach? Any comments are appreciated.
>
> I have been seriously using Python for one year know, mostly in the  
> context of graph algorithms etc., and it has always been a  
> delightful coding experience!

I can definitely see how this would be useful for the real world  
example you mentioned for pruning trees and what-not.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: storing meta data on dictionary keys

2007-10-09 Thread Erik Jones
On Oct 9, 2007, at 7:37 PM, Andreas Kraemer wrote:

> From: Chris Mellon <[EMAIL PROTECTED]>
> Sent: Tuesday, October 9, 2007 1:51:04 PM
>
> > Because, by definition, if you have the key then you don't need  
> to get
> > it from the dict. What you're doing here is conflating 2 mappings  
> into
> > one: string value->person and person->values.  Use 2 explicit  
> dicts to
> > make it clear what you're going, or use one dict and store a  
> tuple of
> > values: person, friends = d["John"].
>
> No, that was not the point.
>
> I don't want the meta data to be used in the dictionary look-up,  
> and in fact I want to be able to modify or delete it later w/o  
> affecting dictionary look-up. In the toy example "John" and Str 
> ("John") are two different objects that map to the same value in  
> the dictionary, and "John" == Str("John") is True, since class Str 
> (str)  inherits __hash__() and __eq__() from str. IOW, if John dyes  
> his hair, his friends shouldn't change :-)
>
> Apart from this silly example I really encountered this issue when  
> using the networkx library. As I mentioned, graph nodes are stored  
> as dictionary keys there, i.e. nodes can be arbitrary objects with  
> the only requirement to be hashable, and I am stuck with this data  
> structure when using the library. In my example nodes are uniquely  
> identified by their name (a string) but may carry other attributes,  
> like their display color and shape, that are not used to identify a  
> node. Therefore, I thought, subclassing str would be the simplest,  
> most straightforward structure for a node object.
>
> Of course there are workarounds (e.g. get all keys with keys()),  
> but I thought something similar to a get_key() dictionary method  
> would be the easiest way to retrieve the actually stored key  
> object, and I was just surprised to discover that no such method  
> does exist 

So, do you not keep references to your nodes anywhere but the actual  
graph dict?  I kind of agree with Chris here in that two dicts will  
work.  One for the nodes, indexed by their strings.  And, to use the  
actual nodes as keys simply override __hash__ in your Node object  
classes.

 >>> class Node(object):
... def __init__(self, name, **kwargs):
... self.name = name
... for k, v in kwargs.items():
... self.__dict__[k] = v
...
... def __hash__(self):
... return hash(self.name)
...
 >>> nodes = {}
 >>> graph = {}
 >>>
 >>> n = Node('foo')
 >>> m = Node('blah', baz=5)
 >>>
 >>> nodes[n.name] = n
 >>> nodes[m.name] = m
 >>>
 >>> for name, node in nodes.items():
...     graph[node] = "whatever for node %s" % name
...
 >>> nodes{'blah': <__main__.Node object at 0x76c50>, 'foo':  
<__main__.Node object at 0x76d30>}
 >>> graph{<__main__.Node object at 0x76c50>: 'whatever for node  
blah', <__main__.Node object at 0x76d30>: 'whatever for node foo'}
 >>> graph[nodes['foo']]'whatever for node foo'

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: The fundamental concept of continuations

2007-10-09 Thread Erik Jones
On Oct 9, 2007, at 3:32 PM, . wrote:

> On Tue, 09 Oct 2007 19:20:06 +, gnuist006 wrote:
>
>> On Oct 8, 11:09 pm, "." <[EMAIL PROTECTED]> wrote:
>>> On Tue, 09 Oct 2007 05:15:49 +, gnuist006 wrote:
>>
>>>
>>>> Can anyone explain:
>>>
>>>> (1) its origin
>>>
>>> One of the lambda papers, I think.  I don't remember which.
>>
>> Hey no-name "dot" you are the only one who says its origin is in
>> one of the old lambda papers. Give me a reference or someone
>> give me a reference. I dont have access to any ACM journals or
>> other conferences. So
>
> I said "I think."  Matthias corrected me.  They're all on  
> readscheme.org
> ( http://library.readscheme.org/page1.html ) though, and well worth
> reading.
>
> I note that I'm being mocked for not using my real name by someone not
> using his/her real name.  Thank you, no-name gnuist006, you make me  
> laugh.

Relax, ., I hardly think he was mocking you.  He probably assumed  
that using a . in a sentence as a form of address would be as  
unintelligible as it has been in these two sentences.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: migrating to packages

2007-10-03 Thread Erik Jones

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

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: global variables

2007-10-02 Thread Erik Jones

On Oct 2, 2007, at 5:20 PM, TheFlyingDutchman wrote:

> Does anyone know how the variables label and scale are recognized
> without a global statement or parameter, in the function resize() in
> this code:
>
>
>
> #!/usr/bin/env python
>
> from Tkinter import *
>
> def resize(ev=None):
>   label.config(font='Helvetica -%d bold' % \
>   scale.get())
>
>
> top = Tk()
> top.geometry('250x150')
>
> label = Label(top, text='Hello World!',
> font='Helvetica -12 bold')
> label.pack(fill=Y, expand=1)
>
> scale = Scale(top, from_=10, to=40,
>  orient=HORIZONTAL, command=resize)
> scale.set(12)
> scale.pack(fill=X, expand=1)
>
> quit = Button(top, text='QUIT',
> command=top.quit, activeforeground='white',
> activebackground='red')
> quit.pack()
>
> mainloop()

It's tricky.  Basically, you only need to use the global statement if  
you intend binding operations (assignments) on the variable name and  
want those to affect the global variable.  If you perform binding  
operations without the global statement it is assumed that you are  
defining a local variable.

class foo(object):
def foofoo(self):
print 7

def showfoo():
f.foofoo()

f = foo()
showfoo()
print f

outputs:

7
<__main__.foo object at ... >

with the same class:

def showfoo():
global f
f.foofoo()
f = 6

f = foo()
showfoo()
f
  outputs:

7
6

with the same class again:

deff showfoo():
f.foofoo()
f = 6

f = foo()
showfoo()

outputs:

Traceback (most recent call last):
   File "", line 1, in 
   File "", line 2, in showfoo
UnboundLocalError: local variable 'f' referenced before assignment

The difference in the last one is that when showfoo() is compiled the  
assignment to f without any global statement makes f a local variable  
and a method is called on it before it is bound which results in the  
exception.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Python 3.0 migration plans?

2007-09-28 Thread Erik Jones

On Sep 28, 2007, at 3:00 PM, TheFlyingDutchman wrote:

> On Sep 28, 12:45 pm, George Sakkis <[EMAIL PROTECTED]> wrote:
>> On Sep 28, 3:29 pm, TheFlyingDutchman <[EMAIL PROTECTED]> wrote:
>>
>>> One issue I have with this group and that I encountered many  
>>> years ago
>>> in the Perl group is that there is no separate group
>>> comp.lang.python.beginner where you can ask questions without  
>>> getting
>>> hit with RTFM! and the like.
>>
>> Which shows once again that you're trying to break the world  
>> record of
>> being wrong in as many sentences as possible:
>>
>>http://mail.python.org/mailman/listinfo/tutor
>>
>> You would do yourself (and others) a favor by migrating there for a
>> few weeks or months.
>>
>> George
>
> Being in a land where every nit can be picked, I am surprised that you
> offered up a mailing list when I was asking for a newsgroup.

I'm usually pretty quiet on this list.  That's what I find is the  
best way to participate.  However, I'm going to have to speak up and  
point out that that response is exactly the type of comment and/or  
reasoning that everyone here is trying to explain to you.  The  
resources are available for you to educate yourself.  Semantic  
arguments such as this are, at best, at a junior high level.  You are  
completely free to ask any question (about Python) you want here --  
just don't argue with the people giving you the answers.  And, to  
address the actual content of that response:  nobody, online or off,  
in newsgroups or on mailing lists, likes a nitpick.  So, please,  
quit.  I'd prefer you didn't leave and, instead, decided to actually,  
actively get along with the others here.  I think some of the  
question you've begun threads with have been both good and valid.   
It's just that you need some work on your e-people skills, man.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Python 3.0 migration plans?

2007-09-28 Thread Erik Jones

On Sep 27, 2007, at 8:17 PM, Steve Holden wrote:

> James Stroud wrote:
>> Steve Holden wrote:
>>> I wondered if a straw poll could get some idea of readers' thoughts
>>> about when they will be migrating to 3.0 on, so I used the new  
>>> widget on
>>> Blogger to add a poll for that.
>>>
>>> I'd appreciate if if you would go to
>>>
>>>   http://holdenweb.blogspot.com/
>>>
>>> and register your vote on your intended migration timescale.
>>>
>>> Thanks!
>>
>> I'm going to abstain voting until 'public beta + about 1 year' is  
>> a choice.
>>
> Richard Jones wrote:
>> I'll use the "no plans" response for my actual "no simple answer"  
>> real
>> response.
>>
>>
> So what we need is a poll on what the questions should be. I *love*  
> c.l.py.

Does professional vs. personal use matter here?  What if I plan to  
switch in the morning or at midnight on the first solstice after the  
second alpha release?  Is Mercury or Venus in retrograde?  These  
things matter... :)

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: True of False

2007-09-27 Thread Erik Jones

On Sep 27, 2007, at 12:29 PM, Erik Jones wrote:

>
> On Sep 27, 2007, at 11:47 AM, Marc 'BlackJack' Rintsch wrote:
>
>> On Thu, 27 Sep 2007 09:33:34 -0700, koutoo wrote:
>>
>>> I tried writing a true and false If statement and didn't get
>>> anything?  I read some previous posts, but I must be missing
>>> something.  I just tried something easy:
>>>
>>> a = ["a", "b", "c", "d", "e", "f"]
>>>
>>> if "c" in a == True:
>>>  Print "Yes"
>>>
>>> When I run this, it runs, but nothing prints.  What am I doing  
>>> wrong?
>>
>> Wow that's odd:
>>
>> In [265]: a = list('abcdef')
>>
>> In [266]: a
>> Out[266]: ['a', 'b', 'c', 'd', 'e', 'f']
>>
>> In [267]: 'c' in a
>> Out[267]: True
>>
>> In [268]: 'c' in a == True
>> Out[268]: False
>>
>> In [269]: ('c' in a) == True
>> Out[269]: True
>>
>> In [270]: 'c' in (a == True)
>> - 
>> -
>> -
>>  Traceback (most recent
>> call last)
>>
>> /home/bj/ in ()
>>
>> : argument of type 'bool' is not  
>> iterable
>>
>>
>> What's going on there?
>
> That is weird.  Given 270, what's happening in 268.
>
> Erik Jones

Cool, Richard Thomas answered this one for me.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: True of False

2007-09-27 Thread Erik Jones

On Sep 27, 2007, at 11:47 AM, Marc 'BlackJack' Rintsch wrote:

> On Thu, 27 Sep 2007 09:33:34 -0700, koutoo wrote:
>
>> I tried writing a true and false If statement and didn't get
>> anything?  I read some previous posts, but I must be missing
>> something.  I just tried something easy:
>>
>> a = ["a", "b", "c", "d", "e", "f"]
>>
>> if "c" in a == True:
>>  Print "Yes"
>>
>> When I run this, it runs, but nothing prints.  What am I doing wrong?
>
> Wow that's odd:
>
> In [265]: a = list('abcdef')
>
> In [266]: a
> Out[266]: ['a', 'b', 'c', 'd', 'e', 'f']
>
> In [267]: 'c' in a
> Out[267]: True
>
> In [268]: 'c' in a == True
> Out[268]: False
>
> In [269]: ('c' in a) == True
> Out[269]: True
>
> In [270]: 'c' in (a == True)
> -- 
> -
>  Traceback (most recent  
> call last)
>
> /home/bj/ in ()
>
> : argument of type 'bool' is not iterable
>
>
> What's going on there?

That is weird.  Given 270, what's happening in 268.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Delete values from a string using the index

2007-09-26 Thread Erik Jones
On Sep 26, 2007, at 3:25 PM, [EMAIL PROTECTED] wrote:

> How do I delete or remove values from a list or string using the
> index.
>
> If a = [1,2,3,4,5,6,7,8] and I want to get rid of 1 -5, how would I do
> that?
>
> Thanks.
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list

 >>> l = [1, 2, 3]
 >>> del l[1]
 >>> l
[1, 3]

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: A question on python performance.

2007-09-26 Thread Erik Jones
On Sep 26, 2007, at 1:26 PM, Joe Goldthwaite wrote:

> Hi everyone,
>
> I'm a developer who's been using python for a couple of years.  I  
> wrote a
> fairly large application using it but I was learning the language  
> at the
> same time so it most of the code kind of sucks.
>
> I've learned a lot since then and I've been going through my code  
> trying to
> organize it better and make better use of Python's features.  I'm  
> still  not
> an expert by any definition but I'm slowly getting better.
>
> I've been working on a trend class that takes twelve monthly  
> numbers and
> returns a period to date, quarter to date, year to date and  
> quarterly year
> to date numbers for a specific period. This worked but I ended up  
> with a lot
> of code like this;
>
> def getValue(trend, param, per):
>if param == 'Ptd':
>  return trend.Ptd(per)
>elif param == 'Qtd':
>   return trend.Qtd(per)
>elif param == 'Ytd':
>   return trend.Ytd(per)
>elif param == 'YtdQ':
>   return trend.YtdQ(per)
>
> The code gets kind of wordy so I started trying to figure out how  
> to call
> them dynamically since the param type is the same as the method the
> retrieves it.  I came up with this;
>
> def getValue(trend, param, per):
>return trend.__class__.__dict__[param](trend, per)
>
> That worked but it seems like the above line would have to do lots  
> more
> object look ups at runtime so I didn't think it would be very  
> efficient.  I
> thought maybe I could add a caller method to the trend class and I  
> came up
> with this;
>
> class trend:
>...
>...
>...
>def caller(self, param, *args):
>   return self.__class__.__dict__[param](self, *args)
>
> This simplified the getValue function to this;
>
> def getValue(trend, param, per):
>   return trend.caller(param, per)


What you're describing is a case of mulitple dispatch.  See http:// 
www.artima.com/weblogs/viewpost.jsp?thread=101605 for a short  
description and (as short) example by Guido.  You can probably fill  
that out and adapt it to your needs.  Alternatively, you could look  
into the multimethods module in the Gnosis Utilities package: http:// 
pypi.python.org/pypi/Gnosis_Utils/1.2.1-a


Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Confused about 'positive lookbehind assertion'

2007-09-25 Thread Erik Jones

On Sep 24, 2007, at 9:38 PM, Robert Dailey wrote:

> Hi,
>
> I've been reading the python documentation on 'positive lookbehind  
> assertion' and I don't understand at all how it works. The python  
> docs give the following example:
>
> " (?<=abc)def will find a match in "abcdef", since the lookbehind  
> will back up 3 characters and check if the contained pattern matches."
>
> Can anyone emphasize more on what this RE operation does? Thanks.


Have you actually tried it out?

 >>> import re
 >>> r = re.compile(r'(?<=abc)def')
 >>> m1 = r.search('bcde')
 >>> m1.group()'def'
'def'
 >>> m2 = r.search('bcdefff')
 >>> m2 == None
True

So, it matches 'def' but only if it is immediately preceded by 'abc'.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: newb: Simple regex problem headache

2007-09-21 Thread Erik Jones

On Sep 21, 2007, at 4:04 PM, crybaby wrote:

> import re
>
> s1 =' 25000 '
> s2 = ' 5.5910 '
>
> mypat = re.compile('[0-9]*(\.[0-9]*|$)')
> rate= mypat.search(s1)
> print rate.group()
>
> rate=mypat.search(s2)
> print rate.group()
> rate = mypat.search(s1)
> price = float(rate.group())
> print price
>
> I get an error when it hits the whole number, that is in this format:
> s1 =' 25000 '
> For whole number s2, mypat catching empty string.  I want it to give
> me 25000.
> I am getting this error:
>
> price = float(rate.group())
> ValueError: empty string for float()
>
> Anyone knows, how I can get 25000 out of s2 = ' 5.5910 '
> using regex pattern, mypat = re.compile('[0-9]*(\.[0-9]*|$)').  mypat
> works fine for real numbers, but doesn't work for whole numbers.

I'm not sure what you just said makes a lot of sense, but if all your  
looking for is a regex that will match number strings with or without  
a decimal point, try '\d*\.?\d*'

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Google and Python

2007-09-19 Thread Erik Jones
On Sep 19, 2007, at 4:01 PM, TheFlyingDutchman wrote:

> On Sep 19, 1:02 pm, Erik Jones <[EMAIL PROTECTED]> wrote:
>> is usually Apache at most sites?
>>
>> No an http server and application server are two different things.
>> An http server services requests of a web server those requests can
>> be for static files or for services of a local application in which
>> case the request if forwarded on to the application.  An application
>> services requests of an application.  They are separate concepts,
>> often chained, although they are sometimes implemented together.
>> What they are saying here is that they have built a highly optimizing
>> custom web server in C++ that services web requests for services of
>> applications written in any of the three listed languages.  So, yes,
>> in this case it is what is often Apache in other installations.
>>
> OK, thanks. Would you know what technique the custom web server uses
> to invoke a C++ app (ditto for Java and Python) CGI is supposed to be
> too slow for large sites.

That's what SWIG is for:  interfacing C++ with other languages.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Google and Python

2007-09-19 Thread Erik Jones
On Sep 19, 2007, at 2:44 PM, TheFlyingDutchman wrote:

>
>> Have you tried Google "google python".  Turns up a lot of links  
>> for me.
>>
> I had done it on this newsgroup, but not google. I did find a pretty
> good link:
>
> http://panela.blog-city.com/python_at_google_greg_stein__sdforum.htm
>
> Which says:
> "A few services including code.google.com and google groups.  Most
> other front ends are in C++ (google.com) and Java (gmail).  All web
> services are built on top of a highly optimizing http server wrapped
> with SWIG."
>
> I am not clear on how you would use a language - whether C++, Java or
> Python to write the web app with this custom http server. Is this http
> server what is referred to as an "application server" or is it the
> main web server which is usually Apache at most sites?

No an http server and application server are two different things.   
An http server services requests of a web server those requests can  
be for static files or for services of a local application in which  
case the request if forwarded on to the application.  An application  
services requests of an application.  They are separate concepts,  
often chained, although they are sometimes implemented together.   
What they are saying here is that they have built a highly optimizing  
custom web server in C++ that services web requests for services of  
applications written in any of the three listed languages.  So, yes,  
in this case it is what is often Apache in other installations.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: how to join array of integers?

2007-09-15 Thread Erik Jones
On Sep 15, 2007, at 11:07 AM, Grant Edwards wrote:

> On 2007-09-15, Arnau Sanchez <[EMAIL PROTECTED]> wrote:
>
>>>> in Python... is the method to use ",".join() ?  but then it
>>>> must take a list of strings... not integers...
>>>>
>>>> any fast method?
>>
>>> print ''.join([str(i) for i in [1,2,3]])
>>
>> It's better to use generator comprehension instead of LC:
>>
>> ",".join(str(i) for i in [1, 2, 3])
>>
>> Or, if you happen to like the itertools modules:
>>
>> from itertools import imap
>> ",".join(imap(str, [1, 2, 3]))
>
> It's nice people have invented so many ways to spell the
> builting "map" ;)
>
>>>> ",".join(map(str,[1,2,3]))
> '1,2,3'

IIRC, map's status as a builtin is going away.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: how to join array of integers?

2007-09-15 Thread Erik Jones

On Sep 15, 2007, at 8:56 AM, Arnau Sanchez wrote:

> js escribió:
>
>>> On 9/15/07, Summercool <[EMAIL PROTECTED]> wrote:
>
>>> in Python... is the method to use  ",".join() ?  but then it must  
>>> take
>>> a list of strings... not integers...
>>>
>>> any fast method?
>
>> print ''.join([str(i) for i in [1,2,3]])
>
> It's better to use generator comprehension instead of LC:
>
> ",".join(str(i) for i in [1, 2, 3])

Why is that?  That entire expression must be evaluated to obtain the  
result, so what is the advantage of using a generator comprehension  
v. a list comprehension?


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


Re: Python 3K or Python 2.9?

2007-09-15 Thread Erik Jones
On Sep 14, 2007, at 11:54 PM, David Trudgett wrote:

> TheFlyingDutchman <[EMAIL PROTECTED]> writes:
>
>> The confusing way about the current Python method when you first
>> encounter it is
>>  why is "self" being passed in when you write the function but not
>> when you call it. If the compiler is smart enough to know that
>>
>> a = MyClass()
>> a.SomeFunction(12)
>>
>> SomeFunction() has a "self" implicitly added to the parameter  
>> list, it
>> seems that it should be smart enough to know that a function defined
>> in a class has a "self" implicitly added to the parameter list.
>
> Several languages use the "object.method(args)" form, which is  
> syntactic
> sugar for "method(object, other_args)" which Ada, for instance, uses.
> Knowing this clears up half the confusion (the object /is/ passed as a
> parameter whichever syntax is used).
>
> The other half of the confusion is cleared up by considering that
> Python methods are ordinary functions that don't magically "know" in
> which "class" context they are executing: they must be told via the
> first parameter.

Yes, that is really the crux of the issue, though.  While the former  
may be syntactic sugar for the latter, once any syntactic sugar has  
become prevalent enough, it becomes the expected norm and the latter  
becomes clutter or, at best, awkward.  It's something that really  
just takes a little use until you get to the point where you don't  
usually think of it but, every so often, the thought creeps in.  I'm  
not complaining, just pointing out if you step out of this particular  
box, you'll realize that it really is a pointless one.  Saying,  
"because that's how Python does it" may be the only valid reason, but  
that argument is about on par with a six year old's "just because...".



Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Haskell like (c:cs) syntax

2007-08-28 Thread Erik Jones

On Aug 28, 2007, at 5:12 PM, Chris Mellon wrote:

> On 8/28/07, Stefan Niemann <[EMAIL PROTECTED]> wrote:
>> Hi,
>>
>> sorry that I'm relatively new to Python. But the syntax and  
>> semantics of
>> Python already fascinate me, because I'm familiar with functional  
>> languages
>> like Haskell.
>>
>> Is there a pattern matching construct in Python like (head :  
>> tail), meaning
>> 'head' matches the first element of a list and 'tail' matches the  
>> rest? I
>> could not find this in the Python documentation.
>>
>> Regards,
>> Stefan
>>
>>
>
> Python does not have haskell like pattern matching. Things are written
> and done in a different way.
>
> When working with lists, Python has a slice syntax (which is rather
> more powerful than Haskells limited head->tail linked list syntax)
> that you can use to chop a sequence up into various parts.

That is extremely arguable (in fact, Haskell's list semantics are  
extremely powerful as they are not limited to just head/tail).  But,  
rather than debate the merits of one language over the other, to the  
OP:  no, Python doesn't have any pattern matching facilities.   
Binding statements must be explicit, so you could do something along  
the lines of (using parallel assignment):

head, tail = l[0], l[1:]

or,

front, last = l[:len(l) - 1], l[len(l) - 1]

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Python Classes

2007-08-28 Thread Erik Jones
On Aug 28, 2007, at 12:04 AM, Lamonte Harris wrote:

> How come you have to set the initialized created variables to equal  
> the parameters, shouldn't that be default?
>
> class testing:
>  def __init__(self,testing):
>self.testing = testing
> x = testing("testing")
> print x.testing
>
>
> How come self.testing = testing
>
> Can someone explain that in more detail, just confused on why you  
> have to set it up like that.
> -- 

Simple Answer:

Because language should never set variable values "by default".   
That's what programmers are for.  There may be application domains  
where *some* defaulting behavior makes sense, but that's what  
frameworks and DSLs are for.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: comparing two lists

2007-08-23 Thread Erik Jones
On Aug 23, 2007, at 11:27 AM, Ladislav Andel wrote:

> Hi,
> what would be the most efficient way to do following?
>
> I have a list of dictionaries  taken from DB e.g.
> dblist = [{'id:1, 'host':'google.com','ip_address':'1.2.3.4'},
> {'id:3, 'host':'yahoo.com','ip_address':'5.6.7.8'},
> {'id:9, 'host':'msn.com','ip_address':'11.3.2.3'}]
>
> and list of object instances in memory(it's just for example)
> which are looping within itself and testing particular hosts
>
> memlist = [,]
> memlist[0].id  is 1 and  memlist[0].host is google.com etc.
> memlist[1].id  is 9 and  memlist[1].host is msn.com etc.
>
> Now I want to add a new instance to memlist since id=3(in dblist)  
> is not
> in memlist.
> How would you iterate through it and insert a new instance?
>
> The result should be:
> memlist = [,, ]
> memlist[0].id  is 1 and  memlist[0].host is google.com etc.
> memlist[1].id  is 3 and  memlist[1].host is yahoo.com etc.
> memlist[2].id  is 9 and  memlist[2].host is msn.com etc.

Well, if you add a constructor for object instances that can take  
those dictionaries you could do something like:

for row in dblist.iteritems():
obj = Obj(row)   # where Obj is your classname
if obj is not in memlist:
memlist.append(obj)
>
> Furthermore, I can have the opposite situation.
> This time I need to remove from memlist a host which is not in dblist.
> How would you do this?

Similarly, if you add an toHash() method to you object class you  
could do something like (using the reverse iteration solution  
mentioned in another reply):

for i in range(len(memlist) -1, -1, -1):
if memlist[[i].toHash() not in dblist:
del memlist[i]

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Database intensive application

2007-08-13 Thread Erik Jones

On Aug 12, 2007, at 7:44 AM, Viewer T. wrote:

>  and Yes, Python
> has awesome database support and can satisfy almost all database  
> needs.

Wow.  Nobody ever told me Python was *that* kind of language :)

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: yield keyword usage

2007-07-30 Thread Erik Jones
On Jul 30, 2007, at 4:13 PM, Ehsan wrote:

> hi
> coulde any one show me the usage of  "yield" keyword specially in this
> example:
>
>
> """Fibonacci sequences using generators
>
> This program is part of "Dive Into Python", a free Python book for
> experienced programmers.  Visit http://diveintopython.org/ for the
> latest version.
> """
>
> __author__ = "Mark Pilgrim ([EMAIL PROTECTED])"
> __version__ = "$Revision: 1.2 $"
> __date__ = "$Date: 2004/05/05 21:57:19 $"
> __copyright__ = "Copyright (c) 2004 Mark Pilgrim"
> __license__ = "Python"
>
> def fibonacci(max):
> a, b = 0, 1
> while a < max:
> yield a
> a, b = b, a+b
>
> for n in fibonacci(1000):
> print n,

As in how it works?  Sure, when you use the yield statement in a  
function or method you turn it into a generator method that can then  
be used for iteration.  What happens is that when fibonacci(1000) is  
called in the for loop statement it executes up to the yield  
statement where it "yields" the then current value of a to the  
calling context at which point n in the for loop is bound to that  
value and the for loop executes one iteration.  Upon the beginning of  
the for loop's next iteration the fibonacci function continues  
execution from the yield statment until it either reaches another  
yield statement or ends.  In this case, since the yield occured in a  
loop, it will be the same yield statement at which point it will  
"yield" the new value of a.  It should be obvious now that this whole  
process will repeat until the condition a < max is not longer true in  
the fibonacci function at which point the function will return  
without yielding a value and the main loop (for n in ...) will  
terminate.



Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: Python in Nashville

2007-07-21 Thread Erik Jones
Me!  Did you just move here?

On Jul 20, 2007, at 10:22 PM, Patrick Altman wrote:

> Anyone on this group writing Python code in Nashville, TN?
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: How to organize control access to source code ?

2007-07-16 Thread Erik Jones
On Jul 16, 2007, at 3:46 PM, [EMAIL PROTECTED] wrote:

> Hello,
> How can we organize development team with code source control policy,
> that limit access to some portion of code ?

The specifics largely depend on the system(s) you're working with  
but, basically (sic), you need to read up on filesystem level  
permissions.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: How to determine which method was used in an inheritance heirarchy?

2007-07-16 Thread Erik Jones
On Jul 16, 2007, at 10:35 AM, Chris Mellon wrote:

> On 7/16/07, Erik Jones <[EMAIL PROTECTED]> wrote:
>> On Jul 16, 2007, at 3:37 AM, Gabriel Genellina wrote:
>>
>>> En Mon, 16 Jul 2007 03:56:18 -0300, Erik Jones <[EMAIL PROTECTED]>
>>> escribió:
>>>
>>>> Perhaps an even better example of what I'm trying to do would be in
>>>> order (this is minus any exception handling):
>>>>
>>>> import sys
>>>>
>>>> def mytrace(frame, event, arg):
>>>>  if event == 'call':
>>>>  func_name = frame.f_code.co_name
>>>>
>>>>  if func_name in frame.f_locals['self'].__class__.__dict__:
>>>>  print frame.f_locals['self'].__class__.__name__
>>>>  else:
>>>>  for base in frame.f_locals 
>>>> ['self'].__class__.__bases__:
>>>>  if func_name in base.__dict__:
>>>>  print base.__name__
>>>>  break
>>>>
>>>>
>>>> class A(object):
>>>>  def __init__(self):
>>>>  pass
>>>>
>>>> class B(A):
>>>>  def __init__(self):
>>>>  A.__init__(self)
>>>>
>>>> sys.settrace(mytrace)
>>>> B()
>>>>
>>>> This will output:
>>>>
>>>> B
>>>> B
>>>
>>> If you don't mind post-processing the results, you can log the
>>> function
>>> name and source module (from frame.f_code.co_name and  
>>> co_filename) and
>>> current line number (frame.f_lineno). Later, obtaining the class
>>> name from
>>> those is a bit tricky (and not even the inspect module does it
>>> right), but
>>> perhaps using the tokenizer module, watching for lines that contain
>>> "class"  is enough.
>>
>>
>> I was afraid of that.  I used pretty much that tokenizer trick for a
>> unit test generator I wrote in php a while back and felt like that
>> was pretty clunky then.
>>
>
>
> Hacky, but maybe this will work:
>
> import sys
> import inspect
>
> def mytrace(frame, event, arg):
> if event == 'call':
> func_name = frame.f_code.co_name
> klassOb = frame.f_locals['self'].__class__
> for klass in inspect.getmro(klassOb):
> cf = klass.__dict__.get(func_name)
> if hasattr(cf, "func_code") and cf.func_code ==  
> frame.f_code:
> print klass.__name__
>
>
> class A(object):
> def __init__(self):
> pass
>
> class B(A):
> def __init__(self):
> A.__init__(self)
>
> sys.settrace(mytrace)
> B()

Chris, that is absolutely perfect.  Also, I don't think there is such  
a thing as profiling code that isn't hacky ;)

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: How to determine which method was used in an inheritance heirarchy?

2007-07-16 Thread Erik Jones
On Jul 16, 2007, at 3:37 AM, Gabriel Genellina wrote:

> En Mon, 16 Jul 2007 03:56:18 -0300, Erik Jones <[EMAIL PROTECTED]>  
> escribió:
>
>> Perhaps an even better example of what I'm trying to do would be in
>> order (this is minus any exception handling):
>>
>> import sys
>>
>> def mytrace(frame, event, arg):
>>  if event == 'call':
>>  func_name = frame.f_code.co_name
>>
>>  if func_name in frame.f_locals['self'].__class__.__dict__:
>>  print frame.f_locals['self'].__class__.__name__
>>  else:
>>  for base in frame.f_locals['self'].__class__.__bases__:
>>  if func_name in base.__dict__:
>>  print base.__name__
>>  break
>>
>>
>> class A(object):
>>  def __init__(self):
>>  pass
>>
>> class B(A):
>>  def __init__(self):
>>  A.__init__(self)
>>
>> sys.settrace(mytrace)
>> B()
>>
>> This will output:
>>
>> B
>> B
>
> If you don't mind post-processing the results, you can log the  
> function
> name and source module (from frame.f_code.co_name and co_filename) and
> current line number (frame.f_lineno). Later, obtaining the class  
> name from
> those is a bit tricky (and not even the inspect module does it  
> right), but
> perhaps using the tokenizer module, watching for lines that contain
> "class"  is enough.


I was afraid of that.  I used pretty much that tokenizer trick for a  
unit test generator I wrote in php a while back and felt like that  
was pretty clunky then.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: How to determine which method was used in an inheritance heirarchy?

2007-07-16 Thread Erik Jones
On Jul 16, 2007, at 12:53 AM, Michele Simionato wrote:

> On Jul 16, 7:18 am, Erik Jones <[EMAIL PROTECTED]> wrote:
>> On Jul 15, 2007, at 11:23 PM, Michele Simionato wrote:
>>
>>> On Jul 16, 5:51 am, Erik Jones <[EMAIL PROTECTED]> wrote:
>>>> Say you're given a call event frame for a method call.  How can you
>>>> tell if the code being executed came from a super class of the  
>>>> object
>>>> or class the method was called on?
>>
>>>> Erik Jones
>>
>>> You look if the method was defined in self.__class__.__dict__.
>>
>>>Michele Simionato
>>
>> That doesn't seem to cover calling super class __init__ methods.
>>
>
> I am probably missing something. In the following code the
> method check_init checks if the current instance
> possess an __init__ or if it just inherits one
> from the ancestors. Is this what you want?
>
> class B(object):
> def __init__(self):
>   'something'
> def check_init(self):
> if '__init__' in self.__class__.__dict__:
> print 'possesses __init__'
> else:
> print 'inherits __init__'
>
> class C(B):
> 'something else'
> def __init__(self):
> print 'calling C.__init__'
>
> class D(B):
> pass
>
> c = C()
> d = D()
>
> c.check_init() #possesses __init__
> d.check_init() #inherits __init__

Ok, I see how I was pretty vague with my original questions.   Given  
the pattern where you need to call a base class's constructor (or,  
other overriden method of the same name as that being called on the  
child class object):

class A(object):
def __init__(self):
print self.__class__.__name__

class B(A):
def __init__(self):
A.__init__(self)
print self.__class__.__name__

B()

This will output:

B
B

How can I get

A
B

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: How to determine which method was used in an inheritance heirarchy?

2007-07-15 Thread Erik Jones

On Jul 16, 2007, at 1:31 AM, Erik Jones wrote:

> On Jul 16, 2007, at 12:53 AM, Michele Simionato wrote:
>
>> On Jul 16, 7:18 am, Erik Jones <[EMAIL PROTECTED]> wrote:
>>> On Jul 15, 2007, at 11:23 PM, Michele Simionato wrote:
>>>
>>>> On Jul 16, 5:51 am, Erik Jones <[EMAIL PROTECTED]> wrote:
>>>>> Say you're given a call event frame for a method call.  How can  
>>>>> you
>>>>> tell if the code being executed came from a super class of the  
>>>>> object
>>>>> or class the method was called on?
>>>
>>>>> Erik Jones
>>>
>>>> You look if the method was defined in self.__class__.__dict__.
>>>
>>>>Michele Simionato
>>>
>>> That doesn't seem to cover calling super class __init__ methods.
>>>
>>
>> I am probably missing something. In the following code the
>> method check_init checks if the current instance
>> possess an __init__ or if it just inherits one
>> from the ancestors. Is this what you want?
>>
>> class B(object):
>> def __init__(self):
>>  'something'
>> def check_init(self):
>> if '__init__' in self.__class__.__dict__:
>> print 'possesses __init__'
>> else:
>> print 'inherits __init__'
>>
>> class C(B):
>> 'something else'
>> def __init__(self):
>> print 'calling C.__init__'
>>
>> class D(B):
>> pass
>>
>> c = C()
>> d = D()
>>
>> c.check_init() #possesses __init__
>> d.check_init() #inherits __init__
>
> Ok, I see how I was pretty vague with my original questions.
> Given the pattern where you need to call a base class's constructor  
> (or, other overriden method of the same name as that being called  
> on the child class object):
>
> class A(object):
>   def __init__(self):
>   print self.__class__.__name__
>
> class B(A):
>   def __init__(self):
>   A.__init__(self)
>   print self.__class__.__name__
>
> B()
>
> This will output:
>
> B
> B
>
> How can I get
>
> A
> B

Perhaps an even better example of what I'm trying to do would be in  
order (this is minus any exception handling):

import sys

def mytrace(frame, event, arg):
 if event == 'call':
 func_name = frame.f_code.co_name

 if func_name in frame.f_locals['self'].__class__.__dict__:
 print frame.f_locals['self'].__class__.__name__
 else:
 for base in frame.f_locals['self'].__class__.__bases__:
 if func_name in base.__dict__:
 print base.__name__
 break


class A(object):
 def __init__(self):
 pass

class B(A):
 def __init__(self):
 A.__init__(self)

sys.settrace(mytrace)
B()

This will output:

B
B

whereas I'm shooting for:

B
A



Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


Re: How to determine which method was used in an inheritance heirarchy?

2007-07-15 Thread Erik Jones

On Jul 15, 2007, at 11:23 PM, Michele Simionato wrote:

> On Jul 16, 5:51 am, Erik Jones <[EMAIL PROTECTED]> wrote:
>> Say you're given a call event frame for a method call.  How can you
>> tell if the code being executed came from a super class of the object
>> or class the method was called on?
>>
>> Erik Jones
>
> You look if the method was defined in self.__class__.__dict__.
>
>Michele Simionato

That doesn't seem to cover calling super class __init__ methods.

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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


How to determine which method was used in an inheritance heirarchy?

2007-07-15 Thread Erik Jones
Say you're given a call event frame for a method call.  How can you  
tell if the code being executed came from a super class of the object  
or class the method was called on?

Erik Jones

Software Developer | Emma®
[EMAIL PROTECTED]
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com


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