Re: finding repeated data sequences in a column

2009-05-21 Thread yadin
On May 20, 6:53 pm, norseman  wrote:
> [email protected] wrote:
> > yadin:
> >> How can I build up a program that tells me that this sequence
> >> 128706
> >> 128707
> >> 128708
> >> is repeated somewhere in the column, and how can i know where?
>
> > Can such patterns nest? That is, can you have a repeated pattern made
> > of an already seen pattern plus something else?
> > If you don't want a complex program, then you may need to specify the
> > problem better.
>
> > You may want something like LZ77 or releated (LZ78, etc):
> >http://en.wikipedia.org/wiki/LZ77
> > This may have a bug:
> >http://code.activestate.com/recipes/117226/
>
> > Bye,
> > bearophile
>
> 
> index on column
> Ndx1 is set to index #1
> Ndx2 is set to index #2
> test Ndx1 against Ndx2
>    if equal write line number and column content to a file
>            (that's two things on one line:  15 128706
>                                            283 128706 )
>    Ndx1 is set to Ndx2
>    Ndx2 is set to index #next
> loop to test    writing out each duplicate set
>
> Then use the outfile and index on line number
>
> In similar manor, check if line current and next line line numbers are
> sequential.  If so scan forward to match column content of lower line
> number and check first matched column's line number and next for
> sequential.  Print them out if so
>
> everything in outfile has 1 or more duplicates
>
> 4  aa               |--
> 5  bb         |--      |      thus 4/5 match 100/101
> 6  cc            |     |
> .                |     |
> 100 aa           |  |--
> 101 bb         |--
> 102 ddd
> 103 cc                  there is a duplicate but not a sequence
> 200 ff
>
> mark duplicate sequences as tested and proceed on through
>    seq1 may have more than one other seq in file.
>    the progress is from start to finish without looking back
>    thus each step forward has fewer lines to test.
>    marking already knowns eliminates redundant sequence testing.
>
> By subseting on pass1 the expensive testing is greatly reduced.
> If you know your subset data won't exceed memory then the "outfile"
> can be held in memory to speed things up considerably.
>
> Today is: 20090520
> no code
>
> Steve- Hide quoted text -
>
> - Show quoted text -

this is the program...I wrote but is not working
I have a list of valves, and another of pressures;
If I am ask to find out which ones are the valves that are using all
this set of pressures, wanted best pressures
this is the program i wrote but is not working properly, it suppossed
to return in the case
find all the valves that are using pressures 1 "and" 2 "and" 3.
It returns me A, A2, A35
The correct answer supposed to be A and A2...
if I were asked for pressures 56 and 78 the correct answer supossed to
be valves G and G2...

Valves = ['A','A','A','G', 'G', 'G',
'C','A2','A2','A2','F','G2','G2','G2','A35','A345','A4'] ##valve names
pressures = [1,2,3,4235,56,78,12, 1, 2, 3, 445, 45,56,78,1, 23,7] ##
valve pressures
result = []

bestpress = [1,2,3] ##wanted base pressures
print bestpress,'len bestpress is' , len(bestpress)

print len(Valves)
print len(Valves)
for j in range(len(Valves)):
#for i in range(len(bestpress)):
#for j in range(len(Valves)):
for i in range(len(bestpress)-2):
if pressures [j]== bestpress[i] and bestpress [i+1]
==pressures [j+1] and bestpress [i+2]==pressures [j+2]:
result.append(Valves[j])
#i = i+1
#j = j+1
# print i, j, bestpress[i]
print "common PSVs are", result
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding a Par construct to Python?

2009-05-21 Thread Paul Rubin
Carl Banks  writes:
> Why?  Do you seriously not see the benefit of simplifying the work of
> extention writers and core maintainers?  You don't have to agree that
> it's a good trade-off but it's a perfectly reasonable goal.
> 
> I highly suspect Aahz here would argue for a GIL even without the
> refcount issue, and even though I wouldn't agree, there's nothing
> weird or unreasonable about the argument, it's just a different
> viewpoint.

How about only setting a lock when a "safe" user extension is called.
There could also be an "unsafe" extension interface with no lock.  The
more important stdlib functions would be (carefully) written with the
unsafe interface.  Ordinary python code would not need a lock.  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Import and absolute file names, sys.path including ''... or not

2009-05-21 Thread greg

Jean-Michel Pichavant wrote:


__import__('/home/jeanmichel/test')


The __import__ function takes *module* names, not
filesystem pathnames.

Giving it a pathname might happen to work some of
the time in some versions of Python, but it's not
an intended feature, and you shouldn't rely on it.

If you want to execute the contents of an arbitrary
file, rather than a module existing somewhere in the
Python module namespace, use execfile().

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


Re: finding repeated data sequences in a column

2009-05-21 Thread Peter Otten
yadin wrote:

> On May 20, 6:53 pm, norseman  wrote:
>> [email protected] wrote:
>> > yadin:
>> >> How can I build up a program that tells me that this sequence
>> >> 128706
>> >> 128707
>> >> 128708
>> >> is repeated somewhere in the column, and how can i know where?
>>
>> > Can such patterns nest? That is, can you have a repeated pattern made
>> > of an already seen pattern plus something else?
>> > If you don't want a complex program, then you may need to specify the
>> > problem better.
>>
>> > You may want something like LZ77 or releated (LZ78, etc):
>> >http://en.wikipedia.org/wiki/LZ77
>> > This may have a bug:
>> >http://code.activestate.com/recipes/117226/
>>
>> > Bye,
>> > bearophile
>>
>> 
>> index on column
>> Ndx1 is set to index #1
>> Ndx2 is set to index #2
>> test Ndx1 against Ndx2
>> if equal write line number and column content to a file
>> (that's two things on one line:  15 128706
>> 283 128706 )
>> Ndx1 is set to Ndx2
>> Ndx2 is set to index #next
>> loop to testwriting out each duplicate set
>>
>> Then use the outfile and index on line number
>>
>> In similar manor, check if line current and next line line numbers are
>> sequential.  If so scan forward to match column content of lower line
>> number and check first matched column's line number and next for
>> sequential.  Print them out if so
>>
>> everything in outfile has 1 or more duplicates
>>
>> 4  aa   |--
>> 5  bb |--  |  thus 4/5 match 100/101
>> 6  cc| |
>> .| |
>> 100 aa   |  |--
>> 101 bb |--
>> 102 ddd
>> 103 cc  there is a duplicate but not a sequence
>> 200 ff
>>
>> mark duplicate sequences as tested and proceed on through
>> seq1 may have more than one other seq in file.
>> the progress is from start to finish without looking back
>> thus each step forward has fewer lines to test.
>> marking already knowns eliminates redundant sequence testing.
>>
>> By subseting on pass1 the expensive testing is greatly reduced.
>> If you know your subset data won't exceed memory then the "outfile"
>> can be held in memory to speed things up considerably.
>>
>> Today is: 20090520
>> no code
>>
>> Steve- Hide quoted text -
>>
>> - Show quoted text -
> 
> this is the program...I wrote but is not working
> I have a list of valves, and another of pressures;
> If I am ask to find out which ones are the valves that are using all
> this set of pressures, wanted best pressures
> this is the program i wrote but is not working properly, it suppossed
> to return in the case
> find all the valves that are using pressures 1 "and" 2 "and" 3.
> It returns me A, A2, A35
> The correct answer supposed to be A and A2...
> if I were asked for pressures 56 and 78 the correct answer supossed to
> be valves G and G2...
> 
> Valves = ['A','A','A','G', 'G', 'G',
> 'C','A2','A2','A2','F','G2','G2','G2','A35','A345','A4'] ##valve names
> pressures = [1,2,3,4235,56,78,12, 1, 2, 3, 445, 45,56,78,1, 23,7] ##
> valve pressures
> result = []
> 
> bestpress = [1,2,3] ##wanted base pressures
> print bestpress,'len bestpress is' , len(bestpress)
> 
> print len(Valves)
> print len(Valves)
> for j in range(len(Valves)):
> #for i in range(len(bestpress)):
> #for j in range(len(Valves)):
> for i in range(len(bestpress)-2):
> if pressures [j]== bestpress[i] and bestpress [i+1]
> ==pressures [j+1] and bestpress [i+2]==pressures [j+2]:
> result.append(Valves[j])
> #i = i+1
> #j = j+1
> # print i, j, bestpress[i]
> print "common PSVs are", result

If I understand your explanation correctly you don't actually care about the 
sequence, you just want all valves that show the pressures specified in 
bestpress. If that's correct your problem becomes easier. You can make a 
lookup table that maps a given pressure to a set of all valves that had it 
and then calculate the intersection.

from collections import defaultdict

def intersection(sets):
return reduce(set.intersection, sets)

valves = ['A', 'A', 'A', 'G', 'G', 'G', 'C', 'A2', 'A2', 'A2', 'F', 'G2', 
'G2', 'G2', 'A35', 'A345', 'A4']
pressures = [1, 2, 3, 4235, 56, 78, 12, 1, 2, 3, 445, 45, 56, 78, 1, 23, 7]

lookup = defaultdict(set)
for v, p in zip(valves, pressures):
lookup[p].add(v)

result = []

wanted_pressures = [1, 2, 3]
print wanted_pressures, "-->", intersection(lookup[p] for p in 
wanted_pressures)

Peter

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


Re: Performance java vs. python

2009-05-21 Thread Duncan Booth
namekuseijin  wrote:

> I find it completely unimaginable that people would even think 
> suggesting the idea that Java is simpler.  It's one of the most stupidly 
> verbose and cranky languages out there, to the point you can't really do 
> anything of relevance without an IDE automatically pumping out lots of 
> scaffold code for you.
> 

But that means Java programmers are obviously more productive than Python 
programmers: they produce many more lines of code per day even if much of 
it is boileplate or even automatically generated. Managers like that.

OTOH, I consider it a productive day if I end up with fewer lines of code 
than I started with.

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


Re: reseting an iterator

2009-05-21 Thread Duncan Booth
Steven D'Aprano  wrote:

> On Wed, 20 May 2009 11:35:47 -0700, Jan wrote:
> 
>> Wouldn't it be easy for Python to implement generating functions so that
>> the iterators they return are equipped with a __reset__() method?
> 
> No.
> 
> def gen():
> for name in os.listdir('.'):
> yield open(name).read()
> os.remove(name)
> 
> 
> How would you "easily" reset this generator so that it returns the same 
> values each time?
> 
> That's an extreme example, but as a general rule, generators/iterators 
> are one-shot: having consumed a value, you can't get it back again 
> without re-creating it from scratch, or possibly not even then. Here's a 
> less destructive example:
> 
> def gen():
> for i in xrange(10):
> yield time.time()
> 

While I agree 100% with your 'No', I don't actually agree with your 
explanation.

Why are you assuming that resetting a generator should return the same 
values each time? Why shouldn't resetting put it back to some known state, 
but allow the resulting output to vary?

For example, that's what happens in the .Net universe with Linq: a Linq 
expression is roughly equivalent to a Python iterable and every time you 
iterate over it you can get a different set of results.

The following code would, I think, work except that generators don't allow 
you to add attributes:

from functools import wraps
def resettable(generator):
@wraps(generator)
def wrapper(*args, **kw):
def __reset__():
gen = generator(*args, **kw)
gen.__reset__ = __reset__
return gen
return __reset__()
return wrapper

@resettable
def gen(n):
for i in xrange(n):
yield i, time.time()


import time
it = gen(3)
for v in it: print v
it = it.__reset__()
for v in it: print v


The simpler solution is just to reconstruct the iterator yourself.

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


Re: scoping problem with list comprehension // learning Python

2009-05-21 Thread Diez B. Roggisch

Adrian Dragulescu schrieb:


I just started to learn python (first posting to the list).

I have a list of dates as strings  that I want to convert to a list of 
datetime objects.  Here is my debugging session from inside a method.


(Pdb) formatIndex
'%Y-%m-%d'
(Pdb) [datetime.strptime(i, formatIndex) for i in self.index[0:3]]
*** NameError: global name 'formatIndex' is not defined
(Pdb) [datetime.strptime(i, '%Y-%m-%d') for i in self.index[0:3]]
[datetime.datetime(2007, 1, 3, 0, 0), datetime.datetime(2007, 1, 4, 0, 
0), datetime.datetime(2007, 1, 5, 0, 0)]

(Pdb)

How come I get an error that formatIndex is not defined? I just show 
that it has value '%Y-%m-%d', in the same method scope.  Not sure why it 
says "global name", as I am in a method.


If I run it as a stand-alone, it works:
  index = ['2007-01-01', '2007-01-02', '2007-01-03']
  formatIndex = '%Y-%m-%d'
  print([datetime.strptime(i, formatIndex) for i in index])

Any suggestions much appreciated.  I'm sure it's something trivial.  I'm 
using Python30.


I think your problem here is Pdb. You can't always assume that scoping 
and assignment works there as desired.


If you do the same in the interpreter instead of Pdb, does it work? It 
works for me in Py2.5, don't have a 3.0 handy.


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


Re: Adding a Par construct to Python?

2009-05-21 Thread Lie Ryan

Carl Banks wrote:

There must be another reason (i.e, the refcounts) to argue _for_ the GIL,


Why?


Nobody likes GIL, but it just have to be there or things starts crumbling...

Nobody would actually argue _for_ GIL, they just know from experience, 
that people that successfully GIL in the past, always produces 
unsatisfactory solution.


Among them are slowing down single threaded code, locking issues, etc.

They are not really arguments _for_ GIL, but a challenge for the next 
people that tries to remove it to think about before dedicating the rest 
of their life to removing GIL; only to found that nobody likes the 
solution...

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


making a python program in windows

2009-05-21 Thread Rustom Mody
I know how to make a python script behave like a (standalone) program
in unix --
1. put a #! path/to/python as the first line
2. make the file executable

The closest I know how to do this in windows is:
r-click the file in win-explorer
goto properties
goto open with
change pythonw to python

Can someone enlighten me about a more scriptish way of doing this?
Basically if I have to setup that program on other (windows) m/cs is
there some .bat or .vbs or some regedit thingy Ive to do to avoid the
Rt-click routine?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: making a python program in windows

2009-05-21 Thread Martin P. Hellwig

Rustom Mody wrote:

I know how to make a python script behave like a (standalone) program
in unix --
1. put a #! path/to/python as the first line
2. make the file executable

