having fun with code

Tag Archives: python

Little tricks: repeating strings in Javascript & Python

How many times have you found yourself printing strings like “————-” or “===========”? I do that a lot when I have to do console.logs or if I’m working on a console application or script. Adding dividers to the output makes it more readable. Python has a very peculiar syntax for repeating strings, which consists in [...]

Little tricks: editing strings by index in Javascript and Python

Editing strings by index maybe something that we don’t do all the time. But it’s one of these things that, coming from languages like C, one would assume is as trivial as assigning the value of an indexed position. Something like this: var a = "hello world" a[0] = "H" console.log(a) // outputs "hello world" [...]

More WebSockets, now with Python!

A couple of weeks ago, Tim and I worked on a little game/demo using WebSockets and C# (I haven’t been able to put it online since I do not have a Windows server). It was a lot of fun and we were able to see the potential of WebSockets and how much internet can will [...]

Crawling sitemaps with Python

This a basic script I have created to crawl an xml sitemap file (does not support nested sitemaps). It will report if the request was successfully processed by the server or if, instead, it returned some kind of error. #!/usr/bin/env python from sys import argv from re import findall from socket import setdefaulttimeout from urllib2 [...]

I’m ready for some fun with Python 3.0

I just found out Python 3.0/3k has been finally released. This is very good news :) Let’s find out how to install it on Mac OS X Leopard.

Making your objects sortable

Making your objects sortable in Python is very simple: add the __cmp__ function and the logic to compare the two objects and you are done! class person:   def __init__(self, name, age):     self.name = name     self.age = age     def __str__(self):     return 'Person %s (%d)' % (self.name, self.age) [...]

Copying objects vs. copying references

C++ is a language that allows creation of static instances of objects, this is without using pointers. This is why copy-constructors are needed, since it is common to copy or clone objects. Other languages like Delphi don’t allow to create static variables to instantiate objects. Instead, all objects are pointers. So in C++, assignments of [...]

Fibonacci

Fibonacci is a fast growing sequence that can easily overflow your integer variables. Fortunately, Python doesn’t have this problem, since it can handle huge integer numbers. #!/usr/bin/env python def fib(n):   if n==0 or n==1:     ret = n   else:     ret = fib(n-1) + fib(n-2)   return ret   for i [...]

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info