nginx

sudo apt-get install nginx

sudo service nginx stop

sudo nginx -c $PWD/nginx_keep_alive.conf

######### To run Nginx as a “100% transparent” proxy for Elasticsearch ###############

events {
    worker_connections  1024;
}
http {
  server {
    listen 8080;
    location / {
      proxy_pass http://localhost:9200;
    }
  }
}



######################## Persistent Connection ##########################

events {
    worker_connections  1024;
}

http {

  upstream elasticsearch {
    server localhost:9200;

    keepalive 15;
  }

  server {
    listen 8080;

    location / {
      proxy_pass http://elasticsearch;
      proxy_http_version 1.1;
      proxy_set_header Connection "Keep-Alive";
      proxy_set_header Proxy-Connection "Keep-Alive";
    }

  }

}

################### Load Balancer - Round Robin Fashion ##########################




events {
    worker_connections  1024;
}

http {

  upstream elasticsearch {
    server 127.0.0.1:9200;
    server 127.0.0.1:9201;
    server 127.0.0.1:9202;

    keepalive 15;
  }

  server {
    listen 8080;

    location / {
      proxy_pass http://elasticsearch;
      proxy_http_version 1.1;
      proxy_set_header Connection "Keep-Alive";
      proxy_set_header Proxy-Connection "Keep-Alive";
    }

  }

}

Logstash

# Source the following file to setup the java version correctly
    source ~/sourceme (to setup the java version correctly)

# Logstash to try from command line
    logstash -e 'input { stdin { } } output { stdout {} }'


# Logstash to try from command line
    logstash -e 'input { stdin { } } output { stdout { codec => rubydebug } }'

    logstash -e 'input { stdin { } } output { elasticsearch { hosts => localhost } }'


# Check if Logstash has been able to insert data into the diretory
    curl -XGET "http://localhost:9200/_cat/indices?pretty

# Run
    logstash -e 'input { stdin { } } output { elasticsearch { hosts => localhost } }'

# List the indices 
    curl -XGET "http://localhost:9200/_cat/indices?pretty"

# After it is started enter “Hello Your Name”



# Run -	
    curl -XGET "http://localhost:9200/logstash-*/_search"?pretty


# Download sample log file -
    cd /home/woir/Downloads/ && wget https://download.elastic.co/demos/logstash/gettingstarted/logstash-tutorial.log.gz
    gunzip logstash-tutorial.log.gz


# Save  following file in your home directory as apache.conf


########## apache.conf ##############
input {
    file {
        path => "/home/woir/Downloads/logstash-tutorial.log"
        start_position => beginning
        sincedb_path => "/dev/null"
        ignore_older => 0
    }
}

filter {
    grok {
        match => { "message" => "%{COMBINEDAPACHELOG}"}
    }
    date {
        match => [ "timestamp" , "dd/MMM/yyyy:HH:mm:ss Z" ]
    }
    geoip {
        source => "clientip"
    }
}

output {
    elasticsearch {
        index=>"gabbar"
    }
}

# Start pipeline 
    logstash -f ~/apache.conf


# See the contents –
    curl -XGET http://localhost:9200/logstash-2015.01.04/_search




######## Apple Stocks Hands on session  ####### 


1. Download the data to be inserted into ES
    wget -O /home/woir/Downloads/table-3.csv http://woir.in/wp-content/uploads/2016/12/table-3.csv





## save the following file as /home/woir/apple.conf
input {
  file {
    path => "/home/woir/Downloads/table*.csv"
    type => "core2"
    start_position => "beginning"    
  }
}
filter {
  csv {
      separator => ","
      columns => ["Date","Open","High","Low","Close","Volume","Adj Close"]
  }
  mutate {convert => ["High", "float"]}
  mutate {convert => ["Open", "float"]}
  mutate {convert => ["Low", "float"]}
  mutate {convert => ["Close", "float"]}
  mutate {convert => ["Volume", "float"]}
}
output {  
    elasticsearch {
        action => "index"
        hosts => ["localhost:9200"]
        index => "stock"
        workers => 1
    }
    stdout {}
}

# Point the config file and run the logstash – it will insert data into elasticsearch
    /home/woir/logstash-5.0.2/bin/logstash -f  /home/woir/apple.conf

# Check data insertion is done or not –
    curl -XGET http://localhost:9200/stock/_search?pretty

Elasticsearch Queries

### Run from your terminal 
## it tells if elasticsearch is up or not
### 
GET /

### Create Index
PUT /govtcompany

### Get Status
GET /govtcompany1?pretty

### Get List of Indices 
GET /_cat/indices?pretty&v

GET /_cat/indices/com*?v&s=index


### Delete and Index-
DELETE /govtcompany
GET /_cat/indices?pretty

### Create index named “company”
PUT /company

### Close Index
POST /company/_close
GET /_cat/indices?pretty

#### a simple search/index on the index which is closed will lead to the exception

GET /company/_search

POST /company/_open
 
######### Index not found exception
POST /myindex1/_close
POST /myindex1/_open

### Open Index
POST /company/_open
GET /_cat/indices?pretty

### Aliases
GET /_cat/aliases?v

DELETE /test1
DELETE /test2
DELETE /test3
DELETE /test4


### Please Create Index test1,test2, test3, test4
PUT /test1
PUT /test2
PUT /test3
PUT /test4
GET /_cat/indices
GET /_cat/aliases

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test1",
        "alias": "alias1"
      }
    }
  ]
}

GET /_cat/aliases

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test1",
        "alias": "alias2"
      }
    }
  ]
}

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test3",
        "alias": "alias1"
      }
    }
  ]
}

POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "test1",
        "alias": "alias1"
      }
    }
  ]
}

POST /_aliases
{
  "actions": [
    {
      "add": {
        "indices": [
          "test1",
          "test2"
        ],
        "alias": "alias1"
      }
    }
  ]
}
######### OK use regular expression
POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test*",
        "alias": "alias1"
      }
    }
  ]
}


POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "test1",
        "alias": "alias1"
      }
    }
  ]
}

######## use regular expression
POST /_aliases
{
  "actions": [
    {
      "remove": {
        "index": "test*",
        "alias": "alias1"
      }
    }
  ]
}

######## will come back to this later - very intereting scenario here
### Filter Alias 
PUT /test1
{
  "mappings": {
    "type1": {
      "properties": {
        "user": {
          "type": "keyword"
        }
      }
    }
  }
}

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test1",
        "alias": "alias2",
        "filter": {
          "term": {
            "user": "kimchy"
          }
        }
      }
    }
  ]
}

DELETE /test1

PUT /test1
{
  "mappings": {
    "type1": {
      "properties": {
        "user": {
          "type": "keyword"
        }
      }
    }
  }
}

POST /test1/type1
{
  "user": "Amar Sharma1",
  "age": 45,
  "experience": 10
}

POST /test1/type1
{
  "user": "Amar Sharma2",
  "age": 45,
  "experience": 10
}

POST /test1/type1
{
  "user": "Amar Sharma3",
  "age": 45,
  "experience": 10
}

