Alphametric fun with Python

2009-01-15 Thread Raymond Hettinger
Thought you guys might enjoy this:

http://code.activestate.com/recipes/576615/

SEND + MORE == MONEY
9567 + 1085 == 10652


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


Re: why cannot assign to function call

2009-01-15 Thread Lie
On Jan 5, 9:03 am, Derek Martin  wrote:
> On Sat, Jan 03, 2009 at 10:15:51AM +, Marc 'BlackJack' Rintsch wrote:
> > On Fri, 02 Jan 2009 04:39:15 -0600, Derek Martin wrote:
>
> > > On Tue, Dec 30, 2008 at 02:21:29PM +, John O'Hagan wrote:
> > > What the Python community often overlooks, when this discussion again
> > > rears its ugly head (as it seems to every other hour or so), is that its
> > > assignment model is BIZARRE, as in it's conceptually different from
> > > virtually all other languages substantially taught in undergraduate
> > > computer science programs.
>
> > What's the difference between Python and Java or C# here!?  Or are they
> > also "BIZARRE"!?
>
> I am happily ignorant of C#.  As for Java, take the following code:
>
>   a = 6;
>   a = 5;
>
> In Python, when you execute the equivalent code, it causes two
> different objects to spring into existence, the first of which may be
> cleaned up by the GC (except that since we're using small integers,
> that's not likely to happen).  Unless I'm misinformed (which is very
> possible, my experience with Java has been extremely limited) in Java
> that's not the case...  the storage is allocated to the name a when
> you execute its declaration, and the *same storage* is reused upon
> subsequent assignment.
>
> That behaves exactly like named bins.
>
> > > And for that matter, it's pretty unintuitive generally.
>
> > Names and objects are quite "natural" IMHO.  There are many real world
> > objects which we attach one or more names to, or refer to in sequences
> > like "please give me the third book on that shelve" (``shelve[2]``).
>
> Indeed, but the way we assign names to them does not behave as it does
> in Python.  Nor does Python's assignment work like it does in algebra,
> or anywhere else the Python student is particularly likely to have
> seen variable assignment before encountering it in Python.  Let's
> define intuitive, shall we?  From dictionary.com (choosing the
> definition which most easily makes my point):
>
>   intuitive: adj.  capable of being perceived or known by intuition.
>
> I'm going to go out on a limb and assert that there's NO POSSIBLE WAY
> a student could intuit Python's variable assignment behavior, having
> never been exposed to that same behavior prior.  It needs to be
> taught.

Which limb would you like to go with?

> > > That is, in what I'll call "normal" computer languages, a variable name
> > > is thought of as the address of a bin where some data is stored, and the
> > > name is inexorably tied to that bin.
>
> > You just call that "normal" or "intuitive" because that's what you
> > learned first.
>
> In a sense, yes... but isn't that what intuition really is?  You can
> figure something out whithout being told how it works...  That's
> either because it's immediately obvious from observing it, or it
> behaves like something you've seen before.  That is what intitive is.

No. You've just said it yourself, something is intuitive if "it
behaves like something you've seen before". People have different
experience, people deeply involved in mathematics would see "named
bin" is the obvious thing to go, however commoners (including
mathematician) will have different say: "Derek is the name of a human
object" instead of "Derek is a specific collection of atoms that
resembles human".

This is also why python community tend to avoid the use of "variable",
because python's name doesn't behave like variables in mathematics, it
behaves like "name". (heck, one would argue that even "variable" in
programming is not exactly the same as "variable" in mathematic
either)

> > I think the "bin model" is more complex because you don't just have a
> > name and an object but always that indirection of the "bin".
>
> I cheerfully disagree. :)  "Named bins" is essentially how algebra
> works, and how several generations of computer languages, not to
> mention the actual machine language those generated, behaved, before
> the current crop.  Those interpretations came first, because, much as
> in the evolution of any other science, that was the model which was
> most intuitive or easily explained.

No, it is neither the most intuitive nor easily explained, but indeed
it is the simplest to implement as humans at that time have limited
capability to design chips. In the early days, ease of use is not a
priority, simplicity is.

> But you need not take my word for it.  Simply read the archives and
> see for yourself how much confusion this has caused on this list.
> [Please include the closely related behavior of parameter passing in
> your search.]

The first time I used python, I didn't have any idea how the object
model works, and I don't have experience in similar programming
concept nor was python my first language, yet I have no difficulty in
understanding why things goes like it is. For me, at least, python's
object model is intuitive (do you remember about the limb?).
--
http://mail.python.org/mailman/listinfo/python-list


Slow Queue.queue? (was: slow network)

2009-01-15 Thread Laszlo Nagy
I had this test program originally that sent message over TCP/IP. 
Messages where buffered two a Queue.Queue instances, one for incoming 
and one for outgoing.


#1. endpoint.send_message(msg) -> endpoint.outgoing.put(msg)
#2. endpoint._process_outgoing() is a thread, that does:  
endpoint.write_into_socket( endpoint.outgoing.get() )


On the other side:

#1 endpoint._process_incoming() is a thread, that does 
endpoint.incoming.put( endpoint.read_from_socket() )

#2 endpoing.recv_messages() -> return endpoint.incoming.get()

This could do 130 message pairs/sec on Linux (on 127.0.0.1) and only 20 
message pairs /sec on Windows XP.


If do not start the _process_outgoing() thread, and do not use the 
outgoing queue:


#1. endpoint.send_message(msg) -> endpoint.write_into_socket(msg)

then the speed goes up to 64 messages/sec on windows and 500 
messages/sec on Linux.


This has nothing to do with TCP/IP. The only change is that I'm not 
using a queue, and not using +1 thread. How can this slow down my 
program? Is it a problem with Queue.Queue, or with the +1 thread?


Thanks,

  Laszlo

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread James Mills
On Thu, Jan 15, 2009 at 5:05 PM, Russ P.  wrote:
(...)

> Wait a minute. Aren't the guy who just took me to task about the
> definition of functional programming? So the definition of functional
> programming is written in stone, but the definition of OO programming
> is written in smoke?

Did anyone say that ? OO concepts are a much
studied concept. However -you- still miss the basic
point. OO programming -is- a model not a paradigm.
Functional Programming -is- a paradigm.

> Just for the record, I really don't care much about the definition of
> OO programming. I brought it up only because someone tried to claim
> that "enforced" encapsulation is a terrible idea. Well, as far as I
> can tell, the majority of OO "programmers" (and software engineers,
> software architects, etc.) seem to think otherwise. Maybe they are
> wrong -- but I seriously doubt it.

Ever thought that perhaps you might be the one
that's wrong ? Not that it really matters, but I am
a Software Engineer myself.

> As I said before, enforced encapsulation may not be appropriate for
> every application, but it is definitely appropriate for some. Not
> every door needs a lock, but certainly some do.

Your analogy is terrible. We are talking about
machines that execute instructions in a sequence.

At the most basic level do you really think a machine
really cares about whether -you- the programmer
has illegally accessed something you shouldn't have ?

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


Re: General direction

2009-01-15 Thread Diez B. Roggisch

MLT schrieb:

Hello all: I'm a beginner to Python (and programming altogether), and
am now looking to create a program of my own.  What I've got in mind
is a very basic pest control program that would keep track of 1) trap
findings and 2) pesticides.  My thought is to make each of these
classes.  Looking at the general outline written below, can anyone
give general impression about whether I'm somewhat going in the right
direction or if I need to approach it entirely differently?  Thanks
for any suggestions or help!


Familiarize yourself with PEP8 for naming and coding-conventions first.



Class Insect_Trapping attributes:
 - Unique Name
 - Location
 - Type


Depending on what the Type is, you might consider using subclassing here 
instead.



 - Frequency of Checks?
 - Next Check Date

Within Insect_Trapping there are Trap_Count objects

Trap_Count objects have the following attributes:
 - Date
 - Time
 - Insect A Finds
 - Insect B Finds
 - Insect C Finds
 - Insect A Number
 - Insect B Number
 - Insect C Number


Instead of manually coding attributes for all kinds of insects, better 
use a mapping of insect-name to attributes. There are plenty of options 
for that - you could a mapping of name to tuple:


{ "cockroach" : (100, 200),
  "wasp" : (2000, 3000)
}

Or introduce a InsectInfo-class that has finds and numbers as fields:

{ "bee" : InsectInfo(finds=100, number=200) }



Class Pesticides attributes:
 - Unique Name
 - Usage Instructions

Within Pesticide there are Pesticide_Usage objects

Pesticide_Usage objects have the following attributes:
 - Date
 - Time
 - Location
 - Reason for Use
 - Amt Used



HTH,

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


Re: Slow Queue.queue? (was: slow network)

2009-01-15 Thread Laszlo Nagy




then the speed goes up to 64 messages/sec on windows and 500 
messages/sec on Linux.
Finally I could reach 1500 messages/sec without using the queue. If I 
comment out one line (use the queue instead of direct write into socket) 
then speed decreases to 40-60 messages/sec. I don't understand why the 
slow version is slower by a factor of 40?


Fast version:

   def send_message(self,sender,recipient,msgtype,body,timeout=3600):
   
self.write_str(self.serializer.serialize([sender,recipient,msgtype,body]))


Slow version:

   def send_message(self,sender,recipient,msgtype,body,timeout=3600):
   self.outgoing.put(self.serializer.serialize([
   sender,recipient,msgtype,body
   ]),1,timeout)

plus this method, executed in a different thread:

   def _process_outgoing(self):
   try:
   while not self.stop_requested.isSet():
   data_ok = False
   while not self.stop_requested.isSet():
   try:
   data = self.outgoing.get(1,1)
   data_ok = True
   break
   except Queue.Empty:
   pass
   if data_ok:
   self.write_str(data)
   except Exception, e:
   if self.router:
   if not isinstance(e,TransportClosedError):
   self.router.logger.error(dumpexc(e))
   self.router.unregister_endpoint(self)
   self.shutdown()
   raise SystemExit(0)

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


Re: process/thread instances and attributes

2009-01-15 Thread Laszlo Nagy



Hey all,

I have this concept I'm working on and here is
the code... Problem is if you run this it doesn't
terminate. I believe you can terminate it in the
main process by calling a.stop() But I can't find a
way for it to self terminate, ie: self.stop() As indicated
by the code...
  
I'm not sure what you wanted to do here, but it is for sure that your 
"running" variable is not shared between the processes. Use this instead:


from multiprocessing import Value
self.running = Value('i',0)

Then these:

self.running.value = 1
self.running.value = 0
if self.running.value:

But I'm affraid your example won't work. Considering your stop() method:

   def stop(self):
   print "%s: Stopping ..." % self
   self.running.value = 0

It would set running.value to zero. However, at that time there will be 
other processes running their start() method and setting the same shared 
value to 1.


What do you want to try with this example?

 L

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


Re: Python 3 isinstance

2009-01-15 Thread Duncan Booth
Terry Reedy  wrote:

> Lambert, David W (S&T) wrote:
>> Overly terse.  I do mean that this is illegal:
>> 
>> isinstance(s, {str, bytes})
>> 
>> tuples have order, immutability, and the possibility of repeat items.
>> 
>> A set is most reasonable in a mathematical sense.
> 
> I agree.  However, isinstance predates set.  Hence the status quo.
> 
> 

There is another reason which nobody has mentioned, and which might make it 
tricky to do a good implementation that does accept sets.

The second argument to isinstance or issubclass is a classinfo where a 
classinfo is either a class or a tuple of classinfo. This makes it easy to 
group related types: you can just combine existing groups of types together 
without having to flatten tuples. The immutability of tuples guarantees 
that you cannot create a recursive structure this way.

If you allow sets then the code which traverses the classinfo has to 
protect itself against the possibility of an infinite recursion.

The same argument also applies to exception handlers which also accept a 
type or a tuple of things they accept. 

Of course you could allow either an existing classinfo or a set of types, 
but not allow the set to contain anything other than types. That would 
still prevent recursion but means you have to be stricter about knowing 
what you are combining. That probably wouldn't be a bad thing if everyone 
used sets consistently but I suspect would be messy if introduced at this 
stage.

-- 
Duncan Booth http://kupuguy.blogspot.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: why o/p is different ???

2009-01-15 Thread asit
On Jan 15, 11:47 am, "James Mills" 
wrote:
> On Thu, Jan 15, 2009 at 4:34 PM, asit  wrote:
> > I recently faced a peculiar o/p.
>
> > My objective is to remove the command name(my script name) from
> > sys.argv[0].
> > I coded like this
>
> If you _really_ want to remove your script_name from
> sys.argv, then do this:
>
> del sys.argv[0]
>
> If you're just after what the name of your script is
> that's being run (for human readability) try the
> following function (taken from pymills):
>
> def getProgName():
>    """getProgName() -> str
>
>    Return the name of the current program being run
>    by working it out from the script's basename.
>    """
>
>    return os.path.basename(sys.argv[0])
>
> --
>
> cheers
> James

Thank you everyone
--
http://mail.python.org/mailman/listinfo/python-list


Possible bug in Tkinter - Python 2.6

2009-01-15 Thread Eric Brunel

