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.