Re: "indexed properties"...

2008-05-17 Thread pataphor
In article <[EMAIL PROTECTED]>, 
[EMAIL PROTECTED] says...

> >>   window.pos = (x,y)
> >>
> >> seems more natural than
> >>
> >>   window.SetPos(x,y);

Yes, and to assign a row in a matrix I'd also like to use either tuples 
or lists on the right side. 

>   def __add__(self, other):
> return Row([x + y for x, y in zip(self, other)])
> 
> worked as hoped if self is a Row and other is a
> Row _or_ a list.)
> 
> Anyway, my m.rows[j] can't be a list, because
> I want to be able to say things like
> m.rows[0] += c*m.rows[2]. Allowing 
> m.rows[j] = [1,2,3] seems convenient, while
> requiring m.rows[j] = Row([1,2,3]) fixes
> this problem. Hmm.

I'm not sure how far you'd go along with Gabriel's idea of decoupling 
views from data but I think there isn't even a need for using a matrix 
as the underlying data type. Why not use a flat list and compute matrix 
positions according to a row or column adressing scheme on the fly? 

P.

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


Re: help compiling Python on vs 2008!

2008-05-17 Thread Martin v. Löwis
> VS2005 seems to be officially supported.

That impression is incorrect. There is the PCbuild8 directory, but
it isn't maintained or tested in a systematic way. Users of it may
need to adjust it before it can do something useful.

Regards,
Martin
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python and Flaming Thunder

2008-05-17 Thread Lie
On May 16, 3:58 am, "[EMAIL PROTECTED]"
<[EMAIL PROTECTED]> wrote:
> On 15 mai, 19:30, Lie <[EMAIL PROTECTED]> wrote:
>
>
>
> > On May 15, 4:08 am, "[EMAIL PROTECTED]"
>
> > <[EMAIL PROTECTED]> wrote:
> > > On 14 mai, 08:08, Lie <[EMAIL PROTECTED]> wrote:
>
> > > > On May 14, 12:51 pm, Lie <[EMAIL PROTECTED]> wrote:
>
> > > > > And your 8 by 8 cross compiler doesn't impress me at all, they're all
> > > > > based on x86/IA-32 architecture which is quite similar, no PowerPC,
> > > > > SPARC, ARM, no other CISC or RISC architecture. And your compiler is a
> > > > > single language compiler instead of three dimensional compiler that
> > > > > GCC is (cross platform, cross target platform, cross language).
>
> > > > And to add, I also need to mention that Python doesn't need to be
> > > > compiled at all,
>
> > > No language needs compilation - it's an implementation problem, not a
> > > language problem. Now all the Python's implementations I know do use
> > > compilation (to byte-code).
> > > > its py and pyo file is architecture independent.
>
> > > True, but this is not strictly related to being compiled or not.
>
> > It's true, it's implementation problem whether a language is compiled
> > or not, but what I was emphasizing was that Python's code is
> > architecture independent at all stages (that is visible to the user
> > and the programmer), on the other hand, a compiled code is a compiled
> > code is a compiled code,
>
> Ever wondered what all these .pyc files were ?
>
> > it cannot be architecture independent without
> > packing multiple binaries in the same executable (like in Macintosh's
> > universal binary) or using an emulation (with huge overheads) or at
> > least using a compatibility layer (which doesn't always work) and all
> > this is done in the layer that is visible to user and programmer
> > (programmer having to compile against everything and user having to
> > download the correct executable) instead of being done in a platform
> > independent way that interpreted language like Python have.
>
> Python is not interpreted, because being interpreted is a feature of
> an implementation, not of a language. And so far, no known Python
> implementation is (strictly speaking) interpreted - they all compile
> to byte-code. "compiled" doesn't necessarily imply "compiled to
> platform native binary code", you know.

That's beside the point, the point is about platform independentness
of the .py file. When I call Python is interpreted, I meant that the
CPU doesn't directly interpret python codes (in most Python
implementation).

> Ok, this may look like a bit on the splitting hairs side. But may I
> remind you that to run ever a .pyc file, you do need to have the
> Python runtime (VM + quite a lot of libs) installed one way (normal
> install) or another (packed in something that looks like an ordinary
> "executable" - whatever this means for the target platform) ? OTHO,
> it's true that a .pyc file is platform-independant  - it just requires
> the correct versions of quite a lot of platform-dependant binaries.
> Wait... Isn't this code some kind of a "visible to the user and
> programmer" "compatibilty layer" ?

With the same bit on being on the splitting hair side: It's the same
with msvcrt in C or msvbvm60 in VB, programming language runtime is
invisible to the user, it's visible to the system administrator
(which, in many cases is also the user though). It is, however,
invisible to both the user and the programmers.
--
http://mail.python.org/mailman/listinfo/python-list


Re: r' question

2008-05-17 Thread Dick Moores

At 10:17 PM 5/17/2008, Gabriel Genellina wrote:

En Sat, 17 May 2008 23:37:16 -0300, Dick Moores <[EMAIL PROTECTED]> escribió:

> I have a text file of phone numbers, which I'd like to search with a regex.
>
> fstr = "\sjoe\s"
> regex = "^.*" + fstr + ".*$"
>
> fstr = "\sjoe\s"
> regex = "r'^.*" + fstr + ".*$'"

The r"..." is a signal to the parser - meaning 
"don't interpret the escape characters here". 
Note that the r is OUTSIDE the quotes. In your 
example, the escape characters are in fstr, so 
it should be written as r"\sjoe\s"


However, (please refer back to my original post) 
I want to keep the fstr, ultimately to be the 
string entered by the user who knows a bit about 
regex, but not how to use r'  ' . Or 
alternatively, not assume any knowledge of regex, 
but build in some options, such as ignoring/not 
ignoring case, searching on just a string, or on 
a word. So I want to know how to build the user's 
fstr into regex. I apologize for not making this clear.


Now, if you want "the lines that contain the 
word joe surrounded by space", just use:


import re
regex = r"\sjoe\s"
p = re.compile(regex, re.I)
f = open('phone.txt', 'r')
for line in f:
 m = p.search(line)
 if m:
 print m.group()
f.close()

A file is its own line iterator (no need for 
readlines). And since you're iterating over 
lines, the regex doesn't have to test for it ^ and $.


Yes, that works. Thanks.

Dick Moores 


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


Re: r' question

2008-05-17 Thread Gabriel Genellina
En Sat, 17 May 2008 23:37:16 -0300, Dick Moores <[EMAIL PROTECTED]> escribió:

> I have a text file of phone numbers, which I'd like to search with a regex.
>
> fstr = "\sjoe\s"
> regex = "^.*" + fstr + ".*$"
>
> fstr = "\sjoe\s"
> regex = "r'^.*" + fstr + ".*$'"

The r"..." is a signal to the parser - meaning "don't interpret the escape 
characters here". Note that the r is OUTSIDE the quotes. In your example, the 
escape characters are in fstr, so it should be written as r"\sjoe\s"

Now, if you want "the lines that contain the word joe surrounded by space", 
just use:

import re
regex = r"\sjoe\s"
p = re.compile(regex, re.I)
f = open('phone.txt', 'r')
for line in f:
 m = p.search(line)
 if m:
 print m.group()
f.close()

A file is its own line iterator (no need for readlines). And since you're 
iterating over lines, the regex doesn't have to test for it ^ and $.

-- 
Gabriel Genellina

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


Re: help compiling Python on vs 2008!

2008-05-17 Thread inhahe
"inhahe" <[EMAIL PROTECTED]> wrote in message news:...
> VS2005 seems to be officially supported.  Here's part of the readme file 
> in the PCBuild8 directory in the Python 2.5 source.
>
> "
> Building Python using VC++ 8.0
> -
> This directory is used to build Python for Win32 platforms, e.g. Windows
> 95, 98 and NT.  It requires Microsoft Visual C++ 8.0
> (a.k.a. Visual Studio 2005).  There are two Platforms defined, Win32
> and x64.
> (For other Windows platforms and compilers, see ../PC/readme.txt.)
> "
>
> Although I take your point about sharing resources.  I'm not sure I should 
> bother compiling 2.6, since I'll probably come across/already use modules 
> that I need that don't support 2.6 since it's so new.  So it would be nice 
> to know why following the instructions doesn't work.  Compilers never work 
> for me, though.. even when I follow instructions and they work for 
> everybody else.  It's like a schroedinbug - I just don't expect them to 
> work.  As a matter of principle, though, it /should/ work.. so it's gotta 
> be *somebody's* job to debug this! :P
>

Actually, I was under the impression that the socket module relied on one of 
the external libraries to work, since it didn't work when I compiled it. 
But reading the readme again I realized that the socketmodule is supposed to 
compile out of the box.  So I compiled Python again on VS2005 instead of 
2008 (the error I posted that didn't work on 2008 or 2005 was trying to 
compile one of the external libraries), and the socket module seems to work. 
The external libraries don't seem that important.  So it was partly my 
fault.. but still, the external libraries didn't compile like they said they 
would. =P



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


Python library for clustering from distance vector

2008-05-17 Thread Sengly
Dear all,

I am looking for a python library which can cluster similar objects
into their respective groups given their similarity score of each two
of them. I have searched the group but I couldn't find any helpful
information yet.

Any suggestion is appreciated. Also, if you know any specific group
that I should post this message, please also suggest.

Thank you.

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


Re: usage of python

2008-05-17 Thread Grant Edwards
On 2008-05-13, Rajarshi <[EMAIL PROTECTED]> wrote:

> Hi, I teach an introductory programming course in Python. As part of
> the introduction I'd like to highlight the usage of Python in
> industry. The idea is to show that there are big players using Python
> for a variety of tasks. Given that the students come from a variety of
> backgrounds and are not necessarily 'hackers', I'm trying to look for
> examples with a bit of wow-factor.
>
> Is there any list with people/groups/companies using Python for
> impressive things?

Have you heard of a company called Google?  They use Python a
lot.

Another company you may have heard of is IBM. I have a laptop
made by them.  It came with Python pre-installed because a
bunch of the system utilities are written in Python.  I think
I've heard that HP and Compaq do the same (I can't verify it
personally).

If you've heard of Linux, there's a company called RedHat that
wrote their system installer and packaging/system-admin stuff
in Python.

-- 
Grant

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


Re: Shelve or pickle module

2008-05-17 Thread Gabriel Genellina
En Sun, 18 May 2008 00:14:19 -0300, Guillaume Bog <[EMAIL PROTECTED]> escribió:

> I read and re-read "Python in a Nutshell" written by Alex Martelli,
> who knows what he is talking about. I'm a bit new to python and I'm
> going to start doing persistence side on a project. Martelli's book
> seems to tell me that I should use shelve module, but any code I
> browsed is using pickle instead. Is there any reason to prefer pickle
> over shelve?

A shelve is just a persistent dictionary that uses pickle to store the objects.
If you want to store one or a few objects, using pickle directly may be easier.
Any problem you may have with pickle (nonpickleable objects, security risks) 
will happen with shelve too.

-- 
Gabriel Genellina

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


Re: Sanitised Newsgroup Feeds?

2008-05-17 Thread Aahz
In article <[EMAIL PROTECTED]>,
Paddy  <[EMAIL PROTECTED]> wrote:
>
>Does anyone do a sanitised newsgroup feed? Something like what mail
>filters do for email?

My ISP (panix.com) is one of the remaining few that still honors netnews
cancels, which results in my seeing a cleaner feed than most people.
Stuff like Xah requires a killfile, though.
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

Need a book?  Use your library!
--
http://mail.python.org/mailman/listinfo/python-list


Re: [off-topic] Usenet

2008-05-17 Thread Aahz
In article <[EMAIL PROTECTED]>,
hdante  <[EMAIL PROTECTED]> wrote:
>
>How can I access Usenet without using Google Groups ? (my ISP doesn't
>have a NNTP server). Do you recommend doing so ?

If you'd like an ISP with a decent news server, I highly recommend
panix.com

>What's your prefered news reader ?

trn3.6, same as it's been for the last eighteen years.  ;-)
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

Need a book?  Use your library!
--
http://mail.python.org/mailman/listinfo/python-list


Re: usage of python

2008-05-17 Thread Aahz
In article <[EMAIL PROTECTED]>,
Rajarshi  <[EMAIL PROTECTED]> wrote:
>
>Is there any list with people/groups/companies using Python for
>impressive things?
>
>Any pointers would be appreciated

And one more:

http://www.pythonology.com/
-- 
Aahz ([EMAIL PROTECTED])   <*> http://www.pythoncraft.com/

Need a book?  Use your library!
--
http://mail.python.org/mailman/listinfo/python-list


Re: help compiling Python on vs 2008!

2008-05-17 Thread inhahe
VS2005 seems to be officially supported.  Here's part of the readme file in 
the PCBuild8 directory in the Python 2.5 source.

"
 Building Python using VC++ 8.0
-
This directory is used to build Python for Win32 platforms, e.g. Windows
95, 98 and NT.  It requires Microsoft Visual C++ 8.0
(a.k.a. Visual Studio 2005).  There are two Platforms defined, Win32
and x64.
(For other Windows platforms and compilers, see ../PC/readme.txt.)
"

Although I take your point about sharing resources.  I'm not sure I should 
bother compiling 2.6, since I'll probably come across/already use modules 
that I need that don't support 2.6 since it's so new.  So it would be nice 
to know why following the instructions doesn't work.  Compilers never work 
for me, though.. even when I follow instructions and they work for everybody 
else.  It's like a schroedinbug - I just don't expect them to work.  As a 
matter of principle, though, it /should/ work.. so it's gotta be 
*somebody's* job to debug this! :P

"Christian Heimes" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> Matthieu Brucher schrieb:
>> Hi,
>>
>> I did not manage to build extension with distutils with Python compiled 
>> with
>> VS different than 2003. The need for 2003 was hard-coded in distutils.
>> You can try building extensions with VS2008 with Scons. This is what I do 
>> a
>> lot, and everything works fine as long as the interface does not use
>> standard structures (like FILE, custom structures are fine) or objects
>> allocated in the extension is freed in the extension.
>
> Python 2.5 is compiled with VS 2003. Neither VS 2005 nor 2008 are
> officially supported.
>
> You can compile extensions with a different version of MS VC but it can
> get you in a lot of trouble. Every version of the VS Compiler uses its
> own C Runtime Library (MSVCRT). You can't share some resources like
> allocated memory and FILE* objects between MSVCRTs.
>
> Christian
> 


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


Re: can't delete from a dictionary in a loop

2008-05-17 Thread Scott David Daniels

Eduardo O. Padoan wrote:

On Fri, May 16, 2008 at 6:40 PM, Gary Herron <[EMAIL PROTECTED]> wrote:

[EMAIL PROTECTED] wrote:

On 16 mai, 23:28, Hans Nowak <[EMAIL PROTECTED]> wrote:

Dan Upton wrote: 

  <>

...to solve the immediate problem:
  for pid in procs_dict.keys():

And then, in Python3, keys() produces something else altogether (call a view
of the dictionary) which would provoke the same problem, so yet another
solution would have to be found then.

In Python 3.0, list(procs_dict.keys()) would have the same effect.

Or (simpler in either 3.0 or 2.X):
for pid in list(procs_dict):
...

--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: Arch problems--how do I build PIL to be 64 bit so it plays nicely on OS X?

2008-05-17 Thread Graham Dumpleton
On May 17, 6:16 am, lampshade <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm using python + django to do some web design and I would really
> like to use the python image library as part of this.  There seems to
> be a problem, however, with apache andmod_pythonbeing 64 bit while
> my python image library(PIL) is only 32 bit.
>
> Does anyone have experience with building the correct architectures so
> that OS X 10.5 plays nicely?  I think, when it comes down to it, I
> just need PIL at this point to be x86_64 so that it plays with apache
> andmod_python.
>
> Any advice, hand-holding, or sage wisdom?

See:

  http://code.google.com/p/modwsgi/wiki/InstallationOnMacOSX

and also the document it references:

  
http://developer.apple.com/releasenotes/OpenSource/PerlExtensionsRelNotes/index.html

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


Re: morning in Python

