Re: My first attempt at a package.

2022-07-25 Thread Antoon Pardon



Op 19/07/2022 om 16:57 schreef David Lowry-Duda:

On Tue, Jul 19, 2022 at 03:58:41PM +0200, Antoon Pardon wrote:

I am writing a python package which has the following structure

PACKAGE
   * module1.py
   * module2.py
   * data.cfg

However the data.cfg should be build at installation time.

Can someone give advise on which packaging tool and how
to use it, to accomplisch this.


A lot of good packaging information can be found at

https://packaging.python.org/en/latest/

and in particular at

https://packaging.python.org/en/latest/tutorials/packaging-projects/

and

https://packaging.python.org/en/latest/overview/

There are a couple of different ways to handle python packaging, and 
it can be a bit confusing. But following the tutorial on packaging is 
a good start.




Yes it is, but it doesn't answer my question: How do I create a package 
in which a file is built at install time.
I just want to build a configuration file that will among some other 
info contain the date the package
was installed. The idea is that you can execute python3 -m 
.release to know what version
of the package you installed and when you installed it. But seem unable 
to find a way to do this.

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


Re: Object in List : how?

2022-07-25 Thread Peter Otten

On 25/07/2022 02:47, Khairil Sitanggang wrote:

Regarding your comment : "
*However, usually object creation and initialization iscombined by allowing
arguments to the initializer:*" , so which one of the two classes Node1,
Node2 below is more common in practice? Option 2, I guess.
Thanks,


# option 1:
class Node1:
 def __init__(self, a):
 self.a = a
 self.b = self.calculation()

 def calculation(self):
 r = self.a + 10
 return r

# option 2:
class Node2:
 def __init__(self, a, b):
 self.a = a
 self.b = b

 self.b = self.calculation()

 def calculation(self):
 r = self.a + 10
 return r

nd1 = Node1(10)
nd2 = Node2(10, 0) # 0 is dummy, will be overwritten by the call to
calculation()


An attribute that can be calculated from other attributes should never
be modified through other means. Some day you may want b to become
something else, write, for example,

node = Node2(10, "twenty")

and because by then you have forgotten about the calculation() call end
up with a buggy script. But manually invoking the calculation() method
is also bug prone. You have to remember to repeat it every time you
change a:

node = Node1(10)
assert node.b == 20  # OK

node.a = 20
assert node.b == 30 # fails, a and b are out of sync.

The solution Python has to offer is called "property". Properties in
their simplest form are calculated read-only attributes, i. e. when you
write

print(node.b)

under the hood node.a + 10 is calculated. Here's how to change Node1 to
turn b into such a property:

class Node3a:
def __init__(self, a):
self.a = a
def calculation(self):
return self.a + 10
b = property(calculation)

node = Node3a(42)
print(node.b)  # 52

node.a = 1
print(node.b)  # 11

Often you are not interested in exposing both the calculation() method
and the property. For cases when you only want to access the property
Python provides a way to define the property with a "decorator":

class Node3b:
def __init__(self, a):
self.a = a
@property
def b(self):
return self.a + 10

When you compare the two classes you can see that I

(1) renamed calculation() to b() and

(2) replaced

def b(self): ...
b = property(b)

with

@property
def b(self): ...

thus avoiding the repetitons of the name.

Are there any disadvantages to properties?

What I presented as an advantage, that the value of the attribute is
recalculated every time the attribute is accessed, may sometimes become
a disadvantage, e. g. when it takes a very long time to calculate. In
most cases that should not be a problem, though.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Object in List : how?

2022-07-25 Thread Khairil Sitanggang
Thank you everyone. The specific requirements for that class:

*(1)* Provide the values of the "input" (via constructors).
 *I think everyone agrees with the way it is implemented in the
example. *

*(2)* Provide other products such as *b* that can be of any type (array,
object, etc.). It is like an "output" if you will.
 *I think everyone suggests that it should be designed such that people
should not be able (inadvertently) to change the value from outside the
class.  I agree, as a matter of fact this is my intent as well.*

*(3)* About the function calc(), my intent is NOT to expose it outside the
class: it is "private".
 *And I see the way to do that from your suggestions. *

