Re: Why does IDLE use a subprocess?

2023-05-31 Thread James Schaffler via Python-list
On Tuesday, May 30th, 2023 at 10:18 PM, Chris Angelico wrote:
> Yep, what you're seeing there is the namespace and nothing else. But
> if you mess with an actual builtin object, it'll be changed for the
> other interpreter too.
> 
> > > > import ctypes
> > > > ctypes.cast(id(42), ctypes.POINTER(ctypes.c_int))[6] = 43
> > > > 41+1
> 
> 43
> 
> > > > from code import InteractiveInterpreter
> > > > interp = InteractiveInterpreter()
> > > > interp.runcode("print(41+1)")
> 
> 43
> 
> (Note that this only works in CPython and only with integers small
> enough to be in the cache, meaning that there is only one such object
> representing that integer.)
> 
> The same is true of C extensions, which often have their own internal
> state, and that state isn't isolated to a single interpreter.
> 
> Better isolation is coming with PEP 554
> https://peps.python.org/pep-0554/ which also has some great
> information about what currently is NOT isolated. (Also, even then,
> some things won't be fully isolated; I think that the ctypes trick
> above might still affect a subinterpreter even in a post-PEP554
> world.)

Amazing example! Thank you everyone for the detailed responses - will be sure 
to check out the PEP as well.

Jim
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best practice for database connection

2023-05-31 Thread dn via Python-list

On 01/06/2023 06.45, Thomas Passin wrote:

On 5/31/2023 2:10 PM, Jason Friedman wrote:

I'm trying to reconcile two best practices which seem to conflict.

1) Use a _with_ clause when connecting to a database so the connection is
closed in case of premature exit.

class_name = 'oracle.jdbc.OracleDriver'
url = f"jdbc:oracle:thin:@//{host_name}:{port_number}/{database_name}"
with jdbc.connect(class_name, url, [user_name, password],
jdbc_jar_file.as_posix()) as connection:
 logger.info(f"Connected.")

2) Use self-made functions to streamline code. For example, there are
several places I need to know if the database object is a particular 
type,

so I create a function like this:

foobar_set = set()
...
def is_foobar(connection: jdbc.Connection, name: str) -> bool:
 """
 :param connection: connection object
 :param name: owner.object
 :return: True if this object is of type foobar
 """
 global foobar_set
 if not foobar_set:
 query = f"""select stuff from stuff"""
 cursor = connection.cursor()
 cursor.execute(query)
 for owner, object_name in cursor.fetchall():
 foobar_set.add(f"{owner}.{object_name}")
 cursor.close()
 return name.upper() in foobar_set


But that requires that I call is_foobar() with a connection object.

Essentially I'd like a function that leverages the one connection I 
create

at the beginning using a with clause.


If you need to have a connection object that persists outside of the 
with block, then


1. you can just not use a "with" block:

connection = jdbc.connect(class_name, url, [user_name, password],
    jdbc_jar_file.as_posix())

You will want to close the connection yourself when you are done with it.

2. do all the subsequent work within the "with" block.


As with many such questions, the answer is "it depends". Sadly that's no 
help!



Please consider: is the database critical to this application? In other 
words, if the script is unable to proceed without access, eg RDBMS is 
down or credentials are not accepted, then must the logic simply stop?


Most of mine fit into that category. Accordingly, script-execution 
starts with setting the environment, eg gathering credentials; followed 
by establishing a connection to the RDBMS. An operational RDBMS is part 
of the environment! Now (wait for many of our colleagues to hurriedly 
suck in their breath) we can see that the connection is a global-value - 
something which resides in a base "frame" of the Python stack, and is 
accordingly available (R-O) 'everywhere'.


NB when I say "connection", it is part of a wider RDBMS-interface object.

