RedNotebook 1.1.6

2011-05-11 Thread Jendrik Seipp

A new RedNotebook version has been released.

You can get the tarball, the Windows installer and links to distribution 
packages at

http://rednotebook.sourceforge.net/downloads.html


What is RedNotebook?

RedNotebook is a **graphical journal** and diary helping you keep track 
of notes and thoughts. It includes a calendar navigation, customizable

templates, export functionality and word clouds. You can also format,
tag and search your entries. RedNotebook is available in the 
repositories of most common Linux distributions and a Windows installer 
is available. It is written in Python and uses GTK+ for its interface.



What's new?
---
* Fix date encoding (LP:775269)
* Some translations updated

Cheers,
Jendrik





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

   Support the Python Software Foundation:
   http://www.python.org/psf/donations/


Re: Python backup programs?

2011-05-11 Thread Laurent Claessens

Le 11/05/2011 01:57, James Mills a écrit :
 On Wed, May 11, 2011 at 9:00 AM, Dan Strombergdrsali...@gmail.com 
wrote:


  What are your favorite backup programs written, in whole or in part, in
  Python?

My favorite one is the one I wrote myself for myself ;)

The point I like :

1. the backup is a simple copy. I can retrieve it without any specific 
programs.
2. if a file changed, before to be copied, the backup file is moved to a 
specific repertory
   whose name is the date and hour. So if I destroy a file and backup 
the destroyed one, the old

   non-destroyed one is still available.
3. Since the program is anyway performing a long os.walk operation, in 
the same time, it performs `git commit`

   in the directories that need it.
4. My program is command-line only. Works fine in tty

Points that are of no importance (very personal and adapted to my 
specific case !) :


1. time. One backup takes between 10 minutes and one hour. I don't 
really care.
2. space. Since my backup is a copy (and copy of copies), my backup 
directory takes ~150Go

   while my home is about 25 Go.

Hope it answer your question.

Have a nice day
Laurent
--
http://mail.python.org/mailman/listinfo/python-list


NewBie Doubt in Python Thread Programming

2011-05-11 Thread vijay swaminathan
Hi All,

I'm new bie to thread programming and I need some assistance in
understanding few concepts ...

I have a very simple program which runs a thread and prints a string.

import threading

class MyThread(threading.Thread):
def __init__(self, parent = None):
threading.Thread.__init__(self)

def run(self):
print 'Hello World'

def main():
for i in range(10):
MyThread_Object = MyThread()
print 'Object id is : ' , id(MyThread_Object)
print 'Staring thread -- ' , MyThread_Object.getName()
MyThread_Object.start()
count = threading.activeCount()
print 'The active thread count is: ' , count

if __name__ == '__main__':
main()

When I run this, I could see 10 thread being called. But when I print the
active thread count it is only 2.

Need some understanding on the following.

1. How the total active thread is 2?
2. how do I stop a thread? does it get automatically stopped after execution
?
3. Am I totally wrong in understanding the concepts.
4. what is the difference between active_count() and activeCount() since
both seem to give the same result.
5. is there a way to find out if the thread is still active or dead?

Please help me in understanding ..

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


Re: NewBie Doubt in Python Thread Programming

2011-05-11 Thread Chris Angelico
On Wed, May 11, 2011 at 4:57 PM, vijay swaminathan swavi...@gmail.com wrote:
     for i in range(10):
     MyThread_Object.start()
     count = threading.activeCount()

 When I run this, I could see 10 thread being called. But when I print the
 active thread count it is only 2.

 Need some understanding on the following.

 1. How the total active thread is 2?

My guess is that all (or all but one) of the threads have finished
already by the time you check activeCount. If you add a call to
time.sleep(1) in the run() method, you'll see 11 or 12 threads (your
main threads and 10 others). If you print out threading.activeCount()
at the top of the program, you'll see that it starts at 2 in IDLE, or
1 in stand-alone Python.

 2. how do I stop a thread? does it get automatically stopped after execution

Once the run() method returns, the thread is terminated. You shouldn't
normally need to stop a thread from another thread.

 3. Am I totally wrong in understanding the concepts.
 4. what is the difference between active_count() and activeCount() since
 both seem to give the same result.

The camelCase function names were inspired by Java's API, the ones
with underscores are more Python's style. They are absolutely the same
though. See the notes at the top of
http://docs.python.org/library/threading.html for that and other
information.

 5. is there a way to find out if the thread is still active or dead?

Yep! Call is_alive() on your thread object. It'll return True if it's
still going. Again, the docs for the threading module
(http://docs.python.org/library/threading.html) have all that sort of
thing.

Threading is a bit of a tricky concept, and takes some getting used
to. There are many places where threads are awesome, and many where
they're pretty useless. The place I most often use threads is in
socket programming; when I run a server, I usually spin off a thread
to handle each incoming socket, as it's the easiest way to handle
sequential actions (especially if the socket protocol is
command-response, like a MUD or a mail server).

Once you get your head around the threading module, you'll find the
multiprocessing module very similar.  For Python, the difference is
sometimes quite important, so it's as well to understand both.

Hope that helps!

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


Re: NewBie Doubt in Python Thread Programming

2011-05-11 Thread Gabriel Genellina
En Wed, 11 May 2011 03:57:13 -0300, vijay swaminathan swavi...@gmail.com  
escribió:



Hi All,

I'm new bie to thread programming and I need some assistance in
understanding few concepts ...

I have a very simple program which runs a thread and prints a string.

import threading

class MyThread(threading.Thread):
def __init__(self, parent = None):
threading.Thread.__init__(self)

def run(self):
print 'Hello World'

def main():
for i in range(10):
MyThread_Object = MyThread()
print 'Object id is : ' , id(MyThread_Object)
print 'Staring thread -- ' , MyThread_Object.getName()
MyThread_Object.start()
count = threading.activeCount()
print 'The active thread count is: ' , count

if __name__ == '__main__':
main()

When I run this, I could see 10 thread being called. But when I print the
active thread count it is only 2.

Need some understanding on the following.

1. How the total active thread is 2?


Because most of them have already finished by then. Your run() method  
executes quite fast. Make it take more time (maybe by adding  
time.sleep(1)) and you'll see 10 active threads.


2. how do I stop a thread? does it get automatically stopped after  
execution

?


You don't; a trhread runs until the run() method exits. After that, the OS  
thread finishes. The Python object (a threading.Thread instance) is still  
alive (until the last reference to it disappears, as any other object).



3. Am I totally wrong in understanding the concepts.


I don't know...


4. what is the difference between active_count() and activeCount() since
both seem to give the same result.


Nothing. active_count is the preferred Python spelling per PEP8;  
activeCount is the original Java spelling.



5. is there a way to find out if the thread is still active or dead?



Yes, use is_alive()

--
Gabriel Genellina

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


Re: NewBie Doubt in Python Thread Programming

2011-05-11 Thread James Mills
On Wed, May 11, 2011 at 4:57 PM, vijay swaminathan swavi...@gmail.com wrote:

[...]

 1. How the total active thread is 2?

Your threads are terminating as normal.
Without some kind of loop in your run() method
they will execute the instructions and terminate.

 2. how do I stop a thread? does it get automatically stopped after execution

Usually by a flag or condition that terminates your run() function/method.

 3. Am I totally wrong in understanding the concepts.
 4. what is the difference between active_count() and activeCount() since
 both seem to give the same result.

They are synonyms.

 5. is there a way to find out if the thread is still active or dead?

See: pydoc threading.Thread or help(threading.Thread)

cheers
James

-- 
-- James Mills
--
-- Problems are solved by method
-- 
http://mail.python.org/mailman/listinfo/python-list


Py3k,email header handling

2011-05-11 Thread TheSaint
Hello,
some time ago, I wrote a program to eliminate undesided emails from the 
server(s) and leave those which comply to certain filter criteria.

I started it when I got to know whit Python 2.3. Now a days I'd like to 
spend some time to improve it, just for my interest, however it didn't 
gather anybody's interest before.
Now python 3.2 (and some version before) started to use byte, rather than 
text strings, almost for all data handling in compliance to unicode. My 
problem arise that my program handle text strings, so I'd like to rewrite 
the code

My program reads from IMAP4 or POP3 server, I'd prefer that a function/class  
will return either a list or a dictionary which contains the following 
fields:

'from', 'to', 'cc', 'bcc', 'date', 'subject', 'reply-to', 'message-id'

The list may be organized as tuple (from, its_content,), etc,etc for each 
field, but I think dictionary would be more efficient to use.

1) is there a way to call the IMAPlib or POPlib and pass the data directly 
to email.parser.HeaderParser to achieve my intention?

2) If the above will do, do re.compile compile unicode results?
I guess yes.

3) any related documentation...

-- 
goto /dev/null
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NewBie Doubt in Python Thread Programming

2011-05-11 Thread Chris Angelico
I'm responding to this on-list on the assumption that this wasn't
meant to be private; apologies if you didn't intend for this to be the
case!

On Wed, May 11, 2011 at 6:38 PM, vijay swaminathan swavi...@gmail.com wrote:
 so If i understand correctly, once the run method of the thread is executed,
 the thread is no more alive.

Once run() finishes executing, the thread dies.

 Actually, I'm trying to invoke a command prompt to run some script and as
 long as the script runs on the command prompt, I would like to have the
 thread alive. But according to your statement, the thread would die off
 after invoking the command prompt. is there a way to keep the thread active
 till I manually close the command prompt?

That depends on how the invoke command prompt function works.

 A snippet of the code written is:
 # Thread definition
 class RunMonitor(QThread):
     def __init__(self, parent=None):
     QThread.__init__(self)
     def run(self):
     print 'Invoking Command Prompt..'
     subprocess.call([start, /DC:\\Scripts,
 scripts_to_execute.bat], shell=True)

  def sendData(self):

     if self.run_timer:
     run_monitor_object = RunMonitor()
     print 'Starting the thread...'
     run_monitor_object.start()
     self.run_timer = False

     if run_monitor_object.isAlive():
     print 'Thread Alive...'
     else:
     print 'Thread is Dead'


subprocess.call() will return immediately, so this won't work. But if
you use os.system() instead, then it should do as you intend.

 to check the status of the thread repeatedly I have the QTimer which would
 call the self.sendData() for every minute.

     self.timer = QTimer()
     self.timer.connect(self.timer, SIGNAL(timeout()),self.sendData)
     self.timer.start(1000)

I'm not really sure what your overall goal is. Can you explain more of
your high-level intentions for this program? There may be a much
easier way to accomplish it.

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


Re: NewBie Doubt in Python Thread Programming

2011-05-11 Thread vijay swaminathan
Sorry. My intention was not to send out a private message. when I chose
reply to all, I was confused if this would start as a new thread. so just
did a reply..

coming back,

I have developed a GUI based on pyQT4 which has a run button. when I click
on run, it invokes a command prompt and runs a .bat file.

Till the execution of .bat file is over, I want the run button on the GUI to
be disabled. so i thought of invoking the command prompt and running the
.bat file on a thread so that I could monitor the status of the thread
(assuming that the thread would be active till command prompt is active -
correct me if I'm wrong).

for this, the code that I had written is;

# to invoke a command prompt and execute the .bat file.
class RunMonitor(threading.Thread):
def __init__(self, parent=None):
threading.Thread.__init__(self)
def run(self):
print 'Invoking Command Prompt..'
subprocess.call([start, /DC:\\Script, scripts_to_execute.bat],
shell=True)


A timer function to call this thread and monitor this thread..

 def runscript(self):
self.timer = QTimer()
self.timer.connect(self.timer, SIGNAL(timeout()),self.sendData)
self.timer.start(1000)

def sendData(self):


if self.run_timer:
run_monitor_object = RunMonitor()
print 'Starting the thread...'
run_monitor_object.start()
self.run_timer = False

if run_monitor_object.isAlive():
print 'Thread Alive...'
else:
print 'Thread is Dead'


Any flaw  in the logic? any other better ways of achieving this?


On Wed, May 11, 2011 at 2:16 PM, Chris Angelico ros...@gmail.com wrote:

 I'm responding to this on-list on the assumption that this wasn't
 meant to be private; apologies if you didn't intend for this to be the
 case!

 On Wed, May 11, 2011 at 6:38 PM, vijay swaminathan swavi...@gmail.com
 wrote:
  so If i understand correctly, once the run method of the thread is
 executed,
  the thread is no more alive.

 Once run() finishes executing, the thread dies.

  Actually, I'm trying to invoke a command prompt to run some script and as
  long as the script runs on the command prompt, I would like to have the
  thread alive. But according to your statement, the thread would die off
  after invoking the command prompt. is there a way to keep the thread
 active
  till I manually close the command prompt?

 That depends on how the invoke command prompt function works.

  A snippet of the code written is:
  # Thread definition
  class RunMonitor(QThread):
  def __init__(self, parent=None):
  QThread.__init__(self)
  def run(self):
  print 'Invoking Command Prompt..'
  subprocess.call([start, /DC:\\Scripts,
  scripts_to_execute.bat], shell=True)
 
   def sendData(self):
 
  if self.run_timer:
  run_monitor_object = RunMonitor()
  print 'Starting the thread...'
  run_monitor_object.start()
  self.run_timer = False
 
  if run_monitor_object.isAlive():
  print 'Thread Alive...'
  else:
  print 'Thread is Dead'
 

 subprocess.call() will return immediately, so this won't work. But if
 you use os.system() instead, then it should do as you intend.

  to check the status of the thread repeatedly I have the QTimer which
 would
  call the self.sendData() for every minute.
 
  self.timer = QTimer()
  self.timer.connect(self.timer, SIGNAL(timeout()),self.sendData)
  self.timer.start(1000)

 I'm not really sure what your overall goal is. Can you explain more of
 your high-level intentions for this program? There may be a much
 easier way to accomplish it.

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




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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On 07 May 2011 02:51:50 GMT, Steven D'Aprano
  steve+comp.lang.pyt...@pearwood.info wrote:
:  On Fri, 06 May 2011 14:57:21 -0700, scattered wrote:
: 
:  is there any problem with
:  
:  (3) if li == []:
:  
:  ?
:  
:  Seems to work when I test it and seems to clearly test what you are
:  trying to test. The only problem might be if in some contexts == has the
:  semantics of checking for object identity.
: 
:  Yes, if li == [] works too. But how do you know li is a list and not some 
:  other sequence type?

It says so in the Subject header :-)

:  The advantage of the if x test is that it is independent of the type of 
:  x.

Sure, but the question wasn't ...