The closest I know how to do this in windows is:
r-click the file in win-explorer
goto properties
goto open with
change pythonw to python

Can someone enlighten me about a more scriptish way of doing this?
Basically if I have to setup that program on other (windows) m/cs is
there some .bat or .vbs or some regedit thingy Ive to do to avoid the
Rt-click routine?


Since you don't know for sure if a Python environment is available on 
the other windows machine and whether the file associations are 
configured correctly, the best thing you can do is to use something like 
py2exe and distribute the result of that.


--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Performance java vs. python

2009-05-21 Thread Sion Arrowsmith
Duncan Booth   wrote:
>namekuseijin  wrote:
>> I find it completely unimaginable that people would even think 
>> suggesting the idea that Java is simpler.  It's one of the most stupidly 
>> verbose and cranky languages out there, to the point you can't really do 
>> anything of relevance without an IDE automatically pumping out lots of 
>> scaffold code for you.
>But that means Java programmers are obviously more productive than Python 
>programmers: they produce many more lines of code per day even if much of 
>it is boileplate or even automatically generated. Managers like that.
>
>OTOH, I consider it a productive day if I end up with fewer lines of code 
>than I started with.

A friend once justified a negative LOC count as being the sign of a
good day with the following observation:

Code that doesn't exist contains no bugs.
Code that doesn't exist takes no time to execute.
Code that doesn't exist takes up no space.
Code that doesn't exist doesn't need maintenance.

Once, when faced with a rather hairy problem that client requirements
dictated a pure Java solution for, I coded up a fully functional
prototype in Python to get the logic sorted out, and then translated
it. Even given the optimisations of manual translation, and being
able to dispose of one portion of the Python which Java supplied the
functionality for out of the box (thread timeout, I think it was),
the code grew by 200%. That was a very unproductive day.

-- 
\S

   under construction

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


Re: making a python program in windows

2009-05-21 Thread rustom
On May 21, 3:19 pm, "Martin P. Hellwig" 
wrote:
> Rustom Mody wrote:
> > I know how to make a python script behave like a (standalone) program
> > in unix --
> > 1. put a #! path/to/python as the first line
> > 2. make the file executable
>
> > The closest I know how to do this in windows is:
> > r-click the file in win-explorer
> > goto properties
> > goto open with
> > change pythonw to python
>
> > Can someone enlighten me about a more scriptish way of doing this?
> > Basically if I have to setup that program on other (windows) m/cs is
> > there some .bat or .vbs or some regedit thingy Ive to do to avoid the
> > Rt-click routine?
>
> Since you don't know for sure if a Python environment is available on
> the other windows machine and whether the file associations are
> configured correctly, the best thing you can do is to use something like
> py2exe and distribute the result of that.

Oh - Oh! Not an exe please! I dont want to move away from readable
text files if possible.

I certainly know that python2.6 is installed.
Why that installation by default does not put python.exe on the path I
dont know but that is best corrected by hand.

Bottom Line: Assume that from a command line (cmd) python runs and
gives its interpreter prompt.
When run from cygwin it hangs but thats another story. Just dont
assume cygwin.

i suppose the question is entirely about setting properly (and
grokking) file associations -- why is a .py file associated with
pythonw and not python? And is making this association right enough to
make a .py file in windows behave like a shebang file in unix?

[And is there a more appropriate list for such questions?]

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


Re: making a python program in windows

2009-05-21 Thread Stef Mientki

Rustom Mody wrote:

I know how to make a python script behave like a (standalone) program
in unix --
1. put a #! path/to/python as the first line
2. make the file executable

The closest I know how to do this in windows is:
r-click the file in win-explorer
goto properties
goto open with
change pythonw to python

Can someone enlighten me about a more scriptish way of doing this?
Basically if I have to setup that program on other (windows) m/cs is
there some .bat or .vbs or some regedit thingy 

Python can replace all of that ;-)
But to your orginal question,
why not change the file associate in windows ?

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


Re: How to build Python 2.6.2 on HP-UX Itanium with thread support?

2009-05-21 Thread Lawrence D'Oliveiro
In message , [email protected] wrote:

> I'm used to "configure; make; make install", which usually works well on
> other platforms...

It's often worthwhile to try

./configure --help

and see if any of the options might be relevant.

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


A question regd configobj

2009-05-21 Thread Srijayanth Sridhar
Hello,

I am wondering if it is possible to have hexadecimal strings in a ini file
and have configobj parse it correctly.

for eg:
moonw...@trantor:~/python/config$ cat foo
foo="\x96\x97"
.
.
.
>>> a=ConfigObj("foo")
>>> a
ConfigObj({'foo': '\\x96\\x97'})
>>> a['foo']
'\\x96\\x97'

As you can see the string has escaped the '\' and I want to suppress that
behavior. I've looked at the documentation and haven't been able to figure
out if this is an available feature or not.

Any help will be greatly appreciated.

Thank you,

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


dbfpy - cannot store new record

2009-05-21 Thread Laszlo Nagy

Given this example program:

import dbfpy
def dbf_open(tblname):
   fpath = os.path.join(local.DB_DIR,tblname)
   f = file(fpath,"ab+")
   f.seek(0)
   tbl = dbf.Dbf(f)
   return tbl

tbl = dbf_open("partners.dbf")
rec = tbl.newRecord()
rec["FIELDNAME1"] = 1
rec["FIELDNAME2"] = "Somebody"
rec.store()
tbl.close()

I get no exception, no error etc. But the new record is NOT appended to 
the table. What is wrong here?


Thanks,

  Laszlo

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


Re: PYTHONPATH on Windows XP module load problem

2009-05-21 Thread Andreas Otto
Hi,

  I solved the problem ...
  thank you for your help


mfg

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


defaultdict's bug or feature?

2009-05-21 Thread Red Forks
from collections import defaultdict

d = defaultdict(set)
assert isinstance(d['a'], set)
assert isinstance(d.get('b'), set)

d['a'] is ok, and a new set object is insert to d, but d.get('b') won't.

It's a bug, or just a feature?

I think dict.get() method is just a *safe* version of dict[key], maybe it
should be:

def get(self, key, default = None):
  try:
return self[key]
  except KeyError:
return default
-- 
http://mail.python.org/mailman/listinfo/python-list


A list with periodic boundary conditions

2009-05-21 Thread youhvee
Hi,

I'm trying to create a new class of list that has periodic boundary
conditions.

Here's what I have so far:

class wrappedList(list):
def __getitem__(self, p):
return list.__getitem__(self, p%len(self))
def __setitem__(self, p, v):
list.__setitem__(self, p%len(self), v)

 a=wrappedList(range(10))
 a[1]
1
 a[11]
1

But I would also like to make slices. For instance I would like
 a[8:11]
[8, 9, 0]

I can do it by copying, but I want to return a view to the original
data, and I have no idea where to start. It seems that the slice needs
to retain some knowledge of the list from which it derived. i.e. it
needs to know that it is a slice. Any ideas on how I can extend this
to allow views?

Thanks for any help.

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


Re: making a python program in windows

2009-05-21 Thread Duncan Booth
rustom  wrote:

> i suppose the question is entirely about setting properly (and
> grokking) file associations -- why is a .py file associated with
> pythonw and not python? And is making this association right enough to
> make a .py file in windows behave like a shebang file in unix?

I think the question about the file association is one you have to answer 
for yourself. When you install Python it associates .pyw with pythonw and 
.py with python. If something on your system has changed this the best 
thing you can do is to change it back.

Try typing the following commands and then fixing any differences by typing 
the expected output as the command parameter 
(e.g. "assoc .py=Python.File"):

C:\>assoc .py
.py=Python.File

C:\>assoc .pyw
.pyw=Python.NoConFile

C:\>ftype Python.file
Python.file="C:\Python26\python.exe" "%1" %*

C:\>ftype Python.NoConFile
Python.NoConFile="C:\Python26\pythonw.exe" "%1" %*


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


Re: [ANN] vim patch to support python3 interface

2009-05-21 Thread Tony Mechelynck

On 12/05/09 18:35, Roland Puntaier wrote:

Hello,

I have ported vim's python interface (if_python.c) to the python3 C-API.

The changed files are:
- Makefile (for linux)
- Make_mvc.mak (for windows)
- if_python3.c is a new file for the python3 related sources. it is based
on if_python.c.

All of them are in the ``src`` subdirectory.

[...]

Make_mvc.mak is not, and by far, the only makefile used by people who 
build Vim on Windows. If you want your port to become the "main" version 
some day, patches for the other makefiles should also be available. For 
instance, Steve Hall (who maintains the Vim builds, with and without 
Cream, but with Python et al., hosted with the Cream project) uses, I 
think, the Make_cyg.mak -- and I've seen now and again posts by Windows 
users building by means of the Make_ming.mak too. I've used the 
make_bc5.mak in the past, but the problems I had with it (when trying to 
use Unicode in Vim on Win98) made me switch to Make_cyg.mak even before 
moving up to XP and then to Linux-only; but my HowTo "for Windows" still 
includes settings for it.



Best regards,
Tony.
--
The greatest dangers to liberty lurk in insidious encroachment by men
of zeal, well-meaning but without understanding.
-- Justice Louis D. Brandeis
--
http://mail.python.org/mailman/listinfo/python-list


ANNOUNCE: libmsgque 3.2

2009-05-21 Thread Andreas Otto

ANNOUNCE a minor feature improvement of libmsgque ...


What is LibMsgque
=

LibMsgque is an OS independent, Programming Language independent and
Hardware independent solution to link applications together to act like a
single application. Or with other words, LibMsgque is an Application-Server
toolkit.


Highlights of the current Release:
=

This release introduce a new windows build into the LibMsgque world.
The build switched from the GNU toolchain to "Visual C++ 2008 Express"

This is the official statement for this switch:

  The decision to use GNU tools on Windows including
  automake, autoconf, libtool and mingw was wrong.
  The tools were replaced by Visual C++ 2008 Express.
  The reason was the extremely low performance of the
  GNU tools on Windows which end up in a 20min overhead per
  configuration life-cycle and a 5min overhead per build.
  This end up in an additional TCO of 2000euro per release.


In addition the performance-comparison between "c", "tcl", "python"
and "java" was updated

  -> results: http://libmsgque.sourceforge.net/performance.htm

After quite a wile I'm now able to support python on windows


The Web-Site was updated:
=

   -> http://libmsgque.sourceforge.net

  For a fast introduction use the following URL:

   -> http://libmsgque.sourceforge.net/features.htm



mfg

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


Re: defaultdict's bug or feature?

2009-05-21 Thread MRAB

Red Forks wrote:

from collections import defaultdict

d = defaultdict(set)
assert isinstance(d['a'], set)
assert isinstance(d.get('b'), set)

d['a'] is ok, and a new set object is insert to d, but d.get('b') won't.

It's a bug, or just a feature?


A feature.

I think dict.get() method is just a /safe/ version of dict[key], maybe 
it should be:
 
def get(self, key, default = None):

  try:
return self[key]
  except KeyError:
return default


Isn't that what it does already?


With dict you have the choice of whether to raise an exception or return 
a default value if the key is missing.


With defaultdict you have the choice of whether to add the value or
return a default value if the key is missing.

Both classes have their uses.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Adding a Par construct to Python?

2009-05-21 Thread Luis Alberto Zarrabeitia Gomez

Quoting Carl Banks :

> I don't have any reply to this post except for the following excerpts:
> 
> On May 20, 8:10 pm, Luis Alberto Zarrabeitia Gomez 
> wrote:
> > 2- in [almost] every other language, _you_ have to be aware of the
> critical
> > sections when multithreading.
> [snip]
> > That's not what I said. We are not talking about the _language_, but about
> one
> > very specific implementation detail. Not even that, I'm talking about one
> of the
> > reasons presented in favor of that specific implementation detail (while
> > agreeing with the others). 
[...]
> 
> No other languages have nesting by indentation (while still being
> reasonablyt successful)
> etc
> 
> Comparisons to other languages are useless here.  In many cases Python
> does things differently from most other languages and usually it's
> better off for it.

You seem to have missed that I'm not talking about the language but about a
specific implementation detail of CPython. I thought that my poor choice of
words in that sentence was completely clarified by the paragraphs that followed,
but apparently it wasn't. In my "2-" point, maybe I should've said instead: "in
[almost] every language, INCLUDING (proper) PYTHON, you have to be aware of
critcal sections when multithreading". 

> The fact that other languages do something differently doesn't mean
> that other way's better, in fact it really doesn't mean anything at
> all.

No, it doesn't mean that it's better, and I didn't say it was. But it _does_
show that it is _possible_. For an argument about how hard could be to write
native extensions in there was no GIL, the fact that there are many other
GIL-less platforms[1] where is not painful to write native extensions is a valid
counter-example. And that, in all those languages and platforms (including
python), the only one where I hear that explicit, granular locking is too hard
or whatever[2], is CPython's /native/ extensions, is what I found weird.

Regards,

Luis

[1] Including the python implementations for .net and java.
[2] Really, this is was my question and nothing more. I was not comparing, I was
trying to understand what was that "whatever" that made it so hard for CPython.
And your footnote in the previous message gave me a reasonable explanation.

-- 
Luis Zarrabeitia
Facultad de Matemática y Computación, UH
http://profesores.matcom.uh.cu/~kyrie

-- 
Participe en Universidad 2010, del 8 al 12 de febrero de 2010
La Habana, Cuba 
http://www.universidad2010.cu

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


Re: Your Favorite Python Book

2009-05-21 Thread Esmail

Shawn Milochik wrote:

On Mon, May 11, 2009 at 5:52 PM,   wrote:

Sam,

In no specific order (I brought them all):

Wesley Chun's "Core Python Programming"

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



I second the Wesley Chun recommendation wholeheartedly. 


This book keeps getting mentioned, I'll have to check it out.
Perhaps some of you can share what about it you like in
particular.

Thanks,
Esmail

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


python question

2009-05-21 Thread Craig

How do i install this.i never seen a python write in c before.


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


[no subject]

2009-05-21 Thread Craig

http://downloads.emperorlinux.com/contrib/pyiw
http://downloads.emperorlinux.com/contrib/pywpa


Sorry fro the 2 post.How do i install a python moudles write en in C?


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


Re: dbfpy - cannot store new record

2009-05-21 Thread MRAB

Laszlo Nagy wrote:

Given this example program:

import dbfpy
def dbf_open(tblname):
   fpath = os.path.join(local.DB_DIR,tblname)
   f = file(fpath,"ab+")
   f.seek(0)
   tbl = dbf.Dbf(f)
   return tbl

tbl = dbf_open("partners.dbf")
rec = tbl.newRecord()
rec["FIELDNAME1"] = 1
rec["FIELDNAME2"] = "Somebody"
rec.store()
tbl.close()

I get no exception, no error etc. But the new record is NOT appended to 
the table. What is wrong here?



I've downloaded dbfpy and experimented with it and I've found that
opening the file with file(fpath,"ab+") causes some extra junk to be
appended to the end of the file when the DBF file is closed. It works
much better (and is simpler!) if you open the DBF file with the
filename:

fpath = os.path.join(local.DB_DIR, "partners.dbf")
tbl = dbf.Dbf(fpath)
rec = tbl.newRecord()
rec["FIELDNAME1"] = 1
rec["FIELDNAME2"] = "Somebody"
rec.store()
tbl.close()
--
http://mail.python.org/mailman/listinfo/python-list


Generating zipped or gzipped attachment with email package?

2009-05-21 Thread skip
I have a script which allows me to generate MIME messages with appropriate
attachments.  It's essentially a lightly modified version of the second
example from this page of the email package docs:

http://docs.python.org/library/email-examples.html

I want to modify my script to automatically zip or gzip files which exceed
some size threshold.  Doing the zip/gzip dance is no problem.  I'm concerned
about how to specify that properly with the email package.  For example,
consider a large CSV file.  I figure out the MIME type is text/csv.  Now
suppose I gzip the file before attaching it.  How would this code change to
specify the compression where "path" is now compressed?

if maintype == 'text':
fp = open(path)
# Note: we should handle calculating the charset
msg = MIMEText(fp.read(), _subtype=subtype)
fp.close()

I guess I'm asking if I can have the Content-Type still be text/csv with
some other MIME header indicating the file is compressed.  If so, how do I
achieve that when attaching the compressed file to the message?

Thanks,

-- 
Skip Montanaro - [email protected] - http://www.smontanaro.net/
America's vaunted "free press" notwithstanding, story ideas that expose
the unseemly side of actual or potential advertisers tend to fall by the
wayside.  Not quite sure why.  -- Jim Thornton
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: LaTeXing python programs

2009-05-21 Thread John Reid



Edward Grefenstette wrote:

I'm trying to figure out how to use pygments. Are there any good usage
examples out there?


The documentation worked for me: http://pygments.org/docs/cmdline/

There is also a LaTeX package to call pygments at latex compilation time 
I forget what that is called though.


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


About dictionary in combobox in pygtk

2009-05-21 Thread shruti surve
hi all,

My data has thousands of entries. I 'd like to feed the combobox with
dictionary.how to use dictionary in combobox?


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


Re: Your Favorite Python Book

2009-05-21 Thread Gökhan SEVER
Hello,

I received an autographed copy of CPP, 2nd Edition after joining to Safari's
"What is Python" webcast. They published the recorded session online as
well. Check
http://www.safaribooksonline.com/Corporate/DownloadAndResources/webcasts.php

As you will see from the lecture, he is a very motivated instructor. Mostly
likely you will want to grab a copy of the book after watching the condensed
Python introduction course.

Gökhan


On Thu, May 21, 2009 at 8:06 AM, Esmail  wrote:

> Shawn Milochik wrote:
>
>> On Mon, May 11, 2009 at 5:52 PM,   wrote:
>>
>>> Sam,
>>>
>>> In no specific order (I brought them all):
>>>
>>> Wesley Chun's "Core Python Programming"
>>>
>>> http://mail.python.org/mailman/listinfo/python-list
>>>
>>>
>> I second the Wesley Chun recommendation wholeheartedly.
>>
>
> This book keeps getting mentioned, I'll have to check it out.
> Perhaps some of you can share what about it you like in
> particular.
>
> Thanks,
> Esmail
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dbfpy - cannot store new record

2009-05-21 Thread David Lyon

Hi,

Try not opening the file in append mode (no "a+")

Inside the logic, there is already a seek to the end of the file
and the record counters at the start of the file need updating
too.

Regards

David


On Thu, 21 May 2009 13:25:04 +0200, Laszlo Nagy 
wrote:
> Given this example program:
> 
> import dbfpy
> def dbf_open(tblname):
> fpath = os.path.join(local.DB_DIR,tblname)
> f = file(fpath,"ab+")
> f.seek(0)
> tbl = dbf.Dbf(f)
> return tbl
> 
> tbl = dbf_open("partners.dbf")
> rec = tbl.newRecord()
> rec["FIELDNAME1"] = 1
> rec["FIELDNAME2"] = "Somebody"
> rec.store()
> tbl.close()
> 
> I get no exception, no error etc. But the new record is NOT appended to 
> the table. What is wrong here?
> 
> Thanks,
> 
>Laszlo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dbfpy - cannot store new record

2009-05-21 Thread Laszlo Nagy

David Lyon írta:

Hi,

Try not opening the file in append mode (no "a+")

Inside the logic, there is already a seek to the end of the file
and the record counters at the start of the file need updating
too.
  
The first thing I tried is to use a filename instead of the file object 
but it didn't work. Then I just tried "r+b" and it worked fine. Thank you!


BTW here is ist constructor:

   def __init__(self, f, readOnly=False, new=False, ignoreErrors=False):
   """Initialize instance.

   Arguments:
   f:
   Filename or file-like object.
   new:
   True if new data table must be created. Assume
   data table exists if this argument is False.
   readOnly:
   if ``f`` argument is a string file will
   be opend in read-only mode; in other cases
   this argument is ignored. This argument is ignored
   even if ``new`` argument is True.
   headerObj:
   `header.DbfHeader` instance or None. If this argument
   is None, new empty header will be used with the
   all fields set by default.
   ignoreErrors:
   if set, failing field value conversion will return
   ``INVALID_VALUE`` instead of raising conversion error.

   """
   if isinstance(f, basestring):
   # a filename
   self.name = f
   if new:
   # new table (table file must be
   # created or opened and truncated)
   self.stream = file(f, "w+b")
   else:
   # tabe file must exist
   self.stream = file(f, ("r+b", "rb")[bool(readOnly)])
   else:
   # a stream
   self.name = getattr(f, "name", "")
   self.stream = f


I have never seen such a construct before. Index a tuple with a boolean???

   self.stream = file(f, ("r+b", "rb")[bool(readOnly)])
Best,

  Laszlo

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


join two selects

2009-05-21 Thread gert
I am trying to figure out how to join two selects ?

SELECT * FROM search
SELECT eid, SUM(pnt) AS total_votes FROM vote

CREATE TABLE votes (
eid  INTEGER PRIMARY KEY,
uid  VARCHAR(64),
pnt  INETEGER DEFAULT 0,
);

CREATE TABLE search (
eid  INTEGER PRIMARY KEY,
txt  VARCHAR(64),
end  DATETIME
);

so the result would be a table that looks like this

["eid", "txt", "end", "total_votes"]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: join two selects

2009-05-21 Thread Tim Golden

gert wrote:

I am trying to figure out how to join two selects ?

SELECT * FROM search
SELECT eid, SUM(pnt) AS total_votes FROM vote

CREATE TABLE votes (
eid  INTEGER PRIMARY KEY,
uid  VARCHAR(64),
pnt  INETEGER DEFAULT 0,
);

CREATE TABLE search (
eid  INTEGER PRIMARY KEY,
txt  VARCHAR(64),
end  DATETIME
);

so the result would be a table that looks like this

["eid", "txt", "end", "total_votes"]


That's what's known technically as a join:

SELECT
 sea.eid,
 sea.txt,
 sea.end,
 SUM (vot.pnt) AS total_votes
FROM
 search AS sea
JOIN votes AS vot ON
 vot.eid = sea.eid
GROUP BY
 sea.eid,
 sea.txt,
 sea.end,


(Guessing the join condition from the column names)

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


Re: dbfpy - cannot store new record

2009-05-21 Thread MRAB

Laszlo Nagy wrote:
[snip]


I have never seen such a construct before. Index a tuple with a boolean???

   self.stream = file(f, ("r+b", "rb")[bool(readOnly)])


Python originally didn't have Boolean; it used 0 for false and 1 for
true. When the Boolean class was added it was subclassed from int in
order not to break existing code, so False and True can be used like 0
and 1 respectively:

>>> False == 0
True
>>> True == 1
True
>>> False + 1
1
>>> True + 1
2

Therefore a_list[False] is the same as a_list[0] and a_list[True] is the
same as a_list[1].
--
http://mail.python.org/mailman/listinfo/python-list


Re: Performance java vs. python

2009-05-21 Thread Lie Ryan

Sion Arrowsmith wrote:
OTOH, I consider it a productive day if I end up with fewer lines of code 
than I started with.


A friend once justified a negative LOC count as being the sign of a
good day with the following observation:

Code that doesn't exist contains no bugs.
Code that doesn't exist takes no time to execute.
Code that doesn't exist takes up no space.
Code that doesn't exist doesn't need maintenance.


Why not call a day productive when the UnitTest that passed has 
increased or stayed constant with reduced LOC.



Once, when faced with a rather hairy problem that client requirements
dictated a pure Java solution for, I coded up a fully functional
prototype in Python to get the logic sorted out, and then translated
it. Even given the optimisations of manual translation, and being
able to dispose of one portion of the Python which Java supplied the
functionality for out of the box (thread timeout, I think it was),
the code grew by 200%. That was a very unproductive day.


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


Overlapping region resolution

2009-05-21 Thread [email protected]
This may be an algorithmic question, but I'm trying to code it in
Python, so...

I have a list of pairwise regions, each with an integer start and end
and a float data point. There may be overlaps between the regions. I
want to resolve this into an ordered list with no overlapping
regions.

My initial solution was to sort the list by the start point, and then
compare each adjacent region, clipping any overlapping section in
half. I've attached code at the bottom. Unfortunately, this does not
work well if you have sections that have three or more overlapping
regions.

A more general solution is to work out where all the overlaps are
before I start. Then I can break up the region space based on what
regions overlap each section and take averages of all the datapoints
that are present in a particular section. Devising an algorithm to do
this is making my brain hurt. Any ideas?

Peter


# also validates the data
def clipRanges(regions):
for i in range(len(regions) - 1):
thispoint = regions[i]
nextpoint = regions[i+1]
assert thispoint[1] > thispoint[0] and  nextpoint[1] > 
nextpoint[0],
"point read not valid"
thisend = thispoint[1]
nextstart = nextpoint[0]
diff = thisend - nextstart
# a difference of zero is too close together
if diff > -1:
if diff % 2 == 1:
diff += 1
correction = diff / 2
newend = thisend - correction
newstart = newend + 1
assert newend > thispoint[0] and nextpoint[1] > 
newstart, "new
range not valid!"
newthispoint = (thispoint[0], newend, thispoint[2])
newnextpoint = (newstart, nextpoint[1], nextpoint[2])
regions[i] = newthispoint
regions[i+1] = newnextpoint
return regions

regions = [ (0,10,2.5), (12,22,3.5), (15,25,1.2), (23, 30,0.01), (27,
37,1.23), (30, 35, 1.45) ]
regions2 = [ (0,10,2.5), (1,11,1.1), (2,12,1.2) ]

# works fine, produces [(0, 10, 2.5), (12, 18, 3.5), (19, 24, 1.2),
(25, 28, 0.01), (29, 33, 1.23), (34, 35, 1.45)]
print clipRanges(regions)
# violates "new range not valid" assertion
print clipRanges(regions2)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Adding a Par construct to Python?

2009-05-21 Thread Nick Craig-Wood
Steven D'Aprano  wrote:
>  On Tue, 19 May 2009 05:52:04 -0500, Grant Edwards wrote:
> 
> > On 2009-05-19, Steven D'Aprano 
> > wrote:
> >> On Mon, 18 May 2009 02:27:06 -0700, jeremy wrote:
> >>
> >>> Let me clarify what I think par, pmap, pfilter and preduce would mean
> >>> and how they would be implemented.
> >> [...]
> >>
> >> Just for fun, I've implemented a parallel-map function, and done a
> >> couple of tests. Comments, criticism and improvements welcome!
> > 
> > My only comment would be that your "slow function" might not be a very
> > simulation for the general-case, since it uses time.sleep() which
> > releases the GIL:
> 
> 
>  I didn't expect my code to magically overcome fundamental limitations of 
>  the CPython interpreter :)
> 
> 
> 
> >> def f(arg):  # Simulate a slow function.
> >> time.sleep(0.5)
> >> return 3*arg-2
> > 
> > Any Python function that isn't calling a library function written in C
> > that releases the GIL won't show any speedup will it?
> 
>  Not necessarily. Here's another function, that uses a loop instead of 
>  sleep.
> 
>  def g(arg, SIZE=8*10**6):
>  # Default SIZE is chosen so that on my machine, the loop 
>  # takes approximately 0.5 second.
>  for x in xrange(SIZE):
>  pass
>  return 3*arg-2
> 
> 
> >>> setup = 'from __main__ import pmap, g; data = range(50)'
> >>> min(Timer('map(g, data)', setup).repeat(repeat=5, number=3))
>  65.093590974807739
> >>> min(Timer('pmap(g, data)', setup).repeat(repeat=5, number=3))
>  20.268381118774414

I don't think that can be right - that shows python working without
the GIL contention.  So unless you ran it under IronPython?

Here is what happens when I run it under CPython 2.5 on my dual core
laptop.  I made SIZE=10**6 because I got bored of waiting ;-)

map
9.85280895233
pmap
28.4256689548

So the pmap took nearly 3 times as long.  I expect this is because the
task was divided into 5 sections each competing madly for the GIL.

I ran the same script under the latest jython beta which was very
interesting! pmap showing a slight improvement, and faster than
cPython!

$ jython2.5rc2/jython pmap.py
map
6.242000103
pmap
5.8881144

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


A fast way to read last line of gzip archive ?

2009-05-21 Thread Barak, Ron
Hi,

I need to read the end of a 20 MB gzip archives (To extract the date from the 
last line of a a gzipped log file).
The solution I have below takes noticeable time to reach the end of the gzip 
archive.

Does anyone have a faster solution to read the last line of a gzip archive ?

Thanks,
Ron.

#!/usr/bin/env python

import gzip

path = "./a/20/mb/file.tgz"

