Re: how to make portable distribution of python 2.6?

2010-08-14 Thread Perica Zivkovic
Hi there,

numpy, matplotlib are already parts of Portable Python, PyQt is coming
in one of the next versions. Creating it is not so difficult, it is
basically repackaging of the python core and the required modules.
Tricky part is keeping it portable as big part of libs is storing
their configuration settings all over the place or require python env.
variables.

Drop me an email an maybe I can help you by including modules you need
in next release of Portable Python. I'm already helping several
universities, maybe we can work together to create one distribution
which targets computer classes (together with tutorials,
documentation, how-to guides etc.)

keep pythoning !

Perica Zivkovic





On Aug 13, 8:23 pm, zaur  wrote:
> On 13 авг, 21:28, Thomas Jollans  wrote:
>
>
>
>
>
> > On 2010-08-13 19:00, zaur wrote:> All greetings!
>
> > > How to make portable distribution of python 2.6?
>
> > I don't know, but what you're looking for probably already exists.
>
> > Do you mean "portable" as in portable, i.e. "take this and build it for
> > your system, it should work if your OS is supported"? Then you can get
> > source tarballs from python.org
>
> >http://python.org/download/
>
> > Or do you understand "portable" the way that is fashionable in the
> > Windows world nowadays for some reason, i.e. "look, Ma, already
> > installed if you happen to use Microsoft Windows of roughly the right
> > version!"
>
> > Then http://www.portablepython.com/ is exactly where Google would have
> > lead you had you searched.
>
> I want to realize howto build my own portable python in order to use
> them without installation.
> I want also to be able install modules (numpy, matplotlib, pyqt,
> etc...) when it is necessary.
> This very usefull for teaching python in computer classes.

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


Re: writing \feff at the begining of a file

2010-08-14 Thread Thomas Jollans
On Saturday 14 August 2010, it occurred to Steven D'Aprano to exclaim:
> On Fri, 13 Aug 2010 18:25:46 -0400, Terry Reedy wrote:
> > A short background to MRAB's answer which I will try to get right.
> > 
> > The byte-order-mark was invented for UTF-16 encodings so the reader
> > could determine whether the pairs of bytes are in little or big endiean
> > order, depending on whether the first two bute are fe and ff or ff and
> > fe (or maybe vice versa, does not matter here). The concept is
> > meaningless for utf-8 which consists only of bytes in a defined order.
> > This is part of the Unicode standard.
> > 
> > However, Microsoft (or whoever) re-purposed (hijacked) that pair of
> > bytes to serve as a non-standard indicator of utf-8 versus any
> > non-unicode encoding. The result is a corrupted utf-8 stream that python
> > accommodates with the utf-8-sig(nature) codec (versus the standard utf-8
> > codec).
> 
> Is there a standard way to autodetect the encoding of a text file? I do
> this:

No, there is no way to autodetect the encoding of a text file. 

> Open the file in binary mode; if the first three bytes are
> codecs.BOM_UTF8, then it's a Microsoft UTF-8 text file; otherwise if the
> first two byes are codecs.BOM_BE or codecs.BOM_LE, the encoding is utf-16-
> be or utf-16-le respectively.

Unless the file happens to be UCS-2/UTF-16, or it happens to be a UTF-8 with 
garbage at the top.

> If there's no BOM, then re-open the file and read the first two lines. If
> either of them match this regex 'coding[=:]\s*([-\w.]+)' then I take the
> encoding name from that. This matches Python's behaviour, and supports
> EMACS and vi encoding declarations.

This is a completely different method, and probably the most common in real 
usage:
 1. Assume the file is ASCII (or some similar code page), but be liberal about
characters you don't recognize
 2. Know the file format you're reading.
 3. Switch encoding once you have reached an indication of which exact
character set to use.
  For Python, use the coding cookie if it's there
  For XML, read the  declaration.
  For HTML, look for a  tag, or just
  guess

If no encoding is specified in a way you recognize, then you're out of luck. 
You'd usually just guess. (better still, you'd know what encoding you're 
dealing with in the first place, but that's too much to ask, I suppose...)
You can try to take an educated guess by cross-referencing character 
frequencies with tables for known encoding/language combinations. I think this 
is what Microsoft IE does when it encounters a web page of unspecified 
encoding.

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


Re: writing \feff at the begining of a file

2010-08-14 Thread Martin v. Loewis
> Is there a standard way to autodetect the encoding of a text file? 

Use the chardet module:

http://chardet.feedparser.org/

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


Re: looping through possible combinations of McNuggets packs of 6,9 and 20

2010-08-14 Thread gslindstrom
On Aug 12, 4:33 am, Paul Rubin  wrote:
> Baba  writes:
> > exercise: given that packs of McNuggets can only be bought in 6, 9 or
> > 20 packs, write an exhaustive search to find the largest number of
> > McNuggets that cannot be bought in exact quantity.
>
> Is that a homework problem?  Hint: first convince yourself that a
> largest number actually exists.

If I recall, this was a "puzzler" on the NPR radio show "Car Talk".
Still might be homework, though.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-14 Thread Baba
On Aug 13, 8:25 pm, Ian Kelly  wrote:

> It's not.  You're not just trying to find the sixth value that can be
> bought in exact quantity, but a sequence of six values that can all be
> bought in exact quantity.  The integers [6, 9, 12, 15, 18, 20] are not
> sequential.

Hi Ian,

Thanks for stating the obvious. I obviously hadn't understood a
fundamental part of the theorem which states that 6 SEQUENTIAL passes
must be found! That's a good lesson learned and will help me in future
exercises to make sure i understand the theory first. Thanks again!

Ok so with your and News123's help (no offence to all others but i
need to keep it simple at this stage)i was able to find the solution:
43

my code is probably not elegant but a huge step forward from where i
started:

def can_buy(n_nuggets):
   for a in range (0,n_nuggets):
   for b in range (0,n_nuggets):
   for c in range (0,n_nuggets):
   #print "trying for %d: %d %d %d" % (n_nuggets,a,b,c)
   if 6*a+9*b+20*c==n_nuggets:
   return [a,b,c]
   return []

for n_nuggets in range(50):
result1 = can_buy(n_nuggets)
result2 = can_buy(n_nuggets+1)
result3 = can_buy(n_nuggets+2)
result4 = can_buy(n_nuggets+3)
result5 = can_buy(n_nuggets+4)
result6 = can_buy(n_nuggets+5)
if result1!=[] and result2!=[] and result3!=[] and result4!=[] and
result5!=[] and result6!=[]:
 if (n_nuggets+5)-n_nuggets==5:
print n_nuggets-1
break

i suppose this can be tweaked to make it shorter? For instance i
wonder if i can do the same with less variable to be defined?

tnx
Baba

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


print v. print()

2010-08-14 Thread Frederick Williams
I am learning Python from Hammond & Robinson's _Python Programming on
Win32_, January 2000 edition.  This

   print "Sleeping for 10 seconds"

which appears in some example code, fails to... um... Compile? 
Interpret?  Well, whatever the word is, it fails.  Trial and error
revealed that

   print("Sleeping for 10 seconds")

does the trick.  I am using version 3.1.2, presumably the book's authors
used some earlier version.  

So why the change from print to print()?

I should warn you that I know nothing about computers in general or
Python in particular.

-- 
I can't go on, I'll go on.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why is python not written in C++ ?

2010-08-14 Thread Aahz
In article ,
Grant Edwards   wrote:
>
>I also looked at Modula-3 once, and thought it had some real promise,
>but I think it's probably deader than Ada now.

That's because you should be using Oberon instead.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"...if I were on life-support, I'd rather have it run by a Gameboy than a
Windows box."  --Cliff Wells
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: print v. print()

2010-08-14 Thread Thomas Jollans
On Saturday 14 August 2010, it occurred to Frederick Williams to exclaim:
> I am learning Python from Hammond & Robinson's _Python Programming on
> Win32_, January 2000 edition.  This
> 
>print "Sleeping for 10 seconds"
> 
> which appears in some example code, fails to... um... Compile?
> Interpret?  Well, whatever the word is, it fails.  Trial and error
> revealed that
> 
>print("Sleeping for 10 seconds")
> 
> does the trick.  I am using version 3.1.2, presumably the book's authors
> used some earlier version.

Yes, indeed. Python 3.0 changed a number of things, the most visible is 
removing the print statement in favour of the print() function.
 
> So why the change from print to print()?

There's no reason for print to be a statement -- it can just as well be a 
function, which makes the language more regular, and therefore quite possibly 
easier to learn.

> 
> I should warn you that I know nothing about computers in general or
> Python in particular.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: print v. print()

2010-08-14 Thread Mel
Thomas Jollans wrote:
> On Saturday 14 August 2010, it occurred to Frederick 
Williams to exclaim:

>> So why the change from print to print()?

> There's no reason for print to be a statement -- it can 
just as well be a
> function, which makes the language more regular, and 
therefore quite
> possibly easier to learn.

The downside to a print() function is that assigning to 
`print` can mask the function, and leave a neophyte without 
any way to get output out of the program.

The problem with that downside, I guess, is that rogue 
assignment to sys.stdout can kill a print statement just as 
dead as a print() function, so the statement's so-called 
advantage is not that great.

Mel.

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


Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-14 Thread Mel
Baba wrote:

> def can_buy(n_nuggets):
>for a in range (0,n_nuggets):
>for b in range (0,n_nuggets):
>for c in range (0,n_nuggets):
>#print "trying for %d: %d %d %d" % (n_nuggets,a,b,c)
>if 6*a+9*b+20*c==n_nuggets:
>return [a,b,c]
>return []
>
> for n_nuggets in range(50):
> result1 = can_buy(n_nuggets)
> result2 = can_buy(n_nuggets+1)
> result3 = can_buy(n_nuggets+2)
> result4 = can_buy(n_nuggets+3)
> result5 = can_buy(n_nuggets+4)
> result6 = can_buy(n_nuggets+5)
> if result1!=[] and result2!=[] and result3!=[] and result4!=[] and
> result5!=[] and result6!=[]:
>  if (n_nuggets+5)-n_nuggets==5:
> print n_nuggets-1
> break
>
> i suppose this can be tweaked to make it shorter? For instance i
> wonder if i can do the same with less variable to be defined?

That can_buy function is a computational heavyweight -- very
repetitive when called inside a loop. It could be cheaper to compute a
list of quantities that can be purchased, then check to see what's in
the list -- or the set, if you optimize a bit more.

Mel.

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


Re: python ide for ubuntu

2010-08-14 Thread Juan Andres Knebel
Hi Bhanu,
 if you want to use QT try eric4 for python2 or eric5 for python3. Is very
