Re: Making Classes Subclassable

2016-07-05 Thread Lawrence D’Oliveiro
On Tuesday, July 5, 2016 at 5:27:01 PM UTC+12, Michael Selik wrote:
> On Mon, Jul 4, 2016, 4:36 AM Lawrence D’Oliveiro wrote:
> 
>> On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
>>>
>>> --> "type(obj)" or "obj.__class__" (there are small differences)
>>> give you the type/class of "obj".
>>
>> When would it not be the same?
> 
> Old-style classes.

Which don’t exist any more.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Making Classes Subclassable

2016-07-05 Thread dieter
Lawrence D’Oliveiro  writes:

> On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
>> --> "type(obj)" or "obj.__class__" (there are small differences)
>> give you the type/class of "obj".
>
> When would it not be the same?

Special proxy objects, i.e. objects that try to behave as much
as possible like another object.

For those objects, you may either want the class of the proxy itself
(--> "type(obj)") or the class of the proxied object (--> "obj.__class__").


Moreover, you might have special descriptors installed which may
affect how "obj.__class__" is resolved.

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


Re: Need help compiling Python-devel

2016-07-05 Thread dieter
TM  writes:

> I have successfully compiled Python-2.7.12 on AIX 6.1 TL09, using steps
> below. However I need the python-devel library/headers. How do I compile
> Python, so that I can use this?

The distinction between "python" and "python-devel" is not a Python
notion but one of package management systems.
"Usual" users of a package do in fact not need the complete package
but only part (the binary part) of it. Only "special" users need more
parts (especially headers, maybe libraries). Therefore, many
package management systems have for most packages two variants
"XXX" (for "typical" users) and "XXX-dev[el]" (for "special" users).

When you compile Python yourself, you get the full Python: binaries,
headers and libraries. I.e. you in fact compile "python-devel"
(not "python").

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


Re: How well do you know Python?

2016-07-05 Thread Steven D'Aprano
On Tuesday 05 July 2016 16:38, Chris Angelico wrote:

> On Tue, Jul 5, 2016 at 4:33 PM, Steven D'Aprano
>  wrote:
>> What happens in this code snippet?
>>
>> L = [1]
>> t = (L,)
>> t[0] += 1
>>
>> Explain what value t has, and why.
> 
> Not sure you have that question right, because it simply gives a
> TypeError. You can't add an integer to a list.

D'oh!


Try this instead:

t = ([0],)
t[0] += [1]


-- 
Steve



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


Re: Making Classes Subclassable

2016-07-05 Thread Steven D'Aprano
On Monday 04 July 2016 18:34, Lawrence D’Oliveiro wrote:

> On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
>> --> "type(obj)" or "obj.__class__" (there are small differences)
>> give you the type/class of "obj".
> 
> When would it not be the same?


class X(object):
def __getattribute__(self, name):
if __name__ == '__class__':
return int
return super().__getattribute__(name)



-- 
Steve

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


Re: How well do you know Python?

2016-07-05 Thread Peter Otten
Chris Angelico wrote:

> After some discussion with a Ruby on Rails programmer about where Ruby
> ends and where Rails begins (and it's definitely not where I'd have
> expected... Rails does a ton of monkey-patching, including of built-in
> types, to provide functionality that is strangely absent from the core
> language), I tried to come up with some somewhat-challenging Python
> questions. But to make them hard, I had to go a smidge more esoteric
> than the Ruby questions did Anyhow, see how you go. Assume Python
> 3.x unless stated.
> 
> 1) Under what circumstances can str.upper() return a string of
> different length to its input?
> 2) What exception do you get when you craft an impossible class hierarchy?
> a. ValueError b. TypeError c. types.ClassInheritanceError d.
> SyntaxError
> 3) What does `from __future__ import braces` do?
> 4) Which operator, removed from Python 3.0, can be reinstated with a
> 'joke' future directive?
> 5) What is the difference between the `/` and `//` operators in Python
> 2.7? In Python 3.x?
> 
> Got any other tricky questions to add?

What will 

$ cat foo.py 
import foo
class A: pass
print(isinstance(foo.A(), A))
$ python -c 'import foo'
...
$ python foo.py
...

print?

It looks like

$ python3 -c 'print({1, 2})'
{1, 2}
$ python3 -c 'print({2, 1})'
{1, 2}

will always print the same output. Can you construct a set from two small 
integers where this is not the case? What's the difference?

What happens if you replace the ints with strings? Why?

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


A nestedmodule decorator (Re: Namespaces are one honking great idea)

2016-07-05 Thread Gregory Ewing

Steven D'Aprano wrote:

There's only so far I can go without support from the compiler.


It turns out one can go surprisingly far. Here's something I
cooked up that seems to meet almost all the requirements.
The only shortcoming I can think of is that a nestedmodule
inside another nestedmodule won't be able to see the names
in the outer nestedmodule directly (much like nested classes).

% python3 test_nestedmodule.py
0.7071067811865475

#--
#
#   test_nestedmodule.py
#
#--

from math import pi, sin
from nestedmodule import nestedmodule

def f(x):
return x**2

@nestedmodule
def test():

def g(x):
return f(x) * pi

def h(x):
return sin(g(x))

y = test.h(0.5)
print(y)

#--
#
#   nestedmodule.py
#
#--

from types import CodeType, ModuleType

def hack_code(f):
"""Hack 'return locals()' onto the end of the bytecode of f."""
code1 = f.__code__
bytes1 = code1.co_code
names1 = code1.co_names
n = len(names1)
names2 = names1 + ('locals',)
bytes2 = bytes1[:-4] + bytes([116, n, 0, 131, 0, 0, 83])
code2 = CodeType(code1.co_argcount, code1.co_kwonlyargcount, 
code1.co_nlocals,
code1.co_stacksize, code1.co_flags, bytes2, code1.co_consts, names2,
code1.co_varnames, code1.co_filename, code1.co_name, 
code1.co_firstlineno,
code1.co_lnotab, code1.co_freevars, code1.co_cellvars)
return code2

def nestedmodule(f):
c = hack_code(f)
l = eval(c, f.__globals__)
m = ModuleType(f.__name__)
m.__dict__.update(l)
return m
--
https://mail.python.org/mailman/listinfo/python-list


Re: How well do you know Python?

2016-07-05 Thread Christian Gollwitzer

Am 05.07.16 um 10:22 schrieb Steven D'Aprano:

On Tuesday 05 July 2016 16:38, Chris Angelico wrote:


On Tue, Jul 5, 2016 at 4:33 PM, Steven D'Aprano
 wrote:

What happens in this code snippet?

L = [1]
t = (L,)
t[0] += 1

Explain what value t has, and why.


Not sure you have that question right, because it simply gives a
TypeError. You can't add an integer to a list.


D'oh!


Try this instead:

t = ([0],)
t[0] += [1]



and after that, try:

>>> a=[0]
>>> t=(a,)
>>> a+=[1]


Christian

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


Re: How well do you know Python?

2016-07-05 Thread Chris Angelico
On Tue, Jul 5, 2016 at 6:36 PM, Peter Otten <__pete...@web.de> wrote:
> What will
>
> $ cat foo.py
> import foo
> class A: pass
> print(isinstance(foo.A(), A))
> $ python -c 'import foo'
> ...
> $ python foo.py
> ...
>
> print?

I refuse to play around with isinstance and old-style classes.
Particularly when circular imports are involved. Run this under Python
3 and/or explicitly subclass object, and then I'd consider it. :)

> It looks like
>
> $ python3 -c 'print({1, 2})'
> {1, 2}
> $ python3 -c 'print({2, 1})'
> {1, 2}
>
> will always print the same output. Can you construct a set from two small
> integers where this is not the case? What's the difference?

Given that the display (iteration) order of sets is arbitrary, I'm not
sure what the significance would ever be, but my guess is that the
display order would be the same for any given set, if constructed this
way. But it sounds as if you know of a set that behaves differently.

> What happens if you replace the ints with strings? Why?

Then hash randomization kicks in, and you can run the exact same line
of code multiple times and get different results. It's a coin toss.

rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'1', '2'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'1', '2'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'1', '2'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'2', '1'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'2', '1'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'1', '2'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'2', '1'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'1', '2'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'2', '1'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'1', '2'}
rosuav@sikorsky:~$ python3 -c 'print({"1", "2"})'
{'1', '2'}

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


Re: How well do you know Python?

2016-07-05 Thread Chris Angelico
On Tue, Jul 5, 2016 at 4:33 PM, Steven D'Aprano
 wrote:
> > Got any other tricky questions to add?
S
P
O
I
L
E
R

S
P
A
C
E



A
N
D

A

B
I
T

M
O
R
E

[Thanks Steven, I just copied and pasted your space. See? You can copy
and paste blank space and use it over and over. Could be a useful tip
for the people who can't find enough newlines to space out their code
or readability.]

> Explain metaclasses, descriptors, the mro, multiple inheritance, and the
> interaction between them. Why is the mro needed?

A metaclass is simply the type of the type of something. By default,
that's type, but if you type "metaclass=X" in a class definition, you
can make your class a type of something else. (Can I use the word
"type" in any other senses? I couldn't work "font" into the above.)

Descriptors are objects stored in the class which, when referenced
from an instance, return something other than themselves. The most
common example is functions; a function in a class becomes a bound
method on an instance, thus proving that descriptors make Python
better than JavaScript.

The MRO is simply the class hierarchy, starting from the current class
and ending with 'object', flattened out into a line. In a pure
single-inheritance situation, this is just the line of parents; when
multiple inheritance comes into play, the MRO becomes more
complicated, but still follows straight-forward rules (children before
parents, and parents in the order they're listed). The MRO's job is
twofold: attributes of classes earlier in the list will shadow those
later, and super() means "next in the MRO".

> obj.spam is a property. How do you get access to the underlying property 
> object
> itself?

Presumably you mean that obj.spam returns the result of calling the
property function, something like this:

class X:
@property
def spam(self):
return "something"

obj = X()

In that case, type(obj).spam is the property object, and
type(obj).spam.{fget,fset,fdel} are the three functions (of which two
will be None in my example).

> Why doesn't the property decorator work inside a Python 2 classic class?

Guido put an explicit check in to prevent you from doing so.
Attempting it will automatically download the latest Python 3.x, point
your web browser to a porting tutorial, and raise SyntaxError("Please
learn to use Python 3.").

Or: Instances of classic classes are all actually instances of
Instance(), so the property would have to be attached to Instance.

Or: It does.

rosuav@sikorsky:~$ python2
Python 2.7.11+ (default, Jun  2 2016, 19:34:15)
[GCC 5.3.1 20160528] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class X:
...   @property
...   def spam(self):
... print("Spamming")
... return "Hello"*5
...
>>> x=X()
>>> x
<__main__.X instance at 0x7f06e44fad88>
>>> x.spam
Spamming
'HelloHelloHelloHelloHello'

Take your pick.

> Explain Python scoping rules, in particular with respect to nested classes and
> nested functions (or even nested classes inside nested functions).

If you have an explicit global or nonlocal directive, the listed
name(s) are looked up in that scope only. Otherwise, Python looks in
local names, then in enclosing function scopes, then the module
globals, and finally the built-ins. While executing a class or
function body, local scope is that class/function. Enclosing class
scopes are not searched.

> Explain attribute lookup rules. When does an instance attribute take priority
> over a class attribute?

With simple attributes, an instance attribute always takes priority,
and then class attributes in method resolution order. With
descriptors... I'd have to go look it up. I've never done any
shadowing of descriptors, at least not deliberately/consciously.

> When is locals() writable? When is locals() writable, AND the effect shows up
> in the local scope? Explain how exec works inside a function, and the
> interaction with locals().

In CPython, locals() is always writable. It's just a dictionary. I'm
not sure whether this is a language guarantee or not.

When does the effect show up in local scope? When locals() is
globals(). Otherwise, the answer has to be "undefined" or at best
"implementation defined". To go further than that, I have to actually
experiment, rather than going from memory.

rosuav@sikorsky:~$ python3
Python 3.6.0a2+ (default:57f3514564f6, Jun 29 2016, 16:27:34)
[GCC 5.3.1 20160528] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def f():
...x = 1
...print(id(locals()), id(globals()))
...locals()["x"] = 2
...print(x)
...
>>> f()
140191368813960 140191369099208
1
>>>

Same is true of CPython 2.7, PyPy 5.1 (including PyPy.js), Jython 2.5,
MicroPython (although this has its own bizarrenesses - locals() is
globals(), somehow), but NOT of Brython. In Brython, locals and
globals have distinct ids, and mutating locals *does* change the local
name binding. And this is legal, according to the docs. End of
experimen

Improved nestedmodule decorator implementation

2016-07-05 Thread Gregory Ewing

I wrote:

The only shortcoming I can think of is that a nestedmodule
inside another nestedmodule won't be able to see the names
in the outer nestedmodule


Actually, that's not correct -- the version I posted before
actually crashes if you try to refer to a name in an outer
nestedmodule from an inner nestedmodule.

Here's an improved version that fully supports nesting to
any depth.

% python3 test_nested_nested.py
foo
blarg

#--
#
#   test_nested_nested.py
#
#--

from nestedmodule import nestedmodule

@nestedmodule
def m1():

def foo():
print("foo")

@nestedmodule
def m2():

def blarg():
foo()
print("blarg")

m1.m2.blarg()

#--
#
#   nestedmodule.py
#
#--

from types import CodeType, FunctionType, ModuleType
from dis import dis

def hack_code(f):
"""Hack 'return locals()' onto the end of the bytecode of f."""
code1 = f.__code__
bytes1 = code1.co_code
names1 = code1.co_names
n = len(names1)
names2 = names1 + ('locals',)
bytes2 = bytes1[:-4] + bytes([116, n, 0, 131, 0, 0, 83])
code2 = CodeType(code1.co_argcount, code1.co_kwonlyargcount, 
code1.co_nlocals,
code1.co_stacksize, code1.co_flags, bytes2, code1.co_consts, names2,
code1.co_varnames, code1.co_filename, code1.co_name, 
code1.co_firstlineno,
code1.co_lnotab, code1.co_freevars, code1.co_cellvars)
f2 = FunctionType(code2, f.__globals__, f.__name__, f.__kwdefaults__, 
f.__closure__)

return f2

def nestedmodule(f):
f2 = hack_code(f)
l = f2()
m = ModuleType(f.__name__)
m.__dict__.update(l)
return m
--
https://mail.python.org/mailman/listinfo/python-list


Re: How well do you know Python?

2016-07-05 Thread Jussi Piitulainen
Chris Angelico writes:

> On Tue, Jul 5, 2016 at 6:36 PM, Peter Otten wrote:
>> It looks like
>>
>> $ python3 -c 'print({1, 2})'
>> {1, 2}
>> $ python3 -c 'print({2, 1})'
>> {1, 2}
>>
>> will always print the same output. Can you construct a set from two small
>> integers where this is not the case? What's the difference?
>
> Given that the display (iteration) order of sets is arbitrary, I'm not
> sure what the significance would ever be, but my guess is that the
> display order would be the same for any given set, if constructed this
> way. But it sounds as if you know of a set that behaves differently.

The first thing that came to mind, {-1,-2} and {-2,-1}.

But I haven't a clue. It doesn't happen with -1 and -3, or with another
pair that I tried, and it doesn't seem to be about object identity.

>> What happens if you replace the ints with strings? Why?
>
> Then hash randomization kicks in, and you can run the exact same line
> of code multiple times and get different results. It's a coin toss.

Oh, nice, a new way to generate random bits in shell scripts.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How well do you know Python?

2016-07-05 Thread Steven D'Aprano
On Tue, 5 Jul 2016 06:36 pm, Peter Otten wrote:

> It looks like
> 
> $ python3 -c 'print({1, 2})'
> {1, 2}
> $ python3 -c 'print({2, 1})'
> {1, 2}
> 
> will always print the same output. Can you construct a set from two small
> integers where this is not the case? What's the difference?

Define "small". According to some mathematicians, any number you can write
down counts as small :-)


> What happens if you replace the ints with strings? Why?

Depends on the version of Python. Recent versions have hash randomisation
turned on, so the order of identical sets/dicts running in identical code
will vary from one run to another.


[steve@ando 3.6]$ ./python -c "print({'a', 'b', 'c', 'd'})"
{'c', 'b', 'd', 'a'}
[steve@ando 3.6]$ ./python -c "print({'a', 'b', 'c', 'd'})"
{'b', 'c', 'a', 'd'}
[steve@ando 3.6]$ ./python -c "print({'a', 'b', 'c', 'd'})"
{'c', 'a', 'd', 'b'}




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: How well do you know Python?

2016-07-05 Thread Steven D'Aprano
On Tue, 5 Jul 2016 07:51 pm, Jussi Piitulainen wrote:

> Chris Angelico writes:
> 
>> On Tue, Jul 5, 2016 at 6:36 PM, Peter Otten wrote:
>>> It looks like
>>>
>>> $ python3 -c 'print({1, 2})'
>>> {1, 2}
>>> $ python3 -c 'print({2, 1})'
>>> {1, 2}
>>>
>>> will always print the same output. Can you construct a set from two
>>> small integers where this is not the case? What's the difference?
>>
>> Given that the display (iteration) order of sets is arbitrary, I'm not
>> sure what the significance would ever be, but my guess is that the
>> display order would be the same for any given set, if constructed this
>> way. But it sounds as if you know of a set that behaves differently.
> 
> The first thing that came to mind, {-1,-2} and {-2,-1}.
> 
> But I haven't a clue. It doesn't happen with -1 and -3, or with another
> pair that I tried, and it doesn't seem to be about object identity.

The hash of most small ints is equal to the int itself:

py> for i in range(100):
... assert hash(i) == i
...
py>

With one exception:

py> hash(-2)
-2
py> hash(-1)
-2

That's because in the C implementation of hash, -1 is used to indicate an
error.


>>> What happens if you replace the ints with strings? Why?
>>
>> Then hash randomization kicks in, and you can run the exact same line
>> of code multiple times and get different results. It's a coin toss.
> 
> Oh, nice, a new way to generate random bits in shell scripts.

O_o

You're joking, right?

I'll just leave this here...

https://docs.python.org/3.6/library/secrets.html




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Improved nestedmodule decorator implementation

2016-07-05 Thread Steven D'Aprano
On Tue, 5 Jul 2016 07:31 pm, Gregory Ewing wrote:

> I wrote:
>> The only shortcoming I can think of is that a nestedmodule
>> inside another nestedmodule won't be able to see the names
>> in the outer nestedmodule
> 
> Actually, that's not correct -- the version I posted before
> actually crashes if you try to refer to a name in an outer
> nestedmodule from an inner nestedmodule.
> 
> Here's an improved version that fully supports nesting to
> any depth.

That's impressive. And scary. And I fear not portable, since it uses
byte-code hacking.

But still nicely done.




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: How well do you know Python?

2016-07-05 Thread Peter Otten
Chris Angelico wrote:

> On Tue, Jul 5, 2016 at 6:36 PM, Peter Otten <__pete...@web.de> wrote:
>> What will
>>
>> $ cat foo.py
>> import foo
>> class A: pass
>> print(isinstance(foo.A(), A))
>> $ python -c 'import foo'
>> ...
>> $ python foo.py
>> ...
>>
>> print?
> 
> I refuse to play around with isinstance and old-style classes.
> Particularly when circular imports are involved. Run this under Python
> 3 and/or explicitly subclass object, and then I'd consider it. :)

The intended lesson was that there may be two distinct classes

__main__.A and foo.A

Even though not just classes, but every object created in the script is 
affected this seems to cause the most subtle bugs.

Maybe the setup can be simplified or the question rephrased to make this 
clearer.


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


Re: How well do you know Python?

2016-07-05 Thread Chris Angelico
On Tue, Jul 5, 2016 at 9:33 PM, Peter Otten <__pete...@web.de> wrote:
> Chris Angelico wrote:
>
>> On Tue, Jul 5, 2016 at 6:36 PM, Peter Otten <__pete...@web.de> wrote:
>>> What will
>>>
>>> $ cat foo.py
>>> import foo
>>> class A: pass
>>> print(isinstance(foo.A(), A))
>>> $ python -c 'import foo'
>>> ...
>>> $ python foo.py
>>> ...
>>>
>>> print?
>>
>> I refuse to play around with isinstance and old-style classes.
>> Particularly when circular imports are involved. Run this under Python
>> 3 and/or explicitly subclass object, and then I'd consider it. :)
>
> The intended lesson was that there may be two distinct classes
>
> __main__.A and foo.A
>
> Even though not just classes, but every object created in the script is
> affected this seems to cause the most subtle bugs.
>
> Maybe the setup can be simplified or the question rephrased to make this
> clearer.

Like I said, change the commands to "python3", or explicitly subclass
object "class A(object): pass", and then it'll be using new-style
classes.

The two distinct classes problem is a very real one, and comes of
circular (or not-technically-circular, as in the second case) imports.

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


Re: Nested class doesn't see class scope

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 5:40 AM, Steven D'Aprano
 wrote:
> On Tuesday 05 July 2016 14:41, Ian Kelly wrote:
>
>> Class definitions don't create closures like functions do. When Python
>> executes a class definition, the metaclass creates a dict, and then
>> the interpreter execs the class body using that dict as the locals.
>> The body of class A has one locals dict, and the body of class B has a
>> completely separate locals dict. The only way to share variables
>> between them (prior to the class objects actually being constructed)
>> is via globals.
>
>
> So, like nested functions in Python before "from __future__ import
> nested_scopes".

In Python 3.4+, the code for a class body does participate in
closures. The CPython compiler implements this using the
LOAD_CLASSDEREF instruction. However, classes don't create closures
and default to storing to the locals dict (as class attributes),
unless a name is declared global or nonlocal. Obviously writing to a
global or nonlocal won't create a class attribute. For example:

def f():
y = 0
class C:
global x
nonlocal y
x = 1
y = 2
z = 3
return types.SimpleNamespace(**locals())

>>> ns = f()
>>> x
1
>>> ns.y
2
>>> ns.C.z
3
>>> sorted(vars(ns.C))
['__dict__', '__doc__', '__module__', '__weakref__', 'z']
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Making Classes Subclassable

2016-07-05 Thread Ian Kelly
On Tue, Jul 5, 2016 at 2:31 AM, Steven D'Aprano
 wrote:
> On Monday 04 July 2016 18:34, Lawrence D’Oliveiro wrote:
>
>> On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
>>> --> "type(obj)" or "obj.__class__" (there are small differences)
>>> give you the type/class of "obj".
>>
>> When would it not be the same?
>
>
> class X(object):
> def __getattribute__(self, name):
> if __name__ == '__class__':
> return int
> return super().__getattribute__(name)

Did you actually test that?

py> class X(object):
... def __getattribute__(self, name):
... if __name__ == '__class__':
... return int
... return super().__getattribute__(name)
...
py> x = X()
py> x.__class__


Certain attributes like __class__ and __dict__ are special.
-- 
https://mail.python.org/mailman/listinfo/python-list


Two curious errors when function globals are manipulated

2016-07-05 Thread Steven D'Aprano
I've come across two curious behaviours of a function using custom globals.
In both cases, I have a function where __globals__ is set to a ChainMap.

In the first, the builtin __import__ appears to both exist and not-exist at
the same time: I can see the __import__ builtin, but the `import` statement
fails with:

ImportError: __import__ not found


In the second, I can use `global` to declare a name in the global scope,
apparently assign to it, but then the name doesn't exist.

Attached is a single demo script which demonstrates the problems, for Python
3.


I think both of these are bugs, or at least they are bugs if
function.__globals__ is allowed to be set to something other than the
module globals.

Have I missed something? Is there documentation that says I shouldn't
replace __globals__ with another dict?

Is there a bug in my code?

If not, I think these are both bugs. Does anyone disagree?



-- 
Steven

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


Re: Making Classes Subclassable

2016-07-05 Thread Ian Kelly
On Mon, Jul 4, 2016 at 1:57 AM, dieter  wrote:
> Lawrence D’Oliveiro  writes:
>
>> Some of the classes in Qahirah, my Cairo binding 
>>  I found handy to reuse elsewhere, for 
>> example in my binding for Pixman . 
>> Subclassing is easy, but then you need to ensure that operations inherited 
>> from the superclass return instances of the right class. This means that 
>> superclass methods must never refer directly to the class by name for 
>> constructing new objects; they need to obtain the current class by more 
>> indirect means.
>
> --> "type(obj)" or "obj.__class__" (there are small differences)
> give you the type/class of "obj".

Another option is a classmethod factory:

class X:
@classmethod
def new(cls, *args, **kwargs):
return cls(*args, **kwargs)

def method(self):
return self.new()

class Y(X): pass

Y().method() will return a new Y instance. There's not a lot of reason
to prefer this over type(self)(), but by overriding the classmethod
you can have subclasses that use different signatures in the
constructor but still support the same signature in the factory for
compatibility with the base class. You can also make the classmethod
private, if you wish.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Making Classes Subclassable

2016-07-05 Thread Ian Kelly
On Mon, Jul 4, 2016 at 2:34 AM, Lawrence D’Oliveiro
 wrote:
> On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
>> --> "type(obj)" or "obj.__class__" (there are small differences)
>> give you the type/class of "obj".
>
> When would it not be the same?

I think the only remaining difference in Python 3 is that
obj.__class__ is assignable and type(obj) is not. For most uses,
type(obj) would be preferred though, in the same way that len(obj) is
preferable to obj.__len__().
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 2:46 PM, Steven D'Aprano  wrote:
> I've come across two curious behaviours of a function using custom globals.
> In both cases, I have a function where __globals__ is set to a ChainMap.

ChainMap implements the MutableMapping abstract base class. But
CPython needs globals to be a dict. In the current implementation,
LOAD_GLOBAL calls _PyDict_LoadGlobal, and STORE_GLOBAL calls
PyDict_SetItem. They don't fall back on the abstract object APIs.

OTOH, when executing unoptimized code in a class, locals can be an
arbitrary mapping, e.g. as returned from a metaclass __prepare__
method. In this case, CPython LOAD_NAME and STORE_NAME fall back on
the abstract APIs PyObject_GetItem and PyObject_SetItem.

I don't see this documented in the execution and data models (maybe I
overlooked it), but it's definitely documented for exec():

If only globals is provided, it must be a dictionary,
which will be used for both the global and the local
variables. If globals and locals are given, they are
used for the global and local variables, respectively.
If provided, locals can be any mapping object.

https://docs.python.org/3/library/functions.html?highlight=globals#exec

For example:

>>> m = collections.ChainMap()

>>> exec('x = 1', m)
Traceback (most recent call last):
  File "", line 1, in 
TypeError: exec() globals must be a dict, not ChainMap

>>> exec('x = 1', {}, m)
>>> m['x']
1
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Making Classes Subclassable

2016-07-05 Thread Steven D'Aprano
On Wed, 6 Jul 2016 12:41 am, Ian Kelly wrote:

> On Tue, Jul 5, 2016 at 2:31 AM, Steven D'Aprano
>  wrote:
>> On Monday 04 July 2016 18:34, Lawrence D’Oliveiro wrote:
>>
>>> On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
 --> "type(obj)" or "obj.__class__" (there are small differences)
 give you the type/class of "obj".
>>>
>>> When would it not be the same?
>>
>>
>> class X(object):
>> def __getattribute__(self, name):
>> if __name__ == '__class__':
>> return int
>> return super().__getattribute__(name)
> 
> Did you actually test that?

Er, no, because if I had, I would have discovered the silly typo: __name__
instead of name, which screws up the whole thing...

Trying again:

py> class X(object):
... def __getattribute__(self, name):
... if name == '__class__':  # NOTE SPELLING
... return int
... return super().__getattribute__(name)
...
py> x = X()
py> x.__class__



[...]
> Certain attributes like __class__ and __dict__ are special.

Indeed they are, but __class__ is not *that* special :-)




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Making Classes Subclassable

