Re: Opinions, please, on PEP 8 and local, 3rd party imports

2009-10-03 Thread Francesco Bochicchio
On Oct 2, 9:50 pm, Philip Semanchuk  wrote:
> Hi all,
>
> PEP 8  says the following:
>
>     Imports should be grouped in the following order:
>     1. standard library imports
>     2. related third party imports
>     3. local application/library specific imports
>
> I'm not sure in which category to place local, 3rd-party modules like  
> configobj.
>
>
...

> Clearly, the best choice is category 2.5?
>

Actually 2.5 is doable :-)
I translate it as "just after any of 2 and before any of 3"

Ciao

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


Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
Raymond Hettinger  writes:
> Can you give an example of a list of trees and a cmp function
> that recursively compares them?

Example of list of trees (nested dicts).  In practice you could get
such a list from the simplejson module:

   list_of_trees = [{'value':1, 'left':{'value':3,'left':None,'right':None},
'right':{'value':7,'left':{'value':5, ...}}},
{'value':19, 'left':{'value':23', ...}},
...
   ]

Example comparison function:

  def compare(tree1, tree2):
 c = cmp(tree1['value'], tree2['value'])
 if c != 0: return c
 c = cmp(tree1['left'], tree2['left'])
 if c != 0: return c
 return cmp(tree1['right'], tree2['right])

> Are the trees user defined classes? 

Not in general.  They might be nested tuples, lists, or dictionaries.
Or they could come from a non-extensible library-defined class, like
from cElementTree.

> From the sound of it, the trees are static during the sort and
> would get a nice O(n log n) --> O(n) speed-up if a key function
> were allowed to flatten them in a single pass.

But the key function has to do all those comparisons on the internal
nodes.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
Paul Rubin  writes:
> Example comparison function:
> 
>   def compare(tree1, tree2):
>  c = cmp(tree1['value'], tree2['value'])
>  if c != 0: return c
>  c = cmp(tree1['left'], tree2['left'])
>  if c != 0: return c
>  return cmp(tree1['right'], tree2['right])

Sorry, meant recursive comparison.

   def compare(tree1, tree2):
  c = cmp(tree1['value'], tree2['value'])
  if c != 0: return c
  c = compare(tree1['left'], tree2['left'])
  if c != 0: return c
  return compare(tree1['right'], tree2['right])
-- 
http://mail.python.org/mailman/listinfo/python-list


Trouble sending / receiving compressed data (using zlib) as HTTP POST to server (in django)

2009-10-03 Thread subeen
Hi,

I am trying to send compressed data to a server written in django. But
it shows error while decompressing the data in the server. After some
experiment I found that the server is not receiving the exact data I
am sending.

data = 'hello, this is a test message this is another message'
data = zlib.compress(data)
# now it looks like: x��HQ(��,V�D���T�p^~IFjL�e
# length is 45

in django (view) I receive it:
data = request.POST['data']
# now it looks like: xQ(�,V�D���^~IFjL�e
# length is 34

Can anybody help me understand the issue and how to get over?


thanks,
Subeen.
http://love-python.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: organizing your scripts, with plenty of re-use

2009-10-03 Thread Stef Mientki

bukzor wrote:

I would assume that putting scripts into a folder with the aim of re-
using pieces of them would be called a package, but since this is an
"anti-pattern" according to Guido, apparently I'm wrong-headed here.
(Reference: http://mail.python.org/pipermail/python-3000/2007-April/006793.html
)

Say you have ~50 scripts or so with lots of re-use (importing from
each other a lot) and you want to organize them into folders. How do
you do this simply?

  

Interesting question, ...
... and although I've a working situation, I would like to see other 
answers.


In my situation I've an estimate of about 2000 scripts (in fact every 
script I ever wrote),

with about zero redundancy.
I still don't use (because I don't fully understand them) packages,
but by trial and error I found a reasonable good working solution,
with the following specifications
- (Almost) any script (what ever it uses from one of the other scripts 
can run standalone
- libraries that need another main program ( e.g. a grid component needs 
a GUI) can launch another main program to test itself

- All __init__ files are generated automatically
Although not containing the last ideas, here's an idea of what I do:
 http://mientki.ruhosting.nl/data_www/pylab_works/pw_importing.html
cheers,
Stef

The intent is to have people be able to check out the directly from
CVS and have the scripts "just work", even if they're not directly on
the PYTHONPATH.

This seems to be the best discussion on the topic, but the conclusion
seemed to be that there's no good way. That seems unthinkable
considering python's dedication to simplicity and elegance.
http://groups.google.com/group/comp.lang.python/browse_thread/thread/c44c769a72ca69fa/


It looks like I'm basically restating this post, which sadly got
dropped without further comment:
http://mail.python.org/pipermail/python-3000/2007-April/006814.html
  


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


Re: organizing your scripts, with plenty of re-use

2009-10-03 Thread Steven D'Aprano
On Fri, 02 Oct 2009 18:14:44 -0700, bukzor wrote:

> I would assume that putting scripts into a folder with the aim of re-
> using pieces of them would be called a package, 

A package is a special arrangement of folder + modules. To be a package, 
there must be a file called __init__.py in the folder, e.g.:

parrot/
+-- __init__.py
+-- feeding/
+-- __init__.py
+-- eating.py
+-- drinking.py
+-- fighting.py
+-- flying.py
+-- sleeping.py
+-- talking.py


This defines a package called "parrot" which includes a sub-package 
feeding and modules fighting, flying, sleeping and talking. You can use 
it by any variant of the following:

import parrot  # loads parrot/__init__.py
import parrot.talking  # loads parrot/talking.py
from parrot import sleeping
import parrot.feeding
from parrot.feeding.eating import eat_cracker

and similar.

Common (but not compulsory) behaviour is for parrot/__init__.py to import 
all the modules within the package, so that the caller can do this:

import parrot
parrot.feeding.give_cracker()

without requiring to manually import sub-packages. The os module behaves 
similarly: having imported os, you can immediately use functions in 
os.path without an additional import.

Just dumping a bunch of modules in a folder doesn't make it a package, it 
just makes it a bunch of modules in a folder. Unless that folder is in 
the PYTHONPATH, you won't be able to import the modules because Python 
doesn't look inside folders. The one exception is that it will look 
inside a folder for a __init__.py file, and if it finds one, it will 
treat that folder and its contents as a package.


> but since this is an
> "anti-pattern" according to Guido, apparently I'm wrong-headed here.
> (Reference:
> http://mail.python.org/pipermail/python-3000/2007-April/006793.html )

Guido's exact words were:

"The only use case seems to be running scripts that happen
to be living inside a module's directory, which I've always seen as an
antipattern."

I'm not sure precisely what he means by that, because modules don't have 
directories, they are in directories. Perhaps he meant package.

In that case, the anti-pattern according to Guido is not to put modules 
in a folder, but to have modules inside a package be executable scripts. 
To use the above example, if the user can make the following call from 
the shell:

$ python ./parrot/talking.py "polly want a cracker"

and have the module talking do something sensible, that's an anti-
pattern. Modules inside a package aren't intended to be executable 
scripts called by the user. There should be one (or more) front-end 
scripts which are called by the user. Since they aren't intended to be 
imported, they can be anywhere, not just on the PYTHONPATH. But they 
import the modules in the package, and that package *is* in the 
PYTHONPATH.

Using the above example, you would install the parrot folder and its 
contents somewhere on the PYTHONPATH, and then have a front-end script 
(say) "talk-to-parrot" somewhere else. Notice that the script doesn't 
even need to be a legal name for a module, since you're never importing 
it.



> Say you have ~50 scripts or so with lots of re-use (importing from each
> other a lot) and you want to organize them into folders. How do you do
> this simply?

Of course you can have a flat hierarchy: one big folder, like the 
standard library, with a mixed back of very loosely connected modules:

eating.py
drinking.py
feeding.py
fighting.py
flying.py
parrot.py
sleeping.py
talking.py


You can do that, of course, but it's a bit messy -- what if somebody 
installs parrot.py and eating.py, but not drinking.py, and as a 
consequence parrot.py fails to work correctly? Or what if the user 
already has a completely unrelated module talking.py? Chaos.

The std library can get away with dumping (nearly) everything in the one 
directory, because it's managed chaos. Users aren't supposed to pick and 
choose which bits of the standard library get installed, or install other 
modules in the same location.

Three alternatives are:

* put your modules in a sub-folder, and tell the user to change the 
Python path to include your sub-folder, and hope they know what you're 
talking about;

* put your modules in a package, tell the user to just place the entire 
package directory where they normally install Python code, and importing 
will just work; or

* have each and every script manually manipulate the PYTHONPATH so that 
when the user calls that script, it adds its parent folder to the 
PYTHONPATH before importing what it needs. Messy and ugly.

 

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


Calendar yr-mnth-day data to day since data

2009-10-03 Thread [email protected]
Hi all,

I have some calendar data in three arrays corresponding to yr, month,
day that I would like to convert to day since data and be consistent
with changes in leap year. I've included a sample of the data
structures below.  Any suggestions???  Thanks in advance

yr  mnthday daySince
19701   1 1
19701   15  15
19701   28  28
19702   1
32
19702   27  59
19703   1
19703   4
19703   29
...   ......

20081   1
20081   8
20081   25
20082   1
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Threaded GUI slowing method execution?

2009-10-03 Thread sturlamolden
On 2 Okt, 21:30, Dave Angel  wrote:

> There could very well be multiprocess support in wxPython.  I'd check
> there first, before re-inventing the wheel.

I don't think there is. But one can easily make a thread in the
subprocess that polls a pipe and calls wx.PostEvent or wx.CallLater
when something is received. I particularly like the Queue object in
multiprocessing for this.

One could also use win32api.SendMessage from pywin32 to send the event
from one process to another. The parent must know the hwnd of the
subprocess main wx.Frame. The subprocess gets that from calling the
GetHandle() method of its wx.Frame, and must post it back to the
parent, presumably via a pipe.





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


Re: re.sub do not replace portion of match

2009-10-03 Thread Duncan Booth
J Wolfe  wrote:

> Hi,
> 
> Is there a way to flag re.sub not to replace a portion of the string?
> 
> I have a very long string that I want to add two new line's to rather
> than one, but keep the value X:
> 
> string = "testX.\n.today"  <-- note X is a value
> string = re.sub("testX.\n.","testX.\n\n.", string)
> 
> This just replaces X with the replacement string.
> 
> Thanks,
> Jonathan
> 

Have you tried reading the documentation?

http://www.python.org/doc/current/library/re.html#re.sub

> Backreferences, such as \6, are replaced with the substring matched by 
> group 6 in the pattern.

and more such options in the docs.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Haskell's new logo, and the idiocy of tech geekers

2009-10-03 Thread Robert H
http://blog.plover.com/prog/haskell/logo.html

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


Re: organizing your scripts, with plenty of re-use

2009-10-03 Thread Steven D'Aprano
On Sat, 03 Oct 2009 10:24:13 +0200, Stef Mientki wrote:

> I still don't use (because I don't fully understand them) packages, but
> by trial and error I found a reasonable good working solution, with the
> following specifications

I find that fascinating. You haven't used packages because you don't 
understand them, but you've used another technique that you *also* don't 
understand well enough to generate a solution, and had to rely on trial 
and error.

Packages are quite well documented. Since the alternative was trial-and-
error on something you also don't fully understand, why did you avoid 
packages?



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


The Python: Rag October issue available

2009-10-03 Thread Bernie

The Python: Rag October issue available


The October issue of The Python: Rag is available at:

http://www.pythonrag.org

A monthly, free, community run, Python magazine - issues are in pdf 
format, intended for anyone interested in Python, without being 
particularly serious.  If you have anything you would like to say about 
Python, please contribute.
-- 
http://mail.python.org/mailman/listinfo/python-list


re.sub do not replace portion of match

2009-10-03 Thread J Wolfe
Hi,

Is there a way to flag re.sub not to replace a portion of the string?

I have a very long string that I want to add two new line's to rather
than one, but keep the value X:

string = "testX.\n.today"  <-- note X is a value
string = re.sub("testX.\n.","testX.\n\n.", string)

This just replaces X with the replacement string.

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


Re: organizing your scripts, with plenty of re-use

2009-10-03 Thread Donn
Great description - wish the Python docs could be as clear. Thanks.

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


Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
Paul Rubin  writes:
>   c = compare(tree1['left'], tree2['left'])

Of course this recursive call crashes if either branch is None.
Oh well, I'll stop trying to correct it since I'm sure you get the idea.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: unicode text file

2009-10-03 Thread Junaid
On Sep 27, 6:39 pm, "Mark Tolonen"  wrote:
> "Junaid"  wrote in message
>
> news:0267bef9-9548-4c43-bcdf-b624350c8...@p23g2000vbl.googlegroups.com...
>
> >I want to do replacements in a utf-8 text file. example
>
> > f=open("test.txt","r") #this file is uft-8 encoded
> > raw = f.read()
> > txt = raw.decode("utf-8")
>
> You can use the codecs module to open and decode the file in one step
>
>
>
> > txt.replace{'English', ur'ഇംഗ്ലീഷ്') #replacing raw unicode string,
> > but not working
>
> The replace method returns the altered string.  It does not modify it in
> place.  You also should use Unicode strings for both the arguments (although
> it doesn't matter in this case).  Using a raw Unicode string is also
> unnecessary in this case.
>
>     txt = txt.replace(u'English', u'ഇംഗ്ലീഷ്')
>
> > f.write(txt)
>
> You opened the file for writing.  You'll need to close the file and reopen
> it for writing.
>
> > f.close()
> > f.flush()
>
> Flush isn't required.  close() will flush.
>
> Also to have text like ഇംഗ്ലീഷ് in a file you'll need to declare the
> encoding of the file at the top and be sure to actually save the file in the
> encoding.
>
> In summary:
>
>     # coding: utf-8
>     import codecs
>     f = codecs.open('test.txt','r','utf-8')
>     txt = f.read()
>     txt = txt.replace(u'English', u'ഇംഗ്ലീഷ്')
>     f.close()
>     f = codecs.open('test.txt','w','utf-8')
>     f.write(txt)
>     f.close()
>
> -Mark

thanx everyone for replying,

I did as Mark suggested, and it worked :)

thanx once more
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Haskell's new logo, and the idiocy of tech geekers

2009-10-03 Thread Tim Rowe
2009/10/3 Xah Lee :

> for my criticism or comment on logos, typical response by these people
> are showcases of complete ignorance of social function of logos

[snip and rearrange]

> discussed now and then in these communities often without my
> involvement.

> “you are a fucking idiot...

> motherfucking aggresive

> it's just few of priest fuckheads

> look at lojban's motherfucking idiotic logo

If you really knew anything about social function you would be able to
work out why people think you are a troll.

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


Re: Trouble sending / receiving compressed data (using zlib) as HTTP POST to server (in django)

2009-10-03 Thread Piet van Oostrum
> subeen  (s) wrote:

>s> Hi,
>s> I am trying to send compressed data to a server written in django. But
>s> it shows error while decompressing the data in the server. After some
>s> experiment I found that the server is not receiving the exact data I
>s> am sending.

>s> data = 'hello, this is a test message this is another message'
>s> data = zlib.compress(data)
>s> # now it looks like: x��HQ(��,V�D.���T�p^~IFj.L.�.e
>s> # length is 45

Note: you can't just paste binary data in the message and expect
something sensible. Better use the result of 'print data'.

>s> in django (view) I receive it:
>s> data = request.POST['data']
>s> # now it looks like: xQ(�,V�D���.^~IFj.L.�.e
>s> # length is 34

>s> Can anybody help me understand the issue and how to get over?

How did you post the data? If you post binary data you should indicate
this with a proper mime type, like application/octet-stream. Otherwise
it might be interpreted as text which it isn't.
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calendar yr-mnth-day data to day since data

2009-10-03 Thread Piet van Oostrum
> "[email protected]"  (sc) wrote:

>sc> Hi all,
>sc> I have some calendar data in three arrays corresponding to yr, month,
>sc> day that I would like to convert to day since data and be consistent
>sc> with changes in leap year. I've included a sample of the data
>sc> structures below.  Any suggestions???  Thanks in advance

>sc> yr  mnthday daySince
>sc> 19701   1 1
>sc> 19701   15  15
>sc> 19701   28  28
>sc> 19702   1
>sc> 32
>sc> 19702   27  59
>sc> 19703   1
>sc> 19703   4
>sc> 19703   29
>sc> ...   ......

>sc> 20081   1
>sc> 20081   8
>sc> 20081   25
>sc> 20082   1

Days since what? It appears here to be since 1969-12-31, or since
1970-1-1 but then starting with 1 instead of 0.
And your 59 is wrong if the others are deemed to be correct.

Depending on what you want you have to add 1 to the following solution

import datetime
startdate = datetime.date(1970, 1, 1)
enddate = datetime.date(1970,3,1)
timediff = enddate - startdate
print timediff.days

result: 59
-- 
Piet van Oostrum 
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fast decimal arithmetic module released

2009-10-03 Thread Stefan Krah
Mark Dickinson  wrote:
> On Oct 2, 8:53 pm, Stefan Krah  wrote:
> > Hi,
> >
> > today I have released the following packages for fast arbitrary precision
> > decimal arithmetic:
> >
> [...]
> 
> Nice!  I'd been wondering how you'd been finding all those decimal.py
> bugs.  Now I know.  :)

Thanks! Yes, actually deccheck.py deserves the credit. ;)


Stefan Krah


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


Need feedback on subprocess-using function

2009-10-03 Thread gb345



I'm relatively new to Python, and I'm trying to get the hang of
using Python's subprocess module.  As an exercise, I wrote the Tac
class below, which can prints output to a file "in reverse order",
by piping it through the Unix tac utility.  (The idea is to delegate
the problem of managing the memory for an arbitrarily large task
to tac.)

class Tac(object):
def __init__(self, path):
out = open(path, 'w')
self.pipe = subprocess.Popen(['tac'], stdout=out,
 stdin=subprocess.PIPE,
 stderr=subprocess.PIPE)
def prn(self, string):
try:
self.pipe.stdin.write('%s\n' % string)
except:
   self.close()
   raise

def close(self):
p = self.pipe
p.stdin.close()
err = p.stderr.read()
if err:
raise OSError(err)

This works OK, as far as I can tell, but I'm not sure that I've
dotted all the i's and crossed all the t's...  E.g., I had to add
the line "p.stdin.close()" to the close method when I when I ran
into sporadic deadlock at the p.stderr.read() statement.  Are there
other similar problems lurking in this code?  Also, there's no
robust mechanism for invoking this close method in case of an
exception (unless the exception happens during the execution of
prn).

Any comments and suggestions would be greatly appreciated.

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


Re: Haskell's new logo, and the idiocy of tech geekers

2009-10-03 Thread Nick Keighley
On 3 Oct, 00:33, Xah Lee  wrote:
> Haskell has a new logo. A fantastic one. Beautiful. For creator,
> context, detail, see bottom of:
>
> • A Lambda Logo Tour
>  http://xahlee.org/UnixResource_dir/lambda_logo.html

I'm amazed he thinks anyone would donate 3 USD
to that site


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


Enormous Input and Output Test

2009-10-03 Thread n00m
Hi, py.folk!

I need your help to understand how
http://www.spoj.pl/problems/INOUTEST/
can be passed in Python.

I see two guys who managed to get accepted:
http://www.spoj.pl/ranks/INOUTEST/lang=PYTH

My code for this is:
===
import psyco
psyco.full()

import sys

def noo(b):
b = b.split()
return str(int(b[0]) * int(b[1])) + '\n'

def foo():
##sys.stdin = open('D:/1583.txt', 'rt')
a = sys.stdin.readlines()
a = a[1:int(a[0]) + 1]
a = map(noo, a)
sys.stdout.writelines(a)

foo()
===

But it gets "Time Limit Exceeded" verdict.
Any ideas?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: setuptools, accessing ressource files

2009-10-03 Thread PJ Eby
On Oct 2, 7:04 am, Patrick Sabin  wrote:
> I use setuptools to create a package. In this package I included some
> images and I checked that they are in the egg-file. The problem is how
> can I access the images in the package?
>
> I tried pkgutil.get_data, but only got an IOError, because the EGG-INFO
> directory doesn't exist.
>
> I tried
> pkg_resources.get_distribution('dist').get_metadata('images/image.png')
> with a similar error (IOError)
>
> What is the best way to access images distributed in an egg file?

The resource_stream(), resource_string(), or resource_filename()
functions:

http://peak.telecommunity.com/DevCenter/PkgResources#basic-resource-access

>
> -Patrick

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


Re: Regular expression to structure HTML

2009-10-03 Thread [email protected]
On Oct 2, 11:14 pm, greg  wrote:
> Brian D wrote:
> > This isn't merely a question of knowing when to use the right
> > tool. It's a question about how to become a better developer using
> > regular expressions.
>
> It could be said that if you want to learn how to use a
> hammer, it's better to practise on nails rather than
> screws.
>
> --
> Greg

It could be said that the bandwidth in technical forums should be
reserved for on-topic exchanges, not flaming intelligent people who
might have something to contribute to the forum. The truth is, I found
a solution where others were ostensibly either too lazy to attempt, or
too eager grandstanding their superiority to assist. Who knows --
maybe I'll provide an alternative to BeautifulSoup one day.

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


Customizing Option Elements

2009-10-03 Thread Victor Subervi
Hi;
I want to create  elements within a  element in which I
could insert html, which, of course, is illegal (don't tell the police ;) so
I'm looking at recreating the form elements using my own customized
elements, that is, hacking the equivalent from scratch, but how do I
proceed? I would like to make blocks of color using 6-digit rgb codes
(#ff) with verbiage. How proceed?
TIA,
Victor
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Q: sort's key and cmp parameters

2009-10-03 Thread kj
In <[email protected]> Paul Rubin 
 writes:

>Python 2.x provides two ways
>and you can use whichever one fits the application better.  I have
>never understood why Python 3.x finds it necessary to break one of
>them.  Maybe I can migrate to Haskell by the time Python 2.x becomes
>deprecated.

!!!

Maybe Haskell is much handier than I give it credit for, but it's
hard for me to imagine that it is as convenient as Python 3, even
without the cmp sort option...  (And I agree with you that getting
rid of sort's cmp in Python 3 was a bad idea.)

What's going on here?  Our lab recently hired a new postdoc who,
to our disbelief, works almost exclusively in OCaml.  And I hear
all this talk about switching to Haskell or Scheme.  I don't get
it.  Despite the elegance of these languages, the libraries are
not there.  It seems to me it would take forever to get the simplest
things done in these languages...

Confused.

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


Re: The Python: Rag October issue available

2009-10-03 Thread Bernie
On Fri, 02 Oct 2009 11:32:41 -0700, steven.oldner wrote:
Hi, no -its just put on the website.  Unless there's a method you can 
suggest?

  Cheers

 Bernie

> On Oct 2, 11:14 am, Bernie  wrote:
>> The Python: Rag October issue available
>>
>> The October issue of The Python: Rag is available at:
>>
>> http://www.pythonrag.org
>>
>> A monthly, free, community run, Python magazine - issues are in pdf
>> format, intended for anyone interested in Python, without being
>> particularly serious.  If you have anything you would like to say about
>> Python, please contribute.
> 
> Thanks!  Any way to subscribe to it?

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


Re: AJAX Widget Framework

2009-10-03 Thread Mathias Waack
Laszlo Nagy wrote:

> I'm looking for an open source, AJAX based widget/windowing framework.
> Here is what I need:
> 
> - end user opens up a browser, points it to a URL, logs in
> - on the server site, sits my application, creating a new session for
> each user that is logged in
> - on the server site, I create windows(frames), put widgets on them,
> write event handlers etc. Just like with wx or pygtk.
> - However, windows are created in the user's browser, and events are
> triggered by Javascript, and sent back to server through AJAX.
> - the server side would be - of course - written in Python.
> 
> I was looking these projects:
> 
> http://www.uize.com/
> http://pyjs.org/
> 
> There are many frameworks listed here which I did not check:
> http://internetmindmap.com/javascript_frameworks. I have no idea which
> has Python support, and probably there are only a few that worth looking
> at. I wonder if you could tell me which are the pros and contras for
> these frameworks. If there is a comparison available on the NET,
> pointing me to the right URL would help a lot.
> 
> The bottom line...
> 
> My main goal is to develop enterprise class OLTP database applications.
> I'll need grid widget to display data in tabular format, and I'll use
> events heavily for data manipulation, live search, tooltips etc. I'm
> familiar with wxWidgets, pygtk and other toolkits, but not with AJAX. I
> have never used a system like that.

Of course I don't know which is the right way for you. I'm very happy with 
pyjamas. It allows you the create client applications in the same manner as 
standalone toolkits like wxWidgets. The message passing between client and 
server is simple done by exchanging json strings (its ajax based of course, 
but this stuff works silently in the background). On the server side there 
are many python toolkits, I prefer cherrypy, others are django and web.py. 

Hope this helps you. 

Mathias

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


PIL : How to write array to image ???

2009-10-03 Thread Martin
Dear group

I'm trying to use PIL to write an array (a NumPy array to be exact) to
an image.
Peace of cake, but it comes out looking strange.

I use the below mini code, that I wrote for the purpose. The print of
a looks like expected:

[[ 200.  200.  200. ...,0.0.0.]
 [ 200.  200.  200. ...,0.0.0.]
 [ 200.  200.  200. ...,0.0.0.]
 ...,
 [   0.0.0. ...,  200.  200.  200.]
 [   0.0.0. ...,  200.  200.  200.]
 [   0.0.0. ...,  200.  200.  200.]]

But the image looks nothing like that.

Please see the images on:
http://hvidberg.net/Martin/temp/quat_col.png
http://hvidberg.net/Martin/temp/quat_bw.png

or run the code to see them locally.

Please – what do I do wrong in the PIL part ???

:-? Martin



import numpy as np
from PIL import Image
from PIL import ImageOps

maxcol = 100
maxrow = 100

a = np.zeros((maxcol,maxrow),float)

for i in range(maxcol):
for j in range(maxrow):
if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
(maxrow/2)):
a[i,j] = 200
else:
a[i,j] = 0

print a

pilImage = Image.fromarray(a,'RGB')
pilImage.save('quat_col.png')
pilImage = ImageOps.grayscale(pilImage)
pilImage.save('quat_bw.png')
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Haskell's new logo, and the idiocy of tech geekers

2009-10-03 Thread Jack Diederich
It's Xah Lee, he's been trolling this and every other programing
language group for over 10 years (preferably all at once).  Let it go.

On Sat, Oct 3, 2009 at 2:53 AM, Chris Withers  wrote:
> Xah Lee wrote:
>>
>> Haskell has a new logo. A fantastic one. Beautiful. For creator,
>> context, detail, see bottom of:
>
> What does this have to do with Python? Nothing.
> So why are you posting it to comp.lang.python?
>
> Chris
>
> --
> Simplistix - Content Management, Batch Processing & Python Consulting
>           - http://www.simplistix.co.uk
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Q: sort's key and cmp parameters

2009-10-03 Thread Paul Rubin
kj  writes:
> !!!
> 
> Maybe Haskell is much handier than I give it credit for, but it's
> hard for me to imagine that it is as convenient as Python 3, even
> without the cmp sort option...  

Heh, yeah, I was being a bit snarky/grouchy.  Haskell has a very steep
learning curve and will never be as convenient as Python for banging
out some small script.  It's worth considering for larger or more
serious programs.

> What's going on here?  Our lab recently hired a new postdoc who,
> to our disbelief, works almost exclusively in OCaml.  And I hear
> all this talk about switching to Haskell or Scheme.  I don't get
> it.  Despite the elegance of these languages, the libraries are
> not there.  It seems to me it would take forever to get the simplest
> things done in these languages...

Haskell's library is growing very rapidly, more so than Python's I'd
say.  Take a look at

  http://donsbot.wordpress.com/2009/03/16/visualising-the-haskell-universe/

if you're willing to count Hackage (sort of the equivalent of the
Python cheese shop).  The Haskell Platform (counterpart to Python
stdlib) is also very actively expanding.

Ocaml and Scheme both seem to me to be sort of stagnant.  Scheme is an
elegant fossil.  Some people find Ocaml to be at a sweet spot,
combining Python's convenience and the more important aspects of
Haskell's expressiveness.  I haven't used it myself.  It seems to me
that Haskell is attracting all the most advanced development
attention.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python resident memory retention & Evan Jones' improvements

2009-10-03 Thread Matt Ernst
On Oct 2, 4:47 pm, Andrew MacIntyre 
wrote:
> There are two things you need to be aware of in this situation:
>
> - not all Python's memory is allocated through Python's specialised
>    malloc() - int and float objects in particular (in 2.x at least) are
>    allocated directly via a privately managed free list, and any
>    allocation requiring more than 256 bytes is directed to the platform
>    malloc().  Any memory not allocated via Python's malloc() is not
>    subject to the memory release facility referred to above.
>
>    Python 2.6 does improve the management of memory consumed by int and
>    float objects via the garbage collector.
>
> - while Python attempts to maximally utilise memory arenas to improve
>    the chances of being able to free them, Python's malloc() does not do
>    any compaction of memory (ie moving allocations between arenas) within
>    existing arenas.  Nor does garbage collection do this.  So fragmented
>    allocations can cause the retention of nearly empty arenas.
>
> I suspect that what you see with the second test script above is caused
> by some sort of fragmentation.

Thank you for the explanation. Since my real application uses many
small objects, it makes sense that memory cannot be reclaimed in the
absence of compaction.

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


Re: PIL : How to write array to image ???

2009-10-03 Thread Robert Kern

Martin wrote:

Dear group

I'm trying to use PIL to write an array (a NumPy array to be exact) to
an image.
Peace of cake, but it comes out looking strange.

I use the below mini code, that I wrote for the purpose. The print of
a looks like expected:

[[ 200.  200.  200. ...,0.0.0.]
 [ 200.  200.  200. ...,0.0.0.]
 [ 200.  200.  200. ...,0.0.0.]
 ...,
 [   0.0.0. ...,  200.  200.  200.]
 [   0.0.0. ...,  200.  200.  200.]
 [   0.0.0. ...,  200.  200.  200.]]

But the image looks nothing like that.

Please see the images on:
http://hvidberg.net/Martin/temp/quat_col.png
http://hvidberg.net/Martin/temp/quat_bw.png

or run the code to see them locally.

Please – what do I do wrong in the PIL part ???

:-? Martin



import numpy as np
from PIL import Image
from PIL import ImageOps

maxcol = 100
maxrow = 100

a = np.zeros((maxcol,maxrow),float)

for i in range(maxcol):
for j in range(maxrow):
if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
(maxrow/2)):
a[i,j] = 200
else:
a[i,j] = 0

print a

pilImage = Image.fromarray(a,'RGB')


You are telling it the wrong mode information. If you want an RGB image, you 
need to give it a uint8 array with shape (height, width, 3). Exactly what are 
you trying to do? I can't infer that from your code.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Customizing Option Elements

2009-10-03 Thread Laszlo Nagy


I want to create  elements within a  element in which 
I could insert html, which, of course, is illegal (don't tell the 
police ;) so I'm looking at recreating the form elements using my own 
customized elements, that is, hacking the equivalent from scratch, but 
how do I proceed? I would like to make blocks of color using 6-digit 
rgb codes (#ff) with verbiage. How proceed?
That are your requirements? If the only requirement is that it should 
look very very fancy and futuristic then you can do it with flash 
programming. (You need Flash knowledge and the result may not be a real 
 but an .) Another way to do it is JavaScript and 
absolute positioned DOM elements (you will need CSS and JavaScript 
knowledge). One thing is sure: this question has almost nothing to do 
with Python. Please post this to a JavaScript or Flash programming 
mailing list.


 Laszlo

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


Re: PIL : How to write array to image ???

2009-10-03 Thread Peter Otten
Martin wrote:

> Dear group
> 
> I'm trying to use PIL to write an array (a NumPy array to be exact) to
> an image.
> Peace of cake, but it comes out looking strange.
> 
> I use the below mini code, that I wrote for the purpose. The print of
> a looks like expected:
> 
> [[ 200.  200.  200. ...,0.0.0.]
>  [ 200.  200.  200. ...,0.0.0.]
>  [ 200.  200.  200. ...,0.0.0.]
>  ...,
>  [   0.0.0. ...,  200.  200.  200.]
>  [   0.0.0. ...,  200.  200.  200.]
>  [   0.0.0. ...,  200.  200.  200.]]
> 
> But the image looks nothing like that.
> 
> Please see the images on:
> http://hvidberg.net/Martin/temp/quat_col.png
> http://hvidberg.net/Martin/temp/quat_bw.png
> 
> or run the code to see them locally.
> 
> Please – what do I do wrong in the PIL part ???
> 
> :-? Martin
> 
> 
> 
> import numpy as np
> from PIL import Image
> from PIL import ImageOps
> 
> maxcol = 100
> maxrow = 100
> 
> a = np.zeros((maxcol,maxrow),float)
> 
> for i in range(maxcol):
> for j in range(maxrow):
> if (i<(maxcol/2) and j<(maxrow/2)) or (i>=(maxcol/2) and j>=
> (maxrow/2)):
> a[i,j] = 200
> else:
> a[i,j] = 0
> 
> print a
> 
> pilImage = Image.fromarray(a,'RGB')
> pilImage.save('quat_col.png')
> pilImage = ImageOps.grayscale(pilImage)
> pilImage.save('quat_bw.png')

The PIL seems to copy the array contents directly from memory without any 
conversions or sanity check. In your example The float values determine the 
gray value of 8 consecutive pixels.

If you want a[i,j] to become the color of the pixel (i, j) you have to use 
an array with a memory layout that is compatible to the Image.
Here are a few examples:

>>> import numpy
>>> from PIL import Image

>>> a = numpy.zeros((100, 100), numpy.uint8)
>>> a[:50, :50] = a[50:, 50:] = 255
>>> Image.fromarray(a).save("tmp1.png")

>>> b = numpy.zeros((100, 100, 3), numpy.uint8)
>>> b[:50, :50, :] = b[50:, 50:, :] = [255, 0, 0]
>>> Image.fromarray(b).save("tmp2.png")

>>> c = numpy.zeros((100, 100), numpy.uint32)
>>> c[:50, :50] = c[50:, 50:] = 0xff808000
>>> Image.fromarray(c, "RGBA").save("tmp3.png")

Peter

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


Re: question about the GC implementation

2009-10-03 Thread ryles
On Oct 2, 11:20 am, Francis Moreau  wrote:
> Hello,
>
> I'm looking at gcmodule.c and in move_unreachable() function, the code
> assumes that if an object has its gc.gc_refs stuff to 0 then it *may*
> be unreachable.
>
> How can an object tagged as unreachable could suddenly become
> reachable later ?
>
> Thanks

It looks like you're not reading through all of the comments:

GC_TENTATIVELY_UNREACHABLE
move_unreachable() then moves objects not reachable (whether
directly or
indirectly) from outside the generation into an "unreachable" set.
Objects that are found to be reachable have gc_refs set to
GC_REACHABLE
again.  Objects that are found to be unreachable have gc_refs set
to
GC_TENTATIVELY_UNREACHABLE.  It's "tentatively" because the pass
doing
this can't be sure until it ends, and GC_TENTATIVELY_UNREACHABLE
may
transition back to GC_REACHABLE.

Only objects with GC_TENTATIVELY_UNREACHABLE still set are
candidates
for collection.  If it's decided not to collect such an object
(e.g.,
it has a __del__ method), its gc_refs is restored to GC_REACHABLE
again.
-- 
http://mail.python.org/mailman/listinfo/python-list


Dictionary with Lists

2009-10-03 Thread Shaun
Hi,

I'm trying to create a dictionary with lists as the value for each
key.  I was looking for the most elegant way of doing it... I thought
this would work:

testDict = {}
...
testDict [1] = testDict.get (1, []).append ("Test0")  # 1 does not
exist, create empty array
print testDict
testDict [1] = testDict.get (1, []).append ("Test1")
print testDict

(Obviously I wouldn't normally write code like this.. but this is how
it would unfold in a loop)
However, the first printout gives {1: None}  instead of the desired
{1: ['test']}.  What's wrong with this syntax?

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


Is pythonic version of scanf() or sscanf() planned?

2009-10-03 Thread ryniek90

Hi

I know that in python, we can do the same with regexps or *.split()*, 
but thats longer and less practical method than *scanf()*. I also found 
that ( http://code.activestate.com/recipes/502213/ ), but the code 
doesn't looks so simple for beginners. So, whether it is or has been 
planned the core Python implementation of *scanf()* ? (prefered as a 
batteries included method)


Thanks for any helpful answers.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary with Lists

2009-10-03 Thread Mick Krippendorf
Hi,

Shaun wrote:
> I'm trying to create a dictionary with lists as the value for each
> key.  I was looking for the most elegant way of doing it... 

from collections import defaultdict

d = defaultdict(list)

d["joe"].append("something")
d["joe"].append("another")
d["jim"].append("slow down, grasshopper")

print d


HTH,
mick.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary with Lists

2009-10-03 Thread Mel
Shaun wrote:
> testDict = {}
> ...
> testDict [1] = testDict.get (1, []).append ("Test0")  # 1 does not
> exist, create empty array
> print testDict
> testDict [1] = testDict.get (1, []).append ("Test1")
> print testDict
> 
[ ... ]
> However, the first printout gives {1: None}  instead of the desired
> {1: ['test']}.  What's wrong with this syntax?

The trouble is that the list.append method returns None, after modifying the 
value of the parent list in place.  So of course, None gets assigned to 
testDict[1] after the valuable work has been done.

The old way to do what you want is

testDict.setdefault (1, []).append ("Test0")

The new way is to inherit from defaultdict.

Mel.


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


Re: Is pythonic version of scanf() or sscanf() planned?

2009-10-03 Thread MRAB

ryniek90 wrote:

Hi

I know that in python, we can do the same with regexps or *.split()*, 
but thats longer and less practical method than *scanf()*. I also found 
that ( http://code.activestate.com/recipes/502213/ ), but the code 
doesn't looks so simple for beginners. So, whether it is or has been 
planned the core Python implementation of *scanf()* ? (prefered as a 
batteries included method)



scanf() uses '%' format like that used for printing, but '%' format is
being replaced in Python by '{}' format, so shouldn't any possible
future scanf() use that instead? :-)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Enormous Input and Output Test

2009-10-03 Thread Chris Rebert
On Sat, Oct 3, 2009 at 6:54 AM, n00m  wrote:
> Hi, py.folk!
>
> I need your help to understand how
> http://www.spoj.pl/problems/INOUTEST/
> can be passed in Python.

> def foo():
>    ##sys.stdin = open('D:/1583.txt', 'rt')
>    a = sys.stdin.readlines()

That line is probably a Very Bad Idea (TM) as it reads the *entire*
enormous file into memory *at once*. It would probably be much better
to iterate over the file, thus only reading one individual line at a
time. I'm betting the massive malloc()ing involved with .readlines()
is a large part of the slowness.


> But it gets "Time Limit Exceeded" verdict.
> Any ideas?

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


Re: Enormous Input and Output Test

2009-10-03 Thread Terry Reedy

n00m wrote:

Hi, py.folk!

I need your help to understand how
http://www.spoj.pl/problems/INOUTEST/
can be passed in Python.

I see two guys who managed to get accepted:
http://www.spoj.pl/ranks/INOUTEST/lang=PYTH

My code for this is:
===
import psyco
psyco.full()

import sys

def noo(b):
b = b.split()
return str(int(b[0]) * int(b[1])) + '\n'

def foo():
##sys.stdin = open('D:/1583.txt', 'rt')
a = sys.stdin.readlines()
a = a[1:int(a[0]) + 1]
a = map(noo, a)
sys.stdout.writelines(a)

foo()
===

But it gets "Time Limit Exceeded" verdict.
Any ideas?


Don't waste your time with problem sites that judge raw-clock time over 
(and before) accuracy, thereby greatly favoring low-level languages and 
hack tricks over clear high-level code.


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


Re: Haskell's new logo, and the idiocy of tech geekers

2009-10-03 Thread Michel Alexandre Salim
On Oct 2, 7:33 pm, Xah Lee  wrote:
> Haskell has a new logo. A fantastic one. Beautiful. For creator,
> context, detail, see bottom of:
>
> • A Lambda Logo Tour
>  http://xahlee.org/UnixResource_dir/lambda_logo.html

Interesting.. rant. Thanks for the logo collection, though, some of
them are visually quite breathtaking.

Q1: if you argue that Lisp and Scheme implementations are not purely
functional enough to merit the use of the lambda character, then
surely you'd be happy with the new Chicken logos, as the stylized
representation is less obvious?

Q2: if you preach about the sanctity of Greek characters, then what
does your use of the sigma character mean -- that your site is a
summation of all knowledge?

Regards,

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


Problem with subprocess module on Windows with open file in append mode

2009-10-03 Thread Andrew Savige
When I run this little test program on Linux:

import subprocess
subprocess.call(["python","-V"], stderr=open("log.tmp","a"))

the file log.tmp is appended to each time I run it.
When I run it on Windows, however, the file log.tmp gets
overwritten each time I run it.

Though I can make it append on Windows like this:

import os
import subprocess
f = open("log.tmp", "a")
f.seek(0, os.SEEK_END)
subprocess.call(["python","-V"], stderr=f)

I don't understand why that should be necessary.

Is this a Python/subprocess bug on Windows?

(I tested with Python 2.5.1 and Python 2.6.2 on Windows XP SP2).

Thanks,
/-\



  
__
Get more done like never before with Yahoo!7 Mail.
Learn more: http://au.overview.mail.yahoo.com/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Calendar yr-mnth-day data to day since data

2009-10-03 Thread [email protected]
On Oct 3, 6:35 am, Piet van Oostrum  wrote:
> > "[email protected]"  (sc) wrote:
> >sc> Hi all,
> >sc> I have some calendar data in three arrays corresponding to yr, month,
> >sc> day that I would like to convert to day since data and be consistent
> >sc> with changes in leap year. I've included a sample of the data
> >sc> structures below.  Any suggestions???  Thanks in advance
> >sc> yr          mnth            day                 daySince
> >sc> 1970    1                       1                     1
> >sc> 1970    1                       15                  15
> >sc> 1970    1                       28                  28
> >sc> 1970    2                       1
> >sc> 32
> >sc> 1970    2                       27                  59
> >sc> 1970    3                       1
> >sc> 1970    3                       4
> >sc> 1970    3                       29
> >sc>     ...       ...                        ...
> >sc> 2008    1                       1
> >sc> 2008    1                       8
> >sc> 2008    1                       25
> >sc> 2008    2                       1
>
> Days since what? It appears here to be since 1969-12-31, or since
> 1970-1-1 but then starting with 1 instead of 0.
> And your 59 is wrong if the others are deemed to be correct.
>
> Depending on what you want you have to add 1 to the following solution
>
> import datetime
> startdate = datetime.date(1970, 1, 1)
> enddate = datetime.date(1970,3,1)
> timediff = enddate - startdate
> print timediff.days
>
> result: 59
> --
> Piet van Oostrum 
> WWW:http://pietvanoostrum.com/
> PGP key: [8DAE142BE17999C4]

Thanks ... This module does the trick nicely
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is pythonic version of scanf() or sscanf() planned?

2009-10-03 Thread Grant Edwards
On 2009-10-03, ryniek90  wrote:

> So, whether it is or has been planned the core Python
> implementation of *scanf()* ?

One of the fist things I remember being taught as a C progrmmer
was to never use scanf.  Programs that use scanf tend to fail
in rather spectacular ways when presented with simple typos and
other forms of unexpected input.  

Given the bad behavior and general fragility of scanf(), I
doubt there's much demand for something equally broken for
Python.

> Thanks for any helpful answers.

Not sure if mine was helpful...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Enormous Input and Output Test

2009-10-03 Thread n00m
On Oct 4, 2:29 am, Chris Rebert  wrote:

>
> That line is probably a Very Bad Idea (TM) as it reads the *entire*
> enormous file into memory *at once*. It would probably be much better
> to iterate over the file, thus only reading one individual line at a
> time. I'm betting the massive malloc()ing involved with .readlines()
> is a large part of the slowness.

Certainly not.
The culprit is line "a = map(noo, a)".
Without it execution time = 2.59s (I've just checked it).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Enormous Input and Output Test

2009-10-03 Thread n00m
PS
Time Limit for this problem = 20s
-- 
http://mail.python.org/mailman/listinfo/python-list


Identify runs in list

2009-10-03 Thread [email protected]
Hi all,

I have a data structure in a list as in: [0 0 0 3 0 5 0 0 0 0 1 0 4 0
5 0 0 7 0 0 0 0 0 12 0 0 4]

I would like to extract three list from this data:

1) runsOfZero: [3 4 5]
2) runsOfNonZero: [3 8 4]
3) SumOfRunsOfNonZero: [8 17 16]

Any suggestions would be appreciated
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Identify runs in list

2009-10-03 Thread Chris Rebert
On Sat, Oct 3, 2009 at 7:21 PM, [email protected]  wrote:
> Hi all,
>
> I have a data structure in a list as in: [0 0 0 3 0 5 0 0 0 0 1 0 4 0
> 5 0 0 7 0 0 0 0 0 12 0 0 4]
>
> I would like to extract three list from this data:
>
> 1) runsOfZero: [3 4 5]
> 2) runsOfNonZero: [3 8 4]
> 3) SumOfRunsOfNonZero: [8 17 16]
>
> Any suggestions would be appreciated

Since this sounds like homework, I won't give actual code, but here's
a gameplan:

1. Split the list into sublists based on where the runs of zeros stop and start.
2. Categorize the sublists and place them into lists-of-lists based on
whether they have nonzero entries. To do the categorization, you'll
have to iterate over the original list and track how many previous 0s
you've seen consecutively.
3. Use len() on the nonzero lists to get their length; puts the
results into a list (runsOfNonZero)
4. Use sum() on the nonzero lists to get their sums, and put the
results into another list (SumOfRunsOfNonZero)
5. Use len() on the all-zero lists to get their length and put the
results into a list (runsOfZero)

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


Re: Identify runs in list

2009-10-03 Thread [email protected]
On Oct 3, 10:36 pm, Chris Rebert  wrote:
> On Sat, Oct 3, 2009 at 7:21 PM, [email protected]  
> wrote:
> > Hi all,
>
> > I have a data structure in a list as in: [0 0 0 3 0 5 0 0 0 0 1 0 4 0
> > 5 0 0 7 0 0 0 0 0 12 0 0 4]
>
> > I would like to extract three list from this data:
>
> > 1) runsOfZero: [3 4 5]
> > 2) runsOfNonZero: [3 8 4]
> > 3) SumOfRunsOfNonZero: [8 17 16]
>
> > Any suggestions would be appreciated
>
> Since this sounds like homework, I won't give actual code, but here's
> a gameplan:
>
> 1. Split the list into sublists based on where the runs of zeros stop and 
> start.
> 2. Categorize the sublists and place them into lists-of-lists based on
> whether they have nonzero entries. To do the categorization, you'll
> have to iterate over the original list and track how many previous 0s
> you've seen consecutively.
> 3. Use len() on the nonzero lists to get their length; puts the
> results into a list (runsOfNonZero)
> 4. Use sum() on the nonzero lists to get their sums, and put the
> results into another list (SumOfRunsOfNonZero)
> 5. Use len() on the all-zero lists to get their length and put the
> results into a list (runsOfZero)
>
> Cheers,
> Chris
> --http://blog.rebertia.com