If you wish to use a Context Manager, then its only content may be a 
call to main() - or preferably a function which better describes what 
the application will do. (per @Thomas' contribution)


PS I don't like using the global keyword/command, but prefer to pass the 
connection as a parameter. A matter of style? A "contract": 
no-connection, no-query? YMMV...



Another approach might be to enact the Dependency Inversion Principle 
(one of 'Uncle Bob' Martin's SOLID Principles. In this case, proceed 
with the application, and when it comes time to issue a query against 
the database, ask the question: "does a connection exist?". In the 
negative case, then call a routine which establishes the connector, 
passes that to the query-routine, and proceeds.


Assuming multiple query-routines, the problem with this is that it takes 
some sleight-of-hand to create a shared connection. Alternately, maybe 
the application is prepared to assume the 'cost' of creating multiple 
connections (over time) because the RDBMS-interactions are few-and-far 
between, or only a small part of the total.



I don't use one, but others enthuse about ORMs, eg SQLAlchemy. This 
suits those who combine RDBMS and OOP, and has its own persistence 
methodology.


--
Regards,
=dn
--
https://mail.python.org/mailman/listinfo/python-list


Re: Log File

2023-05-31 Thread Peter J. Holzer
On 2023-05-31 00:22:11 -0700, ahsan iqbal wrote:
> Why we need a log file ?

A log file contains information about what your program was doing. You
use it to check that your program was performing as intended after the
fact. This is especially useful for tracking down problems with programs
which are either long-running or are running unattended.

For example, if a user tells me that they were having a problem
yesterday evening I can read the log file to see what my program was
doing at the time which will help me to pin down the problem.

The important part to remember is that a log file will only contain
information the program writes. If you didn't think to log some
information then it won't be in the log file and you can't look it up
afterwards. On the other hand you don't want to log too much information
because that might cause performance problems, it might fill up your
disks and it will be a chore to find the relevant information in a sea
of irrelevant details. So deciding what to log is a bit of an art.

> If i read a large text file than how log file help me in this regard?

It won't help you with reading the file.

However, you might want to log some information about operation, e.g.
when you started (log files usually contain time stamps), when you
ended, how large the file was, etc. That way you can compare different
runs (has the file increased in size over the last month? Was reading
especially slow yesterday?). You could also log a status message every
once in a while (e.g. every 100 MB or every 10 lines). That will
give you reassurance that the program is working and a rough estimate
when it will be finished. Or you can log any other information you think
might be useful.

hp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | h...@hjp.at |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Problems Installing and getting started.

2023-05-31 Thread Thomas Passin

On 5/31/2023 3:52 PM, Dennis Lee Bieber wrote:

Python does not provide, normally, a "click me" icon to run Python.
Python is a command line language INTERPRETER/COMPILER. If file
associations are set up, clicking on a script (.py) file/will/  run it --
but the window it opens goes away as soon as the script completes. Instead
one opens a command shell window and runs Python/scripts from that
window...

Microsoft Windows [Version 10.0.19045.2965]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Owner>python
Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.


On Windows, typing "python" may or may not give the expected version 
depending in what has been installed before.  With the python.org 
installation, it's better to type "py" instead (this is Windows only).


For example, on my Windows 10 computer"

python
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 
bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>>

but -

py
Python 3.11.3 (tags/v3.11.3:f3909b8, Apr  4 2023, 23:49:59) [MSC v.1934 
64 bit (AMD64)] on win32

Type "help", "copyright", "credits" or "license" for more information.
>>>

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


Re: Fwd: Problems Installing and getting started.

2023-05-31 Thread Dennis Lee Bieber
On Wed, 31 May 2023 08:34:48 +0100, Mark Bass 
declaimed the following:

>-- Forwarded message -
>From: Mark Bass 
>Date: Wed, 31 May 2023 at 08:09
>Subject: Problems Installing and getting started.
>To: 
>
>
>Good morning,
>
>I installed python several hours ago (from python.org), I then installed
>the IDE PyCharm. I'm using AI to help with a project that requires
>statistical analysis.
>I cannot open python, when I double clicked a "Modify Setup" window
>appeared with the three options Modify, Repair and Uninstall to click. I
>assumed this was part of the final installation process and clicked Modify
>- it seemed to be successful. I still could not open python. I asked the AI
>and it suggested to click Repair, this still made no difference. I finally
>Uninstalled it, shut down my laptop, had a coffee then re-installed it but
>the same problem occurred.
>Can you help ? Any suggestions?
>I'm really pleased with the AI so far  and looking forward to using Python
>to get my project started.
>Best Regards.Mark

You are RERUNNING the Python installer package.

Once you installed Python -- move that installer package to some
safe/obscure location on your computer (you should only need it if you
really need to fix an error with the installation).

Python does not provide, normally, a "click me" icon to run Python.
Python is a command line language INTERPRETER/COMPILER. If file
associations are set up, clicking on a script (.py) file /will/ run it --
but the window it opens goes away as soon as the script completes. Instead
one opens a command shell window and runs Python/scripts from that
window...

Microsoft Windows [Version 10.0.19045.2965]
(c) Microsoft Corporation. All rights reserved.

C:\Users\Owner>python
Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr  5 2023, 00:38:17) [MSC v.1929
64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

C:\Users\Owner>dir *.py
 Volume in drive C is Sys_OS
 Volume Serial Number is B650-6F92

 Directory of C:\Users\Owner

04/15/2023  04:57 PM50 junk.py
   1 File(s) 50 bytes
   0 Dir(s)  1,884,894,617,600 bytes free

C:\Users\Owner>python junk.py
2 4 6 8 10 12
C:\Users\Owner>junk.py
2 4 6 8 10 12
C:\Users\Owner>

Note: depending upon your environment, neither file associates nor
Python may be set up on the path. You may have a "py" command that is set
up to find and run Python (and allow specifying which version if you have
multiple versions (in different directories). Also, you may be affected by
whether Python was installed for "all users" or just the account that did
the install.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Best practice for database connection

2023-05-31 Thread Thomas Passin

On 5/31/2023 2:10 PM, Jason Friedman wrote:

I'm trying to reconcile two best practices which seem to conflict.

1) Use a _with_ clause when connecting to a database so the connection is
closed in case of premature exit.

class_name = 'oracle.jdbc.OracleDriver'
url = f"jdbc:oracle:thin:@//{host_name}:{port_number}/{database_name}"
with jdbc.connect(class_name, url, [user_name, password],
jdbc_jar_file.as_posix()) as connection:
 logger.info(f"Connected.")

2) Use self-made functions to streamline code. For example, there are
several places I need to know if the database object is a particular type,
so I create a function like this:

foobar_set = set()
...
def is_foobar(connection: jdbc.Connection, name: str) -> bool:
 """
 :param connection: connection object
 :param name: owner.object
 :return: True if this object is of type foobar
 """
 global foobar_set
 if not foobar_set:
 query = f"""select stuff from stuff"""
 cursor = connection.cursor()
 cursor.execute(query)
 for owner, object_name in cursor.fetchall():
 foobar_set.add(f"{owner}.{object_name}")
 cursor.close()
 return name.upper() in foobar_set


But that requires that I call is_foobar() with a connection object.

Essentially I'd like a function that leverages the one connection I create
at the beginning using a with clause.


If you need to have a connection object that persists outside of the 
with block, then


1. you can just not use a "with" block:

connection = jdbc.connect(class_name, url, [user_name, password],
   jdbc_jar_file.as_posix())

You will want to close the connection yourself when you are done with it.

2. do all the subsequent work within the "with" block.
--
https://mail.python.org/mailman/listinfo/python-list


Best practice for database connection

2023-05-31 Thread Jason Friedman
I'm trying to reconcile two best practices which seem to conflict.

1) Use a _with_ clause when connecting to a database so the connection is
closed in case of premature exit.

