Re: Possible PEP - two dimensional arrays?

2016-06-08 Thread Terry Reedy

On 6/7/2016 8:17 PM, Harrison Chudleigh wrote:

I was programming a computer game and found that while 1D arrays can be
created using the module array, there is no module for two-dimensional
arrays, unlike languages like C. Currently, the closest thing Python has to
a 2D array is a dictionary containing lists.


A list of lists is standard if one is not using numpy, indexed as, for 
instance 'board[i][j]' A tuple of tuples can be used for static 2d 
array.  I expect people have also used a list of arrays, though for most 
games, the space saving is not enough, plus a list of list is more 
flexible, in that one can put a 'piece' on and 'square'.



I propose that a module , 2DArray, be added to the standard library. This
module will include:
Assignment and retrieval on items on a two-dimensional, finite rectangular
grid. Types are integer, float, character and string.
Resizing the grid - parameters are old size and new size. Any new elements
are initialized with a value of 0 for int, 0.0 for float and ' ' for string
and character arrays.
Removing elements. The parameter is the location. After removal, the value
returned is 0 for int, 0.0 for float and ' ' for string and character
arrays.
A function, pop(), which removes elements from the grid and then returns
them.


You could create your own class based on a list of arrays, and even 
publish it.


--
Terry Jan Reedy

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


how to extract a variable as parameter which has index using by a for loop?

2016-06-08 Thread meInvent bbird
b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in range(m)]
b[21][0:1]+b[21][1:2]
b[21][1:2]+b[21][2:3]
b[21][0:1]+b[21][2:3]


originally,

mmm = 5
H2 = [MM[mmm][b[i][0:1]+b[i][1:2]] for i in range(len(b))]

how to extract b[i][0:1] and b[i][1:2] as parameters?

def node(mmm, b[i][0:1], b[i][1:2]):
 H2 = [MM[mmm][A+B] for i in range(len(b))]
 return H2

node(5, b[i][0:1], b[i][1:2])
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread Antoon Pardon
Op 07-06-16 om 17:33 schreef Random832:
> On Tue, Jun 7, 2016, at 08:32, Antoon Pardon wrote:
>>> Here's a thought experiment for you. Suppose in Python 3.6, Guido announces 
>>> that Python will support a form of high-level pointer (not the scary, 
>>> dangerous 
>>> low-level pointer of C) called "reference". There will be a dereference 
>>> operator, ^, and a "reference to" operator, @. We'll be able to treat 
>>> references as first-class values:
>> That makes very little sense in python.  
> Why not? If you prefer, think of it something like:

The fact that you can make it somehow work, doesn't mean it
makes much sense. If you don't like the limitation of the
assignment semantics in python, it seems more interresting
to change that, than to work around what we have now.

Python could go the simula route, which has two kinds of
assignment. One with the python semantics and one with C
semantics.

Let as use := for the C sematics assignment and <- for the
python sematics assignment. We could then do something like
the following.

ls := [5, 8, 13, 21]
a <- ls[2]
a := 34
print ls  # [5, 8, 34, 21]

Now it may very well be possible to achieve the same results
with what you have in mind, but in my mind it makes not much
sense in trying to achieve the semantics of a copy assignment
by building extra indirection on top of reference assignments.

-- 
Antoon Pardon


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


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread Antoon Pardon
Op 07-06-16 om 18:03 schreef Steven D'Aprano:
> On Tue, 7 Jun 2016 10:32 pm, Antoon Pardon wrote:
>
>> That people often use the shortcut "x is 999" doesn't make the statement
>> wrong that variables are essentially references in Python.
> No, I'm sorry, you're wrong, variables are essentially arrays of bits in
> Python.

No they are not. The difference between variables as references and variables
as containers is an abstract notions that comes to light in the semantics
of the assignment, however these semantics are implemented.

Talking about arrays of bits is talking about a specific implementation. You
seem unable to think of a reference as an abstract model.

-- 
Antoon Pardon

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


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread Steven D'Aprano
On Wednesday 08 June 2016 17:53, Antoon Pardon wrote:

> Python could go the simula route, which has two kinds of
> assignment. One with the python semantics and one with C
> semantics.
> 
> Let as use := for the C sematics assignment and <- for the
> python sematics assignment. We could then do something like
> the following.
> 
> ls := [5, 8, 13, 21]
> a <- ls[2]
> a := 34
> print ls  # [5, 8, 34, 21]

What you seem to be describing is similar to reference parameter semantics from 
Pascal. Assignment doesn't work that way in C, or Python. In C, pointers can be 
used to simulate those semantics, but you have to explicitly dereference the 
pointer when you assign.

Using C assignment, with an array:

# excuse any minor syntax errors, my C is rusty
int A[4] = {5, 8, 13, 21};
int n
n = A[2];
n = 34;

the array A does not change. Assignment *copies* the value into the variable. 
To get the semantics you are after, you need to use a pointer with an explicit 
dereference:

int *p;
p = &A[2];
*p = 34;

which will now change the value of the array. If you leave the dereference out:

p = 34;

you are setting p to point to address 34, whatever that is.

Now, the point is, in order to deserve the term "first class", you the 
programmer shouldn't have to care about manual dereferencing. In C, you do, so 
while you can get "reference variables" in C, they're only third class. In 
Pascal, they're second class, because you can get reference variables but only 
for function/procedure parameters. Pascal doesn't generalise that to allow 
references to any arbitrary variable.

And of course Python doesn't have reference variables either. There is nothing 
you can do in Python to get this effect:

a = 1
b = a
b = 99
assert a == 99


although you can almost fake it using lists:

a = [1]
b = a
b[0] = 99
assert a == [99]


-- 
Steve

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


Re: how to extract a variable as parameter which has index using by a for loop?

2016-06-08 Thread Steven D'Aprano
On Wednesday 08 June 2016 17:31, meInvent bbird wrote:

