Re: Fwd: A typing question
On 30/10/2022 11.59, Paulo da Silva wrote: Solution (below) will not work if the mention of Foos in GLOBALS is a forward-reference. Either move GLOBALS to suit, or surround "Foos" with quotes. This is the problem for me. So far, without typing, I used to have some config and globals classes, mostly to just group definitions an make the program more readable. A matter of taste and style. Agreed, a good practice. Now, "typing" is breaking this, mostly because of this forward reference issue. As a first step, use the quotation-marks to indicate that such will be defined later in the code:- class GLOBALS: Foos: Optional[Foos]=None class GLOBALS: Foos: Optional["Foos"]=None Later, as gather (typing) expertise, can become more sophisticated, as-and-when... The funny thing is that if I replace foos by Foos it works because it gets known by the initial initialization :-) ! Is the objective to write (good) code, or merely to satisfy the type-checker? Something that is misleading is not going to be appreciated by others (including the +6-months you), eg a = a + 1 # decrement total Typing is not compulsory, and has been designed so that we can implement it a bit at a time, eg only one function amongst many contained by a module - if that's the only code that requires maintenance/update. Best not to create "technical debt" though! -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: A typing question
On 10/29/2022 1:45 PM, Paulo da Silva wrote: Hi! Consider this simple script ... ___ from typing import List, Optional class GLOBALS: foos=None class Foo: def __init__(self): pass class Foos: Foos: List[Foo]=[] # SOME GLOBALS ARE USED HERE in a real script def __init__(self): pass GLOBALS.foos: Optional[Foos]=Foos() ___ Running mypy on it: pt9.py:18: error: Type cannot be declared in assignment to non-self attribute pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None") Line 18 is last line and pt9.py is the scrip. Replacing last line by GLOBALS.foos=Foos() and running mypy still gives the second error. pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None") What is the common practice in these cases? Thank you. I don't understand class Foos: Foos: List[Foo]=[] If "Foos" is supposed to be a class attribute, then it cannot have the same name as the class. Perhaps you meant the class to be named "Foo". Then a class attribute of Foos: List[Foo]=[] might barely make sense. Even then, remember that each instance of Foo would only know about the same (originally empty) list Foos. Is that really what you want? -- https://mail.python.org/mailman/listinfo/python-list
Re: Fwd: A typing question
Às 22:34 de 29/10/22, dn escreveu: Out of interest, tested snippet in PyCharm, cf native-mypy. It flags the original: GLOBALS.foos: Optional[Foos]=Foos() but not the fall-back: GLOBALS.foos=Foos() Must admit, the first query coming to mind was: why is the typing taking place at initialisation-time, rather than within the (class) definition? At definition time "foos" has already been typed as None by implication! Solution (below) will not work if the mention of Foos in GLOBALS is a forward-reference. Either move GLOBALS to suit, or surround "Foos" with quotes. This is the problem for me. So far, without typing, I used to have some config and globals classes, mostly to just group definitions an make the program more readable. A matter of taste and style. Now, "typing" is breaking this, mostly because of this forward reference issue. The funny thing is that if I replace foos by Foos it works because it gets known by the initial initialization :-) ! from typing import List, Optional class GLOBALS: Foos: Optional[Foos]=None class Foo: def __init__(self): pass class Foos: Foos: List[Foo]=[] # SOME GLOBALS ARE USED HERE def __init__(self): pass GLOBALS.Foos=Foos() Also, these days (Python version allowing) importing "List" is unnecessary. Instead could use "list". On 30/10/2022 10.23, Sam Ezeh wrote: Do you want the following? ``` from typing import List, Optional class GLOBALS: foos: Optional[Foos] = None class Foo: def __init__(self): pass class Foos: Foos: List[Foo] = [] def __init__(self): pass GLOBALS.foos = Foos() ``` Kind regards, Sam Ezeh On Sat, 29 Oct 2022 at 22:13, Paulo da Silva < p_d_a_s_i_l_v_a...@nonetnoaddress.pt> wrote: Hi! Consider this simple script ... ___ from typing import List, Optional class GLOBALS: foos=None class Foo: def __init__(self): pass class Foos: Foos: List[Foo]=[] # SOME GLOBALS ARE USED HERE in a real script def __init__(self): pass GLOBALS.foos: Optional[Foos]=Foos() ___ Running mypy on it: pt9.py:18: error: Type cannot be declared in assignment to non-self attribute pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None") Line 18 is last line and pt9.py is the scrip. Replacing last line by GLOBALS.foos=Foos() and running mypy still gives the second error. pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None") What is the common practice in these cases? Thank you. -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: str.replace() when str contains \
On 2022-10-29 19:21, Bernard LEDRU wrote: Hello, https://docs.python.org/3/library/stdtypes.html#string-methods str.replace(old, new[, count])¶ Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. Attention when the string contains the escape character. Consider : a="H:\2023"; print(a.replace("\\","/")) H:3 a="H:\a2023"; print(a.replace("\\","/")) H:2023 a="H:\_2023"; print(a.replace("\\","/")) H:/_2023 In a plain string literal, \ followed by 1..3 of the digits 0..7 is an octal (base 8) escape sequence, and \ followed by 'a' is the bel character, so '\7' == '\07' == '\007' == '\a' == '\x07'. -- https://mail.python.org/mailman/listinfo/python-list
Re: str.replace() when str contains \
I believe you would do well to print a, before trying to transform it into something else. '\2' is chr(2). '\a' is the bell character, and is unprintable. '\_' is two characters though. On Sat, Oct 29, 2022 at 2:12 PM Bernard LEDRU wrote: > Hello, > > https://docs.python.org/3/library/stdtypes.html#string-methods > > str.replace(old, new[, count])¶ > Return a copy of the string with all occurrences of substring old > replaced by new. If the optional argument count is given, only the first > count occurrences are replaced. > > Attention when the string contains the escape character. > > Consider : > > >>> a="H:\2023"; print(a.replace("\\","/")) > H:3 > >>> a="H:\a2023"; print(a.replace("\\","/")) > H:2023 > >>> a="H:\_2023"; print(a.replace("\\","/")) > H:/_2023 > > Best regards, > Bernard LEDRU > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: str.replace() when str contains \
Better to use raw strings whenever backslashes are involved. On 10/29/2022 2:21 PM, Bernard LEDRU wrote: Hello, https://docs.python.org/3/library/stdtypes.html#string-methods str.replace(old, new[, count])¶ Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. Attention when the string contains the escape character. Consider : a="H:\2023"; print(a.replace("\\","/")) H:3 a="H:\a2023"; print(a.replace("\\","/")) H:2023 These examples actually produce an "H" with a symbol after it, a different symbol for each. Probably the message posting system can't display it, but I see it in a python terminal session on Windows. So the first a="H:\2023" example produces "Hx", where x is some symbol. Since there is no backslash, no character gets replaced. IOW, a="H:\2023"; print(a) and a="H:\2023"; print(a.replace("\\","/")) print the same thing. a="H:\_2023"; print(a.replace("\\","/")) H:/_2023 What did you expect or want to happen? -- https://mail.python.org/mailman/listinfo/python-list
Re: Fwd: A typing question
Out of interest, tested snippet in PyCharm, cf native-mypy. It flags the original: GLOBALS.foos: Optional[Foos]=Foos() but not the fall-back: GLOBALS.foos=Foos() Must admit, the first query coming to mind was: why is the typing taking place at initialisation-time, rather than within the (class) definition? At definition time "foos" has already been typed as None by implication! Solution (below) will not work if the mention of Foos in GLOBALS is a forward-reference. Either move GLOBALS to suit, or surround "Foos" with quotes. Also, these days (Python version allowing) importing "List" is unnecessary. Instead could use "list". On 30/10/2022 10.23, Sam Ezeh wrote: Do you want the following? ``` from typing import List, Optional class GLOBALS: foos: Optional[Foos] = None class Foo: def __init__(self): pass class Foos: Foos: List[Foo] = [] def __init__(self): pass GLOBALS.foos = Foos() ``` Kind regards, Sam Ezeh On Sat, 29 Oct 2022 at 22:13, Paulo da Silva < p_d_a_s_i_l_v_a...@nonetnoaddress.pt> wrote: Hi! Consider this simple script ... ___ from typing import List, Optional class GLOBALS: foos=None class Foo: def __init__(self): pass class Foos: Foos: List[Foo]=[] # SOME GLOBALS ARE USED HERE in a real script def __init__(self): pass GLOBALS.foos: Optional[Foos]=Foos() ___ Running mypy on it: pt9.py:18: error: Type cannot be declared in assignment to non-self attribute pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None") Line 18 is last line and pt9.py is the scrip. Replacing last line by GLOBALS.foos=Foos() and running mypy still gives the second error. pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None") What is the common practice in these cases? Thank you. -- https://mail.python.org/mailman/listinfo/python-list -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list
Re: str.replace() when str contains \
> On 29 Oct 2022, at 22:14, Bernard LEDRU wrote: > > Hello, > > https://docs.python.org/3/library/stdtypes.html#string-methods > > str.replace(old, new[, count])¶ > Return a copy of the string with all occurrences of substring old replaced by > new. If the optional argument count is given, only the first count > occurrences are replaced. > > Attention when the string contains the escape character. > > Consider : > a="H:\2023"; print(a.replace("\\","/")) > H:3 a="H:\a2023"; print(a.replace("\\","/")) > H:2023 a="H:\_2023"; print(a.replace("\\","/")) > H:/_2023 String a does not quote the \ after :. You need “H:\\2023” etc. check what a is before the replace with a print(repr(a)) Barry > > Best regards, > Bernard LEDRU > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Fwd: A typing question
Do you want the following? ``` from typing import List, Optional class GLOBALS: foos: Optional[Foos] = None class Foo: def __init__(self): pass class Foos: Foos: List[Foo] = [] def __init__(self): pass GLOBALS.foos = Foos() ``` Kind regards, Sam Ezeh On Sat, 29 Oct 2022 at 22:13, Paulo da Silva < p_d_a_s_i_l_v_a...@nonetnoaddress.pt> wrote: > Hi! > > Consider this simple script ... > > ___ > from typing import List, Optional > > class GLOBALS: > foos=None > > class Foo: > > def __init__(self): > pass > > class Foos: > Foos: List[Foo]=[] > # SOME GLOBALS ARE USED HERE in a real script > > def __init__(self): > pass > > GLOBALS.foos: Optional[Foos]=Foos() > ___ > > Running mypy on it: > pt9.py:18: error: Type cannot be declared in assignment to non-self > attribute > pt9.py:18: error: Incompatible types in assignment (expression has type > "Foos", variable has type "None") > Line 18 is last line and pt9.py is the scrip. > > Replacing last line by > GLOBALS.foos=Foos() > and running mypy still gives the second error. > pt9.py:18: error: Incompatible types in assignment (expression has type > "Foos", variable has type "None") > > What is the common practice in these cases? > > Thank you. > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
A typing question
Hi! Consider this simple script ... ___ from typing import List, Optional class GLOBALS: foos=None class Foo: def __init__(self): pass class Foos: Foos: List[Foo]=[] # SOME GLOBALS ARE USED HERE in a real script def __init__(self): pass GLOBALS.foos: Optional[Foos]=Foos() ___ Running mypy on it: pt9.py:18: error: Type cannot be declared in assignment to non-self attribute pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None") Line 18 is last line and pt9.py is the scrip. Replacing last line by GLOBALS.foos=Foos() and running mypy still gives the second error. pt9.py:18: error: Incompatible types in assignment (expression has type "Foos", variable has type "None") What is the common practice in these cases? Thank you. -- https://mail.python.org/mailman/listinfo/python-list
str.replace() when str contains \
Hello, https://docs.python.org/3/library/stdtypes.html#string-methods str.replace(old, new[, count])¶ Return a copy of the string with all occurrences of substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced. Attention when the string contains the escape character. Consider : a="H:\2023"; print(a.replace("\\","/")) H:3 a="H:\a2023"; print(a.replace("\\","/")) H:2023 a="H:\_2023"; print(a.replace("\\","/")) H:/_2023 Best regards, Bernard LEDRU -- https://mail.python.org/mailman/listinfo/python-list
Re: Any PyQt developers here?
On 10/28/22 21:31, DFS wrote: > I found one person that said they did it but their syntax didn't work. > But it doesn't throw an error either. > > model.setData(model.index(tblRow, col), font, Qt.FontRole) I wouldn't expect that to work but it's understandable why it didn't throw an error. setData() is used to edit the contents of the model at the provided index. Remember a model can store anything. All this does is replace whatever was at that index with a Font object instance. I'm puzzled why you keep trying to mess with the model when it's the view that does the actual font setting. Remember that a single model can be used with more than one view at the same time, each view implementing its own style. Thus a model has no information like fonts in it, nor should it, other than perhaps HTML text markup that the view will render. Did you consult the folk on the PyQt mailing list? Or even the main Qt lists? This isn't language-specific stuff you're asking about. -- https://mail.python.org/mailman/listinfo/python-list