Re: How to support annotations for a custom type in a C extension?

2021-09-18 Thread MRAB

On 2021-09-18 16:10, Serhiy Storchaka wrote:

18.09.21 09:40, Marco Sulla пише:

Ooook. I have a question. Why is this code not present in
dictobject.c? Where are the dict annotations implemented?


In dictobject.c.


I just had a look at dictobject.c. It too has a cast to PyCFunction.
--
https://mail.python.org/mailman/listinfo/python-list


Re: How to support annotations for a custom type in a C extension?

2021-09-18 Thread MRAB

On 2021-09-18 16:09, Serhiy Storchaka wrote:

18.09.21 03:54, MRAB пише:

static PyMethodDef customdict_methods[] = {
...
    {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_CLASS |
METH_O | METH_COEXIST, PyDoc_STR("See PEP 585")},
...
};


Note the flags: METH_CLASS says that it's a class method and
METH_COEXIST says that it should use this method instead of the slot.


"(PyCFunction)" is redundant, Py_GenericAlias already has the right
type. Overuse of casting to PyCFunction can hide actual bugs.


I borrowed that from listobject.c, which does have the cast.


METH_COEXIST is not needed. There is no slot for __class_getitem__, and
even if it was, there would be no reasons to prohibit using it.


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


Re: ANN: Dogelog Runtime, Prolog to the Moon (2021)

2021-09-18 Thread Chris Angelico
On Sun, Sep 19, 2021 at 11:46 AM Mostowski Collapse  wrote:
>
> Yeah, it seems weak references could indeed spare
> me mark_term(). But then I am stil left with sweep_trail().
> I did not yet measure what takes more time mark_term()
> or sweep_trail(). The displayed "gc" is the sum of both.
>
> From what I have seen, very large trail practically reduced
> to a zero trail during Prolog GC, I am assuming that
> mark_term() is not the working horse. Usually mark_term()
> only marks what is not-Garbage, and sweep_trail()
>
> has to deal with Garbage and not-Garbage. And there
> is usually a lot of Garbage, much more than not-Garbage.
> Finding the objects that survive, is like finding the needle
> in the haystack, except we do not have to scan the

If you stop referring to something, it is garbage. Python will dispose of it.

You literally need to do nothing at all, and let the language take
care of things.

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


Re: How to support annotations for a custom type in a C extension?

2021-09-18 Thread Serhiy Storchaka
18.09.21 09:40, Marco Sulla пише:
> Ooook. I have a question. Why is this code not present in
> dictobject.c? Where are the dict annotations implemented?

In dictobject.c.

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


Re: How to support annotations for a custom type in a C extension?

2021-09-18 Thread Serhiy Storchaka
18.09.21 03:54, MRAB пише:
> static PyMethodDef customdict_methods[] = {
> ...
>     {"__class_getitem__", (PyCFunction)Py_GenericAlias, METH_CLASS |
> METH_O | METH_COEXIST, PyDoc_STR("See PEP 585")},
> ...
> };
> 
> 
> Note the flags: METH_CLASS says that it's a class method and
> METH_COEXIST says that it should use this method instead of the slot.

"(PyCFunction)" is redundant, Py_GenericAlias already has the right
type. Overuse of casting to PyCFunction can hide actual bugs.

METH_COEXIST is not needed. There is no slot for __class_getitem__, and
even if it was, there would be no reasons to prohibit using it.

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


Re: How to "cast" an object to a derived class?

2021-09-18 Thread Robert Latest via Python-list
Stefan Ram wrote:
> Robert Latest  writes: But how can I "promote" a
>>given Opaque instance to the derived class?
>
>   Sometimes, one can use containment instead of inheritance.

Nah, doesn't work in my case. I'm trying to write a wrapper around
xml.etree.ElemenTree and .Element  to circumvent its idiotic namespace
handling. For that I need inheritance since I want to override the find..()
functions to return my derived MyElement classes. I think it could work but I
somehow need to convert the root Element to a MyElement.

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


Re: ANN: Dogelog Runtime, Prolog to the Moon (2021)

2021-09-18 Thread Greg Ewing

On 16/09/21 6:13 am, Mostowski Collapse wrote:

So in Python I now do the following:

peek = kb.get(functor, NotImplemented)
if peek is not NotImplemented:


If you're able to use None instead of NotImplemented, you
could simplify that to

peek = kb.get(functor)
if peek:
...


But if get() in Python is implemented under the hood with
exception handling. ... then Python get() will probably be quite slow.


No, it doesn't use exceptions as far as I know. It should
be fairly fast.

--
Greg

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


Re: ANN: Dogelog Runtime, Prolog to the Moon (2021)

2021-09-18 Thread Mostowski Collapse
Yeah, it seems weak references could indeed spare
me mark_term(). But then I am stil left with sweep_trail().
I did not yet measure what takes more time mark_term()
or sweep_trail(). The displayed "gc" is the sum of both.

>From what I have seen, very large trail practically reduced
to a zero trail during Prolog GC, I am assuming that 
mark_term() is not the working horse. Usually mark_term()
only marks what is not-Garbage, and sweep_trail()

has to deal with Garbage and not-Garbage. And there
is usually a lot of Garbage, much more than not-Garbage.
Finding the objects that survive, is like finding the needle
in the haystack, except we do not have to scan the 

haystack, the needles are on the goal list. But afterwards,
the second pass, scanning the trail is the work of Heracles
cleaning the Augeas stables. This scan is trowing away the 
hay and keeping the needles.

