Re: Feature Request

2022-03-23 Thread Dennis Lee Bieber
On Wed, 23 Mar 2022 01:55:37 -0700 (PDT), Kazuya Ito
 declaimed the following:

>Add "trun()" function to Python to truncate decimal part.

You'll have to define what specific behavior you think is missing from
the currently available functions?

>>> plusover = 2.78
>>> plusunder = 3.14
>>> minusover = -2.78
>>> minusunder = -3.14
>>> import math
>>> for v in (plusover, plusunder, minusover, minusunder):
... print("%s: int %s, round %s, math.trunc %s, math.floor %s,
math.ceil %s"
... % (v, int(v), round(v), math.trunc(v), math.floor(v),
math.ceil(v)))
... 
2.78: int 2, round 3, math.trunc 2, math.floor 2, math.ceil 3
3.14: int 3, round 3, math.trunc 3, math.floor 3, math.ceil 4
-2.78: int -2, round -3, math.trunc -2, math.floor -3, math.ceil -2
-3.14: int -3, round -3, math.trunc -3, math.floor -4, math.ceil -3
>>> 

int() and .trunc() move toward 0, .floor() moves to less positive,
.ceil() moves to more positive.


-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: difficult start with asyncio async/await

2022-03-23 Thread lacsaP Patatetom
some redesigns around asyncio.gather gave me what I wanted.

here is the code used :
```python
import asyncio
from random import randint
from termcolor import colored
from datetime import datetime

urls = ('yellow', 'cyan', 'green', 'magenta')

async def getItems(url):
print(colored(f'get new items from url {url} with aiohttp...', url))
# simulate aiohttp request/response
await asyncio.sleep(randint(1, 3))
# return a list of some items
return [ url + str(i) for i in range(1, randint(2, 5)) ]

async def postItem(item, server, url):
# url is here only to color print
print(colored(f'post new item {item} to server {server} with 
aiohttp...', url))
# simulate aiohttp request/response
await asyncio.sleep(randint(1, 3))

async def runItems(url):
items = await getItems(url)
posts = [ postItem(item, 'localhost', url) for item in items ]
posts and await asyncio.gather(*posts)

async def loopForever():
while True:
print(colored('\n' + datetime.now().strftime('%H:%M:%S') + ' 
looping...', 'red'))
tasks = [ runItems(url) for url in urls ]
tasks and await asyncio.gather(*tasks)
await asyncio.sleep(30)

asyncio.run(loopForever())
```

do you think this is the right way for what I want to do ?

regards, lacsaP.

