FizzBuzz

We’re hiring a new developer in my group at work, and my boss is including me in the interviewing process. It’s been a few years since I’ve done developer interviews, so I’m a bit rusty. I suggested having candidates do a FizzBuzz test on a whiteboard as part of the interview.

Jeff Atwood wrote a good post about FizzBuzz on his blog back in 2007. It seems like an overly simple test, but it can be quite useful. I’ve only been asked to do FizzBuzz once myself, and it was a good experience. The interviewer was really sharp and asked me a lot of good questions about how I could do it differently or why I chose to do something a certain way. He turned a simple 12-line program into a good conversation.

At very least, FizzBuzz should help filter out candidates who are exaggerating on their resumes. If you say you’ve got five years of C# experience and you can’t write a FizzBuzz program, you’re lying. The two candidates we’ve looked at so far both have an MS in Comp Sci, so they’re both better-educated than I am, at least, and they should both be able to handle FizzBuzz.

Anyway, it occurred to me that I never wrote a FizzBuzz program in X++. So here’s a short job to solve FizzBuzz in X++. I might post it to RosettaCode, if I get around to it. Not that the world really needs one more FizzBuzz solution.

static void AjhFizzBuzz(Args _args)
{
    /* Write a program that prints the numbers from 1 to 100. 
    If it’s a multiple of 3, it should print “Fizz”. 
    If it’s a multiple of 5, it should print “Buzz”. 
    If it’s a multiple of 3 and 5, it should print “Fizz Buzz”. 
    */
    int i;
    
    for (i = 1; i <= 100; i++)
    {
        if (i mod 3 == 0 && i mod 5 == 0)
            info("Fizz Buzz");
        else if (i mod 3 == 0)
            info("Fizz");
        else if (i mod 5 == 0)
            info("Buzz");
        else
            info(int2str(i));
    }
}

Adding an exception logger to a Web API project with Autofac and Serilog

I just spent way too much time figuring out how to add a catch-all logger for exceptions to an ASP.NET Web API project, so I figured I’d write up my experience as a blog post, for anyone else who needs it (and for my own future reference).

The goal, specifically, is to log any unhandled exceptions using Serilog. I don’t want to mess with them in any way, I just want to record them in the log. (For this API, most exceptions are already properly handled, but sometimes something falls through the cracks, so I just want to be able to see when that happens, so I can fix it.)

First, this is an old-fashioned ASP.NET Web API project, not a .NET Core project. I’m using Autofac for dependency injection and Serilog for logging.

And I’m using the Autofac.WebAPI2 package to integrate Autofac into the API. My Autofac configuration looks pretty much just like the example in the “Quick Start” section of the page linked above.

Serilog is linked in like this:

builder.Register((c, p) =>
{
    var fileSpec = AppDomain.CurrentDomain.GetData("DataDirectory").ToString() + "\\log\\log-{Date}.log";
    var outpTemplate = "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Properties:j} {Message:lj}{NewLine}{Exception}";
    return new LoggerConfiguration()
        .WriteTo.RollingFile(fileSpec, outputTemplate: outpTemplate)
        .ReadFrom.AppSettings()
        .CreateLogger();
}).SingleInstance();

I won’t get into how that works, but you could figure it out from the Serilog docs easily enough.

ASP.NET Web API provides a way to hook into unhandled exceptions using an ExceptionLogger class. This is described a bit here. I found several blog posts describing various permutations on this functionality, but I had to mess around a bit to get it all to work right for me.
I created a class that looks like this:

public class MyExcLogger : ExceptionLogger
{
    public override void Log(ExceptionLoggerContext context)
    {
        var config = GlobalConfiguration.Configuration;
        var logger = (ILogger)config.DependencyResolver.GetService(typeof(ILogger));
        if (logger != null)
            logger.Error("Unhandled exception: {exc}", context.Exception);
    }
}

and I hooked it up to Web API by adding this line to my WebApiConfig Register() method:

config.Services.Add(typeof(IExceptionLogger), new MyExcLogger());

There’s not actually much to it, but I went down the wrong path on this thing several times, trying to get it to work. The (slightly) tricky part was getting the logger instance from the dependency resolver. Constructor injection doesn’t work here, so I had to pull it out of the resolver manually, which I’d never actually tried before.

sorting photos with Python

As part of my MacBook replacement project, I wanted to get a good new backup of my photo library. Photos.app stores photos in a database, not individual files, so if you want a “plain old JPEG” backup of your library, you need to export the photos from Photos.app. There are various ways to do this, but the way I chose left me with a folder full of subfolders organized by “moment name,” so a typical subfolder would be named “Somerville, NJ, May 27, 2018” for instance. That’s kind of a mess, since the folder names sort alphabetically. (And the folder dates are all set to today. Setting the folder dates to the date the photos were taken would solve some problems, but let’s ignore that for now.)

To impose some rudimentary organization on this, I wanted to make a top-level folder for each year, and move all the subfolders for that year into that folder. Not perfect, but at least it gets me from having one folder with 2000 subfolders to 10 folders with 200 subfolders each (approximately), which is a bit more manageable.

I looked around at various ways to do this, and settled on writing a quick Python script, which is shown below. I haven’t written a Python program in years, so this probably isn’t elegant Python. But it works. I’m posting it here in case it’s useful for anyone else. (I first learned Python back in 2003, and have used it on and off since, but never regularly.)

I had also looked for third-party utility programs that would allow me to export from Photos with more options than Photos itself allows. I found an old MacStories article from 2012 that talked about exporting from iPhoto to Dropbox using a tool called Phoshare. I had hoped that maybe Phoshare had been kept up to date, and worked with Photos, but nope. It was last updated in 2012.

I feel like there’s probably some interesting stuff I could do with Hazel too, and/or ExifTool, but I didn’t want to over-complicate things. And I also feel like there’s probably a third-party app out there that would make this much easier, but I didn’t find one that did exactly what I was looking for.

# sort_photo_bu.py
# ajh 2018-05-27
# sort Photos app export into folders by year.
# Export from Photos with subfolder format "moment name". 
# We assume the moment name always ends with the year.
# (Note that this may throw a "destination path already exists" error at some point, but that's probably fine.)

import os, shutil, sys

bupath = "/Users/andrew/Pictures/backup20180527"
for (root, dirs, files) in os.walk(bupath):
    for dirname in dirs:
        if len(dirname) &gt; 4:
            year = dirname[-4:]
            yearpath = bupath + "/" + year
            if not os.path.isdir(yearpath):
                print "Making folder for " + year + "..."
                os.mkdir(yearpath)
            origpath = bupath + "/" + dirname
            #print "moving %s to %s..." % (origpath, yearpath)
            shutil.move(origpath, yearpath)
            #sys.exit()
print "All done."

Learning Statistics

I’m starting to read a book on statistics, and this is from the beginning of the chapter on probability:

If you are reading this book as part of a course in statistics, then you are likely pursuing a degree in higher education. On the other hand, if you are reading this book simply out of enjoyment, then you are crazy. That is one thing we can be certain about.

I guess I’m crazy. Oh well. It’s a pretty good book so far, though I think I’m going to need more to really get going.

I started thinking about learning more about statistics and data analysis recently. It ties in a bit with my attempt to learn how to use Power BI. I have all the basics of Power BI down now, meaning that I can import data and make fancy-looking pie charts and bar graphs, and now I’m poking at the edges of more meaningful data analysis.

It’s kind of hard to figure out where to start with statistics and data analysis. I never took a course in statistics when I was in college, so I don’t really know much to begin with. I did read Larry Gonick’s Cartoon Guide to Statistics years ago, back in the 90s I think. I don’t really remember much about it; maybe I should reread it now.

The book I’m currently reading is meant as a college textbook (per the quote above), and isn’t really meant to stand alone. It purposely doesn’t talk about software tools at all; just the background concepts and a little math. I’m thinking about reading this book along with it, which includes some more practical stuff, using Excel as the tool of choice. (Both books are on Safari, so I can read them for free.)

Of course, as a programmer, I’d eventually like to get to a book that talks about statistics and uses a real programming language for the examples. So maybe Think Stats would be good; it uses Python, which I’ve used before (though I’m probably a bit rusty).

I see a lot of references to R when looking into data analysis and statistics. I know almost nothing about R, so maybe I should look at something like this book.