Thanks Chris, Not homework but self learning.  Also, forgot to mention
that I only count runs greater than 3 (zeros).  I will now look over
your suggestions. Thanks again
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Identify runs in list

2009-10-03 Thread Chris Rebert
On Sat, Oct 3, 2009 at 7:53 PM, [email protected]  wrote:
> On Oct 3, 10:36 pm, Chris Rebert  wrote:

>> Since this sounds like homework, I won't give actual code, but here's
>> a gameplan:
>>
>> 1. Split the list into sublists based on where the runs of zeros stop and 
>> start.
>> 2. Categorize the sublists and place them into lists-of-lists based on
>> whether they have nonzero entries.

>> To do the categorization, you'll
>> have to iterate over the original list and track how many previous 0s
>> you've seen consecutively.

Erm, s/categorization/splitting and move this sentence into point #1,
obviously. Mea culpa. :)

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


Re: The Python: Rag October issue available

2009-10-03 Thread TerryP
On Oct 3, 4:29 pm, Bernie  wrote:
> Hi, no -its just put on the website.  Unless there's a method you can
> suggest?

Not to butt in, but off the top of my head, you could probably set up
a mailing list and post the link to the file every cycle - simple but
effective.
-- 
http://mail.python.org/mailman/listinfo/python-list


creating class objects inside methods

