Python 2.6 in shared/VPS environment

2009-07-31 Thread northof40
Hi - I'd really like to have access to Python 2.6 to try something
out. It needs to be on Linux/Unix machine. I don't mind paying but
whichever way I turn I find Python 2.4 is the standard installation.

Anyone know of anyone who offers this out of the box ? Or got any
smart way of achieving it ?

thanks

R.



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


Re: Confused About Python Loggin

2009-07-31 Thread Peter Otten
Jannik Sundø wrote:

> Dear all, I am quite confused about the Python logging. I have read
> and re-read the Python documentation for the Python logging module and
> googled, but to no avail. I simply want one logger to log to a file
> and another logger to log to the console. Neither should log the
> other's messages. The code below causes fileLogger to log to BOTH a
> file and the console, how come? It seems to me that a call such as
> "consoleHandler = logging.StreamHandler()" affects other loggers,
> which I do not want. Also, if I do not do
> logging.basicConfig(level=logging.INFO) in one of the classes of my
> application, the logging does not work. Any ideas how come?
> 
> Thanks a lot for any help! :)
> 
> fileHandler = logging.FileHandler('../../logs/log.txt')
> fileLogger = logging.getLogger('TESTHARNESSFILE')
> fileLogger.addHandler(fileHandler)
> fileLogger.setLevel(logging.INFO)
> consoleHandler = logging.StreamHandler()
> logger = logging.getLogger('TESTHARNESS')
> logger.addHandler(consoleHandler)
> logger.setLevel(logging.INFO)

The code you present should work as you expect:

$ cat tmp.py
import logging

fileHandler = logging.FileHandler('tmp.txt')
fileLogger = logging.getLogger('TESTHARNESSFILE')
fileLogger.addHandler(fileHandler)
fileLogger.setLevel(logging.INFO)

consoleHandler = logging.StreamHandler()
logger = logging.getLogger('TESTHARNESS')
logger.addHandler(consoleHandler)
logger.setLevel(logging.INFO)


logger.info("to console")
fileLogger.info("to file")
$ python tmp.py
to console
$ cat tmp.txt
to file

What problems exactly are you running into if you omit the basicConfig() 
call?

Peter

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


problem on socket programming

2009-07-31 Thread MalC0de
hello there, I'm using Learning Python 3rd edition as my core
reference, the book is great.
here's some snippet for demonstrating echo-server program, but I can't
interpret it in both windows / Gnu linux ...

#!/usr/bin/env python
from socket import *
myHost = ''
myPort = 50007
sockobj = socket(AF_INET,SOCK_STREAM)
sockobj.bind((myHost,myPort))
sockobj.listen(5)
while true :
connection,address = sockobj.accept()
print 'server connected by :',address
while True:
data = connection.recv(1024)
if not data: break
connection.send('echo : '+ data)
connection.close()

where's the problem,
beforehand appreciate ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-07-31 Thread Steven D'Aprano
On Thu, 30 Jul 2009 18:06:31 -0500, Robert Kern wrote:

> On 2009-07-30 16:44, r wrote:
>> On Jul 30, 4:29 pm, Emmanuel Surleau wrote:
 1.) No need to use "()" to call a function with no arguments. Python
 -->  "obj.m2().m3()" --ugly
Ruby -->  "obj.m1.m2.m3"  -- sweeet!
 Man, i must admit i really like this, and your code will look so much
 cleaner.
>>> It has benefits - code does look better. It has also significant cons
>>> - it is ambiguous.
>>> For instance:
>>>
>>> a = b
>>>
>>> Is b a variable or a method called without parameter?
>>
>> Hello Emanuel,
>> Again, who so ever names a method with such a non-descriptive name will
>> get whats coming to him. And if you did for some reason use such a
>> cryptic name as "b", do yourself (and everyone else) a favor and follow
>> it with "()" to denote the method call. Remember when something is
>> optional that means you have an option to use it OR not use it.
> 
> I believe his point is that it is ambiguous to the compiler, not humans
> reading the code. Python functions and methods are first class objects.
> They can be passed around. If they were auto-called, then you could not
> do this.

Oh my, "r" is still around is he??? And now he's singing the praises of 
Ruby, the language which he treated as the Devil's Spawn when he first 
arrived. That's hilarious.

But back on topic... "r" has missed the point. It's not that a=b is hard 
to understand because b is a poor name. The example could have been:

def factory_function():
magic = time.time()  # or whatever
def inner():
return magic
return inner

my_function = factory_function

It's still ambiguous. Does the programmer intend my_function to become 
factory_function itself, or the output of factory_function?

In Python, it's not ambiguous at all -- my_function is set to 
factory_function, and *not* the output of factory_function, because you 
haven't called the function.

Python's model is consistent and simple: given a function "func", you 
*always* refer to the function object itself as func and you *always* 
call it with func(). This applies no matter how many arguments the 
function takes, or what it returns, or where it is defined.

I think it's telling that "r" the fanboy has rejected Python's advantages 
(simplicity, consistency, unambiguity) in favour of lazily saving two 
keystrokes.



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


How to force SAX parser to ignore encoding problems

2009-07-31 Thread Łukasz
Hi,
I have a problem with my XML parser (created with libraries from
xml.sax package). When parser finds a invalid character (in CDATA
section) for example �, throws an exception SAXParseException.

Is there any way to just ignore this kind of problem. Maybe there is a
way to set up parser in less strict mode?

I know that I can catch this exception and determine if this is this
kind of problem and then ignore this, but I am asking about any global
setting.

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


Re: problem on socket programming

2009-07-31 Thread Xavier Ho
Whoops, posted to the wrong address yet again.

I deserve some punishment.

On Fri, Jul 31, 2009 at 5:33 PM, Xavier Ho  wrote:

> On Fri, Jul 31, 2009 at 5:25 PM, MalC0de wrote:
>
>>
>> while true :
>>
>> where's the problem,
>
>
> There. =]
>

(Btw, it's True, not true.)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Test for Pythonwin?

2009-07-31 Thread eliasf

"steve" wrote:

Is there a good way to check if a script is running inside Pythonwin?
Perhaps a property or method that is exposed by that environment?


I don't know how foolproof it is, but this works:

   'pywin' in sys.modules

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Masklinn

On 30 Jul 2009, at 23:52 , Jan Kaliszewski wrote:
Dnia 30-07-2009 o 22:41:57 Masklinn   
napisał(a):

On 30 Jul 2009, at 22:23 , Jan Kaliszewski wrote:

30-07-2009 o 13:36:49 Masklinn  wrote:

On 30 Jul 2009, at 06:04 , alex23 wrote:

On Jul 30, 1:06 pm, r  wrote:

2.) the .each method
container.each{|localVar| block}
This method can really cleanup some ugly for loops, although i  
really

like the readability of for loops.


map(lambda localVar: , sequence)

or:

def usefully_named_func(var):
 
 return var

transformed = [usefully_named_func(v) for v in sequence]

The issue here is of course that `map` and comprehensions are  
transformations. `#each` exists for effectful iterations (Ruby  
has `#map` for the map operation). So the intent expressed by  
`#each` and `map` isn't the same. Furthermore and this is the  
most problematic limitation of Python here, `lambda` doesn't  
allow complex transformations due to its restrictions, so one has  
to switch to named functions which works but isn't sexy (and  
tends to lower readability imo).


I don't see any real limitation. What's wrong in:

for localVar in container:
  block

Well what's wrong with using that rather than `map`, `filter` or a  
list comprehension? (and if you don't see what the limitations of  
`lambda` are, you probably very rarely use it)


I know well about the expression-only-limitation of lambda, fortnately
there is the 'for' loop construct (or, alternatively, you can define
a named function).

And then, what's wrong with using 'for' rather than 'map', 'filter'  
etc.?

Nothing, but then again nothing's wrong using C's for either.

But I do think that using higher-order functions:
* tends to be terser while not going overly terse, it only removes  
boilerplate
* is more composable (it's pretty easy to tack another transformer in  
your chain, the same way defining iterator/generator transformers and  
chaining them is simpler than doing the same thing using explicit  
`for…in`)
* it clearly spells the intent of the programmer, while `for` can be  
anything and everything, and one has to dive into the code to know  
even the high-level operations performed (is it a basic  
transformation? A filtering or partitioning? A reduction? An  
application of side effects?)

Agree, that 'anonymous block syntax' would be nice in some cases, but
I don't see any real limitation caused by lack of it i.e. something  
you

can't (all you can only with unreasonable effort).

There are no limitations, but once again there never are. There are no  
actual limitations to using conditional jumps over iterators either.



And ruby's container.each is very similar to Python's iter()



Uh… not at all…


OK, .each is like Python's iter() + some form of iterating over it
('for' loop or '[i]map'...).
Well of course Enumerable#each is similar to map/imap, it's the same  
core principle.

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


Re: Non-blocking read with popen subprocess

2009-07-31 Thread Piet van Oostrum
> Jonathan Gardner  (JG) wrote:

>JG> On Jul 30, 5:24 pm, Dhanesh  wrote:
>>> 
>>> how can I we have a non blocking read ?

>JG> See http://docs.python.org/library/popen2.html#flow-control-issues

>JG> Note well: In the non-blocking world, you have to use select() or poll
>JG> () to get your job done.

AFAIK, on Windows these work only on sockets, not on files. I think
pipes are in the files camp.

Alternatively you can read one of the two in a different thread.

>JG> You may want to look at "communicate" (http://docs.python.org/library/
>JG> subprocess.html#popen-objects) which may be what you need.

-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem on socket programming

2009-07-31 Thread MalC0de
problem with this code solved in win32, but I didn't test it after
solved problem on gnu/linux .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket send

2009-07-31 Thread Francesco Bochicchio
On Jul 30, 10:16 pm, "Jan Kaliszewski"  wrote:
> 30-07-2009 o 12:29:24 Francesco Bochicchio  wrote:
>
> > On Jul 30, 5:52 am, NighterNet  wrote:
> >> I am trying to figure out how to send text or byte in python 3.1. I am
> >> trying to send data to flash socket to get there. I am not sure how to
> >> work it.
>
> >> buff= 'id=' , self.id , ':balive=False\n'
> >> clientSock.send(buff);
>
> > Try putting a 'b' before the constant string that you want to send:
>
> > >>> type(b'123')
> > 
>
> It's true. In python '...' literals are for strings ('str' type) and
> b'...' literals are for binary data (bytes type).
>
> Sockets' send() and recv() need binary data ('bytes' objects).
>
> > or use something like this to convert non constant strings (with only
> > ASCII characters) into bytes:
> > >>> s = "A non-constant string : %d " % n
> > >>> s
> > 'A non-constant string : 12 '
> > >>> type(s)
> > 
>
> What???
>
> 'str' type in Python 3.x IS NOT a type of "non-constant" strings and
> IS NOT a type of strings "with only ASCII characters"!
>
> 'str' type in Python 3.x *is* the type of immutable ('constant') and
> Unicode character (Unicode, not only ASCII) strings. It's the same what
> 'unicode' type in Python 2.x.
>

... unfortunate choice of words and not enough research on my part
here. WHat I meant was: if you want
to send via socket a constant string, use b"..."; if you want to send
via socket  a string that you
made out of variables (the "non-constant string" ) then you have to
convert it in bytes. Since I did not
now of the encode method, I tried other solutions, like the one-liner
using ord or using the struct
module. Obviously, encode is better.

My bad :-)

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


Re: problem on socket programming

2009-07-31 Thread MalC0de
now I've been encountered to some errors with this snippet :

import sys
from socket import *
serverHost = 'localhost'
serverPort = 50007
message = ['Hello network world']
if len(sys.argv) > 1:
serverHost = sys.argv[1]
if len(sys.argv) > 2:
message = sys.argv[2:]
sockobj = socket(AF_INET,SOCK_STREAM)
sockobj.connect((serverHost,serverPort))
for line in message:
sockobj.send(line)
data = sockobj.recv(1024)
print 'Client recieved :',repr(data)
sockobj.close()


thanks if someone can solve my problem .

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


Processes not exiting

2009-07-31 Thread ma3mju
Hi all,

I'm having trouble with multiprocessing I'm using it to speed up some
simulations, I find for large queues when the process reaches the
poison pill it does not exit whereas for smaller queues it works
without any problems. Has anyone else had this trouble? Can anyone
tell me a way around it? The code is in two files below.

Thanks

Matt

parallel.py
===
import GaussianProcessRegression as GP
import numpy as np
import networkx as nx
import pickle
import multiprocessing

# Things You Can Change

#savefiles
savefile = "wattsdata2"
graphfile = "wattsgraphs2"
#sample sizes
num_graphs = 5
num_sets_of_data = 10
#other things...
intervals = np.ceil(np.logspace(-2,1,50)*500)
noise = [np.sqrt(0.1),np.sqrt(0.01),np.sqrt(0.001),np.sqrt(0.0001)]


#generate graphs
graphs  = []
for i in range(0,num_graphs):
graphs.append(nx.watts_strogatz_graph(500,5,0.01))
#save them for later reference
filehandler = open(graphfile,'w')
pickle.dump(graphs,filehandler,-1)
filehandler.close()

