[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Greg Ewing

On 1/04/20 7:08 am, Serhiy Storchaka wrote:

31.03.20 20:52, Antoine Pitrou пише:

Your search is incomplete, for example you failed to account for
occurrences of "cheese" and "milkshake".


Should we deprecate the word "wheel" as well, since it's a
reference to cheese?

--
Greg
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ITAIH2W6IPVPFWARJZZPJJBTNWJMAQEO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Chris Angelico
On Wed, Apr 1, 2020 at 12:28 PM Kyle Stanley  wrote:
>
> Chris Angelico wrote:
> > We will need a
> > migration plan. Unfortunately, African swallows are non-migratory,
> > severely impacting your proposal.
>
> Well, aren't European swallows migratory? They may not be able to carry a 
> coconut, but certainly a migratory plan would be under their weight limit. :-)
>

True, that would work for many places. But we can't ignore the rest of
the world, where nonmigratory swallows might result in a whole new
Python 2->3 transition, where African Pythonistas are stuck on the old
version.

But perhaps we could import something. That's how you fix everything
else in Python.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/WBB6D6MMQ2OX3OGAQ6VGMJQWRGNDUDJD/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Kyle Stanley
Chris Angelico wrote:
> We will need a
> migration plan. Unfortunately, African swallows are non-migratory,
> severely impacting your proposal.

Well, aren't European swallows migratory? They may not be able to carry a
coconut, but certainly a migratory plan would be under their weight limit.
:-)

On Tue, Mar 31, 2020 at 2:24 PM Chris Angelico  wrote:

> On Wed, Apr 1, 2020 at 4:19 AM Gerrit Holl  wrote:
> > Abstract
> > 
> >
> > Python has long used metasyntactic variables that are based on the
> > consumption of meat and dairy products, such as "spam", "ham", and
> > "eggs".
> > This language is not considerate to pigs or chicken and violates the
> > spirit of the Code of Conduct.  This PEP proposes to retire the use
> > of those names in official Python documentation and source code and to
> > recommend users of Python to do the same.
> >
>
> While I wholeheartedly agree with your intention here, I believe the
> backward incompatibilities would make this untenable. We will need a
> migration plan. Unfortunately, African swallows are non-migratory,
> severely impacting your proposal.
>
> ChrisA
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/FRNE2HZWLBSONIHYHK2Z3OVXCRT4MMY7/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ZI4D5CEBDIVIWHMLAVOUI3BRNXQ5DSY3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Steven D'Aprano
On Tue, Mar 31, 2020 at 07:32:11PM -, jdve...@gmail.com wrote:

> If I understand you are proposing a change from StringIO `write` 
> method to `+=` operator. Is it right?

No, that is not correct. The StringIO.write() method will not be changed 
or removed. The proposal is to extend the class with the `+=` operator 
which will add as equivalent to calling write().

> I cannot see any advantage on this proposal since there is no real 
> change in the implementation of StringIO. Or are you proposing any 
> change in the underlying implementation and I have missed that point?

This proposal isn't about enhancing StringIO's functionality.

The purpose of this proposal is targetted at people who are using string 
concatenation instead of assembling a list then calling join. It is 
about leveraging StringIO's ability to behave as a string builder to 
give people a minimally invasive change from the string concatenation 
anti-pattern:

buf = ''
# repeated many times
buf += 'substring'

to something which can be efficient on all Python interpreters:

buf = StringIO()
buf += 'substring'
buf = buf.getvalue()


> In this case, I disagree with you: StringIO is a stream and I think 
> that it is wrong to make it to "look & feel" like a string. That is my 
> opinion.

Paul has not suggested making StringIO look and feel like a string. 
Nobody is going to add 45+ string methods to StringIO. This is a minimal 
extension to the StringIO class which will allow people to improve their 
string building code with a minimal change.


-- 
Steven
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/V634REJ55ZG6ZPRSMC4MFOSPZPWWOFCU/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Compound with .. else statement

2020-03-31 Thread Christopher Barker
On Tue, Mar 31, 2020 at 2:41 AM Serhiy Storchaka 
wrote:

> 31.03.20 01:32, Christopher Barker пише:
> > In case Serhiy's answer wasn't clear: context managers can be written to
> > handle exceptions (within their context) in any way you see fit.
> >
> > that is: the method:
> > |
> > |
> > |__exit__(||self||, exc_type, exc_value, exc_traceback):|
> >
> > get the exception, and information about it, of one is raised, so you
> > can handle it anyway you want.
>


> Actually I meant the opposite.


I think I was thrown by the use of the example "my_context" -- that is, if
you are writing a conrtect manger, you can handle exceptions any way you
like. If you are using an existing one, then, yes:


> the context manager does not silence a raised exception, so control flow
> is never passed to the statement past the with block if an exception is
> raised inside the with block.
>


>  was_not_raised = False
>  with my_context():
>do_something_sensitive()
>was_not_raised = True
>  if was_not_raised:
>  print("We're all safe.")
>
> You do not need a special syntax for this.


but we don't need special syntax for "else" on a for or while loop (or try
block) either, you could always set a sentinel for those too.

which to me is a case for adding else to a "with" block as well, for all
the same reasons it's there for the other block construct.

Though I don't think I'd advocate it in this case, as the Exception is not
really a clear part of the context manger API, like "break" is to the loops.

-CHB


-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/4VLKOFTNDU3NPDIASCFHAS42PVU3XJX6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Christopher Barker
On Tue, Mar 31, 2020 at 12:21 PM Paul Sokolovsky  wrote:

> Christopher Barker  wrote:
> For avoidance of doubt: nothing in my RFC has anything to do, or
> implies, "a mutable string type".


I said "there are some use cases for a mutable string type" I did not say
that's what was asked for in this thread.

So why did I say that? because:


> A well-know pattern of string
> builder, yes.


As I read this suggestion, it starred with something like:

* lots of people use the a "pattern of string building", using str +=
another_string to build up strings.
* That is not an efficient pattern, and is considered an anti-pattern, even
in cPython, where is has been cleverly optimized.

I think everyone on this thread would agree with the above.

* The "official recommended solution" is another pattern: build up in the
list, and then join it.

You are suggesting that it would nice if there were an efficient
implementation of string building that followed the original anti-pattern's
syntax. After all, if folks want to make a string, then using familiar
string syntax would be nice and natural.

You've pointed out that StringIO already provides an efficient
implementation of string building (which could be made even more efficient,
if one wanted to write that code) .

And that if it grew an __iadd__ method, it would then match the pattern
that you want it to match, and allow folks to improve their code with less
change than going to the list.append then join method.

All good.

But what struck me is that in the end, this is perhaps a more friendly than
the list-based method, but it's still a real shift in paradigm: I think
people use str +=str not because they are thinking "I need a string
builder", but because they are thinking: I need a "string". That is your
choice of variable names:

buf = ""
for i in range(5):
buf += "foo"
print(buf)

is not what most folks would use, because they aren't thinking "I need a
buffer in which to put a bunch of strings", they are thinking: "I need to
make this big string", so would more likely write:

message = "The start of the message"
for i in something:
buf += "some more message"
do_something_with_the_message(message)

which, yes, is almost exactly the same as your example, but with a
different intent -- I start with a string and make it bigger, not "I make a
buffer in which to build a string, and then put things in it, then get the
resulting string out of the buffer.

I teach a lot of beginners, so yes, I do see this code pattern a fair bit.

The difference in intent means that folks are not likely to go looking for
a "buffer" or "string builder" anyway.

So that suggested to me that a mutable string type would completely satisfy
your use case, but be more natural to folks used to strings:

message = MutableString("The start of the message")
for i in something:
buf += "some more message"
do_something_with_the_message(message)

And you could do other nifty things with it, like all the string methods,
without a lot of wasteful reallocating, particularly for methods that don't
change the length of the string. (Though Unicode does make this a
challenge!) (and yes, I know, that the "wasteful reallocating" is probably
hardly ever, if ever, a bottleneck)

In short: a mutable string would satisfy the requirements of a "string
builder", and more.

Anyway, as I said in my previous message, the fact that a Mutable string
hasn't gained any traction tells us something: it really isn't that
important.

And I mentioned a similar effort I made to make a growable numpy array,
and, well, it turned out not to be worth it either.

However if we're all wrong, and there would be a demand for such a "string
builder", then why not write one (could be a wrapper around StringIO if you
want), and put it on PyPi, or even just for own lib, and see how it catches
on.

Have you done that for your own code and found you like it enough to really
want to push this forward?

BTW: I timed += vs StringIO, vs list + join, and found (like you did) that
they are all about the same speed, under cPython 3.7. But I had a thought
-- might string interning be affecting the performance? particularly for
the list method:

In [43]: def list_join():
...: buf = []
...: for i in range(1):
...: buf.append("foo")
...: return "".join(buf)

note that that is only making one string "foo", and reusing it in all items
in the list. In the common case, you wouldn't get that help.

OK, tested it, no it doesn't really make a difference. If you replace "foo"
(which gets interned) with "foo "[:3] (which doesn't), they all take
longer, but still all about the same.

-CHB


-- 
Christopher Barker, PhD

Python Language Consulting
  - Teaching
  - Scientific Software Development
  - Desktop GUI and Web Development
  - wxPython, numpy, scipy, Cython
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to 

[Python-ideas] Re: returning a namedtuple on dict.items()

2020-03-31 Thread Cameron Simpson

On 31Mar2020 22:28, Nadav Wexler  wrote:

that's what I assumed...
thanks for the reply! :)


Also, there are modules to provide this kind of thing when you want it.

For example, my cs.mappings module:

 https://pypi.org/project/cs.mappings/

has dicts_to_namedtuples() which converts an iterable of dicts to 
namedtuples, and the lower level function named_row_tuple(*keys) 
function used to create the specific namedtuple class from some keys (eg 
your_dict.keys()). There's also named_column_tuples() which presumes the 
column/attribute names come from the first data row.