2009-10-03 Thread horos11
All,

I've got a strange one..

I'm trying to create a class object inside another class object by
using the code template below (note.. this isn't the exact code.. I'm
having difficulty reproducing it without posting the whole thing)

Anyways, the upshot is that the first time the Myclass() constructor
is called, the __init__ function gets executed.. The second time, it
calls the '__call__' method (and dies, because I don't have a call
method defined).

To get around this, I've made __call__ a synonym of __init__, which is
a horrid hack, and doesn't help me much (since I don't have a good
idea what's going on).

So - anyone have an idea of what's going on here? It looks like the
second time, the Myclass() call is interpreted as a class instance,
not a  class object, but that seems odd to me.. Is this a python bug?
I'm seeing it in 2.6..If necessary, I can post the whole piece of
code..

Ed

class Myclass:

def __init__(self, b='default1', c='default2'):

self.b = b;
self.c = c;

def function(self):

 other = Myclass();
 return(other)

a = Myclass();

b = a.function()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Enormous Input and Output Test

2009-10-03 Thread alex23
On Oct 3, 11:54 pm, n00m  wrote:
> I need your help to understand howhttp://www.spoj.pl/problems/INOUTEST/
> can be passed in Python.
>
> My code for this is:
> ===
> import psyco
> psyco.full()
>
> import sys
>
> def noo(b):
>     b = b.split()
>     return str(int(b[0]) * int(b[1])) + '\n'
>
> def foo():
>     ##sys.stdin = open('D:/1583.txt', 'rt')
>     a = sys.stdin.readlines()
>     a = a[1:int(a[0]) + 1]
>     a = map(noo, a)
>     sys.stdout.writelines(a)
>
> foo()
> ===
>
> But it gets "Time Limit Exceeded" verdict.
> Any ideas?

