Question on Socket Timeouts

2012-11-18 Thread Abhijeet Mahagaonkar
Hi all,

I'm new to network programming.
I have a question.

Can we set a timeout to limit how long a particular socket can read or
write?

I have used a settimeout() function.
The settimeout() works fine as long as the client doesnt send any data for
x seconds.
The data that I receive in the server after accept()ing a connect() from a
client I check if the client is sending any invalid data.

I'm trying to ensure that a client sending invalid data constantly cannot
hold the server. So is there a way of saying I want the client to use this
socket for x seconds before I close it, no matter what data I receive?

Thanks in advance,

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


Re: Python Interview Questions

2012-11-18 Thread Steven D'Aprano
On Sat, 17 Nov 2012 10:01:01 -0800, chinjannisha wrote:

> Hi I had one doubt.. I know very little bit of python .I wanted to know
> when to use list,tuple,dictionary and set? Please reply me asap

Use a list when you want a list of items that should all be treated the 
same way:

list_of_numbers = [1, 3, 5.1, 2, 6.5]

total = sum(list_of_numbers)


or when you need a collection of items where the order they are in is 
important:

winners = ['george', 'susan', 'henry']  # 1st, 2nd, 3rd place

print('The winner is:', winners[0])


Use a tuple when you want a collection of items that mean different 
things, a bit like a C struct or Pascal record:

a = (23, "henry", True, 'engineer')
b = (42, "natalie", False, 'manager')
c = (17, "george", True, 'student')


Use a dict when you need a collection of key:value mappings:

config = {'name': 'fred', 'pagesize': 'A4', 'verbose': True, 'size': 18}
address = {'fred': '[email protected]', 'sally': '[email protected]'}

if config['verbose']:
print('sending email...')
send_email_to(address['fred'], "Subject: Hello")


Use a set when you want to represent a collection of items and the order 
is not important:

failed = {'fred', 'tom', 'sally'}  # set literal syntax in Python 3 only
# use set(['fred', 'tom', 'sally']) in Python 2

if 'george' in failed:
print('George, you have failed!')



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


Horror Horror Horror!!!!!

2012-11-18 Thread MoneyMaker
Welcome to the site of horror:  http://horrorhorrorhorror.webs.com/

Join now free !!!  Free Horror Movies!! Free Horror Games
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml data or other?

2012-11-18 Thread Artie Ziff

On 11/9/12 5:50 AM, rusi wrote:

On Nov 9, 5:54 pm, Artie Ziff  wrote:
# submit correctedinput to etree 
I was very grateful to get the "leg up" on getting started down that 
right path with my coding. Many thanks to you, rusi. I took your 
excellent advices and have this working.


class Converter():
PREFIX = """

"""
POSTFIX = ""
def __init__(self, data):
self.data = data
self.writeXML()
def writeXML(self):
pattern = re.compile('')
replaceStr = r''
xmlData = re.sub(pattern, replaceStr, self.data)
self.dataXML = self.PREFIX + xmlData.replace("\\", "/") + 
self.POSTFIX


###  main
# input to script is directory:
# sanitize trailing slash
testPkgDir = sys.argv[1].rstrip('/')
# Within each test package directory is doc/testcase
tcDocDir = "doc/testcases"
# set input dir, containing broken files
tcTxtDir = os.path.join(testPkgDir, tcDocDir)
# set output dir, to write proper XML files
tcXmlDir = os.path.join(testPkgDir, tcDocDir + "_XML")
if not os.path.exists(tcXmlDir):
os.makedirs(tcXmlDir)
# iterate through files in input dir
for filename in os.listdir(tcTxtDir):
# set filepaths
filepathTXT = os.path.join(tcTxtDir, filename)
base = os.path.splitext(filename)[0]
fileXML = base + ".xml"
filepathXML = os.path.join(tcXmlDir, fileXML)
# read broken file, convert to proper XML
with open(filepathTXT) as f:
c = Converter(f.read())
xmlFO = open(filepathXML, 'w')   # xmlFileObject
xmlFO.write(c.dataXML)
xmlFO.close()

###

Writing XML files so to see whats happening. My plan is to
keep xml data in memory and parse with xml.etree.ElementTree.

Unfortunately, xml parsing fails due to angle brackets inside
description tags. In particular, xml.etree.ElementTree.parse()
aborts on '<' inside xml data such as the following:



This testcase tests if crontab  installs the cronjob
and cron schedules the job correctly.
<\description>

##

What is right way to handle the extra angle brackets?
Substitute on line-by-line basis, if that works?
Or learn to write a simple stack-style parser, or
recursive descent, it may be called?

I am open to comments to improve my code more to be more readable,
pythonic, or better.

Many thanks
AZ

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


Problem with subprocess.call and windows schtasks

2012-11-18 Thread Tom Borkin
Hi,
I have this code:

#!\Python27\python

import subprocess
#subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program
Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True)
subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
/ST 07:50'], shell=True)
With either call, I get this error:
C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
The system cannot find the path specified.

If I remove the ", shell=True" I get this:
C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
 C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py,
line 4, in 
  subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
/ST 07:50'])
 File "C:\Python27\lib\subprocess.py", line 493, in call
  return Popen(*popenargs, **kwargs).wait()
 File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread,
errwrite)
 File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 2] The system cannot find the file specified
The file exists in said directory. I can execute it from the cmd prompt.

So I tried this:
pgm = "SchTasks"
args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50']
#args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST
07:50']
cmd = [pgm]
cmd.extend(args)
subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
but got this error:
ERROR: Invalid argument/option - <>

If I use the other args list I get this error:
ERROR: Invalid argument/option - '/SC ONCE'
so apparently it liked the first argument.

Please advise.
TIA,
Tom
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-11-18 Thread Roy Smith
In article <[email protected]>,
 Steven D'Aprano  wrote:

> Use a list when you want a list of items that should all be treated the 
> same way [...] or when you need a collection of items where the order they 
> are in is 
> important:
>
> Use a tuple when you want a collection of items that mean different 
> things, a bit like a C struct or Pascal record:

That is certainly the right answer according to the One True Church Of 
Pythonic Orthodoxy And Theoretical Correctness.  But, let me give an 
alternative answer which works for The Unwashed Masses Who Live In The 
Trenches And Write Python Code For A Living:

Use a list when you need an ordered collection which is mutable (i.e. 
can be altered after being created).  Use a tuple when you need an 
immutable list (such as for a dictionary key).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml data or other?

