John Gagliardi

Python, ColdFusion, Flex, jQuery, and any other web technologies. Some Real Estate and Cooking sprinkled in also :)
Tweet to @johnnygag Follow @johnnygag

jQuery $.when()

jQuery

Ever need to know when a group of ajax calls are complete? jQuery $.when() is the answer. Wish I knew about this a long time ago.

http://api.jquery.com/jQuery.when/


Permissions/Flags using Bitwise Operator BitAnd

 

Bitwise Operator

Most all applications have some sort of permission levels or flags. There are many ways to accomplish permissions/flags and one that I like to use recently has been storing numbers in a single column and doing bitwise operation comparisons to figure out if the permission/flag exists.

If you have never heard of bitwise operators before I think you should probably take a look at the following links before proceeding.

http://en.wikipedia.org/wiki/Bitwise_operation

http://wiki.python.org/moin/BitwiseOperators

Basically a bitand (&) operation does a comparison on equal bit patterns lengths.

0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1

Example:

10101
00101
00101 (Passed)

10101
00110
00100 (Failed)

Instead of dealing with bit combinations you can use the decimal representation. If you insist on learning more I would check out the following link. Good luck staying awake.

http://en.wikipedia.org/wiki/Binary_numeral_system

In this very simple example I take a dictionary of permission levels and look up the a permission level based on a binary (base 2) number stored. 

2^0=1
2^1=2
2^2=4
2^3=8

etc

Below is a simple example of a dictionary:

https://gist.github.com/johngag/5341955

So with this example dictionary if you wanted to grant addUsers (1) and createPosts (8) the number would be 1 + 8 = 9.  Next all you have to do is use python bitand operator (&) to check if a specific permission exists within the number. Below is an example:

https://gist.github.com/johngag/5349715

This just touches the surfice of bitwise operators but it has been really handy for dealing with permissions and flags as you only store 1 value. There are many ways to accomplish all problems and this may not be the best for your situation but has been great in certain situations for me. :)


Two Scoops of Django: Best Practices for Django 1.5

Two Scoops of Django: Best Practices for Django 1.5

I would highly recommend this pdf book to any Django developers especially those who are starting out. It has a lot of good best practices and gotchas that most all Django project experience. Get it!!!

 

https://django.2scoops.org/


jQuery .append() vs .html() vs innterHTML list performance

Large list creation performance can vary greatly depending on which tool is used. A common mistake that I see is the use of jQuery .append() inside of a loop. When dealing with small lists you may not see a huge difference but once you start rendering large lists innerHTML will show very solid gains.

As you can see in the graph below innerHTML is much faster than the jQuery .append() approach and even solid gains on jQuery .html(). I have included the specific results for the most popular browsers.

What is really interesting is that IE9 jQuery .html() is way slower than innerHTML and even slower than jQuery .append(). IE8 doesnt show those same results.

Here is the link to the javascript performance test if you would like to test it for yourself. I will be investigating other ways to speed up lists with little changes like this.

http://jsperf.com/jquery-append-vs-html-list-performance/2

jQuery List Graph Results

Chrome Results

Firefox Results

IE8 Results

IE9 Results

Safari Results


jQuery starts-with and ends-with

I found this on stackoverflow today. If you ever need to query text similar to a LIKE sql statement.

https://gist.github.com/4209311

 

Here is the link to the jsFiddle that someone made. 

http://jsfiddle.net/h6KYk/


Python/Django - Keep your pip install requirements up to date with Fabric

Keeping your pip installs in order on development and production can be a pain. A way to handle it is by using a .txt file with a list of required pip installs and version number. Here is an example .txt file.

https://gist.github.com/4192058

Then using the magic of fabric for deployment you simple have to call your normal deploy function and make sure to have the newly created update_requirements function being called.  

https://gist.github.com/4192053


Django - Access ManyToMany objects in view Template

Here is a quick post on how to create ManyToMany relationships in Django and access the objects in view templates. 

First setup your models and add a ManyToMany relationship. Then simple call ModelName.objects.filter which will select your model and manytomany objects associated.

https://gist.github.com/4184578

Then in your view template while looping through your objects you can simple loop through the manytomany objects that were selected. In this case post.category.all represents the manytomany categories associated to the Post object.

https://gist.github.com/4184550

Short but sweet. Really liking the baked in ORM support in Django.


Rails for Zombies

Rails for zombies

For a quick introduction to rails I recommend zipping through the 8 chapters :)

 

http://railsforzombies.org/ 


Ruby on Rails Render Partial

With Rails getting a lot of love recently I wanted to give it a shot.  One of the first cool things I have found is called render partial.  Here are a couple code snippets of what I learned. I am assuming you know a little bit about rails in the first place.

First I made a home controller with a index function.  Then I parsed my tumblr rss feed and set a variable called “posts”. 

After that I made a partial file called _post.html.erb which outputs the post title and links to the post.

Finally in my index.html.erb view I output the rails magic. In one line of code rails loops through all the posts (or in my case I told it to loop over first 5) and output the posts according to the partial created.  This is very DRY (Dont Repeat Yourself) and I love writing less code.

I am very happy so far with how easy and magical rails has been. I cant wait to dive into writing less code to do more. 

https://gist.github.com/4179345


ColdFusion varScoper

I know this is kinda old but if you haven’t used ColdFusion varScoper then you should give it a try. It will ensure that all your variables are properly scoped. Make sure to note that if will catch variables that are commented out and also any argument variables that are not preceded with “argument” will give a false positive.


Git Rebase Workflow

I found this post useful when learning about different Git workflows.

http://mettadore.com/analysis/a-simple-git-rebase-workflow-explained/