2008-05-17 Thread castironpi
On May 17, 9:22 pm, castironpi <[EMAIL PROTECTED]> wrote:
> On May 17, 5:35 am, Ivan Illarionov <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On Sat, 17 May 2008 02:57:08 -0700, castironpi wrote:
> > > Full day later, I think it, to emphasize state, would prioritize
> > > context.  The reason for the huge ramble was, believe it or not,
> > > namespace conflict... as though any other states around here might nose
> > > in.  And thanks to 'inhahe' for coming back with the question.  ...Which
> > > would explain next move to 'prioritize context'. Context is a high
> > > priority for people.
>
> > > I'm proposing to start on one on a computer.  The first things a
> > > computer 'knows' are the boot, keyboard, & mouse.  Yet on another scale,
> > > the first things it knows are BIOS, file system, and OS.  On still
> > > another, the only thing it knows are interruptions.  Knowledge is
> > > important to context.  (The scales are ram on disk on, ram on disk off,
> > > and ram off, which may tell of the currency they and their power are
> > > bought with.  Thence, we should be getting different values for lengths
> > > of time.)
>
> > > (Furthermore, we're all on different longitudes -and- latitudes.)
>
> > > Context comes from motion, perception, and composite perception
> > > (reperception e.a.o. memory).  There is some reason to believe that
> > > motion and sight are different senses, perhaps so with stationary sound
> > > (gatcha) and mobile sound too.  Do you go deaf of a tone after prolonged
> > > duration?  That makes computers valuable commodities*: they have a
> > > symbolic interface, which no other unlive objects have.  They have both
> > > mouse and keyboard.
>
> > > *I'm sure there is a precision to wants: what magnitude of what types of
> > > action a person wants from a day and for a time-- what energy states
> > > they go to and from (note phone on come to and come from.)
>
> > > Therefore, context should originate in mouse and keyboard.
>
> > > Humans have symbolic know-how: knowledge of how to convey intent
> > > digitally, though it may be there is no interpolation of 'intent per
> > > mouse-or-key', even though people are prone to empathize with faces.
> > > However, if you start with a 'me' and a 'no', you can get pretty
> > > logical.
>
> > > Intent per mouse-and-key isn't necessarily scalar, three-dimensional, or
> > > rationally dimensional (?), though they do have magnitudes per mass and
> > > volume.  The contingent of 'rationally dimensional' is having or
> > > beknowing/benouncing an orthonormal basis.  Incidentally, '''orthography
> > > of a language specifies the correct way of using a specific writing
> > > system to write the language. .. Orthography is derived from Greek ὀρθός
> > > orthós ("correct") and γράφειν gráphein ("to write").''' - wikipedia.
>
> > > Further incidentally, context and state may have more important in
> > > common than priority and price: privacy and safety are involved ex
> > > hypothesi.  Incidentally = ...
>
> > > It is not clear that the first (cheapest best) human-computer language
> > > is a computer language, though if two were orthonormal in comparison to
> > > life, Python's fine.  Not my first.
>
> > > In privacy concerns, it is not clear that duals aren't primitives to
> > > humans.  What's a brain primitive?  Lol: what is a primitive brain?
>
> > > On May 16, 10:58 am, "inhahe" <[EMAIL PROTECTED]> wrote:
> > >> I'm not an expert in this but what does it mean to emphasize state?  It
> > >> seems the opposite of that would be a) functional programming, and b)
> > >> passing parameters instead of using global or relatively local
> > >> variables. And maybe c) coroutines (generators as implemented in
> > >> Python), although perhaps coroutines could be said to emphasize state
> > >> inasmuch as they go out of their way to capture, objectify and reuse it
> > >> (Stackless' microthreads, even moreso).  And Python seems to be
> > >> well-noted for implementing some functional programming methodology,
> > >> and as for passing parameters it's just as object-oriented as the rest
> > >> of them.
>
> > >> But as I said, I'm not an expert, so let me know if I've gone astray..
>
> > >> > I have a proposition to ask you all: Python emphasizes state.  Is it
> > >> > true?- Hide quoted text -
>
> > >> - Show quoted text -
>
> > Castironpi,
>
> > I love you! You remind me of all the kittens and puuppies I had when I
> > was a child.
>
> > I #define this. Hope your database could give us something funny again.
>
> > -- Ivan- Hide quoted text -
>
> > - Show quoted text -
>
> I have to talk about crossing threats.  But talking about threads is
> like talking, and people talk about food.  What are some threats to be
> scared of?  What about threads to be thunder?- Hide quoted text -
>
> - Show quoted text -

I have to question sincerity, which is disappointing for a night.
Does anyone live in a city?
--
http://mail.python.org/mailman/listinfo/python-list

Re: write to specific line in file?

2008-05-17 Thread Gabriel Genellina
En Sat, 17 May 2008 21:58:50 -0300, castironpi <[EMAIL PROTECTED]> escribió:

> I am still interested in this one, but doubt if anyone still reading.

I've not filtered you out *yet* - but that may happen very soon.

-- 
Gabriel Genellina

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


Re: Using Python for programming algorithms

2008-05-17 Thread inhahe
what little I know:

The numbers I heard are that Python is 10-100 times slower than C.  So use 
Python if you can wait 10-100 times longer.  Although it won't really be 
that slow using numpy and/or psyco.

Python seems to have a really extensive reportoire of modules available for 
it.  Although I don't know about things in the math field.  If what you want 
is obscure enough, then you might find it for C/C++ and not Python.  You 
might find out in a few seconds by googling.

The advantage to Python (other than production time), is that it would be a 
lot simpler and more readable simply as a syntax for conveying algorithms. 
It would be like pseudo-code... but runnable. ;)





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


Re: Using Python for programming algorithms

2008-05-17 Thread Henrique Dante de Almeida
On May 17, 7:32 pm, Vicent Giner <[EMAIL PROTECTED]> wrote:
> Hello.
>
> I am new to Python. It seems a very interesting language to me. Its
> simplicity is very attractive.
>
> However, it is usually said that Python is not a compiled but
> interpreted programming language —I mean, it is not like C, in that
> sense.
>
> I am working on my PhD Thesis, which is about Operations Research,
> heuristic algorithms, etc., and I am considering the possibility of
> programming all my algorithms in Python.
>
> The usual alternative is C, but I like Python more.
>
> The main drawbacks I see to using Python are these:
>
> * As far as I understand, the fact that Python is not a compiled
> language makes it slower than C, when performing huge amounts of
> computations within an algorithm or program.
>
> * I don't know how likely it is to find libraries in Python related to
> my research field.
>
> * I know Python is a "serious" and mature programming language, of
> course. But I do not know if it is seen as "just funny" in a research
> context. Is Python considered as a good programming language for
> implementing Operations Research algorithms, such as heuristics and
> other soft-computing algorithms?
>
> Maybe this is not the right forum, but maybe you can give me some
> hints or tips...
>
> Thank you in advance.

 I guess that python is not a good language for that. Algorithms
implemented in plain python are many times slower than C ones
(hundreds ?). In practice, algorithms are written in C and wrapped in
python. I have near zero experience in operations research, but once I
looked for linear programming toolkits for python and IIRC, I only
could find a trivial wrapper for glpk (called pulp).

 My opinion: choose compiled or byte compiled languages. Choose the
language paradigm that best suit the algorithms.
--
http://mail.python.org/mailman/listinfo/python-list


Shelve or pickle module

2008-05-17 Thread Guillaume Bog
Hello,

I read and re-read "Python in a Nutshell" written by Alex Martelli,
who knows what he is talking about. I'm a bit new to python and I'm
going to start doing persistence side on a project. Martelli's book
seems to tell me that I should use shelve module, but any code I
browsed is using pickle instead. Is there any reason to prefer pickle
over shelve?

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


r' question

2008-05-17 Thread Dick Moores


Win XP, Python 2.5.1
I have a text file of phone numbers, which I'd like to search with a
regex. 
This script works fine:
import re
fstr = "\sjoe\s"
regex = "^.*" + fstr + ".*$"
p = re.compile(regex, re.I)
f = open('phone.txt', 'r')
for line in f.readlines():
    if p.search(line):
    print
p.search(line).group()
f.close()
But this one, with a different 3rd line, doesn't:
import re
fstr = "\sjoe\s"
regex = "r'^.*" + fstr + ".*$'"
p = re.compile(regex, re.I)
f = open('phone.txt', 'r')
for line in f.readlines():
    if p.search(line):
    print
p.search(line).group()
f.close()
Is there no way to get it to work?
Thanks,
Dick Moores


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

Re: morning in Python

2008-05-17 Thread castironpi
On May 17, 5:35 am, Ivan Illarionov <[EMAIL PROTECTED]> wrote:
> On Sat, 17 May 2008 02:57:08 -0700, castironpi wrote:
> > Full day later, I think it, to emphasize state, would prioritize
> > context.  The reason for the huge ramble was, believe it or not,
> > namespace conflict... as though any other states around here might nose
> > in.  And thanks to 'inhahe' for coming back with the question.  ...Which
> > would explain next move to 'prioritize context'. Context is a high
> > priority for people.
>
> > I'm proposing to start on one on a computer.  The first things a
> > computer 'knows' are the boot, keyboard, & mouse.  Yet on another scale,
> > the first things it knows are BIOS, file system, and OS.  On still
> > another, the only thing it knows are interruptions.  Knowledge is
> > important to context.  (The scales are ram on disk on, ram on disk off,
> > and ram off, which may tell of the currency they and their power are
> > bought with.  Thence, we should be getting different values for lengths
> > of time.)
>
> > (Furthermore, we're all on different longitudes -and- latitudes.)
>
> > Context comes from motion, perception, and composite perception
> > (reperception e.a.o. memory).  There is some reason to believe that
> > motion and sight are different senses, perhaps so with stationary sound
> > (gatcha) and mobile sound too.  Do you go deaf of a tone after prolonged
> > duration?  That makes computers valuable commodities*: they have a
> > symbolic interface, which no other unlive objects have.  They have both
> > mouse and keyboard.
>
> > *I'm sure there is a precision to wants: what magnitude of what types of
> > action a person wants from a day and for a time-- what energy states
> > they go to and from (note phone on come to and come from.)
>
> > Therefore, context should originate in mouse and keyboard.
>
> > Humans have symbolic know-how: knowledge of how to convey intent
> > digitally, though it may be there is no interpolation of 'intent per
> > mouse-or-key', even though people are prone to empathize with faces.
> > However, if you start with a 'me' and a 'no', you can get pretty
> > logical.
>
> > Intent per mouse-and-key isn't necessarily scalar, three-dimensional, or
> > rationally dimensional (?), though they do have magnitudes per mass and
> > volume.  The contingent of 'rationally dimensional' is having or
> > beknowing/benouncing an orthonormal basis.  Incidentally, '''orthography
> > of a language specifies the correct way of using a specific writing
> > system to write the language. .. Orthography is derived from Greek ὀρθός
> > orthós ("correct") and γράφειν gráphein ("to write").''' - wikipedia.
>
> > Further incidentally, context and state may have more important in
> > common than priority and price: privacy and safety are involved ex
> > hypothesi.  Incidentally = ...
>
> > It is not clear that the first (cheapest best) human-computer language
> > is a computer language, though if two were orthonormal in comparison to
> > life, Python's fine.  Not my first.
>
> > In privacy concerns, it is not clear that duals aren't primitives to
> > humans.  What's a brain primitive?  Lol: what is a primitive brain?
>
> > On May 16, 10:58 am, "inhahe" <[EMAIL PROTECTED]> wrote:
> >> I'm not an expert in this but what does it mean to emphasize state?  It
> >> seems the opposite of that would be a) functional programming, and b)
> >> passing parameters instead of using global or relatively local
> >> variables. And maybe c) coroutines (generators as implemented in
> >> Python), although perhaps coroutines could be said to emphasize state
> >> inasmuch as they go out of their way to capture, objectify and reuse it
> >> (Stackless' microthreads, even moreso).  And Python seems to be
> >> well-noted for implementing some functional programming methodology,
> >> and as for passing parameters it's just as object-oriented as the rest
> >> of them.
>
> >> But as I said, I'm not an expert, so let me know if I've gone astray..
>
> >> > I have a proposition to ask you all: Python emphasizes state.  Is it
> >> > true?- Hide quoted text -
>
> >> - Show quoted text -
>
> Castironpi,
>
> I love you! You remind me of all the kittens and puuppies I had when I
> was a child.
>
> I #define this. Hope your database could give us something funny again.
>
> -- Ivan- Hide quoted text -
>
> - Show quoted text -

I have to talk about crossing threats.  But talking about threads is
like talking, and people talk about food.  What are some threats to be
scared of?  What about threads to be thunder?
--
http://mail.python.org/mailman/listinfo/python-list

RE: numpy.frombuffer != unpack() ??

2008-05-17 Thread Marlin Rowley
Gary,
 
That works.  And it's better than my solution:
 
#a = numpy.rollaxis(a,2,0)
#a = numpy.rollaxis(a,0,1)
#a = numpy.rollaxis(a,1,0)
 
Thanks!
 
-M



> Date: Sat, 17 May 2008 17:18:39 -0700> From: [EMAIL PROTECTED]> To: [EMAIL 
> PROTECTED]> CC: python-list@python.org> Subject: Re: numpy.frombuffer != 
> unpack() ??> > Marlin Rowley wrote:> > Actually in my traversal of the 
> never-ending maze of understanding > > arrays in python, I stumbled that the 
> data in the wrong sequence.> >> > Let's get back to this transpose() deal, 
> say we have these values as > > integers (representing rgba again):> >> > 
> [[[0,1,2,3]> > [4,5,6,7]> > [8,9,10,11]> > [12,13,14,15]]> >> > 
> [[16,17,18,19]> > [20,21,22,23]> > [24,25,26,27]> > [28,29,30,31]]]> >> > Now 
> if I do this: transpose((2,0,1)), I get this:> >> > [[[0,4,8,12] 
> [16,20,24,28]]> > [[1,5,9,13] [17,21,25,29]]> > [[2,6,10,14][18,22,26,30]]> > 
> [[3,7,11,15][19,23,27,31]]]> >> > This is NOT what I want. I want the new 
> array to be:> >> > [0,4,8,12][1,5,9,13]> > [2,6,10,14][3,7,11,15]> > 
> [16,20,24,28][17,21,25,29]> > [18,22,26,30][19,23,27,31]> >> > How do I do 
> this?> > That's s little ambiguous, but one of the following two > 
> transpose-reshape-print's might be what you want.> > Gary Herron> > > import 
> numpy> > a = numpy.array([[[0,1,2,3],> [4,5,6,7],> [8,9,10,11],> 
> [12,13,14,15]],> > [[16,17,18,19],> [20,21,22,23],> [24,25,26,27],> 
> [28,29,30,31]]])> > > print numpy.reshape(a.transpose(0,2,1), (8,4))> print 
> numpy.reshape(a.transpose(0,2,1), (4,2,4))> > output is:> > [[ 0 4 8 12]> [ 1 
> 5 9 13]> [ 2 6 10 14]> [ 3 7 11 15]> [16 20 24 28]> [17 21 25 29]> [18 22 26 
> 30]> [19 23 27 31]]> > and> > [[[ 0 4 8 12]> [ 1 5 9 13]]> > [[ 2 6 10 14]> [ 
> 3 7 11 15]]> > [[16 20 24 28]> [17 21 25 29]]> > [[18 22 26 30]> [19 23 27 
> 31]]]> > > > >> > -M> >> >> >> >> >> > 
> > >> 
> > > Date: Sat, 17 May 2008 08:58:08 -0700> > > From: [EMAIL PROTECTED]> > > 
> To: [EMAIL PROTECTED]> > > CC: python-list@python.org> > > Subject: Re: 
> numpy.frombuffer != unpack() ??> > >> > > Marlin Rowley wrote:> > > >> > > > 
> Very cool.> > > >> > > > > > a = ([''],[''])> 
> > > > a represents a tile with height of 2 and width of 4 with 4 bits/pixel> 
> > > > for each color.> > > >> > > > > >>> b = 
> numpy.frombuffer(''.join(sum(a,[])),dtype='S1')> > > > this seperates the 
> stream into individual values - Check> > > >> > > > > >>> b.shape=(2,4,4)> > 
> > >> > > > This reshapes the array so that b.shape=(height,width,#bits/pixel) 
> > > - Check> > > >> > > > >>> c = b.transpose((2,0,1))> > > >> > > > What 
> does the (2,0,1) represent in terms of width and height and> > > > number of 
> bytes?> > >> > > The (2,0,1) tells how to exchange the axes. For instance in 
> a 2D array,> > > a normal transpose exchanges rows and columns. It will 
> change a (a by> > > b) sized array into a (b by a) sized array. This would be 
> equivalent to> > > the more saying interchange axes 0,1 to the new order of 
> 1,0.> > >> > > In numpy with higher dimension arrays, the default transpose 
> just> > > exchanges the first two axes, and the full transpose allows you to> 
> > > specify exactly the new ordering of the exes.> > >> > > So 
> transpose((2,0,1)) means take axes (0,1,2) to the new order> > > (2,1,0). In 
> terms of sizes, an (a by b by c) sized array will end being> > > of size (c 
> by a by b) in size.> > >> > > In terms of implementation, there may not be 
> *any* data re-arrangement> > > in a transpose. The only thing that needs 
> changing is how the indices> > > are converted to an actual machine address 
> of an indexed item. The> > > documentation notes this by saying transpose 
> returns a "new view" of > > the> > > array. This explains why I copied the 
> array before extracting bytes> > > out of it -- you really do need the 
> elements in the new order for the> > > next operation.> > >> > > Gary Herron> 
> > >> > > >> > > >> > > >> > > >> > > >> > > > > > 
> > > > 
> >> > > > > Date: Fri, 16 May 2008 17:08:20 -0700> > > > > From: [EMAIL 
> PROTECTED]> > > > > To: [EMAIL PROTECTED]; python-list@python.org> > > > > 
> Subject: Re: numpy.frombuffer != unpack() ??> > > > >> > > > > Marlin Rowley 
> wrote:> > > > > > All:> > > > > >> > > > > > Say I have an array:> > > > > >> 
> > > > > > a = ([''],[''])> > > > > >> > > > > 
> > How do I make it so that I now have:> > > > > >> > > > > > starting with 
> first element (a[0])> > > > > > new_arr[0] = 'r'> > > > > > new_arr[1] = 'g'> 
> > > > > > new_arr[2] = 'b'> > > > > > new_arr[3] = 'a'> > > > > > new_arr[4] 
> = 'r'> > > > > > .> > > > > >> > > > > > continuing "through" a[1] with 
> the same new_arr> > > > > > new_arr[N] = 'r'> > > > > > new_arr[N+1] = 'g'> > 
> > > > > > > > > > >> > > > > > 

Re: smtplib "authentication required" error

2008-05-17 Thread 7stud
On May 17, 12:18 pm, cher <[EMAIL PROTECTED]> wrote:
> Hey,
>
> Don't know if you can help, but I'm trying to import archived messages
> from Thunderbird into my gmail account and keep getting these types of
> errors.  I am using the GML program to help incorporate, but the SMTP
> server is not being recognized by gmail.  Am I doing something wrong?  
> Anything else I can try?
> SMTP I've tried:
>     smtp.gmail.com
>     gsmtp56.google.com
>
> Error Val : (530, '5.7.0 Must issue a STARTTLS command first.
> m29sm9416768poh.4',
> *** 76 ERROR SENDING MESSAGE FROM: [EMAIL PROTECTED]
> *** UNABLE TO CONNECT TO SERVER OR SEND MESSAGE. ERROR FOLLOWS.
> Error Type: smtplib.SMTPSenderRefused
> Error Val : (530, '5.7.0 Must issue a STARTTLS command first.
> n22sm9448670pof.3', '[EMAIL PROTECTED]')
>
> Thanks,
> Cher

Can you get the following program to work?


#Uses gmail to send an email to someone

import smtplib

sender = "Tom"
to = "Sally"
subject = "Test smtplib"

headers = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n" % (sender, to,
subject)
msg = headers + "Hello.  How are you?"

mailserver = smtplib.SMTP("smtp.gmail.com", 587)
mailserver.ehlo()
mailserver.starttls()
mailserver.ehlo()
mailserver.login("your_gmail_email_address", "your_gmail_password")
mailserver.sendmail("your_gmail_email_addres",
"a_destination_email_address", msg)
mailserver.close()

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


Re: Using Python for programming algorithms

2008-05-17 Thread sturlamolden
On May 18, 12:32 am, Vicent Giner <[EMAIL PROTECTED]> wrote:

> * As far as I understand, the fact that Python is not a compiled
> language makes it slower than C, when performing huge amounts of
> computations within an algorithm or program.

First of all: whatever you do, use NumPy for all numerical work (and
possibly Scipy). Remember that Python with NumPy tend to be faster
than Matlab. Anything that applies to Matlab regarding vectorization
for speed also applies to NumPy.

If your program runs too slowly, try to use the psyco jit compiler
first. If that doesn't help, try one of the following:

- use Cython or Pyrex and compile your (modified) Python code
- inline C++ using scipy.weave
- write a function in C and call it using ctypes
- write a function in Fortran and make it callable from Python using
f2py

Usually, only small bottlenecks matter when it comes to overall
performance. It is also notoriously difficult to guess where they are.
Therefore: write everything in Python first, then profile your code to
identify bottlenecks. Only important bottlenecks need to be translated
to Pyrex, C or Fortran.

http://www.scipy.org/PerformancePython


> * I know Python is a "serious" and mature programming language, of
> course. But I do not know if it is seen as "just funny" in a research
> context.

Google NumPy, SciPy, Matplolib and Sage.

NASA uses Python to process image data from the Hubble telescope.





> Thank you in advance.

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


Re: Malloc error

2008-05-17 Thread mankoff
On May 16, 9:16 pm, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Fri, 16 May 2008 16:08:41 -0300, mankoff <[EMAIL PROTECTED]> escribió:
>
> > I'm brand spanking new to Python. Never coded in it, but I'm
> > comfortable coding in general in other languages.
>
> Welcome...!
>
> > I'm Trying to get
> > some legacy code to run on a newer machine. I don't have access to the
> > legacy box for debugging.
>
> > I'm getting the following error:
>
> > Python(36979) malloc: *** error for object 0x88374: Non-aligned
> > pointer being freed
> > *** set a breakpoint in malloc_error_break to debug
>
> Ouch. I'd like to think that the error doesn't come from Python itself but  
>  from some compiled C extension. Are you using any?
> I'd try with the same Python version the legacy box had, if it was older  
> than 2.5.2
>
> --
> Gabriel Genellina

You're right. I was using some C extensions. The bug was there. I've
fixed it and everything appears to be working.

Thanks,

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


Re: addendum Re: working with images (PIL ?)

2008-05-17 Thread Ken Starks
As others have said, PIL has the 'histogram' method to do most of the 
work. However, as histogram works on each band separately, you have

a bit of preliminary programming first to combine them.

The ImageChops darker method is one easy-to-understand way (done twice),
but there are lots of alternatives, I am sure.


# 

import Image
import ImageChops

Im = Image.open("server\\vol\\temp\\image.jpg")
R,G,B = Im.split()

Result=ImageChops.darker(R,G)
Result=ImageChops.darker(Result,B)

WhiteArea=Result.histogram()[0]
TotalArea=Im.size[0] * Im.size[1]
PercentageWhite = (WhiteArea * 100.0)/TotalArea





Poppy wrote:
I've put together some code to demonstrate what my goal is though looping 
pixel by pixel it's rather slow.


import Image

def check_whitespace():
im = Image.open("server\\vol\\temp\\image.jpg")

size = im.size

i = 0
whitePixCount = 0
while i in range(size[1]):
j = 0
while j in range(size[0]):
p1 = im.getpixel((j,i))
if p1 == (255, 255, 255):
whitePixCount = whitePixCount + 1
if whitePixCount >= 492804:  ## ((image dimensions 1404 x 
1404) / 4) 25%

return "image no good"
j = j + 1
i = i + 1

print whitePixCount

return "image is good"

print check_whitespace()


"Poppy" <[EMAIL PROTECTED]> wrote in message news:...
I need to write a program to examine images (JPG) and determine how much 
area is whitespace. We need to throw a returned image out if too much of it 
is whitespace from the dataset we're working with. I've been examining the 
Python Image Library and can not determine if it offers the needed 
functionality. Does anyone have suggestions of other image libraries I 
should be looking at it, or if PIL can do what I need?






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


Re: multiple databases, what's the best interface ?

2008-05-17 Thread alex23
On May 18, 4:54 am, Stef Mientki <[EMAIL PROTECTED]> wrote:
> What's the best interface so I can use the same program for all databases,
> and just have to change the database name, if I want to use another
> database ?

I really like sqlalchemy: http://www.sqlalchemy.org/features.html

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


Re: comparison of files using set function

2008-05-17 Thread Kam-Hung Soh
On Sun, 18 May 2008 00:47:55 +1000, Beema shafreen  
<[EMAIL PROTECTED]> wrote:



I have files with two column, column 1 is with id and column 2 is with
data(sequence)
My goal is to create a table in such a way, the column one of the table
should have all the id from the files and next column will be have the
respective seq of the file1 with correlation to the id and the third  
column

will be sequence information of the next file with respective to the id
original files look like this

45ytut
46erete
37   dfasf
45  dassdsd


and so on  for all the 10 files that is it has two column as mentioned
above.

The output should look like this:

Idfile1  file2 file3 file4   file5
43ytuhytuh ytuhytuhytuh
46   erteee   rty   ryyy  ertyu
47   yutiorrreeerr



The goal is if the pick all the common id in the files and with their
respective information in the adjacent rows.
the various conditons ca also prevails
1) common id present in all the files, which have same information
2)common id present in all the files, which donot have same information
3) common id may not be present in all the files