This is all sort of leading me into data science, which is apparently the sexiest job of the 21st century, according to Harvard Business Review. I’m not really looking for a new job, and definitely not a “sexy” one, but hey, it can’t hurt to learn a bit.

Learning Azure

I have a project going at work that might (or might not) eventually have something to do with Azure. So I’m using this as an excuse to finally learn a bit about it. I looked at a few ebooks and videos that weren’t that helpful, then I stumbled upon a free ebook from Microsoft titled Fundamentals of Azure. It was last revised in 2016, so it’s a little out of date, but still mostly relevant. It does a good job of walking you through all (well, most) of the services offered on Azure, without getting too deep. (But it does get deep enough to actually be useful to a working admin or programmer.) I wrote a short review of it on Goodreads.

I’m using the $50 monthly Azure credit that comes with my Visual Studio subscription, which is more than enough to play around with (assuming that you remember to delete everything you do after you’re done). I’m glad that my company (finally) decided to pay for VS subscriptions for us. There are a few nice benefits that come with that.

If I really needed to get serious about Azure, I’d look at the Microsoft Professional Program in Cloud Admin at EdX or something like that. (Of course, I’m more of a developer than an admin, myself, so that wouldn’t be right for me either way.)

I’d love to be able to do a big project that uses a mix of Azure services, but that’s probably not in the cards. Still, it’s good to be familiar with all this fancy cloud stuff!

How to use a Stopwatch in C# incorrectly

Here’s something I did wrong this week. And, after realizing I’d done it wrong, I remembered that I’d made the same exact mistake in the past. So I’m going to write up a blog post in the hopes that maybe it’ll help me hammer the right way into my head, so I don’t screw up next time.

If you want to do a little quick and dirty performance logging in an application, you can use the Stopwatch class. Just create a new Stopwatch, then stop it when you’re done and check the elapsed time. Easy, right?

Well, here’s what I did wrong: The Elapsed property is a TimeSpan structure, which has a lot of nice properties on it, like Days, Hours, Milliseconds, and so on. For my purposes, I wanted to know how many milliseconds had elapsed. So I showed sw.Elapsed.Milliseconds. Looks good, right? Except that the Milliseconds property is not the whole span in milliseconds. It’s the milliseconds part of the span. If you want total milliseconds, then you need to get sw.Elapsed.TotalMilliseconds.

This is one of those pernicious little bugs that doesn’t cause anything to fail, but instead causes the programmer to make bad decisions. “Hey, it looks like all my web service calls execute in less than a second! Great! I can go home early!” Nope. Turns out some of them were taking more like 20 seconds.

Here’s a little code snippet showing the wrong way and the right way.

Stopwatch sw = Stopwatch.StartNew();
TimeSpan elapsed;
Thread.Sleep(1500);
sw.Stop();
elapsed = sw.Elapsed;
Console.WriteLine("Wrong: {0}", elapsed.Milliseconds);
Console.WriteLine("Right: {0}", elapsed.TotalMilliseconds);

Simple enough, but I’ve done this wrong at least twice so far in my life. Here’s hoping I can remember to do it right next time!

Five year work anniversary

I hit my five-year anniversary at SHI this week. I don’t really have much to say about that, but I thought I should mark it with a quick blog post. I mentioned SHI in my New Year’s Day post, so that covered my current status pretty well. I first mentioned the job on my blog in March 2013, after I’d been there for a couple of months. My current projects are a mix of straight Dynamics AX work in X++, some .NET stuff, using C#, and some research into Power BI. So it’s a pretty good mix. I think I might see some opportunities to do stuff with Power BI in the cloud and maybe some Azure stuff this year. So that could be fun.

We recently (finally) got current Visual Studio subscriptions at work, so I now have access to VS 2017 Pro and the other random fun stuff that comes with a VS subscription. (Previously, we had some kind of standalone licenses to VS 2012 and 2013.)

I’m always a little worried about stagnating and turning into “that guy” who has been doing COBOL programming for years and hasn’t learned anything new since the Nixon administration. But SHI seems to be giving me enough opportunities to work on new and interesting stuff, and I’m still trying to keep current independently, via services like Pluralsight and Safari, and podcasts like .NET Rocks and Hanselminutes. So I guess I’m still on the right career track.

