[newbie] Recursive algorithm - review

2014-01-03 Thread Wiktor
Hi,
it's my first post on this newsgroup so welcome everyone. :)

I'm still learning Python (v3.3), and today I had idea to design (my first)
recursive function, that generates board to 'Towers' Puzzle:
http://www.chiark.greenend.org.uk/~sgtatham/puzzles/js/towers.html
(so I could in future write algorithm to solve it ;-))

I'm pretty proud of myself - that it works, and that took me only 4 hours
to debug. ;-)
But on Project Euler site sometimes I'm also proud, that I solved some
problem in 30-line script, and then on forum there's one lined solution...

So maybe You might look at this script, and tell me if this can be more
pythonic. It's nothing urgent. I can wait - it works after all. ;-)


Idea is that function "generate()" 'finds' one number at a time (well,
besides first row), then checks if there are no repetitions in column
(because in row there cannot be by design - I pop out numbers from shuffled
list [1, 2, 3, ..., size] for every row.)
If no repetition - calls the same function to find next number, and so on.
If there is repetition at some point - recursion jumps back, and try
different number on previous position.



import random


def check(towers, x=None):
if x:
c = x % len(towers)   # check only column with
column = []   # value added on pos. x
for i in range(len(towers)):
column.append(towers[i][c])
column = [x for x in column if x != 0]
# print(column)   # debugging leftovers ;-)
return len(column) == len(set(column))
else:
for c in range(len(towers)):  # 'x' not provided,
column = []   # so check all columns
for i in range(len(towers)):
column.append(towers[i][c])
column = [x for x in column if x != 0]
# print(column)
if len(column) != len(set(column)):
return False
return True


def generate(size=4, towers=None, row=None, x=0):
if not towers: # executed only once.
row = [a for a in range(1, size+1)]# Then I'll pass towers list
random.shuffle(row)# at every recursion
towers = []
   # not so pretty way to generate
for i in range(size):  # matrix filled with 0's
towers.append([])  # I tried: towers = [[0]*size]*size
for j in range(size):  # but this doesn't work. ;-)
towers[i].append(0)# I don't know how to do this with
   # list comprehension (one inside
row_ = row[:]  # other?)
towers[0] = row_# after adding first row, columns will be
row = []# always unique, so I add entire row at once.
x = size - 1# Then I will be adding only one num at time.
# 'x' is pretty much position of last added el.
if not row:
row = [a for a in range(1, size+1)]
random.shuffle(row)

if x + 1 < size**2:
repeat = True
attempt = 0
while repeat:
# print(towers, row, x)
x += 1
num = row.pop(0)   # take num from right, and
towers[x // size][x % size] = num  # if doesn't match - put
repeat = not check(towers, x)  # back (append) on left -
   # - to rotate
if repeat:   # I'll repeat 'matching' next
row.append(num)  # number as long as last
x -= 1   # changed column is unique
attempt += 1
 # after some attempts I give
if attempt > len(row) - 1:   # up and jump back from
return False # current recursion
else:
if not generate(size, towers, row, x):
repeat = True
row.append(num)  # after some failed attempts
x -= 1   # on this 'level' I give up
attempt += 1 # again...

if attempt > len(row) - 1:
return False# ...and jump back one
# more time...
return towers


def main():
towers6by6 = generate(6)
# print(check(towers6by6))
print(towers6by6)

if __name__ == "__main__": main()



Footnote: English isn't my native language, so forgive me my bad grammar
and/or vocabulary. :-)  

-- 
Best regrds,  Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')  # spam trap
-- 
https://mail.python.org/mailman/listinfo/python-list


[newbie] Recursive algorithm - review

2014-01-03 Thread Wiktor
Hi,
it's my first post on this newsgroup so welcome everyone. :)

I'm still learning Python (v3.3), and today I had idea to design (my first)
recursive function, that generates (filled out) board to 'Towers' Puzzle:

http://www.chiark.greenend.org.uk/~sgtatham/puzzles/js/towers.html

(so I could in future write algorithm to solve it ;-))


I'm pretty proud of myself - that it works, and that took me only 4 hours
to debug. ;-)
But on Project Euler site sometimes I'm also proud, that I solved some
problem in 30-line script, and then on forum there's one lined solution...

So maybe You might look at this script, and tell me if this can be more
pythonic. It's nothing urgent. I can wait - it works after all. ;-)


Idea is that function "generate()" 'finds' one number at a time (well,
besides first row), then checks if there are no repetitions in column
(because in row there cannot be by design - I pop out numbers from shuffled
list [1, 2, 3, ..., size] for every row.)
If no repetition - calls the same function to find next number, and so on.
If there is repetition at some point - recursion jumps back, and try
different number on previous position.



import random