class_name = 'oracle.jdbc.OracleDriver'
url = f"jdbc:oracle:thin:@//{host_name}:{port_number}/{database_name}"
with jdbc.connect(class_name, url, [user_name, password],
jdbc_jar_file.as_posix()) as connection:
logger.info(f"Connected.")

2) Use self-made functions to streamline code. For example, there are
several places I need to know if the database object is a particular type,
so I create a function like this:

foobar_set = set()
...
def is_foobar(connection: jdbc.Connection, name: str) -> bool:
"""
:param connection: connection object
:param name: owner.object
:return: True if this object is of type foobar
"""
global foobar_set
if not foobar_set:
query = f"""select stuff from stuff"""
cursor = connection.cursor()
cursor.execute(query)
for owner, object_name in cursor.fetchall():
foobar_set.add(f"{owner}.{object_name}")
cursor.close()
return name.upper() in foobar_set


But that requires that I call is_foobar() with a connection object.

Essentially I'd like a function that leverages the one connection I create
at the beginning using a with clause.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Fwd: Problems Installing and getting started.

2023-05-31 Thread MRAB

On 2023-05-31 08:34, Mark Bass wrote:

-- Forwarded message -
From: Mark Bass 
Date: Wed, 31 May 2023 at 08:09
Subject: Problems Installing and getting started.
To: 


Good morning,