map() is really at its most efficient when used with built-ins, not
user defined functions. In those cases, I believe you're better off
using a for-loop or list comprehension. Also: are you sure they
support psyco? It's not part of stdlib...

I thought something simpler might work:

import sys

sys.stdin.readline()

for line in sys.stdin.readlines():
nums = map(int, line.split())
print nums[0] * nums[1]

But although this processes 5.5MB < 2 secs on my PC (which meets the
2.5MB/sec criteria outlined in INTEST), I'm getting the same result
that you are.

Do you know how big the input data set actually is?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating class objects inside methods

2009-10-03 Thread Simon Forman
On Sat, Oct 3, 2009 at 11:32 PM, horos11  wrote:
> All,
>
> I've got a strange one..
>
> I'm trying to create a class object inside another class object by
> using the code template below (note.. this isn't the exact code.. I'm
> having difficulty reproducing it without posting the whole thing)
>
> Anyways, the upshot is that the first time the Myclass() constructor
> is called, the __init__ function gets executed.. The second time, it
> calls the '__call__' method (and dies, because I don't have a call
> method defined).
>
> To get around this, I've made __call__ a synonym of __init__, which is
> a horrid hack, and doesn't help me much (since I don't have a good
> idea what's going on).
>
> So - anyone have an idea of what's going on here? It looks like the
> second time, the Myclass() call is interpreted as a class instance,
> not a  class object, but that seems odd to me.. Is this a python bug?
> I'm seeing it in 2.6..If necessary, I can post the whole piece of
> code..
>
> Ed
>
> class Myclass:
>
>    def __init__(self, b='default1', c='default2'):
>
>        self.b = b;
>        self.c = c;
>
>    def function(self):
>
>         other = Myclass();
>         return(other)
>
> a = Myclass();
>
> b = a.function()
> --
> http://mail.python.org/mailman/listinfo/python-list
>