Hi all,

I found a behaviour that might be a bug in Tkinter for Python 2.6. Here is  
the script:


-
from Tkinter import *
from tkMessageBox import *
from tkFileDialog import *

root = Tk()

def ask_file():
  file_name = askopenfilename()
  print file_name

def ask_confirm():
  answer = askyesno()
  print answer

Button(root, text='Ask file', command=ask_file).pack()
Button(root, text='Ask confirm', command=ask_confirm).pack()

root.mainloop()
-

Scenario:
- Run the script.
- Click the 'Ask confirm' button and answer 'Yes'; it should print True,  
which is the expected answer.

- Click the 'Ask file' button, select any file and confirm.
- Click the 'Ask confirm' button and answer 'Yes'.

At the last step, the script prints 'False' for me, which is quite wrong.  
Can anybody confirm this?


I reproduced this problem on Linux Fedora Core 4 and Suse Enterprise  
Server 9, and on Solaris 8 for Sparc and Solaris 10 for Intel. However, it  
seems to work correctly on Windows 2000. I could only test with Python  
2.6, and not 2.6.1. But I didn't see any mention of this problem in the  
release notes.


And BTW, if this is actually a bug, where can I report it?

TIA
--
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"

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


Re: process/thread instances and attributes

2009-01-15 Thread James Mills
On Thu, Jan 15, 2009 at 7:36 PM, Laszlo Nagy  wrote:
(...)

Value won't do obviously :) As there could be
an arbitary no. of processes.

> What do you want to try with this example?

I guess I just am confused as to what
belongs to which process (the main or the child) ...

I need a clear cut example or documetnation
of what's what ...

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


Re: Possible bug in Tkinter - Python 2.6

2009-01-15 Thread bieffe62
On 15 Gen, 11:30, "Eric Brunel"  wrote:
> Hi all,
>
> I found a behaviour that might be a bug in Tkinter for Python 2.6. Here is  
> the script:
>
> -
>  from Tkinter import *
>  from tkMessageBox import *
>  from tkFileDialog import *
>
> root = Tk()
>
> def ask_file():
>    file_name = askopenfilename()
>    print file_name
>
> def ask_confirm():
>    answer = askyesno()
>    print answer
>
> Button(root, text='Ask file', command=ask_file).pack()
> Button(root, text='Ask confirm', command=ask_confirm).pack()
>
> root.mainloop()
> -
>
> Scenario:
> - Run the script.
> - Click the 'Ask confirm' button and answer 'Yes'; it should print True,  
> which is the expected answer.
> - Click the 'Ask file' button, select any file and confirm.
> - Click the 'Ask confirm' button and answer 'Yes'.
>
> At the last step, the script prints 'False' for me, which is quite wrong.  
> Can anybody confirm this?
>
> I reproduced this problem on Linux Fedora Core 4 and Suse Enterprise  
> Server 9, and on Solaris 8 for Sparc and Solaris 10 for Intel. However, it  
> seems to work correctly on Windows 2000. I could only test with Python  
> 2.6, and not 2.6.1. But I didn't see any mention of this problem in the  
> release notes.
>
> And BTW, if this is actually a bug, where can I report it?
>
> TIA
> --
> python -c "print ''.join([chr(154 - ord(c)) for c in  
> 'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"

It works here (no bug), python 2.6.1 on Windows XP.
If it is a bag, maybe it is platform-specific ...


Ciao

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


Re: General direction

2009-01-15 Thread James Stroud

MLT wrote:

Hello all: I'm a beginner to Python (and programming altogether), and
am now looking to create a program of my own.  What I've got in mind
is a very basic pest control program that would keep track of 1) trap
findings and 2) pesticides.  My thought is to make each of these
classes.  Looking at the general outline written below, can anyone
give general impression about whether I'm somewhat going in the right
direction or if I need to approach it entirely differently?  Thanks
for any suggestions or help!

[data structures snipped]

Programs generally *do* something. Here you have described data 
structures, which just sit there being full of data. They aren't very 
active and hence are not terribly interesting.


Here are some bullet points for your consideration:

1. What will your program do?
2. Understand databases (http://tinyurl.com/6flhbz) before you start.
3. Consider PySQlite (http://trac.edgewall.org/wiki/PySqlite)
   before you think about a heavyweight database backend (once
   you realize that you will want a database for this project).

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list


List/Set/Dict..

2009-01-15 Thread bruce
Hi...

i have the test dict/list
 a= {"a": 'a1',"b" : "b1"}
 b= [{"a": 'a1',"b" : "b1"}]

i'm trying to figure out how to programtically tell them apart...

ie, which is a dict, and which is a list...

is there a way to accomplish this..

thanks

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


LGPL license for Qt 4.5

2009-01-15 Thread sturlamolden
According to a Norwegian publication, Nokia will release Qt under LGPL
as of version 4.5.

If I had stocks in Riverbank Computing ltd., I would sell them now...

For the rest of us, this is fantastic news.

http://digi.no/php/art.php?id=800922


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


Open Source Software in the Government

2009-01-15 Thread Alvin ONeal
I thought it might be useful to know that if an idea gets enough votes on
Obama's site, he (supposedly) reviews it.

Here are the ideas related to open source software.
http://citizensbriefingbook.change.gov/search/SearchResults?str=open+source&search.x=0&search.y=0&search=Submit

I hope that that is something that you all find helpful, regardless of your
political orientation.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Why this code is working?

2009-01-15 Thread Aaron Brady
On Jan 14, 7:21 am, "Diez B. Roggisch"  wrote:
> r wrote:
> > Listen Hussien,
>
> Granted, with a name of "r", spelling it wrong is hard, and thus you might
> not be trained in the art of spelling names proper.

Or spelling proper names!  Bah ha.
--
http://mail.python.org/mailman/listinfo/python-list


i want to join developer group

2009-01-15 Thread gkcdri
Hello,
  I want to contribute to the open source projects.

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


Previous PyCon Videos, anyone?

2009-01-15 Thread Phoe6
Hello All,

Youtube channel of pycon09 has some videos ( 
http://www.youtube.com/user/pycon08),
but most of them don't have a sufficiently good audio quality.

Is there anyone who has better recording of an earlier PyCon talk? If
yes, can you please point me to the location?

Thanks,
Senthil
--
http://mail.python.org/mailman/listinfo/python-list


Re: Slow Queue.queue? (was: slow network)

2009-01-15 Thread Laszlo Nagy



I would try something like this inside _process_outgoing:

while not self.stop_requested.isSet():
data_ok = False
while not self.stop_requested.isSet():
if not self.outgoing.empty():
  try:
data = self.outgoing.get(True,0.1)
data_ok = True
break
  except Queue.Empty:
pass
else:
 time.sleep(0.1) # maybe, if the thread usess too much 
CPU
  
I tried your modifications. The result is 9 messages/sec. I guess it is 
because time.sleep(0.1) - if that is called after each message, at most 
10 message/sec is possible.

The hypotesis I made for this suggestion are:

- if the queue is found empty, the queue.get could keep the global
interpreter lock until a message arrive
  (blocking also the thread that put the message in the queue)
  
I hope it is not that way. Consider two threads running intensive 
calculations. Both of them are running python code -> both of them must 
hold the GIL. Python runs the process on one processor, and GIL must be 
released on any thread that is not active on the CPU, regardless of what 
it is doing.

- if the queue is found empty, the handling of the exception can slows
down the execution
  
Because of the timeout specified in my outgoing queue get() call, the 
exception can only happen once per second. It won't slow down anything.

Not sure they are good guesses, because at the rate of your messages
the queue should be almost always full,
and most probably the implementation of Queue.get is smarted than
myself :-). ANyway, it is worth a try ...
  
The problem is with sending one message, and getting one answer for it. 
Most of the time, the queue is empty or has only one message inside it.



Also, is you are using fixed-length queues (the ones that make the
sender wait if the queue is full), try to increase
the queue size, or to use an infinite-size queue.
  
Queue size was 1000 in this example, but it never contains more than two 
messages.



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


Re: List/Set/Dict..

2009-01-15 Thread John Machin
On Jan 14, 1:53 pm, "bruce"  wrote:
> Hi...
>
> i have the test dict/list
>  a= {"a": 'a1',"b" : "b1"}
>  b= [{"a": 'a1',"b" : "b1"}]
>
> i'm trying to figure out how to programtically tell them apart...
>
> ie, which is a dict, and which is a list...

>>> a = {"a": 'a1', "b": "b1"}
>>> b = [{"a": 'a1', "b": "b1"}]
>>> isinstance(a, list)
False
>>> isinstance(b, list)
True
>>> isinstance(a, dict)
True
>>> isinstance(b, dict)
False
>>> type(a)

>>> type(b)

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


List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread mario ruggier
Hello,

I would like to evaluate list comprehension expressions, from within
which I'd like to call a function. For a first level it works fine but
for second level it seems to lose the "_[1]" variable it uses
internally to accumulate the results. Some sample code is:

class GetItemEvaluator(object):
def __init__(self):
self.globals = globals() # some dict (never changes)
self.globals["ts"] = self.ts
self.globals["join"] = "".join
self.locals = {} # changes on each evaluation
def __getitem__(self, expr):
return eval(expr, self.globals, self.locals)
def ts(self, ts, name, value):
self.locals[name] = value
#print ts, name, value, "", self.locals, "", ts % self
return ts % self

gie = GetItemEvaluator()
gie.locals["inner"] = ("a","b","c","d")
print """
pre %(join([ts("%s."%(j)+'%(k)s ', 'k', k) for j,k in enumerate
(inner)]))s post
""" % gie
# OK, outputs: pre 0.a 1.b 2.c 3.d  post

gie = GetItemEvaluator()
gie.locals["outer"] = [ ("m","n","o","p"), ("q","r","s","t")]
print """
pre %(join([ts(
  '''inner pre
  %(join([ts("%s.%s."%(i, j)+'%(k)s ', 'k', k) for j,k in enumerate
(inner)]))s
   inner post''',
  "inner", inner) # END CALL outer ts()
  for i,inner in enumerate(outer)])
)s post
""" % gie

The second 2-level comprehension gives:

  File "scratch/eval_test.py", line 8, in __getitem__
return eval(expr, self.globals, self.locals)
  File "", line 4, in 
NameError: name '_[1]' is not defined

If the print was to be enable, the last line printed out is:

0.3.%(k)s  k p  {'outer': [('m', 'n', 'o', 'p'), ('q', 'r', 's',
't')], 'i': 0, 'k': 'p', 'j': 3, '_[1]': ['0.0.m ', '0.1.n ', '0.2.o
'], 'inner': ('m', 'n', 'o', 'p')}  0.3.p

i.e. it has correctly processed the first inner sequence, until the
(last) "p" element. But on exit of the last inner ts() call, it seems
to lose the '_[1]' on self.locals.

Any ideas why?

Note, i'd like that the first parameter to ts() is as independent as
possible from teh context in expression context, a sort of independent
mini-template. Thus, the i,j enumerate counters would normally not be
subbed *within* the comprehension itself, but in a similar way to how
k is evaluated, within the call to ts() -- I added them this way here
to help follow easier what the execution trail is. Anyhow, within that
mini-template, i'd like to embed other expressions for the % operator,
and that may of course also be list comprehensions.

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


Re: Alphametric fun with Python

2009-01-15 Thread bearophileHUGS
Raymond Hettinger:
> Thought you guys might enjoy this:
>    http://code.activestate.com/recipes/576615/

Nice and short, but it's also very slow on my PC (Psyco may help).

This solves them in moments:
http://labix.org/python-constraint

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


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread bearophileHUGS
mario ruggier, that's a hack that you can forget. Your code can't be
read. Don't use list comps for that purpose. Write code that can be
read.

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


Re: Slow Queue.queue? (was: slow network)

2009-01-15 Thread bieffe62
On 15 Gen, 10:22, Laszlo Nagy  wrote:
> > then the speed goes up to 64 messages/sec on windows and 500
> > messages/sec on Linux.
>
> Finally I could reach 1500 messages/sec without using the queue. If I
> comment out one line (use the queue instead of direct write into socket)
> then speed decreases to 40-60 messages/sec. I don't understand why the
> slow version is slower by a factor of 40?
>
> Fast version:
>
>     def send_message(self,sender,recipient,msgtype,body,timeout=3600):
>
> self.write_str(self.serializer.serialize([sender,recipient,msgtype,body]))
>
> Slow version:
>
>     def send_message(self,sender,recipient,msgtype,body,timeout=3600):
>         self.outgoing.put(self.serializer.serialize([
>             sender,recipient,msgtype,body
>         ]),1,timeout)
>
> plus this method, executed in a different thread:
>
>     def _process_outgoing(self):
>         try:
>             while not self.stop_requested.isSet():
>                 data_ok = False
>                 while not self.stop_requested.isSet():
>                     try:
>                         data = self.outgoing.get(1,1)
>                         data_ok = True
>                         break
>                     except Queue.Empty:
>                         pass
>                 if data_ok:
>                     self.write_str(data)
>         except Exception, e:
>             if self.router:
>                 if not isinstance(e,TransportClosedError):
>                     self.router.logger.error(dumpexc(e))
>                 self.router.unregister_endpoint(self)
>             self.shutdown()
>             raise SystemExit(0)


I would try something like this inside _process_outgoing:

while not self.stop_requested.isSet():
data_ok = False
while not self.stop_requested.isSet():
if not self.outgoing.empty():
  try:
data = self.outgoing.get(True,0.1)
data_ok = True
break
  except Queue.Empty:
pass
else:
 time.sleep(0.1) # maybe, if the thread usess
too much CPU

The hypotesis I made for this suggestion are:

- if the queue is found empty, the queue.get could keep the global
interpreter lock until a message arrive
  (blocking also the thread that put the message in the queue)

- if the queue is found empty, the handling of the exception can slows
down the execution

Not sure they are good guesses, because at the rate of your messages
the queue should be almost always full,
and most probably the implementation of Queue.get is smarted than
myself :-). ANyway, it is worth a try ...

Also, is you are using fixed-length queues (the ones that make the
sender wait if the queue is full), try to increase
the queue size, or to use an infinite-size queue.

HTH

Ciao

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


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread mario ruggier
On Jan 15, 12:29 pm, mario ruggier  wrote:
> Any ideas why?
>
> Note, i'd like that the first parameter to ts() is as independent as
> possible from the context in expression context, a sort of independent
> mini-template. Thus, the i,j enumerate counters would normally not be
> subbed *within* the comprehension itself, but in a similar way to how
> k is evaluated, within the call to ts() -- I added them this way here
> to help follow easier what the execution trail is. Anyhow, within that
> mini-template, i'd like to embed other expressions for the % operator,
> and that may of course also be list comprehensions.

OK, here's the same sample code somewhat simplified
and maybe be easier to follow what may be going on:


class GetItemEvaluator(object):
def __init__(self):
self.globals = globals() # some dict (never changes)
self.globals["ts"] = self.ts
self.globals["join"] = " ".join
self.locals = {} # changes on each evaluation
def __getitem__(self, expr):
return eval(expr, self.globals, self.locals)
def ts(self, ts):
print "ts:", ts, "", self.locals
return ts % self

# one level
gie = GetItemEvaluator()
gie.locals["inner"] = ("a","b","c","d")
TS1 =  """
pre %(join([
ts('%(j)s.%(k)s')
for j,k in enumerate(inner)]))s post
"""
OUT1 = TS1 % gie
print "Output 1:", OUT1

# two level
gie = GetItemEvaluator()
gie.locals["outer"] = [ ("m","n","o","p"), ("q","r","s","t")]
TS2 = """
leading %(join([
ts(
'''
pre %(join([
ts('%(i)s.%(j)s.%(k)s')
for j,k in enumerate(inner)]))s post
''' # identical to TS1, except for additional '%(s)s.'
)
for i,inner in enumerate(outer)])
)s trailing
"""
OUT2 = TS2 % gie
print "Output 2:", OUT2


As the gie.locals dict is being automagically
updated from within the list comprehension
expression, I simplified the previous call to ts().
Thus, executing this with the prints enabled as
shown will produce the following output:


$ python2.6 scratch/eval_test_4.py
ts: %(j)s.%(k)s  {'_[1]': [], 'k': 'a', 'j': 0, 'inner': ('a',
'b', 'c', 'd')}
ts: %(j)s.%(k)s  {'_[1]': ['0.a'], 'k': 'b', 'j': 1, 'inner':
('a', 'b', 'c', 'd')}
ts: %(j)s.%(k)s  {'_[1]': ['0.a', '1.b'], 'k': 'c', 'j': 2,
'inner': ('a', 'b', 'c', 'd')}
ts: %(j)s.%(k)s  {'_[1]': ['0.a', '1.b', '2.c'], 'k': 'd', 'j': 3,
'inner': ('a', 'b', 'c', 'd')}
Output 1:
pre 0.a 1.b 2.c 3.d post

ts:
pre %(join([
ts('%(i)s.%(j)s.%(k)s')
for j,k in enumerate(inner)]))s post
 {'_[1]': [], 'i': 0, 'outer': [('m', 'n', 'o', 'p'), ('q', 'r',
's', 't')], 'inner': ('m', 'n', 'o', 'p')}
ts: %(i)s.%(j)s.%(k)s  {'outer': [('m', 'n', 'o', 'p'), ('q', 'r',
's', 't')], 'i': 0, 'k': 'm', 'j': 0, '_[1]': [], 'inner': ('m', 'n',
'o', 'p')}
ts: %(i)s.%(j)s.%(k)s  {'outer': [('m', 'n', 'o', 'p'), ('q', 'r',
's', 't')], 'i': 0, 'k': 'n', 'j': 1, '_[1]': ['0.0.m'], 'inner':
('m', 'n', 'o', 'p')}
ts: %(i)s.%(j)s.%(k)s  {'outer': [('m', 'n', 'o', 'p'), ('q', 'r',
's', 't')], 'i': 0, 'k': 'o', 'j': 2, '_[1]': ['0.0.m', '0.1.n'],
'inner': ('m', 'n', 'o', 'p')}
ts: %(i)s.%(j)s.%(k)s  {'outer': [('m', 'n', 'o', 'p'), ('q', 'r',
's', 't')], 'i': 0, 'k': 'p', 'j': 3, '_[1]': ['0.0.m', '0.1.n',
'0.2.o'], 'inner': ('m', 'n', 'o', 'p')}
Traceback (most recent call last):
  File "scratch/eval_test.py", line 40, in 
OUT2 = TS2 % gie
  File "scratch/eval_test_4.py", line 8, in __getitem__
return eval(expr, self.globals, self.locals)
  File "", line 9, in 
NameError: name '_[1]' is not defined


Anyone can help clarify what may be going on?

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


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread mario ruggier
On Jan 15, 1:48 pm, bearophileh...@lycos.com wrote:
> mario ruggier, that's a hack that you can forget. Your code can't be
> read. Don't use list comps for that purpose. Write code that can be
> read.

Ya, agree with you whole-heartily, but then so are most
optimizations ;-) It is just an idea I am exploring, and that code
would be never be humanly written (that's why it seems more convoluted
than necessary). I hope the simplified boiled down sample gets the
intention out better... i'd still would like to understand why the '_
[1]' variable is disappearing after first inner loop!

> Bye,
> bearophile

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


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread Peter Otten
mario ruggier wrote:

> Hello,
> 
> I would like to evaluate list comprehension expressions, from within
> which I'd like to call a function. For a first level it works fine but
> for second level it seems to lose the "_[1]" variable it uses
> internally to accumulate the results. Some sample code is:
> 
> class GetItemEvaluator(object):
> def __init__(self):
> self.globals = globals() # some dict (never changes)
> self.globals["ts"] = self.ts
> self.globals["join"] = "".join
> self.locals = {} # changes on each evaluation
> def __getitem__(self, expr):
> return eval(expr, self.globals, self.locals)
> def ts(self, ts, name, value):
> self.locals[name] = value
> #print ts, name, value, "", self.locals, "", ts % self
> return ts % self
> 
> gie = GetItemEvaluator()
> gie.locals["inner"] = ("a","b","c","d")
> print """
> pre %(join([ts("%s."%(j)+'%(k)s ', 'k', k) for j,k in enumerate
> (inner)]))s post
> """ % gie
> # OK, outputs: pre 0.a 1.b 2.c 3.d  post
> 
> gie = GetItemEvaluator()
> gie.locals["outer"] = [ ("m","n","o","p"), ("q","r","s","t")]
> print """
> pre %(join([ts(
>   '''inner pre
>   %(join([ts("%s.%s."%(i, j)+'%(k)s ', 'k', k) for j,k in enumerate
> (inner)]))s
>inner post''',
>   "inner", inner) # END CALL outer ts()
>   for i,inner in enumerate(outer)])
> )s post
> """ % gie
> 
> The second 2-level comprehension gives:
> 
>   File "scratch/eval_test.py", line 8, in __getitem__
> return eval(expr, self.globals, self.locals)
>   File "", line 4, in 
> NameError: name '_[1]' is not defined
> 
> If the print was to be enable, the last line printed out is:
> 
> 0.3.%(k)s  k p  {'outer': [('m', 'n', 'o', 'p'), ('q', 'r', 's',
> 't')], 'i': 0, 'k': 'p', 'j': 3, '_[1]': ['0.0.m ', '0.1.n ', '0.2.o
> '], 'inner': ('m', 'n', 'o', 'p')}  0.3.p
> 
> i.e. it has correctly processed the first inner sequence, until the
> (last) "p" element. But on exit of the last inner ts() call, it seems
> to lose the '_[1]' on self.locals.
> 
> Any ideas why?
> 
> Note, i'd like that the first parameter to ts() is as independent as
> possible from teh context in expression context, a sort of independent
> mini-template. Thus, the i,j enumerate counters would normally not be
> subbed *within* the comprehension itself, but in a similar way to how
> k is evaluated, within the call to ts() -- I added them this way here
> to help follow easier what the execution trail is. Anyhow, within that
> mini-template, i'd like to embed other expressions for the % operator,
> and that may of course also be list comprehensions.

I have no idea what you are trying to do. Please reread the Zen of Python ;)

What happens is:

List comprehensions delete the helper variable after completion:

>>> def f(): [i for i in [1]]
...
>>> dis.dis(f)
  1   0 BUILD_LIST   0
  3 DUP_TOP
  4 STORE_FAST   0 (_[1])
  7 LOAD_CONST   1 (1)
 10 BUILD_LIST   1
 13 GET_ITER
>>   14 FOR_ITER13 (to 30)
 17 STORE_FAST   1 (i)
 20 LOAD_FAST0 (_[1])
 23 LOAD_FAST1 (i)
 26 LIST_APPEND
 27 JUMP_ABSOLUTE   14
>>   30 DELETE_FAST  0 (_[1])
 33 POP_TOP
 34 LOAD_CONST   0 (None)
 37 RETURN_VALUE

If you manage to run two nested listcomps in the same namespace you get a
name clash and the inner helper variable overwrites/deletes the outer:

>>> def xeval(x): return eval(x, ns)
...
>>> ns = dict(xeval=xeval)
>>> xeval("[xeval('[k for k in ()]') for i in (1,)]")
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in xeval
  File "", line 1, in 
NameError: name '_[1]' is not defined

Peter

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


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread Peter Otten
Peter Otten wrote:

> mario ruggier wrote:
> 
>> Hello,
>> 
>> I would like to evaluate list comprehension expressions, from within
>> which I'd like to call a function. For a first level it works fine but
>> for second level it seems to lose the "_[1]" variable it uses
>> internally to accumulate the results. Some sample code is:
>> 
>> class GetItemEvaluator(object):
>> def __init__(self):
>> self.globals = globals() # some dict (never changes)
>> self.globals["ts"] = self.ts
>> self.globals["join"] = "".join
>> self.locals = {} # changes on each evaluation
>> def __getitem__(self, expr):
>> return eval(expr, self.globals, self.locals)
>> def ts(self, ts, name, value):
>> self.locals[name] = value
>> #print ts, name, value, "", self.locals, "", ts % self
>> return ts % self
>> 
>> gie = GetItemEvaluator()
>> gie.locals["inner"] = ("a","b","c","d")
>> print """
>> pre %(join([ts("%s."%(j)+'%(k)s ', 'k', k) for j,k in enumerate
>> (inner)]))s post
>> """ % gie
>> # OK, outputs: pre 0.a 1.b 2.c 3.d  post
>> 
>> gie = GetItemEvaluator()
>> gie.locals["outer"] = [ ("m","n","o","p"), ("q","r","s","t")]
>> print """
>> pre %(join([ts(
>>   '''inner pre
>>   %(join([ts("%s.%s."%(i, j)+'%(k)s ', 'k', k) for j,k in enumerate
>> (inner)]))s
>>inner post''',
>>   "inner", inner) # END CALL outer ts()
>>   for i,inner in enumerate(outer)])
>> )s post
>> """ % gie
>> 
>> The second 2-level comprehension gives:
>> 
>>   File "scratch/eval_test.py", line 8, in __getitem__
>> return eval(expr, self.globals, self.locals)
>>   File "", line 4, in 
>> NameError: name '_[1]' is not defined
>> 
>> If the print was to be enable, the last line printed out is:
>> 
>> 0.3.%(k)s  k p  {'outer': [('m', 'n', 'o', 'p'), ('q', 'r', 's',
>> 't')], 'i': 0, 'k': 'p', 'j': 3, '_[1]': ['0.0.m ', '0.1.n ', '0.2.o
>> '], 'inner': ('m', 'n', 'o', 'p')}  0.3.p
>> 
>> i.e. it has correctly processed the first inner sequence, until the
>> (last) "p" element. But on exit of the last inner ts() call, it seems
>> to lose the '_[1]' on self.locals.
>> 
>> Any ideas why?
>> 
>> Note, i'd like that the first parameter to ts() is as independent as
>> possible from teh context in expression context, a sort of independent
>> mini-template. Thus, the i,j enumerate counters would normally not be
>> subbed *within* the comprehension itself, but in a similar way to how
>> k is evaluated, within the call to ts() -- I added them this way here
>> to help follow easier what the execution trail is. Anyhow, within that
>> mini-template, i'd like to embed other expressions for the % operator,
>> and that may of course also be list comprehensions.
> 
> I have no idea what you are trying to do. Please reread the Zen of Python
> ;)
> 
> What happens is:
> 
> List comprehensions delete the helper variable after completion:
> 
 def f(): [i for i in [1]]
> ...
 dis.dis(f)
>   1   0 BUILD_LIST   0
>   3 DUP_TOP
>   4 STORE_FAST   0 (_[1])
>   7 LOAD_CONST   1 (1)
>  10 BUILD_LIST   1
>  13 GET_ITER
> >>   14 FOR_ITER13 (to 30)
>  17 STORE_FAST   1 (i)
>  20 LOAD_FAST0 (_[1])
>  23 LOAD_FAST1 (i)
>  26 LIST_APPEND
>  27 JUMP_ABSOLUTE   14
> >>   30 DELETE_FAST  0 (_[1])
>  33 POP_TOP
>  34 LOAD_CONST   0 (None)
>  37 RETURN_VALUE
> 
> If you manage to run two nested listcomps in the same namespace you get a
> name clash and the inner helper variable overwrites/deletes the outer:
> 
 def xeval(x): return eval(x, ns)
> ...
 ns = dict(xeval=xeval)
 xeval("[xeval('[k for k in ()]') for i in (1,)]")
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "", line 1, in xeval
>   File "", line 1, in 
> NameError: name '_[1]' is not defined
> 
> Peter
> 

I'd like to add: this can only happen because the code snippets are compiled
independently. Otherwise Python uses different names for each listcomp:

>>> def f():
... [i for i in ()]
... [i for i in ()]
...
>>> f.func_code.co_varnames
('_[1]', 'i', '_[2]')

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


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread mario ruggier
On Jan 15, 2:02 pm, Peter Otten <__pete...@web.de> wrote:
> mario ruggier wrote:
> > Hello,
>
> > I would like to evaluate list comprehension expressions, from within
> > which I'd like to call a function. For a first level it works fine but
> > for second level it seems to lose the "_[1]" variable it uses
> > internally to accumulate the results. Some sample code is:
>
> > class GetItemEvaluator(object):
> >     def __init__(self):
> >         self.globals = globals() # some dict (never changes)
> >         self.globals["ts"] = self.ts
> >         self.globals["join"] = "".join
> >         self.locals = {} # changes on each evaluation
> >     def __getitem__(self, expr):
> >         return eval(expr, self.globals, self.locals)
> >     def ts(self, ts, name, value):
> >         self.locals[name] = value
> >         #print ts, name, value, "", self.locals, "", ts % self
> >         return ts % self
>
> > gie = GetItemEvaluator()
> > gie.locals["inner"] = ("a","b","c","d")
> > print """
> > pre %(join([ts("%s."%(j)+'%(k)s ', 'k', k) for j,k in enumerate
> > (inner)]))s post
> > """ % gie
> > # OK, outputs: pre 0.a 1.b 2.c 3.d  post
>
> > gie = GetItemEvaluator()
> > gie.locals["outer"] = [ ("m","n","o","p"), ("q","r","s","t")]
> > print """
> > pre %(join([ts(
> >   '''inner pre
> >   %(join([ts("%s.%s."%(i, j)+'%(k)s ', 'k', k) for j,k in enumerate
> > (inner)]))s
> >    inner post''',
> >   "inner", inner) # END CALL outer ts()
> >   for i,inner in enumerate(outer)])
> > )s post
> > """ % gie
>
> > The second 2-level comprehension gives:
>
> >   File "scratch/eval_test.py", line 8, in __getitem__
> >     return eval(expr, self.globals, self.locals)
> >   File "", line 4, in 
> > NameError: name '_[1]' is not defined
>
> > If the print was to be enable, the last line printed out is:
>
> > 0.3.%(k)s  k p  {'outer': [('m', 'n', 'o', 'p'), ('q', 'r', 's',
> > 't')], 'i': 0, 'k': 'p', 'j': 3, '_[1]': ['0.0.m ', '0.1.n ', '0.2.o
> > '], 'inner': ('m', 'n', 'o', 'p')}  0.3.p
>
> > i.e. it has correctly processed the first inner sequence, until the
> > (last) "p" element. But on exit of the last inner ts() call, it seems
> > to lose the '_[1]' on self.locals.
>
> > Any ideas why?
>
> > Note, i'd like that the first parameter to ts() is as independent as
> > possible from teh context in expression context, a sort of independent
> > mini-template. Thus, the i,j enumerate counters would normally not be
> > subbed *within* the comprehension itself, but in a similar way to how
> > k is evaluated, within the call to ts() -- I added them this way here
> > to help follow easier what the execution trail is. Anyhow, within that
> > mini-template, i'd like to embed other expressions for the % operator,
> > and that may of course also be list comprehensions.
>
> I have no idea what you are trying to do. Please reread the Zen of Python ;)
>
> What happens is:
>
> List comprehensions delete the helper variable after completion:
>
> >>> def f(): [i for i in [1]]
> ...
> >>> dis.dis(f)
>
>   1           0 BUILD_LIST               0
>               3 DUP_TOP
>               4 STORE_FAST               0 (_[1])
>               7 LOAD_CONST               1 (1)
>              10 BUILD_LIST               1
>              13 GET_ITER
>         >>   14 FOR_ITER                13 (to 30)
>              17 STORE_FAST               1 (i)
>              20 LOAD_FAST                0 (_[1])
>              23 LOAD_FAST                1 (i)
>              26 LIST_APPEND
>              27 JUMP_ABSOLUTE           14
>         >>   30 DELETE_FAST              0 (_[1])
>              33 POP_TOP
>              34 LOAD_CONST               0 (None)
>              37 RETURN_VALUE
>
> If you manage to run two nested listcomps in the same namespace you get a
> name clash and the inner helper variable overwrites/deletes the outer:
>
> >>> def xeval(x): return eval(x, ns)
> ...
> >>> ns = dict(xeval=xeval)
> >>> xeval("[xeval('[k for k in ()]') for i in (1,)]")
>
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "", line 1, in xeval
>   File "", line 1, in 
> NameError: name '_[1]' is not defined
>
> Peter

Ah, brilliant, thanks for the clarification!

To verify if I understood you correctly, I have modified
the ts() method above to:

def ts(self, ts):
_ns = self.locals
self.locals = self.locals.copy()
print "ts:", ts, "", self.locals
try:
return ts % self
finally:
self.locals = _ns

And, it executes correctly, thus the 2nd output is:

Output 2:
leading
pre 0.0.m 0.1.n 0.2.o 0.3.p post

pre 1.0.q 1.1.r 1.2.s 1.3.t post
 trailing

But, the need to do a copy() will likely kill any potential
optimization gains... so, I will only be forced to rite more readable
code ;-)

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


Re: Python 3 isinstance

2009-01-15 Thread MRAB

Lambert, David W (S&T) wrote:

Overly terse.  I do mean that this is illegal:

isinstance(s, {str, bytes})

tuples have order, immutability, and the possibility of repeat items.

A set is most reasonable in a mathematical sense.


You could say the same about s.startswith(("a", "b")).

It might be because sets are a more recent addition to the language; if 
they had been in Python from the start then maybe they would've been 
used instead.

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Luis Zarrabeitia

Quoting Paul Rubin <"http://phr.cx"@NOSPAM.invalid>:

> Luis Zarrabeitia  writes:
> > Wait, do you _really_ believe that _static_ checks could prevent problems
> > arising from _unexpected_ conditions in the _data_?
> 
> The data does not arrive from outer space on a magtape stapled to a
> meteor.  It comes out of another program.  Most of the problems in
> processing it come from mismatches between the processing programs and
> the generation programs.  Static checks would help eliminate those
> mismatches.

No, copy and paste from the original data structures would eliminate those
mismatches. A compiler checking the reimplementation of said data structres,
whatever the language, has no way of knowing if the structure matches.

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Steven D'Aprano
On Wed, 14 Jan 2009 23:10:07 -0800, Paul Rubin wrote:

> I think this sub-thread has been mostly about dynamically creating new
> class instance attributes, but yes, at one point I did suggest adding
> type checking (ML-like inference) to pylint, presumably with feedback to
> the compiler for optimization purposes.  I noted that Python 3.0 in fact
> has some features to support annotations for the purpose of static type
> checking, so it's not as far off the wall as it might sound.

I fear anything that will lead Python moving towards C/Pascal/Java type 
declarations, but I feel great enthusiasm at the thought that maybe by 
the time we get to Python 4.0 there will be type inference which could 
enable compiler optimizations.

Exciting times people, exciting times.


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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Steven D'Aprano
On Wed, 14 Jan 2009 23:05:32 -0800, Russ P. wrote:

> On Jan 14, 10:40 pm, "James Mills"  wrote:
>> On Thu, Jan 15, 2009 at 4:34 PM, Michele
>> Simionato wrote:
>>
>> (...)
>>
>> > There are lots of Python developers (and most of the core developers)
>> > that think the OO community is wrong about enforced encapsulation.
>> > Personally, I think in a few years everybody will realize the mistake
>> > of enforced encapsulation and that the OO definition in the Wikipedia
>> > page will be changed. Even if not, Wikipedia definitions does not
>> > matter much, everybody has his own idea of what OO means, and the
>> > Python definition is good as any other. Don't get pissed off on
>> > words.
>>
>> Amen! The first thing said right in this entire thread! (one of)
>>
>> --JamesMills
> 
> Wait a minute. Aren't the guy who just took me to task about the
> definition of functional programming? So the definition of functional
> programming is written in stone, but the definition of OO programming is
> written in smoke?

Be fair -- James just admitted that everything he's written in this 
thread is wrong. If Michele's post was, and I quote James, "the first 
thing said right in this entire thread", then obviously everything James 
wrote previously was wrong.

*wink*


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


Re: multiprocessing BaseManager doesn't clean up net connections?

2009-01-15 Thread gaius.julius
Hi,

Having exactly the same problem. It appears even if I explicitly call
manager's shutdown() method.

Not sure it is a bug, though.

But also didn't find anything in documentation or source code or debug
output.

On Jan 1, 8:01 am, Chris Brooks  wrote:
> Hi,
>
> I'm trying to use remote managers in the multiprocessing module to listen
> for some events synchronously while my program goes off and does other
> things.  I use the .start() method which forks a new process to handle
> communication.  When I catch the sigint and call sys.exit() though, the
> network port is still bound even after the python interpretor finishes.
> Here is my short code and the output:
>

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Roy Smith
In article <7xwscx6zfo@ruckus.brouhaha.com>,
 Paul Rubin  wrote:

> basically I think C is evil.

C is not evil.  It's a tool.  Would you call a hammer evil because it's not 
very good at driving screws?  C is a very good tool for doing the kind of 
thing it was designed for, which is highly efficient, low-level, portable 
programming.

The fact that C has been used to write all sorts of large-scale 
applications doesn't mean that it's good at that kind of stuff.  It just 
means that all the alternatives suck more than it does for that kind of 
stuff.

If you want evil, look at C++.
--
http://mail.python.org/mailman/listinfo/python-list


Re: General direction

2009-01-15 Thread Steven D'Aprano
On Thu, 15 Jan 2009 10:08:37 +0100, Diez B. Roggisch wrote:

> MLT schrieb:
>> Hello all: I'm a beginner to Python (and programming altogether), and
>> am now looking to create a program of my own.  What I've got in mind is
>> a very basic pest control program that would keep track of 1) trap
>> findings and 2) pesticides.  My thought is to make each of these
>> classes.  Looking at the general outline written below, can anyone give
>> general impression about whether I'm somewhat going in the right
>> direction or if I need to approach it entirely differently?  Thanks for
>> any suggestions or help!
> 
> Familiarize yourself with PEP8 for naming and coding-conventions first.

Or not.

Enough of the PEP8-fascism please. It is not compulsory to follow PEP8 in 
order to be allowed to learn Python. If Guido wanted it compulsory, he'd 
have made the compiler enforce it.

Besides, you're talking to a newbie. Chances are he doesn't even know 
what PEP8 is: the recommended style-guide for Python code.

http://www.python.org/dev/peps/pep-0008/

Having said that, I will *suggest* to the Original Poster that you should 
follow PEP8 as much as possible. It will save you a lot of grief in the 
long run. But right now, it is more important to learn the language than 
to learn conventions for how to name classes. If you can do both, great, 
but don't let the naming conventions get in the way.



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


Py3 - converting bytes to ascii

2009-01-15 Thread Anjanesh Lekshminarayanan
Using Python 3.0

res = urllib.request.urlopen(url)
f = open('file.txt', 'wb') # Since res.read() returns bytes
f.write(res.read())

But newline and return feeds are stored as b14, 58a as text in the text file.

So how do I to convert res.read() to ascii on opening the file in
ascii mode f = open('file.txt', 'w')?

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


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread Steven D'Aprano
On Thu, 15 Jan 2009 03:29:59 -0800, mario ruggier wrote:

> Hello,
> 
> I would like to evaluate list comprehension expressions, from within
> which I'd like to call a function. For a first level it works fine but
> for second level it seems to lose the "_[1]" variable it uses internally
> to accumulate the results. Some sample code is:
> 
> class GetItemEvaluator(object):
> def __init__(self):
> self.globals = globals() # some dict (never changes)

Would you like to put a small wager on that?

>>> len(gie.globals)
64
>>> something_new = 0
>>> len(gie.globals)
65



> self.globals["ts"] = self.ts
> self.globals["join"] = "".join
> self.locals = {} # changes on each evaluation
> def __getitem__(self, expr):
> return eval(expr, self.globals, self.locals)

Can you say "Great Big Security Hole"?

>>> gie = GetItemEvaluator()
>>> gie['__import__("os").system("ls")']
dicttest  dumb.pyrank.pysorting
startup.py
0


http://cwe.mitre.org/data/definitions/95.html



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


Readline and Python 2.6.1 on a Mac

2009-01-15 Thread James Brady
Hi, I just upgraded Python from 2.5.2 up to 2.6.1 on my 10.5 Mac
(installed from the DMG here: http://www.python.org/download/releases/2.6.1/),
and I'm having some problems with readline and rl_completer.

I have PYTHONSTARTUP pointing to this file:
#!/usr/bin/env python

try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
readline.parse_and_bind("bind ^I rl_complete")

Which was working fine for tab completion in 2.5. Now, however, I
can't enter 'b' (lower case b) in interactive python shells - no
character appears.

Just b is broken - 'B' is fine, Ctrl-b is fine, Alt-b even prints an
integration symbol, but 'b' - nothing!

Has anyone seen this before?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Possible bug in Tkinter - Python 2.6

2009-01-15 Thread r
First of all be very careful using from "module" import * or you will
have name conflicts. Tkinter is made to be imported this way and i do
it all the time. for the others do.

import tkMessageBox as MB
import tkFileDialog as FD
or whatever floats your boat.

Secondly i hear all the time about problems with fedora distros, so
that would explain the issue to me.
--
http://mail.python.org/mailman/listinfo/python-list


Wx broken?

2009-01-15 Thread tmallen
I've tried a few of the examples from http://wiki.wxpython.org/Getting%20Started
, but after "Hello World" (window) nothing works. I get the following
error message with the other examples:

Traceback (most recent call last):
  File "C:/Documents and Settings/MyUserName/Desktop/WxExample.py",
line 1, in 
import wx
  File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx
\__init__.py", line 45, in 
from wx._core import *
  File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
line 6, in 
new_instancemethod = new.instancemethod
AttributeError: 'module' object has no attribute 'instancemethod'

Here's a snippet that generates this error:
http://pastebin.com/m2b4f4f9c

For those familiar with wx here, is this a bug in the library, or
should I be providing "instancemethod" at some point? Seeing that it's
an unhandled exception, I do expect the former.

Thanks,
Thomas
--
http://mail.python.org/mailman/listinfo/python-list


Re: Wx broken?

2009-01-15 Thread tmallen
On Jan 15, 10:46 am, tmallen  wrote:
> I've tried a few of the examples 
> fromhttp://wiki.wxpython.org/Getting%20Started
> , but after "Hello World" (window) nothing works. I get the following
> error message with the other examples:
>
> Traceback (most recent call last):
>   File "C:/Documents and Settings/MyUserName/Desktop/WxExample.py",
> line 1, in 
>     import wx
>   File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx
> \__init__.py", line 45, in 
>     from wx._core import *
>   File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
> line 6, in 
>     new_instancemethod = new.instancemethod
> AttributeError: 'module' object has no attribute 'instancemethod'
>
> Here's a snippet that generates this error:http://pastebin.com/m2b4f4f9c
>
> For those familiar with wx here, is this a bug in the library, or
> should I be providing "instancemethod" at some point? Seeing that it's
> an unhandled exception, I do expect the former.
>
> Thanks,
> Thomas

Looking a little closer, this is what's causing the bug:

Wx: _core.py, lines 1-6:

# This file was created automatically by SWIG 1.3.29.
# Don't modify this file, modify the SWIG interface instead.

import _core_
import new
new_instancemethod = new.instancemethod

On this Win32 box, the "new" module doesn't have the "instancemethod"
attribute. What is new.instancemethod used for? "new_instancemethod,"
which should be assigned to the attr, does not show up again in this
particular source file.

Thanks,
Thomas
--
http://mail.python.org/mailman/listinfo/python-list


Re: Py3 - converting bytes to ascii

2009-01-15 Thread Casey
On Jan 15, 9:54 am, "Anjanesh Lekshminarayanan" 
wrote:
> Using Python 3.0
>
> So how do I to convert res.read() to ascii on opening the file in
> ascii mode f = open('file.txt', 'w')?
>

I think this is what you are looking for:

res = urllib.request.urlopen(url)
f = open('file.txt', 'w')
f.write(res.read().decode('ascii'))
--
http://mail.python.org/mailman/listinfo/python-list


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread mario ruggier
On Jan 15, 4:06 pm, Steven D'Aprano  wrote:

Hi Steve!

> > class GetItemEvaluator(object):
> >     def __init__(self):
> >         self.globals = globals() # some dict (never changes)

Ya, this is just a boiled down sample, and for simplicity I set to to
the real globals(), so of course it will change when that changes...
but in the application this is a distinct dict, that is entirely
managed by the application, and it never changes as a result of an
*evaluation*.

> Would you like to put a small wager on that?
>
> >>> len(gie.globals)
> 64
> >>> something_new = 0
> >>> len(gie.globals)
>
> 65


> >         self.globals["ts"] = self.ts
> >         self.globals["join"] = "".join
> >         self.locals = {} # changes on each evaluation
> >     def __getitem__(self, expr):
> >         return eval(expr, self.globals, self.locals)
>
> Can you say "Great Big Security Hole"?

With about the same difficulty as "Rabbit-Proof Fence" ;-)
Again, it is just a boiled down sample, for communication purposes. As
I mentioned in another thread, the real application behind all this is
one of the *few* secure templating systems around. Some info on its
security is at: http://evoque.gizmojo.org/usage/restricted/
Tell you what, if you find a security hole there (via exposed template
source on a Domain(restricted=True) setup) I'll offer you a nice
dinner (including the beer!) somewhere, maybe at some py conference,
but even remotely if that is not feasible... ;-) The upcoming 0.4
release will run on 2.4 thru to 3.0 -- you can have some fun with that
one (the current 0.3 runs on 2.5 and 2.6).

> --
> Steven

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


Re: Wx broken?

2009-01-15 Thread Mark Smith
>   File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
> line 6, in 
>     new_instancemethod = new.instancemethod
> AttributeError: 'module' object has no attribute 'instancemethod'

I'm guessing you either have a python file called 'new.py' in your
working directory, or you have one in your pythonpath.

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


Re: Wx broken?

2009-01-15 Thread tmallen
On Jan 15, 10:56 am, Mark Smith  wrote:
> >   File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py",
> > line 6, in 
> >     new_instancemethod = new.instancemethod
> > AttributeError: 'module' object has no attribute 'instancemethod'
>
> I'm guessing you either have a python file called 'new.py' in your
> working directory, or you have one in your pythonpath.
>
> --Mark

That's correct. Thanks!

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


Embedding Python and program path

2009-01-15 Thread arve.knud...@gmail.com
In my application embedding Python I want to have the standard Python
module path, so I try to achieve this by setting the program path to
that of the the interpreter. Even though I call Py_SetProgramName
before Py_InitializeEx, however, Python thinks the program path (as
returned by Py_GetProgramFullPath) is that of my embedding executable.

I would love if someone could explain what is going wrong here, and
eventually provide a better solution for inheriting the Python
interpreter's standard module path.

Test application demonstrating the problem.

#include 

int main() {
// Hardcoded for simplicity
Py_SetProgramName("C:\\Python26\\python.exe");
Py_InitializeEx(0);
printf("Program path: %s\n", Py_GetProgramFullPath());
Py_Finalize();

return 0;
}


Thanks,
Arve Knudsen
--
http://mail.python.org/mailman/listinfo/python-list


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread mario ruggier
The listcomps exploration above was primarily an attempt
(unsuccessful) to make this piece of code go a little faster:

s = " text %(item)s text "
acc = []
for value in iterator:
some_dict["item"] = value
acc.append(s % evaluator)
"".join(acc)

The item=value pair is essentially a loop variable, and the evaluator
(something like the gie instance above) uses it via the updated
some_dict.

Is there any way to express the above as a list comp or so? Any ideas
how it might be made to go faster?

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


the name of a method

2009-01-15 Thread thomas . steffen75
Hello,

I have a Class:

class myClass:
def __init__(self):
# do something
print "name of class = " +  self.__class__.__name__

def myMethod(self):
# do something
print "name of method = " + "myMethod"
return

...

I print the name of the class with self.__class__.__name__ in
__init__.
I want to print also in every method of myClass the name of the
method.
How can I get the name? I would not like to write e.g. "myMethod". Is
there a variable like self.__class__.__name__ for this?
Thanks for your hints, Thomas
--
http://mail.python.org/mailman/listinfo/python-list


Re: the name of a method

2009-01-15 Thread Diez B. Roggisch
thomas.steffe...@googlemail.com wrote:

> Hello,
> 
> I have a Class:
> 
> class myClass:
> def __init__(self):
> # do something
> print "name of class = " +  self.__class__.__name__
> 
> def myMethod(self):
> # do something
> print "name of method = " + "myMethod"
> return
> 
> ...
> 
> I print the name of the class with self.__class__.__name__ in
> __init__.
> I want to print also in every method of myClass the name of the
> method.
> How can I get the name? I would not like to write e.g. "myMethod". Is
> there a variable like self.__class__.__name__ for this?
> Thanks for your hints, Thomas

This can be done by inspecting the stackframes. Look into the module
inspect. This has also been discussed very often on this list, stackframe &
inspect should be good searchterms.

However, if what you are after is logging, you should take a look into the
logging module. it has many advantages over simple print-statements, and
amongst other things allows you to print out the enclosing callable name
when invoked ala

logger.debug("Some message.")

I strongly recommend using that. And at least you can of course peek into
the logging module's source to see how the extract that information.

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


Re: Python 3 isinstance

2009-01-15 Thread
I have use case, needn't be dynamic, and it's not hard to enclose set as
tuple.  But I also write (for example)

__all__ = 'this that other'.split()
string_list = 'evenOneWord'.split()

instead of

__all__ = 'this','that','imTiredOfMistypingCommasAndQuotes'


if not isinstance(o,set_of_handled_types):
raise TypeError('call blessed programmer, this cannot occur')
if isinstance(o,set_of_handle_types-treat_differently):
treat_thusly(o)
elif isinstance(o,treat_differently):
treat_differently(o)
elif etc.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Executing global code

2009-01-15 Thread Peter Otten
Jakub Debski wrote:

> Is it possible to execute global code (module-level code) more than
> once keeping the state of global variables? This means no reload() and
> no moving the code to a function.

You have a module containing e. g. these two statements

x = 42
x += 1

and want to rerun it with the effect of x becoming 44? That is not possible
because in Python 

x = 42

is a statement, too, and will thus be rerun.

Peter

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


Re: the name of a method

2009-01-15 Thread Jason Scheirer
On Jan 15, 8:37 am, "Diez B. Roggisch"  wrote:
> thomas.steffe...@googlemail.com wrote:
> > Hello,
>
> > I have a Class:
>
> > class myClass:
> >     def __init__(self):
> >         # do something
> >         print "name of class = " +  self.__class__.__name__
>
> >     def myMethod(self):
> >         # do something
> >         print "name of method = " + "myMethod"
> >         return
>
> >     ...
>
> > I print the name of the class with self.__class__.__name__ in
> > __init__.
> > I want to print also in every method of myClass the name of the
> > method.
> > How can I get the name? I would not like to write e.g. "myMethod". Is
> > there a variable like self.__class__.__name__ for this?
> > Thanks for your hints, Thomas
>
> This can be done by inspecting the stackframes. Look into the module
> inspect. This has also been discussed very often on this list, stackframe &
> inspect should be good searchterms.
>
> However, if what you are after is logging, you should take a look into the
> logging module. it has many advantages over simple print-statements, and
> amongst other things allows you to print out the enclosing callable name
> when invoked ala
>
> logger.debug("Some message.")
>
> I strongly recommend using that. And at least you can of course peek into
> the logging module's source to see how the extract that information.
>
> Diez

I agree, this should not be done. However, sometimes it's useful to
see the parameter values:

import inspect
import logging
import sys
def log_fn():
logging.debug("%s%s" % (
sys._getframe().f_back.f_code.co_name,
inspect.formatargvalues(*inspect.getargvalues(sys._getframe
().f_back

logging.getLogger().setLevel(0)
def hello_there(x, y, z, *a):
 log_fn()
 return 1+x

>>> hello_there(1, 'a', 'b', 5, 6, 7, 8, 9)
DEBUG:root:hello_there(x=1, y='a', z='b', *a=(5, 6, 7, 8, 9))

It can be done, but usually you want to actually trace through with
the debugger.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Paul Rubin
Luis Zarrabeitia  writes:
> No, copy and paste from the original data structures would eliminate those
> mismatches.

The whole point is that would be possible if Python had data structure
definitions ("types") that were possible to copy and paste from some
single location, instead of building up structures dynamically, adding
fields on the fly in ways that have become obscure over the evolution
of the code.
--
http://mail.python.org/mailman/listinfo/python-list


IFL 2009: Call for Papers

2009-01-15 Thread IFL 2009
Call for Papers 
IFL 2009
Seton Hall University
SOUTH ORANGE, NJ, USA
http://tltc.shu.edu/blogs/projects/IFL2009/


The 21st IFL symposium, IFL 2009, will be held for the first time in the 
USA. The hosting institution is Seton Hall 
University in South Orange, NJ, USA and the symposium dates are September 
23-25, 2009. It is our goal to make IFL a
regular event held in the USA. The goal of the IFL symposia is to bring 
together researchers actively engaged in the 
implementation and application of functional and function-based 
programming languages. IFL 2009 will be a venue for 
researchers to present and discuss new ideas and concepts, work in 
progress, and publication-ripe results related to 
the implementation and application of functional languages and 
function-based programming.

Following the IFL tradition, IFL 2009 will use a post-symposium review 
process to produce a formal proceedings which we 
expect to be published by Springer in the Lecture Notes in Computer 
Science series. All participants in IFL 2009 are 
invited to submit either a draft paper or and extended abstract describing 
work to be presented at the symposium. 
These submissions will be screened by the program committee chair to make 
sure they are within the scope of IFL and will 
appear in the draft proceedings distributed at the symposium. Submissions 
appearing in the draft proceedings are not 
peer-reviewed publications. After the symposium, authors will be given the 
opportunity to incorporate the feedback from 
discussions at the symposium and will be invited to submit a revised full 
arcticle for the formal review process. These 
revised submissions will be reviewed by the program committee using 
prevailing academic standards to select the best 
articles that will appear in the formal proceedings.


TOPICS

IFL welcomes submissions describing practical and theoretical as well as 
submissions describing applications and tools. 
If you are not sure if your work is appropriate for IFL 2009, please 
contact the PC chair at ifl2...@shu.edu. Topics of 
interest include, but are not limited to:

 language concepts 
 type checking 
 contracts
 compilation techniques 
 staged compilation
 runtime function specialization
 runtime code generation 
 partial evaluation 
 (abstract) interpretation 
 generic programming techniques 
 automatic program generation 
 array processing 
 concurrent/parallel programming 
 concurrent/parallel program execution 
 functional programming and embedded systems 
 functional programming and web applications 
 functional programming and security 
 novel memory management techniques 
 runtime profiling and performance measurements 
 debugging and tracing 
 virtual/abstract machine architectures 
 validation and verification of functional programs 
 tools and programming techniques 


PAPER SUBMISSIONS

Prospective authors are encouraged to submit papers or extended abstracts 
to be published in the draft proceedings and to 
present them at the symposium. All contributions must be written in 
English, conform to the Springer-Verlag LNCS series 
format and not exceed 16 pages. The draft proceedings will appear as a 
technical report of the Department of Mathematics 
and Computer Science of Seton Hall University.


IMPORTANT DATES

Registration deadline   August 15, 
2009
Presentation submission deadline  August 15, 2009
IFL 2009 SymposiumSeptember 23-25, 
2009
Submission for review process deadline  November 1, 2009
Notification Accept/Reject  December 22, 2009
Camera ready version January 15, 2010


PROGRAM COMMITTEE

Peter Achten   University of Nijmegen, The Netherlands
Jost Berthold   Philipps-Universität Marburg, Germany
Andrew Butterfield University of Dublin, Ireland
Robby Findler Northwestern University, USA
Kathleen Fisher   AT&T Research, USA
Cormac Flanagan  University of California at Santa Cruz, USA
Matthew FlattUniversity of Utah, USA
Matthew Fluet   Toyota Technological Institute at 
Chicago, USA
Daniel Friedman Indiana University, USA
Andy GillUniversity of Kansas, USA
Clemens Grelck  University of Amsterdam/Hertfordshire, The 
Netherlands/UK
Jurriaan Hage  Utrecht University, The Netherlands
Ralf Hinze  Oxford University, UK
Paul Hudak   Yale University, USA
John HughesChalmers University of Technology, 
Sweden
Patricia JohannUniversity of Strathclyde, UK
Yukiyoshi KameyamaUniversity of Tsukuba, Japan
Marco T. Morazán (Chair)  Seton Hall University, USA
Rex PageUniversity of Oklah

Re: Standard IPC for Python?

2009-01-15 Thread Mel
Mel wrote:

> Philip Semanchuk wrote:
> 
>> I'm working on message queue support, but the Sys V IPC API is a
>> headache and takes longer to code against than the POSIX API.
> 
> I hadn't found it that bad.  I have a C extension I should perhaps clean
> up and make public.

I have a simple wrapper around the msgget/msgsnd/msgrcv functions that ought
to be downloadable through . GPL3.

I'm studying Philip Semanchuk's semaphore and shared memory code.  Let me
know what you think.

 Mel.

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Paul Rubin
Roy Smith  writes:
> C is not evil.  It's a tool.  Would you call a hammer evil because it's not 
> very good at driving screws?  

I would call a hammer evil if it were built in a way that made it
unnecessarily likely to hit your thumb.

> C is a very good tool for doing the kind of thing it was designed
> for, which is highly efficient, low-level, portable programming.

> The fact that C has been used to write all sorts of large-scale 
> applications doesn't mean that it's good at that kind of stuff.  It just 
> means that all the alternatives suck more than it does for that kind of 
> stuff.

I don't think so:  http://www.adaic.org/whyada/ada-vs-c/cada_art.html
--
http://mail.python.org/mailman/listinfo/python-list


mimetypes oddity

2009-01-15 Thread Sion Arrowsmith
>>> ge = mimetypes.guess_extension
>>> ge('image/jpeg')
'.jpe'
>>> ge('image/jpeg')
'.jpeg'
>>> 

I actually discovered this through explicitly calling mimetypes.init
to reload an edited mime.types file between calls to guess_extension,
but I think the above scenario makes for more disturbing reading 8-)

The problem is that mimetools initialises its type mapping dict
before adding new type mappings from a file by iterating through the
existing type mapping dict. This process is bootstrapped by a hard-
coded dict which gets shadowed by the first file read, so the second
file read uses a dict initialsed from the dict created by the first
file read (got that?) which has a different iteration order to the
hard-coded dict. So the mappings get added in a different order the
second time around, and where there are multiple mappings like this
you get a different answer.

Is this a bug? If I want to be sure of a consistent result, do I
need to use sorted(mimetools.guess_all_extensions(t))[0] (or [-1])?

-- 
\S -- si...@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
--
http://mail.python.org/mailman/listinfo/python-list


No Module Named MySQLdb Error

2009-01-15 Thread Sam
Hi Friends,
 This is Sam, a learner of Python programming
language. I would like to know a feasible and flexible process of
importing MySQLdb module. If anyone know about this and have an
expertise over this can help me over this issue.

1) I installed interface MySQLdb 1.2.2 in my MAC OS X system. In that
i installed Python 2.6 version along with that i installed MySQL
database.

2) So to run an application using these i am using the above
interface. I went to read me file in that and went thro the process.

3) In that they asked to run the setup.py file and setup_posix.py
file. I am unable to get the exact solution to the error, as i am
novice in python.
   I am pasting here that error, so if anyone can help me over
that u are welcome.

Sampath:MySQL-python-1.2.2 venkatarampey$ python setup.py
Traceback (most recent call last):
  File "setup.py", line 12, in 
from setup_posix import get_config
  File "/Users/venkatarampey/Downloads/MySQL-python-1.2.2/
setup_posix.py", line 89
return metadata, ext_options
SyntaxError: 'return' outside function

What is the solution for this error? Is anyone aware of using MySQL
with python can fix this issue. Hope so i will meet a person in
helping my task.

Regards,
Sampath Girish.M
--
http://mail.python.org/mailman/listinfo/python-list


Re: point class help

2009-01-15 Thread Sion Arrowsmith
r   wrote:
>here is what i have, it would seem stupid to use a conditional in each
>method like this...
>
>def method(self, other):
>if isinstance(other, Point2d):
>x, y = origin.x, origin.y
>else:
>x, y = origin[0], origin[1]
>#modify self.x & self.y with x&y

Here's another alternative (I'd've gone with the subclassing tuple if
writing this from scratch):