nice IDE, but if you want to develop only pure python use kate or similar or
eclipse if you need a nice way to see the debug processes

On Thu, Aug 12, 2010 at 7:44 AM, Roald de Vries  wrote:

> Hi Bhanu,
>
>
> On Aug 12, 2010, at 4:15 AM, Bhanu Kumar wrote:
>
>> Hi All,
>>
>> Is there any good free python IDE available in Ubuntu?
>>
>
> See a similar discussion at django-users mailing list:
>
> http://groups.google.com/group/django-users/browse_thread/thread/562189578285211
>
> Cheers, Roald
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Juan Andres Knebel
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-14 Thread member thudfoo
On 8/14/10, Baba  wrote:
> On Aug 13, 8:25 pm, Ian Kelly  wrote:
>
>> It's not.  You're not just trying to find the sixth value that can be
>> bought in exact quantity, but a sequence of six values that can all be
>> bought in exact quantity.  The integers [6, 9, 12, 15, 18, 20] are not
>> sequential.
>
> Hi Ian,
>
> Thanks for stating the obvious. I obviously hadn't understood a
> fundamental part of the theorem which states that 6 SEQUENTIAL passes
> must be found! That's a good lesson learned and will help me in future
> exercises to make sure i understand the theory first. Thanks again!
>
> Ok so with your and News123's help (no offence to all others but i
> need to keep it simple at this stage)i was able to find the solution:
> 43
>
> my code is probably not elegant but a huge step forward from where i
> started:
>
> def can_buy(n_nuggets):
>for a in range (0,n_nuggets):
>for b in range (0,n_nuggets):
>for c in range (0,n_nuggets):
>#print "trying for %d: %d %d %d" % (n_nuggets,a,b,c)
>if 6*a+9*b+20*c==n_nuggets:
>return [a,b,c]
>return []
>
> for n_nuggets in range(50):
> result1 = can_buy(n_nuggets)
> result2 = can_buy(n_nuggets+1)
> result3 = can_buy(n_nuggets+2)
> result4 = can_buy(n_nuggets+3)
> result5 = can_buy(n_nuggets+4)
> result6 = can_buy(n_nuggets+5)
> if result1!=[] and result2!=[] and result3!=[] and result4!=[] and
> result5!=[] and result6!=[]:
>  if (n_nuggets+5)-n_nuggets==5:
> print n_nuggets-1
> break
>
> i suppose this can be tweaked to make it shorter? For instance i
> wonder if i can do the same with less variable to be defined?
>
> tnx
> Baba
>

One tweak:

def can_buy(n_nuggets):
  for a in range (0,n_nuggets):
  for b in range (0,n_nuggets):
  for c in range (0,n_nuggets):
  #print "trying for %d: %d %d %d" % (n_nuggets,a,b,c)
  if 6*a+9*b+20*c==n_nuggets:
  return [a,b,c]
  return []

for n_nuggets in range(50):
if (can_buy(n_nuggets)
and can_buy(n_nuggets+1)
and can_buy(n_nuggets+2)
and can_buy(n_nuggets+3)
and can_buy(n_nuggets+4)
and can_buy(n_nuggets+5)):
   print n_nuggets - 1
   break
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-14 Thread Ian Kelly
On Sat, Aug 14, 2010 at 8:52 AM, Baba  wrote:
> my code is probably not elegant but a huge step forward from where i
> started:
>
> def can_buy(n_nuggets):
>   for a in range (0,n_nuggets):
>       for b in range (0,n_nuggets):
>           for c in range (0,n_nuggets):
>               #print "trying for %d: %d %d %d" % (n_nuggets,a,b,c)
>               if 6*a+9*b+20*c==n_nuggets:
>                   return [a,b,c]
>   return []
>
> for n_nuggets in range(50):
>    result1 = can_buy(n_nuggets)
>    result2 = can_buy(n_nuggets+1)
>    result3 = can_buy(n_nuggets+2)
>    result4 = can_buy(n_nuggets+3)
>    result5 = can_buy(n_nuggets+4)
>    result6 = can_buy(n_nuggets+5)
>    if result1!=[] and result2!=[] and result3!=[] and result4!=[] and
> result5!=[] and result6!=[]:
>     if (n_nuggets+5)-n_nuggets==5:
>        print n_nuggets-1
>        break
>
> i suppose this can be tweaked to make it shorter? For instance i
> wonder if i can do the same with less variable to be defined?

Instead of calling can_buy() 6 times on every iteration of the main
loop, I would suggest maintaining a list of the sequential results.
Just call it once on each number of nuggets in order.  If the number
of nuggets is purchasable, and the list is empty or the last item in
the list is the number of nuggets - 1, then append the number of
nuggets to the list.  If the last item in the list is not the number
of nuggets - 1, then they're not sequential and you start a new list.
When the length of the list reaches 6, you're done, and the answer is
equal to the first item in the list - 1.

You can also improve the can_buy() function by tightening up the loop
limits.  You don't need to go all the way up to n_nuggets on each
loop.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-14 Thread MRAB

Baba wrote:

On Aug 13, 8:25 pm, Ian Kelly  wrote:


It's not.  You're not just trying to find the sixth value that can be
bought in exact quantity, but a sequence of six values that can all be
bought in exact quantity.  The integers [6, 9, 12, 15, 18, 20] are not
sequential.


Hi Ian,

Thanks for stating the obvious. I obviously hadn't understood a
fundamental part of the theorem which states that 6 SEQUENTIAL passes
must be found! That's a good lesson learned and will help me in future
exercises to make sure i understand the theory first. Thanks again!

Ok so with your and News123's help (no offence to all others but i
need to keep it simple at this stage)i was able to find the solution:
43

my code is probably not elegant but a huge step forward from where i
started:

def can_buy(n_nuggets):
   for a in range (0,n_nuggets):
   for b in range (0,n_nuggets):
   for c in range (0,n_nuggets):
   #print "trying for %d: %d %d %d" % (n_nuggets,a,b,c)
   if 6*a+9*b+20*c==n_nuggets:
   return [a,b,c]
   return []

for n_nuggets in range(50):
result1 = can_buy(n_nuggets)
result2 = can_buy(n_nuggets+1)
result3 = can_buy(n_nuggets+2)
result4 = can_buy(n_nuggets+3)
result5 = can_buy(n_nuggets+4)
result6 = can_buy(n_nuggets+5)
if result1!=[] and result2!=[] and result3!=[] and result4!=[] and
result5!=[] and result6!=[]:
 if (n_nuggets+5)-n_nuggets==5:
print n_nuggets-1
break

i suppose this can be tweaked to make it shorter? For instance i
wonder if i can do the same with less variable to be defined?


Increase the number of nuggets one by one and keep a count of the number
of consecutive successes.

If you can buy a number of nuggets exactly, increment the count,
otherwise reset the count.

When the count reaches 6 you know that you're at the end of a sequence
of consecutive successes.
--
http://mail.python.org/mailman/listinfo/python-list


Re: python ide for ubuntu

2010-08-14 Thread Bhanu Kumar
Thanks!!


On Sat, Aug 14, 2010 at 9:49 PM, Juan Andres Knebel wrote:

> Hi Bhanu,
>  if you want to use QT try eric4 for python2 or eric5 for python3. Is very
> nice IDE, but if you want to develop only pure python use kate or similar or
> eclipse if you need a nice way to see the debug processes
>
>
> On Thu, Aug 12, 2010 at 7:44 AM, Roald de Vries wrote:
>
>> Hi Bhanu,
>>
>>
>> On Aug 12, 2010, at 4:15 AM, Bhanu Kumar wrote:
>>
>>> Hi All,
>>>
>>> Is there any good free python IDE available in Ubuntu?
>>>
>>
>> See a similar discussion at django-users mailing list:
>>
>> http://groups.google.com/group/django-users/browse_thread/thread/562189578285211
>>
>> Cheers, Roald
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
>
> --
> Juan Andres Knebel
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: minidom help -- line number

2010-08-14 Thread Thomas Jollans
On Saturday 14 August 2010, it occurred to GZ to exclaim:
> Hi All,
> 
> I am writing a little program that reads the minidom tree built from
> an xml file. I would like to print out the line number of the xml file
> on the parts of the tree that are not valid. But I do not seem to find
> a way to correspond minidom nodes to line numbers.

The DOM does not contain things like line number information. You work with 
the structure of the document, not the appearance of the file you happen to be 
using as a source. You can't use line numbers with minidom.

For stream-based parsers like SAX and eXpat (both in the standard library) 
this makes more sense, and they both allow you to check the current line 
number in one way or another.


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


Re: EXOR or symmetric difference for the Counter class

2010-08-14 Thread Raymond Hettinger
On Aug 12, 1:20 pm, Paddy  wrote:
> I find myself needing to calculate the difference between two Counters
> or multisets or bags.
>
> I want those items that are unique to each bag.

Tell us about your use cases.  I'm curious how a program would ascribe
semantic meaning to the result.  The phrase "unique to each bag"
doesn't quite cover it, perhaps something like "number in either
source above the minimum held in common".

AFAICT, I've never needed something like this as a primitive.  Even
the xor operation for regular sets is rarely used.


> I know how to
> calculate it:
>
>     >>> b = Counter(a=1, b=2)
>     >>> c = Counter(a=3, b=1)
>     >>> diff = (b - c) + (c - b)
> >>> diff
> Counter({'a': 2, 'b': 1})

That seems simple enough.
You could also use:

 diff = (b | c) - (b & c)   # max(b,c) - min(b,c)


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


Re: looping through possible combinations of McNuggets packs of 6, 9 and 20

2010-08-14 Thread John Posner

On 8/14/2010 10:52 AM, Baba wrote:


for n_nuggets in range(50):
 result1 = can_buy(n_nuggets)
 result2 = can_buy(n_nuggets+1)
 result3 = can_buy(n_nuggets+2)
 result4 = can_buy(n_nuggets+3)
 result5 = can_buy(n_nuggets+4)
 result6 = can_buy(n_nuggets+5)
 if result1!=[] and result2!=[] and result3!=[] and result4!=[] and
result5!=[] and result6!=[]:
  if (n_nuggets+5)-n_nuggets==5:
 print n_nuggets-1
 break

i suppose this can be tweaked to make it shorter? For instance i
wonder if i can do the same with less variable to be defined?


[Other responders have covered a lots of the points I make below. I 
guess I need to be quicker!]


First, congratulations on getting to a solution! Code can very often be 
made shorter, although it's not always worth your while to do so. And 
often, shorter code is less understandable code -- which makes a big 
difference if you need to revisit the code later on.


Here are some things that can be done with your for-loop:

1. You don't need this "if" test, because it's always true:

  if (n_nuggets+5)-n_nuggets==5:

2. Whenever you find yourself inventing a series of variables like 
"result1", "result2", "result3", etc., think about using a list instead.


  results = []
  for i in range(6):
  results.append(can_buy(n_nuggets + i))

And to get really fancy, you can use a single "list comprehension" 
statement to do it all


 results = [can_buy(n_nuggets + i) for i in range(6)]

3. Your "if" statement tests whether all the lists are non-empty. In 
Python, expressions (a) and (b) are equivalent:


(a) if result[0] != []
(b) if result[0]

So your "if" test can be expressed as:

if (result[0] and result[1] and result[2] and result[3]
 and result[4] and result[5])

(The parentheses are not required by "if", but they *do* enable you
to split the expression across two or more lines.) And you can use the 
"all" function to rescue this cumbersome statement;


if all(results)

After all this work, the code is getting pretty short:

for n_nuggets in range(50):
results = [can_buy(n_nuggets + i) for i in range(6)]
if all(results):
print n_nuggets-1
break

4. The variable "results" is defined in one statement, and then is used 
just once, in the very next statement. There's no harm in that, and I 
think it makes the mode easier to understand, but you can get rid of it:


for n_nuggets in range(50):
if all([can_buy(n_nuggets + i) for i in range(6)]):
print n_nuggets-1
break

But wait, there's more ... :-)  So far, we've just refined the 
*implementation* of your algorithm. But the algorithm itself could use 
some work.


* When n_nuggets==0, we compute can_buy(0), can_buy(1), can_buy(2), 
can_buy(3), can_buy(4), and can_buy(5).


* When n_nuggets==1, we compute can_buy(1), can_buy(2), can_buy(3), 
can_buy(4), can_buy(5), and can_buy(6).


... and so on. We can use an algorithm in which can_buy(i) is computed 
just once for each value of i:


can_buy_count = 0
n_nuggets = 0
while n_nuggets < 50:
if can_buy(n_nuggets):
can_buy_count += 1
else:
can_buy_count = 0

if can_buy_count == 6:
print n_nuggets - 6
break
else:
n_nuggets += 1

And here's how you could shorten *this* code:

### cbc = "can buy count"
cbc = 0
n_nuggets = -1
while n_nuggets < 50:
n_nuggets += 1
cbc = cbc+1 if can_buy(n_nuggets) else 0
if cbc == 6:
print n_nuggets - 6
break

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


Re: minidom help -- line number

2010-08-14 Thread GZ
On Aug 14, 12:07 pm, Thomas Jollans  wrote:
> On Saturday 14 August 2010, it occurred to GZ to exclaim:
>
> > Hi All,
>
> > I am writing a little program that reads the minidom tree built from
> > an xml file. I would like to print out the line number of the xml file
> > on the parts of the tree that are not valid. But I do not seem to find
> > a way to correspond minidom nodes to line numbers.
>
> The DOM does not contain things like line number information. You work with
> the structure of the document, not the appearance of the file you happen to be
> using as a source. You can't use line numbers with minidom.
>
> For stream-based parsers like SAX and eXpat (both in the standard library)
> this makes more sense, and they both allow you to check the current line
> number in one way or another.

So I am basically out of luck if I want to tie back to the original
file for file error reporting etc. sounds like a deficiency to me.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: minidom help -- line number

2010-08-14 Thread Thomas Jollans
On Saturday 14 August 2010, it occurred to GZ to exclaim:
> On Aug 14, 12:07 pm, Thomas Jollans  wrote:
> > On Saturday 14 August 2010, it occurred to GZ to exclaim:
> > > Hi All,
> > > 
> > > I am writing a little program that reads the minidom tree built from
> > > an xml file. I would like to print out the line number of the xml file
> > > on the parts of the tree that are not valid. But I do not seem to find
> > > a way to correspond minidom nodes to line numbers.
> > 
> > The DOM does not contain things like line number information. You work
> > with the structure of the document, not the appearance of the file you
> > happen to be using as a source. You can't use line numbers with minidom.
> > 
> > For stream-based parsers like SAX and eXpat (both in the standard
> > library) this makes more sense, and they both allow you to check the
> > current line number in one way or another.
> 
> So I am basically out of luck if I want to tie back to the original
> file for file error reporting etc. sounds like a deficiency to me.

The DOM is disjunct from the original file, stream, string, etc. That's in the 
nature of the DOM, and it's probably true for most, if not all, DOM 
implementations, in other programming languages as well as Python.
If you want to stay close to the file, you're better of with SAX. (or a 
similar approach, like eXpat)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: minidom help -- line number

2010-08-14 Thread Ian Kelly
On Sat, Aug 14, 2010 at 11:56 AM, Thomas Jollans  wrote:
> The DOM is disjunct from the original file, stream, string, etc. That's in the
> nature of the DOM, and it's probably true for most, if not all, DOM
> implementations, in other programming languages as well as Python.
> If you want to stay close to the file, you're better of with SAX. (or a
> similar approach, like eXpat)

The minidom parse function can also accept any SAX2 parser, so it
should be possible to create a customized parser that stashes the line
numbers somewhere and use that together with minidom.  I've never
attempted this myself, so I won't be able to help with details.

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


problem in using linalg solver in numpy

2010-08-14 Thread Pramod
Hi Friends


When run the below  program in python i got error like this ,


Matrix
[[ 8 -6  2]
 [-4 11 -7]
 [ 4 -7  6]]
row vecotr X
[[ 28 -40  33]]
Traceback (most recent call last):
  File "solve.py", line 16, in 
print A*B
  File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
line 290, in __mul__
return N.dot(self, asmatrix(other))
ValueError: objects are not aligned




#!/usr/bin/python
N=3
from numpy import linalg
from numpy import matrix


fr=open('mat.txt','r')

A=matrix([[int(fr.readline()) for j in range(N)]for i in range(N)])
B=matrix([[int(fr.readline())for j in range(N)]])

print 'Matrix \n',A
print 'row vecotr X\n',B
#A=matrix([[3,4],[5,2]])
#B=matrix([[11],[9]])
print A*B

#y=linalg.solve(A,B)
#print 'Solution vectoris \n',y


The input file is
8
-6
2
-4
11
-7
4
-7
6
28
-40
33

please try to fix the error in the program


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


Chrome ore Sell Pakistani 30% - 52%,

2010-08-14 Thread iqbal iqbal
Chrome ore Sell Pakistani 30% - 52%,
http://buy-sell-pakistani-minerals.blogspot.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Tk window and contents will not display

2010-08-14 Thread Chris Hare
The scenario is this:

I want to loop around all of the images in a given directory (which I know will 
be images, but I guess I should check), show an image in a window, wait 2 
seconds and show the next one and repeat that indefinitley, which will be until 
the user closes the window.

This is the code I extracted from the larger program and made work - sort of - 
in a standalone fashion.  When I run the code, each of the file names gets 
displayed, and I can view the images, so it has to be something I am doing 
wrong with this chunk of code.  

However, I don't see what the problem is.

from Tkinter import *
import time
import os
import ImageTk
import Image

class externalLoopDisplay:

def show(self):
#
# Create a frame
#
self.window = Tk()
self.f = Frame(self.window, bg="Gray")
self.f.grid()
self.btnRefresh = Button(self.f, text="Close", 
command=self.window.destroy, bg="Gray",highlightbackground="Red", 
highlightcolor="Green")
self.btnRefresh.grid(row=0, column=2)
self.loopImage()

def loopImage(self):
dir =  "Radar/net17"
while 1:
fileList = os.listdir(dir)
number = len(fileList)
c = 1
for gifFile in fileList:
print "externalLoopDisplay.show:","top of for loop " + str(c) + 
" of " + str(number)
print "externalLoopDisplay.show:","showing file "  + dir + "/" 
+ gifFile
self.window.title("Image " + str(c) + " of " + str(number))
image = Image.open(dir + "/" + gifFile)
canvasWidth, canvasHeight = image.size
self.w = Canvas(self.f, width=canvasWidth, height=canvasHeight)
photo = ImageTk.PhotoImage(image=image)
netRadarImage = Label(self.w, image=photo)
netRadarImage.image = photo
self.w.grid(row=1, column=0, columnspan=3)
netRadarImage.grid( row=1, column=0)
time.sleep(10)
c = c + 1
self.w.destroy()

loop=externalLoopDisplay()
loop.show()

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


Re: problem in using linalg solver in numpy

2010-08-14 Thread Peter Otten
Pramod wrote:

> When run the below  program in python i got error like this ,
> 
> 
> Matrix
> [[ 8 -6  2]
>  [-4 11 -7]
>  [ 4 -7  6]]
> row vecotr X
> [[ 28 -40  33]]
> Traceback (most recent call last):
>   File "solve.py", line 16, in 
> print A*B
>   File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
> line 290, in __mul__
> return N.dot(self, asmatrix(other))
> ValueError: objects are not aligned

> A=matrix([[int(fr.readline()) for j in range(N)]for i in range(N)])
> B=matrix([[int(fr.readline())for j in range(N)]])

> print A*B

Row or column; I can't remember which is which either. Try again with

B = matrix([[int(fr.readline())] for j in range(N)]

or

A*B.transpose()

Peter

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


Re: problem in using linalg solver in numpy

2010-08-14 Thread MRAB

Pramod wrote:

Hi Friends


When run the below  program in python i got error like this ,


Matrix
[[ 8 -6  2]
 [-4 11 -7]
 [ 4 -7  6]]
row vecotr X
[[ 28 -40  33]]
Traceback (most recent call last):
  File "solve.py", line 16, in 
print A*B
  File "/usr/lib/python2.6/dist-packages/numpy/core/defmatrix.py",
line 290, in __mul__
return N.dot(self, asmatrix(other))
ValueError: objects are not aligned


[snip]
If you're trying to multiply element-wise use the 'multiply' function.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python Sandbox

2010-08-14 Thread Stephen Hansen
On 8/13/10 8:04 PM, Steven D'Aprano wrote:
> On Fri, 13 Aug 2010 16:37:40 -0700, Stephen Hansen wrote:
> 
>> Howdy-ho.
>>
>> So, I'm working on a project which embeds Python into a bigger system to
>> provide extensibility. In this project, there's basically two types of
>> people who will be entering python code.
>>
>> The trusted folks, who write code which are in files, and which can do
>> anything.
>>
>> The untrusted folks, who are writing very simple chunks of code which
>> can only do limited things.
> 
> I suggest that if the untrusted code is only supposed to be simple and 
> limited, you would be best off to write your own "mini-language" using 
> Python syntax. 

I considered it and rejected it. The return from the effort required
doesn't even vaguely come close to making it worth it. My worst case
fall-back plan is to embed /another/ language (be it Lua or JavaScript
through V8) and offer it a very limited environment. But I don't want to
do that (and considering I solved the while True: pass problem last
night, I'm pretty sure I won't decide to).

> The fact is that Python is not designed to be used by untrusted users, 
> and it is REALLY hard to keep it in a sandbox. There was an attempt to 
> sandbox Python, if I recall correctly it was the bastion module, but it 
> turned out to be so leaky that it was removed from the standard library 
> with extreme prejudice. Since then, others have been working on it, 
> including Google, but I don't know how successful they've been.

I know all this -- but its not relevant really, I think. I'm not trying
to create a safe yet relatively complete or functional Python. All those
efforts to sandbox Python fail because of the incredible dynamic nature
of the language has lots of enticing little holes in it. But I'm not
interested in a full or even vaguely full subset of Python, and I'm not
requiring that this security be done on the code-level.

For example, when you go to save your bit of code, it will go in and if
it finds __ anywhere in the text it just replaces it with xx. And, since
getattr is not available, '_' + '_' won't get you anywhere.

> Here's an example... suppose you wish to allow reading files, but not 
> writing them. Sounds simple?
> 
> http://tav.espians.com/a-challenge-to-break-python-security.html

Yeah, I'm aware of this little challenge-- but every one of those
exploits calls for a special attribute call or method creation which is
impossible(I think) in my setup.

Although Paul Cannon's little exploit is very interesting, and I'm going
to go brute force murder try/except in a similar way to __ above (in
this context, exceptions aren't important) now.

> Now, I'm not suggesting that the exploits there are directly applicable 
> to your sandbox, but they give a small idea of the sorts of things you 
> need to consider.

I'm creating a much, much more restrictive subset of Python then most
sandboxes try to do-- I could make my own custom mini-language, except
good lord, that's a whole lot of work since there are real needs for
*some* programming power here. Or I could embed another language in a
more restrictive way then Python is embedded-- but good lord, now I have
to handle three languages to get things done :)

I just need a certain limited context where someone can be handed
certain Python objects and manipulate them. I'd like people to be able
to use some fundamental Python power -- the rich, beautiful data types
for example (notably in this case, strings), list comprehensions and
stuff, to do what they need to do. Python's very easy, I'd like them to
be able to use that easy.

But I don't need anywhere near full Python power, so sweeping rules
like, 'no, you can't even type __' or, 'sorry, no exception handling for
you', work well.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Tk window and contents will not display

2010-08-14 Thread Peter Otten
Chris Hare wrote:

> The scenario is this:
> 
> I want to loop around all of the images in a given directory (which I know
> will be images, but I guess I should check), show an image in a window,
> wait 2 seconds and show the next one and repeat that indefinitley, which
> will be until the user closes the window.
> 
> This is the code I extracted from the larger program and made work - sort
> of - in a standalone fashion.  When I run the code, each of the file names
> gets displayed, and I can view the images, so it has to be something I am
> doing wrong with this chunk of code.
> 
> However, I don't see what the problem is.

I have not looked at your code in detail, but event loops and time.sleep() 
don't play together very well. Use after(delay_in_milliseconds, callable) 
instead. 

Here's a simple example that loops over images passed from the command line:

import Image
import ImageTk
import os
import sys
import Tkinter as tk

from itertools import cycle

def next_image():
imagefile = next(imagefiles)
image = Image.open(imagefile)

w, h = image.size
image = image.resize((700, 700*h//w))

label.image = label["image"] = ImageTk.PhotoImage(image=image)
root.title("Now showing %s" % os.path.basename(imagefile))

root.after(2000, next_image)

if __name__ == "__main__":
imagefiles = sys.argv[1:]
assert imagefiles
imagefiles = cycle(imagefiles)

root = tk.Tk()
label = tk.Label(root)
label.pack()

root.after_idle(next_image)
root.mainloop()





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


Re: Simple Python Sandbox

2010-08-14 Thread Cameron Simpson
On 14Aug2010 12:56, Stephen Hansen  wrote:
| On 8/13/10 8:04 PM, Steven D'Aprano wrote:
| > On Fri, 13 Aug 2010 16:37:40 -0700, Stephen Hansen wrote:
| >> So, I'm working on a project which embeds Python into a bigger system to
| >> provide extensibility. In this project, there's basically two types of
| >> people who will be entering python code.
| >>
| >> The trusted folks, who write code which are in files, and which can do
| >> anything.
| >>
| >> The untrusted folks, who are writing very simple chunks of code which
| >> can only do limited things.
| > 
| > I suggest that if the untrusted code is only supposed to be simple and 
| > limited, you would be best off to write your own "mini-language" using 
| > Python syntax. 
| 
| I considered it and rejected it. The return from the effort required
| doesn't even vaguely come close to making it worth it.

Ok, what about this: run the untrusted code in a separate process,
if necessary running as a user with different privileges.
Provide objects that need to be shared as some sort of proxy.
Then your untrusted users can do whatever they like in python because
they won't be presented with the inner parts of the privileged stuff.

This is all rather vague because I don't know exactly what your
untrusted users need to be able to do, nor how.

Cheers,
-- 
Cameron Simpson  DoD#743
http://www.cskk.ezoshosting.com/cs/

Computers in the future may weigh no more than 1.5 tons.
  --Popular Mechanics, forecasting the relentless march of
science, 1949
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why is python not written in C++ ?

2010-08-14 Thread Aahz
In article <7xeieevrze@ruckus.brouhaha.com>,
Paul Rubin   wrote:
>
>I'm not sure what the hiring issue is.  I think anyone skilled in C++ or
>Java can pick up Ada pretty easily.  It's mostly a subset of C++ with
>different surface syntax.

Heck, I learned Ada as a sixteen-year-old knowing only BASIC and Pascal.
-- 
Aahz (a...@pythoncraft.com)   <*> http://www.pythoncraft.com/

"...if I were on life-support, I'd rather have it run by a Gameboy than a
Windows box."  --Cliff Wells
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python Sandbox

2010-08-14 Thread geremy condra
On Sat, Aug 14, 2010 at 12:56 PM, Stephen Hansen
 wrote:
> On 8/13/10 8:04 PM, Steven D'Aprano wrote:
>> On Fri, 13 Aug 2010 16:37:40 -0700, Stephen Hansen wrote:
>>
>>> Howdy-ho.
>>>
>>> So, I'm working on a project which embeds Python into a bigger system to
>>> provide extensibility. In this project, there's basically two types of
>>> people who will be entering python code.
>>>
>>> The trusted folks, who write code which are in files, and which can do
>>> anything.
>>>
>>> The untrusted folks, who are writing very simple chunks of code which
>>> can only do limited things.
>>
>> I suggest that if the untrusted code is only supposed to be simple and
>> limited, you would be best off to write your own "mini-language" using
>> Python syntax.
>
> I considered it and rejected it. The return from the effort required
> doesn't even vaguely come close to making it worth it. My worst case
> fall-back plan is to embed /another/ language (be it Lua or JavaScript
> through V8) and offer it a very limited environment. But I don't want to
> do that (and considering I solved the while True: pass problem last
> night, I'm pretty sure I won't decide to).
>
>> The fact is that Python is not designed to be used by untrusted users,
>> and it is REALLY hard to keep it in a sandbox. There was an attempt to
>> sandbox Python, if I recall correctly it was the bastion module, but it
>> turned out to be so leaky that it was removed from the standard library
>> with extreme prejudice. Since then, others have been working on it,
>> including Google, but I don't know how successful they've been.
>
> I know all this -- but its not relevant really, I think. I'm not trying
> to create a safe yet relatively complete or functional Python. All those
> efforts to sandbox Python fail because of the incredible dynamic nature
> of the language has lots of enticing little holes in it. But I'm not
> interested in a full or even vaguely full subset of Python, and I'm not
> requiring that this security be done on the code-level.
>
> For example, when you go to save your bit of code, it will go in and if
> it finds __ anywhere in the text it just replaces it with xx. And, since
> getattr is not available, '_' + '_' won't get you anywhere.
>
>> Here's an example... suppose you wish to allow reading files, but not
>> writing them. Sounds simple?
>>
>> http://tav.espians.com/a-challenge-to-break-python-security.html
>
> Yeah, I'm aware of this little challenge-- but every one of those
> exploits calls for a special attribute call or method creation which is
> impossible(I think) in my setup.
>
> Although Paul Cannon's little exploit is very interesting, and I'm going
> to go brute force murder try/except in a similar way to __ above (in
> this context, exceptions aren't important) now.
>
>> Now, I'm not suggesting that the exploits there are directly applicable
>> to your sandbox, but they give a small idea of the sorts of things you
>> need to consider.
>
> I'm creating a much, much more restrictive subset of Python then most
> sandboxes try to do-- I could make my own custom mini-language, except
> good lord, that's a whole lot of work since there are real needs for
> *some* programming power here. Or I could embed another language in a
> more restrictive way then Python is embedded-- but good lord, now I have
> to handle three languages to get things done :)
>
> I just need a certain limited context where someone can be handed
> certain Python objects and manipulate them. I'd like people to be able
> to use some fundamental Python power -- the rich, beautiful data types
> for example (notably in this case, strings), list comprehensions and
> stuff, to do what they need to do. Python's very easy, I'd like them to
> be able to use that easy.
>
> But I don't need anywhere near full Python power, so sweeping rules
> like, 'no, you can't even type __' or, 'sorry, no exception handling for
> you', work well.

I assume you're cutting out the import machinery?

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


Re: Tk window and contents will not display

2010-08-14 Thread Chris Hare

On Aug 14, 2010, at 3:14 PM, Peter Otten wrote:

> Chris Hare wrote:
> 
>> The scenario is this:
>> 
>> I want to loop around all of the images in a given directory (which I know
>> will be images, but I guess I should check), show an image in a window,
>> wait 2 seconds and show the next one and repeat that indefinitley, which
>> will be until the user closes the window.
>> 
>> This is the code I extracted from the larger program and made work - sort
>> of - in a standalone fashion.  When I run the code, each of the file names
>> gets displayed, and I can view the images, so it has to be something I am
>> doing wrong with this chunk of code.
>> 
>> However, I don't see what the problem is.
> 
> I have not looked at your code in detail, but event loops and time.sleep() 
> don't play together very well. Use after(delay_in_milliseconds, callable) 
> instead. 
> 
> Here's a simple example that loops over images passed from the command line:
> 
> import Image
> import ImageTk
> import os
> import sys
> import Tkinter as tk
> 
> from itertools import cycle
> 
> def next_image():
>imagefile = next(imagefiles)
>image = Image.open(imagefile)
> 
>w, h = image.size
>image = image.resize((700, 700*h//w))
> 
>label.image = label["image"] = ImageTk.PhotoImage(image=image)
>root.title("Now showing %s" % os.path.basename(imagefile))
> 
>root.after(2000, next_image)
> 
> if __name__ == "__main__":
>imagefiles = sys.argv[1:]
>assert imagefiles
>imagefiles = cycle(imagefiles)
> 
>root = tk.Tk()
>label = tk.Label(root)
>label.pack()
> 
>root.after_idle(next_image)
>root.mainloop()
> 

Thanks Peter.  I threw away what I started with and merged your code into my 
class:

class externalLoopDisplay:

def show(self):
main.logging.debug("externalLoopDisplay.show:","start")

self.window = Tk()

self.btnClose = Button(self.window, text="Close", 
command=self.window.destroy, bg=backColor,highlightbackground=warnColor, 
highlightcolor=okColor)
self.btnClose.grid(row=0, column=2)
self.label = Label(self.window)
self.label.grid(row=1, column=0, columnspan=3)
dirName =  getRadarPath() + "/net" + str(netNumber.get()) # e.g. 
.../Radar/net17/net17-MMDDHHMMSS.gif
self.imagefiles = glob.glob(dirName + "/*.gif")
self.imagefiles = cycle(self.imagefiles)
self.window.after_idle(self.next_image)

def next_image(self):
imagefile = next(self.imagefiles)
image = Image.open(imagefile)

w, h = image.size
image = image.resize((600, 550*h//w))

self.label.image = self.label["image"] = 
ImageTk.PhotoImage(image=image) # < bails here
self.window.title("Now showing %s" % os.path.basename(imagefile))

self.window.after(2000, next_image)


I marked where the code bails with an error saying pyimage2 doesn't exist.  All 
of the images exist and worked just fine with your standalone script.

Suggestions?

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


Re: Tk window and contents will not display

2010-08-14 Thread Peter Otten
Chris Hare wrote:

> Thanks Peter.  I threw away what I started with and merged your code into
> my class:
> 
> class externalLoopDisplay:
> 
> def show(self):
> main.logging.debug("externalLoopDisplay.show:","start")
> 
> self.window = Tk()
> 
> self.btnClose = Button(self.window, text="Close",
> command=self.window.destroy,
> bg=backColor,highlightbackground=warnColor,
> highlightcolor=okColor) self.btnClose.grid(row=0, column=2)
> self.label = Label(self.window) self.label.grid(row=1, column=0,
> columnspan=3)
> dirName =  getRadarPath() + "/net" + str(netNumber.get()) # e.g.
> .../Radar/net17/net17-MMDDHHMMSS.gif
> self.imagefiles = glob.glob(dirName + "/*.gif")
> self.imagefiles = cycle(self.imagefiles)
> self.window.after_idle(self.next_image)
> 
> def next_image(self):
> imagefile = next(self.imagefiles)
> image = Image.open(imagefile)
> 
> w, h = image.size
> image = image.resize((600, 550*h//w))
> 
> self.label.image = self.label["image"] =
> ImageTk.PhotoImage(image=image) # < bails here
> self.window.title("Now showing %s" % os.path.basename(imagefile))
> 
> self.window.after(2000, next_image)
> 
> 
> I marked where the code bails with an error saying pyimage2 doesn't exist.
>  All of the images exist and worked just fine with your standalone script.
> 
> Suggestions?

Google says you are calling Tkinter.Tk() more than once where you should 
instead use Tkinter.Toplevel(). As you didn't post that part of the code 
it's hard to verify, but when I add a second 

root = tk.Tk() 

to my example script I get a very similar exception:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
return self.func(*args)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 498, in callit
func(*args)
  File "cycle_image.py", line 16, in next_image
label.image = label["image"] = ImageTk.PhotoImage(image=image)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1212, in __setitem__
self.configure({key: value})
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1205, in configure
return self._configure('configure', cnf, kw)
  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1196, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TclError: image "pyimage1" doesn't exist

By the way: for future posts please remember to cut and paste the traceback, 
don't paraphrase the error message.

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


Pop return from stack?

2010-08-14 Thread bvdp
Assuming I have a module 'foo.py' with something like this:

def error(s):
print "Error", s
sys.exit(1)

def func(s):
... do some processing
... call error() if bad .. go to system exit.
...  more processing

and then I write a new program, test.py, which:

import foo

def myerror(s):
print "new error message"

foo.error = myerror

a = foo.func(..)

Now, if an error is encountered myerror() is called. Fine. But
execution resumes in func(). Not exactly what I wanted.

I can "fix" this simply by wrapping the call to foo.func() in a try/
expect and have myerror() raise an exception. This appears to work,
but I'm hesitant to use this out of fear that I'm building up some
kind of stack overflow or something which will bite me later.

Is there a better way? Simplest for an old assembler guy like me would
be pop a return address off the stack ... but python isn't
assembler :)

I don't want to change stuff in the foo.py module since it's part of
an existing program. But, if I must, I suppose I could. I'd prefer to
just short-circuit this if possible.

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


Re: Tk window and contents will not display

2010-08-14 Thread Chris Hare

On Aug 14, 2010, at 5:49 PM, Peter Otten wrote:

> Chris Hare wrote:
> 
>> Thanks Peter.  I threw away what I started with and merged your code into
>> my class:
>> 
>> class externalLoopDisplay:
>> 
>>def show(self):
>>main.logging.debug("externalLoopDisplay.show:","start")
>> 
>>self.window = Tk()
>> 
>>self.btnClose = Button(self.window, text="Close",
>>command=self.window.destroy,
>>bg=backColor,highlightbackground=warnColor,
>>highlightcolor=okColor) self.btnClose.grid(row=0, column=2)
>>self.label = Label(self.window) self.label.grid(row=1, column=0,
>>columnspan=3)
>>dirName =  getRadarPath() + "/net" + str(netNumber.get()) # e.g.
>>.../Radar/net17/net17-MMDDHHMMSS.gif
>> self.imagefiles = glob.glob(dirName + "/*.gif")
>> self.imagefiles = cycle(self.imagefiles)
>>self.window.after_idle(self.next_image)
>> 
>>def next_image(self):
>>imagefile = next(self.imagefiles)
>>image = Image.open(imagefile)
>> 
>>w, h = image.size
>>image = image.resize((600, 550*h//w))
>> 
>>self.label.image = self.label["image"] =
>>ImageTk.PhotoImage(image=image) # < bails here
>>self.window.title("Now showing %s" % os.path.basename(imagefile))
>> 
>>self.window.after(2000, next_image)
>> 
>> 
>> I marked where the code bails with an error saying pyimage2 doesn't exist.
>> All of the images exist and worked just fine with your standalone script.
>> 
>> Suggestions?
> 
> Google says you are calling Tkinter.Tk() more than once where you should 
> instead use Tkinter.Toplevel(). As you didn't post that part of the code 
> it's hard to verify, but when I add a second 
> 
> root = tk.Tk() 
> 
> to my example script I get a very similar exception:
> 
> Exception in Tkinter callback
> Traceback (most recent call last):
>  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1413, in __call__
>return self.func(*args)
>  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 498, in callit
>func(*args)
>  File "cycle_image.py", line 16, in next_image
>label.image = label["image"] = ImageTk.PhotoImage(image=image)
>  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1212, in __setitem__
>self.configure({key: value})
>  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1205, in configure
>return self._configure('configure', cnf, kw)
>  File "/usr/lib/python2.6/lib-tk/Tkinter.py", line 1196, in _configure
>self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
> TclError: image "pyimage1" doesn't exist
> 
> By the way: for future posts please remember to cut and paste the traceback, 
> don't paraphrase the error message.
> 
> Peter
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Yes - you are bang on.

Thanks.  One final question if I may, how would you suggest I handle checking 
for new files and adding them to the list?  For example, if the loop is playing 
and a new image is added, how can I detect it and then refresh the list of file?

I am stuck on that part with this new approach.

Chris

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


Re: Pop return from stack?

2010-08-14 Thread Thomas Jollans
On Sunday 15 August 2010, it occurred to bvdp to exclaim:
> Assuming I have a module 'foo.py' with something like this:
> 
> def error(s):
> print "Error", s
> sys.exit(1)
> 
> def func(s):
> ... do some processing
> ... call error() if bad .. go to system exit.
> ...  more processing
> 
> and then I write a new program, test.py, which:
> 
> import foo
> 
> def myerror(s):
> print "new error message"
> 
> foo.error = myerror
> 
> a = foo.func(..)
> 
> Now, if an error is encountered myerror() is called. Fine. But
> execution resumes in func(). Not exactly what I wanted.
> 
> I can "fix" this simply by wrapping the call to foo.func() in a try/
> expect and have myerror() raise an exception. This appears to work,
> but I'm hesitant to use this out of fear that I'm building up some
> kind of stack overflow or something which will bite me later.

An exception will walk up the stack, calling any cleaning-up code that needs 
to be done (removing object references, executing finally: blocks, exiting 
context managers properly. It won't break anything. Don't be afraid of 
Python's high-level features!

> 
> Is there a better way? Simplest for an old assembler guy like me would
> be pop a return address off the stack ... but python isn't
> assembler :)

Now that has a decent chance of messing things up and you (if you wrote decent 
assembly ;-)) know it -- without properly cleaning up before resuming 
execution in the right place, you could end up in a right state with memory 
leaks, leaked file descriptors, half-arsed database transactions, etc etc. 

> 
> I don't want to change stuff in the foo.py module since it's part of
> an existing program. But, if I must, I suppose I could. I'd prefer to
> just short-circuit this if possible.

Exceptions. Simple. In the end, all system.exit does is raise a SystemExit 
exception...


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


Re: Simple Python Sandbox

2010-08-14 Thread Roland Koebler
Hi,

> I know all this -- but its not relevant really, I think. I'm not trying
> to create a safe yet relatively complete or functional Python. All those
> efforts to sandbox Python fail because of the incredible dynamic nature
> of the language has lots of enticing little holes in it. But I'm not
> interested in a full or even vaguely full subset of Python, and I'm not
> requiring that this security be done on the code-level.
I had the same problem, and so I created a "pseudo-sandbox" for embedding
Python in templates. This "pseudo-sandbox" creates a restricted Python
environment, where only whitelisted functions/classes are allowed.
Additionally, it prevents things like '0 .__class__'.

You can find some documentation at
http://simple-is-better.org/template/pyratemp.html#evaluation,
and the pseudo-sandbox itself in my template-engine, class
"EvalPseudoSandbox" on the website above.
(Please write me if you have any comments.)

But note that this is not a real sandbox! As soon as you allow *any*
unsafe function (e.g. open, import, eval, getattr etc.), you can easily
break out.
Also, don't directly pass complete modules to the pseudo-sandbox, since
they may contain unsafe functions/classes/etc.

And be warned: There *may* also be ways to break out of the pseudo-sandbox
even without passing unsafe functions to it -- although I don't know any.
If you know or find such a way: Please tell me!


You could also take a look at Jinja (which is also a template-engine),
and which claims to include a sandbox. But the Jinja-sandbox seems to
be much more complicated than my pseudo-sandbox, and I haven't analyzed
it and don't know how it works.

> For example, when you go to save your bit of code, it will go in and if
> it finds __ anywhere in the text it just replaces it with xx. And, since
> getattr is not available, '_' + '_' won't get you anywhere.
I don't think that searching the text is the right way; in my
pseudo-sandbox, I compile the code and search co_names for such
names instead.

> I just need a certain limited context where someone can be handed
> certain Python objects and manipulate them. I'd like people to be able
> to use some fundamental Python power -- the rich, beautiful data types
> for example (notably in this case, strings), list comprehensions and
> stuff, to do what they need to do. Python's very easy, I'd like them to
> be able to use that easy.
I was in the exact same position ;).
(Although I don't have fully untrusted/bad users, and so my pseudo-sandbox
is sufficient for my cases, even though I haven't proved that it really is
secure...)


regards,
Roland

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


Working with PDFs?

2010-08-14 Thread jyoung79
Just curious if anyone knows if it's possible to work with pdf documents 
with Python?  I'd like to do the following:

- Pull out text from each PDF page (to search for specific words)
- Combine separate pdf documents into one document
- Add bookmarks (with destination settings)

A few programs I've been looking at are pdfminer, pyPDF, etc from this 
link:
http://pypi.python.org/pypi?%3Aaction=search&term=pdf&submit=search

Originally, I was using AppleScript and JavaScript to do this in Acrobat. 
But now Acrobat 9 has broken this process and I can't seem to make it 
work.  I'd like to find other workarounds instead of having to rely on 
Adobe.

Thanks for your help.

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


Re: Tk window and contents will not display

2010-08-14 Thread Peter Otten
Chris Hare wrote:

> 
> On Aug 14, 2010, at 5:49 PM, Peter Otten wrote:
> 
>> Chris Hare wrote:
>> 
>>> Thanks Peter.  I threw away what I started with and merged your code
>>> into my class:
>>> 
>>> class externalLoopDisplay:
>>> 
>>>def show(self):
>>>main.logging.debug("externalLoopDisplay.show:","start")
>>> 
>>>self.window = Tk()
>>> 
>>>self.btnClose = Button(self.window, text="Close",
>>>command=self.window.destroy,
>>>bg=backColor,highlightbackground=warnColor,
>>>highlightcolor=okColor) self.btnClose.grid(row=0, column=2)
>>>self.label = Label(self.window) self.label.grid(row=1, column=0,
>>>columnspan=3)
>>>dirName =  getRadarPath() + "/net" + str(netNumber.get()) # e.g.
>>>.../Radar/net17/net17-MMDDHHMMSS.gif

> Thanks.  One final question if I may, how would you suggest I handle
> checking for new files and adding them to the list?  For example, if the
> loop is playing and a new image is added, how can I detect it and then
> refresh the list of file?
> 
> I am stuck on that part with this new approach.
> 
> Chris

Replacing

>>> self.imagefiles = glob.glob(dirName + "/*.gif")
>>> self.imagefiles = cycle(self.imagefiles)

with

self.imagefiles = image_cycler(os.path.join(dirname, "*.gif"))

where image_cycler() looks as follows

def image_cycler(pattern):
while True:
for fn in glob.glob(pattern):
yield fn

would be the simplest way.

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


Re: Simple Python Sandbox

2010-08-14 Thread Steven D'Aprano
On Sun, 15 Aug 2010 01:24:00 +0200, Roland Koebler wrote:

> I had the same problem, and so I created a "pseudo-sandbox" for
> embedding Python in templates. This "pseudo-sandbox" creates a
> restricted Python environment, where only whitelisted functions/classes
> are allowed. Additionally, it prevents things like '0 .__class__'.

Hmmm... is that meant just as an illustration of a general technique, or 
do you actually have something against the class of 0? 0 .__class__ seems 
pretty innocuous to me:

>>> type(0) is 0 .__class__ is int
True


[...]
> But note that this is not a real sandbox! As soon as you allow *any*
> unsafe function (e.g. open, import, eval, getattr etc.), you can easily
> break out.

Isn't that true of any sandbox though? Surely by definition, if you allow 
an unsafe function in any sandbox, it's no longer an effective sandbox.


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


Re: Simple Python Sandbox

2010-08-14 Thread Steven D'Aprano
On Sat, 14 Aug 2010 12:56:45 -0700, Stephen Hansen wrote:

>> I suggest that if the untrusted code is only supposed to be simple and
>> limited, you would be best off to write your own "mini-language" using
>> Python syntax.
> 
> I considered it and rejected it. The return from the effort required
> doesn't even vaguely come close to making it worth it.

I suppose that depends on how simple the untrusted code will be, but I 
guess you're in the best position to make that call.


> My worst case
> fall-back plan is to embed /another/ language (be it Lua or JavaScript
> through V8) and offer it a very limited environment. But I don't want to
> do that (and considering I solved the while True: pass problem last
> night, I'm pretty sure I won't decide to).

I assume you mean you've solved the problem of DOS attacks from users 
running infinite loops. How did you do that?


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


Re: print v. print()

2010-08-14 Thread Steven D'Aprano
On Sat, 14 Aug 2010 11:44:22 -0400, Mel wrote:

> The downside to a print() function is that assigning to `print` can mask
> the function, and leave a neophyte without any way to get output out of
> the program.

On the other hand, the upside to a print() function is that assigning to 
`print` can monkey-patch the function, allowing the advanced user to 
modify it's functionality at runtime. Whether that's a feature or a 
shooting offence is a matter of opinion.

(I think it's a feature, albeit one which is easy to misuse.)


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


Re: shelf-like list?

2010-08-14 Thread kj
In  Raymond 
Hettinger  writes:

>On Aug 12, 1:37=A0pm, Thomas Jollans  wrote:
>> On Tuesday 10 August 2010, it occurred to kj to exclaim:
>>
>> > I'm looking for a module that implements "persistent lists": objects
>> > that behave like lists except that all their elements are stored
>> > on disk. =A0IOW, the equivalent of "shelves", but for lists rather
>> > than a dictionaries.
> . . .
>> You could simply use pickle to save the data every once in a while.

>That is a very reasonable solution.

Sorry I don't follow.  Some sample code would be helpful.
TIA,
~K

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


Re: Pop return from stack?

2010-08-14 Thread Steven D'Aprano
On Sat, 14 Aug 2010 16:05:05 -0700, bvdp wrote:

> Assuming I have a module 'foo.py' with something like this:
> 
> def error(s):
> print "Error", s
> sys.exit(1)
> 
> def func(s):
> ... do some processing
> ... call error() if bad .. go to system exit. ...  more processing
> 
> and then I write a new program, test.py, which:
> 
> import foo
> 
> def myerror(s):
> print "new error message"
> 
> foo.error = myerror
> 
> a = foo.func(..)

This general technique is called "monkey patching". 


> Now, if an error is encountered myerror() is called. Fine. But execution
> resumes in func(). Not exactly what I wanted.

Of course it does. Your new error handler fails to exit, so execution 
resumes like it does after any other function.

You can either manually exit from your own error handler:

def myerror(s):
print "new error message"
sys.exit(2)


or call the original error handler:


def myerror(s):
print "new error message"
foo._error(s)


That second technique requires some preparation before hand. In module 
foo, after defining the error() function, you then need to create a 
second, private, name to it:

_error = error



> I can "fix" this simply by wrapping the call to foo.func() in a try/
> expect and have myerror() raise an exception. This appears to work, but
> I'm hesitant to use this out of fear that I'm building up some kind of
> stack overflow or something which will bite me later.

Exceptions are the standard way of doing things. That's what sys.exit() 
does -- it raises SystemExit exception.

With very few exceptions, if you're writing your own error handlers like 
this, you're doing it wrong. Your error handler throws away useful 
debugging information, and it gives you no extra information that a 
standard Python traceback couldn't give.


> Is there a better way? Simplest for an old assembler guy like me would
> be pop a return address off the stack ... but python isn't assembler :)

Oh my ... I've seen people writing Java in Python, C++ in Python, Perl in 
Python, even VB in Python, but this is the first time I've meet some one 
who wants to write assembler in Python :)



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


How to add silent stretches to MP3 using Python?

2010-08-14 Thread kj


Here's the problem: I have about 25,000 mp3 files, each lasting,
*on average*, only a few seconds, though the variance is wide (the
longest one lasts around 20 seconds).  (These files correspond to
sample sentences for foreign language training.)

The problem is that there is basically no padding before and after
the sound signal.  I want to prepend about 2 seconds of silence to
each file, and append another silent stretch at the end lasting
either 2 seconds or some multiplier of the duration of the original
file, whichever is greater.

I know that manipulating MP3 audio programmatically is usually not
easy, but this has got to be one of the simplest manipulations
possible, so I'm hoping I'll be able to pull it off with Python.

But I have not had much luck finding a Python library to do this.
If someone knows how to do this, and could give me some pointers,
I'd appreciate it.

TIA!

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


Re: Simple Python Sandbox

2010-08-14 Thread Christian Heimes
> For example, when you go to save your bit of code, it will go in and if
> it finds __ anywhere in the text it just replaces it with xx. And, since
> getattr is not available, '_' + '_' won't get you anywhere.

That's not as secure as you might think. First of all you can write "_"
in more way than you may think.

>>> 2*chr(0x5f) + "insecure" + 2*chr(0x5f)
'__insecure__'
>>> "\x5f\x5finsecure\x5f\x5f"
'__insecure__'
>>> str(u"\N{LOW LINE}\N{LOW LINE}insecure\N{LOW LINE}\N{LOW LINE}")
'__insecure__'

If you have access to eval, exec or compile you can easily work around
your restrictions:

>>> getattribute = eval("object.__getattribute__")
>>> getattribute(int, "__new__")(int, "3")
3

As you can see, black listing isn't the best approach here.

Christian

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


Re: Tk window and contents will not display

2010-08-14 Thread Chris Hare

On Aug 14, 2010, at 6:46 PM, Peter Otten wrote:

> Chris Hare wrote:
> 
>> 
>> On Aug 14, 2010, at 5:49 PM, Peter Otten wrote:
>> 
>>> Chris Hare wrote:
>>> 
 Thanks Peter.  I threw away what I started with and merged your code
 into my class:
 
 class externalLoopDisplay:
 
   def show(self):
   main.logging.debug("externalLoopDisplay.show:","start")
 
   self.window = Tk()
 
   self.btnClose = Button(self.window, text="Close",
   command=self.window.destroy,
   bg=backColor,highlightbackground=warnColor,
   highlightcolor=okColor) self.btnClose.grid(row=0, column=2)
   self.label = Label(self.window) self.label.grid(row=1, column=0,
   columnspan=3)
   dirName =  getRadarPath() + "/net" + str(netNumber.get()) # e.g.
   .../Radar/net17/net17-MMDDHHMMSS.gif
> 
>> Thanks.  One final question if I may, how would you suggest I handle
>> checking for new files and adding them to the list?  For example, if the
>> loop is playing and a new image is added, how can I detect it and then
>> refresh the list of file?
>> 
>> I am stuck on that part with this new approach.
>> 
>> Chris
> 
> Replacing
> 
 self.imagefiles = glob.glob(dirName + "/*.gif")
 self.imagefiles = cycle(self.imagefiles)
> 
> with
> 
> self.imagefiles = image_cycler(os.path.join(dirname, "*.gif"))
> 
> where image_cycler() looks as follows
> 
> def image_cycler(pattern):
>while True:
>for fn in glob.glob(pattern):
>yield fn
> 
> would be the simplest way.
> 
> Peter
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Perfect!

Thank you

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


Re: Simple Python Sandbox

2010-08-14 Thread Roland Koebler
On Sun, Aug 15, 2010 at 12:06:35AM +, Steven D'Aprano wrote:
> Hmmm... is that meant just as an illustration of a general technique, or 
> do you actually have something against the class of 0?
It's a short illustration; 0 .__class__ itself is harmless, but e.g.
0 .__class__.__base__.__subclasses__() isn't.

> > But note that this is not a real sandbox! As soon as you allow *any*
> > unsafe function (e.g. open, import, eval, getattr etc.), you can easily
> > break out.
> 
> Isn't that true of any sandbox though? Surely by definition, if you allow 
> an unsafe function in any sandbox, it's no longer an effective sandbox.
In my opinion, a "real" sandbox should allow to use "unsafe" functions
(e.g. open(), import modules etc.) -- so you could run your normal code
in it. But it should prevent the "bad" effects of the code, e.g. by
redirecting I/O, limiting resources etc.

regards,
Roland

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


Re: How to add silent stretches to MP3 using Python?

2010-08-14 Thread Tim Chase

Here's the problem: I have about 25,000 mp3 files, each lasting,
*on average*, only a few seconds, though the variance is wide (the
longest one lasts around 20 seconds).  (These files correspond to
sample sentences for foreign language training.)

The problem is that there is basically no padding before and after
the sound signal.  I want to prepend about 2 seconds of silence to
each file, and append another silent stretch at the end lasting
either 2 seconds or some multiplier of the duration of the original
file, whichever is greater.

I know that manipulating MP3 audio programmatically is usually not
easy, but this has got to be one of the simplest manipulations
possible, so I'm hoping I'll be able to pull it off with Python.

But I have not had much luck finding a Python library to do this.
If someone knows how to do this, and could give me some pointers,
I'd appreciate it.


While it's (1) not 100% python and (2) doesn't allow for your "2 
seconds...or some multiplier of the duration, whichever is 
greater", you can use mp3wrap[1] to losslessly join the files. 
If you happen to make a 2-seconds-of-silence MP3, you can then 
join your files together with the silence.  For 25k files, you 
might have to stitch subsets of them together incrementally and 
then stitch together the resulting files.


-tkc


[1] http://mp3wrap.sourceforge.net/
or "apt-get install mp3wrap" here on my Debian box



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


Re: Pop return from stack?

2010-08-14 Thread Carl Banks
On Aug 14, 4:05 pm, bvdp  wrote:
> Assuming I have a module 'foo.py' with something like this:
>
> def error(s):
>     print "Error", s
>     sys.exit(1)
>
> def func(s):
>     ... do some processing
>     ... call error() if bad .. go to system exit.
>     ...  more processing
>
> and then I write a new program, test.py, which:
>
> import foo
>
> def myerror(s):
>     print "new error message"
>
> foo.error = myerror
>
> a = foo.func(..)
>
> Now, if an error is encountered myerror() is called. Fine. But
> execution resumes in func(). Not exactly what I wanted.
>
> I can "fix" this simply by wrapping the call to foo.func() in a try/
> expect and have myerror() raise an exception. This appears to work,
> but I'm hesitant to use this out of fear that I'm building up some
> kind of stack overflow or something which will bite me later.

What do you think a few words of data the stack are going to do?

Just do it this way.


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


Re: Simple Python Sandbox

2010-08-14 Thread Stephen Hansen
On 8/14/10 5:06 PM, Steven D'Aprano wrote:
> On Sun, 15 Aug 2010 01:24:00 +0200, Roland Koebler wrote:
> 
>> I had the same problem, and so I created a "pseudo-sandbox" for
>> embedding Python in templates. This "pseudo-sandbox" creates a
>> restricted Python environment, where only whitelisted functions/classes
>> are allowed. Additionally, it prevents things like '0 .__class__'.
> 
> Hmmm... is that meant just as an illustration of a general technique, or 
> do you actually have something against the class of 0? 0 .__class__ seems 
> pretty innocuous to me:
> 
 type(0) is 0 .__class__ is int
> True

Assuming you have a totally restricted environment, where none of the
normal built-ins are available-- notably "type"-- in theory I thought
once that you could exec pretty safely. Because there's just no access
to anything!

But, alas, someone showed me I was wrong. 0 .__class__ can lead you to
"type".

And type.__subclasses__ happily leads you to everything in the world.

I solve this by just refusing to allow getattr, and __ anywhere in the
file to be saved just gets turned into xx, so its impossible (I think)
for users to access or use any special method.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python Sandbox

2010-08-14 Thread Stephen Hansen
On 8/14/10 2:25 PM, Cameron Simpson wrote:
> Ok, what about this: run the untrusted code in a separate process,
> if necessary running as a user with different privileges.

Way too much overhead by a really significant margin: I need to do many,
many, many, many, many very short (often very *very* short) little
scripts. Doing IPC to separate processes, even if they're long-running,
and handling proxying of my objects that these scripts need to read from
and tweak in certain ways, would just kill performance.

I really do need an embedded language -- and a very restricted subset of
Python is really ideal for that, which is why I'm going for this. :)

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python Sandbox

2010-08-14 Thread Stephen Hansen
On 8/14/10 4:24 PM, Roland Koebler wrote:
> You can find some documentation at
> http://simple-is-better.org/template/pyratemp.html#evaluation,
> and the pseudo-sandbox itself in my template-engine, class
> "EvalPseudoSandbox" on the website above.
> (Please write me if you have any comments.)

How are you implementing refusing-names-beginning-with-underscore, out
of curiosity?

> You could also take a look at Jinja (which is also a template-engine),
> and which claims to include a sandbox. But the Jinja-sandbox seems to
> be much more complicated than my pseudo-sandbox, and I haven't analyzed
> it and don't know how it works.

I'll take a look.

>> I just need a certain limited context where someone can be handed
>> certain Python objects and manipulate them. I'd like people to be able
>> to use some fundamental Python power -- the rich, beautiful data types
>> for example (notably in this case, strings), list comprehensions and
>> stuff, to do what they need to do. Python's very easy, I'd like them to
>> be able to use that easy.
> I was in the exact same position ;).
> (Although I don't have fully untrusted/bad users, and so my pseudo-sandbox
> is sufficient for my cases, even though I haven't proved that it really is
> secure...)

I don't *really* have a bunch fully untrusted / bad users, in fact I
expect I will sort of trust all the people doing this level of coding --
but I believe that either incompetance and maliciousness is inevitable
in any sort of online community, and making it at least as hard as
possible to do damage while giving people as much freedom and tools to
do great is the ideal goal. :)

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Working with PDFs?

2010-08-14 Thread Terry Reedy

On 8/14/2010 7:44 PM, jyoun...@kc.rr.com wrote:

Just curious if anyone knows if it's possible to work with pdf documents
with Python?  I'd like to do the following:


search python pdf library
reportlab

--
Terry Jan Reedy

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


Re: Simple Python Sandbox

2010-08-14 Thread Stephen Hansen
On 8/14/10 5:36 PM, Christian Heimes wrote:
>> For example, when you go to save your bit of code, it will go in and if
>> it finds __ anywhere in the text it just replaces it with xx. And, since
>> getattr is not available, '_' + '_' won't get you anywhere.
> 
> That's not as secure as you might think. First of all you can write "_"
> in more way than you may think.

Well yes, I know-- but as I said in the original post, eval, exec,
compile, getattr and such, are all unavailable.

So its possible someone can write '_' in various ways (though I'm
disallowing both chr and unichr for entirely different reasons), and
they can even put them together so that there's a __ as a string in some
variable, I can't find any way in which they would then be able to get
to get a special method. Namely, type.__subclasses__ or
object.__getattribute__.

They can't enter any code which does blah.__class__, and they can't
construct a string through twisty ways to then do getattr(blah, twisty)
where twisty = "__class__".


> If you have access to eval, exec or compile you can easily work around
> your restrictions:

I think its pretty much a given that if you're even trying to do some
kind of restricted sandbox, eval and the like are the first things to go. :)

> As you can see, black listing isn't the best approach here.

But I have a two pronged strategy: the black list is only half of the
equation. One, I'm blacklisting all the meta functions out of builtins.
Eval, compile, getattr, and such. Then, I'm doing some blatant textual
munging, making it so someone can not access any __special__ methods, or
use exec.


-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python Sandbox

2010-08-14 Thread Stephen Hansen
On 8/14/10 5:09 PM, Steven D'Aprano wrote:
>> My worst case
>> fall-back plan is to embed /another/ language (be it Lua or JavaScript
>> through V8) and offer it a very limited environment. But I don't want to
>> do that (and considering I solved the while True: pass problem last
>> night, I'm pretty sure I won't decide to).
> 
> I assume you mean you've solved the problem of DOS attacks from users 
> running infinite loops. How did you do that?

Since I only have to run this on Unix-isms, I'm using alarm()/signal().
The C code takes the hash of the source code that's going to be executed
and marks it, then sets an alarm and executes the code (though its
usually been compiled into a code object).

There's no code which would -ever- in this situation take longer then 2
seconds to run (and that's extremely generous), but the alarm is 5: if
the code doesn't return and cancel the alarm by then, I know the code is
functionally broken.

So, the signal handler records the hash of the code that failed -- it'll
never be tried again -- logs an error message, and restarts the whole
process (which happens seamlessly with virtually no user interruption,
but this system is not architected in a way where its readily able to
resume operation in the event of a signal interrupt).

This isn't perfect: infinite loops it kills, but things like [0] *
10^^100 crash head first into the machine and bring it to a crawl. I
haven't figured out a strategy for trying to address that yet, and
ultimately, I may not find one. That's okay: perfection isn't my goal,
infinite loops are easy enough to do on accident that halting them is
important.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pop return from stack?

2010-08-14 Thread Chris Rebert
On Sat, Aug 14, 2010 at 5:23 PM, Steven D'Aprano
 wrote:

> Oh my ... I've seen people writing Java in Python, C++ in Python, Perl in
> Python, even VB in Python, but this is the first time I've meet some one
> who wants to write assembler in Python :)

+1 QOTW

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


Re: Simple Python Sandbox

2010-08-14 Thread geremy condra
On Sat, Aug 14, 2010 at 8:07 PM, Stephen Hansen
 wrote:
> On 8/14/10 5:09 PM, Steven D'Aprano wrote:
>>> My worst case
>>> fall-back plan is to embed /another/ language (be it Lua or JavaScript
>>> through V8) and offer it a very limited environment. But I don't want to
>>> do that (and considering I solved the while True: pass problem last
>>> night, I'm pretty sure I won't decide to).
>>
>> I assume you mean you've solved the problem of DOS attacks from users
>> running infinite loops. How did you do that?
>
> Since I only have to run this on Unix-isms, I'm using alarm()/signal().
> The C code takes the hash of the source code that's going to be executed
> and marks it, then sets an alarm and executes the code (though its
> usually been compiled into a code object).
>
> There's no code which would -ever- in this situation take longer then 2
> seconds to run (and that's extremely generous), but the alarm is 5: if
> the code doesn't return and cancel the alarm by then, I know the code is
> functionally broken.
>
> So, the signal handler records the hash of the code that failed -- it'll
> never be tried again -- logs an error message, and restarts the whole
> process (which happens seamlessly with virtually no user interruption,
> but this system is not architected in a way where its readily able to
> resume operation in the event of a signal interrupt).
>
> This isn't perfect: infinite loops it kills, but things like [0] *
> 10^^100 crash head first into the machine and bring it to a crawl. I
> haven't figured out a strategy for trying to address that yet, and
> ultimately, I may not find one. That's okay: perfection isn't my goal,
> infinite loops are easy enough to do on accident that halting them is
> important.

cpulimit or a cgroups container can both be easy solutions here,
depending on your exact needs.

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


Re: Simple Python Sandbox

2010-08-14 Thread Stephen Hansen
On 8/14/10 8:11 PM, geremy condra wrote:
> cpulimit or a cgroups container can both be easy solutions here,
> depending on your exact needs.

Hmm! I wasn't aware of those, I'll check that out. Thanks.

-- 

   Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/



signature.asc
Description: OpenPGP digital signature
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple Python Sandbox

2010-08-14 Thread geremy condra
On Sat, Aug 14, 2010 at 8:18 PM, Stephen Hansen
 wrote:
> On 8/14/10 8:11 PM, geremy condra wrote:
>> cpulimit or a cgroups container can both be easy solutions here,
>> depending on your exact needs.
>
> Hmm! I wasn't aware of those, I'll check that out. Thanks.

Np. I wrote a set of cgroups bindings in Python a few years ago that I
could probably lay my hands on if they'd help, although they may be
out of date by now.

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


Re: Why is python not written in C++ ?

2010-08-14 Thread Lawrence D'Oliveiro
In message
<44d30ac7-931e-4eb0-9aed-f664c872d...@l20g2000yqm.googlegroups.com>, 
sturlamolden wrote:

> A C++ compiler can use Python's header files and link with Python's C API
> correctly. But it cannot compile Python's C source code. A C compiler
> is required to compile and build Python.

Since when do you find a C++ compiler not accompanied by a C one?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Pop return from stack?

2010-08-14 Thread bvdp

> An exception will walk up the stack, calling any cleaning-up code that needs
> to be done (removing object references, executing finally: blocks, exiting
> context managers properly. It won't break anything. Don't be afraid of
> Python's high-level features!

Okay, I believe you (and the rest of the gang. In my trivial program
the exception in working ... so I'll leave it alone.

Thanks.

> - Thomas

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


Re: Pop return from stack?

2010-08-14 Thread bvdp
On Aug 14, 5:23 pm, Steven D'Aprano  wrote:

> This general technique is called "monkey patching".
>

New term for me :)

> > Now, if an error is encountered myerror() is called. Fine. But execution
> > resumes in func(). Not exactly what I wanted.
>
> Of course it does. Your new error handler fails to exit, so execution
> resumes like it does after any other function.

I guess I wasn't being clear. I don't want to exit in my new bit of
code. Just continue a loop (which I didn't show in the example).

> Exceptions are the standard way of doing things. That's what sys.exit()
> does -- it raises SystemExit exception.

Okay, didn't know that exit() was really an exception. Good to know.
But, like I said, I'm not looking to exit.

>
> With very few exceptions, if you're writing your own error handlers like
> this, you're doing it wrong. Your error handler throws away useful
> debugging information, and it gives you no extra information that a
> standard Python traceback couldn't give.

Yeah, but I really don't want a traceback printed out for a user just
because a file can't be found, or he's got a bad bit of syntax in his
file. So, that's why I have the specific error routine. Works fine in
the main program.

> Oh my ... I've seen people writing Java in Python, C++ in Python, Perl in
> Python, even VB in Python, but this is the first time I've meet some one
> who wants to write assembler in Python :)

Naw, I had my fun with assembler in the good old days. Never want to
write another line of it :)

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


Re: Why is python not written in C++ ?

2010-08-14 Thread Grant Edwards
On 2010-08-14, Aahz  wrote:
> Paul Rubin   wrote:
>>
>>I'm not sure what the hiring issue is.  I think anyone skilled in C++ or
>>Java can pick up Ada pretty easily.  It's mostly a subset of C++ with
>>different surface syntax.
>
> Heck, I learned Ada as a sixteen-year-old knowing only BASIC and Pascal.

Unfortunately, there just aren't that many Aahzes to hire (sixteen
years old or otherwise), and of the non-Aahz progrommers out there
it's shocking how many of them them are apparently incapable of
learning a second language.

Regardless of how easy something is to learn, management always wants
to hire people who don't have to.

-- 
Grant


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


Re: shelf-like list?

2010-08-14 Thread Zac Burns
This should help:

http://docs.python.org/library/pickle.html

--
Zachary Burns
(407)590-4814
Aim - Zac256FL
Production Engineer
Zindagi Games


On Sat, Aug 14, 2010 at 5:13 PM, kj  wrote:

> In 
> Raymond Hettinger  writes:
>
> >On Aug 12, 1:37=A0pm, Thomas Jollans  wrote:
> >> On Tuesday 10 August 2010, it occurred to kj to exclaim:
> >>
> >> > I'm looking for a module that implements "persistent lists": objects
> >> > that behave like lists except that all their elements are stored
> >> > on disk. =A0IOW, the equivalent of "shelves", but for lists rather
> >> > than a dictionaries.
> > . . .
> >> You could simply use pickle to save the data every once in a while.
>
> >That is a very reasonable solution.
>
> Sorry I don't follow.  Some sample code would be helpful.
> TIA,
> ~K
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: shelf-like list?

2010-08-14 Thread Chris Rebert
On Sat, Aug 14, 2010 at 5:13 PM, kj  wrote:
> In  
> Raymond Hettinger  writes:
>>On Aug 12, 1:37=A0pm, Thomas Jollans  wrote:
>>> On Tuesday 10 August 2010, it occurred to kj to exclaim:
>>>
>>> > I'm looking for a module that implements "persistent lists": objects
>>> > that behave like lists except that all their elements are stored
>>> > on disk. =A0IOW, the equivalent of "shelves", but for lists rather
>>> > than a dictionaries.
>> . . .
>>> You could simply use pickle to save the data every once in a while.
>
>>That is a very reasonable solution.
>
> Sorry I don't follow.  Some sample code would be helpful.

I would assume something along the lines of (untested):

from pickle import dump

MOD_THRESHOLD = 42

class PersistentList(list):
def __init__(self, filepath):
self.filepath = filepath
self._mod_count = 0

def save(self):
with open(self.filepath, 'w') as f:
dump(self, f)
self._mod_count = 0

def append(self, *args, **kwds):
super(PersistentList, self).append(*args, **kwds)
self._mod_count += 1
if self._mod_count >= MOD_THRESHOLD:
# obviously time-since-last-dump or other
#   more sophisticated metrics might be used instead
self.save()
# define similar wrappers for list's other mutator methods:
__delitem__, __iadd__, __imul__, __setitem__, extend, insert, pop,
remove, etc.
# using decorators should help decrease code duplication

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


Re: OpenCV_Problem

2010-08-14 Thread Rami Chowdhury
On Sun, Aug 15, 2010 at 12:22, arihant nahata  wrote:
> Hi,
>     I m new to python and openCV.

I think you meant to send this to the list ;-)

> i installed openCV and python and copied
> the necessary folder. and even appended the sys.path. but then too the same
> error.
>
>  from opencv import cv
>
> File "C:\Python26\lib\site-packages\opencv\__init__.py", line 74, in
>
> 
>from cv import *
> File "C:\Python26\lib\site-packages\opencv\cv.py", line 25, in 
>
>_cv = swig_import_helper()
>  File "C:\Python26\lib\site-packages\opencv\cv.py", line 21, in
> swig_import_helper
>_mod = imp.load_module('_cv', fp, pathname, description)
> ImportError: DLL load failed: The specified module could not be found.
>
> can you help me with this??

> Regards,
>
> Arihant

Is there a module _cv.dll in your sys.path?

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


Re: Working with PDFs?

2010-08-14 Thread geremy condra
On Sat, Aug 14, 2010 at 7:58 PM, Terry Reedy  wrote:
> On 8/14/2010 7:44 PM, jyoun...@kc.rr.com wrote:
>>
>> Just curious if anyone knows if it's possible to work with pdf documents
>> with Python?  I'd like to do the following:
>
> search python pdf library
> reportlab

I second the reportlab recommendation; while I've never had to search
PDFs etc, I've produced some very good-looking ones with it and was
very happy with how little effort it took.

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