2nd Try: Trouble writing lines to file that include line feeds - Newbie

2013-12-23 Thread Dan Healy
Overview: I'm attempting to read strings from a serial port. Each string ends 
with a carriage return and line feed. I want to write those strings to a file, 
like a log file. So, if I send P1 and the P2 on a new line, I would expect to 
open this file and find (line 1) P1 (line 2) P2. 

Problem: The file only contains P2. It always overwrites the first line. I can 
send 20 strings and the file will always contain the last string received. 

Code: 

#Import the serial module 
import serial 

#Open the serial port w/ settings 
ser=serial.Serial( 
port="/dev/ttyUSB0", 
baudrate=9600, 
timeout=None) 

#Print data received on the serial port after removing the CR and LF characters 
while True: 
rawcode=ser.readline() 
codelog=open('/home/pi/avdms/codes.log','w') 
codelog.write(rawcode) 
codelog.close() 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Newbie question. Are those different objects ?

2013-12-23 Thread Duncan Booth
Gregory Ewing  wrote:

> rusi wrote:
>> Good idea. Only you were beaten to it by about 2 decades.
> 
> More than 2, I think.
> 
> Algol: x := y

Wher := is pronounced 'becomes'.

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


Re: Airplane mode control using Python?

2013-12-23 Thread rurpy
On Sunday, December 22, 2013 10:37:35 PM UTC-7, Chris Angelico wrote:
> Actually, formatting errors ARE often caused by Google Groups. Maybe
> it wasn't in this instance, but I have seen several cases of GG
> mangling code formatting, so this was a perfectly reasonable theory.

And you have determined format errors are coming from GG how exactly?
You would need to know the original contents entered into GG, yes?
Perhaps you have done experiments to determine these errors that you
could share with us?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: 2nd Try: Trouble writing lines to file that include line feeds - Newbie

2013-12-23 Thread Gary Herron

On 12/22/2013 04:55 PM, Dan Healy wrote:

Overview: I'm attempting to read strings from a serial port. Each string ends 
with a carriage return and line feed. I want to write those strings to a file, 
like a log file. So, if I send P1 and the P2 on a new line, I would expect to 
open this file and find (line 1) P1 (line 2) P2.

Problem: The file only contains P2. It always overwrites the first line. I can 
send 20 strings and the file will always contain the last string received.

Code:

#Import the serial module
import serial

#Open the serial port w/ settings
ser=serial.Serial(
 port="/dev/ttyUSB0",
 baudrate=9600,
 timeout=None)

#Print data received on the serial port after removing the CR and LF characters
while True:
 rawcode=ser.readline()
 codelog=open('/home/pi/avdms/codes.log','w')
 codelog.write(rawcode)
 codelog.close()


First, that code is quite foolish:  If you want to write multiple lines 
to a file, open the file once before the loop, looped through all your 
writes, and then closed it after the loop has exited.:


codelog=open('/home/pi/avdms/codes.log','w')
while True:
rawcode=ser.readline()
codelog.write(rawcode)

codelog.close()



However, on the chance that you are writing this code for testing 
purposes, and you really do have a need to open a file, write something 
onto the end and then close the file, and that you are going to do this 
many times, I'll continue with this response:


   The default behavior of the "open" call is to *truncate* (i.e.,
   clear the  contents) and start writing at the beginning. The
   behavior you want is called *append*, and you get it with a 'wa' as
   the second parameter of the open call.


See http://docs.python.org/3/library/functions.html#open for a list of 
other modes available for the open call.


Gary Herron

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


Re: Airplane mode control using Python?

2013-12-23 Thread Chris Angelico
On Mon, Dec 23, 2013 at 5:59 PM,   wrote:
> On Sunday, December 22, 2013 10:37:35 PM UTC-7, Chris Angelico wrote:
>> Actually, formatting errors ARE often caused by Google Groups. Maybe
>> it wasn't in this instance, but I have seen several cases of GG
>> mangling code formatting, so this was a perfectly reasonable theory.
>
> And you have determined format errors are coming from GG how exactly?
> You would need to know the original contents entered into GG, yes?
> Perhaps you have done experiments to determine these errors that you
> could share with us?

Previous people's posts to this very list. Search the archives, you
know this to be true!

Why, rurpy, do you continue to support, apologize for, and argue in
favour of, a piece of software that (a) you know to be buggy, and (b)
has perfectly viable alternatives? Why is it so important to you? When
you use an ad-funded service, you are paying for it. When you pay for
a service, you send a message that it is the one you want to use. I
use Google Search because it is excellent; other people feel it's too
invasive of privacy and use DuckDuckGo instead. If DDG were hopelessly
buggy, people would argue against its use - *especially* if that
bugginess caused problems for other people. (Imagine if its crawler
violated robots.txt and common sense, and caused problems for web
servers.) How would the owners/authors of DDG feel if they produced
stupidly buggy software but everyone used it anyway? Pretty well
justified, I would think, and so there'd be no reason for them to put
effort into fixing the bugs.

I'm happy to use all sorts of "free" (aka ad-funded) services - Google
Search, Gmail, Kongregate, The Pirate Bay, Google Docs, Stack
Overflow, IMDB... endless list. I use them because they are good, or
at least because they are better than the alternatives. With some of
them, there's a lock-in effect from the community. If you hate Stack
Overflow, for instance, you have to bypass a whole lot of potential
information. But avoiding Google Groups just means using gmane or
Thunderbird or python-list, and you get all the same content without
any loss. So why stick to something that sends mail with mess all over
it?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Trouble writing lines into file with line feeds- Python Newb

2013-12-23 Thread daniel . t . healy
Overview: I'm attempting to read strings from a serial port. Each string ends 
with a carriage return and line feed. I want to write those strings to a file, 
like a log file. So, if I send P1 and the P2 on a new line, I would expect to 
open this file and find (line 1) P1 (line 2) P2. 

Problem: The file only contains P2. It always overwrites the first line. I can 
send 20 strings and the file will always contain the last string received.

Code:

#Import the serial module
import serial

#Open the serial port w/ settings
ser=serial.Serial(
port="/dev/ttyUSB0", 
baudrate=9600, 
timeout=None)

#Establish a placeholder for the variable line
line=[]

#Print data received on the serial port after removing the CR and LF characters
while True:
rawcode=ser.readline()
codelog=open('/home/pi/avdms/codes.log','w')
codelog.write(rawcode)
codelog.close()
-- 
https://mail.python.org/mailman/listinfo/python-list


How can i return more than one value from a function to more than one variable

2013-12-23 Thread dec135
basically what I wanna do is this :

x = 4
y = 7
def switch (z,w):
***this will switch z to w and vice verca***
 c= z
 z=w
 w=c
 print 'Now x =', w, 'and y = ' , z
 return w
x = switch(x,y)

 How am I supposed to do so I can  return also a value to the variable y 
WITHOUT printing 'Now x =', w, 'and y = ' , z   a second time ?

thanks in advance
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Airplane mode control using Python?

2013-12-23 Thread Mark Lawrence

On 23/12/2013 08:46, Chris Angelico wrote:

On Mon, Dec 23, 2013 at 5:59 PM,   wrote:

On Sunday, December 22, 2013 10:37:35 PM UTC-7, Chris Angelico wrote:

Actually, formatting errors ARE often caused by Google Groups. Maybe
it wasn't in this instance, but I have seen several cases of GG
mangling code formatting, so this was a perfectly reasonable theory.


And you have determined format errors are coming from GG how exactly?
You would need to know the original contents entered into GG, yes?
Perhaps you have done experiments to determine these errors that you
could share with us?


Previous people's posts to this very list. Search the archives, you
know this to be true!

Why, rurpy, do you continue to support, apologize for, and argue in
favour of, a piece of software that (a) you know to be buggy, and (b)
has perfectly viable alternatives?


Big +1

Thinking about it the situation is laughable.  You have an entry on the 
*PYTHON* wiki telling you how to get around bugs in *GOOGLE* code.



 Why is it so important to you? When
you use an ad-funded service, you are paying for it. When you pay for
a service, you send a message that it is the one you want to use. I
use Google Search because it is excellent; other people feel it's too
invasive of privacy and use DuckDuckGo instead. If DDG were hopelessly
buggy, people would argue against its use - *especially* if that
bugginess caused problems for other people. (Imagine if its crawler
violated robots.txt and common sense, and caused problems for web
servers.) How would the owners/authors of DDG feel if they produced
stupidly buggy software but everyone used it anyway? Pretty well
justified, I would think, and so there'd be no reason for them to put
effort into fixing the bugs.