class Point2d(object):
...
def __getitem__(self, i):
if not 0 <= i <= 1:
raise IndexError
return getattr(self, ['x', 'y'][i])

def method(self, other):
x, y = other[0], other[1]
...

-- 
\S -- si...@chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
   "Frankly I have no feelings towards penguins one way or the other"
-- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3 isinstance

2009-01-15 Thread MRAB

Lambert, David W (S&T) wrote:
> I have use case, needn't be dynamic, and it's not hard to enclose set as
> tuple.  But I also write (for example)
>
> __all__ = 'this that other'.split()
> string_list = 'evenOneWord'.split()
>
> instead of
>
> __all__ = 'this','that','imTiredOfMistypingCommasAndQuotes'
>
>
> if not isinstance(o,set_of_handled_types):
> raise TypeError('call blessed programmer, this cannot occur')
> if isinstance(o,set_of_handle_types-treat_differently):
> treat_thusly(o)
> elif isinstance(o,treat_differently):
> treat_differently(o)
> elif etc.
>
You could write a Python source processor like 2to3.py which would
replace 'this that other'.split() with ['this', 'that', 'other']. In
fact, you could even write abbreviations such as /this that other/
and then use the script to expand them!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python 3 isinstance

2009-01-15 Thread
Although isinstance predates sets, the python history I recall is that
allowing tuples as second argument to isinstance happened at about the
same time as set type was builtin.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread r
On Jan 14, 1:16 pm, Paul Rubin  wrote:
> "Russ P."  writes:
[snip]
> I have a situation which I face almost every day, where I have some
> gigabytes of data that I want to slice and dice somehow and get some
> numbers out of.  I spend 15 minutes writing a one-off Python program
> and then several hours waiting for it to run.  If I used C instead,
> I'd spend several hours writing the one-off program and then 15
> minutes waiting for it to run, which is not exactly better.  
[snip]
> I would be ecstatic with a version of Python where I might have to
> spend 20 minutes instead of 15 minutes writing the program, but then
> it runs in half an hour instead of several hours and doesn't crash.  I
> think the Python community should be aiming towards this.

