Re: Python Interview Questions

2012-11-19 Thread Terry Reedy

On 11/19/2012 1:01 AM, Ian Kelly wrote:


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.


Interesting results. But what system (hardware, os). These sorts of 
times tend to vary with the system.


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




--
Terry Jan Reedy

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


Re: Point of idle curiosity

2012-11-19 Thread Ulrich Eckhardt

Am 18.11.2012 12:45, schrieb Chris Angelico:

(if you'll forgive the pun)


Nevarr!



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


Maybe. Interestingly, there is also 
http://eric-ide.python-projects.org/, just to add some more unfounded 
conspiracy theories to this discussion. :P


Uli


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


"Premature end of script headers: wsgihandler.py" on usage of BytesIO()

2012-11-19 Thread Yasir Saleem
Hi all,

I am generating graphs using "cairo plot" in web2py project. Here I am using 
BytesIO() stream for generating graphs. Everything runs fine when I run on 
localhost but when I deploy it on apache server and then run from different 
machines OR from different browsers in same machine then the server becomes 
halt and in apache error log, I found this error message:
Premature end of script headers: wsgihandler.py,

Furthermore, it occurs only when I use BytesIO() stream for generating graphs. 
In all other cases, it run smoothly.

Please guide me that how I should resolve this issue. Furthermore, please also 
guide, if I can use any stream other BytesIO()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error importing smtplib

2012-11-19 Thread Eric Frederich
I can do this in stand alone programs because my code does the import and
calls the login function so I can control the order of things.
Unfortunately stand alone programs are not the only ways in which I am
using these Python bindings.

You can customize and extend this 3rd party application at various
extension points all of which are invoked after login.
We have a lot of extensions written in Python.

I guess I will have to back to the BAR vendor and ask if it is okay to
remove their old .so file.
Perhaps their code will just work with the newer 0.9.8e or perhaps they'll
have to relink or recompile.

On Fri, Nov 16, 2012 at 5:00 PM, Terry Reedy  wrote:

> [easy] Do the import before the function call, which is the proper order
> and the one that works.
>
> --
> Terry Jan Reedy
>
> --
> http://mail.python.org/**mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

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

> 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.

Well, I did provide the code that does this.
 
> 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. 

You really might want to read the code I provided.  Here's the reference 
again:

https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/traceba
ck_helper.py

The "header" referred to does indeed contain the exception raised.  And 
the line numbers are included.  Here's a typical output stanza:

2012-11-19T00:00:15+00:00 web5 ˇ˛2012-11-19 00:00:15,831 [2712]: 
songza-api IGPhwNU2SJ691cx8 4C0ABFA9-50A974E7-384995 W6D-HSO 
173.145.137.54 songza.django.middleware ERROR process_exception() Path = 
u'/api/1/station/1459775/next', Exception = 
ValueError(u">: no song ids for mp3",)
/home/songza/env/python/local/lib/python2.7/site-packages/django/core/han
dlers/base.py:111:get_response()
/home/songza/deploy/current/pyza/djapi/decorators.py:11:_wrapped_view_fun
c()
/home/songza/env/python/local/lib/python2.7/site-packages/django/views/de
corators/http.py:45:inner()
/home/songza/deploy/current/pyza/djapi/views.py:1659:station_next()
/home/songza/deploy/current/pyza/models/station.py:660:next_song()
/home/songza/deploy/current/pyza/lib/song_picker.py:327:pick()

> I say "possibly" broken because I don't know what your requirements are.

Our requirements are to scan the logs of a production site and filter 
down the gobs and gobs of output (we produced 70 GB of log files 
yesterday) into something small enough that a human can see what the 
most common failures were.  The tool I wrote does that.

The rest of this conversation is just silly.  It's turning into getting 
hit on the head lessons.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python questions help

2012-11-19 Thread Neil Cerutti
On 2012-11-16, Chris Angelico  wrote:
> On Sat, Nov 17, 2012 at 5:00 AM, rh
>  wrote:
>> "How many people think programming skills are inherent?" i.e.
>> that some people are just born with the gift to be good
>> programmers Result: very few hands raised maybe a couple
>> (possibly non-progammers??)
>
> Maybe, but there's definitely something that happens close to
> birth. If your parents give you the name Chris, you're more
> likely to become a geek and a programmer.

There are people with rare talent who can program in a way that
most others can't, .e.g, Chris Sawyer. But, as Louis Moyse, a
great musician remarked: "Without hard work, talent means
nothing."

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


Re: Python Interview Questions

2012-11-19 Thread Roy Smith
OK, I've just read back over the whole thread.  I'm really struggling to 
understand what point you're trying to make.  I started out by saying:

> 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).

To which you obviously objected.  So now you write:

> I think a tuple is an immutable sequence of items, and a list is a 
> mutable sequence of items.

So how is that different from what I said?  Is this whole argument 
boiling down to your use of "immutable sequence" vs. my use of 
"immutable list"?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: "Premature end of script headers: wsgihandler.py" on usage of BytesIO()

2012-11-19 Thread Hans Mulder
On 19/11/12 14:29:13, Yasir Saleem wrote:
> Hi all,
> 
> I am generating graphs using "cairo plot" in web2py project.
> Here I am using BytesIO() stream for generating graphs.
> Everything runs fine when I run on localhost but when I deploy
> it on apache server and then run from different machines OR
> from different browsers in same machine then the server
> becomes halt and in apache error log, I found this error
> message:
>
> Premature end of script headers: wsgihandler.py,
> 
> Furthermore, it occurs only when I use BytesIO() stream for
> generating graphs. In all other cases, it run smoothly.
> 
> Please guide me that how I should resolve this issue.

