Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

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 = []
Release History

Release History

This version
History Node

0.08

History Node

0.07

History Node

0.06

History Node

0.05

History Node

0.04

History Node

0.03

History Node

0.02

History Node

0.01

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
Preferences-0.08.tar.gz (6.1 kB) Copy SHA256 Checksum SHA256 Source May 8, 2010

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting