[Python-ideas] Re: Extract variable name from itself

2023-09-16 Thread Jeff Allen

On 16/09/2023 11:33, Dom Grigonis wrote:


It works already with existing tools that will treat "nameof(a+b)" as 
a function call, which it is syntactically but not semantically, so 
renaming b to c will now produce the string "a+c". I think my IDE 
does offer to do this with variable names in strings and comments, 
but it produces so many false hits I don't enjoy using the feature.


So the functionality is bound to an editor and anyone using another 
editor would just see a weird string?

After several similar editor features things could get very confusing.

No, the opposite. By "works now", I meant to say that if a nameof() 
pseudo-function were added to the Python language, existing tools would 
not have to change, because they would treat it as a call. This does 
scratch an itch (in renaming) by removing the need to guess or ask the 
human whether a given string is a variable name.


I'm not convinced this feature is widely useful.


So what is the general procedure for determining it?


1. Compelling use cases here or (better) on the Dicourse counterpart
   <https://discuss.python.org/c/ideas/6>.
2. Community and core dev acclamation as a Good Thing.
3. Willigness to implement and maintain.

--

Jeff Allen
___
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/LUDUIZB7VEYPUQWIT2YGYV3ECKMZ2V4J/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Extract variable name from itself

2023-09-16 Thread Jeff Allen

On 16/09/2023 01:14, Chris Angelico wrote:

But if that tool can be taught that nameof indicates a variable name,
then it can rename inside it just like it would rename anything else.
It works already with existing tools that will treat "nameof(a+b)" as a 
function call, which it is syntactically but not semantically, so 
renaming b to c will now produce the string "a+c". I think my IDE does 
offer to do this with variable names in strings and comments, but it 
produces so many false hits I don't enjoy using the feature.

