Copyright © 2012 Tom Coote. All Rights Reserved. Powered by WordPress themed by bavotasan.com.
Author Archive
I have updated my previous auto suggest box plugin for jQuery. It has been a long time coming, the improvements are a combination of feedback received by others and things I have wanted to do for a while myself.
The main points of improvement are;
- Updated for jQuery v1.4.x
- Support for jQuery themes. Takes on the same styling from themes for jQuery’s own autocomplete ui component.
- Better support for being able to use data requested from a URL.
- Improvements in the UI for navigating the results set.
jQuery have their own autocomplete ui widget which I used for a few projects to see if it suited me better than my own. I do really like jQuery’s autocomplete widget as it pretty much does everything that mine does! The only thing extra with mine are a few added options, images that can more easily be displayed in each result and separated markup for extra, non searched, text in each result. For me that was enough to do a few upgrades to my own plugin to bring it more up to date.
So to get the best of both worlds I have updated my plugin to an all new version 2. There are some differences in the setup of the plugin so go check out the demo page to see examples. Most often it will come down to these two choices;
$(‘input#suggestBox’).jsonSuggest({data: jsonData});
or
$(‘input#suggestBox’).jsonSuggest({url: ‘http://mysite.com/get_some_data.json’});
Here are all the options when setting up the plugin;
Name
Default
Description
url
”
A URL that will return a JSON response. Called via $.getJSON, it is passed a data dictionary containing the user typed search phrase. It must return a JSON string that represents the array of results to display.
data
[]
An array or JSON string representation of an array of data to search through. See Examples.
minCharacters
1
Number of characters that the input should accept before running a search.
maxResults
undefined
If set then no more results than this number will be found.
wildCard
”
A character to be used as a match all wildcard when searching. Leaving empty will mean results are matched inside strings but if a wildCard is present then results are matched from the beginning of strings.
caseSensitive
false
True if the filter search’s are to be case sensitive.
notCharacter
!
The character to use at the start of any search text to specify that the results should NOT contain the following text.
maxHeight
350
This is the maximum height that the results box can reach before scroll bars are shown instead of getting taller.
width
undefined
If set this will become the width of the results box else the box will be the same width as the input.
highlightMatches
true
This will add strong tags around the text that matches the search text in each result.
onSelect
undefined
Function that gets called once a result has been selected, gets passed in the object version of the result as specified in the JSON data.
Download the source
Visit plugin page at jQuery
Demo page
Tested to work in the following browsers;
- IE8, IE 7 & 6
- FireFox
- Opera
- Chrome
- Safari Continue Reading »
Have you used Goggles? If not then go try it, have a lot of fun, then come back to read this blog post when you’re finished.
Goggles is great fun but good intentions aside the thing attracts a lot of abuse. For example, visit some of the popular sites on the internet then switch on Goggles and you’ll soon see the hatred that people express while safely masked away.
Here is a script that you can happily place on your web sites to block Goggles if you happen to manage a web site that might attract such abuse.
(function() {
window.setInterval(function() {
if (typeof activateGoggles === ‘function’) {
activateGoggles = undefined;
location.reload();
}
}, 500);
}());
I’ve been programming in C# on a daily bases for 5′ish years. Over the past 2 years I’ve also been programming heavily in Python. I use both languages for the same thing which is to build web applications and web sites. The only thing that made me learn Python was Django because I wanted a framework to help build web apps and there is no framework in C# that comes close to the usefulness and flexibility of Django.
I don’t dislike C#, I think it’s a very powerful language but having now worked extensively in Python I can safely say I prefer Python for the purpose of building web applications. Here is why.
Ways to do things
When you want to get a job done in Python there always seems to be just one obvious way of doing it. In contrast, if you use C# then you’re faced with a varying amount of choices to achieve the same goal. Choice is good right? No! It’s difficult to read code, especially someone else’s. It’s made even more difficult in C# when you’re faced with the variety of data types and syntax that could have been used to achieve the same job. It’s so much easier when your preconception as to roughly how something would have been achieved is more often than not an accurate one. I’m not saying that there isn’t variety in Python, I’m saying that the whole language seems to be designed, presented and documented in such a way that more often than not results in the same choices being made when producing a solution to a problem.
Syntax
Syntax is the next logical thing to talk about. If you’re learning Python and doing a lot of reading around the subject you may well get fed up of reading about how Python syntax is designed to be readable, as if English phrases have been used to describe code. Well it’s true! As well as this, I have been astonished at times at how little syntax is needed to achieve something in Python than it would have in C#. It’s also about elegance, readability and making the job easier to achieve. For example:
Python:
if ‘e’ in ‘hello’:
return True
C#:
if (“hello”.indexOf(“e”) >= 0)
{
return true;
}
Referencing
I’ve never enjoyed how C# handles it’s references to external libraries of code. I don’t think there is a C# programmer out there who can’t recall a time when they have wasted a whole day just figuring out referencing problems in a C# solution. Again, I think the problem is the variety of options. You can make references inside the applications config file or at a project level using visual studio. Each reference can be to a DLL on the file system, another project in the solution, a library that is located in the global assembly cache or a COM object (I don’t know where COM objects exist?). It doesn’t end there, once a reference has been established, your code file needs a using statement to say what namespace from your reference is being used in the code. There is so much opportunity for things to go wrong here that there is no wonder it can get very messy, very easily. So what’s so great about Python referencing? It’s the fact that there is one way to do it and mistakes are obvious. You say what external code you wish to use in your own code file. That code is then found from the file system, either checking in locations relative to your code or checking in locations on your Python path.
Functions
I enjoy functional programming, it’s why I like JavaScript so much. Pythons use of functions is the best I’ve ever seen. If you’re into the DRY principle, and you should be, then you will know that it is often the case that one piece of code can be used again and again by either changing the input or the output to the same code. So to highlight one way of achieving this with C# you have function overloading which works well and looks like this:
public int MyFunction(int x, int y)
{
return x + y;
}
public int MyFunction(int x)
{
return MyFunction(x, 5);
}
So in this example you have a function that can accept two different inputs but the same bit of code is being used to do the actual work. Here is the Python version:
def MyFunction(x, y=5)
return x + y
I’m not going to get into explaining syntax but I can safely say that the Python solution is more elegant, more easy to read and less typing. It’s examples like this throughout Python that continually reinforce my preference towards the language.
Python dynamic types vs C# static types
Okay, so this is an old argument and I don’t subscribe to the thinking that one is better than the other. However, I do have the thinking that one type system can better suite a programming need than the other. When it comes to a web based environment I strongly believe that dynamic types are the best choice. For those of you that are using the most recent release of C# you will know they have started to bring in dynamic types. However, this is yet again adding to the set of features that make the language so difficult to use in the first place, more choices more confusion. The web is dynamic and data that is passed between the layers that make up a web application is handled differently at each layer. From the DOM to JavaScript, then from JavaScript to the HTTP and from there to your programming language of choice. That data has been traversed through all those layers but if C# is your programming language then you’re in for a lot more work to accept that data because it needs to fit into predefined data types. So pick the right language for the job.
Introspection & Reflection
I’ve used these concepts a lot in C# and it’s just difficult. I think it’s inherently a bit more difficult given the extra need to maintain the static types in C#. Once you get into introspection of Python code you soon realise how easy it is in comparison. The ability to manipulate code at runtime in Python is built into the language where as in C# it feels very separated. Like here is C# and over here are all the things you’ll need for introspection, reflection is a bit easier. Added to this, I think all the things that I have mentioned so far all come together here and make introspection & reflection in C# mega difficult. I just don’t want to have to spend the time wrestling with it when I can choose Python.
Conclusion
The list of comparisons between C# and Python is vast and those points I’ve discussed above have only been briefly described. But the reason I choose Python is because I think it’s about choosing the right tools for the job. I do the things I’ve mentioned in this post on a daily basis and Python is the language that makes it easy. I’m not involved in desktop applications, operating systems, game development… etc etc. I build web apps and my language of choice for doing that is Python. C# is more than capable of building extremely good web apps but I can assure you the developers have had a harder time achieving that than if they were to have used Python. I know that because I’ve spent years doing both.
Continue Reading »Have you ever looked at the robots.txt file of a web site? They exist so that web sites can stop search engines and other similar robots from indexing web pages on a web site. However they are often used to hide web pages on live web sites that the web site owners don’t want people to see. This is a bad way of using robots.txt but never the less it happens a lot.
What this shows for anyone who feels like taking a look is a list of web page paths under a web site that are marked as disallowed. Those lines marked disallowed are like big red buttons with a sign above them saying “don’t press the big red button”. You’re going to take a look!
So recently Steve asked me to build a quick page that will find the robots.txt file of a web site and automagically create clickable links for all the paths in the file. This makes it really easy to go hunting on web sites for secret pages.
Have a go: robotsneak.com
EDIT: Steve and I have now turned the quick page into a fully designed, emotive, masterpiece
When Android came out I was excited, a mobile platform by an internet giant, hurray! It wasn’t just that Google developed it, the platform has amazing features and the fact that it is open source, to me, meant new features would come thick and fast and there would be choices. However my enthusiasm for the platform has gone from extremely enthusiastic to utter disappointment for the following reasons.
1. Software from the Android market place is never guaranteed to work with your handset.
2. The versions of Android are confusing and obtaining updates is even more confusing.
3. There seems to be a shed load of really crappy software available for the platform which you often realise after installing it.
4. In order to keep up with the platform you need to upgrade your handset every 6-8 months. (maybe this is true for just about every operating system out there)
I think there is one reason why I’ve had so much disappointment from Android and I’m afraid to say I think the reason was the decision to go open source. I’m a supporter of open source software and the philosophy behind it but I also feel that whilst it can be good for some types of software it can be a bad thing too. When it comes to Android I really feel that making it open source has damaged it. It has meant that any one has been able to build hardware for Android which of course seems great. It certainly seemed great to me because all of a sudden there is choice in mobile handsets. However in the end it has just meant that no one can develop software for Android that will run on all the different hardware builds, there is no control over the quality of software available to users and there is no clear upgrade path between versions. I really wish Google kept it their own, I wish they partnered up with a sole hardware supplier to distribute it and I wish they kept a close eye on third party software developed for it. In fact I still wish they would do that. Google is the software giant, they are the ones who know what they’re doing so why have they made it so easy for others to undo their good work.
I know what Google are doing, they want give to the world, they want there to be no limits on innovation from other parties and they want that innovation to be available to everyone. However, I feel that, as a user of Android, by taking that approach they have made it more difficult to give the end user a good experience and innovation has been hard to appreciate. For me it has become a sad irony.
I am annoyed that I feel so let down by Android. Mainly because I’ve been singing it’s praises for so long and now I feel that those praises are unjustified. I feel like Google have played it safe by opening up Android, I think the real risk would have been to tell everyone else to do it our way or not at all. After all, with big risks come big rewards. In fact I think that applies to all businesses, if you’re claiming to be the experts in something then start telling people what to do because if you’re as good as you’re claiming to be then surely they’ll thank you in the end?
As it happens I do think Google are as good as they say they are, I still worship them as a company. I just feel like they’ve screwed this one up.
Continue Reading »South, the schema and data migration tool for django is really useful. I’ve only started using it myself recently but the benefit is immediate. South creates python files which hold all the information for migrations each time you change the database schema. Each person in a team of developers needs to run these migrations on their development database to keep things up to date and working.
The question is how does one person in a team know that another has changed the schema? If time has passed then simply asking could get a somewhat hazy response.
We use SVN to keep track of code changes amongst the team of developers. SVN is a good place to look to see if migrations were committed. The only thing better than looking in SVN logs for migrations is for SVN to tell us when migrations are committed. So that is what I’ve had a go at doing.
Using the SVN pre-commit hook I’ve been able to send emails to those involved in a project each time someone commits code that contains new database migration changes. The first part of this was to create a pre-commit.bat file in the hooks directory of my projects repository.
C:\python26\python.exe D:\repositories\post_commit_emailer.py %* tom@email.com bob@email.com eddy@email.com
This batch file is run by SVN just before a commit happens. It is passed the repository and transaction name when called. As you can see, I’ve also appended some extra parameters which are the emails of everyone involved with this project.
So the next bit is the python script that this batch file calls.
import smtplib
import sys
import pysvn
import re
from datetime import datetime
SERVER = ‘localhost’
FROM = ’svn@email.co.uk’
SUBJECT = ‘SVN South Migrations Committed’
TEXT = \
”’
The repository location ‘%s’ was committed and included database schema migrations.
Next time you update your working copy for this repository be sure to run the new south migrations on your local database.
The changed files that are thought to be schema migrations are:
– %s
”’
try:
# what was committed?
repo = sys.argv
txn = sys.argv
transaction = pysvn.Transaction(repo, txn)
changes = transaction.changed()
# look for database migrations that may have been part of this commit
migrations = []
for f in changes.keys():
if re.search(r’migrations/.*\.py$’, f):
migrations.append(f)
if migrations:
# prepare the message. Emails to send to are passed as args
emails = sys.argv
message = TEXT % (repo, ‘\n – ‘.join(migrations))
message = “From: %s\nTo: %s\nSubject: %s\n\n%s” % \
(FROM, ‘, ‘.join(emails), SUBJECT, message)
# send the email
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, emails, message)
server.quit()
except Exception as e:
log = open(‘C:\\svn_emailer_log.txt’, ‘a’)
log.write(‘\n%s : %s’ % (datetime.now().isoformat(), str(e)))
log.close()
This script uses pysvn to grab all the changes that are to be committed in this transaction. Once it has that information it looks at the path of each file in the list of changes to find any in a migrations folder. South puts migration files in a folder named migrations. If it finds any then it composes an email to everyone in the parameter list. The email lists the migrations that were committed.
So now everyone involved in a project will get an email if someone commits database schema changes. I’m going to start using this for our django projects and see how it goes.
Continue Reading »Django has a great solution for web site permissions, it allows the developer to decide how a user can interact with an application based on permissions against models (i.e. database tables). By default a model comes with three permissions, add, change and delete. If these aren’t enough then you can create as many custom model based permissions as needed very easily. Infact, this easily:
This is great but what if your app doesn’t have a model. Without a model your app doesn’t get the three default permissions and you can’t create any custom permissions because you don’t have the model to base them upon. Well the solution is a simple one but maybe not an obvious one. Django holds two tables for it’s permissions, one called ContentType and one called Permission. You need to create a new ContentType entry for your app and then add as many entries to the Permission table as you need. The ContentType table has a column for the model name but it’s just fine to leave that blank.
The next question though is where to put the code to do this job? Ideally it needs to go in a place that is parsed when syncdb is run but not in a place that continually get’s called as it would have a negative impact on performance. Since the whole point of creating these permissions is because we aren’t using models, no other part of our app will ever try and import the models file. So the models file is the place to put our code. This file is parsed on syncdb but after that will never be called upon again.
So great, we have the method and we have the place to write the code. All that’s left is the code itself which is below. We’re creating one ContentType with the name of our app and then as many Permission’s as needed, in this case just one!
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.db.models.signals import post_save
def create_app_permissions(sender, **kwargs):
# create a content type for the app if it doesn’t already exist
ct, created = ContentType.objects.get_or_create(model = ”, app_label = ‘myappname’,
defaults = {‘name’: ‘myappname’})
# create a permission if it doesn’t already exist
Permission.objects.get_or_create(codename = ‘can_deliver_pizza’,
content_type__pk = ct.id,
defaults = {‘name’: ‘Can Deliver Pizza’, ‘content_type’: ct})
post_save.connect(create_app_permissions, Permission)
One last thing. This code uses signals to run once the Permission model has saved. This means the code won’t crash when using syncdb with complaints about the Permission or ContentType tables not existing because it won’t get run until django has created it’s permissions.
Continue Reading »
Have you ever needed to interrogate a web form post on the internet? Probably rarely if at all. However with the popularity of API’s and web services it can be very handy to know how to see the data that a post request is sending. There is a great little service that enables you to do this easily, postbin.org. Creating a postbin gives you a URL to use that accepts HTTP web requests. When you post a form to the URL and refresh the postbin page you get to see exactly what data was posted to the URL as a key value pair list. This can be very helpful if you’re trying to figure out what a web form is posting but you don’t have full control over the form, e.g. API webhooks or external services.
So I had a little play by posting some data from my computer to postbin using a short Python script. Check it out.
import urllib, urllib2
data = urllib.urlencode({ ‘name’: ‘Tom’, ‘age’: ‘25′ })
response = urllib2.urlopen(‘http://www.postbin.org/m5isph’, data)
Then by refreshing my postbin I get the following.
Use these powers for good
I’ve been reading about HTML 5 and the new JavaScript API’s for it just recently. I liked the new video stuff so thought I’d have a go at doing something with it. So as a bit of an experiment I have created a pretty simple video tagging app.
It allows you to click in the video to tag an area of the screen at a particular playback time. You can then click the list of tags to go see where they are in the video.
As I said, it’s just an experiment and at the moment only works in chrome, firefox and opera. It would probably also work in safari apart from I didn’t have that video encoded in H.264. Go have a play!
Continue Reading »I’ve been using Wingwares python IDE for Django development for almost 2 years. The Wingware IDE has been very good but I have had a few superficial annoyances with it so decided to look around and see what other IDE’s I could use. This led me to Eclipse. The first time I had a look around for Django IDE’s I remember looking at Eclipse but I think I recall difficulties when trying to run Django projects. Eclipse doesn’t natively support Django, you have to install Pydev to get that. After revisiting Eclipse and Pydev I can safely say I’ve switched from Wingware to Eclipse for Django development. The Eclipse IDE in itself is very good and full of useful development features. The Pydev plugin for it which allows python and Django development is also fantastic as it has a GUI for creating and managing Django specific python projects. Another great feature of Eclipse is it’s free price tag where as a single developer license for Wingware is $179 (roughly £115).
To get my Eclipse set up for Django I installed the following:
Then I read through and followed a few guides to understand Pydev and Django support:
In less than an hour I was up and running and fully understood how to start and develop Django projects in Eclipse. I’d even moved 3 existing Django projects into Eclipse in that time.
Sorry Wingware, you’ve been good to me but I’m trying Eclipse for a while and I don’t think I’m coming back.
Continue Reading »
Recent Comments