Ugly Stool Rotating Header Image

Boston Globe Bibliophiles: Bob Shacochis

The Boston Globe has a weekly column where a bibliophile is interviewed.  The article is edited down to include mostly details about what the person is reading.  If you have a hard time finding the next book to read it’s a great source.  Here’s my attempt to collect, and publish the information for the world at large.

Quick Bio: Bob Shacochis

The interviewee is an author of the following books.


“…about the fastest boat ride every down the Colorado River. It should be an instant classic.”


“Her writing is always gentle and transcendent, and, of course, it sits in darkness.”


“It’s great narrative fun.”


“…and liked it.”

Have you read any other daddy-daughter books?

“Kathryn Harrison’s The Kiss. Kathryn was my independent study student at the Iowa Writer’s Workshop.”

What books do you assign in your nonfiction seminar at Florida State University?

“I don’t assign any. Just give them a list. The five top books are…”


“…am looking forward to reading”

NOTE: All of these links have a kickback to me, so manually search if you want to avoid that.

Boston Globe Bibliophiles: Peter Sagal

The Boston Globe has a weekly column where a bibliophile is interviewed.  The article is edited down to include mostly details about what the person is reading.  If you have a hard time finding the next book to read it’s a great source.  Here’s my attempt to collect, and publish the information for the world at large.

Quick Bio: Peter Sagal

“I tried to read What I Talk About When I Talk About Running by Haruki Murakami, who I love, but couldn’t.”


“…there are a couple a good books.”


“I have friends writing books that I want to read, like…but, I really don’t have time for pleasure reading.”


“…two terrific books”


“…one of the funniest memoirs I ever read.”


“Two really good books.”


“Steve Martin’s Born Standing Up is one of the best books about making art ever written.”


“I’d love to be lost in some big novels.”


“I loved…”


“I’d love to get lost in some nonfiction, too, like…”

NOTE: All of these links have a kickback to me, so manually search if you want to avoid that.

Boston Globe Bibliophiles: Andre’ Gregory

The Boston Globe has a weekly column where a bibliophile is interviewed.  The article is edited down to include mostly details about what the person is reading.  If you have a hard time finding the next book to read it’s a great source.  Here’s my attempt to collect, and publish the information for the world at large.

Quick Bio: Andre’ Gregory

“This last week I was finishing a beautiful novel…”


“I’ve begun a tremendous biography about someone I thought wouldn’t interest me, Teddy Roosevelt”


“One of my favorite books of all time is the four-volume biography of Lyndon Johnson. I’ve read that twice. I’m just hoping he’s still with us for the fifth volume.”


“I’ve read twice.”


“I think she [Debbie Eisenberg] is our greatest short story writer.”

“An American writer I adore is Philip Roth.”


“It’s one of my very favorites.”


“I reread Brothers Karamazov again and found it rather boring. It’s not the book. It’s us. We change.”


“I’ve read Pema Chodron. She’s a Buddhist who’s written staggeringly helpful books, particularly for when you go through hard times.”

NOTE:All of these links have a kickback to me, so manually search if you want to avoid that.

Boston Globe Bibliophiles: Bill McKibben

The Boston Globe has a weekly column where a bibliophile is interviewed.  The article is edited down to include mostly details about what the person is reading.  If you have a hard time finding the next book to read it’s a great source.  Here’s my attempt to collect, and publish the information for the world at large.

Quick Bio: Bill McKibben is a journalist who has dedicated most of his life to fighting global warning.  He became an author on the topic starting in 2007.

"I just read a great novel."


"He’s only written a few books because they are so long…"


"By far my favorite book of last year was Katherine Boo’s."


"There’s a wonderful nonfiction account of Mumbai called."


"Early in my life, [I was drawn to] the America West. I spent a lot of time reading John Muir and Edward Abbey."

"I compulsively read Wendell Berry."


"When I was teaching myself how to organize, the most important document I read was Taylor Branch’s majest three-volume history of the civil rights movement."


There’s a children’s book that was of great help to me.


There are two books in recent times about the fossil-fuel industry that are really important.

NOTE: All of these links have a kickback to me, so manually search if you want to avoid that.

Dumping a Sho Chart in the LINQPad Result Window

If you dump a Sho chart in LINQPad, LINQPad creates a new WinForms tab to hold the dump result.  Actually, every time a WinForm is dumped a new WinForm tab is created.  If you dump a lot of charts this can quickly add up to a lot of tabs.  Worse, the results are not inline so you have to constantly jump between tabs to get results.  Ideally, you want the dumps all in one tab.  There is a way.  LINQPad supports dumping BMP directly to the result window.  Render the chart onto a BMP, and then dump the BMP. 

