Re: MTG: Introductions to PyQt and DataClasses

2024-03-17 Thread Jim Schwartz via Python-list
Will it be recorded?  

Sent from my iPhone

> On Mar 17, 2024, at 1:47 AM, dn via Python-list  
> wrote:
> 
> The Auckland Branch of NZPUG meets this Wednesday, 20 March at 1830 NZDT 
> (0530 UTC, midnight-ish Tue/Wed in American time-zones), for a virtual 
> meeting.
> 
> Part 1: Learn the basics of PyQt with code examples.
> Hannan Khan is currently consulting as a Data Scientist for the (US) National 
> Oceanic and Atmospheric Administration. He holds a Bachelor's degree in 
> Neuroscience as well as a Masters in Computer Science. As a keen member of 
> the PySprings Users' Group (Colorado), his contribution is part of a 
> collaboration between our two PUGs.
> 
> Part 2: Why use Dataclasses?
> - will be the question asked, and answered, by yours truly. After surveying a 
> number of groups, it seems most of us know that Dataclasses are available, 
> but we don't use them - mostly because we haven't ascertained their place in 
> our tool-box. By the end of this session you will, and will have good reason 
> to use (or not) Dataclasses!
> 
> Everyone is welcome from every location and any time-zone. The NZPUG Code of 
> Conduct applies. JetBrains have kindly donated a door-prize. Our 
> BigBlueButton web-conferencing instance is best accessed using Chromium, 
> Brave, Vivaldi, Safari, etc, (rather than Firefox - for now). A head-set will 
> facilitate asking questions but text-chat will be available.
> 
> Please RSVP at https://www.meetup.com/nzpug-auckland/events/299764049/
> See you there!
> =dn, Branch Leader
> --
> https://mail.python.org/mailman/listinfo/python-list

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


Re: Configuring an object via a dictionary

2024-03-17 Thread Peter J. Holzer via Python-list
On 2024-03-17 17:15:32 +1300, dn via Python-list wrote:
> On 17/03/24 12:06, Peter J. Holzer via Python-list wrote:
> > On 2024-03-16 08:15:19 +, Barry via Python-list wrote:
> > > > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list 
> > > >  wrote:
> > > > I've always like writing using the "or" form and have never gotten bit
> > > 
> > > I, on the other hand, had to fix a production problem that using “or” 
> > > introducted.
> > > I avoid this idiom because it fails on falsy values.
> > 
> > Perl has a // operator (pronounced "err"), which works like || (or),
> > except that it tests whether the left side is defined (not None in
> > Python terms) instead of truthy. This still isn't bulletproof but I've
> > found it very handy.
> 
> 
> So, if starting from:
> 
> def method( self, name=None, ):
> 
>  rather than:
> 
> self.name = name if name else default_value
> 
> ie
> 
> self.name = name if name is True else default_value

These two lines don't have the same meaning (for the reason you outlined
below). The second line is also not very useful.



> the more precise:
> 
> self.name = name if name is not None or default_value
> 
> or:
> 
> self.name = default_value if name is None or name

Those are syntax errors. I think you meant to write "else" instead of
"or".

Yes, exactly. That's the semantic of Perl's // operator.

JavaScript has a ?? operator with similar semantics (slightly
complicated by the fact that JavaScript has two "nullish" values).

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: MTG: Introductions to PyQt and DataClasses

2024-03-17 Thread dn via Python-list

On 17/03/24 23:40, Jim Schwartz wrote:

Will it be recorded?


Better than that (assumption) "coming soon" - please join-up or keep an 
eye on PySprings' Meetup ANNs: https://www.meetup.com/pysprings/




On Mar 17, 2024, at 1:47 AM, dn via Python-list  wrote:

The Auckland Branch of NZPUG meets this Wednesday, 20 March at 1830 NZDT (0530 
UTC, midnight-ish Tue/Wed in American time-zones), for a virtual meeting.

Part 1: Learn the basics of PyQt with code examples.
Hannan Khan is currently consulting as a Data Scientist for the (US) National 
Oceanic and Atmospheric Administration. He holds a Bachelor's degree in 
Neuroscience as well as a Masters in Computer Science. As a keen member of the 
PySprings Users' Group (Colorado), his contribution is part of a collaboration 
between our two PUGs.

Part 2: Why use Dataclasses?
- will be the question asked, and answered, by yours truly. After surveying a 
number of groups, it seems most of us know that Dataclasses are available, but 
we don't use them - mostly because we haven't ascertained their place in our 
tool-box. By the end of this session you will, and will have good reason to use 
(or not) Dataclasses!

Everyone is welcome from every location and any time-zone. The NZPUG Code of 
Conduct applies. JetBrains have kindly donated a door-prize. Our BigBlueButton 
web-conferencing instance is best accessed using Chromium, Brave, Vivaldi, 
Safari, etc, (rather than Firefox - for now). A head-set will facilitate asking 
questions but text-chat will be available.

Please RSVP at https://www.meetup.com/nzpug-auckland/events/299764049/
See you there!
=dn, Branch Leader
--
https://mail.python.org/mailman/listinfo/python-list