2012-11-18 Thread rusi
On Nov 18, 6:32 pm, Artie Ziff  wrote:
> Unfortunately, xml parsing fails due to angle brackets inside
> description tags. In particular, xml.etree.ElementTree.parse()
> aborts on '<' inside xml data such as the following:
>
> 
>      
>          This testcase tests if crontab  installs the cronjob
>          and cron schedules the job correctly.
>      <\description>
>
> ##
>
> What is right way to handle the extra angle brackets?
> Substitute on line-by-line basis, if that works?
> Or learn to write a simple stack-style parser, or
> recursive descent, it may be called?
>
> I am open to comments to improve my code more to be more readable,
> pythonic, or better.
>
> Many thanks
> AZ

Start with cgi.escape perhaps?
http://docs.python.org/2/library/cgi.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Point of idle curiosity

2012-11-18 Thread Steven D'Aprano
On Sun, 18 Nov 2012 22:45:43 +1100, Chris Angelico wrote:

> (if you'll forgive the pun)
> 
> Is IDLE named after Eric of that name, or is it pure coincidence?

Well, IDLE is an IDE. The L doesn't seem to mean anything, so it's 
plausible that it is named after Eric Idle.


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


Re: xml data or other?

2012-11-18 Thread rusi
On Nov 18, 8:54 pm, rusi  wrote:

> Start with cgi.escape perhaps?http://docs.python.org/2/library/cgi.html

This may be a better link for starters
http://wiki.python.org/moin/EscapingHtml
(Note the escaping xml at the bottom)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Point of idle curiosity

2012-11-18 Thread Mark Lawrence

On 18/11/2012 15:59, Steven D'Aprano wrote:

On Sun, 18 Nov 2012 22:45:43 +1100, Chris Angelico wrote:


(if you'll forgive the pun)

Is IDLE named after Eric of that name, or is it pure coincidence?


Well, IDLE is an IDE. The L doesn't seem to mean anything, so it's
plausible that it is named after Eric Idle.




https://en.wikipedia.org/wiki/IDLE_%28Python%29

--
Cheers.

Mark Lawrence.

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


Re: Point of idle curiosity

2012-11-18 Thread Chris Angelico
On Mon, Nov 19, 2012 at 3:08 AM, Mark Lawrence  wrote:
> On 18/11/2012 15:59, Steven D'Aprano wrote:
>>
>> On Sun, 18 Nov 2012 22:45:43 +1100, Chris Angelico wrote:
>>
>>> (if you'll forgive the pun)
>>>
>>> Is IDLE named after Eric of that name, or is it pure coincidence?
>>
>>
>> Well, IDLE is an IDE. The L doesn't seem to mean anything, so it's
>> plausible that it is named after Eric Idle.
>
> https://en.wikipedia.org/wiki/IDLE_%28Python%29
>

Thanks both. I went looking at the docs, but didn't find anything
conclusive either way. It's sometimes hard to know just how much Monty
Python there is in the language... which justified me getting a bit
naughty a couple of weeks ago and almost citing the Python language
in-character in my Dungeons and Dragons campaign:

"There is still a cheeseshop owned by his family. It's called PyPi,
though whether that's an abbreviation for something or not is not
reported here."

And then when they went to the place, I described it thusly:

"It's all done up with a pythonic decor. Everything's nicely done up
with snakes; the walls have indented blocks with no braces on them."

Now I just need to find a signwriter and graphic designer named Eric
Haffabee, laying around idle all the time... or maybe that's too
obscure a set of references.

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


Re: Problem with subprocess.call and windows schtasks

2012-11-18 Thread Tim Golden

On 18/11/2012 13:48, Tom Borkin wrote:

import subprocess
#subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program
Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True)
subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
/ST 07:50'], shell=True)
With either call, I get this error:
C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
The system cannot find the path specified.


Generally, with subprocess call:

* Pass each element in the command line as a separate element in the 
list: ['SchTasks', '/Create', '/SC', 'ONCE', ... &c.]


* Don't use shell=True unless you're running a command which is internal 
to cmd.exe (such as dir, copy, etc.). If you're running anything which 
exists as its own .exe, you shouldn't be using shell=True.


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


Re: Problem with subprocess.call and windows schtasks

2012-11-18 Thread Chris Rebert
On Sun, Nov 18, 2012 at 5:48 AM, Tom Borkin  wrote:
> Hi,
> I have this code:
>
> #!\Python27\python
>
> import subprocess
> #subprocess.call(['SchTasks /Create /SC ONCE /TN "My Tasks" /TR "C:/Program
> Files/Apache Group/Apache2/htdocs/ccc/run_alert.py" /ST 07:50'], shell=True)
> subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
> /ST 07:50'], shell=True)
> With either call, I get this error:
> C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
> The system cannot find the path specified.
>
> If I remove the ", shell=True" I get this:
> C:\Program Files\Apache Group\Apache2\htdocs\ccc>cron_alert_activity.py
>  C:\Program Files\Apache Group\Apache2\htdocs\ccc\cron_alert_activity.py,
> line 4, in 
>   subprocess.call(['SchTasks /Create /SC ONCE /TN "test" /TR "run_alert.py"
> /ST 07:50'])
>  File "C:\Python27\lib\subprocess.py", line 493, in call
>   return Popen(*popenargs, **kwargs).wait()
>  File "C:\Python27\lib\subprocess.py", line 679, in __init__ errread,
> errwrite)
>  File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
> startupinfo)
> WindowsError: [Error 2] The system cannot find the file specified
> The file exists in said directory. I can execute it from the cmd prompt.

Per the docs 
(http://docs.python.org/2/library/subprocess.html#frequently-used-arguments
):
"If passing a single string [as the `args` argument], either `shell`
must be True (see below) or else the string must simply name the
program to be executed **without specifying any arguments.**"
(emphasis mine)

> So I tried this:
> pgm = "SchTasks"
> args = ['/Create /SC ONCE /TN "test" /TR "run_alert.py" /ST 07:50']
> #args = ['/Create', '/SC ONCE', '/TN "test"', '/TR "run_alert.py"', '/ST
> 07:50']
> cmd = [pgm]
> cmd.extend(args)
> subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
> but got this error:
> ERROR: Invalid argument/option - <>
>
> If I use the other args list I get this error:
> ERROR: Invalid argument/option - '/SC ONCE'
> so apparently it liked the first argument.
>
> Please advise.

Your tokenization of your command is incorrect. Consult the Note box
in the docs regarding `args` tokenization, and apply it to your
command:
http://docs.python.org/2/library/subprocess.html#subprocess.Popen

The-docs-are-your-friend-ly Yours,
Chris
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-11-18 Thread Steven D'Aprano
On Sun, 18 Nov 2012 08:53:25 -0500, Roy Smith wrote:

> In article <[email protected]>,
>  Steven D'Aprano  wrote:
> 
>> Use a list when you want a list of items that should all be treated the
>> same way [...] or when you need a collection of items where the order
>> they are in is important:
>>
>> Use a tuple when you want a collection of items that mean different
>> things, a bit like a C struct or Pascal record:
> 
> That is certainly the right answer according to the One True Church Of
> Pythonic Orthodoxy And Theoretical Correctness.

Oh I'm sorry, did something I say suggest that the couple of examples I 
gave are the *only* acceptable uses? My apologies for not giving an 
exhaustive list of every possible use of lists and tuples, I'll be sure 
to punish myself severely for the lapse.


> But, let me give an
> alternative answer which works for The Unwashed Masses Who Live In The
> Trenches And Write Python Code For A Living:
> 
> Use a list when you need an ordered collection which is mutable (i.e.
> can be altered after being created).  Use a tuple when you need an
> immutable list (such as for a dictionary key).

I keep hearing about this last one, but I wonder... who *actually* does 
this? I've created many, many lists over the years -- lists of names, 
lists of phone numbers, lists of directory search paths, all sorts of 
things. I've never needed to use one as a dictionary key.

Under what sort of circumstances would somebody want to take a mutable 
list of data, say a list of email addresses, freeze it into a known 
state, and use that frozen state as a key in a dict? What would be the 
point? Even if there was some meaningful reason to look up "this list of 
12000 email addresses" as a single key, it is going to get out of sync 
with the actual mutable list.

Sure, I have built a collection of items as a list, because lists are 
mutable, then frozen it into a tuple, and *thrown the list away*, then 
used the tuple as a key. But that's not the same thing, the intent is 
different. In my case, the data was never intended to be a list, it was 
always intended to be a fixed record-like collection, the use of list was 
as a temporary data structure used for construction. A bit like the idiom 
of ''.join(some_list).

But I can't think of any meaningful, non-contrived example where I might 
want an actual mutable list of values as a dict key.


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


Re: Python Interview Questions

2012-11-18 Thread D'Arcy J.M. Cain
On 18 Nov 2012 16:50:52 GMT
Steven D'Aprano  wrote:
> On Sun, 18 Nov 2012 08:53:25 -0500, Roy Smith wrote:
>> > Use a list when you need an ordered collection which is mutable
> > (i.e. can be altered after being created).  Use a tuple when you
> > need an immutable list (such as for a dictionary key).
> 
> I keep hearing about this last one, but I wonder... who *actually*
> does this? I've created many, many lists over the years -- lists of
> names, lists of phone numbers, lists of directory search paths, all
> sorts of things. I've never needed to use one as a dictionary key.

Well, as long as *you* never needed it then...

CellBlock = 9 # There's a riot going on...
Cell = 17
Bunk = "top"

Prisoner = {(CellBlock, Cell, Bunk): "Bernie Madoff"}

-- 
D'Arcy J.M. Cain  |  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.
IM: [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-11-18 Thread Roy Smith
In article <[email protected]>,
 Steven D'Aprano  wrote:

> Oh I'm sorry, did something I say suggest that the couple of examples I 
> gave are the *only* acceptable uses? My apologies for not giving an 
> exhaustive list of every possible use of lists and tuples, I'll be sure 
> to punish myself severely for the lapse.

Hmmm.  I didn't mean any offense.  I was just pointing out that what's 
true in theory and what's true in practice aren't always the same.

> Under what sort of circumstances would somebody want to take a mutable 
> list of data, say a list of email addresses, freeze it into a known 
> state, and use that frozen state as a key in a dict?

I've got a script which trolls our log files looking for python stack 
dumps.  For each dump it finds, it computes a signature (basically, a 
call sequence which led to the exception) and uses this signature as a 
dictionary key.  Here's the relevant code (abstracted slightly for 
readability):

def main(args):
crashes = {}
[...]
for line in open(log_file):
if does_not_look_like_a_stack_dump(line):
 continue
lines = traceback_helper.unfold(line)
header, stack = traceback_helper.extract_stack(lines)
signature = tuple(stack)
if signature in crashes:
count, header = crashes[signature]
crashes[signature] = (count + 1, header)
else:
crashes[signature] = (1, header)

You can find traceback_helper at 
https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/traceba
ck_helper.py

The stack that's returned is a list.  It's inherently a list, per the 
classic definition:

* It's variable length.  Different stacks have different depths.

* It's homogeneous.  There's nothing particularly significant about each 
entry other than it's the next one in the stack.

* It's mutable.  I can build it up one item at a time as I discover them.

* It's ordered.  f1(f2()) is not the same as f2(f1()).

But, to use it as a dictionary key, I need to make it into a tuple, 
since keys have to be immutable.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Point of idle curiosity

2012-11-18 Thread Terry Reedy

On 11/18/2012 6:45 AM, Chris Angelico wrote:

(if you'll forgive the pun)

Is IDLE named after Eric of that name, or is it pure coincidence?


Officially, I believe it is I(ntegrated) D(eve)L(opement) E(nvironment).
However, now that you mention it, I would not be surprised if Eric was 
in the back of however's mind.


--
Terry Jan Reedy

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


Spam source (Re: Horror Horror Horror!!!!!)

2012-11-18 Thread Terry Reedy

The question was raised as to how much spam comes from googlegroups.
Not all, but more that half, I believe. This one does.

From: MoneyMaker 
...
Message-ID: <[email protected]>

--
Terry Jan Reedy

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


Re: Python Interview Questions

2012-11-18 Thread Chris Angelico
On Mon, Nov 19, 2012 at 4:16 AM, D'Arcy J.M. Cain  wrote:
> On 18 Nov 2012 16:50:52 GMT
> Steven D'Aprano  wrote:
>> On Sun, 18 Nov 2012 08:53:25 -0500, Roy Smith wrote:
>>> > Use a list when you need an ordered collection which is mutable
>> > (i.e. can be altered after being created).  Use a tuple when you
>> > need an immutable list (such as for a dictionary key).
>>
>> I keep hearing about this last one, but I wonder... who *actually*
>> does this? I've created many, many lists over the years -- lists of
>> names, lists of phone numbers, lists of directory search paths, all
>> sorts of things. I've never needed to use one as a dictionary key.
>
> Well, as long as *you* never needed it then...
>
> CellBlock = 9 # There's a riot going on...
> Cell = 17
> Bunk = "top"
>
> Prisoner = {(CellBlock, Cell, Bunk): "Bernie Madoff"}

That's a structure, not a list. Every key will consist of precisely
three values: two integers and one keyword string. Already covered by
a previous example.

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


Re: how to simulate tar filename substitution across piped subprocess.Popen() calls?

2012-11-18 Thread jkn
Hi Hans

[...]
> 
> 
> However, once he does that, it's simpler to cut out xargs and invoke
> 
> "sh" directly.  Or even cut out "sh" and "test" and instead use
> 
> os.path.isfile and then call md5sum directly.  And once he does that,
> 
> he no longer needs to worry about single quotes.
> 

Yes indeed, using os.path.isfile() and them md5sum directly is my plan ... for 
reasons of maintainability (by myself) more than anything else.

> 
> 
> The OP has said, he's going to d all that.  One step at a time.
> 
> That sounds like a sensible plan to me.
> 

Thanks a lot.

J^n
-- 
http://mail.python.org/mailman/listinfo/python-list


Testers please for CodeInvestigator

2012-11-18 Thread martien . friedeman
I have released version 3 of CodeInvestigator.

It is a web-application that allows you to visualize the flow of data through 
your program.
It is intended as a way to easily learn about how your program does its thing.

Softoxi has done a review of the previous version:
http://www.softoxi.com/codeinvestigator.html

Version 3.0.0 can be downloaded from sourceforge:
http://sourceforge.net/project/showfiles.php?group_id=183942

I would be interested in bugs and things that you think should be included.
The 'wouldn't it be nice if ...' features.

Thank you!
Martien Friedeman
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-11-18 Thread Steven D'Aprano
On Sun, 18 Nov 2012 12:53:50 -0500, Roy Smith wrote:

> I've got a script which trolls our log files looking for python stack
> dumps.  For each dump it finds, it computes a signature (basically, a
> call sequence which led to the exception) and uses this signature as a
> dictionary key.  Here's the relevant code (abstracted slightly for
> readability):
> 
> def main(args):
> crashes = {}
> [...]
> for line in open(log_file):
> if does_not_look_like_a_stack_dump(line):
>  continue
> lines = traceback_helper.unfold(line)
> header, stack = traceback_helper.extract_stack(lines)
> signature = tuple(stack)
> if signature in crashes:
> count, header = crashes[signature]
> crashes[signature] = (count + 1, header)
> else:
> crashes[signature] = (1, header)
> 
> You can find traceback_helper at
> https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/
> traceback_helper.py
> 
> The stack that's returned is a list.  It's inherently a list, per the
> classic definition:

Er, no, it's inherently a blob of multiple text lines. Sure, you've built 
it a line at a time by using a list, but I've already covered that case. 
Once you've identified a stack, you never append to it, sort it, delete 
lines in the middle of it... none of these list operations are meaningful 
for a Python stack trace. The stack becomes a fixed string, and not just 
because you use it as a dict key, but because inherently it counts as a 
single, immutable blob of lines.

A tuple of individual lines is one reasonable data structure for a blob 
of lines. Another would be a single string:

signature = '\n'.join(stack)

Depending on what you plan to do with the signatures, one or the other 
implementation might be better. I'm sure that there are other data 
structures as well.


> * It's variable length.  Different stacks have different depths.

Once complete, the stack trace is fixed length, but that fixed length is 
different from one stack to the next. Deleting a line would make it 
incomplete, and adding a line would make it invalid.


> * It's homogeneous.  There's nothing particularly significant about each
> entry other than it's the next one in the stack.
> 
> * It's mutable.  I can build it up one item at a time as I discover
> them.

The complete stack trace is inhomogeneous and immutable. I've already 
covered immutability above: removing, adding or moving lines will 
invalidate the stack trace. Inhomogeneity comes from the structure of a 
stack trace. The mere fact that each line is a string does not mean that 
any two lines are equivalent. Different lines represent different things.

Traceback (most recent call last):
  File "./prattle.py", line 873, in select
selection = self.do_callback(cb, response)
  File "./prattle.py", line 787, in do_callback
raise callback
ValueError: what do you mean?

is a valid stack. But:

Traceback (most recent call last):
raise callback
selection = self.do_callback(cb, response)
  File "./prattle.py", line 787, in do_callback
ValueError: what do you mean?
  File "./prattle.py", line 873, in select

is not. A stack trace has structure. The equivalent here is the 
difference between:

ages = [23, 42, 19, 67,  # age, age, age, age
17, 94, 32, 51,  # ...
]

values = [23, 1972, 1, 34500,  # age, year, number of children, income
  35, 1985, 0, 67900,  # age, year, number of children, income
  ]

A stack trace is closer to the second example than the first: each item 
may be the same type, but the items don't represent the same *kind of 
thing*. 


You could make a stack trace homogeneous with a little work:

- drop the Traceback line and the final exception line;
- parse the File lines to extract the useful fields;
- combine them with the source code.

Now you have a blob of homogeneous records, here shown as lines of text 
with ! as field separator:

./prattle.py ! 873 ! select ! selection = self.do_callback(cb, response)
./prattle.py ! 787 ! do_callback ! raise callback

But there's really nothing you can do about the immutability. There isn't 
any meaningful reason why you might want to take a complete stack trace 
and add or delete lines from it.


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


Re: Unpaking Tuple

2012-11-18 Thread Robert Miles

On 10/9/2012 1:07 AM, Bob Martin wrote:

in 682592 20121008 232126 "Prasad, Ramit"  wrote:

Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
03:08:38PM +, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y=
ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D=
=0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E=
1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo=
re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail=
=3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A=

['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! =

Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b=
ar'?=0D=0A> =0D=0A> Regards,=0D=0A>Thomas=0D=0A> --=0D=0A=0D=0AI think yo=
u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea=
ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th=
e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2=
else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le=
n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e=
mail is confidential and subject to important disclaimers and=0D=0Aconditio=
ns including on offers for the purchase or sale of=0D=0Asecurities, accurac=
y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
an=2Ecom/pages/disclosures/email=2E


How does one unpack this post?  ;-)


There are a number of programs for converting ends of lines between
Linux format, Windows format, and Mac formats.  You could try running
all of those programs your operating system provides on that text,
then checking which one of them gives the most readable results.

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


Re: Spam source (Re: Horror Horror Horror!!!!!)

2012-11-18 Thread Robert Miles
On Sunday, November 18, 2012 1:35:00 PM UTC-6, Terry Reedy wrote:
> The question was raised as to how much spam comes from googlegroups.
> 
> Not all, but more that half, I believe. This one does.
> 
> 
> 
> From: MoneyMaker 
> 
> ...
> 
> Message-ID: <[email protected]>
> 
> 
> 
> -- 
> 
> Terry Jan Reedy

That depends on your definition of spam.  This one does not appear to be trying
to sell anything, and therefore does not meet some of the stricter definitions.
Definitely off-topic, though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Problems on these two questions

2012-11-18 Thread su29090

I all of the other problems but I have issues with these:

1.Given a positive integer  n , assign True to  is_prime if  n has no factors 
other than  1 and itself. (Remember,  m is a factor of  n if  m divides  n 
evenly.) 

2.An  arithmetic progression is a sequence of numbers in which the distance (or 
difference) between any two successive numbers if the same. This in the 
sequence  1, 3, 5, 7, ... , the distance is 2 while in the sequence  6, 12, 18, 
24, ... , the distance is 6. 

 Given the positive integer  distance and the positive integer  n , associate 
the variable  sum with the sum of the elements of the arithmetic progression 
from  1 to  n with distance  distance . For example, if  distance is 2 and  n 
is  10 , then  sum would be associated with  26 because  1+3+5+7+9 = 25 . 

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


Re: Unpaking Tuple

2012-11-18 Thread Hans Mulder
On 9/10/12 08:07:32, Bob Martin wrote:
> in 682592 20121008 232126 "Prasad, Ramit"  wrote:
>> Thomas Bach wrote:=0D=0A> Hi there,=0D=0A> =0D=0A> On Sat, Oct 06, 2012 at =
>> 03:08:38PM +, Steven D'Aprano wrote:=0D=0A> >=0D=0A> > my_tuple =3D my_=
>> tuple[:4]=0D=0A> > a,b,c,d =3D my_tuple if len(my_tuple) =3D=3D 4 else (my_=
>> tuple + (None,)*4)[:4]=0D=0A> >=0D=0A> =0D=0A> Are you sure this works as y=
>> ou expect? I just stumbled over the following:=0D=0A> =0D=0A> $ python=0D=
>> =0A> Python 3=2E2=2E3 (default, Jun 25 2012, 23:10:56)=0D=0A> [GCC 4=2E7=2E=
>> 1] on linux2=0D=0A> Type "help", "copyright", "credits" or "license" for mo=
>> re information=2E=0D=0A> >>> split =3D ['foo', 'bar']=0D=0A> >>> head, tail=
>> =3D split if len(split) =3D=3D 2 else split[0], None=0D=0A> >>> head=0D=0A=
>>> ['foo', 'bar']=0D=0A> >>> tail=0D=0A> >>>=0D=0A> =0D=0A> I don't get it! =
>> Could someone help me, please? Why is head not 'foo'=0D=0A> and tail not 'b=
>> ar'?=0D=0A> =0D=0A> Regards,=0D=0A>  Thomas=0D=0A> --=0D=0A=0D=0AI think yo=
>> u just need to wrap the else in parenthesis so the=0D=0Aelse clause is trea=
>> ted as a tuple=2E Without the parenthesis =0D=0AI believe it is grouping th=
>> e code like this=2E=0D=0A=0D=0Ahead, tail =3D (split if len(split) =3D=3D 2=
>> else split[0] ), None=0D=0A=0D=0AYou want:=0D=0Ahead, tail =3D split if le=
>> n(split) =3D=3D 2 else (split[0], None )=0D=0A=0D=0A=0D=0ARamit=0D=0AThis e=
>> mail is confidential and subject to important disclaimers and=0D=0Aconditio=
>> ns including on offers for the purchase or sale of=0D=0Asecurities, accurac=
>> y and completeness of information, viruses,=0D=0Aconfidentiality, legal pri=
>> vilege, and legal entity disclaimers,=0D=0Aavailable at http://www=2Ejpmorg=
>> an=2Ecom/pages/disclosures/email=2E
> 
> How does one unpack this post?  ;-)

How about:

print re.sub('^>* ', '', this_post, flags=re.M).decode('quopri')


Hope this helps,

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


Re: Problems on these two questions

2012-11-18 Thread Mark Lawrence

On 19/11/2012 01:52, su29090 wrote:


I all of the other problems but I have issues with these:

1.Given a positive integer  n , assign True to  is_prime if  n has no factors 
other than  1 and itself. (Remember,  m is a factor of  n if  m divides  n 
evenly.)

2.An  arithmetic progression is a sequence of numbers in which the distance (or 
difference) between any two successive numbers if the same. This in the 
sequence  1, 3, 5, 7, ... , the distance is 2 while in the sequence  6, 12, 18, 
24, ... , the distance is 6.

  Given the positive integer  distance and the positive integer  n , associate 
the variable  sum with the sum of the elements of the arithmetic progression 
from  1 to  n with distance  distance . For example, if  distance is 2 and  n 
is  10 , then  sum would be associated with  26 because  1+3+5+7+9 = 25 .

Thanks in advance.



Please specify what programming language and OS you're using, what code 
you've tried so far, and what problems if any you're having.  If you're 
using Python please give us the version, if not please try another 
mailing list.


--
Cheers.

Mark Lawrence.

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


Re: Python Interview Questions

2012-11-18 Thread Roy Smith
In article <[email protected]>,
 Steven D'Aprano  wrote:


> > The stack that's returned is a list.  It's inherently a list, per the
> > classic definition:
> 
> Er, no, it's inherently a blob of multiple text lines.

No, it's a list that looks like (taken from the doc string of the code I 
referenced):