One technique is to put at the very top of the script, even
above the import statements:

print "Content-Type: text/plain\n\n"

This will cause all text output by your script to be displayed
in your browser as plain text [1].  If you inspect it, you'll
probably find some kind of warning displayed above the HTML
headers.  You'll need to find a way to not receive that warning.

It's usually best if you can actually solve the issue Python is
warning about.  it that's not possible, suppressing the warning
may be your only alternative.

If you can't figure out what the message means, and Google
doesn't know either, you can post it in this forum and ask
for further guidance.

[1] Except if you use Internet Explorer, which will ask you
whether you want to save the document.  You can either do that
and view the content with another application, or use another
browser, or change the content-type to text/html.  If you do
the latter, IE will notice that the content is really plain
text, and that it is actually quite capable of displaying that.


Hope this helps,

-- HansM

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


Paid Python work for 30mins - 1 hour

2012-11-19 Thread blockeduser
I have three scripts that I would like written, they are designed to do the 
following:

Backup.py – Zip a folder and store it on amazon S3 using BOTO with the date and 
time as the folder name.

Restore.py – Grab a file from S3 and download it and then unzip it in the right 
location with two commandline parameters (1 = Get most recent, 2 = Get specific 
file)

Import.py – Check that I have done this correctly and add command line 
parameter for changing the command

This code is probably 50% completed already and if someone knows what they are 
doing, could be completed in a very short time. It is really basic Python code, 
I just dont know python myself.

If you are interested get in touch!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Paid Python work for 30mins - 1 hour