--
Regards,
=dn

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


Re: MTG: Introductions to PyQt and DataClasses

2024-03-17 Thread Jim Schwartz via Python-list
Actually, I have a sleep disorder that requires me to keep a constant sleep 
schedule. Thats why I asked. 

Sent from my iPhone

> On Mar 17, 2024, at 3:36 PM, dn via Python-list  
> wrote:
> 
> On 17/03/24 23:40, Jim Schwartz wrote:
>> Will it be recorded?
> 
> Better than that (assumption) "coming soon" - please join-up or keep an eye 
> on PySprings' Meetup ANNs: https://www.meetup.com/pysprings/
> 
> 
 On Mar 17, 2024, at 1:47 AM, dn via Python-list  
 wrote:
>>> 
>>> The Auckland Branch of NZPUG meets this Wednesday, 20 March at 1830 NZDT 
>>> (0530 UTC, midnight-ish Tue/Wed in American time-zones), for a virtual 
>>> meeting.
>>> 
>>> Part 1: Learn the basics of PyQt with code examples.
>>> Hannan Khan is currently consulting as a Data Scientist for the (US) 
>>> National Oceanic and Atmospheric Administration. He holds a Bachelor's 
>>> degree in Neuroscience as well as a Masters in Computer Science. As a keen 
>>> member of the PySprings Users' Group (Colorado), his contribution is part 
>>> of a collaboration between our two PUGs.
>>> 
>>> Part 2: Why use Dataclasses?
>>> - will be the question asked, and answered, by yours truly. After surveying 
>>> a number of groups, it seems most of us know that Dataclasses are 
>>> available, but we don't use them - mostly because we haven't ascertained 
>>> their place in our tool-box. By the end of this session you will, and will 
>>> have good reason to use (or not) Dataclasses!
>>> 
>>> Everyone is welcome from every location and any time-zone. The NZPUG Code 
>>> of Conduct applies. JetBrains have kindly donated a door-prize. Our 
>>> BigBlueButton web-conferencing instance is best accessed using Chromium, 
>>> Brave, Vivaldi, Safari, etc, (rather than Firefox - for now). A head-set 
>>> will facilitate asking questions but text-chat will be available.
>>> 
>>> Please RSVP at https://www.meetup.com/nzpug-auckland/events/299764049/
>>> See you there!
>>> =dn, Branch Leader
>>> --
>>> https://mail.python.org/mailman/listinfo/python-list
> 
> --
> Regards,
> =dn
> 
> --
> https://mail.python.org/mailman/listinfo/python-list

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


Re: Configuring an object via a dictionary

2024-03-17 Thread dn via Python-list

On 18/03/24 04:11, Peter J. Holzer via Python-list wrote:

On 2024-03-17 17:15:32 +1300, dn via Python-list wrote:

On 17/03/24 12:06, Peter J. Holzer via Python-list wrote:

On 2024-03-16 08:15:19 +, Barry via Python-list wrote:

On 15 Mar 2024, at 19:51, Thomas Passin via Python-list 
 wrote:
I've always like writing using the "or" form and have never gotten bit


I, on the other hand, had to fix a production problem that using “or” 
introducted.
I avoid this idiom because it fails on falsy values.


Perl has a // operator (pronounced "err"), which works like || (or),
except that it tests whether the left side is defined (not None in
Python terms) instead of truthy. This still isn't bulletproof but I've
found it very handy.



So, if starting from:

 def method( self, name=None, ):

  rather than:

 self.name = name if name else default_value

ie

 self.name = name if name is True else default_value


These two lines don't have the same meaning (for the reason you outlined
below). The second line is also not very useful.




the more precise:

 self.name = name if name is not None or default_value

or:

 self.name = default_value if name is None or name


Those are syntax errors. I think you meant to write "else" instead of
"or".

Yes, exactly. That's the semantic of Perl's // operator.

JavaScript has a ?? operator with similar semantics (slightly
complicated by the fact that JavaScript has two "nullish" values).