#queues
easy_work_queue = multiprocessing.Queue()
hard_work_queue = multiprocessing.Queue()
result_queue = multiprocessing.Queue()
#construct the items in the hard queue
l=0
for j in range(0,len(intervals)):
for i in range(0,len(noise)):
for k in range(0,num_graphs):
if int(intervals[j]) <=4000:
easy_work_queue.put({'datapt': l,'graph': graphs
[k],'noise': noise[i],'number_of_sets_of_data':
num_sets_of_data,'number_of_data_points':int(intervals[j])})
else:
hard_work_queue.put({'datapt': l,'graph': graphs
[k],'noise': noise[i],'number_of_sets_of_data':
num_sets_of_data,'number_of_data_points':int(intervals[j])})
l+=1

#get number of cores and set the number on concurrent processes
num_hard_workers = 2
num_workers = multiprocessing.cpu_count()*1.5
easy_workers = []
hard_workers = []
#add poison pill for each worker and create the worker
for i in range(0,num_workers-num_hard_workers):
easy_work_queue.put(None)
easy_workers.append(multiprocessing.Process
(target=GP.RandomWalkGeneralizationErrorParallel,args=
(easy_work_queue,result_queue,)))
for i in range(0,num_hard_workers):
hard_work_queue.put(None)
hard_workers.append(multiprocessing.Process
(target=GP.RandomWalkGeneralizationErrorParallel,args=
(hard_work_queue,result_queue,)))

#run all workers
for worker in hard_workers:
worker.start()
for worker in easy_workers:
worker.start()
#wait for easy workers to finish
for worker in easy_workers:
worker.join()
print('worker joined')

#set off some of the easy workers on the hard work (maybe double
number of hard)
for i in range(0,num_hard_workers):
hard_work_queue.put(None)
hard_workers.append(multiprocessing.Process
(target=GP.RandomWalkGeneralizationErrorParallel,args=
(hard_work_queue,result_queue,)))
#wait for all hard workers to finish
for worker in hard_workers:
worker.join()

#construct data from the mess in the result queue

tempdata = np.zeros(l)
while not result_queue.empty():
data = result_queue.get()
tempdata[data[0]] = data[1]

finaldata = tempdata.reshape((len(intervals),len(noise),num_graphs))

np.save(savefile,finaldata)

===
GaussianProcessRegression.py
===
import CovarianceFunction as CF
import networkx as nx
import numpy as np
import scipy.linalg as sp
#fortran code from lapack-blas (hopefully when scipy updated this wont
be needed)
import dtrsv
#to use more than one core
import multiprocessing

#Currently we assume Gaussian noise TODO change to general noise
#Assume 0 mean TODO change to general mean Gaussian Process
class GaussianProcessRegression:
def __init__(self,covariance_function,sigma):
#a covariance function object defined in CovarianceFunction
class
#note this uses the parent class but any children can be used
self.C = covariance_function
#a list of pts that are known and their values
self.pts = []
self.vals = []
#the inverse of K as defined in
#...@book{coolen05:theoryofneural,
#ISBN = {0-19-853024-2},
#publisher = {Oxford University Press, USA},
#author = {Coolen, A. C. C. and K{\"{u}}hn, R. and Sollich, P.},
#title = {Theory of neural information processing systems},
#year = {2005},
#}
self.K = np.array([])
#gaussian noise variable
self.sigma = float(sigma)
self.cholL = np.array([])


def add_data_points(self,points,vals):
   #add all points to list
   self.pts += points
   self.

Re: problem on socket programming

2009-07-31 Thread Chris Rebert
On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote:
> now I've been encountered to some errors with this snippet :

And these errors are? Provide error messages and full tracebacks.

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-07-31 Thread Masklinn

On 30 Jul 2009, at 23:57 , Luis Zarrabeitia wrote:
I'd like to ask, what "container.each" is, exactly? It looks like a  
function
call (as I've learned a few posts ago), but, what are its arguments?  
How the
looping "works"? Does it receive a "code" object that it has to  
execute?
Is .each some kind of magic keyword? (This has little to do with  
python or

the current thread, so feel free to reply off-list if you want to...)


#each is simply a method that takes a function (called blocks in  
ruby). One could call it a higher-order method I guess.


It's an implementation of the concept of internal iteration: instead  
of collections yielding iterator objects, and programmers using those  
through specially-built iteration constructs (e.g. `for…in`),  
collections control iteration over themselves (the iteration is  
performed "inside" the collection, thus the "internal" part) and the  
programmer provides the operations to perform at each iterative step  
through (usually) a function.


In Python (assuming we had anonymous defs and an each method on  
lists), the following loop:


for item in some_list:
do_something(item)
do_something_else(item)

some_list.each((def (item):
do_something(item)
do_something_else(item)
))

There's absolutely nothing magic there, it's simply using anonymous  
functions and method calls (SmallTalk initiated this approach in the  
70s, and actually went much, much further than Ruby as it did away not  
only with `for…in` but also with `if…else` and `while` and a bunch of  
other stuff).


Now as IV pointed out, #each isn't the most interesting usage of  
blocks/anonymous functions (though I do like it, because it gets rid  
of no less than two keywords… even if Ruby reintroduced them as  
syntactic sugar), higher-order functions (functions which act on other  
functions) are (among other things) ways to create new control  
structures without having to extend the core language (so are lisp- 
style macros, by the way).


Of course Python does have higher-order functions (functions are first- 
class objects, so it's possible and frequent to have functions act on  
other functions), but since Python doesn't have anonymous functions  
that usage tends to be a bit too verbose beyond simple cases (also, of  
course, this kind of usages is neither in the "genes" nor in the  
stdlib).


In closing, IV has an example of how blocks make `with` unnecessary in  
Ruby (the functionality can be implemented at the library level rather  
than the language one). Generally, anonymous functions in OO languages  
are a way to inject behavior into third-party methods/objects. Kind-of  
a first-class Visitor support.


-m

PS: there's actually a bit of syntactic magic in Ruby's blocks, and  
it's one of the things I hate in the language, but it's not relevant  
to the role of blocks, and unnecessary to it: SmallTalk has no magical  
syntax).

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Steven D'Aprano
On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:

> That and the fact that I couldn't stop laughing for long enough to learn
> any more when I read in the Pragmatic Programmer's Guide that "Ruby,
> unlike less flexible languages, lets you alter the value of a constant."
> Yep, as they say "Bug" = "Undocumented feature"!

That's no different from Python's "constant by convention". We don't even 
get a compiler warning!

On the other hand, we don't have to prefix names with @ and @@, and we 
don't have the compiler trying to *guess* whether we're calling a 
function or referring to a variable.

Somebody who knows more Ruby than me should try writing the Zen of Ruby. 
Something like:

Line noise is beautiful.
Simplicity is for the simple.
Complicated just proves we're smart.
Readability only matters to the schmuck who has to maintain our code.
Special cases require breaking the rules.
In the face of ambiguity, try to guess. Go on, what could go wrong?
The more ways to do it, the better.

Although I'm sure Ruby has its good points. I'm not convinced anonymous 
code blocks are one of them though.


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


Re: If Scheme is so good why MIT drops it?

2009-07-31 Thread Hendrik van Rooyen
On Thursday 30 July 2009 03:09:14 greg wrote:
> Hendrik van Rooyen wrote:
> > And if code is data, where is Pythons ALTER statement?
>
> class Duck:
>
>def quack(self):
>  print "Quack!"
>
> def moo():
>print "Moo!"
>
> def ALTER(obj, name, TO_PROCEED_TO):
>setattr(obj, name, TO_PROCEED_TO)
>
> d = Duck()
> ALTER(d, 'quack', TO_PROCEED_TO = moo)
> d.quack()

Nice, and I really appreciate the Duck, 
but (there is always a but):

1) That is a function, not a statement.

2) The original changed a jump target address from
one address to another, and did not need the
heavy machinery of object oriented code.

So what you are doing is not quite the same.

It is actually not really needed in python, as
one can pass functions around, so that you can
call different things instead of jumping to them:

thing_to_call = moo
thing_to_call()

Does the equivalent without user level OO.

The original ALTER statement found use in building
fast state machines, where the next thing to do was
set up by the current state.  In python one simply returns
the next state.

The equivalent python code is easier to read as it is 
obvious what is happening - The COBOL source was
more obscure, as any jump could have been altered,
and you could not see that until you have read further
on in the program, where the ALTER statement was.

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Xavier Ho
On Fri, Jul 31, 2009 at 6:08 PM, Masklinn  wrote:

> ... but since Python doesn't have anonymous functions that usage
> tends to be a bit too verbose ... 
>

Sorry to interrupt, but wouldn't lambda in Python be considered as
'anonymous functions'?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-07-31 Thread Chris Rebert
On Fri, Jul 31, 2009 at 1:21 AM, Xavier Ho wrote:
> On Fri, Jul 31, 2009 at 6:08 PM, Masklinn  wrote:
>>
>> ... but since Python doesn't have anonymous functions that usage
>> tends to be a bit too verbose ... 
>
> Sorry to interrupt, but wouldn't lambda in Python be considered as
> 'anonymous functions'?

I believe "full" anonymous functions was intended by the author.
lambdas are limited to a single expression. "Full" anonymous functions
would be allowed to contain multiple statements.

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Non-blocking read with popen subprocess

2009-07-31 Thread Javier Collado
Hello,

According to my experience and from what I've read in other threads,
subprocess isn't easy to use for interactive tasks. I don't really
know, but maybe it wasn't even designed for that at all.

On the other hand, pexpect seems to work fine for interactive use and
even provides a method for nonblocking reads, so I think that you
should consider to take a look at it:
http://pexpect.sourceforge.net/pexpect.html#spawn-read_nonblocking

Best regards,
Javier

