Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Friday, September 23, 2011

[Python] Easy Facebook Scripting.

Need a small and neat api for doing Facebook scripting? Look no further!


from urllib import urlretrieve
import imp
urlretrieve('https://raw.github.com/gist/1194123/fbconsole.py', '.fbconsole.py'
fb = imp.load_source('fb', '.fbconsole.py')
fb.AUTH_SCOPE = ['publish_stream']
fb.authenticate()
status = fb.graph_post("/me/feed", {"message":"Hello from my awesome script"})
#Note: Recommend that you download fbconsole.py locally with eg. wget https://raw.github.com/gist/1194123/fbconsole.py

Post a status update:
status = fb.graph_post("/me/feed", {"message":"Hello from my awesome script"})

Fetch likes on a status update:

likes = fb.graph("/"+status["id"]+"/likes")
Delete a status update:
fb.graph_delete("/"+status["id"])
Upload a photo:

fb.graph_post("/me/photos", {"message":"My photo", "source":open("my-photo.jpg")})
Query FQL tables:
friends = fb.fql("SELECT name FROM user WHERE uid IN "
"(SELECT uid2 FROM friend WHERE uid1 = me())")


Source:  Easy Facebook Scripting in Python

Saturday, August 27, 2011

Magic package - WOL.

Python is fun! Did a script for Wake On Lan.

import socket

mac = "00:00:00:00:00:00" # mac address
decoded = "" # string for converting characters to hex

package = mac.split(':') # remove ':''

# iterate through mac address and convert to hex 2 characters at a time
for x in package:
decoded += x.decode('hex')

macAddress = '\xff'*6+decoded*16 # magic package!

#method for sending magic package
def wakeOnLan(mac):
mySocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
mySocket.sendto(mac, ('255.255.255.255', 9)) # broadcast address and port
mySocket.close()
wakeOnLan(macAddress) # Send it away baby!

---

Short version(where you have to manually edit the mac address):


import socket

macAddress = '\xff'*6+'\x00\x00\x00\x00\x00\x00'*16

def wakeOnLan(mac):
mySocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
mySocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
mySocket.sendto(mac, ('255.255.255.255', 9))
mySocket.close()
wakeOnLan(macAddress)

Thursday, August 25, 2011

RuntimeError: maximum recursion depth exceeded.

Fooling around with a simple recursive method. Apparently there is an default limit in Python preventing infinite recursion. You can bypass it by using following method of the sys class:

sys.setrecursionlimit(limit)

Set the maximum depth of the Python interpreter stack to limit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.

The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.


Sample test:

# Simple recursive funcktion in Python
import sys

sys.setrecursionlimit(10000)

def method(arg):
if arg == 0:
print "End."
else:
arg -= 2
print arg
method (arg)

method(10000)




Process big text files in Python.

I'm pretty sure that you could do this in several different and better ways(as usual when it comes to coding). But this is my solution for reading big text files in Python.


from sys import argv

file = argv[1]
input = ""

inputData = open(file)

#Function for reading chuncks

def readChuncks():
return inputData.read(1024)

#Iterate through the file

for data in iter(readChuncks, ''):

input += data #process the data

inputData.close()



Any thoughts/comments/suggestions would be greatly appreciated :-)

Wednesday, August 24, 2011

Python Tip: Simple HTTP server.

python -m SimpleHTTPServer [port]



That's it!

Note: Server will fetch from current working directory.