The problem with 'if x' is that it requires a much more detailed 
understanding of python.  li == [] is as explicit as it gets, and
leaves no room for doubt.  len(li) == 0 is almost as explicit and
much more flexible.  Just x is as generic as it gets, but depends
on python's convolved rules for duck processing and if you aim at
legibility it is better avoided.


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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On 07 May 2011 02:49:53 GMT, Steven D'Aprano
  steve+comp.lang.pyt...@pearwood.info wrote:
:  On Fri, 06 May 2011 16:05:09 -0400, Adam Tauno Williams wrote:
: 
:  I'd never accept code like if not x as an empty test.
: 
:  So much the worse for you then.
: 
:  The point of the if x idiom is that it is a polymorphic test which is 
:  independent of the type.

Normally, polymorphisms implies multiple forms only, where the different
forms has some form of common interpretation.  That's what makes
polymorphism useful and helpful, increasing legibility.

In this case, the interpretation of an arbitrary object as a boolean
is peculiar for python.  An empty list is a real, existing object, and 
the supposition that [] be false is counter-intuitive.  It can be
learnt, and the shorthand may be powerful when it is, but it will 
confuse many readers.

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


Re: checking if a list is empty

2011-05-11 Thread Laurent Claessens

In this case, the interpretation of an arbitrary object as a boolean
is peculiar for python.  An empty list is a real, existing object, and
the supposition that [] be false is counter-intuitive.  It can be
learnt, and the shorthand may be powerful when it is, but it will
confuse many readers.


Once I wrote something like:

def f(x=None):
   if x:
  print x
   else:
  print I have no value


The caller of that function was something like f(cos(2*theta)) where 
theta come from some computations.


Well. When it turned out that theta was equal to pi/4, I got I have no 
value. I spent a while to figure out the problem :)


Conclusion: the boolean value of an object is to be used with care in 
order to tests if an optional parameter is given or not (when default 
value is None).



Have a good noon
Laurent
--
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On Sat, 07 May 2011 21:57:13 -0700, Ethan Furman
  et...@stoneleaf.us wrote:
:  If you're going to use a language, and use it well, you have to learn 
:  how that language works.

And if the world evolves around the compiler and you, that advice
suffices.

However, programming is often as much about developing ideas in a large
and complex community, where perfect universal mastery of one language
is not an option, because half the community do not normally use that
language or aren't really programmers at all.  The less you assume about
the skill of the reader, the better it is.

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


Re: NewBie Doubt in Python Thread Programming

2011-05-11 Thread Chris Angelico
On Wed, May 11, 2011 at 7:08 PM, vijay swaminathan swavi...@gmail.com wrote:
 Sorry. My intention was not to send out a private message. when I chose
 reply to all, I was confused if this would start as a new thread. so just
 did a reply..

No probs. If you just send your response to the list
python-list@python.org. it'll get to everyone.

 I have developed a GUI based on pyQT4 which has a run button. when I click
 on run, it invokes a command prompt and runs a .bat file.

 Till the execution of .bat file is over, I want the run button on the GUI to
 be disabled. so i thought of invoking the command prompt and running the
 .bat file on a thread so that I could monitor the status of the thread
 (assuming that the thread would be active till command prompt is active -
 correct me if I'm wrong).

Yes, but only if you use os.system().

 Any flaw  in the logic? any other better ways of achieving this?


You'll find it easier to get an event at the end of it; simply have
another line of code after the os.system() which will reenable the
button.

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


Re: checking if a list is empty

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 11:48:16 +0200, Laurent Claessens wrote:

 Once I wrote something like:
 
 def f(x=None):
 if x:
print x
 else:
print I have no value
 
 
 The caller of that function was something like f(cos(2*theta)) where
 theta come from some computations.
 
 Well. When it turned out that theta was equal to pi/4, I got I have no
 value. I spent a while to figure out the problem :)

I believe you are grossly oversimplifying whatever code you had. Using 
the definition of f from above:

 theta = math.pi/4
 f(math.cos(2*theta))
6.12303176911e-17

But even if you rounded the result of cos(2*theta) to zero, you will get 
the same result regardless of whether you test for if x or if x != 0.


 Conclusion: the boolean value of an object is to be used with care in
 order to tests if an optional parameter is given or not (when default
 value is None).

Or, to put it another way: if you want to test for an object being None, 
test for the object being None.


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


HELP. This it is the code of tmb_import.py (to matter of .tmb to blender) I need tmb_exporter.py (to export of blender to .tmb) Thanks.

2011-05-11 Thread Jean Carlos Páez Ramírez









Buen día comunidad 



Hola.



El archivo adjunto es un script de blender hecho en python que sirve para
importar archivos .tmb (http://www.mediafire.com/?clmdgkymsfooddd), descargas 
blender y usa el
script e importa el segundo archivo que te envio para que lo veas 
(http://www.mediafire.com/?lbmj594ru6r4b67). El se puede
editar y todo, pero no se puede exportar de nuevo a .tmb







link del
script: http://www.mediafire.com/?clmdgkymsfooddd

Segundo archivo adjunto: http://www.mediafire.com/?lbmj594ru6r4b67



PD: Necesito el script para exportar en extension .tmb En si necesito ayuda en 
ese diseño de un script que exporte de .blend a .tmb







Gracias.

_



Hello. 



The attached file is script of blender fact in python that .tmb serves to
concern archives (secondly attached file), unloadings to blender and uses
script and concerns the second file that you shipment you see so that it.
Everything can be published and, but it is not possible to be exported again to
.tmb 



To unload in this Link: http://www.blender.org/

script: http://www.mediafire.com/?clmdgkymsfooddd

secondly attached file: http://www.mediafire.com/?lbmj594ru6r4b67



PD: I need script to export in extension .tmb



Thanks.













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


HELP. This it is the code of tmb_import.py (to matter of .tmb to blender) I need tmb_exporter.py (to export of blender to .tmb) Thanks.

2011-05-11 Thread Jean Carlos Páez Ramírez

Buen día comunidad 



Hola.



El archivo adjunto es un script de blender hecho en python que sirve para
importar archivos .tmb (http://www.mediafire.com/?clmdgkymsfooddd), descargas 
blender y usa el
script e importa el segundo archivo que te envio para que lo veas 
(http://www.mediafire.com/?lbmj594ru6r4b67). El se puede
editar y todo, pero no se puede exportar de nuevo a .tmb







link del
script: http://www.mediafire.com/?clmdgkymsfooddd

Segundo archivo adjunto: http://www.mediafire.com/?lbmj594ru6r4b67



PD: Necesito el script para exportar en extension .tmb En si necesito ayuda en 
ese diseño de un script que exporte de .blend a .tmb







Gracias.

_



Hello. 



The attached file is script of blender fact in python that .tmb serves to
concern archives (secondly attached file), unloadings to blender and uses
script and concerns the second file that you shipment you see so that it.
Everything can be published and, but it is not possible to be exported again to
.tmb 



To unload in this Link: http://www.blender.org/

script: http://www.mediafire.com/?clmdgkymsfooddd

secondly attached file: http://www.mediafire.com/?lbmj594ru6r4b67



PD: I need script to export in extension .tmb



Thanks.













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


Re: checking if a list is empty

2011-05-11 Thread Laurent Claessens



I believe you are grossly oversimplifying whatever code you had. Using
the definition of f from above:


 theta = math.pi/4
 f(math.cos(2*theta))

6.12303176911e-17


Yes: its oversimplifued. The angle come from a normal vector of a curve 
and so on In particular, I was using Sage; the computations are 
exact: pi is pi and cos(pi) is zero.



 Conclusion: the boolean value of an object is to be used with care in
 order to tests if an optional parameter is given or not (when default
 value is None).


Or, to put it another way: if you want to test for an object being None,
test for the object being None.


It was my conclusion too ;)

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


Re: checking if a list is empty

2011-05-11 Thread Laurent Claessens



I believe you are grossly oversimplifying whatever code you had. Using
the definition of f from above:


 theta = math.pi/4
 f(math.cos(2*theta))

6.12303176911e-17


Yes: its oversimplifued. The angle come from a normal vector of a curve 
and so on In particular, I was using Sage; the computations are 
exact: pi is pi and cos(pi) is zero.



 Conclusion: the boolean value of an object is to be used with care in
 order to tests if an optional parameter is given or not (when default
 value is None).


Or, to put it another way: if you want to test for an object being None,
test for the object being None.


It was my conclusion too ;)

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


Re: checking if a list is empty

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 10:02:42 +0100, Hans Georg Schaathun wrote:

 The problem with 'if x' is that it requires a much more detailed
 understanding of python.

Much more detailed? Hardly.

Understanding that Python accepts any and all objects in truth-testing 
concepts, and the rules thereof, is Python 101 territory. It's beginner-
level knowledge. It is far less advanced than the knowledge that ** is 
used for exponentiation. After all, many programmers have never needed to 
raise a number to a power, and might not learn about it for years, but 
every programmer writes if or while statements at some point.

Not knowing that you can write if x instead of if x == [] is like not 
knowing that you can write 

elif condition

instead of 

else:
if condition

If somebody were to argue that it is better to write else if explicitly, 
instead of the confusing elif, we'd all laugh at them.

Every time the question of conditional testing comes up here, it never 
ceases to astonish me how many developers argue against learning the 
idioms of the language, and prefer to re-use the idioms of other 
languages in Python.

Python is an object-oriented language where objects get to decide for 
themselves whether they should be treated as true or false. Writing:

if x == []:

instead of 

if x:

merely[1] because you worry that it isn't explicit enough, or could 
confuse other developers, or out of some nagging concern that maybe 
Python will do the wrong thing[2] unless you hold its hand through the 
process, is as silly as writing this:

count = 0
for item in x:
count += 1


instead of:

count = len(x)

(As silly, but not as verbose.)

I don't mean to insult anyone, but I've heard and read all the arguments 
against Python's truth-testing, and they don't impress me in the 
slightest. Most of them strike me as silly. The only argument that 
carries any weight to me is one which I haven't seen anyone raise:

if x: turns something which arguably could have been a mistake (oops, 
I forgot to write the condition!) into valid code.





[1]  It may be that there are good, solid reasons for writing explicit 
len(x)==0 tests, although I'm hard-pressed to think of any. The closest I 
come to is when you wish to emphasize equal to some number that just 
happens to be zero rather than it's a false/empty value. If so, you 
get a free pass to write the test the long way. E.g. you might write 
x % 2 == 1 rather than just x % 2 because you want to highlight that 
the remainder equals one, rather than the remainder merely being a true 
value.


[2]  Of course, a custom object x might misbehave when you test it for 
truth value. That would be a bug, just like it would be a bug if it 
misbehaved when you call len(x) == 0. If you can't trust if x to work, 
what makes you think you can trust len(x) == 0 either?


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


Re: Non Programming in python

2011-05-11 Thread Calvin Spealman
If this is the non-programming side of python then maybe some of us have a
lacking definition of what programming is.  My mechanic stilll has to
check the tire pressure and I need to update the version number in PyPI.

On May 10, 2011 12:46 PM, rusi rustompm...@gmail.com wrote:

Sorry for a silly subject change: A better one will be welcome -- cant
think of a name myself.

There is this whole area of python that may be called the non-
programming side of programming:

Is there some central site where all such is put up?
What if any should such a bundle of things be called?

-

 | Area | Tool(s)|
 |--+|
 | packaging| distutils, setuptools, |
 |  | distutils2, distribute |
 |  | Native tools (eg apt)  |
 | versioning   | hg, git, bzr   |
 | multiple pythons | virtualenv |
 | ??   | tox|
 | testing  | unittest, nose, pytest |
 | build| scons, make... |
 | deployment   | fabric |

--
* Primary Development tools/aids

 1. Help
 2. Completion ('intellisense')
 3. Tags (Jumping)
 4. Refactoring
 5. Integration with 'non-programming' above (eg VCSes, packagers
etc)

* Other Development Tools
 - Debugger
 - Profiler
 - Heap Profiler
 - Coverage
--
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Merge multiple source directories into one package with distutils?

2011-05-11 Thread Miki Tebeka
Greg,
 Is there a straightforward way to tell distutils to merge
.py files from more than one source directory into a single
package when installing?
The Selenium Python bindings does something like that, have a look at 
http://selenium.googlecode.com/svn/trunk/setup.py

The other option is to write some code in setup.py before calling setup to do 
the merge.

HTH
--
Miki Tebeka miki.teb...@gmail.com
http://pythonwise.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python backup programs?

2011-05-11 Thread Emile van Sebille

On 5/10/2011 4:00 PM Dan Stromberg said...


What are your favorite backup programs written, in whole or in part, in
Python?


bup


What do you like about them?


resilient and written in python


Dislike about them?


lack of a user accessible front-end to monitor and restore


Are there any features you wish your backup program had, whether in
Python or not?


I like backuppc because of it's front-end.

Emile




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


Re: checking if a list is empty

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 10:14:38 +0100, Hans Georg Schaathun wrote:

 In this case, the interpretation of an arbitrary object as a boolean is
 peculiar for python.  

Incorrect. It is widespread among many languages. Programmers have been 
writing conditional tests using arbitrary values since 1958 when Lisp 
introduced the concept.

C, Forth and Visual Basic treat any non-zero number as true, and zero as 
false; that's not quite arbitrary objects, but it's arbitrary integer 
values. Similarly, Objective C has two different boolean types, BOOL 
which is a C char where 0 is false and everything else is true, and 
bool which is more like the Java boolean type.

Perl treats the empty string, 0, 0 and undefined variables as false, 
and everything else as true.

Ruby treats null and false as false, and everything else as true.

JavaScript treats , null, undefined, NaN, 0 and false as false values, 
and everything else as true.

PHP treats FALSE, 0, , 0, empty arrays, objects with no member 
variables, NULL, unset variables, and SimpleXML objects created from 
empty tags as false, everything else as true.

Clojure treats nil and false as false, everything else as true.

SQL's boolean type has three or four values: true, false, null and 
unknown, where implementations are free to treat null and unknown as 
either the same or different values. (So a 3 or 4 value logic, not 
actually Boolean at all.)

So Python is hardly unique, nor is this some new-fangled innovation.



 An empty list is a real, existing object, and the
 supposition that [] be false is counter-intuitive.

Not to me, nor to anyone who has an intuition about something versus 
nothing. I believe this distinction is fundamental to the universe, and 
that nearly every person understands this intuitively. The distinction 
between something and nothing is so strong that it took centuries of 
argument for the finest minds in Europe[1] to finally decide that, yes, 
zero is a number -- and they only did it because the Arabs and Indians 
had proven how useful it was, and Roman numerals really do suck for doing 
calculations.



 It can be learnt,
 and the shorthand may be powerful when it is, but it will confuse many
 readers.

In my experience, it does not confuse newbies or beginners. The only 
people it confuses are those who are over-educated into thinking that the 
One Correct Way of doing truth testing is with a dedicated boolean type, 
and anything else is heresy.


[1] At least is you asked them.


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


Re: Py3k,email header handling

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 16:04:13 +0800, TheSaint wrote:

 Now python 3.2 (and some version before) started to use byte, rather
 than text strings, almost for all data handling in compliance to
 unicode. My problem arise that my program handle text strings, so I'd
 like to rewrite the code

Before you re-write it, you should run 2to3 over it and see how much it 
can do automatically:

http://docs.python.org/library/2to3.html


 My program reads from IMAP4 or POP3 server, I'd prefer that a
 function/class will return either a list or a dictionary which contains
 the following fields:
 
 'from', 'to', 'cc', 'bcc', 'date', 'subject', 'reply-to', 'message-id'
 
 The list may be organized as tuple (from, its_content,), etc,etc for
 each field, but I think dictionary would be more efficient to use.
 
 1) is there a way to call the IMAPlib or POPlib and pass the data
 directly to email.parser.HeaderParser to achieve my intention?

I'm afraid I don't understand the question.


 2) If the above will do, do re.compile compile unicode results? I guess
 yes.

