Re: SPAM

2007-11-16 Thread Byung-Hee HWANG
dear "just bob",

On Wed, 2007-11-14 at 11:43 -0800, just bob wrote:
> "John Bean" <[EMAIL PROTECTED]> wrote in message 
> news:[EMAIL PROTECTED]
> > On Wed, 14 Nov 2007 11:04:35 -0800, "just bob"
> > <[EMAIL PROTECTED]> wrote:
> >
> >>
> >
> > Your SPAM appears to be non-existent. Vapourware. Not real.
> >
> > Shame, I fancied a Spam fritter.
> >
> 
> The guy gets Google dollars when people view the site or click on links, me 
> thinks. It's spam. 

please read the rfc4871 if you want to attack izb.knu.ac.kr ;;

respect,
bh

-- 
"I'll give you some more straight talk. After the Don died, Mike was set up
to be killed. Do you know who set him up? Tessio. So Tessio had to be killed.
Carlo had to be killed. Because treachery can't be forgiven. Michael could
have forgiven it, but people never forgive themselves and so they would always
be dangerous. Michael really liked Tessio. He loves his sister. But he would
be shrinking his duty to you and his children, to his whole family, to me and
my family, if he let Tessio and Carlo go free. They would have been a danger
to us all, all our lives."
-- Tom Hagen, "Chapter 32", page 442

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


Re: implement random selection in Python

2007-11-16 Thread Boris Borcic
Bruza wrote:
> No. That does not solve the problem. What I want is a function
> 
>   def randomPick(n, the_items):
> 
> which will return n DISTINCT items from "the_items" such that
> the n items returned are according to their probabilities specified
> in the (item, pro) elements inside "the_items".
> 
> If I understand correctly, both of the previous replies will output
> one item at a time independently, instead of returning n DISTINCT
> items at a time.
> 

from random import sample
randomPick = lambda n,its : sample(eval('+'.join('[%r]*%r'%p for p in its)),n)

hth :)

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


Re: Populating a dictionary, fast [SOLVED SOLVED]

2007-11-16 Thread Hrvoje Niksic
Steven D'Aprano <[EMAIL PROTECTED]> writes:

>> Can you post minimal code that exhibits this behavior on Python 2.5.1?
>> The OP posted a lot of different versions, most of which worked just
>> fine for most people.
>
> Who were testing it on single-CPU, 32 bit systems.

Still, I'd like to see a test case that fails (works slowly) for you,
so that I (and others) can try it on different machines.  As I said,
the OP posted several versions of his code, and tended to depend on a
specific dataset.  A minimal test case would help more people debug
it.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: implement random selection in Python

2007-11-16 Thread bearophileHUGS
This recipe of mine may help:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/498229

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Python server j2me client ssl socket handshake error

2007-11-16 Thread eliseo
Hello,

I want to make the same application : python Serveur and J2me Client whit ssl 
socket.
I would like to know if you have solved the problem?
Can you show me the final version of python server code and the  J2ME client 
code, so that I can make my application with your help.

Thanks

Best regards, MELE Eliseo ([EMAIL PROTECTED])



_
Emporter. Brancher. Surfer. [EMAIL PROTECTED] AWAY est l'Internet mobile de 
Sunrise. Avec lui, surfer sur Internet n'a jamais été aussi simple et aussi 
rapide. Plus d'informations sous www.sunrise.ch/takeaway
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: implement random selection in Python

2007-11-16 Thread Boris Borcic
Bruza wrote:
> No. That does not solve the problem. What I want is a function
> 
>   def randomPick(n, the_items):
> 
> which will return n DISTINCT items from "the_items" such that
> the n items returned are according to their probabilities specified
> in the (item, pro) elements inside "the_items".

and in the initial post you wrote :

 > But how about the general case, for N > 1 and N < len(items)?

The problem is you need to make "the n items returned are according
to their probabilities" more precise. "According to their probabilities" 
implies 
n INDEPENDENT picks, but this is contradictory with the n picks having to 
provide DISTINCT results (what clearly constrains picks relative to each other).

Of course there are obvious ways to combine the results of random choices of 
single items to obtain a set like you want, but it is not obvious that they are 
equivalent.

This is the most simple-minded :

def randomPick(n, the_items) :
 assert nhttp://mail.python.org/mailman/listinfo/python-list


Re: implement random selection in Python

2007-11-16 Thread Paul Rubin
Bruza <[EMAIL PROTECTED]> writes:
> But how about the general case, for N > 1 and N < len(items)? Is there
> some clever algorithm using Python standard "random" package

Yeah, I'm not sure what the name for it is, but there'ss a well known
algorithm that's sort of an online verison of random.choice.  The
famous N=1 example where all probabilities are equal goes:

# choose a random element of seq
for k,s in enumerate(seq):
   if random() < 1.0/(k+1):
  choice = s

you should be able to generalize this to N items with different
probabilities.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: cx_Oracle: Non-ASCII characters handling with different versions

2007-11-16 Thread dwahli
On Nov 14, 2:03 pm, Benjamin Hell <[EMAIL PROTECTED]> wrote:
> It's solved: Because of a local full Oracle DB installation the
> "working" box had the registry key
> HKEY_LOCAL_SYSTEM\SOFTWARE\ORACLE\KEY_OraBD10g_home1\NLS_LANG set to
> "AMERICAN_AMERICA.WE8MSWIN1252". A similar key was missing on the
> other box. I added HKEY_LOCAL_SYSTEM\SOFTWARE\ORACLE\NLS_LANG with
> the same value and now it works.

You could use environment variable NLS_LANG to set it at run time
with:

os.environ["NLS_LANG"] = "AMERICAN_AMERICA.WE8MSWIN1252"
import cx_Oracle
...

This way you don't have to deal with registry on each box.
Note that os.environ["NLS_LANG"] must be BEFORE import cx_Oracle.

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


Re: SPAM

2007-11-16 Thread ChrisM
In message [EMAIL PROTECTED],
[EMAIL PROTECTED] <[EMAIL PROTECTED]> Proclaimed from the tallest tower:

> On Wed, 14 Nov 2007 15:12:23 -0500, Lew <[EMAIL PROTECTED]> wrote:
>
>> just bob wrote:
>>> "John Bean" <[EMAIL PROTECTED]> wrote in message
>>> news:[EMAIL PROTECTED]
 On Wed, 14 Nov 2007 11:04:35 -0800, "just bob"
 <[EMAIL PROTECTED]> wrote:

 Your SPAM appears to be non-existent. Vapourware. Not real.

 Shame, I fancied a Spam fritter.

>>>
>>> The guy gets Google dollars when people view the site or click on
>>> links, me thinks. It's spam.
>>
>> Yes, but it's not SPAM.
>>
>> SPAM is a registered trademark of Hormel Foods Corporation for a
>> canned pork product.
>>
>> Spam is unwanted messages or email.
>
> No, that is usually called U.C.E.

