A blog about SQL Server, SSIS, C# and whatever else I happen to be dealing with in my professional life.

Find ramblings

Wednesday, September 9, 2009

Hack and slash

I had assumed everyone created a proof of concept/scratch pad/hack about project but after a conversation in IRC today, I realized that wasn't the case. In Python and other interpreted languages, if I was working with a concept or library I was unfamiliar with, I'd invoke the interpreter and "play" with it until I figured out. In the .NET world, I create a LanguageOrTechnologyHackAndSlash project in my own private sandbox whereever I go. The project typically looks like a single class (Program/Module1) with lots of static methods. While you can create a single .NET solution and have seperate projects per language, I prefer to keep them in different solutions lest I completely bone something and can't get it to compile ever again. YMMV.

Structure
It's as simple as C:\sandbox\CSHackAndSlash, C:\sandbox\VBHackAndSlash, C:\sandbox\SSISHackAndSlash, C:\sandbox\FHackAndSlash, etc Within the startup project, my Main (or equivalent) method is littered with calls to breakpoints, method calls active and commented out. out

Why
The best reason I have for doing this is history. I lose post-it notes. Things written in the interpreter go away when you close it out. It also saves me from re-inventing the wheel on things I can't seem to keep straight in my head---

What goes in it
Everything. My C# one is littered with samples of how do I keep initialize an array in .NET, what's the preferred way of dealing with case-insensitive string equality, what's the regex method I like .Match, .Matches or .IsMatch? My VB one covers the snippets of code I use in SSIS script tasks or sanity checks that my code is doing what I expect, but the data is suspect.

What's missing
I've never compared my home version, it might be interesting to have them synched and/or checked into a version control system but that may be overkill.

Sample


namespace CSHackAndSlash
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

using WR.NetCommon.Util;

