Re: trouble with cmd.Cmd and prompting

2017-01-18 Thread Cameron Simpson

On 04Jan2017 12:20, Cameron Simpson  wrote:

I will try to get a minimal example others can run.


Well I've made some time for this, and it seems to be an interaction with my 
python3, the "input" builtin, and readline. Cmd.cmdloop behaves fine if I turn 
off the .raw_input attribute, and I can reproduce the issue without the cmd 
module.


Test code:

 import readline
 line = input("F> ")
 print("gap")
 line = input("G> ")

Results on a terminal:

 [~]fleet*2> python3 foo.py
 F> a
 F> gap
 n
 G> %
 [~]fleet*>


That "%" is an artifact of no final newline after the "G> ".

If I comment out the import of readline it looks like this:

 G> %
 [~]fleet*> python3 foo.py

 F> a
 gap
 G> b
 [~]fleet*> 

as one would expect. Ignoring my shell prompts, the contents of a typescript 
show:


 python3 foo.py^M
 F> a^M
 F> gap^M
 b^M
 G>

Those "^M"s are carriage returns. I kind of suspect the second "F> " is 
readline doing some kind of redraw. I am baffled by the lack of initial "G> " 
prompt; that "G> " above appears to be the secondary issue, after I've pressed 
return for the "b" line.


This is on a Mac, so I've no strace command to hand. I've also got all manner 
of potential special stuff (readline library from MacPorts, etc). I'll keep 
stripping stuff back...


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


Re: trouble with cmd.Cmd and prompting

2017-01-18 Thread Peter Otten
Cameron Simpson wrote:

> On 04Jan2017 12:20, Cameron Simpson  wrote:
>>I will try to get a minimal example others can run.
> 
> Well I've made some time for this, and it seems to be an interaction with
> my python3, the "input" builtin, and readline. Cmd.cmdloop behaves fine if
> I turn off the .raw_input attribute, and I can reproduce the issue without
> the cmd module.
> 
> Test code:
> 
>   import readline
>   line = input("F> ")
>   print("gap")
>   line = input("G> ")

If readline is not used input() writes its prompt to stderr. I don't have a 
Mac, but it might be worthwhile to investigate what goes where. For 
comparison here's what happens on my Linux system:

$ cat tmp.py
import sys

if "-r" in sys.argv:
import readline

print(
"uses readline" if "readline" in sys.modules
else "no readline"
)
line = input("F> ")
print("gap")
line = input("G> ")

$ python3 tmp.py
no readline
F> alpha
gap
G> beta

$ python3 tmp.py 2>/dev/null
no readline
alpha
gap
beta

$ python3 tmp.py -r 2>/dev/null
uses readline
F> alpha
gap
G> beta
 


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


Re: Emulating Final classes in Python

2017-01-18 Thread alister
On Wed, 18 Jan 2017 13:10:41 +1100, Steven D'Aprano wrote:

> On Tuesday 17 January 2017 20:37, Antoon Pardon wrote:
> 
>> Op 17-01-17 om 08:05 schreef Steven D'Aprano:
>>> I wish to emulate a "final" class using Python, similar to bool:
>>>
>>> py> class MyBool(bool):
>>> ... pass ...
>>> Traceback (most recent call last):
>>>   File "", line 1, in 
>>> TypeError: type 'bool' is not an acceptable base type
> [...]
>> I find those kind of classes annoying as hell and nobody has ever given
>> me a good reason for them. What good was it to change Lock from a
>> factory function to a class if you can't subclass the result anyway.
> 
> I'm not sure which Lock class you're referring to.
> 
> 
>> The result will probably be that users that would prefer to subclass
>> your class will monkey-patch it. Something like:
>> 
>> class MyLock:
>> def __init__(self):
>> self.lock = Lock()
>> 
>> def __getattr__(self, attr):
>> return getattr(self.lock, attr)
> 
> That technique is called "delegation", or sometimes "composition".
> 
> 
>> So I wonder what reasons do you have prefering that your users
>> monkey-patch your class instead of subclassing it?
> 
> Since my class provides no useful behaviour, I don't think anyone will
> seriously have any reason to subclass it.

if it provides "No Usefull behaviour" why does it even exist
what useless behaviour does it provide? :-)

(no mater how pointless there is always someone how will find a use, I 
even know someone who successfully sold a box of paper shredding on ebay!)

> 
> Python has at least three singleton instances which are used purely as
> abstract symbols: they have no state, and very little behaviour besides
> a nice repr. They are None, NotImplemented and Ellipsis. I'm effectively
> trying to make my own abstract symbol.
> 
> But it's mostly a learning exercise.





-- 
Good programmers treat Microsoft products as damage and route
around them.

   -- From a Slashdot.org post
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Emulating Final classes in Python

2017-01-18 Thread Ethan Furman

On 01/17/2017 11:05 PM, Steven D'Aprano wrote:


I've given a metaclass that disallows subclassing:

class MyClass(MyParent, metaclass=FinalMeta):
 ...


Ethan took that one step further by giving a class you inherit from to disallow
subclassing:

class MyClass(MyParent, Final):
 ...


