Monthly Archives: July 2013

Python || How To Install PyPdf Using Python 3

pyPdf” is a pure Python library built as a PDF toolkit. It is capable of:

• Extracting document information (title, author, ...),
• Splitting documents page by page,
• Merging documents page by page,
• Cropping pages,
• Merging multiple pages into a single page,
• Encrypting and decrypting PDF files.

By being pure Python, it should run on any Python platform without any dependencies on external libraries. It can also work entirely on StringIO objects rather than file streams, allowing for PDF manipulation in memory. It is therefore a useful tool for websites that manage or manipulate PDFs.

=== 1. INSTALLING PYPDF ===

A Windows installer for pyPdf is available here. At the time of this writing, the installer that was listed on the download page was titled “pyPdf-1.13.win32.exe.”

Note: If you are using Python 2 and want to install pyPdf, the Windows installer available on the download page should be all you need. No further installation instructions are necessary.

Next, follow the command prompts from the installer and wait!

When the pyPdf installer is completed, you should see the newly installed files which correspond to this module located in the following directory:


C:\Python32\Lib\site-packages\pyPdf

Note: Python 3.2 is installed on my system, and the above directory reflects that. The Python directory may be named something different on your individual system depending on the Python version you are using.

If everything installed correctly, proceed to the next step.

=== 2. UPDATE EXISTING PYPDF FILES ===

pyPdf was originally written for Python 2, but a Python 3 compatible branch has since been made available. The updated files can be found here, and enable pyPdf to be integrated with Python 3.

To update these new Python 3 files with the old Python 2 files, locate the following directory on your system:


C:\Python32\Lib\site-packages\pyPdf

Here is a sample screenshot demonstrating the files which resides in the above directory: (click to enlarge)

Next, update all of the old Python 2 files that’s listed in the above directory with the new Python 3 files that’s listed on this page.

One way to update the files that’s currently stored on your computer is to locate the file on your system with the exact same file name as listed here, and copy/paste the contents of the new file into the contents of the old file that’s currently stored on your computer.

Or you can download all of the files at once here, and move/replace these new files with the existing files that’s currently located on your system.

=== 3. UBUNTU USERS ===

If you are an Ubuntu Linux user and want to install pypdf, open the terminal and run the following command:

NOTE: Replace “python3.2” with whatever version of python that’s installed on your system.


sudo mkdir /usr/local/lib/python3.2/dist-packages/pyPdf

Next, copy and paste the files located in the above download link into the following directory:


/usr/local/lib/python3.2/dist-packages/pyPdf

Once the following steps are completed, you should now be ready to use pyPdf with Python 3 programs!

Python || How To Create An Executable Using Cx_Freeze

Python is a simple and powerful programming language for scripting and application development. Various GUI packages available for Python makes it a great choice for developing applications. But what if you want to create an executable file from the python script you just created? This page demonstrates that process using a free third party module called “cx_Freeze.” This module is ideal for use because it supports Python 2.3 or higher.

=== 1. INTRODUCTION ===

cx_Freeze is a set of scripts and modules for freezing Python scripts into executables, in much the same way that py2exe and py2app do. Unlike these two tools, cx_Freeze is cross platform and should work on any platform that Python itself works on. It supports Python 2.3 or higher (including Python 3), since it makes use of the zip import facility which was introduced in 2.3.

Note: Binary packages must be compatible with the Python installation on your system or you will get errors in your frozen executables about missing builtin modules or missing symbols. This is not likely to be a problem on Windows (since most installations are from python.org) but on Linux it is more likely (since Linux distributions often package Python in different ways). If you get such errors, download the source package and build the binaries yourself.

=== 2. INSTALLING CX_FREEZE ===

First, download the cx_Freeze installer which corresponds to the python version that is installed on your system. I have Python 3.2 installed on my Windows computer, so I will select the cx_Freeze version 3.2 installer to download.

Next, follow the command prompts from the installer and wait!

When the cx_Freeze installer is completed, you should see the newly installed files which correspond to this module located in the following directory:


C:\Python32\Lib\site-packages\cx_Freeze

If everything installed correctly, you should now be ready to create executable Python programs!

=== 3. CREATE THE SETUP SCRIPT ===

To use cx_Freeze you need to create a setup.py file which tells the cx_Freeze “compiler” what you want to do. Here’s a sample setup.py file whose simplicity is appropriate for our sample “Hello World” program.

This is the sample Python file.

This is the setup file.

=== 4. BEGIN COMPILATION ===

After you have created the setup.py file and inserted the data that corresponds to your specific python script within the setup file, copy and paste the setup.py file and ALL of the python files associated with your current python program into the following directory:


C:\Python32

Here is a sample screenshot demonstrating the above instructions: (click to enlarge)

In order to start compiling the hello.py file, we need to open the command prompt (CMD). Click here if you dont know how to do this.

Next, copy and paste these commands into the command prompt (CMD).