But the goal is exactly find the common id in all the files and add their
corresponding information in the file to the table as per the view
 my script :
def file1_search(*files1):
for file1 in files1:
gi1_lis = []
fh = open(file1,'r')
for line in fh.readlines():
data1 = line.strip().split('\t')
gi1 = data1[0].strip()
seq1 = data1[1].strip()
gi1_lis.append(gi1)
return gi1_lis
def file2_search(**files2):
for file2 in files2:
for file in files2[file2]:
gi2_lis = []
fh1 = open(file,'r')
for line1 in fh1.readlines():
data2 = line1.strip().split('\t')
gi2 = data2[0].strip()
seq2 = data2[1].strip()
gi2_lis.append(gi2)

return gi2_lis
def set_compare(data1,data2,*files1,**files2):
A = set(data1)
B = set(data2)
I = A&B # common between thesetwo sets

D = A-B #57 is the len of D
C = B-A #176 is  the len of c
#print len(C)
 #   print len(D)
for file1 in files1:
for gi in D:
fh = open(file1,'r')
for line in fh.readlines():
data1 = line.strip().split('\t')
gi1 = data1[0].strip()
seq1 = data1[1].strip()
if gi == gi1:
#print line.strip()
pass

for file2 in files2:
for file in files2[file2]:
for gi in C:
fh1 = open(file,'r')
for line1 in fh1.readlines():
data2 = line1.strip().split('\t')
gi2 = data2[0].strip()
seq2 = data2[1].strip()
if gi == gi2:
   # print line1.strip()
pass
if __name__ == "__main__":
files1 = ["Fr20.txt",\
  "Fr22.txt",\
  "Fr24.txt",\
  "Fr60.txt",\
  "Fr62.txt"]
files2 = {"data":["Fr64.txt",\
  "Fr66.txt",\
  "Fr68.txt",\
  "Fr70.txt",\
  "Fr72.txt"]}
data1 = file1_search(*files1)

"""113 is the total number of gi"""
data2 = file2_search(**files2)
#for j in data2:
 #   print j
"""232 is the total number of gi found"""
result = set_compare(data1,data2,*files1,**files2)

 It doesnot work fine... some body please suggest me the way i can  
proceed .

Thanks a lot



1. Test with a small number of short files with a clear idea of the  
expected result.
2. Use better variable names.  Names such as file1_search, file2_search,  
gi, gi2, A, B, C and D make it nearly impossible to understand your code.


--
Kam-Hung Soh http://kamhungsoh.com/blog";>Software Salariman

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


Re: write to specific line in file?

2008-05-17 Thread castironpi
On May 16, 5:57 pm, castironpi <[EMAIL PROTECTED]> wrote:
> On May 16, 5:22 pm, castironpi <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > On May 16, 2:25 pm, 7stud <[EMAIL PROTECTED]> wrote:
>
> > > globalrev wrote:
> > > > i ahve a program that takes certain textsnippets out of one file and
> > > > inserts them into another.
>
> > > > problem is it jsut overwrites the first riow every time.
>
> > > > i want to insert every new piece of text into the next row.
> > > > so:
> > > > 1. how do i write to a new line every time i write to the file?
>
> > > > 2. if i dont want to write to a new line but just want to insert it
> > > > after the last token in the file, how do i do then?
>
> > > Generally, you can't "insert" anything into a file.  You can either
> > > append to the end of a file, or you can rewrite the whole file.  It
> > > sounds like you probably want to read the file into an array using
> > > readlines().  Then manipulate that array however you want--appending
> > > and inserting--and when you are done, overwrite the file with your
> > > array.
>
> > > However, you should be aware that as soon as you open a file for
> > > writing all the data is erased, and if your program should happen to
> > > crash right then, the array containing the data will disappear into
> > > the ether and your file will be empty--in other words all your data
> > > will be gone.  To prevent such an occurence, you should write your
> > > final data to another file, then delete the original file, and finally
> > > change the other file's name to the original file name.
>
> > Some options:
>
> > 1.  Limit line length.  Then offset is ( line* length ).
> > 2.  Name your relation.
> > 3.  Hijack the operating system.  Remember:
>
> > abc
> > def
>
> > on disk looks like
> > abc\ndef
>
> > where
>
> > abcd
> > def
>
> > looks like
> > abcd\ndef
>
> > So, if you want line 2, you have to scan line 1.- Hide quoted text -
>
> > - Show quoted text -
>
> You also have
>
> f.seek( X )
> f.write( 'abc' )
>
> which writes 'abc' in the middle, at offset X.
>
> f.seek( 0, SEEK_END )
>
> takes you to the back, and don't forget
>
> f.flush( ).
>
> Say you have:
>
> File:
>
> abc
> def
> ghi
>
> Symbols:
>
> 0-3
> 1-3
> 2-3
>
> abcdefghi
>
> and you want to make abc abC.  Then
>
> f.seek( 2 )
> f.write( 'C' )
>
> does work.
>
> If you want to make abc ab, then you change a different file
>
> table.seek( 0 )
> table.write( chr( 2 ) )
>
> ( or bytes( [ 2 ] ) in 3.0 ).
>
> If you want to make abc abcd, then you change both.
>
> f.seek( 9 )
> f.write( 'd' )
>
> table.seek( 0 )
> table.write( chr( 4 ) )
>
> But.  Now you have item 0 in two places.  'abc' somewhere and 'd'
> somewhere else, at 0 and at 9 respectively.  I think the file system
> overallocates room for String 1.  You would read:
>
> 0- 2- [ 3, 1 ]
>
> for 'abc'+'d'
>
> and
>
> 1- 1- [ 3 ]
> 2- 1- [ 3 ]
>
> for 'def' and 'ghi', where actual representations would be:
>
> 0- 2- [ 0/3, 9/1, 0, 0, 0, 0, 0, 0 ]
> 1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
> 2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
>
> As your chains start to grow, leave table 8xN, and just copy strings
> to new sectors when they become long:
>
> 0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
> 1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
> 2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
>
> You can insort in linear time when you're looking for available slots.
>
> 'jklm' can't fit at 0.
>
> 0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
> 1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
> 2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
> 3- 1- [ 13/4, 0, 0, 0, 0, 0, 0, 0 ]
>
> but 'nop' can.
>
> 0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
> 1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
> 2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
> 3- 1- [ 13/4, 0, 0, 0, 0, 0, 0, 0 ]
> 4- 1- [ 0/3, 0, 0, 0, 0, 0, 0, 0 ]
>
> 'jklm' could've with a split:
>
> 0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
> 1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
> 2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
> 3- 2- [ 0/3, 13/1, 0, 0, 0, 0, 0, 0 ]
>
> and with 'nop':
>
> 0- 1- [ 9/4, 0, 0, 0, 0, 0, 0, 0 ]
> 1- 1- [ 3/3, 0, 0, 0, 0, 0, 0, 0 ]
> 2- 1- [ 6/3, 0, 0, 0, 0, 0, 0, 0 ]
> 3- 2- [ 0/3, 13/1, 0, 0, 0, 0, 0, 0 ]
> 4- 1- [ 14/3, 0, 0, 0, 0, 0, 0, 0 ]
>
> There's an easy chance you can beat DbfilenameShelf for your
> application.- Hide quoted text -
>
> - Show quoted text -

I am still interested in this one, but doubt if anyone still reading.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using Python for programming algorithms

2008-05-17 Thread Vicent Giner

>
> The usual answer is that development
> time is more important than running time.

OK, thank you for your answer.

I just wanted to check if it was a naive idea trying to use Python
instead of C in order to implement my own algorithms, and other
research-related code.


>
> Since you are likely to be using arrays,
> you might look at numpy, where the
> number crunching is using compiled C code.

NumPy seeems to be a very interesting and powerful package. Thank you
again.


>
> Try Google with Python and your area of
> interest.  You could well find
> Python-based packages which meet your needs.
>

Yes, I've been giving a try, and I've found some interesting links.

Thank you!

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


Re: how would you...?

2008-05-17 Thread Mensanator
On May 17, 4:02�am, Sanoski <[EMAIL PROTECTED]> wrote:
> The reason I ask about text files is the need to save the data
> locally, and have it stored in a way where backups can easily
> be made.

Sure, you can always do that if you want. But if your target
is SQLlite or MS-Access, those are files also, so can be
backed up as easily as text files.

>
> Then if your computer crashes and you lose everything, but
> you have the data files it uses backed up, you can just
> download the program, extract the backed up data to a
> specific directory, and then it works exactly the way it
> did before you lost it. I suppose a SQLite database might
> solve this, but I'm not sure.

It will. Remember, once in a database, you have value-added
features like filtering, sorting, etc. that you would have
to do yourself if you simply read in text files.

> I'm just getting started, and I
> don't know too much about it yet.

Trust me, a database is the way to go.
My preference is MS-Access, because I need it for work.
It is a great tool for learning databases because it's
visual inteface can make you productive BEFORE you learn
SQL.

>
> I'm also still not sure how to download and associate the pictures
> that each entry has for it.

See example at end of post.

> The main thing for me now is getting
> started. It needs to get information from the web. In this case,
> it's a simple XML feed.

BeautifulSoup also has an XML parser. Got to their
web page and read the documentation.

> The one thing that seems that would
> make it easier is every post to the feed is very consistent.
> Each header starts with the letter A, which stands for Alpike
> Tech, follow by the name of the class, the room number, the
> leading student, and his GPA. All that is one line of text.
> But it's also a link to more information. For example:
>
> A Economics, 312, John Carbroil, 4.0
> That's one whole post to the feed. Like I say, it's very
> simple and consistent. Which should make this easier.

That's what you want for parsing, how to seperate
a composite set of data. Simple can sometimes be
done with split(), complex with regular expressions.

>
> Eventually I want it to follow that link and grab information
> from there too, but I'll worry about that later. Technically,
> if I figure this first part out, that problem should take
> care of itself.
>


A sample picture scraper:

from BeautifulSoup import BeautifulSoup
import urllib2
import urllib

#
# start by scraping the web page
#
the_url="http://members.aol.com/mensanator/OHIO/TheCobs.htm";
req = urllib2.Request(url=the_url)
f = urllib2.urlopen(req)
www = f.read()
soup = BeautifulSoup(www)
print soup.prettify()

#
# a simple page with pictures
#
##
## 
##  
##   Ohio - The Cobs!
##  
## 
## 
##  
##   Ohio Vacation Pictures - The Cobs!
##  
##  
##  
##  
##  WTF?
##  
##   
##   
##   This is surreal.
##  
##  
##   
##   
##   Six foot tall corn cobs made of concrete.
##  
##  
##   
##   
##   109 of them, laid out like a modern Stonehenge.
##  
##  
##   
##   
##   With it's own Druid worshippers.
##  
##  
##   
##   
##   Cue the
##   
##Also Sprach Zarathustra
##   
##   soundtrack.
##  
##  
##   
##   
##   Air & Space Museums are a dime a dozen.
##   
##   But there's only
##   
##one
##   
##   Cobs!
##  
##  
##  
## 
##

#
# parse the page to find all the pictures (image tags)
#
the_pics = soup.findAll('img')

for i in the_pics:
  print i

##
##
##
##
##
##
##

#
# the picutres have no path, so must be in the
# same directory as the web page
#
the_jpg_path="http://members.aol.com/mensanator/OHIO/";

#
# now with urllib, copy the picture files to the local
# hard drive renaming with sequence id at the same time
#
for i,j in enumerate(the_pics):
  p = the_jpg_path + j['src']
  q = 'C:\\scrape\\' + 'pic' + str(i).zfill(4) + '.jpg'
  urllib.urlretrieve(p,q)

#
# and here's the captured files
#
##  C:\>dir scrape
##   Volume in drive C has no label.
##   Volume Serial Number is D019-C60D
##
##   Directory of C:\scrape
##
##  05/17/2008  07:06 PM  .
##  05/17/2008  07:06 PM  ..
##  05/17/2008  07:05 PM69,877 pic.jpg
##  05/17/2008  07:05 PM71,776 pic0001.jpg
##  05/17/2008  07:05 PM70,958 pic0002.jpg
##  05/17/2008  07:05 PM69,261 pic0003.jpg
##  05/17/2008  07:05 PM70,653 pic0004.jpg
##  05/17/2008  07:05 PM70,564 pic0005.jpg
##  05/17/2008  07:05 PM   113,356 pic0006.jpg
## 7 File(s)536,445 bytes
## 2 Dir(s)  27,823,570,944 bytes free
--
http://mail.python.org/mailman/listinfo/python-list

Re: numpy.frombuffer != unpack() ??

2008-05-17 Thread Gary Herron

Marlin Rowley wrote:
Actually in my traversal of the never-ending maze of understanding 
arrays in python, I stumbled that the data in the wrong sequence.


Let's get back to this transpose() deal, say we have these values as 
integers (representing rgba again):


[[[0,1,2,3]
[4,5,6,7]
[8,9,10,11]
[12,13,14,15]]

[[16,17,18,19]
[20,21,22,23]
[24,25,26,27]
[28,29,30,31]]]

Now if I do this: transpose((2,0,1)), I get this:

[[[0,4,8,12] [16,20,24,28]]
[[1,5,9,13] [17,21,25,29]]
[[2,6,10,14][18,22,26,30]]
[[3,7,11,15][19,23,27,31]]]

This is NOT what I want. I want the new array to be:

[0,4,8,12][1,5,9,13]
[2,6,10,14][3,7,11,15]
[16,20,24,28][17,21,25,29]
[18,22,26,30][19,23,27,31]

How do I do this?


That's s little ambiguous, but one of the following two 
transpose-reshape-print's might be what you want.


Gary Herron


import numpy

a = numpy.array([[[0,1,2,3],
[4,5,6,7],
[8,9,10,11],
[12,13,14,15]],

[[16,17,18,19],
[20,21,22,23],
[24,25,26,27],
[28,29,30,31]]])


print numpy.reshape(a.transpose(0,2,1), (8,4))
print numpy.reshape(a.transpose(0,2,1), (4,2,4))

output is:

[[ 0 4 8 12]
[ 1 5 9 13]
[ 2 6 10 14]
[ 3 7 11 15]
[16 20 24 28]
[17 21 25 29]
[18 22 26 30]
[19 23 27 31]]

and

[[[ 0 4 8 12]
[ 1 5 9 13]]

[[ 2 6 10 14]
[ 3 7 11 15]]

[[16 20 24 28]
[17 21 25 29]]

[[18 22 26 30]
[19 23 27 31]]]





-M







> Date: Sat, 17 May 2008 08:58:08 -0700
> From: [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]
> CC: python-list@python.org
> Subject: Re: numpy.frombuffer != unpack() ??
>
> Marlin Rowley wrote:
> >
> > Very cool.
> >
> > > > a = ([''],[''])
> > a represents a tile with height of 2 and width of 4 with 4 bits/pixel
> > for each color.
> >
> > > >>> b = numpy.frombuffer(''.join(sum(a,[])),dtype='S1')
> > this seperates the stream into individual values - Check
> >
> > > >>> b.shape=(2,4,4)
> >
> > This reshapes the array so that b.shape=(height,width,#bits/pixel) 
- Check

> >
> > >>> c = b.transpose((2,0,1))
> >
> > What does the (2,0,1) represent in terms of width and height and
> > number of bytes?
>
> The (2,0,1) tells how to exchange the axes. For instance in a 2D array,
> a normal transpose exchanges rows and columns. It will change a (a by
> b) sized array into a (b by a) sized array. This would be equivalent to
> the more saying interchange axes 0,1 to the new order of 1,0.
>
> In numpy with higher dimension arrays, the default transpose just
> exchanges the first two axes, and the full transpose allows you to
> specify exactly the new ordering of the exes.
>
> So transpose((2,0,1)) means take axes (0,1,2) to the new order
> (2,1,0). In terms of sizes, an (a by b by c) sized array will end being
> of size (c by a by b) in size.
>
> In terms of implementation, there may not be *any* data re-arrangement
> in a transpose. The only thing that needs changing is how the indices
> are converted to an actual machine address of an indexed item. The
> documentation notes this by saying transpose returns a "new view" of 
the

> array. This explains why I copied the array before extracting bytes
> out of it -- you really do need the elements in the new order for the
> next operation.
>
> Gary Herron
>
> >
> >
> >
> >
> >
> > 


> >
> > > Date: Fri, 16 May 2008 17:08:20 -0700
> > > From: [EMAIL PROTECTED]
> > > To: [EMAIL PROTECTED]; python-list@python.org
> > > Subject: Re: numpy.frombuffer != unpack() ??
> > >
> > > Marlin Rowley wrote:
> > > > All:
> > > >
> > > > Say I have an array:
> > > >
> > > > a = ([''],[''])
> > > >
> > > > How do I make it so that I now have:
> > > >
> > > > starting with first element (a[0])
> > > > new_arr[0] = 'r'
> > > > new_arr[1] = 'g'
> > > > new_arr[2] = 'b'
> > > > new_arr[3] = 'a'
> > > > new_arr[4] = 'r'
> > > > .
> > > >
> > > > continuing "through" a[1] with the same new_arr
> > > > new_arr[N] = 'r'
> > > > new_arr[N+1] = 'g'
> > > > 
> > > >
> > > > -M
> > >
> > > Numpy can do this for you. First, do you really mean the array to
> > > contain lists of one string each? If so:
> > >
> > > >>> import numpy
> > > >>> a = ([''],[''])
> > > >>> b = numpy.frombuffer(''.join(sum(a,[])),dtype='S1') # Kind of a
> > > kludge here
> > > >>> b
> > > array(['r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 
'b', 'a',

> > > 'a', 'a', 'a', 'r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b',
> > > 'b', 'b', 'a', 'a', 'a', 'a'],
> > > dtype='|S1')
> > > >>> b.shape=(2,4,4)
> > > >>> b
> > > array([[['r', 'r', 'r', 'r'],
> > > ['g', 'g', 'g', 'g'],
> > > ['b', 'b', 'b', 'b'],
> > > ['a', 'a', 'a', 'a']],
> > >
> > > [['r', 'r', 'r', 'r'],
> > > ['g', 'g', 'g', 'g'],
> > > ['b', 'b', 'b', 'b'],
> > > ['a', 'a', 'a', 'a']]],
> > > dtype='|S1')
> > > >>> c = b.transpose((2,0,1))
> > > >>> c
> > > array([[['r', 'g', 'b', 'a'],
> > > ['r

Re: Using Python for programming algorithms

2008-05-17 Thread castironpi
On May 17, 5:32 pm, Vicent Giner <[EMAIL PROTECTED]> wrote:
> Hello.
>
> I am new to Python. It seems a very interesting language to me. Its
> simplicity is very attractive.
>
> However, it is usually said that Python is not a compiled but
> interpreted programming language —I mean, it is not like C, in that
> sense.
>
> I am working on my PhD Thesis, which is about Operations Research,
> heuristic algorithms, etc., and I am considering the possibility of
> programming all my algorithms in Python.
>
> The usual alternative is C, but I like Python more.
>
> The main drawbacks I see to using Python are these:
>
> * As far as I understand, the fact that Python is not a compiled
> language makes it slower than C, when performing huge amounts of
> computations within an algorithm or program.
>
> * I don't know how likely it is to find libraries in Python related to
> my research field.
>
> * I know Python is a "serious" and mature programming language, of
> course. But I do not know if it is seen as "just funny" in a research
> context. Is Python considered as a good programming language for
> implementing Operations Research algorithms, such as heuristics and
> other soft-computing algorithms?
>
> Maybe this is not the right forum, but maybe you can give me some
> hints or tips...
>
> Thank you in advance.

You're hearing from 'impossible and useless'-- neither operations.

'Stacks' are pretty important to Python (there is a Stackless Python,
I understand), which makes persistence a little more handy.  It's
still a computer and still a language.  You may be asking how well its
best speakers know, and look at that, I can't tell you.  Some of the
fundamentals of Python may be unexplored to date, as its from the 90s,
and stacks are elements.

I, for one, will assume you're interested in control operations, which
yes Python has, and control is important.  The standard library is a
good size to me (I wouldn't go doubling).  There's a ready graphics
module.  There are code-primitive literals, including lists -and- a
tuple.  I think you're looking for the selling points of dynamic
assignment (a.barproperty= 'unheardof'), typefreeness (a= [2,'bcd']),
dynamic execution (exec('print 2'), which promotes a possibility of
self-referentiality), type-aware function pointers, variable-length
procedure arguments, and platform independence.

I think you just asked at the right time.  Yes that's an impressive
list.

There is one catch to Python, of the importance of which of the powers
that be, I am unaware.  But I do know what you are liable to find on
the newsgroup.  Now, with thousands of dollars of institution time on
the money, what control?  I will be tentatively assuming that you are
not covertly comparing other languages.  I don't think you'll like it
if you're unwise.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using Python for programming algorithms

2008-05-17 Thread Colin J. Williams

Vicent Giner wrote:

Hello.

I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.

However, it is usually said that Python is not a compiled but
interpreted programming language —I mean, it is not like C, in that
sense.

I am working on my PhD Thesis, which is about Operations Research,
heuristic algorithms, etc., and I am considering the possibility of
programming all my algorithms in Python.

The usual alternative is C, but I like Python more.

The main drawbacks I see to using Python are these:

* As far as I understand, the fact that Python is not a compiled
language makes it slower than C, when performing huge amounts of
computations within an algorithm or program.


The usual answer is that development 
time is more important than running time.


Since you are likely to be using arrays, 
you might look at numpy, where the 
number crunching is using compiled C code.




* I don't know how likely it is to find libraries in Python related to
my research field.

* I know Python is a "serious" and mature programming language, of
course. But I do not know if it is seen as "just funny" in a research
context. Is Python considered as a good programming language for
implementing Operations Research algorithms, such as heuristics and
other soft-computing algorithms?


Try Google with Python and your area of 
interest.  You could well find 
Python-based packages which meet your needs.




Maybe this is not the right forum, but maybe you can give me some
hints or tips...

Thank you in advance.


Good luck.

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


Re: help compiling Python on vs 2008!

2008-05-17 Thread Colin J. Williams

inhahe wrote:
"Christian Heimes" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]

inhahe schrieb:

what is going on here, and how do I compile these external libraries?

I assume you are trying to compile Python 2.5 with VS 9.0. It's not
supported. Some extensions don't compile under VS 9.0

Christian



I just installed Visual Studio 8 and tried compling and I bgot the same 
problem.  hope this isn't a redundant post. i don't know if 
[EMAIL PROTECTED] reposts here.



You might consider working with Python 
2.6, which does compile with Visual 
Studio C++ 2008 and running the test 
(Tools\Buildbot\rt.bat) produces very 
few errors.  Just remember that 2.6 is 
tsill at the alpha stage of development.


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


Using Python for programming algorithms

2008-05-17 Thread Vicent Giner
Hello.

I am new to Python. It seems a very interesting language to me. Its
simplicity is very attractive.

However, it is usually said that Python is not a compiled but
interpreted programming language —I mean, it is not like C, in that
sense.

I am working on my PhD Thesis, which is about Operations Research,
heuristic algorithms, etc., and I am considering the possibility of
programming all my algorithms in Python.

The usual alternative is C, but I like Python more.

The main drawbacks I see to using Python are these:

* As far as I understand, the fact that Python is not a compiled
language makes it slower than C, when performing huge amounts of
computations within an algorithm or program.

* I don't know how likely it is to find libraries in Python related to
my research field.

* I know Python is a "serious" and mature programming language, of
course. But I do not know if it is seen as "just funny" in a research
context. Is Python considered as a good programming language for
implementing Operations Research algorithms, such as heuristics and
other soft-computing algorithms?

Maybe this is not the right forum, but maybe you can give me some
hints or tips...

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


Re: how to use the transpose function in numpy?

2008-05-17 Thread Robert Kern

Ask numpy questions on the numpy mailing list:

  http://www.scipy.org/Mailing_Lists

--
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: help compiling Python on vs 2008!

2008-05-17 Thread Christian Heimes
Matthieu Brucher schrieb:
> Even if you compile Python 2.5 with VS2008, VS2003 is used for the
> extensions, AFAIR. I didn't try Python 2.6 because I only have Visual C++
> Express 2008.

You can compile 2.6 and 3.0 with the free Express Edition just fine.

Christian

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


RE: numpy.frombuffer != unpack() ??

2008-05-17 Thread Marlin Rowley
Actually in my traversal of the never-ending maze of understanding arrays in 
python, I stumbled that the data in the wrong sequence.
 
Let's get back to this transpose() deal, say we have these values as integers 
(representing rgba again): [[[0,1,2,3][4,5,6,7][8,9,10,11][12,13,14,15]] 
[[16,17,18,19][20,21,22,23][24,25,26,27][28,29,30,31]]] Now if I do this: 
transpose((2,0,1)), I get this: [[[0,4,8,12] [16,20,24,28]][[1,5,9,13] 
[17,21,25,29]][[2,6,10,14][18,22,26,30]][[3,7,11,15][19,23,27,31]]] This is NOT 
what I want.  I want the new array to be: 
[0,4,8,12][1,5,9,13][2,6,10,14][3,7,11,15][16,20,24,28][17,21,25,29][18,22,26,30][19,23,27,31]
 How do I do this? -M



> Date: Sat, 17 May 2008 08:58:08 -0700> From: [EMAIL PROTECTED]> To: [EMAIL 
> PROTECTED]> CC: python-list@python.org> Subject: Re: numpy.frombuffer != 
> unpack() ??> > Marlin Rowley wrote:> > > > Very cool.> > > > > > a = 
> ([''],[''])> > a represents a tile with 
> height of 2 and width of 4 with 4 bits/pixel > > for each color.> > > > > >>> 
> b = numpy.frombuffer(''.join(sum(a,[])),dtype='S1')> > this seperates the 
> stream into individual values - Check> > > > > >>> b.shape=(2,4,4)> >> > This 
> reshapes the array so that b.shape=(height,width,#bits/pixel) - Check> > > > 
> >>> c = b.transpose((2,0,1))> > > > What does the (2,0,1) represent in terms 
> of width and height and > > number of bytes?> > The (2,0,1) tells how to 
> exchange the axes. For instance in a 2D array, > a normal transpose exchanges 
> rows and columns. It will change a (a by > b) sized array into a (b by a) 
> sized array. This would be equivalent to > the more saying interchange axes 
> 0,1 to the new order of 1,0. > > In numpy with higher dimension arrays, the 
> default transpose just > exchanges the first two axes, and the full transpose 
> allows you to > specify exactly the new ordering of the exes. > > So 
> transpose((2,0,1)) means take axes (0,1,2) to the new order > (2,1,0). In 
> terms of sizes, an (a by b by c) sized array will end being > of size (c by a 
> by b) in size. > > In terms of implementation, there may not be *any* data 
> re-arrangement > in a transpose. The only thing that needs changing is how 
> the indices > are converted to an actual machine address of an indexed item. 
> The > documentation notes this by saying transpose returns a "new view" of 
> the > array. This explains why I copied the array before extracting bytes > 
> out of it -- you really do need the elements in the new order for the > next 
> operation.> > Gary Herron> > >> >> >> >> > > > 
> > >> 
> > > Date: Fri, 16 May 2008 17:08:20 -0700> > > From: [EMAIL PROTECTED]> > > 
> To: [EMAIL PROTECTED]; python-list@python.org> > > Subject: Re: 
> numpy.frombuffer != unpack() ??> > >> > > Marlin Rowley wrote:> > > > All:> > 
> > >> > > > Say I have an array:> > > >> > > > a = 
> ([''],[''])> > > >> > > > How do I make it so 
> that I now have:> > > >> > > > starting with first element (a[0])> > > > 
> new_arr[0] = 'r'> > > > new_arr[1] = 'g'> > > > new_arr[2] = 'b'> > > > 
> new_arr[3] = 'a'> > > > new_arr[4] = 'r'> > > > .> > > >> > > > 
> continuing "through" a[1] with the same new_arr> > > > new_arr[N] = 'r'> > > 
> > new_arr[N+1] = 'g'> > > > > > > >> > > > -M> > >> > > Numpy can do this 
> for you. First, do you really mean the array to> > > contain lists of one 
> string each? If so:> > >> > > >>> import numpy> > > >>> a = 
> ([''],[''])> > > >>> b = 
> numpy.frombuffer(''.join(sum(a,[])),dtype='S1') # Kind of a> > > kludge here> 
> > > >>> b> > > array(['r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 
> 'b', 'a',> > > 'a', 'a', 'a', 'r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 
> 'b',> > > 'b', 'b', 'a', 'a', 'a', 'a'],> > > dtype='|S1')> > > >>> 
> b.shape=(2,4,4)> > > >>> b> > > array([[['r', 'r', 'r', 'r'],> > > ['g', 'g', 
> 'g', 'g'],> > > ['b', 'b', 'b', 'b'],> > > ['a', 'a', 'a', 'a']],> > >> > > 
> [['r', 'r', 'r', 'r'],> > > ['g', 'g', 'g', 'g'],> > > ['b', 'b', 'b', 'b'],> 
> > > ['a', 'a', 'a', 'a']]],> > > dtype='|S1')> > > >>> c = 
> b.transpose((2,0,1))> > > >>> c> > > array([[['r', 'g', 'b', 'a'],> > > ['r', 
> 'g', 'b', 'a']],> > >> > > [['r', 'g', 'b', 'a'],> > > ['r', 'g', 'b', 
> 'a']],> > >> > > [['r', 'g', 'b', 'a'],> > > ['r', 'g', 'b', 'a']],> > >> > > 
> [['r', 'g', 'b', 'a'],> > > ['r', 'g', 'b', 'a']]],> > > dtype='|S1')> > > 
> >>> d=c.copy() # To make it contiguous> > > >>> d.shape = (32,)> > > >>> d> > 
> > array(['r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r',> > 
> > 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g',> > > 'b', 
> 'a', 'r', 'g', 'b', 'a'],> > > dtype='|S1')> > >> > > Done. Cool no?> > >> > 
> > Gary Herron> > >> > > >> > > >> > > >> > > >> > > > > > 
> --

Re: help compiling Python on vs 2008!

2008-05-17 Thread Matthieu Brucher
2008/5/17 Christian Heimes <[EMAIL PROTECTED]>:

> Matthieu Brucher schrieb:
> > Hi,
> >
> > I did not manage to build extension with distutils with Python compiled
> with
> > VS different than 2003. The need for 2003 was hard-coded in distutils.
> > You can try building extensions with VS2008 with Scons. This is what I do
> a
> > lot, and everything works fine as long as the interface does not use
> > standard structures (like FILE, custom structures are fine) or objects
> > allocated in the extension is freed in the extension.
>
> Python 2.5 is compiled with VS 2003. Neither VS 2005 nor 2008 are
> officially supported.


Even if you compile Python 2.5 with VS2008, VS2003 is used for the
extensions, AFAIR. I didn't try Python 2.6 because I only have Visual C++
Express 2008.

You can compile extensions with a different version of MS VC but it can
> get you in a lot of trouble. Every version of the VS Compiler uses its
> own C Runtime Library (MSVCRT). You can't share some resources like
> allocated memory and FILE* objects between MSVCRTs.


I don't see your point, that's what I wrote...

Matthieu
-- 
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher
--
http://mail.python.org/mailman/listinfo/python-list

Re: help compiling Python on vs 2008!

2008-05-17 Thread Christian Heimes
Matthieu Brucher schrieb:
> Hi,
> 
> I did not manage to build extension with distutils with Python compiled with
> VS different than 2003. The need for 2003 was hard-coded in distutils.
> You can try building extensions with VS2008 with Scons. This is what I do a
> lot, and everything works fine as long as the interface does not use
> standard structures (like FILE, custom structures are fine) or objects
> allocated in the extension is freed in the extension.

Python 2.5 is compiled with VS 2003. Neither VS 2005 nor 2008 are
officially supported.

You can compile extensions with a different version of MS VC but it can
get you in a lot of trouble. Every version of the VS Compiler uses its
own C Runtime Library (MSVCRT). You can't share some resources like
allocated memory and FILE* objects between MSVCRTs.

Christian

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


how to use the transpose function in numpy?

2008-05-17 Thread Marlin Rowley
All:
 
Say I have a series of arrays arranged like so:
 
[[[0,1,2,3]
[4,5,6,7]
[8,9,10,11]
[12,13,14,15]]
 
[[16,17,18,19]
[20,21,22,23]
[24,25,26,27]
[28,29,30,31]]]
 
Now if I do this: transpose((2,0,1)), I get this:
 
[[[0,4,8,12] [16,20,24,28]]
[[1,5,9,13] [17,21,25,29]]
[[2,6,10,14][18,22,26,30]]
[[3,7,11,15][19,23,27,31]]]
 
This is NOT what I want.  I want the new array to be:
 
[0,4,8,12][1,5,9,13]
[2,6,10,14][3,7,11,15]
[16,20,24,28][17,21,25,29]
[18,22,26,30][19,23,27,31]
 
How do I do this?
 
-M
_
Make every e-mail and IM count. Join the i’m Initiative from Microsoft.
http://im.live.com/Messenger/IM/Join/Default.aspx?source=EML_WL_ MakeCount--
http://mail.python.org/mailman/listinfo/python-list

Re: waiting on an event blocks all signals

2008-05-17 Thread alan
On May 17, 3:06 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote:
> On Sat, 17 May 2008 12:49:54 -0700, John Schroeder <[EMAIL PROTECTED]> wrote:
> >On Sat, May 17, 2008 at 12:32 PM, alan <[EMAIL PROTECTED]> wrote:
>
> >> This ignores CTRL-C on every platform I've tested:
>
> >> python -c "import threading; threading.Event().wait()"
> >> ^C^C^C^C
>
> >> It looks to me like all signals are masked before entering wait(). Can
> >> someone familiar with the internals explain and/or justify this
> >> behavior? Thanks,
>
> >^C only kills the main thread.  Use Control-Break to kill all threads.
>
> Look at that program.  It's single-threaded.  Where do you think the ^C
> is going? :)
>
> Jean-Paul

Look at this program which is also "single-threaded." Clearly,
python's doing something special. I'm hoping someone can tell me what,
and why.

/*
  pthreadsig.c

  $ gcc pthreadsig.c -lpthread
  $ ./a.out
  ^C
  $
*/

#include 

int main(int argc, char** argv) {
  int rc = 0;

  pthread_mutex_t mtx;
  pthread_cond_t cond;
  pthread_mutex_init(&mtx, 0);
  pthread_cond_init(&cond, 0);

  pthread_mutex_lock(&mtx);
  rc = pthread_cond_wait(&cond, &mtx);
  pthread_mutex_unlock(&mtx);

  return rc;
}
--
http://mail.python.org/mailman/listinfo/python-list


Re: multiple databases, what's the best interface ?

2008-05-17 Thread python
Stef,

Take a look at what the dabo team has put together.

http://dabodev.com

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


Re: waiting on an event blocks all signals

2008-05-17 Thread alan grow
On Sat, May 17, 2008 at 2:49 PM, John Schroeder <[EMAIL PROTECTED]> wrote:
> ^C only kills the main thread.  Use Control-Break to kill all threads.
>>
>> python -c "import threading; threading.Event().wait()"
>> ^C^C^C^C

There's a single main thread here though. Or am I missing something?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Markov Analysis Help

2008-05-17 Thread bearophileHUGS
dave:
>Can you have doctests on random functions?

Yes, you can add doctests to methods, functions, classes, module
docstrings, and in external text files.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Re: waiting on an event blocks all signals

2008-05-17 Thread Jean-Paul Calderone

On Sat, 17 May 2008 12:49:54 -0700, John Schroeder <[EMAIL PROTECTED]> wrote:

On Sat, May 17, 2008 at 12:32 PM, alan <[EMAIL PROTECTED]> wrote:


This ignores CTRL-C on every platform I've tested:

python -c "import threading; threading.Event().wait()"
^C^C^C^C

It looks to me like all signals are masked before entering wait(). Can
someone familiar with the internals explain and/or justify this
behavior? Thanks,



^C only kills the main thread.  Use Control-Break to kill all threads.



Look at that program.  It's single-threaded.  Where do you think the ^C
is going? :)

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


Re: waiting on an event blocks all signals

2008-05-17 Thread John Schroeder
^C only kills the main thread.  Use Control-Break to kill all threads.

On Sat, May 17, 2008 at 12:32 PM, alan <[EMAIL PROTECTED]> wrote:

> This ignores CTRL-C on every platform I've tested:
>
> python -c "import threading; threading.Event().wait()"
> ^C^C^C^C
>
> It looks to me like all signals are masked before entering wait(). Can
> someone familiar with the internals explain and/or justify this
> behavior? Thanks,
>
> -Alan
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list

waiting on an event blocks all signals

2008-05-17 Thread alan
This ignores CTRL-C on every platform I've tested:

python -c "import threading; threading.Event().wait()"
^C^C^C^C

It looks to me like all signals are masked before entering wait(). Can
someone familiar with the internals explain and/or justify this
behavior? Thanks,

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


Re: help compiling Python on vs 2008!

2008-05-17 Thread Matthieu Brucher
Hi,

I did not manage to build extension with distutils with Python compiled with
VS different than 2003. The need for 2003 was hard-coded in distutils.
You can try building extensions with VS2008 with Scons. This is what I do a
lot, and everything works fine as long as the interface does not use
standard structures (like FILE, custom structures are fine) or objects
allocated in the extension is freed in the extension.

Matthieu

2008/5/17 inhahe <[EMAIL PROTECTED]>:

>
> "Christian Heimes" <[EMAIL PROTECTED]> wrote in message
> news:[EMAIL PROTECTED]
> > inhahe schrieb:
> >> what is going on here, and how do I compile these external libraries?
> >
> > I assume you are trying to compile Python 2.5 with VS 9.0. It's not
> > supported. Some extensions don't compile under VS 9.0
> >
> > Christian
> >
>
> I just installed Visual Studio 8 and tried compling and I bgot the same
> problem.  hope this isn't a redundant post. i don't know if
> [EMAIL PROTECTED] reposts here.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
French PhD student
Website : http://matthieu-brucher.developpez.com/
Blogs : http://matt.eifelle.com and http://blog.developpez.com/?blog=92
LinkedIn : http://www.linkedin.com/in/matthieubrucher
--
http://mail.python.org/mailman/listinfo/python-list

Re: working with images (PIL ?)

2008-05-17 Thread Scott David Daniels

Poppy wrote:
I need to write a program to examine images (JPG) and determine how much 
area is whitespace. We need to throw a returned image out if too much of it 
is whitespace from the dataset we're working with. I've been examining the 
Python Image Library and can not determine if it offers the needed 
functionality. Does anyone have suggestions of other image libraries I 
should be looking at it, or if PIL can do what I need? 




If PIL's "histogram" method doesn't give you a clue how you might do
this, hire someone to do it.

--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list


Re: help compiling Python on vs 2008!

2008-05-17 Thread inhahe

"Christian Heimes" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
> inhahe schrieb:
>> what is going on here, and how do I compile these external libraries?
>
> I assume you are trying to compile Python 2.5 with VS 9.0. It's not
> supported. Some extensions don't compile under VS 9.0
>
> Christian
>

I just installed Visual Studio 8 and tried compling and I bgot the same 
problem.  hope this isn't a redundant post. i don't know if 
[EMAIL PROTECTED] reposts here.


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


Re: Python-list Digest, Vol 56, Issue 311

2008-05-17 Thread inhahe
didn't mean to re-post the whole digest. i forgot to clip what was after
this message.

On Sat, May 17, 2008 at 2:59 PM, inhahe <[EMAIL PROTECTED]> wrote:

> i just installed visual studio 8 and tried to compile again, and I got the
> same exact problem.
>
>
>
>
>>
>> From: Christian Heimes <[EMAIL PROTECTED]>
>> To: python-list@python.org
>> Date: Sat, 17 May 2008 14:59:51 +0200
>> Subject: Re: help compiling Python on vs 2008!
>> inhahe schrieb:
>> > what is going on here, and how do I compile these external libraries?
>>
>> I assume you are trying to compile Python 2.5 with VS 9.0. It's not
>> supported. Some extensions don't compile under VS 9.0
>>
>> Christian
>>
>>
>>
>>
--
http://mail.python.org/mailman/listinfo/python-list

multiple databases, what's the best interface ?

2008-05-17 Thread Stef Mientki

hello,

I need to switch fluently between 2 or 3 types of dbases:
SQLite, Sybase ( and in the future MS SQL-server).

I need both closed application and general purpose database manager,
which should run on different platforms (windows, windows mobile, not 
web based).


I would like to write the applications in Python.
What's the best interface so I can use the same program for all databases,
and just have to change the database name, if I want to use another 
database ?


thanks,
Stef Mientki

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


Re: Distributing applications that use 3rd party modules

2008-05-17 Thread eliben
On May 17, 8:54 pm, "Terry Reedy" <[EMAIL PROTECTED]> wrote:
> "eliben" <[EMAIL PROTECTED]> wrote in message
>
> news:[EMAIL PROTECTED]
> | Is there a simple way to find out which packages are used by my
> | script ?
>
> Ggrep for import statements?

This isn't very comprehensive :-)

>  (But that only only gives direct dependencies.)
> Does py2exe have an option to output the list it collects?|
> I think people have mentioned such here.

Digging around the standard library documentation, I came upon the
'modulefinder' module, which seems to be able to do what I want, at
least in terms of finding all the modules used. I wonder how difficult
it should be to wrap something around modulefinder that copies all the
relevant modules into a local directory and modifies sys.path
accordingly.

Eli


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


Re: Install man-pages with distutils/setuptools

2008-05-17 Thread Sebastian 'lunar' Wiesner
[ Hartmut Goebel <[EMAIL PROTECTED]> ]

> Hi,
> 
> is there a standard way or a snippet for installing man-pages with
> 
>python set.py install
> 
> when using distutils or setuptools?
No, not even setuptools provides a standard way for man pages.

-- 
Freedom is always the freedom of dissenters.
  (Rosa Luxemburg)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies and CookieJar

2008-05-17 Thread 7stud
Larry Bates wrote:
> I'm struggling with a project using mechanize and cookies to screen scape a
> website.  The site requires a client created cookie for authentication.

Are you sure that is correct?  As far as I know, the usual course of
events is for a site to set a cookie header in the response it sends
back to the client browser, which gets stored on the client's
computer.  Then when the browser makes subsequent requests to that
site, the browser will automatically include the cookies that were set
by that site.
--
http://mail.python.org/mailman/listinfo/python-list


smtplib "authentication required" error

2008-05-17 Thread cher

Hey,

Don't know if you can help, but I'm trying to import archived messages 
from Thunderbird into my gmail account and keep getting these types of 
errors.  I am using the GML program to help incorporate, but the SMTP 
server is not being recognized by gmail.  Am I doing something wrong?  
Anything else I can try?

SMTP I've tried:
   smtp.gmail.com
   gsmtp56.google.com

Error Val : (530, '5.7.0 Must issue a STARTTLS command first. 
m29sm9416768poh.4',

*** 76 ERROR SENDING MESSAGE FROM: [EMAIL PROTECTED]
*** UNABLE TO CONNECT TO SERVER OR SEND MESSAGE. ERROR FOLLOWS.
Error Type: smtplib.SMTPSenderRefused
Error Val : (530, '5.7.0 Must issue a STARTTLS command first. 
n22sm9448670pof.3', '[EMAIL PROTECTED]')


Thanks,
Cher


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


Re: Cookies and CookieJar

2008-05-17 Thread 7stud
On May 16, 9:21 am, Larry Bates <[EMAIL PROTECTED]> wrote:
> I'm struggling with a project using mechanize and cookies to screen scape a
> website.  The site requires a client created cookie for authentication.  Below
> is the code I'm attempting to use with the traceback I'm getting:
>
>  >>> import Cookie
>  >>> c=Cookie.SimpleCookie()
>  >>> c["Manageopen"]="cards"
>  >>> c['Manageopen']['expires'] = 0
>  >>> c['Manageopen']['path'] = "/"
>  >>> c['Manageopen']['domain'] = ".domain.com"
>  >>> c['Manageopen']['secure'] = ""
>  >>> c.output()
> 'Set-Cookie: Manageopen=cards; Domain=.domain.com; expires=Fri, 16-May-2008
> 14:06:00 GMT; Path=/'
>  >>> import cookielib
>  >>> cj=cookielib.CookieJar()
>  >>> cj.set_cookie(c)
> Traceback (most recent call last):
>    File "", line 1, in 
>    File "C:\Python25\lib\cookielib.py", line 1617, in set_cookie
>      if cookie.domain not in c: c[cookie.domain] = {}
> AttributeError: 'SimpleCookie' object has no attribute 'domain'
>  >>>
>
> I also tried:
>
>  >>> cj.set_cookie(c["Manageopen"])
> Traceback (most recent call last):
>    File "", line 1, in 
>    File "C:\Python25\lib\cookielib.py", line 1617, in set_cookie
>      if cookie.domain not in c: c[cookie.domain] = {}
> AttributeError: 'Morsel' object has no attribute 'domain'
>
> I've looked at everything I can find via Google and nothing seems to help.
>
> Thanks in advance.
>
> Regards,
> Larry

Hi,

In the python docs, set_cookie() is defined like this:

---
set_cookie(cookie)
Set a Cookie, without checking with policy to see whether or not it
should be set.
---

The Cookie mentioned there is a an object of the Cookie class which is
defined in the cookielib module.  On the other hand,
Cookie.SimpleCookie() creates an object of a class called SimpleCookie
(and SimpleCookie inherits from a class called BaseCookie).
Therefore, the object returned by Cookie.SimpleCookie() is not a
Cookie--it's a SimpleCookie.  Just because the module that contains
the SimpleCookie class is called Cookie does not mean that
SimpleCookie inherits from the Cookie class or that SimpleCookie has
any connection to the Cookie class located in the cookielib module.
If that's too confusing, here is the way the python modules are set
up:

Cookie module:
-
BaseCookie class
|
V
SimpleCookie class




cookielib module

Cookie class

CookieJar class
---set_cookie(Cookie)


What you did in your code was you supplied set_cookie() with a
SimpleCookie object, and set_cookie() expects a Cookie object.
SimpleCookie objects are a mapping and you access the data using the
notation:

>  >>> c['Manageopen']['domain'] = ".domain.com"

But if you open up cookilib.py and look at the line in the error
message:

> File "C:\Python25\lib\cookielib.py", line 1617, in set_cookie

you will see that the code uses the notation:

>      if cookie.domain ...

That's because the Cookie class defines simple objects that have
attributes that are accessed normally, i.e. with dot notation.


> The site requires a client created cookie for authentication.

A "cookie" is just one of the headers in a request.  Instead of using
a url string to retrieve a web page with urllib2.urlopen(), you can
use a Request object.  And Request objects allow you to set headers,
which means you can set a cookie header.

But the site will probably check every subsequent request for that
cookie header as well, so that means you will need to add the header
to every subsequent request you make.  That is where CookieJar can
help you.  It can store cookies in a file and then automatically add
them to every request for you.  It's a bit complicated though.  See
here:

http://www.voidspace.org.uk/python/articles/cookielib.shtml










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


Re: Distributing applications that use 3rd party modules

2008-05-17 Thread Terry Reedy

"eliben" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
| Is there a simple way to find out which packages are used by my
| script ?

Ggrep for import statements?
 (But that only only gives direct dependencies.)
Does py2exe have an option to output the list it collects?|
I think people have mentioned such here. 



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


rst2mediawiki?

2008-05-17 Thread Hartmut Goebel

Hello,

I'm looking for a rst2mediawiki writer. Unfortunatly mediawiki is quite 
widespread, but I prefere to sty with rst. Any hint?


--
Schönen Gruß - Regards
Hartmut Goebel

Goebel Consult
Spezialist für IT-Sicherheit in komplexen Umgebungen
http://www.goebel-consult.de
--
http://mail.python.org/mailman/listinfo/python-list


Re: Getting elements and text with lxml

2008-05-17 Thread J . Pablo Fernández
On May 17, 4:17 pm, Stefan Behnel <[EMAIL PROTECTED]> wrote:
> J. Pablo Fernández wrote:
> > I have an XML file that starts with:
>
> > 
> > 
> > 
> >   *-a
> > 
>
> > out of it, I'd like to extract something like (I'm just showing one
> > structure, any structure as long as all data is there is fine):
>
> > [("ofc", "*"), "-", ("rad", "a")]
>
>     >>> root = etree.fromstring(xml)
>     >>> l = []
>     >>> for el in root.iter():    # or root.getiterator()
>     ...     l.append((el, el.text))
>     ...     l.append(el.text)
>
> or maybe this is enough:
>
>     list(root.itertext())
>
> Stefan

Hello,

My object doesn't have iter() or itertext(), it only has:
iterancestors, iterchildren, iterdescendants, itersiblings.

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


Re: [PyOpenGL-Users] glGenLists returns 0

2008-05-17 Thread Gary Herron

Astan Chee wrote:

Hi,
Im trying to do glGenLists while a program is running and it keeps 
returning 0 and whenever I try to do a glCallList, it fails with a 
'invalid value' error.
Is this a known issue? why would my program give valid glGenList while 
being initialized but not while it is running?
  


IIRC,  this can happen if you call glGenLists *before* you create any 
windows (or perhaps more accurately, before an OpenGL context has been 
created).


Im using wx to create a new pygame thread that uses pyopengl. This is 
the glGenList snippet:


dlBody = glGenLists(1)   
glNewList(dlBody,GL_COMPILE)

SphereT = gluNewQuadric()
gluSphere(SphereT, 0.5, 32, 32)
gluQuadricNormals(SphereT, GLU_SMOOTH)
gluQuadricDrawStyle(SphereT, GLU_FILL)   
glEndList()


and here is how I init:

glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

glEnable (GL_LIGHT0)
glEnable (GL_LIGHTING)
glEnable(GL_TEXTURE_2D)
   
LightAmbient  = ( (0., 0., 0., 0.,1.) )

glLightModelfv( GL_LIGHT_MODEL_AMBIENT, LightAmbient )
   
glEnable(GL_COLOR_MATERIAL)


glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, 1.0*width/height, 1.0, 1.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()

  


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


Re: Markov Analysis Help

2008-05-17 Thread Terry Reedy

"dave" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
| bear,
| thanks for the suggestions.  I use IDLE to write the code and when it's
| working I paste it over into a new window.

Or you can just save code you want to keep to a new name.

| To add doctests would I need to use a certain
| filename for the tests to be run on?

You can run a doctest on a file from within the file (as well as from 
without).

if __name__ == '__main__': 

I presume the manual gives the details.

|  Can you have doctests on random functions?

???

tjr



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


Re: numpy.frombuffer != unpack() ??

2008-05-17 Thread Gary Herron

Marlin Rowley wrote:
 
Very cool.
 
> > a = ([''],[''])
a represents a tile with height of 2 and width of 4 with 4 bits/pixel 
for each color.
 
> >>> b = numpy.frombuffer(''.join(sum(a,[])),dtype='S1')

this seperates the stream into individual values - Check
 
> >>> b.shape=(2,4,4)


This reshapes the array so that b.shape=(height,width,#bits/pixel) - Check
 
 >>> c = b.transpose((2,0,1))
 
What does the (2,0,1) represent in terms of width and height and 
number of bytes?


The (2,0,1) tells how to exchange the axes.  For instance in a 2D array, 
a normal transpose exchanges rows and columns.  It will change a (a by 
b) sized array into a (b by a) sized array.  This would be equivalent to 
the more saying interchange axes 0,1 to the new order of 1,0. 

In numpy with higher dimension arrays, the default transpose just 
exchanges the first two axes, and the full transpose allows you to 
specify exactly the new ordering of the exes.  

So transpose((2,0,1))  means take axes (0,1,2) to the new order 
(2,1,0).  In terms of sizes, an (a by b by c) sized array will end being 
of size (c by a by b) in size. 

In terms of implementation, there may not be *any* data re-arrangement 
in a transpose.   The only thing that needs changing is how the indices 
are converted to an actual machine address of an indexed item.   The 
documentation notes this by saying transpose returns a "new view" of the 
array.   This explains why I copied the array before extracting bytes 
out of it -- you really do need the elements in the new order for the 
next operation.


Gary Herron






 



> Date: Fri, 16 May 2008 17:08:20 -0700
> From: [EMAIL PROTECTED]
> To: [EMAIL PROTECTED]; python-list@python.org
> Subject: Re: numpy.frombuffer != unpack() ??
>
> Marlin Rowley wrote:
> > All:
> >
> > Say I have an array:
> >
> > a = ([''],[''])
> >
> > How do I make it so that I now have:
> >
> > starting with first element (a[0])
> > new_arr[0] = 'r'
> > new_arr[1] = 'g'
> > new_arr[2] = 'b'
> > new_arr[3] = 'a'
> > new_arr[4] = 'r'
> > .
> >
> > continuing "through" a[1] with the same new_arr
> > new_arr[N] = 'r'
> > new_arr[N+1] = 'g'
> > 
> >
> > -M
>
> Numpy can do this for you. First, do you really mean the array to
> contain lists of one string each? If so:
>
> >>> import numpy
> >>> a = ([''],[''])
> >>> b = numpy.frombuffer(''.join(sum(a,[])),dtype='S1') # Kind of a
> kludge here
> >>> b
> array(['r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b', 'b', 'b', 'a',
> 'a', 'a', 'a', 'r', 'r', 'r', 'r', 'g', 'g', 'g', 'g', 'b', 'b',
> 'b', 'b', 'a', 'a', 'a', 'a'],
> dtype='|S1')
> >>> b.shape=(2,4,4)
> >>> b
> array([[['r', 'r', 'r', 'r'],
> ['g', 'g', 'g', 'g'],
> ['b', 'b', 'b', 'b'],
> ['a', 'a', 'a', 'a']],
>
> [['r', 'r', 'r', 'r'],
> ['g', 'g', 'g', 'g'],
> ['b', 'b', 'b', 'b'],
> ['a', 'a', 'a', 'a']]],
> dtype='|S1')
> >>> c = b.transpose((2,0,1))
> >>> c
> array([[['r', 'g', 'b', 'a'],
> ['r', 'g', 'b', 'a']],
>
> [['r', 'g', 'b', 'a'],
> ['r', 'g', 'b', 'a']],
>
> [['r', 'g', 'b', 'a'],
> ['r', 'g', 'b', 'a']],
>
> [['r', 'g', 'b', 'a'],
> ['r', 'g', 'b', 'a']]],
> dtype='|S1')
> >>> d=c.copy() # To make it contiguous
> >>> d.shape = (32,)
> >>> d
> array(['r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r',
> 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g', 'b', 'a', 'r', 'g',
> 'b', 'a', 'r', 'g', 'b', 'a'],
> dtype='|S1')
>
> Done. Cool no?
>
> Gary Herron
>
> >
> >
> >
> >
> > 


> > From: [EMAIL PROTECTED]
> > To: [EMAIL PROTECTED]; python-list@python.org
> > Subject: RE: numpy.frombuffer != unpack() ??
> > Date: Fri, 16 May 2008 17:31:30 -0500
> >
> > Thank you! That solved it!
> >
> > -M
> >
> >
> > 


> >
> > > To: python-list@python.org
> > > From: [EMAIL PROTECTED]
> > > Subject: Re: numpy.frombuffer != unpack() ??
> > > Date: Fri, 16 May 2008 17:25:00 -0500
> > >
> > > Marlin Rowley wrote:
> > > > All:
> > > >
> > > > I'm getting different floating point values when I use numpy
> > vs. unpack().
> > > >
> > > > frgba = numpy.frombuffer(, dtype=float32)
> > > > buffer = unpack("!f", byte)
> > > >
> > > > frgba[0] != buffer[0]
> > > >
> > > > why? This is forcing me use the unpack() function since it's
> > giving me
> > > > the correct values. What am I doing wrong?
> > >
> > > Endianness, perhaps? '!' specifies big-endian data (an alias for
> > '>'). Most
> > > likely, you are on a little-endian platform. All of the dtypes
> > in numpy default
> > > to the native-endianness unless specified. If you want to read
> > big-endian data
> > > using numpy, do this:
> > >
> > > frgba = numpy.frombuffer(, dtype='>f')
> > >
> > > If you have any more problems with numpy, please join us on the
> > numpy mai

Re: Markov Analysis Help

2008-05-17 Thread dave

On 2008-05-17 06:01:01 -0600, [EMAIL PROTECTED] said:


dave, few general comments to your code:
- Instead of using a comment that explains the meaning of a function,
add such things into docstrings.
- Your names can be improved, instead of f you can use file_name or
something like that, instead of convert_file you can use a name that
denotes that the conversion is already done, etc.
- You can use xrange instead of range and you can indent less, like 4
spaces.
- This line may be slow, you may want to find simpler ways to do the
same thing:
rkey = random.choice(analyze.keys())
- I suggest you to add doctests to all your functions.

Bye,
bearophile



bear,
thanks for the suggestions.  I use IDLE to write the code and when it's 
working I paste it over into a new window.  I'll tabify before saving 
the pasted code.  To add doctests would I need to use a certain 
filename for the tests to be run on?  Can you have doctests on random 
functions?


Thanks

Dave

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


Re: Dbase / foxpro files

2008-05-17 Thread Johny
Thanks for your reply.Is it possible to delete a record by using the
module?
Thanks
L>
--
http://mail.python.org/mailman/listinfo/python-list


Re: Getting elements and text with lxml

2008-05-17 Thread Stefan Behnel
J. Pablo Fernández wrote:
> I have an XML file that starts with:
> 
> 
> 
> 
>   *-a
> 
> 
> out of it, I'd like to extract something like (I'm just showing one
> structure, any structure as long as all data is there is fine):
> 
> [("ofc", "*"), "-", ("rad", "a")]

>>> root = etree.fromstring(xml)
>>> l = []
>>> for el in root.iter():# or root.getiterator()
... l.append((el, el.text))
... l.append(el.text)

or maybe this is enough:

list(root.itertext())

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


Re: save dictionary for later use?

2008-05-17 Thread castironpi
On May 17, 3:52 am, castironpi <[EMAIL PROTECTED]> wrote:
> On May 16, 4:23 pm, Hans Nowak <[EMAIL PROTECTED]>
> wrote:
>
>
>
>
>
> > globalrev wrote:
> > > pickle.dumps(mg)
> > > pickle.load(mg)
>
> > > 'dict' object has no attribute 'readline'
> > > dumps load(well i dont know but i get no complaint but running load
> > > generates that error)
>
> > The 'loads' and 'dumps' methods use strings:
>
> >  >>> import pickle
> >  >>> d = {"this": 42, "that": 101, "other": 17}
> >  >>> s = pickle.dumps(d)
> >  >>> s
> > "(dp0\nS'this'\np1\nI42\nsS'other'\np2\nI17\nsS'that'\np3\nI101\ns."
> >  >>> pickle.loads(s)
> > {'this': 42, 'other': 17, 'that': 101}
>
> > If you want to store to / restore from file, use 'dump' and 'load':
>
> > # write to file 'out'...
> >  >>> f = open("out")
> >  >>> f = open("out", "wb")
> >  >>> pickle.dump(d, f)
> >  >>> f.close()
>
> > # restore it later
> >  >>> g = open("out", "rb")
> >  >>> e = pickle.load(g)
> >  >>> g.close()
> >  >>> e
> > {'this': 42, 'other': 17, 'that': 101}
>
> > Also seehttp://docs.python.org/lib/pickle-example.html.
>
> > Hope this helps!
>
> > --Hans
>
> I want to compare that cleanliness with other languages to compare
> formats.
>
> Is pickle.load( open( 'out', 'rb' ) ) any better or worse than
> pickle.load( 'out', 'rb' )?- Hide quoted text -
>
> - Show quoted text -

This is a check-in on live-time writing.  pickle.load didn't take two
parameters.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Distributing applications that use 3rd party modules

2008-05-17 Thread eliben
On May 17, 3:23 pm, Mike Driscoll <[EMAIL PROTECTED]> wrote:
> On May 17, 4:42 am, eliben <[EMAIL PROTECTED]> wrote:
>
>
>
> > Hello,
>
> > I'm getting into Python now after years of Perl, and as part of my
> > research I must understand how to do some common tasks I need.
>
> > I have a bunch of Windows PCs at work to which I want to distribute an
> > application I've developed on my PC. All these PCs have Python 2.5
> > installed.
>
> > If my application contains only code I've developed, I simply zip its
> > directory with .py files and send it to everyone, who can then use it
> > by running the entry-point .py file. However, what if I've installed
> > some 3rd party modules on my PC, and my application uses them (for
> > example pyparsing, PiYAML and some others) ? I don't want to manually
> > install all these packages (there may be dozens of them) on all those
> > PCs (there may be dozens of those too). What is the best method I can
> > use ? Naturally, I want all the non-standard packages my app uses to
> > be detected automatically and collected into some kind of convenient
> > distributable that is easy to pass around and run.
>
> > I'm aware of py2exe - tried it and it works fine. But it creates huge
> > executables, and I don't want to distribute those all the time. I much
> > prefer a zipped directory of .py scripts that takes some 10s of KBs.
>
> > Thanks in advance,
> > Eli
>
> You might want to check out Buildout. It allows you to run all that
> stuff in it's own virtual world, so to speak. I'm planning on playing
> with it this week.
>

I looked at its examples and I'm not sure it's what I need. It seems
useful for other cases though.

> Also, there's this site which has collected the various means of
> distributing Python apps:
>
> http://www.freehackers.org/Packaging_a_python_program
>

This page only talks about the packagers that create .exe files that
don't need Python installed.


Is there a simple way to find out which packages are used by my
script ?

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


ANN: Pyrex 0.9.8.1

2008-05-17 Thread greg

Pyrex 0.9.8.1 is now available:

  http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/

Base classes no longer need to be specified in a forward
declaration of an extension type, or in the implementation
part of an extension type defined in a .pxd file.

Also, I've come up with an even better way of handling
circular cimports involving structs, unions and extension
types. You can now say things like

  from spam import struct Foo, union Blarg, class Ham

This simultaneously imports the name and forward-declares
it in the other module.


What is Pyrex?
--

Pyrex is a language for writing Python extension modules.
It lets you freely mix operations on Python and C data, with
all Python reference counting and error checking handled
automatically.
--
http://mail.python.org/mailman/listinfo/python-list


comparison of files using set function

2008-05-17 Thread Beema shafreen
I have files with two column, column 1 is with id and column 2 is with
data(sequence)
My goal is to create a table in such a way, the column one of the table
should have all the id from the files and next column will be have the
respective seq of the file1 with correlation to the id and the third column
will be sequence information of the next file with respective to the id
original files look like this

45ytut
46erete
37   dfasf
45  dassdsd


and so on  for all the 10 files that is it has two column as mentioned
above.

The output should look like this:

Idfile1  file2 file3 file4   file5
43ytuhytuh ytuhytuhytuh
46   erteee   rty   ryyy  ertyu
47   yutiorrreeerr



The goal is if the pick all the common id in the files and with their
respective information in the adjacent rows.
the various conditons ca also prevails
1) common id present in all the files, which have same information
2)common id present in all the files, which donot have same information
3) common id may not be present in all the files

But the goal is exactly find the common id in all the files and add their
corresponding information in the file to the table as per the view
 my script :
def file1_search(*files1):
for file1 in files1:
gi1_lis = []
fh = open(file1,'r')
for line in fh.readlines():
data1 = line.strip().split('\t')
gi1 = data1[0].strip()
seq1 = data1[1].strip()
gi1_lis.append(gi1)
return gi1_lis
def file2_search(**files2):
for file2 in files2:
for file in files2[file2]:
gi2_lis = []
fh1 = open(file,'r')
for line1 in fh1.readlines():
data2 = line1.strip().split('\t')
gi2 = data2[0].strip()
seq2 = data2[1].strip()
gi2_lis.append(gi2)

return gi2_lis
def set_compare(data1,data2,*files1,**files2):
A = set(data1)
B = set(data2)
I = A&B # common between thesetwo sets

D = A-B #57 is the len of D
C = B-A #176 is  the len of c
#print len(C)
 #   print len(D)
for file1 in files1:
for gi in D:
fh = open(file1,'r')
for line in fh.readlines():
data1 = line.strip().split('\t')
gi1 = data1[0].strip()
seq1 = data1[1].strip()
if gi == gi1:
#print line.strip()
pass

for file2 in files2:
for file in files2[file2]:
for gi in C:
fh1 = open(file,'r')
for line1 in fh1.readlines():
data2 = line1.strip().split('\t')
gi2 = data2[0].strip()
seq2 = data2[1].strip()
if gi == gi2:
   # print line1.strip()
pass
if __name__ == "__main__":
files1 = ["Fr20.txt",\
  "Fr22.txt",\
  "Fr24.txt",\
  "Fr60.txt",\
  "Fr62.txt"]
files2 = {"data":["Fr64.txt",\
  "Fr66.txt",\
  "Fr68.txt",\
  "Fr70.txt",\
  "Fr72.txt"]}
data1 = file1_search(*files1)

"""113 is the total number of gi"""
data2 = file2_search(**files2)
#for j in data2:
 #   print j
"""232 is the total number of gi found"""
result = set_compare(data1,data2,*files1,**files2)

 It doesnot work fine... some body please suggest me the way i can proceed .
Thanks a lot

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

Re: "indexed properties"...

2008-05-17 Thread David C. Ullrich
On Sat, 17 May 2008 00:27:31 -0300, "Gabriel Genellina"
<[EMAIL PROTECTED]> wrote:

>(warning: it's a rather long message)
>
>En Fri, 16 May 2008 12:58:46 -0300, David C. Ullrich  
><[EMAIL PROTECTED]> escribió:
>> On Thu, 15 May 2008 10:59:41 -0300, "Gabriel Genellina"
>> <[EMAIL PROTECTED]> wrote:
>>> En Wed, 14 May 2008 18:15:41 -0300, David C. Ullrich  
>>> <[EMAIL PROTECTED]> escribió:
>>>
 The other day I saw a thread where someone asked
 about overrideable properties and nobody offered
 the advice that properties are Bad. So maybe we've
 got over that. I suppose properties could have
 Bad consequences if a user doesn't know they exist
 and think that a certain property of an object is
 just an ordinary attribute. But that applies to
 almost any aspect of any language.
>>>
>>> Which "bad consequences" are you thinking of?
>>
>> Didn't have anything specific in mind - there I was
>> just playing devil's advocate. I've seen a lot of people
>> here say things like "properties should only be used
>> when refactoring" without any explanation that I could
>> see of why properties were bad things. I was referring
>> to whatever problems they had in mind.
>
>I suppose those people were talking about this usage:
>
>   def getfoo(self): return self._foo
>   def setfoo(self, value): self._foo = value
>   foo = property(getfoo, setfoo)

Yeah. _If_ that's what I had in mind then telling me Python
is not Java would have been a great service. 

If I recall correctly (maybe not, haven't done any OP in
a long time) this is one of many things that OP gets righter
than Java: you can have the "read" or "write" for a property
refer to a method _or_ to an actual field; in the second
case access compiles to the same thing as in direct field
access.

>Just use a plain foo attribute instead. Almost nobody will notice the  
>difference, and it's faster, easier to maintain, easier to test, less  
>magic... But if you *do* have more to do besides handling the _foo  
>attribute, using a property is OK, at least for me. And from your other  
>comments, I can see it's fine for you too.
>
 If a person comes from, say, Object Pascal (Delphi)
 then properties are hard to live without. The
>>>
>>> You should read the article "Python is not Java" if you haven't done it  
>>> yet.
>>> http://dirtsimple.org/2004/12/python-is-not-java.html
>>
>> I suspect you misunderstood my point there. I actually read that
>> article years ago. Been using Python for years. I'd never confuse
>> it with OP, I promise (luckily I don't know any Java and don't
>> intend to). I really didn't express what I meant very well - what
>> I should have said was more like "If you're accustomed to properties
>> from a language like OP they seem like a very useful addition to
>> Python".
>
>Oh, sure. I missed them a lot until they became available in Python. And I  
>even wrote a fake property mixin class for Python 2.1 (horrible and slow!).
>Now I understand better what you're saying.
>
>>   window.pos = (x,y)
>>
>> seems more natural than
>>
>>   window.SetPos(x,y);
>>
>> in these cases the work involved in changing the cell
>> value or the window position is going to make the
>> extra overhead of the property interface irrelevant.
>
>Sure, that looks like a good candidate for using a property.
>
>>> But I prefer to have as little magic as possible on my objects: if it  
>>> says m.row[0] = [1,2,3] I
>>> expect m.row[0] to actually *be* that list (whenever possible), and not  
>>> any other object initialized
>>> from the [1,2,3] arguments. (Maybe this is some overreaction against  
>>> C++ "magic" constructors and such horrible things...)
>>
>> Whatever - the idea here is that m.row[0] is going to walk and quack
>> exactly like that list would, but also do other things that the liist
>> can't do, like you can add two of them.
>>
>> (When you say anobject.aproperty = 2 you also expect aproperty to
>> be 2? But if aproperty is a property that's not necessarily so - this
>> seems like as much an argument against properties in general as
>> against my version. Or perversion, if you like.)
>
>No, I don't necesarily expect it, but I try to maintain that behavior as  
>long as I can; I try to keep things simple and intuitive. This is a rather  
>used idiom:
>
>   items = foo.items = []
>   for x in someiterator:
> if somecondition:
>   ...
>   items.append(x)
>   ...
>
>That works fine if foo.items is items - but if items were a property that  
>holds its own internal list, the code above would be broken. In that case,  
>replacing a simple attribute with a property would *not* be equivalent;  
>and I try to follow the "principle of least astonishment" and avoid such  
>situations as long as possible.

Right. Except that for the implementation I have in mind this
moring that code will work just fine. foo.items is going to be
a property of foo, so foo.items = [] will ...

Um. I just realized I have no idea what happen

Re: Distributing applications that use 3rd party modules

2008-05-17 Thread John Machin

eliben wrote:

Hello,

I'm getting into Python now after years of Perl, and as part of my
research I must understand how to do some common tasks I need.

I have a bunch of Windows PCs at work to which I want to distribute an
application I've developed on my PC. All these PCs have Python 2.5
installed.

If my application contains only code I've developed, I simply zip its
directory with .py files and send it to everyone, who can then use it
by running the entry-point .py file. However, what if I've installed
some 3rd party modules on my PC, and my application uses them (for
example pyparsing, PiYAML and some others) ? I don't want to manually
install all these packages (there may be dozens of them) on all those
PCs (there may be dozens of those too). What is the best method I can
use ? Naturally, I want all the non-standard packages my app uses to
be detected automatically and collected into some kind of convenient
distributable that is easy to pass around and run.

I'm aware of py2exe - tried it and it works fine. But it creates huge
executables, and I don't want to distribute those all the time. I much
prefer a zipped directory of .py scripts that takes some 10s of KBs.


What do you call "huge"?

In my environment the end-users don't have Python installed on their own 
PCs, but the team does have a networked shared drive; we put py2exe'd 
applications there. Less complaints about app load time than for things 
like Word/Excel/Outlook, and no complaints from me about distribution, 
and no worries about users running old versions.


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


Re: Help on thread pool

2008-05-17 Thread Alex
On May 17, 2:23 pm, Jeff <[EMAIL PROTECTED]> wrote:
> Your worker threads wait around forever because there is no place for
> them to exit.  Queue.get() by default blocks until there is an item in
> the queue available.  You can do something like this to cause the
> worker to quit when the queue is empty.  Just make sure that you fill
> the queue before starting the worker threads.
>
> from Queue import Queue, Empty
>
> # in your worker
> while True:
>   try:
> item = q.get(block=False)
>   except Empty:
> break
>   do_something_with_item()
>   q.task_done()
>
> You can also use a condition variable and a lock or a semaphore to
> signal the worker threads that all work has completed.

Thanks a lot, it works!


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


Re: Classmethods are evil

2008-05-17 Thread Hans Nowak

Ivan Illarionov wrote:
After re-reading "Python is not Java" I finally came to conclusion that 
classmethods in Python are a very Bad Thing. 

I can't see any use-case of them that couldn't be re-written more clearly 
with methods of metaclass or plain functions.


I agree with your sentiments, although I'm not sure I would pick metaclasses 
over class methods... or over anything at all, for that matter. :-)



They have the following issues:
1. You mix instance-level and class-level functionality in one place 
making your code a mess.

2. They are slower than metaclass methods or plain functions.


The way I see it, a class method is really just sugar for a function operating 
on the class, living in the class namespace.  As such, they are basically 
redundant, and as you point out, they can always be replaced by a function 
outside the class (and in fact, this was what people did before they came along 
in 2.2).  Personally, I don't use them... but some people like them.  Different 
strokes, and all that...


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


Re: help compiling Python on vs 2008!

2008-05-17 Thread Christian Heimes
inhahe schrieb:
> what is going on here, and how do I compile these external libraries? 

I assume you are trying to compile Python 2.5 with VS 9.0. It's not
supported. Some extensions don't compile under VS 9.0

Christian

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


Re: default gettext localedir on windows

2008-05-17 Thread Thorsten Kampe
* ZeeGeek (Sun, 4 May 2008 10:56:52 -0700 (PDT))
> On May 5, 1:16 am, Thorsten Kampe <[EMAIL PROTECTED]> wrote:
> > * ZeeGeek (Sun, 4 May 2008 08:59:05 -0700 (PDT))
> >
> > > Hi, what's the default localedir for gettext module on windows? In
> > > Linux, it's /usr/share/locale. Where should I put the *.mo file in
> > > order to make the translation work?
> >
> > %PYTHONHOME%\share\locale
> 
> I tried moving the *.mo file into %PYTHONHOME%\share\locale\zh_CN
> \LC_MESSAGES, but still no luck. The following is the code snippet I
> use:
> 
> import gettext
> gettext.install('testprogram', unicode = True)

The syntax is correct. Is the test program localised under Linux or 
Cygwin? If not then the error is somewhere in your application.

How did you try to set the language on Windows? It works for me on Vista 
with "set LANG=de" or "set LANGUAGE=de" while I think under XP it had to 
be "set LANG=de_DE" (LANGUAGE or de alone did not work if I remember 
correctly).

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

Re: Distributing applications that use 3rd party modules

2008-05-17 Thread Mike Driscoll
On May 17, 4:42 am, eliben <[EMAIL PROTECTED]> wrote:
> Hello,
>
> I'm getting into Python now after years of Perl, and as part of my
> research I must understand how to do some common tasks I need.
>
> I have a bunch of Windows PCs at work to which I want to distribute an
> application I've developed on my PC. All these PCs have Python 2.5
> installed.
>
> If my application contains only code I've developed, I simply zip its
> directory with .py files and send it to everyone, who can then use it
> by running the entry-point .py file. However, what if I've installed
> some 3rd party modules on my PC, and my application uses them (for
> example pyparsing, PiYAML and some others) ? I don't want to manually
> install all these packages (there may be dozens of them) on all those
> PCs (there may be dozens of those too). What is the best method I can
> use ? Naturally, I want all the non-standard packages my app uses to
> be detected automatically and collected into some kind of convenient
> distributable that is easy to pass around and run.
>
> I'm aware of py2exe - tried it and it works fine. But it creates huge
> executables, and I don't want to distribute those all the time. I much
> prefer a zipped directory of .py scripts that takes some 10s of KBs.
>
> Thanks in advance,
> Eli

You might want to check out Buildout. It allows you to run all that
stuff in it's own virtual world, so to speak. I'm planning on playing
with it this week.

Also, there's this site which has collected the various means of
distributing Python apps:

http://www.freehackers.org/Packaging_a_python_program

Hope that helps!

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


Re: Help on thread pool

2008-05-17 Thread Jeff
Your worker threads wait around forever because there is no place for
them to exit.  Queue.get() by default blocks until there is an item in
the queue available.  You can do something like this to cause the
worker to quit when the queue is empty.  Just make sure that you fill
the queue before starting the worker threads.

from Queue import Queue, Empty

# in your worker
while True:
  try:
item = q.get(block=False)
  except Empty:
break
  do_something_with_item()
  q.task_done()

You can also use a condition variable and a lock or a semaphore to
signal the worker threads that all work has completed.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Markov Analysis Help

2008-05-17 Thread bearophileHUGS
dave, few general comments to your code:
- Instead of using a comment that explains the meaning of a function,
add such things into docstrings.
- Your names can be improved, instead of f you can use file_name or
something like that, instead of convert_file you can use a name that
denotes that the conversion is already done, etc.
- You can use xrange instead of range and you can indent less, like 4
spaces.
- This line may be slow, you may want to find simpler ways to do the
same thing:
rkey = random.choice(analyze.keys())
- I suggest you to add doctests to all your functions.

Bye,
bearophile
--
http://mail.python.org/mailman/listinfo/python-list


Install man-pages with distutils/setuptools

2008-05-17 Thread Hartmut Goebel

Hi,

is there a standard way or a snippet for installing man-pages with

  python set.py install

when using distutils or setuptools? I have not found one when searching 
the web.


--
Schönen Gruß - Regards
Hartmut Goebel

Goebel Consult
Spezialist für IT-Sicherheit in komplexen Umgebungen
http://www.goebel-consult.de
--
http://mail.python.org/mailman/listinfo/python-list


Help on thread pool

2008-05-17 Thread Alex
Hi all.

In order to understand the concept of threading pool in python I'm
working on a simple single-site web crawler.
I would like to stop the program when the threading pool have
downloaded all internal links from a web site, but now my program keep
waiting forever even if there are no more links to download.

Here's my code, I appreciate any comments, I'm programming just for
fun and learning ;-)

Thanks in advance.

from BeautifulSoup import BeautifulSoup
import urllib
from pprint import pprint
import string
from urlparse import urlparse
import sys
from threading import Thread
import time
from Queue import Queue

#dirty hack: set default encoding to utf-8
reload(sys)
sys.setdefaultencoding('utf-8')

opener = urllib.FancyURLopener({})

class Crawler:

def __init__(self):
"""
Constructor
"""
self.missed = 0
self.url_list = []
self.urls_queue = Queue()
self.num_threads = 5

self._create_threads()

def get_internal_links(self,url):
"""
Get all internal links from a web page and feed the queue
"""
self.url = url
url_netloc = urlparse(self.url).netloc
print "Downloading... ", self.url
time.sleep(5)
try:
p = opener.open(self.url)
#print p.info()
except IOError:
print "error connecting to ", self.url
print "wait..."
time.sleep(5)
print "retry..."
try:
p = urllib.urlopen(self.url)
except IOError:
  self.missed = self.missed + 1
  return None

html = p.read()
soup = BeautifulSoup(html)
anchors = soup.findAll('a')
links = [ str(anchor['href']) for anchor in anchors]
internal_links = [link for link in links if
(urlparse(link).netloc == url_netloc)]

for link in internal_links:
if link not in self.url_list and link != self.url:
self.url_list.append(link)
self.urls_queue.put(link)
print "Queue size: ", self.urls_queue.qsize()
print "List size: ", str(len(self.url_list))
print "Errors: ", str(self.missed)
self._queue_consumer()


def _queue_consumer(self):
"""
Consume the queue
"""
while True:
url = self.urls_queue.get()
print 'Next url: ', url
self.get_internal_links(url)
self.urls_queue.task_done()


def _create_threads(self):
"""
Set up some threads to fetch pages
"""
for i in range(self.num_threads):
worker = Thread(target=self._queue_consumer, args=())
worker.setDaemon(True)
worker.start()

#-
#

if __name__ == '__main__':

c = Crawler()
c.get_internal_links('http://www.thinkpragmatic.net/')


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


Re: morning in Python

2008-05-17 Thread Ivan Illarionov
On Sat, 17 May 2008 02:57:08 -0700, castironpi wrote:

> Full day later, I think it, to emphasize state, would prioritize
> context.  The reason for the huge ramble was, believe it or not,
> namespace conflict... as though any other states around here might nose
> in.  And thanks to 'inhahe' for coming back with the question.  ...Which
> would explain next move to 'prioritize context'. Context is a high
> priority for people.
> 
> I'm proposing to start on one on a computer.  The first things a
> computer 'knows' are the boot, keyboard, & mouse.  Yet on another scale,
> the first things it knows are BIOS, file system, and OS.  On still
> another, the only thing it knows are interruptions.  Knowledge is
> important to context.  (The scales are ram on disk on, ram on disk off,
> and ram off, which may tell of the currency they and their power are
> bought with.  Thence, we should be getting different values for lengths
> of time.)
> 
> (Furthermore, we're all on different longitudes -and- latitudes.)
> 
> Context comes from motion, perception, and composite perception
> (reperception e.a.o. memory).  There is some reason to believe that
> motion and sight are different senses, perhaps so with stationary sound
> (gatcha) and mobile sound too.  Do you go deaf of a tone after prolonged
> duration?  That makes computers valuable commodities*: they have a
> symbolic interface, which no other unlive objects have.  They have both
> mouse and keyboard.
> 
> *I'm sure there is a precision to wants: what magnitude of what types of
> action a person wants from a day and for a time-- what energy states
> they go to and from (note phone on come to and come from.)
> 
> Therefore, context should originate in mouse and keyboard.
> 
> Humans have symbolic know-how: knowledge of how to convey intent
> digitally, though it may be there is no interpolation of 'intent per
> mouse-or-key', even though people are prone to empathize with faces.
> However, if you start with a 'me' and a 'no', you can get pretty
> logical.
> 
> Intent per mouse-and-key isn't necessarily scalar, three-dimensional, or
> rationally dimensional (?), though they do have magnitudes per mass and
> volume.  The contingent of 'rationally dimensional' is having or
> beknowing/benouncing an orthonormal basis.  Incidentally, '''orthography
> of a language specifies the correct way of using a specific writing
> system to write the language. .. Orthography is derived from Greek ὀρθός
> orthós ("correct") and γράφειν gráphein ("to write").''' - wikipedia.
> 
> Further incidentally, context and state may have more important in
> common than priority and price: privacy and safety are involved ex
> hypothesi.  Incidentally = ...
> 
> It is not clear that the first (cheapest best) human-computer language
> is a computer language, though if two were orthonormal in comparison to
> life, Python's fine.  Not my first.
> 
> In privacy concerns, it is not clear that duals aren't primitives to
> humans.  What's a brain primitive?  Lol: what is a primitive brain?
> 
> 
> 
> 
> On May 16, 10:58 am, "inhahe" <[EMAIL PROTECTED]> wrote:
>> I'm not an expert in this but what does it mean to emphasize state?  It
>> seems the opposite of that would be a) functional programming, and b)
>> passing parameters instead of using global or relatively local
>> variables. And maybe c) coroutines (generators as implemented in
>> Python), although perhaps coroutines could be said to emphasize state
>> inasmuch as they go out of their way to capture, objectify and reuse it
>> (Stackless' microthreads, even moreso).  And Python seems to be
>> well-noted for implementing some functional programming methodology,
>> and as for passing parameters it's just as object-oriented as the rest
>> of them.
>>
>> But as I said, I'm not an expert, so let me know if I've gone astray..
>>
>>
>>
>> > I have a proposition to ask you all: Python emphasizes state.  Is it
>> > true?- Hide quoted text -
>>
>> - Show quoted text -

Castironpi,

I love you! You remind me of all the kittens and puuppies I had when I 
was a child.

I #define this. Hope your database could give us something funny again.

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

Re: ?

2008-05-17 Thread Paul Hankin
On May 17, 11:08 am, Ruediger <[EMAIL PROTECTED]> wrote:
> urikaluzhny wrote:
> > It seems that I rather frequently need a list or iterator of the form
> > [x for x in <> while <>]
> > And there is no one like this.
> > May be there is another short way to write it (not as a loop). Is
> > there?
> > Thanks
>
> I usually have the same problem and i came up with an solution like that:
>
> from operator import ne
> def test(iterable, value, op=ne):
>     _n = iter(iterable).next
>     while True:
>         _x = _n()
>         if op(_x, value):
>             yield _x
>         else:
>             raise StopIteration

This is better written using takewhile...
itertools.takewhile(lambda x: x != value, iterable)

But if you really need to reinvent the wheel, perhaps this is simpler?

def test(iterable, value, op=operator.ne):
for x in iterable:
if not op(x, value):
return
yield x

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


Re: Instance of class "object"

2008-05-17 Thread castironpi
On May 17, 1:09 am, Carl Banks <[EMAIL PROTECTED]> wrote:
> On May 16, 4:46 am, "甜瓜" <[EMAIL PROTECTED]> wrote:
>
> > Howdy,
> > I wonder why below does not work.
>
> > a = object()
> > a.b = 1# dynamic bind attribute failed...
>
> > To make it correct, we have to create a new class:
> > class MyClass(object): pass
> > a = MyClass()
> > a.b = 1   # OK
>
> > Does this strange behavior break the LSP (Liskov substitution principle)?
>
> I suspect it's possible to come up with a property that causes LSP to
> be violated for any two given distinct classes.
>
> For example, if the property you're considering is "Raises an
> exception when you try to access an attribute", then yes, the subtype
> cannot be substituted for the base type and would violate LSP.
> However, that's not really such a useful property in most cases.
>
> The reason instances of object don't have dynamic attributes is
> because dyanmic attributes are internally represented by a dict
> object.  To keep memory footprint low, instances of many built-in
> types don't have an associated dict and so can't bind attributes
> dynamically.  Instances of object can't have an associated dict
> because, if they did, it would mean all objects would have to have an
> associated dict, because all types are subclasses of object.
>
> Because of this, direct instances of object really don't have much use
> in Python.  It's one main use case is as a sentinel or marker of some
> sort.  It's only useful property is that it is itself, and nothing
> else.  But, that property is true of all instances in Python;
> therefore any instance may be substituted for an object instances,
> therefore it satisfies LSP.
>
> (Phew: what a tangle of nomenclature that was.)
>
> Carl Banks

Python is the same language on sufficiently many distinct machines
that all the people can use it if they want.  That means identical
outputs.  Do their 100%-ers go with the times?
--
http://mail.python.org/mailman/listinfo/python-list


Re: [x for x in <> while <>]?

2008-05-17 Thread Ruediger
urikaluzhny wrote:

> It seems that I rather frequently need a list or iterator of the form
> [x for x in <> while <>]
> And there is no one like this.
> May be there is another short way to write it (not as a loop). Is
> there?
> Thanks

I usually have the same problem and i came up with an solution like that:

from operator import ne
def test(iterable, value, op=ne):
_n = iter(iterable).next
while True:
_x = _n()
if op(_x, value):
yield _x
else:
raise StopIteration

l = range(6)
print [x for x in test(l, 4)]

[EMAIL PROTECTED]:~/tmp> python test18.py
[0, 1, 2, 3]

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


Re: morning in Python

2008-05-17 Thread castironpi
Full day later, I think it, to emphasize state, would prioritize
context.  The reason for the huge ramble was, believe it or not,
namespace conflict... as though any other states around here might
nose in.  And thanks to 'inhahe' for coming back with the
question.  ...Which would explain next move to 'prioritize context'.
Context is a high priority for people.

I'm proposing to start on one on a computer.  The first things a
computer 'knows' are the boot, keyboard, & mouse.  Yet on another
scale, the first things it knows are BIOS, file system, and OS.  On
still another, the only thing it knows are interruptions.  Knowledge
is important to context.  (The scales are ram on disk on, ram on disk
off, and ram off, which may tell of the currency they and their power
are bought with.  Thence, we should be getting different values for
lengths of time.)

(Furthermore, we're all on different longitudes -and- latitudes.)

Context comes from motion, perception, and composite perception
(reperception e.a.o. memory).  There is some reason to believe that
motion and sight are different senses, perhaps so with stationary
sound (gatcha) and mobile sound too.  Do you go deaf of a tone after
prolonged duration?  That makes computers valuable commodities*: they
have a symbolic interface, which no other unlive objects have.  They
have both mouse and keyboard.

*I'm sure there is a precision to wants: what magnitude of what types
of action a person wants from a day and for a time-- what energy
states they go to and from (note phone on come to and come from.)

Therefore, context should originate in mouse and keyboard.

Humans have symbolic know-how: knowledge of how to convey intent
digitally, though it may be there is no interpolation of 'intent per
mouse-or-key', even though people are prone to empathize with faces.
However, if you start with a 'me' and a 'no', you can get pretty
logical.

Intent per mouse-and-key isn't necessarily scalar, three-dimensional,
or rationally dimensional (?), though they do have magnitudes per mass
and volume.  The contingent of 'rationally dimensional' is having or
beknowing/benouncing an orthonormal basis.  Incidentally,
'''orthography of a language specifies the correct way of using a
specific writing system to write the language. .. Orthography is
derived from Greek ὀρθός orthós ("correct") and γράφειν gráphein ("to
write").''' - wikipedia.

Further incidentally, context and state may have more important in
common than priority and price: privacy and safety are involved ex
hypothesi.  Incidentally = ...

It is not clear that the first (cheapest best) human-computer language
is a computer language, though if two were orthonormal in comparison
to life, Python's fine.  Not my first.

In privacy concerns, it is not clear that duals aren't primitives to
humans.  What's a brain primitive?  Lol: what is a primitive brain?




On May 16, 10:58 am, "inhahe" <[EMAIL PROTECTED]> wrote:
> I'm not an expert in this but what does it mean to emphasize state?  It
> seems the opposite of that would be a) functional programming, and b)
> passing parameters instead of using global or relatively local variables.
> And maybe c) coroutines (generators as implemented in Python), although
> perhaps coroutines could be said to emphasize state inasmuch as they go out
> of their way to capture, objectify and reuse it (Stackless' microthreads,
> even moreso).  And Python seems to be well-noted for implementing some
> functional programming methodology, and as for passing parameters it's just
> as object-oriented as the rest of them.
>
> But as I said, I'm not an expert, so let me know if I've gone astray..
>
>
>
> > I have a proposition to ask you all: Python emphasizes state.  Is it
> > true?- Hide quoted text -
>
> - Show quoted text -

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