I wrote these when processing some data received as a spreadsheet, whose 
column names (from the human typed header row) tended to be stable, but 
the number and ordering of the columns were undependable. Feeding them 
through these (via an additional layer in cs.csvutils) got me nice 
tuples with readable attribute names.


I'm sure there must be other modules with similar facilities as well.

Cheers,
Cameron Simpson 
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/UMGJQPP5J44NX5FQNIOUWRTORCIISRVU/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Matthias Bussonnier
On Tue, 31 Mar 2020 at 10:18, Gerrit Holl  wrote:

> the command ``cat $(find . -name '*.py') | grep -oi term | wc -l`` was used.

I'm quite concern in the lack of mention of proper forms and procedure
to perform experiment with domestic felines.
-- 
Mathias
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/YE6GDMDZ53IP5BRKU72XMTSSG24NJ34R/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: dunder methods for encoding & prettiness aware formal & informal representations

2020-03-31 Thread Wes Turner
Bump to CC Re: "[Python-ideas] pretty-printing in the terminal"

On Sun, Mar 22, 2020, 8:53 PM Kyle Stanley  wrote:

> I ended up opening an issue for it at https://bugs.python.org/issue40045.
> 
>
> On Sun, Mar 22, 2020 at 8:33 PM Kyle Stanley  wrote:
>
>> Chistopher Barker wrote:
>> > I'd suggest you make a PR on the docs.
>>
>> Yeah I was planning on either doing that, or opening it as a "newcomer
>> friendly"/"easy" issue on bugs.python.org. IMO, it could make for a
>> decent first PR.
>>
>> On Sun, Mar 22, 2020 at 1:28 PM Christopher Barker 
>> wrote:
>>
>>> On Fri, Mar 20, 2020 at 8:52 PM Kyle Stanley  wrote:
>>>
 >
 https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers

 I remembered the existence of this rule and tried to locate it recently
 (prior to this discussion), but was unable to because it doesn't explicitly
 mention "dunder". IMO, it would make that section much easier to find if it
 were to say either of:

 1) System-defined names, also known as "dunder" names.
 2) System-defined names, informally known as "dunder" names.
 3) System-defined "dunder" names.

>>>
>>> The word "dunder" was coined (relatively) recently. It was not in common
>>> use when those docs were written. Another common nickname is "magic
>>> methods".
>>>
>>> So that explains why the word "dunder" isn't in those docs. And those
>>> docs are pretty "formal" / "technical" anyway, not designed for the casual
>>> user to read.
>>>
>>> But yes, it would be good to add a bit of that text to make it more
>>> findable.
>>>
>>> I'd suggest you make a PR on the docs.
>>>
>>> -CHB
>>>
>>>
>>> Python Language Consulting
>>>   - Teaching
>>>   - Scientific Software Development
>>>   - Desktop GUI and Web Development
>>>   - wxPython, numpy, scipy, Cython
>>>
>> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/I4GRJAO37DYY5ZJDBU4XWIEZQT4WRXR4/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XJTZP6VXRN47GHWZDC3XWRLLYGAYL3NG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: pretty-printing in the terminal

2020-03-31 Thread Wes Turner
"[Python-ideas] dunder methods for encoding & prettiness aware formal &
informal representations"
https://mail.python.org/archives/list/python-ideas@python.org/thread/OQPPJ7SNM5CZUI5RYT5R4Z6YZWMNNTZS/

discusses how pretty printing could be extended. I'll bump the thread

On Tue, Mar 31, 2020, 5:54 PM Per Bothner  wrote:

> Inspired by the LWN article (https://lwn.net/Articles/815265/) about
> enhanced pretty-printing,
> I implemented a modified repr/displayhook that makes uses of the builtin
> pretty-printing of the DomTerm (https://domterm.org) terminal emulator.
>
> See this screenshot http://domterm.org/images/python-pretty1.png which
> shows
> the *same* REPL session displayed in two "panes" of different widths.
> More information here:
> http://domterm.org/Python-tips.html
>
> The idea is that Python handles *what* to print, but the terminal emulator
> (or IDE like Jupyter or ipython) decides where to break the lines, add
> indentation, etc.
> The terminal knows the line-width and can dynamically re-format the output
> if the width changes - even for previously-printed output (and even for an
> exited python command).  This is just like many terminals will
> automatically
> re-wrap over-long lines - but on steroids.
>
> This required a rather small amount of code to insert some extra escape
> sequences
> to delimit grouping, optional line-breaks, and indentation - see here:
>
> https://github.com/PerBothner/DomTerm/blob/master/tools/python/domterm/utils.py
>
> I'm not suggesting pprint should depend on the terminal being able to do
> pretty-printing, but I suggest that at the least type-specific
> pretty-printers
> should use overrideable methods to define grouping and optional newlines.
> That would allow an IDE or terminal-specific code to by-pass the Python
> pretty-printing and instead have the terminal/IDE do it.
>
> As an example you can see how Kawa does it - look for the writeList method
> in
>
> https://gitlab.com/kashell/Kawa/-/blob/master/gnu/kawa/functions/DisplayFormat.java
> This calls conceptually abstract methods like startLogicalBlock and
> endLogicalBlock (to delimit groups) and writeSpaceFill (to mark optional
> newline).
>
> Going beyond pretty-printing, I've been thinking about a "data explorer"
> where
> you click on buttons to hide/show more detail.  I'm most familiar with this
> from the JavaScript consoles of Firefox and Chrome.  I implemented a
> "finite"
> version of this idea for Kawa - see here:
> https://domterm.org/saved/dt-prettynested.html
> Note the triangles are clickable butoons that hide or show detail (and how
> this is integrated with pretty-printing).
> This prototype only handles the case where all the displayable data is
> printed,
> even though some may be initially hidden.  To explore larger or circular
> data
> structures you'd want a protocol where the terminal/IDE can request more
> data.
> (I have a rough design for this, but that is a different discussion.  Let
> me know if this would be interesting.)
>
> (I am not primarily a Python programmer, but it's been fun figuring out
> how to implement Python/DomTerm integration.)
> --
> --Per Bothner
> p...@bothner.com   http://per.bothner.com/
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/Z5CZH7ODJOZLPTY5S7TRWYGZVBAGU4SP/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DAGZLAARTTUF7IOS4IBHFM3ZS7O2GC3B/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] pretty-printing in the terminal

2020-03-31 Thread Per Bothner

Inspired by the LWN article (https://lwn.net/Articles/815265/) about enhanced 
pretty-printing,
I implemented a modified repr/displayhook that makes uses of the builtin
pretty-printing of the DomTerm (https://domterm.org) terminal emulator.

See this screenshot http://domterm.org/images/python-pretty1.png which shows
the *same* REPL session displayed in two "panes" of different widths.
More information here:
http://domterm.org/Python-tips.html

The idea is that Python handles *what* to print, but the terminal emulator
(or IDE like Jupyter or ipython) decides where to break the lines, add 
indentation, etc.
The terminal knows the line-width and can dynamically re-format the output
if the width changes - even for previously-printed output (and even for an
exited python command).  This is just like many terminals will automatically
re-wrap over-long lines - but on steroids.

This required a rather small amount of code to insert some extra escape 
sequences
to delimit grouping, optional line-breaks, and indentation - see here:
https://github.com/PerBothner/DomTerm/blob/master/tools/python/domterm/utils.py

I'm not suggesting pprint should depend on the terminal being able to do
pretty-printing, but I suggest that at the least type-specific pretty-printers
should use overrideable methods to define grouping and optional newlines.
That would allow an IDE or terminal-specific code to by-pass the Python
pretty-printing and instead have the terminal/IDE do it.

As an example you can see how Kawa does it - look for the writeList method in
https://gitlab.com/kashell/Kawa/-/blob/master/gnu/kawa/functions/DisplayFormat.java
This calls conceptually abstract methods like startLogicalBlock and
endLogicalBlock (to delimit groups) and writeSpaceFill (to mark optional 
newline).

Going beyond pretty-printing, I've been thinking about a "data explorer" where
you click on buttons to hide/show more detail.  I'm most familiar with this
from the JavaScript consoles of Firefox and Chrome.  I implemented a "finite"
version of this idea for Kawa - see here:
https://domterm.org/saved/dt-prettynested.html
Note the triangles are clickable butoons that hide or show detail (and how
this is integrated with pretty-printing).
This prototype only handles the case where all the displayable data is printed,
even though some may be initially hidden.  To explore larger or circular data
structures you'd want a protocol where the terminal/IDE can request more data.
(I have a rough design for this, but that is a different discussion.  Let
me know if this would be interesting.)

(I am not primarily a Python programmer, but it's been fun figuring out
how to implement Python/DomTerm integration.)
--
--Per Bothner
p...@bothner.com   http://per.bothner.com/
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/Z5CZH7ODJOZLPTY5S7TRWYGZVBAGU4SP/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Paul Sokolovsky
Hello,

On Tue, 31 Mar 2020 16:28:22 +1100
Steven D'Aprano  wrote:

> On Mon, Mar 30, 2020 at 10:08:06PM -0700, Guido van Rossum wrote:
> > On Mon, Mar 30, 2020 at 10:00 PM Steven D'Aprano
> >  wrote:
> >   
> > > > it’s optimized for a different use case than string building,  
> > >
> > > It is? That's odd. The whole purpose of StringIO is to build
> > > strings.  
> 
> I misspoke: it is not the *whole* purpose. See below.

Steven, I appreciate keeping up discussion from the same angle as I see
it. (I read the thread in chronological order, and see that I could
save on some replies, just +1's your.)

For the last sentence above, I could offer an explanation "from the
other side". Just imagine that we took io.StringIO and:

1. Limit its constructor to StringIO(). (No parameters, no universal
newlines!)
2. Leave only the methods .write(), .getvalue(), and remove all other
methods.

Now, the *whole* purpose of such a class would be string building, it
literally can't do anything else.

Suppose we now indeed split off such a class, and named it
StringBuilder. We'd now have an bit of issue explaining why all that
was done. "StringBuffer implements subset of the functionality of
StringIO". D'oh. 

Looking on the implementation side, this StringBuffer class would also
have the machinery already present in StringIO, like backing store for
accumulated content, size/offset into this backing store. Yes, perhaps
it could have code a bit more specialized for just writing and no
reading, but is it worth code duplication?

So, the core of the criticism seems to be that StringIO was designed for
"another purpose" and that it "does much more". It's a bit strange
argumentation that a more generic/featureful object can't (just
can't) be used for subset of its functionality, you should be always
concerned that it can do much more. It's a bit strange logic, akin to
"lists are intended to store arbitrary objects, so if you store
integers in them, you're doing something wrong".

Let's get to our StringBuffer class as constructed above. Now that it
doesn't have "IO" at the end, we perhaps can give up and add __iadd__
method to it, having the same semantics as .write(), but returning
self (thanks for correction!). But if we can do that, then with
the arguments above re: entity and code duplication, perhaps we can do
that on the original StringIO. About the only thing that can preclude
that is an irrational belief that given that originally it was intended
for more general usecase, then we can't, just can't, nudge to make it
more comfortable for a subset of its usage.


-- 
Best regards,
 Paul  mailto:pmis...@gmail.com
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/74KZDMS5U2Z6RSB5GZXWZ6WSAEAIRZ45/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Compound with .. else statement

2020-03-31 Thread Andrew Barnert via Python-ideas
On Mar 31, 2020, at 03:06, Jimmy Thrasibule  wrote:
> 
> 
>> 
>> But if you use a context manager which silences the exception, like
>> contextlib.suppress() or unittest.TestCase.assertRaises(), it is easy to
>> do too.
>> 
>> was_not_raised = False
>> with my_context():
>>   do_something_sensitive()
>>   was_not_raised = True
>> if was_not_raised:
>> print("We're all safe.")
> 
> That is indeed a way to workaround my use case. I do still find a
> with/else more elegant.

It might help your proposal to just show a small concrete and realistic example 
of how this workaround parallels the workaround for not having for/else, and 
how your proposed change would let you improve your code’s readability in 
exactly the same way as for/else. At least for me, it’s always been easier to 
show a newcomer to Python the point of for/else with a nice example than to try 
to explain the semantics and why they’re useful, and I assume the same would be 
true here.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/C5ATRBHHKOIOFIWET2J7ONGOJGK7VGBA/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Andrew Barnert via Python-ideas
On Mar 31, 2020, at 12:06, Paul Sokolovsky  wrote:

I don’t know why you think being snarky helps make your case. If you make a 
mistake and it’s pointed out and you give a sarcastically over-enthusiastic 
thanks, that doesn’t change the fact that it‘s wrong, and if your rationale 
depends on things that aren’t true, your proposal doesn’t stand.

For example, your demonstration that str.join takes 10x the memory of StringIO 
in CPython is wrong because you didn’t actually include the cost of the list of 
buffers inside the StringIO, and once you do, StringIO is actually larger 
rather than 1/10th the size. It doesn’t matter how much you try to belittle 
that point or the way it was made by exaggeratedly apologizing, it’s still true 
that changing everyone’s code to do things your way instead of the way they’ve 
always done things would increase, not decrease, their memory usage, not just 
in theoretical possible implementations of Python but in multiple real life 
implementations, including CPython.

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PFFAM7PZ7R7X2YBMF3GA3T5QPN3ENR6E/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Paul Sokolovsky
Hello,

On Mon, 30 Mar 2020 23:20:28 -0400
David Mertz  wrote:

> I have myself been "guilty" of using the problem style for N < 10.  In
> fact, I had forgotten about the optimization even, since my uses are
> negligible time.

I personally don't think it's a "problem style" per se. I'm using it all
the time. I'm going to keep using it. We all love Python for being a
language which you can easily prototype in, and don't have to
concern yourself with implementation details unless you want. My
concern for Python to be a language which can be progressively and
easily optimized when you need it.

Going over the code and changing many lines along the lines of diff:

- buf += piece
+ buf.append(piece)

isn't my idea of "seamless" optimization, especially that I know
it guaranteedly will grow my memory usage.

Sadly I came to conclusion that even

- buf += piece
+ buf.write(piece)

patching hurts my aesthetic feelings. At least, that's the only
explanation I have for why in one of my modules
https://github.com/pfalcon/pycopy-lib/blob/master/utokenize/utokenize.py#L44
I converted one loop from "str +=" to "StringIO.write()", but not
another. I then tried to think what could help with that and having +=
on StringIO seemed to do the trick.

> 
> For stuff like this, it's fast no matter what:
> 
> for clause in query_clauses:
> sql += clause
> 
> Maybe I have a WHERE or two.  Maybe an ORDER BY.  Etc.  But if I'm
> sure there won't be more than 6 such clauses to the query I'm
> building, so what? Or probably likewise with bits of a file path, or
> a URL with optional parameters, and a few other things.
> 
> On Mon, Mar 30, 2020 at 11:15 PM David Mertz  wrote:
> 
> > Does anyone know if any linters find and warn about the `string +=
> > word` in a loop pattern? It feels like a linter would be the place
> > to do that.  I don't think we could possibly make it an actual
> > interpreter warning given borderline OK uses (or possibly even
> > preferred ones).  But a little nagging in tooling could draw
> > attention.

[]

-- 
Best regards,
 Paul  mailto:pmis...@gmail.com
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JTWGFXK3KTTTRLXW25OKE7SH37A47AGV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Andrew Barnert via Python-ideas
On Mar 31, 2020, at 12:03, Oleg Broytman  wrote:
> 
> Animals can fight or run, but plants cannot. So we must protect
> plants more, not less!

That’s just what the plants want you to believe. Have you ever seen how trees 
get on when there are no humans about? No, of course you haven’t, because it’s 
impossible. If a tree falls in the forest and nobody’s around to hear it, it’s 
probably breaking into your house. Trust me, your average tree (not a larch, of 
course, but they’re hardly average) can outrun a hedgehog without breaking a 
sweat. We need to treat each plant individually, not overgeneralize them as a 
group. Sure, a young, innocent oak sapling deserves our protection, but some of 
those redwoods, well, they didn’t get to be 4700 years old by being helpless. 
Get cornered by one of them in a dark alley in Mammoth and you’ll be wishing it 
was a bunch of redcurrants or a tiger you were dealing with. Which is why we 
should all hunt and eat redwood trees rather than cows or bananas. And don’t 
even get me started on sunflowers.


___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/SCVCAXII4Z4U53XBBQBMCCHDVQRHS35O/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Andrew Barnert via Python-ideas
On Mar 31, 2020, at 10:54, Evpok Padding  wrote:
> 
> 
> Even if you think this is clever or funny, are you absolutely sure you needed 
> to share it with the subscribers to this mailing list?

Now, I’ve noticed a tendency for this thread to get rather silly. Now I do my 
best to keep things moving along, but I’m not having things getting silly. 
Those last two replies got very silly indeed. And the one about the animal 
cruelty was even sillier. Now, nobody likes a good laugh more than I do. Except 
perhaps my wife and some of her friends. Oh, yes, and Captain Johnson. Come to 
think of it, most people like a good laugh more than I do, but that’s beside 
the point. Now, let’s have a good, clean, healthy April Fools’ PEP thread. Get 
some air into your lungs. 9, 9, 9, 9 and all that. Ah yes, that’s better. Now 
let’s hope this doesn’t get silly.

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/AYF2T7HKXIICVGQH7IDW3ROSOPNLLGXY/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread jdveiga
Paul Sokolovsky wrote:
> Hello,
> On Mon, 30 Mar 2020 16:25:07 -0700
> Christopher Barker python...@gmail.com wrote:
> > As others have pointed out, the OP started in a  bit
> > of an oblique
> > way, but it maybe come down to this:
> > There are some use-cases for a mutable string type.
> > For avoidance of doubt: nothing in my RFC has anything to do, or
> implies, "a mutable string type". A well-know pattern of string
> builder, yes. Piggybacking on existing StringIO/BytesIO classes, yes.
> Anything else, no.
> To not leave it cut and dry: IMHO, we need more const'ness in Python,
> not less. I my dreams I already do stuff like:
> from __future__ import const
> class Foo:
> pass
> # This is an alias for "Foo"
> Bar: const = Foo
> # This is a variable which can store a reference to Foo or any other class
> Baz = Foo
> [This is not a new RFC! Please start a new thread if you'd like to pick
> it up ;-)]

If I understand you are proposing a change from StringIO `write` method to `+=` 
operator. Is it right?

I cannot see any advantage on this proposal since there is no real change in 
the implementation of StringIO. Or are you proposing any change in the 
underlying implementation and I have missed that point?

In this case, I disagree with you: StringIO is a stream and I think that it is 
wrong to make it to "look & feel" like a string. That is my opinion.

Sorry if I misunderstand you.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PHGIESG7VEBZZQGI2IZW7DHWH4MJWLOF/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Mark Dickinson
>
>
> Proposed alternatives
> =
>
> Keeping with the good practice of referencing sketches from Monty
> Python's
> Flying Circus, this PEP proposes to adopt the fruits mentioned in the
> `"Self-Defence Against Fresh Fruit" sketch`_:
>
> * raspberry (not currently in use)
> * banana  (68 times in standard library)

[...]


Sorry, I'm afraid that "banana" is not an acceptable alternative here. It's
well known that botanically, bananas are a type of fish. Please see the
study at https://www.goodreads.com/quotes/6870748 for more information on
this.

-- 
Mark
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ROQI5Z3DP3KRYNC456RAZGCUWSDAU7LO/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: returning a namedtuple on dict.items()

2020-03-31 Thread Nadav Wexler
that's what I assumed...
thanks for the reply! :)

On Tue, Mar 31, 2020 at 9:08 PM Serhiy Storchaka 
wrote:

> 31.03.20 20:15, Nadav Wexler пише:
> > Hi,
> > That is my first post here, I hope it's the right place for it.
> >
> > so I was using some dicts had the following code:
> >  env = dict(t for t in os.environ.items() if 'SUBSTRING' not in
> > t[0])
> >
> > and I thought: It could have really been nice if i could do:
> >  env = dict(t for t in os.environ.items() if 'SUBSTRING' not in
> > t.key)
> >
> > now I see that environ is not exactly a dict, but i think this is useful
> > for all dictionary types. on the other hand, dicts are everywhere and
> > i'm not sure about the performance impact of this for those that just
> > ignore the access by attribute.
> >
> > of course, a matching t.value must be in place as well. ;)
> > I would have opened a PR just to try out, but I feel this is so in the
> > core that I'd first like to hear some comments.
> > I was searching if anyone else had this idea before but couldn't find
> > anything.
>
> This idea was proposed multiple times and was rejected. Python is
> optimized for raw tuples, it is one of most important type. Many
> operations with tuples are much faster than operations with named tuples
> (including creating and destroying). Tuples emitted by dict.item() (an
> enumerate(), and zip()) are usually short-living and produced in mass.
> So replacing tuples with named tuples would harm a lot of code which do
> not need attribute access.
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/VRVLQHMKFMVEC4T7VN5QEVSFGPEZWG7P/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/IPYV2L4H5N5CANRQZMCLK422SBPJU7PN/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Paul Sokolovsky
Hello,

On Mon, 30 Mar 2020 16:25:07 -0700
Christopher Barker  wrote:

> As others have pointed out, the OP started in a  bit of an oblique
> way, but it maybe come down to this:
> 
> There are some use-cases for a mutable string type.

For avoidance of doubt: nothing in my RFC has anything to do, or
implies, "a mutable string type". A well-know pattern of string
builder, yes. Piggybacking on existing StringIO/BytesIO classes, yes.
Anything else, no.


To not leave it cut and dry: IMHO, we need more const'ness in Python,
not less. I my dreams I already do stuff like:

from __future__ import const

class Foo:
pass

# This is an alias for "Foo"
Bar: const = Foo

# This is a variable which can store a reference to Foo or any other class
Baz = Foo

[This is not a new RFC! Please start a new thread if you'd like to pick
it up ;-)]


