Monday, August 31, 2009

Notes about facebook app development

Size of a narrow profile box on facebook appears to be
199x259
Profile wide appears to be 400 width docs say content should be 380 wide

Thursday, July 16, 2009

Google App engine and django

Been experimenting with google app engine and django.
As ussual these are my notes.

To upload the site just do a
appcfg.py update myapp/

To start the google app is development environment
dev_appserver.py myapp


Monday, June 1, 2009

wincom snippits

def getImage(self,imageElement):
document = self.mechanize.getDocument()
cr = win32com.client.dynamic.Dispatch(document.body).createControlRange()
cr.add(imageElement)
cr.execCommand("Copy",False,0)
cr.remove(0);
wx.TheClipboard.Open()
do = wx.BitmapDataObject()
wx.TheClipboard.GetData(do)
wx.TheClipboard.Close()
if self.showCaptcha :
self.showCaptcha(do.GetBitmap())

Wednesday, May 27, 2009

Setting Internet Explorer Proxy

Still have to check this out.
Not sure how it works.

_
InternetSetOption()Function InternetSetOption

Monday, May 11, 2009

A simple scrapper with ruby mechanize


require 'rubygems'
require 'mechanize'

mech = WWW::Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}

def getNames(page)
results = []
rows = page.root.xpath('//table[@cellspacing="3"]/tr')
1.upto(rows.length() - 1 ) { |i|
results.push(rows[i].search("td").first.text.lstrip.rstrip)
}
return results
end

def getMaleNames(mech)
results = []
page = mech.get("http://names.mongabay.com/male_names.htm")
results.concat(getNames(page))
namelinks = []
page.links.each{ |link|
if ( /\/male_names\d/ =~ link.href)
namelinks.push(link.href)
end
}
namelinks.each { |url|
page = mech.get(url)
results.concat(getNames(page))
}
return results
end

def getFemaleNames(mech)
results = []
page = mech.get("http://names.mongabay.com/female_names.htm")
results.concat(getNames(page))
namelinks = []
page.links.each{ |link|
if ( /\/female_names\d/ =~ link.href)
namelinks.push(link.href)
end
}
namelinks.each { |url|
page = mech.get(url)
results.concat(getNames(page))
}
return results
end

def getSurnames(mech)
results = []
1.upto(10).each { |i|
page = mech.get(sprintf("http://names.mongabay.com/data/%d000.html", i))
rows = page.root.xpath('//table[@class="boldtable"]/tr')
1.upto(rows.length() - 1) { |i|
results.push(rows[i].search("td").first.text.lstrip.rstrip)
}
}
return results
end

puts "get male names"
File.open("malenames.txt", "a+") { |file|
getMaleNames(mech).each { |name|
file << name << "\n"
}
}

puts "get female names"
File.open("femalenames.txt", "a+") { |file|
getFemaleNames(mech).each { |name|
file << name << "\n"
}
}

puts "get surnames"
File.open("surnames.txt", "a+") { |file|
getSurnames(mech).each { |name|
file << name << "\n"
}
}

Thursday, April 30, 2009

Queue class for Zeo

Supper cool in that i uses a decorator for the transaction commit
First time I actually used a decorator. I knew about them, but I never thought of a use for them. Ok I am slow :)


from persistent import Persistent
from BTrees.LOBTree import LOBTree
from ZODB.POSException import ConflictError
import Queue
import transaction
import time

def transactionDecorator(fn):
def newfn(*args,**kwargs) :
while 1 :
try :
transaction.manager.begin()
result = fn(*args,**kwargs)
transaction.manager.commit()
return result
except ConflictError:
time.sleep(0.01)
except Exception,e:
transaction.manager.abort()
raise e
return newfn

class BTreeQueue(Persistent) :
def __init__(self):
self.bTree = LOBTree()
@transactionDecorator
def push(self,item):
key = None
try :
key = self.bTree.maxKey()
except ValueError :
key = 0
self.bTree.insert(key+1,item)

@transactionDecorator
def extend(self,items) :
start = None
try :
start = self.bTree.maxKey()
except ValueError :
start = 0
start += 1
result = {}
for i,key in enumerate(range(start,start+len(items))) :
result[key] = items[i]
self.bTree.update(result)

@transactionDecorator
def pop(self):
item = None
error = False
try :
key =self.bTree.minKey()
item = self.bTree.pop(key)
except ValueError :
error = True
finally :
if error :
raise Queue.Empty()
else :
return item

@transactionDecorator
def rotate(self):
"""pops a item then puts it on end again, returns value"""
item = None
error = False
key =self.bTree.minKey()
item = self.bTree.pop(key)
self.bTree.insert(self.bTree.maxKey()+1,item)
return item

@transactionDecorator
def count(self):
count = None
try :
count = self.bTree.maxKey() - self.bTree.minKey() + 1
except ValueError :
count = 0
return count

@transactionDecorator
def empty(self):
self.bTree.clear()

