Every good developer knows reusing code is essential to efficiency.  Most programmers maintain a set of core snippets used in the process of application development.  I decided to approach this a little differently in my Python learning process, by creating a framework to contain utility functions and classes that might be useful among projects.


At the core of the PokeyWorks framework is the PokeyConfig class.

class PokeyConfig(object):
	def __init__(self,fpath):
			self.fpath = fpath
			self.vals = self.load_config()
			# Dynamically set class attributes from config
			for row in self.vals:
				setattr(self, row[0], [row[1],row[2]])
		except Exception as e:
	def __str__(self):
		#Prints all non-standard methods and attributes
		retval = ''
		for key, value in self.__dict__.items():
			if not key.startswith("__"):
				retval+='{}.{}={}\n'.format(type(self).__name__,key, value[0])
		return retval.strip('\n') # Strip the final \n
	def check_attr(self, val):
		if val[0] in self.__dict__.keys():
			return self.__dict__[val[0]][0]
			return val[1]
	def load_config(self):
			with open(self.fpath, 'rb') as c:
				return [row.rstrip().split('\t') for row in c.readlines() if not row.startswith('#') and row.strip() != '']
			return 1

You may have noticed the PokeyConfig class is pretty small.  It only has a few methods available – load_config(), check_attr(), __str__(), __init__() . This is partly due to the intention to use the class as a collection of configuration attributes to be acted upon within your application’s primary execution.

The self.load_config() method attempts to open and parse the file passed in the invocation of this class.  The file format can be adjusted by editing the list comprehension in the return statement :

return [row.rstrip().split('\t') for row in c.readlines() if not row.startswith('#') and row.strip() != '']

As written, this will return a list comprised of lists of 3 values which are in tab-delimited format in the file.  It also ignores any row beginning with a hash # for commenting, or blank lines.

The contents of my config file for the PokeyHost application are :

# Application Configuration
version	0.1	Development Version
def_dest	./default_output	Default output destination folder
def_arch	default_archive	Default archive name

# Remote Database Configuration
db_host	pokeybill.us	Database Host Name
db_tpye	mysql	Type of Database (Currently only MySQL supported)
db_user	wnrmndn_remote	Database Username

# Defaults to non-Unicode
use_unicode	False	Sets default encoding for CHAR,VARCHAR, and TEXT

# Defaults to port 3306 unless otherwise specified
db_port	3306	Database connection port

# SSL Cert paths - if enabled, will attempt to connect using SSL
#db_ssl_cert	/etc/mysql-ssl/client-cert.pem	Path to client SSL certificate
#db_ssl_key	/etc/mysql-ssl/client-key.pem	Path to client SSL key 

# *** Future versions - compression, socket, MySQL conf specification

# Compression options
comp	w:gz	Mode / Compression Algorithm [w|w:gz|w:bz2]

Each configuration option value will become an attribute of the PokeyConfig class containing a list with two values – the value and description of that attribute.

In the example above, once loaded the Compression algorithm option would be accessed via the PokeyConfig.comp attribute, with the value PokeyConfig.comp[0] containing the value, and PokeyConfig.comp[1] containing the description.

Used as a superclass

The PokeyConfig class is intended to be a superclass that could be applied in an application as in the following example.

# PokeyConfig() As a SuperClass

import pokeyworks as fw

def my_app():
    conf = AppConfig('./app.conf')

class AppConfig(fw.PokeyConfig):
    """ Child Class of the PokeyConfig Class """

In this example the AppConfig class is a child class of the PokeyConfig class, and would have access to all of its methods and attributes.  The file app.conf in the script executing directory is loaded and the AppConfig  class would contain all config attributes in this file.  Using a child class allows you to extend the class with custom __init__ and other methods if desired, as your application requires.

More to come

The PokeyWorks framework contains many additional tools, including language parsing (word pluralization, grammar and vocabulary library support), ircBots (for integration with various messaging systems), file system operations (local module installation using easy_install, resource path detector), and much more.

Stay tuned for more in the PokeyWorks series.