[('/usr/lib/.../base.py', 'get_response'),
 ('/home/songza/.../views.py', 'song_info'),
 ('/home/songza.../api.py', 'get_song'),
 ('/home/songza/.../api.py', 'api')]

[it doesn't really have ...'s in the paths; I just elided some text to 
make it easier to read]

> > * It's homogeneous.  There's nothing particularly significant about each
> > entry other than it's the next one in the stack.
> 
> The complete stack trace is inhomogeneous and immutable. I've already 
> covered immutability above: removing, adding or moving lines will 
> invalidate the stack trace. Inhomogeneity comes from the structure of a 
> stack trace. The mere fact that each line is a string does not mean that 
> any two lines are equivalent. Different lines represent different things.

No.  Each entry in the list represents a source file and a function 
name.  They're all the same "shape".  You could remove one or add 
another one, or shuffle the order, and you would have something which 
was syntactically correct and semantically meaningful (even if it didn't 
represent an actual code path.

> - drop the Traceback line and the final exception line;
> - parse the File lines to extract the useful fields;
> - combine them with the source code.

You mean, kind of like the code I cited does? :-)

I think we're going to have to let this be.  You obviously have your 
concept of what a tuple is and what a list is.  I disagree.  I don't 
think either of us is right or wrong, we just have different ways of 
thinking about things.