-- 
Best regards,
 Paul  mailto:pmis...@gmail.com
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XS6FCMOYALLDJWQDZY33OZDAOM3LFJ47/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Paul Sokolovsky
Hello,

On Mon, 30 Mar 2020 13:59:42 -0700
Andrew Barnert  wrote:

> On Mar 30, 2020, at 13:06, Paul Sokolovsky  wrote:
> > 
> > I appreciate expressing it all concisely and clearly. Then let me
> > respond here instead of the very first '"".join() rules!' reply I
> > got.  
> 
> Ignoring replies doesn’t actually answer them.

I'm happy to discuss various points, but it would be nice to have
discussion focused, giving that the change proposed is pretty simple.
I'm not sure if it my fault by having tried to structure the original
RFC as a poor-man's PEP (so it's somewhat long'ish), but I definitely
would like to avoid discussing extended topics along the lines of
"there're some mundane languages which offer those string builder
classes, but Python is so, SO, special, that it doesn't need it, and
whoever thinks otherwise just doesn't get it" or "building a string
from pieces by putting pointers to pieces into array, and then
concatenating them together is the PEAK achievement of the computer
science, and whoever didn't get that just... just... didn't read
CPython (yes, CPython!) FAQ".

> > The issue with "".join() is very obvious:
> > 
> > --
> > import io
> > import sys
> > 
> > 
> > def strio():
> >sb = io.StringIO()
> >for i in range(5):
> >sb.write(u"==%d==" % i)
> >print(sys.getsizeof(sb) + sys.getsizeof(sb.getvalue()))  
> 
> This doesn’t tell you anything useful. As the help for getsizeof
> makes clear, “Only the memory consumption directly attributed to the
> object is accounted for, not the memory consumption of objects it
> refers to”. So this gives you some fixed value like 152, no matter
> how big the buffer and other internal objects may be.

Yeah, I tried to account for that with "sys.getsizeof(sb) +
sys.getsizeof(sb.getvalue())", thanks for noticing that.

> If you’re using CPython with the C accelerator, none of those things
> are available to you from the API, but a quick scan of the C source
> shows what’s there, and it’s generally actually more storage than the
> list version. Oversimplifying a bit: While you’re building, it keeps
> a _PyAccu structure, which is basically a wrapper around that same
> list of strings. When you call getvalue() it then builds a Py_UCS4*
> representation that’s in this case 4x the size of the final string
> (since your string is pure ASCII and will be stored in UCS1, not
> UCS4). And then there’s the final string.

Thanks very much for this intro into the CPython
io.StringIO implementation, much appreciated. Please let me return the
favor and explain how StringIO implemented in Pycopy, which I happen to
maintain, and in MicroPython (as the original implementation was written
by me there). So, there's an array of bytes. Both implementations use
utf-8 to store strings. So, StringIO stores as many bytes as there're
actual data in (utf-8) strings. Of course, there's some over-allocation
policy to avoid severe quadratic behavior on growing. Overall, storing N
bytes of string data requires N + small % of N bytes of data. No
additional array of pointers is needed. Original constituent strings
(each over-allocated of course) can be GCed in the meantime.

The moral is known, and was stated in the original RFC: for as
long as somebody's attention is fixated on CPython, the likely
reply from them would be: "there's no problem with CPython3, so there's
nothing to fix". It takes to step up, think about *multiple*
implementations and *interface* they *can* offer.

> So, if this memory issue makes join unacceptable, it makes your
> optimization even more unacceptable.
> 
> And thinking about portable code makes it even worse. Your code might
> be run under CPython and take even more memory, or it might be run
> under a different Python implementation where StringIO is not
> accelerated (where it’s just a TextIOWrapper around a BytesIO) and
> therefore be a whole lot slower instead. So it has to be able to deal
> with both of those possibilities, not just one; code that uses the
> usual idiom, on the other hand, behaves pretty similarly on all
> implementations.

Indeed, it absolutely and guaranteedly wastes a lot of memory. (It's
also the fastest, no worries.) 

> > There's absolutely no need why performing trivial operation of
> > accumulating string content should take about order of magnitude
> > more memory than actually needed for that string content. Don't get
> > me wrong
> > - if you want to spend that much of your memory, then sure, you
> > can. But jumping with that as *the only right solution* whenever
> > somebody mentions "string concatenation" is a bit ... umm,
> > cavalier  
> 
> And making a wild guess about how things might be implemented and
> offering an optimization based on that guess that actually makes
> things worse and refusing to even reply when people point out the
> problems isn’t even more cavalier?