class Myclass:
def __init__(self, b='default1', c='default2'):
self.b = b
self.c = c

def function(self):
other = Myclass()
return other

a = Myclass()
b = a.function()


>>> a
<__main__.Myclass instance at 0x95cd3ec>
>>> b
<__main__.Myclass instance at 0x95cd5ac>


What's the problem?


(Also, you can leave out the ';' at the end of statements. And
'return' isn't a function, you can leave out the ()'s.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Enormous Input and Output Test

2009-10-03 Thread n00m
> Do you know how big the input data set actually is?

Of course, I don't know exact size of input.
It's several MBs, I guess. And mind the fact:
their testing machines are PIII (750MHz).
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Dictionary with Lists

2009-10-03 Thread John Nagle

Shaun wrote:

Hi,

I'm trying to create a dictionary with lists as the value for each
key.  I was looking for the most elegant way of doing it... 


   Try using a tuple, instead of a list, for each key.  Tuples
are immutable, so there's no issue about a key changing while
being used in a dictionary.

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


Re: Enormous Input and Output Test

2009-10-03 Thread alex23
On Oct 4, 1:58 pm, n00m  wrote:
> > Do you know how big the input data set actually is?
>
> Of course, I don't know exact size of input.
> It's several MBs, I guess. And mind the fact:
> their testing machines are PIII (750MHz).

Well, then, that's moved the problem from "challenging" to
"ludicrous". Being asked to conform to one constraint with no
knowledge of the others seems kind of ridiculous.

On my machine, the above code handles ~50MB in ~10sec.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Enormous Input and Output Test

2009-10-03 Thread n00m
> On my machine, the above code handles ~50MB in ~10sec.

Means their input > 40-50MB
2.
I just see: two guys did it in Python
and I feel myself curious "how on earth?".
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Identify runs in list

2009-10-03 Thread Paul Rubin
"[email protected]"  writes:
> Any suggestions would be appreciated

Look at the docs of the groupby function in the itertools module.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating class objects inside methods

2009-10-03 Thread horos11

> >>> a
>
> <__main__.Myclass instance at 0x95cd3ec b
>
> <__main__.Myclass instance at 0x95cd5ac>
>
> What's the problem?

Like I said, the code was a sample of what I was trying to do, not the
entire thing.. I just wanted to see if the metaphor was kosher.

It sounds to me from your answer that this is unexpected behavior, so
I'll go ahead and post the whole thing. My guess is that it is a
python bug..

Run it (a simple puzzle game solved by breadth first search), and the
first time state() is called inside the method, it calls __init__.
Second time, and therafter, it calls __call__. I've highlighted where
the code fails by putting a pdb.set_trace().

Anyways, I've got a workaround (simply pass in any new objects needed
from the caller), but it is truly annoying that python is either
misleading or broken in this way.

