I'm just sayin.

by 30. April 2010 20:25

VGluZm9pbCBpcyB5b3VyIGZyaWVuZC4=

[edit for Gheri del la Cirque de Soleil....]

Tags:

Parsing Obsession

by 22. April 2010 07:40

This past weekend I had an interview in Austin, TX with a pretty badass company.
The architects hit me with a question that made me cock my head to the side like
a cocker-spaniel that just heard a violin. The question wasn't brain surgery, but
it was complex enough that it required a lot of though and I got a little obsessed.
I had to code it today.

Here was the question (not verbatim, but close):

Write a method that returns a list of strings from a line from a comma delimited
file. Here's the data:

Bill,Brown,"austin, tx", 123, """Jr."""

Output should be:
Bill
Brown
austin, tx
123
"Jr."

I started to pseudocode it using some regex, and they didn't seem to thin that would
work. I went a couple more routes. In the end we discussed and the one architect
said he had solved this by rolling through each character in the line. That seemed
like a perfectly reasonable way, but I was really hung up on a couple of things.

1.) I think that sounds like a lot of CPU cycles for this operation
2.) I wanted to do this without rolling through each character, because that's how I
    roll.


So today my OCD got the best of me... and here's what fell out:

1: using System;
2:     using System.Collections.Generic;
3:     using System.Linq;
4:     using System.Text;
5:     using System.Text.RegularExpressions;
6:     using System.Diagnostics;
7:    
8:     namespace TestLib
9:     {
10:        public class ParserThing
11:         {
12:            public ParserThing()
13:            {
14:                DateTime str = DateTime.Now;
15:                //string dirtyLine = @"Bob,Brown,""""Jr."""",""dayton,oh,N"",123-45-6789";
16:                string dirtyLine = @"Bill,Black,""""""Sr."""",,\/some/thing\/,"""",,""123 Street Ave.,West. SomeCity,OH 45454"",""000000-0000""";
17:                int executions = 100;
18:                List<string> words = new List<string>();
19:                for (int i = 1; i <= executions; i++)
20:                {
21:                    words = CleanIt(dirtyLine);
22:                }
23:                TimeSpan ts = DateTime.Now - str;
24:                Debug.WriteLine("Execution Time: " + Math.Floor(ts.TotalMilliseconds).ToString());
25:                foreach (string word in words)
26:                {
27:                    Debug.WriteLine("Word: " + word);
28:                }            }
29:            public static List<string> CleanIt(string rawLine)
30:            {
31:                const string QUO = @"""";
32:                const string DELIMITER = ",";
33:                List<string> retVal = new List<string>();
34:                List<string> tmpWords = new List<string>();
35:                Regex regx = new Regex(@"""{2,}");
36:                string[] words = rawLine.Split(new string[] { DELIMITER },StringSplitOptions.None);
37:                bool isInQuoteBlock  = false;
38:                StringBuilder finalWord = new StringBuilder();
39:                string tmpWord = string.Empty;
40:                string word = string.Empty;
41:                for (int i = 0, l = words.Length - 1; i <= l; i++)
42:                {
43:                    word = words[i];
44:                    tmpWord = words[i];
45:                    if (word.Contains(QUO))
46:                    {
47:                        if (regx.IsMatch(word))
48:                        {
49:                        tmpWord = regx.Replace(word, @"""");
50:                        }
51:                        else
52:                        {
53:                        tmpWord = word.Replace(@"""", "");
54:                        }
55:                        finalWord.Append(tmpWord);
56:                        if (word.StartsWith(@"""") && !word.EndsWith(@""""))
57:                        {
58:                            // this is a partial word
59:                            finalWord.Append(DELIMITER);
60:                            isInQuoteBlock = true;
61:                        }
62:                        else if (word.EndsWith(@"""") || i == l)
63:                        {
64:                            // this is the end of a block
65:                            isInQuoteBlock = false;
66:                            retVal.Add(finalWord.ToString());
67:                            finalWord.Length = 0; // clear sb
68:                        }
69:                    }
70:                    else if (isInQuoteBlock)
71:                    {
72:                        finalWord.Append(word + DELIMITER);
73:                    }
74:                    else
75:                    {
76:                        retVal.Add(word);
77:                    }
78:                }
79:                return retVal;
80:                }
81:            }
82:        }
Important to note: I wrote this in a pretty short amount of time. There's probably some shortcuts I could do in here, I don't like the if/else if/else use in this. I'm not going to spend any more time on this, but damnit it works, and it works well, and it's reasonably fast. On my box, I can parse 100k iteration in about 1200ms. ... I didn't get the job, btw, but that's ok, it wasn't the right time. I got to see Austin, and I got to interview at a really awesome place. I'm hating blogengine, btw, I need to change this blog to something else.

Tags: , ,

.NET | Development | How To

Parallels vs. VMWare Fusion

by 28. February 2010 21:41

Maybe this is an unfair comparison considering I am using VMWare 2 and Parallels 5. I have been a pretty firm VMWare fanboy for quite some time, but I have to say after using parallels 5 for a week now, I am frickin' SOLD. I have read that Parallels 5 is supposed to be faster, and it is, but WOW, it's a LOT faster. The biggest thing, though is the fact that it properly focuses windows of the guest OS when in Unity/Coherence mode. I am on an older MacBook Pro, it's a 2.16GHz Core Duo, with 2GB of RAM and a 7200 RPM hard drive. As I type this I am running Safari on OSX and Visual Studio 2008, SQL Server 2005 Mgmt Studio, and IE 8. There is... 5% CPU usage and memory consumption is hovering around 70% (and I have 768M allocated to the guest OS, which is XP SP3).

I'm seriously impressed. I think I see a competitive upgrade in my future :) And just for Gheri, yes  I installed VirtualBox, it's ok. It's better than nothing, it's a little lower performance than VMWare.

Tags: , ,

Apple

.NET Triplet and Pair classes… and a lesson in lousy namespacing.

by 20. February 2010 20:16

 

This week I was working on a project and came across a class I’d never used in C# called a Triplet. You can look up the Triplet on MSDN here.

The Triplet is just a class with three properties: First, Second, and Third. Each of the properties holds an instance of an object. That’s it. That’s all this thing does. The very first thing I noticed once I saw what it did was the namespace it’s in. You’d think it would be in System.Collections, or System.Collections.Specialized or something, but no… it’s it System.Web.UI along with things like the System.Web.UI.Control class. WTFBBQ?

I would love to hear a rational explanation for this. I am pretty anal about namespacing and I don’t like having to stretch the tasteful bounds of a namespace to put something in. I will on occasion, but this is a bit much.

I downloaded Reflector to see if I could find any other gems stored in that namespace. Lo and behold… I find Pair. Care to guess what Pair is? Exactly.. it’s the same as Triplet… but with…. TWO properties.

Up on obsessing over this I started to think about why these classes even exist at all.  What is wrong with object[] objArray = new object[2]; ? Is that so complicated that there needs to be a type, and a horribly misplaced type at that?

In lieu of my findings I have created the following class I want added to the .NET Framework …. The Duodecuple.

1: using System;
2: //Stay consistent with completely nonsensical namespacing namespace System.Workflow.Runtime.DebugEngine
3: {
4:      // that's "12" for those of you who don't feel like going to wikipedia
5:      public class Duodecuple
6:      {
7:               public object First { get; set; }
8:               public object Second { get; set; }
9:               public object Third { get; set; }
10:               public object Fourth { get; set; }
11:               public object Fifth { get; set; }
12:               public object Sixth { get; set; }
13:               public object Seventh { get; set; }
14:               public object Eighth { get; set; }
15:               public object Ninth { get; set; }
16:               public object Tenth { get; set; }
17:               public object Eleventh { get; set; }
18:               public object Twelfth { get; set; }
19:               ///
20:               /// Constructor. Since we're taling C# 3.5+ here you can just
21:               /// instantiate with ... new Duodecuple { First = whatever, Second = anotherObject };
22:               /// No sense in having all those constructors
23:               ///
24:               public Duodecuple (){}
25:      } }

you're welcome.

-Mike

 

Tags: , , ,

.NET | Complaining | Development

ASP.NET UserControls that are AWESOME: Using Events, properties, and methods.

by 28. September 2009 07:21

UserControls are underused
I am always shocked to see how little UserControls are used by some ASP.NET developers. To me, the UserControl is where a bulk of the real power in ASP.NET is. It took me a long time to get really used to using them, coming from a Classic ASP background, the loss of Server Side Includes was not easy to get used to. Thankfully, UserControls gave me back a commanding portion of the functionality I needed.

A Better Model / View / Controller
It’s really difficult to implement an MVC pattern in ASP.NET that I can look at and be proud of. Most of the implementations I have seen are very restrictive frameworks that require a lot of adherence to naming conventions, use of ISAPI filters, XML documents that have to be manually maintained as db or class structure changes, problems with Medium / Low Trust environments, and big performance losses.

ASP.NET, I think, had MVC in mind when they developed the paradigm in which they expect pages to operate, but they sure put a boatload of tools in to derail that. Your .aspx page is your view, unfortunately, they tightly coupled a controller (aspx.cs, code-behind) to every view, to the point they are almost synonymous. Then the model, ugh.. you SHOULD be writing a model and a controller to be changing the views, but with WONDERFUL tools like the SqlDataSource, and XmlDataSource they promote bypassing the use of a model or data-layer, and promote addition of business logic in the code-behind. I’m not going to go on about this...

While what you see in my example is certainly not an MVC solution, but it will get you closer to a good and flexible pattern that you can rely on.

UserControls vs. ServerControl

UserControls are not ServerControls and vice-versa. A UserControl is a .ascx(.cs) file and for all shapes and purposes functions very similar to a .aspx(.cs) page. It has a similar life-cycle, and similar properties. The difference is that a ServerControl is typically a small, contained, piece of functionality ... think of a TextBox, or a Button control. ServerControls are usually an element made for a narrowed purpose. A UserControl on the other hand is typically a collection of controls for a specific business purpose. In short, if you extended a TextBox to use specifically for a first name you could put built-in usage and length rules, etc. Great example of a ServerControl. Comparatively, if you wanted a reusable login form for your website, that is something that you would want as a UserControl. UserControls are typically specific to the project or solution, and are very flexible. This is what we will focus on.

Explaining The Code
This would be a good time to download the .ZIP file and get into the code:

The web-app is a basic phonebook, there is no database, all of the entries are stored in a hashtable in Session.

Default.aspx - This is the main view. The UserControls are registered at the top of the .aspx file. In the CodeBehind you will see one method, and that is the EventHandler for one of the UserControls.

/controls - Folder for UserControls

/controls/ViewDataControl.ascx - This control is used for just SHOWING the data, really just contains a gridview. The CodeBehind contains several methods, the public “Refresh” method is the important one because that is exposed in order for the data in the control to be reloaded.

/controls/AddControl.ascx - This is the big one. This UserControl is the form where a person will add a new name to the phonebook. There are a few very important key points to note:
This control contains two properties. They allow abstracted access to the TextBox’s .Text properties. This allows the .ASPX page to read values from the form directly whenever it needs to.
This control contains a public DELEGATE and a public EVENT. This event is fired when the “add” button is pressed. This is how we are able to notify the .ASPX page that something has happened

/App_Code/PhoneBookDataHandler.cs - This class is what actually interacts with the Session data (the hashtable). We break logic like this out from the .ascx.cs and .aspx.cs  files because these functions are universal to the application. the aspx.cs and ascs.cs files SHOULD NOT (not just in this app, either) contain business logic. Those files are there to control the rendering of the page. I said above that I believe these files were also intended to be “controllers” for some perversion of an MVC pattern.

Back to the Default.aspx(.cs) files. - You can probably now see how the Default.aspx page is just the glue that holds the whole thing together. It’s nothing more than a conductor for the UserControls. The UserControls are put on the .aspx page, we then tell the .aspx page that when AddControl.ascx’s “OnEntryAdded” event fires, run the .Refresh() method on ViewDataControl.ascx. ViewDataControl.ascx then grabs the data from Session and puts it in the GridView.

WTF is Business Logic?
Many applications are written for a certain purpose, industry, or business. For this reason the “entities” (like a customer number) adhere universally to certain rules, and those rules have to be validated throughout the application. You do not want that type of logic repeated every time you have to do that, you just want to call a method that does it for you.

You will need to view the code to understand this, most likely. I recommend viewing in this order:

  • Default.aspx
    • Look at the UserControls being registered at the top of the page
    • Look at Line 16 where we tell the OnEntryAdded event to be handled by the method ucAddControl_EntryAdded
  • Default.aspx.cs
    • Look at the ucAddControl_EntryAdded method. All it does is run the .Refresh() method on the ViewData UserControl
  • ViewDataControl.ascx.cs
    • Notice the methods to get data simply ask the PhoneBookDataHandler class for the data.
    • View PhoneBookDataHandler.cs if you want to see the interaction with Session and the HashTable, but it’s not critical to understanding the UserControls.
  • AddControl.ascx
    • Just take a look at what controls are on the page, the text-boxes and buttons. The RequiredFieldsValidators are irrelevant to the tutorial.
  • AddControl.ascx.cs
    • This is the most important file of the entire tutorial.
    • Lines 19 & 20
      • Public properties that allow read-only access to the TextBox’s .Text property
    • Line 26
      • The delegate that defines the signature of the event
    • Line 32
      • The Event that broadcasts when an entry is added
    • Line 43
      • VERY IMPORTANT: This method is what actually fires the event.
    • Line 56
      • Just the button-click event handler. Calls methods that add the data, then fires the event.


I hope this helps you write awesome apps!

-Mike

AwesomeUserControls.zip (7.31 kb)

Tags: , , , , , ,

.NET | Development | How To

SweetLIB is LIVE!

by 19. September 2009 21:27

I've finally uploaded SweetLIB.

You can go view the project here (http://sweetlib.codeplex.com/).

SweetLIB is a library I've had for probably 3 years but never really had the balls to publish. I've done a lot to it.

The Major Features:

  • ADO Wrapper and Factory (helps make apps db agnostic)
  • Helpers for
    • Regex validation of data (zip codes, credit cards, email addys, etc)
    • Finding controls in control trees
    • Finding nodes in Tree controls
  • Object Validation classes
  • Web page error handling with base classes

If you use it, let me know what you think. There is an included web project that serves as documentation right now, but it's not all that great.

Tags: , , ,

Development

Get the tourniquet and the needle... it's time to level up...

by 12. May 2009 08:32

I've never been a big fan of role playing games. I found them to be slow going and kind of drab because there was little or no live action. 

About a year and a half ago some friends got me into Call of Duty 4. Although it's a first person shooter, it has an RPG element to it that... to say the least... became somewhat of an ... unhealthy obsession. See, when you play online you continuously level up, each level unlocks (there's a big thing.. the unlocking) new weapons, perks, etc. It got to be really out of hand. I put well over 100 hours in online, and I suck so bad at it.

Well.. as the drive to play COD wore off, Nicki and I began to get into Facebook. Nicki started playing Pet Society. I thought it looked kinda goofy, really cute and adorable... but nothing I thought I'd be int... woa woa woa... wait .... you level up? ... COUNT ME IN! I created my pet, and it was SO on. There's not a lot to do.. but DAMNIT.. I was GOING to get to the next level... *sigh*

So I'm at work talking to Amy and I mention Pet Society ... she says "Do you play FarmTown?" *sigh* here we go again. I hate FarmTown. The game is terrible. Every time I play it it's like burning myself with a hot poker. I despise every minute I am in it... but damn dude .... I've gotta level up!

Then the big one happens.... Tony and Paul introduce me to Mobsters on the iPhone.... but wait... it's $3! NO WAY.... but no... storm8 has VAMPIRES LIVE... it's pure RPG.. barely any pictures at all... but like.. you play against other people! You level up.... LEVEL. UP! .. and it's in my pocket .. and with this beauty of 3g, I can play anywhere, meetings, family get togethers, funerals. It's so fun. I cannot put it down.

I don't know what it is ... leveling up is like some kind of horribly pathetic personal accomplishment. I feel like I've completed something. There is some satisfaction knowing that I have achieved enough to graduate to the next level of responsibility that is .... THE NEXT LEVEL!

So pathetic. I love it.

 

I gotta go. I have to go level up.

Tags:

Video Games

MacBook Pro Battery

by 10. March 2009 05:31

After 2 1/2 years and something like 500+ cycles, the battery in my Mac died and I had to replace it. First thing first, why the hell are these so expensive (and yes I realize this is a recurring theme in my blog)? I decided not to go to the Apple Store and buy an Apple brand battery because the reviews online are awful, so I decided to go to Batteries Plus and get an Energy+ battery. Turns out... it's an apple battery, granted it's a 5600mAh battery and all.

 So.. we'll see this battery isn't part of the old recall, so that is good, and the gy gave me 10% off so that was nice. lets see how this works.

 Here are a few tips that the battery-guy gave me for long battery life.

 

  1. Only use the battery when you have to. If you use your notebook as a desktop pc, remove the battery and throw the thing on AC.
  2. Only charge the battery when it needs to be charged, unplug the power when the charge is complete.
  3. Batteries are good for 300-500 cycles (cycles = times you charge it)
Good times, I hope this batter lasts until I replace this MacBook Pro.

 

Tags: ,

Apple | Cheapass

Using Time Machine with a Buffalo LinkStation (CH/CL Series)

by 3. January 2009 09:48
I’m not going to get into the details, but last week we had a bit of a data catastrophe in the Jerzakie household.

The potential data loss finally influenced me to buy a NAS for the house. I’ve been running on a crusty old Windows 2000 Server box now for probably 7 years, 40 gigs just isn’t cutting it anymore.

After a short time shopping, and a good recommendation from Andy, I bought a 1Tb Buffalo LinkStation EZ. It says that it works with Time Machine, and the iPhone, and iTunes and all that jazz.

Tonight I got it and got it all set up. The instructions from Buffalo are … to say the least.. lacking … severely. Thanks to another tutorial and my own lack of following directions, I had the NAS working with Time Machine in just a short while.. Below are my awesome instructions.

Things You Will Need (and need to do):
  1. A Buffalo LinkStation that natively supports Time Machine (CL/CH series)
  2. A Mac (or two) with OSX 10.5.6 or higher
  3. A lot of time for your computer to sit.
  4. SHUT TIME MACHINE OFF ON YOUR MAC
  5. Delete the share you created on your NAS 5 hours ago that you haven’t gotten to successfully work yet.
  6. Go into the Time Machine tab in the NAS and disable it.

Creating your image file

Because you aren’t using an actual Apple Time Capsule you will have to do a couple of things yourself. Not hard..
You need to create a disk image to backup to. Weird, I know, but it’s how Time Machine works.

Open Terminal on the Mac (go to Spotlight and type Terminal… there it is). Type the following line

ifconfig | grep ether

You will get 1-2 lines, the first or only line is the MAC address of the WIRED Ethernet port. Regardless of which you will use to backup (wired or wireless) Time Machine needs the MAC of the WIRED port.

Copy that to TextEdit or something.. or write it down.

Now, go into System Preferences > Sharing
In the box at the top you see your computer’s name, below it is the name you will need to use for TM (Time Machine). If it’s complicated and stupid, click EDIT and change it to something easier. I used “MikesMBP” it will automatically add .local on it, that’s fine, let it be.

Now.. here’s where we create the image (sparsebundle).
Open your new friend Terminal again. Type the following…

hdiutil create -size [SIZE IN GIGS]g -fs HFS+J -volname "[VOLUME NAME]" /[COMPUTER NAME]_[MAC ADDRESS WITH NO COLONS].sparsebundle

WTF?!

Ok…
  • SIZE IN GIGS : You can set a max size for the bundle, I used 200 that seemed like plenty.
  • VOLUME NAME : Anything you want, I used “Mikes Backup” very creative, I know.
  • COMPUTER NAME : This is the name from above (the one below the box that you may have edited) don’t add the .local so just “MikesMBP” in my case
  • MAC ADDRESS WITH NO COLONS : Ok, that crazy crap you copied to Text Edit, it’s that… when you remove the colons it should look like 00b443e684f2 or something… so.. the line above, when you are done should look something like this…

hdiutil create -size 200g -fs HFS+J -volname "Backup of Mikes Computer" /MikesMBP_0019e5582bd1.sparsebundle


After you hit enter it will sit for a minute then say that the file is created … TADA!

Setting up the NAS

Very little actually needs to be done on the NAS, thankfully.

First, make sure that your NAS will work with the Mac. Click on Shared Folders > Service Setup and make sure that Apple Talk is on.

Open the NAS admin, go to Shared Folders. Create a new folder called “TimeMachine” or something , it doesn’t matter. Make sure that the share is accessibly via Apple Talk. Save your changes… da da da.

Go back to your Mac. In Finder go to Network, browse to the NAS, you will see the folder you just created. Open it and copy in the sparsebundle file you created above. DELETE THE SPARSEBUNDLE FROM YOUR MAC WHEN IT’S DONE COPYING.

** NOTE ** I have two Macs, to use both in TM, just create a sparsebundle unique to each machine and upload them to the folder. Do this for BOTH machines before moving to the next step.

Back to the NAS web interface…

Click on the Time Machine tab in the NAS. Click the Enable option and select the folder you created a minute ago. Hit apply .. tada.. done with the NAS.

Wait about 2-3 minutes before moving on. This may be a good time to get something to drink.

Getting Time Machine Working


Ok… open Time Machine on the Mac, turn it on.. it will ask you where to backup to, it should find the TM share on the NAS by itself. Once it does, just let ‘er rip. It will be “preparing” for a long time, mine were 10-20 minutes (on a wireless connection).

The backups will start, and it’s on like Donkey Kong.
The backups on wireless are slow, but after the initial sync they are incremental and shouldn’t take so long.

Spend some time setting up the Options in Time Machine, exclude stuff you don’t need, it’s a waste of space. If you’re smart and you keep everything dear to you in you /Users/Your Name folder then you will have an easy time recovering when your piece of crap Hitachi hard drive decides to grind the heads into the platter. Good times.


Hope that helped everyone!

Tags: , , ,

Apple

iPhone Development - Very Discouraging.

by 15. December 2008 06:24
I started an iPhone app about 3 weeks ago in the hopes to learn Objective C, keep myself somewhat involved in the actual act of programming, and make a little extra scratch to pay for the monthly cost of this thing.

I came up with this sweet idea. I decided I would do a calculator, but not just a regular calculator. Mine would have the ability to save the calculated result with a note, AS WELL as the ability to save to "formula" (and I use that term loosely) with a single variable in it.

So I learned Obj-C well enough to write the basic calculator. Lemme tell you, the workflow of a calculator isn't as simple as you'd think. Moving on...

So I got the basic calculator working and it was nice. Mine had a few options that the basic iPhone calc didn't, such as % and √. I figured I could sell that basic calc for maybe $1 or just plop it out there for free. Then I am reading reviews for another calc by the same guy who wrote I Am Rich, and a user mentions turning the iPhone calc sideways and it's a scientific.... WTF?! So I did... lo and behold.. it's got like.. a hozillion options... pretty discouraging

*sigh*

So I take 3-4 days off before I finish the other options. Tonight I was going to get back on track and finish that thing before Christmas.

So tonight I find ThinkDigits.   It's pretty much what I was going to do... only a lot prettier. So.. now.. here I sit gazing over my other ideas ... 50% of which already exist.

Wow.. very discouraging. At least I am more smarterer now.

Tags: , ,

Apple | Development | iPhone


RecentComments

Comment RSS