def check(towers, x=None):
if x:
c = x % len(towers)   # check only column with
column = []   # value added on pos. x
for i in range(len(towers)):
column.append(towers[i][c])
column = [x for x in column if x != 0]
# print(column)   # debugging leftovers ;-)
return len(column) == len(set(column))
else:
for c in range(len(towers)):  # 'x' not provided,
column = []   # so check all columns
for i in range(len(towers)):
column.append(towers[i][c])
column = [x for x in column if x != 0]
# print(column)
if len(column) != len(set(column)):
return False
return True


def generate(size=4, towers=None, row=None, x=0):
if not towers: # executed only once.
row = [a for a in range(1, size+1)]# Then I'll pass towers list
random.shuffle(row)# at every recursion
towers = []
   # not so pretty way to generate
for i in range(size):  # matrix filled with 0's
towers.append([])  # I tried: towers = [[0]*size]*size
for j in range(size):  # but this doesn't work. ;-)
towers[i].append(0)# I don't know how to do this with
   # list comprehension (one inside
row_ = row[:]  # other?)
towers[0] = row_# after adding first row, columns will be
row = []# always unique, so I add entire row at once.
x = size - 1# Then I will be adding only one num at time.
# 'x' is pretty much position of last added el.
if not row:
row = [a for a in range(1, size+1)]
random.shuffle(row)

if x + 1 < size**2:
repeat = True
attempt = 0
while repeat:
# print(towers, row, x)
x += 1
num = row.pop(0)   # take num from right, and
towers[x // size][x % size] = num  # if doesn't match - put
repeat = not check(towers, x)  # back (append) on left -
   # - to rotate
if repeat:   # I'll repeat 'matching' next
row.append(num)  # number as long as last
x -= 1   # changed column is unique
attempt += 1
 # after some attempts I give
if attempt > len(row) - 1:   # up and jump back from
return False # current recursion
else:
if not generate(size, towers, row, x):
repeat = True
row.append(num)  # after some failed attempts
x -= 1   # on this 'level' I give up
attempt += 1 # again...

if attempt > len(row) - 1:
return False# ...and jump back one
# more time...
return towers


def main():
towers6by6 = generate(6)
# print(check(towers6by6))
print(towers6by6)

if __name__ == "__main__": main()



Footnote: English isn't my native language, so forgive me my bad grammar
and/or vocabulary. :-)  

-- 
Best regrds,  Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')  # spam trap
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] Recursive algorithm - review

2014-01-04 Thread Wiktor
On Sat, 4 Jan 2014 13:02:37 +1100, Chris Angelico wrote:

>> def check(towers, x=None):
>> column = []   # value added on pos. x
>> for i in range(len(towers)):
>> column.append(towers[i][c])
>> column = [x for x in column if x != 0]
> 
> Any time you iterate over range(len(something)), you probably want to
> iterate over the thing instead:
> 
> for t in towers:
> column.append(t[c])

  Right, /facepalm/ I've done it so many times. Don't know why not this time.
 
> And any time you iterate over something and append to another list,
> you probably want a list comprehension:
> 
> column = [t[c] for t in towers]
> [...]
> column = [t[c] for t in towers if t[c] != 0]
> [...]
> column = [t[c] for t in towers if t[c]]

  Nice.
 
>> for c in range(len(towers)):  # 'x' not provided,
>> column = []   # so check all columns
> 
> I wouldn't normally wrap a comment onto an unrelated line; I'd put the
> comment above the loop, since it's too long to be a part of the loop
> header itself. It goes as much with the "else" as with the loop,
> anyhow.

  My mistake. I know, that sometimes comment doesn't relate to line that is
next to, but for one second I belived that it would be more readable ('talk'
about the code whitout providing more unnecessary whitespace). Now I see that
it isn't.   

> This is one case where you probably _do_ want to iterate up to
> range(len(towers)), though, which is why I said "probably" above. :)
> 
>> for i in range(len(towers)):
>> column.append(towers[i][c])
>> column = [x for x in column if x != 0]
> 
> This is the same code you had above, so it can benefit from the same
> translation. But maybe it'd be even cleaner to simply call yourself?
> 
> if not check(towers, i): return False

  You're right. It's cleaner this way.
 
>> # print(column)
>> if len(column) != len(set(column)):
>> return False
>> return True
> 
> And in fact, you might want to turn this whole branch into something
> that harks to a more functional programming style:
> 
> return all((check(towers, i) for i in range(len(towers)))

  Great. I didn't know all() before. :-)
  Now check() function looks very neat. 
  Although 'if' statement must now looks like: 'if x is not None:'. Earlier x
never was going to be 0. Now it can be.
 
>> random.shuffle(row)# at every recursion
> 
> Again, I wouldn't wrap comments onto unrelated lines. You see how
> confusing this looks, now that I take this line out of context? Same
> will happen if it throws an exception.

  Yeap. Now I see it. Never gonna do that again. :-)
 