Unsolicited Commercial Email yea, but WHO(and I'm talking about the general 
public) actually uses(or has even heard of) that acronym?
-- 
Regards,
Chris.
(Remove Elvis's shoes to email me) 


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


Re: gc penalty of 30-40% when manipulating large data structures?

2007-11-16 Thread Chris Mellon
On Nov 16, 2007 8:34 AM, Aaron Watters <[EMAIL PROTECTED]> wrote:
> Poking around I discovered somewhere someone saying that
> Python gc adds a 4-7% speed penalty.
>
> So since I was pretty sure I was not creating
> reference cycles in nucular I tried running the tests with garbage
> collection disabled.
>
> To my delight I found that index builds run 30-40% faster without
> gc.  This is really nice because testing gc.collect() afterward
> shows that gc was not actually doing anything.
>
> I haven't analyzed memory consumption but I suspect that should
> be significantly improved also, since the index builds construct
> some fairly large data structures with lots of references for a
> garbage collector to keep track of.
>
> Somewhere someone should mention the possibility that disabling
> gc can greatly improve performance with no down side if you
> don't create reference cycles.  I couldn't find anything like this
> on the Python site or elsewhere.  As Paul (I think) said, this should
> be a FAQ.
>
> Further, maybe Python should include some sort of "backoff"
> heuristic which might go like this: If gc didn't find anything and
> memory size is stable, wait longer for the next gc cycle.  It's
> silly to have gc kicking in thousands of times in a multi-hour
> run, finding nothing every time.
>

The GC has a heuristic where it kicks in when (allocations -
deallocations) exceeds a certain threshold, which has (sometimes quite
severe) implications for building large indexes. This doesn't seem to
be very well known (it's come up at least 3-4 times on this list in
the last 6 months) and the heuristic is probably not a very good one.
If you have some ideas for improvements, you can read about the
current GC in the gc module docs (as well as in the source) and can
post them on python-ideas.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: SPAM

2007-11-16 Thread John Bean
On Fri, 16 Nov 2007 15:29:09 GMT, [EMAIL PROTECTED] wrote:

>On Fri, 16 Nov 2007 15:08:56 -, "ChrisM"
><[EMAIL PROTECTED]> wrote:
>
>>In message [EMAIL PROTECTED],
>>[EMAIL PROTECTED] <[EMAIL PROTECTED]> Proclaimed from the tallest tower:
>>
>>> On Wed, 14 Nov 2007 15:12:23 -0500, Lew <[EMAIL PROTECTED]> wrote:
>>>
 just bob wrote:
> "John Bean" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
>> On Wed, 14 Nov 2007 11:04:35 -0800, "just bob"
>> <[EMAIL PROTECTED]> wrote:
>>
>> Your SPAM appears to be non-existent. Vapourware. Not real.
>>
>> Shame, I fancied a Spam fritter.
>>
>
> The guy gets Google dollars when people view the site or click on
> links, me thinks. It's spam.

 Yes, but it's not SPAM.

 SPAM is a registered trademark of Hormel Foods Corporation for a
 canned pork product.

 Spam is unwanted messages or email.
>>>
>>> No, that is usually called U.C.E.
>>
>>Unsolicited Commercial Email yea, but WHO(and I'm talking about the general 
>>public) actually uses(or has even heard of) that acronym?
>
>I have, apparently!
>

Nah, you're using a proper newsreader to access Usenet so by
definition you're not "general public" who only understand
pointy-clicky-webby things.


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


Re: list of class initiations ?

2007-11-16 Thread Marc 'BlackJack' Rintsch
On Fri, 16 Nov 2007 13:20:59 +0100, stef mientki wrote:

> Is there a way to create a hierarchical list of class headers, including 
> the parameters.
> What I mean (I'm not a programmer) is something like this:
> 
>   
> class Block ( t_BaseShape ):   (self, Pos = [10,10] ):
> class t_BaseShape(Shape):  (self, x=20, y=20, x2=90, y2=90, 
> type='rect' ):
> class Shape(ShapeEvtHandler):  (self, x=[], y=[]):
> class ShapeEvtHandler: ()

Take a look at the `inspect` module.  Should be possible to build
something with those functions.

Ciao,
Marc 'BlackJack' Rintsch
-- 
http://mail.python.org/mailman/listinfo/python-list


gc penalty of 30-40% when manipulating large data structures?

2007-11-16 Thread Aaron Watters
Poking around I discovered somewhere someone saying that
Python gc adds a 4-7% speed penalty.

So since I was pretty sure I was not creating
reference cycles in nucular I tried running the tests with garbage
collection disabled.

To my delight I found that index builds run 30-40% faster without
gc.  This is really nice because testing gc.collect() afterward
shows that gc was not actually doing anything.

I haven't analyzed memory consumption but I suspect that should
be significantly improved also, since the index builds construct
some fairly large data structures with lots of references for a
garbage collector to keep track of.

Somewhere someone should mention the possibility that disabling
gc can greatly improve performance with no down side if you
don't create reference cycles.  I couldn't find anything like this
on the Python site or elsewhere.  As Paul (I think) said, this should
be a FAQ.

Further, maybe Python should include some sort of "backoff"
heuristic which might go like this: If gc didn't find anything and
memory size is stable, wait longer for the next gc cycle.  It's
silly to have gc kicking in thousands of times in a multi-hour
run, finding nothing every time.

Just my 2c.
   -- Aaron Watters

nucular full text fielded indexing: http://nucular.sourceforge.net
===
http://www.xfeedme.com/nucular/pydistro.py/go?FREETEXT=dingus%20fish
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sorting contacts alphabetically, sorted by surname

2007-11-16 Thread J. Clifford Dyer
On Fri, Nov 16, 2007 at 12:31:19PM +, Marie Hughes wrote regarding sorting 
contacts alphabetically, sorted by surname:
> 
>HI
> 
>I have to write a program that contains a text file in the following
>format:
> 
>AcademicPositionRoom Ext.   Email
>Prof Marie Maguire  Head of
>School  M9002    [EMAIL PROTECTED]
> 
>And produce a output file where conatcts are sorted alphabetically-
>sorted by surname.
> 
>Has anyone any ideas.

Well, the first thing you need to do is develop a workable understanding of how 
your data file is formatted.  What separates one field from another?  Is it 
tab-delimited?  Does the field always start at a given column number?  Then you 
need to read each line of the file, and split the lines up according to the 
format.  Save to a data structure, sort by last name (you'll have to further 
parse the name to isolate this information), and reprint the results.

Depending on the details you uncover about the precise format of your data 
file, you will find it helpful to read the documentation on string methods, 
particularly s.split(), and sequence slicing: s[4:14].

Best of luck with it.

Cheers,
Cliff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python beginner!

2007-11-16 Thread Wildemar Wildenburger
Tony wrote:
> On Nov 15, 8:57 pm, Wildemar Wildenburger
> Give me back the old comp.lang.python, where anyone could ask anything
> and be sure of a range of replies, instead of this sort of
> pedanticism. Sorry, nothing personal, maybe Python users have become
> too professional and geeky to remember that Python's main objective is
> fun, that is why it is called Python.
> 
Looking at the two other replies so far (by you and kyosohma), I don't 
see much that a regular google search and some personal investigation 
wouldn't have brought up. I'm a big fan of people showing problem 
solving skills, because they make for much more interesting discussions.

It is true that I could have been way more polite. And I should have 
been. I'm sorry. But I still maintain that this question was not very 
polite either. It was one of these "give me a solution"-type questions 
that I find rather annoying. And maybe hanging out at 
c.l.java.programmer hardened me a bit.

Well anyway, I'll try to be at least a little wordier in the future. :)

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


Re: formated local time

2007-11-16 Thread Nikola Skoric
Dana Thu, 15 Nov 2007 10:12:11 -0600, 
Adam Pletcher <[EMAIL PROTECTED]> kaze:
>> -Original Message-
>> From: [EMAIL PROTECTED]
>> [mailto:[EMAIL PROTECTED] On
> Behalf
>> Of [EMAIL PROTECTED]
>> Sent: Thursday, November 15, 2007 9:56 AM
>> To: python-list@python.org
>> Subject: Re: formated local time
>> If you want the formatted string, you can use strftime:
>> 
>> >>> time.strftime("%Y-%m-%d %H:%M:%S")
>> '2007-11-15 07:51:12'
>
> datetime also has the strftime method:
>
> import datetime
> datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')

Thanks everybody!

-- 
"Now the storm has passed over me
I'm left to drift on a dead calm sea
And watch her forever through the cracks in the beams
Nailed across the doorways of the bedrooms of my dreams"
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sorting contacts alphabetically, sorted by surname

2007-11-16 Thread Boris Borcic
Marie Hughes wrote:
> HI
>  
> I have to write a program that contains a text file in the following format:
>  
> AcademicPositionRoom Ext.   Email
> Prof Marie Maguire  Head of 
> School  M9002    [EMAIL PROTECTED] 
> 
>  
> And produce a output file where conatcts are sorted alphabetically- 
> sorted by surname.
>  
> Has anyone any ideas.

You mean "write a program" or "copy a program" ?




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


Leo 4.4.5 beta 1 released

2007-11-16 Thread Edward K Ream
Leo 4.4.5 beta 1 is available at:
http://sourceforge.net/project/showfiles.php?group_id=3458&package_id=29106

Leo is a text editor, data organizer, project manager and much more. See:
http://webpages.charter.net/edreamleo/intro.html

Leo 4.4.5 fixes several long-delayed bug fixes and adds several new
features.

The highlights of Leo 4.4.5:


- Fixes all known bugs.
- Adds 3 new sort-lines commands.
- Adds commands to insert and delete icons from headlines.
- Adds all the Tango 16x16 icons to Leo's icon library.
- Adds support for @rst-preformat nodes to the rst3 plugin.

Links:
--
Leo:  http://webpages.charter.net/edreamleo/front.html
Home: http://sourceforge.net/projects/leo/
Download: http://sourceforge.net/project/showfiles.php?group_id=3458
CVS:  http://leo.tigris.org/source/browse/leo/
Quotes:   http://webpages.charter.net/edreamleo/testimonials.html


Edward K. Ream   email:  [EMAIL PROTECTED]
Leo: http://webpages.charter.net/edreamleo/front.html



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


sorting contacts alphabetically, sorted by surname

2007-11-16 Thread Marie Hughes

HI
 
I have to write a program that contains a text file in the following format:
 
AcademicPositionRoom Ext.   EmailProf Marie 
Maguire  Head of School  M9002    [EMAIL PROTECTED]
 
And produce a output file where conatcts are sorted alphabetically- sorted by 
surname.
 
Has anyone any ideas.
 
_
Get free emoticon packs and customisation from Windows Live. 
http://www.pimpmylive.co.uk-- 
http://mail.python.org/mailman/listinfo/python-list

list of class initiations ?

2007-11-16 Thread stef mientki
hello,

Is there a way to create a hierarchical list of class headers, including 
the parameters.
What I mean (I'm not a programmer) is something like this:

  
class Block ( t_BaseShape ):   (self, Pos = [10,10] ):
class t_BaseShape(Shape):  (self, x=20, y=20, x2=90, y2=90, 
type='rect' ):
class Shape(ShapeEvtHandler):  (self, x=[], y=[]):
class ShapeEvtHandler: ()

within 1 file would be enough.

thanks,
Stef Mientki
-- 
http://mail.python.org/mailman/listinfo/python-list


Looking for docs

2007-11-16 Thread Donn Ingle
Hi,
I have seen strange looking things in various Python code like:
staticmethod and also lines starting with an @ sign, just before method
defs - I can't find an example right now.
 I have Python 2.5 installed with it's docs, but I can't find any discussion
of 'new style' classes and other info.

I'm not even sure what subject that @ sign stuff falls under.

Anyone have a link?

/d

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


Re: Looking for docs

2007-11-16 Thread Tim Chase
> I'm not even sure what subject that @ sign stuff falls under.

The magic keyword you're looking for is "decorator" which will
help you find the docs (a quick google for "python decorator"
returns scads of helpful results)

-tkc



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


Python Extension in C#?

2007-11-16 Thread jane janet

Hello all,
Please tell me how can I create .dll for Python in C#?
Every advice would be great.
 
Thank you.
 
All the best,
 
_
Windows Live Hotmail and Microsoft Office Outlook – together at last.  Get it 
now.
http://office.microsoft.com/en-us/outlook/HA102225181033.aspx?pid=CL100626971033-- 
http://mail.python.org/mailman/listinfo/python-list

Re: overriding methods - two questions

2007-11-16 Thread Bruno Desthuilliers
Donn Ingle a écrit :
> Hi,
> Here's a framework for the questions:
> 
> --- In a module, part of an API ---
> class Basis ( object ):
>  def foo ( self, arg ):
>   pass
> 
> --- In user's own code ---
> class Child ( Basis ):
>  def foo ( self, not, sure ):
>   ...
> 
> 
> Question 1:
> 
> Given that the user of the API can choose to override foo() or not, how can
> I control the signature that they use?

While technically possible (using inspect.getargspec), trying to make 
your code idiot-proof is a lost fight and a pure waste of time.

> Question 2:
> 
> Say I am in class Basis, doing a loop and I have a list of Child objects. I
> want to run the foo() method for each one that *has* a foo() method. i.e.
> user has done this:
> 
> class Sam ( Child ):
>  ...
> *Sam does not define foo()
> 
> class Judy ( Child ):
>  def foo ( self, arg ):
>   ...
> * Judy does define foo()
> 
> Instances of Sam and Judy have been put into the list (within the instance)
> of Basis. I want Basis to detect that Judy has foo() and run it.
> 
> I can handle question 2 by using a flag that must be set by the user.
> Something like:
> class Judy ( child ):
>  def __init__( self ):
>   self.pleaseCallFoo = true
> 
> And now, Basis can check for that var and only then call foo(), but this is
> ugly and means more for the user to learn API-wise.

Indeed.

> Any ideas?

Quite a few, but I don't have enough context to tell which one would be 
the best - nor why you want to do such a thing. Anyway, the simplest is 
to just check :

for child in self.childrens:
   if 'foo' in child.__class__.__dict__:
 child.foo()

but this won't call foo for :

class Dude(Judy):
   pass

Don't know if that's what you want. If not (ie, you want to call 
child.foo if foo is not Basis.foo), then:

for child in self.childrens:
   if child.foo.im_func is not self.foo.im_func:
 child.foo()

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


Any pythonists in guangzhou china!!!

2007-11-16 Thread ce
Hi, wondering if there is any python community in guangzhou/china

I need ppl to talk to! I am dieing here
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: implement random selection in Python

2007-11-16 Thread duncan smith
Bruza wrote:
> I need to implement a "random selection" algorithm which takes a list
> of [(obj, prob),...] as input. Each of the (obj, prob) represents how
> likely an object, "obj", should be selected based on its probability
> of
> "prob".To simplify the problem, assuming "prob" are integers, and the
> sum of all "prob" equals 100. For example,
> 
>items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)]
> 
> The algorithm will take a number "N", and a [(obj, prob),...] list as
> inputs, and randomly pick "N" objects based on the probabilities of
> the
> objects in the list.
> 
> 
> For N=1 this is pretty simply; the following code is sufficient to do
> the job.
> 
> def foo(items):
> index = random.randint(0, 99)
> currentP = 0
> for (obj, p) in items:
>currentP += w
>if currentP > index:
>   return obj
> 
> But how about the general case, for N > 1 and N < len(items)? Is there
> some clever algorithm using Python standard "random" package to do
> the trick?
> 

I think you need to clarify what you want to do.  The "probs" are
clearly not probabilities.  Are they counts of items?  Are you then
sampling without replacement?  When you say N < len(items) do you mean N
<= sum of the "probs"?

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


Re: sorting contacts alphabetically, sorted by surname

2007-11-16 Thread Boris Borcic
Wow

J. Clifford Dyer wrote:
> On Fri, Nov 16, 2007 at 12:31:19PM +, Marie Hughes wrote regarding 
> sorting contacts alphabetically, sorted by surname:
>>HI
>>
>>I have to write a program that contains a text file in the following
>>format:
>>
>>AcademicPositionRoom Ext.   Email
>>Prof Marie Maguire  Head of
>>School  M9002    [EMAIL PROTECTED]
>>
>>And produce a output file where conatcts are sorted alphabetically-
>>sorted by surname.
>>
>>Has anyone any ideas.
> 
> Well, the first thing you need to do is develop a workable understanding of 
> how your data file is formatted.  What separates one field from another?  Is 
> it tab-delimited?  Does the field always start at a given column number?  
> Then you need to read each line of the file, and split the lines up according 
> to the format.  Save to a data structure, sort by last name (you'll have to 
> further parse the name to isolate this information), and reprint the results.
> 
> Depending on the details you uncover about the precise format of your data 
> file, you will find it helpful to read the documentation on string methods, 
> particularly s.split(), and sequence slicing: s[4:14].
> 
> Best of luck with it.
> 
> Cheers,
> Cliff

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


Re: Resolving declaring class of a method at runtime

2007-11-16 Thread Janne Härkönen
On Nov 15, 2007 11:07 PM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Thu, 15 Nov 2007 13:01:27 +0200, Janne Härkönen
> wrote:
>
> Have you tried looking at dir(TheClass) to see what it lists?

This is the first thing I did, but it shows both the inherited and own
methods.

> Also helpful is TheClass.__dict__.keys().

This actually does the trick, thanks for the tip!

> Python has powerful introspection abilities. Learn to use them, and you
> too will be able to impress your friends with your Python knowledge.

I actually use introspection very frequently, but almost always through dir().


> X is an "old style" class. Most people probably shouldn't use old style
> classes, for various reasons. To use new style classes, you inherit from
> object:

I am also aware of old and new style classes, this was the fastest way to
type it in console :)

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


Re: What is python?????

2007-11-16 Thread Thorsten Kampe
* Cope (Fri, 16 Nov 2007 06:09:31 -0800 (PST))
> please tell me what is python.This group is so crowded.

A Python is dangerous snake[1]. This group here mainly consists of 
misguided snake worshippers. You'd better run before they come to your 
place...

Thorsten
[1] http://en.wikipedia.org/wiki/Pythonidae
-- 
http://mail.python.org/mailman/listinfo/python-list


What is python?????

2007-11-16 Thread Cope
please tell me what is python.This group is so crowded.

Khup
http://groups.google.com/group/download-centre
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Insert image to a List box

2007-11-16 Thread Matt McCredie
On Nov 15, 2007 2:15 PM, linda. s <[EMAIL PROTECTED]> wrote:
>
> On 11/15/07, Matimus <[EMAIL PROTECTED]> wrote:
> > On Nov 15, 12:45 pm, linda.s <[EMAIL PROTECTED]> wrote:
> > > I run the following code and got the error (I put a .gif file on the 
> > > desktop)
> > > Traceback (most recent call last):
> > >   File "11.py", line 25, in 
> > > for gifname in os.listdir(dirpath):
> > > OSError: [Errno 2] No such file or directory: '.\\Desktop\\'
> > >
> > > import os
> > > import Tkinter
> > >
> > > root = Tkinter.Tk()
> > > L = Tkinter.Listbox(selectmode=Tkinter.SINGLE)
> > > gifsdict = {}
> > >
> > > dirpath = '.\\Desktop\\'
> > > for gifname in os.listdir(dirpath):
> > > if not gifname[0].isdigit():
> > >continue
> > > gifpath = os.path.join(dirpath, gifname)
> > > gif = Tkinter.PhotoImage(file=gifpath)
> > > gifsdict[gifname] = gif
> > > L.insert(Tkinter.END, gifname)
> > >
> > > L.pack()
> > > img = Tkinter.Label()
> > > img.pack()
> > > def list_entry_clicked(*ignore):
> > > imgname = L.get(L.curselection()[0])
> > > img.config(image=gifsdict[imgname])
> > > L.bind('', list_entry_clicked)
> > > root.mainloop()
> >
> > The exception points to this line as being the issue:
> > > for gifname in os.listdir(dirpath):
> >
> > and the error says `No such file or directory: '.\\Desktop\\''
> >
> > So, there must be no directory named '.\\Desktop\\' in your current
> > working directory. To find out your current working directory use
> > `os.getcwd()'. Make sure that `os.getcwd()' returns the path to a
> > directory with a Desktop folder in it. This is usually something like
> > 'C:\\Documents and Settings\\username'.
> >
>
> In my mac machine,
> I got something like:
> >>> os.getcwd()
> '/Users/linda/Desktop'
>
> So I changed the code like:
> dirpath = '.\Users\linda\Desktop\\'
>
> But I still got the error:
> ValueError: invalid \x escape
>

Two things. When you type out a path, if you include a '.' in the
front, that means that the path is relative to your current working
directory. So, if your current working directory is
/Users/linda/Desktop, and dirpath is set to './Users/linda/desktop',
then os.listdir(dirpath) is going to search
'/Users/linda/desktop/Users/linda/Desktop', which probably doesn't
exist. You want to use an absolute path just drop the '.'. If you want
to get the files from the current directory, just use a single '.'.
The following two things should work:

dirpath = "." # This always uses the current working directory
dirpath = "/Users/linda/Desktop" # This will always use the same directory

The second thing, which I think is the error you are currently seeing,
is the escape error. In strings in python the backslash is used as an
escape character, if you want to type a single backslash you have to
type two of them '\\' or, for paths (especially on a mac) is is
completely valid to use forward slashes instead. Also, there are raw
strings which don't do escaping. Any of the following will work:

dirpath = "\\Users\\linda\\Desktop"
dirpath = "/Users/linda/Desktop"
dirpath = r"\Users\linda\Desktop" # prepending an 'r' makes it a raw
string (no escaping)


HTH

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


Re: Python Design Patterns - composition vs. inheritance

2007-11-16 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
> In learning about design patterns, I've seen discussion about using
> inheritance when an object's relationship to another object is 'is-a'
> and composition when the relationship is 'has-a'.

wrt/ inheritance, it only makes sens with declarative static type 
systems where polymorphic dispatch depends on subclassing (so 
inheritance is used for both implementation and subtyping). In the 
context of a dynamic type system, inheritance is only for implementation 
reuse, ie given the following class definitions :

class Foo(object):
   def bar(self):
 return 42

class Baaz(object):
   def bar(self):
 return 84


Baaz actually 'is a' (proper subtype of) Foo (and vice-versa).

> 
> Since this is all new and I'm still learning, I was hoping someone can
> give me some pointers on best practices on applying these ideas. If my
> logic is incorrect on anything, please don't hesitate to tell me where
> I'm wrong - I'm completely open to any help offered.
> 
> As a very simplified example, if I had two classes, Pet and Owner, it
> seems that I would not have Pet inherit from Owner, since a pet 'has
> an' owner, but not 'is an' owner.

Both (I mean 'has a' and 'is a') are not necessary exclusive.

> If this is correct, does my code
> below reflect this? I passed the owner object into the pet object's
> constructor - is this the right way to do it?

Here again, it depends on the lifetime cycles of both objects. Look for 
the difference between composition and aggregation. And it of course 
depends on the problem you're trying to solve - there's no such thing as 
an 'absolute' model.

> Also, I've seen talk that ideally you shouldn't have too many "dots"
> in your method calls, instead using delegates to the methods and
> attributes. Can anyone elaborate on this?

"should", "should not", laws and golden rules... Indeed, it's usually 
better to respect encapsulation.

> Ideally, should I be writing
> getattr() methods so I can do pet.address instead of
> pet.owner.address?

What's the use of pet.address ? Is really 'adress' a responsability of 
'Pet' ?

> Should I be doing the same with owner's methods
> like I did with get_foo()?

Python's methods actually are attributes, so you don't (technically) 
need a distinct scheme for methods - __getattr__ will work as well.

__getattr__ is fine when you really have a generic delegation (look for 
the Proxy pattern for an example). Else (ie, you decide that, for this 
given problem, 'address' is really a responsability of Pet), you might 
be better defining an explicit 'address' attribute in Pet - preferably 
using a computed attribute (either a property object or a custom 
descriptor).

Anyway, the simplest your design and implementation, the better.


My 2 cents...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: overriding methods - two questions

2007-11-16 Thread Donn Ingle
> for child in self.childrens:
> if 'foo' in child.__class__.__dict__:
> child.foo()
Bruno, you're the man! I really must take the time to look into all those
under-under score things! 

Thanks.

/d

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


overriding methods - two questions

2007-11-16 Thread Donn Ingle
Hi,
Here's a framework for the questions:

--- In a module, part of an API ---
class Basis ( object ):
 def foo ( self, arg ):
  pass

--- In user's own code ---
class Child ( Basis ):
 def foo ( self, not, sure ):
  ...


Question 1:

Given that the user of the API can choose to override foo() or not, how can
I control the signature that they use? In the example the user has chosen
bad arguments and Python will complain, but it's describing the sig of the
*overridden* method and not the one in the parent class.

 Is there some way I can control the error message to make it clear to the
user that they are using the signature of foo() incorrectly?

Question 2:

Say I am in class Basis, doing a loop and I have a list of Child objects. I
want to run the foo() method for each one that *has* a foo() method. i.e.
user has done this:

class Sam ( Child ):
 ...
*Sam does not define foo()

class Judy ( Child ):
 def foo ( self, arg ):
  ...
* Judy does define foo()

Instances of Sam and Judy have been put into the list (within the instance)
of Basis. I want Basis to detect that Judy has foo() and run it.

I can handle question 2 by using a flag that must be set by the user.
Something like:
class Judy ( child ):
 def __init__( self ):
  self.pleaseCallFoo = true

And now, Basis can check for that var and only then call foo(), but this is
ugly and means more for the user to learn API-wise.

Any ideas?
/d


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


Re: logging.SocketHandler connections

2007-11-16 Thread Vinay Sajip
On Nov 15, 3:23 pm, oj <[EMAIL PROTECTED]> wrote:
> However, initially, I had tried it with a server that closed the
> connection after receiving each record, and the SocketHandler doesn't
> seem to behave as advertised.
>
> My test script was simply this:
>
[snip]
> The SocketHandler documentation says that it will re-establish the
> connection if it has been closed. After a bit of digging, I found a
> patch had been submitted and accepted that made it back off
> exponentially. However, this should be time based. Even if I make my
> sleep here 30 seconds, my server still only receives messages 0, 3, 6
> and 9.
>
> I'm concerned that if a connection is lost at some point, I will
> always lose at least 2 log messages.
>
> Is there some reason for this that I am not aware of? Have I
> misunderstood something, or is this a bug?

Not sure yet - can you please post your test server code? Feel free to
add it as a bug on bugs.python.org, with the code attached, and I'll
look into it. Include "logging" in the subject/summary line.

Thanks,

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


Re: PDF library

2007-11-16 Thread kyosohma
On Nov 15, 7:17 pm, james_027 <[EMAIL PROTECTED]> wrote:
> hi,
>
> I am looking for a python PDF library that starts with 'Q' ... I just
> found them somewhere in the internet but I can't find them now that I
> need it. Does someone knows this?
>
> Thanks

I'm not sure what you're referring to. The best known one is
ReportLabs for generating PDFs in Python code. There's also pyPdf for
merging PDFs and/or extracting information from them, among other
things.

http://pybrary.net/pyPdf/
www.reportlab.org

Maybe if one of these was listed on a foreign site, it was spelled
with a 'Q'?

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


Re: Looking for docs

2007-11-16 Thread Duncan Booth
Donn Ingle <[EMAIL PROTECTED]> wrote:

> Hi,
> I have seen strange looking things in various Python code like:
> staticmethod and also lines starting with an @ sign, just before
> method defs - I can't find an example right now.
>  I have Python 2.5 installed with it's docs, but I can't find any
>  discussion 
> of 'new style' classes and other info.
> 
> I'm not even sure what subject that @ sign stuff falls under.
> 
> Anyone have a link?
> 
http://www.python.org/doc/2.4.4/whatsnew/node6.html

http://www.python.org/dev/peps/pep-0318/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: overriding methods - two questions

2007-11-16 Thread bearophileHUGS
Donn Ingle:
> Say I am in class Basis, doing a loop and I have a list of Child objects. I
> want to run the foo() method for each one that *has* a foo() method.

This may help (on an old Python version):

>>> class Sam: pass
...
>>> class Judy:
...  def foo(self): pass
...
>>> children = [Sam(), Judy(), Sam()]
>>> for child in children: hasattr(child, "foo")
...
False
True
False

Bye,
bearophile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Design Patterns - composition vs. inheritance

2007-11-16 Thread Bruno Desthuilliers
[EMAIL PROTECTED] a écrit :
(snip)
> It's hard to apply some of the available
> material's examples to Python since a lot of the documentation I find
> is specific to implementations in lower-level languages and don't
> apply to Python. 

Fact is that quite a few design patterns are mostly workaround the 
low-level and lack of dynamism of the C++/Java family. In higher-level 
dynamic languages like Python, Ruby etc, they are not "patterns", they 
are idioms with a good builtin support !-)