Based on all your valuable suggestions, I should be able to accomplish the
3 goals.

By the way, I am writing this code for finite element analysis (FEA):
number crunching. Even though the final goal is to get the correct results,
I still want to write it following the correct python "grammar" and style.

Best regards,
-Irfan

On Mon, Jul 25, 2022 at 3:54 AM Peter Otten <[email protected]> wrote:

> On 25/07/2022 02:47, Khairil Sitanggang wrote:
> > Regarding your comment : "
> > *However, usually object creation and initialization iscombined by
> allowing
> > arguments to the initializer:*" , so which one of the two classes Node1,
> > Node2 below is more common in practice? Option 2, I guess.
> > Thanks,
> >
> >
> > # option 1:
> > class Node1:
> >  def __init__(self, a):
> >  self.a = a
> >  self.b = self.calculation()
> >
> >  def calculation(self):
> >  r = self.a + 10
> >  return r
> >
> > # option 2:
> > class Node2:
> >  def __init__(self, a, b):
> >  self.a = a
> >  self.b = b
> >
> >  self.b = self.calculation()
> >
> >  def calculation(self):
> >  r = self.a + 10
> >  return r
> >
> > nd1 = Node1(10)
> > nd2 = Node2(10, 0) # 0 is dummy, will be overwritten by the call to
> > calculation()
>
> An attribute that can be calculated from other attributes should never
> be modified through other means. Some day you may want b to become
> something else, write, for example,
>
> node = Node2(10, "twenty")
>
> and because by then you have forgotten about the calculation() call end
> up with a buggy script. But manually invoking the calculation() method
> is also bug prone. You have to remember to repeat it every time you
> change a:
>
> node = Node1(10)
> assert node.b == 20  # OK
>
> node.a = 20
> assert node.b == 30 # fails, a and b are out of sync.
>
> The solution Python has to offer is called "property". Properties in
> their simplest form are calculated read-only attributes, i. e. when you
> write
>
> print(node.b)
>
> under the hood node.a + 10 is calculated. Here's how to change Node1 to
> turn b into such a property:
>
> class Node3a:
>  def __init__(self, a):
>  self.a = a
>  def calculation(self):
>  return self.a + 10
>  b = property(calculation)
>
> node = Node3a(42)
> print(node.b)  # 52
>
> node.a = 1
> print(node.b)  # 11
>
> Often you are not interested in exposing both the calculation() method
> and the property. For cases when you only want to access the property
> Python provides a way to define the property with a "decorator":
>
> class Node3b:
>  def __init__(self, a):
>  self.a = a
>  @property
>  def b(self):
>  return self.a + 10
>
> When you compare the two classes you can see that I
>
> (1) renamed calculation() to b() and
>
> (2) replaced
>
> def b(self): ...
> b = property(b)
>
> with
>
> @property
> def b(self): ...
>
> thus avoiding the repetitons of the name.
>
> Are there any disadvantages to properties?
>
> What I presented as an advantage, that the value of the attribute is
> recalculated every time the attribute is accessed, may sometimes become
> a disadvantage, e. g. when it takes a very long time to calculate. In
> most cases that should not be a problem, though.
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Object in List : how?

2022-07-25 Thread Weatherby,Gerard
“Private” properties are more simply / commonly designated by sticking an _ in 
front of the name.

class Node:

def __init__(self,a)
self._a = a

I recommend you read https://docs.python.org/3/tutorial/classes.html.


That’s not to say properties don’t have their uses, but making things “private” 
isn’t a major one.

—
Gerard Weatherby | Application Architect NMRbox | NAN | Department of Molecular 
Biology and Biophysics
 UConn Health 263 Farmington Avenue, Farmington, CT 06030-6406 uchc.edu
On Jul 25, 2022, 10:36 AM -0400, Khairil Sitanggang , wrote:
*** Attention: This is an external email. Use caution responding, opening 
attachments or clicking on links. ***

Thank you everyone. The specific requirements for that class:

*(1)* Provide the values of the "input" (via constructors).
*I think everyone agrees with the way it is implemented in the
example. *

