== operator behaves strange in java

23 07 2009

Yesterday I was helping my sister to write one java program. I don’t know java, I was working on php, python, javascript from long time. But I could figure out the logic by seeing any code. So I am checking my sister java code. She wrote one if statement like this.

if(input_data == "Y"){
// some code

There is nothing wrong but the if condition is failing every time even I give the value “Y” for the input_data. This is correct syntax and correct logic, but it is still failing. It is looking very strange and weired for me. When I google for how to compare 2 strings in java then I figured out that “==” operator compare the object references not the content of the variables. In my case if write I should write if condition like this.

// it works fine

This is much difference from other languages. Why it is like this I could not understand.


Why I fail to submit my phrase to engineyard contest

23 07 2009

There is a programming contest which is conducted by engineyard. The contest is interesting so I planned to participate. I wrote program but could not submit the phrase to it. When I analyze why I could not submit, these are the mistake that I did.

1. I calculated wrong time of submission. I am living in IST and the content submission time is based on PST. I am not very familiar with these timezones, so I calculated wrong time. By the time I wanted to submit, the submission time is closed. I learned calculate the time properly and keep alerts

2. Lazy in writing program. I wrote program which will just take the 12 random words from the dictionary and calculate the hamming distance as specified in the contest. There are 2 more things I missed I can add 5 letter word at the end and each letter in the word can be of any case(upper or lower). I wrote these 2 case after the contest time start. I learned finish the task at that time, don’t postpone any piece of work to another day.

3. Lazy in knowing about SHA1 algorithm. I know that taking random words from the dictionary does not work. I know I need to know more about SHA1 but could not find or spend time on knowing more about SHA1. I depend on luck I know this is not correct, still I am lazy and couldn’t read about SHA1. I leaned that do not depend on luck just know more and work.

Drupal – Advertisement module

21 07 2009

Today I got a chance to work on advertisements feature in one of our project. I found Advertisement module. This module is having lot of features, I just need a way to add advertisement with the banner and need to show the ads in a block. I did following steps to get what I need.

1. Download and place Advertisement module in modules directory. I use drush

drush dl ad

2. Enable the ad, ad_image module through admin build modules page. I used drush

drush enable ad

drush enable ad_image

3. Go to admin/content/ad/configure/image page and set the maximum image size for the default group and checked Allow remote hosted images. Here I used default group. If we want we can create groups and assign ads to different groups. When we create a group, the specific group block will be created automatically.

4. Enable the default ad group: default block. Enable respective group block if create groups

5. Create advertisement by going to node/add/ad page. Specify the title and group details

6. Select the Add type in the Select Ad type section and click select then one new section will add at the bottom of Select Ad type section.

7. Specify the destination url and specify Romote Image path if the image is from remote path. We can also upload the image at the bottom of the page in file attachment section.

8. We can set the ad activation, expiration settings at scheduling section.

9. Set the status of the ad and save. Choose the active status if you want to display ad in the block. Save the advertisement.

10. Repeat the steps from 5-9 to add more ads.

11. Now the block will display the ad image that we created now.

12. By default block display 1 ad. we can configure the number of ads to be displayed in the block in block configuration page.

That’s it. I can able to create ads with image and displaying them in a block. Hope this might be helpful for others(I used drupal 6)

Drupal – Restrict access to menu item per role

20 07 2009

When I am working on one project, I created some content types. The view permissions for some of the content types should be restricted only for some roles. There is no view access peromission specific to content type. There are  add, edit, delete permissions for the specific content type.

Any how I am using only one node of that content type. So I used menu_per_role module. Through this module we can restrict the access of a particular menu item. I did following steps to restrict access to one menu item for some roles.

1. install menu_per_role module

2. enable the module

3. Go to menu item edit page, you can see Restrict access permission section. Select the roles which you want to give permission for this menu and save.

That’s it. It worked for me for now, but I need to find out how to restrict view access of particular node type. If some one know please send me comment.

Drupal – Custom filter type for content type

20 07 2009

In one of the project that I am working, there is a requirement to enable full html filter type for one content type. The rest of the content types should have the default filter type configurations set in the admin/settings/filters/list.

I did with the Filter by node type module. Which solved my problem. I followed bellow steps to enable full html filter type for one content type.

1. Install the module

2. Enable module

3. Go to content type edit page.  Path will be admin/content/node-type/<node type>

4. Expand Submission form settings section. Check the allowed input filters for this content type and save.

5. Select the proper roles who can use this filter format. We can set this by going to the corresponding filter configuration page. If we do not set this role even if we set the default filter type it does not effect.

That’s it. I used drupal 6. Hope this might be helpful for others.
update: I found another better module to do this. It is Better Formats module . I did not try this but It looks it works better than filterbynodetype module.

htmlcutstring python package

20 07 2009

I released php, javascript implementation for cut html string, these programs cut the html string by keeping html tags as it is. Now I released same in python with the name htmlcutstring. Check this at http://pypi.python.org/pypi/htmlcutstring/1.0 .

It is easy to extract an excerpt of a text string with a given length limit. But if you want to extract an excerpt from HTML, the tags that may exist in the text string make it more complicated.

This module provides a solution to extract excerpts from HTML documents with a given text length limit without counting the length of any HTML tags.

This module is used to cut the string which is having html tags. It does not count the html tags, it just count the string inside tags and keeps the tags as it is.

ex: If the string is “welcome to <b>Python World</b> <br> Python is bla”. and If we want to cut the string of 16 charaters then output will be “welcome to <b>Python</b>”.

Here while cutting the string it keeps the tags for the cutting string and skip the rest and without distorbing the div structure.

obj = HtmlCutString("welcome to <b>Python World</b> <br> Python is",16)
newCutString = obj.cut()

newCutString = cutHtmlString("welcome to <b>Python World</b> <br> Python is",16)

steps to create mysql database

17 07 2009

Creating a database in mysql is easy with just CREATE TABLE command. For any application just creating database is not enough. We have to create one user and we need to give privileges for that user on the database. These are the commands that will create database, user and give permissions for the user to the database.

Go to mysql prompt

shell>mysql -u root -p

Use -p option if password is set for the root.

Create database

mysql> CREATE DATABASE databasename;

Create user

mysql> CREATE USER username

Grant permissions for the user

mysql> GRANT ALL PRIVILEGES ON databasename.* to username@localhost IDENTIFIED BY 'password';

Flush privileges


Quit mysql prompt

mysql> \q

Note: Untill we flush the privileges we can’t get the new privileges that we assigned.

That’s it we created database, user and gave permissions to user to the database. Now use the following command to go to database with the username

shell> mysql -u username -p databasename