POST /test1/type1
{
  "user": "kimchy",
  "age": 45,
  "experience": 10
}

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "test1",
        "alias": "alias2",
        "filter": {
          "term": {
            "user": "kimchy"
          }
        }
      }
    }
  ]
}

GET /alias2/_search

### Data Insertion#########
###### schemaless
POST /company/employee
{
  "name": "Amar Sharma",
  "age": 45,
  "experience": 10
}


###### record recovery using id

GET company/employee/AV_ksw2CXaNB5r6xnSki


### Routing
PUT /my_index

PUT /my_index/my_type/1?routing=user1&refresh=true&pretty
{
  "title": "This is a document"
}

GET /my_index?pretty


PUT /my_index/my_type/1?routing=user2&refresh=true&pretty
{
  "title": "This is a document"
}

GET /my_index/my_type/1?routing=user1&pretty
GET /my_index/my_type/1?&pretty

PUT /test1/type1/1?routing=user1&refresh=true&pretty
{
  "user": "Srinivas1"
}

PUT /test1/type1/1?routing=user1&refresh=true&pretty
{
  "user": "Srinivas"
}

GET /test1/type1/1

GET /test1/type1/1?routing=amar

GET /test1/type1/1?routing=user3

DELETE company

PUT /company
{
  "mappings": {
    "employee2": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    }
    
  }
}

DELETE company

GET /company?pretty

### Create Mappings ( multiple )
PUT /company
{
  "mappings": {
    "employee": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    },    
    "staff": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    }
    
  }
}

GET /company
### Get the mappings
GET /company?pretty

########### Data Insertion 

PUT /company1
{
  "mappings": {
    "employee": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    }
  }
}

GET /company1/employee/_mapping

GET /company/_search

GET /company1/_search

DELETE /company

PUT /company
{
  "mappings": {
    "employee": {
      "properties": {
        "age": {
          "type": "long"
        },
        "experience": {
          "type": "long"
        },
        "name": {
          "type": "string",
          "analyzer": "standard"
        }
      }
    }
  }
}

POST /company/employee
{
  "name": "Amar Sharma",
  "age": 45,
  "experience": 10
}

POST /company/employee
{
  "name": "Sriknaht Kandi",
  "age": 35,
  "experience": 7
}

POST /company/employee
{
  "name": "Abdul Malik",
  "age": 25,
  "experience": 3
}

######## to specific ID ####


####### auto generated id
POST /company/employee/
{
  "name": "Amar3 Sharma",
  "age": 45,
  "experience": 10
}


POST /company/employee/2
{
  "name": "Amar3 Sharma",
  "age": 45,
  "experience": 10
}

##### let me repeat the above one and notie the version
POST /company/employee/2
{
  "name": "Amar3 Sharma",
  "age": 45,
  "experience": 10
}



GET /company/employee/2


POST /company/employee/2
{
  "name": "Amar3 Sharma",
  "age": 45
}

PUT /company/employee/2
{
  "name": "Anant Sharma",
  "age": 45,
  "FatherName": "Amar Sharma"
}

PUT /company/employee/3
{
  "name": "Anant Sharma1",
  "age": 45,
  "FatherName": "Amar Sharma"
}

PUT /company/employee/4
{
  "name": "Amar5 Sharma",
  "age": 45,
  "FatherName": "Suresh Chandra"
}

PUT /company1/employee/6
{
  "name": "Amar6 Sharma6",
  "age": 45,
  "FatherName": "Suresh Chandra"
}

###### in place updates #######

POST /company/employee/2/_update
{
  "script": "ctx._source.name='Anant2'",
  "retry_on_conflict": 3
}

###########Analyzer

POST /_analyze
{
  "analyzer": "whitespace",
  "text":     "The quick brown fox!"
}

POST /_analyze
{
  "analyzer": "standard",
  "text":     "The quick brown fox."
}

POST /_analyze
{
  "tokenizer": "standard",
  "filter":  [ "lowercase", "asciifolding" ],
  "text":      "Is this déja vu? A quick fox jump over lazy pumped"
}

POST /_analyze
{
  "tokenizer": "standard",
  "filter":  [ "lowercase", "asciifolding","stemmer" ],
  "text":      "Is this déja vu? A quicks foxes jumped over lazy dog"
}

POST /_analyze
{
  "tokenizer": "standard",
  "filter":  [ "synonym", "asciifolding" ],
  "text":      "Is this déja vu?"
}
POST _analyze
{
  "analyzer": "simple",
  "text":     "The 5 quic4k brown fox."
}

POST /_analyze
{
  "analyzer": "simple",
  "text":     "Time2School."
}

##########SEARCH

GET /company/employee/_search?pretty

GET /company*/staff,employee/_search?pretty

GET /_search
{
  "query": {
    "match_all": {}
  }
}

GET /company/employee/_search?pretty=true
{
  "query": {
    "query_string": {
      "query": "Amar"
    }
  }
}

GET /_search?pretty
{
  "query": {
    "match": {
      "name": "Amar Sharma"
    }
  }
}

GET /company/employee/_search?pretty=true
{
  "query": {
    "query_string": {
      "query": "Amar*"
    }
  }
}

########## careful, now filtered is no more supported
GET /company/employee/_search?pretty=true
{
  "query": {
    "filtered": {
      "filter": {
        "term": {
          "name": "Amar"
        }
      }
    }
  }
}

GET /company/employee/_search?pretty
{
  "query": {
    "range": {
      "age": {
        "from": 15,
        "to": 50
      }
    }
  }
}

GET /company/employee/_search?pretty
{
  "query": {
    "range": {
      "age": {
        "from": 26,
        "to": 50
      }
    }
  }
}

GET /company/employee/_search?pretty
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "kandi"
          }
        },
        {
          "match": {
            "name": "Anant Sharma"
          }
        }
      ],
      "must": {
        "range": {
          "age": {
            "from": 45,
            "to": 55
          }
        }
      },
      "minimum_should_match": 2
    }
  }
}

GET /_search?pretty
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "Anant"
        }
      },
      "should": {
        "range": {
          "age": {
            "gte": 46
          }
        }
      }
    }
  }
}


GET /_search?pretty=true
{
  "query": {
    "match": {
      "name": {
        "query": "Anant Sharma",
        "operator": "or"
      }
    }
  }
}


GET /_search?pretty=true
{
  "query": {
    "match": {
      "name": {
        "query": "Anant Sharma",
        "operator": "and"
      }
    }
  }
}

POST /company/employee
{
  "name": "Abdul8 Malik8",
  "username": "abdul2013",
  "age": 25,
  "experience": 3,
  "date": "2013-02-01",
  "FatherName" : "@Amar"
}

POST /company/employee
{
  "name": "Abdul9 Malik9",
  "username": "abdul 2013",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "@Amar"
}

POST /_search
{
  "query": {
    "term" : { "username" : "abdul2013" } 
  }
}

POST /_search
{
  "query": {
    "term" : { "username" : "abdul 2013" } 
  }
}