*(2)* Provide other products such as *b* that can be of any type (array,
object, etc.). It is like an "output" if you will.
*I think everyone suggests that it should be designed such that people
should not be able (inadvertently) to change the value from outside the
class. I agree, as a matter of fact this is my intent as well.*

*(3)* About the function calc(), my intent is NOT to expose it outside the
class: it is "private".
*And I see the way to do that from your suggestions. *

Based on all your valuable suggestions, I should be able to accomplish the
3 goals.

By the way, I am writing this code for finite element analysis (FEA):
number crunching. Even though the final goal is to get the correct results,
I still want to write it following the correct python "grammar" and style.

Best regards,
-Irfan

On Mon, Jul 25, 2022 at 3:54 AM Peter Otten <[email protected]> wrote:

On 25/07/2022 02:47, Khairil Sitanggang wrote:
Regarding your comment : "
*However, usually object creation and initialization iscombined by
allowing
arguments to the initializer:*" , so which one of the two classes Node1,
Node2 below is more common in practice? Option 2, I guess.
Thanks,


# option 1:
class Node1:
def __init__(self, a):
self.a = a
self.b = self.calculation()

def calculation(self):
r = self.a + 10
return r

# option 2:
class Node2:
def __init__(self, a, b):
self.a = a
self.b = b

self.b = self.calculation()

def calculation(self):
r = self.a + 10
return r

nd1 = Node1(10)
nd2 = Node2(10, 0) # 0 is dummy, will be overwritten by the call to
calculation()

An attribute that can be calculated from other attributes should never
be modified through other means. Some day you may want b to become
something else, write, for example,

node = Node2(10, "twenty")

and because by then you have forgotten about the calculation() call end
up with a buggy script. But manually invoking the calculation() method
is also bug prone. You have to remember to repeat it every time you
change a:

node = Node1(10)
assert node.b == 20 # OK

node.a = 20
assert node.b == 30 # fails, a and b are out of sync.

The solution Python has to offer is called "property". Properties in
their simplest form are calculated read-only attributes, i. e. when you
write

print(node.b)

under the hood node.a + 10 is calculated. Here's how to change Node1 to
turn b into such a property:

class Node3a:
def __init__(self, a):
self.a = a
def calculation(self):
return self.a + 10
b = property(calculation)

node = Node3a(42)
print(node.b) # 52

node.a = 1
print(node.b) # 11

Often you are not interested in exposing both the calculation() method
and the property. For cases when you only want to access the property
Python provides a way to define the property with a "decorator":

class Node3b:
def __init__(self, a):
self.a = a
@property
def b(self):
return self.a + 10

When you compare the two classes you can see that I

(1) renamed calculation() to b() and

(2) replaced

def b(self): ...
b = property(b)

with

@property
def b(self): ...

thus avoiding the repetitons of the name.

Are there any disadvantages to properties?

What I presented as an advantage, that the value of the attribute is
recalculated every time the attribute is accessed, may sometimes become
a disadvantage, e. g. when it takes a very long time to calculate. In
most cases that should not be a problem, though.

--
https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!gibw_ZRRGmldOXyvOxYalA9ApszdEvnBdHnTgNVTbgvDAks80irsggsw45ZkniBHx0YRieVHhKAyn1gR$
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Object in List : how?

2022-07-25 Thread Dennis Lee Bieber
On Fri, 22 Jul 2022 23:28:11 -0500, Khairil Sitanggang 
declaimed the following:


>class Node:
>def __init__(self):
>self.NO = 0
>self.A = 20
>
>NODE = Node()
>NODELIST = []
>

Comment...

The convention is that ALL CAPS is used to indicate something that is
to be treated as a CONSTANT. Classes get capitalized initial letters. Names
of variable data is traditionally all lower case, lower case with _ between
"words" (eg: lower_case), or camel case (eg: camelCase).

>NODE.NO = 10
>NODELIST.append(NODE)
>
>NODE.NO = 20
>NODELIST.append(NODE)
>
>NODE.NO = 30
>NODELIST.append(NODE)
>
>
>NO1 = 20
>if NO1 not in NODELIST[:].NO  ???