Le mercredi 23 mars 2022 à 14:15:08 UTC+1, lacsaP Patatetom a écrit :
> hi, 
> 
> difficult start with asyncio async/await... 
> 
> I'm trying to make a mockup that queries a few sites and posts the results to 
> a server, but the result is not what I expected. 
> 
> I was expecting to get the 4 "get" one after the other, followed by the 
> "post" (eventually mixed) but I get something that looks more like procedural 
> than anything else. 
> 
> some help/clarification would be welcome. 
> 
> here is my code : 
> ```python 
> import asyncio 
> from itertools import cycle 
> from random import randint 
> from termcolor import colored 
> 
> urls = ('yellow', 'cyan', 'green', 'magenta') 
> 
> async def getItems(url): 
> print(colored(f'get new items from url {url} with aiohttp...', url)) 
> # simulate aiohttp request/response 
> await asyncio.sleep(randint(1, 5)) 
> # return a list of some items 
> return [ url + str(i) for i in range(1, randint(2, 5)) ] 
> 
> async def postItem(item, server, url): 
> # url is here only to color print 
> print(colored(f'post new item {item} to server {server} with aiohttp...', 
> url)) 
> # simulate aiohttp request/response 
> await asyncio.sleep(randint(1, 5)) 
> 
> async def runItems(url): 
> items = await getItems(url) 
> for item in items: 
> await postItem(str(item), 'localhost', url) 
> 
> async def loopForever(): 
> while True: 
> print(colored('looping...', 'red')) 
> for url in urls: 
> await runItems(url) 
> # sleeping 30s before next loop 
> await asyncio.sleep(10) 
> 
> loop = asyncio.get_event_loop() 
> tasks = [ loop.create_task(loopForever()) ] 
> loop.run_until_complete(asyncio.wait(tasks)) 
> loop.close() 
> ``` 
> 
> and here is the results: 
> ``` 
> looping... 
> get new items from url yellow with aiohttp... 
> post new item yellow1 to server localhost with aiohttp... 
> post new item yellow2 to server localhost with aiohttp... 
> post new item yellow3 to server localhost with aiohttp... 
> post new item yellow4 to server localhost with aiohttp... 
> get new items from url cyan with aiohttp... 
> post new item cyan1 to server localhost with aiohttp... 
> post new item cyan2 to server localhost with aiohttp... 
> post new item cyan3 to server localhost with aiohttp... 
> post new item cyan4 to server localhost with aiohttp... 
> get new items from url green with aiohttp... 
> post new item green1 to server localhost with aiohttp... 
> post new item green2 to server localhost with aiohttp... 
> post new item green3 to server localhost with aiohttp... 
> post new item green4 to server localhost with aiohttp... 
> get new items from url magenta with aiohttp... 
> post new item magenta1 to server localhost with aiohttp... 
> post new item magenta2 to server localhost with aiohttp... 
> post new item magenta3 to server localhost with aiohttp... 
> post new item magenta4 to server localhost with aiohttp... 
> looping... 
> get new items from url yellow with aiohttp... 
> post new item yellow1 to server localhost with aiohttp... 
> post new item yellow2 to server localhost with aiohttp... 
> post new item yellow3 to server localhost with aiohttp... 
> ... 
> ``` 
> 
> here is what was expected : 
> ``` 
> looping... 
> get new items from url yellow with aiohttp... 
> get new items from url cyan with aiohttp... 
> get new items from url green with aiohttp... 
> get new items from url magenta with aiohttp... 
> post new item... 
> looping... 
> get new items from url yellow with aiohttp... 
> get new items from url cyan with aiohttp... 
> get new items from url green with aiohttp... 
> get new items from url 

Re: Feature Request

2022-03-23 Thread Michael F. Stemper

On 23/03/2022 03.55, Kazuya Ito wrote:

Add "trun()" function to Python to truncate decimal part.


Which of these should its behavior copy?


from math import pi
int(pi)

3

pi-int(pi)

0.14159265358979312




--
Michael F. Stemper
This post contains greater than 95% post-consumer bytes by weight.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Difficulty in installing Python

2022-03-23 Thread Dennis Lee Bieber
On Tue, 22 Mar 2022 19:20:55 +0530, Reuel Lewis 
declaimed the following:

>I'm trying to install Python as I'd like to learn it. I'm a newbie in all
>things related to software programming.

Nothing programming related with your problem...

>I have Windows 10 installed on my HP laptop. I tried to install Python but
>I didn't Click on the 'Add python 3.10 to path'. So I tried to uninstall it
>and install Python once more. But I only get options like Modify / Repair.
>If I click on either of those options it does the Modify / Repair thing and
>then I just see the 'Close' button. It does not go anywhere else from
>there.

The Python interpreter (what you use to run Python programs) is NOT the
Python installer (what you use to make Python available). Once you've
installed Python -- hide the installer! It is only used to, well,
modify/repair/uninstall Python.

Python, by itself, is not an IDE (it is not something like Visual
Studio, FreePascal's Lazarus, et al.). It is just a command-line
interpreter.

>So what should I do to start this program so I can start learning? Thank
>you and stay safe always.

Well, if you'd added it to the PATH, you'd open a command shell, and
provide a script file to the interpreter (if you don't provide a script
file, you'd end up in the interactive interpreter prompt --  to
exit)

-=-=-
Microsoft Windows [Version 10.0.19041.1415]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Wulfraed>python
Python ActivePython 3.8.2 (ActiveState Software Inc.) based on
 on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\Users\Wulfraed>
-=-=-

Recent versions of Python install a generic launcher on Windows named
"py", which is supposed to reduce the need to track/update the PATH
variable... but for me... I found too many other packages were installing
Python interpreters which hijacked the version "py" defaulted to running.

Most versions of Python also install a Tkinter script called IDLE which
provides a rudimentary IDE capability.


>
>Kind Regards,
>Reuel R. Lewis


-- 
Wulfraed Dennis Lee Bieber AF6VN
wlfr...@ix.netcom.comhttp://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Feature Request

2022-03-23 Thread Kazuya Ito
Add "trun()" function to Python to truncate decimal part.
-- 
https://mail.python.org/mailman/listinfo/python-list


difficult start with asyncio async/await

2022-03-23 Thread lacsaP Patatetom
hi,

difficult start with asyncio async/await...

I'm trying to make a mockup that queries a few sites and posts the results to a 
server, but the result is not what I expected.

I was expecting to get the 4 "get" one after the other, followed by the "post" 
(eventually mixed) but I get something that looks more like procedural than 
anything else.

some help/clarification would be welcome.

here is my code :
```python
import asyncio
from itertools import cycle
from random import randint
from termcolor import colored

