Re: Best Practice Virtual Environment

2024-10-08 Thread Left Right via Python-list
Hi.  The advice here is from a perspective of someone who does this
professionally, for large, highly loaded systems.  This doesn't
necessarily apply to your case / not to the full extent.

> Debian (or even Python3 itself) doesn't allow to pip install required 
> packages system wide, so I have to use virtual environments even there. But 
> is it right, that I have to do that for every single user?

1. Yes, you can install packages system-wide with pip, but you don't need to.

2. pip is OK to install requirements once, to figure out what they are
(in dev. environment).  It's bad for production environment: it's
slow, inconsistent, and insecure. For more context: pip dependency
resolution is especially slow when installing local interdependent
packages. Sometimes it can take up to a minute per package.
Inconsistency comes from pip not using package checksums and
signatures (by default): so, if the package being installed was
updated w/o version update, to pip it's going to be the same package.
Not just that, for some packages pip has to resort to building them
from source, in which case nobody can guarantee the end result.
Insecurity comes from Python allowing out-of-index package downloads
during install.  You can distribute your package through PyPI, while
its dependency will point to a random Web site in a country with very
permissive laws (and, essentially, just put malware on your computer).
It's impossible to properly audit such situations because the outside
Web site doesn't have to provide any security guarantees.


To package anything Linux-related, use the packaging mechanism
provided by the flavor of Linux you are using.  In the case of Debian,
use DEB. Don't use virtual environments for this (it's possible to
roll the entire virtual environment into a DEB package, but that's a
bad idea). The reason to do this is so that your package plays nice
with other Python packages available as DEB packages. This will allow
your users to use a consistent interface when dealing with installing
packages, and to avoid situation when an out-of-bound tool installed
something in the same path where dpkg will try to install the same
files, but coming from a legitimate package.  If you package the whole
virtual environment, you might run into problems with locating native
libraries linked from Python native modules.  You will make it hard to
audit the installation, especially when it comes to certificates, TLS
etc. stuff that, preferably, should be handled in a centralized way by
the OS.

Of course, countless times I've seen developers do the exact opposite
of what I'm suggesting here. Also, the big actors in the industry s.a.
Microsoft and Amazon do the exact opposite of what I suggest. I have
no problem acknowledging this and still maintaining that they are
wrong and I'm right :) But, you don't have to trust me!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Correct syntax for pathological re.search()

2024-10-08 Thread Michael F. Stemper via Python-list

On 07/10/2024 08.56, Stefan Ram wrote:

"Michael F. Stemper"  wrote or quoted:

if not re.search("\\sout\{", line):


   So, if you're not down to slap an "r" before your string literals,
   you're going to end up doubling down on every backslash.


Never heard of that before, but it did the trick.


   Long story short, those double backslashes in your regex?
   They'll be quadrupling up in your Python string literal!



for line in lines:
 product = re.search( "sout\\{", line )


This also worked.

For now, I'll use the "r" in a cargo-cult fashion, until I decide which
syntax I prefer. (Is there any reason that one or the other is preferable?)

Thanks for your help,
Mike
--
Michael F. Stemper
Economists have correctly predicted seven of the last three recessions.

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


Re: Correct syntax for pathological re.search()

2024-10-08 Thread Karsten Hilbert via Python-list
Am Tue, Oct 08, 2024 at 08:07:04PM +0100 schrieb MRAB via Python-list:

> >unwanted_tex = '\sout{'
> >if unwanted_tex not in line: do_something_with_libreoffice()
> >
> That should be:
>
> unwanted_tex = r'\sout{'

Hm.

Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> tex = '\sout{'
>>> tex
'\\sout{'
>>>

Am I missing something ?

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Correct syntax for pathological re.search()

2024-10-08 Thread Alan Bawden via Python-list
Karsten Hilbert  writes:

   Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
   Type "help", "copyright", "credits" or "license" for more 
information.
   >>> tex = '\sout{'
   >>> tex
   '\\sout{'
   >>>

   Am I missing something ?

You're missing the warning it generates:

> python -E -Wonce
Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> tex = '\sout{'
:1: DeprecationWarning: invalid escape sequence '\s'
>>> 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Correct syntax for pathological re.search()

2024-10-08 Thread MRAB via Python-list

On 2024-10-08 21:59, Alan Bawden via Python-list wrote:

Karsten Hilbert  writes:

Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more 
information.
>>> tex = '\sout{'
>>> tex
'\\sout{'
>>>

Am I missing something ?

You're missing the warning it generates:

 > python -E -Wonce
 Python 3.11.2 (main, Aug 26 2024, 07:20:54) [GCC 12.2.0] on linux
 Type "help", "copyright", "credits" or "license" for more information.
 >>> tex = '\sout{'
 :1: DeprecationWarning: invalid escape sequence '\s'
 >>>


You got lucky that \s in invalid. If it had been \t you would've got a 
tab character.


Historically, Python treated invalid escape sequences as literals, but 
it's deprecated now and will become an outright error in the future 
(probably) because it often hides a mistake, such as the aforementioned 
\t being treated as a tab character when the user expected it to be a 
literal backslash followed by letter t. (This can occur within Windows 
file paths written in plain string literals.)

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


