Monday, 1 December 2014

On my way to install memcached

Installing memcached on my laptop has really been a pain till now. Nothing seems to work as expected. Although memcached is installed , which i see by testing it on the console, it doesn't show up in  phpinfo()  .

While installing memcached , i changed some defaults( unintentionally) , which included apache to run on system startup, so lampp would not start. Here is the solution

when debian7 start,it will start apache2. so you should stop it first,than try to start lampp.

/etc/init.d/apache2 stop
/opt/lampp/lampp restart


This is however a one time solution. No worries!! 

Change the defaults, use this:(ubuntu >11.04)


echo "manual" >> /etc/init/mysql.override
update-rc.d -f apache2 remove


Will update this post when memcached is installed!!


UPDATE!!

After numerous attempts at installing memcached, i uninstalled lampp and installed it again, I didn't uninstall memcached however, and somehow when i restarted wamp, I could see memcached in the phpinfo() !!

This might not work in every situation, but it offers a possibility in case all other methods fail.


Friday, 28 November 2014

Fixing dependencies and autoremove packages in ubuntu

If you are a developer, then you might install various packages and remove them when work is over, or you get a better solution. However, most packages are not standalone, i.e they have dependencies on other packages, which apt-get  installs automatically. apt-get is really a very nice package manager. It internally keeps a list of what packages were installed automatically and which were manual. So, if you remove a package you manually installed, you might not want the other packages that were installed as a part of the dependency. 
    
     Here is a very nice answer from stackexchange:

Now apt implements an Auto-Installed state flag to keep track of these packages that were never installed explicitly. When you uninstall a package you can add the --auto-remove option to additionally remove any packages which have their Auto-Installed flag set and no longer have any packages which depend on it being there (a package may also be kept if another suggests or recommends it depending on the value of the APT::AutoRemove::RecommendsImportant and APT::AutoRemove::SuggestsImportant configuration options).
I would have a look at the list of packages and decide if they are worth keeping, sometimes packages which you may wan to keep are marked Auto-Installed by default. You can get information on what the various packages do by doing apt-cache show package_name. If you decide to keep some, you can use apt-mark manual followed by the names of the packages you want to keep.
Note that usually you would want to have library packages (most packages beginning with lib) marked as Auto-Installed since there are few reasons to have these packages installed on their own - other programs usually require other libraries to run, but they are little use on their own. Even if you are compiling software against the library to need the development package (ending in -dev) which depends on the library itself, so no need to explicitly install the library.
Also using aptitude, you can do aptitude unmarkauto from the command line or change within the curses interface. Within the package lists in the interface, all automatically installed packages have an A next to them. You can change this state by using m to mark an auto installed package as manual and M to mark as manual again (also l to open a search dialog and Enter to view package details)
This pretty much explains everything.

 Now, sometimes it happens that a package is installed without its dependencies, so obviously it does not work. apt-get keeps a list of the unmet dependencies.  You can resolve theses dependencies ,i.e install the packages required by

                                           sudo apt-get -f install

This will install the dependencies and your package will work as expected.


Now, as said earlier, how to get rid of packages that were installed as a part of dependency and no longer required? Here it is

                                          sudo apt-get autoremove

This removes the packages and frees up disk space.





Saturday, 22 November 2014

Django. Django Admin Page and Phpmyadmin

I have been developing web apps for quite some time in Django. I would like to discuss here some things which might not have struck you while developing with Django.

Django comes with a inbuilt admin page, Once you register your model in admin.py , you are set to use the admin page for adding /deleting records in your database.However you might feel constrained by the functionality which this admin page provides, as I did. Since I come from a PHP background,  I badly missed phpmyadmin and its rich features. It feels quite strange to see  PHP and Python associated (it did for me).

    One thing is for sure, to run phpmyadmin, you need a server that supports PHP. So, first of all you need to know that you need to have your wamp/xampp/lamp server running in order to access the database. The default ip for wamp is 127.0.0.0 and that for django inbuilt server is 127.0.0.1 , so running both of them would never be a problem. Now you need to configure the settings.py file inside of your project with the database details like


                                             'ENGINE': 'django.db.backends.mysql'
