Simple Twitter Widget in Django

This is how you can create a widget that displays your latest tweets using python-twitter. It will look like this:

Step 1 Download the following python packages using easy-install or pip installer: python-twitter, oauth, httplib2

Step 2 This is the python code you need which will create a list of tweets that have embeded URLs (includes #hashtags, @mentions, URL's, and time of post)

Step 3 This is an idea of the Django template tags you need to display the list of tweets and the approximate CSS.

Big O

So this problem came from a discussion with some friends that went on for 3 days. Although this looks like a simple big O analysis it was quite deceiving for me (and apparently for everyone else) when I first saw it and went from guessing it was O(N^8), then guessing again O(N^6) to finally O(N^4) with some real analysis. This is a typical case when your brain wants to make things worse than they are. The problem is: What is the big O of a piece of code like this given than array.length is O(N)? for i... N array.length --->for j... N array.length ------>for k... N array.length My approach was to imagine it first for the case we usually know, when array.length is O(1). If you think of it graphically, the first two loops can be seen as a 2D matrix. Every i,j in the matrix represents the big O of one step. Then if we "zoom in" into any of those steps, we see that every "1" unrolls into a O(N) operation because of the third loop. So in total we come up with O(N^3). for i... N array.length --->for j... N array.length ------>for k... N array.length
_________N_____
                                                                            
   j0   j1  j2  j3  j4       
i0 1   1   1   1   1         
i1 1   1   1   1   1 ...    |
i2 1   1   1   1   1        N
i3 1   1   1   1   1        | 
i4 1   1   1   1   1                    <--O(N*N ) = O(N^2)
            |                               
           /\  (exploding a step)
          /  \  
              _        
      k0 1    |                         <--O(N)
      k1 1    N 
      k2 1    |
      k3 1                           So O(N * N^2) = O(N^3)
Now I imagined it for the case of our problem, when array.length takes O(N). In the i,j matrix every step now is not "1" but "2N+1" because it includes 2 array.length (one for the i loop and another for the j loop). Then if we "zoom in" into any of those steps, we see that every "2N+1" becomes "2N+N^2" since the third loop is now O(N^2). So the total of the i,j matrix is O(N^4). for i... N array.length --->for j... N array.length ------>for k... N array.length
________________N______
                                                  
      j0       j1         j2               
i0 ( 2N+1)  ( 2N+1)   ( 2N+1)            
i1 ( 2N+1)  ( 2N+1)   ( 2N+1)         |
i2 ( 2N+1)  ( 2N+1)   ( 2N+1)         N
i3 ( 2N+1)  ( 2N+1)   ( 2N+1)         | 
i4 ( 2N+1)  ( 2N+1)   ( 2N+1)       <-- O((2N+1)*(N)) = O(N^2)
                  |              
                 /\   (exploding a step)
                /  \  
                       
            k0 1+N   |              <--  O(N*(N+1)) = O(N^2)
            k1 1+N   N
            k2 1+N   |
            k3 1+N                


                                        So O(N^2 * N^2) = O(N^4)

Recovering files after rm in Linux

This is in brief how I recovered 20+ files I had lost after an accidental 'rm'. It was simpler than I expected. My case
  • System: Ubuntu
  • Time I started recovery: 1 hour after having done 'rm'
  • Files recovered: html, css, .py

  • How to
  • Run `df` to find out which drive the files are in. Eg. /dev/sda1, or /dev/sd6

  • `sudo vim /etc/foremost.conf` You will see various file formats and need to uncomment the ones you want. In my case I needed to add .py files. For .py files you need to specify how the files usually start, so I used the "import" expression as a match. For html files you can use the "" tag or something of the sort.

  • `sudo foremost -i /dev/sda6 -o recovered_files` (-o is for the directory to where you want to output the recovered files). This will start running and go on for a while trying to recover as many deleted files as it can find. In the meanwhile you can do the next step

  • Look inside the recovered_files folder. Here there should be a directory for each file extension you are looking for. In my case I find a py, html, and css folder. Inside each of those folders you will find hopefully numeric file names with the extensions. (Eg. 2342344.py). These are the recovered files.

  • `find recovered_files/py/ -type f -print0 | xargs -0 grep "class UserProfile:"` This is so that we can narrow it down to the files we want from the recovered files. We cannot use a regular grep because there might be too many files and you will get: /bin/grep: arg list too long

  • For more details checkout the foremost tool.