﻿<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Programming By A Tool &#187; Lessons</title>
	<atom:link href="http://byatool.com/category/lessons/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Create An X  Delimited String From A Char List Using Linq Aggregate</title>
		<link>http://byatool.com/lessons/create-an-x-delimited-string-from-a-char-list-using-linq-aggregate/</link>
		<comments>http://byatool.com/lessons/create-an-x-delimited-string-from-a-char-list-using-linq-aggregate/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 19:16:56 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Aggregate]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=2510</guid>
		<description><![CDATA[A quick example of how to use the Aggregate method to create a string of delimited members, or in this case characters. You might wonder why this example, or at least you should. It&#8217;s true, the character list to delimited string is pretty useless, but some idiot from where I work needed it. [TestMethod] public [...]]]></description>
			<content:encoded><![CDATA[<p>A quick example of how to use the Aggregate method to create a string of delimited members, or in this case characters. You might wonder why this example, or at least you should. It&#8217;s true, the character list to delimited string is pretty useless, but some <a href="http://byatool.com/andre/">idiot from where I work</a> needed it.</p>
<pre>[<span style="color: #008080;">TestMethod</span>]
<span style="color: #0000ff;">public void</span> GetStringFromCharacters()
{
    <span style="color: #0000ff;">var</span> charList = <span style="color: #008080;">Enumerable</span>.Range(0, 10).Select(x =&gt; 'a').ToList();

    charList
        .Aggregate(<span style="color: #800000;">""</span>, (inner, outer) =&gt; inner + outer.ToString() + <span style="color: #800000;">","</span>)
        .Should()
        .Be(<span style="color: #800000;">"a,a,a,a,a,a,a,a,a,a"</span>);
}
</pre>
<p><br/><br />
The big thing here is the &#8220;&#8221; in the Aggregate method signature.  That basically says that Inner is a string.  If I didn&#8217;t have that specified, both inner and outer would be a character.<br />
<br/><br />
YAY</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/create-an-x-delimited-string-from-a-char-list-using-linq-aggregate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>XUnit and Connection Strings</title>
		<link>http://byatool.com/lessons/xunit-and-connection-strings/</link>
		<comments>http://byatool.com/lessons/xunit-and-connection-strings/#comments</comments>
		<pubDate>Sat, 15 Oct 2011 16:41:23 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[F#]]></category>
		<category><![CDATA[Xunit]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=2312</guid>
		<description><![CDATA[A real quick one but maybe a good one. I was running the xUnit.gui.exe program to run units tests using xUnit&#8230; duh. Problem I ran into is that xUnit is not a part of the solution that the tests were. This caused issues when trying to run integration tests since the needed app.config wouldn&#8217;t be [...]]]></description>
			<content:encoded><![CDATA[<p>A real quick one but maybe a good one.  </p>
<p>I was running the <a href="http://xunit.codeplex.com/">xUnit.gui.exe</a> program to run units tests using xUnit&#8230; duh.  Problem I ran into is that xUnit is not a part of the solution that the tests were.  This caused issues when trying to run integration tests since the needed app.config wouldn&#8217;t be read by xUnit as the config file used when running an app is the config file from the topmost (Usually UI) project/application.  So I made a wild guess that if I follow that logic, what little there may be, and add the connection strings to the <a href="http://byatool.com/test/xunit-gui-exe-this-assembly-is-built-by-a-runtime-newer-than-the-currently/">xunit.gui.exe.config</a> file as if it were an app.config/web.config, it should use the connection strings added&#8230; and turns out it did.</p>
<p><a href="http://byatool.com/wp-content/uploads/XunitConnections.png"><img src="http://byatool.com/wp-content/uploads/XunitConnections.png" alt="" title="XunitConnections" width="742" height="452" class="alignleft size-full wp-image-2313" /></a></p>
<p>And there you have it&#8230; it being the solution and not a clown.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/xunit-and-connection-strings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fun with F# and Method Reduction</title>
		<link>http://byatool.com/lessons/fun-with-f-and-method-reduction/</link>
		<comments>http://byatool.com/lessons/fun-with-f-and-method-reduction/#comments</comments>
		<pubDate>Fri, 14 Oct 2011 18:47:44 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=2306</guid>
		<description><![CDATA[So the first and second methods came from this book , but I thought for fun I might reduce it further to see how small a call could get. Basically take a number and divide it by another number 3 times. let bytesToGb item = let itemB = item / 1024I let itemC = itemB [...]]]></description>
			<content:encoded><![CDATA[<p>So the first and second methods came from <a href="http://www.amazon.com/Programming-comprehensive-writing-complex-problems/dp/0596153643"> this book </a>, but I thought for fun I might reduce it further to see<br />
how small a call could get.</p>
<p>Basically take a number and divide it by another number 3 times.</p>
<p><span style="color: #0000ff;">let</span> bytesToGb item =<br />
<span style="color: #0000ff;">let</span> itemB = item / 1024I<br />
<span style="color: #0000ff;">let</span> itemC = itemB / 1024I<br />
<span style="color: #0000ff;">let</span> itemD = itemC / 1024I</p>
<p><strong>Is the same as</strong>:<br />
<span style="color: #0000ff;">let</span> bytesToGb item =<br />
<span style="color: #0000ff;">let</span> x = x / 1024I<br />
<span style="color: #0000ff;">let</span> x = x / 1024I<br />
<span style="color: #0000ff;">let</span> x = x / 1024I</p>
<p><strong>And more reduction:</strong><br />
<span style="color: #0000ff;">let</span> bytesToGb item =<br />
item<br />
|&gt; (fun x -&gt; x/1024L)<br />
|&gt; (fun x -&gt; x/1024L)<br />
|&gt; (fun x -&gt; x/1024L)</p>
<p><strong>Or more:</strong><br />
<span style="color: #0000ff;"> let</span> divide x = x/1024L<br />
<span style="color: #0000ff;">let</span> bytesToGb = divide &gt;&gt; divide &gt;&gt; divide</p>
<p>If you&#8217;re familiar with Func/Action, Linq, ect this might actually look a lot like some of the newer &#8220;functional like&#8221; additions to C#. Well at least the third one. Funny part, and I didn&#8217;t know this, is that most of the more interesting additions to C# (Generics, Linq, Lambda expressions) actually came from F#; Which was built to match other existing functional languages like Haskel)  Go figure.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/fun-with-f-and-method-reduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing 4.0 Web Forms Including Event Handlers Like Page_Load Using Dynamic</title>
		<link>http://byatool.com/lessons/unit-testing-4-0-web-forms-including-event-handlers-like-page_load-using-dynamic/</link>
		<comments>http://byatool.com/lessons/unit-testing-4-0-web-forms-including-event-handlers-like-page_load-using-dynamic/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 21:09:11 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=2196</guid>
		<description><![CDATA[If you&#8217;re too lazy or just plain impatient, you can find the source code here. So one of the biggest pains in a place that doesn&#8217;t lend itself well to be in pain has been trying to unit test web forms like how MVC controllers can be. Before 4.0, this was an aggrevation at the [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re too lazy or just plain impatient,<a href="http://demo.byatool.com/WebFormUnitTest/TestingWebForm.zip"> you can find the source code here.</a></p>
<p>So one of the biggest pains in a place that doesn&#8217;t lend itself well to be in pain has been trying to unit test web forms like how MVC controllers can be.  Before 4.0, this was an aggrevation at the least.  Something in between stubbing one&#8217;s toe and getting only chocolate icecrfeam when you ask for a @*&#038;*ing swirl.  Serious, how hard is it to pull the swirl lever?  Really.</p>
<p>Anyways, so there are certain things in .net web forms that were pretty difficult to hande/mock/get around for testing.  Let&#8217;s take session for instance.  Session is a sealed class with a non public constructor.  To make things more interesting, the Page class only has a get for the Session property.  Now you could go through a lot of hell, mostly reflection, to get aroud this, but who wants to do that?  Personally I gave web forms the ole one finger salute, but being that I am working with web forms again&#8230; yeah had to find a way to get around this.  The answer?  Stems from dynamic.</p>
<p>Basically what I had to do is create a class that has a Session property that is dynamic, and then replace the old Session call with a class that calls a property named Session.  The effect to the code, as far as the immediate code behind is concerned, is just this simple adding of the class before the Session call.  Far as the code to do this, it&#8217;s fairly easy too.</p>
<p>First I started with an interface since using an interface will help later if I need to mock.</p>
<pre>
<img src="http://demo.byatool.com/WebFormUnitTest/Interface.png"></img>
</pre>
<p>This is the bread and butter.  Basically we have a container that will hold the session object.  Making the property dynamic allows it to be set to anything.  All I have to worry about is that whatever I set it to has the functionality used by Session.  This is started by implementing the interface fo use on the site:</p>
<pre>
<img src="http://demo.byatool.com/WebFormUnitTest/LiveHandler.png"></img>
</pre>
<p>And then one for use with testing:</p>
<pre>
<img src="http://demo.byatool.com/WebFormUnitTest/TestHandler.png"></img>
</pre>
<p>How are these used?  Well if we take a base page:</p>
<pre>
<img src="http://demo.byatool.com/WebFormUnitTest/BasePage.png"></img>
</pre>
<p>So what this does is holds two constructors; One for use by .Net when it runs the site and one for the test class when it is testing the page functionality.  The second is a way to stop all the stuff that happens in the Page class constructor when it&#8217;s time to unit test this [explitive].  This might look familiar to people who have used things such as Dependency Injection with MVC to help with Controller testing.  If not, the idea is simple.  It basically just gives the Page the test handler we need it to use when testing.  If we aren&#8217;t testing, then we let it just use the default handler.  </p>
<p>You might wonder why I didn&#8217;t instantiate the live version in the constructor, and the answer is: Session isn&#8217;t ready to be used at constructor time when doing the normal web site thing.  Therefore, it will be set the first time the SessionHandler property is called.  If it&#8217;s a test, well then it&#8217;s fine to set it in the constructor since it is bypassing the normal web site&#8230; thing.</p>
<p>The unit test looks something like this:</p>
<pre>
<img src="http://demo.byatool.com/WebFormUnitTest/UnitTest.png"></img>
</pre>
<p>As you can see, a test version of the handler is created and the Session object is replaced with a Dictionary since that&#8217;s what Session is&#8230; well it has more to it but the most used feature is the key/value match and it happens to be the only part of Session I show in this example.</p>
<p>Since there is the second, test only, constructor on the page, we don&#8217;t have to worry about any asp.net stuff getting in the way when instantiating the page.  The page will look like this:</p>
<pre>
<img src="http://demo.byatool.com/WebFormUnitTest/Page.png"></img>
</pre>
<p>As it can be see, the replacement of the Session call with SessionHandler.Session allows the features of Session but also the testability it lacked for so long.  It&#8217;s true that if you want to use other features of Session, if there are any and I can&#8217;t remember, you&#8217;ll have to just add them to the test version so they can be called at test time.  I can&#8217;t do everything for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/unit-testing-4-0-web-forms-including-event-handlers-like-page_load-using-dynamic/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test Driven Development&#8230; A Simplified Start</title>
		<link>http://byatool.com/lessons/test-driven-development-a-simplified-start/</link>
		<comments>http://byatool.com/lessons/test-driven-development-a-simplified-start/#comments</comments>
		<pubDate>Thu, 02 Jun 2011 16:39:06 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[TDD]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=2161</guid>
		<description><![CDATA[So I went to a &#8220;doctor&#8217;s appointment&#8221; yesterday and was asked to do some pair(ed?) programming to solve a simple-ish request. Basically it was: Get a number in a list of integers that is closest to 0. Sounds easy enough, right? Well there are a few more rules but nothing game breaking: If the list [...]]]></description>
			<content:encoded><![CDATA[<p>So I went to a &#8220;doctor&#8217;s appointment&#8221; yesterday and was asked to do some pair(ed?) programming to solve a simple-ish request.  Basically it was:</p>
<pre>
Get a number in a list of integers that is closest to 0.
</pre>
<p>Sounds easy enough, right?  Well there are a few more rules but nothing game breaking:</p>
<ul>
<li>If the list contains a 0 then return 0</li>
<li>If the list is null then throw ArgumentNullException</li>
<li>If list is empty then throw ArgumentException</li>
<li>If there is no 0 then get the number nearest 0</li>
<li> If the closest is the same for positive and negative (Say -2 and 2), return the positive</li>
</ul>
<p>Again, fairly simple thing to work on.</p>
<p>Right off the bat I set about creating the method for doing this since my usual plan is:</p>
<ol>
<li>Create method</li>
<li>Unit test</li>
<li>???</li>
<li>#$@%!</li>
</ol>
<p>The fourth step is actually repeated a couple times.</p>
<p>So after I had done the more brute force sort of way, and had a failing test, it was suggested I start over.  This time stick with the TDD like mindset of</p>
<ol>
<li>Create test</li>
<li>Fail test</li>
<li>Add code to make it work</li>
<li>Watch it succeed</li>
</ol>
<p>Now the concept of TDD I have known about for a while, but never really had a chance to really jump into it.  After all, most companies just want something done now, not done well.  Beyond that, I had reservations of how it works and whether I was smart enough to pull it off.  After all, I had always thought you have to be able to see the whole method before it&#8217;s written in order to create tests.  Well after yesterday I can say I was wrong&#8230; at least about the second part.</p>
<p>When I started writing test methods I was told to make them each this way:</p>
<pre>
Always add to a method the most simple way to get a test to run.
</pre>
<p>Yeah ok, it sounded better when I heard it yesterday.  However, I have examples.</p>
<p>So if we take the first requirement:</p>
<pre>
If listToCheck contains a 0 then return 0
</pre>
<p>Well what should I write as a test?  Easy:</p>
<pre>
<a href="http://byatool.com/wp-content/uploads/2011/06/FirstTestCreate.png"><img src="http://byatool.com/wp-content/uploads/2011/06/FirstTestCreate.png" alt="" title="FirstTestCreate" width="494" height="272" class="alignleft size-full wp-image-2162" /></a>
</pre>
<p>As you can see I am only checking to make sure there is a 0 returned.  Side Note:  You might notice that the method is red.  This is because it hasn&#8217;t been created yet.  That&#8217;s the next step.  What should I put in the method?  Remember I only want to put in the most simple way to get the test to pass.</p>
<pre>
<a href="http://byatool.com/wp-content/uploads/2011/06/FirstTestAddToMethod.png"><img src="http://byatool.com/wp-content/uploads/2011/06/FirstTestAddToMethod.png" alt="" title="FirstTestAddToMethod" width="415" height="165" class="alignleft size-full wp-image-2163" /></a>
</pre>
<p>Which gives me this guy:</p>
<pre>
<a href="http://byatool.com/wp-content/uploads/2011/06/FirstTestSuccess.png"><img src="http://byatool.com/wp-content/uploads/2011/06/FirstTestSuccess.png" alt="" title="FirstTestSuccess" width="979" height="153" class="alignleft size-full wp-image-2164" /></a>
</pre>
<p>Right now you&#8217;re probably thinking that test is the programing equivalent of Master of the Obvious.  I mean, of course it passes because it always returns 0.  The next unit test might help with seeing the bigger picture:</p>
<pre>
<a href="http://byatool.com/wp-content/uploads/2011/06/SecondTestCreate.png"><img src="http://byatool.com/wp-content/uploads/2011/06/SecondTestCreate.png" alt="" title="SecondTestCreate" width="442" height="259" class="alignleft size-full wp-image-2165" /></a>
</pre>
<p>Ok so now I&#8217;m testing:</p>
<pre>
If the list is null then throw ArgumentNullException
</pre>
<p>So naturally I have a test that expects an ArgumentNullException to be thrown.  I don&#8217;t have that yet in the method though so the expected result when running the test is:</p>
<pre>
<a href="http://byatool.com/wp-content/uploads/2011/06/SecondTestFail.png"><img src="http://byatool.com/wp-content/uploads/2011/06/SecondTestFail.png" alt="" title="SecondTestFail" width="1190" height="169" class="alignleft size-full wp-image-2167" /></a>
</pre>
<p>Boom.  Go figure.  Next step is to add only what is necessary to make the unit test pass.</p>
<pre>
<a href="http://byatool.com/wp-content/uploads/2011/06/SecondTestUpdateMethod1.png"><img src="http://byatool.com/wp-content/uploads/2011/06/SecondTestUpdateMethod1.png" alt="" title="SecondTestUpdateMethod" width="413" height="249" class="alignleft size-full wp-image-2170" /></a>
</pre>
<p>Now you can see that I have added an exception to be thrown if the list is null.  Time to run the tests:</p>
<pre>
<a href="http://byatool.com/wp-content/uploads/2011/06/SecondTestSuccess.png"><img src="http://byatool.com/wp-content/uploads/2011/06/SecondTestSuccess.png" alt="" title="SecondTestSuccess" width="961" height="163" class="alignleft size-full wp-image-2171" /></a>
</pre>
<p>Now I know that the method does take care of the first two requirements.  From here I go requirement by requirement and create unit tests to make sure the requirements are met and have them all fail at first.  After the test fails, add in the minimal amount of code necessary to get the test to pass.</p>
<p>Eventually you will have a working method that is completely covered.</p>
<p>You might be wondering why this site still exists but you might also be wondering why you should go requirement after requirement adding just what&#8217;s needed.  The answer is actually simple:  It lets you know exactly what changes you make breaks any other test.  You see, if you were to do the normal &#8220;build the method and test&#8221; and there&#8217;s a test failing, it becomes harder to track down the issue since the method is now a bunch of code.  Since there are multiple points of failure when you start writing the tests, tracking down a specific error is hard.</p>
<p>If you go requirement by requirement, test by test, you can easily find anything that breaks the created tests since you are only working one requirement at a time.  I the first three requirements are met and tested when you work on adding the fourth and for some reason what you added to the method is breaking one of the successful test, you know exactly where the issue is.</p>
<p><a href="http://demo.byatool.com/TDD.zip">I have the code here if you want to look at it</a> as I&#8217;ve I&#8217;ve commented a lot of the code.  It is in VS 2010 though.  However I don&#8217;t think there&#8217;s anything that couldn&#8217;t work in 3.5.</p>
<p>I admit it&#8217;s kind of a different way of looking at it, but I found it incredibly useful in it&#8217;s simplicity.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/test-driven-development-a-simplified-start/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MVC3, Entity Framework, jQuery&#8230; Everything example</title>
		<link>http://byatool.com/lessons/mvc3-entity-framework-jquery-everything-example/</link>
		<comments>http://byatool.com/lessons/mvc3-entity-framework-jquery-everything-example/#comments</comments>
		<pubDate>Mon, 18 Apr 2011 18:33:21 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Castle]]></category>
		<category><![CDATA[Entity Framework 4]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Validator]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Mvc3]]></category>
		<category><![CDATA[Unit Testing]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=2110</guid>
		<description><![CDATA[How would you like to have a project that has these features? Dependency Injection Entity Framework 4.0 with POCO Inversion of Control with Castle The IResult jQuery jQuery Ajax Posts jQuery Custom Css jQuery Validation Mocking with Rhino Mocks MVC 3/Razor MVC Annotation Based Validation What I call the Repository Pattern Unit Tests - Integration [...]]]></description>
			<content:encoded><![CDATA[<p>How would you like to have a project that has these features?</p>
<pre>
Dependency Injection
Entity Framework 4.0 with POCO
Inversion of Control with Castle
<a href="http://byatool.com/Hosted/Tutorial/IResult.html" style="color:red;">The IResult</a>
jQuery
jQuery Ajax Posts
jQuery Custom Css
jQuery Validation
Mocking with Rhino Mocks
MVC 3/Razor
MVC Annotation Based Validation
What I call the Repository Pattern
Unit Tests - Integration With Entity Framework
Unit Test - MVC Controller Actions

And more s--t I can't think of right now!
</pre>
<p>What&#8217;s the price of this gem?  Just your time which is worth nothing!</p>
<p>Where can you find it?  <a href="http://byatool.com/Hosted/Tutorial/Mvc3Example.zip">Right here!</a></p>
<p>Requirements:</p>
<pre>
Visual Studios 4.0
<a href="http://www.asp.net/mvc/mvc3" style="color:red;">Mvc 3</a>
To open the solution and do a search and replace on "Tutorial" to set your directory paths.
A real f--king need to learn!
</pre>
<p>Act now, operators aren&#8217;t in existence since this is hosted on a website, but if they were they would be standing by!</p>
<p>Disclaimer:  </p>
<p>This may or may not hit you with a wave of awesome.  Be prepared for the worse case awesome scenario.  I am in now way responsible for any heart stoppage due to the shock and awe power of the project.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/mvc3-entity-framework-jquery-everything-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pycharm: Change Error Alert Color/Style</title>
		<link>http://byatool.com/lessons/pycharm-change-error-alert-colorstyle/</link>
		<comments>http://byatool.com/lessons/pycharm-change-error-alert-colorstyle/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 13:42:28 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[PyCharm]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1965</guid>
		<description><![CDATA[Once again I&#8217;m using my blog to help remind myself of something later, but you know what? I&#8217;m OK with that. Took a little bit to find this one, but changing the color of Errors and Warnings makes it hell of lot easier to find them. Go figure that the default &#8220;barely more grey than [...]]]></description>
			<content:encoded><![CDATA[<p>Once again I&#8217;m using my blog to help remind myself of something later, but you know what?  I&#8217;m OK with that.</p>
<p>Took a little bit to find this one, but changing the color of Errors and Warnings makes it hell of lot easier to find them.  Go figure that the default &#8220;barely more grey than white&#8221; color doesn&#8217;t do the same.</p>
<p>Anyways:</p>
<p>File -&gt; Settings -&gt; IDE Settings -&gt; Colors &#038; Fonts -gt; General</p>
<p>At that point you&#8217;ll see stuff to the left with a bunch of colors. (Default Text)  Just click on either Error or Warning to change them.  After you&#8217;ve done that, feel free to go back to your project and find all the errors you couldn&#8217;t see before.  It&#8217;s OK that you have errors though.  Awesome as I am, even I make mistakes. Haha just kidding.  Be ashamed of yourself.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/pycharm-change-error-alert-colorstyle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mock SQLAlchemy scoped_session query and Why Python is My BFF</title>
		<link>http://byatool.com/uncategorized/mock-sqlalchemy-scoped_session-query-and-why-python-is-my-bff/</link>
		<comments>http://byatool.com/uncategorized/mock-sqlalchemy-scoped_session-query-and-why-python-is-my-bff/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 15:55:10 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Mocking]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1954</guid>
		<description><![CDATA[sqlAlchemy has sessionmaker to create a session from which you can use query to get whatever you need. For example: someSession.query(SomeTableRepresentationObject).filter...ect If you&#8217;ve used sqlalchemy, nothing new going on here but it wouldn&#8217;t be me if I didn&#8217;t point out the obvious and take three sentences to do so. Now what you may run into [...]]]></description>
			<content:encoded><![CDATA[<p>sqlAlchemy has sessionmaker to create a session from which you can use query to get whatever you need.  For example:</p>
<pre>someSession.query(SomeTableRepresentationObject).filter...ect</pre>
<p>If you&#8217;ve used sqlalchemy, nothing new going on here but it wouldn&#8217;t be me if I didn&#8217;t point out the obvious and take three sentences to do so.</p>
<p>Now what you may run into is something like this:</p>
<pre><span style="color: #0000ff;">def</span> getCountOfUsersByUserName(userName, session):
  <span style="color: #0000ff;">return</span> session.query(<span style="color: #008080;">User</span>).filter(<span style="color: #008080;">User</span>.userName == userName).count()</pre>
<p>Ok now that I typed that out, I see it&#8217;s kind of a dumb method.  But f&#8211;k it, what&#8217;s done is done.</p>
<p>Now from a testing situation, this could look tough. After all if you come from a more static language background like I did, you should know how hard things can be to mock at times.  (.Net Session&#8230; I&#8217;m looking at you with judging eyes.  Sooooo judging.)  But this isn&#8217;t a static language, it&#8217;s Python.</p>
<p>For purposes of making things less confusing, which is hard for me, when I use the word &#8220;Object&#8221; I mean method OR class.  And remember as always, &#8220;banana&#8221; is the safe word.</p>
<p>You see, to mock that out all you need is an object that has a query object that takes in something and a filter object on the query object that takes in something and has a count method on it.  Yeah that totally makes sense.  Try working it backward.</p>
<p>Filter has one parameter and a count method on it.  Whatever that parameter is, it doesn&#8217;t matter since it is Python.  As long as Filter takes in one parameter, you have a winner.</p>
<p>Query, like Filter, takes in one parameter and has a object named Filter on it.  Once again, it doesn&#8217;t matter what&#8217;s passed into Query, just that it takes something in.</p>
<p>Session is basically an object that has no parameters and has a Query object on it.</p>
<p>Now in a static language, this would be annoying.  You would need 3 interfaces and mock a whole ton of stuff dynamically with something like <a href="http://www.ayende.com/projects/rhino-mocks.aspx"> Rhino Mocks</a> or just create a bunch of classes of those interfaces that you can pass in.  Either way, there are complications.  Once you get into things like Web Session or ViewState, it&#8217;s a ton of work.  Python?  Eh, you can do it in 20ish lines.  How you ask?  Well I&#8217;ll show you how!</p>
<pre>	<span style="color: #0000ff;">class</span> mockFilter(object): <span style="color: #008000;">#This is the ONE parameter constructor</span>
		<span style="color: #0000ff;">def</span> __init__(self):
			self._count = 0
			self._first = <span style="color: #008080;">dynamicObject</span>()

		<span style="color: #0000ff;">def</span> first(self):  <span style="color: #008000;">#This is the another method that's just coming along for the ride.</span>
			<span style="color: #0000ff;">return</span> self._first

		<span style="color: #0000ff;">def</span> count(self):  <span style="color: #008000;">#This is the needed Count method</span>
			<span style="color: #0000ff;">return</span> self._count

	<span style="color: #0000ff;">class</span> mockQuery(object):<span style="color: #008000;"> #This is the ONE parameter constructor</span>
		<span style="color: #0000ff;">def</span> __init__(self):
			self._filter = <span style="color: #008080;">mockFilter</span>()

		<span style="color: #0000ff;">def</span> filter(self, placeHolder):<span style="color: #008000;"> #This is used to mimic the query.filter() call</span>
			<span style="color: #0000ff;">return</span> self._filter 

	<span style="color: #0000ff;">class</span> mockSession(object):
		<span style="color: #0000ff;">def</span> __init__(self):
			self._query = <span style="color: #008080;">mockQuery</span>()
			self.dirty = []

		<span style="color: #0000ff;">def</span> flush(self):
			<span style="color: #0000ff;">pass</span>

		<span style="color: #0000ff;">def</span> query(self, placeHolder):  <span style="color: #008000;">#This is used to mimic the session.query call</span>
			<span style="color: #0000ff;">return</span> self._query

  <span style="color: #008000;">#and this... </span><a href="http://www.youtube.com/watch?v=ETgk56xT4Mk"><span style="color: #008000;">THIS IS SPARTA!!1111</span></a><span style="color: #008000;">... yeah I know, I'm about 3 years too late on that joke.</span></pre>
<p>How does this work?  Say I have the method from above:</p>
<pre><span style="color: #0000ff;">def</span> getCountOfUsersByUserName(userName, session):
  <span style="color: #0000ff;">return</span> session.query(<span style="color: #008080;">User</span>).filter(<span style="color: #008080;">User</span>.userName == userName).count()</pre>
<p>I could test it using this:</p>
<pre>  session = mockSession()
  session.query(<span style="color: #800000;">''</span>).filter(<span style="color: #800000;">''</span>)._count = 0 <span style="color: #008000;">#Initialize the mock session so it returns 0 from count()</span>

  getCountOfUsersByUserName(<span style="color: #800000;">'sadas'</span>, session)</pre>
<p>And boom, you have mocking in ten minutes or less or your code is free.</p>
<p>As you can see, the highly dynamic nature of Python makes it a great fit for any project that will need unit tests and mocking.  And which project doesn&#8217;t?  You know what I&#8217;m sayin&#8217;?  You now what I&#8217;m saying&#8217;?  High five!</p>
<p>Note:  dynamicObject is <a href="http://byatool.com/pontification/python-double-tee-eff-exclamation-mark-one-one/">&#8230;. nothing but a cheesy class that inherits Object but has nothing on it. (Turns out that if I did someObject = Object() I couldnâ€™t do this since Object by default doesnâ€™t contain the ability to add things dynamicallyâ€¦ and this was by design.)</a></p>
<p>And yes, I just quoted myself.  I am that awesome.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/uncategorized/mock-sqlalchemy-scoped_session-query-and-why-python-is-my-bff/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Validation with Multiple Checkboxes</title>
		<link>http://byatool.com/lessons/jquery-validation-with-multiple-checkboxes/</link>
		<comments>http://byatool.com/lessons/jquery-validation-with-multiple-checkboxes/#comments</comments>
		<pubDate>Wed, 09 Feb 2011 16:05:30 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Validator]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1941</guid>
		<description><![CDATA[So this one is done on request and because I am a kind of merciful god I will grant this request. For those bullet point programmers: The working demo can be found here The code can be easily downloaded Now when I say validation, I mean the validation plugin. and this example is built using [...]]]></description>
			<content:encoded><![CDATA[<p>So this one is done <a href="http://byatool.com/lessons/jquery-validator-adding-a-custom-method/#comment-143411747">on request</a> and because I am a kind of merciful god I will grant this request.</p>
<p>For those bullet point programmers:</p>
<ul>
<li>The working demo can be <a href="http://demo.byatool.com/CheckBoxValidation/checkboxValidation.html">found here</a></li>
<li>The code can be  <a href="http://demo.byatool.com/checkBoxValidation.zip">easily downloaded</a></li>
</ul>
<p>Now when I say validation, I mean the <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">validation plugin.</a> and this example is built using <a href="http://byatool.com/lessons/jquery-validator-adding-a-custom-method/">concepts in this post.</a></p>
<p>Here&#8217;s the markup:</p>
<pre>
&lt;<span style="color: #003366;">form</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"formCheckBoxValidation"</span> <span style="color: #ff0000;">method</span>=<span style="color: #800080;">"post"</span> <span style="color: #ff0000;">action</span>=<span style="color: #800080;">"checkboxValidation.html"</span>&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"checkbox"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">name</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">value</span>=<span style="color: #800080;">"One"</span>&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"checkbox"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">name</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">value</span>=<span style="color: #800080;">"Two"</span>&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"checkbox"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">name</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">value</span>=<span style="color: #800080;">"Three"</span>&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"checkbox"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">name</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">value</span>=<span style="color: #800080;">"Four"</span>&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"checkbox"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">name</span>=<span style="color: #800080;">"checkBoxList"</span> <span style="color: #ff0000;">value</span>=<span style="color: #800080;">"Five"</span>&gt;
	&lt;<span style="color: #003366;">br</span> /&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"submit"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"buttonCheckBoxValidation"</span>&gt;
&lt;/<span style="color: #003366;">form</span>&gt;
&lt;<span style="color: #003366;">div</span>&gt;
	&lt;<span style="color: #003366;">div</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"divError"</span> name=<span style="color: #800080;">"divError"</span> style=<span style="color: #800080;">"display:none;"</span>&gt;&lt;/<span style="color: #003366;">div</span>&gt;
&lt;/<span style="color: #003366;">div</span>&gt;

&lt;<span style="color: #003366;">form</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"formCheckBoxValidationDifferentName"</span> <span style="color: #ff0000;">method</span>=<span style="color: #800080;">"post"</span> <span style="color: #ff0000;">action</span>=<span style="color: #800080;">"checkboxValidation.html"</span>&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"checkbox"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"checkBoxListOne"</span> <span style="color: #ff0000;">name</span>=<span style="color: #800080;">"checkBoxListOne"</span> <span style="color: #ff0000;">value</span>=<span style="color: #800080;">"One"</span>&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"checkbox"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"checkBoxListTwo"</span> <span style="color: #ff0000;">name</span>=<span style="color: #800080;">"checkBoxListTwo"</span> <span style="color: #ff0000;">value</span>=<span style="color: #800080;">"Two"</span>&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"checkbox"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"checkBoxListThree"</span> <span style="color: #ff0000;">name</span>=<span style="color: #800080;">"checkBoxListThree"</span> <span style="color: #ff0000;">value</span>=<span style="color: #800080;">"Three"</span>&gt;
	&lt;<span style="color: #003366;">br</span> /&gt;
	&lt;<span style="color: #003366;">input</span> <span style="color: #ff0000;">type</span>=<span style="color: #800080;">"submit"</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"buttonCheckBoxValidationDifferentName"</span>&gt;
&lt;/<span style="color: #003366;">form</span>&gt;
&lt;<span style="color: #003366;">div</span>&gt;
	&lt;<span style="color: #003366;">div</span> <span style="color: #ff0000;">id</span>=<span style="color: #800080;">"divErrorDifferentName"</span> <span style="color: #ff0000;">name</span>=<span style="color: #800080;">"divErrorDifferentName"</span> <span style="color: #ff0000;">style</span>=<span style="color: #800080;">"display:none;"</span>&gt;&lt;/<span style="color: #003366;">div</span>&gt;
&lt;/<span style="color: #003366;">div</span>&gt;
</pre>
<p>For this example I&#8217;m actually presenting two examples(Say &#8216;example&#8217; again, I dare you, I double dare you m&#8212;erf&#8212;er, say &#8216;example&#8217;Â one more God d&#8211;n time!): If the checkboxes are linked by id and if the checkboxes are not linked at all.  There is actually a difference.  You see, if they share the same id, the required method can be used since it will treat them all as the same element and therefore if even on is checked, there is a value.  Otherwise, you have to check all the checkboxes to see if at least one is checked. (Yeah, that made total sense.)</p>
<p>Here&#8217;s the check checkboxes method:</p>
<pre
<span style="color: #008000;">//Add a method to the validator that checks to see if at least one of the
//  three checkboxes specified are checked.</span>
jQuery.validator.addMethod(<span style="color: #800000;">'atLeastOneChecked'</span>, <span style="color: #0000ff;">function</span>(value, element) {
	<span style="color: #0000ff;">var</span> checkedCount = 0;

	if (jQuery(<span style="color: #800000;">'#checkBoxListOne'</span>).is(<span style="color: #800000;">':checked'</span>)){
		checkedCount += 1;
	}

	if (jQuery(<span style="color: #800000;">'#checkBoxListTwo'</span>).is(<span style="color: #800000;">':checked'</span>)){
		checkedCount += 1;
	}

	if (jQuery(<span style="color: #800000;">'#checkBoxListThree'</span>).is(<span style="color: #800000;">':checked'</span>)){
		checkedCount += 1;
	}

	return checkedCount &gt; 0;
});
</pre>
<p>Oooooh baby.</p>
<p>Now for setting up the validation:</p>
<pre>
<span style="color: #008000;">//This is for the first checkbox area where every checkbox has the same id causing them
//  to be a group and therefore the default required works.</span>
<span style="color: #0000ff;">var</span> validationRules = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Object</span>();
validationRules[<span style="color: #800000;">'checkBoxList'</span>] = {required:<span style="color: #0000ff;">true</span>};

<span style="color: #0000ff;">var</span> validationMessages = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Object</span>();
validationMessages[<span style="color: #800000;">'checkBoxList'</span>] = {required:<span style="color: #800000;">'at least one has be checked.'</span>};

jQuery(<span style="color: #800000;">'#formCheckBoxValidation'</span>).validate({
	errorLabelContainer: <span style="color: #800000;">'#divError'</span>,
	wrapper: <span style="color: #800000;">'div'</span>,
	rules: validationRules,
	messages: validationMessages,
	onfocusout: <span style="color: #0000ff;">false</span>,
	onkeyup: <span style="color: #0000ff;">false</span>,
	submitHandler: <span style="color: #0000ff;">function</span> (label) {
		alert(<span style="color: #800000;">'hooray')</span>;
	}
});

<span style="color: #008000;">//This is for the second checkbox area where every checkbox has a different name
//  and therefore the atLeastOneChecked method must be used.</span><span style="color: #008000;">
</span>validationRules = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Object</span>();
validationRules[<span style="color: #800000;">'checkBoxListOne'</span>] = {atLeastOneChecked:true};

validationMessages = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Object</span>();
validationMessages[<span style="color: #800000;">'checkBoxListOne'</span>] = {atLeastOneChecked:<span style="color: #800000;">'at least one has be checked.'</span>};

jQuery(<span style="color: #800000;">'#formCheckBoxValidationDifferentName'</span>).validate({
	errorLabelContainer: <span style="color: #800000;">'#divErrorDifferentName'</span>,
	wrapper: <span style="color: #800000;">'div'</span>,
	rules: validationRules,
	messages: validationMessages,
	onfocusout: <span style="color: #0000ff;">false</span>,
	onkeyup: <span style="color: #0000ff;">false</span>,
	submitHandler: <span style="color: #0000ff;">function</span> (label) {
		alert(<span style="color: #800000;">'hooray'</span>);
	}
});
</pre>
<p>In the words of Q-Bert: @#$% yeah!</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/jquery-validation-with-multiple-checkboxes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SqlAlchemy: Self Referential Many To Many</title>
		<link>http://byatool.com/lessons/sqlalchemy-self-referential-many-to-many/</link>
		<comments>http://byatool.com/lessons/sqlalchemy-self-referential-many-to-many/#comments</comments>
		<pubDate>Sun, 06 Feb 2011 16:46:04 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[sqlalchemy]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1933</guid>
		<description><![CDATA[Ok this was just plain annoying to figure out. A couple of misteps and a few F&#8212;! later I finally got it. So the situation is this: You have a table for user to ignore users. So it&#8217;s basically a many to many where both sides of the relationship are the user table. The creation [...]]]></description>
			<content:encoded><![CDATA[<p>Ok this was just plain annoying to figure out.  A couple of misteps and a few F&#8212;! later I finally got it.  So the situation is this:</p>
<p>You have a table for user to ignore users.  So it&#8217;s basically a many to many where both sides of the relationship are the user table.  The creation might look like this:</p>
<pre><span style="color: #0000ff;">class</span> User(Base):
    __tablename__ = <span style="color: #800000;">"User"</span>
    id = <span style="color: #008080;">Column</span>(<span style="color: #008080;">String</span>(36), primary_key=<span style="color: #0000ff;">True</span>, default=<span style="color: #0000ff;">lambda</span> : str(uuid1()))</pre>
<p>Basically a table named User and it has a primary key.  Yee haw.  Now for the hanging table:</p>
<pre><span style="color: #0000ff;">class</span> UserIgnore(Base):
    __tablename__ = <span style="color: #800000;">"UserIgnore"</span>
    id = <span style="color: #008080;">Column</span>(<span style="color: #008080;">String</span>(36), primary_key=<span style="color: #0000ff;">True</span>, default=<span style="color: #0000ff;">lambda</span> : str(uuid1()))

    ignored_by_id = Column(<span style="color: #800000;">"ignored_by_id"</span>, <span style="color: #008080;">String</span>(36), ForeignKey(<span style="color: #800000;">"User.id"</span>))
    ignored_by = relationship(<span style="color: #800000;">"User"</span>, backref=<span style="color: #800000;">"ignored_list"</span>,  primaryjoin=(<span style="color: #008080;">User</span>.id == ignored_by_id))
    ignored_id = Column(<span style="color: #800000;">"ignored_id"</span>, <span style="color: #008080;">String</span>(36), ForeignKey(<span style="color: #800000;">"User.id"</span>))
    ignored = relationship(<span style="color: #800000;">"User"</span>, backref=<span style="color: #800000;">"ignored_by_list"</span>,  primaryjoin=(<span style="color: #008080;">User</span>.id == ignored_id))</pre>
<p>Basically you need a table/object with two columns:  ignored_by_id (The one doing the ignoring) and ignored_id (The one being ignored).  And at this point the word &#8220;ignored&#8221; should look like it&#8217;s not even a word anymore because I&#8217;ve typed it too much already.</p>
<p>Now the relationship is made using two different properties:</p>
<pre>    ...
    ignored_by = relationship(<span style="color: #800000;">"User"</span>, backref=<span style="color: #800000;">"ignored_list"</span>,  primaryjoin=(<span style="color: #008080;">User</span>.id == ignored_by_id))
    ...
    ignored = relationship(<span style="color: #800000;">"User"</span>, backref=<span style="color: #800000;">"ignored_by_list"</span>,  primaryjoin=(<span style="color: #008080;">User</span>.id == ignored_id))</pre>
<p>As you can see, I had to hit it from both angles. (I&#8217;d hit it! LOLOLOLHARHARORAALROARH)  Not only how to describe the relationship from ignorer to ignoree (Those are words now) but also in reverse.  That way I can have a constructor like this:</p>
<pre>    <span style="color: #0000ff;">def</span> __init__(self, user, userToIgnore):
        self.ignored_by = user
        self.ignored = userToIgnore</pre>
<p>And also if I do something like this:</p>
<pre>    <span style="color: #008080;">UserIgnore</span>(_user, _user2)
    <span style="color: #008080;">UserIgnore</span>(_user2, _user)</pre>
<p>I can have it all save correctly just by adding _user to the session or saving _user id at some point.</p>
<p>Also both those lists will be added to the User object without having to do anything further.</p>
<pre>someUser.ignored_list
</pre>
<p>or</p>
<pre>someUser.ignored_by_list
</pre>
<p>Yay for that.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/sqlalchemy-self-referential-many-to-many/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML Thing&#8230; Label versus Div or Span When it Comes to Inputs</title>
		<link>http://byatool.com/lessons/html-thing-label-versus-div-or-span-when-it-comes-to-inputs/</link>
		<comments>http://byatool.com/lessons/html-thing-label-versus-div-or-span-when-it-comes-to-inputs/#comments</comments>
		<pubDate>Sat, 29 Jan 2011 02:51:46 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1917</guid>
		<description><![CDATA[One of the things that PyCharm tells me over and over again when it comes to HTML and Inputs is that I&#8217;m just not using those &#8212;&#8212;- &#8212; labels you &#8212;- &#8212;&#8212;- excuse for a &#8212;&#8212;- programmer. (Ok, so I might be paraphrasing a bit) Now for the most part it isn&#8217;t the end of [...]]]></description>
			<content:encoded><![CDATA[<p>One of the things that <a href="http://www.jetbrains.com/pycharm/">PyCharm</a> tells me over and over again when it comes to HTML and Inputs is that I&#8217;m just not using those &#8212;&#8212;- &#8212; labels you &#8212;- &#8212;&#8212;- excuse for a &#8212;&#8212;- programmer.  (Ok, so I might be paraphrasing a bit) Now for the most part it isn&#8217;t the end of days if you use a span or div instead.  No kittens will be harmed either way.  However, and I admit I must be slow for not seeing this, but labels do have one advantage as told by the <a href="http://www.w3schools.com/tags/tag_label.asp">dub see three</a>:</p>
<pre>
The label element does not render as anything special for the user. However, it provides a usability improvement for mouse users, because if the user clicks on the text within the label element, it toggles the control.
</pre>
<p>Quite simple put, if the user clicks the label or the input, it will put focus on the input.  Yeah so not earth shattering but for people with stupid hands like me, it just gives me a better chance of actually clicking a textbox than the massive <a href="http://www.codinghorror.com/blog/2009/07/how-not-to-advertise-on-the-internet.html">Evony Online ad</a> right next to it.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/html-thing-label-versus-div-or-span-when-it-comes-to-inputs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Validator: Build Rules and Messages Dynamically</title>
		<link>http://byatool.com/lessons/jquery-validator-build-rules-and-messages-dynamically/</link>
		<comments>http://byatool.com/lessons/jquery-validator-build-rules-and-messages-dynamically/#comments</comments>
		<pubDate>Fri, 28 Jan 2011 15:20:31 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jQuery Validator]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1907</guid>
		<description><![CDATA[Ok so here&#8217;s your typical code for the jQuery Validator: jQuery(ELEMENT_FORM).validate({ errorLabelContainer: ELEMENT_ERROR_DIV, wrapper: 'div', rules: { textboxEmailAddress: { required: true, email: true } }, messages: { textboxEmailAddress: { required: ERROR_EMAIL_ADDRESS_REQUIRED } }, .... As you can see, the elements like &#8220;textboxEmailAddress&#8221; are hard coded in there which means if that name changes in the [...]]]></description>
			<content:encoded><![CDATA[<p>Ok so here&#8217;s your typical code for the <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">jQuery Validator</a>:</p>
<pre>  jQuery(ELEMENT_FORM).validate({
    errorLabelContainer: ELEMENT_ERROR_DIV,
    wrapper: <span style="color: #800000;">'div'</span>,
    rules: {
      textboxEmailAddress: {
        required: true,
        email: true
      }
    },
    messages: {
      textboxEmailAddress: {
        required: ERROR_EMAIL_ADDRESS_REQUIRED
      }
    },
    ....</pre>
<p>As you can see, the elements like &#8220;textboxEmailAddress&#8221; are hard coded in there which means if that name changes in the markup, this breaks.  So the question is: How do I script the rules and messages so that I don&#8217;t have to hard code the element names?</p>
<p>Sorry was just waiting for you to finish you question.  Ok, here&#8217;s the answer and it has to do with how great Javascript is as a language.  (And oddly enough a trick I got from Python)</p>
<pre>  <span style="color: #0000ff;">var</span> validationRules = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Object</span>();
  validationRules[ELEMENT_TEXTBOX_EMAIL] = {required:true};

  <span style="color: #0000ff;">var</span> validationMessages = <span style="color: #0000ff;">new</span> <span style="color: #008080;">Object</span>();
  validationMessages[ELEMENT_TEXTBOX_EMAIL] = {required:ERROR_EMAIL_REQUIRED};

  jQuery(ELEMENT_FORM).validate({
            errorLabelContainer: ELEMENT_ERROR_DIV,
            wrapper: <span style="color: #800000;">'div'</span>,
            rules: validationRules,
            messages: validationMessages,
            ....</pre>
<p>Where ELEMENT_TEXTBOX_EMAIL is a constant somewhere.  How does this work?  Well because the way dynamic languages like Javascript and Python work, objects can be treated like dictionaries which means you can add properties to them the way you would add a record to a dictionary:</p>
<pre>  someObject[<span style="color: #800000;">'somePropertyName'</span>] = <span style="color: #800000;">'hi'</span>;</pre>
<p>is just like:</p>
<pre>  someObject.somePropertyName = <span style="color: #800000;">'hi'</span>;</pre>
<p>In fact I&#8217;m pretty sure the <a href="http://www.hanselman.com/blog/C4AndTheDynamicKeywordWhirlwindTourAroundNET4AndVisualStudio2010Beta1.aspx"> new Dynamic keyword</a> in C# works in the same manor.</p>
<p>Now you could be a annoying and point out that replacing textboxEmailAddress with ELEMENT_TEXTBOX_EMAIL is still hard coding, and I would agree&#8230; to a point.  The constant does two things:</p>
<p>1) Removes a &#8220;magic string&#8221; from your code.</p>
<p>2) Makes it really easy to update that string if the element name/id is changed in the HTML.  Having only to look at one place to replace is a lot easier than 1+ places.</p>
<p>So there, smarty pants.  What you gots to say now?</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/jquery-validator-build-rules-and-messages-dynamically/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Property Merge for Python Objects</title>
		<link>http://byatool.com/lessons/simple-property-merge-for-python-objects/</link>
		<comments>http://byatool.com/lessons/simple-property-merge-for-python-objects/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 18:57:11 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1904</guid>
		<description><![CDATA[Not sure if this is useful to other people, but then again if I cared that would make me human. So here&#8217;s a quick way to merge two objects in python: def mergeObjectProperties(objectToMergeFrom, objectToMergeTo): """ Used to copy properties from one object to another if there isn't a naming conflict; """ for property in objectToMergeFrom.__dict__: [...]]]></description>
			<content:encoded><![CDATA[<p>Not sure if this is useful to other people, but then again if I cared that would make me human.</p>
<p>So here&#8217;s a quick way to merge two objects in python:</p>
<pre><span style="color: #0000ff;">def</span> mergeObjectProperties(objectToMergeFrom, objectToMergeTo):
    """
    Used to copy properties from one object to another if there isn't a naming conflict;
    """
    <span style="color: #0000ff;">for</span> property <span style="color: #0000ff;">in</span> objectToMergeFrom.__dict__:
        <span style="color: #008000;">#Check to make sure it can't be called... ie a method.</span>
        <span style="color: #008000;">#Also make sure the objectobjectToMergeTo doesn't have a property of the same name.</span>
        <span style="color: #0000ff;">if</span> <span style="color: #0000ff;">not</span> callable(objectToMergeFrom.__dict__[property]) <span style="color: #0000ff;">and</span> <span style="color: #0000ff;">not</span> hasattr(objectToMergeTo, property):
            setattr(objectToMergeTo, property, getattr(objectToMergeFrom, property))

    <span style="color: #0000ff;">return</span> objectToMergeTo</pre>
<p>This is a good example of how the whole dynamic addition of properties happens in Python.  The objects in this example have a __dict__ property that is basically where the property/method/other stuff information is held in dictionary form.  What does this mean?  It means that if you use the setattr method you can then later call the property like you would normally.</p>
<pre>  setattr(someObject, <span style="color: #800000;">'someProperty'</span>, <span style="color: #800000;">'some value'</span>)

  someObject.someProperty == 'some value</pre>
<p>Knowing this is important as it allows you to really do some fun dynamic stuff that a lot of other languages just don&#8217;t allow.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/simple-property-merge-for-python-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Validator: Adding a Custom Method</title>
		<link>http://byatool.com/lessons/jquery-validator-adding-a-custom-method/</link>
		<comments>http://byatool.com/lessons/jquery-validator-adding-a-custom-method/#comments</comments>
		<pubDate>Sat, 15 Jan 2011 18:09:28 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Validator]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1879</guid>
		<description><![CDATA[File this one under &#8220;Posted to take less time to find the answer&#8221;: If you&#8217;re using jQuery Validator and the built in validation methods just aren&#8217;t cutting, there&#8217;s a way you can add your own method to the validator itself. For this example let&#8217;s start some pretend time. Uhg I said pretend time not f&#8211;ked [...]]]></description>
			<content:encoded><![CDATA[<p>File this one under  &#8220;Posted to take less time to find the answer&#8221;:</p>
<p>If you&#8217;re using jQuery Validator and the built in validation methods just aren&#8217;t cutting, there&#8217;s a way you can add your own method to the validator itself.  For this example let&#8217;s start some pretend time.  Uhg I said pretend time not f&#8211;ked up fantasy time&#8230; Really, a horse?  Really?</p>
<p>Now that we have that clear: Let&#8217;s pretend you have a form with three textboxes and you want to make sure one and only one is filled out.  This doesn&#8217;t exactly fit the built in methods.  Now you could try using the &#8220;required&#8221; method and replace it with a delegate.  Could do that, but it &#8216;sÂ  actually trickier than it sounds due to how required method actually works.Â   So what&#8217;s an easier way?  Just add a method to the validator:</p>
<pre>    jQuery.validator.addMethod(<span style="color: #ff0000;">'correctCountFilled'</span>, <span style="color: #0000ff;">function</span>(value, element) {
        <span style="color: #0000ff;">var</span> fullCount = 0;
        <span style="color: #0000ff;">if</span>(jQuery(<span style="color: #ff0000;">'#someTextBox1'</span>).val().length &gt; 0){
            fullCount +=1;
        }

        <span style="color: #0000ff;">if</span>(jQuery(<span style="color: #ff0000;">'#someTextBox2'</span>).val().length &gt; 0){
            fullCount +=1;
        }

        <span style="color: #0000ff;">if</span>(jQuery(<span style="color: #ff0000;">'#someTextBox3'</span>).val().length &gt; 0){
            fullCount +=1;
        }

        <span style="color: #0000ff;">return</span> fullCount == 1;
    });
</pre>
<p>Then the call is done lika dis:</p>
<pre>  ...

  rules: {
            someTextBox1: {
                correctCountFilled:  <span style="color: #0000ff;">true </span> <span style="color: #339966;">//This is the name of the method added and what it expects to come back when called to be valid.</span>
            }
        },
  messages: {
            someTextBox1: {
                correctCountFilled: <span style="color: #ff0000;">'Some error message like pointing out how yours is a superior intellect.'</span>
            }
        },
  ...
</pre>
<p>As you can see, the method returns false if all are empty or more than one is filled in.  The method call on the validator expects &#8220;true&#8221; to be valid, so anything but one textbox being filled in will trigger the error message.  Fairly easy, huh?  Now go forth, be fruitful, and don&#8217;t multiply by mistake.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/jquery-validator-adding-a-custom-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python, Pyramid, and Mako&#8230; How to get Mako Working With Pyramid&#8230;</title>
		<link>http://byatool.com/lessons/python-pyramid-and-mako-how-to-get-mako-working-with-pyramid/</link>
		<comments>http://byatool.com/lessons/python-pyramid-and-mako-how-to-get-mako-working-with-pyramid/#comments</comments>
		<pubDate>Thu, 06 Jan 2011 15:25:29 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Lessons]]></category>
		<category><![CDATA[Mako]]></category>
		<category><![CDATA[Pyramid]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=1865</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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.</p>
<p><span style="color: #ff0000;">ANSWER:</span></p>
<p>In the development.ini file, place this under the [app:pytrends] area</p>
<pre>mako.directories=projectName:folderWhereYourTemplatesAre</pre>
<p>For example, the project I have is pyzazz (Don&#8217;t ask me what that is cause I don&#8217;t even know) and the template folder is&#8230; well templates.  So I have it as:</p>
<pre>mako.directories=pyzazz:templates</pre>
<p><span style="color: #ff0000;">MORE USELESS WORDS:</span></p>
<p>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:</p>
<pre> <span style="color: #0000ff;">if</span> (urlParams.actual &amp;&amp; urlParams.actual == <span style="color: #800000;">'true'</span>)</pre>
<p>and it forced me to end an input with the &lt;/input&gt; 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&#8217;s just html and javascript, the runts in the litter as far as programmers care, but you still can&#8217;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.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/lessons/python-pyramid-and-mako-how-to-get-mako-working-with-pyramid/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