You come at it from a theoretical point of view.  You think of each type 
as an embodiment of certain concepts ("it represents a fixed-length 
heterogenous sequence").  Your thinking is driven by what each type was 
intended to be used for.

I come at it from a practical point of view.  To me, each type is a 
collection of methods.  I have certain operations I need to perform.  I 
pick the type which offers those operations.  If the set of operations I 
need to perform (in this case, {append, hash}) don't exist in a single 
type, I'm forced to use both types and convert from one to the other as 
needed.

The theorist understands that a chisel and a screwdriver were intended 
for different purposes, but the pragmatist gets the paint can open.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-11-18 Thread Chris Angelico
On Mon, Nov 19, 2012 at 1:09 PM, Roy Smith  wrote:
> The theorist understands that a chisel and a screwdriver were intended
> for different purposes, but the pragmatist gets the paint can open.

A good tool can always be used in ways its inventor never intended -
and it will function as its user expects.

$ some_program | egrep --color=always '(ERROR|^)'

will highlight the word ERROR in red anywhere it appears in the
program's output, while maintaining all other lines without color. Not
normal use of grep, to be sure, but quite functional.

A tuple may have been intended to be a record, a struct, whatever, but
it is what it is, and I'll use one any time it's the best tool for the
job. Maybe its immutability is critical; or maybe it's just the most
convenient syntax and all I care about is that it be iterable.

But when I'm explaining grep to someone, I'll describe it as a filter
that keeps only some lines from the original, and when I describe a
tuple, I'll point out that it's immutable and (potentially) hashable.
The obvious first, the unobvious later.

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


Re: Spam source (Re: Horror Horror Horror!!!!!)

2012-11-18 Thread Mark Lawrence

On 18/11/2012 19:31, Terry Reedy wrote:

The question was raised as to how much spam comes from googlegroups.



I don't know the answer but I take the greatest pleasure in hurtling 
onto the dread googlegroups and gmane to report spam. Thankfully it's 
easy as the amount I receive via gmane is effectively zero.  YMMV?


--
Cheers.

Mark Lawrence.

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


Re: Problems on these two questions

2012-11-18 Thread Dave Angel
On 11/18/2012 08:52 PM, su29090 wrote:
> I all of the other problems but I have issues with these:
>
> 1.Given a positive integer  n , assign True to  is_prime if  n has no factors 
> other than  1 and itself. (Remember,  m is a factor of  n if  m divides  n 
> evenly.) 

if is_a_prime(n):
is_prime = True

Now all you have to do is write is_a_prime().  if you get stuck, please
show us what you've got, and what the problem is with it.  And as usual,
tell us what version of Python you're writing in, if any.

> 2.An  arithmetic progression is a sequence of numbers in which the distance 
> (or difference) between any two successive numbers if the same. This in the 
> sequence  1, 3, 5, 7, ... , the distance is 2 while in the sequence  6, 12, 
> 18, 24, ... , the distance is 6. 
>
>  Given the positive integer  distance and the positive integer  n , associate 
> the variable  sum with the sum of the elements of the arithmetic progression 
> from  1 to  n with distance  distance . For example, if  distance is 2 and  n 
> is  10 , then  sum would be associated with  26 because  1+3+5+7+9 = 25 . 

Don't call it 'sum' since that's a built-in function.  Coincidentally,
it's a function that takes an iterable, and calculates the sum of its
elements.  Sounds useful, no?  The other thing you might want is xrange,
which takes a start value, and end value, and a distance value.



>
> Thanks in advance.

You never responded to any of the messages in the other thread.  But
Chris's advice was good, and better worded than mine.  Pick a problem,
make an attempt, then ask for help.



-- 

DaveA

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


Re: Problems on these two questions

2012-11-18 Thread su29090
On Sunday, November 18, 2012 8:52:35 PM UTC-5, su29090 wrote:
> I did all of the other problems but I have issues with these:
> 
> 
> 
> 1.Given a positive integer  n , assign True to  is_prime if  n has no factors 
> other than  1 and itself. (Remember,  m is a factor of  n if  m divides  n 
> evenly.) 
> 
> 
> 
> 2.An  arithmetic progression is a sequence of numbers in which the distance 
> (or difference) between any two successive numbers if the same. This in the 
> sequence  1, 3, 5, 7, ... , the distance is 2 while in the sequence  6, 12, 
> 18, 24, ... , the distance is 6. 
> 
> 
> 
>  Given the positive integer  distance and the positive integer  n , associate 
> the variable  sum with the sum of the elements of the arithmetic progression 
> from  1 to  n with distance  distance . For example, if  distance is 2 and  n 
> is  10 , then  sum would be associated with  26 because  1+3+5+7+9 = 25 . 
> 
> 
> 
> Thanks in advance.

I'm using Python 3.2
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Problems on these two questions

2012-11-18 Thread Chris Angelico
On Mon, Nov 19, 2012 at 12:52 PM, su29090 <[email protected]> wrote:
> 1.Given a positive integer  n , assign True to  is_prime if  n has no factors 
> other than  1 and itself. (Remember,  m is a factor of  n if  m divides  n 
> evenly.)
>
> 2.An  arithmetic progression is a sequence of numbers in which the distance 
> (or difference) between any two successive numbers if the same. This in the 
> sequence  1, 3, 5, 7, ... , the distance is 2 while in the sequence  6, 12, 
> 18, 24, ... , the distance is 6.

Each of these problems is in two halves:

a) Understanding the mathematics behind the question
b) Writing the code.

