I found this great article on pygtk.org on how to create simple GUI applications with glade to be used with python and pygtk. This article is very straight forward on how to make a simple HelloWorld GUI application with two widgets. If your used to developing Visual Studio .NET GUI's or are looking to develop GUI for multi-platform then you need to check this out. This is a good start. Before I read this I tried to use glade, but everything is so different than VS.Net that I didn't get anywhere. Now that I read this all makes sense. Also If you are creating applications with wx, you can use wxGlade to make GUI for you and use the same way. Python Rocks. Enjoy.
http://www.pygtk.org/articles/pygtk-glade-gui/Creating_a_GUI_using_PyGTK_and_Glade.htm
On Ubuntu, you can install all things glade like this:
apt-get install `apt-cache search glade|awk '{ if ($1 != "glade-gnome-2" && $1 != "glade-2" && $1 != "glade") print $1 }'|xargs`
Showing posts with label python. Show all posts
Showing posts with label python. Show all posts
Friday, October 17, 2008
Wednesday, October 8, 2008
Linus Torvalds Blog
Linus Torvalds now has a blog. Check it out. You might learn something. It only has a few posts. But what he writes about is really good.
torvalds-family.blogspot.com/
In a post Linus writes how he wrote a tool to limit his kids' computer usage. You can find the project here:
http://git.kernel.org/?p=linux/kernel/git/torvalds/tracker.git;a=summary
It's very interesting to see how a project evolves in open-source . I especially learned a few things from this. One being how to write c for python. Two, how easy it is to use gtk. Three, gtk library doesn't make pyCrust crash, unlike wx. yippie. This is pretty sweet. Definitely a great read. Thanks -A
torvalds-family.blogspot.com/
In a post Linus writes how he wrote a tool to limit his kids' computer usage. You can find the project here:
http://git.kernel.org/?p=linux/kernel/git/torvalds/tracker.git;a=summary
It's very interesting to see how a project evolves in open-source . I especially learned a few things from this. One being how to write c for python. Two, how easy it is to use gtk. Three, gtk library doesn't make pyCrust crash, unlike wx. yippie. This is pretty sweet. Definitely a great read. Thanks -A
Friday, October 3, 2008
PyCrust - The Flakiest Python shell
Python Namespaces:

CallTips:

AutoComplete Objects:

Download PyCrust - The Flakiest Python Shell at sourceforge.net/projects/pycrust/ It's really nice. It has alot of features that let you see into loaded python object. Although when I imported wx and tried to view the loaded details it crashed. As the name implies that was pretty flaky. Try importing sys or os libs and see for your self. This tool is good to have in the toolbox. Thanks. -A
EDIT: You can also checkout SPE(Stani's Python Editor). apt-get install spe

CallTips:

AutoComplete Objects:

Download PyCrust - The Flakiest Python Shell at sourceforge.net/projects/pycrust/ It's really nice. It has alot of features that let you see into loaded python object. Although when I imported wx and tried to view the loaded details it crashed. As the name implies that was pretty flaky. Try importing sys or os libs and see for your self. This tool is good to have in the toolbox. Thanks. -A
EDIT: You can also checkout SPE(Stani's Python Editor). apt-get install spe
Saturday, September 20, 2008
My python curl.py class template with stdout colors support
This is my curl.py class that I use as a template. It supports stdout colors. When a function is called is displays output of the caller and called functions. Also you can display any string, list, dict,tuple,etc...Very Nice, I like. It's the same thing as using print. Only you add color to the output. I especially like it for situations like. Displaying webpage data. Large amounts of it. Logs and CSV files. You can find patters and display them in different colors. You can use this script to download and upload data to any website. Please correct indentation where necessary.
How to Use: sess = curl.session("") #create new session
sess.login("http://domain/site/login_post","admin","super_secret_pass")
How to Use: sess = curl.session("") #create new session
sess.login("http://domain/site/login_post","admin","super_secret_pass")
#!/usr/bin/env python
from ctypes import *
import os, sys, types, urllib, urllib2, urlparse, string, pycurl
import stdout_colours
class curl(object):
"Encapsulate user operations on CGIs through curl."
def __init__(self, base_url=""):
self.func_me_color="white_on_black"
self.soc=stdout_colours.stdout_colors()
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
# These members might be set.
self.base_url = base_url
self.verbosity = 0
# Nothing past here should be modified by the caller.
self.response = ""
self.curlobj = pycurl.Curl()
# Verify that we've got the right site...
self.curlobj.setopt(pycurl.SSL_VERIFYHOST, 2)
# Follow redirects in case it wants to take us to a CGI...
self.curlobj.setopt(pycurl.FOLLOWLOCATION, 1)
self.curlobj.setopt(pycurl.MAXREDIRS, 5)
# Setting this option with even a nonexistent file makes libcurl
# handle cookie capture and playback automatically.
self.curlobj.setopt(pycurl.COOKIEFILE, "/dev/null")
# Set timeouts to avoid hanging too long
self.curlobj.setopt(pycurl.CONNECTTIMEOUT, 30)
self.curlobj.setopt(pycurl.TIMEOUT, 300)
# Set up a callback to capture
def response_callback(x):
self.response += x
self.curlobj.setopt(pycurl.WRITEFUNCTION, response_callback)
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def set_verbosity(self, level):
"Set verbosity to 1 to see transactions."
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.curlobj.setopt(pycurl.VERBOSE, level)
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def get(self, cgi, params="",verbose=0):
"Ship a GET request to a specified CGI, capture the response body."
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
if params:
cgi += "?" + urllib.urlencode(params)
self.curlobj.setopt(pycurl.URL, os.path.join(self.base_url, cgi))
self.curlobj.setopt(pycurl.HTTPGET, 1)
self.response = ""
self.curlobj.perform()
if verbose > 0:
print self.response
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def post(self, cgi, params,verbose=0):
"Ship a POST request to a specified CGI, capture the response body.."
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.curlobj.setopt(pycurl.URL, os.path.join(self.base_url, cgi))
self.curlobj.setopt(pycurl.POST, 1)
self.curlobj.setopt(pycurl.POSTFIELDS, urllib.urlencode(params))
self.response = ""
self.curlobj.perform()
if verbose>0:
print self.response
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def upload(self, cgi, file_name, file, verbose=0):
"POST file from localhost to location/cgi."
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.curlobj.setopt(pycurl.URL, os.path.join(self.base_url, cgi))
self.curlobj.setopt(pycurl.HTTPPOST,[(file_name, (pycurl.FORM_FILE,file))])
self.response = ""
self.curlobj.perform()
if verbose>0:
print self.response
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
filename), "wb").write(content)
#fnames = ",".join([fname for fname, ct, c in files])
#return HttpResponse("me-%s-RECEIVE-OK[POST=%s,files=%s]" % (request.META["SERVER_PORT"], request.POST.values(), fnames ))
def answered(self, check):
"Does a given check string occur in the response?"
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.me_him(['RETURN:',__name__],self.func_me_color)
return self.response.find(check) >= 0
def close(self):
"Close a session, freeing resources."
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.curlobj.close()
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
class session(curl):
def login(self, cgisite,username, password):
"""login - cgi="account/login.php",params=(("username",name),("password",pass),("foo","bar")) """
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.post(cgisite, (("username",username),
("password",password),
("mode","login"),
("usertype","P"),
("redirect","admin")))
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def logout(self, cgisite):
"""logout - cgi="account/logout.php" """
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.get(cgisite)
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
if __name__ == "__main__":
if len(sys.argv) < 3:
print "Usage: %s \"schema://site/cgi\" \"username\" \"password\"" % sys.argv[0]
site=sys.argv[1]
username=sys.argv[2]
password=sys.argv[3]
sess=session("")
sess.set_verbosity(1)
sess.login(site,username,password)
a=""
for i in range(len(password)):
a+="*"
print "YOU ARE LOGGED IN!",site,username,a
sess.logout()
sess.close()
Friday, September 19, 2008
Python Incremental Downloader Script
This is a script that will incremental download all files on a website. It accumulates all names and sends you an email to user@localhost of files that were downloaded and kept. Which ones were downloaded and deleted due to a wrong file size. etc... Make modification/s where necessary. This is something that worked for me. Most sites don't need the session variable passed via query string. For the ones that do this script work great. You may come across sites that require storage of cookie. The most feasible way I can think of that you would accomplish this is if you used pyCurl. You can also add this functionality to a bot that will download files and zip them after reaching a certain directory space limit. For easy download via DCC(in python) or something. A lot of possibilities here. Depending on the site and your connection you make get in extent of 10GB per day download speeds. Of course there may be a better way to implement the functionality. This is one way to do it. You can add more functions named start_ and do what you will with the data. Such as img, form or input fields. Nuff said. Enjoy this script. It not mine really and you can do what you want with it. No Restrictions and all that mumbo jumbo.
NOTE: Modification is required to use this script.
NOTE: Modification is required to use this script.
#!/usr/bin/env python
#download all txt files
from sgmllib import SGMLParser
import os,sys,urllib,string
class URLLister(SGMLParser):
def reset(self):
SGMLParser.reset(self)
self.urls = []
def start_a(self, attrs):
href = [v for k, v in attrs if k == 'href']
if href:
self.urls.extend(href)
class file_downloader(object):
def __init__(self):
self.c="" #used to hold session string
self.modu=150 # perform session check every modulus == 0
self.h="http://www.example.org" #used to get session
self.u="http://www.example.org/download.php?id=" #used to get file via sess
self.d="file/" #dir to dl files to
self.mailer=True #send mail upon complete
self.to_email="user@localhost" #message are sent here if above True
self.ws=110 #wrong file size
self.messagerm="" #used to hold message sent of removed files
self.messagesv="" #used to hold message sent of saved files
self.messageimprm="" #used to hold message sent of impossible removed files
self.messageimpsv="" #used to hold message sent of impossible saved files
self.mfn="file_missed" #impossible files are stored in this file under specified dir
self.missed=[] #missed list
self.impossible=[] #impossible list
def session_var(self):
usock = urllib.urlopen(self.h)
parser = URLLister()
parser.feed(usock.read())
usock.close()
parser.close()
for url in parser.urls:
if string.find("".join(url),"download.php") != -1:
return url.split("&")[1]
def check_wrong_size(self):
d,mfn,missed,ws=self.d,self.mfn,self.missed,self.ws
if os.path.exists(d+mfn) == True:
mls=open(d+mfn).readline()
ml=eval(mls.split("\n")[0])
missed={}.fromkeys(ml).keys()
#os.listdir(os.getcwd()) #list all files in cwd
files=os.listdir(d)
for i in range(len(files)):
f=d+files[i]
if os.path.getsize(f) < int(ws):
print "removing file "+f
os.system("rm "+f)
missed.append(i)
os.system("echo \""+str(missed)+"\" > "+d+mfn)
""" run for loop and get """
def download_files(self,rans,rane):
d,u,mfn,missed,ws,modu=self.d,self.u,self.mfn,self.missed,self.ws,self.modu
messagerm,messagesv=self.messagerm,self.messagesv
messageimprm,messageimpsv=self.messageimprm,self.messageimpsv
c=self.session_var()
if os.path.exists(d+mfn) == True:
mls=open(d+mfn).readline()
ml=eval(mls.split("\n")[0])
missed={}.fromkeys(ml).keys()
for i in range(int(rans), int(rane)):
if i % int(modu) == 0:
c=self.session_var()
print i, c
urllib.urlretrieve(u+str(i)+"&"+c,d+str(i)+".txt")
if os.path.getsize(d+str(i)+".txt") < int(ws):
os.system("rm "+d+str(i)+".txt")
missed.append(str(i))
messagerm += d+str(i)+".txt "
else:
messagesv += d+str(i)+".txt "
#retry impossible & missed files
m=missed
impossible,to_email=self.impossible,self.to_email
for i in range(len(m)):
c=self.session_var()
#mission impossible?
urllib.urlretrieve(u+str(m[i])+"&"+c,d+str(m[i])+".txt")
if os.path.getsize(d+str(m[i])+".txt") < int(ws):
os.system("rm "+d+str(m[i])+".txt")
impossible.append(str(m[i]))
messageimprm += d+str(m[i])+".txt "
else:
messageimpsv += d+str(m[i])+".txt "
os.system("echo \""+str(impossible)+"\" > "+d+mfn)
if self.mailer == True:
os.system("echo \""+messagerm+"\" > "+d+"filerm")
os.system("mail -s 'file removed' "+to_email+" < "+d+"filerm")
os.system("rm "+d+"filerm")
os.system("echo \""+messagesv+"\" > "+d+"filesv")
os.system("mail -s 'file saved' "+to_email+" < "+d+"filesv")
os.system("rm "+d+"filesv")
os.system("echo \""+messageimprm+"\" > "+d+"filerm")
os.system("mail -s 'file impossible removed' "+to_email+" < "+d+"filerm")
os.system("rm "+d+"filerm")
os.system("echo \""+messageimpsv+"\" > "+d+"filesv")
os.system("mail -s 'file impossible saved' "+to_email+" < "+d+"filesv")
os.system("rm "+d+"filesv")
if __name__ == "__main__":
a=file_downloader()
if len(sys.argv) < 2:
a.check_wrong_size()
else:
rans=sys.argv(1) #range start 250
rane=sys.argv(2) #range end 432
a.download_files(rans,rane)
Thursday, August 14, 2008
Python STDOUT Colors Script
This is a great script I use for debugging and/or general stdout colorization when working with python.If you run it from console with no parameters it loops through stdout colors. Displaying them with the string that represents that color. Notice: Some color codes may come out different than what they appear. It's very useful to import inside your other scripts and print your output in color. I though it was great. Makes for debugging large amounts of data a snap. Maybe someone else will find it useful as well. Enjoy!
This script is also located at:
code.google.com/p/python-stdout-colors/
Download PY!
Tell me what you think, leave a comment.
Runnable from terminal:
chmod +x stdout_colours.py
python stdout_colours.py
Use it in your code like this:
self.soc.write(["printing','a','list'],"red")
self.soc.write("printing a string","green")
self.soc.write({"printing":"dictionary","testing":"fun"},'blue')
self.soc.write(("printing","a","tuple"),'yellow')
Add it to your functions like this:
EDIT: I actually like to use this over print when I deal with terminal/console apps. Much easier to tell what is going on when text is scrolling by so fast.
I hope this helps someone. Leave a comment. Enjoy.
This script is also located at:
code.google.com/p/python-stdout-colors/
Download PY!
Tell me what you think, leave a comment.
Runnable from terminal:
chmod +x stdout_colours.py
python stdout_colours.py
Use it in your code like this:
self.soc.write(["printing','a','list'],"red")
self.soc.write("printing a string","green")
self.soc.write({"printing":"dictionary","testing":"fun"},'blue')
self.soc.write(("printing","a","tuple"),'yellow')
Add it to your functions like this:
import stdout_colours
class some_class(object):
def __init__(self):
self.testing="fun"
self.func_me_color="white_on_blue"
self.soc=stdout_colours.stdout_colors()
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.write("doing something:","red")
self.do_something()
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
def do_something(self):
self.soc.me(['ENTER:',__name__],self.func_me_color)
self.soc.write("doing something else:","green")
self.do_something_else()
self.soc.me(['EXIT:',__name__],self.func_me_color)
def do_something_else(self):
self.soc.me_him(['ENTER:',__name__],self.func_me_color)
self.soc.write(['testing','is',testing],"yellow")
self.soc.me_him(['EXIT:',__name__],self.func_me_color)
EDIT: I actually like to use this over print when I deal with terminal/console apps. Much easier to tell what is going on when text is scrolling by so fast.
I hope this helps someone. Leave a comment. Enjoy.
Subscribe to:
Posts (Atom)