> (such as the Strategy pattern?)

The "Strategy" pattern is mostly about delegating (part of) a behaviour 
to someone else. As such, it can be found (one way or another) in almost 
any language - even in C with function pointers (look at how the C 
stdlib sort() function works...).

> My understanding was that using __getattr__ was either called
> delegation or a Command pattern,

Now we have proper computed attributes, __getattr__ is mainly used for 
delegation, yes. But some years ago, it was also the way to implement 
computed attributes.

(snip)

>> Of course there are just reasons to create such delegation methods - if
>> for example the property/method is of general interest to the Pet, but
>> implemented by means of the owner. But I've got difficulties even to
>> imagine such thing, at least in your actual example.
> 
> Yeah, I was struggling to come up with a decent example - a pet's
> address was about the best example of a delegated property I could
> think of. If someone has a better set of objects that make sense, let
> me know and I'll probably feel less foolish asking.


class Forbidden(Exception): pass

class Interceptor(object):
   def __init__(self, obj, allow_access):
 self.__obj = obj
 self.__allow_access = allow_access

   def __getattr__(self, name):
   if self.__allow_access(self.__obj, name):
   return getattr(self.__obj, name)
   else:
   raise Forbidden

As Carl mentioned, real-life code usually uses more technical objects 
than domain objects.


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


Re: overriding methods - two questions

2007-11-16 Thread Donn Ingle
>This may help (on an old Python version):
 class Sam: pass
 class Judy:
> ...def foo(self): pass
> ...
 children = [Sam(), Judy(), Sam()]
 for child in children: hasattr(child, "foo")
> ...
> False
> True
> False

That's not what my tests are showing. While Sam has no foo, it's coming from
(in my OP) Child (which is the parent class), so hasattr(Sam(),"foo") is
returning True.

/d

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


build a hierarchical tree, without using DOM,schema, and sax using expat parser and c

2007-11-16 Thread sharan
I've been experimenting with the jclark's expat parser.  I compiled it
on
linux, and it works just great.  However, finding tags and data by
implementing the callback functions in c language is cumbersome.
Has anybody written code that uses the expat callback's in such a way
as to
build a hierarchical tree, without using DOM,schema, and sax, that can
be traversed.My purpose, just enough to traverse branches and find
leafs of a xml documents without using DOM, schema, and sax, and i
also want help regarding how to convert a xml document to a
hierachical tree which contains tags values. thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is python?????

2007-11-16 Thread Shawn Milochik
However, the python is not poisonous, so it is also edible if you can kill
one before it squeezes you to death. Despite this fact, it is not a major
food source for group members, due to our great respect for the mighty
python.

Shawn



On Nov 16, 2007 9:14 AM, Thorsten Kampe <[EMAIL PROTECTED]> wrote:

> * Cope (Fri, 16 Nov 2007 06:09:31 -0800 (PST))
> > please tell me what is python.This group is so crowded.
>
> A Python is dangerous snake[1]. This group here mainly consists of
> misguided snake worshippers. You'd better run before they come to your
> place...
>
> Thorsten
> [1] http://en.wikipedia.org/wiki/Pythonidae
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Please read:
http://milocast.com/2007/07/31/this-i-believe/

A good reason not to say "I don't know -- what do you want to do?"
http://xkcd.org/330/
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: overriding methods - two questions

2007-11-16 Thread Bruno Desthuilliers
Donn Ingle a écrit :
>> for child in self.childrens:
>> if 'foo' in child.__class__.__dict__:
>> child.foo()
> Bruno, you're the man! I really must take the time to look into all those
> under-under score things! 

Knowing Python's object model can help, indeed !-)

Now while this kind of stuff is ok in the low-level parts of a 
framework, it shouldn't be seen too much in application code IMHO.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: insert comments into elementtree

2007-11-16 Thread Stefan Behnel
Tim Arnold wrote:
> Hi, I'm using the TidyHTMLTreeBuilder to generate some elementtrees from 
> html. One by-product is that I'm losing comments embedded in the html.

That's how the parser in ET works. Use lxml instead, which keeps documents
intact while parsing.

http://codespeak.net/lxml/dev/
http://codespeak.net/lxml/dev/lxmlhtml.html

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


RE: How to evaluate the code object returned by PyParser_SimplePa rseString function?

2007-11-16 Thread Borse, Ganesh
Thanks this is helpful.