Yes. In Python 3, re.compile(some string) is automatically unicode, 
because some string is unicode.


 3) any related documentation...

http://docs.python.org/py3k/library/email.html
http://docs.python.org/py3k/library/re.html
http://docs.python.org/py3k/library/imaplib.html
http://docs.python.org/py3k/library/poplib.html


If you have any more concrete questions, please ask.



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


Re: checking if a list is empty

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 11:47:42 +0100, Hans Georg Schaathun wrote:

 On Sat, 07 May 2011 21:57:13 -0700, Ethan Furman
   et...@stoneleaf.us wrote:
 :  If you're going to use a language, and use it well, you have to learn
 :  how that language works.
 
 And if the world evolves around the compiler and you, that advice
 suffices.
 
 However, programming is often as much about developing ideas in a large
 and complex community, where perfect universal mastery of one language
 is not an option, because half the community do not normally use that
 language or aren't really programmers at all.  The less you assume about
 the skill of the reader, the better it is.

Do you think that we should avoid chained comparisons, class methods, 
closures, co-routines, decorators, default values to functions, 
delegation, doc tests, exceptions, factory functions, generator 
expressions, inheritance, iterators, list comprehensions, operator 
overloading, properties, regular expressions, tuple unpacking, or unit 
tests, to say nothing of *advanced* techniques like descriptors or 
metaclasses, just in case the person reading your code is a clueless n00b?

We routinely and uncontroversially use all of these techniques (well, 
sometimes metaclasses get a few raised eyebrows). Truth testing is MUCH 
simpler than any of those.

It is extremely patronizing to say that we should avoid truth-testing 
arbitrary objects because it is too complicated for other people. It's 
not hard, and they can learn.



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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On 11 May 2011 13:36:02 GMT, Steven D'Aprano
  steve+comp.lang.pyt...@pearwood.info wrote:
:  In this case, the interpretation of an arbitrary object as a boolean is
:  peculiar for python.  
: 
:  Incorrect. It is widespread among many languages. Programmers have been 
:  writing conditional tests using arbitrary values since 1958 when Lisp 
:  introduced the concept.

The fact that you need to list language by language which objects
evaluate as false or equivalent to false illustrates that this has
to be learnt language by language.  Allowing arbitrary objects is
one thing, the particular interpretation is peculiar.

The fact that if and while accepts any object for the condition may
be chapter 1 stuff, but the memorisation of exactly how the
interpretation does not come early (unless you learn it by rote of
course).

:  C, Forth and Visual Basic treat any non-zero number as true, and zero as 
:  false; that's not quite arbitrary objects, but it's arbitrary integer 
:  values. Similarly, Objective C has two different boolean types, BOOL 
:  which is a C char where 0 is false and everything else is true, and 
:  bool which is more like the Java boolean type.

Mentioning C, with no Boolean type at all, in this context is a bit
absurd.  Understanding boolean evaluation in C is very little help
when you want to understand it in python.

:  An empty list is a real, existing object, and the
:  supposition that [] be false is counter-intuitive.
: 
:  Not to me, nor to anyone who has an intuition about something versus 
:  nothing. I believe this distinction is fundamental to the universe, and 
:  that nearly every person understands this intuitively. The distinction 
:  between something and nothing is so strong that it took centuries of 
:  argument for the finest minds in Europe[1] to finally decide that, yes, 
:  zero is a number -- and they only did it because the Arabs and Indians 
:  had proven how useful it was, and Roman numerals really do suck for doing 
:  calculations.

Exactly.  By now we have gotten past that old-fashioned idea that 0
is not a number.  Computer scientists even tend to count 0 as a
natural number.  When 0 is a number as real and existent as any other,
one would think that the empty list is also as real and existent as
any other list.

:  In my experience, it does not confuse newbies or beginners. The only 
:  people it confuses are those who are over-educated into thinking that the 
:  One Correct Way of doing truth testing is with a dedicated boolean type, 
:  and anything else is heresy.

What kind of beginners are you talking about?  Beginners to python
or beginners to programming.

The audience I am concerned about is the ones who are over-educated 
into using and having used a score of different meanings of the same
symbols.  They will be used to their intuition being wrong when they
move into a new context.  Being explicit will help them.


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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On 11 May 2011 12:14:46 GMT, Steven D'Aprano
  steve+comp.lang.pyt...@pearwood.info wrote:
:  Not knowing that you can write if x instead of if x == [] is like not 
:  knowing that you can write 
: 
:  elif condition
: 
:  instead of 
: 
:  else:
:  if condition

My concern was with the reader and not the writer.

What could elif mean other than else: if?

if x could, for instance,  mean if x is defined.


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


Re: checking if a list is empty

2011-05-11 Thread D'Arcy J.M. Cain
On Wed, 11 May 2011 11:47:42 +0100
Hans Georg Schaathun h...@schaathun.net wrote:
 However, programming is often as much about developing ideas in a large
 and complex community, where perfect universal mastery of one language
 is not an option, because half the community do not normally use that
 language or aren't really programmers at all.  The less you assume about
 the skill of the reader, the better it is.

Non-programmers should be able to program?

Should non-doctors be able to doctor?  Should cars be built so that
anyone can intuitively fix them without a mechanic?  Should trucks be
built so that drivers don't have to learn how to split shift?  Why is
programming so different that we can't expect people to actually learn
their discipline?

This discussion is giving me some insight into some of the crap
programming I see these days.

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread D'Arcy J.M. Cain
On Wed, 11 May 2011 15:05:45 +0100
Hans Georg Schaathun h...@schaathun.net wrote:
 What could elif mean other than else: if?

If run by an elf?  Who knows.  You do, of course, if you have learned
the basics of the language you are using.

 if x could, for instance,  mean if x is defined.

It could also mean if x was created on a Tuesday.  A short
introduction to the language explains what it actually means.

When did we come to the idea that people should be able to program in a
language without actually learning it?  The fact that Python comes so
close to that possibility is nothing short of revolutionary.  I suppose
one day a reasoning android will be able to sit down at the terminal of
a star ship computer and ask simple questions while making random hand
movements across a screen but for now I am afraid that programmers
still have to learn programming.

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On 11 May 2011 13:45:52 GMT, Steven D'Aprano
  steve+comp.lang.pyt...@pearwood.info wrote:
:  Do you think that we should avoid chained comparisons, class methods, 
:  closures, co-routines, decorators, default values to functions, 
:  delegation, doc tests, exceptions, factory functions, generator 
:  expressions, inheritance, iterators, list comprehensions, operator 
:  overloading, properties, regular expressions, tuple unpacking, or unit 
:  tests, to say nothing of *advanced* techniques like descriptors or 
:  metaclasses, just in case the person reading your code is a clueless n00b?

Not at all.  On both accounts.
1. My concern was not about clueless newbies.  They need to
  learn.  My concern is about experienced scientists and engineers 
  who are simply new to python.  They will be dealing with a dozen
  different languages (programming and otherwise), with a need to
  read more languages than they can possibly learn to master.

2. You should avoid constructs which can /reasonably/ be avoided to 
  /increase/ legibility.  Writing if x for if len(x)  0 when you
  know that x is of some sort of collection type with len defined
  does nothing to help legibility.  Many of the socalled advanced
  constructs you mention are used to increase legibility one way or
  another.  Others will simply allow functionality which would otherwise
  be impossible.

  I don't object to using if x in a case where x may or may not have
  len() defined.

:  We routinely and uncontroversially use all of these techniques (well, 
:  sometimes metaclasses get a few raised eyebrows). Truth testing is MUCH 
:  simpler than any of those.

Simpler, yes, but it surely depends on more detailed knowledge.  One has
to remember how the boolean conversion works, for each possible data type.
This requires looking up the rules for each data type.

E.g. Anyone who has used list/set comprehension in Z, haskell, set theory,
or whereever will understand python list comprehension immediately.

:  It is extremely patronizing to say that we should avoid truth-testing 
:  arbitrary objects because it is too complicated for other people. It's 
:  not hard, and they can learn.

Again you miss the point.  It is not /too/ complicated.
It is /unnecessarily/ complicated.  You don't noticeably
gain anything by using if x instead of if len(x)  0, if
you know that the latter is defined.  

If everyone who ever needs to see your program is a python
programmer, then your approach works as well as mine.  

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


Re: checking if a list is empty

2011-05-11 Thread Chris Angelico
On Thu, May 12, 2011 at 12:00 AM, Hans Georg Schaathun h...@schaathun.net 
wrote:
 The fact that you need to list language by language which objects
 evaluate as false or equivalent to false illustrates that this has
 to be learnt language by language.  Allowing arbitrary objects is
 one thing, the particular interpretation is peculiar.

Languages have to be learned language by language. Yes, you want to be
similar to others if you can (and in 'most every language where it's
legal syntax, 1+2 will mean 3), but whenever I'm working in a
language with which I'm not _really_ fluent, I like to keep an
operator table handy (precedence, plus oddments like what the
exponentiation operator is, or whether  is bitwise or boolean). They
do differ, and if I'm writing in PHP or Python or Pike or C++ or Java
or whatever else it be, I have to use the operators the way the
interpreter/compiler will understand them, not in some other way that
I deem better.

 Mentioning C, with no Boolean type at all, in this context is a bit
 absurd.  Understanding boolean evaluation in C is very little help
 when you want to understand it in python.

Actually, it's not.

# Python code:
if x:
 statements

/* C code: */
if (x) {
 statements;
}

What's the difference? Both of them let you test the truth of some
arbitrary object or expression, and neither of those statements
implies a Boolean type. In C++, this is exploited extensively with,
for instance, the stream I/O objects evaluating as false when in an
error or EOF state:

while (infile) infile  *ptr++; // Compact and convenient way to read
into an array

Actually that one can be combined down even further, but for clarity I
leave it like that.

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


Re: NewBie Doubt in Python Thread Programming

2011-05-11 Thread Wojtek Mamrak
Is there any special reason you don't want to use QThread?
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qthread.html#details

regards