#1. Change directories into the python folder


cd C:\Python32

#2. Start compiling the hello.py program using the setup.py file


python.exe setup.py build

If cx_Freeze finished with no errors, then the process should be complete!

The python executable should be located in the following directory:


C:\Python32\build\exe.win32-3.2

Here is a sample screenshot demonstrating the directory where the executable file resides: (click to enlarge)

** NOTE: In order for the executable file to run, the other files within that folder that was generated by cx_Freeze must be bundled with the executable file at all times.

And there you have it! Here is a sample screenshot of the running executable: (click to enlarge)

C++ || Snippet – Simple Timer Class Using The Clock Function

The following is sample code for a simple timer class using the “clock()” function. The “clock” function returns the processor time that has been consumed by the program during its execution; or in other words, it returns the number of clock ticks that has elapsed since the program was launched.

The timer class demonstrated on this page has the ability to measure the execution time of a block of code or function call, and display the elapsed running time (in seconds) to the screen.

Unfortunately, by using the clock() function (instead of date based methods) to measure time, this class is only able to measure the time that was actually spent used by the CPU during the execution of the program. That means that if this timer class was used to measure how long it took for a user to enter data into the program, the users CPU wouldn’t notice any change in time, and this timer class would display that 0 seconds had elapsed.

However, by using the clock() function to measure elapsed time, it can be used to implement a simple and portable timer.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

===== DEMONSTRATION HOW TO USE =====

Use of the above class is very simple to use. Here are sample programs demonstrating its use.


SAMPLE OUTPUT:

Starting the timer..
Timer stopped!

It took 3749 clicks (3.749 seconds)

SAMPLE OUTPUT:

How many seconds do you want to sleep?: 5

Starting the timer..
0
1
2
3
4

Timer stopped!

It took 5000 clicks (5 seconds)

C++ || Snippet – Custom “Shuffle” Function For Arrays

The following is a custom “Shuffle” function which shuffles the contents of an array. This function rearranges each item within the array by swapping the value of a given item with that of some other randomly selected item.

The code demonstrated on this page is different from the STL random_shuffle function in that this implementation is meant to work mainly on standard arrays.

This implementation revolves around using a series of void pointers.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

C++ || Snippet – Custom “For Each” Iterator Function For Arrays

A “For Each” algorithm is a function that iterates over all the elements contained in a storage collection, and calls a separate user defined function to manipulate each element within that collection.

The code demonstrated on this page is different from the STL for_each function in that this implementation is meant to work mainly on standard arrays.

The example below illustrates the “For Each” concept in a fully compilable and working demonstration. This example is trivial in that all it does is call a separate user defined function to display all of the contents within each array. This implementation revolves around using a simple function pointer and a series of void pointers.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output

Java || Simple Tic Tac Toe Game Sample Code GUI

The following is another homework assignment which was presented in an intro to Java class. This program was used to provide more practice creating a graphical user interface.

REQUIRED KNOWLEDGE FOR THIS PROGRAM

JFrame
JMenu
JButtons
JPanel
JOptionPane
How To Play Tic-Tac-Toe
Special Fonts For This Game - Click Here To Download
Executable .Jar File - Click Here To Download & Play

This program features a graphical user interface (GUI) which has nine buttons arranged in a square. A user plays the game by clicking the mouse in a available button. The game then places a symbol on the clicked button alternating between the letters “X” and an “O.”

The game begins by clicking on the “New Game” button. This action will clear away any text on the 9 game buttons. The 9 game buttons are still disabled, but the radio button is enabled. This means someone must select who plays first: “X” or “O.” After selecting the starting player then the 9 game buttons become enabled.

When a player clicks on one of the 9 available game buttons, the text on that button will change into an “X” or an “O.” The game stops when one of the two players has 3 identical symbols in a row, a column, or a diagonal. When there is such a winner, the symbol of the winner is displayed to the screen. Sometimes all 9 game buttons have been clicked on but there is still no winner. In that case the text “Tie” is displayed to the screen.

The two buttons “New Game” and “Quit” remain enabled at all times. Clicking on “Quit” will close the window.

This program was implemented into 3 different files, so the code for this program will be broken up into 3 sections. The three-file solution always has one file (the driver) which contains the “main” function. Another file contains the implementation of the graphical user interface (GUI), and the last file contains logic, which in this case is the game algorithms.

==== File #1 – TicTacToe.java ====

==== File #2 – GUI.java ====

This is the GUI (graphical user interface) class for a three-file solution which implements the game of tic tac toe. The sole purpose of this source code is to define the GUI and call methods in the BusinessLogic class when needed.

==== File #3 – BusinessLogic.java ====

This is the BusinessLogic class for a three-file solution which implements the game of tic tac toe. The is the class containing algorithms.


QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

This program uses custom fonts. To obtain those fonts, click here!

Click here to download & play the executable .jar file.

SAMPLE SCREENSHOT:
(click to enlarge)