> When you multiply a list of lists, you get references to the same
> list, yes. But you could use multiplication for one level:
> 
> towers = [[0]*size for _ in range(size)]
> 
> That'll give you independent lists. 

  Got it!

>> if not row:
>> row = [a for a in range(1, size+1)]
>> random.shuffle(row)
> 
> This is the same as you have at the top of 'if not towers'. Can you be
> confident that row is None any time towers is None? If so, just move
> this up above the other check and save the duplication.

  row is None at start, but later it is list - sometimes an empty list. For
that cases this if statement was written. If row == [] -> generate new random
row that I can pop out from.
 
>> num = row.pop(0)   # take num from right, and
>> towers[x // size][x % size] = num  # if doesn't match - put
>> repeat = not check(towers, x)  # back (append) on left -
>># - to rotate
>> if repeat:   # I'll repeat 'matching' next
>> row.append(num)  # number as long as last
>>     x -= 1   # changed column is unique
> 
> Hmm, I'm slightly confused by your comments here. You pop(0) and
> append(), and describe that as taking from the right and putting on
> the left. Normally I'd write a list like this:

  Of course, of course. I was thinking one, writing another. I switched left
and right in my explanation. It looks stupid now. ;-)

  Thank you for all Your comments.

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka') # email spam trap
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] Recursive algorithm - review

2014-01-04 Thread Wiktor
On Fri, 03 Jan 2014 20:47:16 -0500, Terry Reedy wrote:

> [0]*size] is fine for one row
> 
> towers = [[0]*size] for i in range(size)]
> 
> should do what you want for a 2-d array instead of the above.

  Right. Thank you also.

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka') # email spam trap
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] Recursive algorithm - review

2014-01-04 Thread Wiktor
On Sat, 4 Jan 2014 22:18:09 +1100, Chris Angelico wrote:

>>> This is the same as you have at the top of 'if not towers'. Can you be
>>> confident that row is None any time towers is None? If so, just move
>>> this up above the other check and save the duplication.
>>
>>   row is None at start, but later it is list - sometimes an empty list. For
>> that cases this if statement was written. If row == [] -> generate new random
>> row that I can pop out from.
> 
> Yes, but will you ever pass a non-None row and a None towers? If not,
> you can deduplicate that bit of code by simply checking one before the
> other.

  Oh, now I understand what You mean.
  I rewrote that part.

def generate(size=4, towers=None, row=None, x=0):

if not row:
row = [a for a in range(1, size+1)]
random.shuffle(row)

if not towers:
towers = [[0]*size for _ in range(size)]
towers[0] = row[:]
random.shuffle(row)
x = size - 1 

if x + 1 < size**2:
# [...]

  Much more cleaner.
  Thanks!

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka') # email spam trap
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] Recursive algorithm - review

2014-01-04 Thread Wiktor
On Sat, 4 Jan 2014 22:18:09 +1100, Chris Angelico wrote:

>>   Thank you for all Your comments.
> 
> My pleasure! Always happy to help out.

  I'm aware, that at my point of education there's no sense in optimizing code
to squeeze from it every millisecond, but Project Euler gave me habit to
compare time consumption of script every time I make serious change in it.

  Your tune-ups made this script (mostly check() I guess) about 20% faster. So
it's not only 'more readable' profit. :-)

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka') # email spam trap
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [newbie] Recursive algorithm - review

2014-01-04 Thread Wiktor
On Sat, 4 Jan 2014 01:16:14 +0100, Wiktor wrote:

> Hi,

  OK, another question. This time, I think, closer to the original subject
(recursive algorithm).

  Thanks to Terry's and Chris' advises I refined script. Then I thought, that
with some changes and with minimal effort I can force this script to generate
Sudoku board (again: filled out, 'solved' one).
  Well, I was wrong. ;-) It actually took me 2 hours to make this script
working fine - even though changes weren't so big. And another one hour to make
it clear enough to share here.


  Idea is still the same. I start with 2d array
  
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]

  And then I fill it up one number by one (exception: first row). At every step
checking if current column is unique (0's not counted) and if also current
segment 3x3 is unique. If that condition is True I call another instance of
generate(), passing to it a) the board, b) the position in which I last putted
number, and c) a list of numbers that in next step this function can choose
from (if empty, function will generate new list). And so on.
  If that condition is False, I try another number from list of available
numbers, and check again. If all numbers fail, I go back one level and try
another number on previous position.


  I'll paste code now, and under that I'll write what's mine concern now.
(if you uncomment hashed lines it will print out entire process of generating
board, but watch out - it can be several thousands of lines) 