-Original Message-
From: Gabriel Genellina [mailto:[EMAIL PROTECTED] 
Sent: 15 November 2007 12:26
To: python-list@python.org
Subject: Re: How to evaluate the code object returned by PyParser_SimplePa 
rseString function?

En Wed, 14 Nov 2007 23:20:14 -0300, Borse, Ganesh <[EMAIL PROTECTED]> escribió:

> Py_CompileString takes the source code from file, isn't it?
> As can be seen from the syntax of this function: PyObject* 
> Py_CompileString(char *str, char *filename, int start)
>
> I want to parse the code which is in memory - loaded from database.
> In that case, may I know, how to use the Py_CompileString?

The first argument is the actual source code string, a char*; the filename 
argument is only used to generate pretty error messages, you may pass in some 
meaningful name or ''.

> If it is mandatory to read from file for this function?
> Reading from file increases startup time of my application.
> So, I was thinking of using PyParser_SimpleParseString, which takes 
> the code to be parsed in the "char*" format. Quit suitable to my need.

No, both Py_CompileString and PyParser_SimpleParseString take the same first 
argument, a char* with the source code.

> Can I use the output of the function PyParser_SimpleParseString as 
> input to PyEval_EvalCode?

Not directly, you must compile the parsed input first. See pythonrun.c for 
examples, but you will end duplicating what Py_CompileString already does.

--
Gabriel Genellina



==
Please access the attached hyperlink for an important electronic communications 
disclaimer: 

http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
==

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


Re: Memory problem

2007-11-16 Thread Rolf Wester
Hi,

thank you for your comments and your hints (I probably deserve some kind 
of subtle irony). I found the problem:

I thought a numpy array A has shape (n,) but actually it had shape 
(n,1). In the loop I sampled a value from that array:

v.append(A[i])

So what happened was that I got a view of A not a single number and 
append obviously appended the whole array not just a single element 
array. And n is about 64000.

So sorry for bothering you with that stupid fault.

Regards

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


Re: Python Design Patterns - composition vs. inheritance

2007-11-16 Thread A.T.Hofkamp
On 2007-11-15, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> inheritance when an object's relationship to another object is 'is-a'
> and composition when the relationship is 'has-a'.
>
> Since this is all new and I'm still learning, I was hoping someone can
> give me some pointers on best practices on applying these ideas. If my
> logic is incorrect on anything, please don't hesitate to tell me where
> I'm wrong - I'm completely open to any help offered.

>
> As a very simplified example, if I had two classes, Pet and Owner, it
> seems that I would not have Pet inherit from Owner, since a pet 'has
> an' owner, but not 'is an' owner. If this is correct, does my code
> below reflect this? I passed the owner object into the pet object's
> constructor - is this the right way to do it?

This is indeed one way. One of the things you enforce in this way that there is
no Pet object without an owner.

> Also, I've seen talk that ideally you shouldn't have too many "dots"
> in your method calls, instead using delegates to the methods and
> attributes. Can anyone elaborate on this? Ideally, should I be writing

As with most design stuff, they are right and they are also wrong. It's a
trade-off.

They are right in the sense that if you change the structure of your links, you
will break potentionally a lot of code. Imagine you have to ask a data base for
the owner rather than having a direct link. All "pet.owner" references will
need to be changed then. If you hide the connection inside your Pet, there is
only one place that needs to be changed.

They are wrong in the sense that it is not always appropiate for a Pet object
to perform such a function. By hiding "self.owner.address" inside Pet, you are
designing a smart pet that knows how to find the address of its owner.
Unfortunately, smarter also means more complex, and at some point you will
defeat the advantage of OO, namely spreading responsibilities, and having
several simple objects that work together to realize your application.

So there is a trade-off. There is no universal rule, it highly depends on your
application, your future plans, and the lifetime of the app (to name a few).

> getattr() methods so I can do pet.address instead of
> pet.owner.address? Should I be doing the same with owner's methods
> like I did with get_foo()?

I'd say no.
One of the 'rules' (guide lines) of Python is "Be explicit rather than
implicit" [1]. You may save a few dots, on the other hand, you obfuscate how
the link is realized (that is "pet.address" doesn't say it uses owner to make
the connection, unlike "pet.owner.address").
In the long run, the latter may be more important.


In general, I think you shouldn't need advanced trickery like __getattr__() for
your design. If you do, your design is most likely wrong.

[1]: The Zen of Python: http://www.python.org/dev/peps/pep-0020/


Sincerely,
Albert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: overriding methods - two questions

2007-11-16 Thread Donn Ingle
> Actually, Python is complaining about your user's poor choice of
> argument names. 'not' is a reserved keyword.  
My example was poor, but my actual test code did't use 'not'. Python simply
checks the use of foo() to the local sig of foo() and does not go up the
chain. This is understandable and your next answer is more-or-less what I
was expecting.

> Python's philosophy of "you are the human, so you must know what you
> are doing" (which is both an assumption and a directive), I don't
> think you will find much language machinery to prevent it.

Yeah. I guess I was hoping there'd be some clever trick to do it.

/d

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


Re: Populating a dictionary, fast [SOLVED SOLVED]

2007-11-16 Thread Istvan Albert
On Nov 16, 1:18 pm, "Michael Bacarella" <[EMAIL PROTECTED]> wrote:

> You're right, it is completely inappropriate for us to be showing our
> dirty laundry to the public.

you are misinterpreting my words on many levels,

(and I of course could have refrained from the "chair-monitor" jab as
well)

anyhow, it is what it is, I could not reproduce any of the weird
behaviors myself, I got nothing more to add to this discussion
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: gc penalty of 30-40% when manipulating large data structures?

2007-11-16 Thread Istvan Albert
On Nov 16, 10:59 am, "Chris Mellon" <[EMAIL PROTECTED]> wrote:

> The GC has a heuristic where it kicks in when (allocations -
> deallocations) exceeds a certain threshold,

As the available ram increases this threshold can be more easily
reached. Ever since I moved to 2Gb ram I stumbled upon issues that
were easily solved by turning the gc off (the truth is that more ram
made me lazier, I'm a little less keen to keep memory consumption down
for occasional jobs, being overly cavalier with generating lists of
1Gb in size...)

One example, when moving from a list size from 1 million to 10 million
I hit this threshold. Nowadays I disable the gc during data
initialization.

i.



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


Re: Python Design Patterns - composition vs. inheritance

2007-11-16 Thread Neil Cerutti
On 2007-11-16, Dennis Lee Bieber <[EMAIL PROTECTED]> wrote:
> On Thu, 15 Nov 2007 12:28:28 -0800 (PST), "[EMAIL PROTECTED]"
><[EMAIL PROTECTED]> declaimed the following in comp.lang.python:
>
>> 
>> As a very simplified example, if I had two classes, Pet and Owner, it
>> seems that I would not have Pet inherit from Owner, since a pet 'has
>> an' owner, but not 'is an' owner. If this is correct, does my code
>> below reflect this? I passed the owner object into the pet object's
>> constructor - is this the right way to do it?
>>
>   Well, other than the facet that I'd say "Owner" has-a "Pet", but
> this example is a bit too disjoint for the association (IE, it goes both
> ways)
>
>   A common example is something like:
>
> class Vehicle(object):
>   #which has attributes for speed, direction, cargo capacity,
> passenger capacity
>
> class RoadVehicle(Vehicle):
>   #inherits (RoadVehicle IS-A Vehicle with Axles)
>
> class Auto(RoadVehicle)
>   #  HAS-A Front-Axle, Rear-Axle
>   #   and Axle HAS-A Left-Tire, Right-Tire (unless a Dual -- with
> inner left, outer left, inner right, outer right, those those would
> appear on Bus(RoadVehicle) and Truck(RoadVehicle) classes)
>
> Look up "Law of Demeter" (I believe that is the name). The idea
> is that a "user" of, say, Auto, should NOT need to know that it
> has tires (may know it has axles, so it may have a
> "listAxles()" method, which returns a list/tuple of the axles
> that the Auto has. You could then use that list to access the
> tires on each axle { for tire in aRoadVehicle.listAxles():
> print tire.specification() }

The Law of Demeter constrains the attributes that the methods of
Auto may invoke. Given the above containment hierarchy, the Law
says that no method of Auto may depend on any attribute or method
of Tire. So if you needed to compute, in an Auto method,
something that depends on an attribute of Tire, you must make
that information available directly through the Axle interface.

The benefit is that this lowers the coupling between classes. The
Tire interface can change completely without affecting the Auto
class.

> BUT... Just look at the Python library... sys.stdout.write(),
> if following Law of Demeter would turn into:
>
>   myStdout = sys.getStdout()
>   myStdout.write()

The Law of Demeter doesn't apply to that example.

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


Re: build a hierarchical tree, without using DOM,schema, and sax using expat parser and c

2007-11-16 Thread Wildemar Wildenburger
sharan wrote:
> I've been experimenting with the jclark's expat parser.  I compiled it
> on
> linux, and it works just great.  However, finding tags and data by
> implementing the callback functions in c language is cumbersome.
> Has anybody written code that uses the expat callback's in such a way
> as to
> build a hierarchical tree, without using DOM,schema, and sax, that can
> be traversed.My purpose, just enough to traverse branches and find
> leafs of a xml documents without using DOM, schema, and sax, and i
> also want help regarding how to convert a xml document to a
> hierachical tree which contains tags values. thanks

Not sure if I completely follow you. Maybe ElementTree could be of help?

http://effbot.org/zone/element-index.htm>

since Python 2.5 it is even in the standard lib:

http://docs.python.org/lib/module-xml.etree.ElementTree.html>

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


Re: timing puzzle

2007-11-16 Thread Robin Becker
Neil Cerutti wrote:
...


  see why.
> 
> You are no longer making m copies of active_nodes.

my profiling indicated that the main problem was the removes.

> 

> 
> When you have to make many deletions from the middle of a
> sequence, you would normally choose a linked list. Python doesn't
> provide much support for linked lists, unfortunately.
> 
> Instead, filter your list. It looks like you can't use filter
> directly, so just do it manually.
> 
>for i in xrange(m):
>...
>saved_nodes = []
>for A in active_nodes[:]:
>   ..
>   if not cond:
>   saved_nodes.append(A)
> 
>   ..
>active_nodes = saved_nodes
>.
> 

I like this approach, better than mine.
-- 
Robin Becker
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Volume id

2007-11-16 Thread Dieter Verfaillie
On Thu, 2007-11-15 at 17:05 +0100, Gabor Urban wrote:
> OK, you are right... Problem was not precise enough. I need to process
> CDs to create a list. Does it ring a bell for you?
> 
> Thanks

Hello,

The method below will work on linux systems (it uses dbus to communicate
with HAL). You'll maybe have to expand the filter on line 37, but I'm
not sure...

hth,
Dieter

#!/usr/bin/env python

import dbus

def discover():
disks = []
volumes = []

# get a connection to the system bus
bus = dbus.SystemBus ()

# get a HAL object and an interface to HAL to make function calls
hal_obj = bus.get_object ('org.freedesktop.Hal',
  '/org/freedesktop/Hal/Manager')
hal = dbus.Interface (hal_obj, 'org.freedesktop.Hal.Manager')

# find all devices that have the capability 'volume'
udis = hal.FindDeviceByCapability('volume')

for udi in udis:
# get volume info
dev_obj = bus.get_object('org.freedesktop.Hal', udi)
dev = dbus.Interface(dev_obj, 'org.freedesktop.Hal.Device')
volume = str(dev.GetProperty('block.device'))
volume_label = str(dev.GetProperty('volume.label'))
volume_mount_point = str(dev.GetProperty('volume.mount_point'))
volume_fstype = str(dev.GetProperty('volume.fstype'))

# get storage info
parent_udi = dev.GetProperty('info.parent')
dev_obj = bus.get_object('org.freedesktop.Hal', parent_udi)
dev = dbus.Interface(dev_obj, 'org.freedesktop.Hal.Device')
storage = str(dev.GetProperty('block.device'))
storage_product = str(dev.GetProperty('info.product'))

# filter out hard disks
if dev.GetProperty('storage.drive_type') == 'disk':
continue

# store disk
if not storage in disks:
disks.append(storage)

# store volume
volumes.append((storage,
volume,
volume_label,
volume_mount_point,
volume_fstype))

return disks, volumes

if __name__ == '__main__':
disks, volumes = discover()

for disk in disks:
print 'found disk', disk

for volume in volumes:
if volume[0] == disk:
print 'with volume', volume[1]
print 'label', volume[2]
print 'mount point is', volume[3]
print 'fstype is', volume[4]



signature.asc
Description: This is a digitally signed message part
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: timing puzzle

2007-11-16 Thread Neil Cerutti
On 2007-11-16, Robin Becker <[EMAIL PROTECTED]> wrote:
> Neil Cerutti wrote:
> ...
>
>
>   see why.
>> 
>> You are no longer making m copies of active_nodes.
>
> my profiling indicated that the main problem was the removes.

Yeah, I should've added, "for one thing." I'm glad Chris
correctly pointed out that you used del in one case, and remove
in the other.

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


Re: Sorting Countries by Region

2007-11-16 Thread martyw
[EMAIL PROTECTED] wrote:
> Hi all,
> 
> I'm analyzing some data that has a lot of country data.  What I need
> to do is sort through this data and output it into an excel doc with
> summary information.  The countries, though, need to be sorted by
> region, but the way I thought I could do it isn't quite working out.
> So far I can only successfully get the data alphabetically.
> 
> Any ideas?
> 
> import xlrd
> import pyExcelerator
> 
> def get_countries_list(list):
it isn't a good idea to use a built-in object as a variable name
> countries_list=[]
> for country in countries:ii
> if country not in countries_list:
> countries_list.append(country)
> 
> EU = ["Austria","Belgium", "Cyprus","Czech Republic",
> "Denmark","Estonia", "Finland"]
> NA = ["Canada", "United States"]
> AP = ["Australia", "China", "Hong Kong", "India", "Indonesia",
> "Japan"]
> Regions_tot = {'European Union':EU, 'North America':NA, 'Asia
> Pacific':AP,}
i would create a class to capture country information, e.g.
class country(object):
 def __init__(self, name, size = 0, population = 0):
 self.name = name
 self.size = size
 self.poplation = population

 def __cmp__(self, other):
 if self.name < other.name:
 return -1
 elif self.name > other.name:
 return 1
 else:
 return 0

then you can set up the world as

world = {'NA': [country("United States"), country("Canada")], \
  'Europe': [country("Belgium"), country("Austria")]}


now you can sort and print it easy

for region in world:
 print region
 lands = world[region]
 lands.sort()
 for land in lands:
 print land.name

the sort works because the country objects have a method __cmp__

> 
> path_file = "c:\\1\country_data.xls"
> book = xlrd.open_workbook(path_file)
> Counts = book.sheet_by_index(1)
> countries= Counts.col_values(0,start_rowx=1, end_rowx=None)
> 
> get_countries_list(countries)
> 
> wb=pyExcelerator.Workbook()
> matrix = wb.add_sheet("matrix")
> 
> n=1
> for country in unique_countries:
> matrix.write(n,1, country)
> n = n+1
> 
> wb.save('c:\\1\\matrix.xls')
> 
> 
> 

i'm not familiar with the excel modules so i can't help you with that
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Populating a dictionary, fast [SOLVED SOLVED]

2007-11-16 Thread Jeffrey Froman
Steven D'Aprano wrote:

> Can you try it running in 64-bit mode?

Here are my results using the following test.py:
$ cat test.py
#!/usr/bin/python
import time
print "Starting: %s" % time.ctime()
v = {}
for line in open('keys.txt'):
v[long(line.strip())] = True
print "Finished: %s" % time.ctime()


32-bit architecture:
-
[machine1]$ python2.3 test.py
Starting: Fri Nov 16 11:51:22 2007
Finished: Fri Nov 16 11:52:39 2007

[machine2]$ python2.5 test.py
Starting: Fri Nov 16 11:57:57 2007
Finished: Fri Nov 16 11:58:39 2007


64-bit architecture (64-bit mode):
-
[machine3]$ python2.3 test.py
Starting: Fri Nov 16 11:51:44 2007
Finished: Fri Nov 16 12:31:54 2007

[machine3]$ python2.5 test.py
Starting: Fri Nov 16 11:50:03 2007
Finished: Fri Nov 16 11:50:31 2007


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


insert comments into elementtree

2007-11-16 Thread Tim Arnold
Hi, I'm using the TidyHTMLTreeBuilder to generate some elementtrees from 
html. One by-product is that I'm losing comments embedded in the html. So 
I'm trying to put them back in, but I'm doing something wrong: here's the 
code snippet of how I generate the Trees:

from elementtree import ElementTree as ET
from elementtidy import TidyHTMLTreeBuilder
XHTML = "{http://www.w3.org/1999/xhtml}";

htmfile = os.path.join(self.htmloc,filename)
fd = open(htmfile)
tidyTree = TidyHTMLTreeBuilder.TidyHTMLTreeBuilder('utf-8')
tidyTree.feed(fd.read())
fd.close()
try:
tmp = tidyTree.close()
except:
print 'Bad file: %s\nSkipping.' % filename
continue
 tree = ET.ElementTree(tmp)

and here's the method I use to put the comments back in:

def addComments(self,tree):
body = tree.find('./%sbody' % XHTML)
for elem in body:
if elem.tag == '%sdiv' % XHTML and elem.get('class'):
if elem.get('class') == 'remapped':
comElem = ET.SubElement(elem,ET.Comment('stopindex'))

self.addComments(tree)
filename = os.path.join(self.deliverloc,name)
self.htmlcontent.write(tree,filename,encoding=self.encoding

when I try this I get errors from the ElementTree _write method:
TypeError: cannot concatenate 'str' and 'instance' objects

thanks for any help!
--Tim Arnold




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


RE: Populating a dictionary, fast [SOLVED SOLVED]

2007-11-16 Thread Michael Bacarella
> Do you really believe that you cannot create or delete a large
> dictionary with python versions less than 2.5 (on a 64 bit or multi-
> cpu system)? That a bug of this magnitude has not been noticed until
> someone posted on clp?

You're right, it is completely inappropriate for us to be showing our
dirty laundry to the public.  From now we will try to deal with our troubles
privately. 

I am so sorry to have ruined the decorum.

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


Re: Using python as primary language

2007-11-16 Thread Jean-Paul Calderone
On Wed, 14 Nov 2007 09:25:04 -0800, sturlamolden <[EMAIL PROTECTED]> wrote:
> [snip]
>I think the need for these "eventloop unifications" stems from Visual
>Basic. VB programmers never learned to use more than one thread, and
>they are still struggling to unlearn the bad habits they aquired.
>

+1 QOTW

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


timing puzzle

2007-11-16 Thread Robin Becker
I'm trying to get my head round a timing puzzle I find whilst optimizing A 
Kuchling's version of the Knuth line splitting algorithm from the oedipus 
project. The puzzle is as follows in highly abstract form (here
  active_nodes is a list of objects kept in a sorted order, but there are no 
special methods on the objects for comparison etc)


for i in xrange(m):
...
for A in active_nodes[:]:
   ..
   if cond:
   active_nodes.remove(A)
   ..
.

is slow my test takes 2.7 seconds; profiling reveals we're calling A.remove 
thousands of times and it's taking a lot of time. There appear to be no other 
usages of active_nodes in the for A loop.


On a hunch I changed the above to

 removed = []
 aremoved = removed.append
 for i in xrange(m):
...
for iA,A in enumerate(active_nodes):
   ..
   if cond:
   aremoved(iA)
   ..
if removed:
for iA in reversed(removed):
del active_nodes[iA]
del removed[:]
..

this latter is 5 times faster, but I can't really see why. My only guess is 
that 
  appending to the removed list is relatively easy compared with deletes and 
the 
reverse order delete is somehow moving less data about. I think in the original 
Knuth the deletion would have been from some real kind of list and O(1) whereas 
here deletions are O(n/2) on average. On the other hand we have to keep this 
list in sorted order. What data structure should I be using? I should add that 
I 
tried using a __cmp__ method to assist in doing the sorted insert, but that 
interfered with the simple active_nodes.remove.
--
Robin Becker

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


Re: build a hierarchical tree, without using DOM,schema, and sax using expat parser and c

2007-11-16 Thread Stefan Behnel
Wildemar Wildenburger wrote:
> sharan wrote:
>> I've been experimenting with the jclark's expat parser.  I compiled it
>> on
>> linux, and it works just great.  However, finding tags and data by
>> implementing the callback functions in c language is cumbersome.
>> Has anybody written code that uses the expat callback's in such a way
>> as to
>> build a hierarchical tree, without using DOM,schema, and sax, that can
>> be traversed.My purpose, just enough to traverse branches and find
>> leafs of a xml documents without using DOM, schema, and sax, and i
>> also want help regarding how to convert a xml document to a
>> hierachical tree which contains tags values. thanks
> 
> Not sure if I completely follow you. Maybe ElementTree could be of help?

Just a quick note that it actually uses expat for parsing, so there's no need
to fiddle with it on your own - especially since it's pretty hard to get any
faster than the C implementation of ET, cElementTree, is already...

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


Re: Resolving declaring class of a method at runtime

2007-11-16 Thread Steven D'Aprano
On Fri, 16 Nov 2007 19:02:25 +0200, Janne Härkönen
wrote:

>> X is an "old style" class. Most people probably shouldn't use old style
>> classes, for various reasons. To use new style classes, you inherit
>> from object:
> 
> I am also aware of old and new style classes, this was the fastest way
> to type it in console

Ah, an optimization huh?

So, by saving 1/2 a second by not typing "(object)" in your example code, 
the consequences are that I wasted my time explaining that you should use 
new style classes, and you wasted your time reading and responding to my 
explanation, and now I'm wasting my time *again* telling you off for 
wasting my time in the first place.

There should be a term for "optimization" techniques that actually slow 
things down instead of speeding them up.


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

Re: Trouble building pywin32 with Visual Studio 2005

2007-11-16 Thread danfike
> > If you know what you are doing, you can override the logic of distutils.
> > Set up an SDK environment (with LIBRARY, INCLUDE and everything), then
> > also set the MSSdk environment variable (which should get set if you
> > use the standard environment batch file from the SDK), and then also
> > set DISTUTILS_USE_SDK. Then distutils will trust that the environment
> > you set up works correctly, and will use it without further questioning.
>
> I'll see if I can get that working. During my internet searches, I
> came across a mail/post/blog or two indicating DISTUTILS_USE_SDK does
> not work in Visual Studio 2005. I'll see what I can do.

I didn't have much luck with this. It looks like my solution is going
to be to just ignore this problem for now. In most cases, the binary
modules one downloads are written well enough that these DLLs aren't
going to step on eachother's toes. In fact, I haven't found a function
aside from win32com.server.register.RegisterClasses which has the same
problem. Now, if everybody moves to VS2008 for Python 3000, this
problem would work itself out. :)

Thanks for your help, Martin.

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


Re: timing puzzle

2007-11-16 Thread Chris Mellon
On Nov 16, 2007 12:42 PM, Robin Becker <[EMAIL PROTECTED]> wrote:
> I'm trying to get my head round a timing puzzle I find whilst optimizing A
> Kuchling's version of the Knuth line splitting algorithm from the oedipus
> project. The puzzle is as follows in highly abstract form (here
>   active_nodes is a list of objects kept in a sorted order, but there are no
> special methods on the objects for comparison etc)
>
>
> for i in xrange(m):
> ...
> for A in active_nodes[:]:
>..
>if cond:
>active_nodes.remove(A)
>..
> .
>
> is slow my test takes 2.7 seconds; profiling reveals we're calling A.remove
> thousands of times and it's taking a lot of time. There appear to be no other
> usages of active_nodes in the for A loop.
>
>
> On a hunch I changed the above to
>
>  removed = []
>  aremoved = removed.append
>  for i in xrange(m):
> ...
> for iA,A in enumerate(active_nodes):
>..
>if cond:
>aremoved(iA)
>..
> if removed:
> for iA in reversed(removed):
> del active_nodes[iA]
> del removed[:]
> ..
>
> this latter is 5 times faster, but I can't really see why. My only guess is 
> that
>   appending to the removed list is relatively easy compared with deletes and 
> the
> reverse order delete is somehow moving less data about. I think in the 
> original
> Knuth the deletion would have been from some real kind of list and O(1) 
> whereas
> here deletions are O(n/2) on average. On the other hand we have to keep this
> list in sorted order. What data structure should I be using? I should add 
> that I
> tried using a __cmp__ method to assist in doing the sorted insert, but that
> interfered with the simple active_nodes.remove.
> --

remove() does a linear search to find the item to remove, so it's O(n)
+ the actual deletion. Append() is amortized O(1) (overcommit). If you
delete by index instead:
for idx, node in active_nodes:
  if cond:
del active_nodes[idx]


You should see performance comparable to your second option. If you
can trade memory for performance, discarding the old active_nodes
might be even faster:
active_nodes = [node for node in active_nodes if node not in
deleted_nodes], where deleted_nodes is a set.


Normal micro-optimization techniques apply here too, like looking up
the remove() method ahead of time and so on.

> Robin Becker
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Sorting Countries by Region

2007-11-16 Thread patrick . waldo
Hi all,

I'm analyzing some data that has a lot of country data.  What I need
to do is sort through this data and output it into an excel doc with
summary information.  The countries, though, need to be sorted by
region, but the way I thought I could do it isn't quite working out.
So far I can only successfully get the data alphabetically.

Any ideas?

import xlrd
import pyExcelerator

def get_countries_list(list):
countries_list=[]
for country in countries:
if country not in countries_list:
countries_list.append(country)

EU = ["Austria","Belgium", "Cyprus","Czech Republic",
"Denmark","Estonia", "Finland"]
NA = ["Canada", "United States"]
AP = ["Australia", "China", "Hong Kong", "India", "Indonesia",
"Japan"]
Regions_tot = {'European Union':EU, 'North America':NA, 'Asia
Pacific':AP,}

path_file = "c:\\1\country_data.xls"
book = xlrd.open_workbook(path_file)
Counts = book.sheet_by_index(1)
countries= Counts.col_values(0,start_rowx=1, end_rowx=None)

get_countries_list(countries)

wb=pyExcelerator.Workbook()
matrix = wb.add_sheet("matrix")

n=1
for country in unique_countries:
matrix.write(n,1, country)
n = n+1

wb.save('c:\\1\\matrix.xls')



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


Re: What is python?????

2007-11-16 Thread [EMAIL PROTECTED]
On Nov 16, 8:14 am, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
> * Cope (Fri, 16 Nov 2007 06:09:31 -0800 (PST))
>
> > please tell me what is python.This group is so crowded.
>
> A Python is dangerous snake[1]. This group here mainly consists of
> misguided snake worshippers. You'd better run before they come to your
> place...
>
> Thorsten
> [1]http://en.wikipedia.org/wiki/Pythonidae

Don't listen to him, he was joking.

"Python" is short for "Monty Python's Flying Circus",
a British television comedy originally from 1969-1971
(although movies, records, books and Broadway shows
continue to the present). Read the documention,
you'll see numerous references to spam (used in one
of their famous routines) and other Pythonisms.

Anyway, all the posting in this group is comedy.
If you aren't getting it, run out and buy and study
all the DVD's. When you've reached the point where
you have The Argument Clinic dialogue memorized,
come back here and all this will make sense.

Just be careful, some of the Python routines aren't
SUPPOSED to make sense ("cabbage crates over the how's
your father"), that's the joke.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: timing puzzle

2007-11-16 Thread Neil Cerutti
On 2007-11-16, Robin Becker <[EMAIL PROTECTED]> wrote:
> I'm trying to get my head round a timing puzzle I find whilst
> optimizing A Kuchling's version of the Knuth line splitting
> algorithm from the oedipus project. The puzzle is as follows in
> highly abstract form (here active_nodes is a list of objects
> kept in a sorted order, but there are no special methods on the
> objects for comparison etc)
>
> for i in xrange(m):
> ...
> for A in active_nodes[:]:
>..
>if cond:
>active_nodes.remove(A)
>..
> .
>
> is slow my test takes 2.7 seconds; profiling reveals we're
> calling A.remove thousands of times and it's taking a lot of
> time. There appear to be no other usages of active_nodes in the
> for A loop.
>
> On a hunch I changed the above to
>
>  removed = []
>  aremoved = removed.append
>  for i in xrange(m):
> ...
> for iA,A in enumerate(active_nodes):
>..
>if cond:
>aremoved(iA)
>..
> if removed:

'if removed' is reduntant, as the loop body below will not be
executed if removed is empty.

> for iA in reversed(removed):
> del active_nodes[iA]
> del removed[:]
> ..
>
> this latter is 5 times faster, but I can't really see why. 

You are no longer making m copies of active_nodes.

> My only guess is that appending to the removed list is
> relatively easy compared with deletes and the reverse order
> delete is somehow moving less data about. I think in the
> original Knuth the deletion would have been from some real kind
> of list and O(1) whereas here deletions are O(n/2) on average.
> On the other hand we have to keep this list in sorted order.
> What data structure should I be using?

When you have to make many deletions from the middle of a
sequence, you would normally choose a linked list. Python doesn't
provide much support for linked lists, unfortunately.

Instead, filter your list. It looks like you can't use filter
directly, so just do it manually.

   for i in xrange(m):
   ...
   saved_nodes = []
   for A in active_nodes[:]:
  ..
  if not cond:
  saved_nodes.append(A)

  ..
   active_nodes = saved_nodes
   .

-- 
Neil Cerutti
Sermon Outline: I. Delineate your fear II. Disown your fear III. Displace your
rear --Church Bulletin Blooper
-- 
http://mail.python.org/mailman/listinfo/python-list


PIL question

2007-11-16 Thread Thomas Heller
I'm trying to read an image with PIL, crop several subimages out of it,
and try to combine the subimages again into a combined new one.  This is
a test script, later I want to only several single images into a combined one.

I have to work with 16-bit color windows bitmaps in BMP format.

My input image (input.bmp) IS a 16-color bmp.  The cropped images that
the script saves are saved in 8-bit/256-color format; they look correct.
The combined image is also saved in 8-bit/256-color format, but it is
all black - maybe the palette is missing?

Anyway, I cannot use 256-color images.  How do I save the images in 
4-bit/16-color
format?

Thanks,
Thomas

Here is the code; I'm using Python 2.4 and PIL 1.5:


import Image
img = Image.open("input.bmp")

w, h = img.size
print img.size, img.mode

newimg = Image.new("P", (16*30, 15))

x = 0
i = 1
while x < w:
this = img.crop((x, 0, x+16, 15))
this.save("%02d.bmp" % i)
newimg.paste(this, (x, 0, x+16, 15))
x += 16
i += 1

newimg.save("output.bmp")



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


Re: timing puzzle

2007-11-16 Thread Neil Cerutti
On 2007-11-16, Neil Cerutti <[EMAIL PROTECTED]> wrote:
> Instead, filter your list. It looks like you can't use filter
> directly, so just do it manually.
>
>for i in xrange(m):
>...
>saved_nodes = []
>for A in active_nodes[:]:

I meant to remove the slice. That line should be:

  for A in active_nodes:

>   ..
>   if not cond:
>   saved_nodes.append(A)
>
>   ..
>active_nodes = saved_nodes
>.

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


Re: implement random selection in Python

2007-11-16 Thread Boris Borcic
Boris Borcic wrote:
> Bruza wrote:
>> No. That does not solve the problem. What I want is a function
>>
>>   def randomPick(n, the_items):
>>
>> which will return n DISTINCT items from "the_items" such that
>> the n items returned are according to their probabilities specified
>> in the (item, pro) elements inside "the_items".
> 
> and in the initial post you wrote :
> 
>  > But how about the general case, for N > 1 and N < len(items)?
> 
> The problem is you need to make "the n items returned are according
> to their probabilities" more precise. "According to their probabilities" 
> implies 
> n INDEPENDENT picks, but this is contradictory with the n picks having to 
> provide DISTINCT results (what clearly constrains picks relative to each 
> other).
> 
> Of course there are obvious ways to combine the results of random choices of 
> single items to obtain a set like you want, but it is not obvious that they 
> are 
> equivalent.
> 
> This is the most simple-minded :
> 
> def randomPick(n, the_items) :
>  assert n  result = set()
>  while len(result)result.add(singlePick(the_items))
>  return sorted(result)
> 
> This is another (but it won't work with your version of singlePick as it is,
> although it would with that provided by the other posters) :
> 
> def randomPick(n, the_items) :
>  result = []
>  items = dict(the_items)
>  for k in range(n) :
>  pick = singlePick(items.items())
>  result.append(pick)
>  del items[pick]
>  return result
> 
> I would be surprised if they had exactly the same statistical properties, 
> IOW, 
> if they did fit the same exact interpretation of "according to their 
> probabilities".
> 
> 

yet another one, constructing a list of n-sets first, and then picking one;
like the other solutions, it doesn't optimize for repeated use.

def pickn(items,n) :
 "yields all n-sublists of (destructed) items"
 if n<=len(items) :
 if n :
 item = items.pop()
 for res in pickn(items[:],n) :
 yield res
 for res in pickn(items,n-1) :
 res.append(item)
 yield res
 else :
 yield []


def randomPick(n,the_items) :
 "randomly pick n distinct members of the_items"
 the_sets = list(pickn(the_items[:],n))
 divisor = len(the_sets)*float(n)/len(the_items)
 for k,s in enumerate(the_sets) :
 the_sets[k] = (sorted(who for who,_ in s),
int(1+sum(p for _,p in s)/divisor)) # mhh...
 return singlePick(the_sets)

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


Fwd: Sorting Countries by Region

2007-11-16 Thread Sergio Correia
Just a few notes:

1) get_countries_list

What is the purpose of that function? Besides a few errors (an
argument named list, no value returned), it seems you just want to
remove duplicates from a list called countries. You can do that
transforming the list to a 'set'.

new_countries =  list( set(countries) )

2) I would suggest using countries.sort(...) or sorted(countries,...),
specifying cmp or key options too sort by region instead.

3) Instead of doing this:

for country in unique_countries:
   matrix.write(n,1, country)
   n = n+1

Do something like

for i, cou in enumerate(unique_countries):
   matrix.write(1+i, 1, cou)

(so you dont need to increment the 'i' variable manually)


On Nov 16, 2007 2:13 PM,  <[EMAIL PROTECTED]> wrote:
> Hi all,
>
> I'm analyzing some data that has a lot of country data.  What I need
> to do is sort through this data and output it into an excel doc with
> summary information.  The countries, though, need to be sorted by
> region, but the way I thought I could do it isn't quite working out.
> So far I can only successfully get the data alphabetically.
>
> Any ideas?
>
> import xlrd
> import pyExcelerator
>
> def get_countries_list(list):
> countries_list=[]
> for country in countries:
> if country not in countries_list:
> countries_list.append(country)
>
> EU = ["Austria","Belgium", "Cyprus","Czech Republic",
> "Denmark","Estonia", "Finland"]
> NA = ["Canada", "United States"]
> AP = ["Australia", "China", "Hong Kong", "India", "Indonesia",
> "Japan"]
> Regions_tot = {'European Union':EU, 'North America':NA, 'Asia
> Pacific':AP,}
>
> path_file = "c:\\1\country_data.xls"
> book = xlrd.open_workbook(path_file)
> Counts = book.sheet_by_index(1)
> countries= Counts.col_values(0,start_rowx=1, end_rowx=None)
>
> get_countries_list(countries)
>
> wb=pyExcelerator.Workbook()
> matrix = wb.add_sheet("matrix")
>
> n=1
> for country in unique_countries:
> matrix.write(n,1, country)
> n = n+1
>
> wb.save('c:\\1\\matrix.xls')
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Resolving declaring class of a method at runtime

2007-11-16 Thread Alan
On Nov 16, 8:51 pm, Steven D'Aprano <[EMAIL PROTECTED]
cybersource.com.au> wrote:
> There should be a term for "optimization" techniques that actually slow
> things down instead of speeding them up.
>

I belive those are the ones known as "premature optimizations", which
sit at the root of all evil

--
Alan

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


Re: Namespace troubles

2007-11-16 Thread Alan

> class MyFrame(wx.Frame):
> def __init__(self, parent, id, title):
> wx.Frame.__init__(self, parent, id, title, size=(600,500))
>
> nb = wx.Notebook(self)
> self.page1 = Form1(nb, -1)
> nb.AddPage(self.page1, "Choose Patient")
> self.page1.SetFocus()
>
> def patient_lookup(self, first_ltrs):
> self.page2 = Selectable.Repository(nb, -1, first_ltrs)
> nb.AddPage(self.page2, "Patient Lookup")
> self.page2.SetFocus()
>


> I'm getting an error from the patient_lookup function:  "global name
> 'nb' is not defined".  I don't understand how a function in the same
> class cannot see the wx.Notebook instance "nb".  I know it exists
> somewhere I just haven't found the name for it.  I've tried
> "frame.nb", "self.nb", f = GetTopLevelParent(self) then f.nb.  How can
> an instance of something just disappear?
>
> Thanks for any help.
>
> Mike

nb is a local variable within the __init__() method only.

This is the same as, e.g.:

def foo():
i = 0

def bar():
print i

The use of i in bar() is an error, because it is only assigned in
foo()


If you want to make it available to all methods within a class then
you need to attach it to the instance of that class, e.g.:

class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600,500))
self.nb = wx.Notebook(self)
self.page1 = Form1(nb, -1)
self.nb.AddPage(self.page1, "Choose Patient")
self.page1.SetFocus()