I'm happy to use all sorts of "free" (aka ad-funded) services - Google
Search, Gmail, Kongregate, The Pirate Bay, Google Docs, Stack
Overflow, IMDB... endless list. I use them because they are good, or
at least because they are better than the alternatives. With some of
them, there's a lock-in effect from the community. If you hate Stack
Overflow, for instance, you have to bypass a whole lot of potential
information. But avoiding Google Groups just means using gmane or
Thunderbird or python-list, and you get all the same content without
any loss. So why stick to something that sends mail with mess all over
it?

ChrisA



I dislike stackoverflowe as some of the answers there are blatently 
wrong.  However I'll use it but make certain that the answers can be 
verified before proceeding.  However I feel discriminated against using 
Thunderbird to read this via gmane as there isn't an entry on the python 
wiki telling me how to get around the bugs in this software.  Or is that 
because a) there aren't any b) there aren't enough to worry anybody or 
c) it isn't python's responsibility to write up work arounds for bugs in 
Thunderbird or gmane?


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Re: How can i return more than one value from a function to more than one variable

2013-12-23 Thread Simon Hayward
> basically what I wanna do is this :
> 
> x = 4
> y = 7
> def switch (z,w):
> ***this will switch z to w and vice verca***
>  c= z
>  z=w
>  w=c
>  print 'Now x =', w, 'and y = ' , z
>  return w
> x = switch(x,y)
> 
>  How am I supposed to do so I can  return also a value to the variable y
>  WITHOUT printing 'Now x =', w, 'and y = ' , z   a second time ?
> 
> thanks in advance

Using multiple assignment.

# Swap values
x, y = 4, 7
y, x = x, y

Or to keep this in a function, return a tuple and assign from that:

def switch(x, y):
return y, x

x, y = switch(x, y)
-- 
https://mail.python.org/mailman/listinfo/python-list


Deamonify my python script on Android

2013-12-23 Thread Kevin Peterson
Hi, 

I want to daemonify my python script on Android device. That is, it should be 
automatically invoked on boot up.

Appreciate your help.

Thanks,
Kevin Peterson


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


Re: How can i return more than one value from a function to more than one variable

2013-12-23 Thread Rick Johnson
On Sunday, December 22, 2013 4:54:46 PM UTC-6, dec...@msn.com wrote:
> basically what I wanna do is this :
> x = 4
> 
> y = 7
> def switch (z,w):
> ***this will switch z to w and vice verca***
>  c= z
>  z=w
>  w=c
>  print 'Now x =', w, 'and y = ' , z
>  return w
> x = switch(x,y)

If your intent is to swap the values of two variables (which
is really rebinding the names only) then why do you need a
function at all when you could explicitly swap the values
yourself? What gain would a function give you -- even if you
could do it?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Trouble writing lines into file with line feeds- Python Newb

2013-12-23 Thread Jussi Piitulainen
daniel.t.he...@gmail.com writes:
- -
> Problem: The file only contains P2. It always overwrites the first
> line. I can send 20 strings and the file will always contain the
> last string received.
- -
> while True:
> rawcode=ser.readline()
> codelog=open('/home/pi/avdms/codes.log','w')
- -

That's what file mode 'w' does. Use 'a'.

Also consider the logging module.

See  for open.

See  for logging.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How can i return more than one value from a function to more than one variable

2013-12-23 Thread Gary Herron

On 12/22/2013 02:54 PM, dec...@msn.com wrote:

basically what I wanna do is this :

x = 4
y = 7
def switch (z,w):
***this will switch z to w and vice verca***
  c= z
  z=w
  w=c
  print 'Now x =', w, 'and y = ' , z
  return w
x = switch(x,y)

  How am I supposed to do so I can  return also a value to the variable y 
WITHOUT printing 'Now x =', w, 'and y = ' , z   a second time ?

thanks in advance


I don't' understand the question, but if you are just trying to exchange 
the values of x and y, this will do:


x,y = y,x


If you want a function to return several values to several variables, try:

def fn(...):
# calculate a and b
return a,b

p,q = fn(...)

All these comma-separated sequences are tuples, often written with 
parentheses as (x,y)=(y,x) and (p,q)=fn(...), but as here,  the 
parentheses are often not necessary.


Gary Herron


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


Re: Airplane mode control using Python?

2013-12-23 Thread rurpy
On Sunday, December 22, 2013 10:37:35 PM UTC-7, Chris Angelico wrote:
> Actually, formatting errors ARE often caused by Google Groups. Maybe
> it wasn't in this instance, but I have seen several cases of GG
> mangling code formatting, so this was a perfectly reasonable theory.

What sort of formatting errors have you seen and how did you 
determine it was Google Groups fault?  You would need to know 
what the original message was, wouldn't you?  Perhaps these 
were experiments you carried out yourself that you could share 
with us?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Airplane mode control using Python?

2013-12-23 Thread Mark Lawrence

On 23/12/2013 06:09, ru...@yahoo.com wrote:

On Sunday, December 22, 2013 10:37:35 PM UTC-7, Chris Angelico wrote:

Actually, formatting errors ARE often caused by Google Groups. Maybe
it wasn't in this instance, but I have seen several cases of GG
mangling code formatting, so this was a perfectly reasonable theory.


What sort of formatting errors have you seen and how did you
determine it was Google Groups fault?  You would need to know
what the original message was, wouldn't you?  Perhaps these
were experiments you carried out yourself that you could share
with us?



Yawn.

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Google Groups + this list

2013-12-23 Thread Ned Batchelder

On 12/22/13 11:52 PM, ru...@yahoo.com wrote:

Come on Chris, it is just as easy to make typo or copy-and-
paste errors in any other software as GG, there is no evidence
that it was GG's fault.


Can we agree that it's not great to respond to a new poster with *just* 
"please don't use GG, it's bad" and no actual attempt to help them? 
It's really unfriendly.  In this case, it wasn't difficult to see the 
code the OP was asking about, and to try to help them.




If you want to recommend the mailing list, fine, but please
don't make stupid, unfounded, accusatory suggestions.


Rurpy: you're coming on really strong here.  "Stupid"?  No.  People have 
had to deal with the result of Google Groups for a long time, and it's 
not unreasonable to think that the formatting was its fault.




Kevin: just for your own info, there are a few people here
who despise Google Groups.  I and many other people post
from Google Groups regularly and it works fine.


This is disingenuous.  Google Groups clearly does not work fine.  If you 
understand its flaws, and care enough to, you can make it work fine. 
But it's a lot of work.



You might want to take a look at
  https://wiki.python.org/moin/GoogleGroupsPython
for some ways to reduce the annoyance factor for the anti-GG
clique here.


I appreciate the work you put into that page, but those suggestions are 
far from simple for the average newb here.  I think it's very unlikely 
that a new poster is going to read, understand, and follow those 
instructions.


Remember that most posters are not looking to "join the group."  They 
need help with a problem.  They aren't going to put a lot of work into 
anything having to do with this list.  That's just the way it is.


Google Groups is a blessing and a curse.  1) It provides a simple way 
for people to ask questions here.  2) It causes a lot of friction with 
many people on this list.  It won't do any good to pretend that either 
of these things isn't true.


But please, let's not turn this list into an "argue about Google Groups" 
list.


I suggest the following:

1) Don't fault newcomers for using Google Groups.  Politely suggest 
alternatives, but only if you are also helping them, or if they have 
already gotten help.


2) Be careful how you rail against Google Groups.  When you call its 
results "crap" (for example), it can sound like an insult to the poster. 
 You mean to refer to Google Groups, but remember you are also 
referring to the poster's words.


3) Don't let's get into protracted internal debates about Google Groups. 
 It is for the moment at least, an unavoidable part of this list.


--
Ned Batchelder, http://nedbatchelder.com

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


Using asyncio in event-driven network library

2013-12-23 Thread Tobias M.

Hello,

I am currently writing an event-driven client library for a network 
protocol [1] and chose to use the new asyncio module. I have no 
experience with asynchronous IO and don't understand all the concepts in 
asyncio yet. So I'm not sure if asyncio is actually the right choice .


My goal:
I want to provide an easy to use interface to the network protocol.

What I have so far:
I internally use the asyncio event loop and and the user can register 
event handler functions for specific events triggered by the network input.

Reduced to the essentials the architecture of my code looks like this:

class Client(asyncio.Protocol):

def __init__(self, handler, server, port):
self._handler = handler
self._server = server
self._port = port

def run(self):
loop = asyncio.get_event_loop()
task = asyncio.Task(loop.create_connection(self, server, port))
loop.run_until_complete(task)
loop.run_forever()

def data_received(self, data):
# read data and call the appropriate handler methods on 
self._handler

(..)

The user initializes a Client object, passing a handler to the 
constructor. The handler is an instance of a class that contains event 
handler methods implemented by the user. (The expected interface of a 
handler is defined in an abstract base class.)

Afterwards the user calls run() on the client to start processing.