> b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in
> range(m)]
> b[21][0:1]+b[21][1:2]
> b[21][1:2]+b[21][2:3]
> b[21][0:1]+b[21][2:3]
> 
> 
> originally,
> 
> mmm = 5
> H2 = [MM[mmm][b[i][0:1]+b[i][1:2]] for i in range(len(b))]

This is a mess. I don't understand what you are trying to do. You have these 
variable names that don't mean anything, like "b" and "H2", and others which 
aren't defined, like MM. I don't understand what you are trying to accomplish, 
or the purpose of your code.


> how to extract b[i][0:1] and b[i][1:2] as parameters?

I don't understand the question.


> def node(mmm, b[i][0:1], b[i][1:2]):
>  H2 = [MM[mmm][A+B] for i in range(len(b))]
>  return H2
> 
> node(5, b[i][0:1], b[i][1:2])

Explain what node() is supposed to do, in English. Don't write any code yet. 
What is its purpose? What does it return? What arguments does it need to take 
in order to perform its purpose?


-- 
Steve

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


Re: Creating A Pythonic API Binding

2016-06-08 Thread Steven D'Aprano
On Wednesday 08 June 2016 12:31, Lawrence D’Oliveiro wrote:

> The wrong way:
[...]

> The right way :

That's quite good.

A few minor issues:

I don't understand why you have a method that returns files called 
"get_children".

And I would expect that if you passed a relative file name to create_folder() 
it should create a folder relative to (1) the current working directory, if the 
Device has such a concept; (2) your home or user directory, if the Device has 
such a concept; or (3) relative to the photos directory. Not relative to the 
root of the Device, that is very surprising.

Also, it would be good if you could make dev a context manager, so that you 
don't have to manually close them:

with mtpy.get_raw_devices()[0].open() as dev:
p = dev.get_descendant_by_path("/DCIM/Camera")
p.get_children()
# dev is automatically closed

But other than that, it looks quite nice. Thank you for posting the link.



-- 
Steve

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


Re: how to extract a variable as parameter which has index using by a for loop?

2016-06-08 Thread meInvent bbird
just extract b[i][0:1] and b[i][1:2] out of for loop

but i depend on for loop

def node(mmm, A, B): 
 H2 = [MM[mmm][A+B] for i in range(len(b))] 
 return H2 
 
node(5, b[i][0:1], b[i][1:2]) 

it is not convenient to disclose in detail
just expect to discuss from the view of programming

