Re: python3 package import difference?

2024-08-08 Thread Cameron Simpson via Python-list
On 08Aug2024 21:55, Gilmeh Serda wrote: I guess in a sense Py2 was smarter figuring out what whent where and where it came from. Or it was a bad hack that has been removed. No, Python 2 offered less control. -- https://mail.python.org/mailman/listinfo/python-list

Re: python3 package import difference?

2024-08-07 Thread Cameron Simpson via Python-list
On 07Aug2024 08:35, Tobiah wrote: When I do the same import with python3, I get: Traceback (most recent call last): File "/home/toby/me", line 1, in import rcs.dbi File "/usr/regos-1.0/lib/python/rcs/dbi/__init__.py", line 1, in from dbi import * ModuleNotFoundEr

Re: Help needed - - running into issues with python and its tools

2024-08-03 Thread Cameron Simpson via Python-list
On 03Aug2024 16:34, o1bigtenor wrote: So please - - - how do I set up a venv so that I can install and run python 3.12 (and other needed programs related to 3.12) inside? Maybe this github comment will help with this: https://github.com/orgs/micropython/discussions/10255#discussioncomment-444

Re: Best use of "open" context manager

2024-07-07 Thread Cameron Simpson via Python-list
On 07Jul2024 22:22, Rob Cliffe wrote: it's legal, but doesn't work (trying to access the file after "with f" raises the same     ValueError: I/O operation on closed file. Just to this: of course. The with closes the file. But my version runs the with after the try/except. -- https://mail.py

Re: Best use of "open" context manager

2024-07-07 Thread Cameron Simpson via Python-list
On 07Jul2024 22:22, Rob Cliffe wrote: Remember, the `open()` call returns a file object _which can be used as a context manager_. It is separate from the `with` itself. Did you test this?     f = open(FileName) as f: is not legal syntax. No. You're right, remove the "as f:". it's legal, but

Re: Best use of "open" context manager

2024-07-06 Thread Cameron Simpson via Python-list
On 06Jul2024 11:49, Rob Cliffe wrote: try:     f = open(FileName) as f:     FileLines = f.readlines() except FileNotFoundError:     print(f"File {FileName} not found")     sys.exit() # I forgot to put "f.close()" here -:) for ln in File Lines:         print("I do a lot of processing here")      

Re: Suggested python feature: allowing except in context maneger

2024-06-14 Thread Cameron Simpson via Python-list
On 14Jun2024 09:07, Yair Eshel wrote: Cameron, I'm not really sure I got your point. I've used the "file not found" exception as an example for a behavior typical on context managers. This could be a failure to connect to DB, or threads. It also applies to any kind of possible exception, whether

Re: Anonymous email users

2024-06-14 Thread Cameron Simpson via Python-list
On 14Jun2024 18:00, avi.e.gr...@gmail.com wrote: I notice that in some recent discussions, we have users who cannot be replied to directly as their email addresses are not valid ones, and I believe on purpose. Examples in the thread I was going to reply to are: henh

Re: Suggested python feature: allowing except in context maneger

2024-06-13 Thread Cameron Simpson via Python-list
On 13Jun2024 19:44, dieter.mau...@online.de wrote: Why not use: ``` try: with open()... ... except FileNotFoundError: ... ``` This is exactly what the OP was expressing dissatisfaction with. I'm -1 on the idea myself - not every combination of things needs additional syntactic support,

Re: Fwd: IDLE: clearing the screen

2024-06-04 Thread Cameron Simpson via Python-list
On 04Jun2024 22:43, Rob Cliffe wrote: import os def cls(): x=os.system("cls") Now whenever you type cls() it will clear the screen and show the prompt at the top of the screen. (The reason for the "x=" is: os.system returns a result, in this case 0.  When you evaluate an expression in the IDE

Re: Flubbed it in the second interation through the string: range error... HOW?

2024-05-28 Thread Cameron Simpson via Python-list
On 29May2024 01:14, Thomas Passin wrote: Also, it's 2024 ... time to start using f-strings (because they are more readable than str.format()) By which Thomas means stuff like this: print(f'if block {name[index]} and index {index}') Notice the leading "f'". Personally I wouldn't even go t

Re: Terminal Emulator

2024-05-14 Thread Cameron Simpson via Python-list
On 14May2024 18:44, Gordinator wrote: I wish to write a terminal emulator in Python. I am a fairly competent Python user, and I wish to try a new project idea. What references can I use when writing my terminal emulator? I wish for it to be a true terminal emulator as well, not just a Tk text

Re: First two bytes of 'stdout' are lost

2024-04-11 Thread Cameron Simpson via Python-list
On 11Apr2024 14:42, Olivier B. wrote: I am trying to use StringIO to capture stdout, in code that looks like this: import sys from io import StringIO old_stdout = sys.stdout sys.stdout = mystdout = StringIO() print( "patate") mystdout.seek(0) sys.stdout = old_stdout print(mystdout.read()) Well

Re: How to Add ANSI Color to User Response

2024-04-11 Thread Cameron Simpson via Python-list
On 10Apr2024 23:41, Alan Gauld wrote: Normally, for any kind of fancy terminal work, I'd say use curses. My problem with curses is that it takes over the whole terminal - you need to manage everything from that point on. Great if you want it (eg some full-terminal tool like `top`) but comple

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-07 Thread Cameron Simpson via Python-list
On 06Mar2024 15:12, Jacob Kruger wrote: So, this does not make sense to me in terms of the following snippet from the official python docs page: https://docs.python.org/3/faq/programming.html "In Python, variables that are only referenced inside a function are implicitly global. If a variable

Re: Variable scope inside and outside functions - global statement being overridden by assignation unless preceded by reference

2024-03-05 Thread Cameron Simpson via Python-list
On 05Mar2024 20:13, Jacob Kruger wrote: Now, what almost seems to be occurring, is that while just manipulating the contents of a referenced variable is fine in this context, the moment I try to reassign it, that's where the issue is occurring . Because there are no variable definitions in Py

Re: Can one output something other than 'nan' for not a number values?

2024-02-16 Thread Cameron Simpson via Python-list
On 16Feb2024 22:12, Chris Green wrote: I'm looking for a simple way to make NaN values output as something like '-' or even just a space instead of the string 'nan'. This would then make it much easier to handle outputting values from sensors when not all sensors are present. So, for example,

Re: A question about import

2024-02-16 Thread Cameron Simpson via Python-list
On 16Feb2024 20:32, MRAB wrote: On 2024-02-16 20:07, Gabor Urban via Python-list wrote: I need something about modules to be clarified. Suppose I have written a module eg: ModuleA which imports an other module, let us say the datetime. If I import ModuleA in a script, will be datetime importe

Re: Is there a way to implement the ** operator on a custom object

2024-02-09 Thread Cameron Simpson via Python-list
On 09Feb2024 18:56, Left Right wrote: But, more to the point: extending collections.abc.Mapping may or may not be possible in OP's case. We don't yet know if that's what the OP had in mind yet, anyway. Also, if you are doing this through inheritance, this seems really convoluted: why not jus

Re: Is there a way to implement the ** operator on a custom object

2024-02-08 Thread Cameron Simpson via Python-list
On 08Feb2024 12:21, tony.fl...@btinternet.com wrote: I know that mappings by default support the ** operator, to unpack the mapping into key word arguments. Has it been considered implementing a dunder method for the ** operator so you could unpack an object into a key word argument, and the

Re: How would you name this dictionary?

2024-01-21 Thread Cameron Simpson via Python-list
On 21Jan2024 23:39, bagra...@live.com wrote: class NameMe(dict): def __missing__(self, key): return key I would need to know more about what it might be used for. What larger problem led you to writing a `dict` subclass with this particular `__missing__` implementation? -- https:/

Re: Type hints - am I doing it right?

2023-12-13 Thread Cameron Simpson via Python-list
On 13Dec2023 09:19, Frank Millman wrote: I am adding type hints to my code base. [...] In the other module I have this - def config_database(db_params): To add a type hint, I now have this - def config_database(db_params: configparser.SectionProxy): To get this to work, I have to

Re: Newline (NuBe Question)

2023-11-15 Thread Cameron Simpson via Python-list
On 15Nov2023 07:25, Grizzy Adams wrote: Have this (from an online "classes" tutorial) Response inline below. students = [] grades = [] for s in geographyClass: students.append(geographyStudent(s)) for s in students: grades.append(s.school) grades.append(

Re: Checking if email is valid

2023-11-01 Thread Cameron Simpson via Python-list
On 02Nov2023 17:04, Chris Angelico wrote: On Thu, 2 Nov 2023 at 15:20, AVI GROSS via Python-list wrote: Yes, it would be nice if there was a syntax for sending a test message sort of like an ACK that is not delivered to the recipient but merely results in some status being sent back such as D

Re: Checking if email is valid

2023-11-01 Thread Cameron Simpson via Python-list
On 01Nov2023 14:08, Grant Edwards wrote: On 2023-11-01, Simon Connah via Python-list wrote: I'm building a simple project using smtplib and have a question. I've been doing unit testing but I'm not sure how to check if an email message is valid. [...] Could someone push me in the right direc

Re: return type same as class gives NameError.

2023-10-22 Thread Cameron Simpson via Python-list
On 22Oct2023 17:50, Antoon Pardon wrote: I have the following small module: =-=-=-=-=-=-=-=-=-=-=-= 8< =-=-=-=-=-=-=-=-=-=-=-=-= class Pnt (NamedTuple): x: float y: float def __add__(self, other: PNT) -> Pnt: return Pnt(self[0] + other[0], self[1] + other[1]) When this functio

Re: Where I do ask for a new feature

2023-10-19 Thread Cameron Simpson via Python-list
On 19Oct2023 20:16, Bongo Ferno wrote: A with statement makes clear that the alias is an alias and is local, and it automatically clears the variable after the block code is used. No it doesn't: >>> with open('/dev/null') as f: ... print(f) ... <_io.TextIOWrapper name='/dev/

Re: Why doc call `__init__` as a method rather than function?

2023-09-17 Thread Cameron Simpson via Python-list
On 15Sep2023 10:49, scruel tao wrote: ```python class A: ... def __init__(self): ... pass ... ``` On many books and even the official documents, it seems that many authors prefer to call `__init__` as a "method" rather than a "function". The book PYTHON CRASH COURSE mentioned that "A

Re: Imports and dot-notation

2023-08-09 Thread Cameron Simpson via Python-list
On 09Aug2023 12:30, Oliver Schinagl wrote: Looking at a python projects code and repository layout, we see the following directory structure. /project/core /project/components/module1 ... /project/components/moduleN /projects/util (this is far from complete, but enough to help paint a pictur

Re: Where is the error?

2023-08-07 Thread Cameron Simpson via Python-list
On 07Aug2023 08:02, Barry wrote: On 7 Aug 2023, at 05:28, Cameron Simpson via Python-list wrote: Used to use a Pascal compiler once which was uncannily good at suggesting where you'd missing a semicolon. Was that on DEC VMS? It was a goal at DEC for its compilers to do this well.

Re: Where is the error?

2023-08-06 Thread Cameron Simpson via Python-list
On 06Aug2023 22:41, Peter J. Holzer wrote: Mostly, error messages got a lot better in Python 3.10, but this one had me scratching my head for a few minutes. Consider this useless and faulty script: r = { "x": (1 + 2 + 3

Re: isinstance()

2023-08-02 Thread Cameron Simpson via Python-list
On 03Aug2023 10:14, dn wrote: Can you please explain why a multi-part second-argument must be a tuple and not any other form of collection-type? The signature is: isinstance(object, classinfo) leading to "classinfo" of: 1/ a single class/type, eg int 2/ a tuple of same, eg ( int, str, ) 3/ a

Re: Should NoneType be iterable?

2023-06-20 Thread Cameron Simpson via Python-list
On 21Jun2023 10:09, Chris Angelico wrote: On Wed, 21 Jun 2023 at 09:59, Cameron Simpson via Python-list wrote: I wasted some time the other evening on an API which returned a string or None. My own API, and the pain it caused tells me that that API design choice isn't good (it's an

Re: Should NoneType be iterable?

2023-06-20 Thread Cameron Simpson via Python-list
On 21Jun2023 03:01, Greg Ewing wrote: On 20/06/23 7:36 pm, Barry wrote: I have some APIs that do return None or a list. The None says that a list is not available and that the caller is responsible with dealing in a application-domain specific with with that situation. In that case, the calle