Which half are you halfing (oops sorry, *having*) trouble with? If
(a), this isn't a programming question at all - search the web for
information on the problem, as these are well-known challenges. If
(b), you'll need to post your code to get any sort of useful help - we
aren't mindreaders, though we do try to look that way sometimes!

Either way, check this out:
http://www.catb.org/esr/faqs/smart-questions.html#homework

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


Re: Problems on these two questions

2012-11-18 Thread Dave Angel
On 11/18/2012 09:15 PM, su29090 wrote:
> 
> I'm using Python 3.2 

In Python 3.2, xrange has been renamed to range.  So have you tried to
solve problem #2 yet?



-- 

DaveA

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


Re: Spam source (Re: Horror Horror Horror!!!!!)

2012-11-18 Thread Gene Heskett
On Sunday 18 November 2012 21:18:16 Robert Miles did opine:

> On Sunday, November 18, 2012 1:35:00 PM UTC-6, Terry Reedy wrote:
> > The question was raised as to how much spam comes from googlegroups.
> > 
> > Not all, but more that half, I believe. This one does.
> > 
> > 
> > 
> > From: MoneyMaker 
> > 
> > ...
> > 
> > Message-ID: <[email protected]>
> 
> That depends on your definition of spam.  This one does not appear to be
> trying to sell anything, and therefore does not meet some of the
> stricter definitions. Definitely off-topic, though.