You and Everybody -- would be "ecstatic" if this could happen. But
first someone has to design such a complex implementation. You want
everything, but there is a trade-off.

You said you wrote this program in 15 min. How much testing did you
actually do on this data before running it? If you told me you spent
more than 15 minutes i would not believe you. Look, Python is not a
compiled language -- and for good reason -- so for now you need to do
more initial testing if you plan to run a "15 min hack script" on a
multi-GB data source file, and then throw a temper-tantrum when the
damn thing blows chunks!

If Python could give the benefits of compiled languages whilst being
interpreted(without taking the "fun" out of Python), that would be
wonderful, but can you implement such a system? Can anybody at this
point?

If you can, i can assure you will be worshiped as a God.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread r
On Jan 15, 11:13 am, Paul Rubin  wrote:
> Roy Smith  writes:
> > C is not evil.  It's a tool.  Would you call a hammer evil because it's not
> > very good at driving screws?  
>
> I would call a hammer evil if it were built in a way that made it
> unnecessarily likely to hit your thumb.
>
> > C is a very good tool for doing the kind of thing it was designed
> > for, which is highly efficient, low-level, portable programming.
> > The fact that C has been used to write all sorts of large-scale
> > applications doesn't mean that it's good at that kind of stuff.  It just
> > means that all the alternatives suck more than it does for that kind of
> > stuff.
>
> I don't think so:  http://www.adaic.org/whyada/ada-vs-c/cada_art.html

