Re: dbfpy - cannot store new record

2009-05-21 Thread David Lyon
On Fri, 22 May 2009 08:34:17 +0200, Laszlo Nagy 
wrote:
> Now I also tried to set -1. In any of the above cases, if I open that 
> dbf file with a commercial DBF editor application then I see that the 
> value is not null.
> 
> - Borland Database Desktop shows "False" value
> - CDBF shows an invalid value, noted with a question mark (screenshot 
> attached)

dbfpy is very old code.

Try setting up a CHAR(1) field and filling it with "Y" or "N" or
"T" or "F".. indicating yes,no,true or false...

This might be an easier solution...

otherwise... just debug the dbfpy code...


David

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



That's not "invalid", it *is* "NULL" according to DBF convention.
  
Now I also tried to set -1. In any of the above cases, if I open that 
dbf file with a commercial DBF editor application then I see that the 
value is not null.


- Borland Database Desktop shows "False" value
- CDBF shows an invalid value, noted with a question mark (screenshot 
attached)


Best,

  Laszlo



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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Andre Engels
On Thu, May 21, 2009 at 11:05 PM,   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.

Well, how much would 1 / 3.0 be? Maybe 0.33... with a certain
(large) number of threes? And if you multiply that by 3, will it be
1.0 again? No, because you cannot represent 1/3.0 as a precise decimal
fraction.

Internally, what is used are not decimal but binary fractions. And as
a binary fraction, 4/5.0 is just as impossible to represent as 1/3.0
is (1/3.0 = 0.0101010101... and 4/5.0 = 0.110011001100... to be
exact). So 4 / 5.0 gives you the binary fraction of a certain
precision that is closest to 0.8. And apparently that is close to
0.80004


-- 
André Engels, andreeng...@gmail.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: making a python program in windows

2009-05-21 Thread Tim Roberts
Dave Angel  wrote:
>
>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

The better way to do this is to bring up the System control panel applet
(shortcut: WindowsKey + Pause/Break), Advanced, Environment Variables.  In
the System variables, click PATHEXT and Edit, and add ;.PY;.PYW to the end.
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: monitoring friendly applications

2009-05-21 Thread Tim Roberts
Imbaud Pierre  wrote:
>
>I have A LOT of batch applications to monitor, on linux machines, mostly 
>written in python.
>I have to know:
>- which are active, at a given moment?
>- when did the last run occur? How long did it last?
>- for some daemons: are they stuck? generally, waiting for i/o, or lost 
>in some C call.
>...
>By any chance, does something like this exist? Would someone be 
>interested with this development?

http://www.letmegooglethatforyou.com?q=python+daemon+tools
-- 
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Wrapping methods of built-in dict

2009-05-21 Thread George Sakkis
On May 21, 5:55 pm, shailesh  wrote:

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

Sure:
>>> MethodWrapper = type({}.__init__)
>>> isinstance([].__len__, MethodWrapper)
True

But you're better off catching everything by checking with callable()
(or equivalently hasattr(obj, '__call__')).

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

In my experience, it's quite unlikely to really want to decorate
indiscriminately *all* methods of a class/instance, let alone all the
special methods (e.g. __getattribute__ very rarely needs to be
overridden). Do you have an actual use case or are you just playing
around ?

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


Re: defaultdict's bug or feature?

2009-05-21 Thread Rhodri James

Please don't top-post, it makes the thread of argument hard to follow.

On Fri, 22 May 2009 01:44:37 +0100, Red Forks  wrote:

You mean 'get' method should not alter the dict, does 'dict[key]' should  
not

alter the dict either?

d = defaultdict(set)
assert len(d) == 0
print d[1]
assert len(d) == 1

auto insert value to dict, when value is not in dict, is what defaultdict
try to do.


Behaviour you are deliberately avoiding by calling `get`, since that
explicitly behaves the same way that it does for dicts.  You're doing
two different things through two different interfaces with two different
specs.  Why are you expecting them to have the same effect?

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


Re: Adding a Par construct to Python?

2009-05-21 Thread Rhodri James
On Wed, 20 May 2009 09:19:50 +0100,   
wrote:



On 20 May, 03:43, Steven D'Aprano
 wrote:

On Tue, 19 May 2009 03:57:43 -0700, jeremy wrote:
> As I wrote before, concurrency is one of the hardest things for
> professional programmers to grasp. For 'amateur' programmers we need  
to