Learning Power BI

I’ve recently started trying to learn about Power BI, since it looks like we’ll be using it at my job soon. We first started talking about Power BI in 2016; at that time, I looked into it a bit, decided it probably wasn’t something that was going to work out for us, and didn’t really follow up on it. And my boss didn’t follow up with me on it, so I figured it was dead or on the back-burner. I guess it was the latter, since it’s come up again. This time, it sounds like maybe we’re a bit more serious about it than last time, so I’ve been spending a lot more time trying to figure it out than I did back in 2016.

I’ve never really done much BI work, though I’ve done plenty of work around the edges of BI, and have dipped my toes into more serious BI from time to time. I’ve done a lot of ETL work, and that seems to be a big aspect of BI.

To get myself up to speed, I first took a look at what was available on Pluralsight. I started with a course called Getting Started with Power BI, which was a pretty good intro course that just zoomed through a lot of stuff quickly. Then, I watched a course called Introduction to Data Warehousing and Business Intelligence, which helped me get some background info on the current state of BI and data warehousing. (I was already somewhat familiar with the general idea of cubes, but didn’t really know much about them.)

After that, I decided to dig more deeply into the Power BI Desktop tool. This is a free tool from Microsoft that’s actually pretty good, and could be useful as a standalone tool, even if you’re not plugged into the whole Power BI cloud thing. I’m currently reading a book titled Pro Power BI Desktop. It’s covering the product in a lot of detail. (Maybe too much detail. It feels a little like this book doesn’t really need to be 761 pages long. But it’s a pretty good book, overall.)

There’s also a free course on EdX about Power BI. I might give that a try, if I feel like I need to. The course is part of a Microsoft Professional Program in Data Science that’s all available on EdX, and which looks pretty interesting. I’d love to do it all, if I had a lot more spare time than I currently have, and if I wouldn’t miss the $990 it would cost to officially enroll in it and get the certificate from it. The path I’m on now is more about simple BI rather than fancy data science, but I’m really curious about that stuff. I was listening to an episode of Hanselminutes this morning on machine learning and data science, and it was really interesting. I wish I had the time to figure it all out.

Back on the subject of Power BI itself, I was leery about it back in 2016. Microsoft sometimes introduces products like this that don’t last long, or that are overly complicated or expensive, given what they do. Power BI looks like it might actually be a winner though. The Desktop tool is quite versatile and useful even without the cloud service. I’m still trying to figure out whether or not it’s worth buying into the whole ecosystem though. You can do a lot with it for free, but the cost could get pretty high if we start using it for a lot of enterprise-level stuff.

Twelve Days of .NET: Day 13: LINQPad

This post is part of my 12 Days of .NET series. This is a (not terribly ambitious) series of posts on .NET topics that came up while I was working on a recent C# Web API project.

I decided to add one more post to this series, so now it’s a baker’s dozen, I guess.

I went through a little exercise about a year ago, looking at various C# REPL options. I played around with a few options, but never really settled on one as being really useful and, for me, any better than just writing a quick console app.

Somehow, in all of that, I’d completely forgotten about LINQPad. I’d first looked at it all the way back in 2007, and was using it pretty regularly for a while. I stopped using it at some point and forgot about it. I probably stopped using it because I’d switched to Snippet Compiler. Snippet Compiler is pretty much dead now, I think. If I’d been paying attention, I would have switched back to LINQPad years ago, like this guy did.

I saw a reference to LINQPad recently, in a reddit post, and took a look at the current web site. It seems to have matured into a really great, full-featured, product. There are both free and paid versions. The free version is probably good enough for me, but the pro version is only $45 and adds some nice features.

Twelve Days of .NET: Day 12: Getting Started

This post is part of my 12 Days of .NET series. This is a (not terribly ambitious) series of posts on .NET topics that came up while I was working on a recent C# Web API project.

OK, so the last day is a weird place for a “getting started” post, but I’m just about out of other stuff, and I remembered a good article on this. The .NET ecosystem is getting pretty complicated, with Visual Studio Pro vs Visual Studio Code and .NET Framework vs .NET Core and a bunch of other decision points along the way. Here’s a good blog post with a flowchart for figuring out which direction you should go in, if you’re starting a new project and you’re kind of clueless.