Python Forum
Class Modules, and Passing Variables: Seeking Advice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Class Modules, and Passing Variables: Seeking Advice
#11
(Mar-01-2018, 02:25 PM)Robo_Pi Wrote: I would also like to learn more about how to create and implement a configuration file that Gribouillis had suggested for the file path names and global variables.
Here is a way to do this. You first create a configuration file such as this one
Output:
# alysha.conf [tasks] command_file = C:/100_Robo_pass/Commands.txt
This file can be somewhere on your filesystem such as YOURHOMEDIR/.config/alysha.d/alysha.conf

Your program can then read the configuration file by using python's configparser module. For python 2 it reads
from ConfigParser import SafeConfigParser
import os
from os.path import join as pjoin

conf_filename = pjoin(
    os.path.expanduser('~'), '.config', 'alysha.d', 'alysha.conf')

conf = SafeConfigParser()
conf.read(conf_filename)

command_filename = conf.get('tasks', 'command_file')
The location of the configuration file can be fined-tuned later, for example the program can have a command line option to override the default location, different locations can be tried such as the current working directory or some other standard place. The above code gives you a simple start to using a configuration file for your application. You can also read this overview of configparser.
Reply


Messages In This Thread
RE: Class Modules, and Passing Variables: Seeking Advice - by Gribouillis - Mar-01-2018, 05:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Seeking some script design advice... involves asyncio, threads, multiprocessing sawtooth500 1 950 Jul-05-2024, 10:39 PM
Last Post: AdamHensley
  Seeking advice on dask distributed sawtooth500 4 1,844 Apr-15-2024, 11:07 PM
Last Post: sawtooth500
  Unchangeable variables in a class? Calab 12 3,925 Sep-15-2023, 07:15 PM
Last Post: deanhystad
  Class variables and Multiprocessing(or concurrent.futures.ProcessPoolExecutor) Tomli 5 6,977 Nov-12-2021, 09:55 PM
Last Post: snippsat
  How to pass variables from one class to another hobbyist 18 21,064 Oct-01-2021, 05:54 PM
Last Post: deanhystad
  Acess variables from class samuelbachorik 3 2,762 Aug-20-2021, 02:55 PM
Last Post: deanhystad
  Passing Variables between files. victorTJ 3 3,200 Oct-17-2020, 01:45 AM
Last Post: snippsat
  New user seeking help EdRaponi 2 73,549 Jun-23-2020, 12:03 PM
Last Post: EdRaponi
  Class variables menator01 2 2,875 Jun-04-2020, 04:23 PM
Last Post: Yoriz
  Question about naming variables in class methods sShadowSerpent 1 2,802 Mar-25-2020, 04:51 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020