Re: A missing iterator on itertools module?

2024-04-03 Thread Antoon Pardon via Python-list




Op 28/03/2024 om 17:45 schreef ast via Python-list:

Hello

Suppose I have these 3 strings:

s1 = "AZERTY"
s2 = "QSDFGH"
s3 = "WXCVBN"

and I need an itertor who delivers

A Q W Z S C E D C ...

I didn't found anything in itertools to do the job.


The documentation mentions a roundrobin recipe.


So I came up with this solution:


list(chain.from_iterable(zip("AZERTY", "QSDFGH", "WXCVBN")))

['A', 'Q', 'W', 'Z', 'S', 'X', 'E', 'D', 'C', 'R', 'F', 'V', 'T', 'G', 
'B', 'Y', 'H', 'N']


But if your strings are not equal, this will only produce a partial result.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Extract lines from file, add to new files

2024-01-15 Thread Antoon Pardon via Python-list




Op 14/01/2024 om 13:28 schreef Left Right via Python-list:

Python isn't a context-free language, so the grammar that is used to
describe it doesn't actually describe the language... so, it's a
"pretend grammar" that ignores indentation.


No it doesn't. Here is the definition of a block, it clearly mentions
indentation:

block:
|  NEWLINE INDENTstatements  DEDENT
|  simple_stmts But you are correct that python in not a context-free 
language. But so is any programming language. Yet a lot of those non 
context-free language designers thought it helpful to have a 
modified/extended BNF description of a superset of the intended language 
and used other means to further check whether the code in question was 
valid or not. -- Antoon Pardon

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


Re: mypy question

2024-01-12 Thread Antoon Pardon via Python-list

Op 29/12/2023 om 16:02 schreef Karsten Hilbert via Python-list:


Am Fri, Dec 29, 2023 at 07:49:17AM -0700 schrieb Mats Wichmann via Python-list:


I am not sure why mypy thinks this

gmPG2.py:554: error: Argument "queries" to "run_rw_queries" has incompatible type 
"List[Dict[str, str]]"; expected
"List[Dict[str, Union[str, List[Any], Dict[str, Any"  [arg-type]
 rows, idx = run_rw_queries(link_obj = conn, queries = 
queries, return_data = True)
   
^~~

should be flagged. The intent is for "queries" to be

a list
of dicts
with keys of str
and values of
str OR
list of anything OR
dict with
keys of str
and values of anything

I'd have thunk list[dict[str,str]] matches that ?

Dict[str, str] means the key type and value type should both be strings,

Indeed, I know that much, list[dict[str, str]] is what is getting
passed in in this particular invocation of run_rw_queries().

For what it's worth here's the signature of that function:

def run_rw_queries (
link_obj:_TLnkObj=None,
queries:list[dict[str, str | list | dict[str, Any]]]=None,
end_tx:bool=False,
return_data:bool=None,
get_col_idx:bool=False,
verbose:bool=False
) -> tuple[list[dbapi.extras.DictRow], dict[str, int] | None]:

Given that I would have thought that passing in
list[dict[str, str]] for "queries" ought to be type safe.
Mypy indicates otherwise which I am not grokking as to why.


but in your
retelling above you indicate lots of possible value types... actually the mypy 
guess
seems to be a pretty good recreation of your psuedo-code description.

I agree that mypy's grasp of my intent from

queries:list[dict[str, str | list | dict[str, Any]]]=None,

into

"List[Dict[str, Union[str, List[Any], Dict[str, Any"

seems accurate. I just don't understand why list[dict[str,
str]] should not pass that construct.


Sorry for the late reaction and may be I am missing something, but I was 
wondering if
your type hint for queries shouldn't be the following.

queries:list[dict[str,str]|dict[str,list]|dict[str,dict[str, dict[str, Ant]]]

My impression at this moment is that you are write something like: dict[str, 
str | int] as
as shorthand for dict[str, str] | dict[str, int]. But those two are different 
types.

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


Re: making your own DirEntry.

2023-12-23 Thread Antoon Pardon via Python-list




Op 23/12/2023 om 12:34 schreef Barry Scott:



On 23 Dec 2023, at 09:48, Antoon Pardon via Python-list 
 wrote:


Because I have functions with DirEntry parameters.


I would duck-type a class I control to be my DirEnrry in this situation.
Would also help you when debugging as you can tell injected DirEntry 
from "real" DirEntry.



Yes that seems to be, the way to go.

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


Re: making your own DirEntry.

2023-12-23 Thread Antoon Pardon via Python-list

Op 22/12/2023 om 21:39 schreef DL Neil via Python-list:

Antoon,


On 12/23/23 01:00, Antoon Pardon via Python-list wrote:
I am writing a program that goes through file hierarchies and I am 
mostly

using scandir for that which produces DirEntry instances.

At times it would be usefull if I could make my own DirEntry for a 
specific

path, however when I try, I get the following diagnostic:


os.DirEntry('snap')

Traceback (most recent call last):
   File "", line 1, in 
TypeError: cannot create 'posix.DirEntry' instances


Does anyone have an idea for why this limitation and how to go around 
it.


At this moment I don't consider pathlib very usefull, it lacks the
follow_symlinks parameter in the is_dir, is_file, ... methods.



Can't recall ever trying this.


The manual (https://docs.python.org/3/library/os.html#os.DirEntry) 
suggests that a DirEntry is one of those Python data-constructs which 
it creates, but we may only use: "cannot create".


Secondly, that a DirEntry object consists of a lot more than the 
directory-name, eg its path.


Thirdly, that os.scandir() deals (only) with concrete directories - 
unlike pathlib's ability to work with both the real thing and abstract 
files/dirs.



Why create a DirEntry? Why not go directly to os.mkdir() or whatever?


Because I have functions with DirEntry parameters.

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


making your own DirEntry.

2023-12-22 Thread Antoon Pardon via Python-list

I am writing a program that goes through file hierarchies and I am mostly
using scandir for that which produces DirEntry instances.

At times it would be usefull if I could make my own DirEntry for a specific
path, however when I try, I get the following diagnostic:


os.DirEntry('snap')

Traceback (most recent call last):
  File "", line 1, in 
TypeError: cannot create 'posix.DirEntry' instances


Does anyone have an idea for why this limitation and how to go around it.

At this moment I don't consider pathlib very usefull, it lacks the
follow_symlinks parameter in the is_dir, is_file, ... methods.

--
Antoon Pardon.

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


return type same as class gives NameError.

2023-10-22 Thread Antoon Pardon via Python-list

I have the following small module:

=-=-=-=-=-=-=-=-=-=-=-= 8< =-=-=-=-=-=-=-=-=-=-=-=-=

from typing import NamedTuple, TypeAlias, Union
from collections.abc import Sequence

PNT: TypeAlias = tuple[float, float]

class Pnt (NamedTuple):
x: float
y: float

def __add__(self, other: PNT) -> Pnt:
return Pnt(self[0] + other[0], self[1] + other[1])

=-=-=-=-=-=-=-=-=-=-=-= >8 =-=-=-=-=-=-=-=-=-=-=-=-=

But when I import this, I get the following diagnostic:

Traceback (most recent call last):
  File "", line 1, in 
  File "/home/sisc/projecten/iudex/problem.py", line 10, in 
class Pnt (NamedTuple):
  File "/home/sisc/projecten/iudex/problem.py", line 14, in Pnt
def __add__(self, other: PNT) -> Pnt:
 ^^^
NameError: name 'Pnt' is not defined. Did you mean: 'PNT'?


Can someone explain what I am doing wrong?
--
https://mail.python.org/mailman/listinfo/python-list


Re: Evaluation of variable as f-string

2023-01-25 Thread Antoon Pardon

Op 23/01/2023 om 17:24 schreef Johannes Bauer:

Hi there,

is there an easy way to evaluate a string stored in a variable as if 
it were an f-string at runtime?


I.e., what I want is to be able to do this:

x = { "y": "z" }
print(f"-> {x['y']}")

This prints "-> z", as expected. But consider:

x = { "y": "z" }
s = "-> {x['y']}"
print(s.format(x = x))
Traceback (most recent call last):
  File "", line 1, in 
KeyError: "'y'"

Even though

s = "-> {x}"
print(s.format(x = x))

Prints the expected "-> {'y': 'z'}".

I am probably missing something but is there a reason why the following 
wouldn't do what you want:


x = { "y": "z" }
s = "-> {target}"
print(s.format(target = x['y']))
--
https://mail.python.org/mailman/listinfo/python-list


Re: How make your module substitute a python stdlib module.

2022-12-28 Thread Antoon Pardon

Op 27/12/2022 om 16:49 schreef Thomas Passin:

On 12/27/2022 8:25 AM, Antoon Pardon wrote:



Op 27/12/2022 om 13:46 schreef Chris Angelico:
On Tue, 27 Dec 2022 at 23:28, Antoon Pardon  
wrote:

At the moment I am happy with a solution that once the programmer has
imported from QYZlib.threaders that module will used as the threading
module.


Oh! If that's all you need, then yes, a simple patch of sys.modules
will work. You'll have to make sure you get your module imported
before anything else pulls up the vanilla one, though. Otherwise, your
only option is to keep the existing module object and monkeypatch it
with whatever changes you need.

But a simple "sys.modules['threading'] = QYZlib.threaders" will work.
Of course, how *well* this works depends also on how well that module
manages to masquerade as the threading module, but I'm sure you've
figured that part out :)


Well it is what will work for the moment. Thanks for the confirmation
this will indeed work.



What you **should not** do is to make other modules use your code when 
they think they are using the standard library. Raymond Chen in The 
Old New Thing blog often writes about users who want to do a basically 
similar thing. He always asks "What if some other program wants to do 
the same thing?"  One case was when a developer wanted his program's 
icon to force itself to be at the top position in the Windows task 
bar. "What if another program tried this too?"  The two would keep 
trying to displace each other at the top position.


If you set the PYTHONPATH environmental variable, you can put any 
directory you like to be at the start of the module search path after 
the current directory.  Perhaps this would be enough for your use case?


But if I put a threading.py file in that directory, it would make other 
modules use my code when they "think" they are using the standard library.


The problem is that the logging module uses threading, but doesn't allow 
you to specify which threading module to use. So if you want to use an 
alternative, you either have to do things like this, or copy the logging 
packages into your project and adapt it to use your alternative.



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


Re: How make your module substitute a python stdlib module.

2022-12-27 Thread Antoon Pardon




Op 27/12/2022 om 13:46 schreef Chris Angelico:

On Tue, 27 Dec 2022 at 23:28, Antoon Pardon  wrote:

At the moment I am happy with a solution that once the programmer has
imported from QYZlib.threaders that module will used as the threading
module.


Oh! If that's all you need, then yes, a simple patch of sys.modules
will work. You'll have to make sure you get your module imported
before anything else pulls up the vanilla one, though. Otherwise, your
only option is to keep the existing module object and monkeypatch it
with whatever changes you need.

But a simple "sys.modules['threading'] = QYZlib.threaders" will work.
Of course, how *well* this works depends also on how well that module
manages to masquerade as the threading module, but I'm sure you've
figured that part out :)


Well it is what will work for the moment. Thanks for the confirmation
this will indeed work.

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


Re: How make your module substitute a python stdlib module.

2022-12-27 Thread Antoon Pardon




Op 27/12/2022 om 13:09 schreef Chris Angelico:

On Tue, 27 Dec 2022 at 23:06, Antoon Pardon  wrote:

How do you intend to distinguish one from the other? How should the
logging module know which threading module to use?

That is my question! How can I get the logging module to use my module.I was 
hoping the logging module would allow some kind of dependency
injection, so you can tell it what threading module to use. An other
option might be to manipulate sys.modules. -- Antoon Pardon

But presumably you want OTHER modules to continue using the vanilla
threading module? This is likely to end up somewhat hacky. Yes, you
can manipulate sys.modules, but there's only one threading module at a
time, so you need a way to distinguish between modules that should use
the changed one and modules that shouldn't.


But don't these caveats also apply with your original answer of having a
local threading.py file?

At the moment I am happy with a solution that once the programmer has
imported from QYZlib.threaders that module will used as the threading
module.

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


Re: How make your module substitute a python stdlib module.

2022-12-27 Thread Antoon Pardon




Op 27/12/2022 om 12:28 schreef Chris Angelico:

On Tue, 27 Dec 2022 at 22:13, Antoon Pardon  wrote:



Op 27/12/2022 om 11:37 schreef Chris Angelico:

On Tue, 27 Dec 2022 at 21:29, Antoon Pardon  wrote:

OK, I am writing an alternative for the threading module. What I would
like to know is how I can get some library modules call my alternative
instead of the threading module.

For instance there is the logging module, it can log the thread name. So
I would like to know how I can get the logging module to call the
function from my module to get the current_thread, instead of it calling
"current_thread" from the threading module.

Easy: make sure your module is called "threading.py" and is earlier in
the path than the standard library. In fact, it's so easy that people
do it unintentionally all the time... Generally, the current directory
(or the script directory) is the first entry in sys.path, so that's a
good place to put it.

