Thursday, August 27, 2009

Walk Of Shame.

So today as I am looking into why a program failed. I ran across a wonderful little bug that was completely my fault. The offending line of code is.

   76 this._AviableCabinets.RemoveAt(this._AviableCabinets.FindIndex(c => c.Name == LanguageDB.GetText("SelectDestLocalAllCabinetsText")));

The error it will throw is index out of bounds.

The solution is easy.

   76 var cLoc = this._AviableCabinets.FindIndex(c => c.Name == LanguageDB.GetText("SelectDestLocalAllCabinetsText"));
   77             if (cLoc > -1)
   78             {
   79                 this._AviableCabinets.RemoveAt(cLoc);
   80             }

Some Times it is better to not do it all on one line.

Friday, July 24, 2009

Getting the day of the week the first day of the month is.

A co-worker of mine is learning to program and decided to write a calendar program. He needed a way to find out what day of the week the first day of the month would be. In his explaining of how he was doing it we came up with the following code that would work for his needs.

   17 static void Main(string[] args)
   18 {
   20     string date = "1-1-09";
   21     string dofw = GetDayOfWeekFromString(date);
   23 }
   25 private static string GetDayOfWeekFromString(string date)
   26 {
   27     DateTime desiredDay = new DateTime();
   28     DateTime.TryParse(date, out desiredDay);
   29     return desiredDay.DayOfWeek.ToString();
   30 }

After I gave him that code I decided to just pack it into an extension function. Like so.

   36 public static string GetFirstDayOfMonthDayOfWeek(this DateTime dt)
   37 { 
   38     DateTime firstOfMonth = new DateTime();
   39     if (DateTime.TryParse(string.Format("{0}/1/{1}", dt.Month, dt.Year), out firstOfMonth))
   40     {
   41         return firstOfMonth.DayOfWeek.ToString();
   42     }
   43     return string.Empty;
   44 }

Which can be called like this.

   21 string dofw = DateTime.Now.GetFirstDayOfMonthDayOfWeek();

Hope someone else finds it of use.

Thursday, July 23, 2009

My python hours worked calc.

I use this horrid little piece of code to tell me how long i work each day. It isn't all that grate but it gets the job done.

ClockInHoure = 7
ClockInMinuet = 10
ClockOutHoure = 5
ClockOutMinuet = 0
lunchstartHoure = 11
lunchstartMinuet = 28
lunchEndHour = 12
lunchEndMInuets = 05

#caculate hours worked
mHours = 12 - ClockInHoure
aHours = ClockOutHoure
#get theminuets
mMinuets = mHours * 60 - ClockInMinuet
aMinuets = aHours * 60 + ClockOutMinuet

tMins = mMinuets + aMinuets

Lhours = lunchEndHour - lunchstartHoure
lmins = lunchstartMinuet - lunchEndMInuets

bMin = Lhours * 60 - lmins

tMins -= bMin

tHour = tMins / 60
tMinuets = tMins % 60
if(tMinuets < 10):
pMin = "0"+ str(tMinuets)
pMin = str(tMinuets)
print "%d:%s" %(tHour,pMin )

Monday, July 20, 2009

Setting the selected treeItem in a treeview on right click

So over the past several weeks I have been working with a lot of TreeViews in WPF I use a HierarchicalDataTemplate with a custom ItemContainerStyle for my nodes in the tree. I have been having a rather hard time finding a way to easily set the right clicked on node before the context menu is opened on it. My HierarchicalDataTemplate consists of a stackpanel with a context menu and a image and textblock.

In the ContextMenu_Opened function i put the following code.

   93 var cMenu = sender as ContextMenu;
   94             var tbase = (cMenu.PlacementTarget as StackPanel).DataContext as BasicTreeViewBase;
   95             tbase.IsSelected = true;

Which will set my selected item to true. The BasicTreeViewBase is a ViewModdle object baised off of the code found at TreeViewWithViewModel and is rather simple.

Wednesday, May 6, 2009

Some times you just have to rename files.

So I needed to rename a bunch of files. I needed to add the size of the file as a postfix. Same name just an adition of 16x16 which when doing it by hand is tedious. So this little script was created. You have to dump it into the same folder as the files you are working with, and you have to put the path in walkPath. I could have it pull the current directory from the os but that would take work.

   import os

   walkPath = r"some disk path"

   for d in os.walk(walkPath):
      dirpath, dirnames, filenames = d
      for a in filenames:
         if a.endswith(".py"):
            print a
            aSplit = a.split(".")

Monday, March 16, 2009

Strings in c++

I made the mistake of taking a c++ class.  Well itn's not a mistake it is good to be reminded of how much effort things are in c++.  Take strings for example. In c# if you want to lower case a string you do a str.ToLower() and it is lower case in c++ you have to do some thign like this.
   30     static void ToLower(string &itext)
   31     {
   32         transform(itext.begin(), itext.end(), itext.begin(), (int(*)(int)) tolower);
   33     }

This one wasn't all that bad it is still just one line. This what the first one I needed
for the project I am working on at the moment.  The other one I wished I had on several
was str.Trim() just to clear off the white space. So I spend 5 minuets googling and found
something simlar to this.  
   35 static void Trim(string &itext)
   36 {
   37     int startLoc = itext.find_first_not_of(" \t\n");
   38     int endLoc = itext.find_last_not_of(" \t\n");
   39     if((std::string::npos == startLoc) || (std::string::npos == endLoc))
   40     {
   41         itext = "";
   42     }
   43     else
   44     {
   45         itext = itext.substr(startLoc,endLoc - startLoc + 1);
   46     }
   47 }

it dosn't do any thing but white space but still it is better then a kick in the face.