2012-11-19 Thread blockeduser
Forgot to say, my email is [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

2012-11-19 Thread Ian Kelly
On Mon, Nov 19, 2012 at 7:30 AM, Roy Smith  wrote:
> In article <[email protected]>,
>  Steven D'Aprano  wrote:
>>
>> 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.
>
> You really might want to read the code I provided.  Here's the reference
> again:
>
> https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/traceba
> ck_helper.py
>
> The "header" referred to does indeed contain the exception raised.  And
> the line numbers are included.  Here's a typical output stanza:

Yes, but the dict is still keyed on the traceback alone, and only the
first header for a particular traceback is stored.  If two different
exceptions occur at the same line of code and sharing the same
traceback, the second exception would be counted as a second
occurrence of the first, effectively squashing any reporting of the
second exception.
-- 
http://mail.python.org/mailman/listinfo/python-list


Linux compatibility

2012-11-19 Thread EDI Support
Hi All,

I would like know if Python  2.4.3 will be compatible with Linux RHEL 5.5 or 
6.1?

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


Re: Yet another Python textbook

2012-11-19 Thread Kwpolska
On Mon, Nov 19, 2012 at 6:30 AM, Pavel Solin  wrote:
> 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

URL for humans: https://github.com/femhub/nclab-textbook-python

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

You are writing it for something called “NCLab”, not for the general
public, and that sucks.

1. please use math-compatible fonts in LaTeX, like Computer Modern,
Latin Modern, Lucida Bright or MathTime (I suggest one of the first
two).  This way, stuff in the math environment won’t look differently
than the text.  Currently, your text is in a fancy font and maths are
in Computer Modern.
2. IMO, you should be doing a bit more general usage programming, not
science-specific.
3. Code highlighting for inline code and other languages, please.

This are just my basic thoughts from looking through it.

-- 
Kwpolska 
stop html mail  | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Paid Python work for 30mins - 1 hour

2012-11-19 Thread Paul Rubin
[email protected] writes:
> I have three scripts that I would like written, they are designed to
> do the following:
> Backup.py – Zip a folder and store it on amazon S3 ...
> If you are interested get in touch!

You could just type "python s3 upload" into web search and see if you
can use the stuff that turns up.

More realistically I'm not sure it makes sense to set up a 30 minute / 1
hour development gig, especially without a completely precise
specification up front.  There will have to be some back-and-forth about
the user interface, the script will have to be tested, the developer
will need access to an S3 account if s/he doesn't have it already, etc.
So it's likely to end up taking longer than 1 hour even though the task
is pretty simple.  But, maybe someone will take you up on it, and if it
turns out to be more trouble than it was worth, at least they learned
something.

If you already have 50% of the code written, what's preventing you from
doing the other 50% yourself?  If you're hitting snags, maybe you could
post what you have so far, and ask for advice.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Paid Python work for 30mins - 1 hour

2012-11-19 Thread Mark Adam
On Mon, Nov 19, 2012 at 10:14 AM,   wrote:
> I have three scripts that I would like written, they are designed to do the 
> following:
>
> Backup.py – Zip a folder and store it on amazon S3 using BOTO with the date 
> and time as the folder name.
>
> Restore.py – Grab a file from S3 and download it and then unzip it in the 
> right location with two commandline parameters (1 = Get most recent, 2 = Get 
> specific file)
>
> Import.py – Check that I have done this correctly and add command line 
> parameter for changing the command
>
> This code is probably 50% completed already and if someone knows what they 
> are doing, could be completed in a very short time. It is really basic Python 
> code, I just dont know python myself.
>
> If you are interested get in touch!

You might consider putting your request on  elance.com or guru.com
where you can hire programmers for small projects like this.

Good luck,

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


Re: Paid Python work for 30mins - 1 hour

2012-11-19 Thread Paul Rubin
Mark Adam  writes:
> You might consider putting your request on  elance.com or guru.com
> where you can hire programmers for small projects like this.

Even though the coding task is very small, I think it's unrealistic to
scope it at less than 2-4 hours, given communication overhead etc.  It
would be quicker if it were done in person.
-- 
http://mail.python.org/mailman/listinfo/python-list


Robust regex

2012-11-19 Thread Joseph L. Casale
Trying to robustly parse a string that will have key/value pairs separated
by three pipes, where each additional key/value (if more than one exists)
will be delineated by four more pipes.

string = 'key_1|||value_1key_2|||value_2'
regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?'

I am not convinced this is the most effective or safest, any opinions would
be greatly appreciated!

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


Re: Python Interview Questions

2012-11-19 Thread Terry Reedy

On 11/19/2012 9:30 AM, Roy Smith wrote:


Our requirements are to scan the logs of a production site and filter
down the gobs and gobs of output (we produced 70 GB of log files
yesterday) into something small enough that a human can see what the
most common failures were.  The tool I wrote does that.

The rest of this conversation is just silly.  It's turning into getting
hit on the head lessons.


I agree. In early Python, tuples were more different from lists than 
they are today. They did not have any (public) methods. Today, they have 
.index and .count methods, which make little sense from the 'tuple is a 
record' viewpoint. The addition of those methods redefined tuples as 
read-only (and therefore hashable) sequences.


From the collections.abc doc
'''
Sequence | Sized, Iterable, Container |
 __getitem__ __contains__, __iter__, __reversed__, index, and count
...
class collections.abc.Sequence
class collections.abc.MutableSequence
ABCs for read-only and mutable sequences.
'''
>>> from collections.abc import Sequence
>>> issubclass(tuple, Sequence)
True

--
Terry Jan Reedy

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


Re: Robust regex

2012-11-19 Thread Chris Angelico
On Tue, Nov 20, 2012 at 7:32 AM, Joseph L. Casale
 wrote:
> Trying to robustly parse a string that will have key/value pairs separated
> by three pipes, where each additional key/value (if more than one exists)
> will be delineated by four more pipes.
>
> string = 'key_1|||value_1key_2|||value_2'
> regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?'
>
> I am not convinced this is the most effective or safest, any opinions would
> be greatly appreciated!

Is regex a requirement? Since you posted this on python-list, I'm
going to assume you're working in Python.

string = 'key_1|||value_1key_2|||value_2'
content = dict(map(lambda x: x.split("|||"),string.split("")))

--> {'key_1': 'value_1', 'key_2': 'value_2'}

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


Re: Robust regex

2012-11-19 Thread Benjamin Kaplan
On Nov 19, 2012 12:37 PM, "Joseph L. Casale" 
wrote:
>
> Trying to robustly parse a string that will have key/value pairs separated
> by three pipes, where each additional key/value (if more than one exists)
> will be delineated by four more pipes.
>
> string = 'key_1|||value_1key_2|||value_2'
> regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?'
>
> I am not convinced this is the most effective or safest, any opinions
would
> be greatly appreciated!
>
> jlc
> --
> http://mail.python.org/mailman/listinfo/python-list

Do you even need a regular expression for this? Just split on  and then
split those on |||.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Point of idle curiosity

2012-11-19 Thread Alister
On Sun, 18 Nov 2012 16:08:34 +, 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

i think it is fairly safe to assume that for most (if not all) good 
sounding acronyms the acronym was chosen before the meaning or at least 
chosen from short list of possible meanings because it looked good.



-- 
I THINK MAN INVENTED THE CAR by instinct.
-- Jack Handley, The New Mexican, 1988.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Robust regex

2012-11-19 Thread MRAB

On 2012-11-19 20:32, Joseph L. Casale wrote:

Trying to robustly parse a string that will have key/value pairs separated
by three pipes, where each additional key/value (if more than one exists)
will be delineated by four more pipes.

 string = 'key_1|||value_1key_2|||value_2'
 regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?'

I am not convinced this is the most effective or safest, any opinions would
be greatly appreciated!


Do you need to use regex?

It would be simpler to use the .split method:

for pair in string.split(""):
key, value = pair.split("|||")
print("key is {!r}, value is {!r}".format(key, value))

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


Re: Robust regex

2012-11-19 Thread John Gordon
In  "Joseph L. Casale" 
 writes:

> Trying to robustly parse a string that will have key/value pairs separated
> by three pipes, where each additional key/value (if more than one exists)
> will be delineated by four more pipes.
> string = 'key_1|||value_1key_2|||value_2'
> regex = '((?:(?!\|\|\|).)+)(?:\|\|\|)((?:(?!\|\|\|).)+)(?:\|\|\|\|)?'
> I am not convinced this is the most effective or safest, any opinions would
> be greatly appreciated!

Regexes may be overkill here.  A simple string split might be better:

string = 'key_1|||value_1key_2|||value_2'
pairs = string.split('')
for pair in pairs:
keyval = pair.split('|||')
print '%s=%s' % (keyval[0], keyval[1])

-- 
John Gordon   A is for Amy, who fell down the stairs
[email protected]  B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

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


Re: Point of idle curiosity

2012-11-19 Thread Chris Angelico
On Mon, Nov 19, 2012 at 11:38 PM, Ulrich Eckhardt
 wrote:
> Am 18.11.2012 12:45, schrieb Chris Angelico:
>> Is IDLE named after Eric of that name, or is it pure coincidence?
>
>
> Maybe. Interestingly, there is also http://eric-ide.python-projects.org/,
> just to add some more unfounded conspiracy theories to this discussion. :P

Wait, that's completely separate. I don't think Eric Idle was ever a
member of the Eric Conspiracy.

http://www.catb.org/~esr/ecsl/

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


Re: Python questions help

2012-11-19 Thread Chris Angelico
On Tue, Nov 20, 2012 at 1:57 AM, Neil Cerutti  wrote:
> On 2012-11-16, Chris Angelico  wrote:
>> On Sat, Nov 17, 2012 at 5:00 AM, rh
>>  wrote:
>>> "How many people think programming skills are inherent?" i.e.
>>> that some people are just born with the gift to be good
>>> programmers Result: very few hands raised maybe a couple
>>> (possibly non-progammers??)
>>
>> Maybe, but there's definitely something that happens close to
>> birth. If your parents give you the name Chris, you're more
>> likely to become a geek and a programmer.
>
> There are people with rare talent who can program in a way that
> most others can't, .e.g, Chris Sawyer. But, as Louis Moyse, a
> great musician remarked: "Without hard work, talent means
> nothing."

Sure, it definitely takes work. You still have to put in your ten
thousand hours. I don't know what the connection is, but there do seem
to be a LOT of geeky Chrises; in fact, in any mid-length thread here
on python-list, you could probably conclude with a "Thanks for the
tip, Chris, it works now!" without even bothering to read it.

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


RE: xml data or other?

2012-11-19 Thread Prasad, Ramit
Artie Ziff wrote:
> 
> 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 think your description text should be in a CDATA section.
http://en.wikipedia.org/wiki/CDATA#CDATA_sections_in_XML

~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Robust regex

2012-11-19 Thread Joseph L. Casale
> Regexes may be overkill here.  A simple string split might be better:

Yup, and much more robust as I was looking for.

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


re.search when used within an if/else fails

2012-11-19 Thread Kevin T
python version 2.4.3, yes i know that it is old.  getting the sysadmin to 
update the OS requires a first born.

with the following code..
for signal in register['signals'] :

351   sigName = signal['functionName']
352   if re.search( "rsrvd", sigName ) == None :
353  print sigName
354  newVal = "%s%s" % ( '1'*signal['bits'] , newVal ) 
#prepend 0's
355   if re.search( "rsrvd", sigName ) != None :
356  print sigName
357  newVal = "%s%s" % ( '0'*signal['bits'], newVal )

regardless of how i code line 352, i can not EVER use an else clause with it.  
if i use an else clause, the else will NEVER get executed...

has any one experienced anything like this behavior?  any suggestions?  the 
above code works but... why should i have to code it like this?

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


Re: Python Interview Questions

2012-11-19 Thread Steven D'Aprano
On Mon, 19 Nov 2012 09:30:54 -0500, Roy Smith wrote:

> In article <[email protected]>,
>  Steven D'Aprano  wrote:
> 
>> 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.
> 
> Well, I did provide the code that does this.

You did? When? [goes back and looks]

Oh, so you did. Oops.

By the way, your news client seems to be mangling long URLs, by splitting 
them when they exceed the maximum line length. I didn't follow the link 
you gave because it was mangled, and then forgot it even existed. Sorry 
about that.


[...]
> You really might want to read the code I provided.  Here's the reference
> again:
> 
> https://bitbucket.org/roysmith/python-tools/src/4f8118d175ed/logs/
traceba
> ck_helper.py

And mangled again :)