Well I had hope for a somewhat more selective solution. The intention is
to have a number of modules collected in a package where this module is
one of and the package is available via the "installed" search path.

So the programmer should just be able to choose to write his code with
either:

  from threading import Thread

or

  from QYZlib.threaders import Thread


How do you intend to distinguish one from the other? How should the
logging module know which threading module to use?


That is my question! How can I get the logging module to use my module.I was hoping the logging module would allow some kind of dependency 
injection, so you can tell it what threading module to use. An other 
option might be to manipulate sys.modules. -- Antoon Pardon

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


Re: How make your module substitute a python stdlib module.

2022-12-27 Thread Antoon Pardon



Op 27/12/2022 om 11:37 schreef Chris Angelico:

On Tue, 27 Dec 2022 at 21:29, Antoon Pardon  wrote:

OK, I am writing an alternative for the threading module. What I would
like to know is how I can get some library modules call my alternative
instead of the threading module.

For instance there is the logging module, it can log the thread name. So
I would like to know how I can get the logging module to call the
function from my module to get the current_thread, instead of it calling
"current_thread" from the threading module.

Easy: make sure your module is called "threading.py" and is earlier in
the path than the standard library. In fact, it's so easy that people
do it unintentionally all the time... Generally, the current directory
(or the script directory) is the first entry in sys.path, so that's a
good place to put it.


Well I had hope for a somewhat more selective solution. The intention is
to have a number of modules collected in a package where this module is
one of and the package is available via the "installed" search path.

So the programmer should just be able to choose to write his code with
either:

    from threading import Thread

or

    from QYZlib.threaders import Thread

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


How make your module substitute a python stdlib module.

2022-12-27 Thread Antoon Pardon
OK, I am writing an alternative for the threading module. What I would 
like to know is how I can get some library modules call my alternative 
instead of the threading module.


For instance there is the logging module, it can log the thread name. So 
I would like to know how I can get the logging module to call the 
function from my module to get the current_thread, instead of it calling 
"current_thread" from the threading module.


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


Re: Does one have to use curses to read single characters from keyboard?

2022-12-11 Thread Antoon Pardon




Op 11/12/2022 om 12:32 schreef Stefan Ram:

r...@zedat.fu-berlin.de (Stefan Ram) writes:

Curses is not portable IIRC. A more portable means would
be to use tkinter with the "bind" function to bind keys.

import tkinter

text = tkinter.Text()
text.pack()
text.bind\
( "",
   lambda event:
   text.insert
   ( tkinter.END, "Y\nFormatting drive C:\n...\n" )or "break" )
text.insert( tkinter.END, "Format drive C:?\n" )
text.focus()

tkinter.mainloop()

   Not allowing users to edit their keypresses before confirming
   them with [Return]. What could possibly go wrong?


Nothing that can't go wrong otherwise. It is my experience that
when a [Return] is needed, people just type in a two key combination.
They don't type one key, then check, then type [Return].

So in practice the same things go wrong, either way.

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


Re: Optional arguments in a class behave like class attributes.

2022-10-17 Thread Antoon Pardon

You can use the following decorator for what you probably want.

def copy_defaults(func):
"""
This decorator makes that defaults values are copied on a call.
"""

signature = inspect.signature(func)
parameter_items = list(signature.parameters.items())

@wraps(func)
def wrapper(*args, **kwds):
newargs = list(args)
tail_parameters = parameter_items[len(args):]
for name, parameter in tail_parameters:
try:
newargs.append(kwds[name])
del kwds[name]
except KeyError:
if parameter.default is not Parameter.empty:
newargs.append(copy.deepcopy(parameter.default))
else:
break
return func(*newargs, **kwds)

return wrapper

class GameOne:
  @copy_defaults
  def __init__(self, games = []) -> None:
self.games = games

# Results I got after using this decorator in your code

Using a list []
Using a dictionary {}
Using an object []

Op 16/10/2022 om 12:48 schreef Abderrahim Adrabi:

Hi all,

I tried to create a class with some optional arguments as always, but this
time I used the default values to be lists, dictionaries, and object
references.

So, these default values behave like class attributes, here is a demo:

# Using a list -
class GameOne:
   def __init__(self, games = []) -> None:
 self.games = games

h = GameOne()
h.games.append("List, the first round")

g = GameOne()
g.games.append("List, the second round")

k = GameOne()
print('Using a list', k.games)

# Using a dictionary --
class GameTwo:
   def __init__(self, games = {}) -> None:
 self.games = games

h = GameTwo()
h.games['1er'] = "Dictionary, the first round"

g = GameTwo()
g.games['2ed'] = "Dictionary, the second round"

k = GameTwo()
print('Using a dictionary', k.games)

# Using an object --
class Bonus:
   def __init__(self) -> None:
 self.stages = []

class GameThree:
   def __init__(self, bonus = Bonus()) -> None:
 self.bonus = bonus

h = GameThree()
h.bonus.stages.append('Object, the first round')

g = GameThree()
g.bonus.stages.append('Object, the second round')

k = GameThree()
print('Using an object', k.bonus.stages)

# Results 

Using a list ['List, the first round', 'List, the second round']
Using a dictionary {'1er': 'Dictionary, the first round', '2ed':
'Dictionary, the second round'}
Using an object ['Object, the first round', 'Object, the second round']

# Used Python versions ---

3.5.1 (default, Dec  9 2015, 14:41:32)
[GCC 5.2.0]

3.7.14 (default, Sep  8 2022, 00:06:44)
[GCC 7.5.0]

3.8.6 (default, Jan 29 2021, 17:38:16)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]

3.9.9 (main, Nov 20 2021, 21:30:06)
[GCC 11.1.0]

My question: Is this normal behavior?

Thanks.

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


Re: for -- else: what was the motivation?

2022-10-16 Thread Antoon Pardon




Op 17/10/2022 om 04:01 schreef Chris Angelico:

On Mon, 17 Oct 2022 at 10:46,  wrote:

My point Chris was that you can have a conversation where you are exploring
and not proposing. Brainstorming, perhaps.

And my point is that either a proposal is a serious one that can
expect serious discussion, or it isn't. Yes, I'm aware that it wasn't
you who backpedalled as soon as any criticism was posted, but your
caveat comes to the same thing - if you're trying to avoid serious
criticism, you have to not post an idea.


Your reaction was not serious criticisme but a belligerent reaction.
You made it clear you wanted a fight. I choose not to enter that
fight.

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


Re: for -- else: what was the motivation?

2022-10-16 Thread Antoon Pardon



Op 16/10/2022 om 19:01 schreef Peter J. Holzer:

On 2022-10-16 12:17:39 +0200, Antoon Pardon wrote:

Op 16/10/2022 om 00:50 schreefavi.e.gr...@gmail.com:

That might not easily solve this problem. But I wonder if reserving
some kind of prefix might help, so anything like extension.0nNoBreak
could be added to a loop as a final clause and be treated as a
non-key keyword of sorts.

My idea would be to reserve different unicode blocks for the keywords and
the identifiers. e.g. We could reserve the mathematical alphanumeric block
for keywords and all other letters and numbers for identifiers. Doing so
would allow extenting the keywords without breaking programs that already
use that combination as an identifier.

Or you could go back to the old Algol idea of underlining keywords.

Both have the same problem, though: They make editing with an ordinary
text editor pretty much impossible. You need a language-aware IDE which
makes entering these characters or markups simple.


That is not true with my idea. The editor would just need to be able
to work with unicode characters. Vim can work with 퐝퐞퐟 just as it can
with def.

In vim I can also define abbreviations so that whenever I type ^Bd vim
wil insert 퐝퐞퐟 for me.

It may be useful to have an alternative keyboard definition ready to
easily insert these characters into the editor but ordinary editors
should already be able to treat these characters as any other unicode
character.

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


Re: for -- else: what was the motivation?

2022-10-16 Thread Antoon Pardon

Op 16/10/2022 om 17:03 schreef Avi Gross:

Interesting idea, Anton.

I would be interested in hearing more detail on how it would work.

Although much of programming has been centered on the Latin alphabet 
and especially English, that may change. I can imagine a customized 
compiler or interpreter that uses key words in the local language 
instead of for or while or if or else or even import.


If a region of UNICODE was set aside, would it have to be as a sort of 
additional ALT or shift key for anything, or just English characters 
or would it be for abstract symbols that would be mapped to and from a 
long list of reserved key words that may vary by locale?


I think you are carrying my idea further than I intended. I was just 
thinking that instead of using U+0064 U+0065 U+0066 [beinf def] we could 
be using U+1D41D U+1D41E U+1D41F [being  퐝퐞퐟].


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


Re: for -- else: what was the motivation?

2022-10-16 Thread Antoon Pardon




Op 16/10/2022 om 19:03 schreef Chris Angelico:

On Mon, 17 Oct 2022 at 03:57, Antoon Pardon  wrote:


Op 16/10/2022 om 17:05 schreef Chris Angelico:

On Sun, 16 Oct 2022 at 22:47, Antoon Pardon   wrote:

Why would I need good luck? I expressed an idea and you didn't like it.
That won't affect my life in a meaningful way.

Well, with that attitude, it's not going to affect anyone else's life
either, so go ahead, carry on.

What attitude? I just floated a little idea. It was not meant/expected to
affect anyone else's life. So why do you react as if it was?


You expressed an idea that you would like to see implemented in
Python, and part of that idea was that people would be *obliged* to
write their code using non-ASCII keywords. If that were to be
implemented, it absolutely WOULD affect many people's lives.


So what? I made it clear that was an if I didn't expect to happen.
An idea that doesn't happen will not affect anyone, whatever the
consequences tied to the idea.


  So if
you're saying that your idea was not meant to affect anyone else's
life, you are saying that you floated the idea fully intending for it
to be ignored, which makes me wonder why you posted it in the first
place.


People can find it interesting to discuss an idea, even if they think
there is no chance the idea will be carried out.

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


Re: for -- else: what was the motivation?

2022-10-16 Thread Antoon Pardon



Op 16/10/2022 om 17:05 schreef Chris Angelico:

On Sun, 16 Oct 2022 at 22:47, Antoon Pardon  wrote:

Why would I need good luck? I expressed an idea and you didn't like it.
That won't affect my life in a meaningful way.

Well, with that attitude, it's not going to affect anyone else's life
either, so go ahead, carry on.


What attitude? I just floated a little idea. It was not meant/expected to
affect anyone else's life. So why do you react as if it was?

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


Re: for -- else: what was the motivation?

2022-10-16 Thread Antoon Pardon




Op 16/10/2022 om 13:03 schreef Chris Angelico:

On Sun, 16 Oct 2022 at 21:19, Antoon Pardon  wrote:


My idea would be to reserve different unicode blocks for the keywords
and the identifiers. e.g. We could reserve the mathematical alphanumeric
block for keywords and all other letters and numbers for identifiers.
Doing so would allow extenting the keywords without breaking programs
that already use that combination as an identifier.

Python currently defines identifiers as follows:
https://docs.python.org/3/reference/lexical_analysis.html#identifiers

Briefly, what it means is that (aside from some backward compatibility
special cases) an identifier contains letters, numbers, and connector
punctuation, and must not start with a number. It's not by blocks,
it's by types.

It's way WAY too late to change what's allowed for identifiers, as you
will potentially be breaking programs that use the current rules.


So? Python has broken backward compatibility before. The cost could
be acceptable. How many programs do you estimated use the mathematical
alphanumeric block for an identifier at this moment?


Python could slowly transition in this direction by first allowing the
current keywords to be in this block. Every new keyword would only be in
that unicode block. If would then be possible to write python code with
this convention but it wouldn't be obligatory. After some time the
python developers could decide to make it obligatory.

Obligatory??? Please explain how you intend to convince the entire
world that non-ASCII code is an acceptable requirement. Explain to me
how you're going to go to every text editor and ensure that it
supports easy entry of Python keywords that aren't ASCII. And please
explain how this is even better.


Why should I do that? It seems you have already made your mind up.
That is fine. It just makes explaining not very senseful.


I doubt this will idea will get from the ground, but I think it would
allow for a smoother transition into new concepts, as it is no longer a
strugle searching for a keyword that will break as little programs as
possible.

Yeah it won't. Good luck though.


Why would I need good luck? I expressed an idea and you didn't like it.
That won't affect my life in a meaningful way.

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


Re: for -- else: what was the motivation?

2022-10-16 Thread Antoon Pardon

Op 16/10/2022 om 00:50 schreef avi.e.gr...@gmail.com:

This has been discussed so often precisely because I swear NO CHOICE of keyword 
would satisfy everybody! Most languages start with designated keywords and some 
reserve a few for later use. But then things can get frozen in place to avoid 
breaking existing programs or break older compilers/interpreters.

Some languages use techniques to extend themselves more harmlessly such as creating a 
singleton object that has content that can be regular data as in math.pi, or 
functions/methods or new ides like "Symbols" that allow all kinds of extensions 
to the language in a fairly harmless way as no older program would likely have used 
features that did not exist.

That might not easily solve this problem. But I wonder if reserving some kind 
of prefix might help, so anything like extension.0nNoBreak could be added to a 
loop as a final clause and be treated as a non-key keyword of sorts.