Re: Classmethods are evil

2008-05-17 Thread Ivan Illarionov
On Sat, 17 May 2008 02:33:13 -0300, Gabriel Genellina wrote:

> En Sat, 17 May 2008 01:01:50 -0300, Ivan Illarionov
> <[EMAIL PROTECTED]> escribió:
> 
>> After re-reading "Python is not Java" I finally came to conclusion that
>> classmethods in Python are a very Bad Thing.
>>
>> I can't see any use-case of them that couldn't be re-written more
>> clearly with methods of metaclass or plain functions.
> 
> A good use case for class methods are alternate constructors, like
> dict.from_keys. I don't think an alternate constructor would be more
> clear being a method of the metaclass - actually it belongs to the class
> itself, not to its metaclass.
> Metaclass methods are harder to find; they don't show in dir(instance)
> nor dir(class).
> Also the resolution order is harder to grasp for metaclasses - but this
> may be just lack of usage from my part...
> 
>> They have the following issues:
>> 1. You mix instance-level and class-level functionality in one place
>> making your code a mess.
> 
> Not necesarily; some classmethods are naturally tied to the class
> itself, not to the metaclass (like the constructor example above). But
> yes, *some* classmethods could be written as methods of their metaclass
> instead - but that doesn't always make sense.
> 
>> 2. They are slower than metaclass methods or plain functions.
> 
> Hu? How did you come to that?
> I've done a small test and a class method wins by a very minuscule but
> consistent advantage over a metaclass method:
> 
> class A(object):
>  color = "red"
> 
>  @classmethod
>  def foo(cls, x):
>  return getattr(cls, x)
> 
> class MetaB(type):
>  def foo(self, x):
>  return getattr(self, x)
> 
> class B(object):
>  __metaclass__ = MetaB
>  color = "red"
> 
> C:\TEMP>python -m timeit -s "from meta3 import A,B;a,b=A(),B()"
> "A.foo('color')"
> 100 loops, best of 3: 1.19 usec per loop
> 
> C:\TEMP>python -m timeit -s "from meta3 import A,B;a,b=A(),B()"
> "B.foo('color')"
> 100 loops, best of 3: 1.2 usec per loop

