reading a configuration information only once in Python - python

I'm using the ConfigParser to read the configuration information stored in a file. I'm able to read the content and use it across other modules in the project. I'm not sure if the configuration file is read every time I call config.get(parameters). How can I make sure that the configuration information is read only once and rest of the time its read from the cache.

I would try assigning the configuration to a variable.
configVariable = config.get(parameters)
Then you can pass the configuration variable to other modules as necessary.

The default implementation of the ConfigParser class reads its data only once.

Related

How to control python program behavior with external file presence/content?

I developed a software which can be automatically updated, so I need external-placed config file/files. For now I use json file to store user-input variables like user name etc. But I am not sure how the program itself should be controlled. I mean things like checking if program is opened for first time after update to know if update notes should be shown, what functions were already used etc. For now I am doing it with things like:
if os.path.exists(control_file_1):
actions_1
if os.path.exists(control_file_2):
some other actions unrelated to actions_1
it is independent from the files content - so there is no need to read the file content - which is convenient.
What functions should be used to store those information in one file and read them efficiently? Just normal file.read() etc? It seems not very clean-code friendly.
Thanks
UPDATE:
Looks like a ConfigParser is a way to go. Am I right? Or are they any better ways to accomplish what I am going for?
Given that you need to have config information stored in a file. If you choose to have that information in a file that contains a json record then it is the most convenient if the file is used internally and updating and reading the record in the file is easy (treat it as a dict)
However, if you want a more universal config.ini reader then you can go with ConfigParser class which you can use directly or create your own wrapper
class MYConfig_Parser(ConfigParser):
so that you can check stuff in the constructor like if mandatory entries are available etc before processing the entries.

How to read in tableau (twbx) file into python?

I would like to produce some custom output with python with data from Tableau files. I dont have access to the Tableau server to run the 'Tabpy' library.
Is there any other way to do it?
Thank you in advance
You may find the following link useful.
https://community.tableau.com/thread/152463
One of the posts in the thread mentioned the following which is worth exploring:
If you're looking to generate a TWBX dynamically, you should rename
your .twbx file to .zip, extract the contents and you can do whatever
you want with those in Python to dynamically create or adjust a
workbook file. The structure / definition of the workbook file is just
XML so no special code needed to read and parse that.

How to store variables/preferences in Python for later use

