Instatiating module / Reusing module of command-line tool

Loris Bennett loris.bennett at fu-berlin.de
Fri Apr 29 09:02:46 EDT 2022


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.


More information about the Python-list mailing list