Tuesday, 6 November 2012

Emergency Medical Support System in Android


                      Emergency Medical Support System is a system to manage emergency health problems and critical conditions of patients.This medical application in Android phones is a very useful and life saving utility.This system stores the medical conditions of the user and the contacts of the doctors and hospitals he had. The system provides single touch access to contact the persons and organisations,It provides Medical data and contacts for first responders and hospital sta ff to use in case of an emergency.The online website is used for store the database.The GPS service is used for locate the place and based on this location the list of nearest hospitals is retrieve from the online data base.EMSS provides the facilities given below

  • A list of people to call during emergency
  • Insurance information store and retrieve immediately 
  • Doctor's name,number,etc are store and call during emergency
  • Medical conditions store and retrieve immediately
  • Any special instructions for fi rst responder
  • Details of nearby hospitals can be accessed by GPS
  • Call to the nearby hospitals 
  • Ambulance calling 
  • Sms service

                 Any special instructions or other information you wish to provide can be given.The use of gps service enhances the scope of this system.The system is implemented in java language along with tools like eclipse and android sdk.

Scala - Exception Handling


       Scala's exceptions works like exceptions in many other languages like Java. Instead of returning a value in the normal way, a method can terminate by throwing an exception. However, Scala doesn't actually have checked exceptions.

       To handle exceptions use a try{...}catch{...} block like in Java except that the catch block uses matching to identify and handle the exceptions.


Throwing exceptions:

 

       Throwing an exception looks the same as in Java.create an exception object and then throw it with the throw keyword:

             throw new IllegalArgumentException

Catching exceptions:

 

     Scala allows to try/catch any exception in a single block and then perform pattern matching against it using case blocks as shown below:

Tutorials Point, Simply Easy Learning
import java.io.FileReader
import java.io.FileNotFoundException
import java.io.IOException
object Test {
def main(args: Array[String]) {
try {
val f = new FileReader("input.txt")
} catch {
case ex: FileNotFoundException =>>{
println("Missing file exception")
}
case ex: IOException => {
println("IO Exception")
}
}
}
}

The behavior of this try-catch expression is the same as in other languages with exceptions. The body is executed, and if it throws an exception, each catch clause is tried in turn

Scala - Traits

                A trait encapsulates method and field definitions, which can then be reused by mixing them into classes.Unlike class inheritance, in which each class must inherit from just one superclass, a class can mix in any number of traits.

               Traits are used to define object types by specifying the signature of the supported methods. Scala also allows traits to be partially implemented but traits may not have constructor parameters.

A trait definition looks just like a class definition except that it uses the keyword trait as follows:

trait Equal {
        def isEqual(x: Any): Boolean
        def isNotEqual(x: Any): Boolean = !isEqual(x) }

              This trait consists of two methods isEqual and isNotEqual.Here we have not given any implementation for isEqual where as another method has its implementation. Child classes extending a trait can give implementation for the un-implemented methods. So a trait is very similar to what we have abstract classes in Java. Below is a complete example to show the concept of traits:

trait Equal {
         def isEqual(x: Any): Boolean
         def isNotEqual(x: Any): Boolean = !isEqual(x) }

class Point(xc: Int, yc: Int) extends Equal {
          var x: Int = xc
          var y: Int = yc
          def isEqual(obj: Any) =
                obj.isInstanceOf[Point] &&
                obj.asInstanceOf[Point].x == x }

object Test {
        def main(args: Array[String]) {
                val p1 = new Point(2, 3)
                val p2 = new Point(2, 4)
                val p3 = new Point(3, 3)
                println(p1.isNotEqual(p2))
                println(p1.isNotEqual(p3))
                println(p1.isNotEqual(2))
                     }
            }


When to use traits?

  • If the behavior will not be reused, then make it a concrete class. It is not reusable behavior after all.
  • If it might be reused in multiple, unrelated classes, make it a trait. Only traits can be mixed into different parts of the class hierarchy.
  • If you want to inherit from it in Java code, use an abstract class.
  • If you plan to distribute it in compiled form, and you expect outside groups to write classes inheriting from it, you might lean towards using an abstract class.
  • If efficiency is very important, lean towards using a class.

Friday, 5 October 2012

Generators In Python

  

Generators


         A generator is a special routine that can be used to control the iteration behavior of a loop.A generator is similar to a function returning an array. A generator has parameters, it can be called and it generates a sequence of numbers. But unlike functions, which return a whole array, a generator yields one value at a time. This requires less memory.

Generators in Python

  • Are defined with the def keyword.
  • Use the yield keyword.
  • May use several yield keywords.
  • Return an iterator.
This is an example of generators used in a python program


                 def gen():
                    x, y = 1, 2
                    yield x, y
                    x += 1
                    yield x, y


                 it = gen()

                 print it.next()
                 print it.next()

                 try:
                    print it.next()
                 except StopIteration:
                    print "Iteration finished"
 

Representing infinite sequences

       
     Generators present us with some fun ways to manipulate infinite sequences
  
                 from __future__ import generators

                 def foo():
             i = 0
             while 1:
         yield i
         i = i 
 

Recursive Generators

     
     Generator functions can call themselves recursively.
 
                from __future__ import generators

                def abc():
            a = deff()
            for i in a:
     yield i
            yield 'abc'

               def deff():
           a = ijk()
           for i in a:
       yield i
              yield 'deff'

               def ijk():
           for i in (1,2,3):
       yield i
              yield 'ijk'

 

Thursday, 4 October 2012

Functional programming in Python

 