Attached find code, does not work vs. 2.6..


Ed



from collections import deque
import copy
import pdb

class state:

def default_board():

return [
  [ 1, 'x', 'x', 0 ],
  [ 2, 2,  3,  4 ],
  [ 5, 6,  6,  7 ],
  [ 5, 6,  6,  7 ],
  [ 8, 9, 10, 10 ],
  [ 0, 'x', 'x', 0 ]
]

def default_types():

return {
1  : [ 0, 0 ],
2  : [ 0, 0, 0, 1 ],
3  : [ 0, 0 ],
4  : [ 0, 0 ],
5  : [ 0, 0, 1, 0 ],
6  : [ 0, 0, 1, 0, 0, 1, 1, 1 ],
7  : [ 0, 0, 1, 0 ],
8  : [ 0, 0 ],
9  : [ 0, 0 ],
10 : [ 0, 0, 0, 1 ]
}

def default_moves():

return []

def print_move(self, moveno, move):
print str(moveno) + ": " + str(move) + "\n"


def __init__(self, _board=default_board(), _moves=default_moves(),
_types=default_types()):

self.board = _board
self.moves = _moves
self.types = _types

def possible_moves(self):

moves_so_far = set()
moves_so_far.add('x')
moves_so_far.add(0)
ret = []
for y_idx in range(0, len(self.board)):
for x_idx in range(0, len(self.board[y_idx])):

