|
Monday, 19 December 2011 01:00 |
Trim: remove whitespace from the start and end(both sides) of the string.
LTrim: remove start whitespace of the string.
RTrim: remove end whitespace of the string.
Rather than using a clumsy loop, use a simple, elegant regular expression.
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
return this.replace(/\s+$/,"");
}
Example:-
// example of using trim, ltrim, and rtrim
var myString = " hello my name is imdadhusen ";
alert("*"+myString.trim()+"*");
alert("*"+myString.ltrim()+"*");
alert("*"+myString.rtrim()+"*");
 Read more: |
|
Friday, 25 November 2011 02:28 |
Prerequisites
Basic Ruby/Rails knowledge, including an installed version of Ruby 1.9.2, Rubygems, Bundler, and Rails 3.
Basic Git knowledge, including an installed version of Git.
Create a Heroku account http://www.heroku.com/
All the commands work on linux
1# Install the Heroku client:
$ gem install heroku
2# Write Your App
You may be starting from an existing app from http://dl.dropbox.com/u/261809/clothes_recommender.tar.gz. If not you can create your own
$ rails new myapp
$ cd myapp
3# Edit your Gemfile and change the line:
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
gem 'httparty'
4# Install Gems
$ bundle install --without production
5# Store Your App in Git
$ git init
$ git add .
$ git commit -m "init"
6# Deploy to Heroku/Cedar
Create the app on the Cedar stack:
$ heroku create --stack cedar
7# Creating the recommender:
* Create the models
create file app/models/google_weather.rb
class GoogleWeather
include HTTParty
base_uri "www.google.com"
attr_reader :long , :lat
def initialize(options)
@long = options[:long].to_f*1000000
@lat = options[:lat].to_f*1000000
end
def weather
@weather ||= self.class.get("/ig/api", :query => {:weather => ",,,#{@lat.to_i},#{@long.to_i}"}, :format => :xml)
end
def current_condition
@current_condition ||=@weather['xml_api_reply']['weather']['current_conditions']["condition"]["data"]
end
def current_temp
@current_temp ||= @weather['xml_api_reply']['weather']['current_conditions']["temp_f"]["data"]
end
def current_icon
@current_icon ||= "http://www.google.com/#{@weather['xml_api_reply']['weather']['current_conditions']["icon"]["data"]}"
end
def tomorrow_condition
@tomorrow_condition||=@weather['xml_api_reply']['weather']['forecast_conditions'][1]["condition"]["data"]
end
def tomorrow_high
@tomorrow_high||=@weather['xml_api_reply']['weather']['forecast_conditions'][1]["high"]["data"]
end
def tomorrow_low
@tomorrow_low||=@weather['xml_api_reply']['weather']['forecast_conditions'][1]["low"]["data"]
end
def tomorrow_avgtemp
@tomorrow_avgtemp||=(tomorrow_low.to_i+tomorrow_high.to_i)/2
end
end
create file app/models/clothes_recommender.rb
class ClothesRecommender
def self.get_recommendation(lat,long)
response={}
options={:lat=>lat,:long=>long}
gw=GoogleWeather.new(options)
gw.weather
response[:display_text]="Today's weather is #{gw.current_condition} at #{gw.current_temp}F, we recommend #{lookup(gw.current_condition,gw.current_temp)}"
response[:icon]=gw.current_icon
response
end
def self.lookup(condition,temp)
case condition.downcase
when "overcast"
return "you to carry an umberellea or rain coat"
when "rain"
return "not to forget your umberellea/rain coat"
when "mostly sunny"
return "take your umberellea/rain coat"
when "partly sunny"
return "Place holder text"
when "mostly cloudy"
return "Place holder text"
when "partly cloudy"
return "Place holder text"
when "clear"
return "Place holder text"
when "chance of rain"
return "Place holder text"
when "cloudy"
return "Place holder text"
end
case temp.to_i
when 81..100
return "you to wear light cotton clothing and bottle of water"
when 51..80
return "you to wear pants with a top and a lighter jacket"
when 41..50
return "you to take lighter jacket with you"
when 20..40
return "Place holder text"
when 0..30
return "Place holder text"
end
end
end
* Create controller
create file app/controller/recommender_controller.rb
class RecommenderController < ApplicationController
def index
@recommendations= ClothesRecommender.get_recommendation(params[:latitude].to_f,params[:longitude].to_f)
@recommendations[:details_url]="http://#{request.host_with_port}/recommender/details?latitude=#{params[:latitude]},longitude=#{params[:longitude]}"
end
def details
options={:lat=>params[:latitude],:long => params[:longitude]}
@gw=GoogleWeather.new(options)
@gw.weather
@today="Today's weather is #{@gw.current_condition} at #{@gw.current_temp}F, we recommend #{ClothesRecommender.lookup(@gw.current_condition,@gw.current_temp)}"
@tomorrow="Tomorrow's forecast #{@gw.tomorrow_condition} with High at #{@gw.tomorrow_high}F and Low at #{@gw.tomorrow_low}F, we recommend #{ClothesRecommender.lookup(@gw.tomorrow_condition,@gw.tomorrow_avgtemp)}"
end
end
* Create the views
return | tag
* Create xml builder
xml.instruct!
xml.dev_expert do
xml.display_text "#{@recommendations[:display_text]}"
xml.icon_hdpi_url "#{@recommendations[:icon]}"
xml.icon_mdpi_url "#{@recommendations[:icon]}"
xml.icon_ldpi_url "#{@recommendations[:icon]}"
xml.details_view_url "#{@recommendations[:details_url]}"
end
8# Deploy your code:
$ git push heroku master
When prompted for your heroku user name and password please enter Read more: |
|
|
Tuesday, 22 November 2011 07:47 |
// with few slight modifications (like s.recv -> s.read) you can run this script in pys60
// iam too lazy to add comments to this prog, since this is only snippets no comments
import socket
import time
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)#main socket
#***************************************
def sendcmd(cmd):
global s
s.send(cmd+"\r\n")
#***************************************
def getline():
global s
return s.recv(1024)
#***************************************
def execute(cmd):
sendcmd(cmd)
return getline()
#***************************************
#command executing sequence and delay is important in this module
def getpasv(cmd):
global s
ss=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#temporary socket for pasv connection.
a=execute("PASV")
print a
print "connecting to %s:%d" % parse(a)
ss.connect(parse(a))
sendcmd(cmd)
fhandle=open("d:\\a.rar","wb")#replace d: with e: for memory card if pys60
while 1:
tmp = ss.recv(1024)
if not tmp:
break;
fhandle.write(tmp)
fhandle.close()
ss.close()
time.sleep(1) #this delay is required
return getline()
#***************************************
def parse(st):
x=st[st.rindex("(")+1:st.rindex(")")]
y=x.split(",")
ipaddr=y[0]+"."+y[1]+"."+y[2]+"."+y[3]
portnum=(int(y[4])*256)+int(y[5])
return ipaddr,portnum
#***************************************
print "program started"
try:
s.connect(('ftp.myserver.com',21))
print getline()
print execute("USER shankar")
print execute("PASS support123")
print execute("CLNT FileZilla")
print execute("OPTS UTF8 ON")
print execute("CWD /Tools")
print execute("PWD")
print execute("TYPE I")
print getpasv("RETR datarecovery.rar")
print execute("QUIT")
s.close()
except socket.error, e:
print "ERROR: %s" % e
print "done!"
#***************************************
 Read more: |
|
Tuesday, 25 October 2011 21:52 |
public DataTable GetSelectedColumns(DataTable _dataTable)
{
string[] selectedColumns = new[] { "First_Name", "Salary" };
DataTable tableWithOnlySelectedColumns =
new DataView(_dataTable).ToTable(false, selectedColumns);
return tableWithOnlySelectedColumns;
}
 Read more: |
|