My idea would be to reserve different unicode blocks for the keywords 
and the identifiers. e.g. We could reserve the mathematical alphanumeric 
block for keywords and all other letters and numbers for identifiers. 
Doing so would allow extenting the keywords without breaking programs 
that already use that combination as an identifier.


Python could slowly transition in this direction by first allowing the 
current keywords to be in this block. Every new keyword would only be in 
that unicode block. If would then be possible to write python code with 
this convention but it wouldn't be obligatory. After some time the 
python developers could decide to make it obligatory.


I doubt this will idea will get from the ground, but I think it would 
allow for a smoother transition into new concepts, as it is no longer a 
strugle searching for a keyword that will break as little programs as 
possible.


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


Re: Find the path of a shell command

2022-10-12 Thread Antoon Pardon



Op 12/10/2022 om 18:49 schreef Paulo da Silva:

Às 05:00 de 12/10/22, Paulo da Silva escreveu:

Hi!

The simple question: How do I find the full path of a shell command 
(linux), i.e. how do I obtain the corresponding of, for example,

"type rm" in command line?

The reason:
I have python program that launches a detached rm. It works pretty 
well until it is invoked by cron! I suspect that for cron we need to 
specify the full path.
Of course I can hardcode /usr/bin/rm. But, is rm always in /usr/bin? 
What about other commands?



Thank you all who have responded so far.
I think that the the suggestion of searching the PATH env seems the best.


I fear that won't work.

If it doesn't work in cron, that probably means, PATH is not set 
properly in your cron environment. And if PATH is not set properly, 
searching it explicitely won't work either.


If rm works when evoked directly in a cron script but not via a python 
script, you may need to export the PATH in your cron script.


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


Re: flattening lists

2022-10-12 Thread Antoon Pardon




Op 11/10/2022 om 21:32 schreef SquidBits _:

Does anyone else think there should be a flatten () function, which just turns 
a multi-dimensional list into a one-dimensional list in the order it's in. e.g.

[[1,2,3],[4,5,6,7],[8,9]] becomes [1,2,3,4,5,6,7,8,9].

I have had to flatten lists quite a few times and it's quite tedious to type 
out. It feels like this should be something built in to python, anyone else 
think this way?


Depending on what you exactly mean by "flatten", it already is easy to 
flatten a list in python:


>>> lst = [[1,2,3],[4,5,6,7],[8,9]]
>>> list(itertools.chain.from_iterable(lst))
[1, 2, 3, 4, 5, 6, 7, 8, 9]

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


Re: for -- else: what was the motivation?

2022-10-11 Thread Antoon Pardon




Op 10/10/2022 om 04:38 schreef avi.e.gr...@gmail.com:

[This is an answer for Peter and can easily be skipped by those who know or
have no wish to.]

Strictly speaking Peter, the word "pipe" may not mean quite something in
Python but other concepts like chaining may be better.

The original use of the word I am used to was at the UNIX shell level where
an in-core data structure called a pipe was used to connect the output of
one process to the inputr of another, sometimes in long chains like:

  cat file1 file2 file3 | grep pattern | ... | lp


Something like that can be done in python with the same kind of syntax:

https://code.activestate.com/recipes/580625-collection-pipeline-in-python/

I have my own python3 module with stuff like that and I find it very usefull.

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


Re: What to use for finding as many syntax errors as possible.

2022-10-11 Thread Antoon Pardon




Op 10/10/2022 om 19:08 schreef Robert Latest via Python-list:

Antoon Pardon wrote:

I would like a tool that tries to find as many syntax errors as possible
in a python file.

I'm puzzled as to when such a tool would be needed. How many syntax errors can
you realistically put into a single Python file before compiling it for the
first time?


Why are you puzzled? I don't need to make that many syntaxt errors to find
such a tool useful.

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


Re: What to use for finding as many syntax errors as possible.

2022-10-10 Thread Antoon Pardon



Op 10/10/2022 om 00:45 schreef Cameron Simpson:

On 09Oct2022 21:46, Antoon Pardon  wrote:
Is it that onerous to fix one thing and run it again? It was once 
when you

handed in punch cards and waited a day or on very busy machines.


Yes I find it onerous, especially since I have a pipeline with unit 
tests
and other tools that all have to redo their work each time a bug is 
corrected.


It is easy to get the syntax right before submitting to such a 
pipeline.  I usually run a linter on my code for serious commits, and 
I've got a `lint1` alias which basicly runs the short fast flavour of 
that which does a syntax check and the very fast less thorough lint phase.


If you have a linter that doesn't quit after the first syntax error, 
please provide a link. I already tried pylint and it also quits after 
the first syntax error.


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


Re: What to use for finding as many syntax errors as possible.

2022-10-09 Thread Antoon Pardon




Op 9/10/2022 om 21:44 schreef Avi Gross:

But an error like setting the size of a fixed length data structure to the
right size may result in oodles of errors about being out of range that
magically get fixed by one change. Sometimes too much info just gives you a
headache.


So? The user of such a tool doesn't need to go through all the provided 
information.
If after correcting a few errors, the users find the rest of the information 
gives
him a headache, he can just ignore all that and just run a new iteration.

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


Re: What to use for finding as many syntax errors as possible.

2022-10-09 Thread Antoon Pardon




Op 9/10/2022 om 21:18 schreef Avi Gross:

Antoon,  it may also relate to an interpreter versus compiler issue.

Something like a compiler for C does not do anything except write code in
an assembly language. It can choose to keep going after an error and start
looking some more from a less stable place.

Interpreters for Python have to catch interrupts as they go and often run
code in small batches. Continuing to evaluate after an error could cause
weird effects.

So what you want is closer to a lint program that does not run code at all,
or merely writes pseudocode to a file to be run faster later.


I just want a parser that doesn't give up on encoutering the first syntax
error. Maybe do some semantic checking like checking the number of parameters.


I will say that often enough a program could report more possible errors.
Putting your code into multiple files and modules may mean you could
cleanly evaluate the code and return multiple errors from many modules as
long as they are distinct. Finding all errors is not possible if recovery
from one is not guaranteed.


I don't need it to find all errors. As long as it reasonably accuratly
finds a significant number of them.


Is it that onerous to fix one thing and run it again? It was once when you
handed in punch cards and waited a day or on very busy machines.


Yes I find it onerous, especially since I have a pipeline with unit tests
and other tools that all have to redo their work each time a bug is corrected.

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


Re: What to use for finding as many syntax errors as possible.

2022-10-09 Thread Antoon Pardon




Op 9/10/2022 om 21:18 schreef Avi Gross:

Antoon,  it may also relate to an interpreter versus compiler issue.

Something like a compiler for C does not do anything except write code in
an assembly language. It can choose to keep going after an error and start
looking some more from a less stable place.

Interpreters for Python have to catch interrupts as they go and often run
code in small batches. Continuing to evaluate after an error could cause
weird effects.

So what you want is closer to a lint program that does not run code at all,
or merely writes pseudocode to a file to be run faster later.


I just want a parser that doesn't give up on encoutering the first syntax
error. Maybe do some semantic checking like checking the number of parameters.


I will say that often enough a program could report more possible errors.
Putting your code into multiple files and modules may mean you could
cleanly evaluate the code and return multiple errors from many modules as
long as they are distinct. Finding all errors is not possible if recovery
from one is not guaranteed.


I don't need it to find all errors. As long as it reasonably accuratly
finds a significant number of them.


Is it that onerous to fix one thing and run it again? It was once when you
handed in punch cards and waited a day or on very busy machines.


Yes I find it onerous, especially since I have a pipeline with unit tests
and other tools that all have to redo their work each time a bug is corrected.

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


Re: What to use for finding as many syntax errors as possible.

2022-10-09 Thread Antoon Pardon




Op 9/10/2022 om 19:23 schreef Karsten Hilbert:

Am Sun, Oct 09, 2022 at 06:59:36PM +0200 schrieb Antoon Pardon:


Op 9/10/2022 om 17:49 schreef Avi Gross:

My guess is that finding 100 errors might turn out to be misleading. If you
fix just the first, many others would go away.

At this moment I would prefer a tool that reported 100 errors, which would
allow me to easily correct 10 real errors, over the python strategy which quits
after having found one syntax error.

But the point is: you can't (there is no way to) be sure the
9+ errors really are errors.

Unless you further constrict what sorts of errors you are
looking for and what margin of error or leeway for false
positives you want to allow.


Look when I was at the university we had to program in Pascal and
the compilor we used continued parsing until the end. Sure there
were times that after a number of reported errors the number of
false positives became so high it was useless trying to find the
remaining true ones, but it still was more efficient to correct the
obvious ones, than to only correct the first one.

I don't need to be sure. Even the occasional wrong correction
is probably still more efficient than quiting after the first
syntax error.

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


Re: What to use for finding as many syntax errors as possible.

2022-10-09 Thread Antoon Pardon




Op 9/10/2022 om 17:49 schreef Avi Gross:

My guess is that finding 100 errors might turn out to be misleading. If you
fix just the first, many others would go away.


At this moment I would prefer a tool that reported 100 errors, which would
allow me to easily correct 10 real errors, over the python strategy which quits
after having found one syntax error.

--
Antoon.

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


What to use for finding as many syntax errors as possible.

2022-10-09 Thread Antoon Pardon
I would like a tool that tries to find as many syntax errors as possible 
in a python file. I know there is the risk of false positives when a 
tool tries to recover from a syntax error and proceeds but I would 
prefer that over the current python strategy of quiting after the first 
syntax error. I just want a tool for syntax errors. No style 
enforcements. Any recommandations? -- Antoon Pardon

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


Re: How to make a variable's late binding crosses the module boundary?

2022-08-31 Thread Antoon Pardon



Op 31/08/2022 om 09:53 schreef dn:

On 31/08/2022 19.38, Antoon Pardon wrote:


Op 30/08/2022 om 23:52 schreef dn:

The conversation seems to be wandering some way from the OP. Whereas
both of these answers are clever (and I assume work), the question
becomes: why would you want to do this? (especially as it looks ugly and
'smells' a bit convoluted). An example use-case from your experience?


Delving into the list-archive, to get back to the OP: the first message
in the thread quotes another message that's (apparently) not present.

However, in there somewhere is:


from test import *

So, the elephant-in-the-room has always been a very stinky 'code-smell'
- which pretty much every text or web-tutorial will say is a bad idea.

No that is a red herring. If for some reason a variable in a module has
to be (re)set after import time, it doesn't matter whether someone uses

     from module import *

or

     from module import resetable_variable

in both cases the module that did the import will not notice the change
in the original module.

focussing on the code smell, is leading the attention away from the
problem.

Are you the OP?

The behavior has been shown to be exactly that which is (should be)
expected.


That doesn't contradict that the behaviour is a problem. What is expected
(viewed as a language specification) and what is desired (viewed from what
would solve the problem easily) can be at odds.

--
Antoon Pardon.

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


Re: How to make a variable's late binding crosses the module boundary?

2022-08-31 Thread Antoon Pardon




Op 30/08/2022 om 23:52 schreef dn:

The conversation seems to be wandering some way from the OP. Whereas
both of these answers are clever (and I assume work), the question
becomes: why would you want to do this? (especially as it looks ugly and
'smells' a bit convoluted). An example use-case from your experience?


Delving into the list-archive, to get back to the OP: the first message
in the thread quotes another message that's (apparently) not present.

However, in there somewhere is:


from test import *

So, the elephant-in-the-room has always been a very stinky 'code-smell'
- which pretty much every text or web-tutorial will say is a bad idea.


No that is a red herring. If for some reason a variable in a module has
to be (re)set after import time, it doesn't matter whether someone uses

from module import *

or

from module import resetable_variable

in both cases the module that did the import will not notice the change
in the original module.

focussing on the code smell, is leading the attention away from the problem.

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


Re: Exclude 'None' from list comprehension of dicts

2022-08-16 Thread Antoon Pardon



Op 16/08/2022 om 00:20 schreef dn:

On 16/08/2022 00.56, Antoon Pardon wrote:

Op 5/08/2022 om 07:50 schreef Loris Bennett:

Antoon Pardon   writes:


Op 4/08/2022 om 13:51 schreef Loris Bennett:

Hi,

I am constructing a list of dictionaries via the following list
comprehension:

     data = [get_job_efficiency_dict(job_id) for job_id in job_ids]

However,

     get_job_efficiency_dict(job_id)

uses 'subprocess.Popen' to run an external program and this can fail.
In this case, the dict should just be omitted from 'data'.

I can have 'get_job_efficiency_dict' return 'None' and then run

     filtered_data = list(filter(None, data))

but is there a more elegant way?

Just wondering, why don't you return an empty dictionary in case of a
failure?
In that case your list will be all dictionaries and empty ones will
be processed
fast enough.

When the list of dictionaries is processed, I would have to check each
element to see if it is empty.  That strikes me as being less efficient
than filtering out the empty dictionaries in one go, although obviously
one would need to benchmark that.

I may be missing something but why would you have to check each element
to see if it is empty? What would go wrong if you just treated empty
dictionaries the same as non-empty directories?

'Truthiness':-