The point I tried to show is that StringIO is never worse than str +=
regarding performance (stats for 8 

[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Oleg Broytman
On Tue, Mar 31, 2020 at 09:08:50PM +0300, Serhiy Storchaka 
 wrote:
> 31.03.20 20:52, Antoine Pitrou пише:
> > Your search is incomplete, for example you failed to account for
> > occurrences of "cheese" and "milkshake".
> 
> I thought palm trees are plants.

   Animals can fight or run, but plants cannot. So we must protect
plants more, not less!

Oleg.
-- 
Oleg Broytmanhttps://phdru.name/p...@phdru.name
   Programmers don't die, they just GOSUB without RETURN.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PAKIUCXE5233ZRCE7UZL3AULNX2IJRNL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Dan Sommers
On Tue, 31 Mar 2020 19:17:18 +0200
Gerrit Holl  wrote:

> Abstract
> 

> Python has long used metasyntactic variables that are based on the
> consumption of meat and dairy products, such as "spam", "ham", and
> "eggs".

> This language is not considerate to pigs or chicken and violates the
> spirit of the Code of Conduct.  This PEP proposes to retire the use
> of those names in official Python documentation and source code and
> to recommend users of Python to do the same.

Dear Sir,

I wish to complain in the strongest possible terms about the PEP which
has just been posted about the unseemly treatment of farm animals.

My farm raised pythons are never confined to a cage, and I assure you
that all mistreatment is deliberately unsystematic.

Brigadier Sir Daniel Sommers, retired, Mrs.

>Local Variables:
>mode: indented-text
>indent-tabs-mode: nil
>sentence-end-double-space: t
>fill-column: 70
>coding: utf-8
>End:

Oh, dear Lord, no, please don't start another emacs/vi war, let alone
a lengthy line length debate.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/D4YT6BBCUUXLMAKDXLCDYSDT3SHZTQ7F/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Ricky Teachey
Just in case this was not proposed in jest:

How about just adding a note stipulating that if found offensive, all
references to animal derived products are encouraged to be interpreted as
their real or hypothetical plant-based alternatives, some of which exist
today (plant based "ham", "chicken") and others that will presumably also
exist in the future (unless I'm wrong, plant based eggs)?

On Tue, Mar 31, 2020, 2:13 PM Serhiy Storchaka  wrote:

> 31.03.20 20:52, Antoine Pitrou пише:
> > Your search is incomplete, for example you failed to account for
> > occurrences of "cheese" and "milkshake".
>
> I thought palm trees are plants.
> ___
> Python-ideas mailing list -- python-ideas@python.org
> To unsubscribe send an email to python-ideas-le...@python.org
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at
> https://mail.python.org/archives/list/python-ideas@python.org/message/FNBM2HSIH4KHSCSV2CB7MW6NBDTWUDZJ/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/R7IY36E3RSVWLSHWFCE5DPATOQIG7EFL/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Chris Angelico
On Wed, Apr 1, 2020 at 4:19 AM Gerrit Holl  wrote:
> Abstract
> 
>
> Python has long used metasyntactic variables that are based on the
> consumption of meat and dairy products, such as "spam", "ham", and
> "eggs".
> This language is not considerate to pigs or chicken and violates the
> spirit of the Code of Conduct.  This PEP proposes to retire the use
> of those names in official Python documentation and source code and to
> recommend users of Python to do the same.
>

While I wholeheartedly agree with your intention here, I believe the
backward incompatibilities would make this untenable. We will need a
migration plan. Unfortunately, African swallows are non-migratory,
severely impacting your proposal.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FRNE2HZWLBSONIHYHK2Z3OVXCRT4MMY7/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Issue 34850: Syntax Warning in the real world (is 0 / is 1)

2020-03-31 Thread M.-A. Lemburg
On 3/31/2020 7:58 PM, Serhiy Storchaka wrote:
> 31.03.20 20:21, Andrew Barnert via Python-ideas пише:
>> Do you have code that you think actually _should_ be using is 1? Or
>> code that you have to compile over and over (e.g., your deployment
>> server doesn’t cache .pyc files, or you spawn new instances all the
>> time without pre-built .pycs, or your app generates and compiles code
>> on the fly, or …) and can’t change? If there are legitimate cases like
>> those, I think that could definitely be a compelling argument against
>> using SyntaxWarning here. But “the warning fired as intended and
>> showed me a bug in my code” isn’t. And without more context, it’s hard
>> to know which is the case here. So can you explain why this is a
>> problem for you?
> 
> Twice in the last month I have been creating a PR for CPython which
> contains "is 1". I knew what I do.
> 
> One of them was in C. I needed very fast and simple test for integers 0
> and 1, so I used the C equivalent of the "is" operator (compared
> pointers) with objects representing Python integers 0 and 1. And more, I
> used private CPython singletons _PyLong_Zero and _PyLong_One. There is
> no guarantee that all integer 0 an 1 are represented as these singletons
> (even in CPython), but it was only for optimization. In worst case I
> would lose an optimization but still got a correct result.
> 
> In second PR, I needed to determine if the optional argument with
> default value 1 was passed, and use fast path if it was not passed. "==
> 1" did not work, because 1.0 was error. It was first time when I
> encountered a SyntaxWarning I added myself. The workaround was simple:
> add a global `_ONE = 1` and use it as a default value and with the "is"
> operator. It would work even if 1 was not interned. In any way, this PR
> was rejected.
> 
> Conclusion? If you know what do you do you can find a workaround. But if
> you use this because you do not know difference between "is" and "=="
> and it works for you now, the warning can save you from possible bugs
> and teach you something.

I think that's good reasoning. "is " will almost always be
a typo or misunderstanding of what "is" actually does.

"is" should really only be used to determine whether some variable
is pointing to a specific object. Using singletons for e.g. default
values is a case where such a test can be put to good use  - just beware
of certain optimizations in CPython, e.g. the interning of small
integers and (some) single character strings.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Experts (#1, Mar 22 2020)
>>> Python Projects, Coaching and Support ...https://www.egenix.com/
>>> Python Product Development ...https://consulting.egenix.com/


::: We implement business ideas - efficiently in both time and costs :::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   https://www.egenix.com/company/contact/
 https://www.malemburg.com/
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/QCDJLPRW5WQDKYFNEQRD5IXPBEXCM4KU/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Serhiy Storchaka

31.03.20 20:52, Antoine Pitrou пише:

Your search is incomplete, for example you failed to account for
occurrences of "cheese" and "milkshake".


I thought palm trees are plants.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FNBM2HSIH4KHSCSV2CB7MW6NBDTWUDZJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: returning a namedtuple on dict.items()

2020-03-31 Thread Serhiy Storchaka

31.03.20 20:15, Nadav Wexler пише:

Hi,
That is my first post here, I hope it's the right place for it.

so I was using some dicts had the following code:
         env = dict(t for t in os.environ.items() if 'SUBSTRING' not in 
t[0])


and I thought: It could have really been nice if i could do:
         env = dict(t for t in os.environ.items() if 'SUBSTRING' not in 
t.key)


now I see that environ is not exactly a dict, but i think this is useful 
for all dictionary types. on the other hand, dicts are everywhere and 
i'm not sure about the performance impact of this for those that just 
ignore the access by attribute.


of course, a matching t.value must be in place as well. ;)
I would have opened a PR just to try out, but I feel this is so in the 
core that I'd first like to hear some comments.
I was searching if anyone else had this idea before but couldn't find 
anything.


This idea was proposed multiple times and was rejected. Python is 
optimized for raw tuples, it is one of most important type. Many 
operations with tuples are much faster than operations with named tuples 
(including creating and destroying). Tuples emitted by dict.item() (an 
enumerate(), and zip()) are usually short-living and produced in mass. 
So replacing tuples with named tuples would harm a lot of code which do 
not need attribute access.

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/VRVLQHMKFMVEC4T7VN5QEVSFGPEZWG7P/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Issue 34850: Syntax Warning in the real world (is 0 / is 1)

2020-03-31 Thread Serhiy Storchaka

31.03.20 20:21, Andrew Barnert via Python-ideas пише:

Do you have code that you think actually _should_ be using is 1? Or code that 
you have to compile over and over (e.g., your deployment server doesn’t cache 
.pyc files, or you spawn new instances all the time without pre-built .pycs, or 
your app generates and compiles code on the fly, or …) and can’t change? If 
there are legitimate cases like those, I think that could definitely be a 
compelling argument against using SyntaxWarning here. But “the warning fired as 
intended and showed me a bug in my code” isn’t. And without more context, it’s 
hard to know which is the case here. So can you explain why this is a problem 
for you?


Twice in the last month I have been creating a PR for CPython which 
contains "is 1". I knew what I do.


One of them was in C. I needed very fast and simple test for integers 0 
and 1, so I used the C equivalent of the "is" operator (compared 
pointers) with objects representing Python integers 0 and 1. And more, I 
used private CPython singletons _PyLong_Zero and _PyLong_One. There is 
no guarantee that all integer 0 an 1 are represented as these singletons 
(even in CPython), but it was only for optimization. In worst case I 
would lose an optimization but still got a correct result.


In second PR, I needed to determine if the optional argument with 
default value 1 was passed, and use fast path if it was not passed. "== 
1" did not work, because 1.0 was error. It was first time when I 
encountered a SyntaxWarning I added myself. The workaround was simple: 
add a global `_ONE = 1` and use it as a default value and with the "is" 
operator. It would work even if 1 was not interned. In any way, this PR 
was rejected.


Conclusion? If you know what do you do you can find a workaround. But if 
you use this because you do not know difference between "is" and "==" 
and it works for you now, the warning can save you from possible bugs 
and teach you something.

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/PCVDL5UGVB4FUTK2NCT7YJGRJAUBPK7C/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Evpok Padding
Even if you think this is clever or funny, are you absolutely sure you
needed to share it with the subscribers to this mailing list?

On Tue, 31 Mar 2020 at 17:46, Andrew Barnert via Python-ideas <
python-ideas@python.org> wrote:

> Dear Sir, I wish to complain in the strongest possible terms about the PEP
> which you have just transmitted about the inconsiderateness of spam. Many
> of my best friends are horned Vikings, and only a few of them are animal
> torturers. Yours faithfully, Brigadier Sir Andrew Barnert, OBE, retired,
> Mrs.
>
> Sent from my iPhone
>
> > On Mar 31, 2020, at 10:21, Gerrit Holl  wrote:
> >
> > (needs a sponsor)
> >
> > latest version at
> > https://github.com/gerritholl/peps/blob/animal-friendly/pep-.rst
> >
> > PEP: 
> > Title: Retire animal-unfriendly language
> > Author: Gerrit Holl 
> > Discussions-To: python-ideas@python.org
> > Status: Draft
> > Type: Informational
> > Content-Type: text/x-rst
> > Created: 01-Apr-2020
> > Post-History: 01-Apr-2020
> > Sponsor:
> >
> >
> > Abstract
> > 
> >
> > Python has long used metasyntactic variables that are based on the
> > consumption of meat and dairy products, such as "spam", "ham", and
> > "eggs".
> > This language is not considerate to pigs or chicken and violates the
> > spirit of the Code of Conduct.  This PEP proposes to retire the use
> > of those names in official Python documentation and source code and to
> > recommend users of Python to do the same.
> >
> >
> > Motivation and Rationale
> > 
> >
> > Estimates for the number of animals slaughtered for meat every year
> > vary, but `worldindata`_ estimates around 80 billion individuals.
> > Farmed animals are often kept in small cages with little to no access
> > to daylight, suffer stress during life and slaughter, or are otherwise
> > systematically mistreated.
> >
> > The `Python Code of Conduct`_ describes that community members are
> > open, considerate, and respectful.  The Python standard library and
> > documentation contain numerous references to meat or dairy based food
> > products that are not respectful to our fellow inhabitants of planet
> > Earth.  Examples include "spam", "bacon", and "eggs".
> >
> > To align the language use in the standard library and documentation
> > with
> > the Code of Conduct, use of such language should be retired.
> >
> >
> > Current practice
> > 
> >
> > There is a widespread tradition in the Python standard library, the
> > documentation, and the wider community, to include references to Monty
> > Pythons Flying Circus.  The use of "spam", "bacon", "sausage", and
> > "eggs" can be traced back to the `"Spam" sketch`_ originally broadcast
> > by the British Broadcasting Corporation (BBC) on 8 September 1972.
> > In this sketch, a couple are trying to order food in a diner where all
> > items contain spam.  The woman does not like spam and wants to order
> > food without spam.  A group of horned vikings then sing about the
> > wonderful spam.
> >
> > To get an overview of the usage in the current standard library, the
> > command ``cat $(find . -name '*.py') | grep -oi term | wc -l`` was
> > used.
> > This showed 2615 occurences for spam, 593 for ham (this include some
> > false positives, among other reasons due to references to people whose
> > name innociously contains the substring ham), 517 for eggs, 57 for
> > bacon,
> > and 10 for sausage.  Searching ``*.rst`` in the documentation revealed
> > 391 occurrences for spam, 82 for ham, 96 for eggs, 28 for bacon, and
> > 10 for sausage.  The source code for cpython revealed just 2 usages
> > for
> > spam and 1 for eggs.
> >
> > Proposed alternatives
> > =
> >
> > Keeping with the good practice of referencing sketches from Monty
> > Python's
> > Flying Circus, this PEP proposes to adopt the fruits mentioned in the
> > `"Self-Defence Against Fresh Fruit" sketch`_:
> >
> > * raspberry (not currently in use)
> > * banana  (68 times in standard library)
> > * apricot (not currently in use)
> > * pineapple (8 times in standard library)
> > * peach (once in standard library)
> > * redcurrant (not currently in use)
> > * damson (not currently in use)
> > * prune (23 times in standard library)
> >
> > Other possible alternatives keeping with food items:
> >
> > * salad (occurs once in standard library)
> > * aubergine (referred to in the spam sketch)
> > * shallot (the same)
> > * tofu (vegan protein alternative)
> >
> >
> > Specification
> > =
> >
> > For the reasons mentioned in the rationale, all references to meat or
> > dairy
> > products shall be removed from the Python standard library, the
> > documentation,
> > and the cpython source code.  The wider Python community is
> > recommended to
> > follow this practice.  In core Python:
> >
> > * Programmers SHALL NOT use the metasyntactic variables "spam", "ham",
> > "bacon",
> >  or "sausage", neither as variable names, nor in example strings, nor
> > 

[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Antoine Pitrou


Your search is incomplete, for example you failed to account for
occurrences of "cheese" and "milkshake".

Regards

Antoine.


On Tue, 31 Mar 2020 19:17:18 +0200
Gerrit Holl  wrote:
> (needs a sponsor)
> 
> latest version at
> https://github.com/gerritholl/peps/blob/animal-friendly/pep-.rst
> 
> PEP: 
> Title: Retire animal-unfriendly language
> Author: Gerrit Holl 
> Discussions-To: python-ideas@python.org
> Status: Draft
> Type: Informational
> Content-Type: text/x-rst
> Created: 01-Apr-2020
> Post-History: 01-Apr-2020
> Sponsor:
> 
> 
> Abstract
> 
> 
> Python has long used metasyntactic variables that are based on the
> consumption of meat and dairy products, such as "spam", "ham", and
> "eggs".
> This language is not considerate to pigs or chicken and violates the
> spirit of the Code of Conduct.  This PEP proposes to retire the use
> of those names in official Python documentation and source code and to
> recommend users of Python to do the same.
> 
> 
> Motivation and Rationale
> 
> 
> Estimates for the number of animals slaughtered for meat every year
> vary, but `worldindata`_ estimates around 80 billion individuals.
> Farmed animals are often kept in small cages with little to no access
> to daylight, suffer stress during life and slaughter, or are otherwise
> systematically mistreated.
> 
> The `Python Code of Conduct`_ describes that community members are
> open, considerate, and respectful.  The Python standard library and
> documentation contain numerous references to meat or dairy based food
> products that are not respectful to our fellow inhabitants of planet
> Earth.  Examples include "spam", "bacon", and "eggs".
> 
> To align the language use in the standard library and documentation
> with
> the Code of Conduct, use of such language should be retired.
> 
> 
> Current practice
> 
> 
> There is a widespread tradition in the Python standard library, the
> documentation, and the wider community, to include references to Monty
> Pythons Flying Circus.  The use of "spam", "bacon", "sausage", and
> "eggs" can be traced back to the `"Spam" sketch`_ originally broadcast
> by the British Broadcasting Corporation (BBC) on 8 September 1972.
> In this sketch, a couple are trying to order food in a diner where all
> items contain spam.  The woman does not like spam and wants to order
> food without spam.  A group of horned vikings then sing about the
> wonderful spam.
> 
> To get an overview of the usage in the current standard library, the
> command ``cat $(find . -name '*.py') | grep -oi term | wc -l`` was
> used.
> This showed 2615 occurences for spam, 593 for ham (this include some
> false positives, among other reasons due to references to people whose
> name innociously contains the substring ham), 517 for eggs, 57 for
> bacon,
> and 10 for sausage.  Searching ``*.rst`` in the documentation revealed
> 391 occurrences for spam, 82 for ham, 96 for eggs, 28 for bacon, and
> 10 for sausage.  The source code for cpython revealed just 2 usages
> for
> spam and 1 for eggs.
> 
> Proposed alternatives
> =
> 
> Keeping with the good practice of referencing sketches from Monty
> Python's
> Flying Circus, this PEP proposes to adopt the fruits mentioned in the
> `"Self-Defence Against Fresh Fruit" sketch`_:
> 
> * raspberry (not currently in use)
> * banana  (68 times in standard library)
> * apricot (not currently in use)
> * pineapple (8 times in standard library)
> * peach (once in standard library)
> * redcurrant (not currently in use)
> * damson (not currently in use)
> * prune (23 times in standard library)
> 
> Other possible alternatives keeping with food items:
> 
> * salad (occurs once in standard library)
> * aubergine (referred to in the spam sketch)
> * shallot (the same)
> * tofu (vegan protein alternative)
> 
> 
> Specification
> =
> 
> For the reasons mentioned in the rationale, all references to meat or
> dairy
> products shall be removed from the Python standard library, the
> documentation,
> and the cpython source code.  The wider Python community is
> recommended to
> follow this practice.  In core Python:
> 
> * Programmers SHALL NOT use the metasyntactic variables "spam", "ham",
> "bacon",
>   or "sausage", neither as variable names, nor in example strings, nor
> in
>   documentation.
> * Programmers SHALL NOT use the metasyntactic variable "eggs" in
> context with
>   food items, but may still use it in context of other body parts.
> Prohibited:
>   ``["salad", "eggs"]``.  Allowed: ``["ovaries", "pouch", "eggs"]``.
> * Programmers SHALL NOT use any other metasyntactic variable that is
> unfriendly
>   to animals.
> 
> The wider Python community is encouraged to adopt these practices as
> well, but
> the continued use of animal-unfriendly metasyntactic variables will
> not be
> considered a violation of the code of conduct.
> 
> 
> Rejected ideas
> ==
> 
> The authors carefully 

[Python-ideas] Re: PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Andrew Barnert via Python-ideas
Dear Sir, I wish to complain in the strongest possible terms about the PEP 
which you have just transmitted about the inconsiderateness of spam. Many of my 
best friends are horned Vikings, and only a few of them are animal torturers. 
Yours faithfully, Brigadier Sir Andrew Barnert, OBE, retired, Mrs.

Sent from my iPhone

> On Mar 31, 2020, at 10:21, Gerrit Holl  wrote:
> 
> (needs a sponsor)
> 
> latest version at
> https://github.com/gerritholl/peps/blob/animal-friendly/pep-.rst
> 
> PEP: 
> Title: Retire animal-unfriendly language
> Author: Gerrit Holl 
> Discussions-To: python-ideas@python.org
> Status: Draft
> Type: Informational
> Content-Type: text/x-rst
> Created: 01-Apr-2020
> Post-History: 01-Apr-2020
> Sponsor:
> 
> 
> Abstract
> 
> 
> Python has long used metasyntactic variables that are based on the
> consumption of meat and dairy products, such as "spam", "ham", and
> "eggs".
> This language is not considerate to pigs or chicken and violates the
> spirit of the Code of Conduct.  This PEP proposes to retire the use
> of those names in official Python documentation and source code and to
> recommend users of Python to do the same.
> 
> 
> Motivation and Rationale
> 
> 
> Estimates for the number of animals slaughtered for meat every year
> vary, but `worldindata`_ estimates around 80 billion individuals.
> Farmed animals are often kept in small cages with little to no access
> to daylight, suffer stress during life and slaughter, or are otherwise
> systematically mistreated.
> 
> The `Python Code of Conduct`_ describes that community members are
> open, considerate, and respectful.  The Python standard library and
> documentation contain numerous references to meat or dairy based food
> products that are not respectful to our fellow inhabitants of planet
> Earth.  Examples include "spam", "bacon", and "eggs".
> 
> To align the language use in the standard library and documentation
> with
> the Code of Conduct, use of such language should be retired.
> 
> 
> Current practice
> 
> 
> There is a widespread tradition in the Python standard library, the
> documentation, and the wider community, to include references to Monty
> Pythons Flying Circus.  The use of "spam", "bacon", "sausage", and
> "eggs" can be traced back to the `"Spam" sketch`_ originally broadcast
> by the British Broadcasting Corporation (BBC) on 8 September 1972.
> In this sketch, a couple are trying to order food in a diner where all
> items contain spam.  The woman does not like spam and wants to order
> food without spam.  A group of horned vikings then sing about the
> wonderful spam.
> 
> To get an overview of the usage in the current standard library, the
> command ``cat $(find . -name '*.py') | grep -oi term | wc -l`` was
> used.
> This showed 2615 occurences for spam, 593 for ham (this include some
> false positives, among other reasons due to references to people whose
> name innociously contains the substring ham), 517 for eggs, 57 for
> bacon,
> and 10 for sausage.  Searching ``*.rst`` in the documentation revealed
> 391 occurrences for spam, 82 for ham, 96 for eggs, 28 for bacon, and
> 10 for sausage.  The source code for cpython revealed just 2 usages
> for
> spam and 1 for eggs.
> 
> Proposed alternatives
> =
> 
> Keeping with the good practice of referencing sketches from Monty
> Python's
> Flying Circus, this PEP proposes to adopt the fruits mentioned in the
> `"Self-Defence Against Fresh Fruit" sketch`_:
> 
> * raspberry (not currently in use)
> * banana  (68 times in standard library)
> * apricot (not currently in use)
> * pineapple (8 times in standard library)
> * peach (once in standard library)
> * redcurrant (not currently in use)
> * damson (not currently in use)
> * prune (23 times in standard library)
> 
> Other possible alternatives keeping with food items:
> 
> * salad (occurs once in standard library)
> * aubergine (referred to in the spam sketch)
> * shallot (the same)
> * tofu (vegan protein alternative)
> 
> 
> Specification
> =
> 
> For the reasons mentioned in the rationale, all references to meat or
> dairy
> products shall be removed from the Python standard library, the
> documentation,
> and the cpython source code.  The wider Python community is
> recommended to
> follow this practice.  In core Python:
> 
> * Programmers SHALL NOT use the metasyntactic variables "spam", "ham",
> "bacon",
>  or "sausage", neither as variable names, nor in example strings, nor
> in
>  documentation.
> * Programmers SHALL NOT use the metasyntactic variable "eggs" in
> context with
>  food items, but may still use it in context of other body parts.
> Prohibited:
>  ``["salad", "eggs"]``.  Allowed: ``["ovaries", "pouch", "eggs"]``.
> * Programmers SHALL NOT use any other metasyntactic variable that is
> unfriendly
>  to animals.
> 
> The wider Python community is encouraged to adopt these practices as
> well, but
> the 

[Python-ideas] Re: returning a namedtuple on dict.items()

2020-03-31 Thread jdveiga
Nadav Wexler wrote:
> Hi,
> That is my first post here, I hope it's the right place for it.
> so I was using some dicts had the following code:
> env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t[0])
> and I thought: It could have really been nice if i could do:
> env = dict(t for t in os.environ.items() if 'SUBSTRING' not in
> t.key)
> now I see that environ is not exactly a dict, but i think this is useful
> for all dictionary types. on the other hand, dicts are everywhere and i'm
> not sure about the performance impact of this for those that just ignore
> the access by attribute.
> of course, a matching t.value must be in place as well. ;)
> I would have opened a PR just to try out, but I feel this is so in the core
> that I'd first like to hear some comments.
> I was searching if anyone else had this idea before but couldn't find
> anything.
> Thanks,
> Nadav

`env = {key: value for key, value in os.environ.items() if 'SUBSTRING' not in 
key}`
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5AERAPNDPAN4DEWNPKGBNEGQPFIXGEB2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Issue 34850: Syntax Warning in the real world (is 0 / is 1)

2020-03-31 Thread Andrew Barnert via Python-ideas
On Mar 31, 2020, at 05:59, Philip Kahn  wrote:
> 
> I understand it locks in that particular implementation detail, but it also 
> seems unlikely that that particular optimization (int 0 and int 1 as specific 
> memory objects) would ever make sense to NOT be including in a shipping 
> implementation (future proof by practicality).

Why? As far as I know, that optimization has never been included in any release 
of Jython, Iron, PyPy, Brython, or Skulpt. That particular test is true in some 
of these implementations anyway—in Brython because of a completely different 
optimization (ints up to 2**53 are usually smuggled around as doubles rather 
than as references to objects), in PyPy because of a pessimization they added 
to be more bug-compatible with CPython. And in some implementations it’s true 
sometimes and false other times (e.g., if they merge compile-time constants but 
don’t intern small ints, “is 1” will be true for a value that the compiler 
could reduce to a constant, but false for one that had to be computed at 
runtime). If you’re writing code like this because you expect that all Python 
interpreters will always have this optimization, your code is wrong and just 
happens to work in some situations.

Do you have code that you think actually _should_ be using is 1? Or code that 
you have to compile over and over (e.g., your deployment server doesn’t cache 
.pyc files, or you spawn new instances all the time without pre-built .pycs, or 
your app generates and compiles code on the fly, or …) and can’t change? If 
there are legitimate cases like those, I think that could definitely be a 
compelling argument against using SyntaxWarning here. But “the warning fired as 
intended and showed me a bug in my code” isn’t. And without more context, it’s 
hard to know which is the case here. So can you explain why this is a problem 
for you?
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/MOKVXFXJSZCCK5TGS77POTHSBDJFAREZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] returning a namedtuple on dict.items()

2020-03-31 Thread Nadav Wexler
Hi,
That is my first post here, I hope it's the right place for it.

so I was using some dicts had the following code:
env = dict(t for t in os.environ.items() if 'SUBSTRING' not in t[0])

and I thought: It could have really been nice if i could do:
env = dict(t for t in os.environ.items() if 'SUBSTRING' not in
t.key)

now I see that environ is not exactly a dict, but i think this is useful
for all dictionary types. on the other hand, dicts are everywhere and i'm
not sure about the performance impact of this for those that just ignore
the access by attribute.

of course, a matching t.value must be in place as well. ;)
I would have opened a PR just to try out, but I feel this is so in the core
that I'd first like to hear some comments.
I was searching if anyone else had this idea before but couldn't find
anything.

Thanks,
Nadav
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/CCEMPVLWUMDE64UBWCPTXKRLRHLNZB56/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] PEP 9999: Retire animal-unfriendly language

2020-03-31 Thread Gerrit Holl
(needs a sponsor)

latest version at
https://github.com/gerritholl/peps/blob/animal-friendly/pep-.rst

PEP: 
Title: Retire animal-unfriendly language
Author: Gerrit Holl 
Discussions-To: python-ideas@python.org
Status: Draft
Type: Informational
Content-Type: text/x-rst
Created: 01-Apr-2020
Post-History: 01-Apr-2020
Sponsor:


Abstract


Python has long used metasyntactic variables that are based on the
consumption of meat and dairy products, such as "spam", "ham", and
"eggs".
This language is not considerate to pigs or chicken and violates the
spirit of the Code of Conduct.  This PEP proposes to retire the use
of those names in official Python documentation and source code and to
recommend users of Python to do the same.


Motivation and Rationale


Estimates for the number of animals slaughtered for meat every year
vary, but `worldindata`_ estimates around 80 billion individuals.
Farmed animals are often kept in small cages with little to no access
to daylight, suffer stress during life and slaughter, or are otherwise
systematically mistreated.

The `Python Code of Conduct`_ describes that community members are
open, considerate, and respectful.  The Python standard library and
documentation contain numerous references to meat or dairy based food
products that are not respectful to our fellow inhabitants of planet
Earth.  Examples include "spam", "bacon", and "eggs".