Wednesday, April 15, 2009

Notes about Cython classes

Just my own personal Notes for my own reference

In cython the are 2 different types of classes

Normal python classes as in the form

class A(object):
pass

Cython classes
cdef class A :
cdef int someterm

cython classes do not have a __dict__ to store their attributes in.
that is why they are so fast
but the concequence is that you can not add attributes at runtime like you can with normal python classes.
If you peak into the source code generated by cython you see they are actually structs.

Wednesday, April 8, 2009

Running xserver on vps/dedicated server

Use Xvfb on ubuntu/debian it is in package xvfb

used in this manner

Xvfb :1 &
env DISPLAY=:1 firefox
DISPLAY=:1 firefox
ssh -t -L 5900:localhost:5900 far-away.east 'x11vnc -localhost -display :0'
xtightvncviewer -encodings "copyrect tight hextile" localhost:0
ssh -t -L 5900:localhost:5900 server 'x11vnc -localhost -display :1'

Also simpler is you use
xvfb-run python testjs.py

Notes on setting up an ubuntu box

I keep having to do this all the time.
And all the time I need to look on the net for stuff I remember I need to do but can not remember exact command line

apt-get install g++ build-essential

apt-cache search XXXX

Setting of multiple IP addresses on one network card.

sudo ifconfig eth0:0 192.168.1.11 up
what is commonly know as ethernet alias

Setting up ssh to be used with secure keys so password is not needed.
http://polishlinux.org/apps/ssh-tricks/

Tuesday, April 7, 2009

Metaclass Articles

Some resources on Metaclasses

http://cleverdevil.org/computing/78/

http://www.voidspace.org.uk/python/articles/metaclasses.shtml

http://www.devx.com/opensource/Article/31482/1954?pf=true

Will make post on how to create class at runtime

Monday, April 6, 2009

How to simulate events in Javascript

Was searching on google on how to simulate a click using javascript on an html link object. Basically I found stuff that either did not work, or else the common answer was that it is not possible.

Anyway with a little of investigative searching I found the dispatchEvent method. The example ofcourse works in firefox. It also works in the version of webkit that ships with QT. It does not work with konqueror, and it does not work with IE.

It appears according to this website that a slightly different technique has to be used for IE.


Anyway might be useful for future reference.

Saturday, April 4, 2009

Mixins and Python

This is really cool. A way to expand python classes/objects without direct inheritance. Useful also. http://www.linuxjournal.com/node/4540/print .

Abandoned trying to work with webkitgtk

The biggest problem when using webkit is trying to access the DOM structure of the rendered webpage. When using webkit for practical purposes you only have two choices, QT and GTK. The third option working with the ObjectC bindings unless you are on MacOSX is just not practical.

Both QT and GTK are nice GUI, QT has the upper hand in that thier documentation is much nicer. With GTK you have the advantage that it compiles with pure C, so it is much easier to make other language bindings with it. Initially when webkit was released with QT they said that they would provide in the next release the ability to directly access the underlying DOM structure with the API. But now they have said that they are considering it. There is a way to access the DOM structure but it is only by using javascript.

When using webkitgtk there is a patch that you can apply to access the DOM structure directly. The problem is I wish to use python. But I can not get the pywebkitgtk to compile. This is the ultimate solution though.

My solution is to now make a python -> javascript bridge on QT. Javascript object members and properties can be examined at run time. The problem is that QWebFrame::evaluateJavaScript only returns a QVariant. QVariant can only hold basic types such as ints,strings etc. But this is not a problem. Have the bridge examine the result. If it is a basic type just pass it back. If it is a javascript object pass back the object using JSON with all the properties and members contained. Basically this is possible to do because of the "in" operator in javascript.

I have a proof of concept code with pyqt and I will post it once I fine tune it. It seems to work very well since webkit is much faster then internet explorer and firefox.

Sunday, March 29, 2009

Using h2def.py

Just notes that I am making.

2 references
http://www-106.ibm.com/developerworks/linux/library/l-wrap/
http://faq.pygtk.org/index.py?req=show&file=faq01.011.htp

to generate the defs for webkit use
/usr/share/pygobject/2.0/codegen/h2def.py `ls fixedHeaders/*.h`> webkit/webkit.defs

--modulename WebKit seems to do nothing, have no idea what it for.

Sigh having problems wrapping with.

Friday, March 20, 2009

Computers are a lot simpler then people

Computers are not simple. Often when something goes wrong you get obtuse confusing error messages. So yes it sometimes is hard to know what went wrong. But the big difference is when you mess up computers never hold grudges. And if worse comes to worst you just do a complete reformat, reinstall everything and then it is as if you are starting new again.

People also give out confusing error messages. You know something is wrong but you just do not know exactly what. The biggest problem is people get angry at you and hold grudges. And if a person stops talking to you, you can't just hit the reset button and get a fresh reboot. Once errors have been made it is very hard to fix them, and rarely is there a second chance.