Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Monday, June 15, 2009

Python basics : math functions, main funct

Math functions
#Unfortunately, you need to import math, but python has a nice library of math functions



import math
math.pow(2,3)
math.cos(math.pi/2) #<-- radians!
math.pi
math.sqrt(4)



Main function

#Making a main function in Python is a little tricky. Here is an example:



def main(*args):
if len(args) < 4:
return

print "main : args[0] ", args[0], " args[1]", args[1]
print "(main) : args[1] ", args[2]
print "(main) : args[3] ", args[3]

if __name__ == "__main__":


Thursday, June 11, 2009

Checking versions if Office 2004 is at 11.5.5 and Acrobat is at 9.1.2

This is a trick to check version of some software on a group of computers


Here we're checking that Office 2004 11.5.5 update is installed


for ((i=1;i < 21;i++)); do if [[ $i -lt 10 ]]; then echo "station $i" ; ssh station-0${i}.local " ls -l /Applications/Microsoft\ Office\ 2004/Updater\ Logs/11.5.5\ Update\ Log.txt "; else ssh station-${i}.local " ls -l /Applications/Microsoft\ Office\ 2004/Updater\ Logs/11.5.5\ Update\ Log.txt "; fi; done

Here we're checking that Acrobat reader and acrobat are at 9.1.2


for ((i=1;i < 21;i++)); do if [[ $i -lt 10 ]]; then echo "station $i" ; ssh station-0${i}.local "system_profiler SPSoftwareDataType SPApplicationsDataType | grep '9.1.2' "; else ssh station-${i}.local " system_profiler SPSoftwareDataType SPApplicationsDataType | grep '9.1.2' "; fi; done

Wednesday, June 10, 2009

How to set up Jogre for the web

Set up jdk, apache tomcat, and hsqldb
==============================

apt-get install sun-java6-jdk
apt-get install tomcat5.5 tomcat5.5-wepapps tomcat5.5-admin
apt-get install hsqldb-server

Set up jogre
=========

wget http://voxel.dl.sourceforge.net/sourceforge/jogre/jogre_beta_0.3_bin.zip
cd /opt
unzip ~/jogre_beta_0.3.bin.zip
adduser jogre
chown -R jogre:jogre jogre
su jogre
cd
echo "export CLASSPATH=$CLASSPATH:/usr/share/java/hsqldb.jar:." >> .profile
source .profile
java org.hsqldb.Server &
cd /opt/jogre/server
chmod a+x *.sh


Configuration
==========

./server.sh &

#I prefer the gui; it's easier to just change the db settings with this tool
./administrator.sh &
#Login with admin, admin
#Change the database from xml to hsqldb
#The path to the db is jdbc:hsqldb:hsql://localhost/jogre_hsqldb

#restart the jogre server
killall server.sh
./server.sh &


#With apache tomcat admin (http://localhost:8180/admin), add jogreweb.war

Sunday, June 7, 2009

Java - convert byte array to String



int len = (int)c.getLength();