Problem:
So far this works, but only for applications that solely react to events 
from the network. Now I want to be able to use this library for network 
communication in interactive applications (command line or GUI), too. In 
this case it needs to be able to respond to user input, which must be 
somehow incorporated in the event loop. GUI libraries like PyQt even 
provide their own event loop.


Questions:
So how do I integrate those aspects in my library?
Is asyncio the right choice?
Or does my whole approach goes in the wrong direction?

I would greatly appreciate any help and suggestions,

Tobias

PS:
I posted this question to the tutor mailing list [2] but looks like 
python-list is a better place for it.


[1] The network protocol is IRC (Internet Relay Chat) but I think that 
does not really matter here.

[2] https://mail.python.org/pipermail/tutor/2013-December/099089.html
--
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Rick Johnson
On Sunday, December 22, 2013 5:02:51 PM UTC-6, Mark Lawrence wrote:
> On 22/12/2013 22:51, Chris Angelico wrote:

> if a() == 0:
>
>  if b() == 0:
>
>  c()
>
> I can only see one way that you can possibly intepret it.

Hmm, I guess i should not assume color vision to be ubiquitous.

> [snip]

The level of abstraction of that code sample is so high that
no one should assume anything from reading it. The only fact
we CAN be sure of is that IF a() equals 0 and b() equals 0
then c will execute. But that fact gives us no insight into
Frank's *real* source code, not to mention his mind.

Sure. We always want folks to trim example code to it's most
relevant parts before presenting the code on this list, but
just as ten thousands lines of code is useless, so too is 4
lines. It's not the number of lines that matter so much as
the context those line provide to the problem at hand.

If you take the small code example literally, then why does
Frank even need to ask a question about what will happen
when he could simply run the code and observe the results.

Here is the most simplified example of Franks code:

if 0 == 0:
if 0 == 0:
do_something()

This code is so *confined* as to be useless for
generalities. In fact, the only reason to ask a question
about such simplified code is to understand the execution
rules OR syntax of Python source code. Here are some
concrete facts about those three lines:

1. test one will ALWAYS eval true, and proceed to test two

2. test two will ALWAYS eval true, then execute "do_something"

3. what happens after that is undefined

These are fundamental behaviors of Python conditionals,
operators, callables, etc... which we *already* understand
quite completely. But these fundamentals will not help us
understand Frank's problem.

What we need is help Frank is *CONTEXT*.

Now, even though Frank's code offers a small increment in
complexity over my simplified form, it offers *zero*
context of that complexity:

1. test one will evaluate true or false, then proceed (or
not, depending on the return value of "a") to test two

2. test two will evaluate true or false, then proceed (or
not, depending on the return value of "b") to execute the
body

3. What happens in a, b, and c is undefined.

The problem is we have no idea what happens in a,b,and c...
but those facts may not matter, what does matter is what a
return value of zero *means* to Frank, and without an
understanding of these "Frank semantics", how could we
possibly extrapolate a solution to such ambiguous questions?

You see, there exist no rules in Python for what a return
value of 0 should mean. Should it mean "success"? Should it
mean "failure"? Should it mean "eat pasta quickly"? Well it
could mean all those things to different people.

Since function return values are defined by programmers,
*ONLY* the programmer himself-- or those familiar with the
entire code base --can resolve the ambiguities with any
degree of precision.

Even IF you are presented with a small snippet of code that
include the identifier for the return value, you still
cannot be certain that extrapolating meaning from an
identifier *alone* will give you the insight to remove all
ambiguity.

For example, let's say the return value is "proceed" and
attached to name spelled "flag". Do you really think you can
extrapolate the purpose of that value being returned?

"proceed" with what?

Proceed counting unicorns?
Proceed raising exceptions?
Proceed extrapolating to infinity and beyond?!

You can guess, but that is all. Which brings us full circle
to the problem at hand.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: how to develop code using a mix of an existing python-program and console-commands

2013-12-23 Thread Jean-Michel Pichavant
- Original Message -
> Am 18.12.13 21:17, schrieb Jean Dubois:
> > I have a python-program which I want to perform its task first,
> > then
> > switch to
> > the python console to experiment with further commands, using what
> > was
> > already
> > defined in the python-program.
> 
> Excellent way to use/debug a scripting langugage. Use ipython, and
> then
> either
> 
> %run myfile.py

I second his suggestion. Additionally, 

%pdb
%run myfile.py

will automatically call the ipython debugger on unhandled exceptions, making 
post mortem debugging a walk in a park.

JM


-- IMPORTANT NOTICE: 

The contents of this email and any attachments are confidential and may also be 
privileged. If you are not the intended recipient, please notify the sender 
immediately and do not disclose the contents to any other person, use it for 
any purpose, or store or copy the information in any medium. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Ned Batchelder

On 12/22/13 6:57 PM, Rick Johnson wrote:

Even IF you are presented with a small snippet of code that
include the identifier for the return value, you still
cannot be certain that extrapolating meaning from an
identifier*alone*  will give you the insight to remove all
ambiguity.

For example, let's say the return value is "proceed" and
attached to name spelled "flag". Do you really think you can
extrapolate the purpose of that value being returned?

"proceed" with what?

 Proceed counting unicorns?
 Proceed raising exceptions?
 Proceed extrapolating to infinity and beyond?!

You can guess, but that is all. Which brings us full circle
to the problem at hand.


You seem to be arguing that the original question didn't have enough 
information to be usefully answered.  You are doing this after the OP 
got an answer he was pleased with.


Yes, the question had some ambiguity.  That's why other people engaged 
with him to get to a useful point.  Waxing philosophically about the 
impossibility of asking the question isn't helpful.


--
Ned Batchelder, http://nedbatchelder.com

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


Re: cascading python executions only if return code is 0

2013-12-23 Thread Mark Lawrence

On 22/12/2013 23:57, Rick Johnson wrote:

On Sunday, December 22, 2013 5:02:51 PM UTC-6, Mark Lawrence wrote:

On 22/12/2013 22:51, Chris Angelico wrote:



if a() == 0:

  if b() == 0:

  c()

I can only see one way that you can possibly intepret it.




[snip molehill turned into Himalayas]

Again Frank's original question.

"
I have a requirement where I need to sequentially execute a bunch of 
executions, each execution has a return code. the followed executions 
should only be executed if the return code is 0. is there a cleaner or 
more pythonic way to do this other than the following ?


if a() == 0:
if b() == 0:
c()
"

What is so difficult to understand about "is there a cleaner or more 
pythonic way to do this other than the following?"  Peter Otten for one 
certainly managed to get it, and as always managed to beat me to the draw :(


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Re: Airplane mode control using Python?

2013-12-23 Thread Kevin Peterson
Hi Ned,

I havenot named it as android.py and there are no .pyc files in my
directory.

Here is the snapshot
$ python
Python 2.7.2 (default, Jul 20 2013, 22:54:57)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import android
>>> droid = android.Android()
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'module' object has no attribute 'Android'
>>>

Thanks,


On Mon, Dec 23, 2013 at 9:29 AM, Ned Batchelder wrote:

> On 12/22/13 10:41 PM, Michael Torrie wrote:
>
>> On 12/22/2013 08:20 PM, Kevin Peterson wrote:
>>
>>> Hi,
>>>
>>> I am trying to control Aeroplane mode on Android using Python code.
>>> I am running QPyPlus python. When I execute this code(that is widespread
>>> in the net),
>>>
>>>  #!/usr/bin/python
>>>  import android droid = android.Android()
>>>  # go to airplane mode
>>>   droid.toggleAirplaneMode()
>>>
>>>  droid.makeToast('exiting')
>>>
>>> I get the error 'no such attribute Android()'.
>>>
>>
>> Is that really how your code is formatted? The import line is an error.
>>
>> try:
>>
>> import android
>>
>>
>> droid = android.Android()
>>
>>
> The OP reported an error of "no such attribute Android", it's pretty
> obvious that he isn't getting a syntax error.  We can assume the code has
> been mangled in the posting.
>
> Your code sample (once the obvious formatting errors have been fixed) is
> identical to ones I've found online, so I assume it should work.
>
> My best guess is that you have named your own file android.py, shadowing
> the library you're trying to import.  Name it something else, delete all
> the *.pyc files in your directory, and try again.
>
> --
> Ned Batchelder, http://nedbatchelder.com
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Ethan Furman

On 12/22/2013 08:57 PM, Roy Smith wrote:

In article <52b7a0e4$0$29994$c3e8da3$54964...@news.astraweb.com>,
  Steven D'Aprano  wrote:


Anyway, I may be completely misinterpreting what I'm reading. Perhaps the
assertion is checking a function invariant ("one of the strategies will
always succeed") in which case you're doing it exactly right and I should
shut up now :-)


Yes :-)

