Python Save A Class

Let me share a little code snippet I use at times which I call “python save a class”. I came up with a very simple and easy way to do that. Check out my code snippet below.

 

 

import cPickle
import traceback

class someClass():
    def __init__(self):
        #set name from variable name. http://stackoverflow.com/questions/1690400/getting-an-instance-name-inside-class-init
        (filename,line_number,function_name,text)=traceback.extract_stack()[-2]
        def_name = text[:text.find('=')].strip()
        self.name = def_name

        try:
            self.load()
        except:
            ##############
            #to demonstrate
            self.someAttribute = 'bla'
            self.someAttribute2 = ['more']
            ##############

            self.save()

    def save(self):
        """save class as self.name.txt"""
        file = open(self.name+'.txt','w')
        file.write(cPickle.dumps(self.__dict__))
        file.close()

    def load(self):
        """try load self.name.txt"""
        file = open(self.name+'.txt','r')
        dataPickle = file.read()
        file.close()

        self.__dict__ = cPickle.loads(dataPickle)

    def __str__(self):
        return str(self.__dict__)

The class is saved as whatever the name of the class instance is. Please not that this works with all datatypes that Pickle supports.

Example

myClass = someClass()

Will try to load a saved class called “myClass.txt” otherwise set some sample values and save the class. Feel free to use this code.

Credits

I would also like to give credit to StackOverflow – Getting an instance name inside class init which showed the cool little trick of how to getting the class instance name.

I hope I was able to show you how to save a class in python. Let me know if you have any questions or comments.

The perfect season to determination soundness problem is before any visible sign appears. Actually there isn’t anything you can’t purchase on the Internet anymore. Whereas erectile disfunction is more common among older men, that doesn’t something you just have to live with. What about “canadian pharmacy cialis 5 mg” and “cialis canadian pharmacy“? In our generation, there are divers options for those ones who suffer from erectile disfunction. You may have heard about “canadian pharmacy online cialis“. More information about the question available at “canadian pharmacy cialis 20mg“. Why it happen? Can sexual disorders in men be prevented? If you get any prescription remedies like Viagra, check with a dispenser that they are sure to take with your other remedies.

Browser as Desktop UI with CherryPy

I recently had the idea of using a browser as desktop UI (user interface). I have used wxpython for most desktop UI in the past, but I was working on an application that was very closely related to the internet. I had to do a lot of searching and testing until I found a solution that was well documented and actually worked. Let me introduce my working solution:

CherryPy, Mako, py2exe and GccWinBinaries

CherryPy is a minimalistic python web framework that allows for a very simple server implementation on your clients system. Make sure you check out the cherryPy tutorials. You can find them at: python_path\Lib\site-packages\CherryPy-version.egg\cherrypy\tutorial.

I suggest Mako as your template library. You can find a presentation on how to use CherryPy in combination with Mako.

py2exe is than used to turn your browser desktop UI into a stand alone Windows application. I did not succeed converting my CherryPy apps using PyInstaller.

GccWinBinaries was necessary for me because I was getting an error message “error: MSVCP90.dll: No such file or directory“.

You can find a simple application of a browser as desktop UI in the cherrypy wiki. The example is including the setup.py file for the py2exe compilation.

I hope this article was able to get you started to use your browser as desktop UI! Happy coding.

The perfect period to resolution soundness problem is before any visible sign appears. Actually there isn’t anything you can’t purchase on the Internet anymore. Whereas erectile dysfunction is more common among older men, that doesn’t something you just have to live with. What about “canadian pharmacy cialis 5 mg” and “cialis canadian pharmacy“? In our generation, there are divers options for those ones who suffer from erectile dysfunction. You may have heard about “canadian pharmacy online cialis“. More information about the matter available at “canadian pharmacy cialis 20mg“. Why it happen? Can sexual disorders in men be prevented? If you get any prescription remedies like Viagra, check with a druggist that they are sure to take with your other remedies.

Python AHK – Python automation package – Automation made simple

Python is a beautiful programming language, anybody that is not convinced please read my python article to get convinced… But one thing is missing, a python automation module to help automating tasks like autohotkey. I wish there was a single python ahk module.

Python and AHK

I discovered autohotkey in 2007. It was rather simple to learn and I did program several scripts to help me in my everyday life at the computer in no time. As simple as it was too learn, I soon hit its limitations. Autohotkey was pretty annoying to pass variables in functions and process any variable or text was just a pain.

Fortunately, I found python in 2009. It was superior to autohotkey in all circumstance except that I had a rather hard time doing the very simplest automation. I did countless hours of research and testing. I even developed my own module PYHK to master python automation. The result are 5 modules that will make python automation simple. I will call those 5 modules the python automation package. From now on you will be able to automate any task in python.

Python automation package

  • SendKeysCtypes
  • PYHK
  • win32gui
  • pywinauto
  • mouse

SendKeysCtype

SendKeysCtypes allows you to send keystrokes and shortcuts to any window. It is very powerful. SenkeysCtypes is a new and more stable version of SendKeys. I have had some small issues with SendKeys in the past which SendKeysCtypes solves. There is no documentation on SendKeysCtypes, but you can use the documentation from Rutherfurd for his module SendKeys.

PYHK

PYHK helps you receive global hotkeys and trigger functions. PYHK is based on pyHook and makes hotkey registration very simple. The project is developed and hosted by schurpf.com. See the PYHK end user documentation for help.

win32gui

I use win32gui for simple window handling such as moving and resizing. I personally prefer win32gui for short, simple tasks. There is one problem with win32gui, there is no real help file. Fortunately, there are plenty of examples out there. Google is your friend.

pywinauto

I use pywinauto for more complex window tasks. An example would be if I had to access a menu within a program (like File-New). For the most part it is enough to simply use win32gui. Please refer to the pywinauto documentation for help. If you are starting out with python automation, I recommend you to just use pywinauto.

mouse

Mouse is a small module to control the mouse. This is the most robust way I have found so far. The version I use is an extension of a module I found at stackoverflow – ctypes mouse_event. You can find more info on mouse in my blog post Mouse.py – Control your mouse in python.

The python automation package should allow you to automate any task in python. Let me know if you have any questions. Happy coding!

The unimprovable date to resolve heartiness problem is before any visible sign appears. Actually there isn’t anything you can’t purchase on the Internet anymore. Whereas erectile dysfunction is more common among older men, that doesn’t some you just have to live with. What about “canadian pharmacy cialis 5 mg” and “cialis canadian pharmacy“? In our generation, there are divers options for those ones who suffer from erectile dysfunction. You may have heard about “canadian pharmacy online cialis“. More information about the question available at “canadian pharmacy cialis 20mg“. Why it happen? Can sexual disorders in men be prevented? If you get any prescription remedies like Viagra, check with a pharmacist that they are sure to take with your other remedies.

Mouse.py – Control your mouse in python

Automation of user inputs always requires to click several buttons or menus. I personally prefer to use mouse.py to automate and control my mouse in python. It was posted in a forum entry on stackoverlow http://stackoverflow.com/questions/4263608/ctypes-mouse-events.

The module as is works great. I did however miss some functionality: Clicking without moving the cursor. I extended mouse.py to click without moving the cursor. I have noticed that very rarely you still see the cursor moving. This only happens if my computer is rather busy. I remember using PostMessage back in my old AHK days, but unfortunately I can not get it to work.

An other function I included is clicking relative coordinates. I often would rather click coordinates in a window without having to deal with where that window is actually at. On a side note, a great tool for relative coordinates especially with multiple monitors is my module python screen coordinates. It does all the conversion of coordinates and has a hit test to determine what screen you are on.

Do you know of other ways to control your mouse in python? Please share any method you know. I would really like to see a PostMessage example work. I know TableCrab has a working solution, but I didnt get a chance yet to look through all the code.

Here is my extended version of mouse.py

import win32gui, win32api, win32con, ctypes

class Mouse:
    """It simulates the mouse"""
    MOUSEEVENTF_MOVE = 0x0001 # mouse move
    MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
    MOUSEEVENTF_LEFTUP = 0x0004 # left button up
    MOUSEEVENTF_RIGHTDOWN = 0x0008 # right button down
    MOUSEEVENTF_RIGHTUP = 0x0010 # right button up
    MOUSEEVENTF_MIDDLEDOWN = 0x0020 # middle button down
    MOUSEEVENTF_MIDDLEUP = 0x0040 # middle button up
    MOUSEEVENTF_WHEEL = 0x0800 # wheel button rolled
    MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
    SM_CXSCREEN = 0
    SM_CYSCREEN = 1

    def _do_event(self, flags, x_pos, y_pos, data, extra_info):
        """generate a mouse event"""
        x_calc = 65536L * x_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CXSCREEN) + 1
        y_calc = 65536L * y_pos / ctypes.windll.user32.GetSystemMetrics(self.SM_CYSCREEN) + 1
        return ctypes.windll.user32.mouse_event(flags, x_calc, y_calc, data, extra_info)

    def _get_button_value(self, button_name, button_up=False):
        """convert the name of the button into the corresponding value"""
        buttons = 0
        if button_name.find("right") >= 0:
            buttons = self.MOUSEEVENTF_RIGHTDOWN
        if button_name.find("left") >= 0:
            buttons = buttons + self.MOUSEEVENTF_LEFTDOWN
        if button_name.find("middle") >= 0:
            buttons = buttons + self.MOUSEEVENTF_MIDDLEDOWN
        if button_up:
            buttons = buttons << 1
        return buttons

    def move_mouse(self, pos):
        """move the mouse to the specified coordinates"""
        (x, y) = pos
        old_pos = self.get_position()
        x =  x if (x != -1) else old_pos[0]
        y =  y if (y != -1) else old_pos[1]
        self._do_event(self.MOUSEEVENTF_MOVE + self.MOUSEEVENTF_ABSOLUTE, x, y, 0, 0)

    def press_button(self, pos=(-1, -1), button_name="left", button_up=False):
        """push a button of the mouse"""
        self.move_mouse(pos)
        self._do_event(self.get_button_value(button_name, button_up), 0, 0, 0, 0)

    def click(self, pos=(-1, -1), button_name= "left"):
        """Click at the specified placed"""
