Get your own “Tool” blog!

Have you ever thought about writing your own blog?  Well now you can have it! And for only $5!  NetDevInc.com (The brains of this operation, not the comedy) has teamed up with FiveRR.com to bring you the Deal of the Year!

You get:

  • Help setting up your domain name
  • Web hosting for 3 months
  • WordPress Installed
  • Free themes and useful plug-ins
  • And MORE !!!

Check out our offer at http://bit.ly/WordPressForFiveDollars

Python, Pyramid, and Mako… How to get Mako Working With Pyramid…

Well looks like Pylons is now Pyramids, and here go hell come. Now I could go on explaining the differences (Which most likely I will once I get farther in), but this is more just a quick post.

ANSWER:

In the development.ini file, place this under the [app:pytrends] area

mako.directories=projectName:folderWhereYourTemplatesAre

For example, the project I have is pyzazz (Don’t ask me what that is cause I don’t even know) and the template folder is… well templates. So I have it as:

mako.directories=pyzazz:templates

MORE USELESS WORDS:

When using pylons I used mako. It was fast and well it was by default. Now the default template engine is Chameleon which (Sorry Chameleon creator) sucks. First time using it, it was throwing parser errors in javascript lines like:

 if (urlParams.actual && urlParams.actual == 'true')

and it forced me to end an input with the </input> tag. And if you know anything about HTML, input tags DO NOT HAVE END TAGS. True story, go check w3.org if you feel like. Little things like that just annoy the hell out of me. Yeah I know it’s just html and javascript, the runts in the litter as far as programmers care, but you still can’t jack things up like that. And on that note: if someone who uses chameleon (Or helped write it) actually comes here, you are welcome to shoot down my claims.

Linq and Stack… Take versus Pop

So this might be filed under “Who f—ing cares” but I thought it was somewhat interesting. If you’ve ever used a Stack, you should be familiar with Pop and Peek. If not, here’s a little diddy from a guy named diddy. Actually that’s a lie. I have no affiliation Sean “Puffy” “Puff Daddy” “P Diddy” “Whatever he’s called now” Combs. We do share the same first name though. (Annnnnd wait for incoming lawsuit over using his name on this site)

A stack is a first in last in first out structure that in the .Net world uses two methods to get values back from it: Pop and Peek. Pop will give you the item AND remove it from the stack. Peek will merely give you the item but leave it safely on the stack.

What’s the point of this post? I’ll tell you when I find out.

Now when using Linq with a stack, you might get in trouble if you assume the Take method uses pop to get the value:

  return stackToUse.Take(count).ToList();

You would think that this would use Pop since Pop really is the “natural” (For lack of a better word) function of a stack. Most languages can guarantee a Push and Pop for stacks, but not all languages have a Peek. So it would be normal to assume the default is Pop. Problem is: It’s not. The Take method actually uses the Peek method. So these two methods will give a completely different return:

    ///Uses Pop
    ///  Return list with have "count" number of items and stackToUse will have the original
    ///    count of items minus "count"
    public static List<Object> CreateListFromPopOnStack(Stack<Object> stackToUse, Int32 count)
    {
      return Enumerable.Range(0, count).Select(item => stackToUse.Pop()).ToList();
    }

    ///Uses Take/Peek
    ///  Return list will have "count" number of items and stackToUse will have still have
    ///     the same number of items it came in with.
    public static List<Object> CreateListFromTakeOnStack(Stack<Object> stackToUse, Int32 count)
    {
      return stackToUse.Take(count).ToList();
    }

In the end, this is a rare case you will actually need to know, but what the hell? Why not know it?

75k Visits, 110k Views…. 59k uniques This Year