Re: Correct syntax for pathological re.search()

2024-10-08 Thread Jon Ribbens via Python-list
On 2024-10-07, Stefan Ram  wrote:
> "Michael F. Stemper"  wrote or quoted:
>>For now, I'll use the "r" in a cargo-cult fashion, until I decide which
>>syntax I prefer. (Is there any reason that one or the other is preferable?)
>
>   I'd totally go with the r-style notation!
>
>   It's got one bummer though - you can't end such a string literal with
>   a backslash. But hey, no biggie, you could use one of those notations:
>
>   main.py
>
> path = r'C:\Windows\example' + '\\'
>
> print( path )
> 
> path = r'''
> C:\Windows\example\
> '''.strip()
>
> print( path )
>
>   stdout
>
> C:\Windows\example\
> C:\Windows\example\
>
>   .

... although of course in this example you should probably do neither of
those things, and instead do:

from pathlib import Path
path = Path(r'C:\Windows\example')

since in a Path the trailing '\' or '/' is unnecessary. Which leaves
very few remaining uses for a raw-string with a trailing '\'...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Correct syntax for pathological re.search()

2024-10-08 Thread Karsten Hilbert via Python-list
Am Mon, Oct 07, 2024 at 08:35:32AM -0500 schrieb Michael F. Stemper via 
Python-list:

> I'm trying to discard lines that include the string "\sout{" (which is TeX, 
> for
> those who are curious. I have tried:
>   if not re.search("\sout{", line):
>   if not re.search("\sout\{", line):
>   if not re.search("\\sout{", line):
>   if not re.search("\\sout\{", line):

unwanted_tex = '\sout{'
if unwanted_tex not in line: do_something_with_libreoffice()

Karsten
--
GPG  40BE 5B0E C98E 1713 AFA6  5BC0 3BEA AC80 7D4F C89B
-- 
https://mail.python.org/mailman/listinfo/python-list


Signing off

2024-10-08 Thread AVI GROSS via Python-list
Just a final brief note.

I am leaving the python community so don't worry that anything happened to me. 
I have a disagreement with the direction some people are taking with the python 
community that is my issue and it that probably will not bother most people.

I have lots of other interests including many other programming languages and 
it is time I stopped using python when I have so much else to choose from.

My best wishes to everyone here.

Avi

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


Correct syntax for pathological re.search()

2024-10-08 Thread Michael F. Stemper via Python-list

I'm trying to discard lines that include the string "\sout{" (which is TeX, for
those who are curious. I have tried:
  if not re.search("\sout{", line):
  if not re.search("\sout\{", line):
  if not re.search("\\sout{", line):
  if not re.search("\\sout\{", line):

But the lines with that string keep coming through. What is the right syntax to
properly escape the backslash and the left curly bracket?

--
Michael F. Stemper
No animals were harmed in the composition of this message.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Correct syntax for pathological re.search()

2024-10-08 Thread Pieter van Oostrum via Python-list
r...@zedat.fu-berlin.de (Stefan Ram) writes:

> "Michael F. Stemper"  wrote or quoted:
>
> path = r'C:\Windows\example' + '\\'
>
You could even omit the '+'. Then the concatenation is done at parsing time 
instead of run time.
-- 
Pieter van Oostrum 
www: http://pieter.vanoostrum.org/
PGP key: [8DAE142BE17999C4]
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Correct syntax for pathological re.search()

2024-10-08 Thread MRAB via Python-list

On 2024-10-08 19:30, Karsten Hilbert via Python-list wrote:

Am Mon, Oct 07, 2024 at 08:35:32AM -0500 schrieb Michael F. Stemper via 
Python-list:


I'm trying to discard lines that include the string "\sout{" (which is TeX, for
those who are curious. I have tried:
  if not re.search("\sout{", line):
  if not re.search("\sout\{", line):
  if not re.search("\\sout{", line):
  if not re.search("\\sout\{", line):


unwanted_tex = '\sout{'
if unwanted_tex not in line: do_something_with_libreoffice()


That should be:

unwanted_tex = r'\sout{'

or:

unwanted_tex = '\\sout{'

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


Re: Correct syntax for pathological re.search()

2024-10-08 Thread MRAB via Python-list

On 2024-10-07 14:35, Michael F. Stemper via Python-list wrote:

I'm trying to discard lines that include the string "\sout{" (which is TeX, for
those who are curious. I have tried:
if not re.search("\sout{", line):
if not re.search("\sout\{", line):
if not re.search("\\sout{", line):
if not re.search("\\sout\{", line):

But the lines with that string keep coming through. What is the right syntax to
properly escape the backslash and the left curly bracket?

String literals use backslash is an escape character, so it needs to be 
escaped, or you need to use a "raw" string.


However, regex also uses backslash as an escape character.

That means that a literal backslash in a regex that's in a plain string 
literal needs to be doubly-escaped, once for the string literal and 
again for the regex.

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