Could we solve this problem with a decorator?


@final
class MyClass(MyParent):
 ...


Without needing to add any special magic to MyParent or MyClass, apart from the
decorator, can we make MyClass final? That would (in principle) allow us to
make a subclass, and *then* set the class final so that no more subclasses
could be made.


I thought that the decorator could simply set the class' metaclass:

def final(cls):
 if cls.__class__ is type:
 cls.__class__ = Meta
 return cls
 raise TypeErrror('Possible metaclass conflict')


but that's disallowed. Any ideas?


You still need to have the FinalMeta type and Final class available, but to use 
a decorator you'll need to scavenge the bits from the old class to make a new 
class of the same name and return that:

def final(cls):
new_cls = Meta(cls.__name__, (Final, )+cls.__bases__, dict(cls.__dict__))
return new_cls

Not sure if more is needed to handle __slots__, but this should get us started.

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


Re: Python 3.6 Installation

2017-01-18 Thread eryk sun
On Wed, Jan 18, 2017 at 6:23 AM, Terry Reedy  wrote:
> Not everyone has run Windows update since the current runtime was released.

Python's installer tries (and sometimes fails) to install the
KB2999226 update, which installs an old version of the Universal CRT.
Some installers instead bundle the VC++ redistributable, which
includes KB2999226, but that would be overkill for CPython. I thought
we could phase this out in 3.6 to instead require an up-to-date OS. If
people need offline updates, as opposed to using Windows update, it's
better to get the most recent version directly from Microsoft,
KB3118401 [1]. Steve Dower disagreed, and that's the end of it.

As to the loader returning STATUS_INVALID_IMAGE_FORMAT (0xC07B), I
would run the executable/script under a debugger such as WinDbg or
cdb, with loader snaps enabled via gflags. Find the troublesome
DLL(s), and manually resolve the problem.

[1]: https://support.microsoft.com/en-us/kb/3118401
-- 
https://mail.python.org/mailman/listinfo/python-list


sorting list python

2017-01-18 Thread Smith

Hi all,
could you do better?
Thank you in advance

link code:
https://repl.it/FMin/8
--
https://mail.python.org/mailman/listinfo/python-list


Re: sorting list python

2017-01-18 Thread Peter Otten
Smith wrote:

> Hi all,
> could you do better?
> Thank you in advance
> 
> link code:
> https://repl.it/FMin/8
Don't make it unnecessarily hard to find your code -- as long as it's small 
you can include it into your mail

Given

> with open('partite.txt', 'r') as f:
> splitted = [(int(line.split("'")[0]), line) for line in f]
> splitted.sort()
> lsorted = [line for (m, line) in splitted]
> for z in lsorted:
> print(z)
> 

with partite.txt looking like this

> 74' Kessie'
> 90' + 4' D'alessandro
> 51' Mchedlidze
> 54' Banega
> 56' Icardi
> 65' Icardi
> 14' Sau


Assuming you want to perform a numerical sort on the numbers before the ' 
you can just apply sorted

with open(...) as f:
print("".join(sorted(f))

as long as all number strings have the same length. 

If that's not the case python's sorted() has decorate-sort-undecorate 
capabilities built in -- no need to do it manually:

with open("partite.txt") as f:
by_number = sorted(f, key=lambda line: int(line.partition("'")[0]))
print("".join(by_number))


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


Re: sorting list python

2017-01-18 Thread Grant Edwards
On 2017-01-18, Smith  wrote:

> could you do better?

Yes.

> link code:
> https://repl.it/FMin/8

I would have done better by posting the actual code instead of a blind
link that may point to Dog-knows-what...

-- 
Grant Edwards   grant.b.edwardsYow! I had pancake makeup
  at   for brunch!
  gmail.com

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


Re: sorting list python

2017-01-18 Thread MRAB

On 2017-01-18 19:51, Smith wrote:

Hi all,
could you do better?
Thank you in advance

link code:
https://repl.it/FMin/8

If you're wondering about the blank lines, it's because the lines end 
with '\n', which starts a new line, and the print function also starts a 
new line after printing the string.


Try stripping the '\n' off the end of the line read in with the .rstrip 
method.


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


Re: sorting list python

2017-01-18 Thread Smith

On 18/01/2017 21:20, Peter Otten wrote:

with open("partite.txt") as f:
by_number = sorted(f, key=lambda line: int(line.partition("'")[0]))
print("".join(by_number))



Great!!! :-)
--
https://mail.python.org/mailman/listinfo/python-list


Re: sorting list python

2017-01-18 Thread Smith

On 18/01/2017 21:21, Grant Edwards wrote:

I would have done better by posting the actual code instead of a blind
link that may point to Dog-knows-what...


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


Re: sorting list python

2017-01-18 Thread Smith

On 18/01/2017 21:34, MRAB wrote:

If you're wondering about the blank lines, it's because the lines end
with '\n', which starts a new line, and the print function also starts a
new line after printing the string.

Try stripping the '\n' off the end of the line read in with the .rstrip
method.


Thank you for your kind cooperation
--
https://mail.python.org/mailman/listinfo/python-list


