Match statement with literal strings
This gives the expected results: with open(data_file, newline="") as reader: csvreader = csv.DictReader(reader) for row in csvreader: #print(row) match row[RULE_TYPE]: case "RANGE": print("range") case "MANDATORY": print("mandatory") case _: print("nothing to do") This: RANGE = "RANGE" MANDATORY = "MANDATORY" with open(data_file, newline="") as reader: csvreader = csv.DictReader(reader) for row in csvreader: #print(row) match row[RULE_TYPE]: case RANGE: print("range") case MANDATORY: print("mandatory") case _: print("nothing to do") Gives (and I don't understand why): SyntaxError: name capture 'RANGE' makes remaining patterns unreachable -- https://mail.python.org/mailman/listinfo/python-list
Re: Match statement with literal strings
> > The bytecode compiler doesn't know that you intend RANGE > to be a constant -- it thinks it's a variable to bind a > value to. > > To make this work you need to find a way to refer to the > value that isn't just a bare name. One way would be to > define your constants using an enum: > > class Options(Enum): > RANGE = "RANGE" > MANDATORY = "MANDATORY" > > match stuff: > case Options.RANGE: >... > case Options.MANDATORY: >... > Got it, thank you. On Wed, Jun 7, 2023 at 6:01 PM Greg Ewing via Python-list < python-list@python.org> wrote: > On 8/06/23 10:18 am, Jason Friedman wrote: > > SyntaxError: name capture 'RANGE' makes remaining patterns unreachable > > The bytecode compiler doesn't know that you intend RANGE > to be a constant -- it thinks it's a variable to bind a > value to. > > To make this work you need to find a way to refer to the > value that isn't just a bare name. One way would be to > define your constants using an enum: > > class Options(Enum): > RANGE = "RANGE" > MANDATORY = "MANDATORY" > > match stuff: > case Options.RANGE: >... > case Options.MANDATORY: >... > > -- > Greg > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Trouble with defaults and timeout decorator
I'm writing a database connectivity module to be used by other modules and leveraging the jaydebeapi module. >From what I can tell jaydebeapi contains no built-in timeout capability, so then I turned to https://pypi.org/project/timeout-decorator/. My goal is to have a default timeout of, say, 10 seconds, which can be overridden by the caller. import jaydebeapi from timeout_decorator import timeout class Database: database_connection = None database_name, user_name, password, host, port = stuff timeout = None def __init__(self, timeout=10): self.timeout = timeout @timeout(self.timeout) def get_connection(self): if not self.database_connection: self.database_connection = jaydebeapi.connect(some_args) return self.database_connection The trouble occurs on line 12 with: NameError: name 'self' is not defined -- https://mail.python.org/mailman/listinfo/python-list
How to find the full class name for a frame
import inspect def my_example(arg1, arg2): print(inspect.stack()[0][3]) my_frame = inspect.currentframe() args,_,_,values = inspect.getargvalues(my_frame) args_rendered = [f"{x}: {values[x]}" for x in args] print(args_rendered) my_example("a", 1) The above "works" in the sense it prints what I want, namely the method name (my_example) and the arguments it was called with. My question is: let's say I wanted to add a type hint for my_frame. my_frame: some_class_name = inspect.currentframe() What would I put for some_class_name? "frame" (without quotations) is not recognized, Nor is inspect.frame. -- https://mail.python.org/mailman/listinfo/python-list
Re: How to find the full class name for a frame
> My question is: let's say I wanted to add a type hint for my_frame. > > > > my_frame: some_class_name = inspect.currentframe() > > > > What would I put for some_class_name? > > "frame" (without quotations) is not recognized, > > Nor is inspect.frame. > > We know Python code is executed in an execution frame. > (https://docs.python.org/3/reference/executionmodel.html?highlight=frame) > > We are told "Frame objects Frame objects represent execution frames." > (https://docs.python.org/3/reference/datamodel.html?highlight=frame). > The word "represent" conflicts with the idea of "are". > > 'Under the hood' inspect calls sys._current_frames() > (https://docs.python.org/3/library/sys.html?highlight=frame). That code > is: > > def _getframe(*args, **kwargs): # real signature unknown > """ > Return a frame object from the call stack. > > If optional integer depth is given, return the frame object that many > calls below the top of the stack. If that is deeper than the call > stack, ValueError is raised. The default for depth is zero, returning > the frame at the top of the call stack. > > This function should be used for internal and specialized purposes > only. > """ > pass > > Which rather suggests that if the sys library doesn't know the > signature, then neither typing nor we mere-mortals are going to do so, > either. > > > Theory: the concept of a frame does not really exist at the Python-level > (remember "represents"). Frames (must) exist at the C-level > ( > https://docs.python.org/3/c-api/frame.html?highlight=frame#c.PyFrameObject) > > of the virtual-machine - where typing is not a 'thing'. > > > It's an interesting question. Perhaps a better mind than mine can give a > better answer? > > Thank you DN. My ultimate goal is a function I'd put in my main library which other functions could leverage, something like: function_in_another_file(arg): logger.info(my_main_module.render_calling_info(inspect.stack(), inspect.currentframe()) # Now do the work -- https://mail.python.org/mailman/listinfo/python-list
Re: How to find the full class name for a frame
> > Jason Friedman wrote at 2023-8-3 21:34 -0600: > > ... > >my_frame = inspect.currentframe() > > ... > >My question is: let's say I wanted to add a type hint for my_frame. > > `my_frame` will be an instance of `Types.FrameType`. > Confirmed. Thank you! -- https://mail.python.org/mailman/listinfo/python-list
Context manager for database connection
I want to be able to write code like this: with Database() as mydb: conn = mydb.get_connection() cursor = conn.get_cursor() cursor.execute("update table1 set x = 1 where y = 2") cursor.close() cursor = conn.get_cursor() cursor.execute("update table2 set a = 1 where b = 2") cursor.close() I'd like for both statements to succeed and commit, or if either fails to stop and for all to rollback. Is what I have below correct? import jaydebeapi as jdbc class Database: database_connection = None def __init__(self, auto_commit: bool = False): self.database_connection = jdbc.connect(...) self.database_connection.jconn.setAutoCommit(auto_commit) def __enter__(self) -> jdbc.Connection: return self def __exit__(self, exception_type: Optional[Type[BaseException]], exception_value: Optional[BaseException], traceback: Optional[types.TracebackType]) -> bool: if exception_type: self.database_connection.rollback() else: self.database_connection.commit() self.database_connection.close() def get_connection(self) -> jdbc.Connection: return self.database_connection -- https://mail.python.org/mailman/listinfo/python-list
Generating documentation with Sphinx
I have two questions, please (this is after reading https://docs.readthedocs.io/en/stable/guides/cross-referencing-with-sphinx.html#automatically-label-sections ). This is my project structure: my_project api stuff1.py stuff2.py lib stuff3.py stuff4.py main_application.py In my_project/main_application.py I have two functions: def construct_response(exit_code: int, message: str) -> Response: """ Construct a Flask-suitable response :param exit_code: 0 or something else :param message: something useful :return: a Flask-suitable response """ @app.route(f"/{version}/", methods=[GET, POST]) def serve(page) -> Response: """ Do some stuff and return 200 or 500 :param page: this is a REST endpoint we are advertising to callers :return: a Flask Response generated by construct_response """ Question 1: how do I embed in the serve function docstring a link to the construct_response function? Question 2: how do I embed in, for example, lib/stuff3.py, a link to the construct_response function? I tried: :ref:`my_project/api/stuff1:construct_response` but that gives an undefined label warning. -- https://mail.python.org/mailman/listinfo/python-list
Re: Generating documentation with Sphinx
> > def construct_response(exit_code: int, message: str) -> Response: > """ > Construct a Flask-suitable response > > :param exit_code: 0 or something else > :param message: something useful > :return: a Flask-suitable response > """ > > > @app.route(f"/{version}/", methods=[GET, POST]) > def serve(page) -> Response: > """ > Do some stuff and return 200 or 500 > > :param page: this is a REST endpoint we are advertising to callers > :return: a Flask Response generated by construct_response > """ > > > Question 1: how do I embed in the serve function docstring a link to the > construct_response function? > > > Question 2: how do I embed in, for example, lib/stuff3.py, a link to the > construct_response function? > > > I tried: > :ref:`my_project/api/stuff1:construct_response` > > but that gives an undefined label warning. > I can answer my own Question 1: :func:`construct_response` -- https://mail.python.org/mailman/listinfo/python-list
Re: Generating documentation with Sphinx
And I can answer my own Question 2: :func:`my_project.main_application.construct_response` On Mon, Aug 28, 2023 at 1:39 PM Jason Friedman wrote: > def construct_response(exit_code: int, message: str) -> Response: >> """ >> Construct a Flask-suitable response >> >> :param exit_code: 0 or something else >> :param message: something useful >> :return: a Flask-suitable response >> """ >> >> >> @app.route(f"/{version}/", methods=[GET, POST]) >> def serve(page) -> Response: >> """ >> Do some stuff and return 200 or 500 >> >> :param page: this is a REST endpoint we are advertising to callers >> :return: a Flask Response generated by construct_response >> """ >> >> >> Question 1: how do I embed in the serve function docstring a link to the >> construct_response function? >> >> >> Question 2: how do I embed in, for example, lib/stuff3.py, a link to the >> construct_response function? >> >> >> I tried: >> :ref:`my_project/api/stuff1:construct_response` >> >> but that gives an undefined label warning. >> > > I can answer my own Question 1: > :func:`construct_response` > -- https://mail.python.org/mailman/listinfo/python-list
Re: Generating documentation with Sphinx
def construct_response(exit_code: int, message: str) -> Response: >> """ >> Construct a Flask-suitable response >> >> :param exit_code: 0 or something else >> :param message: something useful >> :return: a Flask-suitable response >> """ >> >> >> @app.route(f"/{version}/", methods=[GET, POST]) >> def serve(page) -> Response: >> """ >> Do some stuff and return 200 or 500 >> >> :param page: this is a REST endpoint we are advertising to callers >> :return: a Flask Response generated by construct_response >> """ >> >> >> Question 1: how do I embed in the serve function docstring a link to the >> construct_response function? >> >> >> Question 2: how do I embed in, for example, lib/stuff3.py, a link to the >> construct_response function? >> >> >> I tried: >> :ref:`my_project/api/stuff1:construct_response` >> >> but that gives an undefined label warning. >> > > I can answer my own Question 1: > :func:`construct_response` > And I can answer my own Question 2: :func:`my_project.main_application.construct_response` -- https://mail.python.org/mailman/listinfo/python-list
Re: Help
On Sun, Nov 5, 2023 at 1:23 PM office officce via Python-list < python-list@python.org> wrote: > which python version is better to be used and how to make sure it works on > my window 10 because i downloaded it and it never worked so I uninstall to > do that again please can you give me the steps on how it will work perfectly > > If you are just starting out, the most recent version is 3.12 and is probably your best choice. When you say it never worked, can you describe in more detail what you did and what error messages you encountered? This mailing list does not accept screenshots. -- https://mail.python.org/mailman/listinfo/python-list
Re: new here
On Wed, Aug 21, 2024 at 4:04 PM Daniel via Python-list < python-list@python.org> wrote: > > An example of use, here's a weather service tied to a finger. Put your > city name as the user. This isn't mine, but it is inspiring. Example: > > finger mi...@graph.no > > For all options, go to the help finger: > > finger h...@graph.no Quite cool! -- https://mail.python.org/mailman/listinfo/python-list
Re: Two python issues
> > (a) An error-prone "feature" is returning -1 if a substring is not found > by "find", since -1 currently refers to the last item. An example: > > >>> s = 'qwertyuiop' > >>> s[s.find('r')] > 'r' > >>> s[s.find('p')] > 'p' > >>> s[s.find('a')] > 'p' > >>> > > If "find" is unsuccessful, an error message is the only clean option. > Moreover, using index -1 for the last item is a bad choice: it should be > len(s) - 1 (no laziness!). > I'm not sure if this answers your objection but the note in the documentation (https://docs.python.org/3/library/stdtypes.html#str.find) says: The find() method should be used only if you need to know the position of sub. I think the use case above is a little bit different. -- https://mail.python.org/mailman/listinfo/python-list
Re: [RELEASE] Python 3.13.1, 3.12.8, 3.11.11, 3.10.16 and 3.9.21 are now available
🙏 On Tue, Dec 3, 2024 at 5:06 PM Thomas Wouters via Python-list < python-list@python.org> wrote: > Another big release day! Python 3.13.1 and 3.12.8 were regularly scheduled > releases, but they do contain a few security fixes. That makes it a nice > time to release the security-fix-only versions too, so everything is as > secure as we can make it. > < > https://discuss.python.org/t/python-3-13-1-3-12-8-3-11-11-3-10-16-and-3-9-21-are-now-available/73214/1#p-211771-python-3131-1 > >Python > 3.13.1 > > Python 3.13’s first maintenance release. My child is all growed up now, I > guess! Almost 400 bugfixes, build improvements and documentation changes > went in since 3.13.0, making this the very best Python release to date. > https://www.python.org/downloads/release/python-3131/ > < > https://discuss.python.org/t/python-3-13-1-3-12-8-3-11-11-3-10-16-and-3-9-21-are-now-available/73214/1#p-211771-python-3128-2 > >Python > 3.12.8 > > Python 3.12 might be slowly reaching middle age, but still received over > 250 bugfixes, build improvements and documentation changes since 3.12.7. > https://www.python.org/downloads/release/python-3128/ > < > https://discuss.python.org/t/python-3-13-1-3-12-8-3-11-11-3-10-16-and-3-9-21-are-now-available/73214/1#p-211771-python-3-3 > >Python > 3.11.11 > > I know it’s probably hard to hear, but this is the *second* security-only > release of Python 3.11. Yes, really! Oh yes, I know, I know, but it’s true! > Only 11 commits went in since 3.11.10. > https://www.python.org/downloads/release/python-3/ > < > https://discuss.python.org/t/python-3-13-1-3-12-8-3-11-11-3-10-16-and-3-9-21-are-now-available/73214/1#p-211771-python-31016-4 > >Python > 3.10.16 > > Python 3.10 received a total of 14 commits since 3.10.15. Why more than > 3.11? Because it needed a little bit of extra attention to keep working > with current GitHub practices, I guess. > https://www.python.org/downloads/release/python-31016/ > < > https://discuss.python.org/t/python-3-13-1-3-12-8-3-11-11-3-10-16-and-3-9-21-are-now-available/73214/1#p-211771-python-3921-5 > >Python > 3.9.21 > > Python 3.9 isn’t quite ready for pasture yet, as it’s set to receive > security fixes for at least another 10 months. Very similarly to 3.10, it > received 14 commits since 3.9.20. > https://www.python.org/downloads/release/python-3921/ > < > https://discuss.python.org/t/python-3-13-1-3-12-8-3-11-11-3-10-16-and-3-9-21-are-now-available/73214/1#p-211771-stay-safe-and-upgrade-6 > >Stay > safe and upgrade! > > As always, upgrading is highly recommended to all users of affected > versions. > < > https://discuss.python.org/t/python-3-13-1-3-12-8-3-11-11-3-10-16-and-3-9-21-are-now-available/73214/1#p-211771-enjoy-the-new-releases-7 > >Enjoy > the new releases > > Thanks to all of the many volunteers who help make Python Development and > these releases possible! Please consider supporting our efforts by > volunteering yourself or through organization contributions to the Python > Software Foundation. > > Regards from your tireless, tireless release team, > Thomas Wouters > Ned Deily > Steve Dower > Pablo Galindo Salgado > Łukasz Langa > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Version of OpenSSl ?
> > Trying to compile Python-3.12.9 on Ubuntu-24.04 > > The compilation is complete without errors but I have this message: > > > The necessary bits to build these optional modules were not found: > _hashlib _ssl nis > To find the necessary bits, look in configure.ac and config.log. > > Could not build the ssl module! > Python requires a OpenSSL 1.1.1 or newer > > > > But I have a more newer version: > > > --- > $ openssl version > OpenSSL 3.0.13 30 Jan 2024 (Library: OpenSSL 3.0.13 30 Jan 2024) > > --- > In case this helps you find the correct package to install: $ python3 -c "if True: > import ssl > print('Ok.') > " Ok. $ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=24.04 DISTRIB_CODENAME=noble DISTRIB_DESCRIPTION="Ubuntu 24.04.1 LTS" $ apt list --installed | grep ssl WARNING: apt does not have a stable CLI interface. Use with caution in scripts. libssl-dev/noble-updates,noble-security,now 3.0.13-0ubuntu3.4 amd64 [installed] libssl3t64/noble-updates,noble-security,now 3.0.13-0ubuntu3.4 amd64 [installed,automatic] libxmlsec1t64-openssl/noble,now 1.2.39-5build2 amd64 [installed,automatic] openssl/noble-updates,noble-security,now 3.0.13-0ubuntu3.4 amd64 [installed,automatic] ssl-cert/noble,noble,now 1.1.2ubuntu1 all [installed,automatic] -- https://mail.python.org/mailman/listinfo/python-list