Hammers are not evil, they have no logic, interpreters and compilers
are not evil either -- you and i control there every move. The hammer
will go exactly where you guide it to -- if that happens to be you
thumb...??

Python does exactly what it's told, if you tell Python to smash your
thumb, Python will gladly comply :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Paul Rubin
r  writes:
> You said you wrote this program in 15 min. How much testing did you
> actually do on this data before running it? If you told me you spent
> more than 15 minutes i would not believe you. 

I would say hours, in the sense that the program ran correctly for
that long, processing several GB's of data before hitting something
obscure that it couldn't handle.  This is not a single incident, it's
something that happens all the time; write the program, run it til it
crashes, fix what made it crash, run some more, etc.  In some cases
where the program is a background process listening to external
events, it runs for weeks before hitting something it can't handle.

To be fair, that kind of thing is notoriously difficult to make
airtight in real world systems, which is why Erlang uses a "let it
crash" philosophy that emphasizes recovery from failures rather than
trying to avoid them at all costs.  But, at least in the stuff I'm
hacking, I think a lot of these errors could be avoided with more
automated ways to check for type consistency.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread r
Paul Rubin
> I would say hours, in the sense that the program ran correctly for
> that long, processing several GB's of data before hitting something
> obscure that it couldn't handle.  This is not a single incident, it's