in_file = gzip.open(path, "r")
first_line = in_file.readline()
print "first_line ==",first_line
in_file.seek(-500)
last_line = in_file.readlines()[-1]
print "last_line ==",last_line



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


Re: dbfpy - cannot store new record

2009-05-21 Thread Laszlo Nagy
Here is the next problem. For boolean/logical fields, I can set their 
value to True/False easily. However, setting NULL seems impossible:


rec = tbl.newRecord()
rec["SOMEFIELD1"] = True # Works fine
rec["SOMEFIELD2"] = False # Works fine
rec["SOMEFIELD3"] = None # Will store False
rec["SOMEFIELD3"] = 0 # Will store False
rec["SOMEFIELD3"] = "" # Will store False
rec["SOMEFIELD3"] = chr(32) # Will store False
rec["SOMEFIELD3"] = chr(0) # Will store False
rec.store()

Strange thing: if I do not set the value of a numeric field, it becomes 
NULL. The same thing I cannot do for logical fields: if I do not set the 
value of a logical field, it becomes an invalid value, denoted with a 
question mark.


Any ideas?

Thanks,

  Laszlo

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


Re: Overlapping region resolution

2009-05-21 Thread MRAB

[email protected] wrote:

This may be an algorithmic question, but I'm trying to code it in
Python, so...

I have a list of pairwise regions, each with an integer start and end
and a float data point. There may be overlaps between the regions. I
want to resolve this into an ordered list with no overlapping
regions.

My initial solution was to sort the list by the start point, and then
compare each adjacent region, clipping any overlapping section in
half. I've attached code at the bottom. Unfortunately, this does not
work well if you have sections that have three or more overlapping
regions.

A more general solution is to work out where all the overlaps are
before I start. Then I can break up the region space based on what
regions overlap each section and take averages of all the datapoints
that are present in a particular section. Devising an algorithm to do
this is making my brain hurt. Any ideas?

Peter


# also validates the data
def clipRanges(regions):
for i in range(len(regions) - 1):
thispoint = regions[i]
nextpoint = regions[i+1]
assert thispoint[1] > thispoint[0] and   nextpoint[1] > 
nextpoint[0],
"point read not valid"
thisend = thispoint[1]
nextstart = nextpoint[0]
diff = thisend - nextstart
# a difference of zero is too close together
if diff > -1:
if diff % 2 == 1:
diff += 1
correction = diff / 2
newend = thisend - correction
newstart = newend + 1
assert newend > thispoint[0] and nextpoint[1] > newstart, 
"new
range not valid!"
newthispoint = (thispoint[0], newend, thispoint[2])
newnextpoint = (newstart, nextpoint[1], nextpoint[2])
regions[i] = newthispoint
regions[i+1] = newnextpoint
return regions

regions = [ (0,10,2.5), (12,22,3.5), (15,25,1.2), (23, 30,0.01), (27,
37,1.23), (30, 35, 1.45) ]
regions2 = [ (0,10,2.5), (1,11,1.1), (2,12,1.2) ]

# works fine, produces [(0, 10, 2.5), (12, 18, 3.5), (19, 24, 1.2),
(25, 28, 0.01), (29, 33, 1.23), (34, 35, 1.45)]
print clipRanges(regions)
# violates "new range not valid" assertion
print clipRanges(regions2)


Is the upper bound of a range inclusive or exclusive? If it's exclusive
(like in Python) then it's empty only if lower == upper, and an overlap
occurs only if first.upper > second.lower (and you can have newstart ==
newend in your code).
--
http://mail.python.org/mailman/listinfo/python-list


Simple question about accessing instance properties.

2009-05-21 Thread Lacrima
Hello!

I think I have a very simple question, but I can't understand how to
access object properties in a way described below.
For example I have an instance of any class:

>>> class Person:
def __init__(self):
self.name = 'John'
self.email = '[email protected]'
self.phone = '3453454'
>>> person = Person()

Then I have a list of person's properties represented as strings:
>>> prop_list = ['name', 'email', 'phone']

And my question is how to access person's properties using prop_list?
Do I have to somehow convert 'name', 'email', 'phone'?

With regards,
Max.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A fast way to read last line of gzip archive ?

2009-05-21 Thread MRAB

Barak, Ron wrote:

Hi,
 
I need to read the end of a 20 MB gzip archives (To extract the date 
from the last line of a a gzipped log file).
The solution I have below takes noticeable time to reach the end of the 
gzip archive.
 
Does anyone have a faster solution to read the last line of a gzip archive ?
 
Thanks,

Ron.
 
#!/usr/bin/env python
 
import gzip
 
path = "./a/20/mb/file.tgz"
 
in_file = gzip.open(path, "r")

first_line = in_file.readline()
print "first_line ==",first_line
in_file.seek(-500)
last_line = in_file.readlines()[-1]
print "last_line ==",last_line


It takes a noticeable time to reach the end because, well, the data is
compressed! The compression method used requires the preceding data to
be read first.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple question about accessing instance properties.

2009-05-21 Thread MRAB

Lacrima wrote:

Hello!

I think I have a very simple question, but I can't understand how to
access object properties in a way described below.
For example I have an instance of any class:


class Person:

def __init__(self):
self.name = 'John'
self.email = '[email protected]'
self.phone = '3453454'

person = Person()


Then I have a list of person's properties represented as strings:

prop_list = ['name', 'email', 'phone']


And my question is how to access person's properties using prop_list?
Do I have to somehow convert 'name', 'email', 'phone'?


>>> getattr(person, "name")
'John'
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple question about accessing instance properties.

2009-05-21 Thread Lacrima
On May 21, 7:04 pm, MRAB  wrote:
> Lacrima wrote:
> > Hello!
>
> > I think I have a very simple question, but I can't understand how to
> > access object properties in a way described below.
> > For example I have an instance of any class:
>
>  class Person:
> >    def __init__(self):
> >            self.name = 'John'
> >            self.email = '[email protected]'
> >            self.phone = '3453454'
>  person = Person()
>
> > Then I have a list of person's properties represented as strings:
>  prop_list = ['name', 'email', 'phone']
>
> > And my question is how to access person's properties using prop_list?
> > Do I have to somehow convert 'name', 'email', 'phone'?
>
>  >>> getattr(person, "name")
> 'John'

Hi!
Thanks a lot!!!
That's so simple!

With regards,
Max.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dbfpy - cannot store new record

2009-05-21 Thread MRAB

Laszlo Nagy wrote:
Here is the next problem. For boolean/logical fields, I can set their 
value to True/False easily. However, setting NULL seems impossible:


rec = tbl.newRecord()
rec["SOMEFIELD1"] = True # Works fine
rec["SOMEFIELD2"] = False # Works fine
rec["SOMEFIELD3"] = None # Will store False
rec["SOMEFIELD3"] = 0 # Will store False
rec["SOMEFIELD3"] = "" # Will store False
rec["SOMEFIELD3"] = chr(32) # Will store False
rec["SOMEFIELD3"] = chr(0) # Will store False
rec.store()

Strange thing: if I do not set the value of a numeric field, it becomes 
NULL. The same thing I cannot do for logical fields: if I do not set the 
value of a logical field, it becomes an invalid value, denoted with a 
question mark.



Have you tried -1?

Can you read a DBF file created by the application (dBase, or whatever)
and see what it uses?
--
http://mail.python.org/mailman/listinfo/python-list


Re: popen - reading strings - constructing a list from the strings

2009-05-21 Thread norseman

MRAB wrote:

Aytekin Vargun wrote:

First of all,
Thanks for the suggestions, MRAB and norseman. "split()" was what I 
was looking for. Now I have a follow up question. In my application I 
create radio buttons in a frame. These radio buttons are constructed 
whenever a button is clicked. Therefore the list of items are 
dynamically changing.


What I want to do is to remove the current radio buttons and then 
recreate a new set of radio buttons using a new string list. I could 
not see a command for deleting or removing a set of radio buttons. I 
use Tkinter.


I can provide more details.
Thanks a lot.

PS: I apologize if this creates a new thread. I am new to the list.
Aytekin


I have no idea whether this is possible. I'd just create a new frame. I
hope you're not trying to add and remove buttons on a dialog while it's
open; having a dialog radically change its appearance while the user is
watching would, IMHO, be very bad! :-)

==
"... be very bad.." NOPE.
When it's done right, it's quite an eye catcher.

Actually I use that very technique to great advantage.

(excerpts from non-existent "How to Build a Control Panel")
Leave space for the changing frame in the root or topwindow.
define a frame to fit that space.
define the groups of buttons (static or dynamic) with each using the
  above frame as their home.  (many to one relation)
  put each button group in a def ...() of it's own.
  call the appropriate as needed


example:
(to call the button(s))
if condition.
 DispSubI()
else:
 DispSubNB()  #

where:
   (the content changing frame)
 def SubClas():
try:
  frameSub.destroy()
except:
  pass
frameSub =   define your
. disposable frame
.  template here
label.pack()
return frameSub


   (the buttons)
--
  #
  #   I - IDLE
  def DispSubI():
frameSub= SubClas()
I_SUB = [
  (" 1", "Land not cropped this and/or last season"),
  (" 2", "New lands being prepared for crop production"),
]
#
# -- BUTTONS
#
SubI= []
for mode, text in I_SUB:
  c = Radiobutton(frameSub, text=text, indicatoron=0,
variable=SubVal, value=mode, command=getSub)
  SubI.append(c)
  c.pack()
  #
  #
  # --
  #
  # NB - BARREN AND WASTELAND
  def DispSubNB():
frameSub= SubClas()
NB_SUB = [
  ("**", "None"),
  (" 1", "Dry stream channels"),
  (" 2", "Mine Tailing"),
  (" 3", "Barren land"),
  (" 4", "Salt flats"),
  (" 5", "Sand dunes"),
]
#
# -- BUTTONS
#
SubNB= []
for mode, text in NB_SUB:
  c = Radiobutton(frameSub, text=text, indicatoron=0,
variable=SubVal, value=mode, command=getSub)
  SubNB.append(c)
  c.pack()
  #
  #
  # --


SubWho can be a volatile list - change before calling buttons.


Simplicity itself.



Today is: 20090521
portions are from actual code
Python 2.5.2 and included Tkinter
Linux Slackware 10.2  (same source runs on Win XP PRO)

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


Re: Performance java vs. python