Not withstanding, it was fed to sa-learn -spam here.

Cheers, Gene
-- 
"There are four boxes to be used in defense of liberty:
 soap, ballot, jury, and ammo. Please use in that order."
-Ed Howdershelt (Author)
My web page:  is up!
Only God can make random selections.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-11-18 Thread Mark Lawrence

On 19/11/2012 02:09, Roy Smith wrote:


The theorist understands that a chisel and a screwdriver were intended
for different purposes, but the pragmatist gets the paint can open.



To throw a chiseldriver into the works, IIRC a tuple is way faster to 
create but accessing a list is much faster.  The obvious snag is that 
may have been Python 2.7 whereas 3.3 is completely different.  Sorry but 
I'm currently wearing my XXXL size Lazy Bone Idle Hat so have no figures 
to back my probably incorrect memory up, anyone know anything about this?


--
Cheers.

Mark Lawrence.

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


Getting a seeded value from a list

2012-11-18 Thread Graham Fielding




Hello!  Clueless noob again! :) This time around, I'm trying to figure out the 
random.seed() function -- specifically, I'd like to retrieve the seeded values 
from a list (which I've called levelSeed), and use them in a random-dungeon 
generator.  The numbers are generating and storing properly, but I can't find 
any examples online that aren't specific to a particular purpose (such as 
mathematics, arrays, and the like). Would I use 'getstate' when the dungeon is 
generated, or is there another method that would be better suited to the task?  
 -- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on Socket Timeouts

