2009-02-26

 

A Python Quicky - a function to handle user input (command line)

This is perhaps more a note to self, but of course others are welcome to use and improve on the function.


#!/usr/bin/python

import traceback
import re

def UserInput( question="> ", input_type="numeric", regex="^\d{1}$", action_on_fail="retry", help_on_error="\tYou need to supply exactly 1 numeric digit from 0 to 9. Press CTRL+C to quit." ):
quit = 0
userinput = ""
while quit == 0:
try:
if input_type == "numeric":
userinput = str( input( str( question ) ) )
else:
userinput = str( raw_input( str( question ) ) )
except:
print "ERROR - Exception: "
print traceback.format_exc()
# Validate the input
if re.compile( regex ).search( userinput ):
quit = 1
else:
print "ERROR - Input validation failed."
print str( help_on_error )
if action_on_fail != "retry":
userinput = ""
quit = 1
else:
print ""
return userinput

print "Testing user input...\n\n"
born = UserInput( "In which year were you born? ", "numeric", "^\d{4}$", "retry", "Enter a year valid 4 digit year" )
name = UserInput( "What is your name? ", "alpha", "^\w{3,32}$", "retry", "A valid name contains only alpha characters and is between 3 and 32 characters long" )
if int( born ) < 1980:
print "Hi " + str( name ) + " - You are old :-)"
else:
print str( name ) + " - you're a youngster..."


As you can see, I have used normal regex validation, so nothing too fancy.

2009-02-04

 

Memcached Functions for MySQL (UDF's)

Tangent recently updated their libmemcached_functions_mysql UDF (now version 0.8), and it's looking good. I had some issues getting it to work on 64 bit Linux, so here are some notes (Centos/RHEL 64Bit).

Before you begin you will need all the usual development RPM's including "rpm-build", "mysql-devel", "gcc-c++", "libevent-devel" and "libevent".

Next obtain the various sources:

http://danga.com/memcached/dist/memcached-1.2.6.tar.gz
http://download.tangent.org/libmemcached-0.26-1.src.rpm
http://download.tangent.org/memcached_functions_mysql-0.8.tar.gz

This is also the order in which we build and install the software.

First, get "memcached" build and installed:

$ tar xzf memcached-1.2.6.tar.gz
$ cd memcached-1.2.6
$ ./configure --enable-64bit --enable-threads
$ make
# make install

Next we get the source RPM for "libmemcached" installed:

# rpm -Uvh libmemcached-0.26-1.src.rpm
# cd /usr/src/redhat/SPECS
# rpmbuild -bb libmemcached.spec
# rpm -Uvh /usr/src/redhat/RPMS/x86_64/libmemcached-0.26-1.x86_64.rpm

Finally we build and install "memcached_functions_mysql":

$ cd memcached_functions_mysql-0.8
$ ./configure --with-mysql=/usr/bin/mysql_config --libdir=/usr/lib64/mysql/
$ make
# make install

Finally you are ready to use Memcached from MySQL - all that's left is to install the functions you need (see "utils/install.pl").

Have fun

This page is powered by Blogger. Isn't yours?