> The "header" referred to does indeed contain the exception raised.  And
> the line numbers are included.  Here's a typical output stanza:
[snip]

Ian Kelly has picked up on what I'm trying to say. You might collect the 
traceback in the "header", but it doesn't get used in the key, and each 
time you find a repeated stack trace, you toss away whatever header you 
just saw and keep the header you saw the first time.

[quote]
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)
[end quote]


In general, it is an unsafe assumption that the actual exception raised 
will be the same just because the stack trace is the same. So as I said, 
if you have two *distinct* failures occurring in the same function (not 
even necessarily on the same line), your code appears to treat them as 
the same error. That seems odd to me, but if you have a good reason for 
doing it that way, so be it.



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


Re: Python Interview Questions

2012-11-19 Thread Steven D'Aprano
On Mon, 19 Nov 2012 09:59:19 -0500, Roy Smith wrote:

> OK, I've just read back over the whole thread.  I'm really struggling to
> understand what point you're trying to make.  I started out by saying:
> 
>> 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).
> 
> To which you obviously objected.  So now you write:
> 
>> I think a tuple is an immutable sequence of items, and a list is a
>> mutable sequence of items.
> 
> So how is that different from what I said?  Is this whole argument
> boiling down to your use of "immutable sequence" vs. my use of
> "immutable list"?

Sheesh, of course not. Give me some credit.

I gave some examples of when somebody might use lists, tuples, sets and 
dicts. Apparently I forgot a couple, and you responded with a sarcastic 
comment about the "One True Church Of Pythonic Orthodoxy And Theoretical 
Correctness" and gave a couple of additional examples.

Although I didn't come out and *explicitly* say "I agree" to your 
examples, I actually did, with one proviso: your example of using an 
"immutable list" as dict key. So I asked a question about that *specific* 
use-case:

[quote]
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.
[end quote]

Your reply was to give your stack trace script as an example. That's a 
fine example as a use-case for a temporary list, and I've done similar 
things dozens, hundreds of times myself. As I said:

[quote]
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).
[end quote]

To me, this sounds *exactly* like your use-case: your data, stack traces, 
represent a little chunk of immutable data that you build up a line at a 
time using a temporary list first, just like I wrote. And I said so. 
There's no sign in either your code or your description that the stack 
traces get treated as mutable objects in any way once you have finished 
building them a line at a time. So your real world, practical, "in the 
trenches" example matches my experience: you build a *fixed data record* 
using a *temporary list*, throw the list away, and then never mutate that 
data record again.

So why are we disagreeing? Like many such discussions on the Internet, 
this one has rambled a bit, and I've misunderstood some of your code 
(sorry), and you seem to have misunderstood the question I am asking. 
Maybe my explanation was not clear enough, in which case, sorry again.

I'm asking about the case where one might want the key to remain mutable 
even after it is used as a key, but can't because Python won't let you. 
There's no sign that your stack trace example is such an example.

As I earlier said:

[quote]
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.
[end quote]


and I still can't. 



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


RE: Python Interview Questions

2012-11-19 Thread Prasad, Ramit
Roy Smith wrote:
> 
> OK, I've just read back over the whole thread.  I'm really struggling to
> understand what point you're trying to make.  I started out by saying:
> 
> > 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).
> 
> To which you obviously objected.  So now you write:
> 
> > I think a tuple is an immutable sequence of items, and a list is a
> > mutable sequence of items.
> 
> So how is that different from what I said?  Is this whole argument
> boiling down to your use of "immutable sequence" vs. my use of
> "immutable list"?


'''
Roy:
> 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).
Steven:
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.
'''

To me this is more of a question than an argument. Now moving
on to your specific example.

'''
def extract_stack(lines):
"in traceback_helper module "
header = lines[0]
stack = []
for line in lines:
m = frame_pattern.match(line)
if not m:
continue
frame = (m.group('path'), m.group('function'))
stack.append(frame)
# [Convert to tuple and return after finished building stack.]
return (header, stack)
[...]
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)
'''

Seems to me that Steven is suggesting that stack (after being built)
should converted to a tuple before being returned, because a "stack" 
for any unique exception should be unique and immutable. You do this
anyway; you just do it before putting it into a dictionary rather
than before returning it.

Same net effect (as long as you do not modify `stack` later), so no 
real argument.


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Splitting a line while keeping quoted items together

2012-11-19 Thread josh
I am working on a cmd.Cmd-based program, and normally could just split the 
string and get the right parts.

Now I have a case where I could have two or three words in the string that need 
to be grouped into the same thing.

Then I realized that I'm not the only person who has had to deal with this, and 
I'm wondering if my solution is the best one out there or if this is as ugly at 
it feels?

Code below
...

#x('Seattle 456') -> ('Seattle', '456')
#x('"Portland Alpha" 123') -> ('Portland Alpha', '123')
#x("'Portland Beta' 789') -> ('Portland Beta', '789')


def x(line):
res = []
append = False
appended = None
quote = None
for item in line.split():
if append:
if item.endswith(quote):
appended.append(item[:-1])
res.append(' '.join(appended))
quote = None
appended = None
append = False
else:
appended.append(item)
else:
if item[0] in ["'",'"']:
append = True
appended = [item[1:]]
quote = item[0]
else:
res.append(item)
return res
..

This seem really ugly. Is there a cleaner way to do this? Is there a keyword I 
could search by to find something nicer?

Thanks,

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


Re: Splitting a line while keeping quoted items together

2012-11-19 Thread Chris Rebert
On Monday, November 19, 2012, wrote:

> I am working on a cmd.Cmd-based program, and normally could just split the
> string and get the right parts.
>
> Now I have a case where I could have two or three words in the string that
> need to be grouped into the same thing.
>
> Then I realized that I'm not the only person who has had to deal with
> this, and I'm wondering if my solution is the best one out there or if this
> is as ugly at it feels?
>
> Code below
> ...
>
> #x('Seattle 456') -> ('Seattle', '456')
> #x('"Portland Alpha" 123') -> ('Portland Alpha', '123')
> #x("'Portland Beta' 789') -> ('Portland Beta', '789')
>
>  

This seem really ugly. Is there a cleaner way to do this? Is there a
> keyword I could search by to find something nicer?
>

Use the "shlex" module in the std lib?

Cheers,
Chris


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


re[2]: Splitting a line while keeping quoted items together

2012-11-19 Thread Joshua R English



Well color me ignorant.
Works cleanly. I shouldn't have reinvented the wheel.
Thanks.
 
This seem really ugly. Is there a cleaner way to do this? Is there a keyword I could search by to find something nicer?

Use the "shlex" module in the std lib?

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

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


Re: Linux compatibility

2012-11-19 Thread Cameron Simpson
On Mon, 19 Nov 2012 08:44:37 -0800 (PST), EDI Support
|  declaimed the following in
| gmane.comp.python.general:
| > I would like know if Python  2.4.3 will be compatible with Linux RHEL 5.5 
or 6.1?

It would help if you could qualify what you imagine "compatible with" to
mean...

On 19Nov2012 18:38, Dennis Lee Bieber  wrote:
|   Uhm... 
| 1)Python 2.4.x is practically an antique.  (I just updated my Windows
| machines to 2.7 from 2.5 -- and 2.7 will be the last 2.x version)

