Skip to main content

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 CLASS

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.

Config Files:

The load_files will try to read files from three locations
by default: /etc/file, ~/.file, cwd/.file and load them in
that order but you can always pass in a list of files to
load. load_files uses read_file so you can use it to load
files one at a time if you would prefer.
Here are a few examples.

/etc/my_prefs
Organization: NosrednaKram.com

[User Home]/.my_prefs
Author: Mark Anderson
Maintainer: Mark Anderson
Email: Nosrednakram < at > GMail < dot > Com

[CWD]/.my_prefs
Project= Nosrednakram Preferences
Description= You can do multi-line
Just start lines with four spaces
and they will be appended regardless
of formating
Author: Someone Else
Author: Yet again Somone Else

The file content should begin in postion 1 of the file unless
it’s a continuation then it should start with four spaces then
the content.

The : seperator allows for more than one paramter with that
name.

The = Seperator will remove all previous loaded parameters
with the same name and then add. It will just add if no
previous parameters were set. Basiclly use for a preference
when you only want one returned.

Code Examples: (Assuming at least one file exists)

# Import
from Preferences import Preferences

# Create Preferences instance
project = Preferences()

# Load Preferences for dfault locations if they exist
project.load_files(‘my_prefs’)

# List the Authors with a loop
for author in project.get(‘Author’):
print(‘Author: %s’ % author

# Print Authors as a CSV
print project.get_csv(‘Author’)

# Add and additional Author
project.add({‘Author’: ‘Yet Another Person’})

# Add more preferences from another file
project.read_file(‘additional_file.prf’)

# Dump the project preferences to standard out
project.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 found
Returns 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 matching
key value pair exists and False otherwise.

has_pref(self, pref)
Returns True if a parameter with that name exists and fails
otherwise.

load_files(self, file_name, path=[])
When called with a path that is a list of paths to configuration
files they are read in the order they appear in the list. If not
it will first look for files in /etc, the users home directory, and
then the current working director for the passed in file name. The
exception is a . is prepended to the file name in the users
directory. 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 lines
beginning with a # or newline. splitting on the first =, * or :.

If split on an = other parameters with the same name are removed
then the preference is added irregardless of if there were
pre-exising preferences with the same name.

If split on an * the preference is simply added allowing for the
same preference to appear several times even with the same value.

If split on an : the key value pair is forced to be unique and if
there are multipul key value pairs that match all but one set is
removed.

It will raise a Value error and provide the line number the error
appeard on if there is an line within your preferences file it
doesn’t know how to handle.

replace_key(self, pref, new_value)
First all occurances of the preference are removed. Then the
parameter is added regardless of weather or not an existing
parameter was found. See add for more details

uniq_key_value(self, pref, value)
If you need unique key value pairs. For example if you have a
parameter that my appear in more than one config file but you
only want one occurance reported. Like ~./prefs has author mark
and .cwd also has author mark you would only return one author
mark 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

Preferences-0.08.tar.gz (6.1 kB view details)

Uploaded Source

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

Hashes for Preferences-0.08.tar.gz
Algorithm Hash digest
SHA256 6886bfe2c9b5c629cd68898823804b28ef84ab069f82c5d0f4b5f5d1a1db5dab
MD5 35e2c032f6982be368368ce0d5fa964d
BLAKE2b-256 02ab351cbd737eb773eda94c22fe13ad3e028d472c31566731ff0eabca43cdd3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page