Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

They are relatively good but absolutely terrible. -- Alan Kay, commenting on Apollos


devel / comp.lang.python / Re: Instatiating module / Reusing module of command-line tool

SubjectAuthor
* Instatiating module / Reusing module of command-line toolLoris Bennett
+* Re: Instatiating module / Reusing module of command-line toolStefan Ram
|+* Re: Instatiating module / Reusing module of command-line toolLoris Bennett
||`- Re: Instatiating module / Reusing module of command-line toolCameron Simpson
|`- Re: Instatiating module / Reusing module of command-line toolStefan Ram
`- Re: Instatiating module / Reusing module of command-line toolStefan Ram

1
Instatiating module / Reusing module of command-line tool

<87o80kxcsp.fsf@hornfels.zedat.fu-berlin.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18078&group=comp.lang.python#18078

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.be...@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Instatiating module / Reusing module of command-line tool
Date: Fri, 29 Apr 2022 15:02:46 +0200
Organization: Freie Universitaet Berlin
Lines: 54
Message-ID: <87o80kxcsp.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de tLsbpf7Ken7NqSlp6Jni5QHBfgXDOQ6i08RZX0iMNHps2q
Cancel-Lock: sha1:n3k2wr3Moi202l+dL1UOAd3MTZw=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Fri, 29 Apr 2022 13:02 UTC

Hi,

I have a command-line script in Python to get the correct salutation for
a user name in either English or German from a 'salutation server':

$ get_salutation alice
Dear Professor Müller
$ get_salutation alice -l de
Sehr geehrte Frau Professorin Müller

The hostname, port, user and password for the 'salutation server' can be
given as options on the command-line, but if omitted are read from a
configuration file. The program is implemented in two modules without
any classes:

main.py:

... parse command-line options, read config file ...

salutation = my_mailer.salutations.get_salutation(args.user,
args.lang,
args.host,
args.port,
args.user,
args.secret)

salutations.py

def get_salutation(uid, lang, host, port, user, secret):
...

I have another program that is intended to run as a cron job and send an
email to certain users. This is implemented as a number of modules
without any classes and will need to use the 'get_salutation' function
from the first module.

My question: What is the analogue to initialising an object via the
constructor for a module?

My understanding is that a module is a singleton of the class 'module'.
So do I just write a method which reads the config file, or is there
some more standardised way which corresponds to instantiating an object
via

my_object = MyClass()

in the case of a class.

Cheers,

Loris

--
This signature is currently under construction.

Re: Instatiating module / Reusing module of command-line tool

<modules-20220429162937@ram.dialup.fu-berlin.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18080&group=comp.lang.python#18080

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Instatiating module / Reusing module of command-line tool
Date: 29 Apr 2022 15:31:39 GMT
Organization: Stefan Ram
Lines: 12
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <modules-20220429162937@ram.dialup.fu-berlin.de>
References: <87o80kxcsp.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 9oXv1e1AxCZbxY9w0UcGQg9EQfpZymZ8+0R8x0nv3o8SUV
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 29 Apr 2022 15:31 UTC

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>My question: What is the analogue to initialising an object via the
>constructor for a module?

If you need a class, you can write a class.

When one imports a module, the module actually gets executed.
That's why people write "if __name__ == '__main__':" often.
So, everything one wants to be done at import time can be
written directly into the body of one's module.

Re: Instatiating module / Reusing module of command-line tool

<singletons-20220429232252@ram.dialup.fu-berlin.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18081&group=comp.lang.python#18081

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Instatiating module / Reusing module of command-line tool
Date: 29 Apr 2022 22:26:00 GMT
Organization: Stefan Ram
Lines: 24
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <singletons-20220429232252@ram.dialup.fu-berlin.de>
References: <87o80kxcsp.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de Fq7vwX4lH2sPPlrvGYfh3gli3i0apnP/kI1I3Ap8r12phY
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Fri, 29 Apr 2022 22:26 UTC

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>My understanding is that a module is a singleton of the class 'module'.

A module is an /object/ (instance) of the class "module".

In software engineering, the singleton pattern is a software
design pattern that restricts the instantiation of a class
to one /single/ instance. The class "module" is not restricted
in this way.

|>>> import math
|>>> import string
|>>> print( type( math ))
|<class 'module'>
|>>> print( type( string ))
|<class 'module'>
|>>> print( type( math )== type( string ))
|True
|>>>

Above, "math" and "string" are /two/ instances of the class
"module".

Re: Instatiating module / Reusing module of command-line tool

<87v8uij1xv.fsf@hornfels.zedat.fu-berlin.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18146&group=comp.lang.python#18146

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.be...@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: Instatiating module / Reusing module of command-line tool
Date: Fri, 06 May 2022 14:11:24 +0200
Organization: Freie Universitaet Berlin
Lines: 33
Message-ID: <87v8uij1xv.fsf@hornfels.zedat.fu-berlin.de>
References: <87o80kxcsp.fsf@hornfels.zedat.fu-berlin.de>
<modules-20220429162937@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de Amu0nNRH+dzh/SHnw4U2dQq9nxk12IqgOkGXaZoSTOOHok
Cancel-Lock: sha1:nqlxYKhNDcfhvDwlF3QaoIiLv94=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Fri, 6 May 2022 12:11 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>>My question: What is the analogue to initialising an object via the
>>constructor for a module?
>
> If you need a class, you can write a class.
>
> When one imports a module, the module actually gets executed.
> That's why people write "if __name__ == '__main__':" often.
> So, everything one wants to be done at import time can be
> written directly into the body of one's module.

So if I have a module which relies on having internal data being set
from outside, then, even though the program only ever has one instance
of the module, different runs, say test and production, would require
different internal data and thus different instances. Therefore a class
seems more appropriate and it is more obvious to me how to initialise
the objects (e.g. by having the some main function which can read
command-line arguments and then just pass the arguments to the
constructor.

I suppose that the decisive aspect is that my module needs
initialisation and thus should to be a class. Your examples in the
other posting of the modules 'math' and 'string' are different, because
they just contain functions and no data.

Cheers,

Loris

--
This signature is currently under construction.

Re: Instatiating module / Reusing module of command-line tool

<mailman.331.1651911420.20749.python-list@python.org>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18165&group=comp.lang.python#18165

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: cs...@cskk.id.au (Cameron Simpson)
Newsgroups: comp.lang.python
Subject: Re: Instatiating module / Reusing module of command-line tool
Date: Sat, 7 May 2022 16:28:54 +1000
Lines: 56
Message-ID: <mailman.331.1651911420.20749.python-list@python.org>
References: <87v8uij1xv.fsf@hornfels.zedat.fu-berlin.de>
<YnYRpniCHM1AZiYD@cskk.homeip.net>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Trace: news.uni-berlin.de ROeYQh7WiwHt9s9KyXp/OAMePpiZNiNJ/DM/oE2hhsMA==
Return-Path: <cameron@cskk.id.au>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.002
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'def': 0.04; '(e.g.': 0.05;
'class,': 0.05; 'class.': 0.07; 'directly.': 0.07; 'else.': 0.07;
'modules': 0.07; 'subject:module': 0.07; '>from': 0.09; 'aspect':
0.09; 'module.': 0.09; 'writes:': 0.09; 'cheers,': 0.11; 'import':
0.15; 'all:': 0.16; 'arguments': 0.16; 'bennett': 0.16; 'cameron':
0.16; 'elsewhere': 0.16; 'from:addr:cs': 0.16;
'from:addr:cskk.id.au': 0.16; 'from:name:cameron simpson': 0.16;
'instance': 0.16; 'instances.': 0.16; 'message-
id:@cskk.homeip.net': 0.16; 'python3': 0.16; 'received:10.10':
0.16; 'simpson': 0.16; 'skip:> 10': 0.16; 'specify': 0.16;
'wrote:': 0.16; 'probably': 0.17; 'to:addr:python-list': 0.20;
'written': 0.22; 'bit.': 0.22; 'skip:_ 10': 0.22; 'command': 0.23;
'examples': 0.25; 'stuff': 0.25; 'function': 0.27; 'done': 0.28;
'header:User-Agent:1': 0.30; 'module': 0.31; 'program': 0.31;
'objects': 0.32; 'but': 0.32; "i'm": 0.33; "i'll": 0.33; 'core':
0.34; 'header:In-Reply-To:1': 0.34; 'running': 0.34; '"if': 0.35;
'files': 0.36; 'functions': 0.36; 'people': 0.36; 'main': 0.37;
'class': 0.37; 'though': 0.37; 'way': 0.38; 'read': 0.38; 'quite':
0.39; 'use': 0.39; 'data.': 0.40; 'wants': 0.40; 'should': 0.40;
'here': 0.62; 'internal': 0.63; 'skip:o 20': 0.63; 'ever': 0.63;
'everything': 0.63; 'pass': 0.64; 'down': 0.64; 'thus': 0.64;
'your': 0.64; 'top': 0.65; 'received:userid': 0.66; 'body': 0.67;
'header:Received:6': 0.67; 'right': 0.68; 'obvious': 0.69; '....':
0.76; 'bottom:': 0.84; 'imports': 0.84; 'modes': 0.84; 'obj':
0.84; 'outside,': 0.84; 'subject:tool': 0.91; 'production,': 0.93
X-RG-Spam: Unknown
X-RazorGate-Vade: gggruggvucftvghtrhhoucdtuddrgedvfedrfeeggddutdehucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuuffpveftpgfvgffnuffvtfetpdfqfgfvnecuuegrihhlohhuthemucegtddtnecunecujfgurhepfffhvffukfggtggujggffhesthdtredttdervdenucfhrhhomhepvegrmhgvrhhonhcuufhimhhpshhonhcuoegtshestghskhhkrdhiugdrrghuqeenucggtffrrghtthgvrhhnpeetieevffetudffffeugeegleekledufeekgeeuhfekvdffgfffffeuleelvedutdenucffohhmrghinhepmhhoughulhgvrdhnrghmvgenucfkphepuddrudeghedrgeehrddujedtnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehhvghlohepsghorhhgrdgtshhkkhdrhhhomhgvihhprdhnvghtpdhinhgvthepuddrudeghedrgeehrddujedtpdhmrghilhhfrhhomheptggrmhgvrhhonhestghskhhkrdhiugdrrghupdhnsggprhgtphhtthhopedupdhrtghpthhtohepphihthhhohhnqdhlihhsthesphihthhhohhnrdhorhhg
X-RazorGate-Vade-Verdict: clean 0
X-RazorGate-Vade-Classification: clean
X-RG-VS-CLASS: clean
X-Authentication-Info: Submitted using ID cskk@bigpond.com
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <87v8uij1xv.fsf@hornfels.zedat.fu-berlin.de>
User-Agent: Mutt/2.2.3 (2022-04-12)
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <YnYRpniCHM1AZiYD@cskk.homeip.net>
X-Mailman-Original-References: <87v8uij1xv.fsf@hornfels.zedat.fu-berlin.de>
 by: Cameron Simpson - Sat, 7 May 2022 06:28 UTC

On 06May2022 14:11, Loris Bennett <loris.bennett@fu-berlin.de> wrote:
>ram@zedat.fu-berlin.de (Stefan Ram) writes:
>> If you need a class, you can write a class.
>>
>> When one imports a module, the module actually gets executed.
>> That's why people write "if __name__ == '__main__':" often.
>> So, everything one wants to be done at import time can be
>> written directly into the body of one's module.
>
>So if I have a module which relies on having internal data being set
>from outside, then, even though the program only ever has one instance
>of the module, different runs, say test and production, would require
>different internal data and thus different instances. Therefore a class
>seems more appropriate and it is more obvious to me how to initialise
>the objects (e.g. by having the some main function which can read
>command-line arguments and then just pass the arguments to the
>constructor.
>
>I suppose that the decisive aspect is that my module needs
>initialisation and thus should to be a class. Your examples in the
>other posting of the modules 'math' and 'string' are different, because
>they just contain functions and no data.

Yeah, I do this quite a bit. So I might have the core class which does
it all:

class Thing:
def __init__(self, whatever...):
....

and if I'm exercising this from the command line I'll write a main
function:

def main(argv):
cmd = argv.pop(0)
... use the arguments to specify data files or modes or whatever
...
obj = Thing(...init the thing...)
obj.do_something(...)

That is usually the top thing, after the imports but before everything
else. Then right down the bottom:

if __name__ == '__main__':
sys.exit(main(sys.argv))

for running the module in command line mode:

python3 -m the.module.name args here ...

That way you can import it elsewhere for the "thing" class and also do
basic command line stuff with it directly.

Cheers,
Cameron Simpson <cs@cskk.id.au>
way I'll probably write a class for a command line.

Re: Instatiating module / Reusing module of command-line tool

<modules-20220510025507@ram.dialup.fu-berlin.de>

 copy mid

https://www.novabbs.com/devel/article-flat.php?id=18217&group=comp.lang.python#18217

 copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Instatiating module / Reusing module of command-line tool
Date: 10 May 2022 01:55:42 GMT
Organization: Stefan Ram
Lines: 20
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <modules-20220510025507@ram.dialup.fu-berlin.de>
References: <87o80kxcsp.fsf@hornfels.zedat.fu-berlin.de> <modules-20220429162937@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de 7KD7ZbrK1o1GAWrOsSOz2QaBudr/U449G/CxtYP1PqSiJM
X-Copyright: (C) Copyright 2022 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE, en-US, it, fr-FR
 by: Stefan Ram - Tue, 10 May 2022 01:55 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:
>"Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>>My question: What is the analogue to initialising an object via the
>>constructor for a module?
>If you need a class, you can write a class.
>When one imports a module, the module actually gets executed.
>That's why people write "if __name__ == '__main__':" often.
>So, everything one wants to be done at import time can be
>written directly into the body of one's module.

In his talk "Python's Class Development Toolkit", in 2013,
Raymond Hettinger, elaborated on the close connection
between modules and classes:

|So, the way uh the class works is, the definitions run as
|if they were in their own module and then the module
|dictionary becomes the uh class dictionary.
Raymond Hettinger in 2013.

1
server_pubkey.txt

rocksolid light 0.9.7
clearnet tor