> make it as simple as possible,


Here, I think, is the fatal flaw in your plan.  As Steven pointed out,
concurrency isn't simple.  All you are actually doing is making it
easier for 'amateur' programmers to write hard-to-debug buggy code,
since you seem to be trying to avoid making them think about how to
write parallelisable code at all.


I *do* actually know a bit about concurrency and would never imply
that *any* for loop could be converted to a parallel one. The
intention of my remark "with my suggestion they could potentially get
a massive speed up just by changing 'for' to 'par' or 'map' to
'pmap'." is that it could be applied in the particular circumstances
where there are no dependencies between different iterations of the
loop.


If you can read this newsgroup for a week and still put your hand on
your heart and say that programmers will check that there are no
dependencies before swapping 'par' for 'for', I want to borrow your
rose-coloured glasses.  That's not to say this isn't the right solution,
but you must be aware that people will screw this up very, very
regularly, and making the syntax easy will only up the frequency of
screw-ups.


This shows why the sync event is needed - to avoid  race conditions on
shared variables. It is borrowed from the BSP paradigm - although that
is a distibuted memory approach. Without the sync clause, rule 5 would
just be the standard way of defining a parallelisable loop.


Pardon my cynicism but sync would appear to have all the disadvantages
of message passing (in terms of deadlock opportunities) with none of
advantages (like, say, actual messages).  The basic single sync you put
forward may be coarse-grained enough to be deadlock-proof, but I would
need to be more convinced of that than I am at the moment before I was
happy.


P.S. I have a couple of additional embellishments to share at this
stage:

[snip]

2. Scope of the 'sync' command. It was pointed out to me by a
colleague that I need to define what happens with sync when there are
nested par loops. I think that it should be defined to apply to the
innermost par construct which encloses the statement.


What I said before about deadlock-proofing?  Forget it.  There's hours
of fun to be had once you introduce scoping, not to mention the fact
that your inner loops can't now be protected against common code in the
outer loop accessing the shared variables.

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


Re: When does the escape character work within raw strings?

2009-05-21 Thread Rhodri James
On Fri, 22 May 2009 03:42:18 +0100, walterbyrd   
wrote:



I guess I am confused about when when escape characters are are
interpersonal as escape characters, and escape characters are not
treated as escape characters.


No, you're confused about the number of entirely different things
that are interpreting a string, and the difference between a string
literal and a string object.


Sometimes escape characters in regular strings are treated as escape
characters, sometimes not. Same seems to go for raw strings. So how do
I know?

IMO: '\' characters in raw strings should not be given any special
meaning. That would also solve the common problem of r'c:\whatever\'
not working in python. But I digress.


Escaping the delimiting quote is the *one* time backslashes have a
special meaning in raw string literals.


To me this does not seem right. r'\n' should not equal '\n'


And it doesn't.  Let me explain.  No, that would take too long,
let me summarise. :-)


s = 'x\nx'


`s` is a string object containing the character 'x', a newline, and 'x'.


a = re.sub('\n', 'x', s)


This calls re.sub with a pattern string object that contains a single
newline character.  Since this character has no special meaning to the
sub function it faithfully searches `s` for newlines, and, finding one,
replaces it with an 'x' character.


a = re.sub(r'\n', 'x', s)


This calls re.sub with a pattern string object that contains two
characters, a backslash followed by an 'n'.  This combination *does*
have a special meaning to the sub function, which does it's own
translation of the pattern into a single newline character.  Then,
as before, it spots the newline in `s` and replaces it with an 'x'.

Note, however, that the string object created by the raw string literal
was *two* characters long.  It's re.sub (in common with the rest of the
re module functions) that chooses to interpret the backslash specially.

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


Re: Cursor movement question

2009-05-21 Thread Jive Dadson

Gosh, you guys are slow. :-) I figured it out.
--
http://mail.python.org/mailman/listinfo/python-list


Re: When does the escape character work within raw strings?

2009-05-21 Thread walterbyrd
I guess I am confused about when when escape characters are are
interpersonal as escape characters, and escape characters are not
treated as escape characters.

Sometimes escape characters in regular strings are treated as escape
characters, sometimes not. Same seems to go for raw strings. So how do
I know?

IMO: '\' characters in raw strings should not be given any special
meaning. That would also solve the common problem of r'c:\whatever\'
not working in python. But I digress.