piece = self.board[y_idx][x_idx]

if not piece in moves_so_far:

moves = self.legal_moves(y_idx, x_idx)
moves_so_far.add(piece)

if moves:
ret.extend(moves)

return ret

def is_answer(self):

if self.board[5][3] == 1:
return True
else:
return False

def legal_moves(self, ycoord, xcoord):

ret = []
for dir in [ [ 0, 1 ], [ 0, -1 ], [ 1, 0 ], [ -1, 0 ] ]:
ret.extend(self.addmove(dir[0], dir[1], ycoord, xcoord))

return ret

def empty(self, type, ycoord, xcoord, pieceno):

for itr in range(0, len(type), 2):

yy = type[itr]
xx = type[itr+1]

if not (len(self.board) > (yy+ycoord) >= 0)  or not (len
(self.board[yy+ycoord]) > xx+xcoord >= 0):
return False

if not self.board[yy+ycoord][xx+xcoord] in [ 0, pieceno ]:
return False

return True

def addmove(self, ymult, xmult, ycoord, xcoord):

ret = []
pieceno = self.board[ycoord][xcoord]
type= self.types[pieceno]

if xmult != 0:
for xx in range(xcoord + xmult, -1 if xmult < 0 else 4, -1
if xmult < 0 else 1):
#   if xx == 0:
#   continue
if self.empty(type, ycoord, xx, pieceno):
ret.append(self.newmove(ycoord, xcoord, ycoord,
xx ))
else:
break

if ymult != 0:
for yy in range(ycoord + ymult, -1 if ymult < 0 else 6, -1
if ymult < 0 else 1):
#   if yy == 0:
#   continue
if self.empty(type, yy, xcoord, pieceno):
ret.append(self.newmove(ycoord, xcoord, yy,
xcoord))
else:
break

return ret

def newmove(self, fromy, fromx, toy, tox):

move = {
'fromx' : fromx,
'fromy' : fromy,
'toy'   : toy,
'tox'   : tox,
'piece' : self.board[fromy][fromx]
}

return move

def printout_path(self):

#   print self
pdb.set_trace()
answer = state()

moveno = 0
print "\n==\n"

for moveno in range(0, len(self.moves)):
move = self.moves[moveno]
self.print_move(moveno, move)
answer.apply_move(move)
answer.print_board()
print "\n==\n"

def print_board(self):

for xx in self.board:

print ": ".join([ "%2s" % str(ii) for ii in xx ])

def to_string(self):

return str(self.board)

  

Re: Enormous Input and Output Test

2009-10-03 Thread n00m

And *without* Psyco the above code gets TLE verdict...

A kind of mystery :(
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: creating class objects inside methods

2009-10-03 Thread horos11
Anyways, I see what's going on here:

With the line,

for state in curstate.next_states():
if not state.to_string() in seen_states:
dq.append(state)

Inadvertently using the name of a module as a variable seems to be
causing this.

In any case, this shouldn't cause issues with constructors, so I'd
call this a bug..

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


Re: Enormous Input and Output Test

2009-10-03 Thread John Yeung
On Oct 3, 11:58 pm, n00m  wrote:
> > Do you know how big the input data set actually is?
>
> Of course, I don't know exact size of input.
> It's several MBs, I guess. And mind the fact:
> their testing machines are PIII (750MHz).

You know the maximum size of the input, if you can trust the problem
definition.  The maximum number of lines in the input is 10**6 + 1.
The first line is at most 7 characters, plus EOL.  The subsequent
lines are at most 13 characters each, plus EOL.

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


Re: Threaded GUI slowing method execution?

2009-10-03 Thread Aaron Hoover


On Oct 2, 2009, at 1:18 PM, sturlamolden wrote:


On 2 Okt, 21:30, Dave Angel  wrote:


There could very well be multiprocess support in wxPython.  I'd check
there first, before re-inventing the wheel.


I don't think there is. But one can easily make a thread in the
subprocess that polls a pipe and calls wx.PostEvent or wx.CallLater
when something is received. I particularly like the Queue object in
multiprocessing for this.

One could also use win32api.SendMessage from pywin32 to send the event
from one process to another. The parent must know the hwnd of the
subprocess main wx.Frame. The subprocess gets that from calling the
GetHandle() method of its wx.Frame, and must post it back to the
parent, presumably via a pipe.




So, I managed to solve all of this ultimately by simply creating a new  
process for managing the serial connection with a TX and RX queue.  
Then, in the GUI, I created a thread that's responsible for reading  
from the RX queue and posting the appropriate events based on the data  
it pulls off of the queue.


It's plenty fast for 2KHz sampling rates, so I'm happy with it for  
now. But, I'm also grateful for all the suggestions for different  
approaches to the problem - they've really opened to what's possible  
with Python and the various libraries that enable interfacing with  
"lower" level languages like C.



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


Re: creating class objects inside methods

2009-10-03 Thread Carl Banks
On Oct 3, 10:12 pm, horos11  wrote:
> > >>> a
>
> > <__main__.Myclass instance at 0x95cd3ec b
>
> > <__main__.Myclass instance at 0x95cd5ac>
>
> > What's the problem?
>
> Like I said, the code was a sample of what I was trying to do, not the
> entire thing.. I just wanted to see if the metaphor was kosher.
>
> It sounds to me from your answer that this is unexpected behavior, so
> I'll go ahead and post the whole thing. My guess is that it is a
> python bug..

Sure.


> Run it (a simple puzzle game solved by breadth first search), and the
> first time state() is called inside the method, it calls __init__.
> Second time, and therafter, it calls __call__. I've highlighted where
> the code fails by putting a pdb.set_trace().
>
> Anyways, I've got a workaround (simply pass in any new objects needed
> from the caller), but it is truly annoying that python is either
> misleading or broken in this way.
>
> Attached find code, does not work vs. 2.6..
[snip]

I don't believe you couldn't reduce that to something simpler.

Anyway, I didn't bother reading all the code or running it, but I had
a suspicion what is was so I looked for it and saw what I was looking
for.

Here's you class statement:


> class state:


Now here's some code that appears much later at the top level:


>     else:
>         seen_states.add(curstate.to_string())
>         for state in curstate.next_states():
>             if not state.to_string() in seen_states:
>                 dq.append(state)
>
>                 print "Trying..\n"
>                 state.print_board()
>                 print "\n"


Whoops.  It looks like you rebound the global variable "state" to a
new value, which makes the class no longer accessible by that name.

My immediate suggestion is to change the name of the class, and to
capitalize the first letter.  "state" is a terrible name for a class.
Class instances, almost by default, maintain state, so naming a class
"state" says almost nothing about what the class is.  Something like
GameState or PlayerState would be better.

Here is some reading material that can help you adjust your coding
style to help avoid such errors in the future:

http://www.python.org/dev/peps/pep-0008


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


Re: creating class objects inside methods

2009-10-03 Thread Carl Banks
On Oct 3, 10:34 pm, horos11  wrote:
> Anyways, I see what's going on here:
>
> With the line,
>
> for state in curstate.next_states():
>     if not state.to_string() in seen_states:
>         dq.append(state)
>
> Inadvertently using the name of a module as a variable seems to be
> causing this.

Nope, unless by "module" you meant "class".


> In any case, this shouldn't cause issues with constructors, so I'd
> call this a bug..

It's not a bug.  In Python classes and global variables share the same
namespace.

Don't you think you should learn a bit more about how Python manages
objects and namespaces before going around calling things bugs?


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


Re: creating class objects inside methods

2009-10-03 Thread horos11
Carl,

Thanks for the info, but a couple of points:

1. it wasn't meant to be production code, simply a way to teach
python.

2. this should either be a compile time or a runtime error.

'Actions at a distance' like this are deadly both to productivity and
to correctness - not only is this a runtime error, it is a *silent*
runtime error. Any other language I know would catch this, whether it
be lua, java, or perl.

Saying that 'whoa, this coding error should be handled by naming
convention' may be the only practical way of getting around this
limitation, but it is a limitation nonetheless, and a pretty big one.

Ed



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


Re: Enormous Input and Output Test

2009-10-03 Thread n00m
And this code time limits (no matter with or without Psyco):

import psyco
psyco.full()

import sys

def foo():
##sys.stdin = open('D:/1583.txt', 'rt')
sys.stdin.readline()
while 1:
try:
x, y = sys.stdin.readline().split()
sys.stdout.write(str(int(x) * int(y)) + '\n')
except:
break

foo()
-- 
http://mail.python.org/mailman/listinfo/python-list


defaults for function arguments bound only once(??)

2009-10-03 Thread horos11
All,

Another one, this time a bit shorter.

It looks like defaults for arguments are only bound once, and every
subsequent call reuses the first reference created. Hence the
following will print '[10,2]' instead of the expected '[1,2]'.

Now my question - exactly why is 'default_me()' only called once, on
the construction of the first object? And what's the best way to get
around this if you want to have a default for an argument which so
happens to be a reference or a new object?

 code begins here ---

import copy
class A:

def default_me():
return [1,2]

def __init__(self, _arg=default_me()):
self.arg = _a


a = A()
a.arg[0] = 10
b = A()

print b.arg  # prints [10,2]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: defaults for function arguments bound only once(??)

2009-10-03 Thread Chris Rebert
On Sat, Oct 3, 2009 at 11:29 PM, horos11  wrote:
> All,
>
> Another one, this time a bit shorter.
>
> It looks like defaults for arguments are only bound once, and every
> subsequent call reuses the first reference created. Hence the
> following will print '[10,2]' instead of the expected '[1,2]'.
>
> Now my question - exactly why is 'default_me()' only called once, on
> the construction of the first object?

Actually, the single call happens at the "definition-time" of the
function. As for why, it's less magical than re-evaulating it on every
function call when that parameter is not supplied a value; trust me,
the issue has been argued to death.

> And what's the best way to get
> around this if you want to have a default for an argument which so
> happens to be a reference or a new object?

See http://docs.python.org/tutorial/controlflow.html#default-argument-values
Essentially, use None as the default value, then check for it and
assign the real default value in the function body.

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


Re: creating class objects inside methods

2009-10-03 Thread horos11

> It's not a bug.  In Python classes and global variables share the same
> namespace.
>
> Don't you think you should learn a bit more about how Python manages
> objects and namespaces before going around calling things bugs?
>
> Carl Banks

No, I don't think so..

Say you went to another country, where the people wore lead shoes,
hence not only going slower, but getting lead poisoning from time to
time.

Pointing out that shoes made of fabric might just be better should not
be heresy.

In this case, I think the overall goal was syntax simplicity, but it
sure as hell makes things confusing. No warning, or anything. The sane
behavior IMO would be to disallow the assignment unless put through a
special function, something like:

class(state) = ...

After all, python does have a precedent when you try to join, when:

":".join([1,2])

does not work because [1,2] is an array of ints, whereas

":" . join( str(x) for x in [1,2])

does.

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