Yeah I know there are a billion sites that get that in a day, but still feels like victory of sorts since it’s pretty much 3x what I did last year. Mind you, most of the views come from searching engines, but I want to believe it’s because of all the love. It’s a strange love though. More like an abusive relationship where I yell at you and you still come back. So I guess that makes you guys dogs. But whatever, analogies, and venomous typing, aside I couldn’t have done it without you: random google searching person. You’ve made this site the 735,849th most popular site in the world (277,328 in India… Yeah India. I’d say I owe you a drink but I don’t think I could afford that.) Maybe if I’m extra special I can break into the top 500,000 next year.

Ok I realize that goal is like a product placement for Mad TV’s Lowered Expectations, but you know what? Screw you. As the late and great Leslie Nielsen put it, “It’s a topsy-turvy world, and maybe the problems of two people don’t amount to a hill of beans. But this is our hill. And these are our beans!” I’ve got my beans. You can’t take that from me.

Ok nerd rage subsiding, I’d like to thank some people for this “accomplishment”. First off, I want to thank me for being chock full of awesome. I’d also like to thank… eh… me? What? You thought I was going to say you? I’m waaaaaay too narcissistic for that. (Expect for India, I heart you India)

Anyway, as this post spirals down into the inevitable useless drivel that all two repeat readers have come to expect, I think it’s time to wrap things up, so I’ll leave you with this thought:

Hi.

MVC 3, Razor, and My 1 Cent

So a while back I decided I would jump right into MVC 3 to see what’s new. Now the first plan I had was to still use my bestest friend ever, Spark. After some thought (Very little as thinking is too resource intensive) I decided I would take on Razor with it. Why not? It’s probably going to be the new default standard engine so I better get a feel for it.

There’s an old saying in Tennessee — I know it’s in Texas, probably in Tennessee — that says, meet the old boss same as the… meet the boss… New boss is the same. I can’t help but feel like Razor isn’t really a step revolution as it is just an evolution. Sure there are newer features like replacing the <% with a @ and… well hey you can replace the <% with an @.

Ok so maybe I’m being a bit hyperbolic but in all honesty, why not be? Now I know I’ve been hard o… mean to Microsoft in the past but it’s out of respect really. I’ve come to expect Microsoft to really have its ducks in a row. After all, Entity Framework 2 was a huge improvement over 1 and I think it’s safe to say that C# has come a long way. Razor just feels like a half hearted attempt to bridge the gap between the old MVC engine and something like Spark… lovely, lovely Spark oh how you make me so happy.

After using it for a couple months, just not really impressed and the only thing that I would say it has over Spark is intellisense on the front end, and to be honest, that’s not something a good programmer (IE Not me) should rely on and therefore isn’t really a +1 in the win column.

This is at best baby steps. I expect more from a company like Microsoft where smart people are grown in smart people farms. (You know, free range organic smart people. No steroids.)  Average Joe’s like me look to Microsoft to really just shake the ground beneath me and make my life better at work because let’s be honest, it’s painful convincing Microsoft hardened programmers to adopt something like Spark because it’s not Microsoft. (Holy run on sentence.)  I’m tired of seeing cut up HTML. I’m tired of seeing yellow highlights. Is it too much to ask to take the Spark pseudo HTML look and run with it?

This may seem like a petty thing to hit on, and maybe it is. After there were improvements to the MVC viewing engine with the introduction of Razor, I can’t deny this. Maybe I just have too much love for HTML in the first place. But even something in line with Razor (Uses a similar look and feel) such as Pylons for Python is better (In my opinion, which with $3 still can’t buy you coffee) and that’s completely free to use. IE doesn’t require a business to buy a 10k IDE. (Yeah I know there are alternatives that are a lot less but really, how often do you see businesses going the non Visual Studios route?) I want Razor to sell itself to me. (Take that sentence however you want to.) I want it to make me think, “You know what, it’s not perfect but it’s a damned good replacement for X.” (Sort of like Entity Framework 2 did or MSTest even.) I want it to just hit me over the head with a club and drag me home by the hair. Right now I just feel like it’s doing the “yawn to arm around the shoulder” move. You know, skip that thought since I’m not sure I’m really comfortable with the direction I’m taking these analogies.

