Skip to content
Jan 12 12

Query wiki from command line

by admin
#!/usr/bin/env python

__author__ = "Tom Luo"
__version__ = "$Revision 0.1 $"
__date__ ="$Date: 2012/01/11 11:11PM MST$"

import os, urllib, sys
import re

def f(s):
    return s.find('remove the lines that containt this string') < 0

#url = 'http://en.wikipedia.org/wiki/' + '_'.join(sys.argv[1:])
url = 'http://en.wikipedia.org/w/index.php?search=' + '+'.join(sys.argv[1:])
fobj= os.popen("lynx -dump -width=120 %s" % url)
output = fobj.read()
fobj.close()
start = output.find("From Wikipedia, the free encyclopedia")
end = output.find("Terms of use for details")
lines = output[start:end]
lines = reversed(re.split('\[\d+\]',lines))
lines = filter(f,lines)
print '.'.join(lines)

Save the above script as wiki
Examples

$chmod +x wiki
$wiki china
$wiki barack obama
$wiki austin tx
$wiki salem oregon
Jan 5 12

Google from command line

by admin
#!/usr/bin/env python

__author__ = "Tom Luo"
__version__ = "$Revision 0.1 $"
__date__ ="$Date: 2012/01/04 11:11PM MST$"

import os, urllib, sys
import re

def f(x):
    return x.find('Cached') < 0

filename = 'http://www.google.com/search?' + urllib.urlencode({'q': ' '.join(sys.argv[1:]) })
fobj= os.popen("lynx -dump -width=120 %s" % filename)
output = fobj.read()
fobj.close()
start = output.find("Web Results 1 - 10")
end = output.find("Searches related to:")
lines = output[start:end]
lines = reversed(re.split('\[\d+\]',lines))
lines = filter(f,lines)
print '.'.join(lines)
chmod +x g.sh
g.sh oracle
Dec 30 11

Install Shellinabox on Ubuntu

by admin

1. Download the installer
wget http://shellinabox.googlecode.com/files/shellinabox_2.10-1_i386.deb

2. Change the default port number if you like
/etc/default/shellinabox so that the default port is 443.

Then restart the shellinabox daemon:
sudo invoke-rc.d shellinabox restart

Access shellinabox server from my phone: https://your_home_internet_ip

Dec 28 11

Google-like spell suggestion

by admin

I was reading Peter Norvig’s article on Boxing Day.

I simply added the __main__ section and used it to do my spell checking from shell.

Download big.txt from here.

#!/usr/bin/env python

import re, collections

def words(text): return re.findall('[a-z]+', text.lower()) 

def train(features):
    model = collections.defaultdict(lambda: 1)
    for f in features:
        model[f] += 1
    return model

NWORDS = train(words(file('big.txt').read()))

alphabet = 'abcdefghijklmnopqrstuvwxyz'

def edits1(word):
   splits     = [(word[:i], word[i:]) for i in range(len(word) + 1)]
   deletes    = [a + b[1:] for a, b in splits if b]
   transposes = [a + b[1] + b[0] + b[2:] for a, b in splits if len(b)>1]
   replaces   = [a + c + b[1:] for a, b in splits for c in alphabet if b]
   inserts    = [a + c + b     for a, b in splits for c in alphabet]
   return set(deletes + transposes + replaces + inserts)

def known_edits2(word):
    return set(e2 for e1 in edits1(word) for e2 in edits1(e1) if e2 in NWORDS)

def known(words): return set(w for w in words if w in NWORDS)

def correct(word):
    candidates = known([word]) or known(edits1(word)) or known_edits2(word) or [word]
    return max(candidates, key=NWORDS.get)

if __name__ == "__main__":
    import sys
    import  os
    os.system('clear')
    if len(sys.argv) == 1 :
        print "usage: spell word"
    else:
        s = sys.argv[1]
        print s
        print "Did you mean %s?" % correct(s)
Nov 13 11

