Tuesday, 16 December 2014

Listing the directory structure into CSV format | Python

In order to avoid complicated population of databases, by filename and containing folders, we can in turn switch to saving data in csv file formats.

The problem encountered was:

I had to create a database having 5 fields:

'object', 'category' , 'filename', 'source', 'date'

source was a fixed string and date can easily be found in python by importing the time lib as shown in code below.

Directories were in this order:

category<object<filename

Elaborating, in my root folder , I had several category folders, like Animals, Musical Instruments, Vehicles etc and inside them I had further sub-folders like in Animals, I had dog, cat etc and so on for other folders. Further in this subfolder I had .mp3 and .wav files . I need to have this directory structure presentable in csv format as discussed above.

Doing this manually for above 2000 files is a herculion job, here is the script to simplify it:


import os
import csv
import time

def list_files(dirpath):
    files = []
    for dirname, dirnames, filenames in os.walk(dirpath):
        files += [os.path.join(dirname, filename) for filename in filenames]
    return files

now = time.strftime("%c")
path = '/home/infinite/findsounds'

files=list_files(path)

with open('/tmp/final.csv', 'wb') as csvfile:
  writer = csv.writer(csvfile)
  writer.writerow(['object', 'category' , 'filename', 'source', 'date'])
  for file in files:
    if file:
     
      filestr=str(file)
     
      directories = filestr.split("/")
      writer.writerow([directories[5],directories[4],directories[6],'www.findsounds.com', now])

Please take care of the indentation while copying.

A view of CSV generated (Libre Office, comma separated view):


Small piece of code, hope it helps.

PS: Feel free to drop your suggestions/queries below.

Keep Scripting!

Monday, 15 December 2014

Creating websites using PHP embedded HTML| Incorporating MySQL database

So, in a process of developing this website, I will be jotting down all the prerequisites and the steps thereafter in order to make a php website live online.
Hope it will help the struggling newbies in this field.

Before moving on, let's understand:

I. What's PHP?

PHP, the term derived from "Hypertext Preprocessor", is a server-side, HTML embedded scripting language used to create dynamic Web pages.
As it's server-side related application it does not require any configuration at the client's/viewer's side since the code itself is executed on the server where it's residing.
All that is required is any Web browser currently existing, but that will not suffice to develop php websites, we need to set up a website development environment. (We will be looking after it a bit later. )
Having the PHP as an embedded implementation at the HTML pages within the special PHP tags at the server side no one would be able to access the code itself through the browser as being able with the plain HTML language.
It's syntax parts is borrowed from other languages as C, Java and Perl with some unique features thrown in.
The main goal of the PHP oriented language is to provide the easiest way of generating and developing dynamic Web pages to the developers or site designers.
The PHP language offers an reliable connectivity services to the various database services like MySql etc. The database management tool phpMyAdmin, most popular bullet in board interfaces like PHPBB or PHPNuke are related on the PHP language as well. Internet related connection services as e-mail, email via POP3 or IMAP, url encoding, cookies, redirections etc. are available through the PHP as it's web oriented.
[Source: http://www.oxxus.net/tutorials/php ]
So for this particular website we will be using MySQL for the database.
If you have heard the term "PHP" in your life for less than 7 times, you are recommended to view source of this website to understand what php enabled websites look like at the back end part.
Go to this website, right click and select "View Source", there are three PHP scripts running in the back end.  http://www.neil-kb.com/

II. TOOLS WE WOULD  BE NEEDING:

1. MySQL
2. XAMPP
3. PHPMyAdmin
4. Text Editor
5. Local Host settings....may be. 

Go to this link to dive deep:

Install XAMPP by following these steps:
http://computernetworkingnotes.com/ubuntu-12-04-tips-and-tricks/how-to-install-xampp-1-8-2-in-ubuntu.html

Have SQL installed and configured.
                      


III. Three main youtube videos that will get you started with dynamic websites are these:
What is actually happening?

Unlike the static html pages, this time we need the content to be displayed dynamically by running a php script.
So, the php script senses the argument passed and accordingly loads the corresponding page. PHP facilitates to interact with MySQL or other databases and get the content loaded from there.
This is briefly what is happening in the following videos, have a glance.
 
1. https://www.youtube.com/watch?v=G4DZW9IFQ_U


2. https://www.youtube.com/watch?v=HFZbW9eDRAM

3. https://www.youtube.com/watch?v=SETjgigQmBE

Also, this may help in the process of learning:

https://www.youtube.com/watch?v=IMuhWHO_T-0




So, hope the content and links would prove useful.

Keep hacking!





Wednesday, 16 July 2014

Removing dual boot when you have deleted ubuntu already

Hey folks!

It happens at times that you have two OS running on your laptop say Windows and Ubuntu and you have been successful in establishing dual boot for them back in time but now when you don't need one OS and deleted (say Ubuntu) forever, still dual boot option appears, do you want to get rid of that?

Here is a simple technique I discovered.
 Just run command prompt as administrator (right click cmd and select run as administrator).
Then typr these commands:

>bcdedit

This willl display a list of your boot options, now find the identifier for Ubuntu. It would be enclosed within curly braces.
Say your identifier is {GUID}. It can be a lengthy string too.
Now type:
>bcdedit /delete{GUID}

Operation successfully completed. This marks the end of dual boot.

Hope that helps!
Stay cheered. 

A secret love message ~

Hmm, so the trick of putting up a catchy seasonal title worked out, we got you here! Folks, we will be talking about a really cool tech...