I installed python several hours ago (from python.org), I then installed
the IDE PyCharm. I'm using AI to help with a project that requires
statistical analysis.
I cannot open python, when I double clicked a "Modify Setup" window
appeared with the three options Modify, Repair and Uninstall to click. I
assumed this was part of the final installation process and clicked Modify
- it seemed to be successful. I still could not open python. I asked the AI
and it suggested to click Repair, this still made no difference. I finally
Uninstalled it, shut down my laptop, had a coffee then re-installed it but
the same problem occurred.
Can you help ? Any suggestions?
I'm really pleased with the AI so far  and looking forward to using Python
to get my project started.

What do you mean by "open python"? You said that a "Modify Setup" window 
appeared, which sounds like you're clicking on the *installer*.


To make it clear, the file that you download from python.org is the 
installer. It installs Python only. It's not an IDE.


Having said that, the standard distribution from python.org does come 
with IDLE.


You should be running PyCharm that you installed; that's the IDE. You 
just need to ensure that it's using the version of Python that you've 
installed.

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


Re: Resolution of paths in tracebacks

2023-05-31 Thread Grant Edwards
On 2023-05-31, Vishal Chandratreya  wrote:
> When an exception occurs, the full path to the file from which it
> originates is displayed, but redundant elements are not removed. For
> instance:
> $ ./python ./foo
> Traceback (most recent call last):
>   File "/home/User/cpython/./foo", line 4, in 
> a()
>   File "/home/User/cpython/./foo", line 3, in a
> def a(): raise ValueError
> ValueError
> This happens because the function _Py_abspath (in Python/fileutils.c)
> appends relative_path_to_file to absolute_path_to_current_working_directory.
> Not sure if this should be treated as a bug, because the definition of
> 'absolute path' is not clear. Does it mean a path starting from the root
> directory, or a path without redundant elements?

The former:

  
https://www.linuxfoundation.org/blog/blog/classic-sysadmin-absolute-path-vs-relative-path-in-linux-unix


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


Re: Problems Installing and getting started.

2023-05-31 Thread Richard Damon
How are you trying to “Open” python? If you get that option screen, that sounds 
like you are trying to run the installer again.

Knowing your Operating System would be very helpful. Python is normally run 
from the command line, or since you have the PyCharm IDE, it can run python on 
the program you have loaded into the project.

> On May 31, 2023, at 11:55 AM, Mark Bass  wrote:
> 
> -- Forwarded message -
> From: Mark Bass 
> Date: Wed, 31 May 2023 at 08:09
> Subject: Problems Installing and getting started.
> To: 
> 
> 
> Good morning,
> 
> I installed python several hours ago (from python.org), I then installed
> the IDE PyCharm. I'm using AI to help with a project that requires
> statistical analysis.
> I cannot open python, when I double clicked a "Modify Setup" window
> appeared with the three options Modify, Repair and Uninstall to click. I
> assumed this was part of the final installation process and clicked Modify
> - it seemed to be successful. I still could not open python. I asked the AI
> and it suggested to click Repair, this still made no difference. I finally
> Uninstalled it, shut down my laptop, had a coffee then re-installed it but
> the same problem occurred.
> Can you help ? Any suggestions?
> I'm really pleased with the AI so far  and looking forward to using Python
> to get my project started.
> Best Regards.Mark
> -- 
> https://mail.python.org/mailman/listinfo/python-list

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


Fwd: Problems Installing and getting started.

2023-05-31 Thread Mark Bass
-- Forwarded message -
From: Mark Bass 
Date: Wed, 31 May 2023 at 08:09
Subject: Problems Installing and getting started.
To: 


Good morning,

I installed python several hours ago (from python.org), I then installed
the IDE PyCharm. I'm using AI to help with a project that requires
statistical analysis.
I cannot open python, when I double clicked a "Modify Setup" window
appeared with the three options Modify, Repair and Uninstall to click. I
assumed this was part of the final installation process and clicked Modify
- it seemed to be successful. I still could not open python. I asked the AI
and it suggested to click Repair, this still made no difference. I finally
Uninstalled it, shut down my laptop, had a coffee then re-installed it but
the same problem occurred.
Can you help ? Any suggestions?
I'm really pleased with the AI so far  and looking forward to using Python
to get my project started.
Best Regards.Mark
-- 
https://mail.python.org/mailman/listinfo/python-list


Regarding- Issue on package install