To me this does not seem right. r'\n' should not equal '\n'

>>> s = 'x\nx'
>>> a = re.sub('\n', 'x', s)
>>> a
'xxx'
>>> a = re.sub(r'\n', 'x', s)
>>> a
'xxx'
>>>

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


Re: finding repeated data sequences in a column

2009-05-21 Thread Rhodri James

On Thu, 21 May 2009 08:55:45 +0100, yadin  wrote:


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.


So if I understand you correctly, you actually want to split your
data up by valve name to find each valve that has listed pressures of
1, 2 and 3 in that order?  That's a lot simpler, though it has to be
said that your data isn't in a terribly convenient format.


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


Ah, so the target "best" pressure sequence doesn't have to be all of the
values listed.  Hmm.  Here goes...

HERE BE CODE

from itertools import izip, groupby

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
TARGET = [1, 2, 3]

target_len = len(TARGET) # Since we're using this a lot
result = []

for valve, p in groupby(izip(VALVES, PRESSURES),
key=lambda x: x[0]):
  pressures = [x[1] for x in p]
  for i in xrange((len(pressures) - target_len) + 1):
if pressures[i:i+target_len] == TARGET:
  result.append(valve)
  break

print "The answer you want is", result

HERE ENDETH THE CODE

Not terribly pretty largely because of having to do sublist
matching, but it should work for most "best pressures".

The unfamiliar looking stuff are functions from the iterator
toolkit that make this a lot simpler.  If you don't get what's
going on here, I don't blame you.  I just deleted my attempt
to explain it because it was confusing me :-)  Reading the
descriptions of izip and groupby in the standard library
documentation should make things clearer.

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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Dave Angel

Rob Clewley wrote:

On Thu, May 21, 2009 at 8:19 PM, Gary Herron  wrote:
  

MRAB wrote:


Grant Edwards wrote:
  

On 2009-05-21, Christian Heimes  wrote:


seanm...@gmail.com 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

  


It was over 40 years ago I studied Fortran, with the McCracken book.  
There were big warnings in it about the hazards of binary floating 
point.  This was long before the IEEE

754, Python, Java, or even C.

In any floating point system with finite precision, there will be some 
numbers that cannot be represented exactly.  Beginning programmers 
assume that if you can write it exactly, the computer should understand 
it exactly as well.  (That's one of the reasons the math package I 
microcoded a few decades ago was base 10).


If you try to write 1/3 in decimal notation, you either have to write 
forever, or truncate it somewhere.  The only fractions that terminate 
are those that have a denominator (in lowest terms) comprised only of 
powers of 2 and 5.  So 4/10 can be represented, and so can 379/625.  Any 
other fraction, like 1/7, or 3/91 will make a repeating decimal, 
sometimes taking many digits to repeat, but not repeating with zeroes.


In binary fractions, the rule is similar, but only for powers of 2.  If 
there's a 5 in there, it cannot be represented exactly.


So people learn to use integers, or rational numbers (fractions), or 
decimal representations, depending on what values they're willing to 
have be approximate.


Something that escapes many people is that even when there's an error 
there, sometimes converting it back to decimal hides the error.  So 0.4 
might have an error on the right end, but 0.7 might happen to look good.


Thanks for providing tools that let people play.

An anecdote from many years ago (1975) -- I had people complain about my 
math package, that cos(pi/2) was not zero.  It was something times 
10**-13, but still not zero.  And they wanted it to be zero.  If 
somebody set the math package to work in degrees, they'd see that 
cos(90) was in fact zero.   Why the discrepancy?  Well,  you can't 
represent pi/2 exactly, (in any floating point or fraction system, it's 
irrational).  So if you had a perfect cos package, but give it a number 
that's off just a little from a right angle, you'd expect the answer to 
be off a little from zero.  Turns out that (in a 13 digit floating point 
package), the value was the next 13 digits of pi/2.  And 12 of them were 
accurate.  I was pleased as punch.



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


Re: 4 hundred quadrillonth?

2009-05-21 Thread AggieDan04
On May 21, 5:36 pm, Chris Rebert  wrote:
> On Thu, May 21, 2009 at 2:53 PM, Carl Banks  wrote:
> > On May 21, 2:05 pm, seanm...@gmail.com 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.
...
>
> 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')

>>> Decimal(1) / Decimal(3) * 3
Decimal("0.")
>>> Decimal(2).sqrt() ** 2
Decimal("1.999")