2009-05-21 Thread Sion Arrowsmith
Lie Ryan   wrote:
>Sion Arrowsmith wrote:
>> Once, when faced with a rather hairy problem that client requirements
>> dictated a pure Java solution for, I coded up a fully functional
>> prototype in Python to get the logic sorted out, and then translated
>> it. [And it wasn't pleasant.]
>
>Jython ?

This was back during Jython's wilderness years (barely maintained at
2.1 while the rest of us were on 2.3, or something like that. Mind
you, you could argue it's four versions behind at the moment.) Plus, I
was the only Python-head in the company just then. Plus, I meant that
"client requirements dictated a pure Java solution": they required the
ability to do a code audit. It's amazing the number and variety of
people who've drunk the Java Kool-Aid.

-- 
\S

   under construction

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


Re: Your Favorite Python Book

2009-05-21 Thread Esmail

Gökhan SEVER wrote:

Hello,

I received an autographed copy of CPP, 2nd Edition after joining to 
Safari's "What is Python" webcast. They published the recorded session 
online as well. Check  
http://www.safaribooksonline.com/Corporate/DownloadAndResources/webcasts.php


As you will see from the lecture, he is a very motivated instructor. 
Mostly likely you will want to grab a copy of the book after watching 
the condensed Python introduction course.




Hi Gökhan

This looks interesting .. I have access to Safari books, but for some
reason I can't access this webcast via it .. so perhaps I have to sign
up for a trial membership for this? I'd prefer to do this w/o .. so I
guess I'll poke around a bit more. Thanks for the lead,

Esmail

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


Re: SpellChecker

2009-05-21 Thread abosalim
On May 20, 12:37 pm, Mike Kazantsev 
wrote:
> abosalim wrote:
> > I used this code.It works fine,but on word not whole text.I want to
> > extend this code to correct
> > text file not only a word,but i don't know.If you have any help,please
> > inform me.
> ...
> > def correct(word):
> >     candidates = known([word]) or known(edits1(word)) or known_edits2
> > (word) or [word]
> >     return max(candidates, key=lambda w: NWORDS[w])
>
> Here I assume that "word" is any string consisting of letters, feel free
> to add your own check in place of str.isalpha, like word length or case.
> Note that simple ops like concatenation work much faster with buffers
> than str / unicode.
>
>   text = 'some text to correct (anything, really)'
>   result = buffer('')
>
>   word, c = buffer(''), ''
>   for c in text:
>     if c.isalpha(): word += c
>     else:
>       if word:
>         result += correct(word)
>         word = buffer('')
>       result += c
>
> --
> Mike Kazantsev // fraggod.net
>
>  signature.asc
> < 1KViewDownload
Thanks a lot
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: reseting an iterator

2009-05-21 Thread norseman

Terry Reedy wrote:

Jan wrote:

Wouldn't it be easy for Python to implement generating functions so
that the iterators they return are equipped with a __reset__() method?


No.  Such a method would have to poke around in the internals of the 
__next__ function in implementation specific ways.  The values used to 
initialize that function might have changed, so 'reset' would have to be 
carefully defined.


def squares():
  start = int(input("enter starting int:"))
  stop  = int(input("enter stopping int"))
  for i in range(start,stop):
yield i*i

What does 'reset' mean here?


I don't understand.  Why would one use a reset here in the first place? 
One simply re-runs this.  The output could be collected into a list, 
which one might want to reset, re-list.

bad example?





Here is the context of this question.

Python documentation defines  a "iterator" as an object ITERATOR
having methods __next__() and __iter__() such that the call
ITERATOR.__iter__() returns the object itself, 


This is so that 'iter(iterator) is iterator', 


You would do well to clarify the previous line.
To me it is the same as   a face is a face
function(x) is x  six of one, half dozen of the other
gobble-d-goop, double talk so what?
Not trying to pick a fight - just pointing out the need for some 
clarity.  Had a math teacher that proclaimed any function(x) that only 
returned x was (hummm can't print that here).  Something like useless.



so that functions can take
either an interable or iterator as an argument and proceed without 
checking which it got.


OK.




and once a call ITERATOR. __next__() raises StopIteration every

 >  such subsequent call does the same.



if it can't be restarted (serial line input) then don't, else do.
serial line, ethernet, etc are basically pipes. But a list itself can be 
re-wound, be it a file on disk, something in memory, on tape -- whatever


may have to reach back past the 'pipe' to the 'iterator' there to 
restart but being able to restart (elegantly) restartables is the point.



After returning objects for some number of calls, which might be unbounded.

The protocol is basically one method with defined behavior.  It is 
intentionally minimal so it can be used as the universal within-Python 
object stream protocol.  Multiple ways of getting interators is in line 
with this purpose.


I think clarity is also needed here.  Different disciplines will 
interpret the above differently. Stream means river, channel, pipe, 
singular direction to me.  Once the water flows past there is no hope of 
getting those molecules back in same order.  A list of things in a 
container (variable or file) is not a stream and can be rewound. The 
whole concept of random access hinges on this.

To me this boils down to two distinct concepts.
  one being stream as in here it comes there it goes, never to return.
The stream is not rewindable. The container you put it in might be.
  one being sequential reading of finite number of randomly accessible
things.  This being inherently rewindable.
Testing which is simple enough and can set the ability to rewind. 
Having it 'built-in' will reduce the problems generated by another 
'do-it-yourself' design by person that may or may not have thought 
things out.  The old - "I took the carburettor off the Olds but it 
doesn't work on my Hugo. Can you help me?" would be avoided.

Really - rewind is better if it is builtin and preforms where it should.
The docs should explain what will or will not happen and why. 
Preferably in plain language. :)




In short: are you telling us the reset() can't do in background the 
exact same thing that the docs tell the users to do?  It's a lot simpler 
to move file descriptors and pointers from the inside.


I often get so close to things I forget to look up too.




Terry Jan Reedy



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


Re: Performance java vs. python

2009-05-21 Thread namekuseijin
On May 21, 7:47 am, [email protected] (Sion Arrowsmith) wrote:
> Duncan Booth   wrote:
>
> >namekuseijin  wrote:
> >> I find it completely unimaginable that people would even think
> >> suggesting the idea that Java is simpler.  It's one of the most stupidly
> >> verbose and cranky languages out there, to the point you can't really do
> >> anything of relevance without an IDE automatically pumping out lots of
> >> scaffold code for you.
> >But that means Java programmers are obviously more productive than Python
> >programmers: they produce many more lines of code per day even if much of
> >it is boileplate or even automatically generated. Managers like that.
>
> >OTOH, I consider it a productive day if I end up with fewer lines of code
> >than I started with.
>
> A friend once justified a negative LOC count as being the sign of a
> good day with the following observation:
>
> Code that doesn't exist contains no bugs.
> Code that doesn't exist takes no time to execute.
> Code that doesn't exist takes up no space.
> Code that doesn't exist doesn't need maintenance.

Amusing tales.  And very true too -- managers just love LOC and
straightjacket programming environments.

Here's what the father of Unix, Ken Thompson, said once about LOC:
"One of my most productive days was throwing away 1000 lines of code."

http://www.brainyquote.com/quotes/quotes/k/kenthompso254858.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: making a python program in windows

2009-05-21 Thread Dave Angel



Rustom Mody wrote:

I know how to make a python script behave like a (standalone) program
in unix --
1. put a #! path/to/python as the first line
2. make the file executable

The closest I know how to do this in windows is:
r-click the file in win-explorer
goto properties
goto open with
change pythonw to python

Can someone enlighten me about a more scriptish way of doing this?
Basically if I have to setup that program on other (windows) m/cs is
there some .bat or .vbs or some regedit thingy Ive to do to avoid the
Rt-click routine?

  
Duncan told you about assoc and ftype, two programs that manipulate 
those associations (show and edit).  But one more thing you may want if 
you work much from a command line:


Look at the environment variable PATHEXT.  If it doesn't have a .PY and 
.PYW, you might want to add them.  That way when someone is typing a 
script name at the command prompt, they don't need an extension.



Anyway, now you can see two batch files you could use to make a 
particular version of Python active.  The first one uses assoc and ftype 
to fix the asssociations. And the other changes the environment variable 
PATHEXT to make the extension optional.  Note that changing the 
environment variable is effective only for that DOS box, and its 
children.  If you want a permanent change, you need to change the 
registry, probably at
  hklm\SYSTEM\ControlSet001\Contro/Session\Session 
Manager\Environment\PATHEXT



This can be automated either with a .REG file, or with a few lines of 
Python code that manipulates the registry.  The latter is better, 
because you can avoid disturbing the other extensions that will already 
be there.



You can also manipulate the registry to get SendTo items on the context 
menu for .PY files, and so on.



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


Re: A question regd configobj

2009-05-21 Thread Dave Angel

Srijayanth Sridhar wrote:

Hello,

I am wondering if it is possible to have hexadecimal strings in a ini file
and have configobj parse it correctly.

for eg:
moonw...@trantor:~/python/config$ cat foo
foo="\x96\x97"
.
.
.
  

a=ConfigObj("foo")
a


ConfigObj({'foo': '\\x96\\x97'})
  

a['foo']


'\\x96\\x97'

As you can see the string has escaped the '\' and I want to suppress that
behavior. I've looked at the documentation and haven't been able to figure
out if this is an available feature or not.

Any help will be greatly appreciated.

Thank you,

Jayanth

  
When you are using the Python interpreter, the interpreter will escape 
the strings it displays, if you use your present approach.  Try using 
print() to see what the string really contains.


Both approaches are useful -- the interpreter tries to show you 
approximately what you'd have to type to create that value.  Print just 
displays the character, allowing them to take whatever action is called 
for on the output device.


>>> st = "Line 1\nLine 2"
>>> st
'Line 1\nLine 2'
>>> print st
Line 1
Line 2
>>>

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


Re: python question

2009-05-21 Thread Dave Angel

Craig wrote:

How do i install this.i never seen a python write in c before.


  
Well, I've never seen a snake program in any language, python or 
otherwise.  And I believe python was named after Monty Python, not the 
snake.  But once it got its name, snake puns abound.


Anyway, why not tell you what you want to install, and on what 
platform?  If it's Python 2.6.2 on MS Windows XP, just download and run 
the msi file.
   On web page:   http://www.python.org/download/, you'd choose Python 
2.6.2 Windows installer 
  and it'd give 
you file python-2.6.2.msi




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


Re: How to Spawn a process with P_NOWAIT and pass it some data ?

2009-05-21 Thread Nick Craig-Wood
Barak, Ron  wrote:
>  This is my first try at IPC in Python, and I would like to ask your help wi=
>  th the following problem:
> 
>  I would like to spawn a process with P_NOWAIT, and pass some data to the ch=
>  ild process.
> 
>  I created two scripts to try IPC (in a blocking way):
> 
>  $ cat subprocess_sender.py
>  #!/usr/bin/env python
> 
>  import subprocess
> 
>  proc = subprocess.Popen(["python", "-u", "subprocess_receiver.py"],
>  stdin=subprocess.PIPE, shell=True)

You don't need shell=True here I wouldn't have thought.  It is a bad
idea in general.

>  proc.communicate(input="this is sent from subprocess_sender.py")[0]
>  proc.stdin.close()
>  and
> 
>  $ cat subprocess_receiver.py
>  #!/usr/bin/env python
> 
>  import sys
> 
>  print sys.stdin.readline()
> 
>  These scripts intercommunicate nicely:
> 
>  $ python -u  subprocess_sender.py
>  this is sent from subprocess_sender.py
[snip]
>  Can anyone suggest what is the correct way to implement P_NOWAIT and still
>  be able to communicate with the child process ?

You've written it already!  subprocess doesn't wait for a processes
until you call the communicate or wait methods.

If you want to communicate with the subprocess but not block waiting
for all of its output then use the file handle proc.stdout, eg


import subprocess
proc = subprocess.Popen(["python", "-u", "subprocess_receiver.py"], 
stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=0)
proc.stdin.write("10\n")
proc.stdin.write("this is sent from subprocess_sender.py\n")
proc.stdin.close()
while True:
line = proc.stdout.readline()
if not line:
break
print "Received %r" % line
proc.wait()

# subprocess_receiver.py

import sys
import time

times = int(sys.stdin.readline())
line = sys.stdin.readline().rstrip()

for x in range(times):
print "%d: %s" % (x, line)
time.sleep(1)

$ python subprocess_test3.py
Received '0: this is sent from subprocess_sender.py\n'
Received '1: this is sent from subprocess_sender.py\n'
Received '2: this is sent from subprocess_sender.py\n'
Received '3: this is sent from subprocess_sender.py\n'
Received '4: this is sent from subprocess_sender.py\n'
Received '5: this is sent from subprocess_sender.py\n'
Received '6: this is sent from subprocess_sender.py\n'
Received '7: this is sent from subprocess_sender.py\n'
Received '8: this is sent from subprocess_sender.py\n'
Received '9: this is sent from subprocess_sender.py\n'
Received '10: this is sent from subprocess_sender.py\n'
(printed with a 1 second pause between each line)


If you want to interact with a subprocess (eg send, receive, send,
receive) then use the pexpect module - buffering in subprocess will
cause you nothing but pain otherwise!

>  (Or, is there a way to create a subprocess.Popen object from what I assume =
>  is the process handle integer ?)

Errr, not as far as I know.

-- 
Nick Craig-Wood  -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


How to get path.py ? http://www.jorendorff.com/ is down

2009-05-21 Thread Jorge Vargas
Hello.

Anyone knows what is the problem with this package? apparently the
author's site is down which prevents pip from installing it. I can
download the zip and go from there but It seems most of the docs are
gone with the site.
-- 
http://mail.python.org/mailman/listinfo/python-list


How do I install these C modules in python? The tale of the C programming snake.

2009-05-21 Thread Luis Zarrabeitia

I don't know the answer, but to do you a favour (and increase the visibility), 
I'm replying with a more... explicit subject line.

=== Original message ===

On Thursday 21 May 2009 09:19:23 am Craig wrote:
> http://downloads.emperorlinux.com/contrib/pyiw
> http://downloads.emperorlinux.com/contrib/pywpa
>
>
> Sorry fro the 2 post.How do i install a python moudles write en in C?

=

[Suggestions for next time: always write a subject relevant to the /specific/ 
question you are asking. Bonus if you can make it interesting. Never leave 
the subject line empty. If you need to correct yourself, reply to your own 
message instead of opening a new thread.]

-- 
Luis Zarrabeitia (aka Kyrie)
Fac. de Matemática y Computación, UH.
http://profesores.matcom.uh.cu/~kyrie
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to get path.py ? http://www.jorendorff.com/ is down

2009-05-21 Thread Jorge Vargas
On Thu, May 21, 2009 at 3:43 PM, Jorge Vargas  wrote:
> Hello.
>
> Anyone knows what is the problem with this package? apparently the
> author's site is down which prevents pip from installing it. I can
> download the zip and go from there but It seems most of the docs are
> gone with the site.
>

For future references

there is a copy here
http://wiki.python.org/moin/PathModule
and the article is on the way back machine
http://web.archive.org/web/20071012022445/http://www.jorendorff.com/articles/python/path/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python3 module for dbus ?

2009-05-21 Thread Timothy Madden

Aahz wrote:

In article <[email protected]>,
Timothy Madden   wrote:

[...]
Do you know if I can get dbus bindings for python3 and glib bindings for 
python3 ? Or could I use them otherwise (like without the modules) ?


Sorry, no answers to your questions off-hand, but what's wrong with
using 2.x?


It is now old and will be replaced by 3.0
And I am starting a new project. I think it would be appropriate to 
start it with the new version of python.


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


Slicing an array in groups of eight

2009-05-21 Thread Graham Arden
A python novice writes.

Hello,

I'm trying to extract certain frames from a stack of images as part of
a project.  In order to do this I need to produce an array which
consists of a group of eight, then misses the next 8, then selects the
next eight etc.

i.e (0, 1, 2, 3, 4, 5, 6, 7, 16, 17,18, 19,20,21, 22, 23, 32,33,
etc)

The following code will produce a series of arrays:

a = arange (0,512)
b = [a[i:i + 8] for i in range (0, len(a), 16)]

[array([0, 1, 2, 3, 4, 5, 6, 7]),
 array([16, 17, 18, 19, 20, 21, 22, 23]),
 array([32, 33, 34, 35, 36, 37, 38, 39]),
 array([48, 49, 50, 51, 52, 53, 54, 55]),
 array([64, 65, 66, 67, 68, 69, 70, 71]),
 array([80, 81, 82, 83, 84, 85, 86, 87]),
etc...


unfortunately I can't work out a way of joining then into a single
array.

Alternatively is there a simpler way of producing the array above?

Thanks

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


PyXML difficulties

2009-05-21 Thread emperorcezar
I'm new to using the xml libs. I'm trying to create xml pragmatically,
but I'm finding an issue. I have two elements I'm creating using
createElementNS two elements (soap:Envelope and context). Each having
a different namespace. When I print the created xml, the namespace
attribute gets moved from the context element to the envelope element.
Is there a reason for this, and how can I get it to not do that?

Code:
from xml.dom import implementation
from xml.dom.ext import PrettyPrint

namespace = 'http://www.w3.org/2003/05/soap-envelope'

# create XML DOM document
doc = implementation.createDocument(None, '', None)

# create soap envelope element with namespaces
soapenv = doc.createElementNS(namespace, "soap:Envelope")

# add soap envelope element
doc.appendChild(soapenv)

# create header element
header = doc.createElementNS(namespace, "soap:Header")

context = doc.createElementNS("urn:zimbra", "context")

context.appendChild(doc.createTextNode(' '))

header.appendChild(context)

soapenv.appendChild(header)

PrettyPrint(doc)

What I'm getting as output:



  


  


What I would expect


  


  

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


4 hundred quadrillonth?

2009-05-21 Thread seanm . py
The explaination in my introductory Python book is not very
satisfying, and I am hoping someone can explain the following to me:

>>> 4 / 5.0
0.80004

4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
It bothers me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Overlapping region resolution

2009-05-21 Thread Scott David Daniels

[email protected] wrote:

This may be an algorithmic question, but I'm trying to code it in
Python, so...

I have a list of pairwise regions, each with an integer start and end
and a float data point. There may be overlaps between the regions. I
want to resolve this into an ordered list with no overlapping
regions.
... (indication of having at least struggled a bit) ...
Devising an algorithm to do this is making my brain hurt. Any ideas?


I really hope I'm not giving away the key to a homework problem.
One sort key for such lists that I've found fruitful in the past is:
(start, -end)
Maybe it will prove helpful to you as well.

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread MRAB

[email protected] wrote:

The explaination in my introductory Python book is not very
satisfying, and I am hoping someone can explain the following to me:


4 / 5.0

0.80004

4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
It bothers me.


Read http://docs.python.org/tutorial/floatingpoint.html

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Christian Heimes
[email protected] schrieb:
> The explaination in my introductory Python book is not very
> satisfying, and I am hoping someone can explain the following to me:
> 
 4 / 5.0
> 0.80004
> 
> 4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
> It bothers me.

Welcome to IEEE 754 floating point land! :)

Christian

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread seanm . py
On May 21, 5:36 pm, Christian Heimes  wrote:
> [email protected] schrieb:
>
> > The explaination in my introductory Python book is not very
> > satisfying, and I am hoping someone can explain the following to me:
>
>  4 / 5.0
> > 0.80004
>
> > 4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
> > It bothers me.
>
> Welcome to IEEE 754 floating point land! :)
>
> Christian