2011/5/11 Chris Angelico ros...@gmail.com:
 On Wed, May 11, 2011 at 7:08 PM, vijay swaminathan swavi...@gmail.com wrote:
 Sorry. My intention was not to send out a private message. when I chose
 reply to all, I was confused if this would start as a new thread. so just
 did a reply..

 No probs. If you just send your response to the list
 python-list@python.org. it'll get to everyone.

 I have developed a GUI based on pyQT4 which has a run button. when I click
 on run, it invokes a command prompt and runs a .bat file.

 Till the execution of .bat file is over, I want the run button on the GUI to
 be disabled. so i thought of invoking the command prompt and running the
 .bat file on a thread so that I could monitor the status of the thread
 (assuming that the thread would be active till command prompt is active -
 correct me if I'm wrong).

 Yes, but only if you use os.system().

 Any flaw  in the logic? any other better ways of achieving this?


 You'll find it easier to get an event at the end of it; simply have
 another line of code after the os.system() which will reenable the
 button.

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

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


Re: NewBie Doubt in Python Thread Programming

2011-05-11 Thread Chris Angelico
On Thu, May 12, 2011 at 1:16 AM, Wojtek Mamrak tacyt1...@gmail.com wrote:
 Is there any special reason you don't want to use QThread?
 http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qthread.html#details

Other than that QThread is part of QT and threading isn't, what are
the advantages of QThread? Is it possible (safe) to manipulate QT
objects - in this case, the button - from a thread other than the one
that created them? (If not, that would be a good reason for using
QThread, which will fire an event upon termination.)

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


Re: checking if a list is empty

2011-05-11 Thread Redcat
On Wed, 11 May 2011 10:33:51 -0400, D'Arcy J.M. Cain wrote:

 Non-programmers should be able to program?

Wasn't that sort of the premise behind Visual Basic? I don't know if that 
was the intention, but it sure was the result in a lot of cases.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Ethan Furman

Hans Georg Schaathun wrote:

On 11 May 2011 13:36:02 GMT, Steven D'Aprano
  steve+comp.lang.pyt...@pearwood.info wrote:
:  In this case, the interpretation of an arbitrary object as a boolean is
:  peculiar for python.  
: 
:  Incorrect. It is widespread among many languages. Programmers have been 
:  writing conditional tests using arbitrary values since 1958 when Lisp 
:  introduced the concept.


The fact that you need to list language by language which objects
evaluate as false or equivalent to false illustrates that this has
to be learnt language by language.  Allowing arbitrary objects is
one thing, the particular interpretation is peculiar.



Like so many other things Python got right, I think it got this right as 
well.  something vs nothing is simple, useful, and easy to remember.




By now we have gotten past that old-fashioned idea that 0
is not a number.  Computer scientists even tend to count 0 as a
natural number.  When 0 is a number as real and existent as any other,
one would think that the empty list is also as real and existent as
any other list.


Python is not concerned with whether it exists -- that's a name binding; 
 Python is concerned with whether anything is there.  0 apples is 
nothing and a an empty list is nothing as well.


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


Re: checking if a list is empty

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 15:05:45 +0100, Hans Georg Schaathun wrote:

 My concern was with the reader and not the writer.
 
 What could elif mean other than else: if?

It could mean Oh, the author has made a stupid typo, I better fix it.

It could mean What does the elif command do?

The first time I read Python code, I had literally no idea what to make 
of elif. It seemed so obvious to me that any language would let you write 
else if ... (on a single line) that I just assumed that elif must be 
some other construct, and I had no idea what it was. It never even 
crossed my mind that it could be else if rammed together into one word.

I soon learned better though.

Once you start dumbing down your code for readers who don't know your 
language, it's a race to the bottom. There's very little you can write 
that *somebody* won't misunderstand.



 if x could, for instance,  mean if x is defined.

Yes it could, if you're programming in Perl. But we're not.

When I write a sentence in English, and I use the word gift to mean a 
thing which is given, I don't worry that German or Swedish readers will 
think I'm talking about poison. If I use preservative, I mean something 
which preserves, and if Italian and Spanish readers mistake it for a 
condom, that's their problem, not mine. Writing code is no different. 
When I'm coding in Python, I use Python rules and meanings, not some 
other language.

Why should I code according to what some hypothetical Python dummy 
*might* think the code will do, instead of what the code *actually* does?



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


Re: NewBie Doubt in Python Thread Programming

2011-05-11 Thread Wojtek Mamrak
2011/5/11 Chris Angelico ros...@gmail.com:
 On Thu, May 12, 2011 at 1:16 AM, Wojtek Mamrak tacyt1...@gmail.com wrote:
 Is there any special reason you don't want to use QThread?
 http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qthread.html#details

 Other than that QThread is part of QT and threading isn't, what are
 the advantages of QThread? Is it possible (safe) to manipulate QT
 objects - in this case, the button - from a thread other than the one
 that created them? (If not, that would be a good reason for using
 QThread, which will fire an event upon termination.)



QThread provides mechanism of signals and slots (from and to the
thread), which are used across all pyQt. Unfortunately it is not
possible to use any widget classes in the thread (direct quote from
the docs). On the other hand signals can fire methods from the main
thread (running the app'a main loop), so this is not a big deal.
The signals are:
- finished
- started
- terminated
It is possible to block the thread, make it sleep, check whether the
thread is running, and few others.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Chris Angelico
On Thu, May 12, 2011 at 1:50 AM, Steven D'Aprano
steve+comp.lang.pyt...@pearwood.info wrote:
 On Wed, 11 May 2011 15:05:45 +0100, Hans Georg Schaathun wrote:

 My concern was with the reader and not the writer.

 What could elif mean other than else: if?

 The first time I read Python code, I had literally no idea what to make
 of elif. It seemed so obvious to me that any language would let you write
 else if ... (on a single line) that I just assumed that elif must be
 some other construct, and I had no idea what it was. It never even
 crossed my mind that it could be else if rammed together into one word.

In a Bourne shell script, if ends with fi... case ends with esac... so
file would end with... hmm. Yeah, I think it's best to know the
language you're trying to comprehend, and/or actually look at context
instead of shoving a piece of code under someone's nose and saying I
bet you can't figure out what THIS does!.

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


Re: checking if a list is empty

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 15:34:28 +0100, Hans Georg Schaathun wrote:

 On 11 May 2011 13:45:52 GMT, Steven D'Aprano
   steve+comp.lang.pyt...@pearwood.info wrote:
 :  Do you think that we should avoid chained comparisons, class methods,
 :  closures, co-routines, decorators, default values to functions, : 
 delegation, doc tests, exceptions, factory functions, generator : 
 expressions, inheritance, iterators, list comprehensions, operator : 
 overloading, properties, regular expressions, tuple unpacking, or unit :
  tests, to say nothing of *advanced* techniques like descriptors or : 
 metaclasses, just in case the person reading your code is a clueless
 n00b?
 
 Not at all.  On both accounts.
 1. My concern was not about clueless newbies.  They need to
   learn.  My concern is about experienced scientists and engineers who
   are simply new to python.

Which makes them clueless newbies *about Python*. I don't care how 
experienced they are in astrophysics or biology or calculating the 
average airspeed of an unladen swallow.


   They will be dealing with a dozen different
   languages (programming and otherwise), with a need to read more
   languages than they can possibly learn to master.

Yeah, life is hard and then you die, and scientists don't even get paid 
that much. So what? Do physicists write their scientific papers about 
string theory with the thought What if some Python programmer who knows 
nothing about string theory is reading this? I better dumb it down.

Of course not. A ridiculous idea. They use their tools the way they are 
designed to be used, and outsiders have to learn the language and the 
jargon to make sense of it. This is not a problem that needs solving.

It's one thing to simplify code that you are explicitly using as a 
teaching aid. That's a good thing, I approve of that. But it's a 
completely different thing to dumb down production code because you think 
some non-programmer might have to read it.



 2. You should avoid constructs which can /reasonably/ be avoided to
   /increase/ legibility.  Writing if x for if len(x)  0 when you know
   that x is of some sort of collection type with len defined does
   nothing to help legibility.

Of course it does. 

It means you don't have to care about implementation details of what 
emptiness means for a list. It means you don't force the reader to read, 
parse and understand three extraneous terms. It means you don't force the 
reader to wonder why you bothered to use a long-winded test when a short 
test would do, or be concerned that maybe  0 is a typo and you actually 
meant  10.


[...]
 Simpler, yes, but it surely depends on more detailed knowledge.  One has
 to remember how the boolean conversion works, for each possible data
 type. This requires looking up the rules for each data type.

No. You have that exactly backwards. The point of if x is that you 
DON'T have to care about how the boolean conversion works, because the 
object itself encapsulates that behaviour. This is basic object-oriented 
principles.

When you call len(x) you don't care about the details of how to calculate 
the length of x. The object itself knows so that you don't have to. The 
same applies to truth testing.

I have a data type that is an array of lists. When you call if len(x)  
0 on it, it will blow up in your face, because len(x) returns a list of 
lengths like [12, 0, 2, 5]. But if you say if x, it will do the right 
thing. You don't need to care how to truth-test my data type, because it 
does it for you. By ignoring my type's interface, and insisting on doing 
the truth-test by hand, you shoot yourself in the foot.



 E.g. Anyone who has used list/set comprehension in Z, haskell, set
 theory, or whereever will understand python list comprehension
 immediately.

Yes. And anyone who hasn't, probably won't. But they will learn.



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


Re: Py3k,email header handling

2011-05-11 Thread TheSaint
Steven D'Aprano wrote:

 Before you re-write it, you should run 2to3 over it and see how much it
 can do automatically:

Widely done, only the results from some query has radically changed on 
favour of unicode. Errors raising about results which are not strings 
anymore.
 
 I'm afraid I don't understand the question.

Making an example :

from poplib import POP3 as pop3
pop3.user('userid')
pop3.pass_('password')
numMsg, total = pop3.stat()
for cnt in numMsgs:
   header = pop3.top(cnt)
   # here I'd like to pass the header to some function that will return
   # a dictionary filling
   # from', 'to', 'cc', 'bcc', 'date', 'subject', 'reply-to', 'message-id'
   # keys, if nothing the leave empty string or None
   dict = email.header,decode_header(header) # but might not my result
   # Should I subclass this?

The same would be from the IMAP4 message parsing. Some different process 
would take, would it ?

 If you have any more concrete questions, please ask.

If these aren't concrete questions, forgive me, I perhaps got into wrong 
news group.
In the other and I hugely apreciated your clues. I'll see the docs some more 
long to achieve a clear learning.

-- 
goto /dev/null
-- 
http://mail.python.org/mailman/listinfo/python-list


py3k buffered IO - flush() required between read/write?

2011-05-11 Thread Genstein

Hey all,

Apologies if this is a dumb question (self = Python noob), but under 
py3k is it necessary to flush() a file between read/write calls in order 
to see consistent results?


I ask because I have a case under Python 3.2 (r32:88445) where it does 
appear to be, on both Gentoo Linux and Windows Vista.


I've naturally read http://docs.python.org/py3k/library/io.html and 
http://docs.python.org/py3k/tutorial/inputoutput.html#reading-and-writing-files 
but could find no reference to such a requirement.


PEP 3116 suggested this might not be required in py3k and the 
implementation notes in bufferedio.c state BufferedReader, 
BufferedWriter and BufferedRandom...share a single buffer...this enables 
interleaved reads and writes without flushing. Which seemed conclusive 
but I'm seeing otherwise.


I have a test case, which is sadly rather long: 
http://pastebin.com/xqrzKr5D It's lengthy because it's autogenerated 
from some rather more complex code I'm working on, in order to reproduce 
the issue in isolation.


Any advice and/or flames appreciated.

All the best,

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


Re: checking if a list is empty

2011-05-11 Thread Ian Kelly
On Wed, May 11, 2011 at 8:34 AM, Hans Georg Schaathun h...@schaathun.net 
wrote:
 E.g. Anyone who has used list/set comprehension in Z, haskell, set theory,
 or whereever will understand python list comprehension immediately.

They would understand the underlying concept.  But would somebody who
is not a Python programmer intuitively understand the difference
between this:

[x + 3 for x in xs if x % 2 == 1]

and this:

{x + 3 for x in xs if x % 2 == 1}

and this:

(x + 3 for x in xs if x % 2 == 1)

Somebody with rudimentary Python knowledge might even reason that
since the first two create a list and a set respectively, the third
must therefore create a tuple, which is wrong.

None of this should be taken as an argument against using generator expressions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Eric Snow
All of this just boils down to Python providing an implicit bool cast in
syntactic situations that expect conditional expressions, including if [1].
 So if x: is equivalent to if bool(x).  Perhaps that is the part that is
not immediately obvious, being implicit to Python conditional syntax.

Python also provides a mechanism for customization of a wide variety of
behavior that is not obvious without reading the [excellent] documentation
[2].  So the following three forms will implicitly call the special method
names:

if x:  =  if bool(x):  =  if x.__bool__():  [3]

if x == []:  =  if x.__eq__([]):

if len(x) == 0:  =  if x.__len__() == 0:

Other than the implicit cast to bool for conditional syntax and the implicit
calls to special method names as appropriate I am not clear that there is
any remaining mystery for this question.  And the language reference makes
all of the above very clear, so I highly recommend it.

The only caveat is that builtin types don't always act the same way as user
defined types (classes).  Not sure if the few differences are well
documented, but to be honest that hasn't bit me hard enough that I needed to
look it up.  I do know that the builtin list has a __eq__ method and a
__len__ method, but not a __bool__ method (which it doesn't need [3]).

-eric

[1]
http://docs.python.org/dev/py3k/reference/compound_stmts.html#the-if-statement
[2]
http://docs.python.org/dev/py3k/reference/datamodel.html#special-method-names
[3] http://docs.python.org/dev/py3k/reference/datamodel.html#object.__bool__
so if __bool__ doesn't exist it tries __len__ and so forth.
-- 
http://mail.python.org/mailman/listinfo/python-list


Proper way to handle errors in a module

2011-05-11 Thread Andrew Berg
I'm a bit new to programming outside of shell scripts (and I'm no expert
there), so I was wondering what is considered the best way to handle
errors when writing a module. Do I just let exceptions go and raise
custom exceptions for errors that don't trigger a standard one? Have the
function/method return nothing or a default value and show an error
message? I'm sure there's not a clear-cut answer, but I was just
wondering what most developers would expect a module to do in certain
situations.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: checking if a list is empty

2011-05-11 Thread Prasad, Ramit
 I don't mean to insult anyone, but I've heard and read all the arguments 
 against Python's truth-testing, and they
don't impress me in the slightest. Most of them strike me as silly. The only 
argument that carries any weight to me is
one which I haven't seen anyone raise:

if x: turns something which arguably could have been a mistake (oops, I 
forgot to write the condition!) into valid
code.

The only problem I have had with the if x: notation is when I have values 
that might be empty lists, empty strings, None, or a boolean value being 
returned from the same source. But this is probably an instance when a good 
programmer would explicitly check the type instead of the naive if x: 
notation.

On the other hand, as a fairly n00b Python (and n00b Perl) developer, I find 
the notation if not x: to be far more English readable than if x==None or 
len(x)== 0 or x==0 or bool(x): (or some derivative/combination of those). 



Ramit

Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase  Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase 
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: checking if a list is empty

2011-05-11 Thread Prasad, Ramit
The audience I am concerned about is the ones who are over-educated 
into using and having used a score of different meanings of the same
symbols.  They will be used to their intuition being wrong when they
move into a new context.  Being explicit will help them.

I find this argument to be flawed. Should I stop using built-in generators 
instead of range/xrange for looping through lists? Certainly for loops with 
loop counting are understood more widely than generators. Should I stop using 
any advanced feature Python because it is difficult to understand without 
knowing Python?

I do not code for absolute beginner in Python to read my code. That way lies 
madness! I code with the assumption that someone with intermediate knowledge 
can read my code (unless I am forced into optimizing and then I rely on 
comments to explain). Actually, that is how I attempt to code in any language.

I may not have made the point well, but I cannot see any advantage for trying 
to program for the lowest common denominator.


Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase  Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase 
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Py3k,email header handling

2011-05-11 Thread Terry Reedy

On 5/11/2011 12:27 PM, TheSaint wrote:

Steven D'Aprano wrote:


Before you re-write it, you should run 2to3 over it and see how much it
can do automatically:


Widely done, only the results from some query has radically changed on
favour of unicode. Errors raising about results which are not strings
anymore.


Make sure you use 3.2 and not 3.1 (or 3.0) as there were improvements to 
the email module.


--
Terry Jan Reedy

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


Re: Proper way to handle errors in a module

2011-05-11 Thread Patty


- Original Message - 
From: Andrew Berg bahamutzero8...@gmail.com

To: python-list@python.org
Sent: Wednesday, May 11, 2011 10:29 AM
Subject: Proper way to handle errors in a module



I'm a bit new to programming outside of shell scripts (and I'm no expert
there), so I was wondering what is considered the best way to handle
errors when writing a module. Do I just let exceptions go and raise
custom exceptions for errors that don't trigger a standard one? Have the
function/method return nothing or a default value and show an error
message? I'm sure there's not a clear-cut answer, but I was just
wondering what most developers would expect a module to do in certain
situations.
--
http://mail.python.org/mailman/listinfo/python-list




Hi Andrew -

Sometimes you want an exception come up and then use that information to 
take your
program in some direction.  For example, you might want your program to 
'see' the exception
that comes up when you go one beyond the end of an array.  This means that 
something you
wanted to happen within the array you set up finished successfully (the 
program just reached one
beyond it and you didn't get an error!).  The enduser doesn't need to see 
this displayed on the screen
or in a window, you can just use an exception as-is to your advantage within 
the program.


Regards,

Patty 


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


Re: Proper way to handle errors in a module

2011-05-11 Thread MRAB

On 11/05/2011 18:29, Andrew Berg wrote:

I'm a bit new to programming outside of shell scripts (and I'm no expert
there), so I was wondering what is considered the best way to handle
errors when writing a module. Do I just let exceptions go and raise
custom exceptions for errors that don't trigger a standard one? Have the
function/method return nothing or a default value and show an error
message? I'm sure there's not a clear-cut answer, but I was just
wondering what most developers would expect a module to do in certain
situations.


Generally speaking, a function or method should either do what it's
expected to do, returning the expected result, or raise an exception if
it can't.

Also, it's often clearer to distinguish between a function, which
returns a result, and a procedure, which doesn't (in Python it would
return None).

For example, if you have a list, the functional form is:

sorted(my_list)

which returns a new sorted list, and the procedural form is:

my_list.sort()

which sorts in-place (modifying the list) and returns None.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Proper way to handle errors in a module

2011-05-11 Thread Roy Smith
In article mailman.1415.1305134998.9059.python-l...@python.org,
 Andrew Berg bahamutzero8...@gmail.com wrote:

 I'm a bit new to programming outside of shell scripts (and I'm no expert
 there), so I was wondering what is considered the best way to handle
 errors when writing a module. Do I just let exceptions go and raise
 custom exceptions for errors that don't trigger a standard one? Have the
 function/method return nothing or a default value and show an error
 message? I'm sure there's not a clear-cut answer, but I was just
 wondering what most developers would expect a module to do in certain
 situations.

In general, raise an exception when there is no valid value that can be 
returned from a function.  Sometimes it makes more sense to return 0, 
None, an empty list, etc.  So:

count_vowels(banana) == 3
count_vowels(xyzzy) == 0  # counting y as not a vowel
count_vowels() == 0
count_vowels(None) == raises TypeError

You want to raise specific errors.  Let's say you've got a function like 
this:

def airspeed(swallow):
   speeds = {european: 42,
 african, 196}
   return speeds[swallow]

If somebody passes an invalid string, it will raise KeyError as written.  
Better to do something like:

def airspeed(swallow):
   speeds = {european: 42,
 african, 196}
   try:
   return speeds[swallow]
   except KeyError:
raise UnknownBirdError(swallow)

This lets somebody catch UnknownBirdError at some higher level and do 
something useful.  If you let KeyError escape, that's a lot harder to 
deal with because it could come from almost anywhere.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On Wed, 11 May 2011 10:33:51 -0400, D'Arcy J.M. Cain
  da...@druid.net wrote:
:  Non-programmers should be able to program?

That was not really what I suggested; I was primarily talking
about reading programs and commenting on formulæ and algorithms.

:  Should non-doctors be able to doctor?

If I were God, I might consider simplifying the anatomy to allow that,
yes.

:Should cars be built so that
:  anyone can intuitively fix them without a mechanic?  Should trucks be
:  built so that drivers don't have to learn how to split shift?

That's out of my area so I don't know.  However, contrary to
software, I have never seen any use of rebuilding the car to
do something other than transport ...

And, besides, as mechanics do not design cars or even engines, they 
are not analogous to programmers, but rather to computer technicians 
(maintenance, deployment, installation, etc).

: Why is
:  programming so different that we can't expect people to actually learn
:  their discipline?

It is not different from other engineering disciplines, where you would
have to interact with experts from other disciplines, and understand
and comment on their designs.  That's the way to build a /system/.

Say, you want to create the software to make weather forcasts.
At the end of the day, that's programming, but no way that's going
to be a task for programmers alone.  You need mathematicians,
algorithm theorists, physicists, programmers, and multiple 
specialisations within each discipline.  If you can make your 
programs clear enough to be used as a language of communications, 
you will simplify the development, and allow the code to be
validated by those who knows how the computation has to be done
without specialising in talking to the computer.

:  This discussion is giving me some insight into some of the crap
:  programming I see these days.

I wonder if you would do a better job at programming the software
to crack equations from quantum physics than the physicist :-)

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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On 11 May 2011 16:26:40 GMT, Steven D'Aprano
  steve+comp.lang.pyt...@pearwood.info wrote:
:  1. My concern was not about clueless newbies.  They need to
:learn.  My concern is about experienced scientists and engineers who
:are simply new to python.
: 
:  Which makes them clueless newbies *about Python*. I don't care how 
:  experienced they are in astrophysics or biology or calculating the 
:  average airspeed of an unladen swallow.

Someone who knows how to program is never clueless starting a new
language.  Newbie, may be, but he knows most of the constructions
and semantic principles to look for; most of it is learning the syntax.

:  Yeah, life is hard and then you die, and scientists don't even get paid 
:  that much. So what? Do physicists write their scientific papers about 
:  string theory with the thought What if some Python programmer who knows 
:  nothing about string theory is reading this? I better dumb it down.

That depends on the purpose of that particular paper, but the real 
question is, who writes the software to test that string theory 
empirically?  Please tell.

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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On Thu, 12 May 2011 02:05:21 +1000, Chris Angelico
  ros...@gmail.com wrote:
:  In a Bourne shell script, if ends with fi... case ends with esac... so
:  file would end with... hmm. Yeah, I think it's best to know the
:  language you're trying to comprehend, and/or actually look at context
:  instead of shoving a piece of code under someone's nose and saying I
:  bet you can't figure out what THIS does!.

Code is quite often published to document algorithms, methods and
formulæ for the purpose of scientific research.  Since there is no
universal language which suits everything and everyone, this
is exactly what happens.  One has to have the rudimentary knowledge
to read half a dozen different languages to be able to read the
code and extract the algorithms.  If one tried to maintain the
necessary programming skills to exploit all of those languages to
their full potential, one would simply not be able to keep up with
the application discipline.

If all you do is to write software for computer illiterate users, YMWV.

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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On Wed, 11 May 2011 10:27:49 -0400, D'Arcy J.M. Cain
  da...@druid.net wrote:
:  When did we come to the idea that people should be able to program in a
:  language without actually learning it?  The fact that Python comes so
:  close to that possibility is nothing short of revolutionary.

Revolutionary indeed, so why don't we exploit the revolution
and write the programs to be as accessible as possible?

(Although, python is not the most revolutionary in this respect.)

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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On Wed, 11 May 2011 10:31:59 -0600, Ian Kelly
  ian.g.ke...@gmail.com wrote:
:  (x + 3 for x in xs if x % 2 == 1)

Interesting.  Thanks.  That might come in handy some time.

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


Re: Proper way to handle errors in a module

2011-05-11 Thread Andrew Berg
On 2011.05.11 12:57 PM, Patty wrote:
 Hi Andrew -

 Sometimes you want an exception come up and then use that information to 
 take your
 program in some direction.
Right, but I'm wondering how I should handle errors in a module, where
different people will want their programs to do different things when an
error occurs in a function or method from my module (which is not
necessarily because of a bug in my module - the calling script could
send a bad parameter or some system configuration could prevent the
function from doing something). On one hand, I could let almost all
exceptions go unhandled and have the calling script handle them. This
would force the developer of the calling script to learn all the
exceptions that my module could raise (which could be really annoying if
I have custom exceptions for problems that don't otherwise raise an
exception). OTOH, I could handle all the exceptions and return None or
some default value, but that would make it harder for the developer to
make the script react appropriately.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On Wed, 11 May 2011 13:50:54 -0400, Prasad, Ramit
  ramit.pra...@jpmchase.com wrote:
:  I find this argument to be flawed. Should I stop using built-in 
: generators instead of range/xrange for looping through lists?
: Certainly for loops with loop counting are understood more widely
: than generators. Should I stop using any advanced feature Python
: because it is difficult to understand without knowing Python?

No; I'd suggest the most legible and intuitive construct that /does/ the
job.  Never something which requires one extra (illegible) page to do
the job, or something which does not do the job at all.

:  I may not have made the point well, but I cannot see any advantage 
: for trying to program for the lowest common denominator.

Common to what?  I'd try the lowest common denominator of
legibility and effictiveness.

It is just KISS.

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


Re: py3k buffered IO - flush() required between read/write?

2011-05-11 Thread Terry Reedy

On 5/11/2011 12:27 PM, Genstein wrote:


In py3k is it necessary to flush() a file between read/write calls in order
to see consistent results?

I ask because I have a case under Python 3.2 (r32:88445) where it does
appear to be, on both Gentoo Linux and Windows Vista.

I've naturally read http://docs.python.org/py3k/library/io.html and
http://docs.python.org/py3k/tutorial/inputoutput.html#reading-and-writing-files
but could find no reference to such a requirement.

PEP 3116 suggested this might not be required in py3k and the
implementation notes in bufferedio.c state BufferedReader,
BufferedWriter and BufferedRandom...share a single buffer...this enables
interleaved reads and writes without flushing. Which seemed conclusive
but I'm seeing otherwise.

I have a test case, which is sadly rather long:
http://pastebin.com/xqrzKr5D It's lengthy because it's autogenerated
from some rather more complex code I'm working on, in order to reproduce
the issue in isolation.


I notice that you have required seek calls when switching between 
writing and reading. If you want others to look at this more, you should 
1) produce a minimal* example that demonstrates the questionable 
behavior, and 2) show the comparative outputs that raise your question. 
The code is way too long to cut and paste into an editor and see what is 
does on my windows machine.


*minimal = local minimum rather than global minimum. That means that 
removal or condensation of a line or lines removes the problem. In this 
case, remove extra seeks, unless doing so removes behavior discrepancy. 
Condense 1 byte writes to multibyte writes, unless ... . Are repeated 
interleavings required or is write, seek, read, seek, write enough?


--
Terry Jan Reedy

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


lightweight way to create new projects from templates

2011-05-11 Thread Jonathan Hartley

Hi.

I'm looking for a quick way to create new Python projects from a template.

I understand that 'Paste' (http://pythonpaste.org/) is one way to do 
this, but I find Paste very intimidating because of all the 
functionality it includes. I'm not even sure whether 'creating new 
projects from templates' is the central goal of Paste, or just an 
incidental benefit that it provides while performing some other task.


As a lightweight alternative, I'm creating a project called 'Genesis'. 
Functionally, it will be little more than a 'cp -r' followed by a few 
search and replace operations.



I realise this idea is modest, but I think it could be helpful for:

a) people who are just getting started with Python, and want to see how 
best to structure their projects, and


b) people who are already up-to-speed, and who want to create their own 
templates and spawn projects from them with a minimum of effort.


c) for the community, to exemplify and publicise best practices for new 
projects.



My goals include:

* Create a new project using the default template:

$ genesis myproj

* or using a particular project template:

$ genesis --template=mytemplate myproj

* Allow people to create their own templates, simply stored as 
directories under '~/.genesis'


* Files in a template contain 'tags' of the form 'G{author}'. When a new 
project is created, these tags are replaced. For example, G{author} will 
be replaced with the value for 'author' supplied either on the command-line:


$ genesis myproj author=Jonathan

Or in your ~/.genesis/config file (a python file read using 'exec'):

author = 'Jonathan'

* The default template should embody good practices like those 
demonstrated in the Python Project howto:

http://infinitemonkeycorps.net/docs/pph/

* The default template should include a setup.py, supporting sdist, 
register and upload commands.


* I'd also like the default template to include things like creating 
Windows binaries, so that it is easier for projects to include this out 
of the box. It would be nice to extend this to binaries for other 
platforms too.


* The default template for command-line applications should include an 
acceptance test, which invokes the script in a new process and makes 
assertions about stdout, stderr and exit value.


* One of my inspirations is to create a good template for events like 
pyweek, where many people set out to write opengl or pyglet applications 
from scratch, and yet every time, many entrants fail to properly create 
binaries, etc.



Genesis is only partially written, so is not yet on PyPI, but is on 
bitbucket:

https://bitbucket.org/tartley/genesis/overview

If you have any feedback, it would be very welcome. In particular:

What do you think about the approach in general?

What do you think about the contents of my default template:
https://bitbucket.org/tartley/genesis/src/tip/genesis/config/default/

Do you think a single default template containing all the above ideas is 
viable? Or is it likely to be so idiosyncratic that I might be better 
shipping with several built-in templates, including a 'minimal' one? Do 
I need different templates for applications than for libraries? For 
console apps vs GUI apps?


What do you think of my choice of 'G{name}' for replaceable tags in the 
template? I wanted something that would not be valid Python, and would 
be unlikely to occur naturally in a project.


Best regards,

Jonathan

--
Jonathan Hartleytart...@tartley.comhttp://tartley.com
Made of meat.   +44 7737 062 225   twitter/skype: tartley

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


RE: checking if a list is empty

2011-05-11 Thread Prasad, Ramit
Common to what?  I'd try the lowest common denominator of
legibility and effictiveness.
It is just KISS.

Fair enough. I am a sheep, so I do what other (more knowledgeable) people do. 
It is a fair assumption (for my specific code writing environments) that 
everyone who is going to read my code understands if x: notation or is 
expected to learn enough Python to understand it.




Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


-Original Message-
From: python-list-bounces+ramit.prasad=jpmchase@python.org 
[mailto:python-list-bounces+ramit.prasad=jpmchase@python.org] On Behalf Of 
Hans Georg Schaathun
Sent: Wednesday, May 11, 2011 1:16 PM
To: python-list@python.org
Subject: Re: checking if a list is empty

On Wed, 11 May 2011 13:50:54 -0400, Prasad, Ramit
  ramit.pra...@jpmchase.com wrote:
:  I find this argument to be flawed. Should I stop using built-in 
: generators instead of range/xrange for looping through lists?
: Certainly for loops with loop counting are understood more widely
: than generators. Should I stop using any advanced feature Python
: because it is difficult to understand without knowing Python?

No; I'd suggest the most legible and intuitive construct that /does/ the
job.  Never something which requires one extra (illegible) page to do
the job, or something which does not do the job at all.

:  I may not have made the point well, but I cannot see any advantage 
: for trying to program for the lowest common denominator.

-- 
:-- Hans Georg
-- 
http://mail.python.org/mailman/listinfo/python-list
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase  Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase 
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Ethan Furman

Hans Georg Schaathun wrote:

On Wed, 11 May 2011 13:50:54 -0400, Prasad, Ramit
  ramit.pra...@jpmchase.com wrote:
:  I find this argument to be flawed. Should I stop using built-in 
: generators instead of range/xrange for looping through lists?

: Certainly for loops with loop counting are understood more widely
: than generators. Should I stop using any advanced feature Python
: because it is difficult to understand without knowing Python?

No; I'd suggest the most legible and intuitive construct that /does/ the
job.  Never something which requires one extra (illegible) page to do
the job, or something which does not do the job at all.

:  I may not have made the point well, but I cannot see any advantage 
: for trying to program for the lowest common denominator.


Common to what?  I'd try the lowest common denominator of
legibility and effictiveness.

It is just KISS.


'if li' *is* KISS.

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


Re: checking if a list is empty