2016-07-05 Thread Steven D'Aprano
On Wed, 6 Jul 2016 01:02 am, Ian Kelly wrote:

> On Mon, Jul 4, 2016 at 2:34 AM, Lawrence D’Oliveiro
>  wrote:
>> On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
>>> --> "type(obj)" or "obj.__class__" (there are small differences)
>>> give you the type/class of "obj".
>>
>> When would it not be the same?
> 
> I think the only remaining difference in Python 3 is that
> obj.__class__ is assignable and type(obj) is not. For most uses,
> type(obj) would be preferred though, in the same way that len(obj) is
> preferable to obj.__len__().


Assigning to __class__ will change the result of type(obj).


py> class A(object):
... pass
...
py> class B(object):
... pass
...
py> a = A()
py> type(a)

py> a.__class__ = B
py> type(a)



This is a deliberate design feature! Provided your classes are designed to
be compatible, you can safely change an instance of class A into an
instance of class B, and then have a.method() correctly call B.method.

(Objective C makes *extensive* use of this technique in Cocoa's standard
library functions, but entirely behind the scenes. Apple's semi-official
stance on this is that they are allowed to do it because the know what
they're doing, but anyone else who tries it is a muppet.)

I don't know what this technique is called, but I'm going to call it
adoption (as opposed to adaption) -- your A instance gets adopted by class
B, after which it behaves like any other B instance.

Again, I stress that A and B must be compatible (and written in Python, you
can't do it with classes written in C), otherwise their methods won't find
the attributes they expect.



-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 3:27 PM, eryk sun  wrote:
> ChainMap implements the MutableMapping abstract base class. But
> CPython needs globals to be a dict. In the current implementation,
> LOAD_GLOBAL calls _PyDict_LoadGlobal, and STORE_GLOBAL calls
> PyDict_SetItem. They don't fall back on the abstract object APIs.

Sorry, I was replying from memory, but partly mixed up 2.x and 3.x. In
CPython 3.6, LOAD_GLOBAL does fall back on PyObject_GetItem, but
STORE_GLOBAL still does not. The latter is short enough to quote:

TARGET(STORE_GLOBAL) {
PyObject *name = GETITEM(names, oparg);
PyObject *v = POP();
int err;
err = PyDict_SetItem(f->f_globals, name, v);
Py_DECREF(v);
if (err != 0)
goto error;
DISPATCH();
}
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two curious errors when function globals are manipulated

2016-07-05 Thread Steven D'Aprano
On Wed, 6 Jul 2016 01:27 am, eryk sun wrote:

> On Tue, Jul 5, 2016 at 2:46 PM, Steven D'Aprano 
> wrote:
>> I've come across two curious behaviours of a function using custom
>> globals. In both cases, I have a function where __globals__ is set to a
>> ChainMap.
> 
> ChainMap implements the MutableMapping abstract base class. But
> CPython needs globals to be a dict. In the current implementation,
> LOAD_GLOBAL calls _PyDict_LoadGlobal, and STORE_GLOBAL calls
> PyDict_SetItem. They don't fall back on the abstract object APIs.

Which is why I use ChainDict, a subclass of ChainMap and dict. It's enough
to allow the FunctionType constructor to accept the argument, and the
assignment *appears* to take place.

If PyDict_SetItem expects an actual dict (accept no substitutes or
subclasses), why is there no error? I would have expected a segfault at
worst or at least an exception.

It works with exec:

py> from collections import ChainMap
py> class ChainDict(ChainMap, dict):
... pass
...
py> m = ChainDict()
py> exec("x = 1", m, m)
py> m['x']
1

(Tested in both 3.3 and 3.6.)





-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 3:37 PM, eryk sun  wrote:
> In CPython 3.6, LOAD_GLOBAL does fall back on PyObject_GetItem

I did some digging to find when this changed in 3.3:

https://hg.python.org/cpython/diff/e3ab8aa0216c/Python/ceval.c

Notice in the last comment on issue 14385 that Martijn wanted to
updated the docs for exec() to say that globals no longer has to be a
dict, but that would be wrong since STORE_GLOBAL and DELETE_GLOBAL
weren't updated. I don't know why it falls back on the abstract
PyObject_GetItem for LOAD_GLOBAL considering the other opcodes weren't
updated as well. It's pointless.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two curious errors when function globals are manipulated

2016-07-05 Thread Random832
On Tue, Jul 5, 2016, at 11:50, Steven D'Aprano wrote:
> If PyDict_SetItem expects an actual dict (accept no substitutes or
> subclasses), why is there no error? I would have expected a segfault at
> worst or at least an exception.

Subclasses are fine. Sort of. In general if you pass a *subclass* of
[say] dict to a built-in method that is expecting a real dict, the
effect is that the assignments will go straight through to the real
dict, and never call any overridden method. I.e. it's as if you called
dict.__setitem__(self, key, value). Something your own __setitem__
method may have done [directly or via super()] anyway... or may not
have. So any special logic in your own __setitem__, which may have
included e.g. copying it to an alternate place, changing the key or
value, or simply refusing to add the item to the dictionary at all, will
be ignored, and your object may end up in an inconsistent state.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 3:50 PM, Steven D'Aprano  wrote:
> It works with exec:
>
> py> from collections import ChainMap
> py> class ChainDict(ChainMap, dict):
> ... pass
> ...
> py> m = ChainDict()
> py> exec("x = 1", m, m)
> py> m['x']
> 1
>
> (Tested in both 3.3 and 3.6.)

No, actually it doesn't work. Remember that when you store to a
variable, it's implicitly a local variable, for which CPython uses
STORE_NAME in unoptimized code. To test this properly you need to
declare the variable as global, so that it uses STORE_GLOBAL:

import dis
from collections import ChainMap

class ChainDict(ChainMap, dict):
pass

m = ChainDict()
code = compile(r'''
global x
x, y = 1, 2
''', '', 'exec')

>>> dis.dis(code)
  3   0 LOAD_CONST   3 ((1, 2))
  2 UNPACK_SEQUENCE  2
  4 STORE_GLOBAL 0 (x)
  6 STORE_NAME   1 (y)
  8 LOAD_CONST   2 (None)
 10 RETURN_VALUE

>>> exec(code, m)
>>> m
ChainDict({'y': 2})
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How well do you know Python?

2016-07-05 Thread Carl Meyer
On 07/05/2016 05:50 AM, Chris Angelico wrote:
> On Tue, Jul 5, 2016 at 9:33 PM, Peter Otten <__pete...@web.de> wrote:
>> Chris Angelico wrote:
>>
>>> On Tue, Jul 5, 2016 at 6:36 PM, Peter Otten <__pete...@web.de> wrote:
 What will

 $ cat foo.py
 import foo
 class A: pass
 print(isinstance(foo.A(), A))
 $ python -c 'import foo'
 ...
 $ python foo.py
 ...

 print?
[snip]
>> The intended lesson was that there may be two distinct classes
>>
>> __main__.A and foo.A
[snip]
> The two distinct classes problem is a very real one, and comes of
> circular (or not-technically-circular, as in the second case) imports.

It can also come of pathological setups where a path and its parent are
both on sys.path, so all import paths have an "optional" prefix (but you
actually get a different copy of the module depending on whether you use
that prefix).

Carl



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


Re: Two curious errors when function globals are manipulated

2016-07-05 Thread eryk sun
On Tue, Jul 5, 2016 at 4:12 PM, Random832  wrote:
> So any special logic in your own __setitem__, which may have
> included e.g. copying it to an alternate place, changing the key or
> value, or simply refusing to add the item to the dictionary at all, will
> be ignored, and your object may end up in an inconsistent state.

In this case, here's the value added by STORE_NAME using
PyObject_SetItem, which calls __setitem__:

>>> sorted(m)
['y']

Here are the values added by STORE_GLOBAL using PyDict_SetItem, which
doesn't call __setitem__:

>>> sorted(dict(m))
['__builtins__', 'x']

ChainMap implements the following __setitem__:

def __setitem__(self, key, value):
self.maps[0][key] = value

The item is never actually added to `self` (the dict instance). Then
for __getitem__, we're again only seeing the items from `self.maps`
instead of from `self`:

def __getitem__(self, key):
for mapping in self.maps:
try:
return mapping[key]
except KeyError:
pass
return self.__missing__(key)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Need help compiling Python-devel

2016-07-05 Thread TM
This option is not straight forward. There are too many dependencies.
Easier in Linux not so easy in AIX.

Is it possible to copy the python executable (ie the code below)?
# cp -p python python-devel

Thanks,
Tony



On Tue, Jul 5, 2016 at 12:12 PM, Dieter Maurer  wrote:

> TM wrote at 2016-7-5 11:42 -0400:
> Please stay on the mailin list (where others, too, can help you).
>
> >Thanks for the response. The reason for my install of python was, so I
> >could compile samba from source.
> >
> >If what you say is true then why do I get the error below when I try to
> >compile samba.
> >
> >Checking for custom code
> >   : *Could not find the python development headers*
> >/usr/local/samba-4.4.4/wscript:106: error: the configuration failed (see
> >'/usr/local/samba-4.4.4/bin/config.log')
>
> Likely, because it looks at a different place than you where your
> locally compiled Python has placed it.
>
>
> Likely, the easiest thing to do is installing the package
> "python-dev" (or "python-devel" or similarly named) with you
> operating system packageing tool.
>
>
>
> --
> Dieter
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Help: Python unit testing

2016-07-05 Thread Harsh Gupta
Hello All,

I have been trying to write a test framework for a pretty simple command server 
application in python. I have not been able to figure out how to test the 
socket server.
I would really appreciate if you could help me out in testing this application 
using unittest. I'm new to this.
Please find the commandserver.py file . 

'''
import socket
import time

supported_commands = {'loopback', 'print', 'close'}

class CommandServer:

def __init__(self, ip='localhost', port=5000):

self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind((ip, port))

print "Server running"
self.is_running = True

def start_listening(self, num_connections=2):
self.server.listen(num_connections)
self._accept()

def is_running(self):
return self.is_running

def _accept(self):
self.client, self.remote_addr = self.server.accept()

print "Received connection from %s" % (self.remote_addr,)
self.is_connected = True

def _get_data(self, bufsize=32):
try:
return self.client.recv(bufsize)
except KeyboardInterrupt:
self.close()

def wait_for_commands(self):
while self.is_connected:
cmd = self._get_data()
if cmd:
if cmd not in supported_commands:
print "Received unsupported command"
continue

if cmd in ("loopback", "print"):
payload = self._get_data(512)

if cmd == "loopback":
self.client.sendall(payload)

elif cmd == "print":
print payload

elif cmd == "close":
self.close()

def close(self):
self.client.close()
self.server.close()
self.is_connected = False
print "Connection closed"

if __name__ == '__main__':

cmd_server = CommandServer()
cmd_server.start_listening()
cmd_server.wait_for_commands()

'''

The tests cases I came up with are:
1. Start the server
2. Establish connection with client
3. Test the server by sending 3 commands
- loopback command should  send all payload.
- print command should print payload
- close command should close the connection between server and client.

4. Raise error if any other command is send other than listed 3 commands

Other nominal cases
1. Check if both the server and client are closed. It might happen that only 
one is closed.
2. Go to sleep/Cancel the connection if no command is received after a certain 
period of time.

Thank You

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


Help: Python socket unit test framework

2016-07-05 Thread Harsh Gupta
Hello All,

I have been trying to write a test framework for a pretty simple command server 
application in python. I have not been able to figure out how to test the 
socket server.
I would really appreciate if you could help me out in testing this application 
using unittest.

Please kindly review it.

The tests cases I came up with are:
1. Start the server
2. Establish connection with client
3. Test the server by sending 3 commands
- loopback command should  send all payload.
- print command should print payload
- close command should close the connection between server and client.

4. Raise error if any other command is send other than listed 3 commands

Other nominal cases
1. Check if both the server and client are closed. It might happen that only 
one is closed.
2. Go to sleep/Cancel the connection if no command is received after a certain 
period of time.




import socket
import time

supported_commands = {'loopback', 'print', 'close'}

class CommandServer:

def __init__(self, ip='localhost', port=5000):

self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind((ip, port))

print "Server running"
self.is_running = True

def start_listening(self, num_connections=2):
self.server.listen(num_connections)
self._accept()

def is_running(self):
return self.is_running

def _accept(self):
self.client, self.remote_addr = self.server.accept()

print "Received connection from %s" % (self.remote_addr,)
self.is_connected = True

def _get_data(self, bufsize=32):
try:
return self.client.recv(bufsize)
except KeyboardInterrupt:
self.close()

def wait_for_commands(self):
while self.is_connected:
cmd = self._get_data()
if cmd:
if cmd not in supported_commands:
print "Received unsupported command"
continue

if cmd in ("loopback", "print"):
payload = self._get_data(512)

if cmd == "loopback":
self.client.sendall(payload)

elif cmd == "print":
print payload

elif cmd == "close":
self.close()

def close(self):
self.client.close()
self.server.close()
self.is_connected = False
print "Connection closed"

if __name__ == '__main__':

cmd_server = CommandServer()
cmd_server.start_listening()
cmd_server.wait_for_commands()



Thank You

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


Re: py2exe crashes on simple program

2016-07-05 Thread John Nagle
On 7/4/2016 11:13 PM, Steven D'Aprano wrote:
> If you change it to "library.exe" does it work? Also, I consider this
> a bug in py2exe: - it's an abuse of assert, using it to check
> user-supplied input; - it's a failing assertion, which by definition
> is a bug.

   I'm not trying to build "library.zip". That's a work
file py2exe created.  If I delete it, it's re-created by py2exe.

The problem seems to be that my "setup.py" file didn't include
a "console" entry, which tells py2exe the build target.
Apparently, without that py2exe tries to build something bogus and
blows up.

After fixing that, the next error is

Building 'dist\baudotrss.exe'.
error: [Errno 2] No such file or directory: 'C:\\Program
Files\\Python35\\lib\\site-packages\\py2exe\\run-py3.5-win-amd64.exe'

Looks like Pip installed (yesterday) a version of py2exe that doesn't
support Python 3.5.  The py2exe directory contains
"run-py3.3-win-amd64.exe" and "run-py3.4-win-amd64.exe", but
not 3.5 versions.

That's what PyPi says at "https://pypi.python.org/pypi/py2exe";.
The last version of py2exe was uploaded two years ago (2014-05-09)
and is for Python 3.4.  So of course it doesn't have the 3.5 binary
executable it needs.

Known problem. Stack Overflow reports py2exe is now broken for Python
3.5.  Apparently it's a non-trivial fix, too.

http://stackoverflow.com/questions/32963057/is-there-a-py2exe-version-thats-compatible-with-python-3-5

cx_freeze has been suggested as an alternative, but its own
documents indicate it's only been tested through Python 3.4.
Someone reported success with a development version.

I guess people don't create Python executables much.

John Nagle


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


Appending an asterisk to the end of each line

2016-07-05 Thread Seymore4Head
import os

f_in = open('win.txt', 'r')
f_out = open('win_new.txt', 'w')

for line in f_in.read().splitlines():
f_out.write(line + " *\n")

f_in.close()
f_out.close()

os.rename('win.txt', 'win_old.txt')
os.rename('win_new.txt', 'win.txt') 


I just tried to reuse this program that was posted several months ago.
I am using a text flie that is about 200 lines long and have named it
win.txt.  The file it creates when I run the program is win_new.txt
but it's empty.


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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Joel Goldstick
On Tue, Jul 5, 2016 at 6:05 PM, Seymore4Head
 wrote:
> import os
>
> f_in = open('win.txt', 'r')
> f_out = open('win_new.txt', 'w')
>
> for line in f_in.read().splitlines():
> f_out.write(line + " *\n")
>
> f_in.close()
> f_out.close()
>
> os.rename('win.txt', 'win_old.txt')
> os.rename('win_new.txt', 'win.txt')
>
>
> I just tried to reuse this program that was posted several months ago.
> I am using a text flie that is about 200 lines long and have named it
> win.txt.  The file it creates when I run the program is win_new.txt
> but it's empty.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Are you running program in same folder as text file?

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Seymore4Head
On Tue, 5 Jul 2016 18:27:25 -0400, Joel Goldstick
 wrote:

>On Tue, Jul 5, 2016 at 6:05 PM, Seymore4Head
> wrote:
>> import os
>>
>> f_in = open('win.txt', 'r')
>> f_out = open('win_new.txt', 'w')
>>
>> for line in f_in.read().splitlines():
>> f_out.write(line + " *\n")
>>
>> f_in.close()
>> f_out.close()
>>
>> os.rename('win.txt', 'win_old.txt')
>> os.rename('win_new.txt', 'win.txt')
>>
>>
>> I just tried to reuse this program that was posted several months ago.
>> I am using a text flie that is about 200 lines long and have named it
>> win.txt.  The file it creates when I run the program is win_new.txt
>> but it's empty.
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>Are you running program in same folder as text file?

Yes.  I just reinstalled Python.  Python is not installed in the same
folder and I don't remember if it needs to have path entered.


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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Joel Goldstick
On Tue, Jul 5, 2016 at 6:29 PM, Seymore4Head
 wrote:
> On Tue, 5 Jul 2016 18:27:25 -0400, Joel Goldstick
>  wrote:
>
>>On Tue, Jul 5, 2016 at 6:05 PM, Seymore4Head
>> wrote:
>>> import os
>>>
>>> f_in = open('win.txt', 'r')
>>> f_out = open('win_new.txt', 'w')
>>>
>>> for line in f_in.read().splitlines():
>>> f_out.write(line + " *\n")
>>>
>>> f_in.close()
>>> f_out.close()
>>>
>>> os.rename('win.txt', 'win_old.txt')
>>> os.rename('win_new.txt', 'win.txt')
>>>
>>>
>>> I just tried to reuse this program that was posted several months ago.
>>> I am using a text flie that is about 200 lines long and have named it
>>> win.txt.  The file it creates when I run the program is win_new.txt
>>> but it's empty.
>>>
>>>
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>
>>Are you running program in same folder as text file?
>
> Yes.  I just reinstalled Python.  Python is not installed in the same
> folder and I don't remember if it needs to have path entered.
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

So, if you type python, do you enter the python shell?

-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Appending an asterisk to the end of each line

2016-07-05 Thread MRAB

On 2016-07-05 23:05, Seymore4Head wrote:

import os

f_in = open('win.txt', 'r')
f_out = open('win_new.txt', 'w')

for line in f_in.read().splitlines():
f_out.write(line + " *\n")

f_in.close()
f_out.close()

os.rename('win.txt', 'win_old.txt')
os.rename('win_new.txt', 'win.txt')


I just tried to reuse this program that was posted several months ago.
I am using a text flie that is about 200 lines long and have named it
win.txt.  The file it creates when I run the program is win_new.txt
but it's empty.

Although it creates a file called "win_new.txt", it then renames it to 
"win.txt", so "win_new.txt" shouldn't exist.


Of course, if there's already a file called "win_old.txt", then the 
first rename will raise an exception, and you'll have "win_new.txt" and 
the original "win.txt".


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


Re: Need help compiling Python-devel

2016-07-05 Thread Michael Torrie
On 07/05/2016 10:35 AM, TM wrote:
> This option is not straight forward. There are too many dependencies.
> Easier in Linux not so easy in AIX.
> 
> Is it possible to copy the python executable (ie the code below)?
> # cp -p python python-devel

What is this python-devel thing?  You said you wanted the libraries and
header files. Those are part of the python source distribution.  If you
have Python compiled and running, then the libraries are already there
since python needs them to run, and, if you installed from source, you
should already have the header files.

I'm unclear as to why you would copy the python binary to python-devel.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Seymore4Head
On Tue, 5 Jul 2016 18:40:51 -0400, Joel Goldstick
 wrote:

>On Tue, Jul 5, 2016 at 6:29 PM, Seymore4Head
> wrote:
>> On Tue, 5 Jul 2016 18:27:25 -0400, Joel Goldstick
>>  wrote:
>>
>>>On Tue, Jul 5, 2016 at 6:05 PM, Seymore4Head
>>> wrote:
 import os

 f_in = open('win.txt', 'r')
 f_out = open('win_new.txt', 'w')

 for line in f_in.read().splitlines():
 f_out.write(line + " *\n")

 f_in.close()
 f_out.close()

 os.rename('win.txt', 'win_old.txt')
 os.rename('win_new.txt', 'win.txt')


 I just tried to reuse this program that was posted several months ago.
 I am using a text flie that is about 200 lines long and have named it
 win.txt.  The file it creates when I run the program is win_new.txt
 but it's empty.


 --
 https://mail.python.org/mailman/listinfo/python-list
>>>
>>>Are you running program in same folder as text file?
>>
>> Yes.  I just reinstalled Python.  Python is not installed in the same
>> folder and I don't remember if it needs to have path entered.
>>
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>So, if you type python, do you enter the python shell?

I am using XP and launching the program from another drive/folder than
the boot drive.

The program has .py extension and the icon shows it is associated with
Python.

I tried "start run" and then typed python and it did show the dos box
with c:\python34/python.exe and the python shell.

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


Re: Need help compiling Python-devel

2016-07-05 Thread Lawrence D’Oliveiro
On Wednesday, July 6, 2016 at 4:35:54 AM UTC+12, TM wrote:
>
> This option is not straight forward. There are too many dependencies.
> Easier in Linux not so easy in AIX.

POWER hardware?

It might be easier and quicker to get Linux running on the machine, and figure 
it out there, than to try it under AIX...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Joel Goldstick
On Tue, Jul 5, 2016 at 7:03 PM, MRAB  wrote:
> On 2016-07-05 23:05, Seymore4Head wrote:
>>
>> import os
>>
>> f_in = open('win.txt', 'r')
>> f_out = open('win_new.txt', 'w')
>>
>> for line in f_in.read().splitlines():
>> f_out.write(line + " *\n")
>>
>> f_in.close()
>> f_out.close()
>>
>> os.rename('win.txt', 'win_old.txt')
>> os.rename('win_new.txt', 'win.txt')
>>
>>
>> I just tried to reuse this program that was posted several months ago.
>> I am using a text flie that is about 200 lines long and have named it
>> win.txt.  The file it creates when I run the program is win_new.txt
>> but it's empty.
>>
> Although it creates a file called "win_new.txt", it then renames it to
> "win.txt", so "win_new.txt" shouldn't exist.
>
> Of course, if there's already a file called "win_old.txt", then the first
> rename will raise an exception, and you'll have "win_new.txt" and the
> original "win.txt".
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Why don't you comment out the renames, and see what happens?


-- 
Joel Goldstick
http://joelgoldstick.com/blog
http://cc-baseballstats.info/stats/birthdays
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Seymore4Head
On Wed, 6 Jul 2016 00:03:29 +0100, MRAB 
wrote:

>On 2016-07-05 23:05, Seymore4Head wrote:
>> import os
>>
>> f_in = open('win.txt', 'r')
>> f_out = open('win_new.txt', 'w')
>>
>> for line in f_in.read().splitlines():
>> f_out.write(line + " *\n")
>>
>> f_in.close()
>> f_out.close()
>>
>> os.rename('win.txt', 'win_old.txt')
>> os.rename('win_new.txt', 'win.txt')
>>
>>
>> I just tried to reuse this program that was posted several months ago.
>> I am using a text flie that is about 200 lines long and have named it
>> win.txt.  The file it creates when I run the program is win_new.txt
>> but it's empty.
>>
>Although it creates a file called "win_new.txt", it then renames it to 
>"win.txt", so "win_new.txt" shouldn't exist.
>
>Of course, if there's already a file called "win_old.txt", then the 
>first rename will raise an exception, and you'll have "win_new.txt" and 
>the original "win.txt".

When I run the program it creates a file called win_new.txt and
win.txt remains unchanged.

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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Chris Angelico
On Wed, Jul 6, 2016 at 9:04 AM, Seymore4Head
 wrote:
> I am using XP and launching the program from another drive/folder than
> the boot drive.
>
> The program has .py extension and the icon shows it is associated with
> Python.
>
> I tried "start run" and then typed python and it did show the dos box
> with c:\python34/python.exe and the python shell.

Okay. Step 1: Get a better operating system than Windows XP. Either
upgrade to a newer version of Windows, or (my recommended option)
switch to something like Debian Linux.

Step 2, whether or not you do step 1: Become familiar with the command
line. If you're staying with Windows, that means Start, Run, cmd.exe.
Run your program from the terminal, and (this is the most important
part) *copy and paste* its output into your next email asking for
help. My suspicion is that your script is terminating with an
exception somewhere, and you're not seeing it because the Windows
association system gives you a Python that comes up and then
disappears promptly when the program terminates.

Step 3: If the above hasn't trivially solved your problem, now it's
time for actual debugging work. Pepper your code with print() calls,
showing the values of various things, until you figure out what's
going on.

Have fun!

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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Seymore4Head
On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
 wrote:

>On Tue, Jul 5, 2016 at 7:03 PM, MRAB  wrote:
>> On 2016-07-05 23:05, Seymore4Head wrote:
>>>
>>> import os
>>>
>>> f_in = open('win.txt', 'r')
>>> f_out = open('win_new.txt', 'w')
>>>
>>> for line in f_in.read().splitlines():
>>> f_out.write(line + " *\n")
>>>
>>> f_in.close()
>>> f_out.close()
>>>
>>> os.rename('win.txt', 'win_old.txt')
>>> os.rename('win_new.txt', 'win.txt')
>>>
>>>
>>> I just tried to reuse this program that was posted several months ago.
>>> I am using a text flie that is about 200 lines long and have named it
>>> win.txt.  The file it creates when I run the program is win_new.txt
>>> but it's empty.
>>>
>> Although it creates a file called "win_new.txt", it then renames it to
>> "win.txt", so "win_new.txt" shouldn't exist.
>>
>> Of course, if there's already a file called "win_old.txt", then the first
>> rename will raise an exception, and you'll have "win_new.txt" and the
>> original "win.txt".
>>
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>
>Why don't you comment out the renames, and see what happens?

I really don't care if the filename gets renamed or not.  I commented
out the renames, but I still get a new file called win_new.txt and it
is empty.

The original is unchanged.

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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Seymore4Head
On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head
 wrote:

>On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
> wrote:
>
>>On Tue, Jul 5, 2016 at 7:03 PM, MRAB  wrote:
>>> On 2016-07-05 23:05, Seymore4Head wrote:

 import os

 f_in = open('win.txt', 'r')
 f_out = open('win_new.txt', 'w')

 for line in f_in.read().splitlines():
 f_out.write(line + " *\n")

 f_in.close()
 f_out.close()

 os.rename('win.txt', 'win_old.txt')
 os.rename('win_new.txt', 'win.txt')


 I just tried to reuse this program that was posted several months ago.
 I am using a text flie that is about 200 lines long and have named it
 win.txt.  The file it creates when I run the program is win_new.txt
 but it's empty.

>>> Although it creates a file called "win_new.txt", it then renames it to
>>> "win.txt", so "win_new.txt" shouldn't exist.
>>>
>>> Of course, if there's already a file called "win_old.txt", then the first
>>> rename will raise an exception, and you'll have "win_new.txt" and the
>>> original "win.txt".
>>>
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
>>
>>Why don't you comment out the renames, and see what happens?
>
>I really don't care if the filename gets renamed or not.  I commented
>out the renames, but I still get a new file called win_new.txt and it
>is empty.
>
>The original is unchanged.

I just tried this on a 3 line text file and it works.

I am looking through the text file and have found at least two
suspicious characters.  One is a German letter and the other is a
characters that has been replaced by a square symbol.

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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Seymore4Head
On Wed, 6 Jul 2016 09:38:47 +1000, Chris Angelico 
wrote:

>On Wed, Jul 6, 2016 at 9:04 AM, Seymore4Head
> wrote:
>> I am using XP and launching the program from another drive/folder than
>> the boot drive.
>>
>> The program has .py extension and the icon shows it is associated with
>> Python.
>>
>> I tried "start run" and then typed python and it did show the dos box
>> with c:\python34/python.exe and the python shell.
>
>Okay. Step 1: Get a better operating system than Windows XP. Either
>upgrade to a newer version of Windows, or (my recommended option)
>switch to something like Debian Linux.
>
>Step 2, whether or not you do step 1: Become familiar with the command
>line. If you're staying with Windows, that means Start, Run, cmd.exe.
>Run your program from the terminal, and (this is the most important
>part) *copy and paste* its output into your next email asking for
>help. My suspicion is that your script is terminating with an
>exception somewhere, and you're not seeing it because the Windows
>association system gives you a Python that comes up and then
>disappears promptly when the program terminates.
>
>Step 3: If the above hasn't trivially solved your problem, now it's
>time for actual debugging work. Pepper your code with print() calls,
>showing the values of various things, until you figure out what's
>going on.
>
>Have fun!
>
>ChrisA

Thanks for the suggestions.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Appending an asterisk to the end of each line

2016-07-05 Thread MRAB

On 2016-07-06 00:45, Seymore4Head wrote:

On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head
 wrote:


On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
 wrote:


On Tue, Jul 5, 2016 at 7:03 PM, MRAB  wrote:

On 2016-07-05 23:05, Seymore4Head wrote:


import os

f_in = open('win.txt', 'r')
f_out = open('win_new.txt', 'w')

for line in f_in.read().splitlines():
f_out.write(line + " *\n")

f_in.close()
f_out.close()

os.rename('win.txt', 'win_old.txt')
os.rename('win_new.txt', 'win.txt')


I just tried to reuse this program that was posted several months ago.
I am using a text flie that is about 200 lines long and have named it
win.txt.  The file it creates when I run the program is win_new.txt
but it's empty.


Although it creates a file called "win_new.txt", it then renames it to
"win.txt", so "win_new.txt" shouldn't exist.

Of course, if there's already a file called "win_old.txt", then the first
rename will raise an exception, and you'll have "win_new.txt" and the
original "win.txt".

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


Why don't you comment out the renames, and see what happens?


I really don't care if the filename gets renamed or not.  I commented
out the renames, but I still get a new file called win_new.txt and it
is empty.

The original is unchanged.


I just tried this on a 3 line text file and it works.

I am looking through the text file and have found at least two
suspicious characters.  One is a German letter and the other is a
characters that has been replaced by a square symbol.

That suggests to me that it's an encoding problem (the traceback 
would've indicated that).


Specify an encoding when you open the files:

f_in = open('win.txt', 'r', encoding='utf-8')
f_out = open('win_new.txt', 'w', encoding='utf-8')

assuming that 'win.txt' is indeed encoded in UTF-8. (It might be 
something like ISO-8859-1 instead.)


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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Seymore4Head
On Wed, 6 Jul 2016 01:05:12 +0100, MRAB 
wrote:

>On 2016-07-06 00:45, Seymore4Head wrote:
>> On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head
>>  wrote:
>>
>>>On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
>>> wrote:
>>>
On Tue, Jul 5, 2016 at 7:03 PM, MRAB  wrote:
> On 2016-07-05 23:05, Seymore4Head wrote:
>>
>> import os
>>
>> f_in = open('win.txt', 'r')
>> f_out = open('win_new.txt', 'w')
>>
>> for line in f_in.read().splitlines():
>> f_out.write(line + " *\n")
>>
>> f_in.close()
>> f_out.close()
>>
>> os.rename('win.txt', 'win_old.txt')
>> os.rename('win_new.txt', 'win.txt')
>>
>>
>> I just tried to reuse this program that was posted several months ago.
>> I am using a text flie that is about 200 lines long and have named it
>> win.txt.  The file it creates when I run the program is win_new.txt
>> but it's empty.
>>
> Although it creates a file called "win_new.txt", it then renames it to
> "win.txt", so "win_new.txt" shouldn't exist.
>
> Of course, if there's already a file called "win_old.txt", then the first
> rename will raise an exception, and you'll have "win_new.txt" and the
> original "win.txt".
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Why don't you comment out the renames, and see what happens?
>>>
>>>I really don't care if the filename gets renamed or not.  I commented
>>>out the renames, but I still get a new file called win_new.txt and it
>>>is empty.
>>>
>>>The original is unchanged.
>>
>> I just tried this on a 3 line text file and it works.
>>
>> I am looking through the text file and have found at least two
>> suspicious characters.  One is a German letter and the other is a
>> characters that has been replaced by a square symbol.
>>
>That suggests to me that it's an encoding problem (the traceback 
>would've indicated that).
>
>Specify an encoding when you open the files:
>
>f_in = open('win.txt', 'r', encoding='utf-8')
>f_out = open('win_new.txt', 'w', encoding='utf-8')
>
>assuming that 'win.txt' is indeed encoded in UTF-8. (It might be 
>something like ISO-8859-1 instead.)

Thanks. 

It is working now that I removed those two characters.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Creating a calculator

2016-07-05 Thread BartC

On 02/07/2016 01:16, DFS wrote:

On 7/1/2016 5:34 AM, Pierre-Alain Dorange wrote:



More reduced :
--
u=raw_input('Enter calculation:")
print eval(u)
--
works and compute :
1+2+3+4-1+4*2
2+3.0/2-0.5

Perform better and shorter, but less educationnal of course...



2 lines?  Love it!


That's not really implementing a calculator. It's just feeding the input 
to Python to execute immediately. I can reduce it to zero lines: start 
Python so that the >>> prompt appears. Then type in:


>>> 2+3.0/2-0.5

and so on. No other Python code is needed.

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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread MRAB

On 2016-07-06 01:05, Seymore4Head wrote:

On Wed, 6 Jul 2016 01:05:12 +0100, MRAB 
wrote:


On 2016-07-06 00:45, Seymore4Head wrote:

On Tue, 05 Jul 2016 19:29:21 -0400, Seymore4Head
 wrote:


On Tue, 5 Jul 2016 19:15:23 -0400, Joel Goldstick
 wrote:


On Tue, Jul 5, 2016 at 7:03 PM, MRAB  wrote:

On 2016-07-05 23:05, Seymore4Head wrote:


import os

f_in = open('win.txt', 'r')
f_out = open('win_new.txt', 'w')

for line in f_in.read().splitlines():
f_out.write(line + " *\n")

f_in.close()
f_out.close()

os.rename('win.txt', 'win_old.txt')
os.rename('win_new.txt', 'win.txt')


I just tried to reuse this program that was posted several months ago.
I am using a text flie that is about 200 lines long and have named it
win.txt.  The file it creates when I run the program is win_new.txt
but it's empty.


Although it creates a file called "win_new.txt", it then renames it to
"win.txt", so "win_new.txt" shouldn't exist.

Of course, if there's already a file called "win_old.txt", then the first
rename will raise an exception, and you'll have "win_new.txt" and the
original "win.txt".

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


Why don't you comment out the renames, and see what happens?


I really don't care if the filename gets renamed or not.  I commented
out the renames, but I still get a new file called win_new.txt and it
is empty.

The original is unchanged.


I just tried this on a 3 line text file and it works.

I am looking through the text file and have found at least two
suspicious characters.  One is a German letter and the other is a
characters that has been replaced by a square symbol.


That suggests to me that it's an encoding problem (the traceback
would've indicated that).

Specify an encoding when you open the files:

f_in = open('win.txt', 'r', encoding='utf-8')
f_out = open('win_new.txt', 'w', encoding='utf-8')

assuming that 'win.txt' is indeed encoded in UTF-8. (It might be
something like ISO-8859-1 instead.)


Thanks.

It is working now that I removed those two characters.

So, you didn't really fix it, you just changed the input to what your 
program is able to handle... :-)

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


Re: Making Classes Subclassable

2016-07-05 Thread Lawrence D’Oliveiro
On Wednesday, July 6, 2016 at 3:03:26 AM UTC+12, Ian wrote:
>
> On Mon, Jul 4, 2016 at 2:34 AM, Lawrence D’Oliveiro wrote:
>>
>> On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
>>> --> "type(obj)" or "obj.__class__" (there are small differences)
>>> give you the type/class of "obj".
>>
>> When would it not be the same?
> 
> I think the only remaining difference in Python 3 is that
> obj.__class__ is assignable and type(obj) is not. For most uses,
> type(obj) would be preferred though, in the same way that len(obj) is
> preferable to obj.__len__().

OK, I think that makes sense.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Steven D'Aprano
On Wed, 6 Jul 2016 10:05 am, Seymore4Head wrote:

> On Wed, 6 Jul 2016 01:05:12 +0100, MRAB 
> wrote:

>>That suggests to me that it's an encoding problem (the traceback
>>would've indicated that).
>>
>>Specify an encoding when you open the files:
>>
>>f_in = open('win.txt', 'r', encoding='utf-8')
>>f_out = open('win_new.txt', 'w', encoding='utf-8')
>>
>>assuming that 'win.txt' is indeed encoded in UTF-8. (It might be
>>something like ISO-8859-1 instead.)
> 
> Thanks.
> 
> It is working now that I removed those two characters.


And it will break again next time you use the script, if the input file
contains "those characters" (which, I stress, are perfectly reasonable
characters for a text file to contain).

What you should take from this is Chris' advice to learn better debugging
skills: I am absolutely certain that Python will have displayed a traceback
hinting what went wrong, which you could then use to solve the problem for
good rather than just cover it up. If you're not seeing that traceback,
your ability to program is crippled. You'll be like somebody trying to
assemble a jigsaw puzzle in the dark, wearing thick mittens.

So you're first priority ought to be to work out how to run Python code
without the tracebacks disappearing.

(Either that, or make sure your code is perfect, first time, every time.)

At the moment, you're in the situation of a car manufacturer that has just
discovered a defect in their cars:

"If you drive through a puddle with the rear left wheel at high speed, water
splashes up into the body, causes a short in the electronics, and the gas
tank catches fire."

"That's okay, we can just pave over the factory grounds and get rid of the
puddles."

Your script "catches fire" when given input containing non-ASCII characters.
You don't really know why, and you haven't fixed it, but you've "solved"
the problem by "paving over the puddles". Except not really -- the problem
is still there, and if your script ever gets used on a puddle you've
missed, it will catch fire again.




-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Two curious errors when function globals are manipulated

2016-07-05 Thread Steven D'Aprano
On Wed, 6 Jul 2016 02:13 am, eryk sun wrote:

> On Tue, Jul 5, 2016 at 3:50 PM, Steven D'Aprano 
> wrote:
>> It works with exec:
[...]
> No, actually it doesn't work. Remember that when you store to a
> variable, it's implicitly a local variable, for which CPython uses
> STORE_NAME in unoptimized code. To test this properly you need to
> declare the variable as global, so that it uses STORE_GLOBAL:

/face-palm

Of course you're right, I knew that. Sorry for the noise.






-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

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


Re: Making Classes Subclassable

2016-07-05 Thread Lawrence D’Oliveiro
On Monday, July 4, 2016 at 7:58:07 PM UTC+12, dieter wrote:
>
> Lawrence D’Oliveiro writes:
> 
> > Some of the classes in Qahirah, my Cairo binding
>  I found handy to reuse elsewhere, for
> example in my binding for Pixman .
> Subclassing is easy, but then you need to ensure that operations inherited
> from the superclass return instances of the right class. This means that
> superclass methods must never refer directly to the class by name for
> constructing new objects; they need to obtain the current class by more
> indirect means.
> 
> --> "type(obj)" or "obj.__class__" (there are small differences)
> give you the type/class of "obj".

OK, both updated.

Funny how you find other bugs when revisiting code after a while. :)
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: How well do you know Python?

2016-07-05 Thread Lawrence D’Oliveiro
On Tuesday, July 5, 2016 at 9:51:21 PM UTC+12, Jussi Piitulainen wrote:
>
> Chris Angelico writes:
> 
>> Then hash randomization kicks in, and you can run the exact same line
>> of code multiple times and get different results. It's a coin toss.
> 
> Oh, nice, a new way to generate random bits in shell scripts.

Please, don’t do that...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Spot the bug: getoptquestion.py

2016-07-05 Thread Lawrence D’Oliveiro
On Tuesday, July 5, 2016 at 1:42:42 AM UTC+12, Chris Angelico wrote:

> The getopt module is designed to match the C getopt function, which I've
> never used; for my command-line parsing, I use argparse instead
> (usually via some wrapper that cuts down the duplication, like clize).

getopt seems so much simpler.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Spot the bug: getoptquestion.py

2016-07-05 Thread Chris Angelico
On Wed, Jul 6, 2016 at 2:04 PM, Lawrence D’Oliveiro
 wrote:
> On Tuesday, July 5, 2016 at 1:42:42 AM UTC+12, Chris Angelico wrote:
>
>> The getopt module is designed to match the C getopt function, which I've
>> never used; for my command-line parsing, I use argparse instead
>> (usually via some wrapper that cuts down the duplication, like clize).
>
> getopt seems so much simpler.

Look at clize:

https://github.com/Rosuav/LetMeKnow/blob/master/letmeknow.py

I just put docstrings on my functions, slap "@command" above them, and
with minimal boilerplate, I have a fully-working command line
interface. It's a wrapper around argparse.

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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread Larry Hudson via Python-list

On 07/05/2016 03:05 PM, Seymore4Head wrote:

import os

f_in = open('win.txt', 'r')
f_out = open('win_new.txt', 'w')

for line in f_in.read().splitlines():
 f_out.write(line + " *\n")

f_in.close()
f_out.close()

os.rename('win.txt', 'win_old.txt')
os.rename('win_new.txt', 'win.txt')


I just tried to reuse this program that was posted several months ago.
I am using a text flie that is about 200 lines long and have named it
win.txt.  The file it creates when I run the program is win_new.txt
but it's empty.




Not your problem, but you can simplify your read/write loop to:

for line in f_in:
f_out.write(line[:-1] + ' *\n')

The 'line[:-1]' expression gives you the line up to but not including the 
trailing newline.
Alternately, use:  f_out.write(line.rstrip() + ' *\n')

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


Re: Appending an asterisk to the end of each line

2016-07-05 Thread cs

On 05Jul2016 21:37, Python List  wrote:

On 07/05/2016 03:05 PM, Seymore4Head wrote:

import os

f_in = open('win.txt', 'r')
f_out = open('win_new.txt', 'w')

for line in f_in.read().splitlines():
f_out.write(line + " *\n")

f_in.close()
f_out.close()

os.rename('win.txt', 'win_old.txt')
os.rename('win_new.txt', 'win.txt')

I just tried to reuse this program that was posted several months ago.
I am using a text flie that is about 200 lines long and have named it
win.txt.  The file it creates when I run the program is win_new.txt
but it's empty.


Put a counter in your loop:

   count = 0
   for line in f_in.read().splitlines():
   f_out.write(line + " *\n")
   count += 1
   print("count =", count)

Check that it says 200 (or whatever number you expect).


Not your problem, but you can simplify your read/write loop to:

for line in f_in:
   f_out.write(line[:-1] + ' *\n')

The 'line[:-1]' expression gives you the line up to but not including the 
trailing newline.
Alternately, use:  f_out.write(line.rstrip() + ' *\n')


Importantly for this version, every line _MUST_ have a trailing newline.  
Personally that is what I require of my text files anyway, but some dubious 
tools (and, IMO, dubious people) make text files with no final newline.


For such a file the above code would eat the last character because we don't 
check that a newline is there.


I take a hard line on such files and usually write programs that look like 
this:


   for line in f_in:
   if not line.endswith('\n'):
   raise ValueError("missing final newline on file, last line is: %r" 
(line,))
   f_out.write(line[:-1] + ' *\n')

Then one can proceed secure in the knowledge that the data are well formed.

I consider the final newline something of a termination record; without it I 
have no faith that the file wasn't rudely truncated somehow. In other words, I 
consider a text file to consist of newline-terminated lines, not 
newline-separated lines.


Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: How well do you know Python?

2016-07-05 Thread Jussi Piitulainen
Steven D'Aprano writes:

> On Tue, 5 Jul 2016 07:51 pm, Jussi Piitulainen wrote:
>
>> Chris Angelico writes:
>> 
>>> On Tue, Jul 5, 2016 at 6:36 PM, Peter Otten wrote:
 It looks like

 $ python3 -c 'print({1, 2})'
 {1, 2}
 $ python3 -c 'print({2, 1})'
 {1, 2}

 will always print the same output. Can you construct a set from two
 small integers where this is not the case? What's the difference?
>>>
>>> Given that the display (iteration) order of sets is arbitrary, I'm not
>>> sure what the significance would ever be, but my guess is that the
>>> display order would be the same for any given set, if constructed this
>>> way. But it sounds as if you know of a set that behaves differently.
>> 
>> The first thing that came to mind, {-1,-2} and {-2,-1}.
>> 
>> But I haven't a clue. It doesn't happen with -1 and -3, or with another
>> pair that I tried, and it doesn't seem to be about object identity.
>
> The hash of most small ints is equal to the int itself:
>
> py> for i in range(100):
> ... assert hash(i) == i
> ...
> py>
>
> With one exception:
>
> py> hash(-2)
> -2
> py> hash(-1)
> -2

Thanks. That must be the explanation. I tried object identity but I did
not think of comparing hashes directly.

Amusing that I didn't know this, yet I happened to think of just this
one pair of numbers. Literally the first thing that I thought to try,
and it turns out to be the only thing.

> That's because in the C implementation of hash, -1 is used to indicate
> an error.
>
 What happens if you replace the ints with strings? Why?
>>>
>>> Then hash randomization kicks in, and you can run the exact same
>>> line of code multiple times and get different results. It's a coin
>>> toss.
>> 
>> Oh, nice, a new way to generate random bits in shell scripts.
>
> O_o
>
> You're joking, right?

Er, ok, better not escalate this: Of course I am.

> I'll just leave this here...
>
> https://docs.python.org/3.6/library/secrets.html

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


Learn Python + Swift 3 by creating a comprehensive system with web app and iOS app

2016-07-05 Thread Leo Trieu
Guys, we're from Code4Startup team and we're running a KickStarter campaign for 
the next awesome course to bring best value to Python and Swift community.

In this course, you will learn how to use Python and Swift 3 by cloning cool 
startup apps like UberEats, Doordash or Postmates.

In case you're interested and want to learn more, check it out here: 
http://kck.st/29NjAtR

Feel free to comment here with any questions.

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