Decimal isn't a panacea for floating-point rounding errors.  It also
has the disadvantage of being much slower.

It is useful for financial applications, in which an exact value for
0.01 actually means something.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 4 hundred quadrillonth?

2009-05-21 Thread AggieDan04
On May 21, 5:45 pm, norseman  wrote:
> seanm...@gmail.com 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.

If you want to be picky, the exact value is
0.8000444089209850062616169452667236328125 (i.e.,
3602879701896397/2**52).  Python's repr function rounds numbers to 17
significant digits.  This is the minimum that ensures that float(repr
(x)) == x for all x (using IEEE 754 double precision).

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

The error in this example is roughly equivalent to the width of a red
blood cell compared to the distance between Earth and the sun.  There
are very few applications that need more accuracy than that.


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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Gary Herron

R. David Murray wrote:

Gary Herron  wrote:
  

MRAB wrote:


Grant Edwards wrote:
  

On 2009-05-21, Christian Heimes  wrote:


seanm...@gmail.com 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'



Python 3.1b1+ (py3k:72432, May  7 2009, 13:51:24) 
[GCC 4.1.2 (Gentoo 4.1.2)] on linux2

Type "help", "copyright", "credits" or "license" for more information.
  

4 / 5.0


0.8
  

print(repr(0.8))


0.8

In py3k Eric Smith and Mark Dickinson have implemented Gay's floating
point algorithm for Python so that the shortest repr that will round
trip correctly is what is used as the floating point repr

--David
  


Which won't change the fact that 0.8 and lots of other favorite floats 
are still not representable exactly, but it will hide this fact from 
most newbies.  One of the nicer results of this will be that these 
(almost) weekly questions and discussions will be come a thing of the 
past. 


With a sigh of relief,
Gary Herron





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


Re: Is there a better way to chose a slice of a list?

2009-05-21 Thread Rhodri James
On Wed, 20 May 2009 17:08:08 +0100, walterbyrd   
wrote:



I am processing a huge spreadsheet which I have converted to a csv
format. Each row will be a wiki page with several sub-headings. The
spreadsheet contains information about servers. Wiki sub-headings may
include: 'hardware', 'software', 'users', 'network swith settings'.
'Hardware' may include the spreadsheet columns: 'memory', 'cpu', and
so on. So the first six columns in the spreadsheet may go under
'hardware' the next six under 'software' and so on.

I have already created the wiki pages, using a method similar to what
I first posted. But, it seems like there should be a better way to to
do it. So, for future reference, I was just wondering.


Given that you're already making presumptions about the nature of your
data, named constants or enums are the most concise thing to use together
with a quick check of the column header row to make sure that the
constants really do refer to the right columns.

If you want something a little more bullet-proof, create a dictionary
mapping the column headers (as read in) to column numbers and use that
to generate the slice limits.  Since that still relies on the column
headers being what you expect them to be, and at least partially in the
order you expect them to be, it's probably not enough of a win to bother
with.

Trying to slice a list by value is never going to look pretty because
lists aren't designed to be indexed by value.  Worse, if you were
doing this a lot (as you imply) then it's going to be horribly
inefficient, since you're doing an extra two (or more) O(n) searches
for every row.

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


Re: defaultdict's bug or feature?

2009-05-21 Thread MRAB

Red Forks wrote:
You mean 'get' method should not alter the dict, does 'dict[key]' should 
not alter the dict either?


d = defaultdict(set)
assert len(d) == 0
print d[1]
assert len(d) == 1

auto insert value to dict, when value is not in dict, is what 
defaultdict try to do.



That's the behaviour which makes defaultdict so useful. Compare using
defaultdict:

d = defaultdict(int)

and then:

d["foo"] += 1

to using dict:

d = {}

and then:

try:
d["foo"] += 1
except KeyError:
d["foo"] = 1

or:

d["foo"] = d.get("foo", 0) + 1
--
http://mail.python.org/mailman/listinfo/python-list


Re: 4 hundred quadrillonth?

2009-05-21 Thread R. David Murray
Gary Herron  wrote:
> MRAB wrote:
> > Grant Edwards wrote:
> >> On 2009-05-21, Christian Heimes  wrote:
> >>> seanm...@gmail.com 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'

Python 3.1b1+ (py3k:72432, May  7 2009, 13:51:24) 
[GCC 4.1.2 (Gentoo 4.1.2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 4 / 5.0
0.8
>>> print(repr(0.8))
0.8

In py3k Eric Smith and Mark Dickinson have implemented Gay's floating
point algorithm for Python so that the shortest repr that will round
trip correctly is what is used as the floating point repr

--David

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


Re: Streaming pdf with URLLib

2009-05-21 Thread Aahz
In article <04eacd56-5293-4553-bdb3-ad2e8266c...@z7g2000vbh.googlegroups.com>,
Scooter   wrote:
>
>#!/usr/bin/python
>
>import urllib
>
>u = urllib.urlopen('https://myinternal.server/pdfs/pdfstreamer.aspx')
>print 'Content-type: application/pdf\n\n'
># print 'Content-type: application/x-msdownload; name=\"FileName\"\n
>\n'
># print 'Content-Dispostion: attachment; filename=\"FileName\"\r\n\n'
>
>pdfdoc = u.read()
>print pdfdoc
>
>Hitting my pdfstreamer.aspx app directly through a browser renders a
>pdf just fine. So now I'm attempting to use this python app on an
>external webserver, access it as a cgi, to pull the pdf, and then re-
>render it, but at that point it just shows me the raw pdf in
>the browser.

What application type does the browser report?
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"A foolish consistency is the hobgoblin of little minds, adored by little
statesmen and philosophers and divines."  --Ralph Waldo Emerson
-- 
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, 8:27 pm, MRAB  wrote:
> 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.

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


Re: defaultdict's bug or feature?

2009-05-21 Thread Red Forks
You mean 'get' method should not alter the dict, does 'dict[key]' should not
alter the dict either?

d = defaultdict(set)
assert len(d) == 0
print d[1]
assert len(d) == 1

auto insert value to dict, when value is not in dict, is what defaultdict
try to do.

On Fri, May 22, 2009 at 7:46 AM, Rhodri James
wrote:

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


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:

 seanm...@gmail.com 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: 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 Gary Herron

MRAB wrote:

Grant Edwards wrote:

On 2009-05-21, Christian Heimes  wrote:

seanm...@gmail.com 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 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 MRAB

Grant Edwards wrote:

On 2009-05-21, Christian Heimes  wrote:

seanm...@gmail.com 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: 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: 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: 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: finding repeated data sequences in a column

2009-05-21 Thread norseman

yadin wrote:

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

bearophileh...@lycos.com 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: 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: 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: 4 hundred quadrillonth?

2009-05-21 Thread Grant Edwards
On 2009-05-21, Christian Heimes  wrote:
> seanm...@gmail.com 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: 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: 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: 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: 4 hundred quadrillonth?

2009-05-21 Thread norseman

seanm...@gmail.com 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: 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, seanm...@gmail.com 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, seanm...@gmail.com 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: 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: 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: 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: 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: 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: 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: 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: 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: 4 hundred quadrillonth?

2009-05-21 Thread Carl Banks
On May 21, 2:05 pm, seanm...@gmail.com 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


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


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: 4 hundred quadrillonth?

2009-05-21 Thread seanm . py
On May 21, 5:36 pm, Christian Heimes  wrote:
> seanm...@gmail.com 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


Re: 4 hundred quadrillonth?

2009-05-21 Thread Christian Heimes
seanm...@gmail.com 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 MRAB

seanm...@gmail.com 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: Overlapping region resolution

2009-05-21 Thread Scott David Daniels

psaff...@googlemail.com 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
scott.dani...@acm.org
--
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


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


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


Re: python3 module for dbus ?

2009-05-21 Thread Timothy Madden

Aahz wrote:

In article <4a1281ef$0$90271$14726...@news.sunsite.dk>,
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


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


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


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


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


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: 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: 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: Performance java vs. python

2009-05-21 Thread namekuseijin
On May 21, 7:47 am, s...@viridian.paintbox (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: 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: 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: 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: 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: 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: 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: 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 = 'j...@example.com'
> >            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: 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 = 'j...@example.com'
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: 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


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 = 'j...@example.com'
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: Overlapping region resolution

2009-05-21 Thread MRAB

psaff...@googlemail.com 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


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


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


Overlapping region resolution

2009-05-21 Thread psaff...@googlemail.com
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: 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


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


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


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


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


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 - s...@pobox.com - 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: 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


[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


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


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


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


  1   2   >