Archive for the 'Tip' Category
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 »
December 29th, 2008 | Posted in Python, Tip | No Comments
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 »
December 29th, 2008 | Posted in C#, Tip | No Comments
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 »
December 28th, 2008 | Posted in C#, Math, Tip | 1 Comment
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 »
December 28th, 2008 | Posted in C#, Math, Tip | 1 Comment
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 »
December 27th, 2008 | Posted in C#, Math, Tip | No Comments
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 »
December 27th, 2008 | Posted in C#, Java, Python, Reflection, Tip | No Comments
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 »
December 27th, 2008 | Posted in Collections, Python, Tip | No Comments
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 »
December 26th, 2008 | Posted in Python, Tip | No Comments
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 »
December 26th, 2008 | Posted in Python, Tip | 1 Comment
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 »
December 22nd, 2008 | Posted in Python, Tip | No Comments
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 »
December 20th, 2008 | Posted in Linux, Python, Tip | No Comments
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 »
December 20th, 2008 | Posted in Linux, Tip | No Comments
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 »
December 18th, 2008 | Posted in Java, Tip | No Comments
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 »
December 17th, 2008 | Posted in Java, Tip | No Comments
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 »
December 17th, 2008 | Posted in Collections, Java, Tip | No Comments
This is a bit of a hack, but I don’t know of any way to do it more elegantly. You kind of wish the Java developers had gone one more step and included this in their framework, but anyway….
The same type of idea should work for Integers, Longs, Floats, etc.
public static boolean IsDouble(String value)
{
[...] Read more »
December 17th, 2008 | Posted in Java, Tip | No Comments
Let’s say you have a folder with files that you want to delete. The following code illustrates how to do this. You could also combine it with the code in this post to recursively delete files in subdirectories as well.
import java.io.*;
…
public static void DeleteFilesInDirectory(String dir)
{
String[] fileNames = new File(dir).list();
[...] Read more »
December 17th, 2008 | Posted in Tip | No Comments
In this post, I explained how to find files using wildcard patterns. This current post builds on the code from that post and allows you to do a wildcard search recursively (in all subdirectories) rather than just in a single directory. Notice that this method calls itself (which is what recursiveness is all about).
import java.util.*;
import [...] Read more »
December 16th, 2008 | Posted in Java, Tip | No Comments
Well, if you click on Start and Turn Off Computer, it will bring up a screen asking whether you want to go into StandBy, Restart, or Turn Off. Sometimes I have noticed that it starts to take a really long time for this screen to come up. I have noticed that this usually happens when [...] Read more »
December 14th, 2008 | Posted in Tip | No Comments
Collections, such as ArrayList and Set, in Java used to be able to contain any class. So you could implement the following code without a problem.
ArrayList oldWay = new ArrayList();
oldWay.Add("xyz");
oldWay.Add(456);
oldWay.Add(new CustomWhatever());
But sometimes this flexibility caused problems because you might want to limit what could be contained in that list (for example, limit it to String [...] Read more »
December 14th, 2008 | Posted in Collections, Java, Tip | No Comments