Point is, I expect more from the mothership. The best thing I found with Razor is the ability to use dynamic models and that’s really more on .Net 4.0 than Razor. (Which is a feature I really, really like despite the confused tone of the dynamic model post.)

I suppose my view is a bit tainted though since I pretty much take a lot things for granted since I have used Spark for over a year and can barely remember the original MVC view engine. I suppose it’s possible if I had gone from the old view engine to Razor I would have a different view. I suppose that I use the word suppose too much.

ASP.Net WebService/Jquery Post: Invalid Number of Parameters

A coworker ran into this one today as he was trying to call an old school web service using jQuery Ajax/Post. Keep throwing an exception/returning a 500 error. After some searching, both google and soul, it turns out that services that are expected to be used with scripts (Javascript or Jquery in this case) need the attribute ScriptService. Yeah, that’s it. The silly and cryptic message is really just an attribute needed on the code file above the service class. Go figure.

ASP.Net MVC2, Dynamic Models, Json, and Javascript

Ok so one thing I fell into while using the dynamic keyword as a model, was an issue with the dynamic model being parsed into json and then back again into javasript using jQuery. Silly me, I thought that the dynamic model would represent itself like any other object, but it turns out the dynamic object is actually a dictionary with a key value pair. Now I’m not going to harp on that since I think that’s kind of how Python does it (And javascript for that matter), but I did want this:

function doSomething(result) {
  if(result.Success){
    alert(result.Value.UserName);
  }
}

Where the result value was:

...  //Other junk
dynamic returnModel = new ExpandoObject();
returnModel.UserName = _state.CurrentUser.UserName;
result.Value = returnModel;
...  //set the data on a jSonResult to that result above and return.

But what I got for Value was a dictionary:

  result.Value[0].Key  "UserName"
  result.Value[0].Value  "test@test.com"

As you can guess, using the javascript from above won’t work. However, there is a way to run the above javascript.

    var convertedValue = new Object;

    for(var i = 0; i < result.Value.length; i++){
      convertedValue [result.Value[i].Key] = result.Value[i].Value;
    }

You see with javascript, like the dynamic object, the objects not only respond to a direct assignment like a property, but can also be manipulated like a dictionary. Where does that code above get me? Well simple, I can now do:

  alert(convertedValue.UserName);

Yeah its not perfect and I have to think there will be some way for a better json translation, but for now this is gold.

ASP.Net MVC 2/C# 4… Dynamic Model and Should You Use It?

So there’s a new sheriff in town and its name is dynamic. Actually, its not really that new and that’s a horribly misused cliche. My lack of literary genius aside, I’ve been looking for a reason to use dynamic. Then it came to me: Models. Now the only reason why I started down that path was the use of Python where pretty much anything is dynamic. In using python, I got used to not embracing the rigidity of classes for models and adopted a more “Oh what the f–k” attitude. Back in the .net world though, I was using typed views. Then I readopted the “Oh what the f–k” attitude and applied it to .net mvc.

Here’s an example:

    [RequiresAuthentication]
    public ActionResult ShowBasicInfo()
    {
      IState currentState = ObjectFactory.Create();
      dynamic returnModel = new ExpandoObject();

      returnModel.UserName = currentState.CurrentUser.UserName;

      return View(AccountActions.ShowBasicInfo, returnModel);
    }

As you can see, I created a dynamic class and just added a property to it. Now on the view side:

...
@model dynamic
...
          <label id="showBasicInfoEmail" name="showBasicInfoEmail">@Model.UserName</label>
...

So if that’s all you’re here for, well there you go. Now get out.

Ok so the real point to this post was actually the “should I?” Now with .Net, you really have to adopt a “should I?” attitude on anything that is new otherwise it might come back to bite you in the a–. (Update panels anyone?) Just using something because it looks easier is NOT a reason to do so.