Thanks for the link and the welcome. Now onward to Bitwise
Operations

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


lxml: traverse xml tree and retrieve element based on an attribute

2009-05-21 Thread byron
I am using the lxml.etree library to validate an xml instance file
with a specified schema that contains the data types of each element.
This is some of the internals of a function that extracts the
elements:

schema_doc = etree.parse(schema_fn)
schema = etree.XMLSchema(schema_doc)

context = etree.iterparse(xml_fn, events=('start', 'end'),
schema=schema)

# get root
event, root = context.next()

for event, elem in context:
if event == 'end' and elem.tag == self.tag:
yield elem
root.clear()

I retrieve a list of elements from this... and do further processing
to represent them in different ways. I need to be able to capture the
data type from the schema definition for each field in the element.
i.e.













My thought is to recursively traverse through the schema definition
match the `name` attribute since they are unique to a `type` and
return that element. But I can't seem to make it quite work. All the
xml is valid, validation works, etc. This is what I have:

def find_node(tree, name):
for c in tree:
if c.attrib.get('name') == name:
return c
if len(c) > 0:
return find_node(c, name)
return 0

I may have been staring at this too long, but when something is
returned... it should be returned completely, no? This is what occurs
with `return find_node(c, name) if it returns 0. `return c` works
(used pdb to verify that), but the recursion continues and ends up
returning 0.

Thoughts and/or a different approach are welcome. Thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Slicing an array in groups of eight

2009-05-21 Thread Vlastimil Brom
2009/5/21 Graham Arden :
> A python novice writes.
>
> Hello,
>
> I'm trying to extract certain frames from a stack of images as part of
> a project.  In order to do this I need to produce an array which
> consists of a group of eight, then misses the next 8, then selects the
> next eight etc.
>
> i.e (0, 1, 2, 3, 4, 5, 6, 7, 16, 17,18, 19,20,21, 22, 23, 32,33,
> etc)
>
...>
> Alternatively is there a simpler way of producing the array above?
>
> Thanks
>
> Graham.
>

Hi,
I'm not sure, if I got the requirements for your code completely, but is:
[x for x in range(512) if not (x // 8) % 2]
what you need?

In any case, if you use python lists you should be able to join them
using their extend() or itertools.chain()

###

print [x for x in range(512) if not (x // 8) % 2]

nested_lst = [[1,2],[4,5],[6,7,8,9],[10,11,12]]
flattened_list_1 = list(itertools.chain(*nested_lst))
print flattened_list_1

flattened_list_2 = []
for sublist in nested_lst:
flattened_list_2.extend(sublist)
print flattened_list_2

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


ffmpeg and python big problem

2009-05-21 Thread TerabyteST
Hello. I am trying to make a video from images shot by my webcam in
python. I use a module I found on the net (here
http://osdir.com/ml/python.matplotlib.general/2005-10/msg00145.html )
but, even if I think I am doing everything correctly, what I only get
is a grey video with some multi-color squares on the top-left bit... I
don't think it's a problem with ffmpeg because I tried with two
different versions. Not even with the codec: i tried wmv, mpg, avi and
everything, but still the same result. And also, my webcam does the
capture right because if I check the image it's OK. Last thing left,
IMO, is the module, but I can't seem to find whats the problem... Is
it because the module was (maybe) made on Linux and I'm working on
windows? If you would help me I'd be so glad!
This program is for my friend and he needs it ready preety quick.

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Carl Banks
On May 21, 2:05 pm, [email protected] wrote:
> The explaination in my introductory Python book is not very
> satisfying, and I am hoping someone can explain the following to me:
>
> >>> 4 / 5.0
>
> 0.80004
>
> 4 / 5.0 is 0.8. No more, no less.

That would depend on how you define the numbers and division.

What you say is correct for real numbers and field division.  It's not
true for the types of numbers Python uses, which are not real numbers.

Python numbers are floating point numbers, defined (approximately) by
IEEE 754, and they behave similar to but not exactly the same as real
numbers.  There will always be small round-off errors, and there is
nothing you can do about it except to understand it.


> It bothers me.

Oh well.

You can try Rational numbers if you want, I think they were added in
Python 2.6.  But if you're not careful the divisors can get
ridiculously large.


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


Re: Wrapping methods of built-in dict

2009-05-21 Thread shailesh
On May 20, 7:31 pm, Steven D'Aprano
 wrote:
> On Wed, 20 May 2009 18:42:38 -0700, shailesh wrote:
> > The reason as far as I understand is that the methods on the built-in
> > dict are not of MethodType or FunctionType
>
> That seems to be true:
>
> >>> type({}.get)
> 
>
>>> type(dict.get>
> 
>
> > so they are not included in
> > the result of the inspect.getmembers call and are not wrapped.
>
> But that isn't:
>
> >>> zip(*inspect.getmembers(dict))[0]  # extract the names only
>
> ('__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__',
> '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__',
> '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__',
> '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
> '__setattr__', '__setitem__', '__str__', 'clear', 'copy', 'fromkeys',
> 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys',
> 'pop', 'popitem', 'setdefault', 'update', 'values')
>
> So the problem isn't directly with getmembers, but with the predicate
> functions you have passed to it (inspect.isfunction and inspect.method).
> Try inspect.ismethoddescriptor instead.

Thanks for that. Between ismethod, isfunction, ismethoddescriptor and
isbuiltin I think I can cover all the types needed for wrapping
classes.

I began writing an equivalent version for wrapping objects instead of
classes. The problem I run into is that there isn't a predicate for
some methods of dict instances

>>> all = set([ name for (name, method) in inspect.getmembers({}) ])# get 
>>> all the methods
>>> builtin = set([ name for (name, method) in inspect.getmembers({}, 
>>> inspect.isbuiltin) ])  # get the builtin methods
>>> for m in all.difference(builtin):# print the types of what's left over
... print m, type(getattr({}, m))
...
__ne__ 
__setattr__ 
__hash__ 
__delitem__ 
__str__ 
__getattribute__ 
__class__ 
__cmp__ 
__delattr__ 
__iter__ 
__le__ 
__len__ 
__gt__ 
__setitem__ 
__lt__ 
__ge__ 
__eq__ 
__doc__ 
__init__ 
__repr__ 

>>> [ attr for attr in dir(inspect) if attr.startswith('is') ]   # list of 
>>> predicates supported by inspect
['isabstract', 'isbuiltin', 'isclass', 'iscode', 'isdatadescriptor',
'isframe', 'isfunction', 'isgenerator', 'isgeneratorfunction',
'isgetsetdescriptor', 'ismemberdescriptor', 'ismethod',
'ismethoddescriptor', 'ismodule', 'isroutine', 'istraceback']

>>> inspect.getmembers({}, inspect.ismethod)
[]
>>> inspect.getmembers({}, inspect.isfunction)
[]
>>> inspect.getmembers({}, inspect.ismemthoddescriptor)
[]

There doesn't seem to be a predicate returning method wrappers. Is
there an alternate way to query an object for attributes that are of
method wrappers?

This exercise also makes me question if I'm going about this
correctly. If I want to add functionality to the methods of a class or
an object are decorators and the inspect module the pythonic way to go
about it? I can think of alternative implementations either through
metaclasses or proxy objects.

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


Re: Slicing an array in groups of eight

2009-05-21 Thread Emile van Sebille

On 5/21/2009 1:51 PM Graham Arden said...

A python novice writes.

Hello,

I'm trying to extract certain frames from a stack of images as part of
a project.  In order to do this I need to produce an array which
consists of a group of eight, then misses the next 8, then selects the
next eight etc.

i.e (0, 1, 2, 3, 4, 5, 6, 7, 16, 17,18, 19,20,21, 22, 23, 32,33,
etc)

The following code will produce a series of arrays:

a = arange (0,512)



b = [a[i:i + 8] for i in range (0, len(a), 16)]


How about...

>>> a = range(0,512)
>>> b = []
>>> [b.extend(a[i:i + 8]) for i in range (0, len(a), 16)]
>>> b
[0, 1, 2, 3, 4, 5, 6, 7, 16, 17, 18, 19, 20, 21, 22, 23, 32,
...
497, 498, 499, 500, 501, 502, 503]
>>>

Emile

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


Re: dbfpy - cannot store new record

2009-05-21 Thread David Lyon

well, dbfpy isn't super sophisticated.

If you make your own code fixes, maybe you can provide them
back to the package author.


On Thu, 21 May 2009 17:53:38 +0200, Laszlo Nagy 
wrote:
> Here is the next problem. For boolean/logical fields, I can set their 
> value to True/False easily. However, setting NULL seems impossible:
> 
> rec = tbl.newRecord()
> rec["SOMEFIELD1"] = True # Works fine
> rec["SOMEFIELD2"] = False # Works fine
> rec["SOMEFIELD3"] = None # Will store False
> rec["SOMEFIELD3"] = 0 # Will store False
> rec["SOMEFIELD3"] = "" # Will store False
> rec["SOMEFIELD3"] = chr(32) # Will store False
> rec["SOMEFIELD3"] = chr(0) # Will store False
> rec.store()
> 
> Strange thing: if I do not set the value of a numeric field, it becomes 
> NULL. The same thing I cannot do for logical fields: if I do not set the 
> value of a logical field, it becomes an invalid value, denoted with a 
> question mark.
> 
> Any ideas?
> 
> Thanks,
> 
>Laszlo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ffmpeg and python big problem

2009-05-21 Thread Emile van Sebille

On 5/21/2009 2:48 PM TerabyteST said...

Hello. I am trying to make a video from images shot by my webcam in
python. I use a module I found on the net (here
http://osdir.com/ml/python.matplotlib.general/2005-10/msg00145.html )
but, even if I think I am doing everything correctly, what I only get
is a grey video with some multi-color squares on the top-left bit... I
don't think it's a problem with ffmpeg because I tried with two
different versions. Not even with the codec: i tried wmv, mpg, avi and
everything, but still the same result. And also, my webcam does the
capture right because if I check the image it's OK. Last thing left,
IMO, is the module, but I can't seem to find whats the problem... Is
it because the module was (maybe) made on Linux and I'm working on
windows? If you would help me I'd be so glad!
This program is for my friend and he needs it ready preety quick.


Why not use mencoder directly?  Last time I did this I used Python to 
organize the shots and mencoder to create the video...


Emile

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


Re: PyXML difficulties

2009-05-21 Thread Paul Boddie
On 21 Mai, 22:58, emperorcezar  wrote:
> I'm new to using the xml libs. I'm trying to create xml pragmatically,
> but I'm finding an issue. I have two elements I'm creating using
> createElementNS two elements (soap:Envelope and context). Each having
> a different namespace. When I print the created xml, the namespace
> attribute gets moved from the context element to the envelope element.
> Is there a reason for this, and how can I get it to not do that?

Having the default namespace declared on an ancestor of the context
element is completely valid in this case, since the soap:Envelope
element is associated with a different namespace. You could see what
happens if you serialise the document using a different library
function or document/node method, however.

> Code:
>     from xml.dom import implementation
>     from xml.dom.ext import PrettyPrint
>
>     namespace = 'http://www.w3.org/2003/05/soap-envelope'
>
>     # create XML DOM document
>     doc = implementation.createDocument(None, '', None)

[...]

Note that according to the specifications, the element name should be
None (null) for this kind of createDocument invocation. See here for
details:

http://www.w3.org/TR/DOM-Level-3-Core/core.html#Level-2-Core-DOM-createDocument

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


Re: Slicing an array in groups of eight

2009-05-21 Thread Robert Kern

On 2009-05-21 15:51, Graham Arden wrote:

A python novice writes.

Hello,

I'm trying to extract certain frames from a stack of images as part of
a project.  In order to do this I need to produce an array which
consists of a group of eight, then misses the next 8, then selects the
next eight etc.

i.e (0, 1, 2, 3, 4, 5, 6, 7, 16, 17,18, 19,20,21, 22, 23, 32,33,
etc)

The following code will produce a series of arrays:

a = arange (0,512)
b = [a[i:i + 8] for i in range (0, len(a), 16)]

[array([0, 1, 2, 3, 4, 5, 6, 7]),
  array([16, 17, 18, 19, 20, 21, 22, 23]),
  array([32, 33, 34, 35, 36, 37, 38, 39]),
  array([48, 49, 50, 51, 52, 53, 54, 55]),
  array([64, 65, 66, 67, 68, 69, 70, 71]),
  array([80, 81, 82, 83, 84, 85, 86, 87]),
etc...


unfortunately I can't work out a way of joining then into a single
array.

Alternatively is there a simpler way of producing the array above?


b = a.reshape((-1, 8))

You will want to 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: python3 module for dbus ?

2009-05-21 Thread Stephen Hansen
>
>
>  Do you know if I can get dbus bindings for python3 and glib bindings for
>>> python3 ? Or could I use them otherwise (like without the modules) ?
>>>
>>
>> Sorry, no answers to your questions off-hand, but what's wrong with
>> using 2.x?
>>
>
> It is now old and will be replaced by 3.0
> And I am starting a new project. I think it would be appropriate to start
> it with the new version of python.
>

That's really a premature position to take. 2.x is going to continue to be
developed in tandem with 3.0. They're going to release a 2.7, for example.
It's going to take some non-trivial time for there to be all the big
bindings and third party libraries to even start to be ported over.

In particular, I doubt anything on your list would anyitme soon. I know
wxPython isn't even close from what I've read; I've not heard anything of
any of the PostgreSQL libraries doing it but they might be starting or have
made progress already...

Really. Using 2.x is not developing for a dead-end platform. Yeah you should
code in such a way that its as upwards-compatible to 3.x as possible for a
new project, but... 2.6 is not "old", nor deprecated, nor obsolete... and
2.7 will be out Eventually.

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


Re: reseting an iterator

2009-05-21 Thread Terry Reedy

I will clarify by starting over with current definitions.

Ob is an iterator iff next(ob) either returns an object or raises 
StopIteration and continues to raise StopIteration on subsequent calls.


Ob is an iterable iff iter(ob) raturns an iterator.

It is intentional that the protocol definitions be minimal, so that they 
can used as widely as possible.


As a convenience, the definition of iterators is given a slight 
complication.  They are defined as a subcategory of iterables, with the 
 requirement that iter(iterator) be that same iterator.  This means 
that iterators need the following boilerplate:

  def __iter__(self): return self
The extra burden is slight since most iterators are based on builtins or 
generator functions or expressions, which add the boilerplate 
automatically.  The convenience is that one may write


def f(iterable_or_iterator):
  it = iter(iterable_or_iterator)
  ...

instead of

def f(iterable_or_iterator):
  if is_iterable(iterable_or_iterator):
it = iter(iterable_or_iterator)
  else:
it = iterable_or_iterator

In particular, the internal function that implements for loops can do 
the former.


In other words, a small bit of boilerplate added to iterators, mostly 
automatically, saves boilerplate in the use of iterators and iterables.


When the protocols were defined, there was discussion about whether or 
not to require 'continue to raise StopIteration'.  For instance, an 
iterator that returns objects derived from external input might not have 
any new external input now but expect to get some in the future.  It was 
decided the such iterators should either wait and block the thread or 
return a 'Not now' indicator such as None.  StopIteration should 
consistently mean 'Done, over and out' so for loops, for instance, would 
know to exit.


The OP proposes that StopIteraton should instead mean 'Done until 
reset', without defining 'reset'.  Some comments:

* This would complicate the protocol.
* There are real use cases, and reiterability is a real issue.  But ...
* Depending on the meaning, resetting may or may not be possible.
* When it is possible, it can potentially be done today with a .send() 
method.

* Many use cases are easier with a new iterator.  For instance

for i in iterable: block1()
for i in iterable: block2()

is easier to write than

it = iter(iterable)
for i in it: block1()
it.reset()
for i in it: block2()

with little relative time saving in the second case, for practical 
problems, to compensate for the extra boilerplate.


Terry Jan Reedy

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Chris Rebert
On Thu, May 21, 2009 at 2:53 PM, Carl Banks  wrote:
> On May 21, 2:05 pm, [email protected] wrote:
>> The explaination in my introductory Python book is not very
>> satisfying, and I am hoping someone can explain the following to me:
>>
>> >>> 4 / 5.0
>>
>> 0.80004
>>
>> 4 / 5.0 is 0.8. No more, no less.
>
> That would depend on how you define the numbers and division.
>
> What you say is correct for real numbers and field division.  It's not
> true for the types of numbers Python uses, which are not real numbers.
>
> Python numbers are floating point numbers, defined (approximately) by
> IEEE 754, and they behave similar to but not exactly the same as real
> numbers.  There will always be small round-off errors, and there is
> nothing you can do about it except to understand it.
>
>
>> It bothers me.
>
> Oh well.
>
> You can try Rational numbers if you want, I think they were added in
> Python 2.6.  But if you're not careful the divisors can get
> ridiculously large.

The `decimal` module's Decimal type is also an option to consider:

Python 2.6.2 (r262:71600, May 14 2009, 16:34:51)
>>> from decimal import Decimal
>>> Decimal(4)/Decimal(5)
Decimal('0.8')

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Chris Rebert
On Thu, May 21, 2009 at 2:53 PM, Carl Banks  wrote:
> On May 21, 2:05 pm, [email protected] wrote:
>> The explaination in my introductory Python book is not very
>> satisfying, and I am hoping someone can explain the following to me:
>>
>> >>> 4 / 5.0
>>
>> 0.80004
>>
>> 4 / 5.0 is 0.8. No more, no less.
>
> That would depend on how you define the numbers and division.
>
> What you say is correct for real numbers and field division.  It's not
> true for the types of numbers Python uses, which are not real numbers.
>
> Python numbers are floating point numbers, defined (approximately) by
> IEEE 754, and they behave similar to but not exactly the same as real
> numbers.  There will always be small round-off errors, and there is
> nothing you can do about it except to understand it.
>
>
>> It bothers me.
>
> Oh well.
>
> You can try Rational numbers if you want, I think they were added in
> Python 2.6.  But if you're not careful the divisors can get
> ridiculously large.

The `decimal` module's Decimal type is also an option to consider:

Python 2.6.2 (r262:71600, May 14 2009, 16:34:51)
>>> from decimal import Decimal
>>> Decimal(4)/Decimal(5)
Decimal('0.8')

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread norseman

[email protected] wrote:

The explaination in my introductory Python book is not very
satisfying, and I am hoping someone can explain the following to me:


4 / 5.0

0.80004

4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
It bothers me.

==

Machine architecture, actual implementation of logic on the chip and 
what the compiler maker did all add up to creating rounding errors. I 
have read where python, if left to its own, will output everything it 
computed. I guess the idea is to show

1) python's accuracy and
2) what was left over
so the picky people can have something to gnaw on.

Astrophysics, Astronomers and like kind may have wants of such.
If you work much in finite math you may want to test the combo to see if 
 it will allow the accuracy you need. Or do you need to change machines?


Beyond that - just fix the money at 2, gas pumps at 3 and the 
sine/cosine at 8 and let it ride. :)



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


Re: lxml: traverse xml tree and retrieve element based on an attribute

2009-05-21 Thread MRAB

byron wrote:

I am using the lxml.etree library to validate an xml instance file
with a specified schema that contains the data types of each element.
This is some of the internals of a function that extracts the
elements:

schema_doc = etree.parse(schema_fn)
schema = etree.XMLSchema(schema_doc)

context = etree.iterparse(xml_fn, events=('start', 'end'),
schema=schema)

# get root
event, root = context.next()

for event, elem in context:
if event == 'end' and elem.tag == self.tag:
yield elem
root.clear()

I retrieve a list of elements from this... and do further processing
to represent them in different ways. I need to be able to capture the
data type from the schema definition for each field in the element.
i.e.













My thought is to recursively traverse through the schema definition
match the `name` attribute since they are unique to a `type` and
return that element. But I can't seem to make it quite work. All the
xml is valid, validation works, etc. This is what I have:

def find_node(tree, name):
for c in tree:
if c.attrib.get('name') == name:
return c
if len(c) > 0:
return find_node(c, name)
return 0


You're searching the first child and then returning the result, but what
you're looking for might not be in the first child; if it's not then you
need to search the next child:

def find_node(tree, name):
for c in tree:
if c.attrib.get('name') == name:
return c
if len(c) > 0:
r = find_node(c, name)
if r:
return r
return None


I may have been staring at this too long, but when something is
returned... it should be returned completely, no? This is what occurs
with `return find_node(c, name) if it returns 0. `return c` works
(used pdb to verify that), but the recursion continues and ends up
returning 0.