def patient_lookup(self, first_ltrs):
self.page2 = Selectable.Repository(nb, -1, first_ltrs)
self.nb.AddPage(self.page2, "Patient Lookup")
self.page2.SetFocus()


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


Namespace troubles

2007-11-16 Thread barronmo
I'm new to programming and new to Python; namespace issues are getting
the best of me.  Can someone help me with the following:


import wx
import sys
sys.path.append('~/PyPrograms/EMRGUI')
import Selectable

class MyApp(wx.App):
def __init__(self):
wx.App.__init__(self)
frame = MyFrame(None, -1, 'EMR')#create instance of 
MyFrame
frame.Show(True)#make visible and 
center frame
frame.Centre()


class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600,500))

nb = wx.Notebook(self)  #create instance of 
wx.Notebook
self.page1 = Form1(nb, -1)  #create instance of 
panel Form1 with
Notebook instance as parent
nb.AddPage(self.page1, "Choose Patient")#add the panels as 
Notebook
pages
self.page1.SetFocus()   #give focus to page 1

def patient_lookup(self, first_ltrs):   #passes first letters of
last name and creates new page c results
self.page2 = Selectable.Repository(nb, -1, first_ltrs)  #creates
instance of panel Repository from Selectable mod
nb.AddPage(self.page2, "Patient Lookup")#adds second page with
results
self.page2.SetFocus()   #give focus to new page

