Tuesday, May 31, 2011

Ubuntu Recovering Boot menu after installing Windows

For Ubuntu 9.10 or later:
If you have installed windows OS after installing ubuntu  and windows has deleted the mbr(Master Boot Record)  you can't see the ubuntu at your boot menu, Try this solution.

Boot  from a Ubuntu CD and select Try Ubuntu

First Step:  Just checking the Hard Disk


Open a Terminal,Type in:


sudo fdisk -l


If you are Using only one Hard Disk,  Your Boot record will be in that disk But if you are using two or more and mark the Hard Disk that you have selected to boot first from your Bios Settings.
If you are using one HD your device is - /dev/sda

we will continue assuming your device id is /dev/sda .

Second Step:
Now You have to mount the drive that your Ubuntu was installed previously.
Mount the drive By selecting it from the 'places' menu. It has been mounted at your /media folder(Live CD)
Check the folder name and copy the folder name. You can do it also By dragging any file/folder from the mounted drive.

Now at Terminal:

grub-install is suggested now rather than grub-setup

Try this first:

sudo grub-install --boot-directory=/media/FolderName/boot /dev/sda



Or try this:
sudo grub-setup -d /media/FolderName/boot/grub /dev/sda

Replace The 'FolderName' with the Folder you have found at your /media folder.

Third Step:


Restart Your machine and the boot menu should be there but you will be missing the windows boot menu entry there . So after logging in Open a Terminal and Type:


sudo update-grub

It's Done! It'll show you a message if it finds a boot record for the windows.

If you get any error you check the following link.


There is a detailed description  at http://help.ubuntu.com/community/Grub2

Monday, May 30, 2011

Simple Python HTML,XML DOM Parser

This parser is intended to be simple and easy to use. You can download a copy from github . The parser can detect the various forms of tags like id=lorem , id='lorem', id="lorem". I have been using this script for my current scraping project.


I had been using the python module minidom but it's no use for the malformed xml and html . So I wrote this script using only regular expression.

Usage:

Copy the spp.py in your working directory and import the package.

 Instantiate the parser class by providing the url  or xml|html string

 To Get the 'id' attribute of the 'style' tag
 1.spp.parser('http://www.google.com').getByTag('style').item(0).attr('id')

 To get the attribute of a node whose id is 'csi'

 2.spp.parser('http://www.google.com').getById('csi).attr('style')

 # doc is the html or xml string you want to parse

 3.spp.parser(doc).getById('test').attr('href')

 To get the src of an image which is the 4th image of the document
 4.spp.parser(doc).getBytag('img').item(4).attr('src')

 To get the content of a node which has no child
 5.spp.parser(doc).getById('test').innerText()

There are some nice parsing tools for python available.
  1. Beautiful Soup
  2. lxml

Saturday, May 28, 2011

PHP regex lookaround assertion: lookbehind and lookahead

Regular Expression is a mighty beast, if you can tame it Regex will be a great leverage to solve your Problems.

A Real World Example:

Regex assertion is to check if any string matches following or the preceding characters. Suppose we want to check if there is any '.mp3'or '.mp4' string in our text but we only want the value 'mp3'or 'mp4'

One solution is:

$text = 'demo.mp3, shakira.mp4,lorum ipsum';

if(preg_match_all('/\.mp3|\.mp4/', $text,$matches)):

foreach($matches as $key => $match){
$matches[$key] =  str_replace('.', '' ,$matches[$key]);
}

endif;
var_dump($matches);

But this is ugly, we can use the lookbehind assertion to make the code simple:

preg_match_all('/(?<=.)mp3|mp4/', $text,$matches);
var_dump($matches); 
 
Let's Go Back: Check The Basic: 
 What are the lookahead and lookbehind assertions? and what are the syntax?

Assertions Only check that from the current position if a string matches the following or preceding characters, It does not consume any character. 'lorum(?=ipsum)' matches 'lorum' from the text 'lorumipsum'. Note that it's not matching 'lorumipsum', it's just checking if 'ipsum' are the next characters after 'lorum'.

LookAhead Syntax:
Positive lookahead: (?=)
Negative lookahead: (?!)

LookAhead Assertion Checks if following characters matches(positive lookahead) or does not(negative lookahead) from the current position.

positive Lookahead:
The following example matches 'lorum' first then checks that if 'ipsum' is next, If the checking is successful then function returns a successful match.

preg_match('/lorum(?=ipsum)/','lorumipsum', $match);
var_dump($match); 
 
Negative Lookahead: The following example matches 'lorum' first then checks that if 'test' is not next, If the checking is successful then function returns a successful match.

preg_match('/lorum(?!test)/','lorumipsum', $match);
var_dump($match); 
 
Look Behind Syntax:
Positive lookbehind:(?<=)
Negative lookbehind:(?<!)

positive Lookbehind:
This example matches 'ipsum' first then checks that if 'lorum' is previous, If the checking is successful then function returns a successful match.


preg_match('/(?<=lorum)ipsum/','lorumipsum', $match);
var_dump($match); 
 
 ******
One important thing to remember: Regex engine searches from left to right but in this case as there is nothing too match before our assertion it matces ipsum first to determine the current position. Suppose we would build our regex expression by  '^(?<=lorum)ipsum' then the regex engine would match the start of the line first. Then it would determine whether 'lorum' matches previous which would obviously fail as there is no character preceding 'lorum'.



Negative LookBehind: 
The following example matches 'ipsum' first then checks that if 'hello' is not previous, If the checking is successful then function returns a successful match.

preg_match('/(?<!hello)ipsum/','lorumipsum', $match);
var_dump($match); 

Monday, May 16, 2011

JavaScript Rolling Animation Using marginTop

In My previous Post I wrote about how You can get a rolling animtaion using javascript scrollTop property. As the "scrollTop" is not a W3C recommendation we will use 'marginTop' of the style object to make a better solution.

****
*****
Javacript Rolling Animation Example Using margin

*******
*********
*******
*******
*********
*******
***
Free Javascript scripts



We will set a negative value to the css margin-top property using a recursive function. This will move the div upwards gradually.Here is the sample Code.

d = document.getElementById('cat');

h = 0;

ab = function(){
 if(h == -150){
  h = 0;
  }
 h--;
 d.style.marginTop = h+"px";
 setTimeout('ab();', 10);
 }
 
ab();

Easy and pretty simple huh? This is just the basic, You can make awesome animations/sliding effects. This is compatible in all modern browsers. If you need more info, post a comment and I'll try to help.

Sunday, May 15, 2011

JavaScript Scrolling Animation Using scrollTop

Using only the scrollTop and scrollHeight property it is possible to create a nice scrolling effect.scrollTop and scrollHeight aren't W3C recommendation but these properties exist in all the modern browsers. I have checked it in Chromium,Firefox,IE and Midori the following script works perfectly. Let's see the effect.

Javacript Scrolling Animation Using scrollTop property
****
*****
*******
*********
*******
***
****
*****
*******
*********
*******
***
Javacript Scrolling Animation Using scrollTop property




The code is pretty simple. For achieving the animation effect out our div should be overflown.

a = document.getElementById('scroll');
c = 0;
b = function()
{
 if(200+a.scrollTop == a.scrollHeight)
 {
  a.scrollTop = 0;
  c = 0;
 };
 c++;
 a.scrollTop = c;
 setTimeout('b()',10)
}
b();

Need Explanation?

scrollTop(integer) property shows that how far you have scrolled from the top. You can also set a value to scrollTop which will scroll automatically to a value specified.

we have a recursive function that sets the scrollTop property. If we reach at the bottom of our element that's when if(200+a.scrollTop==a.scrollHeight) we reset the scrollTop property.

Here is a tutorial about How you can achieve the same animation using margin.

If You need more explanation, Let me Know.

About Me

Web Developer From Dhaka, Bangladesh.