On Wednesday, June 8, 2016 at 4:56:56 PM UTC+8, Steven D'Aprano wrote:
> On Wednesday 08 June 2016 17:31, meInvent bbird wrote:
> 
> > b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in
> > range(m)]
> > b[21][0:1]+b[21][1:2]
> > b[21][1:2]+b[21][2:3]
> > b[21][0:1]+b[21][2:3]
> > 
> > 
> > originally,
> > 
> > mmm = 5
> > H2 = [MM[mmm][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
> 
> This is a mess. I don't understand what you are trying to do. You have these 
> variable names that don't mean anything, like "b" and "H2", and others which 
> aren't defined, like MM. I don't understand what you are trying to 
> accomplish, 
> or the purpose of your code.
> 
> 
> > how to extract b[i][0:1] and b[i][1:2] as parameters?
> 
> I don't understand the question.
> 
> 
> > def node(mmm, b[i][0:1], b[i][1:2]):
> >  H2 = [MM[mmm][A+B] for i in range(len(b))]
> >  return H2
> > 
> > node(5, b[i][0:1], b[i][1:2])
> 
> Explain what node() is supposed to do, in English. Don't write any code yet. 
> What is its purpose? What does it return? What arguments does it need to take 
> in order to perform its purpose?
> 
> 
> -- 
> Steve
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread Antoon Pardon
Op 08-06-16 om 10:47 schreef Steven D'Aprano:
> On Wednesday 08 June 2016 17:53, Antoon Pardon wrote:
>
>> Python could go the simula route, which has two kinds of
>> assignment. One with the python semantics and one with C
>> semantics.
>>
>> Let as use := for the C sematics assignment and <- for the
>> python sematics assignment. We could then do something like
>> the following.
>>
>> ls := [5, 8, 13, 21]
>> a <- ls[2]
>> a := 34
>> print ls  # [5, 8, 34, 21]
> What you seem to be describing is similar to reference parameter semantics 
> from 
> Pascal. Assignment doesn't work that way in C, or Python.

I disagree. In python the assignment does work similar to the reference 
parameter
semantics in pascal. See the following

  A = range[4]
  B = A
  B[2] = 5
  print A # [0, 1, 5, 2]

This is exactly the result you would get with B as a reference parameter in 
pascal.

> And of course Python doesn't have reference variables either. There is 
> nothing 
> you can do in Python to get this effect:
>
> a = 1
> b = a
> b = 99
> assert a == 99

It is true that you can't get such an effect in python, but that doesn't imply
that python doesn't have reference variables (as an abstract notion). Because
having reference variables doesn't imply you can have that effect. If all
you have is reference variables and assignments just change what object is 
refered
to, you can't have such an effect either.

-- 
Antoon Pardon.

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


Re: how to extract a variable as parameter which has index using by a for loop?

2016-06-08 Thread Joonas Liik
On 8 June 2016 at 12:20, meInvent bbird  wrote:
> just extract b[i][0:1] and b[i][1:2] out of for loop
>
> but i depend on for loop
>
> def node(mmm, A, B):
>  H2 = [MM[mmm][A+B] for i in range(len(b))]
>  return H2
>
> node(5, b[i][0:1], b[i][1:2])
>
> it is not convenient to disclose in detail
> just expect to discuss from the view of programming
>
> On Wednesday, June 8, 2016 at 4:56:56 PM UTC+8, Steven D'Aprano wrote:
>> On Wednesday 08 June 2016 17:31, meInvent bbird wrote:
>>
>> > b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in
>> > range(m)]
>> > b[21][0:1]+b[21][1:2]
>> > b[21][1:2]+b[21][2:3]
>> > b[21][0:1]+b[21][2:3]
>> >
>> >
>> > originally,
>> >
>> > mmm = 5
>> > H2 = [MM[mmm][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
>>
>> This is a mess. I don't understand what you are trying to do. You have these
>> variable names that don't mean anything, like "b" and "H2", and others which
>> aren't defined, like MM. I don't understand what you are trying to 
>> accomplish,
>> or the purpose of your code.
>>
>>
>> > how to extract b[i][0:1] and b[i][1:2] as parameters?
>>
>> I don't understand the question.
>>
>>
>> > def node(mmm, b[i][0:1], b[i][1:2]):
>> >  H2 = [MM[mmm][A+B] for i in range(len(b))]
>> >  return H2
>> >
>> > node(5, b[i][0:1], b[i][1:2])
>>
>> Explain what node() is supposed to do, in English. Don't write any code yet.
>> What is its purpose? What does it return? What arguments does it need to take
>> in order to perform its purpose?
>>
>>
>> --
>> Steve
> --
> https://mail.python.org/mailman/listinfo/python-list

for a start..
a few things to improve readability.. (as well as perf maybe)

b[i][0:1]+b[i][1:2] == b[i][0]+b[i][1]

possibly dependant of on the data type of b[i] possibly..
b[i][0]+b[i][1] ==  b[i][0:2]

also..

>> > b[21][0:1]+b[21][1:2]
>> > b[21][1:2]+b[21][2:3]
>> > b[21][0:1]+b[21][2:3]
 this is.. really confusing..
usually when you add things you would assign the result to something
or use it in another way.. so this code looks like it is totally
useless.

it might not be 100% useless but if it is.. it is such horrible code u
rly need to burn it with fire .

since u use b[21] 6 times here you should just extract it as a
separate variable  eg..

b21 = b[21]
A = b21[0:1]+b21[1:2]
B = b21[1:2]+b21[2:3]
C = b21[0:1]+b21[2:3]
that is ofc if


now this is rly interesting.. i think there is a big misunderstanding
about what function arguments are..

def node(mmm, b[i][0:1], b[i][1:2]):
 H2 = [MM[mmm][A+B] for i in range(len(b))]
 return H2

node(5, b[i][0:1], b[i][1:2])

if i can guess your intent correctly.. what you actually want is sth like

def node(mmm, b01, b12):
 # b01 = b[i][0:1]; b12 = b[i][1:2]
 # since you obviously omitted some code you might want to take in
some other params as well.. like the variable ´b´ for example.
 # i hope this is at least a little helpful, it really is hard to give
constructive feedback if you cant even post the actual code nor can
you give any info about what you are attempting to do
 H2 = [MM[mmm][A+B] for i in range(len(b))]
 return H2

node(5, b[i][0:1], b[i][1:2])

also it helps to give meaningful names to things, i guarantee next
week even you will be having trouble reading this code...
/now what the heck was that MM thing.. i wonder..  i could totally fix
it if i only named it sth decent../
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Q] ImportError by __import__() on Python >= 3.4

2016-06-08 Thread Makoto Kuwata
On Tue, Jun 7, 2016 at 7:28 AM, Michael Selik 
wrote:

> On Thu, Jun 2, 2016 at 10:06 AM Makoto Kuwata  wrote:
>
>> I have a trouble around __import__().
>>
>
> The docs for __import__ strongly recommend that you use importlib instead
> https://docs.python.org/3.5/library/importlib.html#importlib.import_module
>
> The docs for ``importlib.import_module`` suggest that you use
> ``invalidate_caches`` if you are importing dynamically generated files.
>
> https://docs.python.org/3.5/library/importlib.html#importlib.invalidate_caches
>
> I think you'll find this fixes your bug.
>

Great!
I replaced '__import__()' in my code  with 'importlib.invalidate_cache();
importlib.import_module()'
and found it worked very well.
Thank you very much.

--
regards,
makoto
-- 
https://mail.python.org/mailman/listinfo/python-list



Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread BartC

On 08/06/2016 10:41, Antoon Pardon wrote:

Op 08-06-16 om 10:47 schreef Steven D'Aprano:

On Wednesday 08 June 2016 17:53, Antoon Pardon wrote:


Python could go the simula route, which has two kinds of
assignment. One with the python semantics and one with C
semantics.

Let as use := for the C sematics assignment and <- for the
python sematics assignment. We could then do something like
the following.

ls := [5, 8, 13, 21]
a <- ls[2]
a := 34
print ls  # [5, 8, 34, 21]

What you seem to be describing is similar to reference parameter semantics from
Pascal. Assignment doesn't work that way in C, or Python.


I disagree. In python the assignment does work similar to the reference 
parameter
semantics in pascal. See the following

  A = range[4]
  B = A
  B[2] = 5
  print A # [0, 1, 5, 2]


(Did you mean range(4) and [0, 1, 5, 3]?)


This is exactly the result you would get with B as a reference parameter in 
pascal.


I can't remember exactly how Pascal worked. But your assignment to B[2] 
is an in-place modification. That sort of thing Python can do (when 
allowed as some things are not mutable) with its existing reference system.


But a 'proper' reference allows a complete replacement of what it refers 
to. That would mean being able to do:


  B = "Cat"
  print A # "Cat"

No tricks involving in-place updates such as assigning to list elements 
are needed.


--
Bartc


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


Want to play with or learn a parser system including a grammar for Python? See spark_parser on pypy

2016-06-08 Thread rocky
For those who are interested in experimenting with parser systems in Python, 
there has been one around for a long while. But in my opinion it was a bit 
lacking in graded example demonstrating how to use it.

So in the recently in spark_parser 1.3.0 [1], I've beefed up the examples a 
little. In addition to the example programs which give the classic arithmetic 
expression evaluator, I now include the beginnings of a full Python 2.6 
language. The semantic routines just reformat the input which was parsed into a 
tree and spit that back out formatted according to predefined rules. Included 
is the grammar file from the 2.6.9 distribution.

For those who want to go further and see a full production-like example, see 
the uncompyle6 github repository[2] which uses this. It reads bytecode from 
Python versions 2.3 to 3.5 or so and produces the corresponding Python 2 or 
Python 3 source code.

Alternatively if you want to understand how uncompyle6 works, such as to 
improve it or fix bugs, you'll probably need to understand how the parser 
system it uses works.

[1] https://pypi.python.org/pypi/spark_parser/1.3.0
[2] https://github.com/rocky/python-uncompyle6
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread Antoon Pardon
Op 08-06-16 om 12:33 schreef BartC:
> On 08/06/2016 10:41, Antoon Pardon wrote:
>> Op 08-06-16 om 10:47 schreef Steven D'Aprano:
>>> On Wednesday 08 June 2016 17:53, Antoon Pardon wrote:
>>>
 Python could go the simula route, which has two kinds of
 assignment. One with the python semantics and one with C
 semantics.

 Let as use := for the C sematics assignment and <- for the
 python sematics assignment. We could then do something like
 the following.

 ls := [5, 8, 13, 21]
 a <- ls[2]
 a := 34
 print ls  # [5, 8, 34, 21]
>>> What you seem to be describing is similar to reference parameter
>>> semantics from
>>> Pascal. Assignment doesn't work that way in C, or Python.
>>
>> I disagree. In python the assignment does work similar to the
>> reference parameter
>> semantics in pascal. See the following
>>
>>   A = range[4]
>>   B = A
>>   B[2] = 5
>>   print A # [0, 1, 5, 2]
>
> (Did you mean range(4) and [0, 1, 5, 3]?)

Yes, sorry about that.

>
> But a 'proper' reference allows a complete replacement of what it
> refers to. That would mean being able to do:
>
>   B = "Cat"
>   print A # "Cat"
>
> No tricks involving in-place updates such as assigning to list
> elements are needed.

No it doesn't mean that. It means that if you mutate the object through one 
variable,
you can see the result of that mutation through the other variable. But if the
assignment doesn't mutate, you can't have such effect through assignment.

In python, you can sometimes simulate a mutating assignment and then we get 
this.

>>> A = [8, 5, 3, 2]
>>> B = A
>>> B[:] = [3, 5, 8, 13]
>>> A
[3, 5, 8, 13]

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


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread Rustom Mody
On Wednesday, June 8, 2016 at 4:32:33 PM UTC+5:30, Antoon Pardon wrote:

> It means that if you mutate the object through one variable,
> you can see the result of that mutation through the other variable. But if the
> assignment doesn't mutate, you can't have such effect through assignment.
> 

Its called aliasing:
https://en.wikipedia.org/wiki/Aliasing_%28computing%29
And is the principal reason why we need to talk in terms of 
pointer/references/box-n-arrows
etc

> In python, you can sometimes simulate a mutating assignment and then we get 
> this.
> 
> >>> A = [8, 5, 3, 2]
> >>> B = A
> >>> B[:] = [3, 5, 8, 13]
> >>> A
> [3, 5, 8, 13]

Neat example -- thanks -- something for my tomorrow class
More telling than the one I usually use to talk of this stuff which runs thus:

>>> a=[[1,2,],[1,2]]
>>> a
[[1, 2], [1, 2]]
>>> inner=[1,2]
>>> b=[inner,inner]
>>> b
[[1, 2], [1, 2]]
# So a and b look the same


>>> a == b
True
# So even python thinks them the same

# But Uh... oh... 
>>> a[0][0]=3
>>> a
[[3, 2], [1, 2]]
>>> b[0][0]=3
>>> b
[[3, 2], [3, 2]]
# They dont behave the same!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread BartC

On 08/06/2016 12:01, Antoon Pardon wrote:

Op 08-06-16 om 12:33 schreef BartC:



But a 'proper' reference allows a complete replacement of what it
refers to. That would mean being able to do:

  B = "Cat"
  print A # "Cat"

No tricks involving in-place updates such as assigning to list
elements are needed.


No it doesn't mean that. It means that if you mutate the object through one 
variable,
you can see the result of that mutation through the other variable. But if the
assignment doesn't mutate, you can't have such effect through assignment.

In python, you can sometimes simulate a mutating assignment and then we get 
this.

>>> A = [8, 5, 3, 2]
>>> B = A
>>> B[:] = [3, 5, 8, 13]
>>> A
[3, 5, 8, 13]


Well, it then becomes necessary to separate a mutating assignment 
(a[i]=b) where the left-hand-size modifies part of a larger object, from 
a full assignment (a=b) which replaces a whole object (the value of a) 
with another.


So you have partial updates and full updates. A proper reference will be 
able to do both via the reference. Python can only do a partial update 
and the reason is that the reference points to the object, not the 
variable; there is no way to change the variable to link it with 
another, distinct object.


If the object is a list, then that can be modified to any extent, even 
replacing the contents completely, but it will still be a list. In the 
case of an int or string, then it's impossible to change. So there are 
limitations to what can be done.


Getting back to Pascal (as I /can/ remember how reference parameters 
work for integers), assigning to a reference integer parameter in a 
function will change the caller's version. Python can only emulate that 
by passing a one-element list or using some such trick. Affecting 
readability and, likely, performance.


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


Recursive type annotations

2016-06-08 Thread Nagy László Zsolt

class Test:
def test(self, child : Test):
pass

NameError: name 'Test' is not defined

I understand that the class "Test" is not defined until the class
definition is executed. But it is very very common to build recursive
data structures, and I have concrete use case where the IDE did not
recognize the type of the argument, and as a result it forgot to rename
some method calls when I auto-refactored the name of the method.

I'm not an expert, but I believe that these annotations are not used by
the byte compiler for anything. This is just pure syntax introduced for
the person who reads the code.

Is there a known obsticle that would prevent us from detecting recursive
type annotations? (E.g. bind the annotation to the class that is being
defined.)

Thanks,

   Laszlo


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


Re: Recursive type annotations

2016-06-08 Thread Jon Ribbens
On 2016-06-08, Nagy László Zsolt  wrote:
> class Test:
> def test(self, child : Test):
> pass
>
> NameError: name 'Test' is not defined

I think you can fix this by using a string annotation as follows:

class Test:
def test(self, child: "Test"):
pass
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: [Q] ImportError by __import__() on Python >= 3.4

2016-06-08 Thread Michael Selik
By the way, why choose to write, import, and delete modules? I'd think
exec'ing code would be sufficient.

On Wed, Jun 8, 2016, 5:52 AM Makoto Kuwata  wrote:

> On Tue, Jun 7, 2016 at 7:28 AM, Michael Selik 
> wrote:
>
>> On Thu, Jun 2, 2016 at 10:06 AM Makoto Kuwata  wrote:
>>
>>> I have a trouble around __import__().
>>>
>>
>> The docs for __import__ strongly recommend that you use importlib instead
>> https://docs.python.org/3.5/library/importlib.html#importlib.import_module
>>
>> The docs for ``importlib.import_module`` suggest that you use
>> ``invalidate_caches`` if you are importing dynamically generated files.
>>
>> https://docs.python.org/3.5/library/importlib.html#importlib.invalidate_caches
>>
>> I think you'll find this fixes your bug.
>>
>
> Great!
> I replaced '__import__()' in my code  with 'importlib.invalidate_cache();
> importlib.import_module()'
> and found it worked very well.
> Thank you very much.
>
> --
> regards,
> makoto
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Recursive type annotations

2016-06-08 Thread Nagy László Zsolt

>> pass
>>
>> NameError: name 'Test' is not defined
> I think you can fix this by using a string annotation as follows:
>
> class Test:
> def test(self, child: "Test"):
> pass
Yes, you are right. It is not directly written in the official
documentation ( https://docs.python.org/3/library/typing.html ), but it
is in the PEP 0484, section "Forward references".

Thanks!

   Laszlo

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


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread Antoon Pardon
Op 08-06-16 om 14:34 schreef BartC:
>
> So you have partial updates and full updates. A proper reference will
> be able to do both via the reference. Python can only do a partial
> update and the reason is that the reference points to the object, not
> the variable; there is no way to change the variable to link it with
> another, distinct object.
>
> If the object is a list, then that can be modified to any extent, even
> replacing the contents completely, but it will still be a list. In the
> case of an int or string, then it's impossible to change. So there are
> limitations to what can be done.
>
> Getting back to Pascal (as I /can/ remember how reference parameters
> work for integers), assigning to a reference integer parameter in a
> function will change the caller's version. Python can only emulate
> that by passing a one-element list or using some such trick. Affecting
> readability and, likely, performance.
>
I don't see why we should determine what a /proper/ reference
can do, based on what it does in one specific language.

You can do something like that in simula, but only because
simula has two kinds of assignments. One kind that is
simular to python and one that is similar to C.
The one that is similar that python is the reference assignment.

I don't see why the variables in python wouldn't be reference
variables, just because python lacks the "normal" assignment
from simula.

It is possible to write simula programs that almost exclusively
use reference assignments. Limiting yourself like that, would
also not allow you to do /proper/ referencing.

It seems weird to refuse to call something a /proper/ reference,
while what is missing is not a reference assignment but a
mutating assignment.

-- 
Antoon Pardon.

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


Re: Recursive type annotations

2016-06-08 Thread Ian Kelly
On Wed, Jun 8, 2016 at 7:31 AM, Nagy László Zsolt  wrote:
>
>>> pass
>>>
>>> NameError: name 'Test' is not defined
>> I think you can fix this by using a string annotation as follows:
>>
>> class Test:
>> def test(self, child: "Test"):
>> pass
> Yes, you are right. It is not directly written in the official
> documentation ( https://docs.python.org/3/library/typing.html ), but it
> is in the PEP 0484, section "Forward references".

That link specifically documents the typing module. Forward references
don't really have anything to do with the typing module and are best
covered by the documentation of the static checker in use. Here's
where the MyPy documentation covers it:

http://mypy.readthedocs.io/en/latest/kinds_of_types.html#class-name-forward-references
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread Marko Rauhamaa
Antoon Pardon :

> You can do something like that in simula, but only because
> simula has two kinds of assignments. One kind that is
> simular to python and one that is similar to C.
> The one that is similar that python is the reference assignment.

I see Python as doing the exact same thing with variables as C.

What is different is that in Python, every expression evaluates to a
pointer. Thus, you can only assign pointers to variables.


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


Re: Want to play with or learn a parser system including a grammar for Python? See spark_parser on pypy

2016-06-08 Thread Robin Becker

On 08/06/2016 11:38, rocky wrote:
...

[1] https://pypi.python.org/pypi/spark_parser/1.3.0

...
the page above shows one can implement a time travel machine as it boldly states

"The original version of this was written by John Aycock and was described in 
his 1988 paper: “Compiling Little Languages in Python” at the 7th International 
Python Conference."


I suppose those early primitive pythons had some issue with integers as 
wackypedia states that


"Python was conceived in the late 1980s[1] and its implementation was started in 
December 1989[2] by Guido van Rossum at CWI in the Netherlands"


so that Aycocks's paper must have been at the -1st Python Conference
-parallely yrs-
Robin Becker

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


Design an encrypted time-limited API on Client/Server side

2016-06-08 Thread iMath
​I am planning design an encrypted time-limited API on both Client and Server 
sides, the server side is written in Django, the client side is a GUI program 
which call the API by
import requests
c = requests.post("http://127.0.0.1:8000/VideoParser/";, data={'videoUrl': 
videoUrl })
The way it call the API is desperately exposed to those who can use network 
traffic capturing tools like wireshark and fiddler, while I don't want anyone 
else could call the API with their customized videoUrl, and if people made the 
post call with the same parameters 2 minutes later after the client initially 
made the call, the call should be valid or expired, so how to design the 
encrypted time-limited API on both Client and Server side in this case ?

P.S. I think add an identifier to the post data could prevent them using the API

import requests
c = requests.post("http://127.0.0.1:8000/VideoParser/";, data={'videoUrl': 
videoUrl, 'identifier':value_of_identifier })
provided there is something encrypted in the value_of_identifier and it changes 
with each call, but I don't know how to get started, any idea ?

It would be better to show some code , I really don't know which modules to use 
and  how to start to write code.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread BartC

On 08/06/2016 15:18, Antoon Pardon wrote:

Op 08-06-16 om 14:34 schreef BartC:


So you have partial updates and full updates. A proper reference will
be able to do both via the reference. Python can only do a partial
update and the reason is that the reference points to the object, not
the variable; there is no way to change the variable to link it with
another, distinct object.

If the object is a list, then that can be modified to any extent, even
replacing the contents completely, but it will still be a list. In the
case of an int or string, then it's impossible to change. So there are
limitations to what can be done.

Getting back to Pascal (as I /can/ remember how reference parameters
work for integers), assigning to a reference integer parameter in a
function will change the caller's version. Python can only emulate
that by passing a one-element list or using some such trick. Affecting
readability and, likely, performance.


I don't see why we should determine what a /proper/ reference
can do, based on what it does in one specific language.


Because there are some things that such references can do that Python 
can't do with its object reference model, not without some difficulty or 
having to write convoluted code.


And it doesn't really depend on the language as the differences are easy 
to demonstrate, provided the language still has something along the 
lines of:


  a = b

to do normal assignment.

(1) Reference parameters

   def fn(&x):
  x=1000

   a="ABC"
   fn(a)

   print (a)# should produce 1000

I've used "&" here as a device to make the 'x' param work as though it 
was passed by reference. That is, an /additional/ reference to the one 
Python might already use behind the scenes.


(2) Replace (not just modify) a variable's value and type indirectly

  a = 17
  p = &a

  *p = "Dog"
  print a  # should be "Dog"

Here "&" and "*" are used to indicate possible 'reference'/dereference 
operations.


With the last example, then this Python code:

  a = 17
  p = a

Might result in the following internal structures:

  0x300:a:[pyref:0x10020] Variable
  0x400:p:[pyref:0x10020] Variable
  0x10020:[int: 17]   Object

With the p = &a version, it would be more like:

  0x300:a:[pyref:0x10020] Variable
  0x400:p:[pyref:0x10040] Variable
  0x10020:[int: 17]   Object
  0x10040:[varref: 0x300] Object

It's that 0x300 reference to 'a' itself, not just the object linked to 
it, that is what Python can't do.


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


Re: Want to play with or learn a parser system including a grammar for Python? See spark_parser on pypy

2016-06-08 Thread rocky
On Wednesday, June 8, 2016 at 12:50:57 PM UTC-4, Robin Becker wrote:
> On 08/06/2016 11:38, rocky wrote:
> ...
> > [1] https://pypi.python.org/pypi/spark_parser/1.3.0
> ...
> the page above shows one can implement a time travel machine as it boldly 
> states
> 
> "The original version of this was written by John Aycock and was described in 
> his 1988 paper: “Compiling Little Languages in Python” at the 7th 
> International 
> Python Conference."
> 
> I suppose those early primitive pythons had some issue with integers as 
> wackypedia states that
> 
> "Python was conceived in the late 1980s[1] and its implementation was started 
> in 
> December 1989[2] by Guido van Rossum at CWI in the Netherlands"
> 
> so that Aycocks's paper must have been at the -1st Python Conference
> -parallely yrs-
> Robin Becker

Sorry that should have been 1998 which would make more sense for the 7th  
conference if the 1st one was around 2001. I've corrected the date in [1] 
https://pypi.python.org/pypi/spark_parser/1.3.0 

The automated tests in the package just don't  catch stuff like this. But I'm 
sure there are also other mistakes as well in there so feel free to let me know.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Possible PEP - two dimensional arrays?

2016-06-08 Thread Michael Torrie
On 06/07/2016 06:17 PM, Harrison Chudleigh wrote:
> I was programming a computer game and found that while 1D arrays can be
> created using the module array, there is no module for two-dimensional
> arrays, unlike languages like C. Currently, the closest thing Python has to
> a 2D array is a dictionary containing lists.

I think you meant to say a list of lists.  Which is actually analogous
to how C implements them.
-- 
https://mail.python.org/mailman/listinfo/python-list


MAthematical formula terms make for terrible names in a program (was: What's good for mathematical formulas can be bad for program code (was: how to extract a variable as parameter which has index usi

2016-06-08 Thread Ben Finney
Steven D'Aprano  writes:

> On Wednesday 08 June 2016 17:31, meInvent bbird wrote:
>
> > H2 = [MM[mmm][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
>
> This is a mess. I don't understand what you are trying to do. You have
> these variable names that don't mean anything, like "b" and "H2", and
> others which aren't defined, like MM. I don't understand what you are
> trying to accomplish, or the purpose of your code.

The nice thing about mathematical formulas is that one has latitude, in
the accompanying academic paper, to define at length the
single-character names. This means the formulas are briefer to write
by hand on the blackboard, and the reader can refer to the author's
extensive explanation of what the character means.

Mathematicians (and other scientists), please don't attempt to cram
meaning into single-letter names in your program code. None of the above
advantages apply, and we get only the disadvantages Steven describes.

Instead, choose meaningful names that someone who knows that meaning has
at least a better-than-random chance of remembering.

-- 
 \   “The fundamental principle of science, the definition almost, |
  `\ is this: the sole test of the validity of any idea is |
_o__) experiment.” —Richard P. Feynman |
Ben Finney

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


Re: how to extract a variable as parameter which has index using by a for loop?

2016-06-08 Thread Ben Finney
meInvent bbird  writes:

> it is not convenient to disclose in detail
> just expect to discuss from the view of programming

It is not convenient to discuss programming without a good understanding
of what the program is meant to do.

(Also, please don't top-post, instead post your responses interleaved
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>.)

-- 
 \ “The most dangerous man to any government is the man who is |
  `\   able to think things out for himself, without regard to the |
_o__)  prevailing superstitions and taboos.” —Henry L. Mencken |
Ben Finney

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


Re: Recommendation for GUI lib?

2016-06-08 Thread Dietmar Schwertberger

On 07.06.2016 23:45, Roland Koebler via Python-list wrote:

Both are free, have Python bindings and a graphical GUI designer, and both
have ports for Windows and Mac OS X. Qt does have a better cross-platform-
support and supports more platforms, but GTK+3 also works for Linux, Mac
OS X and Windows.

What is the current state and quality of GTK for Windows and Mac OS?
IIRC it's not too long that Wing IDE was moved from PyGTK to Qt due to 
the problems under Windows and MacOS.



Regards,

Dietmar

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


movie from pictures

2016-06-08 Thread nevzat . guler
Dear Users, 
I am trying to combine several pictures to create a movie file. I create the 
pictures in a loop. I would like to add each picture to a movie within the 
loop, like it is done in MATLAB. However, it is also acceptable to create all 
the picture in the loop, afterwards use some ffmpeg command to combine them 
into a picture. I actually tried the latter option but i ran into trouble with 
paths because the picture location and python code are in different places. The 
former method is also better because I would like to use variable name for the 
picture names, instead of their given name, which are long and have spaces, 
special characters in them.. I will appreciate any guidance here.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Want to play with or learn a parser system including a grammar for Python? See spark_parser on pypy

2016-06-08 Thread Gregory Ewing

Robin Becker wrote:
"Python was conceived in the late 1980s[1] and its implementation was 
started in December 1989[2] by Guido van Rossum at CWI in the Netherlands"


so that Aycocks's paper must have been at the -1st Python Conference


When the time machine was invented, Guido thought it would be
fun to go back and hold a Python conference a little before
Python actually existed, to mess with future historians
trying to put together a timeline for Python development.

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


Re: movie from pictures

2016-06-08 Thread Lawrence D’Oliveiro
On Thursday, June 9, 2016 at 10:11:16 AM UTC+12, Nev wrote:

> ... afterwards use some ffmpeg command to combine them into a picture.

This is a very common use case for FFmpeg.
1) Generate your frames in some high-quality lossless format, like PNG.
2) Name them sequentially “0001.png”, “0002.png” etc. (Assuming 4 digits is 
enough here.)
3) Use an FFmpeg command like

ffmpeg -i /input-dir/%04d.png «output settings» «output filename»

to generate the final movie.

By keeping the full-quality frames from step 2, you can retry step 3 with 
different encoding settings to see which works best. You can even try two-pass 
encoding for best quality.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: for / while else doesn't make sense

2016-06-08 Thread Lawrence D’Oliveiro
On Wednesday, June 8, 2016 at 5:27:41 PM UTC+12, Marko Rauhamaa wrote:

> Someone mentioned you had a 500-line function.

In my undergraduate Comp Sci classes, we used to discuss arbitrary rules like 
limiting functions to n lines. With real-world experience, it soon became clear 
that such rules were a waste of time. A function should be just as big as it 
needs to be, no more, no less. The same with a class, or a module. Or whatever 
other constructs your language may have.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: I'm wrong or Will we fix the ducks limp?

2016-06-08 Thread Lawrence D’Oliveiro
On Thursday, June 9, 2016 at 4:37:58 AM UTC+12, Marko Rauhamaa wrote:
> I see Python as doing the exact same thing with variables as C.
> 
> What is different is that in Python, every expression evaluates to a
> pointer. Thus, you can only assign pointers to variables.

Yup. I think some people are getting confused over assignment to simple 
variables as opposed to more complex expressions on the LHS of assignments in 
Python. For example, given

a = [1, 2, 3, 4]

compare the different effects of

b = a
b = a[:]
b[:] = a # assuming b already has an appropriate initial value

not to mention other possibilities...
-- 
https://mail.python.org/mailman/listinfo/python-list


xlwt 1.1.2 released!

2016-06-08 Thread Chris Withers

Hi All,

I'm pleased to announce the release of xlwt 1.1.2.

This release contains the following:

- Fix failure in style compression under Python 3.

- Documentation tweaks.

If you find any problems, please ask about them on the 
[email protected] list, or submit an issue on GitHub:


https://github.com/python-excel/xlwt/issues

There's also details of all things Python and Excel related here:

http://www.python-excel.org/

NB: If you would like to become the maintainer of xlwt, please get in 
touch! Neither myself nor John Machin have much time to drive things 
forward nowadays, hence the year or so between each release...


cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
  - http://www.simplistix.co.uk
--
https://mail.python.org/mailman/listinfo/python-list


xlutils 2.0.0 released!

2016-06-08 Thread Chris Withers

Hi All,

I'm pleased to announce the release of xlutils 2.0.0:

http://pypi.python.org/pypi/xlutils/2.0.0

This release has a couple of small changes:

- Updated documentation.

- Move to virtualenv/pip based development.

- Move to Read The Docs for documentation.

- Use Travis CI for testing and releases.

- Use features of newer testfixtures in
  xlutils.view.CheckerView.

- Python 3 compatibility.

Full docs here:

http://xlutils.readthedocs.org/

Details of all things Python and Excel related can be found here:

http://www.python-excel.org/

cheers,

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
 - http://www.simplistix.co.uk
--
https://mail.python.org/mailman/listinfo/python-list


run code error with numpy and scipy

2016-06-08 Thread meInvent bbird
when i run code in window has an error not a valid win32 dll

then i install scipy first in this site, then install numpy-mkl
all python 2.7 and win32

http://www.lfd.uci.edu/~gohlke/pythonlibs/

then i run code below, got error , and captured screen in this link

https://drive.google.com/file/d/0Bxs_ao6uuBDUQXROd2VqSURGa00/view?usp=sharing

import numpy as np
from scipy.sparse.linalg import svds
from functools import partial
from scipy.sparse import csr_matrix

def emsvd(Y, k=None, tol=1E-3, maxiter=None):
 if k is None:
  svdmethod = partial(np.linalg.svd, full_matrices=False)
 else:
  svdmethod = partial(svds, k=k)
 if maxiter is None:
  maxiter = np.inf

 muhat = np.nanmean(Y, axis=0, keepdims=1)
 valid = np.isfinite(Y)
 Y_hat = np.where(valid, Y, muhat)

 halt = False
 ii = 1
 v_prev = 0

 while not halt:
  U, s, Vt = svdmethod(Y_hat - muhat)
  Y_hat[~valid] = (U.dot(np.diag(s)).dot(Vt) + muhat)[~valid]
  muhat = Y_hat.mean(axis=0, keepdims=1)
  v = s.sum()
  if ii >= maxiter or ((v - v_prev) / v_prev) < tol:
   halt = True
   ii += 1
   v_prev = v
 return Y_hat, muhat, U, s, Vt


A = csr_matrix([[1, 2, 0], [0, 0, 3], [4, 0, 5]])
emsvd(A)

np.nanmean(A, axis=0, keepdims=1)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: run code error with numpy and scipy

2016-06-08 Thread Ben Finney
meInvent bbird  writes:

> when i run code in window has an error not a valid win32 dll

Simplify the example, to diagnose it. What is the *simplest* example
code that shows the problem?

In other words, remove statements from the example until the problem no
longer happens. Then add back *one* statement to make it happen, and
present that example.

-- 
 \ “I have had a perfectly wonderful evening, but this wasn't it.” |
  `\ —Groucho Marx |
_o__)  |
Ben Finney

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


which library has map reduce and how to use it for this case

2016-06-08 Thread Ho Yeung Lee
i got M1 to M5 and V6 operator

and would like to do a full combination and result will also act among each 
other, 

map reduce attract my application

how to use this in this example?

actually below is like vlookup
then example is op3(op2(op1(x->y, y->z)), x->z)

search which combinations will result in a column result is 1


M1 = {}
M2 = {}
M3 = {}
M4 = {}
M5 = {}
V6 = {}
M1['00']=0
M1['01']=1
M1['02']=1
M1['10']=1
M1['11']=1
M1['12']=1
M1['20']=1
M1['21']=1
M1['22']=1
M2['00']=0
M2['01']=0
M2['02']=1
M2['10']=0
M2['11']=1
M2['12']=1
M2['20']=1
M2['21']=1
M2['22']=1
M3['00']=0
M3['01']=0
M3['02']=0
M3['10']=0
M3['11']=1
M3['12']=1
M3['20']=0
M3['21']=1
M3['22']=1
M4['00']=0
M4['01']=1
M4['02']=0
M4['10']=1
M4['11']=1
M4['12']=1
M4['20']=0
M4['21']=1
M4['22']=1
M5['00']=0
M5['01']=0
M5['02']=0
M5['10']=0
M5['11']=1
M5['12']=1
M5['20']=0
M5['21']=1
M5['22']=1
V6['00']=1
V6['01']=1
V6['02']=1
V6['10']=1
V6['11']=1
V6['12']=1
V6['20']=0
V6['21']=1
V6['22']=1
MM = {}
MM[0] = M1
MM[1] = M2
MM[2] = M3
MM[3] = M4
MM[4] = M5
MM[5] = V6
m = 3
b = [str(i)+str(j)+str(k) for i in range(m) for j in range(m) for k in range(m)]
b[21][0:1]+b[21][1:2]
b[21][1:2]+b[21][2:3]
b[21][0:1]+b[21][2:3]

#def node(mmm, A):
 #H2 = [MM[mmm][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
 #return H2

#node(5, b[i][0:1]+b[i][1:2])


H2 = [MM[5][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
H3 = [MM[5][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
[str(k)+str(v) for k, v in zip(H2, H3)]

import itertools
deep = 3

finalresult = []
def DFS(H2, H3, b, deep, mresult)
 mylist = [i for i in range(0,7-1)]
 for aa,bb in itertools.combinations(mylist, 2):
  print(aa,bb)
  if deep == 3:
   #op0, op1
   op1xy = [MM[aa][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
   op1yz = [MM[aa][b[i][1:2]+b[i][2:3]] for i in range(len(b))]
   op1xz = [MM[aa][b[i][0:1]+b[i][2:3]] for i in range(len(b))]
   op2xy = [MM[bb][b[i][0:1]+b[i][1:2]] for i in range(len(b))]
   op2yz = [MM[bb][b[i][1:2]+b[i][2:3]] for i in range(len(b))]
   op2xz = [MM[bb][b[i][0:1]+b[i][2:3]] for i in range(len(b))]
   mresult.append(op1xy)
   mresult.append(op1yz)
   mresult.append(op1xz)
   mresult.append(op2xy)
   mresult.append(op2yz)
   mresult.append(op2xz)
  else:
   H1 = H2
   H9 = H3
  ba = b
  b = [str(k)+str(v) for k, v in zip(H2, H3)]
  DFS(H1, H9, b)
  b = ba

DFS(0, 0, 0, 3, finalresult)
-- 
https://mail.python.org/mailman/listinfo/python-list