Re: sorting list python

2017-01-18 Thread Germán Fajardo
with open('partite.txt', 'r') as r:
for line in sorted(r):
print(line, end='')

2017-01-18 15:38 GMT-05:00 Smith :

> On 18/01/2017 21:34, MRAB wrote:
>
>> If you're wondering about the blank lines, it's because the lines end
>> with '\n', which starts a new line, and the print function also starts a
>> new line after printing the string.
>>
>> Try stripping the '\n' off the end of the line read in with the .rstrip
>> method.
>>
>
> Thank you for your kind cooperation
> --
> https://mail.python.org/mailman/listinfo/python-list
>



-- 
Atentamente




Germán A. Fajardo G.
-- 
https://mail.python.org/mailman/listinfo/python-list


Adding colormaps?

2017-01-18 Thread Martin Schöön
Is there a way to add colormaps to Matplotlib?

What I would like to do is to add the perceptually uniform 
sequential colormaps introduced in version 1.5.something.
I would like to do this without breaking my Debian system
in which Matplotlib version 1.4.2 is the newest version
available in the repo.

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


Re: Python 3.6 Installation

2017-01-18 Thread Michael Torrie
On 01/18/2017 10:59 AM, eryk sun wrote:
> On Wed, Jan 18, 2017 at 6:23 AM, Terry Reedy  wrote:
>> Not everyone has run Windows update since the current runtime was released.
> 
> Python's installer tries (and sometimes fails) to install the
> KB2999226 update, which installs an old version of the Universal CRT.
> Some installers instead bundle the VC++ redistributable, which
> includes KB2999226, but that would be overkill for CPython. I thought
> we could phase this out in 3.6 to instead require an up-to-date OS. If
> people need offline updates, as opposed to using Windows update, it's
> better to get the most recent version directly from Microsoft,
> KB3118401 [1]. Steve Dower disagreed, and that's the end of it.
> 
> As to the loader returning STATUS_INVALID_IMAGE_FORMAT (0xC07B), I
> would run the executable/script under a debugger such as WinDbg or
> cdb, with loader snaps enabled via gflags. Find the troublesome
> DLL(s), and manually resolve the problem.
> 
> [1]: https://support.microsoft.com/en-us/kb/3118401
> 

Thanks for the clarifications, Erik. Appreciate it.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Emulating Final classes in Python

2017-01-18 Thread Ethan Furman

On 01/18/2017 08:24 AM, Ethan Furman wrote:

On 01/17/2017 11:05 PM, Steven D'Aprano wrote:



I've given a metaclass that disallows subclassing:

class MyClass(MyParent, metaclass=FinalMeta):
 ...


Ethan took that one step further by giving a class you inherit from to disallow
subclassing:

class MyClass(MyParent, Final):
 ...


Could we solve this problem with a decorator?


@final
class MyClass(MyParent):
 ...


Without needing to add any special magic to MyParent or MyClass, apart from the
decorator, can we make MyClass final? That would (in principle) allow us to
make a subclass, and *then* set the class final so that no more subclasses
could be made.


I thought that the decorator could simply set the class' metaclass:

def final(cls):
 if cls.__class__ is type:
 cls.__class__ = Meta
 return cls
 raise TypeErrror('Possible metaclass conflict')


but that's disallowed. Any ideas?


You still need to have the FinalMeta type and Final class available, but to use 
a
 decorator you'll need to scavenge the bits from the old class to make a new 
class
 of the same name and return that:

def final(cls):
 new_cls = Meta(cls.__name__, (Final, )+cls.__bases__, dict(cls.__dict__))
 return new_cls

Not sure if more is needed to handle __slots__, but this should get us started.


One problem with the above is existing instances won't be modified to inherit 
from the updated class.  I am unsure if that is solvable before 3.6, but in 3.6 
one can use the new __init_subclass__ to avoid a Final base class, a FinalMeta 
type, and just update the existing class:

def final(cls):
def init_subclass(cls, **kwargs):
raise Exception('Final class cannot be subclassed')
cls.__init_subclass__ = classmethod(init_subclass)
return cls

This can be used as a decorator at class creation time, or at any later date to 
lock down a class.  The downside is it's less obvious that the class is 
final... meaning there are no clues in the MRO.

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


Re: Emulating Final classes in Python

2017-01-18 Thread Steve D'Aprano
On Thu, 19 Jan 2017 09:02 am, Ethan Furman wrote:

[...]
> One problem with the above is existing instances won't be modified to
> inherit from the updated class.  I am unsure if that is solvable before
> 3.6, but in 3.6 one can use the new __init_subclass__ to avoid a Final
> base class, a FinalMeta type, and just update the existing class:
> 
> def final(cls):
>  def init_subclass(cls, **kwargs):
>  raise Exception('Final class cannot be subclassed')
>  cls.__init_subclass__ = classmethod(init_subclass)
>  return cls
> 
> This can be used as a decorator at class creation time, or at any later
> date to lock down a class.  The downside is it's less obvious that the
> class is final... meaning there are no clues in the MRO.

Ah nice! I didn't know about __init__subclass__.



-- 
Steve
“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