I'm working on a program in Python for Windows, and would like to save variables and user preferences so that I can recall them even after the program has been terminated and restarted.
Is there an ideal way to do this on Windows machines? Would _winreg and the Windows registry be suited for this task? Or do I need to create some sort of database of my own?
Python2 has ConfigParser, which is configparser, in Python3:
import ConfigParser, os
config = ConfigParser.ConfigParser()
config.readfp(open('defaults.cfg'))
config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
Even on windows, you should be aware that the registry is a wretched hive of scum and villainy, and that you should not be using it to store your python app configurations.
You're usually going to want to store it in a configuration folder in the "home" folder. That's easy on *nix systems, more difficult in windows, because you actually need to get the "application data" directory. This usually works for me:
import os
if os.name != "posix":
from win32com.shell import shellcon, shell
homedir = "{}\\".format(shell.SHGetFolderPath(0, shellcon.CSIDL_APPDATA, 0, 0))
else:
homedir = "{}/".format(os.path.expanduser("~"))
After you have the homedirectory, you should create a folder named after your project:
if not os.path.isdir("{0}.{1}".format(homedir,projectname)):
os.mkdir("{0}.{1}".format(homedir,projectname))
Then you can make a config file in that folder and write your options to it in the format of your choosing (my personal favorite is in an XML).
Unless I"m missing something, regex has nothing to do with the windows registry. Just store a config file somewhere, like the user's home directory or whatever. At least that's what I'd do on linux. Store a file somewhere in ~/.config/
If you want to store just key-value pairs of preferences, do it in a text config file. If you want to store python structures, do it with pickle or shelve. If you need a light, hassle free, rdbms use sqlite.
While the pickle module is an obvious choice, the files it writes will be non-human-readable. For configuration files, it would be good if your configuration file was a simple text format that your users can read or even edit. So, I recommend you use JSON and the json module.
There are several ways, devided into two main directions
Access the Windows registry with the _winreg module.
Using the registry it is nontrivial to transfer the settings to another machine or to inspect them or back them up; you'd have to use regedit for those.
The other way store the preferences in a file in the user's 'My Documents' folder. This makes it easier to move the settings to another machine or user, and doesn't mix your program's settings with those of a host of other applications. So things like backing them up and restoring them are easier; you just copy one file. If you choose a text format, it is also easier to inspect and debug the settings.
Put your settings in a list, tuple or dictionary and save them with the cPickle module. This is probably one of the easiest methods. On the downside, while pickled data uses an ASCII format by default, it is not human-readable.
Use the ConfigParser module to save and load config files in a similar structure as Windows' .ini files. These files are human-readable.
Use the json module to store settings in json format. These files are human-readable.
Save the preferences as Python expressions to a text file, load it with execfile(). N.B. This could be used to execute arbitrary code, so there are safety considerations. But on the upside, it is very easy and readable.
To save variables, I recommend using pickle
To determine where to store preferences I recommend using the appdirs package to locate the appropriate "application data", "preferences", or "home" directory for your operating system:
from appdirs import user_data_dir
appname = "SuperApp"
appauthor = "Acme"
appdata_dirpath = user_data_dir(appname, appauthor)
# 'C:\\Users\\me\\AppData\\Local\\Acme\\SuperApp' on Windows
# '/Users/me/Library/Application Support/SuperApp' on macOS
# '/home/me/.local/share/SuperApp' on Linux
import os
os.makedirs(appdata_dirpath, exist_ok=True)
Then inside that directory you can write a file, with many different options for the file format:
Key-Value Strings? Consider an INI file, which you can read/write using the built-in configparser.ConfigParser module.
Nested Dicts, Lists, Strings, and Integers? Consider JSON, which you can read/write using the built-in json module.
Have more advanced needs? Such as storing a large number of preference items, or a need to store binary data? Consider a sqlite database, using the built-in sqlite3 module.
Although it is possible to use the pickle module I don't recommend it because the config files are not human-readable.

Delete INI section in Python

In python, I'm using win32api to read and write INI settings as and when they are required into specific sections based on IDs.
Once I have finished with the section, I want to be able to delete the entire section from the INI file. How would I go about doing this using the win32api python library?
My proposal is to use native Python module for handling INI files ConfigParser. Have you tried it already? It has remove_section method that probably will work for you.
This is not a Python specific answer, but look into calling WritePrivateProfileString with the lpKeyName parameter set to NULL. With this parameter set to NULL the entire section named in lpAppName, including all entries within the section, is deleted.

Approach to upgrade application configuration

My application has a xml based configuration. It has also a xsd file. Before my application starts, xmllint will check the configuration against the xsd file.
With the growth of my application, the configuration structure has changed a bit. Now I have to face this problem: When I provide a new version of my application to customer, I have to upgrade the existing configuration.
How to make this done easy and clever?
My idea is to build a configuration object using python, and then read configuration v1 from file and save it as v2. But if later the structure is changed again, I have to build another configuration object model.
For all configuration settings that remain the same between configurations, have your installation script copy those over from the old config file if it exists. For the rest, just have some defaults that the user can change if necessary, as usual for a config file. Unless I've misunderstood the question, it sounds like you're making a bigger deal out of this than it needs to be.
By the way, you'd really only need one "updater" script, because you could parametrize the XML tagging such that it go through your new config file/config layout file, and then just check the tags in the old file against that and copy the data from the ones that are present in the new file. I haven't worked with XSD files before, so I don't know the specifics of working with them, but I don't think it should be that difficult.

Categories

Resources