The reason why I originally gravitated toward typed views was I didn’t like all the magic sting junk that came with the View dictionary, or whatever the hell it was called. (I’m too lazy to look it up) Typed views gave a sort of concrete nature much like an interface does to a class. You knew exactly WHAT the view could show based on the model. This is good I still think in an environment where you don’t trust people to code correctly or when a person needs an easy place to look up what the incoming model contains. After all, on the second point I mean, its a lot easier to look at a class to find EXACTLY what the model has than looking at a controller action code. Simple for reference.

With that being said, getting stuck in model hell can happen. After all for every action there is an equal and… wait… there is a model. Yeah you can reuse models to cut that down, but its not too hard to imagine it becoming a model infested nightmare. Sometimes you just have to take the good and the bad, but sometimes you’re able to trust people and just go with what is easier.

Why trust? With Python and it overall dynamic nature, it was easy to see that such a tool put in the wrong hands could be a disaster. Anyone who has worked with JavaScript will know this pain. Python is just a fancy way to annihilate your foot, so the concept of allowing dynamic models only made me shiver like a prostitute on Christmas. Sorry, that wasn’t appropriate. I meant a prostitute on a non religious holiday like Thanksgiving or the Chinese New Year. (Sorry non Christian readers)

On the other hand, when in a small group where you have less time and more to do, it could be used as a compromise as it doesn’t use the hated magic string dictionary thing approach, but still had a class like feel. And on top of that, if you wish to create models later, it would be extremely easy to swap the @model dynamic with whatever class type you need. So in that way, I can almost stop my non stop convulsing that is a natural reaction to doing something I deem bad.

Answer is: I don’t f–king know right now, but I’m going to fly with it and see how I like it.

Side note: One drawback of the dynamic route is the lack of intellisense. That could be a deal killer for some.

NUnit Hangs Up When Run Through Nant Using .Net 4.0

Ran into this one yesterday starting with Cruise Control not finishing. At first I just thought Cruise Control just wasn’t that into it, but turns out something was hanging up and that something was N-N-N-UNIT. Well to be more specific it was NUnit while being run through Nant although in the end it wasn’t Nant’s fault. Turns out it has to do the latest stable release of NUnit… 2.5.8.something.whatever.

The fix? Grab 2.5.9.yayayaya from the download page and use the nunit-console from that instead. I guess the whole issue had to do with the latest .Net 4.0 and the latest stable version of NUint. I guess they don’t like each other. They go together like water and something that doesn’t go well with water.

jQuery Validate Date/Check If Is Date

Far as I can tell, and that’s pretty far even though I do wonder if you can actually measure telling in distance, there isn’t a method directly used for validating a date in jquery. I find this rather odd. Not odd like how people keep watching Uwe Bol movies, more like odd that no one has tried to make a time machine to prevent them from ever appearing.

Anyways, bad movies… horrible movies… worst movies of all time aside, there is a date checking method, just not directly in jquery. It’s actually in the jquery.ui.datepicker.js file which is part of the UI files. Basically I grabbed that file and used the parseDate method on it coupled with a try catch. After all, a failure in the parseDate method throws an exception, and we don’t like those. No we don’t. (Which begs the question as why it throws an exception instead of just returning a null date.)

function isValidDate(controlName, format){
    var isValid = true;

    try{
        jQuery.datepicker.parseDate(format, jQuery('#' + controlName).val(), null);
    }
    catch(error){
        isValid = false;
    }

    return isValid;
}

Very simple, and it works. You could ask why I didn’t just roll my own date method, and then I would ask you how ambitious do you think I am? Then you would punch me because I answered your question with a question like a complete tool bag. Then I would cry. And THEN I would answer your original question. Fact is, I trust that the method created for the DatePicker is in fact well tested, otherwise the whole jquery thing just isn’t worth trusting seeing as it’s a part of their core library. And that I just refuse to believe.