Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, January 27, 2011

Simulating mouse click on Snow Leopard

After googling for 1 hour i came up with the following python script.

import sys
import time
from Quartz import *

def createMouseEvent(type, posx, posy):
theEvent = CGEventCreateMouseEvent(None, type, (posx,posy), kCGMouseButtonLeft)
CGEventPost(kCGHIDEventTap, theEvent)

if __name__ == '__main__':
x = int(sys.argv[1])
y = int(sys.argv[2])

createMouseEvent(kCGEventLeftMouseDown, x, y);
createMouseEvent(kCGEventLeftMouseUp, x, y);


Usage: python script.py 100 100
It'll move the mouse to (100, 100)

Here is the same script on github. click.py

I used the following sources
metapep's blogpost
pepijndevos's PyMouse
GeekOrgy's Python script
a stackoverflow entry

thanks!

Thursday, January 6, 2011

Generating random locations around a location

Handy.

def generateLocation(startlat, startlon, maxdist):

# converting them radians
startlat *= pi / 180
startlon *= pi / 180
rand1 = random.random()
rand2 = random.random()

maxdist = maxdist / earthradius
dist = acos(rand1*(cos(maxdist) - 1) + 1)
brg = 2*pi*rand2

lat = asin(sin(startlat)*cos(dist) + cos(startlat)*sin(dist)*cos(brg))
lon = startlon + atan2(sin(brg)*sin(dist)*cos(startlat), cos(dist)-sin(startlat)*sin(lat))

if lon < -pi:
lon = lon + 2 * pi

if lon > pi:
lon = lon - 2 * pi

lat *= 180 / pi
lon *= 180 / pi

return [lat, lon]

Monday, December 20, 2010

Google App Engine Development Console from command line

When you develop on localhost the interactive console that Google provides is not convenient.
In my project I had to run few scripts in the console quite often, so instead of going to the interactive console all the time I could do it from command line.

This example executes the 'gen_pilot_data.py' script on the interactive console.

curl --data-urlencode "code=`cat src/gen_pilot_data.py`" http://localhost:8079/_ah/admin/interactive/execute

Please notice that you may have to the change the port number.

Sunday, December 19, 2010

Latitude and longitude of cities

http://www.getlatlon.com

cities = [
{ 'name': 'Tokyo', 'lat': 35.6894875, 'lon': 139.6917064 },
{ 'name': 'Seoul', 'lat': 37.566535, 'lon': 126.9779692 },
{ 'name': 'Beijing', 'lat': 39.904214, 'lon': 116.407413 },
{ 'name': 'New Delhi', 'lat': 28.635308, 'lon': 77.22496 },
{ 'name': 'Moscow', 'lat': 55.755786, 'lon': 37.617633 },
{ 'name': 'Kiev', 'lat': 50.45, 'lon': 30.5233333 },
{ 'name': 'Budapest', 'lat': 47.4984056, 'lon': 19.0407578 },
{ 'name': 'Berlin', 'lat': 52.5234051, 'lon': 13.4113999 },

{ 'name': 'Paris', 'lat': 48.8566667, 'lon': 2.3509871 },
{ 'name': 'Madrid', 'lat': 40.4166909, 'lon': -3.7003454 },
{ 'name': 'New York', 'lat': 40.7143528, 'lon': -74.0059731 },

{ 'name': 'Mexico City', 'lat': 19.4270499, 'lon': -99.1275711 },
{ 'name': 'Honolulu', 'lat': 21.3069444, 'lon': -157.8583333 },

{ 'name': 'Sydney', 'lat': -33.8599722, 'lon': 151.2111111 },
{ 'name': 'Taipei', 'lat': 25.091075, 'lon': 121.5598345 }
]

Wednesday, July 7, 2010

simpleHTTPServer, a simple web server

If you wish to have a simple web server that shares files inside your local network there is a good news. You can use simpleHTTPServer which is a Python (2.5+) builtin HTTP Server.

Go to the folder that you want to share and start the server.

python -m SimpleHTTPServer 8000

It will start the server at 8000 and it will serve the files that the current folder contains.