It a simple class that creates a list of dictionarries to store preferences as key value pairs for managing parameter sets. You can use the class directly or load the parameters from files. The main reason I wrote it was so that I could have an easy way of storing more than one preference with the same name and load them form files in a manner that allows for easy appending or replacing of parmeters that could have been loaded from a default prameter file. There are also several methods to make managing the preferences easier.
Project description
Help on module Preferences:
- NAME
Preferences
- FILE
/home/nosrednakram/work/preferences/src/Preferences/Preferences.py
- CLASSES
Preferences
- class Preferences
- Preference CLASSIt a simple class that creates a list of dictionarries to storepreferences as key value pairs for managing parameter sets. Youcan use the class directly or load the parameters from files.The main reason I wrote it was so that I could have an easy wayof storing more than one preference with the same name and loadthem form files in a manner that allows for easy appending orreplacing of parmeters that could have been loaded from adefault prameter file.Config Files:The load_files will try to read files from three locationsby default: /etc/file, ~/.file, cwd/.file and load them inthat order but you can always pass in a list of files toload. load_files uses read_file so you can use it to loadfiles one at a time if you would prefer.Here are a few examples./etc/my_prefsOrganization: NosrednaKram.com[User Home]/.my_prefsAuthor: Mark AndersonMaintainer: Mark AndersonEmail: Nosrednakram < at > GMail < dot > Com[CWD]/.my_prefsProject= Nosrednakram PreferencesDescription= You can do multi-lineJust start lines with four spacesand they will be appended regardlessof formatingAuthor: Someone ElseAuthor: Yet again Somone ElseThe file content should begin in postion 1 of the file unlessit’s a continuation then it should start with four spaces thenthe content.The : seperator allows for more than one paramter with thatname.The = Seperator will remove all previous loaded parameterswith the same name and then add. It will just add if noprevious parameters were set. Basiclly use for a preferencewhen you only want one returned.Code Examples: (Assuming at least one file exists)# Importfrom Preferences import Preferences# Create Preferences instanceproject = Preferences()# Load Preferences for dfault locations if they existproject.load_files(‘my_prefs’)# List the Authors with a loopfor author in project.get(‘Author’):print(‘Author: %s’ % author# Print Authors as a CSVprint project.get_csv(‘Author’)# Add and additional Authorproject.add({‘Author’: ‘Yet Another Person’})# Add more preferences from another fileproject.read_file(‘additional_file.prf’)# Dump the project preferences to standard outproject.dump()# see test.py for more usage examples.Methods defined here:__init__(self, param=None)add(self, pref=None)Append a parameter to the params list.dump(self)I use this for debugging some times.get(self, pref)get(self, pref)Looks up all preferences of the provieded type.Returns ‘’ if no parameters were found.Returns value if only one parameter was foundReturns list of values if more than one parameter was found.get_csv(self, pref)This return a csv list of the specified preference values or‘’ if there are not matching preferences.has(self, pref, value)When passed a parameter and value it returns true if a matchingkey value pair exists and False otherwise.has_pref(self, pref)Returns True if a parameter with that name exists and failsotherwise.load_files(self, file_name, path=[])When called with a path that is a list of paths to configurationfiles they are read in the order they appear in the list. If notit will first look for files in /etc, the users home directory, andthen the current working director for the passed in file name. Theexception is a . is prepended to the file name in the usersdirectory. These files are all loaded so you can define global,local and user preferences. See read_file for more on thy syntax.read_file(self, file)Reads preferences from a file line by line. Skipping all linesbeginning with a #. splitting on the first = or :. If spliton an = other parameters with the same name are removed then thepreference is added irregardless of if there were pre-exisingpreferences with the same name. If split on an : the preferenceis simply added allowing for the same preference to appearseveral times.It will raise a Value error and provide the line number the errorappeard on if there is an line withing your preferences file itdoesn’t know how to handle.replace_key(self, pref, new_value)First all occurances of the preference are removed. Then theparameter is added regardless of weather or not an existingparameter was found. See add for more details———————————————————————-Data and other attributes defined here:params = []
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.