GET /company/_search?pretty=true
{
  "query": {
    "bool": {
      "should": [
        {
          "term": {
            "username": "Amar"
          }
        },
        {
          "term": {
            "username": "abdul2013"
          }
        }
      ],
      "minimum_should_match": 0

    }
  }
}

GET /company/_search 
GET /company,company1/_search 
GET /company*/_search 
GET /company/employee/_search 
GET /company/staff,employee/_search 
GET /company/employee/_search 
GET /_all/employee/_search 

GET /company/_search?size=5&from=0 
GET /company/_search?size=5&from=5 
GET /company/_search?size=5&from=10 

GET /_search?q=name:Sharma
GET /_search?q=name:Amar5
GET /_search?q=name:Amar

POST /company/employee
{
  "name": "Abdul2 Malik2",
  "age": 25,
  "experience": 3,
  "date": "2013-04-01"
}
POST /company/employee
{
  "name": "Abdul3 Malik3",
  "age": 25,
  "experience": 3,
  "date": "2013-03-01"
}

POST /company/employee
{
  "name": "Abdul4 Malik4",
  "age": 25,
  "experience": 3,
  "date": "2013-02-01"
}

GET /_search?q=+date:>2013-03-01+name:Amar

POST /company/employee
{
  "name": "Abdul4 Malik4",
  "age": 25,
  "experience": 3,
  "date": "2013-02-01",
  "FatherName" : "@Amar"
}

######## are same #######
GET /_search?q=_all:Amar
GET /_search?q=Amar

############################

POST /company/employee
{
  "name": "Abdul7 Malik7",
  "username": "abdul2013",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "@Amar"
}

POST /company/employee
{
  "name": "Abdul7 Malik7",
  "username": "abdul2013",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "@Amar"
}

POST /company/employee
{
  "name": "Abdul8 Malik8",
  "username": "abdul2013",
  "age": 25,
  "experience": 3,
  "date": "2013-02-01",
  "FatherName" : "@Amar"
}

POST /company/employee
{
  "name": "Abdul9 Malik9",
  "username": "abdul 2013",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "@Amar"
}

GET /company/_mapping

GET /company/_search?q=2013

GET /company/_search?q=date:2013

POST /company/employee
{
  "name": "Andrew Thomas",
  "username": "Abdul9 Malik9",
  "age": 25,
  "experience": 3,
  "date": "2014-02-01",
  "FatherName" : "David Knol"
}

GET /_search?pretty
{
  "query": {
    "bool": {
      "must": {
        "match": {
          "name": "Andrew Gabbar"
        }
      },
      "should": {
        "range": {
          "age": {
            "gte": 15
          }
        }
      }
    }
  }
}




POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "myindex",
        "alias": "alias1"
      }
    }
  ]
}

PUT /myapp
{
  "mapping": {
    "tweet": {
      "properties": {
        "tweet": {
          "type": "string"
        },
        "name": {
          "type": "string"
        },
        "nick": {
          "type": "string"
        },
        "Date": {
          "type": "date", "format": "yyyy-MM-dd HH:mm:ss"
        },
        "rt": {
          "type": "long"
        }
      }
    }
  }
}

POST _analyze
{
  "analyzer": "whitespace",
  "text":     "The Brown-Cow's Part_No. #A.BC123-456 joe@bloggs.com"
}

Synopsys




GET _search
{
 "query": {
 "match_all": {}
 }
}

POST /_aliases
{
 "actions": [
 {
 "add": {
 "index": "myindex",
 "alias": "alias1"
 }
 }
 ]
}

DELETE /test1

PUT /test1
{
 "mappings": {
 "type1": {
 "properties": {
 "user": {
 "type": "keyword"
 }
 }
 }
 }
}

POST /test1/type1
{
 "user": "Amar Sharma1",
 "age": 45,
 "experience": 10
}


POST /test1/type1
{
 "user": "Amar Sharma2",
 "age": 45,
 "experience": 10
}

POST /test1/type1
{
 "user": "Amar Sharma3",
 "age": 45,
 "experience": 10
}

POST /test1/type1
{
 "user": "kimchy",
 "age": 45,
 "experience": 10
}

POST /_aliases
{
 "actions": [
 {
 "add": {
 "index": "test1",
 "alias": "alias2",
 "filter": {
 "term": {
 "user": "kimchy"
 }
 }
 }
 }
 ]
}

GET /alias2/_search

PUT /test1/type1/1?routing=user1&refresh=true&pretty
{
 "user": "Srinivas"
}

PUT /test1/type1/1?routing=user1&refresh=true&pretty
{
 "user": "Srinivas"
}

GET /test1/type1/1

GET /test1/type1/1?routing=amar

GET /test1/type1/1?routing=user1


PUT /company1
{
 "mappings": {
 "employee": {
 "properties": {
 "age": {
 "type": "long"
 },
 "experience": {
 "type": "long"
 },
 "name": {
 "type": "string",
 "analyzer": "standard"
 }
 }
 }
 }
}

GET company1/employee/_mapping

GET company/_search

GET company1/_search

DELETE company

PUT /company
{
 "mappings": {
 "employee": {
 "properties": {
 "age": {
 "type": "long"
 },
 "experience": {
 "type": "long"
 },
 "name": {
 "type": "string",
 "analyzer": "standard"
 }
 }
 }
 }
}
 
POST /company/employee
{
 "name": "Amar Sharma",
 "age": 45,
 "experience": 10
}

POST /company/employee
{
 "name": "Sriknaht Kandi",
 "age" : 35,
 "experience" : 7
 }

POST /company/employee
{
 "name": "Abdul Malik",
 "age" : 25,
 "experience" : 3,
 }

PUT /company/employee/2
{
 "name": "Amar3 Sharma",
 "age" : 45,
 "experience" : 10
 }
GET /company/employee/2


PUT /company/employee/2
{
 "name": "Amar3 Sharma",
 "age" : 45
 }

PUT /company/employee/2
{
 "name": "Anant Sharma",
 "age" : 45,
 "FatherName": "Amar Sharma"
 }
PUT /company/employee/3
{
 "name": "Anant Sharma1",
 "age" : 45,
 "FatherName": "Amar Sharma"
 }
 
PUT /company/employee/4
{
 "name": "Amar5 Sharma",
 "age" : 45,
 "FatherName": "Suresh Chandra"
 }
 
PUT /company1/employee/6
{
 "name": "Amar6 Sharma6",
 "age" : 45,
 "FatherName": "Suresh Chandra"
 }
 
 
POST /company/employee/2/_update
{
"script": "ctx._source.age+=1", 
"retry_on_conflict": 3
}

GET /company/employee/_search?pretty

GET /company/employee/_search?pretty=true
{ 
 "query": {
 "query_string": {
 "query": "Amar"
 }
 }
}

GET /_search?pretty
{
 "query": {
 "match": {
 "name": "Amar Sharma"
 }
 }
 }

GET /company/employee/_search?pretty=true
{ 
 "query": {
 "query_string": {
 "query": "Amar*"
 }
 }
}
 
