RSS Feed

RSS

Comments RSS


Python – Accessing Oracle Databases

Still learning the Python programming language here. However, along the way, I have found a need to branch out with some add-on modules for specific tasks. One of these is a module for accessing Oracle Databases. The module is cx_Oracle. Performing a query to an Oracle database become this simple process:

import the cx_Oracle module

import cx_Oracle

make your connection to the Oracle database

connection = cx_Oracle.connect(“username”,”password”,”oracle_instance”)

create an instance of the cursor

cursor = connection.cursor()

Perform a query. This demonstrates a simple select query, how to pass some user input as an argument to it, and how to do something something with the result.

user_input = str.upper(str(input(“Please provide value to query>>”)))

cursor.execute(“select some_field from some_table where some_field like :arg_1″, arg_1 = user_input)

for some_field in cursor:

print(some_field)

Python – a learning exercise

In the process of learning Python, I have given myself a programming exercise to do.  This is a standard thing I do whenever learning a new programming language and I have used this same exercise several times before.  It is a simple text based game that simply lets a user explore a maze of rooms.   If fully fleshed out, it would be a Zork or D&D type game.  It demonstrates some basic programming tasks such as reading data into an array from a file, getting and responding to user input, looping, control structures, system calls and function definitions.  I share it here as example code for others also learning Python and I welcome comments.   The code is for Python 3.x, but the comments explain how to easily convert it for use in Python 2.x.  It is far from perfect and is for learning purposes only – something to tinker with.  It is based on concepts from Creating Adventure Games on Your Computer by Tim Hartnell, which used the BASIC programming language.   You are welcome to take this code and use and modify for any purpose you like.  The current version of this code will always be at this location: mazegame-current .  You can create an appropriate data file by making a text file with contents similar to the following, the current code requires a fixed number of columns (7 at the time of this writing and subject to change) and an unlimited number of rows.
0 0 0 0 0 0 0
0 0 4 2 0 0 0
0 0 0 1 3 6 0
0 0 5 0 2 0 0
0 1 0 5 0 0 0
0 3 0 0 4 0 7
0 0 0 0 0 0 2
0 0 0 0 0 5 0

My previous blog entry Learning Python provides resources for those wanting to learn Python, as does the Dev page from my main website.

Learning Python

I have picked up learning to program in Python again.   Looking to add it to my trophy case along with BASIC, x86 Assembler, IBM BAL, Pascal, Modula-2, Forth and C.   In the process I have found some great resources, aside from the official Python website to share. The first two sites are excellent on-line books about programming in Python. The last two sites teach programming skills generally using Python as the example programming language.

A Byte of Python
Dive Into Python
Learning to Program
How to Think Like a Computer Scientist

More resources can be found on my Web Development and Programming page.

Useful Linux tips

This page has some very useful Linux tips.  Mainly for beginners, but some intermediate.   Good stuff!   Thanks to the author for sharing with the community.
Highly useful Linux commands & configurations

CSH PROGRAMMING CONSIDERED HARMFUL

Why script writing in the Unix shell csh is considered harmful.   A classic, but useful, Usenet rant.

http://www.faqs.org/faqs/unix-faq/shell/csh-whynot/

FOSS Encryption Software

I have been using EncryptionPlus Hard Disk encryption software at work now for encrypting laptops (per corporate security policies) for over six years now.   The product has work reasonably well.  Aside from personal file and email encryption products such as GPG, I was unaware of any FOSS (Free and Open-Source Software) full disk encryption systems similar to what I had been using at work, until a coworker brought TrueCrypt to my attention.   I have read the documentation and it seems perfectly capable for what I need so I am going to give it a try.  My laptop is unencrypting right now and EncryptionPlus will be uninstalled.   What makes me want to try this out is not just that TrueCrypt is FOSS, but also that I could not previously have a Wubi install of Ubuntu on this laptop while it was encrypted with the EPHD product – the Wubi Ubuntu would not boot.   Once this finishes unencrypting and EPHD is gone, I will once again do a Wubi install of Ubuntu and then try TrueCrypt and see if every thing works.   I will let the blogosphere know how this progresses!