More specifically, the assertion exception will get caught way up in
some django middleware which will log a stack trace and return a HTTP
50-something.  This will typically be followed by somebody like me
noticing the stack dump and trying to figure out WTF happened.


This is completely misusing what assertions are for.  I hope this bit of middleware (or django itself) is very clear 
about never running with assertions turned off.




Assertions are great tools.


Only when used properly.


People should use them more often.


I see them being (mis)used too much as it is.


In a
sense, they're executable comments.  They're a programmer's way of
standing on a hilltop and shouting to all the world, "I swear to you,
this is true.  There may be a gazillion lines of code out there and
GBytes of program state, but right here, right now, within this circle
I've drawn in the sand,


Considering how easy it is to disable assertions, a circle in the sand is an 
amazingly appropriate metaphor.  :)

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


Re: cascading python executions only if return code is 0

2013-12-23 Thread Mark Lawrence

On 23/12/2013 12:45, Ethan Furman wrote:


Considering how easy it is to disable assertions, a circle in the sand
is an amazingly appropriate metaphor.  :)

--
~Ethan~


It might be easy, but surely it's far more fun providing an itertools 
solution :)


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Re: python socket query

2013-12-23 Thread Piet van Oostrum
smilesonisa...@gmail.com writes:

> Hi,
>I am trying to write a TCP socket program in python. I am using python 2.6 
> in linux. 
>
> I referred following link:
> http://www.ibm.com/developerworks/linux/tutorials/l-pysocks/section4.html
> I am actually writing the client-side stream socket.
> I wrote a small program which creates the socket, bind to the socket, connect 
> to socket and send() close(). I see that there is no reply coming from server 
> and the TCP disconnect happens.
> import socket
>
> def tcp(host, request, port=34567):
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
> s.connect((host, port))
>
> s.send(request)
>
> reply = s.recv(2**14)
>
> s.close()
>
> return reply
>
> My problem is even if the request is sent the length(reply) is is 0. I tried 
> to put a timeout of 1 sec s.settimeout() call after the send call but it 
> doesnot help.
>
> I tried by commenting s.close() still it did not work.
>
> Any idea what is the problem?

Length(reply) == 0 means that the other side closed the socket without sending 
anything back.
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Ethan Furman

On 12/22/2013 06:33 PM, Steven D'Aprano wrote:

Roy Smith wrote:


 else:
 assert 0, "can't create picker (classes = %s)" % classes


¡Ay, caramba! I was with you until the very last line. The above code is
possibly buggy and inappropriately designed. [...]

First, the bug: there are circumstances where no exception is raised even if
all the strategies fail.


Since Steven wasn't explicit about the circumstances, I'll list the two I'm aware of:  if Python is started with -O or 
-OO then assertions are cut from the code and do not run:


  ethan@media:~/source/python/issue19995$ ./python -O
  Python 3.4.0b1 (default:be22ffb4fdf1, Dec 20 2013, 12:26:10)
  [GCC 4.7.3] on linux
  Type "help", "copyright", "credits" or "license" for more information.
  --> assert 0, "Testing"
  --> # no exception raised
--
https://mail.python.org/mailman/listinfo/python-list


Re: Airplane mode control using Python?

2013-12-23 Thread Ned Batchelder

On 12/23/13 7:45 AM, Kevin Peterson wrote:

Hi Ned,

I havenot named it as android.py and there are no .pyc files in my
directory.

Here is the snapshot
$ python
Python 2.7.2 (default, Jul 20 2013, 22:54:57)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> import android
 >>> droid = android.Android()
Traceback (most recent call last):
   File "", line 1, in 
AttributeError: 'module' object has no attribute 'Android'
 >>>

Thanks,


Hmm, then perhaps your android module is not the one you think it is. 
Try this to see what you get:


>>> import android
>>> print android
.. this will show where it's imported from ..
>>> dir(android)
.. this will show the names available in the android module ..
.. if any of them look like __version__ or VERSION, print that
>>> android.__version__


--
Ned Batchelder, http://nedbatchelder.com

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


Re: [OT] vnc-problem with idle running as sudo on raspberry pi

2013-12-23 Thread Jean Dubois
Op zondag 22 december 2013 18:06:39 UTC+1 schreef Michael Torrie:
> On 12/22/2013 06:27 AM, Jean Dubois wrote:
> > I was wrong writing idle_as_root worked this way. As a matter of fact,
> > this method also does not work as expected, as can be seen from this
> > message:
> > 
> > X11 connection rejected because of wrong authentication.
> > 
> > New 'X' desktop is raspberrypi:1
> > 
> > Starting applications specified in /home/pi/.vnc/xstartup
> > Log file is /home/pi/.vnc/raspberrypi:1.log
> > 
> > Starting TightVNC server for pi 
> In future, I suspect you'll need to take questions like this that don't
> really have anything to do with Python to a Raspberry Pi forum or e-mail
> list.  Or a general Linux list.
> Short answer:
> Edit /etc/sudoers and add this line somewhere in it (probably near other
> lines that look similar):
> Defaults env_keep += "DISPLAY XAUTHORITY"
> If that doesn't work, then you'll have to talk to folks running the
> Rasbian on their Pi.  Sometimes XAUTHORITY isn't set.

I thought this would be something python-people are familiar with, after
all idle is a Python IDE and running it as a root sometimes is necessary.
Anyway,
I tried out your suggestion but it didn't work, however googling the line
you suggested brought me close to a solution:
the following command does the trick:
xhost +local:root 

unfortunately putting it in ~/.bash_profile doesn't make it work
automatically
after booting, neither does putting it in ~/.xsession, in ~/.xinitrc nor
in an autostart-file in ~/.config/openbox/ 
putting it in .bashrc does work _after_ opening a terminal which isn't
very nice.

thanks and kind regards,
jean
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: urllib and authentication script integration

2013-12-23 Thread Jeff James
I have some simple code I would like to share with someone that can assist
me in integrating authentication script into.  I'm sure it's an easy answer
for any of you.  I am still researching, but on this particular project,
time is of the essence and this is the only missing piece of the puzzle for
the completion of this script.  I know all of you are busy, but if anyone
has some time for me to send them what I have, I'd rather not share it on
the public forum.   Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Deamonify my python script on Android

2013-12-23 Thread Larry Martell
On Sun, Dec 22, 2013 at 10:22 PM, Kevin Peterson  wrote:
> Hi,
>
> I want to daemonify my python script on Android device. That is, it should be 
> automatically invoked on boot up.
>
> Appreciate your help.


I don't know anything about Android programming, but for Linux i
followed the advice on these 2 pages to achieve this:

http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
http://fedoraproject.org/wiki/Packaging:SysVInitScript

This probably isn't directly applicable to you, but it still may help.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Roy Smith
In article ,
 Ethan Furman  wrote:

> On 12/22/2013 08:57 PM, Roy Smith wrote:
> > In article <52b7a0e4$0$29994$c3e8da3$54964...@news.astraweb.com>,
> >   Steven D'Aprano  wrote:
> >
> >> Anyway, I may be completely misinterpreting what I'm reading. Perhaps the
> >> assertion is checking a function invariant ("one of the strategies will
> >> always succeed") in which case you're doing it exactly right and I should
> >> shut up now :-)
> >
> > Yes :-)
> >
> > More specifically, the assertion exception will get caught way up in
> > some django middleware which will log a stack trace and return a HTTP
> > 50-something.  This will typically be followed by somebody like me
> > noticing the stack dump and trying to figure out WTF happened.
> 
> This is completely misusing what assertions are for.  I hope this bit of 
> middleware (or django itself) is very clear 
> about never running with assertions turned off.

Sigh.  Sometimes I'm not sure which is worse.  The anti-assertion 
zealotry on this list, or the anti-regex zealotry.

The use of an assertion here is perfectly reasonable.  It is a statement 
that "this can never happen".

The bit of middleware I was talking about catches ALL uncaught 
exceptions.  We design our code so that all exceptions are supposed to 
get caught and handled at some appropriate place in application code.  
Nothing is ever supposed to leak up to the point where that middleware 
catches it, and anything caught there is evidence of a bug.  This is a 
common pattern in any kind of long-lived server.

And, if we didn't have the piece of middleware, the assertion (or any 
other uncaught exception) would get caught at some deeper level inside 
the django core.  The result would still be a 500 HTTP response, but 
without the added diagnostic logging we do, so it would be a lot harder 
to understand what happened.

And, if django didn't have that "except Exception" block wrapping 
everything, the gunicorn process would exit and upstart would catch that 
and restart it.

And, yes, I know that assertions get turned off with -O (frankly, I 
think that's a design flaw).  We don't run with -O.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Mark Lawrence

On 23/12/2013 15:12, Dennis Lee Bieber wrote:

On Mon, 23 Dec 2013 13:33:08 +1100, Steven D'Aprano
 declaimed the following:


Roy Smith wrote:



 else:
 assert 0, "can't create picker (classes = %s)" % classes


¡Ay, caramba! I was with you until the very last line. The above code is
possibly buggy and inappropriately designed. (I may be completely
misinterpreting this, in which case feel free to ignore the following
rant.)



I'd think the biggest problem with this is that if one runs Python with
optimization turned on, the assert statement itself vanishes, leaving one
with an empty else clause...

Try debugging that problem!



I'll pass on that one :)

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Re: [OT] vnc-problem with idle running as sudo on raspberry pi

2013-12-23 Thread Michael Torrie
On 12/23/2013 07:06 AM, Jean Dubois wrote:
> I thought this would be something python-people are familiar with, after
> all idle is a Python IDE and running it as a root sometimes is necessary.

On most desktop distros like Fedora, sudo idle would indeed work.

The fact that it's not working on your pi, means it must be specific to
the distro that your pi is running.  And by the way you never mentioned
what distro this is.

> Anyway,
> I tried out your suggestion but it didn't work, however googling the line
> you suggested brought me close to a solution:

The solution likely lies in understanding how MIT-Cookie's work with X11
and how to use xauth.

> the following command does the trick:
> xhost +local:root 
> 
> unfortunately putting it in ~/.bash_profile doesn't make it work
> automatically
> after booting, neither does putting it in ~/.xsession, in ~/.xinitrc nor
> in an autostart-file in ~/.config/openbox/ 
> putting it in .bashrc does work _after_ opening a terminal which isn't
> very nice.
> 
> thanks and kind regards,
> jean

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


Re: Airplane mode control using Python?

2013-12-23 Thread Chris Angelico
On Mon, Dec 23, 2013 at 5:09 PM,   wrote:
> On Sunday, December 22, 2013 10:37:35 PM UTC-7, Chris Angelico wrote:
>> Actually, formatting errors ARE often caused by Google Groups. Maybe
>> it wasn't in this instance, but I have seen several cases of GG
>> mangling code formatting, so this was a perfectly reasonable theory.
>
> What sort of formatting errors have you seen and how did you
> determine it was Google Groups fault?  You would need to know
> what the original message was, wouldn't you?  Perhaps these
> were experiments you carried out yourself that you could share
> with us?

Unless people were lying through their teeth when they said that their
posted code had worked on their system, I can be pretty sure it was
the fault of the delivery mechanism. Dig through the archive. Also,
you haven't answered the other part of the post, the more important
part.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Google Groups + this list

2013-12-23 Thread Chris Angelico
On Mon, Dec 23, 2013 at 10:48 PM, Ned Batchelder  wrote:
> I suggest the following:
>
> 1) Don't fault newcomers for using Google Groups.  Politely suggest
> alternatives, but only if you are also helping them, or if they have already
> gotten help.
>
> 2) Be careful how you rail against Google Groups.  When you call its results
> "crap" (for example), it can sound like an insult to the poster.  You mean
> to refer to Google Groups, but remember you are also referring to the
> poster's words.

I'm always careful of both of these. When I post alternatives to GG,
it's as part of another informative and on-topic post. If GG annoys me
but I don't have anything useful to say, I just move right along. (And
sometimes even if I do have something useful I could say. Sometimes
it's just not worth the hassle of cleaning up someone's misformatted
code so I can help them.)

> 3) Don't let's get into protracted internal debates about Google Groups.  It
> is for the moment at least, an unavoidable part of this list.

This is where I'm not so good :|

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: urllib and authentication script integration

2013-12-23 Thread Chris Angelico
On Tue, Dec 24, 2013 at 12:47 AM, Jeff James  wrote:
> I have some simple code I would like to share with someone that can assist
> me in integrating authentication script into.  I'm sure it's an easy answer
> for any of you.  I am still researching, but on this particular project,
> time is of the essence and this is the only missing piece of the puzzle for
> the completion of this script.  I know all of you are busy, but if anyone
> has some time for me to send them what I have, I'd rather not share it on
> the public forum.   Thanks

