Re: Await expressions

2024-01-27 Thread Mild Shock via Python-list



We say that an object is an awaitable object if it can be used in an 
await expression. Many asyncio APIs are designed to accept awaitables.


There are three main types of awaitable objects:
coroutines, Tasks, and Futures.

Stefan Ram schrieb:

   In "The Python Language Reference, Release 3.13.0a0",
   there is this section:

|6.4 Await expression
|
|Suspend the execution of coroutine on an awaitable object.
|Can only be used inside a coroutine function.
|
|await_expr ::= "await" primary
|
|New in version 3.5.

   . And this is the whole section.

   What I do not understand:

   - Which coroutine is suspended?
   - Which object is the object mentioned?
   - For what purpose is the value of the primary expression used?
   - What does it mean to "suspend something on something"?



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


Re: Await expressions

2024-01-27 Thread Mild Shock via Python-list

Maybe consult:

PEP 492 – Coroutines with async and await syntax
Created: 09-Apr-2015
Python-Version: 3.5
https://peps.python.org/pep-0492/

Mild Shock schrieb:


We say that an object is an awaitable object if it can be used in an 
await expression. Many asyncio APIs are designed to accept awaitables.


There are three main types of awaitable objects:
coroutines, Tasks, and Futures.

Stefan Ram schrieb:

   In "The Python Language Reference, Release 3.13.0a0",
   there is this section:

|6.4 Await expression
|
|Suspend the execution of coroutine on an awaitable object.
|Can only be used inside a coroutine function.
|
|await_expr ::= "await" primary
|
|New in version 3.5.

   . And this is the whole section.

   What I do not understand:

   - Which coroutine is suspended?
   - Which object is the object mentioned?
   - For what purpose is the value of the primary expression used?
   - What does it mean to "suspend something on something"?





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


Re: Await expressions (Posting On Python-List Prohibited)

2024-02-03 Thread Mild Shock via Python-list



I am still waiting for async files in the
style of nodejs that works on windows and

is bundled with the main python distribution.
I am not very  fond on doing something

like adding listeners to a file descriptor,
in nodejs async files are based on callbacks

not on listeners. Whats the roadmap here?

Lawrence D'Oliveiro schrieb:

On 2 Feb 2024 09:12:06 GMT, Stefan Ram wrote:


|The IO part of the event loop is built upon a single crucial
|function called "select".


select(2) has limitations. Better to use poll(2). Depending on *nix
variant, other more advanced alternatives may also be available
.



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


Re: Await expressions (Posting On Python-List Prohibited)

2024-02-03 Thread Mild Shock via Python-list


And whats the roadmap for an asyncified module
loader, is this on the radar of Python?

Mild Shock schrieb:


I am still waiting for async files in the
style of nodejs that works on windows and

is bundled with the main python distribution.
I am not very  fond on doing something

like adding listeners to a file descriptor,
in nodejs async files are based on callbacks

not on listeners. Whats the roadmap here?

Lawrence D'Oliveiro schrieb:

On 2 Feb 2024 09:12:06 GMT, Stefan Ram wrote:


|The IO part of the event loop is built upon a single crucial
|function called "select".


select(2) has limitations. Better to use poll(2). Depending on *nix
variant, other more advanced alternatives may also be available
.





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


Re: Await expressions (Posting On Python-List Prohibited)

2024-02-03 Thread Mild Shock via Python-list

The docu tells me:

Windows
loop.add_reader() and loop.add_writer() only accept
socket handles (e.g. pipe file descriptors are not supported).
https://docs.python.org/3/library/asyncio-platforms.html

Alternatives are aiofiles and anyio and maybe more,
but not sure whether they span all platforms, i.e. unix,
windows and mac or whether they have similar restrictions.

Theoretically on Windows IOCP should also cover file handles:
https://en.wikipedia.org/wiki/Input/output_completion_port

Its use by libuv the basis for node.js.

Lawrence D'Oliveiro schrieb:

On Sat, 3 Feb 2024 00:45:18 +0100, Mild Shock wrote:


... that works on windows ...


You lost me there.



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


Re: Await expressions (Posting On Python-List Prohibited)

2024-02-03 Thread Mild Shock via Python-list



Funny source code tells me IOCP is used;

proactor is only implemented on Windows with IOCP.
https://github.com/python/cpython/blob/3.12/Lib/asyncio/proactor_events.py

But maybe the focus is more on networking than file system.

But it has sock_sendfile() that might avoid copying data to userspace.