2012-11-18 Thread Cameron Simpson
On 18Nov2012 03:27, Abhijeet Mahagaonkar  wrote:
| I'm new to network programming.
| I have a question.
| 
| Can we set a timeout to limit how long a particular socket can read or
| write?

On the socket itself? Probably not. But...

| I have used a settimeout() function.
| The settimeout() works fine as long as the client doesnt send any data for
| x seconds.
| The data that I receive in the server after accept()ing a connect() from a
| client I check if the client is sending any invalid data.
| I'm trying to ensure that a client sending invalid data constantly cannot
| hold the server. So is there a way of saying I want the client to use this
| socket for x seconds before I close it, no matter what data I receive?

Not the time you set up the socket, or when you accept the client's
connection. Thereafter, ever time you get some data, look at the clock.
If enough time has elapsed, close the socket yourself.

So, not via an interface to the socket but as logic in your own code.

Cheers,
-- 
Cameron Simpson 

Their are thre mistakes in this sentence.
- Rob Ray DoD#3 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on Socket Timeouts

2012-11-18 Thread Abhijeet Mahagaonkar
I also tried looking at SO_RCVTIMEO option.
Turns out that also resets if data is received.

And yeah I implemented that as a separate logic in my code.
I was wondering if sockets natively provided this functionality.

Thanks again for clarifying.

Cheers,
Abhijeet


On Mon, Nov 19, 2012 at 12:40 AM, Cameron Simpson  wrote:

> On 18Nov2012 03:27, Abhijeet Mahagaonkar  wrote:
> | I'm new to network programming.
> | I have a question.
> |
> | Can we set a timeout to limit how long a particular socket can read or
> | write?
>
> On the socket itself? Probably not. But...
>
> | I have used a settimeout() function.
> | The settimeout() works fine as long as the client doesnt send any data
> for
> | x seconds.
> | The data that I receive in the server after accept()ing a connect() from
> a
> | client I check if the client is sending any invalid data.
> | I'm trying to ensure that a client sending invalid data constantly cannot
> | hold the server. So is there a way of saying I want the client to use
> this
> | socket for x seconds before I close it, no matter what data I receive?
>
> Not the time you set up the socket, or when you accept the client's
> connection. Thereafter, ever time you get some data, look at the clock.
> If enough time has elapsed, close the socket yourself.
>
> So, not via an interface to the socket but as logic in your own code.
>
> Cheers,
> --
> Cameron Simpson 
>
> Their are thre mistakes in this sentence.
> - Rob Ray DoD#3 
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Yet another Python textbook

2012-11-18 Thread Pavel Solin
I would like to introduce a new Python textbook
aimed at high school students:

http://femhub.com/textbook-python/.

The textbook is open source and its public Git
repository is located at Github:

[email protected]:femhub/nclab-textbook-python.git

Feedback and contributions are very much
welcome, every contributor becomes automatically
a co-author.

Best regards,

Pavel

-- 
Pavel Solin
Associate Professor
Applied and Computational Mathematics
University of Nevada, Reno
http://hpfem.org/~pavel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Question on Socket Timeouts

2012-11-18 Thread Cameron Simpson
On 19Nov2012 14:40, I wrote:
| Not the time you set up the socket, or when you accept the client's
| connection. Thereafter, ever time you get some data, look at the clock.
| If enough time has elapsed, close the socket yourself.

That would be "Note", not "Not". Sorry.
-- 
Cameron Simpson 

No keyboard present
Hit F1 to continue
Zen engineering?
- Haiku Error Messages 
http://www.salonmagazine.com/21st/chal/1998/02/10chal2.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-11-18 Thread Ian Kelly
On Sun, Nov 18, 2012 at 7:42 PM, Mark Lawrence  wrote:
> To throw a chiseldriver into the works, IIRC a tuple is way faster to create
> but accessing a list is much faster.  The obvious snag is that may have been
> Python 2.7 whereas 3.3 is completely different.  Sorry but I'm currently
> wearing my XXXL size Lazy Bone Idle Hat so have no figures to back my
> probably incorrect memory up, anyone know anything about this?

It's not been my experience with Python 2.7 that list access is faster
than tuple access.  Tuples are as fast as or faster than lists, pretty
much universally.  They seem to have closed the gap a bit in
Python 3.3, though, as the following timings show.  For one-shot
construction, tuples seem to be more efficient for short sequences,
but then lists win for longer sequences, although not by much.  Of
course, lists are always going to be much slower if you build them up
with appends and extends.

C:\>python -m timeit -s "x = range(10)" "tuple(x)"
100 loops, best of 3: 0.773 usec per loop

C:\>python -m timeit -s "x = range(10)" "list(x)"
100 loops, best of 3: 0.879 usec per loop

C:\>python -m timeit -s "x = range(100)" "tuple(x)"
10 loops, best of 3: 2.88 usec per loop

C:\>python -m timeit -s "x = range(100)" "list(x)"
10 loops, best of 3: 2.63 usec per loop

C:\>python -m timeit -s "x = range(1000)" "tuple(x)"
1 loops, best of 3: 37.4 usec per loop

C:\>python -m timeit -s "x = range(1000)" "list(x)"
1 loops, best of 3: 36.2 usec per loop

C:\>python -m timeit -s "x = range(1)" "tuple(x)"
1000 loops, best of 3: 418 usec per loop

C:\>python -m timeit -s "x = range(1)" "list(x)"
1000 loops, best of 3: 410 usec per loop


For iteration, tuples are consistently 7-8% faster.


C:\>python -m timeit -s "x = tuple(range(10))" "for i in x: pass"
100 loops, best of 3: 0.467 usec per loop

C:\>python -m timeit -s "x = list(range(10))" "for i in x: pass"
100 loops, best of 3: 0.498 usec per loop

C:\>python -m timeit -s "x = tuple(range(100))" "for i in x: pass"
10 loops, best of 3: 3.31 usec per loop