Access PI from Python

by admin

# Python PISDK test on Python 3.x
# Install Python extension for Windows

import win32com.client
import time

pi_sdk = win32com.client.Dispatch('PISDK.PISDK')
conn   = win32com.client.Dispatch('PISDKDlg.Connections')

pi_server = pi_sdk.Servers('my_pi_server') # PI server or collective name
conn.Login(pi_server,'pidemo','',1,0)

# using point tag: SINUSOID (pre-configured by default in demo PI server)
pi_point = pi_server.PIPoints['SINUSOID']

# retriving recorded data for the last 6 hours:
recorded_values = pi_point.Data.RecordedValues('*-2h','*',3,"",0,None)
time.sleep(0.1) # allow time for the call to complete.

print (recorded_values.Count)
for sample in recorded_values:
    v = sample.Value
    t = sample.TimeStamp.LocalDate
    print ("Value =",v, " (timestamp =",t,")")

# Print Snapshot value
pv = pi_point.Data.Snapshot
print (pv.Value)
Sep 1 11

Seconds since midnight

by admin

ORACLE:

select to_number(to_char(sysdate,'SSSSS')) from dual;

OSISofit PI Performance Equation
DaySec(‘*’)

Aug 18 11

shutdown – Ubuntu

by admin

sudo gedit /etc/crontab

– shutdown at 10:00pm with 1 minute notice.
0 22 * * * root shutdown -h +1

Jul 11 11

Firebird Recursive Query – Tree

by admin

Firebird Tree Query

Jul 11 11

Units conversion in Ubuntu

by admin

#sudo apt-get install units

.units
2411 units, 71 prefixes, 33 nonlinear units

You have: 16 meters
You want: feet
* 52.493438
/ 0.01905
You have:

Press Ctr + d to exit

Many more interesting examples

May 29 11

How to Compile and run C# application on Ubuntu

by admin

[sony]$cat hello.cs

using System;
public class ParentClass
{
public ParentClass()
{
Console.WriteLine("Parent Constructor.");
}
public void print()
{
Console.WriteLine("I'm a Parent Class.");
}
}

public class ChildClass : ParentClass
{
public ChildClass()
{
Console.WriteLine("Child Constructor.");
}
public static void Main()
{
ChildClass child = new ChildClass();
child.print();
}
}

[sony]$gmcs hello.cs

[sony]$mono hello.exe
Hello C#

May 25 11

VBScript DateDiff

by admin

DateDiff(interval,date1,date2[,firstdayofweek[,firstweekofyear]])

Notes:

1. interval ‘m’ stands for month, use ‘n’ for seconds

2. returns a negative nubmer if date1 < date2, otherwise, positve

Parameter Description
interval The interval you want to use to calculate the differences between date1 and date2
Can take the following values:

yyyy – Year
q – Quarter
m – Month
y – Day of year
d – Day
w – Weekday
ww – Week of year
h – Hour
n – Minute
s – Second

May 25 11

Replace multiple spaces with one space – Python

by admin
s=" <b>Jennifer</b> Lynn <b>Lopez</b> (born July 24, 1969), also known by    her nickname J.Lo, is an American actress,    singer"
" ".join(s.split())
May 12 11

GROUP BY in Pentaho – GROUP_CONAT in MySQL or LISTAGG in Oracle

by admin

Found a very useful feature in Pentaho Data Integration that is equivalent to the GROUP_CONAT function in MySQL or LISTAGG function in Oracle.
It can concatenate all rows into a string sepated by “,” or any character grouped by another field.

group_by

Apr 27 11

csharp on Ubuntu

by admin

Ubuntu comes with Tomboy, Gbrainy and F-Spot, and therefore comes with Mono installed by default.

Mono provides a complete CLR (Common Language Runtime) including compiler and runtime, which can produce and execute CIL (Common Intermediate Language) bytecode (aka assemblies), and a class library. It contains the interactive C# shell named csharp. csharp permits dynamically evaluating C# statements, and can be used for writing scripts or testing code fragments.