Functional Programming


           In Functional programming a problem can be decomposes into a set of functions. The functions take inputs and produce outputs, and don’t have any internal state that affects the output produced.Some of the examples off functional programming languages are Lisp,Scala,Scheme, Haskell, ML, OCAML, Clean, Mercury, or Erlang,etc

Pure Functions: Functions that have no side effects at all are called purely functional. Avoiding side effects means not using data structures that get updated as a program runs; every function’s output must only depend on its input.

First class function

          
        This is an example of first class function.he absence of any special notation; we are manipulating functions as if they were objects like arrays and numbers.    
>>>
>>>def cube(x): return x*x*x
...
>>>def sqr(x): return x*x
...
>>>cube
<function cube at 0xa1fb10c>
>>>a = [cube, sqr]
>>>a[0](2)
>>>def compose(f, g): return f(g(x))
...
>>>compose(cube, sqr, 3)
729
>>>

 

Higher Order Function

    
        A `function' (or a subroutine, subprogram, procedure) is considered to be a mechanism for capturing patterns.The ability to pass functions as arguments to functions greatly broadens the scope of this `pattern capturing' mechanism. Let's examine a simple function, `sum': 
 
                 def sum(a, b):
                    if (a > b): return 0
                    else: return a + sum(a+1, b)
                 def sigma(term, a, next, b):
                    if(a > b): return 0
                       return term(a) + sigma(term, next(a), next, b) 
                 def term(x): return 1.0/(x * (x+2))
                 def next(x): return x + 4
call the Function 
                 sigma(term, 1, next, 1000)
 
  

Using `lambda'

         
          The lambda keyword is used for creating anonymous functions. The body of a lambda should be composed of simple expressions only. In this example, The lambda is used to create a function which accepts an argument and returns it after multiplication.
>>>
>>>lambda y: y*10
<function <lambda;> at  0x9f751b4>
>>>f = lambda y: y*10
>>>f(10)
100
>>>

Closures 

     The main idea of Closures can be explained in this example

>>> def mul3(x): return lambda y: lambda z: x*y*z
>>> mul3(10)(2)(3)
60
>>>
 

Thursday, 27 September 2012

A Simple Python Program for Moving a Car

        This is a program in python for develop a GUI for moving car using buttons 

from Tkinter import *
import Tkinter as tk
class Carcontrol(Frame):
    def __init__(self,name):
        Frame.__init__(self,bg='white')
        self.pack(expand=YES,fill=BOTH)
        self.master.title("Control the CAR")       

        f=Frame(self,width=500,height=500,bg='white')
        f.pack()
       
        self.f1=Frame(f)
        self.f1.pack(side=TOP,padx=10,pady=10)
        f2=Frame(f)
        f2.pack(side=TOP)

        self.w=Canvas(self.f1)
        self.w.canvasx ( 500, gridspacing=None )
        self.w.canvasy ( 500, gridspacing=None )
        self.p=self.w.create_polygon( 50,50,100,50,125,75,25,75,fill="green")
        self.r=self.w.create_rectangle(10,75,140,100,fill="red")
        self.o1=self.w.create_oval(20,95,45,125,fill="black")
        self.o2=self.w.create_oval(105,95,130,125,fill="black")
        self.w.pack()
       
        b1=Button(f2,text="LEFT",command=self.left)
        b1.pack(side=LEFT)
   
        b2=Button(f2,text="RIGHT",command=self.right)
        b2.pack(side=RIGHT)

    def left(self):
        print "left"
        self.w.move(self.p,-10,0)
        self.w.move(self.r,-10,0)
        self.w.move(self.o1,-10,0)
        self.w.move(self.o2,-10,0)
       
    def right(self):
        print "right"
        self.w.move(self.p,10,0)
        self.w.move(self.r,10,0)
        self.w.move(self.o1,10,0)
        self.w.move(self.o2,10,0)
       
def main():
    Carcontrol("").mainloop()

if __name__=="__main__":
    main()





Tuesday, 11 September 2012

Constructing Procedures Using Lambda in Functional Programming

A powerful programming language is the ability to build abstractions by assigning names to common patterns and then to work in terms of the abstractions directly.Procedures provide this ability.Procedures that manipulate procedures are called higher-order procedures.

 The higher-order procedures can serve as powerful abstraction mechanisms, vastly increasing the expressive power of our language

 (lambda (x) (+ x 4))

 and

 (lambda (x) (/ 1.0 (* x (+ x 2))))
.

This is pi-sum  procedure
  
(define (pi-sum a b)
  (if (> a b)
      0
      (+ (/ 1.0 (* a (+ a 2))) (pi-sum (+ a 4) b))))



pi-sum procedure can be expressed without defining any auxiliary procedures as


(define (pi-sum a b)
  (sum (lambda (x) (/ 1.0 (* x (+ x 2))))
       a
       (lambda (x) (+ x 4))
       b))


using lambda, we can write the integral procedure without having to define the auxiliary procedure add-dx:


(define (integral f a b dx)
  (* (sum f
          (+ a (/ dx 2.0))
          (lambda (x) (+ x dx))
          b)
     dx))


In general, lambda is used to create procedures in the same way as define, except that no name is specified for the procedure

(lambda (<formal-parameters>) <body>)

We can read a lambda expression as follows:
    (lambda             (x)             (+    x     4))
                                                
 the procedure   of an argument x  that adds  x and 4


a procedure as its value, a lambda expression can be used as the operator in a combination such as


((lambda (x y z) (+ x y (square z))) 1 2 3)
12