To align the language use in the standard library and documentation
with
the Code of Conduct, use of such language should be retired.


Current practice


There is a widespread tradition in the Python standard library, the
documentation, and the wider community, to include references to Monty
Pythons Flying Circus.  The use of "spam", "bacon", "sausage", and
"eggs" can be traced back to the `"Spam" sketch`_ originally broadcast
by the British Broadcasting Corporation (BBC) on 8 September 1972.
In this sketch, a couple are trying to order food in a diner where all
items contain spam.  The woman does not like spam and wants to order
food without spam.  A group of horned vikings then sing about the
wonderful spam.

To get an overview of the usage in the current standard library, the
command ``cat $(find . -name '*.py') | grep -oi term | wc -l`` was
used.
This showed 2615 occurences for spam, 593 for ham (this include some
false positives, among other reasons due to references to people whose
name innociously contains the substring ham), 517 for eggs, 57 for
bacon,
and 10 for sausage.  Searching ``*.rst`` in the documentation revealed
391 occurrences for spam, 82 for ham, 96 for eggs, 28 for bacon, and
10 for sausage.  The source code for cpython revealed just 2 usages
for
spam and 1 for eggs.

Proposed alternatives
=

Keeping with the good practice of referencing sketches from Monty
Python's
Flying Circus, this PEP proposes to adopt the fruits mentioned in the
`"Self-Defence Against Fresh Fruit" sketch`_:

* raspberry (not currently in use)
* banana  (68 times in standard library)
* apricot (not currently in use)
* pineapple (8 times in standard library)
* peach (once in standard library)
* redcurrant (not currently in use)
* damson (not currently in use)
* prune (23 times in standard library)

Other possible alternatives keeping with food items:

* salad (occurs once in standard library)
* aubergine (referred to in the spam sketch)
* shallot (the same)
* tofu (vegan protein alternative)


Specification
=

For the reasons mentioned in the rationale, all references to meat or
dairy
products shall be removed from the Python standard library, the
documentation,
and the cpython source code.  The wider Python community is
recommended to
follow this practice.  In core Python:

* Programmers SHALL NOT use the metasyntactic variables "spam", "ham",
"bacon",
  or "sausage", neither as variable names, nor in example strings, nor
in
  documentation.
* Programmers SHALL NOT use the metasyntactic variable "eggs" in
context with
  food items, but may still use it in context of other body parts.
Prohibited:
  ``["salad", "eggs"]``.  Allowed: ``["ovaries", "pouch", "eggs"]``.
* Programmers SHALL NOT use any other metasyntactic variable that is
unfriendly
  to animals.

The wider Python community is encouraged to adopt these practices as
well, but
the continued use of animal-unfriendly metasyntactic variables will
not be
considered a violation of the code of conduct.


Rejected ideas
==

The authors carefully considered the widespread use of the word "bug"
in the meaning of a source code error.  Insects including bugs play
a crucial role in ecosystems around the world, and it is not fair to
blame them for an error that can only be the programmer's.  However,
the use of the word "bug" for a source code error is too much
ingrained
into daily use, it far predates the Python community, is not limited
to
the Python community, and the word "bug" is less unfriendly 

[Python-ideas] Re: Issue 34850: Syntax Warning in the real world (is 0 / is 1)

2020-03-31 Thread Nick Timkovich
>
> I understand it locks in that particular implementation detail, but it
> also seems unlikely that that particular optimization (int 0 and int 1 as
> specific memory objects) would ever make sense to NOT be including in a
> shipping implementation (future proof by practicality).
>

Do you explicitly want to differentiate between *CPython integer* 0's and
1's and floats? How about Numpy and other numeric libraries?

assert 0.0 is not 0
assert 1.0 is not 1
assert numpy.ones(1, dtype=int)[0] is not 1
assert numpy.int8(1) is not 1

I think you're going to cause sneaky bugs for yourself.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HLXJHCBM27BWZFWWZRBRBLO2EFYOK7EJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Stephen J. Turnbull
Steven D'Aprano writes:
 > On Mon, Mar 30, 2020 at 10:08:06PM -0700, Guido van Rossum wrote:

 > > StringIO was created in order to fit code designed to a file,
 > > where all you want to do is capture its output and process it
 > > further, in the same process.

 > But it does that by *building a string*, does it not?

Not all two-pass processes on external streams build strings
internally.  At least, earlier you insisted that StringIO is not a
string.

 > That's what the getvalue() method is for.

True, but there's no guarantee a given process will ever invoke it.
For example, I might read a file encoded as ISO-2022 into a StringIO,
then read that StringIO normalizing it to another StringIO as NFD,
then encode it to a file as UTF-8.  Look Ma, no .getvalue!

