June 27, 2008

Back Bay Lightning Strike

The Building across the street from me (Back Bay, Boston, MA) just got struck by lightning during a thunderstorm:

Python - Sort A Dictionary By Values

Python dictionaries are unordered, but you can extract the keys and values and sort them yourself in a list.

lets say you have this dictionary:
{'a': 2, 
 'b': 4, 
 'c': 3,
 'd': 1}
.. and you want to convert it into a list, sorted by value (not key), like this:
[('d', 1), 
 ('a', 2), 
 ('c', 3), 
 ('b', 4)]
There is a Python Recipe for it with various implementations discussed in the comments.

My favorite one is this:
sorted(foo.items(), key=lambda(k,v):(v,k))

June 20, 2008

MS SQL Server - Trace Information and Stopping Traces

Server-side tracing is the process of having your SQL Server machine save events to a physical file on that machine without using the Profiler client tool. Server-side tracing is enabled and controlled by using SQL Server system-supplied stored procedures and functions. With these system-supplied processes, you can identify what to trace, when to start and stop tracing, what traces are running, and view trace information stored in the trace file.

Here is how you view the number of traces currently running:
SELECT count(*) FROM :: fn_trace_getinfo(default) WHERE property = 5 and value = 1
Here is how you can find more detail about the running traces:
SELECT * FROM :: fn_trace_getinfo(default)
You can terminate a trace with the 'sp_trace_setstatus' stored procedure using the traceid:
EXEC sp_trace_setstatus 1, @status = 0
EXEC sp_trace_setstatus 1, @status = 2
setting the status to 0 stops the trace setting the status to 2 closes the trace and deletes its definition from the server

June 19, 2008

June 12, 2008

Tools to Develop Faster Web Pages

Jacob Gube just published "15 Tools to Help You Develop Faster Web Pages" at sixrevisions.com. It gives a nice list and high level overview of free tools that are useful for web performance. It includes such tools as load generators, profilers, and debugging proxies.

Pylot (my tool) made his list.. so go have a look!

His description:
"Pylot is an open-source performance and scalability testing tool. It uses HTTP load tests so that you can plan, benchmark, analyze and tweak performance. Pylot requires that you have Python installed on the server - but you don’t need to know the language, you use XML to create your testing scenarios."

June 5, 2008

Python - Generating Sparkline Graphs For Stock Pricing




In a previous post, I introduced the new historical stock pricing function in the ystockquote.py module.

Here is an interesting use of the module to create sparklines of historical stock pricing for a given stock.

To generate the sparklines, I use Grig Gheorghiu's Sparkplot Python module (which uses Matplotlib for graphing).

This module expects a file named data.txt, which contains lines of data to be graphed. All you need to do is generate a data file with the pricing data, and the Sparkplot module will take care of the rest.

To generate the data, I use a script like this:

import ystockquote

ticker = 'GOOG'
start = '20080101'
end = '20080523'

data = ystockquote.get_historical_prices(ticker, start, end)

closing_prices = [x[4] for x in data][1:]
closing_prices.reverse()

fh = open('data.txt', 'w')
for closing_price in closing_prices:
    fh.write(closing_price + '\n')
fh.close()
Now that you have the data.txt file setup, you can call the Sparkplot script via the command line to generate the sparkline image:
python sparkplot.py --label_first --label_last
*Note: The Sparkplot module needs Matplotlib installed. The sparkline output is an image (png) like this:

June 4, 2008

Java - Run a System Command and Return Output

Run a system command and return output:
import java.io.*;

public static String cmdExec(String cmdLine) {
    String line;
    String output = "";
    try {
        Process p = Runtime.getRuntime().exec(cmdLine);
        BufferedReader input = new BufferedReader
            (new InputStreamReader(p.getInputStream()));
        while ((line = input.readLine()) != null) {
            output += (line + '\n');
        }
        input.close();
        }
    catch (Exception ex) {
        ex.printStackTrace();
    }
    return output;
}
Sample Usage:
public static void main(String[] args) {
    CmdExec cmd = new CmdExec();
    System.out.println(cmd.run("ls -a"));
}