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


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


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


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: 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: 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: 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: Beazley's Problem

2024-10-06 Thread Antoon Pardon via Python-list

Op 23/09/2024 om 09:44 schreef Annada Behera via Python-list:

The "next-level math trick" Newton-Raphson has nothing to do with
functional programming. I have written solvers in purely iterative
style.


What is your point. Any problem solved in a functional style can
also be solved in a pure interative style. So you having written
something in an interative style doesn't contradict Newton-Raphson being 
expressable in a functional style.

  As far as I know, Newton-Raphson is the opposite of functional
programming as you iteratively solve for the root. Functional programming
is stateless where you are not allowed to store any state (current best
guess root).


That doesn't prevent you from passing state along as a parameter,
usualy in some helper function.

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