Thanks Peter!
(yes, sad consequences of suffering a neighbor's party-til-midnight 
followed by an 0530 meeting-start - children: don't code exhausted!)



Herewith, an illustration of how the corrected version of the above 
works - and how (in what seem unusual cases) it avoids any truthy/falsy 
confusion, as raised earlier in this thread:


>>> default_value = "default"
>>> name = "Fred Flintstone"
>>> name if name is not None else default_value
'Fred Flintstone'

>>> name = None
>>> name if name is not None else default_value
'default'
>>> name = False
>>> name if name is not None else default_value
False
>>> name = 1
>>> name if name is not None else default_value
1
>>> name = 0
>>> name if name is not None else default_value
0


Personally: I find the above coding more logical, because our primary 
interest is on 'the happy case', ie where the value has been assigned 
(to "name"); and the default_value is only applied as a "guard".



On the other hand, I dislike the not-condition because it forces me to 
think (and maybe dust-off DeMorgan). Accordingly:


>>> name = "Fred Flintstone"
>>> default_value if name is None else name
'Fred Flintstone'
>>> name = None
>>> default_value if name is None else name
'default'
>>> name = False
>>> default_value if name is None else name
False
...


YMMV!
NB your corporate Style Guide may prefer 'the happy path'...

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


Re: MTG: Introductions to PyQt and DataClasses

2024-03-17 Thread dn via Python-list

On 18/03/24 10:02, Jim Schwartz wrote:

Actually, I have a sleep disorder that requires me to keep a constant sleep 
schedule. Thats why I asked.


At a weekend meeting, discussion swirled around topics such as the best 
way to learn/work, how much work we should attempt in one sitting, 
could/should I 'do more', and similar.


One of the valuable observations is that most of us would benefit by 
improving our sleep-schedule and ensuring we do sleep for sufficient 
time (probably longer than current habit).


--
Regards,
=dn

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


RE: Configuring an object via a dictionary

2024-03-17 Thread AVI GROSS via Python-list
If we are bringing up other languages, let's return to what was part of the 
original question.

How van a dictionary be used in python if your goal is to sort of use it to 
instantiate it into a set of variables and values inside the local or global or 
other namespaces? Can we learn anything from other programming languages with 
other paradigms?

I was thinking that in one sense, python has other kinds of collections such as 
a class or class member with internal variables that also has some features 
similar enough.

 I mean if I were to call a function with one argument being a dictionary like 
{"a":1, "c": 3} where in theory any number of similar key value pairs might 
exist or not be present, then the question sounded like a request to create 
variables, in this case a, and c, that hold those values. I will not debate the 
wisdom of doing that, of course. Nor will I debate what should happen if some 
of those variable names are already in use. OK?

There are ways to do this albeit some are a tad obscure such as taking a 
printable representation of the dictionary and editing it and feeding that to 
an eval. 

But could you serve a similar purpose by passing an object containing varying 
internal fields (or methods) and values including some of the dataclasses Dave 
Neal is highlighting? Is there some overlap with using dictionaries?

In Javascript, I would say they have a very different view in which all kinds 
of things overlap and their objects can even implement what others might call 
arrays albeit a tad weirder like allowing missing indices and ignoring 
non-numeric indices for some purposes. Someone may wish to chime in if people 
can and do take such objects passed in and split them into individual variables 
as requested.

What I wanted to mention is some philosophical issues in the R language in 
which the default language used other data structures to loosely support what 
dictionaries often do in python. They have named lists where components 
optionally can have a name as in list(a=5, "b"=6, 7, "hello world") and they 
have data structures called environments. Loosely, an environment is a set of 
name=value pairs and is pretty much like a dictionary and is mostly used behind 
the scenes as the interpreter searches for variable names in a sequence of 
environments such as the parent environment. But you can use environments all 
over the place on purpose and as noted, a named list simulates an environment.

So a fairly common usage when using base R is to take a data.frame (which is at 
first approximation a list of named vectors all the same length) and want to 
work with the column names without extra typing. If I had a data.frame that 
looked like mydf <- data.frame(a=1:3, b=3:1) then if I wanted to add 
corresponding entries, I might type:

result <- mydf$a + mydf$b

inside a with statement, an environment is put on the stack consisting of the 
contents of mydf and you can now use things like:

result <- with(mydf, a+b)

There is more but the point is for those who hate the extra typing of long 
names, this can be useful and a tad dangerous if the variable names are not 
unique.

But as R delays evaluation in various ways, a similar style has evolved in an 
assortment of packages to the point where I often program in a style that looks 
like:

result <-
  mydf |>
  mutate(newcol = a+b, doubled = 2*newcol, ...)

The point is that all kinds of things that seem like local variables can be 
used as if they had been declared withing some environment but that are not 
available once you leave that region.

So perhaps there is some validity in a request to be able to just pass an 
argument as a dictionary in python and have it unpacked. 

In actuality, I wonder if the OP is aware of the unpacking functionality you 
can get using **dict in a function invocation.

Say you have a function that expects any combination of three variables called 
the unoriginal names of alpha/beta/gamma and you want to call it with a 
dictionary that contains any subset of those same keys and nothing else:

mydict = {"alpha":5, "beta":6}

def unpacked(alpha=None, beta=None, gamma=None):
  print(alpha if alpha != None else "No alpha")
  print(beta if beta != None else "No beta")
  print(gamma if gamma != None else "No gamma")

  If I now call unpacked with ** mydict:

>>> unpacked(**mydict)
5
6
No gamma

Within the body of that function, arguably, I can tell if something was passed 
or not, assuming None or any other sentinel I choose is not used except in 
setting the default.

And if you add other unknown names, like delta, they seem to be ignored without 
harmful effects or can be caught in other ways.

>>> unpacked({"gamma":7, "delta":8})
{'gamma': 7, 'delta': 8}
No beta
No gamma

So given a function similar to this, and you wanting LOCAL variables set, how 
would this do if you wanted these three or any number, and in a row:

mydict = {"beta":6, "alpha":5}

def dict_to_vars(alpha=None, beta=None, gamma=None):