2011-05-11 Thread Chris Torek
In article 4dcab8bf$0$29980$c3e8da3$54964...@news.astraweb.com
Steven D'Aprano  steve+comp.lang.pyt...@pearwood.info wrote:
When you call len(x) you don't care about the details of how to calculate 
the length of x. The object itself knows so that you don't have to. The 
same applies to truth testing.

I have a data type that is an array of lists. When you call if len(x)  
0 on it, it will blow up in your face, because len(x) returns a list of 
lengths like [12, 0, 2, 5]. But if you say if x, it will do the right 
thing. You don't need to care how to truth-test my data type, because it 
does it for you. By ignoring my type's interface, and insisting on doing 
the truth-test by hand, you shoot yourself in the foot.

What this really points out is that if x and if len(x)  0 are
*different tests*.  Consider xml.etree.ElementTree Element objects.
The documentation says, in part:

In ElementTree 1.2 and earlier, the sequence behavior means
that an element without any subelements tests as false (since
it's an empty sequence), even if it contains text and
attributions. ...

Note: This behavior is likely to change somewhat in ElementTree
1.3.  To write code that is compatible in both directions, use
... len(element) to test for non-empty elements.

In this case, when x is an Element, the result of bool(x) *could*
mean just x has sub-elements, but it could also/instead mean x
has sub-elements, text, or attributions.

The issue raised at the beginning of this thread was: which test
is better when x is a list, the test that invokes bool(x), or
the test that invokes len(x)?  There is no answer to that, any more
than there is to which ice cream flavor is best. [%]  A more
interesting question to ask, in any given bit of code, is whether
bool(x) or len(x) is more appropriate for *all* the types x might
take at that point, rather than whether one or the other is better
for lists, where the result is defined as equivalent.

(The biggest problem with answering that tends to be deciding
what types x might take.)

[% Chocolate with raspberry, or mint, or similar.]
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)  http://web.torek.net/torek/index.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: py3k buffered IO - flush() required between read/write?

2011-05-11 Thread Genstein

On 11/05/2011 19:24, Terry Reedy wrote:

writing and reading. If you want others to look at this more, you should
1) produce a minimal* example that demonstrates the questionable
behavior, and 2) show the comparative outputs that raise your question.


Thanks for a quick response. Perhaps I was being unclear - in py3k, 
given the following code and assuming no errors arise:


 f = open(foo, w+b)
 f.write(b'test')
 f.seek(0)
 print(f.read(4))

What is the printed result supposed to be?

i) b'test'
ii) never b'test'
iii) platform dependent/undefined/other

All the best,

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


RE: checking if a list is empty

2011-05-11 Thread Prasad, Ramit
 Someone who knows how to program is never clueless starting a new
language.  Newbie, may be, but he knows most of the constructions
and semantic principles to look for; most of it is learning the syntax.

I claim to be able to program (Java/Python), but would be absolutely lost 
programming in Lisp. It is more than just learning the syntax, it includes a 
thought paradigm as well.

Just like I claim to be able to do math, but severely suck at RPN math. (Okay, 
that analogy is bad but it is all I can come up with at the moment.)

Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


-Original Message-
From: python-list-bounces+ramit.prasad=jpmchase@python.org 
[mailto:python-list-bounces+ramit.prasad=jpmchase@python.org] On Behalf Of 
Hans Georg Schaathun
Sent: Wednesday, May 11, 2011 1:06 PM
To: python-list@python.org
Subject: Re: checking if a list is empty

On 11 May 2011 16:26:40 GMT, Steven D'Aprano
  steve+comp.lang.pyt...@pearwood.info wrote:
:  1. My concern was not about clueless newbies.  They need to
:learn.  My concern is about experienced scientists and engineers who
:are simply new to python.
: 
:  Which makes them clueless newbies *about Python*. I don't care how 
:  experienced they are in astrophysics or biology or calculating the 
:  average airspeed of an unladen swallow.

:  Yeah, life is hard and then you die, and scientists don't even get paid 
:  that much. So what? Do physicists write their scientific papers about 
:  string theory with the thought What if some Python programmer who knows 
:  nothing about string theory is reading this? I better dumb it down.

That depends on the purpose of that particular paper, but the real 
question is, who writes the software to test that string theory 
empirically?  Please tell.

-- 
:-- Hans Georg
-- 
http://mail.python.org/mailman/listinfo/python-list
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase  Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase 
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Proper way to handle errors in a module

2011-05-11 Thread Prasad, Ramit
The simple but code heavy system is to create two functions. One that raises an 
error and one that returns None or something that is mutually agreed to be 
invalid.  You can even get away with one of them doing the actual work and the 
other one just as a wrapper. I feel too lazy to fix the mistakes below, but you 
get the idea.

 Def a():
Blahblah
Raise SomeError

Def b():
Try:
A()
Except SomeError:
Return blah


Ramit



Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423


-Original Message-
From: python-list-bounces+ramit.prasad=jpmchase@python.org 
[mailto:python-list-bounces+ramit.prasad=jpmchase@python.org] On Behalf Of 
Andrew Berg
Sent: Wednesday, May 11, 2011 1:15 PM
To: python-list@python.org
Subject: Re: Proper way to handle errors in a module

On 2011.05.11 12:57 PM, Patty wrote:
 Hi Andrew -

 Sometimes you want an exception come up and then use that information to 
 take your
 program in some direction.
Right, but I'm wondering how I should handle errors in a module, where
different people will want their programs to do different things when an
error occurs in a function or method from my module (which is not
necessarily because of a bug in my module - the calling script could
send a bad parameter or some system configuration could prevent the
function from doing something). On one hand, I could let almost all
exceptions go unhandled and have the calling script handle them. This
would force the developer of the calling script to learn all the
exceptions that my module could raise (which could be really annoying if
I have custom exceptions for problems that don't otherwise raise an
exception). OTOH, I could handle all the exceptions and return None or
some default value, but that would make it harder for the developer to
make the script react appropriately.
-- 
http://mail.python.org/mailman/listinfo/python-list
This communication is for informational purposes only. It is not
intended as an offer or solicitation for the purchase or sale of
any financial instrument or as an official confirmation of any
transaction. All market prices, data and other information are not
warranted as to completeness or accuracy and are subject to change
without notice. Any comments or statements made herein do not
necessarily reflect those of JPMorgan Chase  Co., its subsidiaries
and affiliates.

This transmission may contain information that is privileged,
confidential, legally privileged, and/or exempt from disclosure
under applicable law. If you are not the intended recipient, you
are hereby notified that any disclosure, copying, distribution, or
use of the information contained herein (including any reliance
thereon) is STRICTLY PROHIBITED. Although this transmission and any
attachments are believed to be free of any virus or other defect
that might affect any computer system into which it is received and
opened, it is the responsibility of the recipient to ensure that it
is virus free and no responsibility is accepted by JPMorgan Chase 
Co., its subsidiaries and affiliates, as applicable, for any loss
or damage arising in any way from its use. If you received this
transmission in error, please immediately contact the sender and
destroy the material in its entirety, whether in electronic or hard
copy format. Thank you.

Please refer to http://www.jpmorgan.com/pages/disclosures for
disclosures relating to European legal entities.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On Wed, 11 May 2011 14:59:34 -0400, Prasad, Ramit
  ramit.pra...@jpmchase.com wrote:
:  Fair enough. I am a sheep, so I do what other (more knowledgeable)
: people do. It is a fair assumption (for my specific code writing 
: environments) that everyone who is going to read my code understands 
: if x: notation or is expected to learn enough Python to understand it.

That's fair enough.  You know your code, so it is probably true.
It would not be true for the code I am writing.

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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On Wed, 11 May 2011 12:17:33 -0700, Ethan Furman
  et...@stoneleaf.us wrote:
:  'if li' *is* KISS.

It /might/ be in some contexts, but a priori it is not, as it
superimposes a truth value on a data type which is otherwise
a pretty accurate model of real objects (outside python).

One principle of object oriented programming is to bestow the
objects with properties reflecting known properties from the
domain being modelled.  Lists do not have truth values in the
application domain, and therefore truth values in the
implementation domain is complicated.

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


Re: checking if a list is empty

2011-05-11 Thread Hans Georg Schaathun
On Wed, 11 May 2011 14:44:37 -0400, Prasad, Ramit
  ramit.pra...@jpmchase.com wrote:
:  Someone who knows how to program is never clueless starting a new
: language.  Newbie, may be, but he knows most of the constructions
: and semantic principles to look for; most of it is learning the syntax.
: 
:  I claim to be able to program (Java/Python), but would be absolutely
: lost programming in Lisp. It is more than just learning the syntax,
: it includes a thought paradigm as well.

OK.  I should written 'how to program imperatively' ... 'new imperative
language'.  Or substitute functional/logical/whatever for imperative.

You would not be completely clueless moving to
ada/fortran/C/pascal/simula.  There may be new concepts,
and some concepts which must be adapted to another level of
abstraction, but you do have a clue about the core concepts.

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


How to deal with optional argument evaluation

2011-05-11 Thread Rodrick Brown
I'm having a hard time dealing with the following scenario

My class takes a hash like the following:
rdargs =
{'env:'prod','feed':'opra','hostname':'host13dkp1','process':'delta','side':'a','zone','ny'}

All the keys in this hash can be optional.

I'm having a hard time dealing with all 36 possible combinations and I dont
want to use a monstorous if not and else or etc... to evaluate all possible
combinations before I call my search routine.

How do others deal with situations like this? And have it look readable?

if someone supplies just a host and side my search can return 400 responses,
if they supply all 6 elements it can return just 1-5 matches.

class RD(object):
   def __init__(self,**kwargs):
   self.value = 0
   #for field in ('env', 'side', 'zone', 'feed', 'hostname', 'process',
'status'):
   #val = kwargs[field] if kwargs.has_key(field) else False
   #setattr(self, field, val)
   self.env = kwargs.get('env',False)
   self.side = kwargs.get('side',False)
   self.zone = kwargs.get('zone',False)
   self.feed = kwargs.get('feed',False)
   self.hostname = kwargs.get('hostname',False)
   self.process = kwargs.get('process',False)
   self.status = kwargs.get('status',False)

-- 
[ Rodrick R. Brown ]
http://www.rodrickbrown.com http://www.linkedin.com/in/rodrickbrown
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread Ian

On 11/05/2011 20:13, Hans Georg Schaathun wrote:

On Wed, 11 May 2011 12:17:33 -0700, Ethan Furman
   et...@stoneleaf.us  wrote:
:  'if li' *is* KISS.

It /might/ be in some contexts, but a priori it is not, as it
superimposes a truth value on a data type which is otherwise
a pretty accurate model of real objects (outside python).

One principle of object oriented programming is to bestow the
objects with properties reflecting known properties from the
domain being modelled.  Lists do not have truth values in the
application domain, and therefore truth values in the
implementation domain is complicated.

Exactly. Its just a convention.  If it exists, its true, if if doesn't 
its false.


In the real world  lists of zero items do not exist.
You don't go shopping with a shopping list of zero items.
You don't take a journey going nowhere.
You wouldn't have a party and invite nobody.
What kind of building is one with zero floors?
Would you have an Aircraft Carrier with no aircraft?

Oh Wait - the UK has one of those coming into service soon.

Regards

Ian





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


Re: How to deal with optional argument evaluation

2011-05-11 Thread Ethan Furman

Rodrick Brown wrote:

I'm having a hard time dealing with the following scenario
 
My class takes a hash like the following:
rdargs = 
{'env:'prod','feed':'opra','hostname':'host13dkp1','process':'delta','side':'a','zone','ny'}
 
All the keys in this hash can be optional.
 
I'm having a hard time dealing with all 36 possible combinations and I 
dont want to use a monstorous if not and else or etc... to evaluate all 
possible combinations before I call my search routine.
 
How do others deal with situations like this? And have it look readable?
 
if someone supplies just a host and side my search can return 400 
responses, if they supply all 6 elements it can return just 1-5 matches.


class RD(object):
   def __init__(self,**kwargs):
   self.value = 0
   #for field in ('env', 'side', 'zone', 'feed', 'hostname', 
'process', 'status'):

   #val = kwargs[field] if kwargs.has_key(field) else False
   #setattr(self, field, val)
   self.env = kwargs.get('env',False)
   self.side = kwargs.get('side',False)
   self.zone = kwargs.get('zone',False)
   self.feed = kwargs.get('feed',False)
   self.hostname = kwargs.get('hostname',False)
   self.process = kwargs.get('process',False)
   self.status = kwargs.get('status',False)


class RD(object):
def __init__(
self,
env=False,
side=False,
zone=False,
feed=False,
hostname=False,
process=False,
status=False
):
self.env = env
self.side = side
self.zone = zone
self.feed = feed
self.hostname = hostname
self.process = process
self.status = status

What does the code look like that actually uses this information?

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


Re: checking if a list is empty

2011-05-11 Thread harrismh777

Hans Georg Schaathun wrote:

Code is quite often published to document algorithms, methods and
formulæ for the purpose of scientific research.  Since there is no
universal language which suits everything and everyone, this
is exactly what happens.  One has to have the rudimentary knowledge
to read half a dozen different languages to be able to read the
code and extract the algorithms.


+1  QOTW





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


Re: checking if a list is empty

2011-05-11 Thread harrismh777

D'Arcy J.M. Cain wrote:

Non-programmers should be able to program?

Should non-doctors be able to doctor?  Should cars be built so that
anyone can intuitively fix them without a mechanic?


   Non-programmers should not be expected to program in 'C' nor in lisp...

   ... but non-programmers were able to program in BASIC jes fine...


I contend that non-programmers are able to learn rudimentary python and 
work with it to solve real problems 'without' formal training in 
'programming'.


Python is the New BASIC for 'people'. At least, it has the potential to 
be that...   people should be able to talk to their computers (whatever 
we mean by that) and have the computer respond with real-world 
solutions.  We need to move away from 'canned apps' to a new day where 
the masses can sit down to their computer and solve new problems with it 
through intuitive language skills.  Why not?


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


Re: py3k buffered IO - flush() required between read/write?

2011-05-11 Thread Terry Reedy

On 5/11/2011 3:08 PM, Genstein wrote:

On 11/05/2011 19:24, Terry Reedy wrote:

writing and reading. If you want others to look at this more, you should
1) produce a minimal* example that demonstrates the questionable
behavior, and 2) show the comparative outputs that raise your question.


Thanks for a quick response. Perhaps I was being unclear - in py3k,
given the following code and assuming no errors arise:

  f = open(foo, w+b)
  f.write(b'test')
  f.seek(0)
  print(f.read(4))

What is the printed result supposed to be?

i) b'test'
ii) never b'test'
iii) platform dependent/undefined/other


Good clear question. I expect i).