Update — did the operation as described above. Wubi still did not work, but that now makes sense to me and I don’t think that could have worked. However, TrueCrypt itself is working wonderfully! I really like this product. I did a full disk encryption on a 75GB disk and it took much less than two hours. Also, the documentation for the software if very thorough. It is amazing that such a good system is FOSS!

Final Update — I could not be more pleased with TrueCrypt. I will highly recommend it to all my geek friends! :-)

HOWTO – Ububtu, a customizable Compose Key System

In my previous blog entry, I introduced both Unicode character entry and Compose Key entry of special characters.  One thing that becomes apparent is that there might be some characters or symbols missing from the default GTK+ code in Gnome.   Unfortunately, this is in compiled code and cannot be changed.  One option is to use Compose Key sequences for most special character entries and Unicode character entry for the occasional odd symbol.  However, if that “odd symbol” is one you intend to use often, you might wish you had a Compose Key sequence for it.   This can be accomplished, at a small price.

Situation, say you are an electronics enthusiast and have a need to type the Ohms symbol, Ω, quite often.  This symbol is not in the default GTK+ Compose Key sequences, and there is no way to add them easily into that source.   An option is to use the <Shft><Ctrl><u>03A9 Unicode method entry to get the Ohms symbol.  Sure, this works fine, but could be tedious if done on a regular basis.  What we need is a way to get this into a nice, intuitive, Compose Key sequence.  The solution is to take advantage of the fact that there is an alternate source for the Compose Key definition table and that this can be modified to add in missing symbols that you may need to use often.   This is the .XCompose file, which I will show here how to create and edit.

The method we will use is to override the default Gnome method with the orignal Xwindow method, which is also available on your system, but must be configured and activated.  It is called the Xwindow Input Method (XIM).  The system must be told to use this method instead by setting the GTK_IM_MODULE environment variable.   This is done with the following command from the shell prompt:

export GTK_IM_MODULE="xim"

For this environment variable setting to be automatically set every time you log into the system, add this line to the .gnomerc or .Xsession file in your home directory.  If these files do not exist, then create one (.gnomerc) by entering the following text in any text editor and saving the file as .gnomerc (do not neglect to include the leading dot!):

#!/bin/bash
export GTK_IM_MODULE=”xim”

Next, you want your own copy of the XIM Compose Key file to have and modify to your own liking.   We do this by copying a system copy of the file to our home directory and giving it the name .XCompose (this file name is CASE SENSITIVE!):

cp /usr/share/X11/locale/en_US.UTF-8/Compose ~/.XCompose

Finally, you will want to edit your .XCompose file to add in the symbols and keybindings  you are interested in.   For instance, here are the lines I added to bind the Unicode 03A9 for the Ohms symbol to <Multi_key> (AltGR in my case, whatever you have set as your Compose Key in your case) <o><m> (get it?) and <m><o>, also some other symbols I find handy.

<Multi_key> <o> <m>                   : “Ω”   U03A9 # OHM SYMBOL
<Multi_key> <p> <i>                    : “π”   U03C0 # PI SYMBOL
<Multi_key> <d> <t>                    : “Δ”   U0394 # DELTA SYMBOL

Is there a downside to overriding the default Gnome GTK+ Compose Key tables?  Yes, there is.  You loose the <Shft><Ctrl><u> keybinding to be able to do Unicode entry anywhere in the Gnome interface.  However, if your aim is to exclusively use Compose Key sequences and to update your data table to include any missing ones you might need, then it is not a bad trade off.

Finally, a tip on editing the .XCompose file.  Just how do you enter a represenation of the missing symbol into the file if you have already activated XIM which disables the <Shft><Ctrl><u> Unicode entry method?  Edit the file in an editor such as Vi that has its own Unicode entry method or use the Gnome “Character Map” utility to get a copy of the symbol into an editor that does not have a method of its own.   In Vi (or Vim), go into text insert mode and type <Ctrl><v><u>+unicode to enter Unicode symbols.