Thoughts and/or a different approach are welcome. Thanks


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


Re: reseting an iterator

2009-05-21 Thread norseman

Terry Reedy wrote:

I will clarify by starting over with current definitions.

Ob is an iterator iff next(ob) either returns an object or raises 
StopIteration and continues to raise StopIteration on subsequent calls.


Ob is an iterable iff iter(ob) raturns an iterator.

It is intentional that the protocol definitions be minimal, so that they 
can used as widely as possible.


As a convenience, the definition of iterators is given a slight 
complication.  They are defined as a subcategory of iterables, with the 
 requirement that iter(iterator) be that same iterator.  This means that 
iterators need the following boilerplate:

  def __iter__(self): return self
The extra burden is slight since most iterators are based on builtins or 
generator functions or expressions, which add the boilerplate 
automatically.  The convenience is that one may write


def f(iterable_or_iterator):
  it = iter(iterable_or_iterator)
  ...

instead of

def f(iterable_or_iterator):
  if is_iterable(iterable_or_iterator):
it = iter(iterable_or_iterator)
  else:
it = iterable_or_iterator

In particular, the internal function that implements for loops can do 
the former.


In other words, a small bit of boilerplate added to iterators, mostly 
automatically, saves boilerplate in the use of iterators and iterables.


When the protocols were defined, there was discussion about whether or 
not to require 'continue to raise StopIteration'.  For instance, an 
iterator that returns objects derived from external input might not have 
any new external input now but expect to get some in the future.  It was 
decided the such iterators should either wait and block the thread or 
return a 'Not now' indicator such as None.  StopIteration should 
consistently mean 'Done, over and out' so for loops, for instance, would 
know to exit.


The OP proposes that StopIteraton should instead mean 'Done until 


Done unless you put the data pointer back to offset zero


reset', without defining 'reset'.  Some comments:
* This would complicate the protocol.
* There are real use cases, and reiterability is a real issue.  But ...
* Depending on the meaning, resetting may or may not be possible.
* When it is possible, it can potentially be done today with a .send() 
method.

* Many use cases are easier with a new iterator.  For instance

for i in iterable: block1()
for i in iterable: block2()

is easier to write than

it = iter(iterable)
for i in it: block1()
it.reset()
for i in it: block2()

with little relative time saving in the second case, for practical 
problems, to compensate for the extra boilerplate.





while testing:
  for i in it:
code
  it.reset()




Terry Jan Reedy



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


Re: reseting an iterator

2009-05-21 Thread Terry Reedy

norseman wrote:

Terry Reedy wrote:

I will clarify by starting over with current definitions.

Ob is an iterator iff next(ob) either returns an object or raises 
StopIteration and continues to raise StopIteration on subsequent calls.


Ob is an iterable iff iter(ob) raturns an iterator.

It is intentional that the protocol definitions be minimal, so that 
they can used as widely as possible.


As a convenience, the definition of iterators is given a slight 
complication.  They are defined as a subcategory of iterables, with 
the  requirement that iter(iterator) be that same iterator.  This 
means that iterators need the following boilerplate:

  def __iter__(self): return self
The extra burden is slight since most iterators are based on builtins 
or generator functions or expressions, which add the boilerplate 
automatically.  The convenience is that one may write


def f(iterable_or_iterator):
  it = iter(iterable_or_iterator)
  ...

instead of

def f(iterable_or_iterator):
  if is_iterable(iterable_or_iterator):
it = iter(iterable_or_iterator)
  else:
it = iterable_or_iterator

In particular, the internal function that implements for loops can do 
the former.


In other words, a small bit of boilerplate added to iterators, mostly 
automatically, saves boilerplate in the use of iterators and iterables.


When the protocols were defined, there was discussion about whether or 
not to require 'continue to raise StopIteration'.  For instance, an 
iterator that returns objects derived from external input might not 
have any new external input now but expect to get some in the future.  
It was decided the such iterators should either wait and block the 
thread or return a 'Not now' indicator such as None.  StopIteration 
should consistently mean 'Done, over and out' so for loops, for 
instance, would know to exit.


The OP proposes that StopIteraton should instead mean 'Done until 


Done unless you put the data pointer back to offset zero


And if there is not data pointer?




reset', without defining 'reset'.  Some comments:
* This would complicate the protocol.
* There are real use cases, and reiterability is a real issue.  But ...
* Depending on the meaning, resetting may or may not be possible.
* When it is possible, it can potentially be done today with a .send() 
method.

* Many use cases are easier with a new iterator.  For instance

for i in iterable: block1()
for i in iterable: block2()

is easier to write than

it = iter(iterable)
for i in it: block1()
it.reset()
for i in it: block2()

with little relative time saving in the second case, for practical 
problems, to compensate for the extra boilerplate.





while testing:
  for i in it:
code
  it.reset()




Terry Jan Reedy





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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Grant Edwards
On 2009-05-21, Christian Heimes  wrote:
> [email protected] schrieb:
>> The explaination in my introductory Python book is not very
>> satisfying, and I am hoping someone can explain the following to me:
>> 
> 4 / 5.0
>> 0.80004
>> 
>> 4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
>> It bothers me.
>
> Welcome to IEEE 754 floating point land! :)

Floating point is sort of like quantum physics: the closer you
look, the messier it gets.

-- 
Grant

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


Re: join two selects

2009-05-21 Thread gert
On May 21, 4:54 pm, Tim Golden  wrote:
> gert wrote:
> > I am trying to figure out how to join two selects ?
>
> > SELECT * FROM search
> > SELECT eid, SUM(pnt) AS total_votes FROM vote
>
> > CREATE TABLE votes (
> >     eid  INTEGER PRIMARY KEY,
> >     uid  VARCHAR(64),
> >     pnt  INETEGER DEFAULT 0,
> > );
>
> > CREATE TABLE search (
> >     eid  INTEGER PRIMARY KEY,
> >     txt  VARCHAR(64),
> >     end  DATETIME
> > );
>
> > so the result would be a table that looks like this
>
> > ["eid", "txt", "end", "total_votes"]
>
> That's what's known technically as a join:
>
> SELECT
>   sea.eid,
>   sea.txt,
>   sea.end,
>   SUM (vot.pnt) AS total_votes
> FROM
>   search AS sea
> JOIN votes AS vot ON
>   vot.eid = sea.eid
> GROUP BY
>   sea.eid,
>   sea.txt,
>   sea.end,
>
> (Guessing the join condition from the column names)
>
> TJG

Thanks works great :-)
just needed to at LEFT JOIN and remove sea.txt sea.end from the GROUP
BY
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: A list with periodic boundary conditions

2009-05-21 Thread Rhodri James

On Thu, 21 May 2009 13:08:39 +0100,  wrote:


Hi,

I'm trying to create a new class of list that has periodic boundary
conditions.

Here's what I have so far:

class wrappedList(list):
def __getitem__(self, p):
return list.__getitem__(self, p%len(self))
def __setitem__(self, p, v):
list.__setitem__(self, p%len(self), v)


a=wrappedList(range(10))
a[1]

1

a[11]

1

But I would also like to make slices. For instance I would like

a[8:11]

[8, 9, 0]

I can do it by copying, but I want to return a view to the original
data, and I have no idea where to start. It seems that the slice needs
to retain some knowledge of the list from which it derived. i.e. it
needs to know that it is a slice. Any ideas on how I can extend this
to allow views?


Reading the docs, this looks like a very messy area of Python 2.x in
that for a builtin like `list` you have to provide a __getslice__ method
despite it being deprecated and only dealing with simple slices.  Good
luck with that.  In Python 3, you just have to deal with the fact that
your __getitem__ and __setitem__ `p` arguments can be slice objects
instead of integers.

If you're set on making your slices views on the original (which means
that changes to the slice will change the original, unlike normal lists!)
then you need to extend your class to remember what it's viewing, and
what the start, stop and step of the slice were.  When it's a view, it
passes (massaged) requests up to its parent.  There's a cute (but likely
inefficient) way of doing this that uses the fact that your view is still
a list under the hood, and stashes the parental indices in it.  This will
also make len() work correctly, for a bonus :-)  It gets quite horrid
quite fast, but here's a very incomplete untested skeleton:

class wrappedList(list):
  def __init__(self, parent=None, *args):
list.__init__(self, *args)
self.parent = parent

  def __getitem__(self, p):
if self.parent is None:
  self.primary_getitem(p)
else:
  self.view_getitem(p)

  def view_getitem(self, p):
return self.parent[list.__getitem__(self, p%len(self))]

...and similarly for __setitem__, where primary_getitem() is your
previous __getitem__ method.  All four need to be modified to cope
with slices, of course, and to create new wrappedList objects.
Something like this:

  if isinstance(p, slice):
if p.start is None:
  start = 0
else:
  start = p.start
if p.step is None:
  step = 1
else:
  step = p.step
indices = range(start, p.stop, step)
return wrappedList(indices, parent=self)

This will go horribly, horribly wrong if you delete anything from
your original list, but I can't off-hand think of a view method
that won't.

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: finding repeated data sequences in a column

2009-05-21 Thread norseman

yadin wrote:

On May 20, 6:53 pm, norseman  wrote:

[email protected] wrote:

yadin:

How can I build up a program that tells me that this sequence
128706
128707
128708
is repeated somewhere in the column, and how can i know where?

Can such patterns nest? That is, can you have a repeated pattern made
of an already seen pattern plus something else?
If you don't want a complex program, then you may need to specify the
problem better.
You may want something like LZ77 or releated (LZ78, etc):
http://en.wikipedia.org/wiki/LZ77
This may have a bug:
http://code.activestate.com/recipes/117226/
Bye,
bearophile


index on column
Ndx1 is set to index #1
Ndx2 is set to index #2
test Ndx1 against Ndx2
   if equal write line number and column content to a file
   (that's two things on one line:  15 128706
   283 128706 )
   Ndx1 is set to Ndx2
   Ndx2 is set to index #next
loop to testwriting out each duplicate set

Then use the outfile and index on line number

In similar manor, check if line current and next line line numbers are
sequential.  If so scan forward to match column content of lower line
number and check first matched column's line number and next for
sequential.  Print them out if so

everything in outfile has 1 or more duplicates

4  aa   |--
5  bb |--  |  thus 4/5 match 100/101
6  cc| |
.| |
100 aa   |  |--
101 bb |--
102 ddd
103 cc  there is a duplicate but not a sequence
200 ff

mark duplicate sequences as tested and proceed on through
   seq1 may have more than one other seq in file.
   the progress is from start to finish without looking back
   thus each step forward has fewer lines to test.
   marking already knowns eliminates redundant sequence testing.

By subseting on pass1 the expensive testing is greatly reduced.
If you know your subset data won't exceed memory then the "outfile"
can be held in memory to speed things up considerably.

Today is: 20090520
no code

Steve- Hide quoted text -

- Show quoted text -


this is the program...I wrote but is not working
I have a list of valves, and another of pressures;
If I am ask to find out which ones are the valves that are using all
this set of pressures, wanted best pressures
this is the program i wrote but is not working properly, it suppossed
to return in the case
find all the valves that are using pressures 1 "and" 2 "and" 3.
It returns me A, A2, A35


looking at the data that seems correct.
there are 3 '1's in the list, 1-A, 1-A2, 1-A35
there are 2 '2's in the list, 2-A, 2-A2
there are 2 '3's in the list, 3-A, 3-A2
and so on

  after the the two sets are paired
indexing on the right yields 1-A,2-A,3-A,1-A2,2-A2,3-A2,7-A4...
indexing on the left  yiels1 1-A,1-A2,1-A35,2-A,2-A2,3-A,3-A2,7-A4...
and the two 78s would pair with a G and with a G2  (78-G, 78-G2)
beyond that I'm a bit lost.

20090521 Steve


The correct answer supposed to be A and A2...
if I were asked for pressures 56 and 78 the correct answer supossed to
be valves G and G2...

Valves = ['A','A','A','G', 'G', 'G',
'C','A2','A2','A2','F','G2','G2','G2','A35','A345','A4'] ##valve names
pressures = [1,2,3,4235,56,78,12, 1, 2, 3, 445, 45,56,78,1, 23,7] ##
valve pressures
result = []

bestpress = [1,2,3] ##wanted base pressures
print bestpress,'len bestpress is' , len(bestpress)

print len(Valves)
print len(Valves)
for j in range(len(Valves)):
#for i in range(len(bestpress)):
#for j in range(len(Valves)):
for i in range(len(bestpress)-2):
if pressures [j]== bestpress[i] and bestpress [i+1]
==pressures [j+1] and bestpress [i+2]==pressures [j+2]:
result.append(Valves[j])
#i = i+1
#j = j+1
# print i, j, bestpress[i]
print "common PSVs are", result


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


Re: dbfpy - cannot store new record

2009-05-21 Thread John Machin
On May 22, 1:53 am, Laszlo Nagy  wrote:
> Here is the next problem. For boolean/logical fields, I can set their
> value to True/False easily. However, setting NULL seems impossible:
>
> rec = tbl.newRecord()
> rec["SOMEFIELD1"] = True # Works fine
> rec["SOMEFIELD2"] = False # Works fine
> rec["SOMEFIELD3"] = None # Will store False
> rec["SOMEFIELD3"] = 0 # Will store False
> rec["SOMEFIELD3"] = "" # Will store False
> rec["SOMEFIELD3"] = chr(32) # Will store False
> rec["SOMEFIELD3"] = chr(0) # Will store False
> rec.store()
>
> Strange thing: if I do not set the value of a numeric field, it becomes
> NULL. The same thing I cannot do for logical fields: if I do not set the
> value of a logical field, it becomes an invalid value, denoted with a
> question mark.

That's not "invalid", it *is* "NULL" according to DBF convention.

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


Re: defaultdict's bug or feature?

2009-05-21 Thread Rhodri James

On Thu, 21 May 2009 13:07:50 +0100, Red Forks  wrote:


from collections import defaultdict

d = defaultdict(set)
assert isinstance(d['a'], set)
assert isinstance(d.get('b'), set)

d['a'] is ok, and a new set object is insert to d, but d.get('b') won't.

It's a bug, or just a feature?


Feature.  You're blaming 'get' for doing exactly what it said it would,
both in returning None and not gratuitously altering the dictionary.

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


Re: 4 hundred quadrillonth?

2009-05-21 Thread Carl Banks
On May 21, 3:45 pm, norseman  wrote:
> Beyond that - just fix the money at 2, gas pumps at 3 and the
> sine/cosine at 8 and let it ride. :)


Or just use print.

>>> print 4.0/5.0
0.8

Since interactive prompt is usually used by programmers who are
inspecting values it makes a little more sense to print enough digits
to give unambiguous representation.


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


Re: 4 hundred quadrillonth?

2009-05-21 Thread MRAB

Grant Edwards wrote:

On 2009-05-21, Christian Heimes  wrote:

[email protected] schrieb:

The explaination in my introductory Python book is not very
satisfying, and I am hoping someone can explain the following to me:


4 / 5.0

0.80004

4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
It bothers me.

Welcome to IEEE 754 floating point land! :)


Floating point is sort of like quantum physics: the closer you
look, the messier it gets.


I have the same feeling towards databases.
--
http://mail.python.org/mailman/listinfo/python-list


Re: lxml: traverse xml tree and retrieve element based on an attribute

2009-05-21 Thread byron
On May 21, 6:57 pm, MRAB  wrote:
> byron wrote:
> > I am using the lxml.etree library to validate an xml instance file
> > with a specified schema that contains the data types of each element.
> > This is some of the internals of a function that extracts the
> > elements:
>
> >         schema_doc = etree.parse(schema_fn)
> >         schema = etree.XMLSchema(schema_doc)
>
> >         context = etree.iterparse(xml_fn, events=('start', 'end'),
> > schema=schema)
>
> >         # get root
> >         event, root = context.next()
>
> >         for event, elem in context:
> >             if event == 'end' and elem.tag == self.tag:
> >                 yield elem
> >             root.clear()
>
> > I retrieve a list of elements from this... and do further processing
> > to represent them in different ways. I need to be able to capture the
> > data type from the schema definition for each field in the element.
> > i.e.
>
> >     
> >         
> >             
> >                 
> >                 
> >                 
> >                 
> >                 
> >             
> >         
> >     
>
> > My thought is to recursively traverse through the schema definition
> > match the `name` attribute since they are unique to a `type` and
> > return that element. But I can't seem to make it quite work. All the
> > xml is valid, validation works, etc. This is what I have:
>
> >     def find_node(tree, name):
> >         for c in tree:
> >             if c.attrib.get('name') == name:
> >                 return c
> >             if len(c) > 0:
> >                 return find_node(c, name)
> >     return 0
>
> You're searching the first child and then returning the result, but what
> you're looking for might not be in the first child; if it's not then you
> need to search the next child:
>
>      def find_node(tree, name):
>          for c in tree:
>              if c.attrib.get('name') == name:
>                  return c
>              if len(c) > 0:
>                  r = find_node(c, name)
>                  if r:
>                      return r
>          return None
>
> > I may have been staring at this too long, but when something is
> > returned... it should be returned completely, no? This is what occurs
> > with `return find_node(c, name) if it returns 0. `return c` works
> > (used pdb to verify that), but the recursion continues and ends up
> > returning 0.
>
> > Thoughts and/or a different approach are welcome. Thanks
>
>

Thanks. Yes i tried something like this, but I think I overwrite `c`
when i wrote it, as in:

if len(c) > 0:
c = fin_node(c, name)
if c is not None:
return c

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Gary Herron

MRAB wrote:

Grant Edwards wrote:

On 2009-05-21, Christian Heimes  wrote:

[email protected] schrieb:

The explaination in my introductory Python book is not very
satisfying, and I am hoping someone can explain the following to me:


4 / 5.0

0.80004

4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
It bothers me.

Welcome to IEEE 754 floating point land! :)


Floating point is sort of like quantum physics: the closer you
look, the messier it gets.


+1 as QOTW


And just to add one bit of clarity:  This problem has nothing to do with 
the OP's division of 4 by 5.0, but rather that the value of 0.8 itself 
cannot be represented exactly in IEEE 754.  Just try


>>> print repr(0.8)  # No division needed
'0.80004'

Gary Herron








I have the same feeling towards databases.


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


Re: lxml: traverse xml tree and retrieve element based on an attribute

2009-05-21 Thread MRAB

byron wrote:
[snip]


Thanks. Yes i tried something like this, but I think I overwrite `c`
when i wrote it, as in:

if len(c) > 0:
c = fin_node(c, name)
if c is not None:
return c


FYI, doing that won't actually matter in this case; 'c' will still be
bound to the next value on the next iteration of the loop because it's
just a reference to the iterator and 'assigning' won't affect the
iterator as in soem other languages.
--
http://mail.python.org/mailman/listinfo/python-list


Re: 4 hundred quadrillonth?

2009-05-21 Thread Rob Clewley
On Thu, May 21, 2009 at 8:19 PM, Gary Herron  wrote:
> MRAB wrote:
>>
>> Grant Edwards wrote:
>>>
>>> On 2009-05-21, Christian Heimes  wrote:

 [email protected] schrieb:
>
> The explaination in my introductory Python book is not very
> satisfying, and I am hoping someone can explain the following to me:
>
 4 / 5.0
>
> 0.80004
>
> 4 / 5.0 is 0.8. No more, no less. So what's up with that 4 at the end.
> It bothers me.

 Welcome to IEEE 754 floating point land! :)
>>>

FYI you can explore the various possible IEEE-style implementations
with my python simulator of arbitrary floating or fixed precision
numbers:

http://www2.gsu.edu/~matrhc/binary.html
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ffmpeg and python big problem

2009-05-21 Thread Rhodri James

On Thu, 21 May 2009 22:48:33 +0100, TerabyteST  wrote:


Hello. I am trying to make a video from images shot by my webcam in
python. I use a module I found on the net (here
http://osdir.com/ml/python.matplotlib.general/2005-10/msg00145.html )
but, even if I think I am doing everything correctly, what I only get
is a grey video with some multi-color squares on the top-left bit...


Obvious starter question: do you have ffmpeg properly compiled for
Windows (i.e. using Cygwin or MinGW)?


I
don't think it's a problem with ffmpeg because I tried with two
different versions.


This is not a guarantee.  ffmpeg is wonderful when it works, and a
total pig when it doesn't.  Sometimes it's big and obvious about
not working and seg-faults on you, and sometimes it just does
something completely implausible with the information you give
it.


Not even with the codec: i tried wmv, mpg, avi and
everything, but still the same result.


The output vcodec doesn't matter as much as the input vcodec,
which is mjpeg.  I think webcams are mjpeg devices, but I've
never used one so I'm not sure.


Last thing left,
IMO, is the module, but I can't seem to find whats the problem... Is
it because the module was (maybe) made on Linux and I'm working on
windows? If you would help me I'd be so glad!


I'm not nearly so worried about the module being written on Linux as
ffmpeg.  The module is full of bad practice, but it doesn't do
anything os-specifically bad.

There is one further thing to check: your code that uses the module.
How do you feed data from the webcam to the VidStream object?  If
you're creating intermediate files of webcam stuff, please say
what they are and how they came to be.

I have to admit, if you've got images from your webcam in a file
already, I don't see why you aren't using ffmpeg directly.


This program is for my friend and he needs it ready preety quick.


Aha. Ahahahahahahahaha.

Ahem.

Sorry, but "pretty quick" and "ffmeg" don't go together well in
my experience.

--
Rhodri James *-* Wildebeeste Herder to the Masses
--
http://mail.python.org/mailman/listinfo/python-list


  1   2   >