Installing oursql on ubuntu

sudo aptitude install python-pip libmysqlclient-dev
sudo pip install oursql
Advertisements

The Dark Magic of MongoDB

There is no other explanation for this but magic, ha? Look at this sequence of commands I just typed in my terminal. I simply cannot decide whether this is great or scary, mean, who saved that data for me in between restarts? And do not get me wrong, I am all excited about MongoDB, yet, these ghost driven technology which works for you behind the scenes is a bit scary, ain’t it?

tzury@x200:/$ sudo service mongodb stop
[sudo] password for tzury:
mongodb stop/waiting
tzury@x200:/$ status mongodb
mongodb stop/waiting
tzury@x200:/$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import Connection
>>> Connection().test_db.books.count()
0
>>> Connection().test_db.books.insert({"autor": "Charles Dickens"})
ObjectId('4c1e592b6b13eb0ee3000000')
>>> Connection().test_db.books.count()
1
>>>
tzury@x200:/$ sudo service mongodb start
mongodb start/running, process 3830
tzury@x200:/$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from pymongo import Connection
>>> Connection().test_db.books.count()
1
>>> Connection().test_db.books.find()[0]
{u'autor': u'Charles Dickens', u'_id': ObjectId('4c1e592b6b13eb0ee3000000')}
>>>
>>>
tzury@x200:/$

running wireshark as root on Mac OSX

After installing the .dmg packge you should run

$ sudo /Applications/Wireshark.app/Contents/MacOS/Wireshark

In order to capture traffic on the interfaces


Default Parameters Values and Mutable/Callable Objects

Quoting from the python Language Reference[link]:

Default parameter values are evaluated when the function definition is executed.
This means that the expression is evaluated once, when the function is defined, and that that same “pre-computed” value is used for each call. This is especially important to understand when a default parameter is a mutable object, such as a list or a dictionary: if the function modifies the object (e.g. by appending an item to a list), the default value is in effect modified. This is generally not what was intended. A way around this is to use None as the default, and explicitly test for it in the body of the function, e.g. …

I was not aware of this. I have an excuse though, in previous versions of Python, language reference was described as “for language lawyers” [link]. Since I never liked lawyers, I never bothered reading it. I am happy though it was changed in current release and now it is described as “describes syntax and language elements”. I guess I have to read it now.

Below is an example for a bug I had caused by this feature and which made me aware of this issue.

>>> import time
>>> def now(t=time.time()):
...     print t
...
>>> now()
1228988224.36
>>> now()
1228988224.36

Installing Python 3.0 on Ubuntu 8.10 Intrepid Ibex

UPDATE (Dec, 7): apt-get install python3 works now. this might be the prefered way for most of us.

Based on sofen’s blog’s post about python 2.6

This was the easiest thing I did recently. It went so smooth that this post is totally redundant.

Seven commands and you are all unicode.

Step 1: Make sure you got all the prerequisites

$ sudo apt-get install build-essential libncursesw5-dev libreadline5-dev libssl-dev libgdbm-dev libbz2-dev libc6-dev libsqlite3-dev tk-dev g++ gcc

Step 2: Grab the bits, extract them and build them

$ wget http://www.python.org/ftp/python/3.0/Python-3.0.tgz
$ tar xvzf Python-3.0.tgz
$ cd Python-3.0/
$ ./configure
$ make
$ sudo make install

That’s it my friend. You now have the best software development platform in the entire universe!

$ python3.0
Python 3.0 (r30:67503, Dec  4 2008, 21:27:40)
[GCC 4.3.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

Good luck and enjoy the sweetness of open source software!


error_help() for python hackers

Just came across this what might be a usefull utility which provides help within the python interperter using collaborative data gathered at http://bug.gd

Example:

tzury@regulus:~$ sudo easy_install bug.gd
tzury@regulus:~$ sudo /usr/bin/python error_help_config.py
tzury@regulus:~$ python
Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) 
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 0/0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
>>> error_help()
========== 1 of 7 ==========
Error: Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero

Solution: I divided by zero as a test. Maybe you did the same thing? The trick is not to divide by zero!
...
...

		

Pydoc’s built-in HTTP server

Just came across this in python mailing list (link)

import pydoc
pydoc.gui()

(then click the ‘open browser’ button)