Once you have set the environment variable and created your file, log out and then back in to activate everything and you are all set to have a Compose Key system that you can customize to suit your own needs.

Much of this information was extracted from the following source and should be referred to for further information on the subject:
https://help.ubuntu.com/community/ComposeKey

HOWTO – Ubuntu special characters

In Ubuntu, if you want to type any Unicode character, do so my holding down Shift+Ctrl+U then the unicode for the needed character.  Shift+Ctrl+U gives you an underlined u and you type the code and hit <enter>.  Works in every application I have tried.

example:

Shift+Ctrl+U 00f4 results in ô
Shift+Ctrl+U 00e1 results in á

Of course, you have to know the Unicodes.  This chart will help  http://www.alanwood.net/demos/ansi.html

Also, you can use the COMPOSE KEY!

Addition information on configuring a compose key sequence and its use can be found here:  https://help.ubuntu.com/community/ComposeKey

I defined my compose key to be the Right-Alt key on Ubuntu 10.04 like this:

System>Preferences>Keyboard>Layouts>Options>Compose key position>Right Alt

Good list of compose key combinations to get all the weird, odd and desperately needed characters you want:  http://www.seedwiki.com/wiki/takomapark/compose2

Wikipedia entry on Compose key:  http://en.wikipedia.org/wiki/Compose_key

Another blogger’s nice short write up on using compose keys:  http://sivers.org/compose-key

Finally, for Windows users who would like to have Unix/Linux style compose key functionality this program works well and is free:  AllChars

Ubuntu, when things go badly – Synaptic HOWTO

This is good information for anyone who is going to be managing an Ubuntu system.

https://help.ubuntu.com/community/SynapticHowto

also good documentation on using the shell interface for Apt

https://help.ubuntu.com/community/AptGet/Howto

I had a 9.10 to 10.04 upgrade break due to an internet failure during the process.   The following is from the SynapticHowto.  The upgrade was so broken that it was leaving me at a shell prompt to login, the Gnome desktop was not loading automatically and the Update Manager would not finish the upgrade.  I did the following and it saved my upgrade and got everything working properly.

Broken Upgrade or Installation

  • What to do if an installation process fails and you find it is no longer possible to install or remove packages:
  • Open a Terminal and type the following commands, pressing the Return or Enter key after each (you may have to type in your password):
     sudo dpkg --configure -a
     sudo apt-get install -f

a neat trick via Ghostscript

I have found a neat trick.  Not original, found it here:  http://www.sls.psi.ch/controls/help/howto/tips_n_tricks.html#PostScript