Mild Shock schrieb:

The docu tells me:

Windows
loop.add_reader() and loop.add_writer() only accept
socket handles (e.g. pipe file descriptors are not supported).
https://docs.python.org/3/library/asyncio-platforms.html

Alternatives are aiofiles and anyio and maybe more,
but not sure whether they span all platforms, i.e. unix,
windows and mac or whether they have similar restrictions.

Theoretically on Windows IOCP should also cover file handles:
https://en.wikipedia.org/wiki/Input/output_completion_port

Its use by libuv the basis for node.js.

Lawrence D'Oliveiro schrieb:

On Sat, 3 Feb 2024 00:45:18 +0100, Mild Shock wrote:


... that works on windows ...


You lost me there.





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


Re: Python told me a Joke

2024-09-03 Thread Mild Shock via Python-list

You can try:

>>> 1,2 == 2,2
(1, True, 2)

Its the same as:

>>> 1, (2 == 2), 2
(1, True, 2)

Hope this helps!

Alan Bawden schrieb:

 Python 3.10.5 (v3.10.5:f37715, Jul 10 2022, 00:26:17) [GCC 4.9.2] on linux
 Type "help", "copyright", "credits" or "license" for more information.
 >>> x,_,z = [1,2,3]

Works as expected.

Now I didn't expect the following to work (but Python sometimes
surprises me!), so I tried:
  
 >>> x,2,z = [1,2,3]

   File "", line 1
 x,2,z = [1,2,3]
 ^^^
 SyntaxError: invalid syntax. Maybe you meant '==' or ':=' instead of '='?

Yeah, that makes sense, no surprises today...  Except "maybe you meant
'=='..." caught my attention.  _Could_ that be what someone would want
in this situation I wondered?  So I tried:

 >>> x,2,z == [1,2,3]
 (1, 2, False)

Now that made me laugh.

- Alan

[ Some people reading this will be tempted to explain what's really
   going on here -- it's not hard to understand.  But please remember that
   a joke is never funny if you have to explain it. ]



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


Re: Two aces up Python's sleeve

2024-11-08 Thread Mild Shock via Python-list

Hi,

In Java its possible to work this way
with the Integer datatype, just call
Integer.valueOf().

I am not sure whether CPython does the
same. Because it shows me the same behaviour
for small integers that are more than

only in the range -128 to 128. You can try yourself:

Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
>>> x,y = 10**10, 10**9*10
>>> id(x) == id(y)
True

Maybe the idea that objects have an address
that can be accessed via id() has been abandoned.
This is already seen in PyPy. So maybe we

are falsly assuming that id() gives na object address.

Greg Ewing schrieb:

On 8/11/24 3:04 am, Mild Shock wrote:

This only works for small integers. I guess
this is because tagged pointers are used
nowadays ?


No, it's because integers in a certain small range are cached. Not sure 
what the actual range is nowadays, it used to be something like -5 to 
256 I think.


BTW you have to be careful testing this, because the compiler sometimes 
does constant folding, so you need to be sure it's actually computing 
the numbers at run time.




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


Re: Two aces up Python's sleeve

2024-11-08 Thread Mild Shock via Python-list



For example this article:

https://www.codementor.io/@arpitbhayani/python-caches-integers-16jih595jk

about the integer singletons claims:

>>> x, y = 257, 257
>>> id(x) == id(y)
False

But on Windows my recent CPython doesn't do that:

Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
>>> x, y = 257, 257
>>> id(x) == id(y)
True

Mild Shock schrieb:

Hi,

In Java its possible to work this way
with the Integer datatype, just call
Integer.valueOf().

I am not sure whether CPython does the
same. Because it shows me the same behaviour
for small integers that are more than

only in the range -128 to 128. You can try yourself:

Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
 >>> x,y = 10**10, 10**9*10
 >>> id(x) == id(y)
True

Maybe the idea that objects have an address
that can be accessed via id() has been abandoned.
This is already seen in PyPy. So maybe we

are falsly assuming that id() gives na object address.

Greg Ewing schrieb:

On 8/11/24 3:04 am, Mild Shock wrote:

This only works for small integers. I guess
this is because tagged pointers are used
nowadays ?


No, it's because integers in a certain small range are cached. Not 
sure what the actual range is nowadays, it used to be something like 
-5 to 256 I think.


BTW you have to be careful testing this, because the compiler 
sometimes does constant folding, so you need to be sure it's actually 
computing the numbers at run time.






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


Re: Two aces up Python's sleeve