GET /test-data/cities/_search?pretty=true
{
 "query": {
 "filtered": {
 "filter": {
 "term": {
 "name": "Amar"
 }
 }
 }
 }
}

GET /company/employee/_search?pretty
{
 "query": {
 "range": {
 "age": {
 "from": 15,
 "to": 50
 }
 }
 }
}
 
GET /company/employee/_search?pretty
{
 "query": {
 "range": {
 "age": {
 "from": 26,
 "to": 50
 }
 }
 }
}


GET /company/employee/_search?pretty
{
 "query": {
 "bool": {
 "should": [
 {
 "match": {
 "name": "kandi1"
 }
 },
 {
 "match": {
 "name": "Anant Sharma"
 }
 }
 ],
 "must": {
 "range": {
 "age": {
 "from": 45,
 "to": 55
 }
 }
 },
 "minimum_should_match": 1
 }
 }
}


GET /_search?pretty
{
 "query": {
 "bool": {
 "must": {
 "match": {
 "name": "Anant"
 }
 },
 "should": {
 "range": {
 "age": {
 "gte": 46
 }
 }
 }
 }
 }
}


GET /index/_search 
GET /index1,index2/_search 
GET /company*/_search 
GET /index/type/_search 
GET /index/type1,type2/_search 
GET /index/type*/_search 
GET /_all/type*/_search


GET /company/_search?size=5&from=0 
GET /company/_search?size=5&from=5 
GET /company/_search?size=5&from=10

GET /_search?q=name:Sharma
GET /_search?q=name:Amar5
GET /_search?q=name:Amar

POST /company/employee
{
 "name": "Abdul2 Malik2",
 "age": 25,
 "experience": 3,
 "date": "2013-04-01"
}
POST /company/employee
{
 "name": "Abdul3 Malik3",
 "age": 25,
 "experience": 3,
 "date": "2013-03-01"
}

POST /company/employee
{
 "name": "Abdul4 Malik4",
 "age": 25,
 "experience": 3,
 "date": "2013-02-01"
}

GET /_search?q=+date:>2013-03-01+name:Amar

POST /company/employee
{
 "name": "Abdul4 Malik4",
 "age": 25,
 "experience": 3,
 "date": "2013-02-01",
 "FatherName" : "@Amar"
}

######## are same #######
GET /_search?q=_all:Amar
GET /_search?q=Amar

############################

POST /company/employee
{
 "name": "Abdul7 Malik7",
 "username": "abdul2013",
 "age": 25,
 "experience": 3,
 "date": "2014-02-01",
 "FatherName" : "@Amar"
}

POST /company/employee
{
 "name": "Abdul7 Malik7",
 "username": "abdul2013",
 "age": 25,
 "experience": 3,
 "date": "2014-02-01",
 "FatherName" : "@Amar"
}

POST /company/employee
{
 "name": "Abdul8 Malik8",
 "username": "abdul2013",
 "age": 25,
 "experience": 3,
 "date": "2013-02-01",
 "FatherName" : "@Amar"
}

POST /company/employee
{
 "name": "Abdul9 Malik9",
 "username": "abdul 2013",
 "age": 25,
 "experience": 3,
 "date": "2014-02-01",
 "FatherName" : "@Amar"
}

GET company/_mapping

GET company/_search?q=2013

GET company/_search?q=date:2013

POST /company/employee
{
 "name": "Andrew Thomas",
 "username": "Abdul9 Malik9",
 "age": 25,
 "experience": 3,
 "date": "2014-02-01",
 "FatherName" : "David Knol"
}

GET /_search?pretty
{
 "query": {
 "bool": {
 "must": {
 "match": {
 "name": "Andrew Gabbar"
 }
 },
 "should": {
 "range": {
 "age": {
 "gte": 15
 }
 }
 }
 }
 }
}

GET /_search?pretty=true
{
 
"query": { 
 "match": { 
 "name": { 
 "query": "Anant Sharma", 
 "operator": "or" 
 } 
 } 
}
}

POST _search
{
 "query": {
 "term" : { "username" : "abdul 2013" } 
 }
}

GET company/_search?pretty=true
{ 
 "query": { 
 "bool": { 
 "should": [ 
 { "term": { "username": "Amar" }}, 
 { "term": { "username": "abdul2013"}} 
 ] 
 }
 } 
}


PUT /myapp
{
 "mapping": {
 "tweet": {
 "properties": {
 "tweet": {
 "type": "string"
 },
 "name": {
 "type": "string"
 },
 "nick": {
 "type": "string"
 },
 "Date": {
 "type": "date", "format": "yyyy-MM-dd HH:mm:ss"
 },
 "rt": {
 "type": "long"
 }
 }
 }
 }
}

POST _analyze
{
 "analyzer": "whitespace",
 "text": "The quick brown fox."
}

POST _analyze
{
 "tokenizer": "standard",
 "filter": [ "lowercase", "asciifolding" ],
 "text": "Is this déja vu? A quick fox jump over lazy pumped"
}

POST _analyze
{
 "tokenizer": "standard",
 "filter": [ "lowercase", "asciifolding","stemmer" ],
 "text": "Is this déja vu? A quick fox jumped over lazy dog"
}

POST _analyze
{
 "tokenizer": "standard",
 "filter": [ "synonym", "asciifolding" ],
 "text": "Is this déja vu?"
}
POST _analyze
{
 "analyzer": "simple",
 "text": "The 5 quic4k brown fox."
}

POST _analyze
{
 "analyzer": "simple",
 "text": "Time2School."
}

Python Map Reduce Program

Mapper.py

#!/usr/bin/env python

import sys

for line in sys.stdin:

  line = line.strip()

  words = line.split()

for word in words:

  print '%s\t%s' % (word, 1)

Reducer.py

#!/usr/bin/env python

from operator import itemgetter

import sys

current_word = None

current_count = 0

word = None

# input comes from STDIN

for line in sys.stdin:

    line = line.strip()

    word, count = line.split('\t', 1)

    # convert count (currently a string) to int

    try:

        count = int(count)

    except ValueError:

        continue

    if current_word == word:

        current_count += count

    else:

        if current_word:

            print '%s\t%s' % (current_word, current_count)

        current_count = count

        current_word = word

if current_word == word:

    print '%s\t%s' % (current_word, current_count)

 

 

 

