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/