Archive for July 2009

Creating fake words: a pseudoword generator

I was trying to figure out how to create a word that's not a word. What I ended up doing was creating a way of generating a random syllable, and then simply appending 2 or 3 of them together. It seems to work well enough. Here's what I got in Python:

import random
 
vowels = ["a", "e", "i", "o", "u"]
consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 
              'r', 's', 't', 'v', 'w', 'x', 'y', 'z']
 
def _vowel():
    return random.choice(vowels)
 
def _consonant():
    return random.choice(consonants)
 
def _cv():
    return _consonant() + _vowel()
 
def _cvc():
    return _cv() + _consonant()
 
def _syllable():
    return random.choice([_vowel, _cv, _cvc])()
 
def create_fake_word():
    """ This function generates a fake word by creating between two and three
        random syllables and then joining them together.
    """
    syllables = []
    for x in range(random.randint(2,3)):
        syllables.append(_syllable())
    return "".join(syllables)
 
if __name__ == "__main__":
    print create_fake_word()

The first four functions are for generating an individual type of syllable (V, CV, or CVC) and then _syllable() just chooses one of them at random. Finally, create_fake_word() calls _syllable() a few times and joins them together. Here is some example output:

hojocina
eliphaa
deaketyed
ciboa
tiuzi

I haven't a clue whether or not there is a better means of generating words that look somewhat real. If you know of a better method, I'd love to hear it!

A simple event-driven plugin system in Python

So I just wanted to share my solution for integrating a plugin system into Python applications. Overall it's separated into four parts: the plugin manager, the plugin(s), a config file listing the plugins, and the application that triggers the events. In this system, plugins are just functions that get registered through the use of a decorator.

Let's start with the code for the plugin manager:

# pluginmanager.py
from collections import defaultdict
 
 
plugins = defaultdict(list)
def register(*events):
    """ This decorator is to be used for registering a function as a plugin for
        a specific event or list of events. 
    """
    def registered_plugin(funct):
        for event in events:
            plugins[event].append(funct)
        return funct
    return registered_plugin
 
 
def trigger_event(event, *args, **kwargs):
    """ Call this function to trigger an event. It will run any plugins that
        have registered themselves to the event. Any additional arguments or
        keyword arguments you pass in will be passed to the plugins.
    """
    for plugin in plugins[event]:
        plugin(*args, **kwargs)
 
 
def load_plugins(config_file):
    """ This reads a config file of a list of plugins to load. It ignores
        empty lines or lines beginning with a hash mark (#). It is so plugin
        imports are more dynamic and you don't need to continue appending
        import statements to the top of a file.
    """
    with open(config_file, "r") as fh:
        for line in fh:
            line = line.strip()
            if line.startswith("#") or line == "":
                continue
            __import__(line,  globals(), locals(), [], -1)

The plugin system is separated into three functions. The first is register() which is the decorator for registering plugins. The function trigger_event() is used by your application notify the plugin system that an event has occured. It goes out and runs any plugins registered with the event. Lastly, load_plugins() accepts a file location that contains the plugins to be loaded. We'll investigate these more closely later on in the article.

An example plugin

Let's build a package called "plugins" and create a module inside that called "example.py" with the following data:

import pluginmanager
 
@pluginmanager.register("FOO_ACTIVE")
def print_data(*args, **kwargs):
    if "data" in kwargs:
        print "Received the following: %s" % kwargs["data"]
    else:
        print "Didn't receive any data."

Here, the print_data plugin is registered for the FOO_ACTIVE event, and will be run if that event is triggered.

How to trigger the plugin

First, the plugin needs to be loaded. Right now, the source file is just sitting out there and unless we import it we'll never be able to do anything with it. So, you can either do a manual import or you can call the load_plugins() function and have it parse a list of plugin locations. We'll use the latter method.

The config file (plugins.list)

Let's create a config file with data resembling the following:

# Blank lines and lines starting with a hash (#) are ignored.
# So be as verbose as you want.
plugins.example

As you can see from above, it specifies to import the plugins.example module (since plugins is a package and example is a module inside of it). I'm doing it this way so adding or removing additional plugins is simple.

Application use

Finally, let's look at how an application would utilize this system:

import pluginmanager
pluginmanager.load_plugins("plugins.list") # load any plugins in the list
 
# ... code ...
 
# The FOO_ACTIVE event occurs somewhere
pluginmanager.trigger_event("FOO_ACTIVE", 
                            data="this data is sent to the plugin", 
                            foo="so is this")
 
# ... code ...

What happenes here is pretty straightforward. The manger loads the plugins, the application eventually triggers the FOO_ACTIVE event and sends data along with it. Any arguments or keyword arguments after the event in trigger_event() are sent directly to the plugin. When the event is triggered, it finds any plugins that have registered with it and activates it.

So yeah, that's what I came up with. It does have the downside of not being able to easily extend a plugin like you can with class-based systems since you can't inherit. Still, it's pretty nice for smaller apps that want a taste of pluggability.

If you have your own system for dealing with this type of situation, I'd love to hear from you.

Take Albuterol Inhaler
Order Inhaler Albuterol
Buy Albuterol Online no Prescription
Online Buy Albuterol Without a Prescription
Purchase Online Without a Prescription Albuterol
Online Buying Aldactone
Purchase Online Without a Prescription Aldactone
Aldactone for Acne
Buying Aldactone Legally
Buy Tablets Aldactone Online
Cheapest Buy Allopurinol
Purchase Allopurinol Online no Prescription
Buy Without a Prescription Allopurinol
Buy Allopurinol Free Delivery
Legally Order Allopurinol
Buy Anafranil Without a Prescription
Buy Anafranil Pills
Buy Anafranil Medication
Buying Without a Prescription Anafranil Online
Take no Prescription Anafranil
Buying Atarax Overnight Delivery
Purchase Atarax Free Delivery
Alternative Purchase Atarax
Purchase Atarax by Phone
Legally Purchase Atarax
Order Avodart Without a Prescription
Tablets Order Avodart
Pills Buying Avodart
Buy Avodart Medication
Tablets Purchase Avodart
Alternative Purchase Bactrim
Take Bactrim Next Day Delivery
Online Buying Bactrim
Cheapest Buy Bactrim
Purchase Bactrim Without a Prescription
Buy Celecoxib Overnight Delivery
Purchase Cheapest Celecoxib
Tablets Buy Celecoxib
Buying Without Prescription Celecoxib
Order Online Without a Prescription Celecoxib
Take Cipro Legally
Purchase Cipro by Phone
Purchase Online no Prescription Cipro
Buy Pills Cipro
Order Cipro COD
Purchase Clomid Serophene Generic
100 Clomid Serophene
Cheapest Buy Clomid Serophene
Order Clomid Serophene by Phone
Take Clomid Serophene Medication
Buying Dapsone
Purchase Dapsone COD
Buy Dapsone Free Delivery
Take Dapsone Next Day Delivery
Order Cheapest Dapsone
Generic Buying Desyrel
Order Cheap Desyrel
Take Desyrel Next Day Delivery
Buy Desyrel Tablets
Buy Desyrel Cheapest
Buy Diflucan Medication
Buy Diflucan Pills
Take Diflucan Pills
Purchase no Prescription Diflucan
Order Cheapest Diflucan
Buying Dilantin Next Day Delivery
Dilantin Phenytoin
Buying Dilantin Overnight Delivery
Legally Purchase Dilantin
Purchase Dilantin Alternative
Purchase Diovan Pills
Buy Diovan Pills
Order no Prescription Diovan
Purchase Online Without Prescription Diovan
Buy Cheap Diovan
Buy Doxycycline Medication
Order Doxycycline Without a Prescription
Take Doxycycline Daily Dose
100 Doxycycline
Buying Doxycycline
Buy Elavil COD
Buy Elavil Medication
Order Elavil Generic
Order Elavil Pills
Purchase Elavil Drug
Order Generic Erythromycin
Buy Erythromycin Medication
Buy Erythromycin Without a Prescription
Purchase Online Without a Prescription Erythromycin
Buying Erythromycin Overnight Delivery
Order no Prescription Estrace
Buying Without Prescription Estrace
Take Estrace Drug
Purchase Estrace Alternative
Order Generic Estrace
Order Furosemide Pills
Buy Alternative Furosemide
Order Pills Furosemide
Take Furosemide Medication
Order Cheapest Furosemide
Buy Glucophage Pills
Tablets Order Glucophage
Take Glucophage Legally
Purchase no Prescription Glucophage
Why Take Glucophage
Purchase Imitrex Free Delivery
Buying Imitrex Without Prescription
Buying Without a Prescription Imitrex Online
Buy Cheapest Imitrex
Imitrex Generic
Buying Pills Inderal
Buy Inderal no Prescription
Purchase Inderal COD
Alternative Purchase Inderal
Purchase no Prescription Inderal
Order Lasix no Prescription
Purchase Lasix COD
Purchase Lasix Without a Prescription
Buying Lasix Without Prescription
Tablets Purchase Lasix
Buy no Prescription Levaquin
Buy Levaquin Next Day Delivery
Take no Prescription Levaquin
Purchase Levaquin Generic
Buy Alternative Levaquin
Buying Lexapro no Prescription
Generic Buying Lexapro
Purchase Lexapro COD
Purchase Lexapro no Prescription
Tablets Purchase Lexapro
Buy Lipitor Free Delivery
Order Lipitor no Prescription
Online Buy Lipitor Without a Prescription
Tablets Buying Lipitor
Generic Purchase Lipitor
Purchase Lisinopril Free Delivery
Purchase Lisinopril Alternative
Take Lisinopril Without Prescription
Online Order Lisinopril Without Prescription
Tablets Buying Lisinopril
Alternative Order Methotrexate
Purchase Methotrexate COD
Why Take Methotrexate
Purchase Methotrexate Online Without Prescription
Purchase Methotrexate Online no Prescription
Cheapest Buy Nizoral
Order Nizoral Free Delivery
Purchase Nizoral by Phone
Alternative Order Nizoral
Purchase Nizoral Drug
Take Nolvadex Without Prescription
Cheapest Order Nolvadex
Take Nolvadex Drug
Purchase Online no Prescription Nolvadex
Purchase Nolvadex Medication
Nortriptyline Antidepressant
Buy Tablets Nortriptyline Online
When to Take Nortriptyline
Purchase Nortriptyline Pills
Buying Without Prescription Nortriptyline
Tablets Buy Ortho Tri-Cyclen
Order Ortho Tri-Cyclen Generic
Take no Prescription Ortho Tri-Cyclen
Purchase Ortho Tri-Cyclen COD
Take Ortho Tri-Cyclen Without Prescription
Purchase Online Without a Prescription Premarin
Pills Buying Premarin
Tablets Buying Premarin
Order Premarin COD
Tablets Buy Premarin
Buy Tablets Proscar Online
Buy Tablets Proscar
Buy no Prescription Proscar Online
Purchase Proscar Alternative
Purchase Cheapest Proscar
Buy Risperdal by Phone
Order Risperdal Medication
Generic Order Risperdal
Purchase Risperdal Online no Prescription
Buy Risperdal Next Day Delivery
Take Synthroid Free Delivery
Take Synthroid Pills
Buy Without a Prescription Synthroid
Purchase Synthroid Alternative
Take Synthroid Legally
Generic Order Topamax
Order Online Without a Prescription Topamax
Take no Prescription Topamax
Order Cheap Topamax
Buy Topamax Without a Prescription
Valtrex Acyclovir
Pills Purchase Valtrex
Generic Buying Valtrex
Online Buy Valtrex Without a Prescription
Order Valtrex COD
Purchase Online no Prescription Zithromax
Buy Zithromax Next Day Delivery
Purchase Zithromax Pills
Buy Zithromax Free Delivery
Buying Without a Prescription Zithromax Online