<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Skippy Records &#187; python</title>
	<atom:link href="http://skippyrecords.wordpress.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://skippyrecords.wordpress.com</link>
	<description></description>
	<lastBuildDate>Tue, 17 Jan 2012 22:41:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='skippyrecords.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Skippy Records &#187; python</title>
		<link>http://skippyrecords.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://skippyrecords.wordpress.com/osd.xml" title="Skippy Records" />
	<atom:link rel='hub' href='http://skippyrecords.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Python resources with properties file, update</title>
		<link>http://skippyrecords.wordpress.com/2009/12/11/python-resources-with-properties-file-update/</link>
		<comments>http://skippyrecords.wordpress.com/2009/12/11/python-resources-with-properties-file-update/#comments</comments>
		<pubDate>Fri, 11 Dec 2009 16:22:48 +0000</pubDate>
		<dc:creator>Dr. Skippy</dc:creator>
				<category><![CDATA[Programming Projects]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[resources]]></category>

		<guid isPermaLink="false">http://blog.drskippy.com/?p=387</guid>
		<description><![CDATA[The code I introduced in this post a few days ago has been evolving.  Below is the latest version.  Additions include searching multiple paths for the properties file, explicit naming of properties and minimal exception handling.  If you find this useful, please drop me a line in the comments. # $Date: 2009-12-07 19:11:09 -0700 (Mon, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=skippyrecords.wordpress.com&amp;blog=13069636&amp;post=387&amp;subd=skippyrecords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The code I introduced in <a href="http://blog.drskippy.com/2009/11/24/python-resources-as-java-like-properties-files/">this post</a> a few days ago has been evolving.  Below is the latest version.  Additions include searching multiple paths for the properties file, explicit naming of properties and minimal exception handling.  If you find this useful, please drop me a line in the comments.</p>
<pre style="padding-left:30px;">#  $Date: 2009-12-07 19:11:09 -0700 (Mon, 07 Dec 2009) $
#  $Author: scott.hendrickson&lt;at&gt;buildingsi&lt;dot&gt;com $
#
##############################################
# Default resource files are named after class:
# class_name.properties
#
# If this class is the base class, then properties
# file is required.

import os, sys
import logging

class res:
    def __init__(self, logger=None,
                 resourceName=None):
        self.resourceValidFlag = False
        self.logger = logger
        self.resourceMap = {}
        # current path, path of script
        searchPaths = [os.getenv('PWD'),
                       sys.path[0]]
        if resourceName is None:
            filename = ''.join([self.__class__.__name__,
                                ".properties"])
        else:
            filename = ''.join([resourceName,
                                ".properties"])
        propFile = None
        for path in searchPaths:
            tmp = os.path.normpath(''.join([path,
                                            '/',
                                            filename]))
            if os.path.exists(tmp):
                propFile = tmp
        if propFile is not None:
            if self.logger is not None:
                self.logger.info("opening properties file %s" %
                                 filename)
            try:
                f = open(propFile, 'rb')
                for prop in f:
                    if prop[0] &lt;&gt; "#" and prop[0] &lt;&gt; '\n':
                        list = prop.split("=")
                        value = ''
                        for i in range(1,len(list)):
                            value += list[i] + "="
                        key = list[0]
                        value = value[:-1]
                        self.resourceMap[key] = value.strip("\n\r '\"").strip('\n\r')
                        if self.logger is not None:
                            self.logger.debug("property %s set to '%s'" %
                                              (key,
                                               self.resourceMap[key]))
                self.resourceValidFlag = True
            except IOError:
                if self.logger is not None:
                    self.logger.info("unable to read resource file %s" %
                                     propFile)
                else:
                    sys.stderr.write("unable to read resource file %s\n" %
                                     propFile)
        else:
            if self.logger is not None:
                self.logger.info("unable to find resource file %s in %s" %
                                 (filename,
                                  str(searchPaths)))
            else:
                sys.stderr.write("unable to find resource file %s in %s" %
                                 (filename,
                                  str(searchPaths)))

    def isValid(self):
        return self.resourceValidFlag

    def getString(self, key):
        if key in self.resourceMap:
            return self.resourceMap[key]
        else:
            if self.logger is not None:
                self.logger.error("resource %s missing" %
                                  key )
            else:
                sys.stderr.write("resource %s missing\n" %
                                 key )
        return None</pre>
<br /> Tagged: java, python, resources <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/skippyrecords.wordpress.com/387/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/skippyrecords.wordpress.com/387/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/skippyrecords.wordpress.com/387/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/skippyrecords.wordpress.com/387/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/skippyrecords.wordpress.com/387/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/skippyrecords.wordpress.com/387/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/skippyrecords.wordpress.com/387/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/skippyrecords.wordpress.com/387/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/skippyrecords.wordpress.com/387/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/skippyrecords.wordpress.com/387/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/skippyrecords.wordpress.com/387/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/skippyrecords.wordpress.com/387/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/skippyrecords.wordpress.com/387/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/skippyrecords.wordpress.com/387/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=skippyrecords.wordpress.com&amp;blog=13069636&amp;post=387&amp;subd=skippyrecords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://skippyrecords.wordpress.com/2009/12/11/python-resources-with-properties-file-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fd95bd67cd406fcb27a627a44570f2a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drskippy27</media:title>
		</media:content>
	</item>
		<item>
		<title>Python resources as Java-like properties files</title>
		<link>http://skippyrecords.wordpress.com/2009/11/24/python-resources-as-java-like-properties-files/</link>
		<comments>http://skippyrecords.wordpress.com/2009/11/24/python-resources-as-java-like-properties-files/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 02:42:36 +0000</pubDate>
		<dc:creator>Dr. Skippy</dc:creator>
				<category><![CDATA[Programming Projects]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[resources]]></category>

		<guid isPermaLink="false">http://blog.drskippy.com/?p=363</guid>
		<description><![CDATA[BuildingSI&#8217;s application stack is Linux-Tomcat-MySQL-Java Servlet/Python/Wicket.  Wicket, Log4j, Tomcat etc. use of Java properties files to manage resources and Localized resources.  Because I am working in this framework all of the time, I decided to write a small base class for all of my Python objects that allows the Java-like properties file use for Python. Of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=skippyrecords.wordpress.com&amp;blog=13069636&amp;post=363&amp;subd=skippyrecords&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a title="Building Systems Insight" href="http://buildingsi.com">BuildingSI&#8217;s</a> application stack is Linux-Tomcat-MySQL-Java Servlet/Python/Wicket.  Wicket, Log4j, Tomcat etc. use of Java properties files to manage resources and Localized resources.  Because I am working in this framework all of the time, I decided to write a small base class for all of my Python objects that allows the Java-like properties file use for Python.</p>
<p>Of course, with pretty code formatting, managing string constants and varying configurations within your Python code is not as inconvenient as with a compiled language. The main advantage is uniformity.  During maintenance or message changing tasks, you merely edit all of the *.properties files at once.  A secondary advantage has been the ability to use identical code for staging and production by using platform/environment-dependent properties like,</p>
<pre style="padding-left:30px;">"SOURCE.hostname="/cygdrive/c/..."</pre>
<p>accessed by the Python code snippet,</p>
<pre style="padding-left:30px;">hostname = os.environ['HOSTNAME']</pre>
<pre style="padding-left:30px;">SOURCE = self.getString("SOURCE." + hostname)</pre>
<p>I haven&#8217;t worked out Localization analogous to Java resources, but it seems like a natural extension.</p>
<p>To use the code, just add the module <em>res </em>to your PYTHON_PATH and use it as a base class for each object (named like the module, if you want <em>foo.py</em> and <em>foo.properties</em>, for example).</p>
<pre># Resource files are named after class:
# class_name.properties
#
# If this class is the base class, then properties
# file is required.</pre>
<pre># Logging is standard Python logging framework</pre>
<pre>import sys

class res:
    def __init__(self, logger=None):
        self.logger = logger
        self.resMap = {}
        filename = ''.join([sys.path[0],'/',self.__class__.__name__,".properties"])
        if self.logger is not None:
            self.logger.info("opening properties file %s"%filename)
        try:
            f = open(filename, 'rb')
            for prop in f:
                if prop[0] &lt;&gt; "#" and prop[0] &lt;&gt; '\n':
                    list = prop.split("=")
                    value = ''
                    for i in range(1,len(list)):
                        value += list[i] + "="
                    key = list[0]
                    value = value[:-1]
                    self.resMap[key] = value.strip("\n\r '\"").strip('\n\r')
                    if self.logger is not None:
                        self.logger.debug("property %s set to '%s'"%(key, self.resMap[key]))
        except IOError:
            if self.logger is not None:
                self.logger.info("unable to read resource file %s"%filename)
            else:
                sys.stderr.write("unable to read resource file %s\n"%filename)

    def getString(self, key):
        if key in self.resMap:
            return self.resMap[key]
        else:
            if self.logger is not None:
                self.logger.error("resource %s missing"%key )
            else:
                sys.stderr.write("resource %s missing\n"%key )
        return ""</pre>
<p>Be sure to call res&#8217;s __init__ function for your class. Notice you can call it with a logger reference if you are using Python logging.</p>
<pre style="padding-left:30px;">class test1(res.res):
    def __init__(self):
        res.res.__init__(self)</pre>
<p>To access a property,</p>
<pre style="padding-left:30px;">case1.getString("key1")</pre>
<p>where the properties file looks just like it&#8217;s Java counterpart. If you try it out or make any extensions, let me know how it goes.</p>
<br /> Tagged: java, python, resources <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/skippyrecords.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/skippyrecords.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/skippyrecords.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/skippyrecords.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/skippyrecords.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/skippyrecords.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/skippyrecords.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/skippyrecords.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/skippyrecords.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/skippyrecords.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/skippyrecords.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/skippyrecords.wordpress.com/363/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/skippyrecords.wordpress.com/363/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/skippyrecords.wordpress.com/363/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=skippyrecords.wordpress.com&amp;blog=13069636&amp;post=363&amp;subd=skippyrecords&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://skippyrecords.wordpress.com/2009/11/24/python-resources-as-java-like-properties-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/fd95bd67cd406fcb27a627a44570f2a2?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">drskippy27</media:title>
		</media:content>
	</item>
	</channel>
</rss>