PostScript and PDF

  • To concatenate PostScript or PDF files into a single PDF file:

    gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite \
    -sOutputFile=<outfile> <infile> <infile> …

  • To concatenate PostScript or PDF files into a single PostScript file:

    gs -q -dNOPAUSE -dBATCH -sDEVICE=pswrite \
    -sOutputFile=<outfile> <infile> <infile> …

  • This trick used in combination with the fact that in Ubuntu you can print not only to a printer, but also to either a postscript or PDF file directly, means that if a book is presented in html on the web, say with each chapter being an individual HTML page, you can easily print each of these to a file and then use the trick to concatenate the ps or pdf files together to reassemble the entire book.  For instance, I am doing that with this book that is online:  http://www.gigamonkeys.com/book/

    I do not know how many other books that I have run across on the web that were HTML like this and I wanted to reassemble them for a good print out.  This is a way to do it.

    By the way, “gs” is the command line program for Ghostscript, which should be already installed on your Ubuntu system by default.  If you install Ghostscript on a Window system, the same trick should work there too so long as you print the HTML to a PDF printer driver.  Ghostscript can be downloaded from here:  http://pages.cs.wisc.edu/~ghost/

    Oh, yes, this is also cool…:  http://pages.cs.wisc.edu/~ghost/redmon/index.htm

    Why Desktop Linux (still) Sucks…

    AND WHAT WE CAN DO TO FIX IT!

    This is not a slam against Linux.  It is all about making our favourite OS more viable and available to more people.

    Ubuntu 10.04 LTS is released

    The new Unbuntu, 10.04 LTS, has been released. It is great!
    Go get Ubuntu 10.04.
    A good review from another blogger: Ubuntu 10.04 – Perfect

    The Magic SysRq key

    The Magic SysRq key!   If you use a GNU/Linux OS, discover what the SysRq key on your keyboard can do for you.  http://en.wikipedia.org/wiki/Magic_SysRq_key

    Revolution OS Movie

    Richard Stallman, I love his software but hate his politics!

    SDFARC – the SDF.org Amateur Radio Club

    Here on SDF.org there are nearly 2000 clearly identified and active Amateur Radio operators.  That is a great base of users from which to draw an active membership of the newly forming SDFARC – the SDF.org Amateur Radio Club.  There has been a fair amount of interest shown so far, even with the relatively limited word that has been put out about it.  It is hoped that this organisation will be able to be an international Amateur Radio Club that will serve the interests of Amateur Radio Operators who are members of SDF.org.  We are planning on having nets, starting out by using echolink (see the growing list of echolink repeaters we are using: http://sdf.org/?tutorials/sdfarc_repeaters), but hope to expand our communications to also include HF, CW, and other modes. If you are a Ham and already an SDF.org member, please consider joining us. Find our discussions about SDFARC on the SDF.org shell bboard Amateur sig, or on the SDFARC webforum. If you are just a visitor to my blog but are interested. Please visit the WA5PB Ham Website to find out more about becoming a licensed Amateur Radio Operator. Also, please visit SDR.org to learn how to join the best online community on the Internet.

    73,
    Bill – WA5PB
    The WA5PB Amateur Radio & Unix Webpages

    40 years since the beginning of time(2)!

    I was reading bboard on SDF this morning and discovered that user kickmule had posted a link to a blog pointing out that this January 1, 2010 is the 4oth anniversary of the Unix system call time(2) which has been faithfully counting the seconds for all of us since January 1, 1970.  So, Happy B-Day to time(2)!
    Happy Birthday time(2)!

    Also, here is a link to kickmule’s blog on SDF, very nice!

    HOWTO – PHP simple webpage hit counter

    This is a snippet of PHP code designed to give you a working hit counter for a website.

    To set it up create a file called hitcounter.txt, and save it to your webspace. This will be the storage for the hit count data. It will need to have both read and write permissions.

    Next, on any web page you want to hit count, add the following code: (this assumes a .php file or .html file that will be processed for PHP)

    < ?php
    include ("counter.php");
    ?>

    Finally, type or copy the following to a text file and save it as counter.php to your webspace. You will want to adjust the location shown to hitcounter.txt in the code to match your own file structure. Also, you may want to change the text on the echo line, between the quotes, to suit your needs. There is no error checking on the file open or close since one would presume you will have full control over location and state of the hitcounter.txt file.

    < ?php
    $count_my_page = ("/my_web/hitcounter.txt");
    $hits = file($count_my_page);
    $a = $hits[0];
    $b = $a + 1;
    $hits[0] = $b;
    $fp = fopen($count_my_page,"w");
    fputs($fp,"$hits[0]");
    fclose($fp);
    echo $hits[0] . " visits to this website since Dec. 21, 2009";
    ?>

    HOWTO – PHP page last modified code

    This is a little snippet of PHP code that makes a webpage self-aware of when it was last modified and prints out an appropriately formatted message. (assumes that webpage is .php or that .htaccess is configured for .html to be processed as PHP)


    // outputs e.g. This file was last modified: December 29 2002 22:16:23 UTC.

    $currentFile = $_SERVER["PHP_SELF"];
    $parts = Explode(‘/’, $currentFile);

    $filename = $parts[count($parts) - 1];
    if (file_exists($filename)) {
    echo “This file was last modified: ” . date (“F d Y H:i:s”, filemtime($filename)) . ” UTC.”;
    }

    Advice on surviving in the corporate IT jungle

    Stuff I’ve learned at Microsoft. A very insightful article by Sriram Krishnan about how to survive in the corporate IT jungle. Lots of good advice!

    HOWTO – install EiffelStudio v6.4 on Ubuntu 9.10

    (What follows is intended to be helpful, but you may notice that I am less than pleased with the whole experience of obtaining and installing this particular software package.)

    After having some fun and games getting EiffelStudio 6.4 installed on my Ubuntu 9.10 system, I have finally had success and I herein share what I had to do in hopes of sparing others the same aggravation.   I am assuming you have already went through the gauntlet of creating a login account for the Eiffel Software site and accepting their EULA just to be able to download the install package (get the GPL version), not to mention figuring out that the two packages listed for the Linux platform 64 bit and 32 bit are actually the exact same package.  If this was not enough to discourage you from bothering, then proceed!

    Once you have downloaded the installation package, you have to figure out what to do with it.  Well, if you are an experienced Linux user you go ahead and extract it out somewhere.   You will get an Eiffel64 folder.   However, the software is not yet fully installed.  In the Eiffel64 folder, you will find an INSTALL.readme file.  This file contains a link back to the Eiffel site with the actual installation instructions, which oddly enough, gives you instructions on how to extract the very folder you just found the INSTALL.readme file in.  I can only presume it is intended as a sick inside joke about recursion…   Any how, here is that link to the installation instructions.
    http://docs.eiffel.com/book/eiffelstudio/eiffelstudio-linux
    The instructions assume you have logged in as root, but as any Ubuntu user will know, that is not necessarily the case, so apply sudo to the instructions given as appropriate. In what follows I give my slant on the official instructions, so go ahead and read those now and then come back here to see my interpretation.

    Ready???

    First, you are asked to check your system to see if you have GTK+ 2.4 or higher. If you are like me, you may not know or you assume you have the latest greatest. I mean, you did just install Ubuntu 9.10 with the current Gnome build! Even if you do check, you will find that you have GTK+ 2.18 ( rember 18 is greater than 4!), so you think you are all set. Not so fast bucko! Unless you intentionally went to the trouble of installing Ubuntu 9.10 with all the development packages, you do not have the actual GTK+ development libraries you are going to need. You should install gnome-core-devel. Either use Synaptic Package Manager to do this or do this at the shell prompt:
    $ sudo apt-get install gnome-core-devel

    Second, you will be given instructions on extracting the Eiffel64 package. Since you have already extracted somewhere, just copy the whole folder to the intend final location. You ultimately want the EiffelStudio installation to be at /usr/local/Eiffel64.

    Third, you will be asked to add some environment variables to you system and to adjust your system PATH. Instead of doing it exactly as they say, save yourself some frustration and just add the following to your lines to the end of you .profile file. Log out and then back in to make these take affect prior to continuing.

    # Settings for EiffelStudio
    export ISE_EIFFEL=/usr/local/Eiffel64
    export ISE_PLATFORM=linux-x86
    PATH=$PATH:$ISE_EIFFEL/studio/spec/$ISE_PLATFORM/bin

    Finally, go down to the bottom of the Eiffel installation instructions page. You will find instructions on building the examples. I do very much recommend you do this because it will also build the precompiled environment for the EiffelStudio development system you will need. However, you will need to grant yourself privileges to the directory in which the compilation with take place, so do this at the shell prompt:

    $ sudo chmod 777 /usr/local/Eiffel64/precomp/spec/linux-x86

    Now you can follow the final instructions that are given successfully, which are to run this $ISE_EIFFEL/make_install at the shell prompt. This will start the compilation process which will take SEVERAL minutes. Take a break while this is running, you deserve it! ;-)

    You should be able to now run estudio from the shell prompt or create yourself an equivalent desktop icon to launch it and have a functioning EiffelStudio 6.4 development system. Though I have been a bit aggravated by the process to get this installed, it does appear to be a very nice system and I have created a base EiffelVision2 project successfully. Give it a try! :-)