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.7 or higher, including Python 3.

Contents

1. Introduction
2. Installing cx_freeze
3. Verify Installation
4. Create The Setup Script
5. Begin Compilation
6. Output Directory


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.7 or higher, including Python 3.


2. Installing cx_freeze

In the terminal (cmd), install by running the following command:


pip install cx-Freeze

– OR –


python -m pip install cx-Freeze

If this process completes with no errors, skip to the next step!

Note: If using Windows and you get an error that says 'python' is not recognized as an internal or external command, a simple solution is to navigate in the terminal (cmd) to the directory of your Python installation folder, and then follow the steps below.

The default Python installation directory is the following:


C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Your-Python-Version\

Once you have navigated to the Python folder for the version you are using, in the terminal (cmd), run the following command.


python.exe -m pip install cx-Freeze

Tip: If you want to run the Python command from the terminal without having to navigate to the installation folder, it is recommended that you add your Python directory to the Windows Path. Click here for information on how this is done.


3. Verify Installation

Once the cx_Freeze installation is complete, you should see the newly installed files for this module located in the following directory (Windows):


C:\Path-To-Your-Python-Version\Lib\site-packages\cx_Freeze

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


4. Create The Setup Script

To use cx_Freeze we 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.py file.


5. Begin Compilation

In order to start compiling the hello.py file, we need to open the terminal (cmd).

Next, once you have created the setup.py file, save the setup.py file into the same directory as your project, and run the following command in the terminal (cmd) from your project directory:


python setup.py build

This command essentially starts compiling the hello.py file.

If this process completes with no errors, skip to the next step!

Note: If using Windows and you get an error that says 'python' is not recognized as an internal or external command, a simple solution is to copy and paste the setup.py file and ALL of the Python files associated with your project into the directory of your Python installation before running the command.

The default Python installation directory is the following:


C:\Users\YOUR_USERNAME\AppData\Local\Programs\Python\Your-Python-Version\

Note: I have Python installed in the following directory, so the steps below will use this path.


C:\Python39

Here is a sample screenshot of the instructions after copy and pasting the files into the Python installation directory: (click to enlarge)

Next, copy and paste these commands into the terminal (cmd).

#1. Change directories into your Python installation folder


cd C:\Python39

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


python.exe setup.py build


6. Output Directory

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

By default, the Python executable should be located in a folder titled ‘build‘, located in the same directory as where your setup.py resides.

If you followed the steps above to copy and paste your files during the build process, it should be located at the following:


C:\Path-To-Your-Python-Version\build\exe.win-3.9

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 are 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 ‘Hello World’ executable: (click to enlarge)

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.

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)