Greg Ewing schrieb am Samstag, 18. September 2021 um 05:49:37 UTC+2:
> On 17/09/21 7:56 am, Mostowski Collapse wrote: 
> > The trail in Dogelog 
> > Runtime is a single linked list: 
> > 
> > -->[ A ]-->[ B ]-->[ C ]--> 
> > 
> > Now if B becomes unused, you need to rewire 
> > the trail, it should then look like: 
> > 
> > -->[ A ]-->[ C ]-->
> Python has a way of creating weak references (see the weakref 
> module). You could set the trail up like this: 
> 
> -->[*]-->[*]-->[*]--> 
> | | | 
> v v v 
> [w] [w] [w] 
> : : : 
> v v v 
> [A] [B] [C] 
> 
> where [w] is a weak reference object. Then you could periodically 
> scan the trail looking for dead weakref objects and remove the 
> corresponding [*] node from the list. 
> 
> You can also attach callbacks to weakref objects that are triggered 
> when the referenced object dies. You might be able to make use of 
> that to remove items from the trail instead of the periodic scanning. 
> 
> -- 
> Greg
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: ANN: Dogelog Runtime, Prolog to the Moon (2021)

2021-09-18 Thread Greg Ewing

On 17/09/21 8:41 pm, Mostowski Collapse wrote:

Are exceptions
blocks in Python cheap or expensive? Are they like
in Java, some code annotation, or like in Go
programming language pushing some panic handler?


They're not free, but they're not hugely expensive either.
Don't be afraid to use them when it makes sense to do so.

I'm not sure what you mean by "some code annotations",
so I don't know how to answer that question. I suspect
that the way exceptions are implemented in the JVM is
similar to the way CPython does it, but I don't know
a lot about the JVM.

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


Re: ANN: Dogelog Runtime, Prolog to the Moon (2021)

2021-09-18 Thread Greg Ewing

On 17/09/21 7:56 am, Mostowski Collapse wrote:

The trail in Dogelog
Runtime is a single linked list:

     -->[ A ]-->[ B ]-->[ C ]-->

Now if B becomes unused, you need to rewire
the trail, it should then look like:

     -->[ A ]-->[ C ]-->


Python has a way of creating weak references (see the weakref
module). You could set the trail up like this:

-->[*]-->[*]-->[*]-->
| | |
v v v
   [w]   [w]   [w]
: : :
v v v
   [A]   [B]   [C]

where [w] is a weak reference object. Then you could periodically
scan the trail looking for dead weakref objects and remove the
corresponding [*] node from the list.

You can also attach callbacks to weakref objects that are triggered
when the referenced object dies. You might be able to make use of
that to remove items from the trail instead of the periodic scanning.

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


Re: Question again

2021-09-18 Thread Michael F. Stemper

On 16/09/2021 19.11, Alan Gauld wrote:

On 16/09/2021 06:50, af kh wrote:

Hello,
I was doing some coding on a website called replit


I have no idea what that is but...


after answering 'no' or 'yes' after the last sentence I wrote,
the Python window shut off,


That's what you told it to do in the code.
Regardless of which answer the user gives the program
reaches the end and stops.


  in replit I added one more sentence, but it isn't shown on Python,


I dn;t know what this means.


#Gives greetings to the user
import random

...

#Ask to pick between numbers 1~10 to see if you will get lucky today: Third 
question
number = input("Please pick between numbers 1~10 to see your luck for today: ")

#From number 1~3 and an answer
if number == "1" or number == "2" or number == "3" :
   print("You're in grat luck today!")

#From number 4~7 and an answer
elif number == "4" or number == "5" or number == "6" :
   print("damn, bad luck is coming your way")


The cde and comment are not consistent.


#From number 8~10 and an answer
elif number == "7" or number == "8" or number == "9" or number == "10" :
   print("I cannot sense any luck today, try again next time")


Same here.


This is, of course, why comments shouldn't just translate the code
into English. They'll get out of synch. Instead, comments should say
*why* the code is doing what it does.

--
Michael F. Stemper
Exodus 22:21
--
https://mail.python.org/mailman/listinfo/python-list


How to "cast" an object to a derived class?

2021-09-18 Thread Robert Latest via Python-list
Hi all, let's assume I'm using a module that defines some class "Opaque" and
also a function that creates objects of that type. In my program I subclass
that type because I want some extra functionality. But how can I "promote" a
given Opaque instance to the derived class? Of course I could just create an
instance of the derived type and copy all attributes from the original object
to the new one, but that either breaks when the opaque.py module changes, or it
requires introspection. It's easily done of course but seems overly clumsy. Is
there a more Pythonic way?

# this is the module opaque.py
class Opaque(): def __init__(self, x): assert isinstance(x, int) self.n = x

def make_opaque(): return Opaque(0)

# this is my program
import opaque class MyClass(opaque.Opaque):
# generic __init__ b/c I don't want to have to know anything about that
# class
def __init__(self, *a, **k): super().__init__(*a, *k) self.__something = 0

def get_something(self): return self.something

op = opaque.make_opaque()

# But I want to have this as type MyClass. This obviously doesn't work:
my_object = MyClass(op)
# But what does?
-- 
https://mail.python.org/mailman/listinfo/python-list


¡¡¡uoɥʇʎꓒ uᴉ ʇxǝʇ uʍoꓷ ǝpᴉsdꓵ ǝʇɐǝɹↃ oʇ ʍoΗ

2021-09-18 Thread Python 4 Fun
How to Create Upside Down text in Python!!!

This is a very simple yet very interesting program in python. 

We are going to make our text, word or string UPSIDE DOWN which will be still 
readable on phone or desktop if you stand on your head lol.

But any ways... lets do a little research first.

I am going to collect upside down texts using google as it cannot be written by 
yourself and if you do it would be time consuming.

To make this quick here are some text that I have found from following sources:

Read more...

👉 
https://pysnakeblog.blogspot.com/2021/09/create-upside-down-text-for-instagram.html
-- 
https://mail.python.org/mailman/listinfo/python-list