2023-05-31 Thread Rakesh Vettrivel
I have an issue on updating both pip and setup tools, setuptools latest
version is getting installed successfully but after giving ok still its
shows to update.
pip is not getting installed and it shows some error.
Help me please with these issue.

pip error:

Collecting pip==23.1.2
  Using cached pip-23.1.2-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
  Attempting uninstall: pip
Found existing installation: pip 23.0
Uninstalling pip-23.0:
  Successfully uninstalled pip-23.0
  Rolling back uninstall of pip
  Moving to c:\users\rajesh.k\appdata\roaming\python\python38\scripts\
   from C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\~cripts
  Moving to
c:\users\rajesh.k\appdata\roaming\python\python38\site-packages\pip-23.0.dist-info\
   from
C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\~ip-23.0.dist-info
  Moving to
c:\users\rajesh.k\appdata\roaming\python\python38\site-packages\pip\
   from C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\~ip

ERROR: Exception:
Traceback (most recent call last):
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_internal\cli\base_command.py",
line 160, in exc_logging_wrapper
status = run_func(*args)
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_internal\cli\req_command.py",
line 247, in wrapper
return func(self, options, args)
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_internal\commands\install.py",
line 503, in run
installed = install_given_reqs(
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_internal\req\__init__.py",
line 73, in install_given_reqs
requirement.install(
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_internal\req\req_install.py",
line 796, in install
install_wheel(
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_internal\operations\install\wheel.py",
line 729, in install_wheel
_install_wheel(
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_internal\operations\install\wheel.py",
line 646, in _install_wheel
generated_console_scripts = maker.make_multiple(scripts_to_generate)
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_vendor\distlib\scripts.py",
line 436, in make_multiple
filenames.extend(self.make(specification, options))
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_internal\operations\install\wheel.py",
line 427, in make
return super().make(specification, options)
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_vendor\distlib\scripts.py",
line 425, in make
self._make_script(entry, filenames, options=options)
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_vendor\distlib\scripts.py",
line 325, in _make_script
self._write_script(scriptnames, shebang, script, filenames, ext)
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_vendor\distlib\scripts.py",
line 249, in _write_script
launcher = self._get_launcher('t')
  File
"C:\Users\RAJESH.K\AppData\Roaming\Python\Python38\site-packages\pip\_vendor\distlib\scripts.py",
line 404, in _get_launcher
raise ValueError(msg)
ValueError: Unable to find resource t32.exe in package pip._vendor.distlib

[notice] A new release of pip is available: 23.0 -> 23.1.2
[notice] To update, run: python.exe -m pip install --upgrade pip

i tried this too but it didn't work.
-- 
https://mail.python.org/mailman/listinfo/python-list


Resolution of paths in tracebacks

2023-05-31 Thread Vishal Chandratreya
When an exception occurs, the full path to the file from which it
originates is displayed, but redundant elements are not removed. For
instance:
$ ./python ./foo
Traceback (most recent call last):
  File "/home/User/cpython/./foo", line 4, in 
a()
  File "/home/User/cpython/./foo", line 3, in a
def a(): raise ValueError
ValueError
This happens because the function _Py_abspath (in Python/fileutils.c)
appends relative_path_to_file to absolute_path_to_current_working_directory.
Not sure if this should be treated as a bug, because the definition of
'absolute path' is not clear. Does it mean a path starting from the root
directory, or a path without redundant elements? The os module, for
instance, does the following.
$ ./python
>>> import os.path
>>> os.path.isabs('/home/..')
True
>>> os.path.abspath('/home/..')
'/'
Either way: should the traceback display the path without redundant
elements?

I am using CPython 3.13.0a0 (595ffddb33e95d8fa11999ddb873d08e3565d2eb).
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Log File

2023-05-31 Thread Tobiah

On 5/31/23 00:22, ahsan iqbal wrote:

Why we need a log file ? If i read a large text file than how log file help me 
in this regard?


If you were parsing each line of this text file looking for information,
perhaps some of the lines would not be formatted correctly, and you would be 
unable
to get the information from those lines.  Maybe you would like to
write those bad lines to a log file so that you could analyze them later.



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


Log File

2023-05-31 Thread ahsan iqbal
Why we need a log file ? If i read a large text file than how log file help me 
in this regard?
-- 
https://mail.python.org/mailman/listinfo/python-list