It a simple class that creates a list of dictionarries to store preferences as key value pairs. You can use the class directly or load the preferences from files. The main reason I wrote it was to create 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 # or newline. splitting on the first =, * or :.If split on an = other parameters with the same name are removedthen the preference is added irregardless of if there werepre-exising preferences with the same name.If split on an * the preference is simply added allowing for thesame preference to appear several times even with the same value.If split on an : the key value pair is forced to be unique and ifthere are multipul key value pairs that match all but one set isremoved.It will raise a Value error and provide the line number the errorappeard on if there is an line within 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 detailsuniq_key_value(self, pref, value)If you need unique key value pairs. For example if you have aparameter that my appear in more than one config file but youonly want one occurance reported. Like ~./prefs has author markand .cwd also has author mark you would only return one authormark but you could have another author that would also get returned.———————————————————————-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.
Source Distribution
File details
Details for the file Preferences-0.08.tar.gz
.
File metadata
- Download URL: Preferences-0.08.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6886bfe2c9b5c629cd68898823804b28ef84ab069f82c5d0f4b5f5d1a1db5dab |
|
MD5 | 35e2c032f6982be368368ce0d5fa964d |
|
BLAKE2b-256 | 02ab351cbd737eb773eda94c22fe13ad3e028d472c31566731ff0eabca43cdd3 |