class Program
{
/// <summary>
/// When this boolean property is set to True,
/// the VerifyAddress method will always return
/// the preferred city name for the submitted
/// address, regardless of whether or not an approved
/// vanity name was sent to the method.
/// </summary>
private bool usePreferredCity;

/// <summary>
/// Gets or sets a value indictating whether we should use
/// a vanity name for a city. e.g. Westwood KS vs Mission KS
/// </summary>
public bool UsePreferredCity
{
get { return this.usePreferredCity; }
set { this.usePreferredCity = value; }
}

/// <summary>
/// Major challenge I see with enums is we lose leading zeros
/// </summary>
public enum Rank { RegionalVicePresident = 128, ManagingPrincipal = 234 };

static void Main(string[] args)
{
bool foo = false;
// foo = Program.stringMatches("I don't think of myself as a lion. You might as well though, I have a mighty roar", "i DON'T THINK OF MYSELF AS A LION. yOU MIGHT AS WELL THOUGH, i HAVE A MIGHTY ROAR");
Regex re = new Regex(@"\d");
Match m = null;
Match m2 = null;
m = re.Match("abc main");
m2 = re.Match("200 main");
re.Mat
int foo = -1;
foo = m.Length;
Program.TestProdWebService();
}

static void TestLoadWebService()
{
loadserver.AddressCleanerService service = null;
service = new CSHackAndSlash.loadserver.AddressCleanerService();
service.Timeout = 60 * 60 * 1000; // in milliseconds
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
loadserver.Entity result = null;
result = service.CleanAddress("1318 truman boolevard", "rolla mo 65400");

// try the big CASS
string fname = @"\\loadserver\AddressCleanerServiceData\BulkCleanerValidation.txt";
bool results;
try
{
results = service.CleanBulkAddresses(fname);
}
catch (Exception ex)
{
;
}
}

static void TestProdWebService()
{
prodserver.AddressCleanerService service = null;
service = new CSHackAndSlash.prodserver.AddressCleanerService();
service.Timeout = 60 * 60 * 1000; // in milliseconds
service.Credentials = System.Net.CredentialCache.DefaultCredentials;
prodserver.Entity result = null;
result = service.CleanAddress("1318 truman boolevard", "rolla mo 65400");

// try the big CASS
string fname = @"\\prodserver\AddressCleanerServiceData\BulkCleanerValidation.txt";
bool results;
try
{
results = service.CleanBulkAddresses(fname);
}
catch (Exception ex)
{
;
}
}

static void iMain(string[] args)
{
// Program.stringManip();
// Program.stringcomparengrams();
// Program.GenerateAddressDuplicateCode(null, null);

string value = string.Empty;
String objValue = null;
object valueObj = null;
Object objValueObj = null;
/*
unsafe
{
int* addrOfValue;
int* addrOfObjValue;
int* addrOfObjValue2;
int* addrOfValueObj;
int* addrOfObjValueObj;

value = "Hello world";
addrOfValue = &value;
objValue = new String(value.ToCharArray());
addrOfObjValue = &objValue;
objValue = value;
addrOfObjValue2 = &objValue;
valueObj = new object();
addrOfValueObj = &valueObj;
objValueObj = new Object();
addrOfObjValueObj = &objValueObj;

System.Console.WriteLine("The address stored in addrOfValue: {0}", addrOfValue->ToString());
System.Console.WriteLine("The address stored in addrOfObjValue: {0}", addrOfObjValue->ToString());
System.Console.WriteLine("The address stored in addrOfObjValue2: {0}", addrOfObjValue2->ToString());
System.Console.WriteLine("The address stored in addrOfValueObj: {0}", addrOfValueObj->ToString());
System.Console.WriteLine("The address stored in addrOfObjValueObj: {0}", addrOfObjValueObj->ToString());
}
*/

}
/// <summary>
/// Generate an address duplicate code which serves as a hash function
/// for standardized addresses.
/// </summary>
/// <param name="postalCode">5 digit postal code</param>
/// <param name="addressLine1">The first line of an address</param>
/// <returns>A hash of input parameters</returns>
public static string GenerateAddressDuplicateCode(string postalCode, string addressLine1)
{
string soundex = string.Empty;
StringBuilder digits = new StringBuilder(postalCode);
StringBuilder alphas = new StringBuilder();
List<char> digitList = new List<char>(new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' });
List<char> alphaList = new List<char>(new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' });

// hash = zip5 + line1.digits + Soundex(line1.alphas)
if (addressLine1 != null)
{
foreach (char c in addressLine1.ToCharArray())
{
if (digitList.Contains(c))
{
digits.Append(c);
}
else if (alphaList.Contains(c))
{
alphas.Append(c);
}
}
}

soundex = Matching.Soundex(alphas.ToString());

// Couldn't compute the soundex
if (soundex.StartsWith("00"))
{
soundex = string.Empty;
}

return string.Format("{0}{1}", digits.ToString(), soundex);
}

public static void stringcomparengrams()
{
string line1 = "quidditch A";
string line2 = "uqidditch a";

string actual1 = string.Empty;
string actual2 = string.Empty;
List<NGram> list1 = null;
List<NGram> list2 = null;
int matchCount = 0;

// at times like this, I really miss python's simple split(' ').join('') syntax
actual1 = String.Concat(line1.Split(new char[] { ' ', '\t', '\n' }));
actual2 = String.Concat(line2.Split(new char[] { ' ', '\t', '\n' }));

// Console.WriteLine(string.Format("Before: {0}\tAfter: {1}", string1, actual1));
list1 = new List<NGram>(Matching.GetNgrams(actual1, 3));
list2 = new List<NGram>(Matching.GetNgrams(actual2, 3));
List<NGram> matchList = null;
NGram element = list1[0];
list2.Find(delegate(NGram a) { return a.Equals(element); });

Matching.StringCompareNgrams(line1, line2);
Console.Read();
}

public static void stringManip()
{
string m_Recipient = string.Empty;
string _pagerAddress = string.Empty;
m_Recipient = "dbasDistlist@domain.tld;DotNet@domain.tld;PageWeb1@domain.tld";
_pagerAddress = "pageweb1@domain.tld";

if (m_Recipient.ToLower().Contains(_pagerAddress))
{
List<string> _list = null;
_list = new List<string>(m_Recipient.ToLower().Split(';'));
_list.Remove(_pagerAddress);
m_Recipient = string.Join(";", _list.ToArray());
}
Console.WriteLine(m_Recipient);
Console.Read();
}

public static bool stringMatches(string string1, string string2)
{
if ((string1 == null) && (string2 == null))
{
return true;
}

if ((string1 == null) (string2 == null))
{
return false;
}

// The most simple of a match, same string
if (string1.Equals(string2, StringComparison.OrdinalIgnoreCase))
{
return true;
}

// use advanced algs, double metaphone, ngrams, etc
;
}
}
}

No comments: