Blogs
Version Control using Git - A two-day open-house training by Mr. Noufal Ibrahim @ Bengaluru
My friend Mr. Noufal Ibrahim just announced a public (open-house) training on Version Control using Git at the Millenia, Bengaluru on October 22nd 2011 and October 23rd 2011.
More details at the following URL:
Git Training by Mr. Noufal Ibrahim
Please do spread the word around :-)
Weekend modular training program on Core Python Programming
Hi All,
On behalf of Slashprog Technologies, I’m organizing a 40 hour
weekend training program on Core Python programming starting
from February 5th, 2011 (Saturday). The details of this
training program are as below:
Course duration: 40 hours (5 hours per module x 8 weekends)
Course span: 2 months, 8 weekends (approximately).
The training program will be conducted on every Saturday
between 9:00 AM to 2:00 PM
The first introductory training module is FREE for all and is
scheduled on February 5th, 2011 (Saturday) between
9:00 AM to 2:00 PM at our office venue.
Kindly do refer to your friends/colleagues who might be
interested in learning Python programming.
For more details, kindly visit the following URL:
Core Python Training program @ Slashprog
Regards,
Chandrashekar Babu.
Extending Python using C - A open-house training by Mr. Noufal Ibrahim @ Bengaluru
My friend Mr. Noufal Ibrahim just announced a public (open-house) training on Extending Python using C at Regus Business Centre - MG Road, Bengaluru on January 8th 2011 and January 9th 2011.
I personally know Mr. Noufal Ibrahim since the IRC days (year 2000) as a brilliant developer/programmer with passion for Python programming. He has contributed immensely to various projects on Python/related technology in FOSS. He is known for his active involvement in the Python community mainly in India, and has been the lead organizer of PyCon India Conference (2009, 2010).
More details about his training at this URL: http://nibrahim.net.in/trainings/python-extensions.html
To know more about him - kindly visit http://nibrahim.net.in/
Trainer's Union project has finally gathered momentum!
It was an idea of mine about 3 years back to create a business networking vertical around trainers and the training industry - to get trainers from all over the world (technical and non-technical trainers) into a community where they can interact and to provide a list of usability features (schedule calendar, contact network, references, profile and so on) and address common issues that most trainers would face in day-to-day activities. In simple words - my plan was to create a resource-hub site for trainers.
Now, with a team of people that I trained @ Slashprog Technologies, this project has gathered momentum. I'm happy that each team member has been motivated to take this up, learn challenging technologies, apply and try the same.
It's been 10 days now, and things are starting to shape up pretty well. Of course, the best part of this exercise is to ensure that each team member learn a lot of technologies, apply agile project development methodologies (XP, SCRUM, TDD) to get this project shaped up.
The TU project should ideally become the show-case for technical talent, hard-work and dedication of the team in general. Once the initial sprint is complete (Trainer component) - we should be getting the site live on a Staging server for extensive testing and continue development - adding features, fixing bug/issues sprint by sprint.
The team members have started to hack on Dojo Toolkit, JSON and 960.gs/Blueprint.css for the front end design/development. I was initially planning on plain PHP+SQLite/MySQL for the backend - but later thought of using CodeIgniter. But now I'm contemplating on using Rails3 for the backend/middle-tier. We are still on process of deciding the same.
Though my schedule is still hectic (hopping cities for corporate training assignments) - Mr. Thyagarajan Shanmugham has joined aboard to keep the wheel spinning while I'm away. A lot of developments are on pipe-line - time is the only limit.
Meanwhile, I've updated my friend's links - to blogs of my team members @ Slashprog
My company website is largely functional now
Finally, I managed to put together all pending efforts to get my company website up and working. Well - its in a perpetual beta mode right now. A lot of work is still pending. But it must be largely usable right now (with some rough edges - of course).
As an update, I will be conducting full-time (8 hours stretch) fast track training programs on a couple of technologies starting this month. More information about it can be found at http://www.slashprog.com/
Be sure to tune into http://www.slashprog.com/events.html to get the latest updates on training programs that I conduct at my office premises.
God bless!
Ruby sample scripts - Active records
add-cities.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
class City < ActiveRecord::Base ; end
City.create(:name => "Chennai", :country => "India")
City.create(:name => "Sydney", :country => "Australia")
add-people.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
class Person < ActiveRecord::Base
belongs_to :city
has_and_belongs_to_many :book
end
class Book < ActiveRecord::Base
has_and_belongs_to_many :person
end
class City < ActiveRecord::Base
has_many :person
end
Person.create(:name => "John", :city =>
City.find(:first, :conditions =>
{ :name => "Sydney" }))
Book.create(:name => "Programming in Ruby", :isbn => "343434")
create-books.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
ActiveRecord::Base.connection.create_table :books do |t|
t.column :name, :string, :length=>32
t.column :isbn, :string, :length=>10
end
create-person.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
ActiveRecord::Base.connection.create_table :city do |t|
t.column :name, :string, :length=>32
t.column :country, :string
end
ActiveRecord::Base.connection.create_table :people do |t|
t.column :name, :string, :length => 32
t.references :city
end
entries.rb
require 'ActiveRecord'
class Entries < ActiveRecord::Base
end
entry = Entry.new
entry.name = "John"
entry.comments = "What a great Web site!"
entry.save
entry = Entry.find(1)
puts entry.name # output: "John"
find-cities.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "root",
:password => "welcome"
)
class City < ActiveRecord::Base ; end
my_city = City.find(:first, :conditions => {:name => "Chennai" })
print my_city.name
print my_city.country
people-books.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "devel",
:password => "welcome"
)
class Person < ActiveRecord::Base
belongs_to :city
end
class City < ActiveRecord::Base
has_many :person
end
Person.create(:name => "John", :city =>
City.find(:first, :conditions =>
{ :name => "Sydney" }))
simple.rb
require 'rubygems'
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:database => "rails_learn",
:user => "devel",
:password => "welcome"
)
class Rubyist < ActiveRecord::Base
end
Rubyist.create(:name => 'Luc Juggery', :city => "Nashville, Tenessee")
Rubyist.create(:name => 'Sunil Kelkar', :city => "Pune, India")
Rubyist.create(:name => 'Adam Smith', :city => "San Fransisco, USA")
participant = Rubyist.find(:first)
puts %{#{participant.name} stays in #{participant.city}}
#Rubyist.find(:first).destroy
Ruby sample scripts - Looping constructs
loop1.rb
#!/usr/bin/env ruby
for x in 1..10
puts "Counting " + x.to_s
end
loop2.rb
#!/usr/bin/env ruby
for x in 1...10
puts "Counting " + x.to_s
end
loop3.rb
#!/usr/bin/env ruby
10.times do |i|
puts "Counting #{i}"
end
loop4.rb
#!/usr/bin/env ruby
(1..10).each {|i| puts "Counting #{i}" }
loop5.rb
#!/usr/bin/env ruby
1.upto(10) {|i| puts "Counting #{i}" }
loop6.rb
#!/usr/bin/env ruby
# Checking whether a number is prime can be
# much simpler than this. But this example demonstrates
# loop construct and duck-typing.
# A simple duck-typing example.
# Add a new method/functionality to an existing class
class Fixnum
def is_prime?
(2..Math.sqrt(self).floor).each {|x| return false if self % x == 0 }
return true
end
end
loop do
print "Enter a number (0 to exit): "
num = gets.to_i
break if num == 0
puts "#{num} is" + (num.is_prime? ? " " : " NOT ") + "prime."
end
loop7.rb
puts "Ho! Ho! Ho!" while (Time.now.min == 15)
#while (Time.now.min == 15)
# puts "Ho! Ho! Ho!"
#end
num1.rb
#!/usr/bin/env ruby
10.times do
puts "Hello"
end
num2.rb
#!/usr/bin/env ruby
10.times do |x|
puts "Counting " + x.to_s
end
Ruby sample scripts - file operations
calc-dir-size.rb
#!/usr/bin/env ruby
size = 0
Dir.open(ARGV[0]).each { |file| size += File.size? ARGV[0] + "/" + file }
puts size
calc-dir-size.sh
#!/bin/bash
SIZE=0
DIR=$1
for file in $DIR/*
do
((SIZE+=$(stat -c "%s" "$file")))
done
echo $SIZE
calc-dir-size2.rb
#!/usr/bin/env ruby
# Run as below:
# ls -l /bin | ruby calc-dir-size2.rb
total = 0
STDIN.read.each {|line| total += line.split(" ")[4].to_i }
puts total
calc-dir-size2.sh
#!/bin/sh
DIR=$1
expr $(ls -l $DIR | sed 's/ \+/ /g' | cut -f5 -d' ' | xargs | sed 's/ / + /g')
calc-dir-size3.sh
#!/bin/sh
DIR=$1
ls -l $DIR | awk '{ total += $5 } END { print total }'
check-file.rb
#!/usr/bin/env ruby
if ARGV.length < 1
puts "Enter filename/path: "
file = gets
else
file = ARGV[0]
end
# Generic redundant code below. Commented.
#puts "File #{file} exists." if File.exist? file
#puts "File #{file} is a regular file." if File.file? file
#puts "File #{file} is a directory." if File.directory? file
#puts "File #{file} is a symlink." if File.symlink? file
#puts "File #{file} is a block device file." if File.blockdev? file
#puts "File #{file} is a character device file." if File.chardev? file
#puts "File #{file} is a socket file." if File.socket? file
#puts "File #{file} is a FIFO file." if File.pipe? file
#puts "-" * 40
#puts "File #{file} is readable." if File.readable? file
#puts "File #{file} is writable." if File.writable? file
#puts "File #{file} is executable/searchable." if File.executable? file
#puts "File #{file} has SETUID-bit set." if File.setuid? file
#puts "File #{file} has SETGID-bit set." if File.setgid? file
#puts "File #{file} has sticky-bit set." if File.sticky? file
#puts "File #{file} is owned by current effective user." if File.owned? file
#puts "File #{file} is owned by current effective group." if File.grpowned? file
#puts "File #{file} is empty." if File.zero? file
# Smarter code below.
file_methods = {
:exist? => "exists",
:file? => "is a regular file",
:directory? => "is a directory",
:symlink? => "is a symbolic link",
:blockdev? => "is a block device file",
:chardev? => "is a character device file",
:pipe? => "is a FIFO file",
:socket? => "is a UNIX socket file",
:readable? => "is readable",
:writable? => "is writable",
:executable? => "is executable/searchable",
:setuid? => "has set-UID bit set",
:setgid? => "has set-GID bit set",
:sticky? => "has sticky bit set",
:owned? => "is owned by current effective user",
:grpowned? => "is owned by current effective group",
:zero? => "is an empty file"
}
file_methods.each {|method, message| puts "File #{file} #{message}" if File.send method, file }
chmod.rb
#!/usr/bin/env ruby
File.chmod 0644, "test.txt"
cp.rb
#!/usr/bin/env ruby
# Validations... validations... validations...
if ARGV.length < 2
STDERR.write "usage: #{$0} source-file destination-file.\n"
exit 1
end
unless File.file? ARGV[0]
STDERR.write "#{$0}: #{ARGV[0]}: not a regular file.\n"
exit 2
end
if File.exists? ARGV[1]
print "Destination #{ARGV[1]} already exists. Overwrite (y/n) ? "
exit 3 unless gets.upcase.start_with? "Y"
end
# Phew, this is where the actual work gets done.
# The commented code below is the pythonic way of doing it.
# File.open(ARGV[0]) do |src|
# File.open(ARGV[1], "w") do |dst|
# dst.write src.read
# end
# end
# The code below is the Rubyist way of doing it ;-)
puts "Copied #{File.open(ARGV[1], "w").write(File.open(ARGV[0]).read)} bytes from #{ARGV[0]} to #{ARGV[1]}"
delete-file.rb
#!/usr/bin/env ruby
if ARGV.length < 1
print "Enter file to delete: "
file = gets.chomp!
else
file = ARGV[0]
end
File.delete file
listdir.rb
d = Dir.new(ARGV[0])
d.each {|file| puts file }
recursive-copy.rb
#!/usr/bin/env ruby
require 'Fileutils'
if ARGV.length < 2
STDERR.write "usage: #{$0} source-file destination-file.\n"
exit 1
end
unless File.file? ARGV[0]
STDERR.write "#{$0}: #{ARGV[0]}: not a regular file.\n"
exit 2
end
if File.exists? ARGV[1]
print "Destination #{ARGV[1]} already exists. Overwrite (y/n) ? "
exit 3 unless gets.upcase.start_with? "Y"
end
Fileutils.cp_r ARGV[0], ARGV[1]
rename-file.rb
#!/usr/bin/env ruby
if ARGV.length < 2
STDERR.write "usage: #{$0} source-file destination-file.\n"
exit 1
end
unless File.file? ARGV[0]
STDERR.write "#{$0}: #{ARGV[0]}: not a regular file.\n"
exit 2
end
if File.exists? ARGV[1]
print "Destination #{ARGV[1]} already exists. Overwrite (y/n) ? "
exit 3 unless gets.upcase.start_with? "Y"
end
File.rename ARGV[0], ARGV[1]
scan-setuid-files.rb
#!/usr/bin/env ruby
Dir.open(ARGV[0]).each { |file| puts file if File.setuid? ARGV[0] + "/" + file }
Ruby sample scripts - sockets (examples from Ruby manual)
clnt.rb
# socket example - client side
# usage: ruby clnt.rb [host] port
require "socket"
if ARGV.length >= 2
host = ARGV.shift
else
host = "localhost"
end
print("Trying ", host, " ...")
STDOUT.flush
s = TCPSocket.open(host, ARGV.shift)
print(" done\n")
print("addr: ", s.addr.join(":"), "\n")
print("peer: ", s.peeraddr.join(":"), "\n")
while line = gets()
s.write(line)
print(s.readline)
end
s.close
svr.rb
# socket example - server side
# usage: ruby svr.rb
# this server might be blocked by an ill-behaved client.
# see tsvr.rb which is safe from client blocking.
require "socket"
gs = TCPserver.open(0)
addr = gs.addr
addr.shift
printf("server is on %s\n", addr.join(":"))
socks = [gs]
loop do
nsock = select(socks);
next if nsock == nil
for s in nsock[0]
if s == gs
ns = s.accept
socks.push(ns)
print(s, " is accepted\n")
else
if s.eof?
print(s, " is gone\n")
s.close
socks.delete(s)
# single thread gets may block whole service
elsif str = s.gets
s.write(str)
end
end
end
end
tsvr.rb
# socket example - server side using thread
# usage: ruby tsvr.rb
require "socket"
gs = TCPserver.open(0)
addr = gs.addr
addr.shift
printf("server is on %s\n", addr.join(":"))
loop do
Thread.start(gs.accept) do |s|
print(s, " is accepted\n")
while line = s.gets
s.write(line)
end
print(s, " is gone\n")
s.close
end
end