Code to do the translation from WinForm to BMP is below.  I suggest you add it to your My Extensions class so it is always available.

void DumpShoChart(ShoChart shoChart, int width, int height)
{
    var field = shoChart.GetType().GetField("plot", BindingFlags.Instance | BindingFlags.NonPublic);
    var chart = field.GetValue(shoChart) as System.Windows.Forms.DataVisualization.Charting.Chart;
    chart.Dock = DockStyle.None;
    chart.Size = new Size(width, height);
    using (var stream = new MemoryStream())
    {
        chart.SaveImage(stream, ImageFormat.Bmp);
        Image.FromStream(stream).Dump();
    }
}

LINQPad Charting with Sho

The post, Dumping Charts in LINQPad, is just a start.  There’s actually an easier way to create visualizations in LINQPad by using Sho.  Sho describes itself as the following:

Sho is an interactive environment for data analysis and scientific computing that lets you seamlessly connect scripts (in IronPython) with compiled code (in .NET) to enable fast and flexible prototyping. The environment includes powerful and efficient libraries for linear algebra as well as data visualization that can be used from any .NET language, as well as a feature-rich interactive shell for rapid development. Sho is available under the following licenses: Sho for IronPython 2.6 license and Sho for IronPython 2.7 license. See the Installers page for more details about the two versions.

If you take a small slice of the awesomeness that is Sho, and use it with LINQPad you can create visualizations easily.  The scriptability experience afforded by gnuplot or R is easily duplicated with Sho and LINQPad, however; if you now LINQ the experience is easier and more intuitive in my opinion.  Slice and dice the data in C#, and then pass the IEnumerable<> to Sho for rendering.

To get started, install Sho by downloading and running the MSI.  To run the Sho shell you need to install IronPython, but for these examples, I am only using the Sho libraries so you can skip the IronPython install if you want.

You need to add references to LINQPad for the appropriate assemblies and namespaces.  Download the sample LINQPad script as a starting point – it has all of the references set for you.  (It assumes you installed Sho on a 64-bit machine, so adjust references as appropriate.)

As a first example plot 100 random numbers using Sho.

var chart = new ShoChart();
var random = new Random();
var xs = Enumerable.Range(0, 100);
var ys = xs.Select(x => random.NextDouble());
chart.AddSeries(ys);
chart.Dump();

This results in the following graphs.  (The results will be slightly different between executions because the y values are random.)

image

The amount of code required to dump this chart is small, just three lines (minus the boiler plate).

  1. Construct the ShoChart.
  2. Add a data series to the chart.
  3. Dump the chart.

Sho’s charting can do much more.  Let’s tweak the previous chart a little bit.  Change the line color and style, insert a legend, and add a title.  Overlay a regression line on the chart to better understand the shape of the data.

var chart = new ShoChart();
var random = new Random();
var xs = Enumerable.Range(0, 100);
var ys = xs.Select(x=> random.NextDouble());
var seriesOptions =  new Dictionary<string, object>()
{
    { "color", Color.Red },
    { "linetype", "-.." },
};
var coeff = Poly.PolyFit(xs, ys, 1).Dump();
var regression = Poly.PolyVal(coeff, xs);
var  regressionOptions =  new Dictionary<string, object>()
{
    { "color", Color.Green },
};
chart.Title = "Sho Chart Title";
chart.HasLegend = true;
chart.AddSeries(xs, ys, seriesOptions);
chart.AddSeries(xs, regression, regressionOptions);
chart.SeriesNames[0] = "My Series";
chart.SeriesNames[1] = "Regression";
chart.Dump();

image

This is just some simple line plots, see the Sho documentation for more information, specifically “The Book of Sho.” 

When I pull data from a DB or a OData I usually want to visual it.  I want to understand the shape of my data, and get a better understanding of what it is trying tell me, i.e. graphical analysis.  I do this using either Excel, R, or gnuplot.  Excel is easy to get to from LINQPad, just export, select, and chart.  R and gnuplot are more involved, but create beautiful visualizations. 

Sho means I do not have to leave LINQPad.  I do not have to fiddle with Excel wizards or menus to create graphs.  I do not have the power of R or gnuplot, but enough to get the job done.