class Form1(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id) #inherits from wx.Panel
f = wx.GetTopLevelParent(self)
self.button = wx.Button(self, 10, "Search", wx.Point(200,
325))   #create instance of wx.Button
wx.Button.SetTransparent(self.button, 100)  
#experiment with
SetTransparent method
self.lblname = wx.StaticText(self, -1, "Enter first letters of
last name:",wx.Point(20,60))
self.editname = wx.TextCtrl(self, 20, "", wx.Point(150, 60),
wx.Size(140,-1))
wx.EVT_BUTTON(self, 10, f.patient_lookup(self.editname.Value))
#calls function to get list of patients



app = MyApp()   #create instance of 
MyApp
app.MainLoop()  #run program


I'm getting an error from the patient_lookup function:  "global name
'nb' is not defined".  I don't understand how a function in the same
class cannot see the wx.Notebook instance "nb".  I know it exists
somewhere I just haven't found the name for it.  I've tried
"frame.nb", "self.nb", f = GetTopLevelParent(self) then f.nb.  How can
an instance of something just disappear?

Thanks for any help.

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


Re: overriding methods - two questions

2007-11-16 Thread Paul McGuire
On Nov 16, 11:03 am, Donn Ingle <[EMAIL PROTECTED]> wrote:
> Hi,
> Here's a framework for the questions:
>
> --- In a module, part of an API ---
> class Basis ( object ):
>  def foo ( self, arg ):
>   pass
>
> --- In user's own code ---
> class Child ( Basis ):
>  def foo ( self, not, sure ):
>   ...
>
> Question 1:
>
> Given that the user of the API can choose to override foo() or not, how can
> I control the signature that they use? In the example the user has chosen
> bad arguments and Python will complain, but it's describing the sig of the
> *overridden* method and not the one in the parent class.

Actually, Python is complaining about your user's poor choice of
argument names. 'not' is a reserved keyword.  Change it to 'naught' or
'knot' or 'not_' and Python will accept this just fine.

Whether this is a good idea or not is a separate question.  But given
Python's philosophy of "you are the human, so you must know what you
are doing" (which is both an assumption and a directive), I don't
think you will find much language machinery to prevent it.

-- Paul


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


Re: timing puzzle

2007-11-16 Thread Robin Becker
Chris Mellon wrote:

> 
> remove() does a linear search to find the item to remove, so it's O(n)
> + the actual deletion. Append() is amortized O(1) (overcommit). If you
> delete by index instead:
> for idx, node in active_nodes:
>   if cond:
> del active_nodes[idx]
> 
> 

that's what I was missing; my indexed deletes avoid the linear search. I 
was looking only at the data movements

> You should see performance comparable to your second option. If you
> can trade memory for performance, discarding the old active_nodes
> might be even faster:
> active_nodes = [node for node in active_nodes if node not in
> deleted_nodes], where deleted_nodes is a set.
> 
> 
> Normal micro-optimization techniques apply here too, like looking up
> the remove() method ahead of time and so on.
>.

yes indeed and they'll start to count if I can eliminate the main 
problem areas.
-- 
Robin Becker
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is python?????

2007-11-16 Thread kyosohma
On Nov 16, 1:16 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
> On Nov 16, 8:14 am, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
>
> > * Cope (Fri, 16 Nov 2007 06:09:31 -0800 (PST))
>
> > > please tell me what is python.This group is so crowded.
>
> > A Python is dangerous snake[1]. This group here mainly consists of
> > misguided snake worshippers. You'd better run before they come to your
> > place...
>
> > Thorsten
> > [1]http://en.wikipedia.org/wiki/Pythonidae
>
> Don't listen to him, he was joking.
>
> "Python" is short for "Monty Python's Flying Circus",
> a British television comedy originally from 1969-1971
> (although movies, records, books and Broadway shows
> continue to the present). Read the documention,
> you'll see numerous references to spam (used in one
> of their famous routines) and other Pythonisms.
>
> Anyway, all the posting in this group is comedy.
> If you aren't getting it, run out and buy and study
> all the DVD's. When you've reached the point where
> you have The Argument Clinic dialogue memorized,
> come back here and all this will make sense.
>
> Just be careful, some of the Python routines aren't
> SUPPOSED to make sense ("cabbage crates over the how's
> your father"), that's the joke.

I still don't get it and I've been haunting this group for months...

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


Re: overriding methods - two questions

2007-11-16 Thread [EMAIL PROTECTED]
On Nov 16, 11:35 am, Donn Ingle <[EMAIL PROTECTED]> wrote:
> >This may help (on an old Python version):
>  class Sam: pass
>  class Judy:
> > ...def foo(self): pass
> > ...
>  children = [Sam(), Judy(), Sam()]
>  for child in children: hasattr(child, "foo")
> > ...
> > False
> > True
> > False
>
> That's not what my tests are showing. While Sam has no foo, it's coming from
> (in my OP) Child (which is the parent class), so hasattr(Sam(),"foo") is
> returning True.
>
> /d

But also in your OP: "I want to run the foo() method for each one that
*has* a foo() method "  So hasattr(child, "foo") really does
answer the question as posed, even if it's not really what you want.
I am curious as to why you want to go through such contortions.  What
do you gain.  What happens, for example, if a subclass of Judy is
passed in that does not override foo?  Should foo be called in that
case or not?

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


Re: Namespace troubles

2007-11-16 Thread kyosohma
On Nov 16, 2:51 pm, barronmo <[EMAIL PROTECTED]> wrote:
> I'm new to programming and new to Python; namespace issues are getting
> the best of me.  Can someone help me with the following:
>
> import wx
> import sys
> sys.path.append('~/PyPrograms/EMRGUI')
> import Selectable
>
> class MyApp(wx.App):
> def __init__(self):
> wx.App.__init__(self)
> frame = MyFrame(None, -1, 'EMR')#create instance of 
> MyFrame
> frame.Show(True)#make visible and 
> center frame
> frame.Centre()
>
> class MyFrame(wx.Frame):
> def __init__(self, parent, id, title):
> wx.Frame.__init__(self, parent, id, title, size=(600,500))
>
> nb = wx.Notebook(self)  #create instance of 
> wx.Notebook
> self.page1 = Form1(nb, -1)  #create instance of 
> panel Form1 with
> Notebook instance as parent
> nb.AddPage(self.page1, "Choose Patient")  #add the panels as 
> Notebook
> pages
> self.page1.SetFocus()   #give focus to page 1
>
> def patient_lookup(self, first_ltrs):   #passes first letters 
> of
> last name and creates new page c results
> self.page2 = Selectable.Repository(nb, -1, first_ltrs)  #creates
> instance of panel Repository from Selectable mod
> nb.AddPage(self.page2, "Patient Lookup")  #adds second page with
> results
> self.page2.SetFocus()   #give focus to new 
> page
>
> class Form1(wx.Panel):
> def __init__(self, parent, id):
> wx.Panel.__init__(self, parent, id) #inherits from 
> wx.Panel
> f = wx.GetTopLevelParent(self)
> self.button = wx.Button(self, 10, "Search", wx.Point(200,
> 325))   #create instance of wx.Button
> wx.Button.SetTransparent(self.button, 100)  
> #experiment with
> SetTransparent method
> self.lblname = wx.StaticText(self, -1, "Enter first letters of
> last name:",wx.Point(20,60))
> self.editname = wx.TextCtrl(self, 20, "", wx.Point(150, 60),
> wx.Size(140,-1))
> wx.EVT_BUTTON(self, 10, f.patient_lookup(self.editname.Value))
> #calls function to get list of patients
>
> app = MyApp()   #create instance of 
> MyApp
> app.MainLoop()  #run program
>
> I'm getting an error from the patient_lookup function:  "global name
> 'nb' is not defined".  I don't understand how a function in the same
> class cannot see the wx.Notebook instance "nb".  I know it exists
> somewhere I just haven't found the name for it.  I've tried
> "frame.nb", "self.nb", f = GetTopLevelParent(self) then f.nb.  How can
> an instance of something just disappear?
>
> Thanks for any help.
>
> Mike

If you want to able to access variables or objects from functions (or
methods) within a class, you'll have to put "self" on the front part
of their name. In this case, change nb to self.nb

This makes nb into an attribute of the class that can be accessed by
any method/function within that class.

See the following links for more on classes:

http://docs.python.org/tut/node11.html
http://www.diveintopython.org/object_oriented_framework/defining_classes.html

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


Re: Sorting Countries by Region

2007-11-16 Thread Alan
On Nov 16, 8:28 pm, martyw <[EMAIL PROTECTED]> wrote:
>
> i would create a class to capture country information, e.g.


What is the advantage of this:

>  def __cmp__(self, other):
>  if self.name < other.name:
>  return -1
>  elif self.name > other.name:
>  return 1
>  else:
>  return 0
>

over
def __cmp__(self,other):
return cmp(self.name,other.name)

?

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


Re: PIL question

2007-11-16 Thread Thomas Heller
Thomas Heller schrieb:
> I'm trying to read an image with PIL, crop several subimages out of it,
> and try to combine the subimages again into a combined new one.  This is
> a test script, later I want to only several single images into a combined one.
[...]
> Here is the code; I'm using Python 2.4 and PIL 1.5:

PIL 1.1.5, of course; sorry for the confusion.


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


Re: What is python?????

2007-11-16 Thread Alan
On Nov 16, 8:29 pm, [EMAIL PROTECTED] wrote:
> I still don't get it and I've been haunting this group for months...
>
> Mike

Go on then  ...

What ?

The punchline, do the punchline

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


Re: Resolving declaring class of a method at runtime

2007-11-16 Thread Chris Mellon
On Nov 16, 2007 2:51 PM, Steven D'Aprano
<[EMAIL PROTECTED]> wrote:
> On Fri, 16 Nov 2007 19:02:25 +0200, Janne Härkönen
> wrote:
>
> >> X is an "old style" class. Most people probably shouldn't use old style
> >> classes, for various reasons. To use new style classes, you inherit
> >> from object:
> >
> > I am also aware of old and new style classes, this was the fastest way
> > to type it in console
>
> Ah, an optimization huh?
>
> So, by saving 1/2 a second by not typing "(object)" in your example code,
> the consequences are that I wasted my time explaining that you should use
> new style classes, and you wasted your time reading and responding to my
> explanation, and now I'm wasting my time *again* telling you off for
> wasting my time in the first place.
>
> There should be a term for "optimization" techniques that actually slow
> things down instead of speeding them up.


pessimization, of course.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What is python?????

2007-11-16 Thread Shawn Milochik
On Nov 16, 2007 2:16 PM, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> On Nov 16, 8:14 am, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
> > * Cope (Fri, 16 Nov 2007 06:09:31 -0800 (PST))
> >
> > > please tell me what is python.This group is so crowded.
> >
> > A Python is dangerous snake[1]. This group here mainly consists of
> > misguided snake worshippers. You'd better run before they come to your
> > place...
> >
> > Thorsten
> > [1]http://en.wikipedia.org/wiki/Pythonidae
>
> Don't listen to him, he was joking.
>
> "Python" is short for "Monty Python's Flying Circus",
> a British television comedy originally from 1969-1971
> (although movies, records, books and Broadway shows
> continue to the present). Read the documention,
> you'll see numerous references to spam (used in one
> of their famous routines) and other Pythonisms.
>
> Anyway, all the posting in this group is comedy.
> If you aren't getting it, run out and buy and study
> all the DVD's. When you've reached the point where
> you have The Argument Clinic dialogue memorized,
> come back here and all this will make sense.
>
> Just be careful, some of the Python routines aren't
> SUPPOSED to make sense ("cabbage crates over the how's
> your father"), that's the joke.
> --
> http://mail.python.org/mailman/listinfo/python-list
>



Actually, this answer is the true one, and should be considered
authoritative.

Thanks, mensanator.

P.S.
Another "Python" is a programming language invented by Guido van Rossum,
which he named after Monty Python. You can see a lot about that at
python.org.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: tracking/ordering log files

2007-11-16 Thread nik
OK, that's it,
so to do what I want I am using:
self.logger = logging.getLogger('debuglog')
hdlr = logging.handlers.RotatingFileHandler(debugfilename,
'a', 0, 5)
hdlr.doRollover()
formatter = logging.Formatter('%(asctime)s %
(levelname)s:%(message)s')
hdlr.setFormatter(formatter)
self.logger.addHandler(hdlr)


Thanks for your help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Insert image to a List box

2007-11-16 Thread Matt McCredie
> Thanks a lot for your patience.
> I put the gif file to a folder called fig in my desktop.
> dirpath = './fig'
> still got error:
>
> Traceback (most recent call last):
>   File "11.py", line 238, in 
> img.config(image=gifsdict[imgname])
> NameError: name 'imgname' is not defined
>

You should reply to the python list. That way everyone gets the
benefit of your questions.

The problem is here:
[code]
def list_entry_clicked(*ignore):
   imgname = L.get(L.curselection()[0])
img.config(image=gifsdict[imgname])
L.bind('', list_entry_clicked)
[/code]

I think the main problem is that the line
`img.config(image=gifsdict[imgname])' is supposed to be part of
`list_entry_clicked' and it isn't.

What you want to do (maybe? I made some assumptions):
[code]
def list_entry_clicked(*ignore):
   imgname = L.get(L.curselection()[0])
   img.config(image=gifsdict[imgname])

L.bind('', list_entry_clicked)
[/code]

That _should_ work, but it is untested.

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


Re: Python beginner!

2007-11-16 Thread Shawn Milochik
I completely support Wildemar. Lazy questions like that deserve absolutely
nothing.

I agree that cushioning the reply with a brief explanation of why that
question sucks would have helped the original poster, but he doesn't deserve
any effort from any of us until he has shown evidence of his own efforts.
Then he will find a lot of friendly help.
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Fwd: Sorting Countries by Region

2007-11-16 Thread patrick . waldo
Great, this is very helpful.  I'm new to Python, so hence the
inefficient or nonsensical code!

>
> 2) I would suggest using countries.sort(...) or sorted(countries,...),
> specifying cmp or key options too sort by region instead.
>

I don't understand how to do this.  The countries.sort() lists
alphabetically and I tried to do a lambda x,y: cmp() type function,
but it doesn't sort correctly.  Help with that?

For martyw's example, I don't need to get any sort of population
info.  I'm actually getting the number of various types of documents.
So the entry is like this:

Argentina   Food and Consumer Products  Food Additives  Color
Additives   1
Argentina   Food and Consumer Products  Food Additives  Flavors 
1
Argentina   Food and Consumer Products  Food Additives
General 6
Argentina   Food and Consumer Products  Food Additives  labeling
1
Argentina   Food and Consumer Products  Food Additives  Prohibited
Additives   1
Argentina   Food and Consumer Products  Food ContactCellulose   
1
Argentina   Food and Consumer Products  Food ContactFood
Packaging   1
Argentina   Food and Consumer Products  Food ContactPlastics
4
Argentina   Food and Consumer Products  Food Contact
Waxes   1
Belize
etc...

So I'll need to add up all the entries for Food Additives and Food
contacts, the other info like Color Additives isn't important.

So I will have an output like this
  Food AdditivesFood Contact
Argentina 107
Belize
etc...

Thanks so much for the help!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: overriding methods - two questions

2007-11-16 Thread Steven D'Aprano
On Fri, 16 Nov 2007 18:28:59 +0100, Bruno Desthuilliers wrote:

>> Question 1:
>> 
>> Given that the user of the API can choose to override foo() or not, how
>> can I control the signature that they use?
> 
> While technically possible (using inspect.getargspec), trying to make
> your code idiot-proof is a lost fight and a pure waste of time.


Worse: it's actually counter-productive!

The whole idea of being able to subclass a class means that the user 
should be able to override foo() *including* the signature. Why do you 
want to stop them? It's their subclass, not yours. You don't know what 
arguments it needs.

Let me give a practical example: in mathematics there is a construct 
known as a continued fraction. What it is isn't especially important, if 
you're curious you can google for it. If you were defining a class for 
continued fractions, you might do this:

class ContinuedFraction(object):
def __init__(self, list_of_numerators, list_of_denominators):
pass
# lots of other methods

cf = ContinuedFraction([3, 7, 2, 8, 9, 5], [2, 3, 1, 5, 3, 7])

If I wanted to subclass your ContinuedFraction class to provide regular 
continued fractions, I could do this:

class RegularCF(ContinuedFraction):
def __init__(self, *denominators):
numerators = [1]*len(denominators)
super(RegularCF, self).__init__(numerators, denominators)
# all other methods are inherited from super-class without change

cf = RegularCF(4, 9, 1, 2, 6, 3)


But if you did what you're proposing to do, I couldn't do that. I'd need 
to do something silly like this:

class RegularCF(ContinuedFraction):
def __init__(self, list_of_numerators, list_of_denominators):
numerators = [1]*len(list_of_denominators)
super(RegularCF, self).__init__(numerators, list_of_denominators)

cf = RegularCF(None, [4, 9, 1, 2, 6, 3])


just so that the signatures matched. What a waste of time. 

And worse, what if my subclass needed *more* arguments than your 
signature provided? The hoops I would have to jump through would not only 
be flaming, they'd be spinning and flying through the air, with rotating 
knives and trip-wires.



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


Re: Populating a dictionary, fast [SOLVED SOLVED]

2007-11-16 Thread Steven D'Aprano
On Fri, 16 Nov 2007 11:24:24 +0100, Hrvoje Niksic wrote:

> Steven D'Aprano <[EMAIL PROTECTED]> writes:
> 
>>> Can you post minimal code that exhibits this behavior on Python 2.5.1?
>>> The OP posted a lot of different versions, most of which worked just
>>> fine for most people.
>>
>> Who were testing it on single-CPU, 32 bit systems.
> 
> Still, I'd like to see a test case that fails (works slowly) for you, so
> that I (and others) can try it on different machines.  As I said, the OP
> posted several versions of his code, and tended to depend on a specific
> dataset.  A minimal test case would help more people debug it.


http://groups.google.com.au/group/comp.lang.python/msg/b33ceaf01db10a86


#!/usr/bin/python
"""Read a big file into a dict."""

import gc
import time
print "Starting at %s" % time.asctime()
flag = gc.isenabled()
gc.disable()
id2name = {}
for n, line in enumerate(open('id2name.txt', 'r')):
if n % 100 == 0:
# Give feedback.
print "Line %d" % n
id,name = line.strip().split(':', 1)
id = long(id)
id2name[id] = name
print "Items in dict:", len(id2name)
print "Completed import at %s" % time.asctime()
print "Starting to delete dict..."
del id2name
print "Completed deletion at %s" % time.asctime()
if flag:
gc.enable()
print "Finishing at %s" % time.asctime() 


I've since tried variants where the dict keys were kept as strings, and 
it made no difference to the speed, and where the data was kept as a list 
of (key, value) tuples, which made a HUGE difference.

You should also read this post here:
http://groups.google.com.au/group/comp.lang.python/msg/2535dc213bc45f84


showing Perl running very fast on the same machine that Python was 
running like a one-legged sloth.



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


Re: Embedded Python - Blocking Python Function

2007-11-16 Thread andy
On Nov 15, 5:03 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Thu, 15 Nov 2007 16:18:45 -0300, <[EMAIL PROTECTED]> escribió:
>
> > On Nov 15, 9:43 am, [EMAIL PROTECTED] wrote:
> >> On Nov 14, 4:20 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
> >> wrote:
>
> >> > Not forcibly - you need some cooperation from the Main function. Maybe
> >> > setting a global variable that Main checks periodically.
>
> > It works but the problem is that the script will be written by the end
> > user. If they make a mistake and the cancel flag isn't perodically
> > checked then it seems I have no way of cleanly ending the interpreter.
> > If I wait for a specific time after requesting the Main function stop
> > I need to be able to kill the interpreter without a runtime error. Any
> > ideas?
>
> You could use PyThreadState_SetAsyncExc - it's supposed to raise an
> exception in another thread. There is a Cookbook recipe using it here
> 
> I've never actually used it, but I want to try it some day, so please
> report back your findings if you decide to use this function.
>
> --
> Gabriel Genellina


It seems this is the function I need, however the following gave an
access violation:

PyEval_AcquireLock();
PyThreadState_Swap(thread);

// stop interpreter by sending system exit exception to it's thread
PyThreadState_SetAsyncExc(thread->thread_id, PyExc_SystemExit);

PyThreadState_Swap(maininterpreter);
PyEval_ReleaseLock();

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


Re: PyCheck for a classes defined in python and user data in PyObject_HEAD

2007-11-16 Thread sndive
On Nov 15, 10:00 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Fri, 16 Nov 2007 00:27:42 -0300, <[EMAIL PROTECTED]> escribió:
>
>
>
> > On Nov 1, 11:04 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
> > wrote:
> >> En Thu, 01 Nov 2007 22:13:35 -0300, <[EMAIL PROTECTED]> escribió:
> >> >> I'm in a situation when i don't really need to extend python with any
> >> >> classes of my own but
> >> >> i do have extra luggage for the python data structures such as
> >> tuples,
> >> >> lists, dictionaries, etc
> >> >> on the c++ side. I see no place in PyObject_HEAD where i can stick a
> >> >> void* to my extra data.
>
> >> Assuming you can recompile all the extensions you use, you could insert
> >> your void* into _PyObject_HEAD_EXTRA and _PyObject_EXTRA_INIT
>
> > i added two void pointers to head_extra and 0,0, to the extra_init.
> > For some reason i get garbage in them from PyEval_EvalCode :-(
> > (that's after i blew the build dir and rebuilt 2.4.4 from scratch
> > so i don't think i have a bad build)
>
> Can't you use instead the other approaches suggested on this thread? Like
> keeping a weakkey dictionary holding your additional attributes? Seems a
> lot safer to do that way.
>
> I've never changed that headers myself so I can't guarantee it works.
> Perhaps there are some assumptions somewhere in the code about a specific
> layout. But since _PyObject_HEAD_EXTRA is used in the debug build I think
> you should be able to add your own fields, at least in principle...

i finally found that i need to reset my fields in _Py_NewReference(op)
also.
weakref approach won't work for me because i need extra data in lists/
dictionaries/tuples in addition to instances of classes
(all instances of all classes: can't derive and essentially
make my own bloated psl to maintain)

> Remember that this change invalidates ALL binaries for ANY library or
> extension you may be using. An additional check might be to change the

that's not a problem since i build with a prefix other than /usr/local
and the target is an embedded system so i have full control over
the environment (until someone cracks it :-)

i finally got most of the code working after i figured out that
HEAD_INIT is ignored by tuple allocation code, etc
and _Py_NewReference has to be modified.

thank all of you for all the help. you got me unstuck.
-- 
http://mail.python.org/mailman/listinfo/python-list


Symposium "Image Processing and Analysis" within the ICCES'08 USA - Announce & Call for Papers

2007-11-16 Thread [EMAIL PROTECTED]
--

Symposium "Image Processing and Analysis"
International Conference on Computational and Experimental Engineering
& Sciences 2008 (ICCES'08)
USA, 17-22 March 2008
http://icces.org/cgi-bin/ices08/pages/index

We would appreciate if you could distribute this information by your
colleagues and co-workers.

--

Dear Colleague,

Within the International Conference on Computational and Experimental
Engineering & Sciences 2008 (ICCES'08), to be held in USA, in 17-22
March 2008, we are organizing the Symposium "Image Processing and
Analysis".
Examples of some topics that will be considered in that symposium are:
Image restoring, Image description, Image compression, Image
segmentation, Objects description, Objects tracking, Objects matching,
Objects reconstruction, Objects Registration, Software Development for
Image Processing and Analysis, Grid Computing in Image Processing and
Analysis, Applications of Image Processing and Analysis.
Due to your research activities in those fields, we would like to
invite you to submit your work and participate in the Symposium "Image
Processing and Analysis".

For instructions and submission, please access to the conference
website at: http://icces.org/cgi-bin/ices08/pages/index.
Please note that, when submitting your work you should choose the
Symposium "Image Processing and Analysis".

Important dates:

- 15 Dec 2007: Deadline for abstract submission;
- 20 Dec 2007: End of abstract selection;
- 1 Feb 2008: Deadline for final 6-page paper submission.

Kind regards,

João Manuel R. S. Tavares ([EMAIL PROTECTED])
Renato Natal Jorge ([EMAIL PROTECTED])
(University of Porto, Portugal - Symposium organizers)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Sorting Countries by Region

2007-11-16 Thread Sergio Correia
About the sort:

Check this (also on http://pastebin.com/f12b5b6ca )

def make_regions():

# Values you provided
EU = ["Austria","Belgium", "Cyprus","Czech Republic",
"Denmark","Estonia", "Finland"]
NA = ["Canada", "United States"]
AP = ["Australia", "China", "Hong Kong", "India", "Indonesia",
"Japan"]
regions = {'European Union':EU, 'North America':NA, 'Asia Pacific':AP}

ans = {}
for reg_name, reg in regions.items():
for cou in reg:
ans[cou] = reg_name
return ans

def cmp_region(cou1, cou2):
ans = cmp(regions[cou1], regions[cou2])
if ans: # If the region is the same, sort by country
return cmp(cou1, cou2)
else:
return ans

regions = make_regions()
some_countries = ['Austria', 'Canada', 'China', 'India']

print 'Old:', some_countries
some_countries.sort(cmp_region)
print 'New:', some_countries


Why that code?
Because the first thing I want is a dictionary where the key is the
name of the country and the value is the region. Then, I just make a
quick function that compares considering the region and country.
Finally, I sort.

Btw, the code is just a quick hack, as it can be improved -a lot-.


About the rest of your code:
- martyw's example is much more useful than you think. Why? because
you can just iterate across your document, adding the values you get
to the adequate object property. That is, instead of using size or
pop, use the variables you are interested in.

Best, and good luck with python,
Sergio

On Nov 16, 2007 5:15 PM,  <[EMAIL PROTECTED]> wrote:
> Great, this is very helpful.  I'm new to Python, so hence the
> inefficient or nonsensical code!
>
> >
> > 2) I would suggest using countries.sort(...) or sorted(countries,...),
> > specifying cmp or key options too sort by region instead.
> >
>
> I don't understand how to do this.  The countries.sort() lists
> alphabetically and I tried to do a lambda x,y: cmp() type function,
> but it doesn't sort correctly.  Help with that?
>
> For martyw's example, I don't need to get any sort of population
> info.  I'm actually getting the number of various types of documents.
> So the entry is like this:
>
> Argentina   Food and Consumer Products  Food Additives  Color
> Additives   1
> Argentina   Food and Consumer Products  Food Additives  Flavors   
>   1
> Argentina   Food and Consumer Products  Food Additives
> General 6
> Argentina   Food and Consumer Products  Food Additives  labeling  
>   1
> Argentina   Food and Consumer Products  Food Additives  Prohibited
> Additives   1
> Argentina   Food and Consumer Products  Food ContactCellulose 
>   1
> Argentina   Food and Consumer Products  Food ContactFood
> Packaging   1
> Argentina   Food and Consumer Products  Food ContactPlastics  
>   4
> Argentina   Food and Consumer Products  Food Contact
> Waxes   1
> Belize
> etc...
>
> So I'll need to add up all the entries for Food Additives and Food
> contacts, the other info like Color Additives isn't important.
>
> So I will have an output like this
>   Food AdditivesFood Contact
> Argentina 107
> Belize
> etc...
>
> Thanks so much for the help!
> --
>
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: implement random selection in Python

2007-11-16 Thread Bruza
On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]>
wrote:
> Bruza wrote:
> > I need to implement a "random selection" algorithm which takes a list
> > of [(obj, prob),...] as input. Each of the (obj, prob) represents how
> > likely an object, "obj", should be selected based on its probability
> > of
> > "prob".To simplify the problem, assuming "prob" are integers, and the
> > sum of all "prob" equals 100. For example,
>
> >items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)]
>
> > The algorithm will take a number "N", and a [(obj, prob),...] list as
> > inputs, and randomly pick "N" objects based on the probabilities of
> > the
> > objects in the list.
>
> > For N=1 this is pretty simply; the following code is sufficient to do
> > the job.
>
> > def foo(items):
> > index = random.randint(0, 99)
> > currentP = 0
> > for (obj, p) in items:
> >currentP += w
> >if currentP > index:
> >   return obj
>
> > But how about the general case, for N > 1 and N < len(items)? Is there
> > some clever algorithm using Python standard "random" package to do
> > the trick?
>
> I think you need to clarify what you want to do.  The "probs" are
> clearly not probabilities.  Are they counts of items?  Are you then
> sampling without replacement?  When you say N < len(items) do you mean N
> <= sum of the "probs"?
>
> Duncabn

I think I need to explain on the probability part: the "prob" is a
relative likelihood that the object will be included in the output
list. So, in my example input of

  items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)]

So, for any size of N, 'Tom' (with prob of 45) will be more likely to
be included in the output list of N distinct member than 'Mary' (prob
of 30) and much more likely than that of 'John' (with prob of 10).

I know "prob" is not exactly the "probability" in the context of
returning a multiple member list. But what I want is a way to "favor"
some member in a selection process.

So far, only Boris's solution is closest (but not quite) to what I
need, which returns a list of N distinct object from the input
"items". However, I tried with input of

   items = [('Mary',1), ('John', 1), ('Tom', 1), ('Jane', 97)]

and have a repeated calling of

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


Re: implement random selection in Python

2007-11-16 Thread Bruza
On Nov 16, 4:47 pm, Bruza <[EMAIL PROTECTED]> wrote:
> On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]>
> wrote:
>
>
>
> > Bruza wrote:
> > > I need to implement a "random selection" algorithm which takes a list
> > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how
> > > likely an object, "obj", should be selected based on its probability
> > > of
> > > "prob".To simplify the problem, assuming "prob" are integers, and the
> > > sum of all "prob" equals 100. For example,
>
> > >items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)]
>
> > > The algorithm will take a number "N", and a [(obj, prob),...] list as
> > > inputs, and randomly pick "N" objects based on the probabilities of
> > > the
> > > objects in the list.
>
> > > For N=1 this is pretty simply; the following code is sufficient to do
> > > the job.
>
> > > def foo(items):
> > > index = random.randint(0, 99)
> > > currentP = 0
> > > for (obj, p) in items:
> > >currentP += w
> > >if currentP > index:
> > >   return obj
>
> > > But how about the general case, for N > 1 and N < len(items)? Is there
> > > some clever algorithm using Python standard "random" package to do
> > > the trick?
>
> > I think you need to clarify what you want to do.  The "probs" are
> > clearly not probabilities.  Are they counts of items?  Are you then
> > sampling without replacement?  When you say N < len(items) do you mean N
> > <= sum of the "probs"?
>
> > Duncabn
>
> I think I need to explain on the probability part: the "prob" is a
> relative likelihood that the object will be included in the output
> list. So, in my example input of
>
>   items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)]
>
> So, for any size of N, 'Tom' (with prob of 45) will be more likely to
> be included in the output list of N distinct member than 'Mary' (prob
> of 30) and much more likely than that of 'John' (with prob of 10).
>
> I know "prob" is not exactly the "probability" in the context of
> returning a multiple member list. But what I want is a way to "favor"
> some member in a selection process.
>
> So far, only Boris's solution is closest (but not quite) to what I
> need, which returns a list of N distinct object from the input
> "items". However, I tried with input of
>
>items = [('Mary',1), ('John', 1), ('Tom', 1), ('Jane', 97)]
>
> and have a repeated calling of
>
> Ben

OOPS. I pressed the Send too fast.

The problem w/ Boris's solution is that after repeated calling of
randomPick(3,items), 'Jane' is not the most "frequent appearing"
member in all the out list of 3 member lists...

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


Re: Namespace troubles

2007-11-16 Thread barronmo
Mike and Alan, thanks very much, working fine now.  I'll review the
classes references too.

Mike

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


Getting file timestamp from url

2007-11-16 Thread mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
Is is possible to get the timestamp of a file on a web server if it
has a URL?

For example, let's say that I want to know when the following file was
created:

http://www.w3schools.com/xml/note.xml

I can get an HTTPMessage object using urllib2, like this:

---
#!/usr/bin/python
import urllib2
url = 'http://www.w3schools.com/xml/note.xml'
f = urllib2.urlopen(url)
finfo = f.info()
---

My finfo is an HTTPMessage object, which has getdate() and
getdate_tz() methods.  However, they both require a 'name' argument.
I can't figure out what this is...

Am I going down the wrong path?  Is there a path I can go down to get
what I want?

Thanks,

Mike

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


Re: implement random selection in Python

2007-11-16 Thread Jordan
Maybe it would help to make your problem statement a litte rigorous so
we can get a clearer idea of whats required.

One possible formulation:

Given a list L of pairs of values, weightings: [ (v_0, w_0), (v_1,
w_1), ], and some N between 1 and length(L)

you would like to randomly select a set of N (distinct) values, V,
such that for any ints i and j,

Prob (v_i is in V) / Prob (v_j is in V) = w_i / w_j

This matches your expectations for N = 1. Intuitively though, without
having put much thought into it, I suspect this might not be possible
in the general case.

You might then want to (substantially) relax thec ondition to

Prob (v_i is in V) >= Prob (v_j is in V) iff w_i >= w_j

but in that case its more an ordering of likelihoods rather than a
weighting, and doesn't guarantee the right behaviour for N = 1, so i
don't think thats really what you want.

I can't think of any other obvious way of generalising the behaviour
of the N = 1 case.

- Jordan

On Nov 17, 10:50 am, Bruza <[EMAIL PROTECTED]> wrote:
> On Nov 16, 4:47 pm, Bruza <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On Nov 16, 6:58 am, duncan smith <[EMAIL PROTECTED]>
> > wrote:
>
> > > Bruza wrote:
> > > > I need to implement a "random selection" algorithm which takes a list
> > > > of [(obj, prob),...] as input. Each of the (obj, prob) represents how
> > > > likely an object, "obj", should be selected based on its probability
> > > > of
> > > > "prob".To simplify the problem, assuming "prob" are integers, and the
> > > > sum of all "prob" equals 100. For example,
>
> > > >items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)]
>
> > > > The algorithm will take a number "N", and a [(obj, prob),...] list as
> > > > inputs, and randomly pick "N" objects based on the probabilities of
> > > > the
> > > > objects in the list.
>
> > > > For N=1 this is pretty simply; the following code is sufficient to do
> > > > the job.
>
> > > > def foo(items):
> > > > index = random.randint(0, 99)
> > > > currentP = 0
> > > > for (obj, p) in items:
> > > >currentP += w
> > > >if currentP > index:
> > > >   return obj
>
> > > > But how about the general case, for N > 1 and N < len(items)? Is there
> > > > some clever algorithm using Python standard "random" package to do
> > > > the trick?
>
> > > I think you need to clarify what you want to do.  The "probs" are
> > > clearly not probabilities.  Are they counts of items?  Are you then
> > > sampling without replacement?  When you say N < len(items) do you mean N
> > > <= sum of the "probs"?
>
> > > Duncabn
>
> > I think I need to explain on the probability part: the "prob" is a
> > relative likelihood that the object will be included in the output
> > list. So, in my example input of
>
> >   items = [('Mary',30), ('John', 10), ('Tom', 45), ('Jane', 15)]
>
> > So, for any size of N, 'Tom' (with prob of 45) will be more likely to
> > be included in the output list of N distinct member than 'Mary' (prob
> > of 30) and much more likely than that of 'John' (with prob of 10).
>
> > I know "prob" is not exactly the "probability" in the context of
> > returning a multiple member list. But what I want is a way to "favor"
> > some member in a selection process.
>
> > So far, only Boris's solution is closest (but not quite) to what I
> > need, which returns a list of N distinct object from the input
> > "items". However, I tried with input of
>
> >items = [('Mary',1), ('John', 1), ('Tom', 1), ('Jane', 97)]
>
> > and have a repeated calling of
>
> > Ben
>
> OOPS. I pressed the Send too fast.
>
> The problem w/ Boris's solution is that after repeated calling of
> randomPick(3,items), 'Jane' is not the most "frequent appearing"
> member in all the out list of 3 member lists...- Hide quoted text -
>
> - Show quoted text -

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


  1   2   >