17 janvier 2011

Get a random element from an array

I used that expression before:
random_choice = array[rand(array.size)]

Then I read this quicktip and decided to try:
random_choice = array.choice

Unfortunately, on my production server, I still have Ruby 1.8.6. So, after a long search, I finally settled on:
random_choice = array.rand

Which is supported in both Ruby 1.8.6. and 1.8.7

11 octobre 2010

Bind socket to interface: No such device

My VM with Ubuntu on it has trouble sometimes dealing with the changes between bridged eth0 and eth1.

The symptom:
SIOCSIFFADDR: No such device
eth0: ERROR while getting interface flags: No such device
Bind socket to interface: No such device
Failed to bring eth0

First check all your network interfaces:
ifconfig -a
You should see that the eth1 interface is actually present (but not connected).

Note the MAC address of eth1. Edit /etc/udev/rules.d/70-persistent-net.rules and change eth1 -> eth0 (the MAC address should be the correct one). You can delete the old line for eth0.

Restart the VM and the network should be OK.

28 septembre 2010

Using multiple inheritance for Python mocks

Today, I needed to mock just one function from a class instance.

I used multiple inheritance to achieve that like so:



The ProdEngineMock class inherits everything from the ProdEngine class, except the mocked function.

Nice and elegant!

25 septembre 2010

Convert a sql_ascii Postgres database to a UTF8 one

pg_dump -f your_db.dmp your_db

iconv -f ISO8859-1 -t UTF-8 your_db.dmp > utf.dmp

psql your_utf_db < utf.dmp

Note that Rails was used to input the data into my old sql_ascii database, so the encoding was consistent. You might not be so lucky...

Identify large tables on Postgres

To quickly know which of your Postgres tables are the largest:

select relname, reltuples from pg_class order by reltuples desc;

reltuples are the number of rows, so this command will show your largest tables first.

Rails validates_length_of and byte encoding

Why you should NEVER create a Postgres database with the default sql_ascii encoding set to use with Rails


Ruby prior to 1.9 considers strings as bytes, and that can cause some maddening issues. In my experience, all encoding issues are maddening...

Anyway, I had a validates_length_of like so:



I got a problematic lead from a customer (that included special Word characters that were multi-bytes), put them in a test and started a debug session:



What???

After some digging, Rails, in order to consider characters and not bytes, uses split(//).size instead of size directly. Split(//) is the default tokenizer of validates_length_of and it can be changed as an option passed to the validates method. See http://railspikes.com/2009/7/20/validates_length_of-gotcha for more details.

My original problem, though, was that the lead was 499 characters, but 517 bytes and my Postgres database was encoded with the default sql_ascii encoding set.

Therefore, my constraint on Postgres column lead "character varying(500)" translated to 500 bytes maximum...

Conclusion: never ever create a Postgres database with the default sql_ascii encoding set to use with Rails. Always use the utf8 encoding set. Even if you deal only with English: special characters exist in all languages...

15 septembre 2010

Samba to follow symbolic links on Ubuntu

Add to /etc/samba/smb.conf:
follow symlinks = yes
wide symlinks = yes
unix extensions = no
And reload the Samba configuration:
sudo service smbd reload