In what way is that relevant in this case?

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


Re: Exclude 'None' from list comprehension of dicts

2022-08-15 Thread Antoon Pardon

Op 5/08/2022 om 07:50 schreef Loris Bennett:

Antoon Pardon  writes:


Op 4/08/2022 om 13:51 schreef Loris Bennett:

Hi,

I am constructing a list of dictionaries via the following list
comprehension:

data = [get_job_efficiency_dict(job_id) for job_id in job_ids]

However,

get_job_efficiency_dict(job_id)

uses 'subprocess.Popen' to run an external program and this can fail.
In this case, the dict should just be omitted from 'data'.

I can have 'get_job_efficiency_dict' return 'None' and then run

filtered_data = list(filter(None, data))

but is there a more elegant way?

Just wondering, why don't you return an empty dictionary in case of a failure?
In that case your list will be all dictionaries and empty ones will be processed
fast enough.

When the list of dictionaries is processed, I would have to check each
element to see if it is empty.  That strikes me as being less efficient
than filtering out the empty dictionaries in one go, although obviously
one would need to benchmark that.


I may be missing something but why would you have to check each element
to see if it is empty? What would go wrong if you just treated empty
dictionaries the same as non-empty directories?

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


Re: Exclude 'None' from list comprehension of dicts

2022-08-04 Thread Antoon Pardon

Op 4/08/2022 om 13:51 schreef Loris Bennett:

Hi,

I am constructing a list of dictionaries via the following list
comprehension:

   data = [get_job_efficiency_dict(job_id) for job_id in job_ids]

However,

   get_job_efficiency_dict(job_id)

uses 'subprocess.Popen' to run an external program and this can fail.
In this case, the dict should just be omitted from 'data'.

I can have 'get_job_efficiency_dict' return 'None' and then run

   filtered_data = list(filter(None, data))

but is there a more elegant way?


Just wondering, why don't you return an empty dictionary in case of a failure?
In that case your list will be all dictionaries and empty ones will be processed
fast enough.

--
Antoon Pardon.

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


Re: My first attempt at a package.

2022-07-26 Thread Antoon Pardon

Op 25/07/2022 om 16:43 schreef Dennis Lee Bieber:

On Mon, 25 Jul 2022 10:39:46 +0200, Antoon Pardon 
declaimed the following:


Yes it is, but it doesn't answer my question: How do I create a package
in which a file is built at install time.
I just want to build a configuration file that will among some other
info contain the date the package
was installed. The idea is that you can execute python3 -m
.release to know what version
of the package you installed and when you installed it. But seem unable
to find a way to do this.

Does
https://stackoverflow.com/questions/72320778/autostart-installed-package-with-python
provide any hints?


Yes it does, thanks.

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


Re: My first attempt at a package.

2022-07-25 Thread Antoon Pardon



Op 19/07/2022 om 16:57 schreef David Lowry-Duda:

On Tue, Jul 19, 2022 at 03:58:41PM +0200, Antoon Pardon wrote:

I am writing a python package which has the following structure

PACKAGE
   * module1.py
   * module2.py
   * data.cfg

However the data.cfg should be build at installation time.

Can someone give advise on which packaging tool and how
to use it, to accomplisch this.


A lot of good packaging information can be found at

https://packaging.python.org/en/latest/

and in particular at

https://packaging.python.org/en/latest/tutorials/packaging-projects/

and

https://packaging.python.org/en/latest/overview/

There are a couple of different ways to handle python packaging, and 
it can be a bit confusing. But following the tutorial on packaging is 
a good start.




Yes it is, but it doesn't answer my question: How do I create a package 
in which a file is built at install time.
I just want to build a configuration file that will among some other 
info contain the date the package
was installed. The idea is that you can execute python3 -m 
.release to know what version
of the package you installed and when you installed it. But seem unable 
to find a way to do this.

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


My first attempt at a package.

2022-07-19 Thread Antoon Pardon

I am writing a python package which has the following structure

PACKAGE
   * module1.py
   * module2.py
   * data.cfg

However the data.cfg should be build at installation time.

Can someone give advise on which packaging tool and how
to use it, to accomplisch this.

e.g. de data config.cfg should be build with the following linix command:

$ date '+installed on %Y:%M:%d at %T' > data.cfg

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


Re: Creating lambdas inside generator expression

2022-06-29 Thread Antoon Pardon

Or you could try this as an alternative:

conds = [ (lambda code: lambda msg: msg.hascode(code))(z) for z in 
("foo", "bar") ]



Op 29/06/2022 om 12:43 schreef Johannes Bauer:

Aha!

conds = [ lambda msg, z = z: msg.hascode(z) for z in ("foo", "bar") ]

Is what I was looking for to explicitly use the value of z. What a
caveat, didn't see that coming.

Learning something new every day.

Cheers,
Joe


Am 29.06.22 um 11:50 schrieb Johannes Bauer:

Hi list,

I've just encounted something that I found extremely unintuitive and
would like your feedback. This bit me *hard*, causing me to question my
sanity for a moment. Consider this minimal example code (Py 3.10.4 on
Linux x64):


class Msg():
def hascode(self, value):
print("Check for", value)
return False

conds = [
lambda msg: msg.hascode("foo"),
lambda msg: msg.hascode("bar"),
]

msg = Msg()
print(conds[0](msg))
print(conds[1](msg))



It works perfectly and does exactly what it looks like. The output is:

Check for foo
False
Check for bar
False

But now consider what happens when we create the lambdas inside a list
comprehension (in my original I used a generator expresison, but the
result is the same). Can you guess what happens when we create conds
like this?

conds = [ lambda msg: msg.hascode(z) for z in ("foo", "bar") ]

I certainly could not. Here's what it outputs:

Check for bar
False
Check for bar
False

I.e., the iteration variable "z" somehow gets bound inside the lambda
not by its value, but by its reference. All checks therefore refence
only the last variable.

This totally blew my mind. I can understand why it's happening, but is
this the behavior we would expect? And how can I create lambdas inside a
generator expression and tell the expression to use the *value* and not
pass the "z" variable by reference?

Cheers,
Joe


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


Re: min, max with position

2022-06-05 Thread Antoon Pardon




Op 5/06/2022 om 01:52 schreef Greg Ewing:

On 5/06/22 10:07 am, dn wrote:

On 05/06/2022 09.50, Chris Angelico wrote:

min(enumerate(l), key=lambda x: x[1])

(0, 1.618033)


But, but, but which of the above characters is an 'el' and which a 
'one'???

(please have pity on us old f...s and the visually-challenged!)



ell = l
one = 1
min(enumerate(ell), key=lambda x: x[one])


I'd like to point to the operator module which allows you to write the 
above as:


min(enumerate(ell), key=operator.itemgetter(one))

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


Re: tail

2022-04-24 Thread Antoon Pardon




Op 23/04/2022 om 20:57 schreef Chris Angelico:

On Sun, 24 Apr 2022 at 04:37, Marco Sulla  wrote:

What about introducing a method for text streams that reads the lines
from the bottom? Java has also a ReversedLinesFileReader with Apache
Commons IO.


1) Read the entire file and decode bytes to text
2) Split into lines
3) Iterate backwards over the lines

Tada! Done. And in Python, quite easy. The downside, of course, is
that you have to store the entire file in memory.


Why not just do:

tail = collections.deque(text_stream, maxlen = nr_of_lines)
tail.reverse()
...

--
Antoon Pardon

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


Re: Tuple unpacking inside lambda expressions

2022-04-19 Thread Antoon Pardon

Op 16/04/2022 om 23:36 schreef Sam Ezeh:

Two questions here.

Firstly, does anybody know of existing discussions (e.g. on here or on
python-ideas) relating to unpacking inside lambda expressions?

I found myself wanting to write the following.

```
map(
 lambda (module, data): result.process(module, data),
  jobs
)
```
However, it's of course not legal Python syntax.


Why not write:

itertools.starmap(result.process, jobs)

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


Re: Comparing sequences with range objects

2022-04-12 Thread Antoon Pardon




Op 11/04/2022 om 02:31 schreef Dan Stromberg:


It sounds a little like you're looking for interval arithmetic.

Maybe https://pypi.org/project/python-intervals/1.5.3/ ?


Not completely but it suggested an idea to explore.

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


Re: Comparing sequences with range objects

2022-04-12 Thread Antoon Pardon



Op 11/04/2022 om 02:01 schreef duncan smith:

On 10/04/2022 21:20, Antoon Pardon wrote:



Op 9/04/2022 om 02:01 schreef duncan smith:

On 08/04/2022 22:08, Antoon Pardon wrote:


Well my first thought is that a bitset makes it less obvious to 
calulate

the size of the set or to iterate over its elements. But it is an idea
worth exploring.





def popcount(n):
    """
    Returns the number of set bits in n
    """
    cnt = 0
    while n:
    n &= n - 1
    cnt += 1
    return cnt

and not tested,

def iterinds(n):
    """
    Returns a generator of the indices of the set bits of n
    """
    i = 0
    while n:
    if n & 1:
    yield i
    n = n >> 1
    i += 1

Sure but these seem rather naive implementation with a time 
complexity of
O(n) where n is the maximum number of possible elements. Using these 
would

turn my O(n) algorithm in a O(n^2) algorithm.



I thought your main concern was memory. Of course, dependent on 
various factors, you might be able to do much better than the above. 
But I don't know what your O(n) algorithm is, how using a bitset would 
make it O(n^2), or if the O(n^2) algorithm would actually be slower 
for typical n. The overall thing sounds broadly like some of the 
blocking and clustering methods I've come across in record linkage.


Using bitsets would make change my algorithm from O(n) into O(n^2) because
the bitset operations are O(n) instead of O(1). If I have a 2 people
a bitset will take a vector of about 300, 64bit words. With 4 people
the bitset will take a vector of about 600. So doubling the population
will also double the time for the bitset operations, meaning doubling
the population will increase execution time four times.

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


Re: Comparing sequences with range objects

2022-04-10 Thread Antoon Pardon



Op 9/04/2022 om 02:01 schreef duncan smith:

On 08/04/2022 22:08, Antoon Pardon wrote:


Well my first thought is that a bitset makes it less obvious to calulate
the size of the set or to iterate over its elements. But it is an idea
worth exploring.





def popcount(n):
    """
    Returns the number of set bits in n
    """
    cnt = 0
    while n:
    n &= n - 1
    cnt += 1
    return cnt

and not tested,

def iterinds(n):
    """
    Returns a generator of the indices of the set bits of n
    """
    i = 0
    while n:
    if n & 1:
    yield i
    n = n >> 1
    i += 1


Sure but these seem rather naive implementation with a time complexity of
O(n) where n is the maximum number of possible elements. Using these would
turn my O(n) algorithm in a O(n^2) algorithm.

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


Re: Comparing sequences with range objects

2022-04-08 Thread Antoon Pardon



Op 8/04/2022 om 16:28 schreef duncan smith:

On 08/04/2022 08:21, Antoon Pardon wrote:


Yes I know all that. That is why I keep a bucket of possible duplicates
per "identifying" field that is examined and use some heuristics at the
end of all the comparing instead of starting to weed out the duplicates
at the moment something differs.

The problem is, that when an identifying field is judged to be unusable,
the bucket to be associated with it should conceptually contain all 
other

records (which in this case are the indexes into the population list).
But that will eat a lot of memory. So I want some object that behaves as
if it is a (immutable) list of all these indexes without actually 
containing

them. A range object almost works, with the only problem it is not
comparable with a list.



Is there any reason why you can't use ints? Just set the relevant bits.


Well my first thought is that a bitset makes it less obvious to calulate
the size of the set or to iterate over its elements. But it is an idea
worth exploring.

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


Re: Comparing sequences with range objects

2022-04-08 Thread Antoon Pardon




Op 8/04/2022 om 08:24 schreef Peter J. Holzer:

On 2022-04-07 17:16:41 +0200, Antoon Pardon wrote:

Op 7/04/2022 om 16:08 schreef Joel Goldstick:

On Thu, Apr 7, 2022 at 7:19 AM Antoon Pardon   wrote:

I am working with a list of data from which I have to weed out duplicates.
At the moment I keep for each entry a container with the other entries
that are still possible duplicates.

[...]

Sorry I wasn't clear. The data contains information about persons. But not
all records need to be complete. So a person can occur multiple times in
the list, while the records are all different because they are missing
different bits.

So all records with the same firstname can be duplicates. But if I have
a record in which the firstname is missing, it can at that point be
a duplicate of all other records.

There are two problems. The first one is how do you establish identity.
The second is how do you ween out identical objects. In your first mail
you only asked about the second, but that's easy.

The first is really hard. Not only may information be missing, no single
single piece of information is unique or immutable. Two people may have
the same name (I know about several other "Peter Holzer"s), a single
person might change their name (when I was younger I went by my middle
name - how would you know that "Peter Holzer" and "Hansi Holzer" are the
same person?), they will move (= change their address), change jobs,
etc. Unless you have a unique immutable identifier that's enforced by
some authority (like a social security number[1]), I don't think there
is a chance to do that reliably in a program (although with enough data,
a heuristic may be good enough).