I use LINQPad and Sho to create quick reports that I can share with my colleagues.  Use the Dump() functionality of LINQPad to create a table, and use Sho to create an appropriate visualization immediately below it.  Export the LINQPad result window to HTML, convert the HTML to an .MHT file, and viola – a quick a dirty analysis of your data in one file that can be viewed by anyone.

image

I’m doing Sho a disservice by only focusing on the visualization aspects – it can do so much more.  See the documentation to find out what else it can do.

Dumping Charts in LINQPad

Erik Meijer and Brian Beckman did a video together on Channel 9 called LINQ – Composability Guaranteed.  The video is good, but in the middle of it Brian showed how to dump charts in LINQPad.  This is awesome!

LINQPad already makes it dead simple to pull data from a DB or an OData feed, or anything for that matter.  Before it was just  another step to graph the data.  Export the data to Excel, then click, click, click, and out pops a nice graph.  Brian showed that going to Excel was unnecessary.  You never have to leave the comfort of LINQPad at all.

With just a little bit of code you can create these types of graphs in the LINQPad result window.  (Copy the code from the forum, or download a copy from here.)

 

yowpdl

tqtfkn

muqcny

muyjnw

LINQPad supports dumping WinForm and WPF controls directly.  Brian’s first attempt actually spawned new windows, but there’s no need.  Just dump your forms, and LINQPad will do the right thing.  Notice how LINQPad created a new button to hold the dumped WinForm chart.  Every time a WinForm is dumped a new WinForm button is created.

linqpad

Test Configuration Sections in .config Files

The .config files centralize and standardize how per application or per library configuration data are stored.  The format can be a little dense to get through, and I always tend to leak this information from my brain.  I only need to know when I start a new project. 

I have had to relearn configuration data yet again, so I figured I should commit it to the electrons so I do not forget again.

Use the tests with external file dependencies post as a starting point.  The tests have to be written this way because the System.Configuration API does not support accepting input from a stream.

Step 1: Create a minimal configuration.  In this case, I have two configuration values.  The maximum number of tasks, and the period.

namespace My.Library
{
    public class MyConfiguration : ConfigurationSection
    {
        [ConfigurationProperty("maxTasks", IsRequired = true)]
        public int MaxTasks
        {
            get { return (int)this["maxTasks"];  }
            set { this["maxTasks"] = value; }
        }
        [ConfigurationProperty("period", IsRequired = false, DefaultValue = "00:00:15")]
        public TimeSpan Period
        {
            get { return (TimeSpan)this["period"]; }
            set { this["period"] = value; }
        }
    }
}

Step 2: Create an example configuration file to use for testing.

<configuration>
  <configSections>
    <section name="myConfig" type="My.Library.MyConfiguration, Library" />
  </configSections>
  <myConfig
    maxTasks="1"
    period="00:00:01" />
</configuration>

NOTE: When defining the configuration section (<section />) at a minimum the type attribute must include the full name of the type (My.Library.MyConfiguration).  The value after the comma is the assembly name (minus .exe/.dll) containing the type.  Additional information can be specified such as the version, and public key token, i.e. strong name of the assembly.

Step 3: Write a test to validate the configuration file.

[Fact]
public void MyConfiguration_OverrideAllValues()
{
    string fileName = Assembly.GetExecutingAssembly().LoadFileAssemblyResource(
        "My.Tests.examples.example1.config");
    try
    {
        ConfigurationFileMap fileMap = new ConfigurationFileMap(fileName);
        Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(
            fileMap);
        MyConfiguration mine = (MyConfiguration)configuration.GetSection(
            "myConfig");
        Assert.Equal(1, mine.MaxTasks);
        Assert.Equal(TimeSpan.FromSeconds(1), mine.Period);
    }
    finally
    {
        File.Delete(fileName);
    }
}

This test validates the configuration data loaded matches the data in the seed file, example1.config.  There are two other tests that should be written.

  1. Validate that period correctly defaults to 15 seconds.
  2. Validate that MaxTasks is a required parameter.

Tests with External File Dependencies

Testing a system where you have to reach outside the environment can be awkward.  A piece of code that requires a file name instead of a Stream is bad, and to be avoided.  While this can be enforced when using one’s own code, it is not true when utilizing third party code.

If you have to test an interaction with file data there are several ways to handle it.

  1. Explicitly copy the file in the test.
  2. Use the test framework to ensure the file is deployed with your test.
  3. Embed the test data in your assembly, and recover the file data at test time.

I favor option the last option because I think it offers the most flexibility.  The first option requires you to account for a resource beyond your test and binary bits.  The second option requires you to manage data outside of your IDE, i.e. cleaning up all of those test specific directories.

