Code Comments

Code Comments

Tips and short tutorials on various programming technologies

Code Comments RSS Feed
 
 
 
 

Archive for December, 2008

Find the Class of a Python Object

Sometimes you are working with an object in Python, and because Python is not a strongly typed language, you don’t always know which class it is an implementation of. A quick way to find out is to use the type method, which can be invoked on any object and gives you a String representation of [...] Read more »

How to Override toString() in Python

In popular object-oriented languages such as Java and C#, they have a built-in method to each class that allows you to obtain a String representation of that class. They call these methods toString() or ToString(), respectively. You can use the default implementation, which basically gives you the name of the class (not usually very helpful). [...] Read more »

Convert Results to Comma-delimited List in Oracle

Let’s say you are running a query and that there is a one-to-many relationship between one of the columns in one table and a column in another table. You could always retrieve the data from the database and process it using a regular programming language, but sometimes you want to do it all on the [...] Read more »

Python Yield Example

In Python, there is a bit of functionality that makes it convenient at times to process data while it is being processed inside another method. For example, let’s say you are searching for files within a directory that match a certain pattern, but you don’t want to wait the full time for the files to [...] Read more »

Search for Files in Directories Recursively in Python

Let’s say you want to search for files in a directory tree of files and directories. And suppose you want to search for all .zip files. You can do this from within Python by looping through the folders and trying to find files that match the *.zip wildcard pattern.
Here is some code I copied from [...] Read more »

How to Perform HTTP GET in C#

Suppose you have a Web resource that you want to download programmatically (as opposed to doing it manually from a Web browser) from C#. The first thing you need to do is send an HTTP request to the server with the URL (e.g. http://www.google.com). The server will then process the request and send an HTTP [...] Read more »

Find the Mean of an Array of Numbers in C#

Let’s say you have an array of numbers and want to be able to find the mathematical mean of those numbers in C#. To my knowledge, the .NET Framework does not have a function to do this built in. (Please let me know if you know otherwise.) So I built a method that does this. [...] Read more »

Find the Sum of an Array of Numbers in C#

Let’s say you have an array of numbers (either doubles or integers) and want to be able to find the sum of these numbers. To my knowledge, there is not a built-in way to do this in the .NET Framework, so I created a little utility method that does this for doubles and for integers. [...] Read more »

Find the Mathematical Product of Double Array in C#

If you have an array of doubles (or floats) in C#, there is no built-in method (that I know of) to find the product of those numbers. The product is where you multiply all of the numbers together. I needed this, so I created a utility method that does this simple operation. See below.

public static [...] Read more »

How to Invoke a Method Dynamically in Python (Reflection)

Sometimes you want to be able to invoke a method dynamically rather than calling it explicitly. Or in other words, let’s say you have a bunch of methods, and you have a string object telling you which method to execute, but rather than doing a bunch of if statements you want to be able to [...] Read more »

Cast a List from One Type to Another in Python

One of the key properties of Python is that it is a weakly typed language. In contract to strongly typed languages such as Java, you don’t have to define the class of an object when you instantiate it. This offers much flexibility. But sometimes you want to be able to define precisely what class of [...] Read more »

Easy Way to Append PDF Files Programmatically in Python

Recently I had a bunch of one-page PDF documents that I wanted to be able to paste together so I can read them all as a single document. It turns out this is possible to do with Adobe Acrobat (which makes sense), but you have to pay for this product, and I wanted to do [...] Read more »

How to Properly Sort Strings Containing Numbers in Python

Let’s say you have a list of strings and want to be able to sort them. Python makes this extremely easy to do, which is something I really love about it.

x = ["abc", "def", "aaa"]
x.sort() // would result in [’aaa’, ‘abc’, ‘def’]

But what if you have numbers embedded in those strings? Most of us have [...] Read more »

List Files in Directory Using Wildcards With Python

Let’s say you are trying to retrieve the names of a bunch of files in a given directory that match a specific pattern. You can do this pretty easily at the command line using a command such as the following in Windows.

dir *.txt

Or something like this in Linux:

ls *.txt

But if you want to do this [...] Read more »

How to Encrypt and Decrypt Files in Python

If you want to be able to encrypt and decrypt files with Python, you might check out the following sites:
http://www.dlitz.net/software/pycrypto/
http://www.amk.ca/python/code/crypto.html
However, if you know you’re going to only be using Linux, I recommend doing file encryption as explained in this post. You can invoke commands at the command line by following the explanation here. Read more »

Login as a Different User in Linux

Say you log in to a Linux machine but then want to switch to a different user. You can do this without logging out as the first user. A time when this might come in handy is when you are logged in as a regular user but want to install some software as the root [...] Read more »

Is a Date in a Leap Year in Java

Going back to my very first programming class in 1994, I learned how to design an algorithm that would tell whether a given date was in a leap year or not. We all know that every fourth year is a leap year. However, it gets a tiny bit tricky at the turn of the century [...] Read more »

Get Day, Month, Year from Dates in Java

Please refer to this post for an explanation of how to create Date objects in Java. The code below builds on the code described there. Unfortunately, it takes a little extra code to find the day, month, or year from a Date object. You have to create a Calendar object. Apparently, the Java developers wanted [...] Read more »

How to Compare Strings (Determine Equality) in Java

Most of what you need to know about this is explained in great detail in this post.
The short answer is when you create String objects using the String constructor, you cannot use the == operator to compare those two strings in Java (like you can in C#). You have to use the equals method that [...] Read more »

Find the Difference Between Two Arrays in Java

Please refer to this post for explanation of how to do this using Lists in Java. If you wanted to do the same thing with arrays, you would simply need to convert the arrays to ArrayLists before you find the difference. Read more »