A little more information would be helpful. I gather from your subject
line that you're using urllib; that's a start. What version of Python,
and what platform(s)? What sort of authentication are you trying to
do? Can you cut down the code in question until it's no longer
sensitive (that is, so it's safe to post in public), so we can see
what it needs to do?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using asyncio in event-driven network library

2013-12-23 Thread Chris Angelico
On Mon, Dec 23, 2013 at 10:50 PM, Tobias M.  wrote:
> I am currently writing an event-driven client library for a network protocol
> [1] and chose to use the new asyncio module. I have no experience with
> asynchronous IO and don't understand all the concepts in asyncio yet. So I'm
> not sure if asyncio is actually the right choice .

As was acknowledged on -tutor, asyncio is extremely new. I don't know
if anyone's yet used it for GUI handling, or even if the module was
intended for that.

You may be able to wrap your GUI code up so it fits inside asyncio.
Rather than run a simple event loop, you could have your own loop that
does some kind of checking: PyGTK has functions for peeking at events
and doing just one iteration of the main loop, so you could pump all
events with something like this:

while gtk.events_pending(): gtk.main_iteration()

No doubt other toolkits have something similar.

Alternatively, you could run two threads: one for your GUI, and
another one for your asyncio. How easy and clean that would be is hard
to say without trying it; it could work out beautifully for your code,
or it could be horribly clunky.

This might be your big contribution to Python for the year: a nice,
easy, working example of using asyncio with a GUI toolkit! You might
be the first to look for this, but you won't be the last.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT] vnc-problem with idle running as sudo on raspberry pi

2013-12-23 Thread Jean Dubois
Op maandag 23 december 2013 16:29:09 UTC+1 schreef Michael Torrie:
> On 12/23/2013 07:06 AM, Jean Dubois wrote:
> > I thought this would be something python-people are familiar with, after
> > all idle is a Python IDE and running it as a root sometimes is necessary.
>
> On most desktop distros like Fedora, sudo idle would indeed work.
>
> The fact that it's not working on your pi, means it must be specific to
> the distro that your pi is running.  And by the way you never mentioned
> what distro this is.
>
> > Anyway,
> > I tried out your suggestion but it didn't work, however googling the line
> > you suggested brought me close to a solution:
>
> The solution likely lies in understanding how MIT-Cookie's work with X11
> and how to use xauth.
>
In fact no need for that, changing sudo /usr/bin/idle to gksu /usr/bin/idle
is all is needed to make this work.

kind regards,
jean
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Steven D'Aprano
Roy Smith wrote:

> Sigh.  Sometimes I'm not sure which is worse.  The anti-assertion
> zealotry on this list, or the anti-regex zealotry.

I'm not anti-assertions. I love assertions. I wouldn't be surprised if I use
assert more than you do. What I dislike is people misusing assertions.

> And, yes, I know that assertions get turned off with -O (frankly, I
> think that's a design flaw).  We don't run with -O.

Until such time as somebody decides they can speed up your code by 5% by
running with optimizations turned on.

Unfortunately, it's not always clear when an assert is appropriate and when
it isn't. Fundamentally, if an assertion can never fail[1], then there's no
point testing for it:

x = []
assert x == [] and len(x) == 0

So there's always tension between "why am I testing something that can't
fail?" and "but what if it does?", and different people are going to draw
the line in different places. So even if you and I draw the line in
different places, I can acknowledge that you seem to be using asserts the
same way I would, as executable comments and for testing program
invariants, and I'm sorry that my rant was aimed in your direction
unnecessarily.





[1] Short of hardware failure or serious bugs in the compiler, but in both
those cases you can't trust the assertions to trigger correctly either.

-- 
Steven

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


Re: cascading python executions only if return code is 0

2013-12-23 Thread Ethan Furman

On 12/23/2013 07:10 AM, Roy Smith wrote:

In article ,
  Ethan Furman  wrote:


On 12/22/2013 08:57 PM, Roy Smith wrote:

In article <52b7a0e4$0$29994$c3e8da3$54964...@news.astraweb.com>,
   Steven D'Aprano  wrote:


Anyway, I may be completely misinterpreting what I'm reading. Perhaps the
assertion is checking a function invariant ("one of the strategies will
always succeed") in which case you're doing it exactly right and I should
shut up now :-)


Yes :-)

More specifically, the assertion exception will get caught way up in
some django middleware which will log a stack trace and return a HTTP
50-something.  This will typically be followed by somebody like me
noticing the stack dump and trying to figure out WTF happened.


This is completely misusing what assertions are for.  I hope this bit of
middleware (or django itself) is very clear
about never running with assertions turned off.


Sigh.  Sometimes I'm not sure which is worse.  The anti-assertion
zealotry on this list, or the anti-regex zealotry.


I am not a zealot (I'm not!  Really!! ;) .  I just find it alarming to have major pieces of software rely on a feature 
that can be so easily tuned out, and it wasn't clear from your comment that it was /any/ exception.


Mostly I don't want newbies thinking "Hey!  I can use assertions for all my 
confidence testing!"

Just as one data point OpenERP, which has a lot of good features, unfortunately 
uses assert to test user input.  :(

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


Re: cascading python executions only if return code is 0

2013-12-23 Thread Steven D'Aprano
Dennis Lee Bieber wrote:

> On Mon, 23 Dec 2013 13:33:08 +1100, Steven D'Aprano
>  declaimed the following:
> 
>>Roy Smith wrote:
>>
>>
>>> else:
>>> assert 0, "can't create picker (classes = %s)" % classes
>>
>>¡Ay, caramba! I was with you until the very last line. The above code is
>>possibly buggy and inappropriately designed. (I may be completely
>>misinterpreting this, in which case feel free to ignore the following
>>rant.)
>>
> 
> I'd think the biggest problem with this is that if one runs Python with
> optimization turned on, the assert statement itself vanishes, leaving one
> with an empty else clause...
> 
> Try debugging that problem!


Actually, that's not correct. If the assert statement vanishes, so does the
else clause, since there's nothing in it.


[steve@ando ~]$ python2.7 -c "from dis import dis
> code = compile('''\
> for x in [1, 2, 3]:
> pass
> else:
> assert x
> ''', '', 'exec')
> dis(code)"""
  1   0 SETUP_LOOP  35 (to 38)
  3 LOAD_CONST   0 (1)
  6 LOAD_CONST   1 (2)
  9 LOAD_CONST   2 (3)
 12 BUILD_LIST   3
 15 GET_ITER
>>   16 FOR_ITER 6 (to 25)
 19 STORE_NAME   0 (x)

  2  22 JUMP_ABSOLUTE   16
>>   25 POP_BLOCK

  4  26 LOAD_NAME0 (x)
 29 POP_JUMP_IF_TRUE38
 32 LOAD_GLOBAL  1 (AssertionError)
 35 RAISE_VARARGS1
>>   38 LOAD_CONST   3 (None)
 41 RETURN_VALUE


Compare to the case with optimizations on:


[steve@ando ~]$ python2.7 -O -c "from dis import dis
code = compile('''\
for x in [1, 2, 3]:
pass
else:
assert x
''', '', 'exec')
dis(code)"""
  1   0 SETUP_LOOP  23 (to 26)
  3 LOAD_CONST   0 (1)
  6 LOAD_CONST   1 (2)
  9 LOAD_CONST   2 (3)
 12 BUILD_LIST   3
 15 GET_ITER
>>   16 FOR_ITER 6 (to 25)
 19 STORE_NAME   0 (x)

  2  22 JUMP_ABSOLUTE   16
>>   25 POP_BLOCK

  4 >>   26 LOAD_CONST   3 (None)
 29 RETURN_VALUE





-- 
Steven

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


Re: cascading python executions only if return code is 0

2013-12-23 Thread Peter Otten
Ethan Furman wrote:

>> Sigh.  Sometimes I'm not sure which is worse.  The anti-assertion
>> zealotry on this list, or the anti-regex zealotry.
> 
> I am not a zealot (I'm not!  Really!! ;) .  I just find it alarming to
> have major pieces of software rely on a feature that can be so easily
> tuned out, and it wasn't clear from your comment that it was /any/
> exception.
> 
> Mostly I don't want newbies thinking "Hey!  I can use assertions for all
> my confidence testing!"
> 
> Just as one data point OpenERP, which has a lot of good features,
> unfortunately uses assert to test user input.  :(

Put the following at the beginning of every affected module:

try:
assert 0
except AssertionError:
pass
else:
raise AssertionError("won't run with assertions turned off")

:)

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


Re: GUI:-please answer want to learn GUI programming in python , how should i proceed.

2013-12-23 Thread Wolfgang Keller
> > On an actual operating system, the attitude of the developers (do
> > they actually care or just don't give a darn) is *the* critical
> > issue for end-user productivity. If a developer makes a statement
> > such as of "just get a faster computer" or "just get more RAM",
> > then (s)he probably doesn't give darn. C++ applications, just like
> > Java applications, tend to leak horrible amounts of memory these
> > days, just because the vendors/developers don't care.
> 
> I'll note that Python core developers do care about memory leaks.

And that's a really good thing.

But unfortunately, these days, it's rather the exception.

Sincerely,

Wolfgang
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Roy Smith
On Monday, December 23, 2013 12:05:22 PM UTC-5, Steven D'Aprano wrote:
> Roy Smith wrote:

> > And, yes, I know that assertions get turned off with -O (frankly, I
> > think that's a design flaw).  We don't run with -O.
> 
> 
> Until such time as somebody decides they can speed up your code by 5% by
> running with optimizations turned on.

Well, there's lots of changes people could make that would break things.  Many 
of them are in the name of efficiency. [1]  But, let's say they did that.  We 
would fall off the end of the function, return None, and the caller would end 
up doing:

with None:
whatever

leaving somebody to puzzle over why the logs contained a stack dump ending in:

AttributeError: __exit__

So, here's the deeper question.  Is your issue strictly that -O elides assert 
statements? [2]  That's a purely mechanical issue that could be solved by using 
the rather more verbose:

if not condition:
raise AssertionError("")

Would you feel differently then?

> So there's always tension between "why am I testing something that can't
> fail?" and "but what if it does?"

Trust, but verify.  Especially when the thing you're verifying is your 
understanding of how your own code works :-)

[1] and most of those are premature optimizations.  To a first order 
approximation, for us, application speed is all about database performance, and 
not at all about Python code execution speed.  That's a pretty good second 
order approximation as well.

[2] In which case, we would just add some middleware which did:

assert "-O" not in sys.argv
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: GUI:-please answer want to learn GUI programming in python , how should i proceed.

2013-12-23 Thread wxjmfauth
Le lundi 23 décembre 2013 18:59:41 UTC+1, Wolfgang Keller a écrit :
> > > On an actual operating system, the attitude of the developers (do
> 
> > > they actually care or just don't give a darn) is *the* critical
> 
> > > issue for end-user productivity. If a developer makes a statement
> 
> > > such as of "just get a faster computer" or "just get more RAM",
> 
> > > then (s)he probably doesn't give darn. C++ applications, just like
> 
> > > Java applications, tend to leak horrible amounts of memory these
> 
> > > days, just because the vendors/developers don't care.
> 
> > 
> 
> > I'll note that Python core developers do care about memory leaks.
> 
> 
> 
> And that's a really good thing.
> 
> 
> 
> But unfortunately, these days, it's rather the exception.
> 
> 

Memory? Let me laugh!

>>> sys.getsizeof('a')
26
>>> sys.getsizeof('€')
40
>>> 40 - 26
14
>>> sys.getsizeof('\U0001')
44
>>> 44 - 26
18


I'm deeply convinced, one will soon see that
technology in CID font.

jmf
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Chris Angelico
On Tue, Dec 24, 2013 at 6:03 AM, Roy Smith  wrote:
> [2] In which case, we would just add some middleware which did:
>
> assert "-O" not in sys.argv

Aside from the fact that this wouldn't work, it won't work :) By the
time you see argv, the -O option has been eaten. But why stop at that?

def assertions_working():
try:
assert False
except AssertionError:
return True
return False

assert assertions_working()  # refuse to run in -O mode

Can't imagine why that wouldn't work..

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: GUI:-please answer want to learn GUI programming in python , how should i proceed.

2013-12-23 Thread Chris Angelico
On Tue, Dec 24, 2013 at 6:05 AM,   wrote:
> Memory? Let me laugh!
>

Is it a crime to hijack an already-hijacked thread?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: GUI:-please answer want to learn GUI programming in python , how should i proceed.

2013-12-23 Thread Mark Lawrence

On 23/12/2013 19:14, Chris Angelico wrote:

On Tue, Dec 24, 2013 at 6:05 AM,   wrote:

Memory? Let me laugh!



Is it a crime to hijack an already-hijacked thread?

ChrisA



Yes, especially when written with our favourite bug ridden pile of garbage.

Perhaps we should write up something for the Python wiki so that 
everyone can send double line feeds from any technology, that way 
everybody would feel at home as we'd all be in the same boat.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Re: GUI:-please answer want to learn GUI programming in python , how should i proceed.

2013-12-23 Thread Dan Stromberg
On Sat, Dec 14, 2013 at 4:25 AM, Chris Angelico  wrote:
> On Sat, Dec 14, 2013 at 11:12 PM, Jai  wrote:
>> GUI:-want to learn GUI programming in python  , how should i proceed.
>>
>> There are lots of book here so I am  confuse which book  i should refer so 
>> that i don't waste time . please answer
>
> There are many ways to build a GUI with Python. Some of the more
> popular toolkits are Tk (tkinter), wxWidgets (wxpython), and GTK.
> Explore those and see which one you like; I personally quite like GTK,
> and the others have their fans too. There are GUI builders for each of
> the above (I think; definitely wx and GTK do), or you can build
> everything directly in code (my preferred style). Play around with it
> and see what you like!

I also like GTK.  I thought it would be relevant to point out that
pygtk only works on Python 2.x; gi.repository.Gtk works on 2.x and
3.x.
-- 
https://mail.python.org/mailman/listinfo/python-list


RE: cascading python executions only if return code is 0

2013-12-23 Thread Nick Cash
> assert assertions_working()  # refuse to run in -O mode
> 
> Can't imagine why that wouldn't work..

Why overthink this? 

assert not sys.flags.optimize

is clearly the one, and only one, obvious way to do it. 

Of course, it works about as well as the rest of these solutions. Which is to 
say, not at all.

-Nick Cash
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Using asyncio in event-driven network library

2013-12-23 Thread Terry Reedy

On 12/23/2013 11:47 AM, Chris Angelico wrote:

On Mon, Dec 23, 2013 at 10:50 PM, Tobias M.  wrote:

I am currently writing an event-driven client library for a network protocol
[1] and chose to use the new asyncio module. I have no experience with
asynchronous IO and don't understand all the concepts in asyncio yet. So I'm
not sure if asyncio is actually the right choice .


As was acknowledged on -tutor, asyncio is extremely new. I don't know
if anyone's yet used it for GUI handling, or even if the module was
intended for that.


Asyncio is aimed, as the name says, at non-blocking io, but allows 
non-blocking on other tasks. It replaces asyncore, now deprecated (but 
not removed). Developers of Twisted, Tornado, and other async frameworks 
had input. It was partly inspired, I think, by C#'s await, which allows 
async code to look much like normal blocking code (Guido does not like 
writing callbacks ;-). The default event loop is intended to be 
replaceable by other event loops that expose the same interface.


I know that Twisted can work with GUI loops but I do not know the 
details.  I suspect that the same methods can and will be make to work 
with the asyncio loop, or that the Twisted loop will get an adaptor so 
it can replace the asyncio default loop.


What would be easiest for user-developers would be if someone were able 
to wrap a gui loop in a way to give it the needed interface, so the gui 
loop itself replaced and became the asyncio loop.



You may be able to wrap your GUI code up so it fits inside asyncio.
Rather than run a simple event loop, you could have your own loop that
does some kind of checking: PyGTK has functions for peeking at events
and doing just one iteration of the main loop, so you could pump all
events with something like this:

while gtk.events_pending(): gtk.main_iteration()

No doubt other toolkits have something similar.


I think tk(inter) has 'run pending events' or something.


Alternatively, you could run two threads: one for your GUI, and
another one for your asyncio. How easy and clean that would be is hard
to say without trying it; it could work out beautifully for your code,
or it could be horribly clunky.

This might be your big contribution to Python for the year: a nice,
easy, working example of using asyncio with a GUI toolkit!


Such would definitely be welcome.

--
Terry Jan Reedy

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


Please stop the trolling

2013-12-23 Thread Terry Reedy

On 12/23/2013 2:05 PM, wxjmfa...@gmail.com wrote:


Le lundi 23 décembre 2013 18:59:41 UTC+1, Wolfgang Keller a écrit :

[me]

I'll note that Python core developers do care about memory leaks.

And that's a really good thing.



Memory? Let me laugh!
[snip repeated (for about the 5th time) posting of single character 
memory sizes]


Jim, since I know you are smart enough to know the different between a 
small fixed size and a continuous leak, I can only think that the 
purpose of your repeated post was to get similarly a inane response from 
a couple of other people. That is the definition of trolling. It is 
disrespectful of others and in my opinion is a violation of the Python 
Code of Conduct, which *does* apply to python-list. Please desist.


--
Terry Jan Reedy


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


Re: Deamonify my python script on Android

2013-12-23 Thread Ben Finney
Kevin Peterson  writes:

> I want to daemonify my python script on Android device. That is, it
> should be automatically invoked on boot up.

Those aren't the same thing. To daemonise a program is independent of
whether the program starts automatically; it can start automatically
without detaching (e.g. the process that requests a login) it can detach
on request without starting automatically (e.g. any daemon started by
a user), etc.

If you want the program to start automatically on boot up, that's not a
python question; you want to find out about the Android boot process and
what facilities it has for starting a program on boot. But that's
independent of Python and independent of daemonising the program.

If you want to have a program become a daemon (i.e. a background
process) https://en.wikipedia.org/wiki/Daemon_%28computing%29> the
‘python-daemon’ library is designed for this
https://pypi.python.org/pypi/python-daemon/>. But that's nothing to
do with automatic start-up, nor Android; you're on your own for those.

Good hunting!

-- 
 \  “My interest is in the future, as I am going to spend the rest |
  `\  of my life there.” —Charles F. Kettering |
_o__)  |
Ben Finney

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


Re: Please stop the trolling

2013-12-23 Thread Mark Lawrence

On 23/12/2013 20:53, Terry Reedy wrote:

On 12/23/2013 2:05 PM, wxjmfa...@gmail.com wrote:


Le lundi 23 décembre 2013 18:59:41 UTC+1, Wolfgang Keller a écrit :

[me]

I'll note that Python core developers do care about memory leaks.

And that's a really good thing.



Memory? Let me laugh!

[snip repeated (for about the 5th time) posting of single character
memory sizes]

Jim, since I know you are smart enough to know the different between a
small fixed size and a continuous leak, I can only think that the
purpose of your repeated post was to get similarly a inane response from
a couple of other people. That is the definition of trolling. It is
disrespectful of others and in my opinion is a violation of the Python
Code of Conduct, which *does* apply to python-list. Please desist.



Thanks for this Terry.  I was sorely tempted earlier to say something 
but managed to keep control of myself.  Now I'll be keeping quiet and 
let Jim speak for himself.


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


How to check the date validity?

2013-12-23 Thread Igor Korot
Hi, ALL,
I'm working on the python script which reads the data from the csv file.
In this file I have 3 different kind of fields: one consist of the
sole date, one - sole time and one - datetime. The time includes
milliseconds, i.e. "12:55:55.705"
All fields of the file including those 3 I am reading as the string.
All those strings after validating will go into mySQL table.

Now, it looks that the python way of validating the date/time of the
string is to call strptime(). However looking at the docs and trying
to use the function I didn't find a way to check for the milliseconds.
Now the dates can be formatted differently I think according to the
locale under which the csv is generated.

So, my question is: since there is a simple way of inserting
preformatted string into the datetime field of mySQL, how do I
validate the date string?
I don't want to convert that string to datetime object, just want to
check if all those types of dates are good dates and does not contain
garbage.

I tried to do an RE, but its static validation and so will not work in
all cases.

Thank you for any hints.
-- 
https://mail.python.org/mailman/listinfo/python-list


[OT]Royal pardon for codebreaker Turing

2013-12-23 Thread Mark Lawrence
Maybe of interest to some of you 
http://www.bbc.co.uk/news/technology-25495315


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Re: How to check the date validity?

2013-12-23 Thread Chris Angelico
On Tue, Dec 24, 2013 at 11:30 AM, Igor Korot  wrote:
> So, my question is: since there is a simple way of inserting
> preformatted string into the datetime field of mySQL, how do I
> validate the date string?

Well, the easiest way would be to simply attempt the SQL query. If it
comes back with an error, the date wasn't valid. To protect a
transaction against being broken by an error, you can use a SAVEPOINT
- check the database docs. (I've never actually used savepoints in
MySQL, only PostgreSQL, so things may be a bit different. Check the
docs.)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to check the date validity?

2013-12-23 Thread Mark Lawrence

On 24/12/2013 00:30, Igor Korot wrote:

Hi, ALL,
I'm working on the python script which reads the data from the csv file.
In this file I have 3 different kind of fields: one consist of the
sole date, one - sole time and one - datetime. The time includes
milliseconds, i.e. "12:55:55.705"
All fields of the file including those 3 I am reading as the string.
All those strings after validating will go into mySQL table.

Now, it looks that the python way of validating the date/time of the
string is to call strptime(). However looking at the docs and trying
to use the function I didn't find a way to check for the milliseconds.
Now the dates can be formatted differently I think according to the
locale under which the csv is generated.

So, my question is: since there is a simple way of inserting
preformatted string into the datetime field of mySQL, how do I
validate the date string?
I don't want to convert that string to datetime object, just want to
check if all those types of dates are good dates and does not contain
garbage.

I tried to do an RE, but its static validation and so will not work in
all cases.

Thank you for any hints.



You actually need the %f format code for milliseconds, not microseconds. 
 Note 5 from 
http://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior 
"When used with the strptime() method, the %f directive accepts from one 
to six digits and zero pads on the right. %f is an extension to the set 
of format characters in the C standard (but implemented separately in 
datetime objects, and therefore always available)"


--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

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


Re: [OT]Royal pardon for codebreaker Turing

2013-12-23 Thread Tim Johnson

* Mark Lawrence  [131223 15:39]:
> Maybe of interest to some of you 
> http://www.bbc.co.uk/news/technology-25495315
  I note the following
  """
  "Dr Alan Turing was an exceptional man with a brilliant mind," said
  Mr Grayling.

  He said the research Turing carried out during the war at
  Bletchley Park undoubtedly shortened the conflict and saved
  thousands of lives.
  """

  From my research - general (and later president) Eisenhower seemed
  to think that Project Ultra save millions (not thousands) of
  lives, shortened and maybe won the War.
  - Never forget. 

-- 
Tim 
tim at tee jay forty nine dot com or akwebsoft dot com
http://www.akwebsoft.com, http://www.tj49.com
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How to check the date validity?

2013-12-23 Thread Jason Friedman
> In this file I have 3 different kind of fields: one consist of the
> sole date, one - sole time and one - datetime. The time includes
> milliseconds, i.e. "12:55:55.705"
> All fields of the file including those 3 I am reading as the string.
> All those strings after validating will go into mySQL table.

> I tried to do an RE, but its static validation and so will not work in
> all cases.

Not sure what you mean by static validation.  Would this not work?
import re
if re.search(r"\d{1,2}:\d{2}:\d{2}(.\d{1,3})?", "12:55:55.705"):
# It's a time
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: python socket query

2013-12-23 Thread smilesonisamal
On Monday, December 23, 2013 8:49:30 AM UTC+5:30, Chris Angelico wrote:
> On Mon, Dec 23, 2013 at 2:05 PM,   wrote:
> 
> > I wrote a small program which creates the socket, bind to the socket, 
> > connect to socket and send() close(). I see that there is no reply coming 
> > from server and the TCP disconnect happens.
> 
> > import socket
> 
> >
> 
> > def tcp(host, request, port=34567):
> 
> >
> 
> > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> 
> >
> 
> > s.connect((host, port))
> 
> >
> 
> > s.send(request)
> 
> >
> 
> > reply = s.recv(2**14)
> 
> >
> 
> > s.close()
> 
> >
> 
> > return reply
> 
> 
> 
> First off, your formatting has become mangled. This is likely to be
> 
> because of Google Groups, which tends to make a mess of posts. I
> 
> strongly recommend you get a better newsreader, such as Thunderbird,
> 
> or switch to the mailing list:
> 
> 
> 
> https://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> I'm going to assume that (a) the code you've provided is all part of
> 
> the tcp() function, and (b) that you are actually calling tcp()
> 
> somewhere and seeing what comes back. But once you sort out your
> 
> posting issues, you may want to post a complete program (probably not
> 
> more than a couple of additional lines beyond what you have above) so
> 
> we know what's actually going on.
> 
> 
> 
> Terminology point: You mention binding to the socket. In networking,
> 
> "bind" has a specific meaning - binding to an address, usually done
> 
> for servers, and something you're not doing here.
> 
> 
> 
> Are you sure the server's doing something? I tried what you had there
> 
> (albeit under Python 3.3), and it seems to be fine. Perhaps you need
> 
> to terminate the request with something - maybe a newline. The send()
> 
> method will send exactly the bytes you give it, nothing more.
> 
> 
> 
> Can you telnet to the server?
> 
> 
> 
Thanks Chris.I have put bind call but it did not work.  
Even telnet hangs if i tried to connect to the same IP and port manually. Is 
there a workaround to fix this issue?

If the socket gets closed at the other end. How can we get around with the 
issue? Any idea?
> ChrisA

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


Re: cascading python executions only if return code is 0

2013-12-23 Thread Steven D'Aprano
Roy Smith wrote:

> So, here's the deeper question.  Is your issue strictly that -O elides
> assert statements?  That's a purely mechanical issue that could be
> solved by using the rather more verbose:
> 
> if not condition:
> raise AssertionError("")
> 
> Would you feel differently then?


Not quite. As I said in my initial rant, there's also the issue of using an
appropriate exception. Naming is important for comprehending what the code
does. Just as it's bad to do:

my_list = 42
my_str = [my_list * 2]


so it's bad to have badly chosen exceptions:

def handle_string(s):
if not isinstance(s, str):
raise ImportError("not a string")
...


Substitute AssertionError for ImportError:

def handle_string(s):
if not isinstance(s, str):
raise AssertionError("not a string")
...


and it's still wrong because the exception is misleading and/or confusing to
whoever has to deal with the code.

I've been known to use asserts inside private functions and explicit tests
inside public ones:

def func(x):
if x <= 0:
raise ValueError("x must be positive")
return _func(x + 1)

def _func(x):
assert x > 1
return sqrt(1/(x-1))


If I think that the test is checking an internal invariant, assert is okay,
if not, it isn't. In this case, the fact that the actual calculation of
func(x) is factored out into a private function _func is purely an internal
implementation issue. I might refactor the code and move _func inline:

def func(x):
if x <= 0:
raise ValueError("x must be positive")
x += 1
assert x > 1
return sqrt(1/(x-1))


in which case the assertion is obviously checking an internal invariant and
therefore acceptable. So it's still checking an internal invariant even
when the code is moved into a purely internal function.

The other rule I use is that if an assert might ever be triggered in the
normal run of the program, it shouldn't be an assert. In principle, we
should run Python code with -O in production.

(It would have been better if Python optimized code by default, and had a -D
switch to turn debugging on, including asserts. I believe that's what
languages like Eiffel do.)



-- 
Steven

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


Re: python socket query

2013-12-23 Thread Chris Angelico
On Tue, Dec 24, 2013 at 2:33 PM,   wrote:
> Thanks Chris.I have put bind call but it did not work.
> Even telnet hangs if i tried to connect to the same IP and port manually. Is 
> there a workaround to fix this issue?
>
> If the socket gets closed at the other end. How can we get around with the 
> issue? Any idea?

Normally, bind isn't necessary for client connections. It's almost
never significant, unless you have multiple IP addresses or you
actually need to specify the port.

If telnet hangs, you need to figure out what your server is doing.
Without knowing the server at all, I can't really help.

But please, have a look at how your post comes out:

https://mail.python.org/pipermail/python-list/2013-December/663308.html

All those messy blank lines are because of Google Groups. There are
other problems, as well. Please, can you switch to a better client, so
we don't have to wade through that mess? Mozilla Thunderbird is one
good option, or you can join the mailing list:

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

Thanks!

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cascading python executions only if return code is 0

2013-12-23 Thread Roy Smith
In article <52b90263$0$30003$c3e8da3$54964...@news.astraweb.com>,
 Steven D'Aprano  wrote:

> Roy Smith wrote:
> 
> > So, here's the deeper question.  Is your issue strictly that -O elides
> > assert statements?  That's a purely mechanical issue that could be
> > solved by using the rather more verbose:
> > 
> > if not condition:
> > raise AssertionError("")
> > 
> > Would you feel differently then?
> 
> 
> Not quite. As I said in my initial rant, there's also the issue of using an
> appropriate exception.
> [...]
> If I think that the test is checking an internal invariant, assert is okay,
> if not, it isn't.

Well, in this case, that's what it's doing, so I guess I'm good :-)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [OT]Royal pardon for codebreaker Turing

2013-12-23 Thread Steven D'Aprano
On Tue, 24 Dec 2013 00:32:31 +, Mark Lawrence wrote:

> Maybe of interest to some of you
> http://www.bbc.co.uk/news/technology-25495315

While I'm happy for Alan Turing, may he rest in peace, I think the 
thousands of other homosexuals who have been prosecuted for something 
which shouldn't be a crime in the first place might be a bit peeved that 
he is singled out for a pardon.

Personally, I think that people ought to throw a party celebrating 
Turing's rehabilitation, and do it right outside the Russian Embassy.



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


Re: How to check the date validity?

2013-12-23 Thread Andreas Perstinger
Jason Friedman  wrote:
>Would this not work?
>import re
>if re.search(r"\d{1,2}:\d{2}:\d{2}(.\d{1,3})?", "12:55:55.705"):
># It's a time

No, because this regexp also matches stuff like "99:99:99.999".
Checking for the number of digits is not enough, because not all
combinations of two digits are valid hours, minutes or seconds.

Bye, Andreas
-- 
https://mail.python.org/mailman/listinfo/python-list