2009/7/31 Dhanesh :
> Hi ,
>
> I am trying to use subprocess popen on a windows command line
> executable with spits messages on STDOUT as well as STDIN. Code
> snippet is as below :-
> ##
> sOut=""
>    sErr=""
>    javaLoaderPath = os.path.join("c:\\","Program Files","Research In
> Motion","BlackBerry JDE 4.7.0","bin","Javaloader.exe")
>    cmd = [javaLoaderPath,'-u','load','helloworld.jad']
>    popen = subprocess.Popen
> (cmd,bufsize=256,shell=False,stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)
>    while True:
>        sErr = sErr + popen.stderr.read(64)
>        sOut = sOut + popen.stdout.read(64)--> Blocks
> here
>        if None != popen.poll():
>            break;
> ##
>
> I observed that python scripts blocks at stdout read on the other hand
> when I do not create a child stdout PIPE  say  " stdout=None" , things
> seems to be working fine.
>
> how can I we have a non blocking read ?
> And why does stdout block even where data is available to be read
> ( which seem to be apparent when stdout=None, and logs appear on
> parents STDOUT) ?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-07-31 Thread Xavier Ho
On Fri, Jul 31, 2009 at 6:25 PM, Chris Rebert  wrote:

> I believe "full" anonymous functions was intended by the author.
> lambdas are limited to a single expression. "Full" anonymous functions
> would be allowed to contain multiple statements.
>
> Cheers, but what about this:

 def goBig(x):
while True:
x = x ** 2
yield x

for result in goBig(10):
if result > 10 ** 100:
break
print result

It's a silly example, but wouldn't goBig(10) in this example be a "full
anonymous function"?

Just being curious.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-07-31 Thread Chris Rebert
On Fri, Jul 31, 2009 at 1:31 AM, Xavier Ho wrote:
> On Fri, Jul 31, 2009 at 6:25 PM, Chris Rebert  wrote:
>>
>> I believe "full" anonymous functions was intended by the author.
>> lambdas are limited to a single expression. "Full" anonymous functions
>> would be allowed to contain multiple statements.
>>
> Cheers, but what about this:
>
>  def goBig(x):
>     while True:
>     x = x ** 2
>     yield x
>
> for result in goBig(10):
>     if result > 10 ** 100:
>     break
>     print result
>
> It's a silly example, but wouldn't goBig(10) in this example be a "full
> anonymous function"?

No, because it has a name, namely "goBig"; this obviously prevents it
from being "anonymous".

For comparison, note how the function in the following example is
never given a name, and is thus anonymous:
>>> (lambda x: x+5)(6)
11

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem on socket programming

2009-07-31 Thread MalC0de
On Jul 31, 12:07 pm, Chris Rebert  wrote:
> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote:
> > now I've been encountered to some errors with this snippet :
>
> And these errors are? Provide error messages and full tracebacks.
>
> Cheers,
> Chris
> --http://blog.rebertia.com

these are errors :

Traceback (most recent call last):
  File "echo-client.py", line 11, in ", line 1, in connect
socket.error: (10061, 'Connection refused')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-07-31 Thread Xavier Ho
On Fri, Jul 31, 2009 at 6:38 PM, Chris Rebert  wrote:

> No, because it has a name, namely "goBig"; this obviously prevents it
> from being "anonymous".
>
> For comparison, note how the function in the following example is
> never given a name, and is thus anonymous:
> >>> (lambda x: x+5)(6)
> 11
>

Oh, I see. I was thinking in terms of using a variable to "give the function
a name". In that case, I understand. Thanks again for bearing me with my
random thoughts.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Does python have the capability for driver development ?

2009-07-31 Thread Hendrik van Rooyen
On Thursday 30 July 2009 15:20:45 Dave Angel wrote:

> As far as I know, nobody has yet built a microcode implementation of a
> Python VM (Virtual Machine).  Nor have I seen one for the Java VM.

Atmel has made an ARM that has support for Java Bytecode.

AT91SAM9X512 and friends (smaller versions)

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


Re: If Scheme is so good why MIT drops it?

2009-07-31 Thread greg

Hendrik van Rooyen wrote:

The COBOL source was
more obscure, as any jump could have been altered,
and you could not see that until you have read further
on in the program, where the ALTER statement was.


Well, in Python you can pretty much replace any
function with any other function, so you can
obfuscate things just as much if you really want!

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


RE: Run pyc file without specifying python path ?

2009-07-31 Thread Barak, Ron
 

> -Original Message-
> From: Dave Angel [mailto:[email protected]] 
> Sent: Thursday, July 30, 2009 16:03
> To: Barak, Ron
> Cc: 'Dave Angel'; '[email protected]'
> Subject: RE: Run pyc file without specifying python path ?
> 
> Barak, Ron wrote:
> >> -Original Message-
> >> From: Dave Angel [mailto:[email protected]]
> >> Sent: Wednesday, July 29, 2009 21:05
> >> To: Barak, Ron
> >> Cc: '[email protected]'
> >> Subject: Re: Run pyc file without specifying python path ?
> >>
> >> Barak, Ron wrote:
> >> 
> >>> Hi,
> >>>
> >>> I wanted to make a python byte-code file executable,
> >>>   
> >> expecting to be able to run it without specifying "python" on the 
> >> (Linux bash) command line.
> >> 
> >>> So, I wrote the following:
> >>>
> >>> [r...@vmlinux1 python]# cat test_pyc.py #!/usr/bin/env python
> >>>
> >>> print "hello"
> >>> [r...@vmlinux1 python]#
> >>>
> >>> and made its pyc file executable:
> >>>
> >>> [r...@vmlinux1 python]# ls -ls test_pyc.pyc
> >>> 4 -rwxr-xr-x  1 root root 106 Jul 29 14:22 test_pyc.pyc
> >>> [r...@vmlinux1 python]#
> >>>
> >>> So, I see:
> >>>
> >>> [r...@vmlinux1 python]# file test_pyc.py*
> >>> test_pyc.py:  a python script text executable
> >>> test_pyc.pyc: python 2.3 byte-compiled
> >>> [r...@vmlinux1 python]#
> >>>
> >>> If I try to do the following, no problem:
> >>>
> >>> [r...@vmlinux1 python]# python test_pyc.pyc hello
> >>> [r...@vmlinux1 python]#
> >>>
> >>> However, the following fails:
> >>>
> >>> [r...@vmlinux1 python]# ./test_pyc.pyc
> >>> -bash: ./test_pyc.pyc: cannot execute binary file
> >>> [r...@vmlinux1 python]#
> >>>
> >>> Is there a way to run a pyc file without specifying the
> >>>   
> >> python path ?
> >> 
> >>> Bye,
> >>> Ron.
> >>>
> >>>   
> >>>   
> >> I don't currently run Unix, but I think I know the problem.
> >>
> >> In a text file, the shell examines the first line, and if 
> it begins 
> >> #!
> >> it's assumed to point to the executable of an interpreter for that 
> >> text file.  Presumably the same trick doesn't work for a .pyc file.
> >>
> >> Why not write a trivial wrapper.py file, don't compile it, and let 
> >> that invoke the main code in the .pyc file?
> >>
> >> Then make wrapper.py executable, and you're ready to go.
> >>
> >> DaveA
> >>
> >>
> >> 
> >
> > Hi Dave,
> > Your solution sort of defeats my intended purpose (sorry 
> for not divulging my 'hidden agenda').
> > I wanted my application to "hide" the fact that it's a 
> python script, and look as much as possible like it's a 
> compiled program.
> > The reason I don't just give my user a py file, is that I 
> don't want a cleaver user to change the innards of the script.
> > On the other hand, I don't want to make a compiled 
> (freezed?) version of the application, because it'll grow the 
> resulting file significantly, and I don't have the experience 
> to know how it will run on different Linuxes.
> > Bye,
> > Ron. 
> >   

Hi Dave,

> Most of the other answers basically paraphrased my suggestion 
> of making a wrapper file, not compiling it, and making it 
> executable.  With that 
> approach, the user just types   "wrapper.py" on his command line.
> 
> And wrapper.py only needs two lines, a shebang, and an 
> import, no big deal if the user modifies it.  The rest of 
> your code can be .pyc files.
> 
> Steven makes some good points.  You have to define what level 
> of clever you're protecting from.  A determined hacker will 

The clever I try to guard against is not at hacker level, just the curios 
worker.

> get in no matter what you do, unless you want to ship the 
> program in a proprietary embedded system, encased in epoxy.  
> Further, if you have an extension of .py or .pyc, a 
> knowledgeable hacker will know it's probably python.

Granted, but the user will know that from the user manual anyway :-)

> 
> You imply you want it to run unmodifed on multiple unknown 
> Linux versions.  I think that lets out binfmt solutions.  

Actually, no: I know the set of Linuxes my users are likely to have.

> That means you need to test and support not only multiple 
> Linux implementations, but multiple Python versions, because 
> who knows what the user may have installed.  I think you need 
> to rethink your support strategy.  And maybe concentrate on 
> being able to detect change, rather than prevent it.
> 
> DaveA
> 
> 

Thanks for the helpful suggestions.
Ron.
 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Use existing IE cookie

2009-07-31 Thread KB
Winner, winner, chicken dinner... resolved.

http://wwwsearch.sourceforge.net/mechanize/doc.html

import mechanize
cj = mechanize.MSIECookieJar(delayload=True)
cj.load_from_registry()  # finds cookie index file from registry

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


Re: Processes not exiting

2009-07-31 Thread ma3mju
Sorry

###fortran
call###

 is meant to be

###fortran call###

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


Re: How to "gunzip-iterate" over a file?

2009-07-31 Thread kj


Robert, Paul, thanks.  That was just what I was looking for.

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Jean-Michel Pichavant

r wrote:

The purpose of his thread was to get feedback on how Python
and Ruby ideas could be cumulated into the best high level language.
And being that i am the BDFL of the "Confessions of a Python Fanboy"
thread, you have my personal permission to continue on with this
subject matter...,

  
Challenging even the most accepted mechanisms of a language is a proof 
of intelligence.


Descartes's doubt method :
"The basic strategy of /Descartes/ 
's method of doubt 
 is to defeat skepticism 
 on its own ground. Begin 
by doubting the truth of everything—not only the evidence 
 of the senses and the 
more extravagant cultural presuppositions, but even the fundamental 
process of reasoning itself. If any particular truth about the world can 
survive this extreme skeptical challenge, then it must be truly 
indubitable and therefore a perfectly certain foundation for knowledge"


So let's make the method call parenthesis a "truly indubitable and 
therefore a perfectly certain foundation".


Those who want to remove the parenthesis on python method calls raise 
the hand !


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


Re: How to force SAX parser to ignore encoding problems

2009-07-31 Thread Łukasz
On 31 Lip, 09:28, Łukasz  wrote:
> Hi,
> I have a problem with my XML parser (created with libraries from
> xml.sax package). When parser finds a invalid character (in CDATA
> section) for example ,

After sending this message I noticed that example invalid characters
are not displaying on some platforms :)

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


file comparison

2009-07-31 Thread learner learner
Hi all,

I want to compare two text files line by line and eliminate the
matching/repeated line and store the unmatched/leftout lines into a third
file or overwrite into one of them.

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


Re: file comparison

2009-07-31 Thread Xavier Ho
On Fri, Jul 31, 2009 at 7:25 PM, learner learner wrote:

> Hi all,
>
> I want to compare two text files line by line and eliminate the
> matching/repeated line and store the unmatched/leftout lines into a third
> file or overwrite into one of them.
>

Sounds like homework to me. Why not look into:

- the file() function and its methods.

It's in the documentation.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python 2.6 in shared/VPS environment

2009-07-31 Thread northof40
Ignore this question. Managed to get Amazon Web Services going and
have installed Python 2.6 on there. Thanks for your eyeballs time.

On Jul 31, 7:09 pm, northof40  wrote:
> Hi - I'd really like to have access to Python 2.6 to try something
> out. It needs to be on Linux/Unix machine. I don't mind paying but
> whichever way I turn I find Python 2.4 is the standard installation.
>
> Anyone know of anyone who offers this out of the box ? Or got any
> smart way of achieving it ?
>
> thanks
>
> R.

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


Re: file comparison

2009-07-31 Thread Gerhard Häring
learner learner wrote:
> Hi all,
>  
> I want to compare two text files line by line and eliminate the
> matching/repeated line and store the unmatched/leftout lines into a
> third file or overwrite into one of them.

gl & hf!

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


Re: file comparison

2009-07-31 Thread Chris Rebert
On Fri, Jul 31, 2009 at 2:25 AM, learner learner wrote:
> Hi all,
>
> I want to compare two text files line by line and eliminate the
> matching/repeated line and store the unmatched/leftout lines into a third
> file or overwrite into one of them.

See the `difflib` module:
http://docs.python.org/library/difflib.html

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Generators through the C API

2009-07-31 Thread Duncan Booth
Lucas P Melo  wrote:

> Hello, I'm a total noob about the C API. Is there any way to create a 
> generator function using the C API? I couldn't find anything like the 
> 'yield' keyword in it.
> 
> Thanks in advance.

You define a new type with tp_flags including Py_TPFLAGS_HAVE_ITER. 
Anything that would be a local variable in your generator needs to become 
an attribute in the type.

The tp_init initialization function should contain all the code up to the 
first yield, tp_iter should return self and tp_iternext should execute code 
up to the next yield.

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Masklinn

On 31 Jul 2009, at 10:25 , Chris Rebert wrote:
On Fri, Jul 31, 2009 at 1:21 AM, Xavier Ho  
wrote:
On Fri, Jul 31, 2009 at 6:08 PM, Masklinn   
wrote:


... but since Python doesn't have anonymous functions that  
usage

tends to be a bit too verbose ... 


Sorry to interrupt, but wouldn't lambda in Python be considered as
'anonymous functions'?


I believe "full" anonymous functions was intended by the author.
lambdas are limited to a single expression.
Yes, and they're limited to a single *expression*, so before Python 3,  
lambda: print "foo" is out. Likewise, it isn't possible to have an if/ 
else statement within a lambda (though a ternary is ok), or a with, …  
since Python statements aren't expressions.

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Iain King
On Jul 31, 8:28 am, Steven D'Aprano  wrote:
> On Thu, 30 Jul 2009 18:06:31 -0500, Robert Kern wrote:
> > On 2009-07-30 16:44, r wrote:
> >> On Jul 30, 4:29 pm, Emmanuel Surleau wrote:
>  1.) No need to use "()" to call a function with no arguments. Python
>  -->  "obj.m2().m3()" --ugly
>     Ruby -->  "obj.m1.m2.m3"  -- sweeet!
>  Man, i must admit i really like this, and your code will look so much
>  cleaner.
> >>> It has benefits - code does look better. It has also significant cons
> >>> - it is ambiguous.
> >>> For instance:
>
> >>> a = b
>
> >>> Is b a variable or a method called without parameter?
>
> >> Hello Emanuel,
> >> Again, who so ever names a method with such a non-descriptive name will
> >> get whats coming to him. And if you did for some reason use such a
> >> cryptic name as "b", do yourself (and everyone else) a favor and follow
> >> it with "()" to denote the method call. Remember when something is
> >> optional that means you have an option to use it OR not use it.
>
> > I believe his point is that it is ambiguous to the compiler, not humans
> > reading the code. Python functions and methods are first class objects.
> > They can be passed around. If they were auto-called, then you could not
> > do this.
>
> Oh my, "r" is still around is he??? And now he's singing the praises of
> Ruby, the language which he treated as the Devil's Spawn when he first
> arrived. That's hilarious.
>
> But back on topic... "r" has missed the point. It's not that a=b is hard
> to understand because b is a poor name. The example could have been:
>
> def factory_function():
>     magic = time.time()  # or whatever
>     def inner():
>         return magic
>     return inner
>
> my_function = factory_function
>
> It's still ambiguous. Does the programmer intend my_function to become
> factory_function itself, or the output of factory_function?

Not only that - does 'return inner' return the function inner or the
result of function inner?

How does ruby pass a function as an object?

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


Re: problem on socket programming

2009-07-31 Thread Piet van Oostrum
> MalC0de  (M) wrote:

>M> On Jul 31, 12:07 pm, Chris Rebert  wrote:
>>> On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote:
>>> > now I've been encountered to some errors with this snippet :
>>> 
>>> And these errors are? Provide error messages and full tracebacks.
>>> 
>>> Cheers,
>>> Chris
>>> --http://blog.rebertia.com

>M> these are errors :

>M> Traceback (most recent call last):
>M>   File "echo-client.py", line 11, in M> sockobj.connect((serverHost,serverPort))
>M>   File "", line 1, in connect
>M> socket.error: (10061, 'Connection refused')

Is your server running?
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Processes not exiting

2009-07-31 Thread Piet van Oostrum
> ma3mju  (m) wrote:

>m> Hi all,
>m> I'm having trouble with multiprocessing I'm using it to speed up some
>m> simulations, I find for large queues when the process reaches the
>m> poison pill it does not exit whereas for smaller queues it works
>m> without any problems. Has anyone else had this trouble? Can anyone
>m> tell me a way around it? The code is in two files below.

How do you know it doesn't exit. You haven't shown any of your output.

I did discover a problem in your code, but it should cause an exception:

>m> #set off some of the easy workers on the hard work (maybe double
>m> number of hard)
>m> for i in range(0,num_hard_workers):
>m> hard_work_queue.put(None)
>m> hard_workers.append(multiprocessing.Process
>m> (target=GP.RandomWalkGeneralizationErrorParallel,args=
>m> (hard_work_queue,result_queue,)))
>m> #wait for all hard workers to finish
>m> for worker in hard_workers:
>m> worker.join()

Here you create new hard workers, but you never start them. The join
should then give an exception when it reaches these.
-- 
Piet van Oostrum 
URL: http://pietvanoostrum.com [PGP 8DAE142BE17999C4]
Private email: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-07-31 Thread Masklinn

On 31 Jul 2009, at 11:54 , Iain King wrote:

On Jul 31, 8:28 am, Steven D'Aprano  wrote:

On Thu, 30 Jul 2009 18:06:31 -0500, Robert Kern wrote:

On 2009-07-30 16:44, r wrote:
On Jul 30, 4:29 pm, Emmanuel Surleau  
wrote:
1.) No need to use "()" to call a function with no arguments.  
Python

-->  "obj.m2().m3()" --ugly
   Ruby -->  "obj.m1.m2.m3"  -- sweeet!
Man, i must admit i really like this, and your code will look  
so much

cleaner.
It has benefits - code does look better. It has also significant  
cons

- it is ambiguous.
For instance:



a = b



Is b a variable or a method called without parameter?



Hello Emanuel,
Again, who so ever names a method with such a non-descriptive  
name will

get whats coming to him. And if you did for some reason use such a
cryptic name as "b", do yourself (and everyone else) a favor and  
follow

it with "()" to denote the method call. Remember when something is
optional that means you have an option to use it OR not use it.


I believe his point is that it is ambiguous to the compiler, not  
humans
reading the code. Python functions and methods are first class  
objects.
They can be passed around. If they were auto-called, then you  
could not

do this.


Oh my, "r" is still around is he??? And now he's singing the  
praises of
Ruby, the language which he treated as the Devil's Spawn when he  
first

arrived. That's hilarious.

But back on topic... "r" has missed the point. It's not that a=b is  
hard

to understand because b is a poor name. The example could have been:

def factory_function():
magic = time.time()  # or whatever
def inner():
return magic
return inner

my_function = factory_function

It's still ambiguous. Does the programmer intend my_function to  
become

factory_function itself, or the output of factory_function?


Not only that - does 'return inner' return the function inner or the
result of function inner?

How does ruby pass a function as an object?
Ruby doesn't have functions as such. It has methods and blocks (which  
would be anonymous functions). `def` always creates methods.


To get a (bound) method object, you simply call `method` on an  
instance e.g. `foo.method(:bar)` is equivalent to `foo.bar` in Python  
(it returns a bound method object without calling it).


Blocks are usually created as part of the calls: `foo.bar  
{do_something}` the part between braces (braces included) is a block,  
which will be passed to the method `bar`. Sadly, Ruby's blocks are  
magical syntax (doesn't mean they have to be, in Smalltalk there's  
nothing magical about blocks for instance) so you can't just do `foo =  
{do_something}`, you have to turn them into `Proc` objects with the  
`proc` constructor (or `lambda`, it's equivalent and looks better so  
I'll use that): `foo = lambda {do_something}`. If you use the magical  
syntax previously shown, Ruby handles the turning of a block into an  
actual `Proc` instance.


And since Ruby doesn't have a `()` operator, it uses a method instead  
(`#call`), so you simply do `foo.call` to execute the proc and get its  
value.


All in all, much like Smalltalk, Ruby tends not to favor raw functions  
the way Python does, so a direct translation of the Python code  
doesn't make much sense.

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


Re: Processes not exiting

2009-07-31 Thread MRAB

ma3mju wrote:

Hi all,

I'm having trouble with multiprocessing I'm using it to speed up some
simulations, I find for large queues when the process reaches the
poison pill it does not exit whereas for smaller queues it works
without any problems. Has anyone else had this trouble? Can anyone
tell me a way around it? The code is in two files below.


[snip]


#get number of cores and set the number on concurrent processes
num_hard_workers = 2
num_workers = multiprocessing.cpu_count()*1.5
easy_workers = []
hard_workers = []
#add poison pill for each worker and create the worker
for i in range(0,num_workers-num_hard_workers):
easy_work_queue.put(None)
easy_workers.append(multiprocessing.Process
(target=GP.RandomWalkGeneralizationErrorParallel,args=
(easy_work_queue,result_queue,)))
for i in range(0,num_hard_workers):
hard_work_queue.put(None)
hard_workers.append(multiprocessing.Process
(target=GP.RandomWalkGeneralizationErrorParallel,args=
(hard_work_queue,result_queue,)))


You have 2 hard workers and ceil(CPU_count * 1.5) - 2 easy workers.
What if the number of CPUs was 1? That would give 2 hard and 0 easy!

Also, I recommend that you put only 1 'poison pill' in each queue and
have the workers put it back when they see it.

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


Re: file comparison

2009-07-31 Thread Hendrik van Rooyen
On Friday 31 July 2009 11:25:17 learner learner wrote:
> Hi all,
>
> I want to compare two text files line by line and eliminate the
> matching/repeated line and store the unmatched/leftout lines into a third
> file or overwrite into one of them.

This is not as simple as it seems.

You will probably be better off using diff, or,
if it is a learning exercise, studying its source.

- Hendrik

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


Re: problem on socket programming

2009-07-31 Thread Diez B. Roggisch

MalC0de schrieb:

On Jul 31, 12:07 pm, Chris Rebert  wrote:

On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote:

now I've been encountered to some errors with this snippet :

And these errors are? Provide error messages and full tracebacks.

Cheers,
Chris
--http://blog.rebertia.com


these are errors :

Traceback (most recent call last):
  File "echo-client.py", line 11, in ", line 1, in connect
socket.error: (10061, 'Connection refused')


Can you ping and telnet to the port on the desired server? Firewalls can 
be an issue here.


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


Re: problem on socket programming

2009-07-31 Thread Diez B. Roggisch

MalC0de schrieb:

On Jul 31, 12:07 pm, Chris Rebert  wrote:

On Fri, Jul 31, 2009 at 1:02 AM, MalC0de wrote:

now I've been encountered to some errors with this snippet :

And these errors are? Provide error messages and full tracebacks.

Cheers,
Chris
--http://blog.rebertia.com


these are errors :

Traceback (most recent call last):
  File "echo-client.py", line 11, in ", line 1, in connect
socket.error: (10061, 'Connection refused')


Can you ping and telnet to the port on the desired server? Firewalls can 
be an issue here.


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


Re: Confessions of a Python fanboy

2009-07-31 Thread Bruno Desthuilliers

Steven D'Aprano a écrit :

On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:


That and the fact that I couldn't stop laughing for long enough to learn
any more when I read in the Pragmatic Programmer's Guide that "Ruby,
unlike less flexible languages, lets you alter the value of a constant."
Yep, as they say "Bug" = "Undocumented feature"!


That's no different from Python's "constant by convention".


Well, at least Python doesn't pretend to have real symbolic constants - 
we all know it's only a convention !-)


We don't even 
get a compiler warning!


Of course - from the compiler's POV, it's only usual rebinding.


On the other hand, we don't have to prefix names with @ and @@,


Nope, we have to prefix them with 'self' or 'cls' (or even 
'self.__class__').


and we 
don't have the compiler trying to *guess* whether we're calling a 
function or referring to a variable.


Please re-read a bit more carefully - it's *all* method call. Python is 
'uniform' in obj.name is always an attribute lookup (methods being 
attributes), Ruby is uniform in that 'obj.name' is always a method call.


Somebody who knows more Ruby than me should try writing the Zen of Ruby. 
Something like:


(snip childish parody of Python Zen)

Steven, is that any useful ?



Although I'm sure Ruby has its good points. I'm not convinced anonymous 
code blocks are one of them though.


Ruby's code blocks come from Smalltalk, where they are an absolute 
necessity since message passing (which code blocks are part of) is the 
*only* builtin control flow in Smalltalk - so you just *need* this 
construction to provide branching and iterations.


Wether it makes sense to have code blocks in Ruby is another question 
since Ruby does provide traditional control flow features, but then one 
could question Python's "lambda" (hem...) too.

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


Re: Confused About Python Loggin

2009-07-31 Thread Jannik Sundø
Hi all. I think I fixed this problem by setting fileLogger.propagate =  
0. Otherwise it will propagate up to the root logger, which outputs to  
stdout, as far as I can understand.



On 31 Jul 2009, at 01:17, Jannik Sundø wrote:

Dear all, I am quite confused about the Python logging. I have read  
and re-read the Python documentation for the Python logging module  
and googled, but to no avail. I simply want one logger to log to a  
file and another logger to log to the console. Neither should log  
the other's messages. The code below causes fileLogger to log to  
BOTH a file and the console, how come? It seems to me that a call  
such as "consoleHandler = logging.StreamHandler()" affects other  
loggers, which I do not want. Also, if I do not do  
logging.basicConfig(level=logging.INFO) in one of the classes of my  
application, the logging does not work. Any ideas how come?


Thanks a lot for any help! :)

fileHandler = logging.FileHandler('../../logs/log.txt')
fileLogger = logging.getLogger('TESTHARNESSFILE')
fileLogger.addHandler(fileHandler)
fileLogger.setLevel(logging.INFO)
consoleHandler = logging.StreamHandler()
logger = logging.getLogger('TESTHARNESS')
logger.addHandler(consoleHandler)
logger.setLevel(logging.INFO)


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


Re: problem on socket programming

2009-07-31 Thread MalC0de

you want to say shall i  disable the firewall, there's no specific
firewall or IPS/IDS system out there, this is just windows xp firewall
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: problem on socket programming

2009-07-31 Thread Diez B. Roggisch

MalC0de schrieb:

you want to say shall i  disable the firewall, there's no specific
firewall or IPS/IDS system out there, this is just windows xp firewall


Which is a firewall, don't you agree? So it could well be the reason for 
the problems you see.


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


Re: Confessions of a Python fanboy

2009-07-31 Thread Masklinn

On 31 Jul 2009, at 13:38 , Bruno Desthuilliers wrote:

Steven D'Aprano a écrit :

On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:
That and the fact that I couldn't stop laughing for long enough to  
learn

any more when I read in the Pragmatic Programmer's Guide that "Ruby,
unlike less flexible languages, lets you alter the value of a  
constant."

Yep, as they say "Bug" = "Undocumented feature"!

That's no different from Python's "constant by convention".
Ruby's code blocks come from Smalltalk, where they are an absolute  
necessity since message passing (which code blocks are part of) is  
the *only* builtin control flow in Smalltalk - so you just *need*  
this construction to provide branching and iterations.


I'm not so sure about the way you say it. I'm pretty sure  
"traditional" flow control structures preceded Smalltalk by a pair of  
decades so it's not that Smalltalk's designers found it necessary to  
use blocks & messages, but that they understood blocks & messages  
could trivially replace most control structures (making *those*  
unnecessary), making the core language simpler and more flexible.


In other words, I see it the other way around.

Wether it makes sense to have code blocks in Ruby is another  
question since Ruby does provide traditional control flow features
Well it does at least allow for the creation of new flow control  
structures in library land when the existing ones aren't enough (e.g.  
allows Ruby not to require the introduction of a `with` statement).  
Though Ruby's blocks are nowhere near as flexible as Smalltalk's.

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


Re: Confused About Python Loggin

2009-07-31 Thread Vinay Sajip
On Jul 31, 12:41 pm, Jannik Sundø  wrote:
> Hi all. I think I fixed this problem by setting fileLogger.propagate =  
> 0. Otherwise it will propagate up to the root logger, which outputs to  
> stdout, as far as I can understand.
>

Only if you specifically configure it to do so - for example, calling
basicConfig() configures the root logger (if it has no handlers) by
adding a StreamHandler which outputs to sys.stderr.

You shouldn't need to set propagate - the script in Peter's reply
appears to do what you need.

Regards,

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


Re: problem on socket programming

2009-07-31 Thread MalC0de
no, I disabled my firewall, no problem with even enabled firewall ...
no one can help !?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: file comparison

2009-07-31 Thread Dave Angel

Hendrik van Rooyen wrote:

On Friday 31 July 2009 11:25:17 learner learner wrote:
  

Hi all,

I want to compare two text files line by line and eliminate the
matching/repeated line and store the unmatched/leftout lines into a third
file or overwrite into one of them.



This is not as simple as it seems.

You will probably be better off using diff, or,
if it is a learning exercise, studying its source.

- Hendrik


  


It is not only "not simple," it's totally underspecified.


Learner:

If this is a homework assignment, you should read the whole assignment, 
and see what it says.  Without knowing something about the text files, I 
couldn't even guess what the real goal is.


For example, if the text files are really text (e.g. a letter to 
grandma), then we want a context-sensitive comparison, the kind that 
Windiff, RCSDiff, DiffMerge, and others can do.


If the text files are each a list of items, with order and duplication 
irrelevant, then your best bet is probably to build a set from each, and 
difference the sets.


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


Re: Confused About Python Loggin

2009-07-31 Thread Jean-Michel Pichavant

Jannik Sundø wrote:
Hi all. I think I fixed this problem by setting fileLogger.propagate = 
0. Otherwise it will propagate up to the root logger, which outputs to 
stdout, as far as I can understand.



On 31 Jul 2009, at 01:17, Jannik Sundø wrote:

Dear all, I am quite confused about the Python logging. I have read 
and re-read the Python documentation for the Python logging module 
and googled, but to no avail. I simply want one logger to log to a 
file and another logger to log to the console. Neither should log the 
other's messages. The code below causes fileLogger to log to BOTH a 
file and the console, how come? It seems to me that a call such as 
"consoleHandler = logging.StreamHandler()" affects other loggers, 
which I do not want. Also, if I do not do 
logging.basicConfig(level=logging.INFO) in one of the classes of my 
application, the logging does not work. Any ideas how come?


Thanks a lot for any help! :)

fileHandler = logging.FileHandler('../../logs/log.txt')
fileLogger = logging.getLogger('TESTHARNESSFILE')
fileLogger.addHandler(fileHandler)
fileLogger.setLevel(logging.INFO)
consoleHandler = logging.StreamHandler()
logger = logging.getLogger('TESTHARNESS')
logger.addHandler(consoleHandler)
logger.setLevel(logging.INFO)


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


Please do not top post :o)

Are sure you have used the code provided above ?
Loggers only propagate events to its parent.

fileLogger = logging.getLogger('TESTHARNESSFILE"
logger = logging.getLogger('TESTHARNESS')
Those one have no parent relationship.

However these one have parent relationship:
fileLogger = logging.getLogger('TESTHARNESS.FILE") # note the dotted name
logger = logging.getLogger('TESTHARNESS')


Or maybe you have added a handler to the root logger...

Using the propagate attribute is correct only between 2 loggers that are 
meant to be parents.


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


Re: problem on socket programming

2009-07-31 Thread MRAB

MalC0de wrote:

no, I disabled my firewall, no problem with even enabled firewall ...
no one can help !?


I hope you disconnected your computer from the internet before disabling
the firewall...

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Bruno Desthuilliers

Masklinn a écrit :

On 31 Jul 2009, at 13:38 , Bruno Desthuilliers wrote:

Steven D'Aprano a écrit :

On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:
That and the fact that I couldn't stop laughing for long enough to 
learn

any more when I read in the Pragmatic Programmer's Guide that "Ruby,
unlike less flexible languages, lets you alter the value of a 
constant."

Yep, as they say "Bug" = "Undocumented feature"!

That's no different from Python's "constant by convention".
Ruby's code blocks come from Smalltalk, where they are an absolute 
necessity since message passing (which code blocks are part of) is the 
*only* builtin control flow in Smalltalk - so you just *need* this 
construction to provide branching and iterations.



I'm not so sure about the way you say it.


I'm not sure about the way you understand it !-)

I'm pretty sure "traditional" 
flow control structures preceded Smalltalk by a pair of decades


Yes, of course - and that's not the point. What's important is that:


so it's 
not that Smalltalk's designers found it necessary to use blocks & 
messages, but that they understood blocks & messages could trivially 
replace most control structures (making *those* unnecessary), making the 
core language simpler and more flexible.


Exactly.


In other words, I see it the other way around.


My wording may have been a bit confusing, indeed. It was implied (but 
perhaps a bit unclear) that restricting control structures to messages 
and blocks was a design choice.


Wether it makes sense to have code blocks in Ruby is another question 
since Ruby does provide traditional control flow features

>
Well it does at least allow for the creation of new flow control 
structures in library land when the existing ones aren't enough (e.g. 
allows Ruby not to require the introduction of a `with` statement). 


Yeps. But then other "traditionnal" control flow features become redundant.


Though Ruby's blocks are nowhere near as flexible as Smalltalk's.

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


Re: problem on socket programming

2009-07-31 Thread Processor-Dev1l
On Jul 31, 2:37 pm, MalC0de  wrote:
> no, I disabled my firewall, no problem with even enabled firewall ...
> no one can help !?

Connection refused... connection was refused by the listening socket,
it could also be due to the settings of your service, if it accepts
only from localhost to localhost (127.0.0.1 to 127.0.0.1) then other
connections will not be granted. Try to find some socket samples on
python.org (there are plenty of tip and tricks for using sockets)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Run pyc file without specifying python path ?

2009-07-31 Thread Processor-Dev1l
On Jul 30, 4:47 pm, "Barak, Ron"  wrote:
> > -Original Message-
> > From: Dave Angel [mailto:[email protected]]
> > Sent: Thursday, July 30, 2009 16:03
> > To: Barak, Ron
> > Cc: 'Dave Angel'; '[email protected]'
> > Subject: RE: Run pyc file without specifying python path ?
>
> > Barak, Ron wrote:
> > >> -Original Message-
> > >> From: Dave Angel [mailto:[email protected]]
> > >> Sent: Wednesday, July 29, 2009 21:05
> > >> To: Barak, Ron
> > >> Cc: '[email protected]'
> > >> Subject: Re: Run pyc file without specifying python path ?
>
> > >> Barak, Ron wrote:
>
> > >>> Hi,
>
> > >>> I wanted to make a python byte-code file executable,
>
> > >> expecting to be able to run it without specifying "python" on the
> > >> (Linux bash) command line.
>
> > >>> So, I wrote the following:
>
> > >>> [r...@vmlinux1 python]# cat test_pyc.py #!/usr/bin/env python
>
> > >>> print "hello"
> > >>> [r...@vmlinux1 python]#
>
> > >>> and made its pyc file executable:
>
> > >>> [r...@vmlinux1 python]# ls -ls test_pyc.pyc
> > >>> 4 -rwxr-xr-x  1 root root 106 Jul 29 14:22 test_pyc.pyc
> > >>> [r...@vmlinux1 python]#
>
> > >>> So, I see:
>
> > >>> [r...@vmlinux1 python]# file test_pyc.py*
> > >>> test_pyc.py:  a python script text executable
> > >>> test_pyc.pyc: python 2.3 byte-compiled
> > >>> [r...@vmlinux1 python]#
>
> > >>> If I try to do the following, no problem:
>
> > >>> [r...@vmlinux1 python]# python test_pyc.pyc hello
> > >>> [r...@vmlinux1 python]#
>
> > >>> However, the following fails:
>
> > >>> [r...@vmlinux1 python]# ./test_pyc.pyc
> > >>> -bash: ./test_pyc.pyc: cannot execute binary file
> > >>> [r...@vmlinux1 python]#
>
> > >>> Is there a way to run a pyc file without specifying the
>
> > >> python path ?
>
> > >>> Bye,
> > >>> Ron.
>
> > >> I don't currently run Unix, but I think I know the problem.
>
> > >> In a text file, the shell examines the first line, and if
> > it begins
> > >> #!
> > >> it's assumed to point to the executable of an interpreter for that
> > >> text file.  Presumably the same trick doesn't work for a .pyc file.
>
> > >> Why not write a trivial wrapper.py file, don't compile it, and let
> > >> that invoke the main code in the .pyc file?
>
> > >> Then make wrapper.py executable, and you're ready to go.
>
> > >> DaveA
>
> > > Hi Dave,
> > > Your solution sort of defeats my intended purpose (sorry
> > for not divulging my 'hidden agenda').
> > > I wanted my application to "hide" the fact that it's a
> > python script, and look as much as possible like it's a
> > compiled program.
> > > The reason I don't just give my user a py file, is that I
> > don't want a cleaver user to change the innards of the script.
> > > On the other hand, I don't want to make a compiled
> > (freezed?) version of the application, because it'll grow the
> > resulting file significantly, and I don't have the experience
> > to know how it will run on different Linuxes.
> > > Bye,
> > > Ron.
>
> Hi Dave,
>
> > Most of the other answers basically paraphrased my suggestion
> > of making a wrapper file, not compiling it, and making it
> > executable.  With that
> > approach, the user just types   "wrapper.py" on his command line.
>
> > And wrapper.py only needs two lines, a shebang, and an
> > import, no big deal if the user modifies it.  The rest of
> > your code can be .pyc files.
>
> > Steven makes some good points.  You have to define what level
> > of clever you're protecting from.  A determined hacker will
>
> The clever I try to guard against is not at hacker level, just the curios 
> worker.
>
> > get in no matter what you do, unless you want to ship the
> > program in a proprietary embedded system, encased in epoxy.  
> > Further, if you have an extension of .py or .pyc, a
> > knowledgeable hacker will know it's probably python.
>
> Granted, but the user will know that from the user manual anyway :-)
>
>
>
> > You imply you want it to run unmodifed on multiple unknown
> > Linux versions.  I think that lets out binfmt solutions.  
>
> Actually, no: I know the set of Linuxes my users are likely to have.
>
> > That means you need to test and support not only multiple
> > Linux implementations, but multiple Python versions, because
> > who knows what the user may have installed.  I think you need
> > to rethink your support strategy.  And maybe concentrate on
> > being able to detect change, rather than prevent it.
>
> > DaveA
>
> Thanks for the helpful suggestions.
> Ron.

how can a compiled python file (pyc) can defend your code against
clever-user? Try command cat "yourcode.pyc" and you will is how
"hardly" it can be read or modified :).
The most easy way how to distribute such file is with some shell batch
containing
#!/usr/bin/sh
python ./lib/myscript.pyc

the topology of the app would be a shell script called simply run, or
name of your app and something like that and a folder called lib where
your pyc file is stored, just make the script executable (chmod a+x
shellfile or chmod 75

Re: Confessions of a Python fanboy

2009-07-31 Thread Masklinn

On 31 Jul 2009, at 15:12 , Bruno Desthuilliers wrote:

Masklinn a écrit :

On 31 Jul 2009, at 13:38 , Bruno Desthuilliers wrote:

Steven D'Aprano a écrit :

On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:
That and the fact that I couldn't stop laughing for long enough  
to learn
any more when I read in the Pragmatic Programmer's Guide that  
"Ruby,
unlike less flexible languages, lets you alter the value of a  
constant."

Yep, as they say "Bug" = "Undocumented feature"!

That's no different from Python's "constant by convention".
Ruby's code blocks come from Smalltalk, where they are an absolute  
necessity since message passing (which code blocks are part of) is  
the *only* builtin control flow in Smalltalk - so you just *need*  
this construction to provide branching and iterations.



[misunderstandings on my part/clarifications on yours]
Well it does at least allow for the creation of new flow control  
structures in library land when the existing ones aren't enough  
(e.g. allows Ruby not to require the introduction of a `with`  
statement).


Yeps. But then other "traditionnal" control flow features become  
redundant.
They can be anyway: Ruby doesn't deprecate most control flows as the  
actual usages of blocks are a bit restricted (cannot be used for  
`while` as it would require the serialization to a Proc, and Ruby's  
syntax doesn't allow sending multiple blocks to a method so `if…else`  
is out as well). And I assume they reintroduced the for…in sugar to  
ease the transition from more traditional languages (using #each and  
others seems the suggested collection iterators across the community).

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


Re: fast video encoding

2009-07-31 Thread Scott David Daniels

gregorth wrote:

for a scientific application I need to save a video stream to disc for
further post processing. My cam can deliver 8bit grayscale images with
resolution 640x480 with a framerate up to 100Hz, this is a data rate
of 30MB/s. Writing the data uncompressed to disc hits the data
transfer limits of my current system and creates huge files. Therefore
I would like to use video compression, preferably fast and high
quality to lossless encoding. Final file size is not that important.

Well, it sounds like it better be enough to affect bandwidth.


I am a novice with video encoding. I found that few codecs support
gray scale images. Any hints to take advantage of the fact that I only
have gray scale images?


You might try to see if there is a primitive .MNG encoder around.
That could give you lossless with perhaps enough compression to make
you happy, and I'm sure it will handle the grayscale.

.MNG is pictures only, but that doesn't hurt you in the least.

--Scott David Daniels
[email protected]
--
http://mail.python.org/mailman/listinfo/python-list


Re: problem on socket programming

2009-07-31 Thread MalC0de
none of you can't interpret it ? do you have errors like me !?
-- 
http://mail.python.org/mailman/listinfo/python-list


supported versions policy?

2009-07-31 Thread Gary Wilson
Does Python have a formal policy on the support lifetime (bug fixes,
security fixes, etc.) for major and minor versions?  I did a bit of
searching on the Python web site and this group, but didn't find
anything.  If there is a policy posted somewhere (and I just didn't
dig deep enough), would someone please point me in the right
direction.  If there is no posted policy, would someone be kind enough
to describe the practiced policy here.

I'm looking for is something like this:
http://docs.djangoproject.com/en/dev/internals/release-process/#supported-versions

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


Re: Test for Pythonwin?

2009-07-31 Thread Roger Miller
On Jul 31, 2:09 am, "steve"  wrote:
> Is there a good way to check if a script is running inside Pythonwin?
> Perhaps a property or method that is exposed by that environment?

I've used
  if sys.stdin.fileno() < 0:
This is not precisely a test for pythonwin, but indicates whether
standard streams are available, which is usually what I really
want to know.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-07-31 Thread Ethan Furman

Steven D'Aprano wrote:

On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:



That and the fact that I couldn't stop laughing for long enough to learn
any more when I read in the Pragmatic Programmer's Guide that "Ruby,
unlike less flexible languages, lets you alter the value of a constant."
Yep, as they say "Bug" = "Undocumented feature"!



That's no different from Python's "constant by convention". We don't even 
get a compiler warning!




That's quite different, actually.  Python doesn't claim to have 
constants!  Can't misinterpret what you can't have, can you?


[Holds breath while awaiting counter-example... :]

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


Re: Regexp problem

2009-07-31 Thread Ethan Furman

MRAB wrote:

Ethan Furman wrote:


Marcus Wanner wrote:

Wow, I really need to learn more about regexp...
Any tutorials you guys can recommend?

Marcus



Mastering Regular Expressions
Powerful Techniques for Perl and Other Tools
By Jeffrey E. F. Friedl

Great book!


+1

I have the first edition, seventh printing (December 1998). It refers to
the 'regex' module of Python 1.4b1, which was subsequently replaced by
the current 're' module and then removed from the standard library. I
hope it's been updated since then. :-)


I have the second edition (no idea which printing ;), and according to 
his preface it has indeed been much updated.  Most examples are in perl, 
the few in python are decent.  The knowledge embodied seems very 
thorough.  Since I've had the book (two weeks now?)  I've been able to 
solve two otherwise thorny issues using regular expressions.  Yay!


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


Re: Confessions of a Python fanboy

2009-07-31 Thread Iain King
On Jul 31, 4:08 pm, Ethan Furman  wrote:
> Steven D'Aprano wrote:
> > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:
>
> >>That and the fact that I couldn't stop laughing for long enough to learn
> >>any more when I read in the Pragmatic Programmer's Guide that "Ruby,
> >>unlike less flexible languages, lets you alter the value of a constant."
> >>Yep, as they say "Bug" = "Undocumented feature"!
>
> > That's no different from Python's "constant by convention". We don't even
> > get a compiler warning!
>
> That's quite different, actually.  Python doesn't claim to have
> constants!  Can't misinterpret what you can't have, can you?
>
> [Holds breath while awaiting counter-example... :]
>
> ~Ethan~

The convention being detailed in PEP8: http://www.python.org/dev/peps/pep-0008/
basically, anything in ALL_CAPS is a constant, assuming you follow
those style guidelines.

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Benjamin Kaplan
On Fri, Jul 31, 2009 at 11:35 AM, Iain King wrote:
> On Jul 31, 4:08 pm, Ethan Furman  wrote:
>> Steven D'Aprano wrote:
>> > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:
>>
>> >>That and the fact that I couldn't stop laughing for long enough to learn
>> >>any more when I read in the Pragmatic Programmer's Guide that "Ruby,
>> >>unlike less flexible languages, lets you alter the value of a constant."
>> >>Yep, as they say "Bug" = "Undocumented feature"!
>>
>> > That's no different from Python's "constant by convention". We don't even
>> > get a compiler warning!
>>
>> That's quite different, actually.  Python doesn't claim to have
>> constants!  Can't misinterpret what you can't have, can you?
>>
>> [Holds breath while awaiting counter-example... :]
>>
>> ~Ethan~
>
> The convention being detailed in PEP8: 
> http://www.python.org/dev/peps/pep-0008/
> basically, anything in ALL_CAPS is a constant, assuming you follow
> those style guidelines.
>

Right, but that's a style guide and not a language definition. Nobody
claims that anything in there is a part of the language.

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Steven D'Aprano
On Fri, 31 Jul 2009 13:38:56 +0200, Bruno Desthuilliers wrote:

>> On the other hand, we don't have to prefix names with @ and @@,
> 
> Nope, we have to prefix them with 'self' or 'cls' (or even
> 'self.__class__').

Incorrect.

>>> class K:
... class_attribute = 'No @@ required.'
...
>>> K().class_attribute
'No @@ required.'


No 'self' or 'cls' in sight.


I can see a vague advantage to using @ to create instance attributes and 
@@ to creating class attributes -- it makes it easy to create class 
attributes inside a method without needing to do a double lookup. In 
Python terms:

def K:
def method(self):
self.__class__.attr = "Class attribute"

would be:

def K:
def method(self):
@@attr = "Class attribute"

Advantages: you save a few characters and (possibly!) a couple of runtime 
lookups.

Disadvantages: your code is filled with line noise. It's an arbitrary 
choice between @@ meaning instance attribute and @@ meaning class 
attribute -- there's no logical reason for choosing one over the other, 
so you have to memorise which is which. It's easy to get it wrong.

'self' and 'cls' at least are words, even if 'cls' is badly misspelled :)



>> and we
>> don't have the compiler trying to *guess* whether we're calling a
>> function or referring to a variable.
> 
> Please re-read a bit more carefully - it's *all* method call. 

What did I misread from here?

[quote]
When Ruby sees a name such as ``a'' in an expression, it needs to 
determine if it is a local variable reference or a call to a method with 
no parameters. To decide which is the case, Ruby uses a heuristic. As 
Ruby reads a source file, it keeps track of symbols that have been 
assigned to. It assumes that these symbols are variables. When it 
subsequently comes across a symbol that might be either a variable or a 
method call, it checks to see if it has seen a prior assignment to that 
symbol. If so, it treats the symbol as a variable; otherwise it treats it 
as a method call.
[end quote]

And see the example "pathological case" comparing a function call (not 
method) and a variable.

http://ruby-doc.org/docs/ProgrammingRuby/html/language.html


> Python is
> 'uniform' in obj.name is always an attribute lookup (methods being
> attributes), Ruby is uniform in that 'obj.name' is always a method call.

Which would be relevant if I was talking about method calls, but I wasn't.



>> Somebody who knows more Ruby than me should try writing the Zen of
>> Ruby. Something like:
> 
> (snip childish parody of Python Zen)
> 
> Steven, is that any useful ?

It made me feel good.

But seriously, while I admit that I have very little Ruby experience, and 
so aren't in a great position to judge, it seems to me that Ruby doesn't 
have anything like Python's over-riding design principles (the Zen). If 
there is a design principle to Ruby, I can't see what it is.

I'm the first to admit that I'm far too inexperienced with the language 
to make this a fair judgement. Unfair it might be, but it doesn't 
necessarily mean I'm wrong! *wink*

Ruby just seems to be far more complicated than Python: things which 
Python does at runtime, with a function call, Ruby has special syntax for:

"a bunch of words".split()
%w(a bunch of words)

ord('a')
?a

That makes the barrier to entry far higher: it's easier to leverage 
existing knowledge to interpret unfamiliar Python code than unfamiliar 
Ruby code. Or so it seems to me.

Oh, and I admit that Python decorators are a conspicuous counter-example. 
I wouldn't do without them, but neither would I expect somebody to intuit 
what they do.


>> Although I'm sure Ruby has its good points. I'm not convinced anonymous
>> code blocks are one of them though.
> 
> Ruby's code blocks come from Smalltalk, where they are an absolute
> necessity since message passing (which code blocks are part of) is the
> *only* builtin control flow in Smalltalk - so you just *need* this
> construction to provide branching and iterations.

Just because Smalltalk had a particular (mis?)feature doesn't mean that 
other languages should copy it. I know, I know, Ruby people swear by 
anonymous code blocks, and I've read Paul Graham too. But I'm really not 
so sure that the benefits of anonymous code blocks are great enough to 
overcome the disadvantages of anonymous code blocks.


> Wether it makes sense to have code blocks in Ruby is another question
> since Ruby does provide traditional control flow features, but then one
> could question Python's "lambda" (hem...) too.

lambda, by allowing the function to be only a single expression, doesn't 
suffer the disadvantages of anonymous code blocks. lambda, by allowing 
the function to be only a single expression, also has fewer advantages 
than anonymous code blocks. I think lambda ends up on the "more 
advantages than disadvantages" side. I'm keeping my mind open regarding 
Ruby code blocks.



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


Re: Confessions of a Python fanboy

2009-07-31 Thread Masklinn

On 31 Jul 2009, at 17:55 , Steven D'Aprano wrote:
But seriously, while I admit that I have very little Ruby  
experience, and
so aren't in a great position to judge, it seems to me that Ruby  
doesn't
have anything like Python's over-riding design principles (the Zen).  
If

there is a design principle to Ruby, I can't see what it is.

As far as I know, Ruby doesn't have anything like the Zen no. But then  
again, I don't know any language other than Python with such a document.



Ruby just seems to be far more complicated than Python: things which
Python does at runtime, with a function call, Ruby has special  
syntax for:


"a bunch of words".split()
%w(a bunch of words)

ord('a')
?a


That's the Perl heritage. And since it inherits from Perl, TIMTOWTDI:
>> "a bunch of words".split
=> ["a", "bunch", "of", "words"]
>> "a"[0]
=> 97
(yes, the indexing operator returns a character code if provided a  
single index. With two, it returns as string slice)


Oh and %w() isn't really equivalent to split(): you don't use it to  
split a string but to create a list of strings, so the equivalent  
expression in Python would be `["a", "bunch", "of", "words"]`.



That makes the barrier to entry far higher: it's easier to leverage
existing knowledge to interpret unfamiliar Python code than unfamiliar
Ruby code. Or so it seems to me.


That's probable.

Although I'm sure Ruby has its good points. I'm not convinced  
anonymous

code blocks are one of them though.


Ruby's code blocks come from Smalltalk, where they are an absolute
necessity since message passing (which code blocks are part of) is  
the

*only* builtin control flow in Smalltalk - so you just *need* this
construction to provide branching and iterations.


Just because Smalltalk had a particular (mis?)feature doesn't mean  
that

other languages should copy it. I know, I know, Ruby people swear by
anonymous code blocks, and I've read Paul Graham too. But I'm really  
not

so sure that the benefits of anonymous code blocks are great enough to
overcome the disadvantages of anonymous code blocks.


What are the disadvantages of anonymous functions?

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Terry Reedy

Masklinn wrote:

#each is simply a method that takes a function (called blocks in ruby). 
One could call it a higher-order method I guess.


It's an implementation of the concept of internal iteration: instead of 
collections yielding iterator objects, and programmers using those 
through specially-built iteration constructs (e.g. `for…in`), 
collections control iteration over themselves (the iteration is 
performed "inside" the collection, thus the "internal" part) and the 
programmer provides the operations to perform at each iterative step 
through (usually) a function.


Python's iterator protocol was developed in part to avoid the 
(inside-out) callback style of programming. Writing virtual collections 
as generator functions instead of iterator or iterable classes saves a 
lot of boilerplate code. The itertools modules shows how nicely 
iterators can be composed in a way that is much more awkward with callbacks.


In Python (assuming we had anonymous defs and an each method on lists), 
the following loop:


for item in some_list:
do_something(item)
do_something_else(item)

some_list.each((def (item):
do_something(item)
do_something_else(item)
))


And how does Ruby do the equivalent of

def double(it):
  for i in it:
yield 2*i

for i,j in zip(double(some_list), some_gen_func(args)):
  print(do_something(i+j,i-j))

Terry Jan Reedy

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


Re: os.path.exists() and Samba shares

2009-07-31 Thread BDZ
On Jul 30, 4:41 pm, Loïc Domaigné 
wrote:
> Hi,
>
> > Hello. I have written a Python 3.1 script running on Windows that uses
> > os.path.exists() to connect to network shares. If the various network
> > shares require different user account and password combos than the
> > account the script is running under the routine returns false. I need
> > something like os.samba.path.exists(username,password,path). Does
> > anyone have a suggestion on how I can accomplish what I need to do in
> > Python?
>
> Could the Python Samba module PySamba be interesting for 
> you?http://sourceforge.net/projects/pysamba/
>
> HTH,
> Loïc
> --
> My blog:http://www.domaigne.com/blog

Unfortunately, although it has the calls I'd want, pysamba appears to
be *nix only. I need something that will work under Windows. Is there
a set of Python Windows functions (official or contributed) that might
do what I need? (I'm new to Python)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: supported versions policy?

2009-07-31 Thread Terry Reedy

Gary Wilson wrote:

Does Python have a formal policy on the support lifetime (bug fixes,
security fixes, etc.) for major and minor versions?  I did a bit of
searching on the Python web site and this group, but didn't find
anything.  If there is a policy posted somewhere (and I just didn't
dig deep enough), would someone please point me in the right
direction.  If there is no posted policy, would someone be kind enough
to describe the practiced policy here.


Defining Pyx.y.z as Pymajor.minor.bugfix, the last normal bugfix comes 
out about the same time as the next minor. True security fixes (very 
rare) come out for a couple of years thereafter. (Exception, 3.0 support 
ended with 3.0.1 -- it should be replaced with 3.1.) Currently Py2 and 
Py3 are running in parallel. The future 2.7.z's will probably be the end 
of the Py2 series. Everything is contingent on voluteer time to do the 
above.


tjr

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


threads check socket

2009-07-31 Thread NighterNet
I been trying to find a way to check the socket is open or not. The
thread are in a group and loop if the sockets are open. If they are
not open delete the thread and remove the group. I need on this.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: fast video encoding

2009-07-31 Thread David Bolen
gregorth  writes:

> I am a novice with video encoding. I found that few codecs support
> gray scale images. Any hints to take advantage of the fact that I only
> have gray scale images?

I don't know that there's any good way around the fact that video
encoding is simply one of the heavier CPU-bound activities you're
likely to encounter.  So I suspect that codec choice (barring letting
quality drop a bit) is going to move the bar less than picking the
beefiest CPU you can.

If I were looking to do this, I'd probably include investigating
pumping the raw camera images into an ffmpeg encoding subprocess and
let it handle all the encoding.  There's about a gazillion options and
a variety of codec options to play with for performance.  You could
grab and store a short video sample from the camera and use it as a
benchmark to compare encoding options.

ffmpeg does have a -pix_fmt option that can be used to indicate the
input pixel type - "gray" would be 8-bit, and result in a 4:0:0 image
with a YUV-based encoder, for example.  Not sure how much, if any,
impact it would have on encoding speed though.

To be honest, with your data rate, I might even consider getting
Python out of the pure encoding path once it starts - depending on the
raw network format delivered by the camera you might be able to have
ffmpeg read directly from it.  Might not be worth it, since the data
transfer is probably I/O bound, but a 640x480x1 stream at 100Hz is
still nothing to sniff at, and even managing the raw data flow in
Python might eat up some CPU that you could better allocate to the
encoding process, or require extra data copies along the way that
would be best to avoid.

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Tim Rowe
2009/7/31 Steven D'Aprano :
> On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:
>
>> That and the fact that I couldn't stop laughing for long enough to learn
>> any more when I read in the Pragmatic Programmer's Guide that "Ruby,
>> unlike less flexible languages, lets you alter the value of a constant."
>> Yep, as they say "Bug" = "Undocumented feature"!
>
> That's no different from Python's "constant by convention". We don't even
> get a compiler warning!

We don't actually *declare* that something is constant and then have
that declaration ignored. Python doesn't lie to us, although (as in
any language) a programmer might.

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Falcolas
On Jul 31, 3:49 am, Masklinn  wrote:
> On 31 Jul 2009, at 10:25 , Chris Rebert wrote:> On Fri, Jul 31, 2009 at 1:21 
> AM, Xavier Ho  
> > wrote:
> >> On Fri, Jul 31, 2009 at 6:08 PM, Masklinn   
> >> wrote:
>
> >>> ... but since Python doesn't have anonymous functions that  
> >>> usage
> >>> tends to be a bit too verbose ... 
>
> >> Sorry to interrupt, but wouldn't lambda in Python be considered as
> >> 'anonymous functions'?
>
> > I believe "full" anonymous functions was intended by the author.
> > lambdas are limited to a single expression.
>
> Yes, and they're limited to a single *expression*, so before Python 3,  
> lambda: print "foo" is out. Likewise, it isn't possible to have an if/
> else statement within a lambda (though a ternary is ok), or a with, …  
> since Python statements aren't expressions.

