Re: From JoyceUlysses.txt -- words occurring exactly once

2024-06-08 Thread Larry Martell via Python-list
On Sat, Jun 8, 2024 at 10:39 AM Mats Wichmann via Python-list <
python-list@python.org> wrote:

> On 6/5/24 05:10, Thomas Passin via Python-list wrote:
>
> > Of course, we see this lack of clarity all the time in questions to the
> > list.  I often wonder how these askers can possibly come up with
> > acceptable code if they don't realize they don't truly know what it's
> > supposed to do.
>
> Fortunately, having to explain to someone else why something is giving
> you trouble can help shed light on the fact the problem statement isn't
> clear, or isn't clearly understood. Sometimes (sadly, many times it
> doesn't).


The original question struck me as homework or an interview question for a
junior position. But having no clear requirements or specifications is good
training for the real world where that is often the case. When you question
that, you are told to just do something, and then you’re told it’s not what
is wanted. That frustrates people but it’s often part of the process.
People need to see something to help them know what they really want.

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


Re: Serializing pydantic enums

2024-05-29 Thread Larry Martell via Python-list
On Wed, May 29, 2024 at 12:27 PM Larry Martell  wrote:
>
> On Tue, May 28, 2024 at 11:46 AM Left Right via Python-list
>  wrote:
> >
> > Most Python objects aren't serializable into JSON. Pydantic isn't
> > special in this sense.
> >
> > What can you do about this? -- Well, if this is a one-of situation,
> > then, maybe just do it by hand?
> >
> > If this is a recurring problem: json.dumps() takes a cls argument that
> > will be used to do the serialization. Extend json.JSONEncoder and
> > implement the encode() method for the encoder class you are passing. I
> > believe that the official docs have some information about this too.
>
> Yeah, I know I can do this, but I seem to recall reading that pydantic
> handled serialization.  Guess not.

Actually it's as simple as adding this to any model that uses an enum model:

class Config:
use_enum_values = True


> > On Tue, May 28, 2024 at 2:50 PM Larry Martell via Python-list
> >  wrote:
> > >
> > > Just getting started with pydantic. I have this example code:
> > >
> > > class FinishReason(Enum):
> > > stop = 'stop'
> > >
> > > class Choice(BaseModel):
> > > finish_reason: FinishReason = Field(...)
> > >
> > >
> > > But I cannot serialize this:
> > >
> > > json.dumps(Choice(finish_reason=FinishReason.stop).dict())
> > > *** TypeError: Object of type FinishReason is not JSON serializable
> > >
> > >
> > > I get the object not the value:
> > >
> > > (Pdb) Choice(finish_reason=FinishReason.stop)
> > > Choice(finish_reason=)
> > >
> > >
> > > Also tried it with .value, same result.
> > >
> > > What am I missing here?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Serializing pydantic enums

2024-05-29 Thread Larry Martell via Python-list
On Tue, May 28, 2024 at 11:46 AM Left Right via Python-list
 wrote:
>
> Most Python objects aren't serializable into JSON. Pydantic isn't
> special in this sense.
>
> What can you do about this? -- Well, if this is a one-of situation,
> then, maybe just do it by hand?
>
> If this is a recurring problem: json.dumps() takes a cls argument that
> will be used to do the serialization. Extend json.JSONEncoder and
> implement the encode() method for the encoder class you are passing. I
> believe that the official docs have some information about this too.

Yeah, I know I can do this, but I seem to recall reading that pydantic
handled serialization.  Guess not.

> On Tue, May 28, 2024 at 2:50 PM Larry Martell via Python-list
>  wrote:
> >
> > Just getting started with pydantic. I have this example code:
> >
> > class FinishReason(Enum):
> > stop = 'stop'
> >
> > class Choice(BaseModel):
> > finish_reason: FinishReason = Field(...)
> >
> >
> > But I cannot serialize this:
> >
> > json.dumps(Choice(finish_reason=FinishReason.stop).dict())
> > *** TypeError: Object of type FinishReason is not JSON serializable
> >
> >
> > I get the object not the value:
> >
> > (Pdb) Choice(finish_reason=FinishReason.stop)
> > Choice(finish_reason=)
> >
> >
> > Also tried it with .value, same result.
> >
> > What am I missing here?
-- 
https://mail.python.org/mailman/listinfo/python-list