2024-11-08 Thread Mild Shock via Python-list

The wiked brain of ChatGPT gives me a lead:

PEP 659
Storing data caches before the bytecode.

Maybe its an effect of constant folding
and constant pooling by the compiler?

Mild Shock schrieb:


For example this article:

https://www.codementor.io/@arpitbhayani/python-caches-integers-16jih595jk

about the integer singletons claims:

 >>> x, y = 257, 257
 >>> id(x) == id(y)
False

But on Windows my recent CPython doesn't do that:

Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
 >>> x, y = 257, 257
 >>> id(x) == id(y)
True

Mild Shock schrieb:

Hi,

In Java its possible to work this way
with the Integer datatype, just call
Integer.valueOf().

I am not sure whether CPython does the
same. Because it shows me the same behaviour
for small integers that are more than

only in the range -128 to 128. You can try yourself:

Python 3.14.0a1 (tags/v3.14.0a1:8cdaca8, Oct 15 2024, 20:08:21)
 >>> x,y = 10**10, 10**9*10
 >>> id(x) == id(y)
True

Maybe the idea that objects have an address
that can be accessed via id() has been abandoned.
This is already seen in PyPy. So maybe we

are falsly assuming that id() gives na object address.

Greg Ewing schrieb:

On 8/11/24 3:04 am, Mild Shock wrote:

This only works for small integers. I guess
this is because tagged pointers are used
nowadays ?


No, it's because integers in a certain small range are cached. Not 
sure what the actual range is nowadays, it used to be something like 
-5 to 256 I think.


BTW you have to be careful testing this, because the compiler 
sometimes does constant folding, so you need to be sure it's actually 
computing the numbers at run time.








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


Re: Two aces up Python's sleeve (Posting On Python-List Prohibited)

2024-11-08 Thread Mild Shock via Python-list

Well you can use your Browser, since
JavaScript understand post and pre increment:

> x = 5
5
> x ++
5
> x = 5
5
> ++ x
6

So we have x ++ equals in Python:

x + = 1
x - 1

And ++ x equals in Python:

x += 1
x

But I don't know how to combine an
assignment and an expression into one
expession. In JavaScript one can use

the comma:

> x = 5
5
> y = (x += 1, x - 1)
5
> x = 5
5
> y = (x += 1, x)
6

But in Python the comma would create a tuple.

Lawrence D'Oliveiro schrieb:

On Thu, 07 Nov 2024 12:55:53 +0530, Annada Behera wrote:


I heard this behavior is because python's integers are immutable.


Nothing to do with that.


++x or x++ will redefine 5 to 6, which the interpreter forbids ...


One of those is actually syntactically valid.

It just won’t do what you expect it to do.



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


Re: Two aces up Python's sleeve

2024-11-08 Thread Mild Shock via Python-list

This only works for small integers. I guess
this is because tagged pointers are used
nowadays ? For large integers, also known

as bigint, it doesn't work:

Python 3.13.0a1 (tags/v3.13.0a1:ad056f0, Oct 13 2023, 09:51:17)

>>> x, y = 5, 4+1
>>> id(x) == id(y)
True

>>> x, y = 10**200, 10**199*10
>>> x == y
True
>>> id(x) == id(y)
False

In tagged pointers a small integer is
directly inlined into the pointer. The
pointer has usually some higher bits,

that identify the type and when masking
to see the lower bits, one gets the
integer value.

But I don't know for sure whats going on,
would need to find a CPython documentation.

P.S.: I also tested PyPy it doesn't show
the same behaviour, because it computes
an exaberated id():

Python 3.10.14 (39dc8d3c85a7, Aug 27 2024, 14:33:33)
[PyPy 7.3.17 with MSC v.1929 64 bit (AMD64)]
 x, y = 5, 4+1
 id(x) == id(y)
True

 x, y = 10**200, 10**199*10
 id(x) == id(y)
True
 id(x)
16
00
00
00
01

Quite funny!

Annada Behera schrieb:

Then please explain why I have to write:

     i += 1

Instead of the shorter:

     i ++

My short-term memory is really stressed.


I heard this behavior is because python's integers are immutable.
For example:

 >>> x,y = 5,5
 >>> id(x) == id(y)
 True

5 is a object that x and y points to. ++x or x++ will redefine 5 to
6, which the interpreter forbids to keep it's state mathematically
consistent. Also, by not supporting x++ and ++x, it avoids the pre-
and post-increment (substitute-increment v. increment-substitute) bugs
that plagues C and it's children.




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