Steve
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5BBJ3HVBA4F6RASCFRAGAUPGFOVEG3GI/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Issue 34850: Syntax Warning in the real world (is 0 / is 1)

2020-03-31 Thread Chris Angelico
On Tue, Mar 31, 2020 at 11:57 PM Philip Kahn  wrote:
>
> Yikes, this change is spitting out a lot of warnings on first run, and plenty 
> in stuff that's not my code to boot.
>
> A huge number of the cases, though, seem to be "is 0" and "is 1" comparisons. 
> Having read through the original issue I get where the SyntaxWarning is 
> coming from, but it seems it'd be a genuine end-user experience improvement 
> to not raise the warning on those two classic "truthy" comparisons.
>
> I understand it locks in that particular implementation detail, but it also 
> seems unlikely that that particular optimization (int 0 and int 1 as specific 
> memory objects) would ever make sense to NOT be including in a shipping 
> implementation (future proof by practicality).
>
> Mostly wanted to get that out there, but for now I'm rolling back my python 
> version rather than seeing a deluge of useless warnings. Right now, this is 
> just a great way to drown out real warnings in noisy unhelpful ones.
>

Your code is buggy. The warnings are telling you that your code is
buggy, even though *at the moment* it happens to work. And the fix is
simple - replace every "is 0" with "== 0", and your code will be
correct.

ChrisA
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/XJ4HBKNUFS7BQTEBEC5AH2HREQWY7VN2/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Issue 34850: Syntax Warning in the real world (is 0 / is 1)

2020-03-31 Thread Philip Kahn
Yikes, this change is spitting out a lot of warnings on first run, and
plenty in stuff that's not my code to boot.

A huge number of the cases, though, seem to be "is 0" and "is 1"
comparisons. Having read through the original issue I get where the
SyntaxWarning is coming from, but it seems it'd be a genuine end-user
experience improvement to not raise the warning on those two classic
"truthy" comparisons.

I understand it locks in that particular implementation detail, but it also
seems unlikely that that particular optimization (int 0 and int 1 as
specific memory objects) would ever make sense to NOT be including in a
shipping implementation (future proof by practicality).

Mostly wanted to get that out there, but for now I'm rolling back my python
version rather than seeing a deluge of useless warnings. Right now, this is
just a great way to drown out real warnings in noisy unhelpful ones.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/JVHOMRPCXFGR4L64T7M6CFJULPP3IMTI/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Compound with .. else statement

2020-03-31 Thread Jimmy Thrasibule
> But if you use a context manager which silences the exception, like
> contextlib.suppress() or unittest.TestCase.assertRaises(), it is easy to
> do too.
>
>  was_not_raised = False
>  with my_context():
>do_something_sensitive()
>was_not_raised = True
>  if was_not_raised:
>  print("We're all safe.")

That is indeed a way to workaround my use case. I do still find a
with/else more elegant.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/TO7ZCRVTPKSUL5EJ76F2IOMGUBTYLA33/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Compound with .. else statement

2020-03-31 Thread Serhiy Storchaka

31.03.20 01:32, Christopher Barker пише:
In case Serhiy's answer wasn't clear: context managers can be written to 
handle exceptions (within their context) in any way you see fit.


that is: the method:
|
|
|__exit__(||self||, exc_type, exc_value, exc_traceback):|

get the exception, and information about it, of one is raised, so you 
can handle it anyway you want.


Actually I meant the opposite. Sorry for being unclear. In normal case 
the context manager does not silence a raised exception, so control flow 
is never passed to the statement past the with block if an exception is 
raised inside the with block.


But if you use a context manager which silences the exception, like 
contextlib.suppress() or unittest.TestCase.assertRaises(), it is easy to 
do too.


was_not_raised = False
with my_context():
  do_something_sensitive()
  was_not_raised = True
if was_not_raised:
print("We're all safe.")

You do not need a special syntax for this. was_not_raised will never be 
set to True if do_something_sensitive() raises an exception.

___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/ERCV4NY5MHHXVZMSFZDISRG3OKIFRLOC/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Stephen J. Turnbull
Steven D'Aprano writes:

 > [I]t ought to be clear from Paul's well-written and detailed post,
 > which carefully explains what he wants.

Whose value to Python I still don't understand, because AFAICS it's
something that on the one hand violates TOOWTDI and has no parallels
elsewhere in the io module, and on the other hand is trivial to
implement for any programmer who really thirsts for StringIO.__iadd__.
Unless there are reasons why a derived class won't do?

I agree there seem to be possible space performance issues with
str.join that are especially painful for embedded applications (as
came out later in the thread I believe), but if those are solved by
StringIO, they're solved by StringIO.  So the whole thing seems to be
a cosmetic need for niche applications[1] for a niche platform[2] that
is addressed by a 4-line class definition[3] for users who want the
syntactic sugar.  Me, I'm perfectly happy with StringIO.write because
that's what I expect from the io module.  FWIW YMMV of course.

Footnotes: 
[1]  I don't even use strings at all in any of my adafruit applications!

[2]  OK, that's going too far, sorry.  Embedded matters, their needs
are real needs, and they face tight constraints most of us rarely need
to worry about.  It's still at present a minority platform, I believe,
and the rest of the sentence applies AFAIK.

[3]  Paul's "exact alias of .write() method", which can be done in 1
line, fails because .write() doesn't return self.  Thanks, Serhiy.  In
the stdlib we might even want a check for "at end of buffer" (.write()
can overwrite Unicode scalars anywhere in the buffer).  That's
definitely overengineering for a user, but in the stdlib, dunno.
___
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/FBYJRWHOAUP4GQ73ADHBDW4MNDCTK5PM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Explicitly defining a string buffer object (aka StringIO += operator)

2020-03-31 Thread Andrew Barnert via Python-ideas
On Mar 30, 2020, at 22:03, Steven D'Aprano  wrote:
> 
> On Mon, Mar 30, 2020 at 01:59:42PM -0700, Andrew Barnert via Python-ideas 
> wrote:
> 
> [...]
>> When you call getvalue() it then builds a Py_UCS4*
>> representation that’s in this case 4x the size of the final string 
>> (since your string is pure ASCII and will be stored in UCS1, not 
>> UCS4). And then there’s the final string.
>> 
>> So, if this memory issue makes join unacceptable, it makes your 
>> optimization even more unacceptable.
> 
> You seem to be talking about a transient spike in memory usage, as the 
> UCS4 string is built then disposed of. Paul seems to be talking about 
> holding on to large numbers of substrings for long periods of time, 
> possibly minutes or hours or even days in the case of a long running 
> process.

But StringIO has the same long-term cost of the list, _plus_ a transient spike. 
There’s no way that can be better than just the same long-term cost. You can 
try to argue that it’s not that much worse, or that it isn’t worse in some 
cases, or that it could be optimized to not be as much worse; I’ll snip our all 
of those arguments because even if you’re right, it’s still not better. So this 
proposal amounts to changing Python, so that we can then get everyone to stop 
using the idiom they’ve been using for decades and use a different one, just to 
get maybe at best the same performance they already have. Why does that sound 
reasonable to you?

> Whether StringIO takes advantage of that opportunity *right now* or not 
> is, in a sense, irrelevent. It's an opportunity that lists don't have. 
> Any (potential) inefficiency in StringIO could be improved, but it's 
> baked into the design of lists that it *must* keep each string as a 
> separate object.

The reason StringIO keeps a list (well, a C struct that’s almost the same thing 
as a list) is because it’s fast. It’s not the simplest implementation, it’s 
something that people put a lot of work into optimizing. 

Is it possible that someone could come up with something that’s even better for 
the main uses of StringiO (simulating a file) , and that also happens to be 
good for use as a string builder? Sure, I suppose it’s possible. But do you 
really think we should mame a change just so we can encourage people to switch 
to using something that’s slower and takes more memory (and doesn’t work in 
older versions of Python) just because it’s not impossible that one day someone 
will come up with a new optimization that makes it better instead of worse?

> And if some specific implementation happens to have a particularly 
> inefficient StringIO, that's a matter of quality of implementation and 
> something for the users of that specific interpreter to take up with its 
> maintainers. It's not a reason for use to reject Paul's proposal.

But if every implementation of StrjngIO, in every interpreter, is actually 
worse than joining lists, isn’t that a reason for us to reject the proposal?

>> And thinking about portable code makes it even worse. Your code might 
>> be run under CPython and take even more memory, or it might be run 
>> under a different Python implementation where StringIO is not 
>> accelerated (where it’s just a TextIOWrapper around a BytesIO) and 
>> therefore be a whole lot slower instead.
> 
> So wait, let me see if I understand your argument:
> 
> 1. CPython's string concatentation is absolutely fine, even though it is 
> demonstrably slower on 11 out of the 12 interpreters that Paul tested.

No. This is no part of my argument. The recommended way to handle building 
large strings out of lots of little strings is, and always has been, to join a 
list. It’s in the FAQ. It’s even baked into the code of CPython (see the error 
message from calling sum on strings). People should not be concatenating 
strings, but we don’t need to offer them a better solution because they already 
have a better solution.

> 2. The mere possibility of even a single hypothetical Python interpreter 
> that has a slow and unoptimized StringIO buffer is enough to count 
> against Paul's proposal.

No, the fact of every real life Python interpreter having a StringIO that’s at 
least a little worse than string join, and in some cases a lot worse, is enough 
to rule out the proposal. (The facts that StringIO also has the wrong semantics 
 is less obvious for the purpose, and isn’t a decades-long established idiom 
are additional problems with the proposal. And the biggest problem is that the 
proposal is trying to fix a problem that doesn’t exist in the first place.)

> Is that correct, or have I missed some nuance to your defence of string 
> concatenation and rejection of Paul's proposal?

You haven’t missed any nuance, you’ve missed the entire point. I am not 
defending string concatenation, I’m defending the established idiom of join. I 
am not arguing to reject Paul’s proposal because it might theoretically be 
inefficient on some implementation, but because it