Perhaps you can't do lambda foo: print foo, but you *can* do lambda x:
sys.stdout.write(x).

Combined with the ternary if, it seem sufficient if you want to stick
to the ideal "Simple is better than Complex". Sure, with statements
allowed in anonymous functions, you can save on a line of typing (def
blargh(x):), but I don't feel the obfuscation is worth the cost.

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


Re: Help understanding the decisions *behind* python?

2009-07-31 Thread Joshua Bronson
On Jul 22, 7:55 am, Duncan Booth  wrote:
> I find it interesting that the heapq functions tell you in the
> documentation that they aren't suitable for use where n==1 or where n is
> near the total size of the sequence whereas random.sample() chooses what it
> thinks is the best algorithm based on the input. At the very least I would
> have thought the heapq functions could check for n==1 and call min/max when
> it is.

I've had the same thought myself a number of times, so I filed a bug:
http://bugs.python.org/issue6614
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: threads check socket

2009-07-31 Thread Gabriel Genellina
En Fri, 31 Jul 2009 13:35:10 -0300, NighterNet   
escribió:



I been trying to find a way to check the socket is open or not. The
thread are in a group and loop if the sockets are open. If they are
not open delete the thread and remove the group. I need on this.


What means an "open socket"? Do you want to detect whether the other side  
closed the connection? In that case reading from the socket returns an  
empty string.


Regarding the "threads in a group", I don't understand what you mean. But  
in general, you don't delete a thread, you just return from its run()  
method and the thread finishes execution.


--
Gabriel Genellina

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


Re: threads check socket

2009-07-31 Thread Marcus Wanner

On 7/31/2009 12:35 PM, NighterNet wrote:

I been trying to find a way to check the socket is open or not. The
thread are in a group and loop if the sockets are open. If they are
not open delete the thread and remove the group. I need on this.

Being a bit more specific would help.

Are you using tcp or udp? Is it a local socket or a remote one? What is 
controlling the socket? Define "open".


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


Re: file comparison

2009-07-31 Thread Gabriel Genellina
En Fri, 31 Jul 2009 06:25:17 -0300, learner learner   
escribió:



I want to compare two text files line by line and eliminate the
matching/repeated line and store the unmatched/leftout lines into a third
file or overwrite into one of them.


Look at the difflib module: http://docs.python.org/library/difflib.html

If that's not what you want, you have to provide much more details.

--
Gabriel Genellina

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


Re: Help understanding the decisions *behind* python?

2009-07-31 Thread Raymond Hettinger
On Jul 22, 4:55 am, Duncan Booth  wrote:
> Steven D'Aprano  wrote:
> > But that's the wrong solution to the problem. The OP wants the largest
> > (or smallest) item, which he expects to get by sorting, then grabbing
> > the first element:
>
> > sorted(alist)[0]
>
> > That requires sorting the entire list, only to throw away everything
> > except the first item. A better solution is to use min() and max(),
> > neither of which sort the list, so they are much faster.
>
> And if they want the n smallest or largest items (where n is significantly
> less than the length of the list[*]) they might consider using
> heapq.nsmallest() and heapq.nlargest()
>
> I find it interesting that the heapq functions tell you in the
> documentation that they aren't suitable for use where n==1 or where n is
> near the total size of the sequence whereas random.sample() chooses what it
> thinks is the best algorithm based on the input. At the very least I would
> have thought the heapq functions could check for n==1 and call min/max when
> it is.

The heapq.py code in Py2.7 and Py3.1 already does some automatic
algorithm selection:
http://svn.python.org/view/python/branches/release31-maint/Lib/heapq.py?revision=73579&view=markup

The automatic seletion of alternatives only occurs in clear-cut cases
(such as n==1
or where n==len(iterable) when the iterable has a known length).   For
the
remaining cases, the switchover point from heapq to sorted needs a
programmer's judgment based on whether the input iterable has a known
length, the cost of comparison function, and whether input is already
partially ordered.

The advice in the docs helps the reader understand the
relationships between min, max, nsmallest, nlargest, and sorted.


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


Re: threads check socket

2009-07-31 Thread NighterNet
On Jul 31, 10:23 am, "Gabriel Genellina" 
wrote:
> En Fri, 31 Jul 2009 13:35:10 -0300, NighterNet   
> escribió:
>
> > I been trying to find a way to check the socket is open or not. The
> > thread are in a group and loop if the sockets are open. If they are
> > not open delete the thread and remove the group. I need on this.
>
> What means an "open socket"? Do you want to detect whether the other side  
> closed the connection? In that case reading from the socket returns an  
> empty string.
>
> Regarding the "threads in a group", I don't understand what you mean. But  
> in general, you don't delete a thread, you just return from its run()  
> method and the thread finishes execution.
>
> --
> Gabriel Genellina

Thread added into the array. Well this thread when user leave the
server want to make sure it not in used so that I can reuse it some
how or move into another array.

Need to know the thread speed that need for game server timer to
control it. Don't want it too fast or slow for the game server.
fps = 30
def run(self):
while True:
self.gametime += 1
if self.gametime > 1000/fps:
self.gametime = 0;
-- 
http://mail.python.org/mailman/listinfo/python-list


heapq "key" arguments

2009-07-31 Thread Joshua Bronson
According to http://docs.python.org/library/heapq.html, Python 2.5
added an optional "key" argument to heapq.nsmallest and
heapq.nlargest. I could never understand why they didn't also add a
"key" argument to the other relevant functions (heapify, heappush,
etc). Say I want to maintain a heap of (x, y) pairs sorted only by
first coordinate. Without being able to pass key=itemgetter(0), won't
heapifying a list of such pairs unnecessarily compare both
coordinates? And worse, if the second coordinate is actually an object
with no ordering defined for it, heapifying will cause an error even
though all I care about is sorting by the first coordinate, which does
have an ordering. Am I missing something?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help understanding the decisions *behind* python?

2009-07-31 Thread Raymond Hettinger
On Jul 20, 9:27 am, Phillip B Oldham  wrote:
> Specifically the "differences" between lists and tuples have us
> confused and have caused many "discussions" in the office. We
> understand that lists are mutable and tuples are not, but we're a
> little lost as to why the two were kept separate from the start. They
> both perform a very similar job as far as we can tell.

The underlying C code for the two is substantially the same.  In terms
of code, tuples are in effect just immutable lists that have the
added
feature of being hashable (and therefore usable as dictionary keys or
elements of sets).

Beyond the mutable/hashable distinction, there is an important
philosophical distinction articulated by Guido.  He deems tuples
to be useful for struct like groupings of non-homogenous fields
and lists to be useful for sequences of homogenous data suitable
for looping.

While nothing in the list/tuple code requires you to make that
distinction,
it is important because that philosophy pervades the language.  If you
follow Guido's direction, you'll find that the various parts of the
language fit together better.  Do otherwise and you'll be going
against
the grain.


Raymond

P.S.  The C code for lists and tuples have a slightly difference
internal
structure that makes tuples a little more space efficient (since their
size is known at the outset and doesn't change during use).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: heapq "key" arguments

2009-07-31 Thread Jonathan Gardner
On Jul 31, 10:44 am, Joshua Bronson  wrote:
> Say I want to maintain a heap of (x, y) pairs sorted only by
> first coordinate. Without being able to pass key=itemgetter(0), won't
> heapifying a list of such pairs unnecessarily compare both
> coordinates?

It will compare the second value only if the first values are equal.
-- 
http://mail.python.org/mailman/listinfo/python-list


base64 Incorrect Padding

2009-07-31 Thread Brandon Fredericks
I did a search within this group, but couldn't find any information on
this.

I am sending base64 encoded data as the content over http using
urllib2 urlopen. When I receive the data and attempt to decode it, I
get an "Incorrect Padding" error. Is there a simple way to fix this? A
better way to send and receive the data possibly (using base64 of
course)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Confessions of a Python fanboy

2009-07-31 Thread Masklinn

On 31 Jul 2009, at 18:24 , Terry Reedy wrote:

Masklinn wrote:

#each is simply a method that takes a function (called blocks in  
ruby). One could call it a higher-order method I guess.
It's an implementation of the concept of internal iteration:  
instead of collections yielding iterator objects, and programmers  
using those through specially-built iteration constructs (e.g. `for… 
in`), collections control iteration over themselves (the iteration  
is performed "inside" the collection, thus the "internal" part) and  
the programmer provides the operations to perform at each iterative  
step through (usually) a function.


Python's iterator protocol was developed in part to avoid the  
(inside-out) callback style of programming. Writing virtual  
collections as generator functions instead of iterator or iterable  
classes saves a lot of boilerplate code. The itertools modules shows  
how nicely iterators can be composed in a way that is much more  
awkward with callbacks.


In Python (assuming we had anonymous defs and an each method on  
lists), the following loop:

   for item in some_list:
   do_something(item)
   do_something_else(item)
   some_list.each((def (item):
   do_something(item)
   do_something_else(item)
   ))


And how does Ruby do the equivalent of

def double(it):
 for i in it:
   yield 2*i

for i,j in zip(double(some_list), some_gen_func(args)):
 print(do_something(i+j,i-j))



Somethign along the lines of

some_list.map{|e| 2*e}.zip(some_gen_func(args)).each {|i, j|
puts(do_something(i+j, i-j))
}

The `#each` call after `#zip` is not actually necessary, but I find it  
clearer. Oh, and some_gen_func and do_something probably wouldn't work  
that way (as I said above, Ruby isn't big on named functions and  
doesn't actually have them)

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Steven D'Aprano
On Fri, 31 Jul 2009 18:15:15 +0200, Masklinn wrote:

> > I know, I know, Ruby people swear by
> > anonymous code blocks, and I've read Paul Graham too. But I'm really
> > not so sure that the benefits of anonymous code blocks are great
> > enough to overcome the disadvantages of anonymous code blocks.
> >
> What are the disadvantages of anonymous functions?

In no particular order, and not necessarily exhaustive:

* The risk of obfuscation in your code. That's fairly minimal for 
lambdas, because they're just a single expression, but for a large 
anonymous code block (ACB) defined inside a named function, it may be 
difficult for the reader to easily distinguish which bits are the outer 
function and which are the ACB.


* Loss of useful debugging information. Take this example from Python:

>>> def main(f):
... return f(3)
...
>>> main(lambda n: 2/(n-3))
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in main
  File "", line 1, in 
ZeroDivisionError: integer division or modulo by zero
>>>
>>> def my_special_function(n):
... return 2/(n-3)
...
>>> main(my_special_function)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 2, in main
  File "", line 2, in my_special_function
ZeroDivisionError: integer division or modulo by zero

If your code has only one anonymous function (whether a lambda or a full 
multi-line block), then it's easy to identify which lambda raised the 
exception: there is only one it could be. But if your code uses lots of 
lambdas, the lack of a function name makes it hard to distinguish one 
 from another . Anonymity makes identification harder.


* Risk of code-duplication and breaking the principle of Once And Only 
Once. Anonymous functions are generally created, used, then immediately 
thrown away -- or at least made more-or-less inaccessible for reuse. An 
anonymous function stored in a callback still exists, but the coder isn't 
able to easily re-use it for another callback somewhere else in the code. 
Consequently, there's a temptation for the coder to write the same 
function multiple times:

add_button("Parrot", colour=blue, callback=lambda x: x.stuff('a'))
add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b'))
add_button("Canary", colour=yellow, callback=lambda x: x.stuff('a'))

instead of:

def bird_callback(x):
return x.stuff('a')

add_button("Parrot", colour=blue, callback=bird_callback)
add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b'))
add_button("Canary", colour=yellow, callback=bird_callback)

* Recursion is more or less impossible without fragile tricks.

(At least for Python. I don't know how recursion operates in Ruby.)





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


Newbie Question regarding __init__()

2009-07-31 Thread Simon
Hi

I want to create an instance of dcCursor which inherits from
dcObject.  When I run the following code it gives the error shown.
Can some explain to me what is wrong? I have included the dcObject.py
and dcCursor.py below.

>>>import dcObject
>>> import dcCursor
>>> x = dcCursor.dcCursor()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: __init__() takes no arguments (1 given)

# -*- coding: utf-8 -*-
# dcCursor.py - The base Cursor Class

from dcObject import dcObject

class dcCursor(dcObject):
"""The base Cursor Class"""
def OpenCursor(tcTemp,tcTable):
"""Opens the specified cursor
Parameters: tcTemp,tcTable"""
pass

def CreateCursor(tcTable):
"""Creates the specified cursor
Parameters: tcTable"""
pass

def init_Exec():
print("init_Exec called")



# -*- coding: utf-8 -*-
# dcObject.py - Base Object
class dcObject(object):
"""Base Object"""
def __init__():
"""default init method has the form
of init_Pre() and init_Exec
init_Post()"""
self.init_Pre() and self.init_Exec()
self.init_Post()

def init_Pre():
"""Always called before init_Exec()
if it returns false init_Exec is
not executed"""
return True

def init_Exec():
"""Base __init__ code goes here and this is
only executed if init_Pre() returns true"""
return True

def init_Post():
"""Always called after the init_Pre() and
init_Exec()"""
return True

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


Re: Printing with colors in a portable way

2009-07-31 Thread Nobody
On Thu, 30 Jul 2009 15:40:37 -0700, Robert Dailey wrote:

> Anyone know of a way to print text in Python 3.1 with colors in a
> portable way? In other words, I should be able to do something like
> this:
> 
> print_color( "This is my text", COLOR_BLUE )
> 
> And this should be portable (i.e. it should work on Linux, Mac,
> Windows).

The way that terminals (and emulators) handle colour is fundamentally
different from the DOS/Windows console. If you want something which will
work on both, you will have write separate implementations for terminals
and the DOS/Windows console.

For terminals, you can use the "curses" package, e.g.:

import curses

curses.setupterm()
setaf = curses.tigetstr('setaf')
setab = curses.tigetstr('setab')

def foreground(num):
if setaf:
sys.stdout.write(curses.tparm(setaf, num))

def background(num):
if setab:
sys.stdout.write(curses.tparm(setab, num))

For the Windows console, you'll need to use ctypes to interface to the
SetConsoleTextAttribute() function from Kernel32.dll.

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


Re: Newbie Question regarding __init__()

2009-07-31 Thread MRAB

Simon wrote:

Hi

I want to create an instance of dcCursor which inherits from
dcObject.  When I run the following code it gives the error shown.
Can some explain to me what is wrong? I have included the dcObject.py
and dcCursor.py below.


import dcObject
import dcCursor
x = dcCursor.dcCursor()

Traceback (most recent call last):
  File "", line 1, in 
TypeError: __init__() takes no arguments (1 given)


All instance methods must have the instance as the first argument, by
convention called 'self':


# -*- coding: utf-8 -*-
# dcCursor.py - The base Cursor Class

from dcObject import dcObject

class dcCursor(dcObject):
"""The base Cursor Class"""
def OpenCursor(tcTemp,tcTable):

def OpenCursor(self, tcTemp, tcTable):


"""Opens the specified cursor
Parameters: tcTemp,tcTable"""
pass

def CreateCursor(tcTable):

def CreateCursor(self, tcTable):
...

and so on, including the '__init__' method.
--
http://mail.python.org/mailman/listinfo/python-list


.dbf tables and Null

2009-07-31 Thread Ethan Furman

Mornin'!  and a good one, too, I hope.

Question for you...

First part of the question:  What is the general value in having Null 
capability for fields?


Second part:  Is there a tangible difference between Null, and the 
nothing of 0, '', False, etc, in python?


Third part:  If there is a tangible difference, do those of us who use 
python and these old, refuse-to-die, .dbf files actually have need of, 
or have tables including, Null values?


P.S. part (for John Machin, if he sees this  ;)
Will the dbf package you are working on support Null values?

Any and all information appreciated!

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


Re: Confessions of a Python fanboy

2009-07-31 Thread Emmanuel Surleau
On Friday 31 July 2009 18:54:23 Tim Rowe wrote:
> 2009/7/31 Steven D'Aprano :
> > On Thu, 30 Jul 2009 18:47:04 +0100, Tim Rowe wrote:
> >> That and the fact that I couldn't stop laughing for long enough to learn
> >> any more when I read in the Pragmatic Programmer's Guide that "Ruby,
> >> unlike less flexible languages, lets you alter the value of a constant."
> >> Yep, as they say "Bug" = "Undocumented feature"!
> >
> > That's no different from Python's "constant by convention". We don't even
> > get a compiler warning!
>
> We don't actually *declare* that something is constant and then have
> that declaration ignored. Python doesn't lie to us, although (as in
> any language) a programmer might.

You could say that Ruby doesn't either, you just need to read the 
documentation. Ruby's unwritten motto is "flexibility über alles". In this 
regard, it is consistent (1). Not much is really bolted down in Ruby. You get 
encapsulation, but it's so easy to break that it's mostly symbolic. It's a 
language which gives great power to the programmer. Whether Ruby programmers 
handle this power responsibly is another debate.

Cheers,

Emm

(1) I find Ruby to be pretty consistent in general, which is not always the 
case of Python.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Help understanding the decisions *behind* python?

2009-07-31 Thread Emmanuel Surleau
On Friday 31 July 2009 19:49:04 Raymond Hettinger wrote:
> On Jul 20, 9:27 am, Phillip B Oldham  wrote:
> > Specifically the "differences" between lists and tuples have us
> > confused and have caused many "discussions" in the office. We
> > understand that lists are mutable and tuples are not, but we're a
> > little lost as to why the two were kept separate from the start. They
> > both perform a very similar job as far as we can tell.
>
> The underlying C code for the two is substantially the same.  In terms
> of code, tuples are in effect just immutable lists that have the
> added
> feature of being hashable (and therefore usable as dictionary keys or
> elements of sets).
>
> Beyond the mutable/hashable distinction, there is an important
> philosophical distinction articulated by Guido.  He deems tuples
> to be useful for struct like groupings of non-homogenous fields
> and lists to be useful for sequences of homogenous data suitable
> for looping.
>
> While nothing in the list/tuple code requires you to make that
> distinction,
> it is important because that philosophy pervades the language.  If you
> follow Guido's direction, you'll find that the various parts of the
> language fit together better.  Do otherwise and you'll be going
> against
> the grain.

I might be wrong, but I get the impression that many people don't indeed "get 
it" and use tuples as static arrays and lists when the array needs to be 
dynamically resized. This is certainly how I use them as well.

This would tend to show that Guido's notion here was not particularly 
intuitive.

Cheers,

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


Re: base64 Incorrect Padding

2009-07-31 Thread Peter Otten
Brandon Fredericks wrote:

> I did a search within this group, but couldn't find any information on
> this.
> 
> I am sending base64 encoded data as the content over http using
> urllib2 urlopen. When I receive the data and attempt to decode it, I
> get an "Incorrect Padding" error. Is there a simple way to fix this? A
> better way to send and receive the data possibly (using base64 of
> course)?

Have the server send some known data. Read it with urllib2.urlopen(). 
Compare with that same data that you encoded locally. What are the 
differences?

Peter


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


Re: Confessions of a Python fanboy

2009-07-31 Thread Masklinn

On 31 Jul 2009, at 20:17 , Steven D'Aprano wrote:

On Fri, 31 Jul 2009 18:15:15 +0200, Masklinn wrote:


I know, I know, Ruby people swear by
anonymous code blocks, and I've read Paul Graham too. But I'm really
not so sure that the benefits of anonymous code blocks are great
enough to overcome the disadvantages of anonymous code blocks.


What are the disadvantages of anonymous functions?


In no particular order, and not necessarily exhaustive:

* The risk of obfuscation in your code. That's fairly minimal for
lambdas, because they're just a single expression, but for a large
anonymous code block (ACB) defined inside a named function, it may be
difficult for the reader to easily distinguish which bits are the  
outer

function and which are the ACB.


I believe that one's unadulterated BS.


* Loss of useful debugging information. Take this example from Python:


def main(f):

... return f(3)
...

main(lambda n: 2/(n-3))

Traceback (most recent call last):
 File "", line 1, in 
 File "", line 2, in main
 File "", line 1, in 
ZeroDivisionError: integer division or modulo by zero


def my_special_function(n):

... return 2/(n-3)
...

main(my_special_function)

Traceback (most recent call last):
 File "", line 1, in 
 File "", line 2, in main
 File "", line 2, in my_special_function
ZeroDivisionError: integer division or modulo by zero

If your code has only one anonymous function (whether a lambda or a  
full

multi-line block), then it's easy to identify which lambda raised the
exception: there is only one it could be. But if your code uses lots  
of

lambdas, the lack of a function name makes it hard to distinguish one
 from another . Anonymity makes identification harder.

The traceback gives you the line of the anonymous function (even in  
python) so unless you have several anonymous functions on the same  
line, there's no reason why that would be much of an issue.  
Furthermore, Python doesn't provide any more information when the  
error happens out of a function (in a `for` or a `with`), so it's not  
like there's much of a difference here between Ruby's block-based  
approach and Python's statements-based approach.



* Risk of code-duplication and breaking the principle of Once And Only
Once. Anonymous functions are generally created, used, then  
immediately
thrown away -- or at least made more-or-less inaccessible for reuse.  
An
anonymous function stored in a callback still exists, but the coder  
isn't
able to easily re-use it for another callback somewhere else in the  
code.

Consequently, there's a temptation for the coder to write the same
function multiple times:

add_button("Parrot", colour=blue, callback=lambda x: x.stuff('a'))
add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b'))
add_button("Canary", colour=yellow, callback=lambda x: x.stuff('a'))

instead of:

def bird_callback(x):
   return x.stuff('a')

add_button("Parrot", colour=blue, callback=bird_callback)
add_button("Cheese", flavour=tasty, callback=lambda x: x.thing('b'))
add_button("Canary", colour=yellow, callback=bird_callback)

Yes, that one I can give you though I don't think that's a big issue.  
And it's not like it's hard to extract the anonymous function into a  
named one and then use that on the third strike, so I really don't  
believe that point holds much water.



* Recursion is more or less impossible without fragile tricks.

(At least for Python. I don't know how recursion operates in Ruby.)
Code blocks are rarely if ever used recursively. If an operation is  
using anonymous functions recursively, then there's often something  
very wrong with the idea leading to that code. So I find this  
objection irrelevant.

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


Re: Help understanding the decisions *behind* python?

2009-07-31 Thread Masklinn

On 31 Jul 2009, at 20:48 , Emmanuel Surleau wrote:

On Friday 31 July 2009 19:49:04 Raymond Hettinger wrote:
On Jul 20, 9:27 am, Phillip B Oldham   
wrote:

Specifically the "differences" between lists and tuples have us
confused and have caused many "discussions" in the office. We
understand that lists are mutable and tuples are not, but we're a
little lost as to why the two were kept separate from the start.  
They

both perform a very similar job as far as we can tell.


The underlying C code for the two is substantially the same.  In  
terms

of code, tuples are in effect just immutable lists that have the
added
feature of being hashable (and therefore usable as dictionary keys or
elements of sets).

Beyond the mutable/hashable distinction, there is an important
philosophical distinction articulated by Guido.  He deems tuples
to be useful for struct like groupings of non-homogenous fields
and lists to be useful for sequences of homogenous data suitable
for looping.

While nothing in the list/tuple code requires you to make that
distinction,
it is important because that philosophy pervades the language.  If  
you

follow Guido's direction, you'll find that the various parts of the
language fit together better.  Do otherwise and you'll be going
against
the grain.


I might be wrong, but I get the impression that many people don't  
indeed "get
it" and use tuples as static arrays and lists when the array needs  
to be

dynamically resized. This is certainly how I use them as well.

This would tend to show that Guido's notion here was not particularly
intuitive.
It's intuitive if you come to Python knowing other languages with  
tuples (which are mostly functional, and in which tuples are *never*  
sequences/iterables). At the end of the day, and if Guido's intention  
truly was what Raymond says, implementing tuples as immutable sequence  
was a mistake. And the documentation is to blame as well: in it,  
tuples are clearly described as a *sequence* type, not a *structure*  
type. 
--

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


Re: heapq "key" arguments

2009-07-31 Thread Joshua Bronson
On Jul 31, 2:02 pm, Jonathan Gardner 
wrote:
> On Jul 31, 10:44 am, Joshua Bronson  wrote:
>
> > Say I want to maintain a heap of (x, y) pairs sorted only by
> > first coordinate. Without being able to pass key=itemgetter(0), won't
> > heapifying a list of such pairs unnecessarily compare both
> > coordinates?
>
> It will compare the second value only if the first values are equal.

I don't see how that helps. That's not at all the same thing as being
able to pass key=itemgetter(0).
-- 
http://mail.python.org/mailman/listinfo/python-list


  1   2   >