I am a programmer by heart and by trade. This is where I share my views and creations. I love Python, open source, music and tough challenges.
Thursday, 13 December 2012
A useful Django Template tag hidden
It seems to be really useful for debugging and creating those initial quick and dirty templates.
I often want to stay off my template code while programming view code. When I create templates, I make them minimal and just want visual confirmation of success and not creating a full template.
Thursday, 6 December 2012
Wildcards in python (the fnmatch module)
Filtering and matching against wildcard patterns is really easy in python, using the fnmatch module. I found this out while looking in an article by Dan Carrol for a solution to a Django problem of mine.
By using the fnmatch function, one can match strings against a case-insensitive pattern. Use fnmatchcase for case-sensitive matching).
>>> import fnmatch
>>> fnmatch.fnmatch('example', 'exampl*')
True
>>> fnmatch.fnmatch('example', '*e')
True
>>> fnmatch.fnmatch('example', '*es')
False
>>> fnmatch.fnmatch('examples', '*es')
True
There's also a filter function, to filter a list against a pattern.
>>> files = ['file.py', 'file.txt']
>>> fnmatch.filter(files, '*.py')
['file.py']
Besides *, ? and [] are also available. And that's it. It's a very simple syntax. A moderately powerful syntax which everybody can use.
>>> fnmatch.fnmatch('a', '?')
True
>>> fnmatch.fnmatch('1', '[13579]')
True
>>> fnmatch.fnmatch('4', '[13579]')
False
>>>
The wildcard characters cannot be escaped with slashes. You can only escape with square brackets. For example:
>>> fnmatch.fnmatch('Here is a star: *', '*\*')
False
>>> fnmatch.fnmatch('Here is a star: *', '* [*]')
True
At first I thought this way of escaping was impractical, but because of that, you can use unescaped user input without the user ever getting unexpected results.
And, if you want to get the original regex (for reusing later) you can always use translate:
>>> as_regex = fnmatch.translate('m[ae]tch th?is!')
>>> as_regex
'm[ae]tch\ th.is\!\Z(?ms)'
Because this module uses regular expressions internally and allows to get the actual regular expression, I can use it as a less error-prone re in some scenarios.
I think this module is great. It gives me a bit less matching power than regular expressions, but then I can empower the user by asking them what and how they want to search for. You could arguably do this with regular expressions, but you would end up wasting time and money in documentation and customer support because regex is error-prone and dangerous.
Check out the docs for more information on this module.
Saturday, 1 December 2012
Phasing subtitles using python
Not content with finding other subtitles on the web, I opened up the python interpreter and loaded the file into
lines.A little code followed
import datetime
import re
lines = open('subs.srt', 'rb').read().splitlines()
with open('out.srt', 'wb') as outp:
for line in lines:
if subtime.findall(line):
time = datetime.datetime(1,1,1,*map(int, line[:8].split(':')))
time += datetime.timedelta(seconds=1)
outp.write('%02d:%02d:%02d%s' % (
time.hour, time.minute, time.second, line[8:]))
else:
outp.write(line + ' ')
It was just a few lines of code, showing off quite well a lot of the capabilities of python I love most. Text processing is always a cinch.Explaining the code
The format of the subtitles was: [blank line]
ID
hh:mm:ss,ms: [text]
This explains why I had to check if the regex findall returned a match. The regex was ^\d\d:\d\d:\d\d.When this regex found a line with subtitle time written on it, I did the reading, updating and writing the time. Otherwise, I just copied the line verbatim to the output file.
I simply cut the line using slice syntax.
[:8] and [8:] got me the line's contents up to the seventh character, and from the eight character onwards, respectively.I used the first seven characters of the line, split by the colon
: character, as arguments to the datetime.datetime constructor, in true functional fashion. I had to map a call to int to turn all these number strings into integers.To update the seconds correctly, I had to create an instance of
datetime.timedelta with seconds set to 1 (which was my estimate of how off the time was), and add it the the time I got from the split string.Having forgotten how to do date formatting, I just used string formatting against time.hour, time.minute and time.second, and joined in the rest
[:8] of the string in the same operation.It was quite fun, but my friends eventually grew impatient so in the end no film was watched.
Thursday, 29 November 2012
Getting your own (IP) address and port in a javascript web app
While hacking something up using socket.io I needed to know the IP of the server, so I could connect a socket back to it. localhost was the solution at first, but I wanted to access it from other machines in my network.
I didn't want to get the local IP using node. I might want to serve the application from a server with more than one network card. I needed to have the IP fixed.
But I often develop the app while commuting, on my EEEPC. When I get home, I sync my code as I turn on my desktop computer to proceed work.
So I found myself needing to have a fixed IP over two different machines: my desktop PC, and my EEEPC. A fixed IP was clearly not an option.
I had the idea of using window.location. I used window.location.hostname to get the IP address or domain name of any server I was connected to. It was such a simple solution I was very positively surprised.
All uses of window.location:
example url: http://192.168.1.7:8080/chat?nickname=F%C3%A1bio#fragment
hash(#fragment)host(192.168.1.7:8080)hostname(192.168.1.7)href(http://192.168.1.7:8080/chat?nickname=F%C3%A1bio#fragment)pathname(/chat)protocol(http:)search(?nickname=F%C3%A1bio)
Wednesday, 28 November 2012
Common Regex
Tuesday, 13 November 2012
Dividing by zero for fun and profit
It's monday, and I'm back from vacation, feeling like I'm wasting my time and inevitably falling victim to the great cycle of life, money and everything. while (42) { }. Fortunately, I have sweet, sweet sarcasm on my side.
Anyway, this post is supposed to be about dividing by zero.
In python, it's a great way to find if a certain code path deep inside your call stack is really getting called, and when. You get to write one line which results in a noisy exeption, so your pain and confusion is properly turned into a Traceback.
class ReturnStatement(Statement):
def __init__(self, returnee):
1/0
super(ReturnStatement, self).__init__(returnee, '<return>')
"Oh. This time the exception never fired. I was sure this was supposed to be executed."
That's the kind of thought you are supposed to get, or something among the lines of:
"There, the exception.. Then why is this @!$# method not working if it's being called?"
Anyway, you get a good troubleshooting test just for typing three characters and a new line. Good bargain!
Of course, in JavaScript it's useless.
Yields Infinity. That's a discussion for another point in time. Maybe. I may never be inclined again to speak of that matter. Hours and hours of agony because of a number having a completely unpredictable number. Ugh.
In compiled languages, it's mostly useless, too.
Mondays.
Wednesday, 7 November 2012
Valilang
I have started a new project. Its name is valilang.
Create validation rules in a single format, meant to be used in both client and server sides.
Although I have given up my first plan of making valilang a minimal imperative programming language, the name still has the suffix "lang". I have opted to base the language syntax upon JSON, so it will be easier to learn and use.
The format is very easy to write. There are :
fields, which correspond to form fields, andrules, which are (mostly premade) short functions taking avalueargument and doing an assertion upon that value.
A valilang file will have an object with these keys:
fields, a list of fields.fieldValidation, an object mapping field names to a list of rules applied sequentially to these fields.
Here is an example valilang file, for a form with a single field:
{
"fields": ["name"],
"fieldValidation": {
"name": [
"required",
"min-10",
"max-50"
]
}
}
In the above object, we can see we have a single field name which has the following rules:
- It's a
requiredfield - It takes at least ten characters (
min-10) - and at most 50 characters (
max-50).
These rules (which are actually functions) are executed sequentially, until any function returns null, in which case the validation fails.
Notice how arguments are handled. They are extracted from after the dash in each of the rule strings, provided these strings have a dash.
On the client side, you just have to include a valilang file and valilang.js.
<script type="text/x-valilang">
{
"fields": ["name"],
"fieldValidation": {
"name": [
"required",
"min-10",
"max-50"
]
}
}
</script>
<script type="text/javascript" src="valilang.js"></script>
On the server side, you will load the valilang library for your framework or language, and ask it to validate your fields.
Of course this is all when both the client side and the server side are implemented. Remote loading of scripts is not yet supported. The server side hasn't been prepared for any language except for javascript (by requireing valilang.js in node and using its API), and there are too few validators (validation functions). Also unit testing is not done for the client or the server.
However, valilang.js is definitely compact, under 5 kb minified, and it is in a nearly usable state. Care to try it out and maybe contribute to its development? Report bugs and fork the github repository.