RHEL 5.x ships with python 2.4; a up to date RHEL 5.x box here has
2.4.3. I would not surprise me if that was what shipped with 5.0.

One feature if the RHEL distro is that it is _stable_. (See Dennis' point
(3)). They backport bugfixes and security fixes, but otherwise the base OS
doesn't change API. This produces reliable, predictable behaviour for
stuff you have deployed to such a platform.

The price for that is that pretty soon the versions of things supplied
are quite dated.

Anyway, qualify what "compatible" is supposed to mean for you.
-- 
Cameron Simpson 

in rec.moto, jsh wrote:
> Dan Nitschke wrote:
> > Ged Martin wrote:
> > > On Sat, 17 May 1997 16:53:33 +, Dan Nitschke scribbled:
> > > >(And you stay *out* of my dreams, you deviant little
> > > >weirdo.)
> > > Yeah, yeah, that's what you're saying in _public_
> > Feh. You know nothing of my dreams. I dream entirely in text (New Century
> > Schoolbook bold oblique 14 point), and never in color. I once dreamed I
> > was walking down a flowchart of my own code, and a waterfall of semicolons
> > was chasing me. (I hid behind a global variable until they went by.)
> You write code in a proportional serif? No wonder you got extra
> semicolons falling all over the place.
No, I *dream* about writing code in a proportional serif font.
It's much more exciting than my real life.
/* dan: THE Anti-Ged -- Ignorant Yank (tm) #1, none-%er #7 */
Dan Nitschke  [email protected]  [email protected]
-- 
http://mail.python.org/mailman/listinfo/python-list


Stack_overflow error

2012-11-19 Thread Aung Thet Naing
I'm having Stack_overflow exception in _ctypes_callproc (callproc.c). The error 
actually come from the:

 cleanup:
for (i = 0; i < argcount; ++i)
Py_XDECREF(args[i].keep);

when args[i].keep->ob_refCnt == 1

Aung. 

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


Re: Splitting a line while keeping quoted items together

2012-11-19 Thread Steven D'Aprano
On Mon, 19 Nov 2012 16:05:30 -0800, josh wrote:

> I am working on a cmd.Cmd-based program, and normally could just split
> the string and get the right parts.
> 
> Now I have a case where I could have two or three words in the string
> that need to be grouped into the same thing.

Try shlex.split.



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


Re: Stack_overflow error

2012-11-19 Thread Chris Angelico
On Tue, Nov 20, 2012 at 11:49 AM, Aung Thet Naing
 wrote:
> I'm having Stack_overflow exception in _ctypes_callproc (callproc.c). The 
> error actually come from the:
>
>  cleanup:
> for (i = 0; i < argcount; ++i)
> Py_XDECREF(args[i].keep);
>
> when args[i].keep->ob_refCnt == 1

Can you offer more details? I'm guessing you're using ctypes from a
Python script; can you share the script with us?

What Python version are you using? What procedure are you calling?

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


Re: Splitting a line while keeping quoted items together

2012-11-19 Thread Tim Chase
>> Use the "shlex" module in the std lib?
>
> Well color me ignorant.
> 
> Works cleanly. I shouldn't have reinvented the wheel.

I've experienced this enough:  the csv module, option parsing,
config-file parsing, logging, timeit, and pwd all come to mind as
code I've written before realizing the stdlib already has it.  Now,
if my task sounds remotely like something that somebody else might
have implemented already, my first stop is always to browse through
the stdlib docs.  Then try http://pypi.python.org/pypi to see if
somebody else has already solved the problem without the solution
getting into the stdlib.  Only then do I proceed to trying to code
up something of my own.

-tkc


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


Re: re.search when used within an if/else fails

2012-11-19 Thread MRAB

On 2012-11-19 23:43, Kevin T wrote:

python version 2.4.3, yes i know that it is old.  getting the sysadmin to 
update the OS requires a first born.

with the following code..
 for signal in register['signals'] :

351   sigName = signal['functionName']
352   if re.search( "rsrvd", sigName ) == None :
353  print sigName
354  newVal = "%s%s" % ( '1'*signal['bits'] , newVal ) 
#prepend 0's
355   if re.search( "rsrvd", sigName ) != None :
356  print sigName
357  newVal = "%s%s" % ( '0'*signal['bits'], newVal )

regardless of how i code line 352, i can not EVER use an else clause with it.  
if i use an else clause, the else will NEVER get executed...

has any one experienced anything like this behavior?  any suggestions?  the 
above code works but... why should i have to code it like this?


Have you checked the indentation? There may be a mixture of tabs and spaces.

A couple of points:

1. You should be using "is None" and "is not None" instead of "== None" 
and "!= None".


2. You don't need to use regex. Use "rsrvd" in sigName and "rsrvd" not 
in sigName.


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


Re: re.search when used within an if/else fails

2012-11-19 Thread Steven D'Aprano
On Tue, 20 Nov 2012 01:24:54 +, Steven D'Aprano wrote:

> Your code is mangled to the point of unreadability.

Ah, never mind, that's *my* fault, not yours. Or rather, my news reader 
software. Sorry about the noise.

The rest of my post still stands:


- simplify your example to the simplest example that we can run
  http://sscce.org/

- don't put line numbers at the start of lines

- your code doesn't have an else clause

- use "if something is None", not == None.



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


Re: re.search when used within an if/else fails

2012-11-19 Thread Steven D'Aprano
On Mon, 19 Nov 2012 15:43:10 -0800, Kevin T wrote:

> python version 2.4.3, yes i know that it is old.  getting the sysadmin
> to update the OS requires a first born.
> 
> with the following code..
> for signal in register['signals'] :
> 
> 351   sigName = signal['functionName'] 352  
> if re.search( "rsrvd", sigName ) == None : 353 
> print sigName 354  newVal = "%s%s" % (
> '1'*signal['bits'] , newVal ) #prepend 0's 355   if
> re.search( "rsrvd", sigName ) != None : 356  print
> sigName 357  newVal = "%s%s" % ( '0'*signal['bits'],
> newVal )


Your code is mangled to the point of unreadability. 

Please resend, and make sure you send it as PLAIN TEXT and not as HTML 
("rich text"), since many mail programs feel that they are allowed to 
arbitrarily rewrap HTML text however they like.

Preferably simplify your example to the simplest example that we can run:

http://sscce.org/

Being able to run it means you shouldn't put line numbers on the left. If 
you must draw our attention to a specific line, use a comment. This isn't 
1975 and we're not programming in BASIC.

# BAD don't do this:
350  do_this(x)
351  do_that(y, z)
352  if something:
353  do_something_else(x, y, z)


# GOOD do this:
do_this(x)
do_that(y, z)
if something:   # LINE 352
do_something_else(x, y, z)




> regardless of how i code line 352, i can not EVER use an else clause
> with it.  if i use an else clause, the else will NEVER get executed...

The code you show doesn't actually have an `else` clause, which might 
explain why it doesn't get executed.

By the way, you should not write "if something == None", always use "if 
something is None" or "is not None". Technically, there are some 
exceptions but if you have to ask what they are, you don't need to know 
*wink*



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


Re: Linux compatibility

2012-11-19 Thread Steven D'Aprano
On Mon, 19 Nov 2012 08:44:37 -0800, EDI Support wrote:

> Hi All,
> 
> I would like know if Python  2.4.3 will be compatible with Linux RHEL
> 5.5 or 6.1?

I don't see any reason why it wouldn't be, but why would you want to use 
Python 2.4 in production if you don't have to? RHEL will come with Python 
already installed, I believe it is Python 2.6.

It is *much* better to use Python 2.6 if you can: it is faster, has fewer 
bugs, and more modern features.

But if you must use Python 2.4, make sure that you do NOT replace the 
system Python already installed. If installing from source, use "make 
altinstall" instead of "make install" to ensure that it doesn't overwrite 
the system Python.



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


Re: Problems on these two questions

2012-11-19 Thread Dave Angel
On 11/19/2012 06:16 PM, Dennis Lee Bieber wrote:
> On Sun, 18 Nov 2012 21:18:19 -0500, Dave Angel  declaimed
> the following in gmane.comp.python.general:
>
>
>> 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.
>>
>   Since "is_a_prime" is returning a Boolean, this condenses to:
>
>   is_prime = is_a_prime(n)

Nope, the original problem statement didn't say what should should
happen if n does have such factors.  Clearly, it's only describing a
program fragment.

"""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.) 

"""

-- 

DaveA

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


[OT]Two-tonne Witch computer gets a reboot

2012-11-19 Thread Mark Lawrence


You may find this interesting http://www.bbc.co.uk/news/technology-20395212

--
Cheers.

Mark Lawrence.

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


Re: Python Interview Questions

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

> By the way, your news client seems to be mangling long URLs, by splitting 
> them when they exceed the maximum line length.

Hmmm.  So it did.  My bad.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Yet another Python textbook

2012-11-19 Thread alex23
On Nov 20, 2:58 am, Kwpolska  wrote:
> You are writing it for something called “NCLab”, not for the general
> public, and that sucks.

And making it available to the general public to consume. What's wrong
with writing for one audience and providing for a broader?

If you're that concerned with the "NCLab"-ness of it, fork it.

> 2. IMO, you should be doing a bit more general usage programming,
> not science-specific.

It's produced by the Network Computing Laboratory for Science,
Technology, Engineering and Mathematics, of course their focus will be
on science. It's not like there's a lack of more generalised
programming guides for Python.

Some nice bikeshedding there, btw.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Interview Questions

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

> I'm asking about the case where one might want the key to remain mutable 
> even after it is used as a key, but can't because Python won't let you.

Ah.  Now I see what you're getting at.  Thank you.

Well, I will admit that it probably doesn't make sense to mutate an 
object after it's put into a dict (or at least mutate it in a way which 
changes it's hash value and/or whether it compares equal to the original 
object).  If you did (assuming lists were allowed as keys):

l = [1, 2, 3]
d = {l: "spam"}
l.append(4)
print d[l]

I'm not sure what I would expect to print.  It's not too hard to 
experiment, though.  All you need do is:

class HashableList(list):
def __hash__(self):
return hash(tuple(self))

and python is then happy to let you use a list as a key.  I just played 
around with this a bit off-line.  I think I got the results I was 
expecting, but since I'm not sure what I was expecting, that's hard to 
say.

However, you didn't ask if it made sense to mutate an object after using 
it as a key.  You asked if it made sense to let the object remain 
mutable after using it as a key.  That's a harder question.

Let's say I had lots of of lists I wanted to use a dictionary keys.  As 
it stands now, I have to convert them to tuples, which means copying all 
the data.  For a lot of data, that's inefficient.