The [:], in this statement, just says "make a copy of nodelist". The
/list/ does not have an attribute named "NO". You have to ask for each
element IN the list.

One convoluted way (I've not tested it) is:

if len([node for node in nodelist if node.no == no1]):
print("Found at least one occurence")

This is a list comprehension; it loops over each element in nodelist,
making a new list if the element attribute matches the criteria. Python
treats 0 as "false" and if no element matched, the list created is empty,
so len() is 0. Anything else implies a match was found.


-- 
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Information about updating my python notebook

2022-07-25 Thread Dennis Lee Bieber
On Sat, 23 Jul 2022 18:16:20 +0200, nhlanhlah198506
 declaimed the following:

>Can I update my python account Sent from my Galaxy

WHAT Python account? 


-- 
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: spyder v5: invalid file name when selecting interpreter in preferences

2022-07-25 Thread Dennis Lee Bieber
On Sat, 23 Jul 2022 10:30:09 -0700, Leif Svalgaard 
declaimed the following:

>error message: invalid file path: C:/Users/leifs/anaconda3/python3105.exe
>what is wrong with that?

Please cut and paste the ENTIRE error message (not a screen image,
select the TEXT) including context. If that is something being passed to a
Windows command shell, the shell does not handle / (that's a command
OPTION), only \ for paths.

Have you navigated and proved you have such a file?



-- 
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: spyder v5: invalid file name when selecting interpreter in preferences

2022-07-25 Thread Dennis Lee Bieber
On Sat, 23 Jul 2022 22:51:17 +0100, MRAB 
declaimed the following:

>On 23/07/2022 18:30, Leif Svalgaard wrote:
>> error message: invalid file path: C:/Users/leifs/anaconda3/python3105.exe
>> what is wrong with that?
>> 
>Is there a file called python3105.exe in the folder 
>C:/Users/leifs/anaconda3?
>
>That filename looks wrong to me because the file is usually called 
>python.exe.

PS C:\Users\Wulfraed> Get-ChildItem -Path 'C:\Python38\' -Name -Filter
"python*.exe"

python.exe
python3.8.exe
python3.exe
pythonservice.exe
pythonw.exe
PS C:\Users\Wulfraed>

That's from a stale ActiveState build... Anaconda may drop the "." in
the version (I'd expect it is python3.10.5)


-- 
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: My first attempt at a package.

2022-07-25 Thread Dennis Lee Bieber
On Mon, 25 Jul 2022 10:39:46 +0200, Antoon Pardon 
declaimed the following:

>Yes it is, but it doesn't answer my question: How do I create a package 
>in which a file is built at install time.
>I just want to build a configuration file that will among some other 
>info contain the date the package
>was installed. The idea is that you can execute python3 -m 
>.release to know what version
>of the package you installed and when you installed it. But seem unable 
>to find a way to do this.

Does
https://stackoverflow.com/questions/72320778/autostart-installed-package-with-python
provide any hints?


-- 
Wulfraed Dennis Lee Bieber AF6VN
[email protected]://wlfraed.microdiversity.freeddns.org/
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Object in List : how?

2022-07-25 Thread Khairil Sitanggang
Again, thank you so much for all your suggestions; it's even easier for me
now to proceed with my coding with all the guidance in one place (this
email thread).  I highly appreciate all of your help.
I love this python community :)

Regards,
-Irfan


On Mon, Jul 25, 2022 at 12:38 PM Dennis Lee Bieber 
wrote:

> On 23 Jul 2022 22:55:47 GMT, [email protected] (Stefan Ram) declaimed
> the following:
>
> >Dennis Lee Bieber  writes:
> >>  if len([node for node in nodelist if node.no == no1]):
> >>  print("Found at least one occurence")
> >
> >  I'm not sure you need the "len"; an empty list already is falsy.
> >
>
> Just wanted to be explicit for the OP.
>
>
> --
> Wulfraed Dennis Lee Bieber AF6VN
> [email protected]
> http://wlfraed.microdiversity.freeddns.org/
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list