C:\>python -m timeit -s "x = list(range(100))" "for i in x: pass"
10 loops, best of 3: 3.56 usec per loop

C:\>python -m timeit -s "x = tuple(range(1000))" "for i in x: pass"
1 loops, best of 3: 31.6 usec per loop

C:\>python -m timeit -s "x = list(range(1000))" "for i in x: pass"
1 loops, best of 3: 34.3 usec per loop

C:\>python -m timeit -s "x = tuple(range(1))" "for i in x: pass"
1000 loops, best of 3: 318 usec per loop

C:\>python -m timeit -s "x = list(range(1))" "for i in x: pass"
1000 loops, best of 3: 341 usec per loop


For direct item access, tuples seem to be about 2-3% faster.


C:\>python -m timeit -s "import operator as o; x = tuple(range(10)); g
= o.itemgetter(*range(len(x)))" "g(x)"
100 loops, best of 3: 0.67 usec per loop

C:\>python -m timeit -s "import operator as o; x = list(range(10)); g
= o.itemgetter(*range(len(x)))" "g(x)"
100 loops, best of 3: 0.674 usec per loop

C:\>python -m timeit -s "import operator as o; x = tuple(range(100));
g = o.itemgetter(*range(len(x)))" "g(x)"
10 loops, best of 3: 4.52 usec per loop

C:\>python -m timeit -s "import operator as o; x = list(range(100)); g
= o.itemgetter(*range(len(x)))" "g(x)"
10 loops, best of 3: 4.65 usec per loop

C:\>python -m timeit -s "import operator as o; x = tuple(range(1000));
g = o.itemgetter(*range(len(x)))" "g(x)"
1 loops, best of 3: 43.2 usec per loop

C:\>python -m timeit -s "import operator as o; x = list(range(1000));
g = o.itemgetter(*range(len(x)))" "g(x)"
1 loops, best of 3: 43.7 usec per loop

C:\>python -m timeit -s "import operator as o; x =
tuple(range(1)); g = o.itemgetter(*range(len(x)))" "g(x)"
1000 loops, best of 3: 422 usec per loop

C:\>python -m timeit -s "import operator as o; x = list(range(1));
g = o.itemgetter(*range(len(x)))" "g(x)"
1000 loops, best of 3: 447 usec per loop
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-11-18 Thread Steven D'Aprano
On Sun, 18 Nov 2012 21:09:36 -0500, Roy Smith wrote:

> In article <[email protected]>,
>  Steven D'Aprano  wrote:
> 
> 
>> > The stack that's returned is a list.  It's inherently a list, per the
>> > classic definition:
>> 
>> Er, no, it's inherently a blob of multiple text lines.
> 
> No, it's a list that looks like (taken from the doc string of the code I
> referenced):
> 
> [('/usr/lib/.../base.py', 'get_response'),
>  ('/home/songza/.../views.py', 'song_info'),
>  ('/home/songza.../api.py', 'get_song'), 
>  ('/home/songza/.../api.py', 'api')]
> 
> [it doesn't really have ...'s in the paths; I just elided some text to
> make it easier to read]

I see. It wasn't clear from your earlier description that the items had 
been post-processed from collections of raw log lines to fixed records. 
But it doesn't actually change my analysis any. See below.

By the way, based on the sample data you show, your script is possibly 
broken. You don't record either the line number that raises, or the 
exception raised, so your script doesn't differentiate between different 
errors that happen to occur with similar stack traces. (I say "possibly" 
broken because I don't know what your requirements are. Maybe your 
requirements are sufficiently wide that you don't care that distinct 
failures are counted together.)

E.g. these three stack traces will probably generate the same fixed 
record, even though the errors are distinct:

#1
Traceback (most recent call last):
  File "./spam.py", line 20, in select
selection = func(a, b)
  File "./spam.py", line 60, in func
return 1/x
ZeroDivisionError: float division


#2
Traceback (most recent call last):
  File "./spam.py", line 20, in select
selection = func(a, b)
  File "./spam.py", line 60, in func
return 1/x
TypeError: unsupported operand type(s) for /: 'int' and 'NoneType'


#3
Traceback (most recent call last):
  File "./spam.py", line 20, in select
selection = func(a, b)
  File "./spam.py", line 55, in func
y = 1/(a + b)
ZeroDivisionError: float division


Maybe that's okay for your application, but it strikes me as odd that you 
do distinguish *some* distinct errors in the same function, but not 
others.



>> > * It's homogeneous.  There's nothing particularly significant about
>> > each entry other than it's the next one in the stack.
>> 
>> The complete stack trace is inhomogeneous and immutable. I've already
>> covered immutability above: removing, adding or moving lines will
>> invalidate the stack trace. Inhomogeneity comes from the structure of a
>> stack trace. The mere fact that each line is a string does not mean
>> that any two lines are equivalent. Different lines represent different
>> things.
> 
> No.  Each entry in the list represents a source file and a function
> name.  They're all the same "shape".  You could remove one or add
> another one, or shuffle the order, and you would have something which
> was syntactically correct and semantically meaningful (even if it didn't
> represent an actual code path.

If you remove/add/shuffle lines in the stack, you no longer have the same 
stack. Take the example you gave before:

stack1 = [('/usr/lib/.../base.py', 'get_response'),
  ('/home/songza/.../views.py', 'song_info'),
  ('/home/songza.../api.py', 'get_song'), 
  ('/home/songza/.../api.py', 'api')
  ]

Here's a different stack trace, representing a different code path, which 
as you say is syntactically correct and semantically meaningful:

stack2 = [('/home/songza/.../api.py', 'api'),
  ('/home/songza.../api.py', 'get_song'),
  ('/home/songza/.../views.py', 'song_info'),
  ('/usr/lib/.../base.py', 'get_response')
  ]

Since they are different stacks, they are treated as different keys:

data = {stack1: 11, stack2: 22}

Do you agree that this is what your application expects? Different stack 
traces are different keys, associated with different values.

I claim this only makes sense if you treat the stacks as inherently 
immutable. Never mind Python's limitation. Let's pretend we were running 
this code under some other language, NeoPython, which allowed mutable 
keys.

You claim that stacks are *inherently mutable*. So I should be able to do 
this:

stack1.sort()  # it's the *same stack*, all I've done is mutate it
print data[stack1]

and expect to see "11" printed. I am looking at the same key, right? So I 
certainly don't expect to see the value associated with a completely 
different key.

But wait a minute... after sorting, stack1 and stack2 now are equal. I 
could just as easily expect to see "22" printed.

I thought we had just agreed that stack1 and stack2 are *different* keys. 
Of course they are different. They represent different code paths. But 
after sorting stack1, it looks exactly like stack2. It looks like a 
different code path. It *lies* -- it no longer represents the code path 
that it actually represen