Serializing pydantic enums

2024-05-28 Thread Larry Martell via Python-list
Just getting started with pydantic. I have this example code:

class FinishReason(Enum):
stop = 'stop'

class Choice(BaseModel):
finish_reason: FinishReason = Field(...)


But I cannot serialize this:

json.dumps(Choice(finish_reason=FinishReason.stop).dict())
*** TypeError: Object of type FinishReason is not JSON serializable


I get the object not the value:

(Pdb) Choice(finish_reason=FinishReason.stop)
Choice(finish_reason=)


Also tried it with .value, same result.

What am I missing here?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: PyCon

2024-05-17 Thread Larry Martell via Python-list
LOn Fri, May 17, 2024 at 8:57 PM Larry Martell 
wrote:

> I’m at PyCon in Pittsburgh and I’m haven’t an amazing time!


s/haven’t/having/
-- 
https://mail.python.org/mailman/listinfo/python-list


PyCon

2024-05-17 Thread Larry Martell via Python-list
I’m at PyCon in Pittsburgh and I’m haven’t an amazing time!
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Version of NymPy

2024-05-15 Thread Larry Martell via Python-list
On Wed, May 15, 2024 at 2:43 PM Popov, Dmitry Yu via Python-list
 wrote:
>
> What would be the easiest way to learn which version of NumPy I have with my 
> Anaconda distribution?

>>> import numpy
>>> numpy.__version__
'1.24.4'
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Extract lines from file, add to new files

2024-01-30 Thread Larry Martell via Python-list
On Tue, Jan 30, 2024 at 1:13 AM AVI GROSS via Python-list
 wrote:
>
> It can be quite frustrating figuring out what someone wants, Grant,
> especially when they just change it.
>
> It is worse when instead of starting a new thread with an appropriate
> subject line, it continues and old one that was also frustrating to
> understand.

Is it worse than top posting?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Running a subprocess in a venv

2023-10-21 Thread Larry Martell via Python-list
On Sat, Oct 21, 2023 at 12:10 PM Johannes Findeisen  wrote:
>
> On Sat, 21 Oct 2023 11:32:03 -0400
> Larry Martell  wrote:
>
> > On Sat, Oct 21, 2023 at 9:49 AM Johannes Findeisen
> >  wrote:
> > >
> > > On Sat, 21 Oct 2023 09:01:18 -0400
> > > Larry Martell via Python-list  wrote:
> > >
> > > > I have a python script, and from that I want to run another
> > > > script in a subprocess in a venv. What is the best way to do
> > > > that? I could write a file that activates the venv then runs the
> > > > script, then run that file, but that seems messy. Is there a
> > > > better way?
> > >
> > > How do you do that?
> >
> > How? Open a file and write the commands I need then invoke that.
> >
> > > It sounds messy but not wrong...
> > >
> > > I would activate the venv and then run my Python script. In the
> > > Python script you can call another python script in a subprocess
> > > like this:
> > >
> > > import sys
> > > import subprocess
> > >
> > > #
> > > https://docs.python.org/3/library/subprocess.html#popen-constructor
> > > proc = subprocess.Popen([sys.executable,
> > > "/path/to/an/otherscript.py"])
> > >
> > > # https://docs.python.org/3/library/subprocess.html#popen-objects
> > > # Do your process communication/handling... proc.communicate(),
> > > # proc.wait(), proc.terminate(), proc.kill() etc.
> > >
> > > Is this the answer you are looking for?
> > >
> > > Detailed docs: https://docs.python.org/3/library/subprocess.html
> >
> > I know how to use Popen. What I was missing was running the script
> > using sys.executable. Thanks.
>
> sys.executable is the path to the actual Python binary, e.g.
> "/usr/bin/python". You could add "/usr/bin/python" there manually but
> this is not portable to Windows for example.
>
> When you add a shebang line to your other script and the file is
> executable, you may not need to add sys.executable as first argument to
> Popen but using sys.executable is the most reliable way to do this... ;)

I need the path to whichever venv is being used so sys.executable works for me.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Running a subprocess in a venv