How to run –

  • Unix
    • echo “foo foo quux labs foo bar quux” | ~/Mapper.py
    • echo “foo foo quux labs foo bar quux” | ~/Mapper.py | sort -k1,1 | ~/Reducer.py
  • Hadoop
    • hadoop dfs -mkdir -p /tmp/synopsys/input
    • hadoop dfs -copuyFromLocal ~/example/WordCount1/file* /tmp/synopsys/input
    • hadoop dfs -ls  /tmp/synopsys/input/
    • /home/woir/hadoop-2.6.0/bin/hadoop jar /home/woir/hadoop-2.6.0/share/hadoop/tools/lib/hadoop-streaming-2.6.0.jar \
      -file /home/woir/Mapper.py -mapper /home/woir/Mapper.py \
      -file /home/woir/Reducer.py -reducer /home/woir/Reducer.py \
      -input /tmp/synopsys/input/* -output /user/amar/gutenberg-output1

 

 

 

Python Exercise 3

-------> file name hello.py
def foo():
    print "Hello Amar Sharma"

if __name__ == "__main__":
 foo()
 Exercise: Take the following list, nlis, and compute its average. That is, write a function 'average(numlis)' that uses a 'for' loop to sum up the numbers in numlis and divide by the length of numlis. Just to be sure that you got all the numbers in numlis, print each one in your 'for' loop and print the length of the the list. nlis = [2,4,8,105,210,-3,47,8,33,1] # average should by 41.5 Exercise : Write a function multiply(num1,num2) which prints the multiplication of the two numbers. Careful to take care of the input type.

------------------- Sample for Debugging and Profiler ---------------------
import re

import sys 

import time

def if_statement():

 """ Three slightly difference versions of if: if, if-else, if-elif-else"""

 time.sleep(1)

 x = 5

 y = 0

 z = 0

 if x > 0:

     print "x is positive" 

 if y > 0:

     print "y is positive" 

 else:

     print "y is not positive" 

 # elif can be repeated as often as necessary 

 if z > 0:

     print "z is positive" 

 elif z < 0:

     print "z is negative" 

 else:

     print "z must be 0" 

def foo():

    print "Hello Amar Sharma"

def hello():

 """ prints hello, world """

 print("Hello, world!")

if __name__ == "__main__":

 foo()

 hello()

 if_statement()




Sample Output 
  

Anants-MacBook-Pro:pythonexamplesset1 amars$ python -m pdb /tmp/d.py 
> /tmp/d.py(1)<module>()

-> import re

(Pdb) n

> /tmp/d.py(2)<module>()

-> import sys

(Pdb) n

> /tmp/d.py(3)<module>()

-> import time

(Pdb) n

> /tmp/d.py(5)<module>()

-> def if_statement():

(Pdb) n

> /tmp/d.py(28)<module>()

-> def foo():

(Pdb) n

> /tmp/d.py(30)<module>()

-> def hello():

(Pdb) n

> /tmp/d.py(34)<module>()

-> if __name__ == "__main__":

(Pdb) n

> /tmp/d.py(35)<module>()

-> foo()

(Pdb) s

--Call--

> /tmp/d.py(28)foo()

-> def foo():

(Pdb) s

> /tmp/d.py(29)foo()

-> print "Hello Amar Sharma"

(Pdb) s

Hello Amar Sharma

--Return--

> /tmp/d.py(29)foo()->None

-> print "Hello Amar Sharma"

(Pdb) s

> /tmp/d.py(36)<module>()

-> hello()

(Pdb) s

--Call--

> /tmp/d.py(30)hello()

-> def hello():

(Pdb) s

> /tmp/d.py(32)hello()

-> print("Hello, world!")

(Pdb) s

Hello, world!

--Return--

> /tmp/d.py(32)hello()->None

-> print("Hello, world!")

(Pdb) s

> /tmp/d.py(37)<module>()

-> if_statement()

(Pdb) s

--Call--

> /tmp/d.py(5)if_statement()

-> def if_statement():

(Pdb) s

> /tmp/d.py(8)if_statement()

-> time.sleep(1)

(Pdb) s

> /tmp/d.py(9)if_statement()

-> x = 5

(Pdb) p x

*** NameError: NameError("name 'x' is not defined",)

(Pdb) n

> /tmp/d.py(10)if_statement()

-> y = 0

(Pdb) p x

5

(Pdb) h

Documented commands (type help <topic>):

========================================

EOF    bt         cont      enable  jump  pp       run      unt   

a      c          continue  exit    l     q        s        until 

alias  cl         d         h       list  quit     step     up    

args   clear      debug     help    n     r        tbreak   w     

b      commands   disable   ignore  next  restart  u        whatis

break  condition  down      j       p     return   unalias  where 

Miscellaneous help topics:

==========================

exec  pdb

Undocumented commands:

======================

retval  rv

(Pdb) q
 

Python Exercise 2

------> file name hello.py
def foo():
    print "Hello Amar Sharma"

if __name__ == "__main__":
 foo()



Exercise-
Using List Comprehension get the list of vowels in word "INDIAN".

-------------------sample----------------------------
# Python Module example


def add(a, b):
   """This program adds two
   numbers and return the result"""

   result = a + b
   return result

Exercise - Make a module named mymath and add remaining function 
( multiply, division, subtraction in the module and call them 
in your program using following ways -
1. import mymath
2. from mymath import add,subtraction,division, multiplicaiton
3. from mymath import *


-------------------- sample class --------------------

class ComplexNumber:
 def __init__(self,r = 0,i = 0):
 self.real = r
 self.imag = i

 def getData(self):
 print("{0}+{1}j".format(self.real,self.imag))

# Create a new ComplexNumber object
c1 = ComplexNumber(2,3)

# Call getData() function
# Output: 2+3j
c1.getData()

Exercise - Create a class Employee with data members name, address and salary. 
Provide a method to display the salary and name of the employee. In the main 
program instantiate an object of the class and demonstrate the use of the class.


--------------------sample---------------------------------

Let's say you have a class ClassA which contains a method methodA defined as:

def methodA(self, arg1, arg2):
 # do something
and ObjectA is an instance of this class.
Now when ObjectA.methodA(arg1, arg2) is called, python internally converts it for you as:
ClassA.methodA(ObjectA, arg1, arg2)
The self variable refers to the object itself.
Exercise
Please read through a text file, split each line into its constituent
words, add each word to a dictionary, then add one to the number of times the
word occurs in the dictionary. Finally, we sort the dictionary and print it
out listing each word (key) and its count (value). 

def count_words(filename):
Reads through a text file and counts the number of appearances of each word.
 
import sys

filename = sys.argv[1]
# print("\n",filename,"\n") # You can check that the filename is correct
 
text_file = open(filename) # open the file for reading

# Set up an empty dictionary to start a standard design pattern loop
words_dic = {}

# This loop adds each word to the dictionary and updates its count. Change 
# all words to lower case so Horse and horse are seen as the same word.
for line in text_file: # step through each line in the text file
 for word in line.lower().split(): # split into a list of words
 word = word.strip("'?,.;!-/\"") # strip out the stuff we ignore
 if word not in words_dic:
 words_dic[word] = 0 # add word to words with 0 count
 words_dic[word] = words_dic[word] + 1 # add 1 to the count

text_file.close() 
 
# Sorts the dictionary words into a list and then print them out
print("List of words in the file with number of times each appears.")
word_list = sorted(words_dic)
for word in word_list:
 print(words_dic[word], word)
 
# Sorts the dictionary words into a list and then print them out
print("List of words in the file with number of times each appears.")
word_list = sorted(words_dic)
for word in word_list:
 print(words_dic[word], word)
#%% 


Python Exercise 1

 

----------------- sample -----------------------
def hello():
 """ prints hello, world """
 print "Hello, world!"
 
def areacircle(radius):
 """ Computes the area of a circle of the given radius """
 area = 3.14*radius**2
 print "The area of a circle of radius",radius,"is", area 

"""
Exercise:
Write a function 'def areatriangle(b,h)' to compute the area
of a triangle: formula is area = .5*b*h.
Output should look like:
The area of a triangle of base 3 and height 5 is 7.5
-------------------------------------------------------------------
Exercise: 
Print following using print on your screen.

name = "His name is Conan O'Brien"
cat = 'My cat is named "Butters"'


--------------------------------sample-----------------------------------------------------
def fahrenheit_to_celsius(temp):
 """ Converts Fahrenheit temperature to Celsius. 
 Formula is 5/9 of temp minus 32 """
 # Note that this line is not executed
 # end='' keeps print from starting a new line.
 newTemp = 5*(temp-32)/9
 print "The Fahrenheit temperature",temp,"is equivalent to",newTemp,end='' 
 print " degrees Celsius"
 
#

"""
Exercise:
Write a function 'def celsius_to_fahrenheit(temp)' to convert Celsius
to Fahrentheit temperature. The formula is (9/5) times temp plus 32. 
Print the output in the form:
The Celsius temperature 50.0 is equivalent to 122.0 degrees Fahrenheit.
"""

----------------------------------sample --------------------------------------
def name():
 """ Input first and last name, combine to one string and print """
 fname = input("Enter your first name: ")
 lname = input("Enter your last name: ")
 fullname = fname + " " + lname

 print "Your name is:", fullname


Exercise:
Extend the name function written in class to include the city and state.
That is, ask two more questions to get the city and the state you live in.
Print where you are from on a new line. Put the customary comma between
city and state. to save time, here is the starting function.
Your run should look like the following (even if this is not the customary
way in your country):
Enter your first name: Bill

Enter your last name: Boyd

Enter the city you live in: Middletown

Enter the state you live in: CT
Your name is: Bill Boyd
You live in: Middletown, CT

-------------------------------- sample -------------------------------
def if_statement():
 """ Three slightly difference versions of if: if, if-else, if-elif-else"""
 x = 5
 y = 0
 z = 0
 if x > 0:
     print "x is positive" 
 
 if y > 0:
     print "y is positive" 
 else:
     print "y is not positive" 
 
 # elif can be repeated as often as necessary 
 if z > 0:
     print "z is positive" 
 elif z < 0:
     print "z is negative" 
 else:
     print "z must be 0" 
 
Exercise:
Write a function absolutevalue(num) that computes the absolute value of
a number. You will need to use an 'if' statement. Remember if a number is 
less than zero then you must multiply by -1 to make it greater than zero.
Give output in the form:
The absolute value of -5 is 5

------------------------------------------------ sample -----------------------
The 'while' loop. Loops are used to repeat actions and the scope of this
repetition is indicated by the indention after the 'while' statement.
"""
#
def cheer():
 """ Prints 2 4 6 8, who do we appreciate .... Note that everything in 
 the while loop is indented. The first line not indented is the first
 line following the while loop. """
 ct = 2
 while ct <= 8:
     print ct,end=" "  # end = " " keeps from starting a new line
     ct = ct + 2
 print  # now we'll start a new line
 print "Who do we appreciate?" 
 print "Knowledge!" 
 
#
"""
Exercise:
Write a function count_down() that starts at 10 and counts down to rocket 
launch. It's output should be 10 9 8 7 6 5 4 3 2 1 BLASTOFF! You can make
all the numbers on the same line or different lines. Use a while loop.
"""
-------------------------------------------------------------------------
def cheer2():
 """ Same as cheer, but uses a for loop and range()
 range uses a start number, a stop number and a step size. """
 for ct in range(2,9,2):
     print ct,end=' ' 
     print
     print "Who do we appreciate?" 
     print "Engineers"

Exercise:
Write a function countdown1() that starts at 10 and counts down to rocket 
launch. It's output should be 10 9 8 7 6 5 4 3 2 1 BLASTOFF! You can make
all the numbers on the same line or different lines. Use a 'for' loop and
range(). range has a start and a stop and a step that MAY BE NEGATIVE.
"""

------------------------------------------ sample -----------------------

Some of our exercises involve finding and fixing errors in code. 
Here is an example. Can you see the errors (there are two)? Note that the
editor is pointing out one line with troubles.
You can find the error by reading the example carefully.
def favorite():
 my_toy = input("What is my favorite toy? ")
  print "Your favorite toy is", my-toy

Python

Exercises at -
Python Exercise 1
Python Exercise 2
Python Exercise 3
----> hello.py
import re

def foo():
    print "Hello Amar Sharma"

if __name__ == "__main__":
 foo()

TRY FROM REPL

Strings examples

>> name1 = "John"
 >>> name2 = "Doe"
 >>> "My name is %s " % name1
 'My name is John '
 >>> name1 + " " + name2
 'John Doe'
 >>> if "J" in name1:
 ...     print "Yes, J's there"
 ...
 Yes, J's there

Strings examples

>> for i in name1:
 ...     print i
 ...
 J
 o
 h
 n
 >>>name1.replace("h","a")
 'Joan'
 >>> name1
 'John'

Strings examples

name1.find("hn")
 2
 >>> list(name1)
 ['J', 'o', 'h', 'n']
 >>> name1*4
 'JohnJohnJohnJohn'
 >>> foo = "Python:Perl:Ruby:TCL:Shell"
 >>> foo.split(":")
 ['Python', 'Perl', 'Ruby', 'TCL','Shell']

List examples

foo = ["J","o","h","n"]
 >>> foo
 ['J', 'o', 'h', 'n']
 >>> foo.append(" ")
 >>> foo
 ['J', 'o', 'h', 'n', ' ']
 >>> foo.extend(["D","o","e"])
 >>> foo
 ['J', 'o', 'h', 'n', ' ', 'D', 'o', 'e']
 >>> "".join(foo)
 'John Doe'
 >>> " ".join(foo)
 'J o h n D o e'
 >>> foo.reverse()
 >>> foo

['e', 'o', 'D', ' ', 'n', 'h', 'o', 'J']

>> foo = ["J","o","h","n"]
 >>> for i in foo:
 ... print foo.index(i),i
 ...
 0 J
 1 o
 2 h
 3 n
 >>> foo
 ['J', 'o', 'h', 'n']
 >>> foo[2]
 'h'
 >>> foo[2]="a"
 >>> foo
 ['J', 'o', 'a', 'n']
 >>> foo.sort()
 >>> foo
 ['J', 'a', 'n', 'o']
 >>> foo.pop()
 'o'
 >>> foo.pop()
 'n'
 >>> foo.pop()
 'a'
 >>> foo.pop()
 'J'
 >>> foo
 []

>> foo = "This is the last time I'm going to say this".split()
 >>> foo
 ['This', 'is', 'the', 'last', 'time', "I'm", 'going', 'to', 'say', 'this']
 >>> foo[2]
 'the'
 >>> foo[2:6]
 ['the', 'last', 'time', "I'm"]
 >>> foo[-2]
 'say'
 >>> foo[:-2]
 ['This', 'is', 'the', 'last', 'time', "I'm", 'going', 'to']
 >>> foo[4:]
 ['time', "I'm", 'going', 'to', 'say', 'this']
 >>>

Dictionary examples

>> os = { "Microsoft" : "Windows",
 ...    "Sun" : "Solaris",
 ...    "Bell labs" : "Plan 9",
 ...    "SGI" : "Irix"}
 >>>
 >>> os
 {'Bell labs': 'Plan 9', 'Sun': 'Solaris', 'SGI': 'Irix', 'Microsoft': 'Windows'}

for i in os.keys():
 ...    print i,os[i]
 ...
 Bell labs Plan 9
 Sun Solaris
 SGI Irix
 Microsoft Windows
 >>>
 >>> os.has_key("Apple")
 False
 >>> "Apple" in os
 False
 >>> "Sun" in os
 True
 >>> os.values()
 ['Plan 9', 'Solaris', 'Irix', 'Windows']
 >>> os.keys()
 ['Bell labs', 'Sun', 'SGI', 'Microsoft']
 >>> os2 = {"FSF": "Gnu/Linux",
 ... "Apple" : "OSX"}
 >>> os2
 {'Apple': 'OSX', 'FSF': 'Gnu/Linux'}
 >>> os.update(os2)
 >>> for i in os.keys():
 ... print i,":",
 ...
 Bell labs : SGI : Apple : FSF : Sun : Microsoft :
 >>>

Tuples

>> tuple("Python")
 ('P', 'y', 't', 'h', 'o', 'n')
 >>> foo = (1,2,3)
 >>> foo[1] = 7
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 TypeError: 'tuple' object does not support item assignment
 >>> foo = (1,)
 >>> type(foo)
 <type 'tuple'>
 >>>

Files

>> foo = open("/home/mohan/ip.php")
 >>> foo.read(20)
 '<?php\necho $_SERVER['
 >>> foo.readline()
 "'REMOTE_ADDR'];\n"
 >>> foo.readline()
 '\n'
 >>> foo.readline()
 '$url = json_decode(file_get_contents("http://api.ipinfodb.com/v3/ip-city/?key=<your_api_key>&ip=".$_SERVER[\'REMOTE_ADDR\']."&format=json"));\n'
 >>> x=foo.readlines()
 >>> len(x)
 23
 >>> x[4]
 'echo "<table border=\'1\' width=\'50%\' align=\'center\'><tr><td>COUNTRY:</td><td>";\n'
 >>> foo.close()
 >>> foo.read()
 Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 ValueError: I/O operation on closed file
 >>>

Idea of python objects

  • >> foo = “Wozniak”
>>> bar = foo
 >>> id(foo)
 139807526833968
 >>> id(bar)
 139807526833968
 >>> baz = "Wozniak"
 >>> id(baz)
 139807526833968
 >>>

Idea of python objects

>> foo = range(1,100)
 >>> bar = range(1,100)
 >>> id(foo)
 139807526796536
 >>> id(bar)
 139807526796320
 >>> foo is bar
 False
 >>> foo == bar
 True
 >>>

Assignments

>> foo = ("Wozniak","Apple","Designer")
 >>> name,company,position = foo
 >>> name
 'Wozniak'
 >>> position
 'Designer'
 >>> foo,bar = 1,2
 >>> foo,bar = bar,foo
 >>> foo
 2
 >>> bar
 1
 >>>
  • Multiple targets
>> a=b=1
 >>> a
 1
 >>> b
 1
 >>>
  • Augmented assignments
>> foo=1
 >>> foo+=1
 >>> foo
 2
 >>>

Conditional Branching statements

  • If statements
     >>> foo = 11
    
     >>> if foo < 9:
    
     ...    rint "Less than nine"
    
     ... elif foo > 10:
  • ...    print "Greater than 10"
  •  ... else:
    
     ...   print "Between 9 and 10"
    
     ...
    
     Greater than 10
  • Multiway branching with dictionaries
> choice = "eggs"
 >>> print {"spam" : 10,
 ... "eggs" : 20,
 ... "veg" : 15}[choice]
 20
 >>>

Indefinite iteration

>> while 1:
 ... x = next()
 ... if not x: break
 ... process(x)
 ...

Definite iteration

>> for i in range(1,10):
 ... print i,":",
 ...
 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 :

Zip Function

>> l1 = [1,3,5,7,9]
 >>> l2 = [2,4,6,8,10]
 >>> for (x,y) in zip(l1,l2):
 ... print "(%d,%d) : " % (x,y),
 ...
 (1,2) : (3,4) : (5,6) : (7,8) : (9,10) :
 >>>

Example Function

>> def thrice(arg):
 ... return arg*3
 ...
 >>> thrice(4)
 12
 >>> thrice([1,2,3])
 [1, 2, 3, 1, 2, 3, 1, 2, 3]
 >>> thrice("Ni!")
 'Ni!Ni!Ni!'
 >>>

Functions…

>> foo = thrice
 >>> foo(4)
 12

Ref or Value ?

> bar = [1,2,3,4,5]
 >>> foo(bar)
 >>> bar
 [1, 2, 1, 4, 5]
 >>> bar = [1,2,3,4,5]
 >>> foo(bar[:]) #Passing a copy
 >>> bar
 [1, 2, 3, 4, 5]
 >>>
  • Arguments by name
> def fn(name,company,designation):
 ... print "%s works at %s as an %s" % (name,company,designation)
 ...
 >>> fn("Noufal","Synopsys","Engineer")
 Noufal works at Synopsys as an Engineer
 >>> fn(company = "Synopsys",name = "Noufal",designation="Engineer")
 Noufal works at Synopsys as an Engineer
 >>>
  • Default arguments
>> def fn(name,age=25):
 ... print "%s is %d years old" % (name,age)
 ...
 >>> fn("John")
 John is 25 years old
 >>> fn("John",30)
 John is 30 years old
 >>>
  • Variable argument lengths by position
>> def fn(*foo):
 ...     for i in foo:
 ...         print i," ",
 ...
 >>> fn(1,2,3,4,5,6,7)
 1 2 3 4 5 6 7
 >>>
  • Variable argument lengths by keyword
>> def fn(**foo):
 ...     for i in foo.keys():
 ...         print i,":",foo[i]
 ...
 >>> fn(name="John",age="50")
 age : 50
 name : John
 >>>

Modules

>> import display
 >>> display.fn("John",40)
 John is 40 years old

>> from display import *
 >>> fn("John",40)
 John is 40 years old

>> from display import fn
 >>> fn("John",40)
 John is 40 years old
  • Module concepts
>> mod = __import__("display")
 >>> mod.fn("John",50)
 John is 50 years old
  • Some standard modules
>> import os
 >>> foo = os.popen("/bin/ls")
 >>> foo
 <open file '/bin/ls', mode 'r' at 0x127860>
 >>> op = foo.read()
 >>> op
 '#.bbdb#\n#.newsrc-dribble#\nMail\nNews\nStarOffice52\naudio...'
 >>> files = op.split()
 >>> for i in files:
 ... print i
 ...
 #.bbdb#
 #.newsrc-dribble#
 Mail
 News

>> import sre
>>> numerical = sre.compile(“[0-9]+”)
>>> sstring = “Apple PowerBook for just 66000”
>>> foo = numerical.search(sstring)
>>> foo
<_sre.SRE_Match object at 0x1a16e8>
>>> foo.group()
‘66000‘
>>>

>>> import pickle
 >>> spam = {"Wall" : "Perl", "Guido" : "Python", "Matsumoto" : "Ruby"}
 >>> spam.keys()
 ['Wall', 'Matsumoto', 'Guido']
 >>> foo = open("/u/noufal/ldict.pkl","w")
 >>> pickle.dump(spam,foo)
 >>> foo.close()
 >>> del(spam)
 >>> foo = open("/u/noufal/ldict.pkl","r")
 >>> ham = pickle.load(foo)
 >>> ham
 {'Wall': 'Perl', 'Matsumoto': 'Ruby', 'Guido': 'Python'}

Classes and OOP

  • Classes behaviour
>> class FirstClass:
 ... def setdata(self,value):
 ... self.data = value
 ... def display(self):
 ... print self.data
 ...
 >>> x = FirstClass()
 >>> y = FirstClass()
 >>> x.setdata("Eric Idle")
 >>> x.display()
 Eric Idle
 >>> dir(x)
 ['__doc__', '__module__', 'data', 'display', 'setdata']
 >>>
  • Inheritance
>> class SecondClass(FirstClass):
 ... def display(self):
 ... print "Current value is %s" % str(self.data)
 ...
 >>> x = SecondClass()
 >>> x.setdata("King Arthur")
 >>> x.display()
 Current value is King Arthur
>> class ThirdClass(SecondClass):
 ... def __init__(self,value):
 ... self.data = value
 ... def __add__(self,other):
 ... return ThirdClass(self.data + other)
 ...
 >>> a = ThirdClass("abc")
 >>> a.display()
 Current value is abc
 >>> b = a + "123"
 >>> b.display()
 Current value is abc123
 >>>
  • Classes odds and ends
>> getattr(b,"data")
 'abc123'
 >>> setattr(b,"data","xzy")
 >>> b.data
 'xzy'
 >>> b.display()
 Current value is xzy
  • Exceptions

 

> foo = [1,2,3,4,5]
 >>> foo[10]
 Traceback (most recent call last):
 File "<stdin>", line 1, in ?
 IndexError: list index out of range
 >>> try:
 ... foo[10]
 ... except IndexError:
 ... print "Index too large."
 ...
 Index too large.
  • Raising your own exceptions

 

> foo = "FunError"
 >>> raise foo
 Traceback (most recent call last):
 File "<stdin>", line 1, in ?
 FunError
 >>> raise foo,"This is what you get for erroring out"
 Traceback (most recent call last):
 File "<stdin>", line 1, in ?
 FunError: This is what you get for erroring out
  • Documenting code

 

> def fn(name,age=30):
 ... "Displays persons name and age. Age defaults to 30."
 ... print "%s is %d years old" % (name,age)
 ...
 >>> fn("John")
 John is 30 years old
 >>> print fn.__doc__
 Displays persons name and age. Age defaults to 30.
  • Functional programming primitives
>> foo = lambda x:x*3
 >>> foo(2)
 6

>> apply (lambda x:x*100,(2,))
 200

>> foo = [1,2,3,4,5,6,7]
 >>> map(lambda x:x*2,foo)
 [2, 4, 6, 8, 10, 12, 14]
  • Functional programming continued
>> filter (lambda x:x%2==0,range(20))
 [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
 reduce: Apply function on sequence elements pairwise
 >>> reduce(lambda x,y:x+y,range(10))
 45
 >>> foo = 0
 >>> for i in range(10):
 ... foo += i
 ...
 >>> print foo
 45

>> def create_incrementer(inc):
 ... return lambda x:x+inc
 ...
 >>> inc3=create_incrementer(3)
 >>> inc3(10)
13
  • List comprehensions
>> [x*2 for x in range(1,10)]
 [2, 4, 6, 8, 10, 12, 14, 16, 18]
 >>> [x**2 for x in range(1,10) if x%2 == 0]
 [4, 16, 36, 64]
 >>> [(x,x**2) for x in range(1,10) if x%2 == 0]
 [(2, 4), (4, 16), (6, 36), (8, 64)]
  • Generator functions
>> def gensq(n):
 ... for i in range(n):
 ... yield i ** 2
 ...
 >>> for i in gensq(5):
 ... print i,":",
 ...
 0 : 1 : 4 : 9 : 16 :
 >>> x = gensq(10)
 >>> x
 <generator object at 0x191da0>
 >>> x.next()
 0
 >>> x.next()
 1
 >>> x.next()
 4
  • Useful development stuff
>> import rlcompleter, readline
 >>> readline.parse_and_bind('tab: complete')
 >>> foo = "hello"
 >>> foo.<TAB><TAB>
 foo.__add__ foo.__lt__ foo.encode foo.replace
 foo.__class__ foo.__mod__ foo.endswith foo.rfind
 foo.__contains__ foo.__mul__ foo.expandtabs foo.rindex
 foo.__delattr__ foo.__ne__ foo.find foo.rjust
 foo.__doc__ foo.__new__ foo.index foo.rstrip
 foo.__eq__ foo.__reduce__ foo.isalnum foo.split
 foo.__ge__ foo.__reduce_ex__ foo.isalpha foo.splitlines
 foo.__getattribute__ foo.__repr__ foo.isdigit foo.startswith
 foo.__getitem__ foo.__rmod__ foo.islower foo.strip
 foo.__getnewargs__ foo.__rmul__ foo.isspace foo.swapcase
 foo.__getslice__ foo.__setattr__ foo.istitle foo.title
 foo.__gt__ foo.__str__ foo.isupper foo.translat
 foo.__hash__ foo.capitalize foo.join foo.upper
 foo.__init__ foo.center foo.ljust foo.zfill
 foo.__le__ foo.count foo.lower
 foo.__len__ foo.decode foo.lstrip

 

WOIR is Hiring Interns/Trainees from Ist Week of Oct, 2017

WOIR Software is looking for interns/trainees (one or two) for a period of approx. 2-3 months. The position is based in Hyderabad (Gachibowli). It has to be full time – starting from 1st week of October 2017. The work will include coding in python, shell, web based application and Big Data, NoSQL. He/she should be excellent in programming, logical thinking, data structures and good in Python. Should be willing to learn new technologies and should be excited to work in a startup. Hiring will include really tough interview where he/she will be asked to demonstrate the logical thinking and programming skill. It is unpaid position, they would be given a valid certificate from private limited company for the period. Please write email to amar@woir.in (or call 9555295300 for any specific query) with your resume along with the reference (how you did you get to know about this position) if you want to apply for the position.