How did I come to this:
http://code.djangoproject.com/changeset/7098

I measured this and there was a marginal speed increase when classmethods 
wher moved to metaclass.

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

Distributing applications that use 3rd party modules

2008-05-17 Thread eliben
Hello,

I'm getting into Python now after years of Perl, and as part of my
research I must understand how to do some common tasks I need.

I have a bunch of Windows PCs at work to which I want to distribute an
application I've developed on my PC. All these PCs have Python 2.5
installed.

If my application contains only code I've developed, I simply zip its
directory with .py files and send it to everyone, who can then use it
by running the entry-point .py file. However, what if I've installed
some 3rd party modules on my PC, and my application uses them (for
example pyparsing, PiYAML and some others) ? I don't want to manually
install all these packages (there may be dozens of them) on all those
PCs (there may be dozens of those too). What is the best method I can
use ? Naturally, I want all the non-standard packages my app uses to
be detected automatically and collected into some kind of convenient
distributable that is easy to pass around and run.

I'm aware of py2exe - tried it and it works fine. But it creates huge
executables, and I don't want to distribute those all the time. I much
prefer a zipped directory of .py scripts that takes some 10s of KBs.

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


Re: Getting elements and text with lxml

2008-05-17 Thread John Machin

J. Pablo Fernández wrote:

On May 17, 2:19 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
En Fri, 16 May 2008 18:53:03 -0300, J. Pablo Fernández <[EMAIL PROTECTED]>  
escribió:





Hello,
I have an XML file that starts with:



  *-a

out of it, I'd like to extract something like (I'm just showing one
structure, any structure as long as all data is there is fine):
[("ofc", "*"), "-", ("rad", "a")]
How can I do it? I managed to get the content of boths tags and the
text up to the first tag ("\n   "), but not the - (and in other XML
files, there's more text outside the elements).

Look for the "tail" attribute.


That gives me the last part, but not the one in the middle:

In : etree.tounicode(e)
Out: u'\n  *-a\n\n'

In : e.text
Out: '\n  '

In : e.tail
Out: '\n'



You need the text content of your initial element's children, which 
needs that of their children, and so on.


See http://effbot.org/zone/element-bits-and-pieces.htm

HTH,
John


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


Re: how would you...?

2008-05-17 Thread Sanoski
The reason I ask about text files is the need to save the data
locally, and have it stored in a way where backups can easily be made.
Then if your computer crashes and you lose everything, but you have
the data files it uses backed up, you can just download the program,
extract the backed up data to a specific directory, and then it works
exactly the way it did before you lost it. I suppose a SQLite database
might solve this, but I'm not sure. I'm just getting started, and I
don't know too much about it yet.

I'm also still not sure how to download and associate the pictures
that each entry has for it. The main thing for me now is getting
started. It needs to get information from the web. In this case, it's
a simple XML feed. The one thing that seems that would make it easier
is every post to the feed is very consistent. Each header starts with
the letter A, which stands for Alpike Tech, follow by the name of the
class, the room number, the leading student, and his GPA. All that is
one line of text. But it's also a link to more information. For
example:

A Economics, 312, John Carbroil, 4.0
That's one whole post to the feed. Like I say, it's very simple and
consistent. Which should make this easier.

Eventually I want it to follow that link and grab information from
there too, but I'll worry about that later. Technically, if I figure
this first part out, that problem should take care of itself.





On May 17, 1:08 am, Mensanator <[EMAIL PROTECTED]> wrote:
> On May 16, 11:43�pm, Sanoski <[EMAIL PROTECTED]> wrote:
>
>
>
> > I'm pretty new to programming. I've just been studying a few weeks off
> > and on. I know a little, and I'm learning as I go. Programming is so
> > much fun! I really wish I would have gotten into it years ago, but
> > here's my question. I have a longterm project in mind, and I wont to
> > know if it's feasible and how difficult it will be.
>
> > There's an XML feed for my school that some other class designed. It's
> > just a simple idea that lists all classes, room number, and the person
> > with the highest GPA. The feed is set up like this. Each one of the
> > following lines would also be a link to more information about the
> > class, etc.
>
> > Economics, Room 216, James Faker, 3.4
> > Social Studies, Room 231, Brain Fictitious, 3.5
>
> > etc, etc
>
> > The student also has a picture reference that depicts his GPA based on
> > the number. The picture is basically just a graph. I just want to
> > write a program that uses the information on this feed.
>
> > I want it to reach out to this XML feed, record each instance of the
> > above format along with the picture reference of the highest GPA
> > student, download it locally, and then be able to use that information
> > in various was. I figured I'll start by counting each instance. For
> > example, the above would be 2 instances.
>
> > Eventually, I want it to be able to cross reference data you've
> > already downloaded, and be able to compare GPA's, etc. It would have a
> > GUI and everything too, but I am trying to keep it simple right now,
> > and just build onto it as I learn.
>
> > So lets just say this. How do you grab information from the web,
>
> Depends on the web page.
>
> > in this case a feed,
>
> Haven't tried that, just a simple CGI.
>
> > and then use that in calculations?
>
> The key is some type of structure be it database records,
> or a list of lists or whatever. Something that you can iterate
> through, sort, find max element, etc.
>
> > How would you
> > implement such a project?
>
> The example below uses BeautifulSoup. I'm posting it not
> because it matches your problem, but to give you an idea of
> the techniques involved.
>
> > Would you save the information into a text file?
>
> Possibly, but generally no. Text files aren't very useful
> except as a data exchange media.
>
> > Or would you use something else?
>
> Your application lends itself to a database approach.
> Note in my example the database part of the code is disabled.
> Not every one has MS-Access on Windows.
>
> > Should I study up on SQLite?
>
> Yes. The MS-Access code I have can be easily changed to SQLlite.
>
> > Maybe I should study classes.
>
> I don't know, but I've always gotten along without them.
>
> > I'm just not sure. What would be the most effective technique?
>
> Don't know that either as I've only done it once, as follows:
>
> ##  I was looking in my database of movie grosses I regulary copy
> ##  from the Internet Movie Database and noticed I was _only_ 120
> ##  weeks behind in my updates.
> ##
> ##  Ouch.
> ##
> ##  Copying a web page, pasting into a text file, running a perl
> ##  script to convert it into a csv file and manually importing it
> ##  into Access isn't so bad when you only have a couple to do at
> ##  a time. Still, it's a labor intensive process and 120 isn't
> ##  anything to look forwards to.
> ##
> ##  But I abandoned perl years ago when I took up Python, so I
> ##  can use Python to completely automate t

Re: save dictionary for later use?

2008-05-17 Thread castironpi
On May 16, 4:23 pm, Hans Nowak <[EMAIL PROTECTED]>
wrote:
> globalrev wrote:
> > pickle.dumps(mg)
> > pickle.load(mg)
>
> > 'dict' object has no attribute 'readline'
> > dumps load(well i dont know but i get no complaint but running load
> > generates that error)
>
> The 'loads' and 'dumps' methods use strings:
>
>  >>> import pickle
>  >>> d = {"this": 42, "that": 101, "other": 17}
>  >>> s = pickle.dumps(d)
>  >>> s
> "(dp0\nS'this'\np1\nI42\nsS'other'\np2\nI17\nsS'that'\np3\nI101\ns."
>  >>> pickle.loads(s)
> {'this': 42, 'other': 17, 'that': 101}
>
> If you want to store to / restore from file, use 'dump' and 'load':
>
> # write to file 'out'...
>  >>> f = open("out")
>  >>> f = open("out", "wb")
>  >>> pickle.dump(d, f)
>  >>> f.close()
>
> # restore it later
>  >>> g = open("out", "rb")
>  >>> e = pickle.load(g)
>  >>> g.close()
>  >>> e
> {'this': 42, 'other': 17, 'that': 101}
>
> Also seehttp://docs.python.org/lib/pickle-example.html.
>
> Hope this helps!
>
> --Hans

I want to compare that cleanliness with other languages to compare
formats.

Is pickle.load( open( 'out', 'rb' ) ) any better or worse than
pickle.load( 'out', 'rb' )?
--
http://mail.python.org/mailman/listinfo/python-list


Re: Getting elements and text with lxml

2008-05-17 Thread J . Pablo Fernández
On May 17, 2:19 am, "Gabriel Genellina" <[EMAIL PROTECTED]>
wrote:
> En Fri, 16 May 2008 18:53:03 -0300, J. Pablo Fernández <[EMAIL PROTECTED]>  
> escribió:
>
>
>
> > Hello,
>
> > I have an XML file that starts with:
>
> > 
> > 
> > 
> >   *-a
> > 
>
> > out of it, I'd like to extract something like (I'm just showing one
> > structure, any structure as long as all data is there is fine):
>
> > [("ofc", "*"), "-", ("rad", "a")]
>
> > How can I do it? I managed to get the content of boths tags and the
> > text up to the first tag ("\n   "), but not the - (and in other XML
> > files, there's more text outside the elements).
>
> Look for the "tail" attribute.

That gives me the last part, but not the one in the middle:

In : etree.tounicode(e)
Out: u'\n  *-a\n\n'

In : e.text
Out: '\n  '

In : e.tail
Out: '\n'

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


Re: can't delete from a dictionary in a loop

2008-05-17 Thread castironpi
On May 17, 3:06 am, George Sakkis <[EMAIL PROTECTED]> wrote:
> On May 16, 5:22 pm, "Dan Upton" <[EMAIL PROTECTED]> wrote:
>
>
>
>
>
> > This might be more information than necessary, but it's the best way I
> > can think of to describe the question without being too vague.
>
> > The task:
>
> > I have a list of processes (well, strings to execute said processes)
> > and I want to, roughly, keep some number N running at a time.  If one
> > terminates, I want to start the next one in the list, or otherwise,
> > just wait.
>
> > The attempted solution:
>
> > Using subprocess, I Popen the next executable in the list, and store
> > it in a dictionary, with keyed on the pid:
> > (outside the loop)
> > procs_dict={}
>
> > (inside a while loop)
> > process = Popen(benchmark_exstring[num_started], shell=true)
> > procs_dict[process.pid]=process
>
> > Then I sleep for a while, then loop through the dictionary to see
> > what's terminated.  For each one that has terminated, I decrement a
> > counter so I know how many to start next time, and then try to remove
> > the record from the dictionary (since there's no reason to keep
> > polling it since I know it's terminated).  Roughly:
>
> > for pid in procs_dict:
> >   if procs_dict[pid].poll() != None
> >    # do the counter updates
> >    del procs_dict[pid]
>
> Since you don't look up processes by pid, you don't need a dictionary
> here. A cleaner and efficient solution is use a deque to pop processes
> from one end and push them to the other if still alive, something like
> this:
>
> from collections import deque
>
> processes = deque()
> # start processes and put them in the queue
>
> while processes:
>     for i in xrange(len(processes)):
>         p = processes.pop()
>         if p.poll() is None: # not finished yet
>             processes.append_left(p)
>     time.sleep(5)
>
> HTH,
> George- Hide quoted text -
>
> - Show quoted text -

No underscore in appendleft.
--
http://mail.python.org/mailman/listinfo/python-list


Re: can't delete from a dictionary in a loop

2008-05-17 Thread George Sakkis
On May 16, 5:22 pm, "Dan Upton" <[EMAIL PROTECTED]> wrote:
> This might be more information than necessary, but it's the best way I
> can think of to describe the question without being too vague.
>
> The task:
>
> I have a list of processes (well, strings to execute said processes)
> and I want to, roughly, keep some number N running at a time.  If one
> terminates, I want to start the next one in the list, or otherwise,
> just wait.
>
> The attempted solution:
>
> Using subprocess, I Popen the next executable in the list, and store
> it in a dictionary, with keyed on the pid:
> (outside the loop)
> procs_dict={}
>
> (inside a while loop)
> process = Popen(benchmark_exstring[num_started], shell=true)
> procs_dict[process.pid]=process
>
> Then I sleep for a while, then loop through the dictionary to see
> what's terminated.  For each one that has terminated, I decrement a
> counter so I know how many to start next time, and then try to remove
> the record from the dictionary (since there's no reason to keep
> polling it since I know it's terminated).  Roughly:
>
> for pid in procs_dict:
>   if procs_dict[pid].poll() != None
>    # do the counter updates
>    del procs_dict[pid]

Since you don't look up processes by pid, you don't need a dictionary
here. A cleaner and efficient solution is use a deque to pop processes
from one end and push them to the other if still alive, something like
this:

from collections import deque

processes = deque()
# start processes and put them in the queue

while processes:
for i in xrange(len(processes)):
p = processes.pop()
if p.poll() is None: # not finished yet
processes.append_left(p)
time.sleep(5)


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


Re: namespaces and eval

2008-05-17 Thread Arnaud Delobelle
[EMAIL PROTECTED] writes:

> On May 16, 2:47 pm, "[EMAIL PROTECTED]"
> <[EMAIL PROTECTED]> wrote:
>> On 16 mai, 23:23, [EMAIL PROTECTED] wrote:
>>
>> > Thanks for the responses.  I'm well aware that the function can be
>> > passed in the parameters, passing in the functino as an arg defeats
>> > the purpose of what I'm going after.
>>
>> Why so ?
>> > @ Arnaud - Nice. I'm not sure what the performance of mine vs. yours,
>> > but a perfunctory glance looks like we're doing the close to the same
>> > thing.  Maybe one advanage to doing it wrap(x).foo() is that you
>> > can pass in other parameters to foo.
>>
>> I may be wrong (if so please pardon my lack of intelligence and/or
>> provide a couple use case), but it looks like you're trying to
>> reinvent partial application.
>>
>> from functools import partial
>> def foo(x, y):
>>   return x + y
>>
>> pfoo = partial(foo, 2)
>> print pfoo(42)
>>
>> Don't know if this helps...
>
> Ok, so that solves the issue of the aforementioned compose function.
> We could do compose( partialfoo,) ) etc (although I might say I
> prefer wrap(x).foo(23).foo(16 ..etc )  The original idea was to
> provide wrapper around an object that lets you call abritrary
> functions on that object with it as a parameter - i.e., as if it were
> a method in a class.  The partial function was only a component of the
> code I posted earlier. Or am I missing something you're saying?

Ok so you want to do

wrap(x).foo(42).bar('spam')...

What is the advantage over

foo(x, 42)
bar(x, 'spam')
...

?

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