if (len > 0) {
int actual = 0;
int bytesread = 0 ;
byte[] data = new byte[len];
while ((bytesread != len) && (actual != -1)) {
actual = is.read(data, bytesread, len - bytesread);
bytesread += actual;

}

//assuming utf-8 encoding
String test = new String(data,0,data.length,"UTF8");
System.out.println(test);


Java - how to make an anonymous thread

Creating an anonymous thread is pretty easy. If you want to make a thread that performs a simple operation anonymous threads can be convenient.

Java threads perform their task in the run() function, which is initiated with the start method.

Here is an example:



Thread t = new Thread( new Runnable(){
public void run(){
System.out.println(" do some work");


}
});

t.start();

Saturday, June 6, 2009

Hello world Midlet


import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class LoginScreen extends MIDlet {

public LoginScreen() {
// TODO Auto-generated constructor stub
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
// TODO Auto-generated method stub

}

protected void pauseApp() {
// TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
// TODO Auto-generated method stub

Form f = new Form("Login Form");
Display d = Display.getDisplay(this);
d.setCurrent(f);

f.append("Hello");

}

}

Flash tricks - global vars, functions, timers

Creating global var-
_global.myvar

Creating a timer-
var starttime = getTimer();
var atime=0;

while (atime < _global.timestop+starttime){

atime = getTimer();

if (_global.conn_state == true){
break;
}
}

Creating a function-
function populateJoin(){

gamelist.data = _global.currentmsg.split(_global.SEP);
gamelist.labels = _global.currentmsg.split(_global.SEP);


}

Java Me Notes

Links

Main site

http://java.sun.com/javame/index.jsp

Download

http://java.sun.com/products/sjwtoolkit/download.html

Eclipse plugin

http://download.eclipse.org/dsdp/mtj/updates/0.9/stable/

Need to use eclipse to export jad and jar for the project (which is in deploy)


Conventions

startApp() --> like main

import javax.microedition.lcdui.*; //required to use display

Display // interface with phone screen

Form //set up the screen then attach it to display

myform.append("some text"); //a label

Wednesday, June 3, 2009

Generate a command window for each iteration of a loop

rem This is good for those commands that take a lot of time.
rem Each iteration generates a new cmd window

for /l %a in (1,1,10) do start cmd /k psexec \\computer-%a time-consuming.exe

Keeping track of station numbers with a text file

rem Usually I keep track of the configurations associated with a station by its mac
rem Without the macs, one can keep track of what configuration to use by using a counter
rem
rem This works well with a network or a flash drive


for /f %%a in ('type \\server\share\count.txt') do SET temp=%%a
rem echo %temp%
SET /A temp+=1
echo %temp% > \\server\share\count.txt

rem The counts of count.txt is simply a number and can be used in a script

Monday, June 1, 2009

Change a computer name with vbscript

Scripting the changing of computer names in windows is straight-forward with a vbscript.

You can change a computer via the registry, so this is the approach used here.

Set the computer name you want in the sNewName variable.



sNewName = "new_pc_name"

Set oShell = CreateObject ("WSCript.shell")



sCCS = "HKLM\SYSTEM\CurrentControlSet\"

sTcpipParamsRegPath = sCCS & "Services\Tcpip\Parameters\"

sCompNameRegPath = sCCS & "Control\ComputerName\"



With oShell

.RegDelete sTcpipParamsRegPath & "Hostname"

.RegDelete sTcpipParamsRegPath & "NV Hostname"



.RegWrite sCompNameRegPath & "ComputerName\ComputerName", sNewName

.RegWrite sCompNameRegPath & "ActiveComputerName\ComputerName", sNewName

.RegWrite sTcpipParamsRegPath & "Hostname", sNewName

.RegWrite sTcpipParamsRegPath & "NV Hostname", sNewName

End With ' oShell

VBScript tricks : no logo, filesystem, control blocks, argument


'When running from the console, here is how to hide the logo

cscript //Nologo myscript.vbs

'How to reference an argument
WScript.Arguments.Item(0)


'Here is how to read a file

set f = fso.opentextfile(source, 1,false)



if fso.fileexists(source) then



while not f.atendofstream



inputline = f.readline



msgbox inputline

wend

end if

Inserting a blank line with sed

#I found this to be a bit tricky

sed -e '/pattern/i\
\ # <-- this is the key
' input_file > output_file

Saturday, May 30, 2009

awk example using arrays, the split() function, and regex

#record format

Request: 10
As Of: 05/27/2009 1:34:58 pm
Irrevelant Data ...
Summary: Virus on PC.
Description: Virus on PC.

Request: 11
As Of: 05/27/2009 1:34:58 pm
Irrevelant Data...
Summary: room1 several computers in a row are freezing up and student...
Description: room1 several computers in a row are freezing up and students have to reboot.


awk -v pat1="room1" -v pat2="freezing|crash" 'BEGIN { RS=""; FS="Summary"; i=0; j=0; k=0 } {\
i=i+1; \
field1=$1; \
split(field1,sub_fields,":"); \
request=sub_fields[1]; \
the_date=sub_fields[4]; \
if ($2 ~ pat1){ \
k=k+1; \
if ($2 ~ pat2){ \
j=1+j; \
print "***Record***", request, the_date; print " ---Start Data---", $2; } } \
} END { print "Processed, ", i, ",num of lab matches,", k,", records num of keywords matches: ,", j }' requests


#Output

***Record*** Request 05/27/2009 1
---Start Data--- : room1 several computers in a row are freezing up and student...
Description: room1 several computers in a row are freezing up and students have to reboot.
Processed, 2 ,num of lab matches, 1 , records num of keywords matches: , 1

Friday, May 29, 2009

Removing carriage returns with sed

sed -e s/$'\r'/\ /g input_file # remove carriage returns

sed -e 's/\n/ /g' input_file # remove newlines

Insert text before a pattern with sed

cat some_file | sed '/pattern/i\
text to insert
' > output_file

Example of processing multiline records with awk

Processing multiline records is tricky. It is easier to use blank lines as record separators.


#example records
ink1 4.99
red

ink2 5.99
yellow

awk ' BEGIN { RS=""; FS=" "} { r1=$1; r2=$2; r3=$3; print "***Record***" r1 " " r2 " " r3 }' inks

#What it prints out
***Record***ink1 4.99
red
***Record***ink2 5.99
yellow

Wednesday, May 27, 2009

awk script to generate count of each program

#csv of format:
#the_program, any_data, any_data, ....
#
#this script counts number of repetitions of a program



cat program-list.csv | awk -F',' '/[a-zA-Z0-9]+/{print $1}' | sort | uniq -c

Tuesday, May 26, 2009

Create a dojo dialog widget

Create Messagebox / popup for Java

//Assuming that the current object is a JFrame:

JOptionPane.showMessageDialog(this, "Some Message");