Yes I know all that. That is why I keep a bucket of possible duplicates
per "identifying" field that is examined and use some heuristics at the
end of all the comparing instead of starting to weed out the duplicates
at the moment something differs.

The problem is, that when an identifying field is judged to be unusable,
the bucket to be associated with it should conceptually contain all other
records (which in this case are the indexes into the population list).
But that will eat a lot of memory. So I want some object that behaves as
if it is a (immutable) list of all these indexes without actually containing
them. A range object almost works, with the only problem it is not
comparable with a list.

--
Antoon Pardon.

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


Re: Comparing sequences with range objects

2022-04-07 Thread Antoon Pardon

Op 7/04/2022 om 16:08 schreef Joel Goldstick:

On Thu, Apr 7, 2022 at 7:19 AM Antoon Pardon  wrote:

I am working with a list of data from which I have to weed out duplicates.
At the moment I keep for each entry a container with the other entries
that are still possible duplicates.

The problem is sometimes that is all the rest. I thought to use a range
object for these cases. Unfortunatly I sometimes want to sort things
and a range object is not comparable with a list or a tuple.

So I have a list of items where each item is itself a list or range object.
I of course could sort this by using list as a key function but that
would defeat the purpose of using range objects for these cases.

So what would be a relatively easy way to get the same result without wasting
too much memory on entries that haven't any weeding done on them.

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

I'm not sure I understand what you are trying to do, but if your data
has no order, you can use set to remove the duplicates


Sorry I wasn't clear. The data contains information about persons. But not
all records need to be complete. So a person can occur multiple times in
the list, while the records are all different because they are missing
different bits.

So all records with the same firstname can be duplicates. But if I have
a record in which the firstname is missing, it can at that point be
a duplicate of all other records.

--
Antoon Pardon

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


Comparing sequences with range objects

2022-04-07 Thread Antoon Pardon

I am working with a list of data from which I have to weed out duplicates.
At the moment I keep for each entry a container with the other entries
that are still possible duplicates.

The problem is sometimes that is all the rest. I thought to use a range
object for these cases. Unfortunatly I sometimes want to sort things
and a range object is not comparable with a list or a tuple.

So I have a list of items where each item is itself a list or range object.
I of course could sort this by using list as a key function but that
would defeat the purpose of using range objects for these cases.

So what would be a relatively easy way to get the same result without wasting
too much memory on entries that haven't any weeding done on them.

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


Re: Reducing "yield from" overhead in recursive generators

2022-03-18 Thread Antoon Pardon

Op 18/03/2022 om 04:14 schreef Rathmann:

...

```python
def recursive_generator_driver(g):
 """g is a generator object, which is likely to call other generators"""
 stack = [g]
 while True:
 g = stack[-1]
 try:
 val = next(g)
 if isinstance(val, types.GeneratorType):
 # Yielded value represented a recursive call, so push
 stack.append(val)
 else:
 # Regular value for iterator, give to caller
 yield val
 except StopIteration:
 stack.pop()
 if len(stack) == 0:
 return
```

and the modified tree traverser is just

```python
def inorder_traverse_mod(node):
 """ONLY for use with recursive_generator_driver"""
 k, l, r = node
 if l:
 yield inorder_traverse_mod(l) # NOT yield from
 yield k
 if r:
 yield inorder_traverse_mod(r) # NOT yield from
```

...

So what do people think of this hack/technique?  Is it

A) A terrible idea?  (Please be specific.)
B) Already well-known (and I just missed it.)
C) Potentially useful for the niche case of deeply recursive
generators?


I would go with B' + C. It seems there is a resemblance with the trampoline 
technique
in order to eliminate tail recursion. I know the people of macropy have written 
a
decorating macro that would eliminate tail recursion from such decorated 
functions.
Maybe if you contact them they can be interested in making a similar decorating 
macro
for use with such recursive decorators.

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


Re: Behavior of the for-else construct

2022-03-07 Thread Antoon Pardon



Op 4/03/2022 om 02:08 schreef Avi Gross via Python-list:

If Python was being designed TODAY, I wonder if a larger set of key words would 
be marked as RESERVED for future expansion including ORELSE and even 
NEVERTHELESS.


I think a better solution would be to have reserved words written letters form 
the mathematical lettter block.

Something like:

퐝퐞퐟 foo(bar):
   in = file(...)
   퐟퐨퐫 line 퐢퐧 in:
  ...

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


Re: All permutations from 2 lists

2022-03-02 Thread Antoon Pardon




Op 2/03/2022 om 15:58 schreef Larry Martell:

On Wed, Mar 2, 2022 at 9:37 AM Antoon Pardon  wrote:



If one list is empty I want just the other list. What I am doing is
building a list to pass to a mongodb query. If region is empty then I
want to query for just the items in the os list. I guess I can test
for the lists being empty, but I'd like a solution that handles that
as down the road there could be more than just 2 lists.

How about the following: Keep a list of your lists you want to permute over.
Like the following:

permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]

permutation = itertools.product(*permutation_elements)

If you don't include the empty list, you will get more or less what you
seem to want.

But I need to deal with that case.

What does that mean? How does using the above method to produce the permutations
you want, prevent you from dealing with an empty list however you want when you
encounter them? Just don't add them to the permutation_elements.

I need to know what items are in which position. If sometimes the
regions are in one index and sometimes in another will not work for
me.


I am starting to suspect you didn't think this through. What you are telling 
here
contradicts what you told earlier that if either list was empty, you just wanted
the other list. Because then you wouldn't know what items were in that list.

The only solution I can see now is that if a list is empty, you either add 
[None] or
[""] to the permutation_elements (whatever suits you better) and then use
itertools.product
--
https://mail.python.org/mailman/listinfo/python-list


Re: All permutations from 2 lists

2022-03-02 Thread Antoon Pardon




Op 2/03/2022 om 15:29 schreef Larry Martell:

On Wed, Mar 2, 2022 at 9:10 AM Antoon Pardon  wrote:

Op 2/03/2022 om 14:44 schreef Larry Martell:

On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon   wrote:

Op 2/03/2022 om 14:27 schreef Larry Martell:

On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>wrote:

On 2022-03-01 at 19:12:10 -0500,
Larry Martellwrote:


If I have 2 lists, e.g.:

os = ["Linux","Windows"]
region = ["us-east-1", "us-east-2"]

How can I get a list of tuples with all possible permutations?

So for this example I'd want:

[("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
"us-east-1"), "Windows", "us-east-2')]

The lists can be different lengths or can be 0 length. Tried a few
different things with itertools but have not got just what I need.

[(o, r) for o in os for r in region]

This does not work if region = []. I wrote in my question that either
list could be empty.

What do you mean it doesn't work? The result seems to be an empty list,
which IMO is a perfectly valid result.

All possible permutations over two collections where one collection is
empty, should IMO give you an empty collection.

If one list is empty I want just the other list. What I am doing is
building a list to pass to a mongodb query. If region is empty then I
want to query for just the items in the os list. I guess I can test
for the lists being empty, but I'd like a solution that handles that
as down the road there could be more than just 2 lists.

How about the following: Keep a list of your lists you want to permute over.
Like the following:

permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]

permutation = itertools.product(*permutation_elements)

If you don't include the empty list, you will get more or less what you
seem to want.

But I need to deal with that case.


What does that mean? How does using the above method to produce the permutations
you want, prevent you from dealing with an empty list however you want when you
encounter them? Just don't add them to the permutation_elements.

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


Re: All permutations from 2 lists

2022-03-02 Thread Antoon Pardon

Op 2/03/2022 om 14:44 schreef Larry Martell:

On Wed, Mar 2, 2022 at 8:37 AM Antoon Pardon  wrote:


Op 2/03/2022 om 14:27 schreef Larry Martell:

On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>   wrote:

On 2022-03-01 at 19:12:10 -0500,
Larry Martell   wrote:


If I have 2 lists, e.g.:

os = ["Linux","Windows"]
region = ["us-east-1", "us-east-2"]

How can I get a list of tuples with all possible permutations?

So for this example I'd want:

[("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
"us-east-1"), "Windows", "us-east-2')]

The lists can be different lengths or can be 0 length. Tried a few
different things with itertools but have not got just what I need.

[(o, r) for o in os for r in region]

This does not work if region = []. I wrote in my question that either
list could be empty.

What do you mean it doesn't work? The result seems to be an empty list,
which IMO is a perfectly valid result.

All possible permutations over two collections where one collection is
empty, should IMO give you an empty collection.

If one list is empty I want just the other list. What I am doing is
building a list to pass to a mongodb query. If region is empty then I
want to query for just the items in the os list. I guess I can test
for the lists being empty, but I'd like a solution that handles that
as down the road there could be more than just 2 lists.


How about the following: Keep a list of your lists you want to permute over.
Like the following:

permutation_elements = [["Linux","Windows"],["us-east-1", "us-east-2"]]

permutation = itertools.product(*permutation_elements)

If you don't include the empty list, you will get more or less what you
seem to want.

Antoon Pardon.

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


Re: All permutations from 2 lists

2022-03-02 Thread Antoon Pardon



Op 2/03/2022 om 14:27 schreef Larry Martell:

On Tue, Mar 1, 2022 at 7:21 PM<2qdxy4rzwzuui...@potatochowder.com>  wrote:

On 2022-03-01 at 19:12:10 -0500,
Larry Martell  wrote:


If I have 2 lists, e.g.:

os = ["Linux","Windows"]
region = ["us-east-1", "us-east-2"]

How can I get a list of tuples with all possible permutations?

So for this example I'd want:

[("Linux", "us-east-1"), ("Linux", "us-east-2"), ("Windows",
"us-east-1"), "Windows", "us-east-2')]

The lists can be different lengths or can be 0 length. Tried a few
different things with itertools but have not got just what I need.

[(o, r) for o in os for r in region]

This does not work if region = []. I wrote in my question that either
list could be empty.


What do you mean it doesn't work? The result seems to be an empty list,
which IMO is a perfectly valid result.

All possible permutations over two collections where one collection is
empty, should IMO give you an empty collection.

--
Antoon Pardon.

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


Re: Best way to check if there is internet?

2022-02-22 Thread Antoon Pardon



Op 22/02/2022 om 09:40 schreef Chris Angelico:

On Tue, 22 Feb 2022 at 19:33, Abdur-Rahmaan Janhangeer
  wrote:

As discussed here but, it would have been nevertheless great to have this
tiny function instead of
nothing


Here's a function that determines whether or not you have an internet
connection. It's almost as reliable as some of the other examples
given - I know this, because I tried it ten times, and it gave the
correct result every time!


So, you discovered a way of testing that is not very thorough.


def has_internet():
 return True

Tell me, is it useful to have something that doesn't always give the
right answer, even if it usually does? Is there any value whatsoever
in a lie?


Yes that is useful. Live is full of that kind of situations. We in computerland
are spoiled with the accuracy we can enjoy. It seems even spoiled to the extend
that when offered a solution that is not 100% accurated we consider it a lie.

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


Re: Why There Is No Python Compressed Archive or Binaries ?

2022-01-17 Thread Antoon Pardon

You could try miniconda.

Op 17/01/2022 om 20:53 schreef Sina Mobasheri:

Consider scenario that I want run python 3.10 in CentOS 8, I think last python 
version in CentOS repository is 3.6, if I use epel I can get 3.8 so ..., I 
think (correct me if I'm wrong ) the only way that I can run python 3.10 is 
to compile it manually, which is need to know what dependencies python needs 
for compilation ... (different distribution different packages, which packages 
for what, you can see that it is intimidating for beginners like me)
It's useful to just use wget , 
unzipped, ser path and ta-da you have cpython 3.10 in CentOS 8

This is Linux specific use case that I can think of, I'm sure there are plenty 
for windows, consider scenario that I wrote script for scraping some site and 
entered in some excel worksheet I can simply ship cpython with my script to 
clients machine and there is no need that client install cpython by himself...

Sorry about my bad grammar

From: Calvin Spealman 
Sent: Monday, January 17, 2022, 22:53
To: Sina Mobasheri
Cc: python-list@python.org
Subject: Re: Why There Is No Python Compressed Archive or Binaries ?

Well, on its own, I'd say the reason we don't have such a download is that it 
wouldn't be very useful.

On Mon, Jan 17, 2022 at 2:08 PM Sina Mobasheri 
mailto:sinamobash...@outlook.com>> wrote:
It's cool project definitely something that I'm personally interested about, 
but I talking about compressed archive of cpython that we can simply unzipped 
and starting developing an app, not running  an app that already developed... 

From: Calvin Spealman mailto:cspea...@redhat.com>>
Sent: Monday, January 17, 2022 10:19:13 PM
To: Sina Mobasheri mailto:sinamobash...@outlook.com>>
Cc: python-list@python.org 
mailto:python-list@python.org>>
Subject: Re: Why There Is No Python Compressed Archive or Binaries ?

I maintain a small project that provides this, a drop-in Python runtime you can 
ship without installation called Feet. Get it? It makes Python run.

https://github.com/ironfroggy/feet

On Mon, Jan 17, 2022 at 11:16 AM Sina Mobasheri 
mailto:sinamobash...@outlook.com>> wrote:
Java offers download JDK as Compressed 
Archive or NodeJS 
offers download Node as Binaries both give 
us a compressed file for Linux and windows that we can just unzipped it and put in a custom 
directory and set some environment variables and start working

I'm aware that Python also have something called Embedded 
Zip for 
Windows and nothing like that for Linux as far as I know, and I think this Embedded 
Zip is not something that user wants to work with that directly it's for embedding in 
a C++ application, so it's not the same as options that Java and NodeJS offers

My question is why Python hasn't option for downloading as Compressed Archive ?
--
https://mail.python.org/mailman/listinfo/python-list



--

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

calvin.speal...@redhat.com  M: 
+1.336.210.5107

[https://red.ht/sig]
TRIED. TESTED. TRUSTED.


--

CALVIN SPEALMAN

SENIOR QUALITY ENGINEER

calvin.speal...@redhat.com  M: 
+1.336.210.5107

[https://red.ht/sig]
TRIED. TESTED. TRUSTED.



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


Re: Why operations between dict views return a set and not a frozenset?

2022-01-05 Thread Antoon Pardon




Op 4/01/2022 om 19:27 schreef Marco Sulla:

$ python
Python 3.10.0 (heads/3.10-dirty:f6e8b80d20, Nov 18 2021, 19:16:18)
[GCC 10.1.1 20200718] on linux
Type "help", "copyright", "credits" or "license" for more information.

a = {1:2}
c = {1:2, 3:4}
c.keys() - a.keys()

{3}

Why not frozenset({3})?


My 2 cents worths: Because dictviews mutate with the directory. That makes
them more like set than like frozenset. So operations on them produce a set.

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


Re: A problem with itertools.groupby

2021-12-17 Thread Antoon Pardon




but:

li = [grp for k, grp in groupby("aahfffddnnb")]
list(li[0])

[]

list(li[1])

[]

It seems empty ... I don't understand why, this is
the first read of an iterator, it should provide its
data.


The group-iterators are connected. Each group-iterator is a wrapper 
around the original iterator
with an extra termination condition. So in order to start the next 
group-iterator the previous
group-iterator is exhausted, because the original iterator has to be 
ready to produce values

for the next group-iterator.

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


Re: Negative subscripts

2021-11-26 Thread Antoon Pardon

Op 26/11/2021 om 10:17 schreef Frank Millman:

Hi all

In my program I have a for-loop like this -

>>> for item in x[:-y]:
...    [do stuff]

'y' may or may not be 0. If it is 0 I want to process the entire list 
'x', but of course -0 equals 0, so it returns an empty list.


In theory I can say

>>> for item in x[:-y] if y else x:
...    [do stuff]

But in my actual program, both x and y are fairly long expressions, so 
the result is pretty ugly.


Are there any other techniques anyone can suggest, or is the only 
alternative to use if...then...else to cater for y = 0?


It depends. Since you are talking about x being a fairly long expression,
how about adapting that expression so that is produces the reverse of
what is now produced and then use the [y:] slice?

You can also adapt the y expression so that instead of y it produces
len(x) - y.

You may consider writing a reverse_view function, that takes a list
as argument and produces something that behaves as the reversed list
without actually reversing the list and then as in the first option
use the [y:] slice on that.

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


Re: New assignmens ...

2021-10-29 Thread Antoon Pardon



Op 28/10/2021 om 19:36 schreef Avi Gross via Python-list:

Now for a dumb question. Many languages allow a form of setting a variable to a 
value like:

  


 assign(var, 5+sin(x))

  


If we had a function that then returned var or the value of var, cleanly, then 
would that allow an end run on the walrus operator?

  


if (assign(sign, 5+sin(x)) <= assign(cosign, 5+cos(x))) …

  


Not necessarily pretty and I am sure there may well be reasons it won’t work, 
but I wonder if it will work in more places than the currently minimal walrus 
operator.


This was the orginal code to illustrate the question:

if (self.ctr:=self.ctr-1)<=0

So if I understand your sugested solution it would be something like:

def setxattr(obj, attr, value):
setattr(obj, attr, value)
return value

if setxattr(self, 'ctr', self.ctr - 1) <= 0

Did I get that right?

--
Antoon Pardon.

   


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


Re: New assignmens ...

2021-10-29 Thread Antoon Pardon




Op 28/10/2021 om 19:36 schreef Avi Gross via Python-list:

Antoon,

  
You keep beating a dead horse. NOBODY denies there are benefits to suggestions like the one we are describing. It is a logical fallacy to keep arguing this way.


Please point to the specific logical falacy you think I am commiting. Don't 
hand wave about what I supposedly
am doing.


And nobody (meaning me) suggests costs are a dominant factor in decisions no 
matter the benefits. The realistic suggestion is to not only weight costs and 
benefits for one proposal but for all reasonable proposals and then choose.


So what if *you* don't suggest that. Others have. Then when I responded to 
those with a remark about seperating
costs and benefits, you budded in with the assertion that those who suggest 
seperating benefits and cost belong
in the accademic world because you understood that remark as implying costs are 
not as issue. I then explain you
misunderdood en now you come with the above.

Maybe you should be more aware of the history of a thread before coming with 
this kind of statements.

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


Re: New assignmens ...

2021-10-28 Thread Antoon Pardon




Op 27/10/2021 om 17:05 schreef Christman, Roger Graydon:

I'm going to provide two loop-and-a-half segments to illustrate my 
interpretation
of this PEP and the purpose of the walrus operator:

[ first example ]

Now contrast with this example:

Without the walrus:

replay = True
while replay:
 play_game()
 replay = input("Play again? ") in ['y','Y','yes','Yes']

(I think it silly to ask about playing again at first).

With the walrus:

replay = None
while replay==None or (replay := input("Play again? ") in ['y','Y','yes','Yes']:
  play_game()

To use the walrus operator here, I have to fabricate a value that would
allow me to bypass the input operation, that cannot be otherwise produced.
I do not find this second version any clearer or more intuitive than the first
(and the PEP did emphasize the value of clarity).


But the above is not a one and a half loop. The above is essentially a do
loop (repeat loop in pascal), where you have the test at the end of the
loop body. But because python doesn't have that loop you have to use a
boolean to control the loop that is initialized to True and then later
assign that boolean the result of the test which you use to control this
loop.

Should I think it worth the trouble to rewrite your example, quod non,
it would be like below, with that unneeded list.

while [
play_game(),
input("Play again? ") in ['y', 'Y', 'yes', 'Yes']][-1]:
pass

--
Antoon Pardon.

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


Re: New assignmens ...

2021-10-28 Thread Antoon Pardon




Op 27/10/2021 om 20:20 schreef Avi Gross:

I think anyone who suggests we should separate costs from benefits belongs
securely within the academic world and should remain there.

Practical things need to be built considering costs. Theoretical things,
sure, cost is not an issue.


Seperating costs from benefits doesn't mean costs are not an issue. It means
you don't deny de benefits because there are costs. Sure in the end the costs
may outweight the benefits but that is still not the same as there being no
benefits at all.

If you want to weight the costs against the benefits you need to acknowledge
both and not start by denying the benefits because you presume they will
not outweight the costs.

--
Antoon.

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


Re: New assignmens ...

2021-10-27 Thread Antoon Pardon




Op 27/10/2021 om 18:16 schreef Christman, Roger Graydon:

On 27/10/2021 at 12:45  Antoon Pardon wrote:

However with the introduction of the walrus operator there is a
way to simulate a significant number of one and a half loops.
Consider the following:

  >do
  >   a = expr1
  >   b = expr2
  >   while 2 * a > b:
  >  more calculations


We could write that now as

  >while [
  >a := expr1,
  >   b := expr2,
  >   2 * a > b][-1]:
  >  more calculations

Why don't you try this?


Because this is about a general idea, not about the specifics of the example.
 

while 2 * (a := expr1) > (b := expr2):
   more calculations

It seems you are just compelled to create tuple and lists
in all of your use-cases, even when they serve no purpose.


Do you have a procedure that will always eliminate a list and will be
more or less as readable as the one and a half loop?

I know the list serves no purpose, other than to provide a way to
easily write the calculations in the order that seems most natural.

But being able to write calculations in the order that makes them
more understandable is IMO worth more than eliminating the list.
Even if the list serves no real purpose in the calculations.

So suppose I have an arbitrary number of simple statements. The
latter possible using results from previous assignment and at the
end a condition to control the one and a half loop. How do you write
the python code so that the one and a half loop is easy to recognize?

--
Antoon Pardon

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


Re: New assignmens ...

2021-10-27 Thread Antoon Pardon



Op 27/10/2021 om 11:59 schreef Chris Angelico:

You can argue the word "need" all you like, but the fact remains that
YOU want a change, so YOU have to convince people of the benefits.


That is true. But there is nothing wrong in asking what might convince
people.

But I'll give you my thought below and you can decide in how far this
is convincing to you.

I regularly come with a problem for which a one and a half loop is very
suited to solve it. Unfortunately python doesn't have a one and a half
loop. However with the introduction of the walrus operator there is a
way to simulate a significant number of one and a half loops.

Consider the following:

do
a = expr1
b = expr2
   while 2 * a > b:
   more calculations

We could write that now as

while [
a := expr1,
b := expr2,
2 * a > b][-1]:
more calculations

Now there is some ugly side on the above and it may not be obvious at first
what is going on, but once you understand it is a rather easy idiom. I certainly
prefer it over writing something like

while True:
a = expr1
b = expr2
if not (2 * a > b):
break
more calculations.

So for me any limitation on the walrus operator that is removed is a plus 
because
it will allow me to write more one and a half loops in more natural way.

Off course should the python developers decide to intoduce a real one and a half
loop all the above is probably a whole let useful.

--
Antoon Pardon

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


Re: New assignmens ...

2021-10-27 Thread Antoon Pardon




Op 27/10/2021 om 10:49 schreef Chris Angelico:

On Wed, Oct 27, 2021 at 7:46 PM Antoon Pardon  wrote:

So if you want this added, show a use-case that makes it look way
better than the alternatives (including a generator, a mid-loop break,
etc).

Way better according to which criteria? IMO to realy make something like
this you would need a one and a half loop. But although at some time there
was a strong indication it would get included, the idea was eventually 
discarted.

So I'll argue for incremental better if I see a possibility. A few incremental
betters can eventually result in something that is way better than the original.

According to any criteria you like. Remember, the onus is not on the
status quo to justify itself; the onus is on someone who wants to make
a change.


Then don't answer that I can justify it according to any criteria I like.
It will have to be done according to criteria that are important to the
people who like the status quo. That it will be justified according to
criteria I like, will be totally unimportant.


Demonstrate that a change is needed by showing the benefits.


Nothing is needed. But the walrus operator wasn't needed either. Asking to
show something is needed is putting a burden on who wants to advocate for
this, that isn't put on others.

--
Antoon Pardon.

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


Re: New assignmens ...

2021-10-27 Thread Antoon Pardon

Op 27/10/2021 om 10:38 schreef dn via Python-list:

On 24/10/2021 22.23, O365 Dict wrote:

Well I have the following use case:

 while (temp_result := calculate_next_couple(a, b))[1]:
 a, b = temp_result
 more calculations

Which IMO would be clearer if I could just write:

 while ((a, b) := calculate_next_couple(a,b))[1]:
 more calculations

Of course it would even more clear if I could write something like:

 while (a, b) := calculate_next_couple(a, b); b:
 more calculations

or

 do:
 a, b = calculate_next_couple(a, b)
 while b:
 more calculations


Found (all of) the above less-than-obvious to read. Putting it in front
of trainees this morning caused only confusion - even the
currently-legal variation.


A lot of python idioms are less than obvious for trainees when they first
encounter them. I don't think "Is obvious for trainess" is a good measure
to evaluate possible future programming constructs.


Accordingly: is this a job for the walrus operator at all? Let's "talk
of many [other] things"*.

...


That all looks simple. What is dn complaining about?


Could we use a data structure to continue to keep things straight-forward?


Yes off course we could. Before the walrus operator was introduced, we could
also introduce extra code so that we wouldn't need the walrus operator at all.



...
Hope the above gives you some ideas/pause for thought!


No they don't. You have taken an example to illustrate an idea and
treated it as if it was some production code you had to review.

What you are suggesting boils downto that if one has a loop in which
one has two work variables, with a condition on those variable to
control a loop. In order to make use of the walrus operator we should
combine those two work variables into some kind of instance no matter
how contrived.

--
Antoon Pardon.


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


Re: New assignmens ...

2021-10-27 Thread Antoon Pardon




Op 27/10/2021 om 10:05 schreef Chris Angelico:

On Wed, Oct 27, 2021 at 6:00 PM Antoon Pardon  wrote:



Op 26/10/2021 om 00:24 schreef Chris Angelico:

TBH, I don't think there's a lot of value in multiple-assignment,
since it has a number of annoying conflicts of syntax and few viable
use-cases. But if you have great examples of "x.y :=" or "x[y] :=",
then by all means, post on python-ideas to propose widening the scope.

I think you should seperate the costs from the benefits. It is not because
the costs can be high there is little value.

And how do you count use cases? What about the following pattern:

while (a, b) := next_couple(a,b)[-1]:
  ...

Is that one use case or is that a use case for each kind of couple?

And even if the benefits are little per case, they can add up with every
occasion such a case pops up.


I'm not sure that it's much of a use-case; isn't it an infinite loop as written?

And that's the problem. With multiple-assignment, the overall value is
going to be the tuple, so you then have to add extra parentheses and
subscripting to get what you want to check. That makes it far less
clean, far less tempting, far less valuable. You have to parenthesize
the assignment target (otherwise it'll build a tuple out of one value
and the assigned target), then parenthesize again, and subscript at
the end.


That is only a problem if you want to consider it one. This wouldn't be the
only place in python where you have to be careful with parentheses. IMO it
should be the responsibility of each programmer to decide which way to
program will be most clean. Maybe you are completly right and at the
same time it could still be more clean than the other possibilitie python
allows.


So if you want this added, show a use-case that makes it look way
better than the alternatives (including a generator, a mid-loop break,
etc).


Way better according to which criteria? IMO to realy make something like
this you would need a one and a half loop. But although at some time there
was a strong indication it would get included, the idea was eventually 
discarted.

So I'll argue for incremental better if I see a possibility. A few incremental
betters can eventually result in something that is way better than the original.

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


Re: New assignmens ...

2021-10-27 Thread Antoon Pardon




Op 27/10/2021 om 10:05 schreef Chris Angelico:

On Wed, Oct 27, 2021 at 6:00 PM Antoon Pardon  wrote:



Op 26/10/2021 om 00:24 schreef Chris Angelico:

TBH, I don't think there's a lot of value in multiple-assignment,
since it has a number of annoying conflicts of syntax and few viable
use-cases. But if you have great examples of "x.y :=" or "x[y] :=",
then by all means, post on python-ideas to propose widening the scope.

I think you should seperate the costs from the benefits. It is not because
the costs can be high there is little value.

And how do you count use cases? What about the following pattern:

while (a, b) := next_couple(a,b)[-1]:
  ...

Is that one use case or is that a use case for each kind of couple?

And even if the benefits are little per case, they can add up with every
occasion such a case pops up.


I'm not sure that it's much of a use-case; isn't it an infinite loop as written?




And that's the problem. With multiple-assignment, the overall value is
going to be the tuple, so you then have to add extra parentheses and
subscripting to get what you want to check. That makes it far less
clean, far less tempting, far less valuable. You have to parenthesize
the assignment target (otherwise it'll build a tuple out of one value
and the assigned target), then parenthesize again, and subscript at
the end.

So if you want this added, show a use-case that makes it look way
better than the alternatives (including a generator, a mid-loop break,
etc).

ChrisA


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


Re: New assignmens ...

2021-10-27 Thread Antoon Pardon




Op 26/10/2021 om 00:24 schreef Chris Angelico:

TBH, I don't think there's a lot of value in multiple-assignment,
since it has a number of annoying conflicts of syntax and few viable
use-cases. But if you have great examples of "x.y :=" or "x[y] :=",
then by all means, post on python-ideas to propose widening the scope.


I think you should seperate the costs from the benefits. It is not because
the costs can be high there is little value.

And how do you count use cases? What about the following pattern:

while (a, b) := next_couple(a,b)[-1]:
...

Is that one use case or is that a use case for each kind of couple?

And even if the benefits are little per case, they can add up with every
occasion such a case pops up.

--
Antoon Pardon.

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


Re: New assignmens ...

2021-10-27 Thread Antoon Pardon




Op 26/10/2021 om 19:46 schreef Schachner, Joseph:

Why force unpacking?   Why not assign a tuple?  That would look like a simple 
assignment: x := (alpha, beta, gamma)
And you could access x[0],  x[1] and x[2].

I think asking := to support x, y := alpha, beta  is a request to address an 
unnecessary, easily worked around, issue.  And as previously pointed out you 
can still just use = .


Because the names usually have meaning, while the tuple has not.

And you just confirmed my point that the walrus operator isn't very useful here
by suggesting that I should abandon it and use an assignment.

--
Antoon Pardon.

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


Re: New assignmens ...

2021-10-27 Thread Antoon Pardon




Op 25/10/2021 om 18:47 schreef Christman, Roger Graydon:

Message: 8
Date: Mon, 25 Oct 2021 11:20:52 +0200
From: Antoon Pardon 
To: python-list@python.org
Subject: Re: New assignmens ...
Message-ID: <5761dd65-4e87-8b8c-1400-edb821204...@vub.be>
Content-Type: text/plain; charset=utf-8; format=flowed
On 25/10/2021 11:20, Anton Pardon wrote:

Suppose I would like to write a loop as follows:

  >while ((a, b) := next_couple(a, b))[1]:
  >do needed calculations



What I can do is write it as follows:
 while [tmp := next_couple(a,b), a := tmp[0], b := tmp[1]][-1]:

  >do needed calculations


I really don't see what is gained by "forcing" me to right the second code over 
the first.

No, nobody is forcing you to right it the second way over the first.
Nobody is forcing you to use the walrus operator at all!

Instead, I would recommend something more like:

while b:
  do needed calculations
  (a,b) = next_couple(a,b)


But AIU the walrus operator was introduced so we no longer needed, to write 
such code,
with the calculation of the next candidate at the bottom and the test at the 
top.
You just confirmed the walrus operator is not very useful once the next 
candidate is
no longer just a name.

--
Antoon.

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


Re: New assignmens ...

2021-10-26 Thread Antoon Pardon
Op 25/10/2021 om 23:03 schreef Chris Angelico:
> On Tue, Oct 26, 2021 at 7:18 AM Antoon Pardon  wrote:
>> Op 25/10/2021 om 20:39 schreef Chris Angelico:
>>> On Tue, Oct 26, 2021 at 5:35 AM Antoon Pardon  wrote:
>>>> By putting limits on the walrus code, you are not reducing complexity, you 
>>>> are increasing it.
>>>> You are increasing complexity because you can't just reuse the code that 
>>>> handles an ordinary
>>>> assignment. You now need specific code to limit it's use.
>>>>
>>> What does this code do?
>>>
>>> while x, y := foo():
>>> ...
>>>
>>> Is it more complicated or less complicated when arbitrary assignment
>>> targets are permitted?
>> Well I would guess it would do something similar to
>>
>> while [x, y := foo()]:
>> ...
>>
> And does it unpack what foo returns?
>
> Should it?

1) No it doesn't.

2) I don't care. This is IMO not a question of what should or should not, but 
just a
question of deciding how you want to treat this. I guess that since it doesn't 
unpack
already, this behaviour is more or less fixed for futere releases. Should the 
python
developers in the future decide that de walrus operator can unpack things. The 
above
code will not unpack in order to not break already existing code and if you want
to unpack one will have to write something like:

while [(x, y) := foo()]:
...

But the answer to that second question has very little relevance to how 
complicated the
parser will be. It is just deciding which of , or := has a higher precedence. 
Since that
decision has already been more or less made, there is not much to decide here 
either.

-- 
Antoon Pardon.

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


Re: New assignmens ...

2021-10-25 Thread Antoon Pardon
Op 25/10/2021 om 20:39 schreef Chris Angelico:
> On Tue, Oct 26, 2021 at 5:35 AM Antoon Pardon  wrote:
>> By putting limits on the walrus code, you are not reducing complexity, you 
>> are increasing it.
>> You are increasing complexity because you can't just reuse the code that 
>> handles an ordinary
>> assignment. You now need specific code to limit it's use.
>>
> What does this code do?
>
> while x, y := foo():
> ...
>
> Is it more complicated or less complicated when arbitrary assignment
> targets are permitted?

Well I would guess it would do something similar to

while [x, y := foo()]:
...

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


Re: New assignmens ...

2021-10-25 Thread Antoon Pardon
Op 25/10/2021 om 18:06 schreef Avi Gross via Python-list:
> Antoon,
>
> Just to be clear. I am talking about a different measure of efficiency.

No you are not.

>
> The topic here is the Python run-time parser though.

Yes and that is what I am talking about.

>  It is reading your code
> and doing whatever complex set of things it has to do to parse from a fairly
> large set of possible valid programs as well as invalid ones. I have never
> looked deeply at how it works but my guess is that somewhere in there are
> concepts like:
>
> simple_asignment_expression can look like THIS.
> complex _assignment expression can look like simple_assignment_expression OR
> THAT OR ...
>
> So to parse code you often need to look at alternate ways of connecting
> symbols and hopefully find the one and only way it has to be looked at.
> Parentheses as an example have many possible meanings and you may not know
> which meaning when you encounter it until you keep going and see where there
> may be a matching one but ignore any within a character string. I won't go
> on but the point is that the parser can jump through more hoops even in the
> most usual cases when it has to look for new cases not originally designed
> in.

IMO that extra complexity is insignificant. You really don't reduce the 
complexity of your
parser much if you would limit it so that indexes can only be names so that the 
programmer
instead of being able to write:

var = tab[some expression]

is forced to write it as:

index = some expression
var = tab[index]

Because all that machinery to evaluate some expression needs to be there anyway.

In the same way we have already all the machinery present for assignments.

By putting limits on the walrus code, you are not reducing complexity, you are 
increasing it.
You are increasing complexity because you can't just reuse the code that 
handles an ordinary
assignment. You now need specific code to limit it's use.

-- 
Antoon Pardon.

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


Re: New assignmens ...

2021-10-25 Thread Antoon Pardon

On 25/10/2021 01:46, Avi Gross via Python-list wrote:

No, many things need not be as general as possible once you consider how
much work it may take to develop code and how many bugs and oddities might
be introduced and even how much it may slow the interpreter.

...

I imagine you can create some fairly complex examples you can suggest should
be handled for generality including some very indirect references created
dynamically. The code to recognize any abstract use of symbols may not only
slow down every operation of even the simplest type but generate all kinds
of error messages nobody will understand, let alone translate into other
languages properly! Right now, it is simpler. An error message can say that
only certain simple usages are allowed.


I don't consider this a strong argument. Limiting the scope of the walrus 
operator
will just force people organizing there code where they will use a normal 
assignment.
So the resulting code will not be faster, less complex or generate less error 
messages
because the complexity of the assignment that is needed is still the same.

Or you force people to be "creative" as follows:

Suppose I would like to write a loop as follows:

while ((a, b) := next_couple(a, b))[1]:
do needed calculations


What I can do is write it as follows:

while [tmp := next_couple(a,b), a := tmp[0], b := tmp[1]][-1]:
do needed calculations

I really don't see what is gained by "forcing" me to right the second code over 
the first.

--
Antoon Pardon

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


importing a module from a file without the '.py' suffix

2021-10-22 Thread Antoon Pardon
I have a file with python code but the name doesn't end with the '.py' 
suffix.


What is the easiest way to import this code as a module without changing 
its name?


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


Mailing list activity low

2021-10-12 Thread Antoon Pardon
Have I missed something and has the maillinglist been moved. Activity is 
very low here, about one message every five days.


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


f-strings and internationalisation.

2021-05-24 Thread Antoon Pardon
I have now come across several occasion where an author advice the use
of f-strings above the %-formatting and the format method. However it
seems these authors were only thinking about rather straight forward
english communication.

So what if you want your application to work with multiple languages.
Can that be done with f-strings?

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


Re: name for a mutually inclusive relationship

2021-02-26 Thread Antoon Pardon




Op 24/02/21 om 17:12 schreef Ethan Furman:
I'm looking for a name for a group of options that, when one is 
specified, all of them must be specified.



It seems you are looking at an equivalence.
--
https://mail.python.org/mailman/listinfo/python-list


python 2.6: Need some advise for installing modules on a legacy system

2021-02-24 Thread Antoon Pardon
I need to do some development on this legacy system. It only runs 
python2.6 and there is little hope of installing an other version. How 
can I best proceed to install modules for working with mysql and ldap?


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


Mutable defaults

2021-02-09 Thread Antoon Pardon

Most of us know of the perils of mutable default values. So I came up with the 
following proof of concept:

from inspect import signature as signature_of, Parameter
from itertools import zip_longest
from copy import copy

def copy_defaults(f):

signature = signature_of(f)

def wrapper(*args):
newargs = []
for argument, parameter_item in zip_longest(args, 
signature.parameters.items(), fillvalue = Parameter.empty):
_, parameter = parameter_item
if argument is not Parameter.empty:
newargs.append(argument)
elif (parameter.default is not Parameter.empty):
newargs.append(copy(parameter.default))
else:
raise TypeError("Not enough arguments")
return f(*newargs)

return wrapper

@copy_defaults
def prepare(value, lst = []):
for vl in range(value):
lst.append(vl)
return lst

print(prepare(2))
print(prepare(3))

Running the above will produce:

[0, 1]
[0, 1, 2]


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


Re: Best practice for handling exceptions raised at lower levels?

2021-02-02 Thread Antoon Pardon

Op 2/02/21 om 01:54 schreef Skip Montanaro:

Here's the crux of the problem. Where does responsibility generally
fall for low level exception handling? I don't mean to pick on
IMAPClient. It's just a recent example and got me thinking about the
problem. Is it (generally) the responsibility of the application
author or the package author? This isn't an issue just for network
applications with a number of moving parts (socket, ssl, higher level
packages, etc). It happens in almost all applications or packages,
even if it's just to deal with exceptions raised by internal data
structures.


Here is my two cents worth.

I think it should be the resposibility of the package. The package 
should catch the low level exception and raise its own exception. Why? 
Because in a sense we are talking about implementation details. If in 
the future the package would choose an implementation that relies on 
other low-level packages, the appication should still run as before, the 
application shouldn't have to care about how the package is implemented 
and thuse shouldn't have to care about what potential low level 
exception can be raised.


Maybe python should introduce a ComponentError as a new exception. If a 
low level exception is raised in the package, the package should catch 
it and raise the ComponentErrror instead.


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


Re: How do you debug in Python? Coming from a Matlab and R user. I'm already aware of pdb.

2021-01-27 Thread Antoon Pardon




Op 27/01/21 om 05:17 schreef Dan Stromberg:

On Tue, Jan 26, 2021 at 8:13 PM Dan Stromberg  wrote:


On Tue, Jan 26, 2021 at 4:01 PM C W  wrote:


Hello everyone,

I'm a long time Matlab and R user working on data science. How do you
troubleshooting/debugging in Python?


I frequently read tracebacks and think about what's up in the code.

I also often add print functions or logging - empiricism often beats
theorizing when the problems are weird.

And once in a while I will use pudb - it's probably a pretty good fit for
a vim user like me, both being curses-based.  pdb is sad.  There are other
debuggers for Python:
https://wiki.python.org/moin/PythonDebuggingTools


BTW, there's a new tool I haven't tried yet, that sounds pretty
interesting, especially for newcomers: It's called "friendly traceback" and
can be found at https://pypi.org/project/friendly-traceback/

Oh, and of course google search is a terrific tool for deciphering error
messages.

I had a look at that and I think I still would prefer this recipe:
https://code.activestate.com/recipes/52215-get-more-information-from-tracebacks/

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


Re: Simple question - end a raw string with a single backslash ?

2020-10-19 Thread Antoon Pardon



Op 13/10/20 om 15:14 schreef Serhiy Storchaka:

13.10.20 11:52, Tony Flury via Python-list пише:

I am trying to write a simple expression to build a raw string that ends
in a single backslash. My understanding is that a raw string should
ignore attempts at escaping characters but I get this :

     >>> a = r'end\'
       File "", line 1
         a = r'end\'
       ^
    SyntaxError: EOL while scanning string literal

I interpret this as meaning that the \' is actually being interpreted as
a literal quote - is that a bug ?


r'You can\'t end raw string literal with a single "\"'

If backslash be true inner in a raw string, the above literal would end
after \'. It would be very hard to write a raw string containing both \'
and \", and even \''' and \""" (there are such strings in the stdlib).

So you have problem either with trailing backslash, or with inner
backslash followed by quotes. Both problems cannot be solved at the same
time. Python parser works as it works because initially it was easier to
implement, and now this cannot be changed because it would break some
amount of correct code.


IMO the way python does this is broken.

>>> st=r'You can\'t end raw string literal with a single "\"'
>>> print(st)

Now either the \ is special or it is not.

1) If it is special, it should change how the ' is treated but not 
appear itself.


2) If it is not special, it should just appear and not change how the ' 
is treated.


What python does here is a combination of both. The \ appears and it
changes how the ' is treated. That is IMO broken.

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


Faking stdlib

2020-10-01 Thread Antoon Pardon

I'm playing with the following idea.

Have the main program do some kind of preparations so that all further 
modules can import the standard modules via stdlib. So instead of


import sys
from itertools import chain

I could do

import stdlib.sys
from stdlib.itertools import chain

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


Re: How to install your personal module/package on site.

2020-08-21 Thread Antoon Pardon




Op 21/08/20 om 01:49 schreef Cameron Simpson:

On 16Aug2020 08:32, Marco Sulla  wrote:

Sorry, didn't read well, Apart the other suggestion, you (or your
sysop) can create a private Pypi:
https://pypi.org/project/private-pypi/


Even simpler, you can put a code repo path into your requirements.txt
(or directly with pip via its "-e" option). Example from a project
requirements.txt file:

 -e 
git+https://github.com/SpectraLogic/ds3_python3_sdk.git@v5.0.3#egg=ds3-sdk

So if you've an work internal service for your revision control you can
pull directly from that with pip. Note that the above example pulls a
particular tagged release via the "@5.0.3" suffix.


That is a nice feature. I will have a closer look at this.

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


Re: How explain why Python is easier/nicer than Lisp which has a simpler grammar/syntax?

2020-08-15 Thread Antoon Pardon
Op 15/08/20 om 15:02 schreef Chris Angelico:
> On Sat, Aug 15, 2020 at 10:45 PM Antoon Pardon
>  wrote:
>>
>>
>> I don't understand this argument. The trace back information that is
>> destroyed with this optimization, is information that isn't available
>> anyway if you write the code in an iterative fashion.
> 
> Your counter-argument applies only to recursion, but TCO applies to
> *any* tail call. Consider this:
> 
> @some_deco
> def spam(n):
> ...
> return spam(n // 2)
> 
> Not a recursive tail call and cannot be rewritten as a loop, unless
> you know for sure that some_deco returns the original function. But
> TCO can still optimize this - by collapsing the stack frames. Which
> loses traceback info, unless you deliberately preserve it.

And how often does this kind of situation come up and destroy important
trace back information? Sure you can come up with artificial examples
like the above, but if the above code gets into trouble because you
run into the recursion limit, you still will have to transform it into
a loop somehow.

>> I think writing code that is at heart a state machine would be a lot more
>> easy if python would have TCO. Then each state could be implemented by
>> a function and transitioning from one state to the next would be just
>> calling the next function.
> 
> I'm honestly not sure how much you'd gain by writing the transitions
> as additional calls. But then, I don't tend to write pure state
> machines in Python. If anything, they're "state machines with
> resumption" or something, and the additional wrinkles mean it's best
> to maintain state in a data structure and maintain code in a function,
> instead of trying to do both on the call stack.

Why are you talking about a stack?

I don't know what best suits your code but my code often enough doesn't
have enough wrinkles to bother with extra data structures.

> 
> ISTM that everyone who begs for tail recursion optimization is trying
> to write loops as recursion. Maybe that's why Python has never
> bothered - because it's an optimization for an inferior way to write
> the same logic. Prove me wrong. Show me something where it actually
> couldn't be better written iteratively, yet it still benefits from the
> optimization.

What standard do you use to measure what is inferior of superior? Sometimes
a state machines is easiest defined as a number of mutual recursive functions
that just tail call each other. So with TCO I could just write it like the
following.

def state1(...):
...
if ready_for_2:
return state2(...)
elif ready_for_7:
return state7(...)
elif finished:
return result

def state2(...):
...

and you just call it like:

result = state1(...)

Without TCO I am forced to write it as follow:

def state1(...):
...
if ready_for_2:
return state2, (...) # notice the comma between function and args.
elif ready_for_7:
return state7, (...)
elif finished:
return None, result

def state2(...):
...

and then call it like:

state = state0
args = ...
while state is not None:
   state, args = state(*args)
result = args

I realy don't see what I gain by having this loop or what I could gain by some 
extra
data structure.

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


Re: How explain why Python is easier/nicer than Lisp which has a simpler grammar/syntax?

2020-08-15 Thread Antoon Pardon
Op 7/08/20 om 18:45 schreef Chris Angelico:
> On Sat, Aug 8, 2020 at 2:21 AM <2qdxy4rzwzuui...@potatochowder.com> wrote:
>>
>> On 2020-08-07 at 17:55:45 +0200,
>> Marco Sulla  wrote:
>>> @Chris: note that "real" recursion in Python is not possible, since
>>> there's no support for tail recursion. Maybe something similar can be
>>> done using async functions.
>>
>> Python has real recursion.  Whether or not there's tail recursion on the
>> inside is an implementation detail.
> 
> More specifically: Python has real recursion. Whether or not tail
> recursion is optimized away is an implementation detail.
> 
> Tail call optimization (there's no reason to restrict it to recursion
> alone) is something a Python implementation could choose to do, but
> the trouble is that full optimization tends to destroy traceback
> information, so it's often not worth doing.

I don't understand this argument. The trace back information that is
destroyed with this optimization, is information that isn't available
anyway if you write the code in an iterative fashion.

So people are advised to rewrite tail recursive code in an iterative
fashion, which results in less trace back information and the reason
for not doing tail call optimization is, that it destroy the trace back
information they don't have anyway by transforming the code to an iterative
version.

 And the cases where
> partial optimization is of value just aren't compelling enough for
> anyone to implement it into CPython, nor any other major
> implementation (to my knowledge). The biggest uses for TCO tend to be
> the situations where recursion is the wrong solution to the problem.

I think writing code that is at heart a state machine would be a lot more
easy if python would have TCO. Then each state could be implemented by
a function and transitioning from one state to the next would be just
calling the next function.

Sure you can resolve this by writing your state function trampoline style
but it forces you into writing some boiler plate that otherwise wouldn't
be necessary.

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


Re: How to install your personal module/package on site.

2020-08-15 Thread Antoon Pardon
Op 15/08/20 om 07:33 schreef dn via Python-list:
> On 14/08/2020 22:32, Antoon Pardon wrote:
>> Well the question is in the subject.
>>
>> I have a number of modules/packages which were until recently
>> personal use only. However python is getting more popular
>> at work and some of my work was considered useful enough to
>> install in a public available spot.
>>
>> How should I approach this?
> 
> 
> Does the word "public" mean world-wide, or perhaps only amongst your 
> work-colleagues?

Only among work-colleagues.

We only want that anyone writing and running python scripts on particular 
hosts, can
easily import these modules/packages.

-- 
Antoon


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


How to install your personal module/package on site.

2020-08-14 Thread Antoon Pardon
Well the question is in the subject.

I have a number of modules/packages which were until recently
personal use only. However python is getting more popular
at work and some of my work was considered useful enough to
install in a public available spot.

How should I approach this?

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


How to turn a defaultdict into a normal dict.

2020-06-10 Thread Antoon Pardon
The problem I face is that at the building face, I need a defaultdict 
because the values are lists that are appended too. So a 
defaultdict(list) is convenient in that new entries are treated as if

the value is an empty list.

However later when I actually use it, accessing a key that is not 
present, should raise KeyError.


Is that somehow possible?

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


Re: Multithread and locking issue

2020-05-15 Thread Antoon Pardon




Op 15/05/20 om 00:36 schreef Stephane Tougard:



Hello,

A multithreaded software written in Python is connected with a Postgres
database. To avoid concurrent access issue with the database, it starts
a thread who receive all SQL request via queue.put and queue.get (it
makes only insert, so no issue with the return of the SQL request).

As long as it runs with 10 threads, no issues. At 100 threads, the
software is blocked by what I think is a locking issue.

I guess Python multithreading and queue are working good enough that it
can handle 100 threads with no issue (give me wrong here), so I guess
the problem is in my code.


It is not the number of threads in itself that can cause problems. But 
my experience is that if you have an unbounded queue and your producers 
out pace the consumers, that can cause problems. And if you have 100 
times more producers as you have consumers that can easily be the case.


So my advice it to never use an unbounded queue. If the number of 
producers is small I go for a size of 10. If the number of producers 
gets larger, I go for a size between the number of producerers and the

double of that.

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


Re: Function to avoid a global variable

2020-04-28 Thread Antoon Pardon




Op 27/04/20 om 18:39 schreef Bob van der Poel:

Thanks Chris!

At least my code isn't (quite!) as bad as the xkcd example :)

Guess my "concern" is using the initialized array in the function:

def myfunct(a, b, c=array[0,1,2,3] )

always feels like an abuse.

Has anyone seriously considered implementing  a true static variable in a
function? Is there a PEP?


You can emulate a static variable is with a closure.

def make_parseStack():

depth = 0

def parseStack(inc):
   nonlocal depth

if depth > 50:
... report error and die nicely
depth += inc

return parseStack

parseStack = make_parseStack()

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


Re: Is it possible to inheret a metaclass.

2020-04-10 Thread Antoon Pardon




Op 9/04/20 om 18:37 schreef Peter Otten:

Antoon Pardon wrote:


I am experimenting with subclasses that all need the same metaclass as the
base class. Is there a way to make the metaclass be inherited, so that you
don't have to repeat the "metaclass = MetaClass" with every subclass.


?

This is not only possible, this is the default:

  >>> class Pardon(type): pass
...

class A(metaclass=Pardon): pass

...

class B(A): pass

...

type(B)




Can you explain what is wrong with the code below:

This produces only: Calling Pardon with A.


def Pardon(cls, *args):
print("Calling Pardon with", cls)
return type(cls, *args)

class A(metaclass=Pardon): pass

class B(A): pass
--
https://mail.python.org/mailman/listinfo/python-list


  1   2   3   4   5   6   7   8   9   10   >