python3 import idlelib.PyShell fails

2013-06-30 Thread Helmut Jarausch
Hi,

I have a strange error. When I try import idlelib.PyShell from Python3.3 it 
fails
with

Python 3.3.2+ (3.3:68ff68f9a0d5+, Jun 30 2013, 12:59:15) 
[GCC 4.7.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import idlelib.PyShell
Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib64/python3.3/idlelib/PyShell.py", line 29, in 
from idlelib.EditorWindow import EditorWindow, fixwordbreaks
  File "/usr/lib64/python3.3/idlelib/EditorWindow.py", line 11, in 
import webbrowser
  File "/usr/lib64/python3.3/webbrowser.py", line 7, in 
import shlex
  File "./shlex.py", line 56
print 'shlex: reading from %s, line %d' \
  ^
SyntaxError: invalid syntax


Looking at shlex.py from /usr/lib64/python3.3 there is no print
statement in line 56.

But, looking at /usr/lib/python2.7/shlex.py there is this print statement
in line 56.

Why does idlelib.PyShell when imported from Python3 import shlex from
Python2 ?

What's going on here?

Many thanks for a hint,
Helmut.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python3 import idlelib.PyShell fails

2013-06-30 Thread Helmut Jarausch
On Sun, 30 Jun 2013 13:20:24 +0200, Peter Otten wrote:

Thanks a lot!
Helmut.
-- 
http://mail.python.org/mailman/listinfo/python-list


How to make this faster

2013-07-05 Thread Helmut Jarausch
Hi,

I have coded a simple algorithm to solve a Sudoku (probably not the first one).
Unfortunately, it takes 13 seconds for a difficult problem which is more than 
75 times slower
than the same algorithm coded in C++.
Is this to be expected or could I have made my Python version faster *** 
without *** sacrificing readability.
Profiling shows that the function find_good_cell is called (only) 45267 times 
and this take 12.9 seconds
CPU time (on a 3.2 GHz machine)

For your pleasure I include the full code below.

Many thanks for a hint,
Helmut


#!/usr/bin/python3
import numpy as np

Grid= np.zeros((9,9),dtype=int)
Row_Digits = np.asarray(np.zeros((9,10)),dtype=bool)
Col_Digits = np.asarray(np.zeros((9,10)),dtype=bool)
Sqr_Digits = np.asarray(np.zeros((9,10)),dtype=bool)

def Read_Sudoku(Input) :
  r= -1
  R_Cells= 81
  for line in Input :
line= line.strip()
if  len(line) == 0  or  line[0] == '#' : continue
r+= 1
for (c,ch) in enumerate(line) :
  if  ch == '_' : continue
  if not ch in "123456789_" :
raise ValueError("invalid character {0} in input line 
{1}".format(c,line))
  Sq_No= (r//3)*3+c//3
  d= int(ch)
  Grid[r,c]= d
  Row_Digits[r,d]= True
  Col_Digits[c,d]= True
  Sqr_Digits[Sq_No,d]= True
  R_Cells-= 1
  
  return R_Cells

def Print_Grid() :
  Sep = "+---+---+---#---+---+---#---+---+---+"
  SepK= "#"
  print(Sep)
  for r in range(9) :
print('|',end='')
for c in range(9) :
  d= Grid[r,c]
  print(" {0} {1}".format( str(d) if d>0 else ' ',
   '#' if (c+1)%3==0 and c>0 and c<8 else '|' ), 
end='')
print("\n{}".format(SepK if (r+1)%3==0 and r>0 and r<8 else Sep))


def find_good_cell() :
  Best= None
  minPoss= 10
  for r in range(9) :
for c in range(9) :
  if  Grid[r,c] > 0 : continue
  Sq_No= (r//3)*3+c//3
  Possibilities= 0
  for d in range(1,10) :
if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : continue
Possibilities+= 1
  
  if ( Possibilities < minPoss ) :
minPoss= Possibilities
Best= (r,c)

  if minPoss == 0 : Best=(-1,-1)
  return Best

def Solve(R_Cells) :
  if  R_Cells == 0 :
print("\n\n++ S o l u t i o n ++\n")
Print_Grid()
return True

  r,c= find_good_cell()
  if r < 0 : return False
  Sq_No= (r//3)*3+c//3
  
  for d in range(1,10) :
if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : continue
# put d into Grid
Grid[r,c]= d
Row_Digits[r,d]= True
Col_Digits[c,d]= True
Sqr_Digits[Sq_No,d]= True

Success= Solve(R_Cells-1)

# remove d again
Grid[r,c]= 0
Row_Digits[r,d]= False
Col_Digits[c,d]= False
Sqr_Digits[Sq_No,d]= False
  
if Success :
  Zuege.append((d,r,c))
  return True

  return False

from io import StringIO

Problem='''
_
_3_85
__1_2
___5_7___
__4___1__
_9___
5__73
__2_1
4___9
'''

Input= StringIO(Problem)
from time import process_time
R_Cells= Read_Sudoku(Input)
Print_Grid()
Zuege=[]
Start= process_time()
# import cProfile
# cProfile.run("Success = Solve(R_Cells)")
Success = Solve(R_Cells)
Stop= process_time()
print("after {} seconds:".format(Stop-Start))
if Success :
  print("\nZuege:")
  n=0
  for Z in reversed(Zuege) :
print("{0} -> ({1},{2})\t".format(Z[0],Z[1]+1,Z[2]+1),end='')
n+= 1
if n%5 == 0 :
  print()

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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 10:38:35 +0100, Fábio Santos wrote:

> [Skipping to bottleneck]
> 
>> def find_good_cell() :
> 
> In this function you are accessing global variables a lot of times. Since
> accessing globals takes much more time than accessing locals, I advise you
> to assign them to local names, and use them.

I've tried to use local "references" like
G= Grid and using G instead of Grid below and similarly for
Row_Digits, Col_Digits and Sqr_Digits but it had no noticeable effect.

> 
>>   Best= None
>>   minPoss= 10
>>   for r in range(9) :
>> for c in range(9) :
>>   if  Grid[r,c] > 0 : continue
>>   Sq_No= (r//3)*3+c//3
>>   Possibilities= 0
>>   for d in range(1,10) :
> 
> On this condition (below) you can try to check which condition is True more
> often and put that condition first in order to take advantage of short
> circuiting and minimize array access.
Unfortunately, that's unpredictable.
> 
>> if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] :
> continue

Many thanks,
Helmut
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 11:13:33 +0100, Oscar Benjamin wrote:

> My one comment is that you're not really making the most out of numpy
> arrays. Numpy's ndarrays are efficient when each line of Python code
> is triggering a large number of numerical computations performed over
> the array. Because of their N-dimensional nature and the fact that
> they are in some sense second class citizens in CPython they are often
> not as good as lists for this kind of looping and indexing.
> 
> I would actually expect this program to run faster with ordinary
> Python lists and lists of lists. It means that you need to change e.g.
> Grid[r, c] to Grid[r][c] but really I think that the indexing syntax
> is all you're getting out of numpy here.
> 

Thanks Oscar, that was a big improvement, indeed.
Using lists of lists instead of numpy arrays made the code more than
twice as fast (13 seconds down to 6 seconds)

Since I don't do any numerical stuff with the arrays, Numpy doesn't seem to be
a good choice. I think this is an argument to add real arrays to Python.

I even tried to use dictionaries instead of Numpy arrays. This version is a bit
slower then the lists of lists version (7.2 seconds instead of 6 second) but 
still
much faster than the Numpy array solution.

Helmut

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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 14:41:23 +0100, Oscar Benjamin wrote:

> On 5 July 2013 11:53, Helmut Jarausch  wrote:
>> I even tried to use dictionaries instead of Numpy arrays. This version is a 
>> bit
>> slower then the lists of lists version (7.2 seconds instead of 6 second) but 
>> still
>> much faster than the Numpy array solution.
> 
> When you switched to dictionaries did you take advantage of the
> sparseness by iterating over dictionary keys instead of indices? This
> is the kind of thing that I meant when I said that in Python it's
> often easier to implement a better algorithm than in C. What I mean is
> that if Grid is a dict so that Grid[(r, c)] is the entry at row r and
> column c (if it exists) then you can change a loop like:
> 
> for r in range(9):
> for c in range(9):
> if Grid[r, c] > 0: continue
> # do stuff
> 
> so that it looks like:
> 
> for r, c in Grid:
> # do stuff
> 
> If the grid is sparsely occupied then this could be a significant improvement.
> 
> 
> Oscar

This gives a big speedup. Now, the time is gone down to 1.73 seconds in 
comparison to
original 13 seconds or the 7 seconds for the first version above.

Many thanks,
it seems hard to optimize a Python program,
Helmut
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 12:02:21 +, Steven D'Aprano wrote:

> On Fri, 05 Jul 2013 10:53:35 +, Helmut Jarausch wrote:
> 
>> Since I don't do any numerical stuff with the arrays, Numpy doesn't seem
>> to be a good choice. I think this is an argument to add real arrays to
>> Python.
> 
> Guido's time machine strikes again:
> 
> import array
> 
> 
> By the way, I'm not exactly sure how you go from "I don't do numerical 
> calculations on numpy arrays" to "therefore Python should have arrays".

I should have been more clear. I meant multi-dimensional arrays (2D, at least)
Numpy is fine if I do math with matrices (without loops in python).

Given that I don't like to use the old FORTRAN way (when "dynamic" arrays are 
passed to
functions) of indexing a 2-d array I would need a MACRO or an INLINED function 
in Python
or something like a META-compiler phase transforming

def access2d(v,i,j,dim1) :  # doesn't work on the l.h.s.
  return v[i*dim1+j]

access2d(v,i,j,dim1) = 7# at compile time, please

to

v[i*dim1+j]= 7  # this, by itself, is considered ugly (the FORTRAN way)

Thanks for the discussion,
Helmut




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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 13:44:57 +0100, Fábio Santos wrote:
May I suggest you avoid range and use enumerate(the_array) instead? It
might be faster.

How does this work?

Given

Grid= [[0 for j in range(9)] for i in range(9)]

for (r,c,val) in (Grid) :

Helmut

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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 15:45:25 +0100, Oscar Benjamin wrote:

> Presumably then you're now down to the innermost loop as a bottle-neck:
> 
>   Possibilities= 0
>   for d in range(1,10) :
> if Row_Digits[r,d] or Col_Digits[c,d] or Sqr_Digits[Sq_No,d] : 
> continue
> Possibilities+= 1
> 
> If you make it so that e.g. Row_Digits[r] is a set of indices rather
> than a list of bools then you can do this with something like
> 
> Possibilities = len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
> 
> or perhaps
> 
> Possibilities = len(set.union(Row_Digits[r], Col_Digits[c],
> Sqr_Digits[Sq_No]))
> 
> which I would expect to be a little faster than looping over range
> since the loop is then performed under the hood by the builtin
> set-type.
> 
> It just takes practice. 

indeed

> It's a little less obvious in Python than in
> low-level languages where the bottlenecks will be and which operations
> are faster/slower but optimisation always involves a certain amount of
> trial and error anyway.
> 
> 
> Oscar

I've tried the following version

def find_good_cell() :
  Best= None
  minPoss= 10
  for r,c in Grid :
if  Grid[(r,c)] > 0 : continue
Sq_No= (r//3)*3+c//3
Possibilities= 9-len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
if ( Possibilities < minPoss ) :
  minPoss= Possibilities
  Best= (r,c)

  if minPoss == 0 : Best=(-1,-1)
  return Best

All_digits= set((1,2,3,4,5,6,7,8,9))

def Solve(R_Cells) :
  if  R_Cells == 0 :
print("\n\n++ S o l u t i o n ++\n")
Print_Grid()
return True

  r,c= find_good_cell()
  if r < 0 : return False
  Sq_No= (r//3)*3+c//3
  
  for d in All_digits - (Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) :
# put d into Grid
Grid[(r,c)]= d
Row_Digits[r].add(d)
Col_Digits[c].add(d)
Sqr_Digits[Sq_No].add(d)

Success= Solve(R_Cells-1)

# remove d again
Grid[(r,c)]= 0
Row_Digits[r].remove(d)
Col_Digits[c].remove(d)
Sqr_Digits[Sq_No].remove(d)
  
if Success :
  Zuege.append((d,r,c))
  return True

  return False

which turns out to be as fast as the previous "dictionary only version".
Probably,  set.remove is a bit slow

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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 16:18:41 +0100, Fábio Santos wrote:

> On 5 Jul 2013 15:59, "Helmut Jarausch"  wrote:
>>
>> On Fri, 05 Jul 2013 13:44:57 +0100, Fábio Santos wrote:
>> May I suggest you avoid range and use enumerate(the_array) instead? It
>> might be faster.
>>
>> How does this work?
>>
>> Given
>>
>> Grid= [[0 for j in range(9)] for i in range(9)]
>>
>> for (r,c,val) in (Grid) :
>>
>> Helmut
> 
> for r, row_lst in enumerate(Grid):
> for c, val in enumerate(row_lst):

This is only slightly faster. I assume the creation of the temporary lists 
"row_list"
is a bit expensive.
Taking 5.4 seconds it's much slower than the current champion ( 0.79 seconds )

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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 16:38:43 +0100, Oscar Benjamin wrote:

> On 5 July 2013 16:17, Helmut Jarausch  wrote:
>>
>> I've tried the following version
>>
>> def find_good_cell() :
>>   Best= None
>>   minPoss= 10
>>   for r,c in Grid :
>> if  Grid[(r,c)] > 0 : continue
> 
> Sorry, I think what I meant was that you should have a structure
> called e.g. Remaining which is the set of all (r, c) pairs that you
> want to loop over here. Then there's no need to check on each
> iteration whether or not Grid[(r, c)] > 0. When I said "sparse" I
> meant that you don't need to set keys in Grid unless you actually have
> a value there so the test "Grid[(r, c)] > 0" would look like "(r, c)
> in Grid". Remaining is the set of all (r, c) pairs not in Grid that
> you update incrementally with .add() and .remove().
> 
> Then this
> 
>for r,c in Grid :
>  if  Grid[(r,c)] > 0 : continue
> 
> becomes
> 
> for r, c in Remaining:
> 
>> Sq_No= (r//3)*3+c//3
>> Possibilities= 9-len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
>> if ( Possibilities < minPoss ) :
>>   minPoss= Possibilities
>>   Best= (r,c)
>>
>>   if minPoss == 0 : Best=(-1,-1)
>>   return Best
>>
>> All_digits= set((1,2,3,4,5,6,7,8,9))
> 
> All_digits= set(range(1, 10))
> 
> or
> 
> All_digits = {1,2,3,4,5,6,7,8,9}
> 
>>
>> def Solve(R_Cells) :
>>   if  R_Cells == 0 :
>> print("\n\n++ S o l u t i o n ++\n")
>> Print_Grid()
>> return True
>>
>>   r,c= find_good_cell()
>>   if r < 0 : return False
>>   Sq_No= (r//3)*3+c//3
>>
>>   for d in All_digits - (Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) :
>> # put d into Grid
>> Grid[(r,c)]= d
>> Row_Digits[r].add(d)
>> Col_Digits[c].add(d)
>> Sqr_Digits[Sq_No].add(d)
>>
>> Success= Solve(R_Cells-1)
>>
>> # remove d again
>> Grid[(r,c)]= 0
>> Row_Digits[r].remove(d)
>> Col_Digits[c].remove(d)
>> Sqr_Digits[Sq_No].remove(d)
>>
>> if Success :
>>   Zuege.append((d,r,c))
>>   return True
>>
>>   return False
>>
>> which turns out to be as fast as the previous "dictionary only version".
>> Probably,  set.remove is a bit slow
> 
> No it's not and you're not using it in your innermost loops anyway.
> Probably the loop I referred to isn't your bottleneck.
> 

I've tried your suggestion, but unless I've misunderstood your suggestion it's 
a bit slower
than the solution above.

The solution above take 0.79 seconds (mean of 100 calls) while the following 
version
take 1.05 seconds (mean of 100 calls):


Grid = {(i,j):0 for i in range(9) for j in range(9)}
Remaining= {(i,j) for i in range(9) for j in range(9)}
Row_Digits = [set() for r in range(9)]
Col_Digits = [set() for c in range(9)]
Sqr_Digits = [set() for s in range(9)]

 remove some pairs from Remaining for the initial set of the given Sudoku

def find_good_cell() :
  Best= None
  minPoss= 10
  for r,c in Remaining :
Sq_No= (r//3)*3+c//3
Possibilities= 9-len(Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No])
if ( Possibilities < minPoss ) :
  minPoss= Possibilities
  Best= (r,c)

  if minPoss == 0 : Best=(-1,-1)
  return Best

All_digits= set(range(1,10))

def Solve(R_Cells) :
  if  R_Cells == 0 :
print("\n\n++ S o l u t i o n ++\n")
Print_Grid()
return True

  r,c= find_good_cell()
  if r < 0 : return False
  Sq_No= (r//3)*3+c//3
  
  for d in All_digits - (Row_Digits[r] | Col_Digits[c] | Sqr_Digits[Sq_No]) :
# put d into Grid
Grid[(r,c)]= d
Remaining.remove((r,c))
Row_Digits[r].add(d)
Col_Digits[c].add(d)
Sqr_Digits[Sq_No].add(d)

Success= Solve(R_Cells-1)

# remove d again
Grid[(r,c)]= 0
Remaining.add((r,c))
Row_Digits[r].remove(d)
Col_Digits[c].remove(d)
Sqr_Digits[Sq_No].remove(d)
  
if Success :
  Zuege.append((d,r,c))
  return True

  return False


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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 16:50:41 +, Steven D'Aprano wrote:

> On Fri, 05 Jul 2013 16:07:03 +, Helmut Jarausch wrote:
> 
>> The solution above take 0.79 seconds (mean of 100 calls) while the
>> following version take 1.05 seconds (mean of 100 calls):
> 
> 1) How are you timing the calls?

I've put the work horse "Solve" into a loop executing 100 times.
That's on an otherwise idle Linux machine.

> 
> 2) Don't use the mean, that's the wrong statistic when you are measuring 
> something where the error is always one sided. You want the minimum, not 
> the mean.

Here you assume time measuring itself is without error - I doubt that.
If the timing version, which executes function "Solve" one hundred times,
runs about 80-100 seconds without a significant variation, then taking the
mean is mathematically correct. I can't take the minimum since I don't
measure the time a single call takes.

> 
> When you measure the time taken for a piece of code, the number you get 
> is made up of two components:
> 
> 
> 1) the actual time the code would have taken, if there were no random 
> fluctuations due to other processes, etc.; and
> 
> 2) random errors due to switching to other processes, etc.
> 
> Both of these are unknown; you only know the total. But obviously the 
> random errors are always positive. They can never be negative, and you 
> can never measure a time which is less than the fastest your code could 
> run.
> 
> (If your anti-virus software starts scanning in the middle of the trial, 
> it can only make your code take more time to run, never less.)
> 
> So the only way to minimize the error is to pick the minimum time, not 
> the average. The average just gives you:
> 
>   - some unknown "true" time, plus some unknown error, somewhere
> between the smallest error and the biggest error;
> 
> whereas the minimum gives you:
> 
>   - some unknown "true" time, plus the smallest error yet seen.

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


Re: How to make this faster

2013-07-05 Thread Helmut Jarausch
On Fri, 05 Jul 2013 17:25:54 +0100, MRAB wrote:


> For comparison, here's my solution:

Your solution is very fast, indeed.
It takes 0.04 seconds (mean of 1000 runs) restoring "grid"
in between.
But that's a different algorithm which is IMHO more difficult to understand.
Many thanks,
Helmut

> 
> from collections import Counter
> 
> problem = '''
> _
> _3_85
> __1_2
> ___5_7___
> __4___1__
> _9___
> 5__73
> __2_1
> 4___9
> '''
> 
> # Build the grid.
> digits = "123456789"
> 
> grid = []
> 
> for row in problem.splitlines():
>if not row:
>  continue
> 
>new_row = []
> 
>for cell in row:
>  if cell.isdigit():
>new_row.append({cell})
>  else:
>new_row.append(set(digits))
> 
>grid.append(new_row)
> 
> # Solve the grid.
> changed = True
> while changed:
>changed = False
> 
># Look for cells that contain only one digit.
>for r in range(9):
>  for c in range(9):
>if len(grid[r][c]) == 1:
>  digit = list(grid[r][c])[0]
> 
>  # Remove from other cells in same row.
>  for c2 in range(9):
>if c2 != c and digit in grid[r][c2]:
>  grid[r][c2].remove(digit)
>  changed = True
> 
>  # Remove from other cells in same column.
>  for r2 in range(9):
>if r2 != r and digit in grid[r2][c]:
>  grid[r2][c].remove(digit)
>  changed = True
> 
>  # Remove from other cells in the same block of 9.
>  start_row = r - r % 3
>  start_column = c - c % 3
>  for r2 in range(start_row, start_row + 3):
>for c2 in range(start_column, start_column + 3):
>  if (r2, c2) != (r, c) and digit in grid[r2][c2]:
>grid[r2][c2].remove(digit)
>changed = True
> 
># Look for digits that occur in only one cell in a row.
>for r in range(9):
>  counts = Counter()
>  for c in range(9):
>counts += Counter(grid[r][c])
> 
>  unique = {digit for digit, times in counts.items() if times == 1}
> 
>  for c in range(9):
>if len(grid[r][c]) > 1 and len(grid[r][c] & unique) == 1:
>  grid[r][c] &= unique
>  changed = True
> 
># Look for digits that occur in only one cell in a column.
>for c in range(9):
>  counts = Counter()
>  for r in range(9):
>counts += Counter(grid[r][c])
> 
>  unique = {digit for digit, times in counts.items() if times == 1}
> 
>  for r in range(9):
>if len(grid[r][c]) > 1 and len(grid[r][c] & unique) == 1:
>  grid[r][c] &= unique
>  changed = True
> 
># Look for digits that occur in only one cell in a block of 9.
>for start_row in range(0, 9, 3):
>  for start_column in range(0, 9, 3):
>counts = Counter()
>for r in range(start_row, start_row + 3):
>  for c in range(start_column, start_column + 3):
>counts += Counter(grid[r][c])
> 
>unique = {digit for digit, times in counts.items() if times == 1}
> 
>for r in range(start_row, start_row + 3):
>  for c in range(start_column, start_column + 3):
>if len(grid[r][c]) > 1 and len(grid[r][c] & unique) == 1:
>  grid[r][c] &= unique
>  changed = True
> 
> # Display the solution.
> for row in grid:
>for cell in row:
>  print("".join(sorted(cell)), end=" ")
> 
>print()

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


Re: How to make this faster

2013-07-06 Thread Helmut Jarausch
On Sat, 06 Jul 2013 03:05:30 +, Steven D'Aprano wrote:

> That doesn't explain how you time it, only that you have a loop executing 
> 100 times. Are you using time.time, or time.clock? (I trust you're not 
> measuring times by hand with a stop watch.)
> 
> I expect you're probably doing something like this:
> 
> start = time.time()

I have been using  time.process_time

 
>> If the timing version, which executes function "Solve" one hundred
>> times, runs about 80-100 seconds without a significant variation, then
>> taking the mean is mathematically correct.


> For longer running code, like this, you might also like this:
> http://code.activestate.com/recipes/577896/


Thanks for the pointer.


> If the best you can say is it takes "80-100 seconds", that's pretty 
> significant variation, of the order of 20%.

That's not a variation of a SINGLE variant. One variant takes 80 seconds
and the other variant to be compared with takes 100 seconds.

> 
> In this case, with times of the order of a second per loop, it may be 
> reasonable to say "in this specific case, the error is too small to care 
> about", or "I just don't care about the error, since it will be about the 
> same for different variations of my solve function". But in that case, 
> why bother looping 100 times?
> 
> 
>> I can't take the minimum
>> since I don't measure the time a single call takes.
> 
> Then perhaps you should.

Many thanks,
Helmut
-- 
http://mail.python.org/mailman/listinfo/python-list


Procedure to request adding a module to the standard library - or initiating a vote on it

2012-08-07 Thread Helmut Jarausch
Hi,

I'd like to request adding the module

http://pypi.python.org/pypi/regex

to Python's standard library in the (near) future or to even replace the 
current 're' module by it.

Personally I'm in need for fuzzy regular expressions and I don't see how 
to do this easily and efficiently without this module.

For a long term project I also need some "guarantee" that this 
functionality will exist in future.

So, is there a (formal) procedure for such a request or for initiating 
some sort of vote on it? 

I know there is a "Benevolent Dictator" for Python.
Should I try to contact him personally?

Many thanks for a hint,

Helmut Jarausch

RWTH Aachen University
Germany

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


Re: Procedure to request adding a module to the standard library - or initiating a vote on it

2012-08-07 Thread Helmut Jarausch
On Tue, 07 Aug 2012 13:15:29 +0200, Peter Otten wrote:

> I don't think that will help. From PEP 408:
> 
> """
> As part of the same announcement, Guido explicitly accepted Matthew
> Barnett's 'regex' module [4] as a provisional addition to the standard
> library for Python 3.3 (using the 'regex' name, rather than as a drop-in
> replacement for the existing 're' module).
> """

What is  a "provisional addition"? Python-3.3 (20120708) doesn't have 
such a module.

Many thanks,
Helmut.

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


print(....,file=sys.stderr) buffered?

2012-08-13 Thread Helmut Jarausch
Hi,

for tracing purposes I have added some print outs like

print('+++ before calling foo',file=sys.stderr)
x=foo(..)
print('--- after  calling foo',

and within 'foo'
print('>>> entering foo ...',file=sys.stderr)

Now, when executing this, I always get

+++ before calling foo
--- after  calling foo
>>> entering foo ...

When outputting to stderr from C/C++ it's guaranteed that the different 
outputs appear in the same order as they have been generated.

Is this guarantee no more valid in Python 3.2 ?

Many thanks for a comment,
Helmut.

(That's a single-threaded application)

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


Re: print(....,file=sys.stderr) buffered?

2012-08-13 Thread Helmut Jarausch
On Mon, 13 Aug 2012 15:43:31 +, Grant Edwards wrote:

> On 2012-08-13, Helmut Jarausch  wrote:
>> Hi,
>>
>> for tracing purposes I have added some print outs like
>>
>> print('+++ before calling foo',file=sys.stderr)
>> x=foo(..)
>> print('--- after  calling foo',

Sorry, this is a cut'n paste error. I did use
print('--- after  calling foo',file=sys.stderr)

>>
>> and within 'foo'
>> print('>>> entering foo ...',file=sys.stderr)
>>
>> Now, when executing this, I always get
>>
>> +++ before calling foo --- after  calling foo
>>>>> entering foo ...
>>
>> When outputting to stderr from C/C++ it's guaranteed that the different
>> outputs appear in the same order as they have been generated.
> 
> You're not printing to stderr in the second print() call -- you're
> printing to stdout.  The two file objects have separate buffers and may
> even be using two different buffering modes (e.g. line vs. block).
> You can't interleave writes to stderr and stdout and assume order is
> preserved unless you take specific steps (such as forcing them both to
> be unbuffered or flushing them at certain points).
> 
>> Is this guarantee no more valid in Python 3.2 ?
> 
> If you write to stderr all three times, it should work the way you want
> it to.

It seems it doesn't do so in my case.

Thanks,
Helmut.


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


email with a non-ascii charset in Python3 ?

2012-08-15 Thread Helmut Jarausch
Hi,

I'm sorry to ask such a FAQ but still I couldn't find an answer - neither in 
the docs nor the web.

What's wrong with the following script?

Many thanks for a hint,
Helmut.

#!/usr/bin/python3
#_*_ coding: latin1 _*_

import smtplib
from email.message import Message
import datetime

msg= Message()
msg.set_charset('latin-1')
msg['Subject'] = "*** Email Test ***"
msg['From'] = "email_tes...@numa-sv.igpm.rwth-aachen.de"
msg['To']   = "jarau...@igpm.rwth-aachen.de"
msg['Date'] = datetime.datetime.utcnow().strftime('%m/%d/%Y %I:%M:%S %p')

server= smtplib.SMTP("igpm.igpm.rwth-aachen.de")
msg.set_payload("Gedanken über einen Test","iso-8859-1")

## I have tried msg.set_payload("Gedanken über einen 
Test".encode("iso-8859-1"),"iso-8859-1")
## which fails, as well

server.send_message(msg)
  Traceback (most recent call last):
File "./Test_EMail_Py3.py", line 17, in 
  server.send_message(msg)
File "/usr/lib64/python3.2/smtplib.py", line 812, in send_message
  g.flatten(msg_copy, linesep='\r\n')
File "/usr/lib64/python3.2/email/generator.py", line 91, in flatten
  self._write(msg)
File "/usr/lib64/python3.2/email/generator.py", line 137, in _write
  self._dispatch(msg)
File "/usr/lib64/python3.2/email/generator.py", line 163, in _dispatch
  meth(msg)
File "/usr/lib64/python3.2/email/generator.py", line 396, in _handle_text
  super(BytesGenerator,self)._handle_text(msg)
File "/usr/lib64/python3.2/email/generator.py", line 201, in _handle_text
  self.write(payload)
File "/usr/lib64/python3.2/email/generator.py", line 357, in write
  self._fp.write(s.encode('ascii', 'surrogateescape'))
  UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in position 
9: ordinal not in range(128)
server.quit()



This is Python 3.2.4 (GIT 20120805)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: email with a non-ascii charset in Python3 ?

2012-08-15 Thread Helmut Jarausch
On Wed, 15 Aug 2012 14:48:40 +0200, Christian Heimes wrote:

> Am 15.08.2012 14:16, schrieb Helmut Jarausch:
>> Hi,
>> 
>> I'm sorry to ask such a FAQ but still I couldn't find an answer -
>> neither in the docs nor the web.
>> 
>> What's wrong with the following script?
>> 
>> Many thanks for a hint,
>> Helmut.
>> 
>> #!/usr/bin/python3 #_*_ coding: latin1 _*_
>> 
>> import smtplib from email.message import Message import datetime
>> 
>> msg= Message()
>> msg.set_charset('latin-1')
>> msg['Subject'] = "*** Email Test ***"
>> msg['From'] = "email_tes...@numa-sv.igpm.rwth-aachen.de"
>> msg['To']   = "jarau...@igpm.rwth-aachen.de"
>> msg['Date'] = datetime.datetime.utcnow().strftime('%m/%d/%Y %I:%M:%S
>> %p')
>> 
>> server= smtplib.SMTP("igpm.igpm.rwth-aachen.de")
>> msg.set_payload("Gedanken über einen Test","iso-8859-1")
> 
> You mustn't combine set_charset() with set_payload() with a charset.
> That results into invalid output:
> 
>>>> msg = Message()
>>>> msg.set_payload("Gedanken über einen Test", "iso-8859-1")
>>>> msg.as_string()
> 'MIME-Version: 1.0\nContent-Type: text/plain;
> charset="iso-8859-1"\nContent-Transfer-Encoding:
> quoted-printable\n\nGedanken =FCber einen Test'
> 
>>>> msg2 = Message()
>>>> msg2.set_charset("iso-8859-1")
>>>> msg2.set_payload("Gedanken über einen Test", "iso-8859-1")
>>>> msg2.as_string()
> 'MIME-Version: 1.0\nContent-Type: text/plain;
> charset="iso-8859-1"\nContent-Transfer-Encoding:
> quoted-printable\n\nGedanken über einen Test'
> 


Thanks!
Just, one mustn't use 
server.send_message(msg.as_string())

But what if msg['From'] contains a non-ASCII character?

I wonder what the usage of msg.set_charset('latin-1') is.

With
msg.set_charset('latin-1') 
msg.set_payload("Gedanken über einen Test")  # is accepted BUT
server.send_message(msg)

gives
Traceback (most recent call last):
  File "Test_EMail_Py3_2.py", line 21, in 
server.send_message(msg)
  File "/usr/lib64/python3.2/smtplib.py", line 812, in send_message
g.flatten(msg_copy, linesep='\r\n')
  File "/usr/lib64/python3.2/email/generator.py", line 91, in flatten
self._write(msg)
  File "/usr/lib64/python3.2/email/generator.py", line 137, in _write
self._dispatch(msg)
  File "/usr/lib64/python3.2/email/generator.py", line 163, in _dispatch
meth(msg)
  File "/usr/lib64/python3.2/email/generator.py", line 396, in 
_handle_text
super(BytesGenerator,self)._handle_text(msg)
  File "/usr/lib64/python3.2/email/generator.py", line 201, in 
_handle_text
self.write(payload)
  File "/usr/lib64/python3.2/email/generator.py", line 357, in write
self._fp.write(s.encode('ascii', 'surrogateescape'))
UnicodeEncodeError: 'ascii' codec can't encode character '\xfc' in 
position 9: ordinal not in range(128)

Helmut.




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


Python3.3 email policy date field

2012-08-23 Thread Helmut Jarausch
Hi,

in response to a bug report I got the follow helpful comments from R. David 
Murray.
Many thanks to him. (Unfortunately, I don't know his email, so I can write him 
directly)

To generate an email (with non-ascii letters)

R. David Murray wrote:

>>> But even better, so will this:

>>> m = Message(policy=policy.SMTP)
>>> m['From'] = "Günter Weiße "




This works, but now I cannot add a date field

Trying

m['Date'] = datetime.datetime.utcnow().strftime('%m/%d/%Y %I:%M:%S %p')

I get

Traceback (most recent call last):
  File "Test_EMail_Py3_4.py", line 23, in 
msg['Date'] = datetime.datetime.utcnow().strftime('%m/%d/%Y %I:%M:%S %p')
  File "/usr/lib64/python3.3/email/message.py", line 359, in __setitem__
self._headers.append(self.policy.header_store_parse(name, val))
  File "/usr/lib64/python3.3/email/policy.py", line 119, in header_store_parse
return (name, self.header_factory(name, value))
  File "/usr/lib64/python3.3/email/headerregistry.py", line 583, in __call__
return self[name](name, value)
  File "/usr/lib64/python3.3/email/headerregistry.py", line 194, in __new__
cls.parse(value, kwds)
  File "/usr/lib64/python3.3/email/headerregistry.py", line 300, in parse
value = utils.parsedate_to_datetime(value)
  File "/usr/lib64/python3.3/email/utils.py", line 243, in parsedate_to_datetime
*dtuple, tz = __parsedate_tz(data)
TypeError: 'NoneType' object is not iterable


Many thanks for a hint,
Helmut.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python3.3 email policy date field

2012-08-23 Thread Helmut Jarausch
On Thu, 23 Aug 2012 12:36:01 +0100, MRAB wrote:
>  From what I've tried, it looks like the date can't be a string:
> 
>  >>> m['Date'] = datetime.datetime.utcnow()
>  >>> m['Date']
> 'Thu, 23 Aug 2012 11:33:20 -'

Many thanks - it's even easier!

Waiting for Python 3.3 to become standard!

Helmut.


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


exec with partial globals

2012-10-30 Thread Helmut Jarausch
Hi,

I'd like to give the user the ability to enter code which may only rebind
a given set of names but not all ones.
This does NOT work
A=1
B=2
Code=compile('A=7','','exec')
exec(Code,{'A':0})
print("I've got A={}".format(A)) # prints 1


How can 'filter' the gobal namespace such that modifying 'A' is allowed
but any attempt to modify 'B' should give an exception.


Many thanks for a hint,
Helmut.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: exec with partial globals

2012-10-30 Thread Helmut Jarausch
On Tue, 30 Oct 2012 08:33:38 -0400, Dave Angel wrote:

> On 10/30/2012 08:00 AM, Helmut Jarausch wrote:
>> Hi,
>>
>> I'd like to give the user the ability to enter code which may only rebind
>> a given set of names but not all ones.
>> This does NOT work
>> A=1
>> B=2
>> Code=compile('A=7','','exec')
>> exec(Code,{'A':0})
>> print("I've got A={}".format(A)) # prints 1
>>
>>
>> How can 'filter' the gobal namespace such that modifying 'A' is allowed
>> but any attempt to modify 'B' should give an exception.
>>
>>
>> Many thanks for a hint,
>> Helmut.
> 
> A=1
> B=2
> Code=compile('A=7','','exec')
> vars = {'A':A}
> exec(Code, vars)
> A = vars["A"]
> print("I've got A={}".format(A)) # prints 1
> 
> That now prints "I've got A=7"
> 
> More generally, you could write a loop, copying globals into vars, and
> another one, copying them back.
> 
> No idea what you're really after;  this is one of the more dangerous
> things to try.
> 
> Although you can constrain the globals seen by the code, that code can
> still use builtins, do imports, delete files, etc.
> 
> Further, if your user is clever enough, he can do:
> 
> Code=compile('A=7; print("howdy"); import __main__;
> __main__.B=42','','exec')
> 
> What are you really trying to permit him to do?  Initialize some
> variables for you?  How about an .ini file ?

Many thanks Chris, many thanks to Dave.

First, in my application only trusted people will use it.

Here my example. I'd like to update a spreadsheet by data given by another
spreadsheet. I like to allow to modify only certain columns of the first
spreadsheet. The update formula and possible conditions are entered at 
run time and the available fields are only known once the spreadsheets 
have been read.

Given spreadsheet  S (Source) and D (Destination) as objects (wrapping a 
dictionary) a possible (legal) input would be

D.price= D.price-S.discount

No other fields of 'D' should be modifiable.

Again, I don't need to take actions against a malicious user,
just take care about a typo or mistake

Thanks again,
Helmut.

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


Python3.3 str() bug?

2012-11-09 Thread Helmut Jarausch
Hi,

probably I'm missing something.

Using   str(Arg) works just fine if  Arg is a list.
But
  str([],encoding='latin-1')

gives the error
TypeError: coercing to str: need bytes, bytearray or buffer-like object, 
   list found

If this isn't a bug how can I use str(Arg,encoding='latin-1') in general.
Do I need to flatten any data structure which is normally excepted by str() ?

Many thanks for a hint,
Helmut.

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


Re: Python3.3 str() bug?

2012-11-09 Thread Helmut Jarausch
On Fri, 09 Nov 2012 10:37:11 +0100, Stefan Behnel wrote:

> Helmut Jarausch, 09.11.2012 10:18:
>> probably I'm missing something.
>> 
>> Using   str(Arg) works just fine if  Arg is a list.
>> But
>>   str([],encoding='latin-1')
>> 
>> gives the error
>> TypeError: coercing to str: need bytes, bytearray or buffer-like object, 
>>list found
>> 
>> If this isn't a bug how can I use str(Arg,encoding='latin-1') in general.
>> Do I need to flatten any data structure which is normally excepted by str() ?
> 
> Funny idea to call this a bug in Python. What your code is asking for is to
> decode the object you pass in using the "latin-1" encoding. Since a list is
> not something that is "encoded", let alone in latin-1, you get an error,
> and actually a rather clear one.
> 
> Note that this is not specific to Python3.3 or even 3.x. It's the same
> thing in Py2 when you call the equivalent unicode() function.
> 

For me it's not funny, at all.

Whenever Python3 encounters a bytestring it needs an encoding to convert it to
a string. If I feed a list of bytestrings or a list of list of bytestrings to 
'str' , etc, it should use the encoding for each bytestring component of the 
given data structure.

How can I convert a data strucure of arbitrarily complex nature, which contains
bytestrings somewhere, to a string?

This problem has arisen while converting a working Python2 script to Python3.3.
Since Python2 doesn't have bytestrings it just works.

Tell me how to convert  str(obj) from Python2 to Python3 if obj is an
arbitrarily complex data structure containing bytestrings somewhere 
which have to be converted to strings with a given encoding?

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


Re: Python3.3 str() bug?

2012-11-09 Thread Helmut Jarausch
On Fri, 09 Nov 2012 23:22:04 +1100, Chris Angelico wrote:

> On Fri, Nov 9, 2012 at 10:08 PM, Helmut Jarausch
>  wrote:
>> For me it's not funny, at all.
> 
> His description "funny" was in reference to the fact that you
> described this as a bug. This is a heavily-used mature language; bugs
> as fundamental as you imply are unlikely to exist (consequences of
> design decisions there will be, but not outright bugs, usually);
> extraordinary claims require extraordinary evidence.

Just for the record.
I first discovered a real bug with Python3 when using os.walk on a file system
containing non-ascii characters in file names.

I encountered a very strange behavior (I still would call it a bug) when trying
to put non-ascii characters in email headers.
This has only been solved satisfactorily in Python3.3. 
> 
>> Whenever Python3 encounters a bytestring it needs an encoding to convert it 
>> to
>> a string. If I feed a list of bytestrings or a list of list of bytestrings to
>> 'str' , etc, it should use the encoding for each bytestring component of the
>> given data structure.
>>
>> How can I convert a data strucure of arbitrarily complex nature, which 
>> contains
>> bytestrings somewhere, to a string?
> 
> Okay, now we're getting somewhere.
> 
> What you really should be doing is not transforming the whole
> structure, but explicitly transforming each part inside it. I
> recommend you stop fighting the language and start thinking about your
> data as either *bytes* or *characters* and using the appropriate data
> types (bytes or str) everywhere. You'll then find that it makes
> perfect sense to explicitly translate (en/decode) from one to another,
> but it doesn't make sense to encode a list in UTF-8 or decode a
> dictionary from Latin-1.
> 
>> This problem has arisen while converting a working Python2 script to 
>> Python3.3.
>> Since Python2 doesn't have bytestrings it just works.
> 
> Actually it does; it just calls them "str". And there's a Unicode
> string type, called "unicode", which is (more or less) the thing that
> Python 3 calls "str".
> 
> You may be able to do some kind of recursive cast that, in one sweep
> of your data structure, encodes all str objects into bytes using a
> given encoding (or the reverse thereof). But I don't think this is the
> best way to do things.

Thanks, but in my case the (complex) object is returned via ctypes from the 
aspell library.
I still think that a standard function in Python3 which is able to 'stringify'
objects should take an encoding parameter.

Thanks,
Helmut.

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


python3.3 - tk_setPalette bug?

2012-11-23 Thread Helmut Jarausch
Hi,

AFAIK, this should work:

import tkinter as Tk
root= Tk.Tk()
root.tk_setPalette(background = 'AntiqueWhite1', foreground = 'blue')

but python-3.3:0e4574595674+ gives

Traceback (most recent call last):
  File "Matr_Select.py", line 174, in 
root.tk_setPalette(background = 'AntiqueWhite1', foreground = 'blue')
  File "/usr/lib64/python3.3/tkinter/__init__.py", line 390, in 
tk_setPalette
+ _flatten(args) + _flatten(kw.items()))
TypeError: argument must be sequence


What am I missing?

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


email.message.Message - as_string fails

2012-12-28 Thread Helmut Jarausch
Hi,

I'm trying to filter an mbox file by removing some messages.
For that I use 
Parser= FeedParser(policy=policy.SMTP)
and 'feed' any lines to it.
If the mbox file contains a white line followed by '^From ',
I do

Msg= Parser.close()

(lateron I delete the Parser and create a new one by
Parser= FeedParser(policy=policy.SMTP)
)

I can access parts of the message by  Msg['Message-ID'], e.g.
but even for the very first message, trying to print it or convert it to a 
string
by  MsgStr=Msg.as_string(unixfrom=True)

lets Python (3.3.1_pre20121209) die with

Traceback (most recent call last):
  File "Email_Parse.py", line 35, in 
MsgStr=Msg.as_string(unixfrom=True)
  File "/usr/lib64/python3.3/email/message.py", line 151, in as_string
g.flatten(self, unixfrom=unixfrom)
  File "/usr/lib64/python3.3/email/generator.py", line 112, in flatten
self._write(msg)
  File "/usr/lib64/python3.3/email/generator.py", line 171, in _write
self._write_headers(msg)
  File "/usr/lib64/python3.3/email/generator.py", line 198, in _write_headers
self.write(self.policy.fold(h, v))
  File "/usr/lib64/python3.3/email/policy.py", line 153, in fold
return self._fold(name, value, refold_binary=True)
  File "/usr/lib64/python3.3/email/policy.py", line 176, in _fold
(len(lines[0])+len(name)+2 > maxlen or
IndexError: list index out of range


What am I missing?

Many thanks for a hint,
Helmut.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: email.message.Message - as_string fails

2012-12-29 Thread Helmut Jarausch
On Fri, 28 Dec 2012 20:57:46 -0500, Terry Reedy wrote:

> On 12/28/2012 7:22 AM, Helmut Jarausch wrote:
>> Hi,
>>
>> I'm trying to filter an mbox file by removing some messages.
>> For that I use Parser= FeedParser(policy=policy.SMTP)
>> and 'feed' any lines to it.
>> If the mbox file contains a white line followed by '^From ',
>> I do
>>
>> Msg= Parser.close()
>>
>> (lateron I delete the Parser and create a new one by Parser=
>> FeedParser(policy=policy.SMTP)
>> )
>>
>> I can access parts of the message by  Msg['Message-ID'], e.g.
>> but even for the very first message, trying to print it or convert it
>> to a string by  MsgStr=Msg.as_string(unixfrom=True)
>>
>> lets Python (3.3.1_pre20121209) die with
>>
>> Traceback (most recent call last):
>>File "Email_Parse.py", line 35, in 
>>  MsgStr=Msg.as_string(unixfrom=True)
>>File "/usr/lib64/python3.3/email/message.py", line 151, in as_string
>>  g.flatten(self, unixfrom=unixfrom)
>>File "/usr/lib64/python3.3/email/generator.py", line 112, in flatten
>>  self._write(msg)
>>File "/usr/lib64/python3.3/email/generator.py", line 171, in _write
>>  self._write_headers(msg)
>>File "/usr/lib64/python3.3/email/generator.py", line 198, in
>>_write_headers
>>  self.write(self.policy.fold(h, v))
>>File "/usr/lib64/python3.3/email/policy.py", line 153, in fold
>>  return self._fold(name, value, refold_binary=True)
>>File "/usr/lib64/python3.3/email/policy.py", line 176, in _fold
>>  (len(lines[0])+len(name)+2 > maxlen or
>> IndexError: list index out of range
> 
> The only list index visible is 0 in lines[0]. If this raises, lines is
> empty. You could trace back to see where lines is defined. I suspect it
> is all or part of the Msg you started with.
> 
> I believe that some of email was rewritten for 3.3, so it is possible
> that you found a bug based on an untrue assumption. It is also possible
> that you missed a limitation in the doc, or tripped over an intended but
> not written limitation. So I hope you do the tracing, so if doc or code
> need a fix, a tracker issue can be opened.

Thanks Terry,
I've debugged it and it smells like a bug.
I have created  http://bugs.python.org/issue16811

Helmut.

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


re: ignore case only for a part of the regex?

2012-12-30 Thread Helmut Jarausch
Hi,

is there a means to specify that 'ignore-case' should only apply to a part
of a regex?

E.g.

the regex should match  Msg-id:, Msg-Id, ...  but not  msg-id: and so on.

I've tried the pattern
r'^Msg-(?:(?i)id):'
but (?i) makes the whole pattern ignoring case.

In my simple case I could say
r'Msg-[Ii][Dd]:'
but that's a bit clumsy.

Is there a more elegant way? Is there a way to compose a pattern
from subpatterns which are compiled with different flags?

Many thanks for a hint,
Helmut.
-- 
http://mail.python.org/mailman/listinfo/python-list


numpy.genfromtxt with Python3 - howto

2012-04-06 Thread Helmut Jarausch
Hi

I have a machine with a non-UTF8 local.
I can't figure out how to make numpy.genfromtxt work

I pipe some ascii data into the following script but get this
bytes to str hell.

Many thanks for a hint,
Helmut.


#!/usr/bin/python3
import numpy as np
import io
import sys

inpstream = io.open(sys.stdin.fileno(), "r", encoding='latin1')

data = np.genfromtxt(inpstream)
'''
Traceback (most recent call last):
  File "SimpleMatInp.py", line 8, in 
data = np.genfromtxt(inpstream)
  File "/usr/lib64/python3.2/site-packages/numpy/lib/npyio.py", line 
1274, in genfromtxt
first_values = split_line(first_line)
  File "/usr/lib64/python3.2/site-packages/numpy/lib/_iotools.py", line 
206, in _delimited_splitter
line = line.split(self.comments)[0].strip(asbytes(" \r\n"))
TypeError: Can't convert 'bytes' object to str implicitly
'''
print(data)

print(data.dtype)

print(data.shape)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Is it possible to open a dbf

2005-01-03 Thread Helmut Jarausch
Miklós P wrote:
Paul Rubin wrote:

John Fabiani <[EMAIL PROTECTED]> writes:
I'm wondering if there is a module available that will open a dbf

So far (more than a minute) I have discovered a reader only.  So if you
have
a URL or a search string it would be very helpful.

TIA
John

Yes, "dBase Python" yields only some code for reading dBase ... and lots of
enquires about such a thing...
I've been using
http://www.fiby.at/dbfpy/
without any problems including writing/modifying dbf files.
--
Helmut Jarausch
Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
--
http://mail.python.org/mailman/listinfo/python-list


Re: regex question

2005-06-25 Thread Helmut Jarausch
Felix Schwarz wrote:
> Hi all,
> 
> I'm experiencing problems with a regular expression and I can't figure 
> out which words I use when googling. I read the python documentation for 
> the re module multiple times now but still no idea what I'm doing wrong.
> 
> What I want to do:
> - Extract all digits (\d) in a string.
> - Digits are separated by space (\w)
> 
> What my program does:
> - It extracts only the last digit.
> 
> Here is my program:
> import re
> line = ' 123'
> regex = '^' + '(?:\s+(\d))*' + '$'
> match = re.match(regex, line)
> print "lastindex is: ",match.lastindex
> print "matches: ",match.group(1)
> 
> 
> Obviously I do not understand how (?:\s+(\d))* works in conjunction with 
>  ^ and $.
> 

I am sure what you like to do.
What about
regex= re.compile('\s+\d')
print regex.findall(line)



-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


BaseHTTPServer and priviledge separation?

2005-06-25 Thread Helmut Jarausch
Hi,

to use a port below 1000 on a Unix system one needs root priviledges.
But it's dangerous to execute all of a script under those priviledges.
Therefore I'd like to drop the root priviledges as soon as possible.
(How) is this possible?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


extracting a heapq in a for loop - there must be more elegant solution

2013-12-03 Thread Helmut Jarausch
Hi,

I'd like to extracted elements from a heapq in a for loop.
I feel my solution below is much too complicated.
How to do it more elegantly? 
I know I could use a while loop but I don't like it.

Many thanks for some lessons in Python.

Here is my clumsy solution

from heapq import heappush, heappop
# heappop raises IndexError if heap is empty

H=[]
for N in 'H','C','W','I' :
  heappush(H,N)

# how to avoid / simplify the following function

def in_sequence(H) :
  try :
while True :
  N= heappop(H)
  yield N
  except IndexError :
raise StopIteration

# and here the application:

for N in in_sequence(H) :
  print(N)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: extracting a heapq in a for loop - there must be more elegant solution

2013-12-04 Thread Helmut Jarausch
On Tue, 03 Dec 2013 04:40:26 -0800, rusi wrote:

> On Tuesday, December 3, 2013 5:48:59 PM UTC+5:30, Helmut Jarausch wrote:
>> Hi,
>>
>> I'd like to extracted elements from a heapq in a for loop.
>> I feel my solution below is much too complicated.
>> How to do it more elegantly? 
>> I know I could use a while loop but I don't like it.
> 
> How about
> 
> def in_sequence(h):
>   for i in range(len(h)):
> yield heapq.heappop(h)
> 
> Yeah its a bit fiddly:
> 1. i needed for for but not used
> 2. The range in the for loop -- normally a python 'smell'
> 
> If python3
> 
> def ins3(h):
>yield from (heapq.heappop(h) for i in range(len(h)))

Many thanks, I'm using Python3 anyway!
Helmut
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: extracting a heapq in a for loop - there must be more elegant solution

2013-12-04 Thread Helmut Jarausch
On Tue, 03 Dec 2013 13:38:58 +0100, Peter Otten wrote:

> Helmut Jarausch wrote:
> 
>> Hi,
>> 
>> I'd like to extracted elements from a heapq in a for loop.
>> I feel my solution below is much too complicated.
>> How to do it more elegantly?
>> I know I could use a while loop but I don't like it.
>> 
>> Many thanks for some lessons in Python.
>> 
>> Here is my clumsy solution
>> 
>> from heapq import heappush, heappop
>> # heappop raises IndexError if heap is empty
>> 
>> H=[]
>> for N in 'H','C','W','I' :
>>   heappush(H,N)
> 
> H = ["H", "C", "W", "I"]
> heapq.heapify(H)
> 
> But see below.
> 
>> # how to avoid / simplify the following function
>> 
>> def in_sequence(H) :
>>   try :
>> while True :
>>   N= heappop(H)
>>   yield N
>>   except IndexError :
>> raise StopIteration
>> 
>> # and here the application:
>> 
>> for N in in_sequence(H) :
>>   print(N)
> 
> If you are iterating over the complete heap I see no advantage over a sorted 
> list. So
> 
> for N in sorted(H):
> print(N)
> 
> If H is huge use H.sort() instead of sorted() to save memory.
> If you need only a few items use heapq.nsmallest().


Many thanks!
In my real application the data which is pushed onto the heap will be extracted 
from a small file
which is executed several thousands times. So, I thought, I could keep the CPU 
a bit busy while
the OS is doing file I/O. Of course, I could have appended all these strings to 
a list which is
sorted at the end.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: extracting a heapq in a for loop - there must be more elegant solution

2013-12-04 Thread Helmut Jarausch
On Tue, 03 Dec 2013 13:06:05 +, Duncan Booth wrote:

> Helmut Jarausch  wrote:
> 
>> Hi,
>> 
>> I'd like to extracted elements from a heapq in a for loop.
>> I feel my solution below is much too complicated.
>> How to do it more elegantly? 
>> I know I could use a while loop but I don't like it.
>> 
>> Many thanks for some lessons in Python.
>> 
>> Here is my clumsy solution
>> 
>> from heapq import heappush, heappop
>> # heappop raises IndexError if heap is empty
>> 
>> H=[]
>> for N in 'H','C','W','I' :
>>   heappush(H,N)
>> 
>> # how to avoid / simplify the following function
>> 
>> def in_sequence(H) :
>>   try :
>> while True :
>>   N= heappop(H)
>>   yield N
>>   except IndexError :
>> raise StopIteration
>> 
>> # and here the application:
>> 
>> for N in in_sequence(H) :
>>   print(N)
>> 
> 
> If all you want to do is pull all of the elements out of the heap in 
> order, you would probably be better off just doing:
> 
> for N in sorted(H):
> print(N)
> 
> Heaps are mostly useful if you want only some of the elements, or if you 
> are continually producing more elements while also processing the 
> smallest ones.
> 
> However, if you really wnt to do this:
> 
> for N in iter(lambda: heappop(H) if H else None, None):
> print(N)
> 
> will work so long as H cannot contain None. If it can just replace both 
> occurences of None with some other sentinel:
> 
> sentinel = object()
> for N in iter(lambda: heappop(H) if H else sentinel, sentinel):
> print(N)
> 
> 
> Alternatively your 'in_sequence' function would look better without the 
> exception handling:
> 
> def in_sequence(H) :
> while H:
> yield heappop(H)

Many thanks!
And as noted in another reply, I try to overlap CPU time with file I/O since
I have to open / read / close a file for each line that gets pushed onto the 
heap.
Helmut
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: extracting a heapq in a for loop - there must be more elegant solution

2013-12-04 Thread Helmut Jarausch
On Tue, 03 Dec 2013 15:56:11 +0200, Jussi Piitulainen wrote:

> Helmut Jarausch writes:
> ...
>> I know I could use a while loop but I don't like it.
> ...
>> from heapq import heappush, heappop
>> # heappop raises IndexError if heap is empty
> ...
>> # how to avoid / simplify the following function
>> 
>> def in_sequence(H) :
>>   try :
>> while True :
>>   N= heappop(H)
>>   yield N
>>   except IndexError :
>> raise StopIteration
> 
> That seems equivalent to this:
> 
> def in_sequence(H):
>   while H: yield heappop(H)

Many thanks, that's something I'd hoped for.

> But I don't like the side-effect. I'd change the name to something
> that indicates the draining of the heap - heapaerobic? - or consider
> sorted(H) instead as others suggested.

Since I fill the heap once and empty it afterwards I regard the side-effect
clearly visible.

Helmut
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: extracting a heapq in a for loop - there must be more elegant solution

2013-12-04 Thread Helmut Jarausch
On Wed, 04 Dec 2013 08:13:03 +1100, Cameron Simpson wrote:

> On 03Dec2013 12:18, Helmut Jarausch  wrote:
>> I'd like to extracted elements from a heapq in a for loop.
>> I feel my solution below is much too complicated.
>> How to do it more elegantly? 
> 
> I can't believe nobody has mentioned PriorityQueue.
> 
> A PriorityQueue (from the queue module in python 3 and the Queue
> module in python 2) is essentially just a regular Queue using a
> heapq for the storage.
> 
> Example (untested):
> 
>   from Queue import PriorityQueue
> 
>   PQ = PriorityQueue()
>   for item in [1,2,3,7,6,5,9,4]:
> PQ.put(item)
> 
>   while not PQ.empty():
> item = PQ.get()
> ... do stuff with item ...
> 
> I iterate over Queues so often that I have a personal class called
> a QueueIterator which is a wrapper for a Queue or PriorityQueue
> which is iterable, and an IterablePriorityQueue factory function.
> Example:
> 
>   from cs.queues import IterablePriorityQueue
> 
>   IPQ = IterablePriorityQueue()
>   for item in [1,2,3,7,6,5,9,4]:
> IPQ.put(item)
> 
>   for item in IPQ:
> ... do stuff with item ...
> 
> Cheers,

Many thanks!

I think you QueueIterator would be a nice addition to Python's library.
Helmut
-- 
https://mail.python.org/mailman/listinfo/python-list


smart splitting - how to

2013-12-13 Thread Helmut Jarausch
Hi,

I'd like to read several strings by using 'input'. 
These strings are separated by white space but I'd like to allow for
some quoting, e.g.

"Guido van" Rossum

should be split into 2 strings only

Now, a simple split doesn't work since it splits the quoted text as well.
Is there a simple way to do so?
It would be nice if it could handle embedded quotes which are escaped 
by a backslash, too.

Is there something simpler then a sophisticated regular expression 
or even a parser?

Many thanks for a hint,
Helmut
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: smart splitting - how to

2013-12-13 Thread Helmut Jarausch
On Fri, 13 Dec 2013 11:39:57 +, Chris Angelico and Robert Kern wrote:

> On 2013-12-13 11:28, Helmut Jarausch wrote:
>> Hi,
>>
>> I'd like to read several strings by using 'input'.
>> These strings are separated by white space but I'd like to allow for
>> some quoting, e.g.
>>
>> "Guido van" Rossum
>>
>> should be split into 2 strings only
>>
>> Now, a simple split doesn't work since it splits the quoted text as well.
>> Is there a simple way to do so?
>> It would be nice if it could handle embedded quotes which are escaped
>> by a backslash, too.
>>
>> Is there something simpler then a sophisticated regular expression
>> or even a parser?
> 
> http://docs.python.org/3.3/library/shlex
> 
> 
> [~]
> |1> import shlex
> 
> [~]
> |2> shlex.split(r'"Guido \"van\" Rossum" invented Python')
> ['Guido "van" Rossum', 'invented', 'Python']

Many thanks, that works perfectly and is so simple.
Helmut


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


Python3 - temporarily change the file encoding

2014-03-21 Thread Helmut Jarausch
Hi,

my locale is en_US.iso88591

But now I'd like to process a restructuredtext file which is encoded in utf-8.

rst2html has

#!/usr/bin/python3.3

# $Id: rst2html.py 4564 2006-05-21 20:44:42Z wiemann $
# Author: David Goodger 
# Copyright: This module has been placed in the public domain.

"""
A minimal front end to the Docutils Publisher, producing HTML.
"""

try:
import locale
locale.setlocale(locale.LC_ALL, '')
except:
pass

from docutils.core import publish_cmdline, default_description


description = ('Generates (X)HTML documents from standalone reStructuredText '
   'sources.  ' + default_description)

publish_cmdline(writer_name='html', description=description)

--

Even if I comment out the part containing 'import locale' the utf-8 encoding of 
my
rst-file is not recognized.

How can I change this so that rst2html use utf-8 when reading and writing files.
Of course, I don't want to change the docutils package for that.

Many thanks for a hint,
Helmut
-- 
https://mail.python.org/mailman/listinfo/python-list


__iadd__ for a subclass of array - howto

2013-08-05 Thread Helmut Jarausch
Hi,

I'd like to subclass array.array and implement operators like __iadd__
How can this be accomplished.

I'tried

from array import array

class Vec(array) :
  def __new__(cls,Vinit) :
return array.__new__(cls,'d',Vinit)

  def __init__(self,*args) :
self.N = len(self)

  def __str__(self) :
out=[ "{}".format(self.__getitem__(i)) for i in range(0,self.N) ]
return ",".join(out)

  def __iadd__(self,Op) :
#for i,x in enumerate(self) :
#  x+= Op[i] # this doesn't update self
for i in range(0,self.N) :
  (self.__getitem__(i))+= Op[i]  # __getitem__ doesn't return a "reference" 
(like in C++)
return self


Many thanks for a hint,
Helmut
-- 
http://mail.python.org/mailman/listinfo/python-list


Python3 exec locals - this must be a FAQ

2013-02-12 Thread Helmut Jarausch
Hi,

I've tried but didn't find an answer on the net.

The exec function in Python modifies a copy of locals() only.
How can I transfer changes in that local copy to the locals of my function
** without **  knowing the names of these variables.

E.g.  I have a lot of local names.

Doing

_locals= locals()
expr=compile(input('statements assigning to some local variables '),
 'user input','exec')
exec(expr,globals(),_locals)

How can I "copy" the new values within _locals to my current locals.

If I knew that  Var1  has changed I could say
Var1 = _locals['Var1'] but what to do in general?

Many thanks for a hint,
Helmut.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python3 exec locals - this must be a FAQ

2013-02-12 Thread Helmut Jarausch
On Tue, 12 Feb 2013 08:27:41 -0500, Dave Angel wrote:

> On 02/12/2013 06:46 AM, Helmut Jarausch wrote:
>> Hi,
>>
>> I've tried but didn't find an answer on the net.
>>
>> The exec function in Python modifies a copy of locals() only.
>> How can I transfer changes in that local copy to the locals of my
>> function ** without **  knowing the names of these variables.
>>
>> E.g.  I have a lot of local names.
>>
>> Doing
>>
>> _locals= locals()
> 
> This doesn't copy everything.  But perhaps you know that and you're just
> testing us.

No, I didn't know. And I'm bit surprised since this is recommend
several times, e.g. in "Python Essential Reference, 4th ed" by
David Beazley.

> 
>> expr=compile(input('statements assigning to some local variables '),
>>   'user input','exec')
>> exec(expr,globals(),_locals)
>>
>> How can I "copy" the new values within _locals to my current locals.
>>
>> If I knew that  Var1  has changed I could say Var1 = _locals['Var1']
>> but what to do in general?
> 
> locals()["Var1"] = _locals["Var1"]  will set the same Var1 local.

Thanks for this hint which surprises me again since I thought
locals() by itself is a copy only.

> 
> So you might write a loop on _locals.
> 
> But beware if someone has deleted one of the "variables" it may not do
> what you'd like.  You cannot necessarily add back a local with the above
> syntax.

Does this mean that adding something completely new won't work?

Many thanks,
Helmut.

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


raw format string in string format method?

2013-02-28 Thread Helmut Jarausch
Hi,

I'd like to print a string with the string format method which uses
{0}, ...

Unfortunately, the string contains TeX commands which use lots of
braces. Therefore I would have to double all these braces just for the
format method which makes the string hardly readable.

Is there anything like a "raw" format string and any other means
to circumvent this?

Many thanks for a hint,
Helmut.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: raw format string in string format method?

2013-02-28 Thread Helmut Jarausch
On Fri, 01 Mar 2013 01:22:48 +1100, Chris Angelico wrote:

> On Fri, Mar 1, 2013 at 1:11 AM, Helmut Jarausch 
> wrote:
>> Hi,
>>
>> I'd like to print a string with the string format method which uses
>> {0}, ...
>>
>> Unfortunately, the string contains TeX commands which use lots of
>> braces. Therefore I would have to double all these braces just for the
>> format method which makes the string hardly readable.
>>
>> Is there anything like a "raw" format string and any other means to
>> circumvent this?
> 
> You could use a different string formatting function, such as
> percent-formatting:
> 
> "Hello, {0}, this is %s" % some_string
> 
> The {0} will be output literally, and the %s will be replaced by the
> string. Braces are ignored, percent signs are significant.
> 
> ChrisA

Originally I had used percent-formatting
But isn't  it deprecated in Python 3.X ?

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


new.instancemethod - how to port to Python3

2013-04-07 Thread Helmut Jarausch
Hi,

I'm trying to port a class to Python3.3 which contains

class  Foo :

def to_binary(self, *varargs, **keys):
   


self.to_binary = new.instancemethod(to_binary, self, self.__class__) 

# Finally call it manually
return apply(self.to_binary, varargs, keys)



The last line has been transformed to 
return self.to_binary(*varargs, **keys)

by  2to3

But how to transform the line with new.instancemethod.
I've seen examples where
new.instancemethod(to_binary, )
is replaced by  to_binay
but this doesn't work here since to_binary isn't known.

If I simply delete it, I get an infinite recursion.

So, what's a working transcript of this code?

Many thanks for a hint,
Helmut.

P.S.  Is there collection of examples of necessary transformations to
Python3 which are not managed by 2to3 ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: new.instancemethod - how to port to Python3

2013-04-07 Thread Helmut Jarausch
On Sun, 07 Apr 2013 11:41:46 +0100, Arnaud Delobelle wrote:

> On 7 April 2013 10:50, Helmut Jarausch  wrote:
>> Hi,
>>
>> I'm trying to port a class to Python3.3 which contains
>>
>> class  Foo :
>> 
>> def to_binary(self, *varargs, **keys):
>>
>>
>> 
>> self.to_binary = new.instancemethod(to_binary, self,
>> self.__class__)
> 
> `self` isn't bound in this lexical scope (the class scope).  Or is your
> indentation incorrect?  However, if this statement was within the
> `to_binary` method, then `to_binary` wouldn't be bound so I can't make a
> reasonable guess.
> 
>> # Finally call it manually return apply(self.to_binary, varargs,
>> keys)
>>
> [...]

Sorry, the excerpt wasn't complete (it's not my code):
Here a more complete excerpt:

class  Foo :

def to_binary(self, *varargs, **keys):
   
   code= ...
   args= ...
   # Add function header
   code = 'def to_binary(self, %s):\n' % ', '.join(args) + code
   exec(code)
   self.to_binary = new.instancemethod(to_binary, self, self.__class__)
   return self.to_binary(*varargs, **keys)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: new.instancemethod - how to port to Python3

2013-04-07 Thread Helmut Jarausch
On Sun, 07 Apr 2013 11:07:07 +, Steven D'Aprano wrote:

> On Sun, 07 Apr 2013 10:54:46 +, Helmut Jarausch wrote:
> 
>> class  Foo :
>> 
>> def to_binary(self, *varargs, **keys):
>>
>>code= ...
>>args= ...
>># Add function header
>>code = 'def to_binary(self, %s):\n' % ', '.join(args) + code
>>exec(code)
>>self.to_binary = new.instancemethod(to_binary, self,
>>self.__class__)
>>return self.to_binary(*varargs, **keys)
> 
> 
> Self-modifying code! Yuck!
> 
> Nevertheless, try changing the line with new.instancemethod to this:
> 
> self.to_binary = types.MethodType(to_binary, self)
> 
> 
> and see if it works. If it complains about the call to exec, then change 
> that part of the code to this:

Thanks, that worked
> 
> ns = {}
> exec(code, ns)
I just had to replace this with
  exec(code,globals(),ns)

> to_binary = ns['to_binary']
> self.to_binary = types.MethodType(to_binary, self)

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


Re: new.instancemethod - how to port to Python3

2013-04-07 Thread Helmut Jarausch
On Sun, 07 Apr 2013 10:52:11 +, Steven D'Aprano wrote:

> On Sun, 07 Apr 2013 09:50:35 +, Helmut Jarausch wrote:
> 
>> Hi,
>> 
>> I'm trying to port a class to Python3.3 which contains
>> 
>> class  Foo :
>> 
>> def to_binary(self, *varargs, **keys):
>>
>> 
>> self.to_binary = new.instancemethod(to_binary, self, self.__class__)
>> # Finally call it manually
>> return apply(self.to_binary, varargs, keys)
> 
> 
> I do not understand this code. Can you give a short example that actually 
> works please?
> 
> As written, your code has a class that defines a to_binary method. Then, 
> *outside* of the method, in the class definition, you refer to "self", 
> and use a return statement. This is not possible -- self does not exist, 
> and return gives a SyntaxError.
> 
> But, if the indentation is wrong, it looks like you are trying to get the 
> to_binary method to replace itself with a global to_binary function. I do 
> not understand this.

Sorry, the first excerpt was to too short.
As noted in the reply to Arnaud, a better excerpt is:

class  Foo :

def to_binary(self, *varargs, **keys):
   
   code= ...
   args= ...
   # Add function header
   code = 'def to_binary(self, %s):\n' % ', '.join(args) + code
   exec(code)
   self.to_binary = new.instancemethod(to_binary, self, self.__class__)
   return self.to_binary(*varargs, **keys)

If you'd like to see the full code (not by me)
please see the Python package http://python-xlib.sourceforge.net/
file Xlib/protocol/rq.py   method to_binary in class Struct
> 
> I cannot answer your question, because I don't understand it. But perhaps 
> this will help:
> 
> 
> # === Python 2 version ===
> class Spam:
> pass
> 
> x = Spam()
> 
> def method(self, arg):
> return {arg: self}
> 
> import new
> x.method = new.instancemethod(method, x, x.__class__)
> 
> x.method("hello")
> => returns {'hello': <__main__.Spam instance at 0xa18bb4c>}
> 
> 
> Here is the Python 3 version:
> 
> 
> # === Python 3 ===
> class Spam:
> pass
> 
> x = Spam()
> 
> def method(self, arg):
> return {arg: self}
> 
> import types
> x.method = types.MethodType(method, x)
> 
> x.method("hello")
> => returns {'hello': <__main__.Spam object at 0xb7bc59ac>}
> 
> 
> 
> Does this help?

Yes, 

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


NNTPlib::xover problem

2006-08-07 Thread Helmut Jarausch
Hi

I try to regularly extract recent news from some newsgroups.
If News is an NNTP object I try
(Response,Articles)= News.xover(str(int(Last)+1),'1000')
where 'Last' is the (previously saved) number of the last
article read.
If there are no new articles I get an Exception

Traceback (most recent call last):
   File "/home/jarausch/Python_My/News", line 36, in -toplevel-
 (Response,Articles)= News.xover(str(int(Last)+1),'1000')
   File "/usr/local/lib/python2.4/nntplib.py", line 479, in xover
 resp, lines = self.longcmd('XOVER ' + start + '-' + end, file)
   File "/usr/local/lib/python2.4/nntplib.py", line 265, in longcmd
 return self.getlongresp(file)
   File "/usr/local/lib/python2.4/nntplib.py", line 236, in getlongresp
 resp = self.getresp()
   File "/usr/local/lib/python2.4/nntplib.py", line 219, in getresp
 raise NNTPTemporaryError(resp)
NNTPTemporaryError: 420 No such article


I would have expected to get an empty 'Response' or the value None
for 'Articles'.

What am I missing?

(This is Python 2.4.3)

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: NNTPlib::xover problem

2006-08-07 Thread Helmut Jarausch
Michiel Sikma wrote:
> Hi Helmut,
> 
> I guess it simply raises an exception in case there are no articles; 
> this may not be what you expected, but it would seem that this is the 
> way it operates. You should try catching the exception to plan out a 
> course of action in case no articles are present.

Thanks,

though the name of the exception 'nntplib.NNTPTemporaryError'
sound 'temporary'

Helmut.


> Op 7-aug-2006, om 12:50 heeft Helmut Jarausch het volgende geschreven:
> 
>> Hi
>>
>> I try to regularly extract recent news from some newsgroups.
>> If News is an NNTP object I try
>> (Response,Articles)= News.xover(str(int(Last)+1),'1000')
>> where 'Last' is the (previously saved) number of the last
>> article read.
>> If there are no new articles I get an Exception
>>
>> Traceback (most recent call last):
>>File "/home/jarausch/Python_My/News", line 36, in -toplevel-
>>  (Response,Articles)= News.xover(str(int(Last)+1),'1000')
>>File "/usr/local/lib/python2.4/nntplib.py", line 479, in xover
>>  resp, lines = self.longcmd('XOVER ' + start + '-' + end, file)
>>File "/usr/local/lib/python2.4/nntplib.py", line 265, in longcmd
>>  return self.getlongresp(file)
>>File "/usr/local/lib/python2.4/nntplib.py", line 236, in getlongresp
>>  resp = self.getresp()
>>File "/usr/local/lib/python2.4/nntplib.py", line 219, in getresp
>>  raise NNTPTemporaryError(resp)
>> NNTPTemporaryError: 420 No such article
>>
>>
>> I would have expected to get an empty 'Response' or the value None
>> for 'Articles'.
>>
>> What am I missing?
>>
>> (This is Python 2.4.3)
>>
>> Many thanks for a hint,
>>
>> Helmut Jarausch
>>
>> Lehrstuhl fuer Numerische Mathematik
>> RWTH - Aachen University
>> D 52056 Aachen, Germany
>> --http://mail.python.org/mailman/listinfo/python-list
> 


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: hide python code !

2006-08-11 Thread Helmut Jarausch
John Machin wrote:
> Bayazee wrote:
>> hi
>> can we hide a python code ?
>> if i want to write a commercial software can i hide my source code from
>  [1]
>> users access ?
>> we can conver it to pyc but this file can decompiled ... so ...!!
>> do you have any idea about this ...?
>>
>> ---
>> First Iranian Open Source Community : www.python.ir
> ^^[2]
> 
> 
> [1] and [2] don't seem to be compatible.

I suppose all of you who have commented about this, are sitting in the
 >>> free world <<< .
But there are countries (like .ir) where the government has totally different
ideas of 'freedom'. So taking the freedom to write something can be very
dangerous at times. Fortunately most of those guys which intercept every
email and check every web server are not so smart to reverse engineer
everything in a short time since they have to check thousands of pieces of
information each day. Let's make their work a bit harder!
-- 
http://mail.python.org/mailman/listinfo/python-list


Python in a nutshell - new edition ?

2006-06-28 Thread Helmut Jarausch
Hi,

is there a new edition of "Python in a Nutshell"
covering Python 2.5  coming soon?

Many thanks,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


How to terminate a main script?

2006-07-11 Thread Helmut Jarausch
Hi,

I'm still looking for an elegant and clear means to
terminate the main script in Python.

Unfortunately, Python doesn't allow a 'return'
instruction in the main script.

Using sys.exit(0) produces an error
message which looks dangerous to an
uninitiated user.

The same is true for an exception.

And setting a 'flag' and testing
at several place for 'fall through'
is ugly and error-prone.

So what is a good choice?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to terminate a main script?

2006-07-11 Thread Helmut Jarausch
Fredrik Lundh wrote:
> Helmut Jarausch wrote:
> 
>> Using sys.exit(0) produces an error
>> message which looks dangerous to an
>> uninitiated user.
> 
> sys.exit(0) doesn't print anything at all.

Yes, sorry, I was trying in in 'idle'
There you get
Traceback (most recent call last):
   File "", line 1, in -toplevel-
 sys.exit(0)
SystemExit: 0


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


upgrade 2.4 -> 2.5 HowTo

2006-09-04 Thread Helmut Jarausch
Hi,

what has to be done for upgrading from Python 2.4 to 2.5?

- How can I find out which packages (in addition to the core packages) have been
   installed up to now
- Can I just copy   /usr/local/lib/python2.4/site-packages  to
 /usr/local/lib/python2.5 ?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: upgrade 2.4 -> 2.5 HowTo

2006-09-04 Thread Helmut Jarausch
Sibylle Koczian wrote:
> Helmut Jarausch schrieb:
>> Hi,
>>
>> what has to be done for upgrading from Python 2.4 to 2.5?
>>
>> - How can I find out which packages (in addition to the core packages)
>> have been
>>   installed up to now
>> - Can I just copy   /usr/local/lib/python2.4/site-packages  to
>> /usr/local/lib/python2.5 ?
>>
>> Many thanks for a hint,
>>
> 
> Which OS?
> 
> Windows: control panel / software will list all python packages which
> came with separate installers.
> 
> Linux: the package manager of your distribution should tell you what's
> installed.
> 

It's Linux.
Thanks, but I don't have a package manager. I'm working with Linux-From-Scratch.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Test for number?

2006-09-04 Thread Helmut Jarausch
Tim Williams wrote:
> On 04/09/06, Dr. Pastor <[EMAIL PROTECTED]> wrote:
>> In the following code I would like to ascertain
>> that x has/is a number. What the simplest TEST should be?
>> (Could not find good example yet.)
>> ---
>> x=raw_input('\nType a number from 1 to 20')
>> if TEST :
>> Do_A
>> else:
>> Do_B
>> ---
> 
> Something simple like the following might help,
> 
>>>> def numtest():
> ... x=raw_input('\nType a number from 1 to 20')
> ... try:
> ... x=int(x)
> ... print x, "is a number between 1 & 20 "
> ... except:
> ... print x, "is not a number"
> ...
>>>> numtest()   # enter 1
> 1 is a number between 1 & 20
>>>> numtest()  #  enter "f"
> f is not a number
> 
> its not a final solution though,  think input = -2, 5.5  or 21
> 

One step further

try:
eval(x+'0')



-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mutable numeric type

2007-01-02 Thread Helmut Jarausch
[EMAIL PROTECTED] wrote:
> Way to go.
> Try doing this.
> x = MutableNumeric(42)
   ^^
where is this defined?

> y = x
> x += 42
> print y
> 


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


python2.5 frameobject - how to upgrade?

2007-01-02 Thread Helmut Jarausch
Hi,

I'd like to install a package ('rekall') which uses
frame->f_nlocals
which is no longer contained in frameobject.h

What's the recommended way to upgrade such an application?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python2.5 frameobject - how to upgrade?

2007-01-02 Thread Helmut Jarausch
[EMAIL PROTECTED] wrote:
> Helmut> I'd like to install a package ('rekall') which uses
> Helmut> frame->f_nlocals which is no longer contained in frameobject.h.
> 
> Helmut> What's the recommended way to upgrade such an application?
> 
> I suspect PySequence_Length(frame->f_locals) will do the trick.

Yes, thanks, that fixed it,
Happy New Year to you.

A personal question: Have you converted from Perl to Python, as well?

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: File processing - is Python suitable?

2007-06-19 Thread Helmut Jarausch
ferrad wrote:
> I have not used Python before, but believe it may be what I need.
> 
> I have large text files containing text, numbers, and junk.  I want to
> delete large chunks process other bits, etc, much like I'd do in an
> editor, but want to do it automatically.  I have a set of generic
> rules that my fingers follow to process these files, which all follow
> a similar template.
> 
> Question: can I translate these types of rules into programmatical
> constructs that Python can use to process these files?  Can Python do
> the trick?

I think that's one of the great strength of Python.

Just some pointers

  http://gnosis.cx/TPiP/
  http://www.egenix.com/products/python/mxBase/mxTextTools/


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


restructuredtext latin1 encoding (FAQ?)

2007-07-03 Thread Helmut Jarausch
Hi,

I did try to find something on the net but I couldn't find anything useful.

Is there a way to write documents with the reST tools using the Latin1
encoding?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: restructuredtext latin1 encoding (FAQ?)

2007-07-03 Thread Helmut Jarausch
Marc 'BlackJack' Rintsch wrote:
> On Tue, 03 Jul 2007 12:12:04 +0200, Helmut Jarausch wrote:
> 
>> Is there a way to write documents with the reST tools using the Latin1
>> encoding?
> 
> Yes of course there is.  Just write the documents in Latin-1 encoding. 
> Take a look at the charset command line options if Latin-1 is not the
> default encoding used by your operating system.
> 

Many thanks!

Is there also a way to tell it  dugui.py ?

Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


wxPython Cannot convert from the charset 'latin-1'

2007-07-03 Thread Helmut Jarausch
Hi,

I'm trying to teach  "dugui.py" (a tiny GUI for restructuredtext written in 
wxPython) to accept files in isolatin-1 encoding.  It displays e.g. German 
umlauts correctly but then I get a popup window saying
Python Error
Cannot convert from the charset 'latin-1'!

How can I find out where this did come from. Running it under pdb
isn't helpful either.

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


ReSTedit ported to Linux?

2007-07-04 Thread Helmut Jarausch
Hi,
does anybody know if ReSTedit (originally for Mac OS X) has been ported to 
Linux?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple search and Display system, no need for db?

2007-07-11 Thread Helmut Jarausch
flit wrote:
> Hello All,
> 
> I was discussing with a friend and we get in a hot debate.
> 
> We have homepage that act like a name list.
> 
> The user goes there:
> 
> - Choose the building -- Department and the user receive an html table
> with all names .
> 
> So the user gives two inputs and receive the names.
> 
> Currently we are using this in mysql, This is the reason of the
> debate.
> 
> If our list will never change and we are supposing that will be true
> for a long time, is it not too much use an mysql?
> 
> Which kind of structure can we use in python? in web?
> 

What about using a simple dictionary in Python. You can use the module
pickle (or cpickle) to dump it to disk and load it next time.
Furthermore you can easily write the whole web server in Python,
e.g. I like http://karrigell.sourceforge.net/

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pattern match !

2007-07-11 Thread Helmut Jarausch
[EMAIL PROTECTED] wrote:
> Extract the application name with version from an RPM string like
> hpsmh-1.1.1.2-0-RHEL3-Linux.RPM, i require to extract hpsmh-1.1.1.2-0
> from above string. Sometimes the RPM string may be hpsmh-1.1.1.2-RHEL3-
> Linux.RPM.
> 

Have a try with

import re
P=re.compile(r'(\w+(?:[-.]\d+)+)-RHEL3-Linux\.RPM')
S="hpsmh-1.1.1.2-0-RHEL3-Linux.RPM"
PO= P.match(S)
if  PO :
   print PO.group(1)



-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


split a string of space separated substrings - elegant solution?

2007-07-31 Thread Helmut Jarausch
Hi,

I'm looking for an elegant solution to the following (quite common)
problem:

Given a string of substrings separated by white space,
split this into tuple/list of elements.
The problem are quoted substrings like

abc "xy z"  "1 2 3"  "a \" x"

should be split into  ('abc','xy z','1 2 3','a " x')

For that, one probably has to protect white space between
quotes, then split by white space and finally converted the
'protected white space' to normal white space again.
Is there an elegant solution - perhaps without using a lexer
and something else. With regular expressions alone it seems
clumsy.

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: split a string of space separated substrings - elegant solution?

2007-08-01 Thread Helmut Jarausch
Many thanks to all of you!
It's amazing how many elegant solutions there are in Python.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


decorators - more than just syntactic sugar

2007-08-11 Thread Helmut Jarausch
Hi,

are decorators more than just syntactic sugar in python 2.x and what
about python 3k ?
How can I find out the predefined decorators?

Many thanks for your help,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Drawing a graph

2007-08-13 Thread Helmut Jarausch
Ghirai wrote:
> Hello list,
> 
> I need to draw a graph, 2 axes, 2D, nothing fancy.
> One of the axes is time, the other one is a series of integers.
> 
> I don't care much about the output format.
> 
> Are there any specialized libraries for this, or should i use PIL?
> 

What about Gnuplot.py
http://gnuplot-py.sourceforge.net/


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


sendmail a long message

2007-11-21 Thread Helmut Jarausch
Hi,

to send a possibly long email I have seen a solution
which does os.popen to an external sendmail program and
then writes the message into that pipe.

I wonder if it possible to use smtplib.SMTP.sendmail
but this requires building the complete body as one long
string in memory before calling this.

Is there an alternative solution, e.g. where
smtplib.SMTP.sendmail calls a generator.

Many thanks for a hint,


Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sendmail a long message

2007-11-21 Thread Helmut Jarausch
Laszlo Nagy wrote:
> Helmut Jarausch wrote:
>> Hi,
>>
>> to send a possibly long email I have seen a solution
>> which does os.popen to an external sendmail program and
>> then writes the message into that pipe.
>>
>> I wonder if it possible to use smtplib.SMTP.sendmail
>> but this requires building the complete body as one long
>> string in memory before calling this.
>>
>> Is there an alternative solution, e.g. where
>> smtplib.SMTP.sendmail calls a generator.
>>   
> using socket.connect, and feed the message body into it in smaller chunks.
> 
> Of course, you need to create the message body first, but it was not 
> your question - I suppose you already have the message stored on disk?
> 

Thanks,
it's a mail filter, the message is read in smaller chunks from sys.stdin .
Helmut.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


asynchronous timer events - how to?

2008-01-09 Thread Helmut Jarausch
Hi,

I'm using a web server (Karrigell) which is based on the asyncore module.
I'd like to be able to checkpoint some data (e.g. pickled dictionaries) to disk
from time to time.
For that I would need to setup a timer which calls a Python object/function 
when 
its time interval has expired. While this function is running I need access to 
the variables of the server.

Can this be done in a simple way?

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: asynchronous timer events - how to?

2008-01-09 Thread Helmut Jarausch
Fredrik Lundh wrote:
> Helmut Jarausch wrote:
> 
>> I'm using a web server (Karrigell) which is based on the asyncore module.
>> I'd like to be able to checkpoint some data (e.g. pickled 
>> dictionaries) to disk
>> from time to time.
>> For that I would need to setup a timer which calls a Python 
>> object/function when its time interval has expired. While this 
>> function is running I need access to the variables of the server.
> 
> the usual way to do that with asyncore is to add an outer control loop 
> that calls asyncore.loop with a count argument (or poll directly), and 
> checks a "task queue" at regular intervals.
> 
> but in your case, it's probably easier to set up a cron job that does
> a "wget" against your server with a "secret" URL, and have the server do 
> the checkpointing whenever that URL is fetched.

Thanks for this ingenious idea,
Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


module finalizer - is there such a beast?

2008-01-11 Thread Helmut Jarausch
Hi,

when a module gets imported the statements not contained in
function definitions or classes are executed.
This can be thought of an initializer for the module.

But how can I get control when the module gets unloaded
either by Python's gc, Python's exit or by a module reload.

Many thanks for a hint,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


common problem - elegant solution sought

2008-01-15 Thread Helmut Jarausch
Hi,

I'm looking for an elegant solution of the following tiny but common problem.

I have a list of tuples  (Unique_ID,Date) both of which are strings.
I want to delete the tuple (element) with a given Unique_ID, but
I don't known the corresponding Date.

My straight forward solution is a bit lengthy, e.g.

L=[("a","070501"),("b","080115"),("c","071231")]
pos=-1
found=-1
for (Key,Date) in L :
 pos+= 1
 if  Key == "b" :
 found= pos
 break

if  found >= 0 :
 del L[found]

print L

Most probably there are much more elegant solutions.
Unfortunately, the index-list-method doesn't take an
additional function argument for the comparisons.

Many thanks for your hints,

Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: common problem - elegant solution sought

2008-01-15 Thread Helmut Jarausch
Thanks to you all for your help.

The solution to regenerate the list skipping
the one to be deleted is fine for me since my
lists are of moderate size and the operation
is infrequent.

Helmut.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: common problem - elegant solution sought

2008-01-15 Thread Helmut Jarausch
Neil Cerutti wrote:
> On Jan 15, 2008 5:33 AM, Helmut Jarausch <[EMAIL PROTECTED]> wrote:
>> Hi,
>>
>> I'm looking for an elegant solution of the following tiny but common problem.
>>
>> I have a list of tuples  (Unique_ID,Date) both of which are strings.
>> I want to delete the tuple (element) with a given Unique_ID, but
>> I don't known the corresponding Date.
>>
>> My straight forward solution is a bit lengthy, e.g.
>>
>> L=[("a","070501"),("b","080115"),("c","071231")]
> 
> If the data is truly sorted by Unique_ID, then binary search may be
> feasible (though not actually recommended for such small list).
> 
> import bisect
> 
> i = bisect.bisect_left(L, ('b', '00'))
> if i[0] == 'b':
> del L[i]
> 

Thanks,
unfortunately, they are sorted on 'Date'
Helmut.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Benchmark [was Re: common problem - elegant solution sought]

2008-01-15 Thread Helmut Jarausch
Again, many thanks to all who provide their solution.
I have timed these (though on my old P3(0.9GHz)) - see below
Helmut.

Helmut Jarausch wrote:
> Hi,
> 
> I'm looking for an elegant solution of the following tiny but common 
> problem.
> 
> I have a list of tuples  (Unique_ID,Date) both of which are strings.
> I want to delete the tuple (element) with a given Unique_ID, but
> I don't known the corresponding Date.
> 

#!/usr/bin/python

import random
import timeit

Lorg=[]

def genList(L) :
   for f in range(ord('A'),ord('z')+1) :
 for s in range(ord('A'),ord('z')+1) :
   L.append((chr(f)+chr(s),str(random.randrange(0,100

genList(Lorg)
Times= 1000
T0= timeit.Timer('L=list(Lorg)','from __main__ import Lorg').timeit(Times)
print T0

SetUp1=r'''from __main__ import Lorg
def del_by_key(L,key) :
   d= dict(L)
   del d[key]
   L[:]= d.items()
'''

SetUp2=r'''from __main__ import Lorg
def del_by_key(L,key) :
   d= dict(L)
   t= (key,d[key])
   L.remove(t)
'''

SetUp3=r'''from __main__ import Lorg
def del_by_key(L,key) :
   index= [k for k,val in L]
   pos  = index.index(key)
   del L[pos]
'''

SetUp4=r'''from __main__ import Lorg
def del_by_key(L,key) :
   for pos, (k,d) in enumerate(L):
 if  k == key :
   del L[pos]
   break
'''

SetUp5=r'''from __main__ import Lorg
def del_by_key(L,key) :
   L[:]= [(k,d) for (k,d) in L if k !=key]
'''

SetUp6=r'''from __main__ import Lorg
class Tester(object) :
   def __init__(self,key) :
 self.key= key
   def __eq__(self,other) :
 return other[0] == self.key

def del_by_key(L,key) :
   del L[L.index(Tester(key))]
'''

print '*** ready ***'


T= timeit.Timer("L=list(Lorg);del_by_key(L,'Zz')",SetUp1).timeit(Times)
print "Method 1 :",T-T0

T= timeit.Timer("L=list(Lorg);del_by_key(L,'Zz')",SetUp2).timeit(Times)
print "Method 2 :",T-T0

T= timeit.Timer("L=list(Lorg);del_by_key(L,'Zz')",SetUp3).timeit(Times)
print "Method 3 :",T-T0

T= timeit.Timer("L=list(Lorg);del_by_key(L,'Zz')",SetUp4).timeit(Times)
print "Method 4 :",T-T0

T= timeit.Timer("L=list(Lorg);del_by_key(L,'Zz')",SetUp5).timeit(Times)
print "Method 5 :",T-T0

T= timeit.Timer("L=list(Lorg);del_by_key(L,'Zz')",SetUp6).timeit(Times)
print "Method 6 :",T-T0

# Results on an old P3 (0.9 GHz)
# *** ready ***
# Method 1 : 10.9850928783
# Method 2 : 5.96455168724
# Method 3 : 3.97821164131
# Method 4 : 1.66151881218
# Method 5 : 8.90886187553
# Method 6 : 6.2503888607


The clear winner is

def del_by_key(L,key) :
   for pos, (k,d) in enumerate(L):
 if  k == key :
   del L[pos]
   break


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Benchmark [was Re: common problem - elegant solution sought]

2008-01-15 Thread Helmut Jarausch
Paul Rubin wrote:
> Helmut Jarausch <[EMAIL PROTECTED]> writes:
>> def del_by_key(L,key) :
>>for pos, (k,d) in enumerate(L):
>>  if  k == key :
>>del L[pos]
>>break
> 
> This looks very dangerous, mutating L while iterating over it.

No, as Bruno Desthuilliers has pointed out, because one
breaks out of the loop immediately.

Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: super, decorators and gettattribute

2008-01-15 Thread Helmut Jarausch
Michele Simionato wrote:

> I really need to publish this one day or another, since these
> questions
> about super keeps coming out:
> 
> http://www.phyast.pitt.edu/~micheles/python/super.html

Unfortunately the links [2], [3] and [4] are not given,

Helmut.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


ctypes CDLL - which paths are searched?

2008-01-21 Thread Helmut Jarausch
Hi,

how can I specify the paths to be searched for a dynamic library
to be loaded by ctypes' CDLL class on a Linux system.

Do I have to set os.environment['LD_LIBRARY_PATH'] ?

Many thanks for a hint,
Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: ctypes CDLL - which paths are searched?

2008-01-22 Thread Helmut Jarausch
Thomas Heller wrote:
> Helmut Jarausch schrieb:
>> Hi,
>>
>> how can I specify the paths to be searched for a dynamic library
>> to be loaded by ctypes' CDLL class on a Linux system.
>>
>> Do I have to set os.environment['LD_LIBRARY_PATH'] ?
>>
> 
> ctypes passes the argument given to CDLL(path) straight to
> the dlopen(3) call, so your system documentation should tell you.
> 

Thanks,

but then it's difficult to use CDLL. Setting
os.environ['LD_LIBRARY_PATH'] within the script which
calls CDLL is too late.
What other methods are possible rather than put an explicit
export LD_LIBRARY_PATH=...
before running the script, if I don't want to put the dynamic
library into a standard system library.

Many thanks,
Helmut.



-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Removing objects

2008-01-23 Thread Helmut Jarausch
[EMAIL PROTECTED] wrote:
> I am writing a game, and it must keep a list of objects. I've been
> representing this as a list, but I need an object to be able to remove
> itself. It doesn't know it's own index. If I tried to make each object
> keep track of it's own index, it would be invalidated when any object
> with a lower index was deleted.  The error was that when I called
> list.remove(self), it just removed the first thing in hte list with
> the same type as what I wanted, rather than the object I wanted. The
> objects have no identifying charachteristics, other than thier
> location in memory
> 
> So my question: How do I look something up in a list by it's location
> in memory? does python even support pointers?
> 
> Is there a better way?

You could use a doubly linked list.
The class 'deque' from collections is such a list
but I don't known how to delete a specific element without
this ugly (expensive?) rotate method.

Here is my solution in pure Python

#!/usr/bin/python

import re

class Queue(object):
 __slots__= 'Head', 'Tail', '__iter__','NNd'

 def __init__(self):
 self.Head= None
 self.Tail= None
 self.NNd= None

 def append(self,N):
 if  self.Head == None:
 self.Head= N
 self.Tail= self
 N._enqueue_after(self.Tail)
 self.Tail= N

 def dequeue(self,N):
 Father, Son= N._dequeue()
 if  self.Tail == N:
 self.Tail= Father
 if  self.Head == N:
 self.Head= Son
 if  self.Head == None:  self.Tail= None

 def enqueue_after(self,N,Father):
 N._enqueue_after(Father)
 if  self.Tail == Father:
 self.Tail= N

 def enqueue_before(self,N,Son):
 N._enqueue_before(Son)
 if  self.Head == Son:
 self.Head= N

 def __iter__(self):
 next= self.Head  # allows to dequeue the current element in a loop
 while True:
 current= next
 if  current == None: raise StopIteration
 next= current._next()
 yield current



class Node(object):   # partially application specific
 __slots__= 'next', 'NNd','PNd','key','data'

 def __init__(self,Key,Data,P=None,N=None):  # P = Previous  N = Next
 if  P != None:   P.NNd= self
 if  N != None:   N.PNd= self
 self.PNd= P
 self.NNd= N
 self.key= Key
 self.data= Data

 def _enqueue_after(self,Father):
 self.NNd= Father.NNd
 self.PNd= Father
 Father.NNd= self

 def _enqueue_before(self,Son):
 self.NNd= Son
 self.PNd= Son.PNd
 Son.PNd= self

 def _dequeue(self):
 if self.PNd != None:
 self.PNd.NNd= self.NNd
 if self.NNd != None:
 self.NNd.PNd= self.PNd
 Father= self.PNd
 Son=self.NNd
 self.PNd= self.NNd= None
 return Father,Son

 def _next(self):
 return self.NNd

 def __str__(self):   # highly application specific
 return '(%s:%s)' % (self.key,self.data)

#  some tests

MyQ= Queue()
MyQ.append( Node('HJ','3949') )

print MyQ.Head,MyQ.Tail,MyQ.Head.key
MyQ.append( Node('CJ','10149') )
print MyQ.Head,MyQ.Tail,MyQ.Head.key
N= MyQ.Head
print "queue: ",N.key,"->"

N= N.NNd
print "next: ",N.key,"->"
N= N.NNd
if  N != None:
 print "next: ",N.key,"->"
else:
 print "no next:"

for N in MyQ:
 print "loop->",N
 print N.key,N.data

MyQ.dequeue(MyQ.Tail)
print "--- after dequeue"
print "Head: ",MyQ.Head,"  Tail: ",MyQ.Tail
for N in MyQ:
 print "loop2->",N
 print N.key,N.data

MyQ.dequeue(MyQ.Tail)
print "after second dequeue"
print "Head: ",MyQ.Head,"  Tail: ",MyQ.Tail

for N in MyQ:
 print "loop3->",N
 print N.key,N.data


ENJOY,
Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: global/local variables

2008-01-25 Thread Helmut Jarausch
Tim Rau wrote:

> I'm sorry: I forgot to say what my problem was. Python seems to think
> that nextID is a local, and complains that it can't find it. THis is
> not the full text of the function, just the level that is causing
> errors. the lack of : on the if is a transcription error.
> Traceback (most recent call last):
>   File "C:\Documents and Settings\Owner\My Documents\NIm's code\sandbox
> \sandbox.py", line 248, in 
> thing.step()
>   File "C:\Documents and Settings\Owner\My Documents\NIm's code\sandbox
> \sandbox.py", line 112, in step
> allThings[len(allThings)-1].id = nextID
> UnboundLocalError: local variable 'nextID' referenced before
> assignment

But in the next line in the same function you say
   nextID+= 1

Since there is an assignment to nextID in the same function without
a global statement, you tell Python, that nextID is a local variable.

Note:  Assignment (anywhere in a function) is a local definition unless
there is a global statement in this function

> I want to know why it says 'local variable' it never had a problem
> when just allThings was in there.
> 
> as for the objection to global variable: If I didn't make them global,
> I'd make them part of a singleton class called gameVars that would get
> passed everywhere. It's unlikely that they'll get mixed in with
> anyhting else, as they fulfill a unique function. Also, I think it's
> more convenient, and I am, after all, my own employer when it comes to
> programming.


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter - incremental input ?

2008-01-30 Thread Helmut Jarausch
Hi,

I don't want to reinvent the wheel but I cannot find it so far.

Many editors have a so-called incremental search feature.
As you type characters, elements of a set of strings which fit so far are
displayed or at least, the first one of these is displayed.

Now I want to do something similar in Tkinter - an Entry widget
which displays possible 'completions'
e.g. given the list of names (...,'Hardy','Helmut',..)

As soon as enter the character 'H' the string 'Hardy'
would be displayed in the Entry widget - but the cursor
is still at position 2 (given 'H' is a position 1)
Furthermore, as soon as I enter 'e', it would change the
text to 'Helmut', and so on.

While I can bind '' to a callback, I haven't figured out how
to get (and later on set) the cursor within the Entry widget.
In other words I need to know at which character position the last
character was entered.
Currently I can only see the brute force method: keeping track of
all cursor positioning means like , , the '<-' and '->' keys
and mouse clicks.
Is there an easier method?

Many thanks for a hint or even a pointer to an example,
Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Trouble loading dll via ctypes

2008-01-30 Thread Helmut Jarausch
subopt inTheVicinityOf geemail.com wrote:
> I'm trying to load a dll via ctypes by doing this:
> 
> cdll.LoadLibrary('/path/to/mylib.so')
> 
> But i'm getting this:
> 
> /path/to/mylib.so: cannot open shared object file: No such file or
> directory What am i doing wrong?
> 
> The dll in question is in a directory mounted via NSF, but no part of
> the path/filename is a symlink. When i try code from the docs:
> 
> cdll.LoadLibrary('libc.so.6')
> 
> ,then all is fine.
> 
> I've also tried to set my LD_LIBRARY_PATH before starting Python,
> checking it via os.environ, then doing the cdll.LoadLibrary(...), but
> that fails with the same complaint. What am i doing wrong?
> 

I vaguely remember you need execute permissions for a dynamic library to load.
Permissions on an NFS mounted directory are different, especial for user 'root'.

Check if can execute some executable in that NFS path.

Helmut.



-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sine Wave Curve Fit Question

2008-01-30 Thread Helmut Jarausch
Iain Mackay wrote:
> Python Folks
> 
> I'm a newbie to Python and am looking for a library / function that can help 
> me fit a 1D data vector to a sine  wave. I know the frequency of the wave, 
> so its really only phase and amplitude information  I need.
> 
>  I can't find anything in the most widely known libraries (they seem to be 
> strong on polynomial fitting, but not, apparently, on trig functions) and I 
> wondered if any one here had recommendations?
> 
>  Something that implemented IEEE 1057 , or similar, would be perfect.

Let's do a bit math first.

Your model is  A*sin(omega*t+alpha)  where  A and alpha are sought.
Let T=(t_1,...,t_N)' and  Y=(y_1,..,y_N)'  your measurements (t_i,y_i)
( ' denotes transposition )

First, A*sin(omega*t+alpha) =
   A*cos(alpha)*sin(omega*t) + A*sin(alpha)*cos(omega*t) =
   B*sin(omega*t) + D*cos(omega*t)

by setting  B=A*cos(alpha)  and  D=A*sin(alpha)

Once, you have  B and D,  tan(alpha)= D/B   A=sqrt(B^2+D^2)
Then in vector notation  S=sin(omega*T)  C=cos(omega*T)
you get the 2x2 system for B and D :

(S'*S) * B  +  (S'*C) * D=  S'*Y
(S'*C) * B  +  (C'*C) * D=  C'*Y

where  S'*C  is the scalar product of the vectors  S and C and similarly.

Now, for Python, to handle vectors and scalar products efficiently, have a look 
at numpy.



-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sine Wave Curve Fit Question

2008-01-31 Thread Helmut Jarausch
Mikael Olofsson wrote:
> Helmut Jarausch wrote:
>> Your model is  A*sin(omega*t+alpha)  where  A and alpha are sought.
>> Let T=(t_1,...,t_N)' and  Y=(y_1,..,y_N)'  your measurements (t_i,y_i)
>> ( ' denotes transposition )
>>
>> First, A*sin(omega*t+alpha) =
>>   A*cos(alpha)*sin(omega*t) + A*sin(alpha)*cos(omega*t) =
>>   B*sin(omega*t) + D*cos(omega*t)
>>
>> by setting  B=A*cos(alpha)  and  D=A*sin(alpha)
>>
>> Once, you have  B and D,  tan(alpha)= D/B   A=sqrt(B^2+D^2)
> 
> This is all very true, but the equation tan(alpha)=D/B may fool you. 

You're right: standard Python's math library missing the function arctan2.
But you can get it from numpy or numarray.
In case of doubt just compute the residuum with different possibilities .

> This may lead you to believe that alpha=arctan(D/B) is a solution, which 
> is not always the case. The point (B,D) may be in any of the four 
> quadrants of the plane. Assuming B!=0, the solutions to this equation 
> fall into the two classes
> 
> alpha = arctan(D/B) + 2*k*pi
> 
> and
> 
> alpha = arctan(D/B) + (2*k+1)*pi,
> 
> where k is an integer. The sign of B tells you which class gives you the 
> solution. If B is positive, the solutions are those in the first class. 
> If B is negative, the solutions are instead those in the second class. 
> Whithin the correct class, you may of course choose any alternative.
> 
> Then we have the case B=0. Then the sign of D determines alpha. If D is 
> positive, we have alpha=pi/2, and if D is negative, we have alpha=-pi/2.
> 
> Last if both B and D are zero, any alpha will do.
> 
> /MiO


-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


helper function in a class' namespace

2008-01-31 Thread Helmut Jarausch
Hi,

the following code works fine

def Helper(M) :
   return 'H>'+M

class A(object):
   def __init__(self,Msg) :
 print Helper(Msg)

B=A('ZZ')

but the Helper function is in the module's namespace.

I'd like to put it into class A's namespace.
Note, the Helper function doesn't need access to any instance attributes.

The first variant

class A(object):
   def Helper(M) :
 return 'H>'+M
   def __init__(self,Msg) :
 print Helper(Msg)

doesn't work since Python is looking for a global function Helper (why?)

The alternative

class A(object):
   def Helper(M) :
 return 'H>'+M
   def __init__(self,Msg) :
 print A.Helper(Msg)

doesn't work either since now the first argument to A.Helper must be 'A'.

So, isn't it possible to have a helper function in the namespace of a class
without (the overhead of) passing a 'self' or a 'cls' parameter?

Probably I'm hurt by my C++ history.

Many thanks for your help,
Helmut.



-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: a trick with lists ?

2008-02-08 Thread Helmut Jarausch
Sébastien Vincent  I've found some class on the Net which takes basically this form :
> 
> ##
> class Foo:
> def __init__(self):
> self.tasks = []
>...
> 
> def method1(self):
> tasks = []
> while True:
>   ...
>   append/pop elements into/from tasks
>   ...
>   if condition : break
> 
> self.tasks[:] = tasks
> return
> ##
> 
> What I do not fully understand is the line "self.tasks[:] = tasks". Why does 
> the guy who coded this did not write it as "self.tasks = tasks"? What is the 
> use of the "[:]" trick ?
> 

I've just run into this difference myself.
As several others have pointed out, assignment to
self.task[:] modifies this list in place.

Here my example showing a striking difference

class MyClass(object) :
   def shorten_list(self,outer_list) :
 ll=len(outer_list)
 if  ll > 0 :
   outer_list[:]= outer_list[:ll-1]


mylist=[1,2,3]
MyClass().shorten_list(mylist)
print mylist

// this prints  [1, 2]   (as expected)

class MyClass2(object) :
   def shorten_list(self,outer_list) :
 ll=len(outer_list)
 if  ll > 0 :
   outer_list= outer_list[:ll-1]


mylist=[1,2,3]
MyClass2().shorten_list(mylist)
print mylist

# this prints  [1, 2, 3]

The shortened list outer_list[:ll-1] has been assigned (bound in Python terms)
to the LOCAL reference (to a list) 'outer_list'

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter equiv for setPalette

2008-02-10 Thread Helmut Jarausch
Hi,

I am to convert an old Perl-Tk script to Python.
It starts by
my $MW= new MainWindow;
$MW->setPalette(background => 'AntiqueWhite1', foreground => 'blue');

Is there an equivalent for Tkinter? How can I set default colors for
background and foreground for the whole application (root window and its
children)

Many thanks for a hint,
Helmut.

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Tkinter - tk_focusNext broken for years?

2008-02-10 Thread Helmut Jarausch
Hi,

I'd like to bind the '' event to a function
which does some calculations (like a database lookup)
and then invoke  tk_focusNext.
I get an exception as many others have reported earlier, see
http://bugs.python.org/issue799428
There has even a patch been suggested. Why hasn't it been fixed -
I haven't seen a fix.
The problem can be reproduced by the following tiny script
I'm using Python 2.5.1 .

Should I fixed the Tkinter source myself or is there a workaround?

Many thanks for your help,
Helmut.
---
import Tkinter as Tk

def Proc_Enter(Event) :
   print "Enter - normally lookup a database"
   Event.widget.tk_focusNext()

root= Tk.Tk()
Tk.Label(root,text='Name').grid(column=0,row=0,sticky=Tk.W)
Nm_input= Tk.StringVar()
Nm_entry= Tk.Entry(root,textvariable=Nm_input)
Nm_entry.bind('',Proc_Enter)
Nm_entry.grid(column=1,row=0,sticky=Tk.W)

Tk.Label(root,text='City').grid(column=0,row=1,sticky=Tk.W)
City_input= Tk.StringVar()
City_entry= Tk.Entry(root,textvariable=City_input)
City_entry.bind('',Proc_Enter)
City_entry.grid(column=1,row=1,sticky=Tk.W)

Tk.mainloop()
---
 >>>>>>>>>>>>
Exception in Tkinter callback
Traceback (most recent call last):
   File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 1403, in __call__
 return self.func(*args)
   File "FocusBug.py", line 6, in Proc_Enter
 Event.widget.tk_focusNext()
   File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 479, in tk_focusNext
 return self._nametowidget(name)
   File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 1064, in nametowidget
 if name[0] == '.':
TypeError: '_tkinter.Tcl_Obj' object is unsubscriptable
---

-- 
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Why the result same as before modifing py files

2008-02-20 Thread Helmut Jarausch
zaley wrote:
> In my C++ program ,python is embeded . I import One module(py file)
> and execute some functions .Then I modify py files in order to modify
> func parameters when process is still running .I import the module and
> execute functions again.But I find  the result is same as before

'import' is cached. If do want Python to re-import a  just call
reload(http://mail.python.org/mailman/listinfo/python-list


  1   2   >