2023-10-21 Thread Larry Martell via Python-list
On Sat, Oct 21, 2023 at 9:49 AM Johannes Findeisen  wrote:
>
> On Sat, 21 Oct 2023 09:01:18 -0400
> Larry Martell via Python-list  wrote:
>
> > I have a python script, and from that I want to run another script in
> > a subprocess in a venv. What is the best way to do that? I could write
> > a file that activates the venv then runs the script, then run that
> > file, but that seems messy. Is there a better way?
>
> How do you do that?

How? Open a file and write the commands I need then invoke that.

> It sounds messy but not wrong...
>
> I would activate the venv and then run my Python script. In the Python
> script you can call another python script in a subprocess like this:
>
> import sys
> import subprocess
>
> # https://docs.python.org/3/library/subprocess.html#popen-constructor
> proc = subprocess.Popen([sys.executable, "/path/to/an/otherscript.py"])
>
> # https://docs.python.org/3/library/subprocess.html#popen-objects
> # Do your process communication/handling... proc.communicate(),
> # proc.wait(), proc.terminate(), proc.kill() etc.
>
> Is this the answer you are looking for?
>
> Detailed docs: https://docs.python.org/3/library/subprocess.html

I know how to use Popen. What I was missing was running the script
using sys.executable. Thanks.
-- 
https://mail.python.org/mailman/listinfo/python-list


Running a subprocess in a venv

2023-10-21 Thread Larry Martell via Python-list
I have a python script, and from that I want to run another script in
a subprocess in a venv. What is the best way to do that? I could write
a file that activates the venv then runs the script, then run that
file, but that seems messy. Is there a better way?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: path to python in venv

2023-09-27 Thread Larry Martell via Python-list
On Wed, Sep 27, 2023 at 12:53 PM Niktar Lirik  wrote:
>
> Hi Larry,
>
> You could just create venv with option '—copies'
>
>
>
> For example:
>
> python -m venv -–copies .venv

Thanks! That is just what I was looking for.

> From: Larry Martell via Python-list
> Sent: 27 сентября 2023 г. 22:48
> To: Jon Ribbens
> Cc: python-list@python.org
> Subject: Re: path to python in venv
>
>
>
> On Wed, Sep 27, 2023 at 12:42 PM Jon Ribbens via Python-list
>
>  wrote:
>
> >
>
> > On 2023-09-27, Larry Martell  wrote:
>
> > > I was under the impression that in a venv the python used would be in
>
> > > the venv's bin dir. But in my venvs I see this in the bin dirs:
>
> > >
>
> > > lrwxrwxrwx 1 larrymartell larrymartell7 Sep 27 11:21 python -> python3
>
> > > lrwxrwxrwx 1 larrymartell larrymartell   16 Sep 27 11:21 python3 ->
>
> > > /usr/bin/python3
>
> > ...
>
> > > Not sure what this really means, nor how to get python to be in my venv.
>
> >
>
> > WHy do you want python to be "in your venv"?
>
>
>
> Isn't that the entire point of a venv? To have a completely self
>
> contained env? So if someone messes with the system python it will not
>
> break code running in the venv.
>
> --
>
> https://mail.python.org/mailman/listinfo/python-list
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: path to python in venv

2023-09-27 Thread Larry Martell via Python-list
On Wed, Sep 27, 2023 at 12:42 PM Jon Ribbens via Python-list
 wrote:
>
> On 2023-09-27, Larry Martell  wrote:
> > I was under the impression that in a venv the python used would be in
> > the venv's bin dir. But in my venvs I see this in the bin dirs:
> >
> > lrwxrwxrwx 1 larrymartell larrymartell7 Sep 27 11:21 python -> python3
> > lrwxrwxrwx 1 larrymartell larrymartell   16 Sep 27 11:21 python3 ->
> > /usr/bin/python3
> ...
> > Not sure what this really means, nor how to get python to be in my venv.
>
> WHy do you want python to be "in your venv"?

Isn't that the entire point of a venv? To have a completely self
contained env? So if someone messes with the system python it will not
break code running in the venv.
-- 
https://mail.python.org/mailman/listinfo/python-list


path to python in venv

2023-09-27 Thread Larry Martell via Python-list
I was under the impression that in a venv the python used would be in
the venv's bin dir. But in my venvs I see this in the bin dirs:

lrwxrwxrwx 1 larrymartell larrymartell7 Sep 27 11:21 python -> python3
lrwxrwxrwx 1 larrymartell larrymartell   16 Sep 27 11:21 python3 ->
/usr/bin/python3

Googling this I read:

The presence of symbolic links like python and python3 in the bin
directory of your virtual environment pointing to the system Python
executable (/usr/bin/python) suggests that the virtual environment was
created using the system Python interpreter rather than a standalone
Python installation.

This can happen if you create a virtual environment using a
system-wide Python interpreter, and the virtual environment inherits
some of the symbolic links or shortcuts from the system Python
installation. In this case, your virtual environment is not fully
isolated because it still relies on the system Python interpreter.

Not sure what this really means, nor how to get python to be in my venv.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Why do I always get an exception raised in this __init__()?

2023-08-31 Thread Larry Martell via Python-list
On Thu, Aug 31, 2023 at 3:19 PM Chris Green via Python-list
 wrote:
>
> I'm obviously doing something very silly here but at the moment I
> can't see what.
>
> Here's the code:-
>
> #!/usr/bin/python3
> #
> #
> # GPIO
> #
> import gpiod
> #
> #
> # Simple wrapper class for gpiod to make set and clearing outputs
> easier
> #
> class Gpiopin:
>
> def __init__(self, pin):
> #
> #
> # scan through the GPIO chips to find the line/pin we want
> #
> for c in ['gpiochip0', 'gpiochip1', 'gpiochip2', 'gpiochip3']:
>
> chip = gpiod.Chip(c)
> for l in range(32):
> line = chip.get_line(l)
> if pin in line.name():
> print("Found: ", line.name())
> return
> else:
> raise ValueError("Can't find pin '" + pin + "'")
>
> def print_name(self):
> print (self.line.name())
>
> def set(self):
> self.line.set_value(1)
>
> def clear(self):
> self.line.set_value(0)
>
>
> This is by no means the final code, the print() in the __init__() is
> just a diagnostic for example. However I really can't understand why I
> see the following when I try it:-
>
> >>> import ngp
> >>> ngp.Gpiopin("P9_23")
> Found:  P9_23
> Traceback (most recent call last):
>   File "", line 1, in 
>   File "/home/chris/.cfg/hosts/bbb/bin/ngp.py", line 24, in __init__
> return
> ValueError: Can't find pin 'P9_23'
> >>>
>
> Does a return in __init__() not do what I think it does?
>
> How else could/should I do this?

Change the return to a break
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: pip-sync

2023-07-24 Thread Larry Martell via Python-list
On Fri, Jul 21, 2023 at 11:08 AM Larry Martell  wrote:
>
> I am trying to set up and maintain a venv with pip-sync. On my bare
> metal I have the apparmor python package installed, but it is not
> installed in my venv and it's not in my requirements file. When I run
> pip-sync I get:
>
> Found existing installation: apparmor 2.13.3
> ERROR: Cannot uninstall 'apparmor'. It is a distutils installed
> project and thus we cannot accurately determine which files belong to
> it which would lead to only a partial uninstall.
>
> Since it's not installed in the venv why does it want to uninstall it?

Found out what was going on - I had pip-sync installed on the bare
metal and when I ran it from within the venv it was picking that one
up, which then looked at packages outside the venv. After I
uninstalled it from the bare metal it was then working as expected.
-- 
https://mail.python.org/mailman/listinfo/python-list


pip-sync

2023-07-21 Thread Larry Martell via Python-list
I am trying to set up and maintain a venv with pip-sync. On my bare
metal I have the apparmor python package installed, but it is not
installed in my venv and it's not in my requirements file. When I run
pip-sync I get:

Found existing installation: apparmor 2.13.3
ERROR: Cannot uninstall 'apparmor'. It is a distutils installed
project and thus we cannot accurately determine which files belong to
it which would lead to only a partial uninstall.

Since it's not installed in the venv why does it want to uninstall it?
-- 
https://mail.python.org/mailman/listinfo/python-list