Re: Help

2023-11-06 Thread Jason Friedman via Python-list
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: Generating documentation with Sphinx

2023-08-28 Thread Jason Friedman via Python-list
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: Generating documentation with Sphinx

2023-08-28 Thread Jason Friedman via Python-list
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

2023-08-28 Thread Jason Friedman via Python-list
>
> 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


Generating documentation with Sphinx

2023-08-28 Thread Jason Friedman via Python-list
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


Context manager for database connection

2023-08-23 Thread Jason Friedman via Python-list
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


Re: How to find the full class name for a frame

2023-08-04 Thread Jason Friedman via Python-list
>
> 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


Re: How to find the full class name for a frame

2023-08-04 Thread Jason Friedman via Python-list
> 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


How to find the full class name for a frame

2023-08-03 Thread Jason Friedman via Python-list
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


Trouble with defaults and timeout decorator

2023-06-24 Thread Jason Friedman via Python-list
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


Re: Match statement with literal strings

2023-06-07 Thread Jason Friedman via Python-list
>
> 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


Match statement with literal strings

2023-06-07 Thread Jason Friedman via Python-list
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