##        self.move_mouse(pos)
        self._do_event(self._get_button_value(button_name, False)+self._get_button_value(button_name, True), 0, 0, 0, 0)

    def double_click (self, pos=(-1, -1), button_name="left"):
        """Double click at the specifed placed"""
        for i in xrange(2):
            self.click(pos, button_name)

    def get_position(self):
        """get mouse position"""
        return win32api.GetCursorPos()
#-----------------------------------------------------------------------------------------
#Added functions
#-----------------------------------------------------------------------------------------
    def invisible_click(self,pos=(-1, -1), button_name="left"):
        """Click in specified place without moving mouse"""
        xcur,ycur = win32gui.GetCursorPos()
        ctypes.windll.user32.SetCursorPos(pos[0],pos[1])
        self.click(pos,button_name)
        ctypes.windll.user32.SetCursorPos(xcur,ycur)

    def invisible_click_rel(self,handle,pos, button_name="left"):
        """Click in window coordinates without moving mouse"""
        #get window info
        xleft, ytop, xright, ybottom = win32gui.GetWindowRect(handle)

        xcur,ycur = win32gui.GetCursorPos()

        ctypes.windll.user32.SetCursorPos(pos[0]+xleft,pos[1]+ytop)
        self.click((pos[0]+xleft,pos[1]+ytop),button_name)
        ctypes.windll.user32.SetCursorPos(xcur,ycur)

if __name__ == '__main__':
##    p = (210,260)
##    print win32gui.GetForegroundWindow()
##    mouse = Mouse()
##    mouse.invisible_click_rel(win32gui.GetForegroundWindow(),p)

I hope it helps you, please let me know about questions. Comments are always welcome!

The perfect time to determination soundness problem is before any visible sign appears. Actually there isn’t anything you can’t purchase on the Internet anymore. Whereas erectile malfunction is more common among older men, that doesn’t slightly you just have to live with. What about “canadian pharmacy cialis 5 mg” and “cialis canadian pharmacy“? In our generation, there are divers options for those ones who suffer from erectile disfunction. You may have heard about “canadian pharmacy online cialis“. More info about the problem available at “canadian pharmacy cialis 20mg“. Why it happen? Can sexual disorders in men be prevented? If you get any prescription remedies like Viagra, check with a pharmacist that they are sure to take with your other remedies.

PageRank.py – Get google PageRank with python

I spend several hours trying to find a python script that would return google PageRank with a python script. There seems to be a script out there that was working in 2010 from Corey Goldberg. It is not working for me. I looked around for quite a bit till I found a python script on GitHub. The script is sponsored by Phurix and uses toolbar queries. I decided to republish it with a small modification:

#!/usr/bin/env python

# Google Pagerank Checksum Algorithm (Firefox Toolbar)
# Downloaded from http://pagerank.phurix.net/
# Requires: Python >= 2.4

# Versions:
# pagerank2.py 0.2 - Fixed a minor formatting bug
# pagerank2.py 0.1 - Public release

# Settings
prhost='toolbarqueries.google.com'
prpath='/tbr?client=navclient-auto&ch=%s&features=Rank&q=info:%s'

# Function definitions
def GetHash (query):
    SEED = "Mining PageRank is AGAINST GOOGLE'S TERMS OF SERVICE. Yes, I'm talking to you, scammer."
    Result = 0x01020345
    for i in range(len(query)) :
        Result ^= ord(SEED[i%len(SEED)]) ^ ord(query[i])
        Result = Result >> 23 | Result << 9
        Result &= 0xffffffff
    return '8%x' % Result

def GetPageRank (query):
    import httplib
    conn = httplib.HTTPConnection(prhost)
    hash = GetHash(query)
    path = prpath % (hash,query)
    conn.request("GET", path)
    response = conn.getresponse()
    data = response.read()
    conn.close()
    return data.split(":")[-1]

if __name__ == "__main__" :
    print GetPageRank("http://schurpf.com/")

I did modify the original script a tiny bit to only return the page rank as a string. That is it. I hope it helps you, let me know comments or questions and I will try to answer as good as I can.

The perfect season to resolve soundness problem is before any visible sign appears. Actually there isn’t anything you can’t purchase on the Internet anymore. Whereas erectile malfunction is more common among older men, that doesn’t some you merely have to live with. What about “canadian pharmacy cialis 5 mg” and “cialis canadian pharmacy“? In our generation, there are divers options for those ones who suffer from erectile malfunction. You may have heard about “canadian pharmacy online cialis“. More info about the problem available at “canadian pharmacy cialis 20mg“. Why it happen? Can sexual disorders in men be prevented? If you get any prescription remedies like Viagra, check with a pharmacist that they are sure to take with your other remedies.