Your problem here is a limitation of the variable renaming tool. I'm
suggesting a solution to that problem. This is not a language problem,
it's an editor problem, so it needs an editor solution.
It needs language (parser) support because it does what you identified 
early on: "take whatever is given and put it in quotes", where "whatever 
is given" is the text of the expression only available at run-time. 
(Actually, I can think of something complicated one might possibly do 
with the text produced in exceptions, but please don't.)


Taking an example from my REPL just now (it happens that r2 is the AST 
of an Expression), you could simulate nameof with f-strings:


>>> f"{r2.body.op.test=}"
'r2.body.op.test=42'
>>> f"{r2.body.op.test=}".split('=')[0]
'r2.body.op.test'

This also evaluates the expression, encodes the result as text then 
discards it, which is undesirable. The point is that renaming r2, or the 
fields of a BinOp (if it were project-local, not a stdlib object) would 
cause the string to change in most existing IDEs.


Attempts to guess the name from the expression at runtime are futile and 
a red herring. Trying to describe the desired semantics that way leads 
to nonsensical ideas, I agree.


I'm not convinced this feature is widely useful. Here I'm just trying to 
focus us on the *viable* semantics MRAB identified.


--

Jeff Allen
___
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/RQISFW5UV2EW6ZB3ZKUDNV2R45MWIH3O/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Extract variable name from itself

2023-09-15 Thread Jeff Allen

On 13/09/2023 17:21, MRAB wrote:

I think the point is to have an equivalent to C#'s 'nameof'.

It would be evaluated at compile time to a string, but with the 
advantage that it's clear that it's a name and not some random string 
that just happens to look like a name.


I'd like to draw attention to this as the first contribution to the 
thread that has given a precise meaning to the initial idea. It tells us 
both what it means and why you might want it (at least if the OP 
agrees). It seems to be being overlooked.


As others have amply demonstrated (short of proof, I know) what is being 
asked is not possible with the information available at run-time. You 
can't reliable get from the "reference", which is actually an 
expression, to the text of the expression.


The parallel with f-string = is helpful, for which compile-time support 
is essential, of course.


--

Jeff Allen
___
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/45P4HD5LBNDCPUGRRWWDV6XGR6H3GCDB/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Have del return a value

2023-09-09 Thread Jeff Allen

On 06/09/2023 03:47, Daniel Walker wrote:
Perhaps this is a solution in search of a problem but I recently 
encountered this situation in one of my projects.


I have an object, foo, which, due to the references it contains, I'd 
rather not keep around longer than necessary.


I use foo to instantiate another object:

    bar = Bar(foo)

bar is free to manipulate foo however it wants and even del it if 
necessary.  However, since the foo name is still around, those 
resources won't get cleaned up even if bar is done with them.  The 
simple solution is



It's a neat idea but there are other ways that may be better.

I assume foo is a "specification", that is, an object you configure 
through a number of optional calls or attribute assignments, then pass 
to Bar() as a design alternative to Bar() having a complicated set of 
arguments. A pattern often used is the fluent API:


bar = Bar(Foo().a().b(42).c('penguin'))

Now only Bar has a reference to the Foo you made.

But foo is not necessarily destroyed immediately the last reference to 
it is dropped, even with del or when nameless in the fluent API. If you 
wanted to be certain, you could make Foo a context manager to use like this:


with Foo() as foo:
    foo.a()
    foo.b(42)
    foo.c('penguin')
    bar = Bar(foo)
bar.do_stuff()

(Think I got that right. Not tried it.) Now Foo.__exit__ can be made to 
properly tear down the resources in a foo.


--

Jeff Allen
___
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/75EXWHY3SFZSZTZUQJPIBMXFRF3J327I/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: `importlib.resources` access whole directories as resources

2022-05-18 Thread Jeff Allen

On 16/05/2022 23:22, Filipe Laíns wrote:

No, Python source files are resources too. "resource" is an abstract concept
akin to files, its propose is to allow support other use-cases than just files
on the OS file system (eg. zip file, tarball, database).

Adding a "directory" reference goes against the purpose of abstracting the FS
away. Packages are akin to directories, files are akin to resources, when
operating on a FS.


This is a very important point that Christopher Barker has also made several 
times and isn't being heard. A resource, including Python source or byte code, 
could be stored in any manner and found by any kind of loader along the 
sys.meta_path. This abstraction is emphasised repeatedly in the documentation 
for the import system. (The point Chris made was more easily isolated in 
Filipe's message so I quote that.)

It is very difficult to shake the idea that packages and modules are exactly 
filesystem constructs, and likewise that resources are only ever the content of 
files that sit alongside in the same directories. It is difficult to shake 
because it almost always how the abstraction is realised in our own work. Also, 
API exists that seems not to be thought out for any other case, which worries 
me. But it's not true: can we please try here?

The importlib abstraction has semantics that are very like a file system, but 
differ in critical ways (see namespace packages or how built-ins are found).

The appeal for "a directory" runs contrary to this abstraction (in its language). It 
probably maps to a valid idea in the abstraction, and Chris has tried to get us to think in terms 
of a "collection" of resources not necessarily always represented by files. I don't 
understand this well, but I note 
thathttps://docs.python.org/3/library/importlib.html#module-importlib.resources  talks about 
resource containers. (Unfortunately, it also only uses filesystem-like examples.) Might that be 
what is asked for?

--

Jeff Allen
___
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/4QLOLI3OR6A7E4WWCAUHRUNKWEV2ZVC3/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: NAN handling in statistics functions

2021-08-27 Thread Jeff Allen

On 26/08/2021 19:41, Brendan Barnwell wrote:

On 2021-08-23 20:53, Steven D'Aprano wrote:

So I propose that statistics functions gain a keyword only parameter to
specify the desired behaviour when a NAN is found:

- raise an exception
- return NAN
- ignore it (filter out NANs)

which seem to be the three most common preference. (It seems to be
split roughly equally between the three.)

Thoughts? Objections?


I'd like to suggest that there isn't a single answer that is most 
natural for all functions. There may be as few as two.


Guido's proposal was that mean return nan because the naive arithmetic 
formula would return nan. The awkward first example was median(), which 
is based on order (comparison). Now Brendan has pointed out:


One important thing we should think about is whether to add 
similar handling to `max` and `min`.  These are builtin functions, not 
in the statistics module, but they have similarly confusing behavior 
with NAN: compare `max(1, 2, float('nan'))` with `max(float('nan'), 1, 
2)`.


The real behaviour of max() is to return the first argument that is not 
exceeded by any that follow, so:


>>> max(nan, nan2, 1, 2) is nan
True
>>> max(nan2, nan, 1, 2) is nan2
True

As a definition, that is not as easy to understand as "return the 
largest argument". The behaviour is because in Python, x>nan is False. 
This choice, which is often sensible, makes the set of float values less 
than totally ordered. It seems to me to be an error in principle to 
apply a function whose simple definition assumes a total ordering, to a 
set that cannot be ordered. So most natural to me would be to raise an 
error for this class of function.


Meanwhile, functions that have a purely arithmetic definition most 
naturally return nan. Are there any other classes of function than 
comparison or arithmetic? Counting, perhaps or is that comparison again?


Proposals for a general solution, especially if based on a replacement 
value, are more a question of how you would like to pre-filter your set. 
An API could offer some filters, or it may be clearer left to the 
caller. It is no doubt too late to alter the default behaviour of 
familiar functions, but there could be a "strict" mode.


--

Jeff Allen

___
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/FQNZLNISKHV74CYJMU2HPG5273VMWXUK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Allow modifying message of TypeError from NotImplemented

2021-06-19 Thread Jeff Allen

On 19/06/2021 05:38, Steven D'Aprano wrote:

On Sat, Jun 19, 2021 at 03:30:05AM -, wyz2...@163.com wrote:


Python raises TypeError when NotImplemented is returned from __add__,
__invert__ etc. and __radd__ etc. aren't available.

Roughly correct. It's more complex than that.

I just wanted to add that NotImplemented is *only* for binary (and 
binary comparison) operators.


https://docs.python.org/3/library/constants.html?highlight=notimplemented#NotImplemented

Ok, also when __length_hint__ can't help. Generally though, other 
contexts from which the special method might be called do not check for it.


--

Jeff Allen

___
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/6ERUPVCUFKYKHMT5CUNVRZ4ZRIBMNJSZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Pre PEP: Python Literals (was custom strings before)

2021-06-12 Thread Jeff Allen

On 10/06/2021 15:46, Ricky Teachey wrote:


Makes me wonder if stepping back and thinking, instead, about a way to 
automatically (optionally?) put the current calling context 
dictionary(ies) somewhere that a called function can get at it/them, 
so it can do what it wants with the context.


I think that would violate the expectation that a function call means 
the same thing in different contexts, hence f-strings work by supplying 
values at the "call site" not variables. I know you will find exceptions 
(like import context), but they are not as all-consuming as snapping the 
entire namespace. If you really want to, there's sys._getframe.


Jeff

___
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/DUEB3HLOW3LI4RXTLZN7JQFFX44FBWSJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Changing The Theme of Python Docs Site

2021-05-03 Thread Jeff Allen

On 30/04/2021 14:29, Rob Cliffe via Python-ideas wrote:


On 30/04/2021 07:06, Abdur-Rahmaan Janhangeer wrote:


The Masonite docs for example is quite
nice:
https://docs.masoniteproject.com/ <https://docs.masoniteproject.com/>


I read as far as the 4th sentence:
    "Use it for your next SaaS!" [no link atached] What on earth (I 
wanted to use a stronger expression) is an SaaS? I'd never heard of 
it. OK, Google told we what it stood for, but I don't feel any the 
wiser. Rob Cliffe 


It means "mainframe". If you get an account on their computer, you're 
allowed to log in and pay to use the software. It's very modern: at 
last, the value of free, open-source software can accrue to rich 
industrialists, for whom we should all be working in our spare time.


I'm with you that the writing in the Masonite docs is not an example to 
follow. I only got as far as "actual batteries included" before doubts 
set in. Then the writer loses control of the sentence structure, 
obscuring the thought he began. However, visual style not literary 
content is the question.


The visual style is quite like the existing Python docs prepared in ReST 
(e.g. https://docs.python.org/3/reference/expressions.html). The Sphinx 
"Alabaster" theme is even cleaner, but intended in part, I think, as a 
blank canvas for your branding. I prefer the way the sidebar scrolls 
independently in the Masonite site. I found no examples of images, 
diagrams or tables to compare. I would not say there was much wrong with 
the Python visual style, but then I'm used to spending hours in the 
(downloaded) documentation.


Is it really just GitBook that we are slightly admiring? You would not, 
I think, want to re-write the ReST documents in GitBook, just to get a 
cleaner look. Time from someone clever with CSS in Sphinx may be all it 
takes.


If you follow the documentation link on the home page to 
https://www.python.org/doc/, much of that links to the wiki, which in my 
opinion does look a bit cobwebby. Style is only part of problem: the 
size and proportions of text look pokey to me. Here the content does not 
read very well, being crowd-sourced I imagine without much editorial 
control. (E.g. https://wiki.python.org/moin/BeginnersGuide is unsure of 
its level: veering from "if you've never programmed before" through to 
inviting patches.) Were we including the Wiki in this?


Jeff Allen

___
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/U6QJC5RDXQB6T2CIUXPY6S2SNS5LTPVK/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Change the exception type and message raised when _curses is not found

2021-04-05 Thread Jeff Allen

On 04/04/2021 19:05, Shreyan Avigyan wrote:

When importing the curses module, be it on Windows or Darwin or UNIX-based OS 
or any other platform, if the _curses module is not found then just a 
ModuleNotFoundError is raised. But this error is not very informational in case 
of _curses module. Since the curses module is packaged with the Python 
interpreter itself at first it may seem, to beginners especially, that the 
Python interpreter was not installed correctly and then they would go searching 
for an answer for about 4-5 days.

We know that curses library is not installed on windows by default and may or 
may not be present on other operating systems. Most UNIX system have ncurses or 
other curses library installed by default.

Python errors have a reputation of being very informational. I would like to submit a PR 
to modify the curses module a little bit by declaring a BaseException class and raising 
that Exception with the message "_curses module not found. Make sure a curses 
library is installed" or some kind of message like that.

But before I do that I would like to take advice from experienced developers 
about somethings. Is this change in the exception, raised when _curses module 
is not found, acceptable by the Python Community? If it is then should a draft 
PEP be submitted or should a PR be directly submitted to 
https://github.com/python/cpython?


I don't think this requires a new exception type. The point about 
BaseException being a worse choice than ModuleNotFoundError has already 
been made.


The stdlib curses module depends on _curses that may not be present, 
which is a flaw in my view, and obviously it has taken some research to 
find the reason. Your complaint is either against the documentation of 
the curses module 
(https://docs.python.org/3/library/curses.html#module-curses), for not 
telling you sooner, or against the text of the curses module itself.


The how-to (https://docs.python.org/3/howto/curses.html#curses-howto) 
does mention the problem and provides pointers to fix it, but 
incorrectly states that it is the curses module itself that will not be 
found, not the inner _curses. So that may sow additional confusion. I 
expect the reasons for this inaccuracy are in the change history.


In curses, the author could have wrapped the specific import in 
try-except to raise a more informative ModuleNotFoundError:


try:
    from _curses import *
except ModuleNotFoundError:
    raise ModuleNotFoundError("_curses! If only this were not a pesky Windows 
machine!")

Or something better you choose.

Either the documentation fixes or the code change would make a sensible 
PR, I think. Or why not addess all three? (It's surely not a PEP.)



Jeff Allen

___
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/VST2TULO3ACN47LTQKRNSE2PJE4ECYRB/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Standard tool for iterating over recursive data structures?

2021-01-01 Thread Jeff Allen

On 01/01/2021 19:36, Richard Damon wrote:

On 1/1/21 2:00 PM, Jonathan Fine wrote:

Hi Richard

You wrote

 I believe that one solution to detecting the cycles is to create a set
 of the object IDs you have visited and started to print. If you come
 across an ID you have already seen, this point is in a cycle. Sets are
 fairly compact and intentionally fast to search for an item.


Indeed. But I see a flaw. The problem is that we want to know about
EQUALITY of nodes, not equality of the ID (memory address in disguise)
at which the node is stored.


As to the first, just because to points have equal values doesn't mean
that we have recursion.

That is why I put the id()s in the set, the id()s are by definition
hashable, and an object always has the same id() and no two different
objects can have the same id().

Simple example:

list1 = [ ['one', 'two'], ['one', 'two'] ]

This makes me realise that the spanning tree I brought into the 
discussion is very likely a red herring. The print (or more generally 
iteration/visitation) of the structure needs to be curtailed not at an 
object you already visited, but only at an object that is between you 
and the root. (In the case of printing or serialisation, that is an 
object you are part-way through processing.)


Here, you definitely want to visit a three times when printing b:

>>> a = [1, 2, 3]
>>> b = [a, a, a]
>>> b
   [[1, 2, 3], [1, 2, 3], [1, 2, 3]]

But after:

>>> c = [4, a, b]
>>> a.append(c)

revisiting a and b is to be avoided during the visit to c

>>> b
   [[1, 2, 3, [4, [...], [...]]], [1, 2, 3, [4, [...], [...]]], [1, 2,
   3, [4, [...], [...

However, I'm definitely with Richard that it is the identity of the node 
that matters, not its value (hashable or not). Of course, I'm assuming I 
know what kind of iteration Steven wanted, based on the reference to 
printing (repr).


The question remains whether this can be done as a general capability, 
and I still see the problem as the inscrutability of built-in types (and 
their repr, etc.).


Jeff

___
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/42YGZUPUGDMVQ5V7OXSDGGROENQ6P3PC/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] (Off-topic) Different object, same id

2021-01-01 Thread Jeff Allen

On 01/01/2021 19:00, Jonathan Fine wrote:


By the way, this surprised me. Would anyone like to explain this?
    >>> id(f()), id(f())
    (139927013695536, 139927013695536)

That made me think. The result of the first f() has gone out of scope by 
the time the second call, and so the id (that is, the memory) is 
available next time CPython needs a place for an int of about the same 
bit_length.


>>> def f(): return 10**1000

>>> def g(): return 9**1048

>>> id(f()), id(g())
(2149044150320, 2149044150320)

Neat demonstration that the id of an object is only unique in its lifetime.

Jeff

___
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/PW3SYSZMAFSJCB6VBEZXPWJF5BNZSYVM/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Standard tool for iterating over recursive data structures?

2021-01-01 Thread Jeff Allen

On 01/01/2021 14:50, Jonathan Fine wrote:

...
To summarise, it's loops / cycles in the representation of the data 
that's the problem. ... .


Final idea. Perhaps a tool to detect cycles in data might be a good idea.

Detecting cycles will involve a lot of bikeshedding. (Sorry, couldn't 
resist.)


But you're right, that's a better word for discussing the problem. 
Steven's problem data structures are cyclic graphs. I don't agree that 
such structures are a sign one is doing something wrong (outside the 
naive approach to printing them out).


If memory serves me right, what we're talking about is finding and 
walking a "spanning tree" of the graph. There is a twist in our case 
that we would like to insert a special object where a link was elided to 
break the cycle: an object that maybe contains the original reference 
but would reproduce as "..." in print.


Since it appears necessary to create a data structure with as many nodes 
as there are in the tree, just to remember where we've been, we may as 
well say that the required result takes the form of an iterable of the 
nodes, which may subsequently be iterated by a consumer. Actually, I can 
think of two styles of iterator: one where "..." objects are skipped by 
a "processing" iterator, and one where they are delivered by a "print" 
iterator. It is valid to do this with objects that can't be hashed, so 
the structure is perhaps a dict keyed by id.


Would this be possible as a general capability? I think only if the 
references were in the __dict__ of each instance. With a built-in, one 
is at the mercy of the implementor. But we must have to do something 
very similar to pickle arbitrary (potentially cyclic) structures, which 
is likewise dependent on special help from the particular built-in type. 
Can something be founded on |||__getstate__| ?


Happy New Year

Jeff Allen


___
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/HOUAPRNH7CXYGH7PBAQBRB5JSA7MIGTQ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Standalone bool?

2020-12-22 Thread Jeff Allen

On 22/12/2020 00:52, Christopher Barker wrote:
On Mon, Dec 21, 2020 at 3:37 PM Greg Ewing 
mailto:greg.ew...@canterbury.ac.nz>> wrote:


> However, that ship has sailed. I think it would have been minimally
> disruptive when True and False were first introduced,

It would have been just as disruptive back then -- that's the
reason bool was made a subclass of int in the first place.


I know why, but I'm not so sure -- no one was using a built in True or 
False as an integer, because they didn't exist. I suppose folks were 
using the results of, e.g.  `a == b` as an integer, but how often? 
Where else is an explicit True or False returned by Python itself?


The `bool` function, of course, `in`, `is` and `isinstance`, 
`str.isdigit`, ..., |`threading.||Lock.acquire|`. Quite a few if you 
search for "return PyBool_".


Except for counting (with `sum`), I don't think you would use any of 
those arithmetically. But there's this kind of thing:


>>> [f"{n+1} {g}{(n>0)*'s'}" for n, g in enumerate(gifts)][::-1]
['4 calling birds', '3 french hens', '2 turtle doves', '1 partridge in a 
pear tree']


Go on, you know you want to.

Jeff

___
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/Y64AGTVPTKD5VKWUZCKUZU6TOUREAQIV/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Using explicit parenthesization to convey aspects of semantic meaning?

2020-12-15 Thread Jeff Allen

On 13/12/2020 22:09, Paul Sokolovsky wrote:

Thanks for hanging with me so far, we're getting to the crux of the
question:

Do you think there can be difference between the following two
expressions:

obj.meth()
(obj.meth)()

?


No. The value of an expression in parentheses is the value of the 
expression inside the parentheses, and in this case does not affect the 
order of evaluation.



python3.6 -m dis meth_call.py
python3.7 -m dis meth_call.py

Then, to try to explain the difference at the suitable level of
abstraction. If that doesn't provide enough differentiation, it might
be helpful to add the 3rd line:

t = obj.meth; t()

And run all 3 lines thru CPython3.7, and see if the pattern is now
visible, and a distortion in the pattern too.

What would be the explanation for all that?


The explanation is an optimisation introduced in 3.7 that the use of an 
intermediate variable prevents. The compiler applies it when it can see 
the only use of the attribute is an immediately following call. Having 
burrowed into the implementation, I'm certain it tries hard to be 
indistinguishable from the unoptimised implementation (and succeeds I 
think), even to the point of failing in the same way when that is the 
programmed outcome.


LOAD_METHOD goes far enough down the execution of LOAD_ATTR to be sure 
the bound object would be a types.MethodType containing a pair of 
pointers that CALL_FUNCTION would have to unpack, and pushes the 
pointers on the stack instead of creating the new object. Otherwise it 
completes LOAD_ATTR and pushes a bound object and a NULL, which is what 
CALL_METHOD uses to decide which case it is dealing with.


The meaning of the code is what it does detectably in Python, not what 
it compiles to (for some definition of "detectably" that doesn't include 
disassembly).


Jeff Allen

___
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/GUMGCGGBUR73DKEWOOOYLMLO7QBQVLQZ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: A new suggestion for Python

2020-10-02 Thread Jeff Allen

On 30/09/2020 19:02, Steven D'Aprano wrote:


But more importantly, unless the right hand side is severely limited, it
is going to be very hard to be unambiguous. The standard augmented
assignments take *any expression at all* for the right hand side:

 value += 2*x - y

but this could not:

 value .= 2*x - y


I think this is closest on the thread to explaining why this doesn't 
work, or at least is a semantic nightmare. I've been steeped in 
descriptors these last several weeks, so I can put it another way: "." 
is not really an operator, it is attribute access.


Or if one is forced to think of it as an operator, its right operand may 
only be the name that follows. In translating the "operator", the 
compiler quotes the right argument as a litereal, rather than evaluating 
it. Thus:


>>> 'hello'.replace('ell', 'ipp')

means the same as:

>>> ('hello'.replace)('ell', 'ipp')

and the same as:

>>> getattr('hello', 'replace')('ell', 'ipp')

and not:

>>> 'hello'.(replace('ell', 'ipp'))

which doesn't mean anything at all, because "replace('ell', 'ipp')" 
cannot be the right hand side of ".".


Jeff Allen

___
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/HGDFGUCHCKFIB6YTM7MX5KJF2JQVVIR6/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: argmax and argmin to python list

2020-08-31 Thread Jeff Allen

On 31/08/2020 01:14, Christopher Barker wrote:
On Sun, Aug 30, 2020 at 7:28 AM Barry > wrote:



How is it supposed to work with set or dict or other iterables
without clear order?


see the discussion in another recent thread about making dict 
indexable -- which looks like it's not going to happen.

Unfortunately, there are a lot of those.
so no -- this should not work with general iterables, indexes don't 
really make sense for iterables, only Sequences.


It would be as useful as:

>>> min(dict(b=1, c=3, a=2))
'a'

I half expected 1 (and from argmin would expect 'b') but I see why the 
actual result is "consistent".


Jeff

___
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/GE5JFKDFNHVGZSMDGLWVI6GJRAFTS44G/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: argmax and argmin to python list

2020-08-30 Thread Jeff Allen

On 30/08/2020 09:51, Barry Scott wrote:




>>> a.index(max(a))
1

Barry

This has the drawback of passing twice over the list. The following 
doesn't, but the complexity somewhat makes Filipp's point:


>>> min((e, i) for i, e in enumerate(a))[1]
0


That is 4x slower then my code for 1,000,000 items.

Ha! Fair enough. In principle, however, passing over the list once only 
is preferable. The trade-off would depend on the cost of doing 
comparisons: cheap here while iteration is clearly costing a lot. In the 
alternative, the second pass for index() is probably only doing pointer 
comparison.


It will often be the case and is perhaps why we don't have this 
function. That and its existence in various libraries e.g. 
https://iteration-utilities.readthedocs.io/en/latest/generated/argmin.html#iteration_utilities.argmin 
.


Passing over the list once is what we would do in a C implementation, 
I'm sure. One solution is to add a counter in bltinmodule.c::min_max()).


Jeff


___
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/PKT4ZAFNNPPKTLHZ3XQLJZYM4FWIJC5P/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: argmax and argmin to python list

2020-08-30 Thread Jeff Allen

On 29/08/2020 14:17, Barry Scott wrote:
On 29 Aug 2020, at 13:42, Filipp Bakanov <mailto:fil...@bakanov.su>> wrote:


I'd like to propose adding argmax and argmin functions to the python 
list. These functions return the index of a maximum / minimum element 
of the list. Eg:


a = [1, 4, 2, 3]
print(a.argmax())  # 1
print(a.argmin())  # 0

It's a very popular request (based on stackoverflow 
https://stackoverflow.com/questions/16945518/finding-the-index-of-the-value-which-is-the-min-or-max-in-python 
), and currently there is no elegant way to find it.


What do you think?


Just do this:

>>> a=[1,4,2,3]
>>> min(a)
1
>>> a.index(min(a))
0
>>> a.index(max(a))
1

Barry

This has the drawback of passing twice over the list. The following 
doesn't, but the complexity somewhat makes Filipp's point:


>>> min((e, i) for i, e in enumerate(a))[1]
0

I think one would want argmin() and argmax() to work with general 
iterables, so I wonder if the stdlib would not be a better home than 
list itself. I half expected it to be an itertools recipe. The advantage 
of a recipe is that variations such as needing the last occurrence of 
the minimum are easily accommodated.


Jeff Allen

___
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/BIT3CJZTC3NM4U2ALCS3JALH5KUPYVBJ/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Add builtin function for min(max())

2020-07-05 Thread Jeff Allen


On 05/07/2020 05:39, Steven D'Aprano wrote:

On Sun, Jul 05, 2020 at 12:18:54PM +0900, Stephen J. Turnbull wrote:


  > and I'd like to toss a possible `coerce`

Here my issue is that for me the *target* of a coercion should be a
"single thing", which could be a type, but might also be a scalar. ...

No, I agree. In computing, coerce nearly always means to coerce to a
type, not to coerce to some range of values.


"bound", or probably "bounded" (for the same reason we have "sorted").

"clamp" and "clip" sound to me like things you do to a waveform (with 
Schottky diodes!), so it works for me but I'm not sure it travels well. 
Elsewhere in the thread (tree) we're already calling arg[1:2] "bounds", 
so reading this as "the value of x bound[ed] by the range -1 to +5" 
seems natural.


Or "limited" possibly?

I'm +0 on the idea FWIW. I also find it difficult to read, but I tend to 
in-line it as ifs, in part for clarity.


Jeff Allen

___
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/T7HC7CNOZAHZHLKB36OR3772MAICWIUG/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Amend PEP-8 to require clear, understandable comments instead of Strunk & White Standard English comments

2020-06-28 Thread Jeff Allen
l imperialism of 
asking people to write this educated English. The argument leads to 
"everyone should be free to write in their own variant of English, with 
no consideration for readers". It just wasn't pursued this far. Still, 
the outcome is less good for inclusivity than it might have been, 
because the means to achieve parity are now (in a trivial way) made less 
accessible.



Jeff Allen

[1] Strictly read, only "Python coders from non-English speaking 
countries" are required to write comments in English. So the rest of us 
... exprimons-nous librement!



___
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/5M56KKP2S3POELVGTHGXGWANEVK72R3E/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: addition of "nameof" operator

2020-02-02 Thread Jeff Allen

On 01/02/2020 02:48, Richard Damon wrote:

On 1/31/20 9:28 PM, Christopher Barker wrote:

I am really confused by this whole thread:


My understanding is that the impetus of the request is that if you 
start from an expression like nameof(foo.bar) to get to "bar" then if 
you refactor your code you have a chance that the tool will be able to 
see the variable expression foo.bar, and process that like other 
references to the item, and thus be able to change it. Just using the 
string "bar" would be very hard to see the connection between foo.bar 
and "bar".


Maybe I haven't been paying attention but, if accurate, this is the most 
helpful explanation in the thread.


Some sort of conventional mark-up for variable names seems more 
appropriate than a (pseudo-)function. Possibly something the compiler is 
made to ignore in strings, but that the IDE treats as identifying a 
variable name. It would work in comments too. Single quotes do it pretty 
well, making it a coding standards and IDEs question.


Jeff Allen

___
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/CDMD4Z3U3VZJSN2MB74MMGNEVNFZ3GLI/
Code of Conduct: http://python.org/psf/codeofconduct/


[Python-ideas] Re: Lists create

2019-06-21 Thread Jeff Allen
This list is for people who have ideas how to improve Python, not for 
those looking for help learning it.


There's such a lot of help online, it's hard to know where to send you. 
This gives the answer to your question: 
https://www.learnpython.org/en/Lists . This seems like a good list of 
places to learn Python: https://docs.python-guide.org/intro/learning/ . 
Code club (https://codeclubprojects.org/en-GB/python/) might be a good 
place to start.


Jeff Allen

On 20/06/2019 10:38, 21ispi...@parklandsacademy.co.uk wrote:

How to list create.[09]
___
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/7HNHBW3GTBYOR3RYGLHJPURNQMWQYLFR/
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/54VEHEM37KREAX6DQLLHGSARNGUGGI75/
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] add an additional dataclasses.asdict option for non-dataclasses

2019-05-11 Thread Jeff Allen

On 11/05/2019 02:46, Christopher Barker wrote:


Though yeah, that’s the key question— how often is this useful???

I have wanted something similar to this in Jython. Java supplies a 
battery of containers (with different performance and concurrency 
characteristics) and we monkepatch onto them the methods of list, set or 
dict according to their interface as java.util.List, Set or Map. This 
works "slightly badly", because sometimes those Python types have 
methods with the same name as one in the underlying container, but a 
different meaning. E.g. list.pop() disgrees about which end.


I'd much rather have some kind of explicit wrapping or cast into their 
Python type: I had imagined the wrapped object would identify and behave 
as exactly that Python type (or ABC), however, it would be a *proxy* to 
the original. Thus one obtains a "view" onto the original collection, or 
looked at another way, a way to choose the implementation behind a 
particular Python dict, set or list.


Several alternative ways of expressing this come to mind, but .asdict() 
is one.


It's not always clear in this discussion whether a proxy is envisaged 
(where a put() goes through to the original object, if allowed) or just 
a way of constructing and filling a new dict. The latter would not fit 
my imagined use.


Jeff

Jeff Allen

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] META: Is a PEP a good place to record Python's core design decisions and coding principles?

2019-03-24 Thread Jeff Allen

On 24/03/2019 17:44, Christopher Barker wrote:

Jonathan,

This is the glory of open source projects -- if you have a great idea, 
you can simply do it:


- Start a document that describes Python's Core design principles
- Put it up somewhere (gitHub would be good) where others can 
contribute to it
- If it becomes a wonderful thing, then propose that it be published 
somewhere "official" -- as a meta-PEP or whatever.


And of course people have.I'd like to thank Victor Stinner and Eli 
Bendersky for their articles about aspects of Python and its 
implementation. I've found both useful to go back to. Others may like to 
bookmark these:


https://pythondev.readthedocs.io/

https://eli.thegreenplace.net/tag/python

I looked briefly for an article on .join(), but amusingly all I found 
was further evidence that the question that started this thread is a 
recurring one (https://eli.thegreenplace.net/2008/06/06/python-impressions).


Jeff Allen

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] New Data Structure - Non Well-Founded Dict

2019-03-18 Thread Jeff Allen

Stephanie:

Welcome. The "Python idea" here is to allow a broader range of types as 
keys to a dictionary. The gap appears to be that certain types (like 
set) "don't work" as keys (or rather their identities not values work), 
but this is a misunderstanding. A set is mutable: it is as if, in an 
ordinary dictionary (lexically sorted), one were to allow changes to the 
spelling of a word while keping the definition. It's not unreasonable to 
do, but the entry is now potentially in the wrong place and ought to be 
re-inserted so someone can find it.


Others have rightly suggested python-list as a place you could explore 
how to construct the data structure you need, using existing features of 
Python. However, I'll just mention that frozenset is worth a look.


Jeff Allen

On 17/03/2019 16:35, Savant Of Illusions wrote:
I am in desperate need of a dict similar structure that allows sets 
and/or dicts as keys /and/ values. My application is NLP conceptual 
plagiarism detection. Dealing with infinite grammars communicating 
illogical concepts. Would be even better if keys could nest the same 
data structure, e.g. set(s) or dict(s) in set(s) or dict(s) of the 
set(s) or dict(s) as key(s).


In order to detect conceptual plagiarism, I need to populate a data 
structure with if/then equivalents as a decision tree. But my 
equivalents have potentially infinite ways of arranging them 
syntactically/and/ semantically.


A dict having keys with identical set values treats each key as a 
distinct element. I am dealing with semantics or elemental equivalents 
and many different statements treated as equivalent statements 
involving if/then (key/value) or a implies b, where a and/or b can be 
an element or an if/then as an element. Modeling the syntactic 
equivalences of such claims is paramount, and in order to do that, I 
need the data structure.


Hello, I am Stephanie. I have never contributed to any open source. I 
am about intermediate at python and I am a self-directed 
learner/hobbyist. I am trying to prove with my code that a particular 
very famous high profile pop debate intellectual is plagiarizing 
Anders Breivik. I can show it via observation, but his dishonesty is 
dispersed among many different talks/lectures. I am dealing with a 
large number of speaking hours as transcripts containing breadcrumbs 
that are very difficult for a human to piece together as having come 
from the manifesto which is 1515 pages and about half copied from 
other sources. The concepts stolen are rearrangements and 
reorganizations of the same identical claims and themes. He 
occasionally uses literal string plagiarism but not very much at once. 
He is very good at elaboration which makes it even more difficult.


Thank you, for your time,
Stephanie

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Vectorization [was Re: Add list.join() please]

2019-02-02 Thread Jeff Allen

On 02/02/2019 18:44, MRAB wrote:

On 2019-02-02 17:31, Adrien Ricocotam wrote:
> I personally would the first option to be the case. But then vectors 
shouldn't be list-like but more generator like.

>
OK, here's another one: if you use 'list(...)' on a vector, does it 
apply to the vector itself or its members?


>>> list(my_strings)

You might be wanting to convert a vector into a list:

['one', 'two', 'three']

or convert each of its members onto lists:

Vector([['one'], ['two'], ['three']])


More likely you mean:

>>> [list(i) for i in ['one', 'two', 'three']]
[['o', 'n', 'e'], ['t', 'w', 'o'], ['t', 'h', 'r', 'e', 'e']]

The problem, of course, is that list() now has to understand Vector 
specially, and so does any function you think of applying to it. 
Operators are easier (even those like [1:]) because Vector can make its 
own definition of each through (a finite set of) dunder methods. To make 
a Vector accept an arbitrarily-named method call like my_strings.upper() 
to mean:


>>> [i.upper() for i in ['one', 'two', 'three']]
['ONE', 'TWO', 'THREE']

is perhaps just about possible by manipulating __getattribute__ to 
resolve names matching methods on the underlying type to a callable that 
loops over the content.


Jeff

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Add list.join() please

2019-01-29 Thread Jeff Allen

On 29/01/2019 01:40, Jamesie Pic wrote:
... I'm still sometimes confused between the different syntaxes used 
by join methods:


0. os.path.join takes *args
1. str.join takes a list argument, this inconsistence make it easy to 
mistake with the os.path.join signature


It seems fairly consistent to make:

    os.path.join('a', 'b', 'c')

short for:

    os.path.sep.join(['a', 'b', 'c'])


Also, I still think that:

'_'.join(['cancel', name])

Would be more readable as such:

['cancel', name].join('_')


Please, no. This would be un-Pythonic in my view. It makes so much more 
sense that str should have a method that takes an iterable, returning 
str, than that every iterable should have a join(str) returning str. 
Consider you get this kind of thing for free:


    "-".join(str(i) for i in range(10))

I learned enough Groovy last year to use Gradle and was so disappointed 
to find myself having to write:


   excludes: exclusions.join(',')    // Yes, it's that way round :o

Even Java agrees (since 1.8) with Python.

Jeff Allen

___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] Why shouldn't Python be better at implementing Domain Specific Languages?

2018-08-31 Thread Jeff Allen
The word "domain" appears in this sense on the first page of Aho and 
Ullman and ANTLR (which I know you've used) describes itself as a tool 
for building domain-specific languages. Both pre-date Ruby I'm fairly sure.


James Lu, quoting Jonathan Fine, used the term "internal DSL" and 
although that's new to me, people seem to be interpreting it in the 
sense that Gradle is a Groovy DSL (note caps), a build tool a lot of 
software developers will be familiar with. In that case, what you write 
really is Groovy, but the execution environment has been pre-conditioned 
with objects and libraries that (almost) make a new language. When you 
understand what's going on (not sure I always do), it becomes possible 
to mix Gradle statements and Groovy freely. The most immediate benefit 
is that all the apparatus of expressions and types/methods is already 
present. So "internal" is the key word.


The point about punctuation is spot-on, I think: Groovy is relatively 
free of (makes optional) some punctuation, including the parentheses 
that make calls easily identifiable in Python. So quite possibly 
starting from Python is limiting if what you want is an*internal* DSL 
with a grammar you choose: the object system is fantastic-plastic, but 
the grammar is not. DSLs embedded in Python are common, of course 
(f-strings, regexes, SQL), and DSLs can generate Python from fragments 
with almost no constraints on their own grammar. iPython strikes me as 
possibly a Python internal DSL, or Django, but what they've done does 
not take us far from pure Python.


Jeff Allen

On 31/08/2018 05:07, Guido van Rossum wrote:


Hm. YAML is indeed a great, readable alternative to JSON or XML. But 
the term DSL implies (to me) more than just nested key-value pairs. 
(Though who knows maybe that's all Keras needs, and then it's a poor 
argument for having a DSL.)


Then again maybe I'm confusing DSL (which appears to be a Rubyism) 
with "little language": http://wiki.c2.com/?LittleLanguage


--
--Guido van Rossum (python.org/~guido <http://python.org/%7Eguido>)



___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/