Step 1: Add the file to test against to the solution.  I typically add mine to an examples folder in the test project.

image

Step 2: Embedded the file in the assembly, so when you build the project the file is packed into the assembly.

To embed the file on build do the following.

  1. Right-click the file to embed.
  2. Select properties.
  3. Under Build Action select Embedded Resource.

image

Step 3: Create an extension method to extract the resource data to a temporary location on disk.

public static class MyExtensions
    {
        public static string LoadFileAssemblyResource(this Assembly assembly, string resourceName)
        {
            string tempFileName = Path.GetTempFileName();
            using (FileStream fileStream = File.OpenWrite(tempFileName))
            using (Stream stream = assembly.GetManifestResourceStream(resourceName))
            {
                stream.CopyTo(fileStream);
            }
            return tempFileName;
        }
    }

I like the idea of an extension method, but it is not required.  Accepting the Assembly as a parameter is necessary because you will probably need to differentiate between assemblies instead of just picking the executing assembly.

Step 4: Write a test that requires the external file, run the test, assert the results, and cleanup the fie.

[Fact]
public void Test()
{
    string fileName = Assembly.GetExecutingAssembly().LoadFileAssemblyResource(
        "My.Tests.examples.example1.txt");
    try
    {
        var lines = File.ReadAllLines(fileName);
        Assert.Equal(5, lines.Count());
    }
    finally
    {
        File.Delete(fileName);
    }
}

NOTE: The name of the file is not used to locate the file in the assembly, rather the name of the resource is.  The resource name is made up of the default assembly namespace, the folder path, and the name of the file all concatenated by a period.  This is why the file is loaded by using the name “My.Tests.examples.example1.txt” and not “example1.txt”.

ruby Sinatra and SSL

I am experimenting with the Google JSON API by writing a back end service.  My intent is to re-use the available client libraries to communicate with my service.  I need to test my implementation, and the mechanics of it all to understand how it works.  I used an existing simple Google service to see how the client would interact with it.

I wanted to mock the Google service quickly, so I started with Sinatra.  I implemented HTTPS support just because – not strictly necessary for this experiment.  I had to hack up the Google service document schema to redirect all URIs from www.googleapis.com to localhost, and I made a few edits to the Google ruby client to override the default port.

The code snippet below creates a self-signed certificate on every start.  I never remember the correct OpenSSL incantation to create a self-signed certificate.  I can find the answer by asking the oracle, but it seems to take a while.  I did not want to have to create another certificate ever again.  By default the auto-generated certificate is good for one year.  (I never expect the service to run for that long.)

Sinatra is configured to response with Hello World! when issuing an HTTP GET request to http://localhost:4567/.  All other HTTP GET requests simply respond with URI requested.  HTTP POST requests echo the data back to the client in the payload.

require 'sinatra/base'
require 'webrick'
require 'webrick/https'
require 'openssl'
name = "/C=US/ST=SomeState/L=SomeCity/O=Organization/OU=Unit/CN=localhost"
ca   = OpenSSL::X509::Name.parse(name)
key = OpenSSL::PKey::RSA.new(1024)
crt = OpenSSL::X509::Certificate.new
crt.version = 2
crt.serial  = 1
crt.subject = ca
crt.issuer = ca
crt.public_key = key.public_key
crt.not_before = Time.now
crt.not_after  = Time.now + 1 * 365 * 24 * 60 * 60 # 1 year
webrick_options = {
    :Port               => 4567,
    :SSLEnable          => true,
    :SSLVerifyClient    => OpenSSL::SSL::VERIFY_NONE,
    :SSLCertificate     => crt,
    :SSLPrivateKey      => key,
    :SSLCertName        => [[ "CN", WEBrick::Utils::getservername ]],
}
def agg_lines(data)
    data.each_line.collect{|x| "  >> #{x}" }.join
end
class MyServer < Sinatra::Base
    get '/' do
        "Hello World!\n"
    end
    get '*' do |x|
        "GET of URI #{x}\n"
    end
    post "*" do |x|
        request.body.rewind
        "POST to URI #{x}\n#{agg_lines(request.body.read)}\n"
    end
end
server = ::Rack::Handler::WEBrick
trap(:INT) do
    server.shutdown
end
server.run(MyServer, webrick_options)

I pieced this script together by crawling StackOverflow posts, and reading documentation.

Page optimized by WP Minify WordPress Plugin