So what was the fatal error, care to post a traceback?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Paul Rubin
r  writes:
> So what was the fatal error, care to post a traceback?

Usually it's "expected to find some value but got None", or got a
list, or expected some structure but got a different one, or some
field was missing, etc.  It's not a single traceback, it's a recurring
theme in developing this stuff.  
--
http://mail.python.org/mailman/listinfo/python-list


Re: Possible bug in Tkinter - Python 2.6

2009-01-15 Thread José Matos
On Thursday 15 January 2009 15:28:36 r wrote:
> First of all be very careful using from "module" import * or you will
> have name conflicts. Tkinter is made to be imported this way and i do
> it all the time. for the others do.
>
> import tkMessageBox as MB
> import tkFileDialog as FD
> or whatever floats your boat.

Importing explicitly works for me in Fedora 11 (rawhide):
-
from Tkinter import *
from tkMessageBox import askyesno
from tkFileDialog import askopenfilename

root = Tk()

def ask_file():
   file_name = askopenfilename()
   print file_name

def ask_confirm():
   answer = askyesno()
   print answer

Button(root, text='Ask file', command=ask_file).pack()
Button(root, text='Ask confirm', command=ask_confirm).pack()

root.mainloop()
-

> Secondly i hear all the time about problems with fedora distros, so
> that would explain the issue to me.

That seems a long jump in the reasoning since OP reported the same behavior in 
Suse and Solaris.

-- 
José Abílio
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread r
On Jan 15, 11:59 am, Paul Rubin  wrote:
> r  writes:
> > So what was the fatal error, care to post a traceback?
>
> Usually it's "expected to find some value but got None", or got a
> list, or expected some structure but got a different one, or some
> field was missing, etc.  It's not a single traceback, it's a recurring
> theme in developing this stuff.  

Sounds like the results of poor testing and lack of design good
program logic
--
http://mail.python.org/mailman/listinfo/python-list


Re: mimetypes oddity

2009-01-15 Thread skip
[mimetype weirdness reported]

Sion> Is this a bug?

Might be.  Can you file a bug report in the Python issue tracker with a
small script that demonstrates the behavior?

-- 
Skip Montanaro - s...@pobox.com - http://smontanaro.dyndns.org/
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread r
Heres a little food for thought,
Maybe you did tell Python to hit the nail head, but your calculations
of the direction vector were slightly off. Instead of a direct hit,
the hammer grazed the head and now the resultant vector aims strait
for your thumb -- Who's to blame here?
--
http://mail.python.org/mailman/listinfo/python-list


Re: the name of a method

2009-01-15 Thread Matimus
On Jan 15, 8:23 am, thomas.steffe...@googlemail.com wrote:
> Hello,
>
> I have a Class:
>
> class myClass:
>     def __init__(self):
>         # do something
>         print "name of class = " +  self.__class__.__name__
>
>     def myMethod(self):
>         # do something
>         print "name of method = " + "myMethod"
>         return
>
>     ...
>
> I print the name of the class with self.__class__.__name__ in
> __init__.
> I want to print also in every method of myClass the name of the
> method.
> How can I get the name? I would not like to write e.g. "myMethod". Is
> there a variable like self.__class__.__name__ for this?
> Thanks for your hints, Thomas

I would just use a decorator:

>>> def print_method_name(meth):
... def new_meth(*args, **kwargs):
... print meth.func_name
... return meth(*args, **kwargs)
... return new_meth
...
>>> class MyClass(object):
... @print_method_name
... def my_method(self):
... pass
...
>>> x = MyClass()
>>> x.my_method()
my_method


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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Paul Rubin
r  writes:
> Sounds like the results of poor testing and lack of design good
> program logic

It would sure be nice if the language made it easier, not harder.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Executing global code

2009-01-15 Thread Zac Burns
The first line: doLoad = False, is to be ignored.
--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



On Thu, Jan 15, 2009 at 10:30 AM, Zac Burns  wrote:
> I'm not sure I fully understand the question "no moving the code to a
> function", but you can prevent reload in a module by doing something
> like this:
>
> doLoad = False
> try:
>  no_reload
> except NameError:
>  no_reload = True
> else:
>  raise RuntimeError, "This module is not meant to be reloaded."
>
> --
> Zachary Burns
> (407)590-4814
> Aim - Zac256FL
> Production Engineer (Digital Overlord)
> Zindagi Games
>
>
>
> On Thu, Jan 15, 2009 at 8:50 AM, Peter Otten <__pete...@web.de> wrote:
>> Jakub Debski wrote:
>>
>>> Is it possible to execute global code (module-level code) more than
>>> once keeping the state of global variables? This means no reload() and
>>> no moving the code to a function.
>>
>> You have a module containing e. g. these two statements
>>
>> x = 42
>> x += 1
>>
>> and want to rerun it with the effect of x becoming 44? That is not possible
>> because in Python
>>
>> x = 42
>>
>> is a statement, too, and will thus be rerun.
>>
>> Peter
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Crashes

2009-01-15 Thread Martin P. Hellwig

koranthala wrote:


This does sounds more to me like a windows/hardware problem, what you 
could do is check the windows log for errors, especially look for read 
errors from the hard disk.


Windows sometimes can behave very strangely especially if the external 
libs don't behave well on a multi process machine or when the execution 
prevention gets in the way.


Both problems can be worked around though (setting a uniprocessor flag 
at the binary, adding an exception for the execution prevention) but 
should not be done without proper reason.


Good luck.

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


Injecting a global into a defined function??

2009-01-15 Thread Cong Ma
Hi,

I'd appreciate your hints on this problem. I'm writing a module in which several
functions can alter the value of a global variable (I know this sounds evil,
please forgive me...). What I'm trying to do is to eliminate the "global foo"
lines in those functions' bodies and to use a decorator for the same task. For
example:

@global_injected("SPAM")
def foo():
... ...

will have the same effect as

def foo():
global SPAM
... ...

Leaving the evilness of globals aside, I wonder how I can implement this (for
Python 2.x). I'd like to hear your opinions. Thank you.

Regards,
Cong.

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


Re: Injecting a global into a defined function??

2009-01-15 Thread MRAB

Cong Ma wrote:

Hi,

I'd appreciate your hints on this problem. I'm writing a module in which several
functions can alter the value of a global variable (I know this sounds evil,
please forgive me...). What I'm trying to do is to eliminate the "global foo"
lines in those functions' bodies and to use a decorator for the same task. For
example:

@global_injected("SPAM")
def foo():
... ...

will have the same effect as

def foo():
global SPAM
... ...

Leaving the evilness of globals aside, I wonder how I can implement this (for
Python 2.x). I'd like to hear your opinions. Thank you.


I doubt that's possible because 'global' is syntax.
--
http://mail.python.org/mailman/listinfo/python-list


Executing global code

2009-01-15 Thread Jakub Debski

Hi,

Is it possible to execute global code (module-level code) more than 
once keeping the state of global variables? This means no reload() and 
no moving the code to a function.


regards,
Jakub


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


Re: Python 3 isinstance

2009-01-15 Thread Carl Banks
On Jan 15, 3:35 am, Duncan Booth  wrote:
> Terry Reedy  wrote:
> > Lambert, David W (S&T) wrote:
> >> Overly terse.  I do mean that this is illegal:
>
> >> isinstance(s, {str, bytes})
>
> >> tuples have order, immutability, and the possibility of repeat items.
>
> >> A set is most reasonable in a mathematical sense.
>
> > I agree.  However, isinstance predates set.  Hence the status quo.
>
> There is another reason which nobody has mentioned, and which might make it
> tricky to do a good implementation that does accept sets.
>
> The second argument to isinstance or issubclass is a classinfo where a
> classinfo is either a class or a tuple of classinfo. This makes it easy to
> group related types: you can just combine existing groups of types together
> without having to flatten tuples. The immutability of tuples guarantees
> that you cannot create a recursive structure this way.
>
> If you allow sets then the code which traverses the classinfo has to
> protect itself against the possibility of an infinite recursion.
>
> The same argument also applies to exception handlers which also accept a
> type or a tuple of things they accept.
>
> Of course you could allow either an existing classinfo or a set of types,
> but not allow the set to contain anything other than types. That would
> still prevent recursion but means you have to be stricter about knowing
> what you are combining. That probably wouldn't be a bad thing if everyone
> used sets consistently but I suspect would be messy if introduced at this
> stage.

I don't see what the big deal is.  Right now isinstance accepts a type
or a tuple of types.  The code could be changed to allow a type, or
any iterable the returns types (wherein every items of the sequence is
required to be a type).  What's messy about that?


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


Re: Alphametric fun with Python

2009-01-15 Thread Raymond Hettinger
> > Thought you guys might enjoy this:
> >    http://code.activestate.com/recipes/576615/
>
> Nice and short, but it's also very slow on my PC (Psyco may help).
>
> This solves them in moments:http://labix.org/python-constraint

Intelligent search beats brute force permutation search.
The constraint solver is pretty nice.
The recipe is mainly about how to use itertools.permutations()
for simple programs that take minutes to write and get the job done.

Raymond


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


Re: Executing global code

2009-01-15 Thread Zac Burns
I'm not sure I fully understand the question "no moving the code to a
function", but you can prevent reload in a module by doing something
like this:

doLoad = False
try:
  no_reload
except NameError:
  no_reload = True
else:
  raise RuntimeError, "This module is not meant to be reloaded."

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer (Digital Overlord)
Zindagi Games



On Thu, Jan 15, 2009 at 8:50 AM, Peter Otten <__pete...@web.de> wrote:
> Jakub Debski wrote:
>
>> Is it possible to execute global code (module-level code) more than
>> once keeping the state of global variables? This means no reload() and
>> no moving the code to a function.
>
> You have a module containing e. g. these two statements
>
> x = 42
> x += 1
>
> and want to rerun it with the effect of x becoming 44? That is not possible
> because in Python
>
> x = 42
>
> is a statement, too, and will thus be rerun.
>
> Peter
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Readline and Python 2.6.1 on a Mac

2009-01-15 Thread Ned Deily
In article 
<4632a161-ed13-477c-a40a-2b606fda1...@a29g2000pra.googlegroups.com>,
 James Brady  wrote:
> Hi, I just upgraded Python from 2.5.2 up to 2.6.1 on my 10.5 Mac
> (installed from the DMG here: http://www.python.org/download/releases/2.6.1/),
> and I'm having some problems with readline and rl_completer.
> 
> I have PYTHONSTARTUP pointing to this file:
> #!/usr/bin/env python
> 
> try:
> import readline
> except ImportError:
> print "Module readline not available."
> else:
> import rlcompleter
> readline.parse_and_bind("bind ^I rl_complete")
> 
> Which was working fine for tab completion in 2.5. Now, however, I
> can't enter 'b' (lower case b) in interactive python shells - no
> character appears.

Your startup file was set up for a Python (likely the Apple-supplied 
2.5) that was built using the BSD editline library for readline.  But 
the 2.6 python.org python was built with GNU readline which uses a 
different syntax.

s/"bind ^I rl_complete"/"tab: complete"/



-- 
 Ned Deily,
 n...@acm.org

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


Re: Python 3 isinstance

2009-01-15 Thread Duncan Booth
Carl Banks  wrote:

> I don't see what the big deal is.  Right now isinstance accepts a type
> or a tuple of types.  The code could be changed to allow a type, or
> any iterable the returns types (wherein every items of the sequence is
> required to be a type).  What's messy about that?

No, it isn't restricted to a type or a tuple of types: right now isinstance 
accepts X where X is a type or a tuple of X. The definition is recursive, 
and the data structure can be deeply nested, but (because tuples are 
immutable) it cannot be infinite.

e.g.
>>> isinstance('x', (int, (float, str)),),),),))
True

If you change the code so that it only allows iterables one level deep then 
existing code may break. If you change it to allow mutable nested values 
then the interpreter has to avoid getting stuck in an infinite loop.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread r
On Jan 15, 12:25 pm, Paul Rubin  wrote:
> r  writes:
> > Sounds like the results of poor testing and lack of design good
> > program logic
>
> It would sure be nice if the language made it easier, not harder.

I am for anything that makes debugging easier, as long as that "thing"
doesn't take away the freedom i enjoy while writing Python code. If
you can give me both then i will support your efforts -- The world
does not need two Javas!

Python's existence resides in a unique niche, Simplistic-Elegant-
Programming-Bliss. Python promotes self reliance, you don't get the
safety net you do with other languages. You must consider all the
consciences/possible side-effects of your code.

If you are going to use Python to design enormous systems(or operate
on enormous data sources) -- at this point --, then you will need to
do some enormous testing.

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


Re: 2to3 Help?

2009-01-15 Thread Terry Reedy

marco.m.peter...@gmail.com wrote:

I have Python 3.0. I tried to use the 2to3 program included with the
interpreter to convert some scripts for Python 2.5 to Python 3.0 ones.


I believe 2to3 is will work 'best' with files modified to use some new 
2.6 features (backported from 3.0) where 'best' means 'Runs with 2.6 and 
then with 3.0 after 2to3 conversion without hand editing'.  If you want 
to do a one-time conversion, then 2.5 to 3.0 is fine.


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


Re: Python 3 isinstance

2009-01-15 Thread Terry Reedy

Duncan Booth wrote:

Terry Reedy  wrote:


Lambert, David W (S&T) wrote:

Overly terse.  I do mean that this is illegal:

isinstance(s, {str, bytes})

tuples have order, immutability, and the possibility of repeat items.

A set is most reasonable in a mathematical sense.

I agree.  However, isinstance predates set.  Hence the status quo.




There is another reason which nobody has mentioned, and which might make it 
tricky to do a good implementation that does accept sets.


The second argument to isinstance or issubclass is a classinfo where a 
classinfo is either a class or a tuple of classinfo. This makes it easy to 
group related types: you can just combine existing groups of types together 
without having to flatten tuples. The immutability of tuples guarantees 
that you cannot create a recursive structure this way.


If you allow sets then the code which traverses the classinfo has to 
protect itself against the possibility of an infinite recursion.


The same argument also applies to exception handlers which also accept a 
type or a tuple of things they accept. 

Of course you could allow either an existing classinfo or a set of types, 
but not allow the set to contain anything other than types. That would 
still prevent recursion but means you have to be stricter about knowing 
what you are combining. That probably wouldn't be a bad thing if everyone 
used sets consistently but I suspect would be messy if introduced at this 
stage.


Nice explanation.  A mathematical set is a frozen set of course (except 
that mathematical sets can also be recursive! -- depending on the 
particular set theory), and there is no literal syntax for that.


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


Re: Alphametric fun with Python

2009-01-15 Thread bearophileHUGS
Raymond Hettinger:
> for simple programs that take minutes to write and get the job done.

For fun here's a specific example:

from csp import Problem, timing
print "SEND+MORE=MONEY problem:"
p = Problem("recursivebacktracking")
p.addvars("sendmory", range(10))
p.addrule(lambda d,e,y: (d+e)%10 == y) # alternative syntax
p.addrule("(n*10+d+r*10+e)%100 == e*10+y")
p.addrule("(e*100+n*10+d+o*100+r*10+e)%1000 == n*100+e*10+y")
p.addrule("1000*s+100*e+10*n+d + 1000*m+100*o+10*r+e == 1*m+1000*o
+100*n+10*e+y")
p.notin([0], "sm")
p.alldifferent()
solutions, time = timing(p.solutions)
print "Computing time:", time, "s"
for s in solutions:
print "%(s)d%(e)d%(n)d%(d)d + %(m)d%(o)d%(r)d%(e)d = %(m)d%(o)d%(n)
d%(e)d%(y)d" % s
print

Probably it's not too much difficult to write a code able to solve a
more general alphametric problem: you can write it more or less like
yours, but it leads to a single equation, that is slow to solve. To
give the solver engine a chance to speed up the computation you have
to split the single equation into many equations. This allows the
solver to prune that large search space in a faster way (the search
space may have 3+ millions items so it's not huge anyway).

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


Parallel package hierarchies/directories

2009-01-15 Thread atleta
  Hi,

I'd need a logical way to organize my unit tests into packages, but I
don't know how to do it in python. I'd like to separate my test
sources (modules) from the code. The problem is that it seems that
python does not allow a package to spread through multiple
directories. I'd like to have the following layout (which is the usual
way we do it e.g. in java):

+-project_root/
|
+-src/
|   |
|   +-soap
|
+-test/
|
+-soap

'soap' is a package under the source tree. I.e. I'd like my test
source tree to mirror the source tree and have package specific tests
in the same package as the code they excersize. However I found that
this is not possible, at least not the way I wanted, not without
moving files. I tried to place a script that would run all tests under
the 'test' directory. It added 'src' to the sys.path, but then it was
only able to import packages from test/soap and not src/soap.

Is there a way I can have python look for soap package modules in both
places? If not, then could someone tell me what is the usual way to
layout test code?

  Thanks,
atleta
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python Crashes

2009-01-15 Thread Terry Reedy


I am using 9 3rd party modules and there are some DLLs too - PIL etc.
But the problem is that the issue comes only after 3 hrs usually.
When I checked the memory using taskmanager, I found that it is not
going too high.


I know of 3 general ways to crash the interpreter.

1. Use 3rd party modules.  By far most common.  I would try to do 
everything but use one of them, possibly by replacing import with stub.


2. Use ctypes.  This can bypass all protections.

3. Write a subtle infinite recursion in a class method that is not 
checked for infinite recursion.  The following illustrates the idea 
without testing whether this crashes.


class c():
  def __add__(self, other): return self+other # calls this __add__


I believe some of these vulnerabilities existed in 2.4 but many were 
crushed in 2.5.  Backported to 2.4.3? No idea.  This could hit so fast 
you never see it in task manager.


If your 3rd party modules work with 2.5, consider upgrading to 2.5.3. 
Many bug fixes.


tjr

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Terry Reedy

Paul Rubin wrote:

r  writes:

So what was the fatal error, care to post a traceback?


Usually it's "expected to find some value but got None", or got a
list, or expected some structure but got a different one, or some
field was missing, etc.  It's not a single traceback, it's a recurring
theme in developing this stuff.  


I do not see any connection, really, between what you describe above and 
your desire for static type-checking expressed elsewhere.  When I was 
regularly doing analysis of empirical data files, I learned (sometimes 
the hard way, as you describe above) to **ALWAYS** run preliminary 
checks of all fields through the entire file so I would know the actual 
range of values of each and whether the ranges comported with the 
requirements of the intended analyses.  The principle was/is the same 
whether using BMDP, SAS, C, or Python.


Terry Jan Reedy

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


UnicodeError for join()

2009-01-15 Thread tmallen
This line of code is throwing a UnicodeError for a handful of the few
hundred files I'm processing:

rc_file.write("\n\n".join([self.title, "### BEGIN CONTENT ###",
self.content]))
.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position
442: ordinal not in range()

What should I change to make this unicode-safe?

Thanks,
Thomas
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeError for join()

2009-01-15 Thread Martin v. Löwis
> rc_file.write("\n\n".join([self.title, "### BEGIN CONTENT ###",
> self.content]))
> .
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position
> 442: ordinal not in range()
> 
> What should I change to make this unicode-safe?

One of self.title and self.content is a Unicode string, the other is
a byte string. You need to change them to have the same type (depending
on whether you want to process them as Unicode or byte strings).

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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Terry Reedy

Michele Simionato wrote:

On Jan 14, 3:27 am, Paul Rubin  wrote:>

I don't understand why some folks spew such violent rhetoric against
the idea of augmenting Python with features to alert you automatically
when you depart from the convention, so that you can check that the
departure is actually what you wanted.  A lot of the time, I find, the
departures are accidental and automated checks would save me
considerable debugging.


I would be fine having something like pylint built-in in the language
and running at every change of the source code (unless disabled with a
command line switch). I think this is the only reasonable solution to
get some additional protection we can hope for. A true change of the
language IMO is impossible, both technically, politically and for
legacy issue. Also, I am not convinced it would be a good idea, even
theoretically. It is easier to write a new Python-like language
from scratch than to add type checking to Python (I think you
were not proposing adding type checking in this post, right?).


Integrating checkers with the interpreter is something that could/should 
be done by an IDE.  It should not be too hard, for instance, to give 
IDLE a configurable run-thru-PyLint option keyed, for instance, to 
shift-F5 (given that F5 is run-with-CPython).  Alt-F5 for instance could 
be run-Pylint-and-if-no-errors-then-CPython.


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


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Terry Reedy

Michele Simionato wrote:

On Jan 15, 7:38 am, "James Mills" 
wrote:

If one is a clumsy programmer, then perhaps
one ought to be using a language that holds one's hand.


Yes, and I use Python because it is a language that holds my hand,
otherwise I would use C and enjoy segmentation faults all the time.


So perhaps it is a matter of which handholding one wants and which one 
hates.  To avoid all, stick with assembler ;-).


tjr

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


Re: List comprehension - NameError: name '_[1]' is not defined ?

2009-01-15 Thread Mark Wooding
mario ruggier  writes:

> Some info on its security is at:
> http://evoque.gizmojo.org/usage/restricted/ 

> Tell you what, if you find a security hole there (via exposed template
> source on a Domain(restricted=True) setup) I'll offer you a nice
> dinner (including the beer!) somewhere, maybe at some py conference,
> but even remotely if that is not feasible... ;-) The upcoming 0.4
> release will run on 2.4 thru to 3.0 -- you can have some fun with that
> one (the current 0.3 runs on 2.5 and 2.6).

I'm pretty sure I can break this on 3.0, because the f_restricted frame
flag has gone.  Here's how:

>>> import template, domain
>>> dom = domain.Domain('/tmp/mdw/', restricted = True, quoting = 'str')
>>> t = template.Template(dom, 'evil', from_string = True, src =
>>> "${inspect.func_globals['_'*2+'builtins'+'_'*2].open('/tmp/mdw/target').read()}")
2009-01-15 20:30:29,177 ERROR [evoque] RuntimeError: restricted
attribute:   File "", line 1, in 
: 
EvalError(inspect.func_globals['_'*2+'builtins'+'_'*2].open('/tmp/mdw/target').read())
u'[RuntimeError: restricted attribute:   File "", line 1, in
\n:
EvalError(inspect.func_globals[\'_\'*2+\'builtins\'+\'_\'*2].open(\'/tmp/mdw/target\').read())]'

which means that it's depending on the func_globals attribute being
rejected by the interpreter -- which it won't be because 3.0 doesn't
have restricted evaluation any more.

Python is very leaky.  I don't think trying to restrict Python execution
is a game that's worth playing.

-- [mdw]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Does Python really follow its philosophy of "Readability counts"?

2009-01-15 Thread Bruno Desthuilliers

Paul Rubin a écrit :

Bruno Desthuilliers  writes:

We're not talking about libraries here.

Yes we are. If the default is "non-dynamic", then a class author is in
charge of explicitely allowing it when *he* see fits.


Oh, I see what you mean, the standard libraries would be affected if
the language default were to tone down the dynamism.  True.


Not only the standard lib. Quite a lot of frameworks relies on these 
features.



As long as it's up to the *user* to choose, that's ok. Your "@dynamic"
class decorator doesn't have the same implications.


Well, you could use it on library classes too:

   from library import Libclass
   Libclass = dynamic(Libclass)
   ...


That's fine when you directly use the class. Not when you work with 
instances provided by a framework (or another lib) that won't be 
affected by your above rebinding.


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


Convention vs. fascism (was: General direction)

2009-01-15 Thread Ben Finney
Steven D'Aprano  writes:

> On Thu, 15 Jan 2009 10:08:37 +0100, Diez B. Roggisch wrote:
> 
> > Familiarize yourself with PEP8 for naming and coding-conventions
> > first.
> 
> Enough of the PEP8-fascism please. It is not compulsory to follow
> PEP8 in order to be allowed to learn Python. If Guido wanted it
> compulsory, he'd have made the compiler enforce it.

You are responding to something in Diez's message that I can't see.
Nowhere does he say anything about PEP 8 being compulsory. Quite the
contrary, he suggests gaining *familiarity* with PEP 8, and calls it a
set of *conventions*.

> Having said that, I will *suggest* to the Original Poster that you
> should follow PEP8 as much as possible. It will save you a lot of
> grief in the long run.

This, on the other hand, seems pretty much a re-phrasing of what Diez
already said.

-- 
 \   “You've got the brain of a four-year-old boy, and I'll bet he |
  `\ was glad to get rid of it.” —Groucho Marx |
_o__)  |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnicodeError for join()

2009-01-15 Thread tmallen
On Jan 15, 3:34 pm, "Martin v. Löwis"  wrote:
> > rc_file.write("\n\n".join([self.title, "### BEGIN CONTENT ###",
> > self.content]))
> > .
> > UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position
> > 442: ordinal not in range()
>
> > What should I change to make this unicode-safe?
>
> One of self.title and self.content is a Unicode string, the other is
> a byte string. You need to change them to have the same type (depending
> on whether you want to process them as Unicode or byte strings).
>
> HTH,
> Martin

How can I do that?

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


Re: UnicodeError for join()

2009-01-15 Thread Martin v. Löwis
>> One of self.title and self.content is a Unicode string, the other is
>> a byte string. You need to change them to have the same type (depending
>> on whether you want to process them as Unicode or byte strings).
>>
> 
> How can I do that?

First, you need to find out what the respective types are:

print type(self.title), type(self.content), repr(self.title),
repr(self.content)

With that information, as a very important next step, you need to
understand why the error occurs.

Then, you need to fix it, e.g. by converting all strings to byte
strings.
Suppose title is a unicode string, and further suppose the output
is to be encoded in cp1252, then you change the line to

rc_file.write(u"\n\n".join([self.title.encode("cp1252"),
"### BEGIN CONTENT ###",
self.content]))

Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   3   >