HOW TO Lower Case All Dictionary Keys in a Complex Python Dictionary

This tutorial shows you how to write and use a function that will iterate over complex, nested dictionary or list or combination of the two. For any dictionaries it finds, it will rename the keys to lower-case.

This is useful when trying to match an known string with a key name returned from another source where the characters are correct, but may come back in camelCase or in the wrong or just unknown case.

First: Write the function.
def renameKeys(iterable):
    if type(iterable) is dict:
        for key in iterable.keys():
            iterable[key.lower()] = iterable.pop(key)
            if type(iterable[key.lower()]) is dict or type(iterable[key.lower()]) is list:
                iterable[key.lower()] = renameKeys(iterable[key.lower()])
    elif type(iterable) is list:
        for item in iterable:
            item = renameKeys(item)
    return iterable

Next, create some sort of test list:
somelist = [
  {
  'myKey': 'asdA',
  'ASDFASDurKey':
    [
      {'QDD' : 'booger'},
      'asdf',
      [1,2]
    ]
  }
]

Finally call it:
somelist = renameKeys(somelist)

CAUTION: If your dictionary has two elements with the same ascii characters in two separate cases such as mydict[‘key1’] and mydict[‘KEY1’], the function works on a last-match basis. That means whichever dictionary element gets renamed last wins.

Did you find this post useful or have questions or comments? Please let me know!

Posted in How Tos, Programming, python, Software | 1 Comment

HOW TO Build non-root FreeTDS for Linux

This tutorial shows you how to build FreeTDS on any Linux (and probably Unix) system in a stand-alone fashion. No root or packages required.

Prerequisites:
You do need a compiler and functional build environment. If you can successfully ./configure && make && make install then you are all set.

Step 1: Get the software
Download the latest stable release from freetds.org

Step 2: Unpack
Unpack the software in to a directory different from the install directory.

In this example, I’m using /appserver/freetdsbuild/, but I will install in to /appserver/freetds-0.91/

mkdir /appserver/freetdsbuild/
cp freetds-stable.tgz /appserver/freetdsbuild/
cd /appserver/freetdsbuild/
gunzip freetds-stable.tgz
tar xf freetds-stable.tar
cd freetds-0.91

Step 3: Compile
Here we are going to configure it to install in to an alternate directory

./configure --prefix=/appserver/freetds-0.91 --exec-prefix=/appserver/freetds-0.91/ --enable-msdblib
make
make install

At this point it is safe to remove /appserver/freetdsbuild/ if you don’t need it.

Step 4: Make the symlink
The final stop is to make a symlink from /appserver/freetds-0.91 to /appserver/freetds. This lets you point your application to the freetds directory. If you need to modify to upgrade FreeTDS, you can build a new version and simply move the symlink. Your application won’t need to change.

ln -s /appserver/freetds-0.91/ /appserver/freetds/

Done!

P.S. You can find your config files in /appserver/freetds/etc/

Did you find this post useful or have questions or comments? Please let me know!

Posted in How Tos, linux | Tagged | Leave a comment

HOW TO Install mcrypt for PHP on RedHat Linux 6 and Oracle Linux 6

This tutorial shows you how to install libmcrypt and the companion PHP module under RedHat Linux 6 and Oracle Linux 6.

By default, RHEL and OL do not provide mcrypt or libmcrypt packages for the 6.x release. Subsequently, they don’t provide PHP5 mcrypt modules either. Many PHP apps including PHPMyAdmin and Magento make use of the better crypto support of libmcrypt.

So how do you install it? You have to build it.

Part 1: Building libmcrypt

Part 1, Step 1: Install your build environment
libmcrypt is written in C++ so you need a C++ compiler.
sudo yum install gcc-c++.x86_64
That will install the compiler and dependencies.

Part 1, Step 2: Get the software
Download it here: http://sourceforge.net/projects/mcrypt/files/
Remember, download libmcrypt, not mcrypt.

Part 1, Step 3: Pick a location and unpack it
Most people install software in to the default location, but I’m going to install in to somewhere that isn’t owned by root just to illustrate how to separate your custom app components from the OS.
mkdir /myappserver
cd /myappserver
cp /path/to/download/libmcrypt-2.5.8.tar.bz2 /myappserver/
bunzip2 libmcrypt-2.5.8.tar.bz2
tar xf libmcrypt-2.5.8.tar
mv libmcrypt-2.5.8 libmcrypt-install

Part 1, Step 4: Build it!
cd libmcrypt-install
./configure --prefix=/myappserver/libmcrypt-2.5.8 --exec-prefix=/myappserver/libmcrypt-2.5.8 --disable-posix-threads
make
make install

Part 1, Step 4: Make a Symlink
In order to make life easier for upgrades later, symlink libmcrypt to the version you built. This will let you compile a new version later and simply move the symlink. Anything else you build against this package will only need to know about the symlink.
ln -s /myappserver/libmcrypt-2.5.8/ /myappserver/libmcrypt

You have successfully installed libmcrypt. You can move it from server to server by simply copying /myappserver/libmcrypt-2.5.8/ to any other machine.

Part 2: Building the PHP Module
Building the PHP module can be done in one of two ways.

  1. Build your own PHP and copy the module to an existing PHP installation
  2. Build your own PHP and use it

I’m not going to do a full tutorial on building PHP here (Option 2). Just note that the steps are the same as Option 1, but instead of copying the extention in to your PHP installation, compile PHP with all the modules you need, then install and use the PHP you built.

Option 1: Build your own PHP and copy the module to an existing PHP installation

Part 2, Option 1, Step 1: Install your build environment
PHP is written in C so you need a C compiler.
sudo yum install gcc.x86_64
That should give you everything you need to compile PHP.

Part 2, Option 1, Step 2: Get the software
MAKE SURE YOU DOWNLOAD THE PHP VERSION THAT MATCHES YOUR INSTALLATION
Download it here: http://us3.php.net/downloads.php

For this tutorial, I used PHP 5.5.13

Part 2, Option 1, Step 3: Pick a location and unpack it
I’m going to use a non-standard location since I’m just building the software. I’m not going to use it from here.
mkdir /myappserver
cd /myappserver
cp /path/to/download/php-5.5.13.tar.bz2 /myappserver/
bunzip2 php-5.5.13.tar.bz2
tar xf php-5.5.13.tar
cd php-5.5.13

Part 2, Option 1, Step 4: Build it!
./configure --prefix=/myappserver/php-5.5.13 --exec-prefix=/myappserver/php-5.5.13 --with-mcrypt=/myappserver/libmcrypt
make

Note the --with-mcrypt=/myappserver/libmcrypt option. This points to the symlink above. If you need to upgrade libmcrypt later or rebuild the module for any reason, you don’t need to worry about what version of libmcrypt you have, just use the symlink!

You should now be able to find your module in /myappserver/php-5.5.13/ext/mcrypt

Part 2, Option 1, Step 5: Use it!

  • Copy the mcrypt directory above in to the ext directory in your PHP installation.
  • Restart your Apache server

Test it with a little PHP code:
<?php
extension_loaded('mcrypt') ? print 'Mcrypt is working' : print 'Mcrypt is not working';
?>

Did you find this post useful or have questions or comments? Please let me know!

Posted in Apache, gcc, How Tos, libmcrypt, linux, mcrypt, Oracle Linux, php, Programming, redhat, Unix | 1 Comment