Skip to content
Apr 14 12

Dictionary using dictd on Ubuntu

by admin

1. Save the script as d.php in /var/www/
2. Install PHP if it has not been installed.
3. Install dictd server
4. Edit the database list
sudo vi /var/lib/dictd/db.list
5. Restart dictd server
sudo /etc/init.d/dictd restart
6. http://your_ip/d.php

It’s best viewed from iPad

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
Tom Luo 2012 - dictionary
</title>
<style type="text/css">
body { font-family: calibri;font-size:23px; }
p { font-size: 13px; }
h2 { font-size: 24px;
	font-style: italic;
}
A { text-decoration:none; }
A:hover { color: #800080; font-weight:bold; }
input {
	border: 3px solid #d0d0ff;
	font-size: 30px;
	padding-right:4px;
	padding-left:4px;
}
</style>
</head>
<body>
<script language="JavaScript">
function processLink(s)
{
	s = s.replace(/\s+/g," ");
	location.href=self.name + "?query=" + escape(s);
}
</script>
<form name="form1">
Enter word to search: <input value="<?php
	$query = $_REQUEST['query'];
	# clean up passed value
	$query = preg_replace("/^\s+/","",$query);
	$query = preg_replace("/\s+$/","",$query);
	$query = preg_replace("/\s+/"," ",$query);
	$value = preg_replace("/\"/","&quot;",$query);
	echo "$value"
?>" type="text" name="query" onChange="submit()"><p>
</form>
<?php
if($query != "") {
	$equery = escapeshellarg($query);
	exec("/usr/bin/dict $equery 2>&1",$output,$error);
	$output = implode("\n",$output);
	if($error) {
		echo "<pre><b>Error: $output<b></pre>";
	}
	else {
		if (preg_match("/no definitions found/i",$output)) {
			# turn suggested alternatives into links
			$output = preg_replace("/(: +)(\w+)/",
				"\\1<a href=\"javascript:processLink('\\2');\">\\2</a>",$output);
			echo "<pre>$output</pre>";
		}
		else {
			# bold first line
			$output = preg_replace("/^(.*)/","<b>\\1</b>",$output);
			# wrap first line of each reference in table to control background color
			$output = preg_replace("/(\n\nFrom )(.*)\n/","\n\n<table cellpadding=4
				bgcolor=#d0d0ff><tr><td><b>\\2</b></td></tr></table>",$output);
			# find and process document internal links
			$output = preg_replace("/\{+(.*?)\}+/s",
				"<a href=\"javascript:processLink('\\1');\">\\1</a>",$output);
			echo "<pre>$output</pre>";
		}
	}
}
?>
<script language="JavaScript">
	document.form1.query.focus();
</script>
</body>
</html>
Apr 3 12

Interactive server-side Python shell for Google App Engine

by admin

http://shell.appspot.com/

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;