and change the username,password etc. to access the database. Don't forget to run manage,py syncdb after this.  Now, how to make phpmyadmin notice this?

Head over to config.inc.php in htdocs directory of your wamp installation and provide the connection details to the django built in server in the manner it has been done for the apache server above . Remember to put the $i++ before the configuration details.


Now, restart apache and go to phpmyadmin. You will get an option to select beween the 2 servers. Choose 127.0.0.1 (django) and you will find the database sitting right there.

The bad thing about this is:
You cannot modify the database structure. You can only add/update/delete records from the database. There must be a tool for overriding this, so I will update later!!

Handling change in usernames, in a better way!

Some websites that have account for users , give an option to change their username. It appears to be a trivial task. I have implemented this functionality in 2 of my projects . The functionality i used was to ask a LOGGED IN user for the new username and would ask him to reenter the password. This is a very simple approach and works well for small applications that do not have very much functionality in terms of databases.
     
     I recently changed my username on Github and was wondering what procedure they followed. I think it is highly improbable that they use the usernames that we see internally in databases too. What I mean is that suppose Mr. XYZ does a commit in his repo in Gihub (XYZ is the username), so in their database that stores the history of commits, XYZ is not stored, rather a hash( for more security) or a simple unique number is stored. Their would be a user table where each such number or hash uniquely determines a user and does not change anytime, no matter how many times the user changes his username. Whenever some information is displayed that should identify "who ? ", they would simply fetch the username associated with the unique number or hash (of course they could store this number in a session variable to avoid querying the same thing). This gives the advantage is that you don't have to traverse through your entire database and substitute the old username with the new one. You simply change the username in the user table and the unique identifier(the hash or the number) remains the same. It would greatly bring down computations required for trivial task like this. 

Wednesday, 19 November 2014

Processing Feedbacks

It's a day before the endsemester exams and I can't help thinking about how the ERP team evaluates my feedback about professors. 10,000 + students. One obvious way which I can think of is- It gets a overall rating of each teacher. If the overall rating is good enough, I don't think they worry about the suggestion. If there is a poor rating, they would manually take some random suggestions (if they take at all).

Improper and inefficient I would say (if at all this is the process).

I am thinking of using Python's Natural Language Toolkit for this purpose. I will look for words like "good" "bad" "should" and get their context like "good at *" "bad at *" "should do *". Similar suggestions (a group of students giving similar suggestions) would be forwarded to the teacher to improve the classroom experience. Further updates after the endsem.

Saturday, 15 November 2014

Ordering your Notes

You are a student and did not make notes properly for a particular subject? And you took photos of your friend's notes to study? Then this post is for you.

I too had such a situation once. One thing to highlight is the naming convention by most devices. Most phones and cameras name the shooted images as yyyymmdd_hhmmss .

Obviously, this naming convention makes sure that no two photos have the same name .(I have not tested what happens when you deliberately change your device time to an earlier time which corresponds to an already present image on device, though it is very difficult to remain accurate to the second.)

Python to the rescue.

My method is not efficient but it is quick and "quick" is what i wanted. So,  i sorted out the names of the images on the basis of when they were clicked (to be more accurate, time when they were clicked) and then renamed them with integers starting from 1 .

This way you can easily remember what page you were on when you last left.

My code is available at https://github.com/light94/rename-notes


UPDATE:
I have updated my code to rotate the images by 90 degree so that everytime a new image comes, we don't have to manually rotate it.

Note: One important thing that I learnt from this was that python's os.listdir() generates the list randomly if no parameters are passed to it. 

Tuesday, 11 November 2014

Fixing Permissions of htdocs in Ubuntu

If you are using xampp, you might already know that the default location of your projects will be /opt/lampp/htdocs . However you cannot edit contents of this folder without root permissions. However, using the root account for trivial things like editing files is a very bad idea since you can do harmful changes to your system unknowingly.

There is a very detailed and beautiful description of how to avoid this in this blogpost.

The idea is, we need to change the ownership of the folder to something other than the root. No more sudo for triviality.!! Go ahead and read that article.

UPDATE: The -R flag to chown applies command to all the subdirectories( recursively) as well.