###  
import random


attempt_global = 0


def check(board, x=None, sudoku=False):
global attempt_global
attempt_global += 1

if sudoku and len(board) == 9 and x is not None:
c = x % len(board)
r = x // len(board)

# print('Attempt #{}; Checking ({}x{})'.format(attempt_global,
#  r, c))
# for row in board:
# print(row)
# print()

column = [t[c] for t in board if t[c]]

br_min, br_max = r//3 * 3, r//3 * 3 + 3
bc_min, bc_max = c//3 * 3, c//3 * 3 + 3
block = [t[bc_min:bc_max] for t in board[br_min:br_max]]
block_flat = [item for row in block for item in row if item]

return len(column) == len(set(column)) and \
len(block_flat) == len(set(block_flat))

elif x is not None:
c = x % len(board)
column = [t[c] for t in board if t[c]]
return len(column) == len(set(column))

elif sudoku and len(board) == 9:
return all((check(board, i, sudoku) for i in range(0,
   len(board)**2,
   4)))

else:
return all((check(board, i) for i in range(len(board


def generate(size=4, board=None, row=None, x=0, sudoku=False):
if not row:
row = [a for a in range(1, size+1)]
random.shuffle(row)

if not board:
board = [[0]*size for _ in range(size)]
board[0] = row[:]
random.shuffle(row)
x = size - 1

if x + 1 < size**2:
repeat = True
attempt = 0

while repeat:
x += 1
num = row.pop(0)
board[x // size][x % size] = num
repeat = not check(board, x, sudoku)

if repeat:
row.append(num)
board[x // size][x % size] = 0
x -= 1
attempt += 1

if attempt > len(row) - 1:
return False
else:
if not generate(size, board, row, x, sudoku):
repeat = True
row.append(num)
board[x // size][x % size] = 0
x -= 1
attempt += 1

if attempt > len(row) - 1:
return False

return board


def main():

global attempt_global
sudoku_board = generate(9, sudoku=True)

for row in sudoku_board:
print(row)

print('Attempts:', attempt_global)


if __name__ == "__main__": main()

###



  OK, it works fine. Most of the time it generates board in less than 400
attempts, so not so bad. But sometimes it takes over thousand tries to generate
board.

  For example, one time, at attempt #46 it validates last putted '1', and of
course it passes,

Attempt #46; Checking (4x1)
[1, 5, 3, 9, 4, 2, 8, 6, 7]
[2, 7, 6, 5, 8, 1, 3, 9, 4]
[9, 8, 4, 7, 3, 6, 1, 5, 2]
[8, 9, 5, 3, 7, 4, 6, 2, 1]
[7, 1, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0]

  then fills out entire row

Re: [newbie] Recursive algorithm - review

2014-01-04 Thread Wiktor
On Sat, 4 Jan 2014 20:07:33 +0100, Wiktor wrote:

> I guess that some kind of you have done this before. ;-)


  Damn it. This 'kind' shouldn't be there. Now it sounds silly, 
even offensive. ;-) Normally I would supersede it, but probably attached
mailing-list doesn't recognize those Usenet articles (Chris previously answered
to my superseded article).

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka') # email spam trap
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Flip a graph

2014-01-04 Thread Wiktor
On Sat, 4 Jan 2014 09:15:39 -0700, Jason Friedman wrote:

> My concern is whether the average 11-year-old will be able to follow such
> logic.  Is there a better approach?

  Basically mine approach is the same, but maybe is easier to explain it to
kids.

  
max_height = max(measurement_dict.values())

temporary_graph = []

for t, y in measurement_dict.items():
temporary_graph.append('X'*y + ' '*(max_height - y))

for i in range(max_height-1, -1, -1):
for item in temporary_graph:
print(item[i], end='')
print()

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka') # email spam trap
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Python prime numbers

2014-02-01 Thread Wiktor
On Sat, 1 Feb 2014 07:33:47 -0800 (PST), Panagiotis Anastasiou wrote:

> Hi i'm new in programming and in python and i have an assignment that
> i cant complete. I have to Write a Python program to compute and print the 
> first 200 prime numbers. The output must be formatted with a title and the 
> prime numbers must be printed in 5 properly aligned columns . I have used 
> this 
> code so far :

  Hi,
  try out this code:

for i in range(200):
   print '{0:>5}'.format(i),
   if (i-4) % 5 == 0:
   print

  Or maybe, if it's still unclear, try execute these lines:

print 'Hello {0}'.format('world')
print '|{0:>30}|'.format('right')
print '|{0:<30}|'.format('left')
print '|{0:^30}|'.format('center')
print '|{0:>16}|'.format('right'),
print '|{0:<16}|'.format('left'),
print '|{0:^16}|'.format('center')

  But still, it might be hard to implement this printing for..in loop while
you're verifying primes (in another loop), so maybe think about getting first
200 primes in while loop like you do (and only storing them in a list), and
then printing them out from this list in external for..in loop.



  Now, to your primetest() function. It may be good for small primes, but try
to verify with it, if 832475734579 is a prime. :)

> def primetest(potentialprime):
> divisor = 2
> while divisor <= potentialprime:

  First of all, see that you rarely use this loop - you check this condition at
most two times. You end up for good in the second while loop.

> if potentialprime == 2:
> return True
> elif potentialprime % divisor == 0:
> return False
> break

  'break' after return is redundant - never executes

> while potentialprime % divisor != 0:
> if potentialprime - divisor > 1:
> divisor += 1
> else:
> return True

  So, this is your main loop. Very inefficient. Think about that:
a) do you really have to check divisors up to the potentialprime? 
   Maybe there is a point, where you may say, that you've checked all 
   possibilities? Remember that a * b = b * a 
b) do you really have to check every divisor? I mean, increasing 
   it by 1 in every step?

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Loading Python 2.7

2014-11-24 Thread Wiktor
On Mon, 24 Nov 2014 18:25:25 -0500, Seymore4Head wrote:

> What I do when I need to run 2.7 code is to just save the file to my
> Python 2.7 folder and run a command prompt.  I then just type the py
> file.  That works but I am left with a dos prompt when the file
> finishes.  I sometimes would like to be able to use the interactive
> shell with a 2.7 file in memory.
> 
> I don't know how to do that.

  Don't save it to your Python27 folder. Just "run command prompt" in
folder where your *.py file is located and type in there:
> py -2 -i name_of_file.py

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


Forking PyPI package

2014-05-28 Thread Wiktor
Hello.

  There's script pwdhash https://pypi.python.org/pypi/pwdhash.py/0.1.1,
which I always* wanted to port to Python 3. (* - well, i.e. 6 months ;-))
  I'm using this hashing algorithm quite often for years in my browser
(Opera plugin), so I thought that it would be cool to have it as python
script.

  It took me some time to make it work under Python 3, because I knew
nothing about str <-> bytes encoding, and this was biggest issue why
original script wasn't Py3 compatible. So now my version works, it even
supports Unicode characters (original JS script does that, but pwdhash.py
script doesn't), and now I'm planning to do simple GUI version, because
copying from console isn't very comfortable (and I couldn't find good [os
and other packages independent] solution to copy string to clipboard. Best
answer from here http://goo.gl/8V9Ba6 isn't working).
  So, my point is, I think maybe it would be useful also to others.

  I'm newbie not only to Python, but to programming in general, so I don't
know, what is best practice in OS programming community. How forking works.
How py2->py3 porting (by 3rd person) is realized in Pythonians community.
Can you suggest me something? I see few scenarios:

1) I'm trying to contact with original script's author, and send him my
propositions of changes in code. (Oh, one more thing: my code isn't
backward compatible, and I don't know Py2 that much to include all those
try/except, so it could be Py2+Py3 compatible). He decides, if he wants to
release it as Py3 only version, or combine it with his script and release
Py2+Py3 common version.

2) I'm not contacting with him, and I'm forking his project on GitHub
   a) under the same name? - probably bad idea
   b) under new name (e.g. py3pwdhash)? 
Of course all credits about original author stay in code / setup.py.

2.1) After forking on GitHub, I'm creating new package on PyPI
   a) under old name, but different version number, and new description?
   b) under new name, to not confuse users?
   
   
  So, what should I do?
  I know, that maybe I shouldn't touch that script in first place and just
mail to author "Hey, would you please port it to Py3?", but I also treated
it as programming exercise, and I didn't think about consequences. ;-) 

TIA
-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Forking PyPI package

2014-05-29 Thread Wiktor
On Thu, 29 May 2014 02:31:56 +0200, Wiktor wrote:

>   So, what should I do?

  Thanks guys, you're right. I'll contact the Lion. ;-)
  
  Yes, I forgot to mention that pwdhash.py was published under BSD licence.
Without knowing that I wouldn't even consider forking it.

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Forking PyPI package

2014-06-05 Thread Wiktor
On Thu, 29 May 2014 15:54:09 -0600, Ian Kelly wrote:

> On Thu, May 29, 2014 at 1:40 AM, Chris Angelico  wrote:
>> If you absolutely can't get in touch with him, the only option is to
>> go back to the original protocol and manually reimplement it,
>> completely ignoring this code. It's sad but true; some code dies
>> because of a trivial thing like "Oops, I forgot to actually say that
>> this is MIT-licensed".
> 
> The second part of that is that the code should actually *include* the
> license text. Just writing "BSD license" somewhere on the website or
> in package metadata is annoyingly common but somewhat questionable in
> how a judge might interpret it.  For instance, there at least four
> different versions of the BSD license; which one did you mean?

  OK, it's almost week now and I have no response from author of that