Wouldn't it be nice (or at least, more efficient) if I could just use 
the original lists as keys directly, without the extra copy?  I would 
have to understand that even though they are mutable, interesting (and 
perhaps, unwanted) things if I actually mutated them.  But, we're all 
consenting adults here.  If I'm willing to accept responsibility for the 
consequences of my actions in return for the efficiency gain, why 
shouldn't I be allowed to?

I guess the answer is, that I am allowed to.  I just need to do the 
HashableList deal, shown above (no broken URL required to read the code).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: changing process name

2012-11-19 Thread Tim Roberts
andrea crotti  wrote:

>I have very long processes to spawn which I want to lauch as separate
>processes (and communicate with ZeroMQ), but now the problem is that the
>forked process appears in "ps" with the same name as the launcher
>process.

http://code.google.com/p/py-setproctitle/
-- 
Tim Roberts, [email protected]
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Getting a seeded value from a list

2012-11-19 Thread frednotbob
> 
>   Are you generating the entire level on entry, or as each room is
> 
> "opened" (if, "as opened", you have the complication that going down a
> 
> level and back up will result in different random numbers vs going
> 
> straight to the room).
> 
> 
> 
>   Generating on entry only needs the seed for the initial generation
> 
> point (as mentioned, first time you could use the system clock and save
> 
> the value).

I'm generating the room on entry -- the 'stairs' object calls 'make_map()' 
which creates the floor and lays out monsters and treasure.

What I'm trying to do is set a persistent state for the levels generated by 
make_map(), so the player can move between floors without generating a totally 
new randomized floor each time.
 
> 
>   But for a dungeon -- you may be best served by generating the entire
> 
> level (all rooms, doors, static encounters [traps, treasure]) when
> 
> entering the level, and saving the entire dungeon (after all, after a
> 
> treasure is collected, it shouldn't re-appear the next time you start
> 
> that same level -- if you only save the starting seed, then all
> 
> encounters will also be regenerated). In this scenario, where you save
> 
> the entire dungeon, you don't even need to worry about the seed --
> 
> you'll never really want to recreate the same dungeon for another party
> 
> [unless running a competition in which case you seed every computer the
> 
> same so every participant is running the identical dungeon].


That actually sounds close to what I'd like to ?do.  How would I go about 
saving the dungeon?  I'm guessing I'd need to define how many levels to 
generate, first of all
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: xml data or other?

2012-11-19 Thread Stefan Behnel
Prasad, Ramit, 19.11.2012 22:42:
> Artie Ziff wrote:
>> 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 think your description text should be in a CDATA section.
> http://en.wikipedia.org/wiki/CDATA#CDATA_sections_in_XML

Ah, don't bother with CDATA. Just make sure the data gets properly escaped,
any XML serialiser will do that for you. Just generate the XML using
ElementTree and you'll be fine. Generating XML as literal text is not a
good idea.

Stefan


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


Re: Problems on these two questions

2012-11-19 Thread Ian Kelly
On Mon, Nov 19, 2012 at 4:15 PM, Dennis Lee Bieber
 wrote:
> On Sun, 18 Nov 2012 17:52:35 -0800 (PST), su29090 <[email protected]>
> declaimed the following in gmane.comp.python.general:
>
>>
>> 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.)
>>
> Google: Sieve of Eratosthenes (might be mis-spelled)

No, the Sieve is nifty, but it's meant for generating sequences of
primes, not for testing individual primality.  It's also more complex
than is necessary.  A better starting place for a programming novice
is with trial division, which is a somewhat simpler algorithm and all
that is needed here.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Yet another Python textbook

2012-11-19 Thread Ian Kelly
On Sun, Nov 18, 2012 at 10:30 PM, Pavel Solin  wrote:
> 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.

First impression: I'm opening up the book and reading the
introduction, and I get to section 1.6, and the very first code
example given is:

>>> print "Hello, World!"

A fine tradition to be sure, but I have to say that I'm a little
disappointed that a new textbook on Python being written in 2012 is
focused squarely on Python 2, especially when I just read on the
previous page that Python 3 was released in 2008.  Is there any work
underway get Python 3 into NCLab?

The issue comes up again four pages later in section 2.4, when
division is being demoed, and the text takes a page-and-a-half detour
to caution about the use of floor division for expressions like:

>>> 33 / 6

If the book were teaching Python 3, then this warning would be
unnecessary, since division in Python 3 is *automatically* true
division, unless you go out of your way to invoke floor division by
using the special // operator.  I think that the earliness and
frequency that these differences arise underscore the point that it
would be best if the book could simply be teaching Python 3 to start
with.

Getting off that soapbox and moving along, I notice that on pages
20-22 there are some examples by way of comparison that are written in
C, which makes me wonder what audience this textbook is really
intended for.  The previous pages and references to Karel have given
me the impression that this is geared toward beginning programmers,
who most likely are not familiar with C.  The most troublesome is the
last of these examples, which is led up to with this text:

The asterisks in the code below are pointers, an additional
programming concept that one needs to learn and utilize here:

This seems to suggest that the reader should stop reading here and do
a Google search on pointers, in order to understand the example.
Since this is not a textbook on C, and Python has no concept of
pointers at all, doing this would be a complete waste of the reader's
time.

Skimming through a few more chapters, I don't see anything else that
sets my spidey sense tingling.  I hope that what I've written above
gives you some things to consider, though.

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