You can install it if it’s not there.

 sudo apt-get install mono-csharp-shell 
csharp> using System;
csharp> using System.Diagnostics;
csharp> Process[] runningProcs = Process.GetProcesses(".");
csharp> foreach (Process p in runningProcs)
      > {
      > string info = string.Format("{0} {1}", p.Id, p.ProcessName);
      > Console.WriteLine(info);
      > }

1 /sbin/init
2 kthreadd
3 ksoftirqd/0
4 migration/0
5 watchdog/0
6 events/0
7 cpuset
8 khelper
9 netns
10 async/mgr
11 pm
12 sync_supers
13 bdi-default
14 kintegrityd/0
15 kblockd/0
16 kacpid

Apr 20 11

OSIsoft PI String

by admin

Not sure why max size of string is 976 characters.
This is the best guess I can think of.

(314*3.14)-10
975.96

#or
pow(math.pi,2)*99
977.0908357078464
Apr 20 11

OSISoft PI Data Type of Point

by admin

Data Type of Point: PointType Attribute

Int16
Points with integer values between 0 and 32767 (15-bit unsigned integers).
Int32
Points with integer values between -2147450880 and 2147483647 (32-bit signed integers).

Python code

int('111111111111111',2)
32767
int('1'*15, 2)
32767

int('1'*31,2)
2147483647

bin(2147483647)
'0b1111111111111111111111111111111'
bin(2147483647).count('1')
31

bin(-2147450880)
'-0b1111111111111111000000000000000'

bin(-2147450880).count('1')
16
bin(-2147450880).count('0')
16
Apr 13 11

Maximum number of rows allowed in MS Excel 2007

by admin
pow(2,(8*2 + (2007-2003)))
# or
1 << (16 + (2007-2003))
# or
1 << 20

Excel 2003 = 65,536 rows
Excel 2007 = 1,048,576 rows

Mar 21 11

Describe tables/views in Firebird

by admin

To List all tables:


-- list all tables
show table
-- or

-- describe a table
show table TABLE_NAME

select rdb$relation_name
from rdb$relations
where rdb$view_blr is null
and (rdb$system_flag is null or rdb$system_flag = 0);

To List all views

select rdb$relation_name
from rdb$relations
where rdb$view_blr is not null
and (rdb$system_flag is null or rdb$system_flag = 0);

To list all tables and columns

select f.rdb$relation_name, f.rdb$field_name
from rdb$relation_fields f
join rdb$relations r on f.rdb$relation_name = r.rdb$relation_name
and r.rdb$view_blr is null
and (r.rdb$system_flag is null or r.rdb$system_flag = 0)
order by 1, f.rdb$field_position;
Feb 27 11

Nikon D7000 SDK object hierarchy diagram

by admin

Wrote a C program to control my Nikon D7000 camera to capture a picture in less than a second. Here is the SDK module object hierarchy diagram I drew.
Nikon D7000 SDK

Feb 17 11

Tunnelling Firefox traffic over ssh

by admin

Facebook and Yourtube are blocked in China, I had to tunnel all web browser traffic over ssh.

1. Install ssh server on your home computer
2. Open the sshd port number in your router configuration
3. On your local machine, run
ssh -fnD localhost:$NONE_USED_PORT ip_address_of_your_home_computer
for example:
ssh -fnD localhost:9999 public_ip_address_of_your_home_network
4. Configure Firefox to use the tunnel
Edit->Preferences-> Advanced -> Network -> Settings
Select Manual proxy configuration, enter localhost in the SOCKS Host field and the port you used for your tunnel, in my example above: 9999
5. Configure Firefox to use the tunnel also for DNS
Enter about:config in the address field, and search for network.proxy.socks_remote_dns and set it to true
6. Stop the tunnelling
ps -ef | grep ssh
Kill the SSH process with localhost:9999