script.

  Just like you said, there's only inscription "BSD license" on PYPI
website, and in 'PKG-INFO' and 'setup.py' files. No 'readme.txt' or
'license.txt' is included. I can see now, that in fact it means that script
isn't published under any BSD license.

  I guess, I'll try to do what Chris proposed. Forget about this
implementation and write python script from the scratch looking only at the
original JavaScript version. :-/

  Thank you guys.  
  
-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Forking PyPI package

2014-06-05 Thread Wiktor
On Fri, 6 Jun 2014 03:37:56 +1000, Chris Angelico wrote:

> On Fri, Jun 6, 2014 at 2:56 AM, Wiktor  wrote:
>>   I guess, I'll try to do what Chris proposed. Forget about this
>> implementation and write python script from the scratch looking only at the
>> original JavaScript version. :-/
> 
> Sadly, that may be your only safe option.
> 
> Let this be a lesson to all: If you want something to be free
> software, make it very clear, because "it looks like he meant that to
> be open source" just isn't enough :(

  Lesson taken. ;-)
  Interesting thing is, that for another 4 people, lack of license in this
script wasn't problem to publish its modified version. I've just searched
phrase "pwdhash" on GitHub, to simply check if someone else hadn't port
this script to Python3 earlier, or maybe ported it (with proper license) to
Python2 so I would have better start. And I've found practically the same
script here: https://github.com/ali01/pwdhash.py, forked then 3 times.
  Of course I'm not going to act now "Oh, they could do that without
consequences, so why should I bother?" - no, I'm going to do this right (as
a good start in OS community) - but it feels awkward now. ;-)

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


cmd.exe on WIndows - problem with displaying some Unicode characters

2014-08-03 Thread Wiktor

Hi,

as OO programming exercise, I'm trying to port to Python one of my favorite
game from early'90 (Atari 65XL/XE) - Kolony (here's video from original
version on C64 https://www.youtube.com/watch?v=UFycYOp2cbE, and here's
video from modern rewritten (for Atari emulators) version: Kolony 2106
https://www.youtube.com/watch?v=eX20Qqqm5eg - you get the idea? ;-)).

OO Design is one thing, but I want to make it look as near as possible to
the original (those windows-like menus in console window). I tried to use
'standard' Unicode characters (I can see that most of my Windows monospaced
fonts have them) to draw frame around menu. Something like this:

 ┌──╖
 │ Construction ║
 │ Production   ║
 │ Research ║
 │ Exploration  ║
 ├··╢
 │ Next turn║
 ╘══╝

(I like the look of double lines on right and at the bottom)
But when I try to print those characters, I get an error:

| Traceback (most recent call last):
|   File "E:\Moje dokumenty\python\kolony\menu.py", line 14, in 
| """
|   File "C:\Python34\lib\encodings\cp852.py", line 19, in encode
| return codecs.charmap_encode(input,self.errors,encoding_map)[0]
| UnicodeEncodeError: 'charmap' codec can't encode character '\u2556' in 
position 1
| 6: character maps to 

Now I know what that means. Code page that my cmd.exe is using (852)
doesn't have "╖", "╘", "╢" and "·" symbols. Changing code page to Unicode
(65001) doesn't really help, because all is messed up:
 ┌──╖
 │ Construction ║
 │ Production   ║
 │ Research ║
 │ Exploration  ║
 ├··╢
 │ Next turn║
 ╘══╝
�·╢
 │ Next turn║
 ╘══╝
��╝
��═╝
═╝
(I believe that's cmd.exe bug with Unicode support, not Python fault)


Before I drop entirely this idea of using double lines on right and bottom
edges, and make it look like this 
┌──┐
│ Construction │
├--┤
│ Next turn│
└──┘
I have to ask - is there a way to make that original concept work? I know,
that CP437 has symbols "╖", "╢" and "╘", but does not have polish letters -
and I need to display them too. 
I also know, that cmd.exe can display those Unicode characters (by
copy/paste them in command line or by listing filenames containing that
characters), no matter what CP is set. How does it manage to do it? Can I
exploit that writing my Python program?
Wiktor

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd.exe on WIndows - problem with displaying some Unicode characters

2014-08-04 Thread Wiktor
  Hi,
first, thank you all for responses. I decided to just use single line frame
around menu. Yes, those double+single line corners work fine in ConEmu, but
I don't want this Python script to be dependent on external program. Maybe
one day it will be worth of showing to others, and it's silly to tell them
'It is pure console based game/framework but works only in ConEmu'...


  Now, to Terry's post:

On 8/3/2014 6:52 PM, Wiktor wrote:

>> as OO programming exercise, I'm trying to port to Python one of my favorite
>> game from early'90 (Atari 65XL/XE) - Kolony (here's video from original
>> version on C64 https://www.youtube.com/watch?v=UFycYOp2cbE, and here's
> 
> This appears to be an actual text screen, no graphics.
> 
>> video from modern rewritten (for Atari emulators) version: Kolony 2106
>> https://www.youtube.com/watch?v=eX20Qqqm5eg - you get the idea? ;-)).
> 
> This appears to be text boxes on a graphics screen.
> 
>> OO Design is one thing, but I want to make it look as near as possible to
>> the original (those windows-like menus in console window).
> 
> Which original? the C64 or Atari.  The important characteristic of both 
> is that both have multiple overlapping popup boxes. This means that 
> either you or a widget framework much keep track of stacking order and 
> how to restore what was hidden when a box goes away or is moved down in 
> the stacking order. I would not be surprised if the Atari had at least a 
> rudimentary widget framework.

  Yes, I'm aware that first link is to the text based game, and second to
graphic based game. I provided both links, because I couldn't find screen
cast from original Atari game (which is also text based, but IMO looks
better than C64's version), and this modern game is translated to English,
so is better for you to understand character of game.
  Yes, I'd like to make text game, that looks like window-based, with popup
boxes, inactive windows grayed out and all this stuff. And all this running
on standard console window (cmd.exe).

  I'm not starting from scratch. I'm using packages 'termcolor', 'colorama'
and 'colorconsole' - they provide functions to print text at desired
position on screen, and change color of foreground/background of this text.
With those packages I already developed some classes that allow me to
construct some simple menus for my console programs. Short demo of silly
program calculating degree of n-th root:
  http://youtu.be/V8ilLhHAT_k
  (I link to the video, because there's too much code lines to paste them
here. Also it's dependent upon those third party packages, and still
work-in-progress).

  So, I'm not worry about randomly access, colors, overprinting existing
characters. At this point I know how to do that. 
  I'm taking next step, so I tried to draw nice frame around menu (that's
why I posted yesterday).
  Next step would be to manage those widgets to draw one over another, to
keep track which one window opens which, and when the other window must be
closed and when only grayed out. At this point I still don't know how to do
this right, but I'm thinking about this very hard. :-) Probably one day
I'll ask it here, if I don't figure it out. :-)

Wiktor

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd.exe on WIndows - problem with displaying some Unicode characters

2014-08-04 Thread Wiktor
On Mon, 04 Aug 2014 17:43:41 +0200, Wolfgang Maier wrote:

>>I'm not starting from scratch. I'm using packages 'termcolor', 'colorama'
>> and 'colorconsole' - they provide functions to print text at desired
>> position on screen, and change color of foreground/background of this text.
> 
> Thanks for pointing out these packages! Since you say you're using all 
> three of them: where do colorama and colorconsole differ. From a quick 
> look, I can see that termcolor is a bit different, but colorama and 
> colorconsole seem pretty similar in scope.

  From colorama I just use one function - init(). Without this
initialization all those ansii escape characters (used by colorama itself,
but also by termcolor.colored()) don't work in cmd.exe. At least I couldn't
make it work.
  All coloring work I make in termcolor.colored() function, because it
returns string, which I can work on (store and/or send it to print_at()
function later).
  And colorconsole is helpful with its screen.print_at() function [where
screen = colorconsole.terminal.get_terminal()].
  
  So, yes, it's matter of (probably bad) design, but now I need all three
packages. Maybe if I resign from storing my colored strings, and color them
just while sending them to printing function, I could get rid of colorama
and termcolor...
  Well, thanks for asking, because now, during writing this response, I see
that maybe redesign is worth of trying...

Wiktor

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd.exe on WIndows - problem with displaying some Unicode characters

2014-08-04 Thread Wiktor
On Tue, 5 Aug 2014 03:06:41 +1000, Chris Angelico wrote:

> On Tue, Aug 5, 2014 at 2:48 AM, Wiktor  wrote:
>>   From colorama I just use one function - init(). Without this
>> initialization all those ansii escape characters (used by colorama itself,
>> but also by termcolor.colored()) don't work in cmd.exe. At least I couldn't
>> make it work.
> 
> I dug into colorama's source code, and it seems that "just one
> function" is a little dismissive :) When you call colorama's init(),
> it replaces stdout with a wrapper that parses ANSI sequences and turns
> them into API calls. So, yeah, without that anything that outputs ANSI
> sequences isn't going to work.

  Maybe I didn't write it clear. :-) What I meant was, that even though I
don't use any other functions from colorama (I color all the strings with
termcolor) - I still have to use init() function from colorama. 
  termcolor doesn't want to work alone, even though its described as OS
independent. I guess it works fine on Linux terminal without init()
function from colorama. In cmd.exe I need colorama just for this.

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd.exe on WIndows - problem with displaying some Unicode characters

2014-08-04 Thread Wiktor
On Mon, 04 Aug 2014 15:17:04 -0400, Terry Reedy wrote:

>>I'm taking next step, so I tried to draw nice frame around menu (that's
>> why I posted yesterday).
> 
> Is there no working codepage with ascii text and the line chars? I 
> suppose I am not surprised if not.

  With single line (└┘┌┐─│├┤┬┴┼) and double line (╣║╗╝╚╔╩╦╠═╬) - many
codepages, CP852 for sure.

  With corners/crosses where single and double lines meet (╖╘╡╢╕╜╛╞╟
╧╨╤╥╙╘╒╓╫╪) - I know only one: CP437.
  
  But I can't have both - Polish letters and all those line chars, so I
can't do this fancy frame from first post with Polish strings inside. There
will be simpler version instead.

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd.exe on WIndows - problem with displaying some Unicode characters

2014-08-05 Thread Wiktor
On Tue, 05 Aug 2014 04:51:15 +0400, Akira Li wrote:

> Unicode has line drawing characters [1]. win_unicode_console [2] allows
> to print Unicode in cmd.exe. win_unicode_console and colorama will
> probably conflict. You could look at the source to see how hard to
> combine both functionalities.
> 
> [1] http://en.wikipedia.org/wiki/Box-drawing_character
> [2] https://pypi.python.org/pypi/win_unicode_console
> 
> btw, blessings [3] provides an easy-to-use interface if you need to add
> colors and move text in a terminal. It claims that it also supports
> colors on Windows if used with colorama.
> 
> [3] https://pypi.python.org/pypi/blessings/

  [2] - indeed does not work with colorconsole/colorama. And I'm not that
smart to combine those two functionalities. :-)
  [3] - maybe it works with colorama (colorama for coloring, and blessing
for positioning text), but now I don't even use colorama. I use
colorconsole to color AND position text, and I find it very handy. Don't
think that blessing+colorama would be more easy-to-use.

  Thanks, I really appreciate every proposition to fix original problem you
all are giving me - I check them all. 
  But you need to understand, that I'm already OK with those cmd.exe
limitations, and really not trying to achieve look of frame from first
post. I'm OK with those single-only and double-only lines.

  Now my game would look like this:
https://dl.dropboxusercontent.com/u/10544563/kolony_prntscr.png [*]
and I think it's very neat.

Wiktor

*) it's mocup (I don't have 'window manager' yet, so I have to show/
activate/deactivate/etc. every window by hand), but still

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: cmd.exe on WIndows - problem with displaying some Unicode characters

2014-08-05 Thread Wiktor
On 05 Aug 2014 20:26:08 GMT, Tony the Tiger wrote:

> On Mon, 04 Aug 2014 00:52:29 +0200, Wiktor wrote:
> 
>> okumenty\python\kolony\menu.py", line 14, in 
> 
> This works for me on Linux:

  I believe you, but I use Windows and its cmd.exe (as mentioned in
subject).

-- 
Best regards, Wiktor Matuszewski
'py{}@wu{}em.pl'.format('wkm', 'ka')
-- 
https://mail.python.org/mailman/listinfo/python-list


PyThreadState_Swap crash

2011-04-04 Thread Wiktor Adamski
I have 2 threads in C code using python 2.5.2. First thread creates
new interpreter (i need several interpreters but those 2 threads use
only one) like that:

PyEval_AcquireLock();
threadState = Py_NewInterpreter();
PyThreadState_Swap(threadState);

// calling python API

PyThreadState_Swap(NULL);
PyEval_ReleaseLock();

Second thread uses interpreter created in first thread:

PyEval_AcquireLock();
PyThreadState_Swap(threadState);

and sometimes PyThreadState_Swap crashes in debug build
(PyGILState_GetThisThreadState() returns garbage). In release build
that code doesn't run and so far no other problem was found.
I call PyEval_InitThreads() at the begining of program and every
PyEval_AcquireLock() has PyEval_ReleaseLock().

Am I allowed to use the same threadState in different threads?
If I am, is there another problem in my code?
Or maybe it's a bug in python - acording to documentation "Python
still supports the creation of additional interpreters (using
Py_NewInterpreter()), but mixing multiple interpreters and the
PyGILState_*() API is unsupported." - I don't use PyGILState_ but it's
used internally in PyThreadState_Swap(). I also don't use
PyEval_RestoreThread() - comment sugests that crashing code is present
because possibility of calling from PyEval_RestoreThread().
-- 
http://mail.python.org/mailman/listinfo/python-list