Tuesday, August 28, 2012

how to split the given statement using split() function in python


s = input("enter any sentence")
for w in s.split():
    print(w)

this script will split the entered sentence based on the space 

if we entered the sentence as  , python is using by rby , then result is 

python
is
using
by
rby

if we use the split like below 

s = input("enter any sentence")
for w in s.split(':'):
    print(w)


then 

it will work as follows

ramesh:rby:python


ramesh
rby
python

Monday, August 27, 2012

printing first n natural numbers starting from 0 in python


def ranarr(len):
    arr=[]
    for i in range(0,len):
        arr.append(i)
        print(i)
    return arr

print(ranarr(10))



result :

0
1
2
3
4
5
6
7
8
9
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Sunday, August 26, 2012

finding the length of a string in python


s='ramesh'
print(len(s))



open ant notepad type above two lines save it with some name of ur choice with file extension as py the execute 

adding two number in python

below is the simple python script to add two numbers



x = input("enter value of x")
y= input("enter value of y")
z=int(x)+int(y)
print("x :"+str(x)+" + Y:"+str(y)+" = "+str(z))


save it as add.py and enjoy

Wednesday, August 8, 2012

how to retrieve the values in .properties into java using the spring and spring annotations

In this , u will see , how to get the values of .properties into java using spring annotations

in the spring application context xml file defined as below

      
    <util:properties id="pwdProperties"
        location="classpath:/PwdRules.properties" />


and define a properties file with the name PwdRules.properties, add any name value pair to this file

let consider we added like below

name=rby

now define a class , with the name . AppConfig.java , inside , write the below code


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;


@Configuration
//spring config that loads the properties file
@ImportResource("classpath:/login-connector.xml")
public class AppConfig {

/**
     * Using property 'EL' syntax to load values from the
     * jetProperties value
     */
    private @Value("#{pwdProperties[' name  ']}") String  name  ;
    
    /**
     * Create a jetBean within the Spring Application Context
     * @return a bean
     */
    public @Bean(name = "pwdBean")
    PwdBean pwdBean() {
    PwdBean bean = new PwdBean();
        bean.setName(name);
        return bean;
    }
 
}


define a bean class with the name

PwdBean.java and add one member field for that as, name and generate setter and getter for that 


and u can write the test case as below to test this

ApplicationContext ctx =
            new AnnotationConfigApplicationContext(AppConfig.class);
PwdBean bean = ctx.getBean(PwdBean.class);
System.out.print(bean.getPwdrule());