With 3.2 on winxp, that is what I get with StringIO, text file, and 
bytes file (the first two with b's removed). I would expect the same on 
any system. If you get anything different, I would consider it a bug


--
Terry Jan Reedy

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


unicode by default

2011-05-11 Thread harrismh777

hi folks,
   I am puzzled by unicode generally, and within the context of python 
specifically. For one thing, what do we mean that unicode is used in 
python 3.x by default. (I know what default means, I mean, what changed?)


   I think part of my problem is that I'm spoiled (American, ascii 
heritage) and have been either stuck in ascii knowingly, or UTF-8 
without knowing (just because the code points lined up). I am confused 
by the implications for using 3.x, because I am reading that there are 
significant things to be aware of... what?


   On my installation 2.6  sys.maxunicode comes up with 1114111, and my 
2.7 and 3.2 installs come up with 65535 each. So, I am assuming that 2.6 
was compiled with UCS-4 (UTF-32) option for 4 byte unicode(?) and that 
the default compile option for 2.7  3.2 (I didn't change anything) is 
set for UCS-2 (UTF-16) or 2 byte unicode(?).   Do I understand this much 
correctly?


   The books say that the .py sources are UTF-8 by default... and that 
3.x is either UCS-2 or UCS-4.  If I use the file handling capabilities 
of Python in 3.x (by default) what encoding will be used, and how will 
that affect the output?


   If I do not specify any code points above ascii 0xFF does any of 
this matter anyway?




Thanks.

kind regards,
m harris

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


Re: checking if a list is empty

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 19:05:03 +, Chris Torek wrote:

 In article 4dcab8bf$0$29980$c3e8da3$54964...@news.astraweb.com Steven
 D'Aprano  steve+comp.lang.pyt...@pearwood.info wrote:
When you call len(x) you don't care about the details of how to
calculate the length of x. The object itself knows so that you don't
have to. The same applies to truth testing.

I have a data type that is an array of lists. When you call if len(x) 
0 on it, it will blow up in your face, because len(x) returns a list of
lengths like [12, 0, 2, 5]. But if you say if x, it will do the right
thing. You don't need to care how to truth-test my data type, because it
does it for you. By ignoring my type's interface, and insisting on doing
the truth-test by hand, you shoot yourself in the foot.
 
 What this really points out is that if x and if len(x)  0 are
 *different tests*.

*Potentially* different tests. Which is exactly the point. Given an 
arbitrary object, the developer doesn't know what test is appropriate. 
Should I write len(x) == 0 or list(x) == [] or x.next is None or 
something else? How can I tell which is appropriate without knowing 
everything about the internals of every object I ever see?

But if x is a non-broken object, then you don't have to. Just test on x 
itself, because it knows what to do to be considered a truth value.

Of course there may be objects where this doesn't work. There are no 
guarantees in Python. You can't even guarantee that printing an object 
won't blow up:

 class Broken:
...  def __str__(self):
...  return ... %s ... % 1/2
...  __repr__ = __str__
...
 b = Broken()
 b
Traceback (most recent call last):
  File stdin, line 1, in module
  File stdin, line 3, in __str__
TypeError: unsupported operand type(s) for /: 'str' and 'int'


 Consider xml.etree.ElementTree Element objects.
 The documentation says, in part:
 
 In ElementTree 1.2 and earlier, the sequence behavior means that an
 element without any subelements tests as false (since it's an empty
 sequence), even if it contains text and attributions. ...
 
 Note: This behavior is likely to change somewhat in ElementTree 1.3.
  To write code that is compatible in both directions, use ...
 len(element) to test for non-empty elements.
 
 In this case, when x is an Element, the result of bool(x) *could* mean
 just x has sub-elements, but it could also/instead mean x has
 sub-elements, text, or attributions.

Either behaviour is correct, but a semantic change to the class means 
that you have to do more work if you care about supporting both versions 
in the same code base. That is a neat example of where you might choose 
*not* to let an object decide for itself whether it is true or false, but 
instead impose your own definition on it.




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


Re: checking if a list is empty

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 20:13:35 +0100, Hans Georg Schaathun wrote:

 One principle of object oriented programming is to bestow the objects
 with properties reflecting known properties from the domain being
 modelled.  Lists do not have truth values in the application domain

Yes they do. Empty lists are nothing, ergo false, and non-empty lists are 
something, ergo true.


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


Re: checking if a list is empty

2011-05-11 Thread Steven Howe

On 05/11/2011 02:47 PM, Steven D'Aprano wrote:

On Wed, 11 May 2011 20:13:35 +0100, Hans Georg Schaathun wrote:


One principle of object oriented programming is to bestow the objects
with properties reflecting known properties from the domain being
modelled.  Lists do not have truth values in the application domain

Yes they do. Empty lists are nothing, ergo false, and non-empty lists are
something, ergo true.


  a=[]
  if a:
... print 'Yes'
...
  a=[1]
  if a:
... print 'Yes'
...
Yes


Cool; I'd never of thought to try that method to determine if a list was 
empty. I usually test it's length.

This looks faster.


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


Re: unicode by default

2011-05-11 Thread Ian Kelly
On Wed, May 11, 2011 at 3:37 PM, harrismh777 harrismh...@charter.net wrote:
 hi folks,
   I am puzzled by unicode generally, and within the context of python
 specifically. For one thing, what do we mean that unicode is used in python
 3.x by default. (I know what default means, I mean, what changed?)

The `unicode' class was renamed to `str', and a stripped-down version
of the 2.X `str' class was renamed to `bytes'.

   I think part of my problem is that I'm spoiled (American, ascii heritage)
 and have been either stuck in ascii knowingly, or UTF-8 without knowing
 (just because the code points lined up). I am confused by the implications
 for using 3.x, because I am reading that there are significant things to be
 aware of... what?

Mainly Python 3 no longer does explicit conversion between bytes and
unicode, requiring the programmer to be explicit about such
conversions.  If you have Python 2 code that is sloppy about this, you
may get some Unicode encode/decode errors when trying to run the same
code in Python 3.  The 2to3 tool can help somewhat with this, but it
can't prevent all problems.

   On my installation 2.6  sys.maxunicode comes up with 1114111, and my 2.7
 and 3.2 installs come up with 65535 each. So, I am assuming that 2.6 was
 compiled with UCS-4 (UTF-32) option for 4 byte unicode(?) and that the
 default compile option for 2.7  3.2 (I didn't change anything) is set for
 UCS-2 (UTF-16) or 2 byte unicode(?).   Do I understand this much correctly?

I think that UCS-2 has always been the default unicode width for
CPython, although the exact representation used internally is an
implementation detail.

   The books say that the .py sources are UTF-8 by default... and that 3.x is
 either UCS-2 or UCS-4.  If I use the file handling capabilities of Python in
 3.x (by default) what encoding will be used, and how will that affect the
 output?

If you open a file in binary mode, the result is a non-decoded byte stream.

If you open a file in text mode and do not specify an encoding, then
the result of locale.getpreferredencoding() is used for decoding, and
the result is a unicode stream.

   If I do not specify any code points above ascii 0xFF does any of this
 matter anyway?

You mean 0x7F, and probably, due to the need to explicitly encode and decode.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Recursion or iteration (was Fibonacci series recursion error)

2011-05-11 Thread Hans Mulder

On 03/05/2011 09:52, rusi wrote:


[If you believe it is, then try writing a log(n) fib iteratively :D ]


It took me a while, but this one seems to work:

from collections import namedtuple

Triple = namedtuple('Triple', 'hi mid lo')
Triple.__mul__ = lambda self, other: Triple(
self.hi * other.hi + self.mid * other.mid,
self.hi * other.mid + self.mid * other.lo,
self.mid * other.mid + self.lo * other.lo,
)

def fib(n):
f = Triple(1, 1, 0)
a = Triple(1, 0, 1)
while n:
if n  1:
a *= f
f *= f
n = 1
return a.mid


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


Re: py3k buffered IO - flush() required between read/write?

2011-05-11 Thread Martin P. Hellwig

On 11/05/2011 19:08, Genstein wrote:

On 11/05/2011 19:24, Terry Reedy wrote:

writing and reading. If you want others to look at this more, you should
1) produce a minimal* example that demonstrates the questionable
behavior, and 2) show the comparative outputs that raise your question.


Thanks for a quick response. Perhaps I was being unclear - in py3k,
given the following code and assuming no errors arise:

  f = open(foo, w+b)
  f.write(b'test')
  f.seek(0)
  print(f.read(4))

What is the printed result supposed to be?

i) b'test'
ii) never b'test'
iii) platform dependent/undefined/other

All the best,

-eg.


from:
http://docs.python.org/py3k/library/functions.html#open

open(file, mode='r', buffering=-1, encoding=None, errors=None, 
newline=None, closefd=True)¶

cut
buffering is an optional integer used to set the buffering policy. Pass 
0 to switch buffering off (only allowed in binary mode), 1 to select 
line buffering (only usable in text mode), and an integer  1 to 
indicate the size of a fixed-size chunk buffer. When no buffering 
argument is given, the default buffering policy works as follows:


* Binary files are buffered in fixed-size chunks; the size of the 
buffer is chosen using a heuristic trying to determine the underlying 
device’s “block size” and falling back on io.DEFAULT_BUFFER_SIZE. On 
many systems, the buffer will typically be 4096 or 8192 bytes long.
* “Interactive” text files (files for which isatty() returns True) 
use line buffering. Other text files use the policy described above for 
binary files.



So given that explanation, and assuming I understand it, I go for option 
'iii'.


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


Re: unicode by default

2011-05-11 Thread Benjamin Kaplan
On Wed, May 11, 2011 at 2:37 PM, harrismh777 harrismh...@charter.net wrote:
 hi folks,
   I am puzzled by unicode generally, and within the context of python
 specifically. For one thing, what do we mean that unicode is used in python
 3.x by default. (I know what default means, I mean, what changed?)

   I think part of my problem is that I'm spoiled (American, ascii heritage)
 and have been either stuck in ascii knowingly, or UTF-8 without knowing
 (just because the code points lined up). I am confused by the implications
 for using 3.x, because I am reading that there are significant things to be
 aware of... what?

   On my installation 2.6  sys.maxunicode comes up with 1114111, and my 2.7
 and 3.2 installs come up with 65535 each. So, I am assuming that 2.6 was
 compiled with UCS-4 (UTF-32) option for 4 byte unicode(?) and that the
 default compile option for 2.7  3.2 (I didn't change anything) is set for
 UCS-2 (UTF-16) or 2 byte unicode(?).   Do I understand this much correctly?


Not really sure about that, but it doesn't matter anyway. Because even
though internally the string is stored as either a UCS-2 or a UCS-4
string, you never see that. You just see this string as a sequence of
characters. If you want to turn it into a sequence of bytes, you have
to use an encoding.

   The books say that the .py sources are UTF-8 by default... and that 3.x is
 either UCS-2 or UCS-4.  If I use the file handling capabilities of Python in
 3.x (by default) what encoding will be used, and how will that affect the
 output?

   If I do not specify any code points above ascii 0xFF does any of this
 matter anyway?

ASCII only goes up to 0x7F. If you were using UTF-8 bytestrings, then
there is a difference for anything over that range. A byte string is a
sequence of bytes. A unicode string is a sequence of these mythical
abstractions called characters. So a unicode string u'\u00a0' will
have a length of 1. Encode that to UTF-8 and you'll find it has a
length of 2 (because UTF-8 uses 2 bytes to encode everything over 128-
the top bit is used to signal that you need the next byte for this
character)

 If you want the history behind the whole encoding mess, Joel Spolsky
wrote a rather amusing article explaining how this all came about:
http://www.joelonsoftware.com/articles/Unicode.html

And the biggest reason to use Unicode is so that you don't have to
worry about your program messing up because someone hands you input in
a different encoding than you used.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread harrismh777

Steven D'Aprano wrote:

modelled.  Lists do not have truth values in the application domain

Yes they do. Empty lists are nothing, ergo false, and non-empty lists are
something, ergo true.



No they don't.  Empty lists are empty lists... which just happen to 
become False when type cast  bool(list[])-- False.


lists cannot have a truth... binary False or True... however, when cast 
with bool(list) they can be used in a test (even with 'not') to make 
some decision.


Lists by themselves, empty or not, cannot have a 'truth' in an of 
themselves.



kind regards,
m harris

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


Re: Merge multiple source directories into one package with distutils?

2011-05-11 Thread Gregory Ewing

Miki Tebeka wrote:


.py files from more than one source directory into a single
package when installing?
The Selenium Python bindings does something like that, have a look at 
http://selenium.googlecode.com/svn/trunk/setup.py


Unless I'm missing something, nothing out of the ordinary is
happening there. Each destination directory is getting .py
files from just one source directory.

I'm not talking about a package with multiple submodules,
I know how to do that. I'm talking about putting .py files
from more than one source directory into the same intallation
directory.

E.g. Source:

  stuff/__init__.py
  stuff/dir1/foo.py
  stuff/dir2/blarg.py

Installation:

  stuff/__init__.py
  stuff/foo.py
  stuff/blarg.py

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


Re: unicode by default

2011-05-11 Thread harrismh777

Ian Kelly wrote:

   Ian, Benjamin,  thanks much.


The `unicode' class was renamed to `str', and a stripped-down version
of the 2.X `str' class was renamed to `bytes'.


   ... thank you, this is very helpful.


 If I do not specify any code points above ascii 0xFF does any of this
  matter anyway?



You mean 0x7F, and probably, due to the need to explicitly encode and decode.


Yes, actually, I did... and from Benjamin's reply it seems that 
this matters only if I am working with bytes.  Is it true that if I am 
working without using bytes sequences that I will not need to care about 
the encoding anyway, unless of course I need to specify a unicode code 
point?


Thanks again.

kind regards,
m harris

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


Re: checking if a list is empty

2011-05-11 Thread Gregory Ewing

Hans Georg Schaathun wrote:

 0 is a number as real and existent as any other,
one would think that the empty list is also as real and existent as
any other list.


0 does have some special properties, though, such as
being the additive identity and not having a multiplicative
inverse. Adding falseness as another special property isn't
too much of a stretch and turns out to be useful.

Likewise, it's useful to treat empty containers as having
a similar special property, since they're often a base or
terminating case in algorithms.

It's especially useful in a dynamic language where any
additional operations such as finding the length or
comparing with zero has a run-time cost.

Yes, you have to learn it, but it's a small thing to
learn with a considerable payoff.

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


Re: checking if a list is empty

2011-05-11 Thread harrismh777

harrismh777 wrote:

Lists by themselves, empty or not, cannot have a 'truth' in an of
themselves.


   ... forgot.,


   Based on Ian's comment a couple of days ago...

   if alist:


 ... is actually :


   if bool(alist):


   I think this is more than just semantics or silly argumentation. Its 
important to understand where the boolean truth is coming from...  my 
understanding 'now' is that the  list[] has no such boolean attribute, 
but is cast for the 'if' construct



so...if alist:is   --  if bool(alist):

and...   if not alist:is also  --  if not bool(alist):


   The 'alist[]' has no boolean attribute,   right??


kind regards,

m harris



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


Re: unicode by default

2011-05-11 Thread John Machin
On Thu, May 12, 2011 8:51 am, harrismh777 wrote:
 Is it true that if I am
 working without using bytes sequences that I will not need to care about
 the encoding anyway, unless of course I need to specify a unicode code
 point?

Quite the contrary.

(1) You cannot work without using bytes sequences. Files are byte
sequences. Web communication is in bytes. You need to (know / assume / be
able to extract / guess) the input encoding. You need to encode your
output using an encoding that is expected by the consumer (or use an
output method that will do it for you).

(2) You don't need to use bytes to specify a Unicode code point. Just use
an escape sequence e.g. \u0404 is a Cyrillic character.



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


urllib2 request with binary file as payload

2011-05-11 Thread Michiel Sikma
Hi there,
I made a small script implementing a part of Youtube's API that allows
you to upload videos. It's pretty straightforward and uses urllib2.
The script was written for Python 2.6, but the server I'm going to use
it on only has 2.5 (and I can't update it right now, unfortunately).
It seems that one vital thing doesn't work in 2.5's urllib2:

--

data = open(video['filename'], 'rb')

opener = urllib2.build_opener(urllib2.HTTPHandler)
req = urllib2.Request(settings['upload_location'], data, {
'Host': 'uploads.gdata.youtube.com',
'Content-Type': video['type'],
'Content-Length': '%d' % os.path.getsize(video['filename'])
})
req.get_method = lambda: 'PUT'
url = opener.open(req)

--

This works just fine on 2.6:
send: open file 'file.mp4', mode 'rb' at 0x1005db580
sendIng a read()able

However, on 2.5 it refuses:
Traceback (most recent call last):
  File ./pyup, line 119, in module
main()
  File ./pyup, line 116, in main
url = opener.open(req)
  File /usr/lib/python2.5/urllib2.py, line 381, in open
response = self._open(req, data)
  File /usr/lib/python2.5/urllib2.py, line 399, in _open
'_open', req)
  File /usr/lib/python2.5/urllib2.py, line 360, in _call_chain
result = func(*args)
  File /usr/lib/python2.5/urllib2.py, line 1107, in http_open
return self.do_open(httplib.HTTPConnection, req)
  File /usr/lib/python2.5/urllib2.py, line 1079, in do_open
h.request(req.get_method(), req.get_selector(), req.data, headers)
  File /usr/lib/python2.5/httplib.py, line 866, in request
self._send_request(method, url, body, headers)
  File /usr/lib/python2.5/httplib.py, line 892, in _send_request
self.send(body)
  File /usr/lib/python2.5/httplib.py, line 711, in send
self.sock.sendall(str)
  File string, line 1, in sendall
TypeError: sendall() argument 1 must be string or read-only buffer, not file

Is there any other way I can do this? I'm pretty new to Python so I'm
not sure how to proceed at this point.

Thanks!

Michiel Sikma
We Demand HTML

http://wedemandhtml.com/
mich...@wedemandhtml.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: urllib2 request with binary file as payload

2011-05-11 Thread John Machin
On Thu, May 12, 2011 10:20 am, Michiel Sikma wrote:
 Hi there,
 I made a small script implementing a part of Youtube's API that allows
 you to upload videos. It's pretty straightforward and uses urllib2.
 The script was written for Python 2.6, but the server I'm going to use
 it on only has 2.5 (and I can't update it right now, unfortunately).
 It seems that one vital thing doesn't work in 2.5's urllib2:

 --

 data = open(video['filename'], 'rb')

 opener = urllib2.build_opener(urllib2.HTTPHandler)
 req = urllib2.Request(settings['upload_location'], data, {
   'Host': 'uploads.gdata.youtube.com',
   'Content-Type': video['type'],
   'Content-Length': '%d' % os.path.getsize(video['filename'])
 })
 req.get_method = lambda: 'PUT'
 url = opener.open(req)

 --

 This works just fine on 2.6:
 send: open file 'file.mp4', mode 'rb' at 0x1005db580
 sendIng a read()able

 However, on 2.5 it refuses:
 Traceback (most recent call last):
[snip]
 TypeError: sendall() argument 1 must be string or read-only buffer, not
 file

I don't use this stuff, just curious. But I can read docs. Quoting from
the 2.6.6 docs:


class urllib2.Request(url[, data][, headers][, origin_req_host][,
unverifiable])
This class is an abstraction of a URL request.

url should be a string containing a valid URL.

data may be a string specifying additional data to send to the server, or
None if no such data is needed. Currently HTTP requests are the only ones
that use data; the HTTP request will be a POST instead of a GET when the
data parameter is provided. data should be a buffer in the standard
application/x-www-form-urlencoded format. The urllib.urlencode() function
takes a mapping or sequence of 2-tuples and returns a string in this
format.


2.6 is expecting a string, according to the above. No mention of file.
Moreover it expects the data to be urlencoded. 2.7.1 docs say the same
thing. Are you sure you have shown the code that worked with 2.6?


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


pymultimouse access violation error

2011-05-11 Thread jpiee

 A couple of years back  Oscar Lindberg wrote a program called pymultimouse, 
which has a homepage at http://code.google.com/p/pymultimouse/. I've been using 
the file rawinputreader.py, which is avaliable in the zipped folder in the 
downloads section of the above link.

I'm trying to get raw mouse input in a program I'm working on, and I've been 
trying to use pymultimouse . I'm trying to run the code he's got, but it uses 
ctypes, and when I try to run I'm getting this error:


Traceback (most recent call last):
  File C:\Users\jmite\rawinputreader.py, line 324, in module
pprint.pprint(rir.pollEvents())
  File C:\Users\jmite\rawinputreader.py, line 210, in pollEvents
windll.user32.DispatchMessageA(pMsg)
WindowsError: exception: access violation writing 0x0001



Does anybody have ideas as to how the code could be patched to possibly fix 
this? There's only been one release of it, and other people seem to be able to 
run it just fine, so is it possibly something machine specific?
I'm new to python, let alone ctypes and win32 modules, so any help would be 
tremendously wonderful at this point.

Thanks!

Jmite




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


Re: checking if a list is empty

2011-05-11 Thread Steven D'Aprano
On Wed, 11 May 2011 17:38:58 -0500, harrismh777 wrote:

 Steven D'Aprano wrote:
 modelled.  Lists do not have truth values in the application domain
 Yes they do. Empty lists are nothing, ergo false, and non-empty lists
 are something, ergo true.


 No they don't.  Empty lists are empty lists... which just happen to
 become False when type cast  bool(list[])-- False.
 
 lists cannot have a truth... binary False or True... however, when cast
 with bool(list) they can be used in a test (even with 'not') to make
 some decision.


Have you been not paying any attention at all?


 alist = []
 if alist: pass
... else: print alist is a false value
...
alist is a false value

 if type(alist) is bool: pass
... else: print but not a bool
...
but not a bool



 Lists by themselves, empty or not, cannot have a 'truth' in an of
 themselves.

Despite your confident tone, they really do. Just like EVERY OTHER OBJECT 
IN PYTHON. And Perl, and Ruby, and Javascript, and PHP, and many other 
languages. You might not like that fact, but it is a fact.



-- 
Steven

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


Re: unicode by default

2011-05-11 Thread harrismh777

John Machin wrote:

(1) You cannot work without using bytes sequences. Files are byte
sequences. Web communication is in bytes. You need to (know / assume / be
able to extract / guess) the input encoding. You need to encode your
output using an encoding that is expected by the consumer (or use an
output method that will do it for you).

(2) You don't need to use bytes to specify a Unicode code point. Just use
an escape sequence e.g. \u0404 is a Cyrillic character.



Thanks John.  In reverse order, I understand point (2). I'm less clear 
on point (1).


If I generate a string of characters that I presume to be ascii/utf-8 
(no \u0404 type characters) and write them to a file (stdout) how does 
default encoding affect that file.by default..?   I'm not seeing that 
there is anything unusual going on...   If I open the file with vi?  If 
I open the file with gedit?  emacs?




Another question... in mail I'm receiving many small blocks that look 
like sprites with four small hex codes, scattered about the mail... 
mostly punctuation, maybe?   ... guessing, are these unicode code 
points, and if so what is the best way to 'guess' the encoding? ... is 
it coded in the stream somewhere...protocol?


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


Re: unicode by default

2011-05-11 Thread MRAB

On 12/05/2011 02:22, harrismh777 wrote:

John Machin wrote:

(1) You cannot work without using bytes sequences. Files are byte
sequences. Web communication is in bytes. You need to (know / assume / be
able to extract / guess) the input encoding. You need to encode your
output using an encoding that is expected by the consumer (or use an
output method that will do it for you).

(2) You don't need to use bytes to specify a Unicode code point. Just use
an escape sequence e.g. \u0404 is a Cyrillic character.



Thanks John. In reverse order, I understand point (2). I'm less clear on
point (1).

If I generate a string of characters that I presume to be ascii/utf-8
(no \u0404 type characters) and write them to a file (stdout) how does
default encoding affect that file.by default..? I'm not seeing that
there is anything unusual going on... If I open the file with vi? If I
open the file with gedit? emacs?



Another question... in mail I'm receiving many small blocks that look
like sprites with four small hex codes, scattered about the mail...
mostly punctuation, maybe? ... guessing, are these unicode code points,
and if so what is the best way to 'guess' the encoding? ... is it coded
in the stream somewhere...protocol?


You need to understand the difference between characters and bytes.

A string contains characters, a file contains bytes.

The encoding specifies how a character is represented as bytes.

For example:

In the Latin-1 encoding, the character £ is represented by the 
byte 0xA3.


In the UTF-8 encoding, the character £ is represented by the byte 
sequence 0xC2 0xA3.


In the ASCII encoding, the character £ can't be represented at all.

The advantage of UTF-8 is that it can represent _all_ Unicode
characters (codepoints, actually) as byte sequences, and all those in
the ASCII range are represented by the same single bytes which the
original ASCII system used. Use the UTF-8 encoding unless you have to
use a different one.

A file contains only bytes, a socket handles only bytes. Which encoding
you should use for characters is down to protocol. A system such as
email, which can handle different encodings, should have a way of
specifying the encoding, and perhaps also a default encoding.
--
http://mail.python.org/mailman/listinfo/python-list


Re: unicode by default

2011-05-11 Thread Steven D'Aprano
On Thu, 12 May 2011 03:31:18 +0100, MRAB wrote:

 Another question... in mail I'm receiving many small blocks that look
 like sprites with four small hex codes, scattered about the mail...
 mostly punctuation, maybe? ... guessing, are these unicode code points,
 and if so what is the best way to 'guess' the encoding? ... is it coded
 in the stream somewhere...protocol?

 You need to understand the difference between characters and bytes.


http://www.joelonsoftware.com/articles/Unicode.html

is also a good resource.


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


Re: checking if a list is empty

2011-05-11 Thread alex23
Hans Georg Schaathun h...@schaathun.net wrote:
 Revolutionary indeed, so why don't we exploit the revolution
 and write the programs to be as accessible as possible?

Where do you draw the line, though?

No decorators, as they're not intuitively obvious? No custom
descriptors, as that requires a deeper knowledge of Python internals
that utter ignorance allows?

Both of those aspects alone seem far more complex and advanced than
treating empty collections as False.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: checking if a list is empty

2011-05-11 Thread alex23
On May 12, 7:24 am, harrismh777 harrismh...@charter.net wrote:
 We need to move away from 'canned apps' to a new day where
 the masses can sit down to their computer and solve new problems with it
 through intuitive language skills.  Why not?

Because the vast majority of them don't seem to want to be bothered?

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


Learning new languages (was: checking if a list is empty)

2011-05-11 Thread Teemu Likonen
* 2011-05-11T20:26:48+01:00 * Hans Georg Schaathun wrote:

 On Wed, 11 May 2011 14:44:37 -0400, Prasad, Ramit
   ramit.pra...@jpmchase.com wrote:
 I claim to be able to program (Java/Python), but would be absolutely
 lost programming in Lisp. It is more than just learning the syntax,
 it includes a thought paradigm as well.

 OK. I should written 'how to program imperatively' ... 'new imperative
 language'. Or substitute functional/logical/whatever for imperative.

Common Lisp is an imperative language. It is also functional and
object-oriented language. It does not force any paradigm but supports
many. Thus, it is called a multi-paradigm language. I understand that
Lisp can be scary, though. Lisp code looks weird and it seems that the
myth that it's a functional language can't be busted.

Anyway, I agree with this:

* 2011-05-11T19:05:31+01:00 * Hans Georg Schaathun wrote:
 Someone who knows how to program is never clueless starting a new
 language. Newbie, may be, but he knows most of the constructions
 and semantic principles to look for; most of it is learning the
 syntax.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode by default

2011-05-11 Thread harrismh777

Steven D'Aprano wrote:

You need to understand the difference between characters and bytes.


http://www.joelonsoftware.com/articles/Unicode.html

is also a good resource.


Thanks for being patient guys, here's what I've done:


astr=pound sign
asym= \u00A3
afile=open(myfile, mode='w')
afile.write(astr + asym)

12

afile.close()



When I edit myfile with vi I see the 'characters' :

pound sign £

   ... same with emacs, same with gedit  ...


When I hexdump myfile I see this:

000 6f70 6375 2064 6973 6e67 c220 00a3


This is *not* what I expected... well it is (little-endian) right up to 
the 'c2' and that is what is confusing me


I did not open the file with an encoding of UTF-8... so I'm assuming 
UTF-16 by default (python3) so I was expecting a '00A3' little-endian as 
'A300' but what I got instead was UTF-8 little-endian  'c2a3' 


See my problem?... when I open the file with emacs I see the character 
pound sign... same with gedit... they're all using UTF-8 by default. By 
default it looks like Python3 is writing output with UTF-8 as default... 
and I thought that by default Python3 was using either UTF-16 or UTF-32. 
So, I'm confused here...  also, I used the character sequence \u00A3 
which I thought was UTF-16... but Python3 changed my intent to  'c2a3' 
which is the normal UTF-8...


Thanks again for your patience... I really do hate to be dense about 
this...  but this is another area where I'm just beginning to dabble and 
I'd like to know soon what I'm doing...


Thanks for the link Steve... I'm headed there now...




kind regards,
m harris



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


  1   2   3   >