urls = ('yellow', 'cyan', 'green', 'magenta')

async def getItems(url):
print(colored(f'get new items from url {url} with aiohttp...', url))
# simulate aiohttp request/response
await asyncio.sleep(randint(1, 5))
# return a list of some items
return [ url + str(i) for i in range(1, randint(2, 5)) ]

async def postItem(item, server, url):
# url is here only to color print
print(colored(f'post new item {item} to server {server} with 
aiohttp...', url))
# simulate aiohttp request/response
await asyncio.sleep(randint(1, 5))

async def runItems(url):
items = await getItems(url)
for item in items:
await postItem(str(item), 'localhost', url)

async def loopForever():
while True:
print(colored('looping...', 'red'))
for url in urls:
await runItems(url)
# sleeping 30s before next loop
await asyncio.sleep(10)

loop = asyncio.get_event_loop()
tasks = [ loop.create_task(loopForever()) ]
loop.run_until_complete(asyncio.wait(tasks))
loop.close()
```

and here is the results:
```
looping...
get new items from url yellow with aiohttp...
post new item yellow1 to server localhost with aiohttp...
post new item yellow2 to server localhost with aiohttp...
post new item yellow3 to server localhost with aiohttp...
post new item yellow4 to server localhost with aiohttp...
get new items from url cyan with aiohttp...
post new item cyan1 to server localhost with aiohttp...
post new item cyan2 to server localhost with aiohttp...
post new item cyan3 to server localhost with aiohttp...
post new item cyan4 to server localhost with aiohttp...
get new items from url green with aiohttp...
post new item green1 to server localhost with aiohttp...
post new item green2 to server localhost with aiohttp...
post new item green3 to server localhost with aiohttp...
post new item green4 to server localhost with aiohttp...
get new items from url magenta with aiohttp...
post new item magenta1 to server localhost with aiohttp...
post new item magenta2 to server localhost with aiohttp...
post new item magenta3 to server localhost with aiohttp...
post new item magenta4 to server localhost with aiohttp...
looping...
get new items from url yellow with aiohttp...
post new item yellow1 to server localhost with aiohttp...
post new item yellow2 to server localhost with aiohttp...
post new item yellow3 to server localhost with aiohttp...
...
```

here is what was expected :
```
looping...
get new items from url yellow with aiohttp...
get new items from url cyan with aiohttp...
get new items from url green with aiohttp...
get new items from url magenta with aiohttp...
post new item...
looping...
get new items from url yellow with aiohttp...
get new items from url cyan with aiohttp...
get new items from url green with aiohttp...
get new items from url magenta with aiohttp...
post new item...
...
```

regards, lacsaP.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: convenience

2022-03-23 Thread Barry Scott



> On 22 Mar 2022, at 18:00, Avi Gross via Python-list  
> wrote:
> 
> An earlier post talked about a method they used for "convenience" in a way 
> they apparently did not understand and many of us educated them, hopefully.
> 
> That made me wonder of teh impact on our code when we use various forms
> of convenience. Is it convenient for us as programmers, other potential 
> readers,
> or a compiler or interpreter?

Using aliases can impose a cost for maintenance. It adds a burden on the reader
to figure what an alias really refers to.

I encountered this:

from time import time as now.

The use of now() was just confusing to maintainers.

I avoid the convenience path as it often makes maintenance harder.


> 
> The example used was something like:
> 
> varname = objectname.varname
> 
> The above clearly requires an existing object. But there is no reason it
> has to be the same name. Consider the somewhat related idea used
> almost always in code:
> 
> import numpy as np
> 
> Both cases make a sort of pointer variable and are a tad shorter to write.
> 
> But what impact does it have on interpreters and compilers?

On the positive side aliases are very useful to improve the speed of
hot-path code.

read = this.that.the_other.read
while condition:
buf = read()
...

As with all optimisations only do this if you can justify it with benchmark 
results.

Barry

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