More Sharpy

Since the last post I’ve added basic database support for MySQL and PostgreSQL.  In addition is a basic ACL and a DataMapper class to build models from.  I just finished up adding in the ability to execute methods before and after any action by using before_filter and after_filter.

Things are moving along.  Hopefully there will be something useful in the near future.

Posted in Internet, Patterns, PHP | Leave a comment

Sharpy

So I decided to start working on a rapid application development (RAD) framework for PHP the other day after a brief stint with Ruby on Rails.  I’ve decided to call the framework Sharpy.  The code is hosted by Google Code and can be found at http://code.google.com/p/sharpy/

I still need to add a database handler, internationalization, [lots more...] and refine the code comitted thus far, but it is a functional and small PHP MVC framework that supports templates.

Hopefully someone out there will find it useful.

Posted in Internet, Patterns, PHP | Leave a comment

Generate and validate UUID / GUID with PHP

    function UUID(){
    	return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff),
        mt_rand(0, 0x0fff) | 0x4000,
        mt_rand(0, 0x3fff) | 0x8000,
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff));
    }

    function validUUID($uuid){
    	return preg_match("#^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$#", $uuid);
    }
Posted in PHP | Leave a comment

phpize not found

This error is typically generated when you don’t have the PHP development libraries installed.

Installing the libraries via repository is the easiest and quickest fix.

For Fedora/Red Hat:
yum install php5-dev

For Ubuntu/Debian:
apt-get install php5-dev

Posted in Linux, PHP | Tagged , , , | Leave a comment

Python MySQL Associative Array

By default, the MySQLdb module to access a MySQL database returns the data as an integer indexed array. Sometimes this is not desirable and an associative array is preferred, such as the mysql_fetch_assoc function in PHP. In addition, we will be using the pprint module which is similar PHP’s print_r

Here is the Python equivalent:

import MySQLdb
import pprint
# connect to the database here
db = MySQLdb.connect(...)
c = db.cursor(cursorclass=MySQLdb.cursors.DictCursor)
query = "SELECT `name`, `address` FROM `table`"
c.execute(query)
rows = c.fetchall()
c.close()
pprint.pprint(rows) # i.e. print_r($rows)

Hope that helps someone.

Posted in MySQL, PHP, Python | Tagged , , | Leave a comment

Python emulate mouse click in Windows

The following bit of code, which requires the win32api, simulates a mouse click using Python under the Windows OS.

import win32api, win32con

def click(x,y):
    win32api.SetCursorPos((x,y))
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)

click(100, 100) # simulate mouse click at 100px, 100px
Posted in Python, Windows | Leave a comment

Windows Batch FTP Routine

Here is some example code to connect to an FTP server and run the commands that follow.

@echo on

%windir%\system32\ftp.exe -n -s:"%~f0" server.com
goto done
user myusername
mypassword
cd /home/directory/public_html
ls -al
quit
:done
pause

The key to this working are the following:

The -n switch allows you to not be prompted for a username upon connection.
The -s:”%~f0″ says to use the rest of the file as the commands to execute
The goto command is not a valid FTP command, so the FTP server ignores it.

So you have the script using the Windows FTP utility to connect to a server and a routine running your commands. You can even extend off of this.

Posted in Windows | Tagged , | Leave a comment

Python Print Screen and Save Image in Windows

So you need to do a “Print Screen” and save the image in the clipboard huh?  I had that exact need too.  Let me save you a few hours of trouble and tell you that the win32clipboard does not yet have the ability to return the contents of the clipboard when the contents are BITMAP, despite the CF_BITMAP constant.

How do you do it then?  I was wondering the same thing.  Behold, PIL.  PIL, the Python Image Library, saved me from a massive headache.

Here is the code to take a screen shot and save it.  You will need the win32api and PIL libraries.

import win32api, win32con, ImageGrab
win32api.keybd_event(win32con.VK_SNAPSHOT, 1)
im = ImageGrab.grabclipboard()
im.save("screenshot.jpg", "JPEG")

Now wasn’t that easy!

Posted in Python, Windows | Tagged | Leave a comment

Python Abstract Snow Art using PyGame

#
#   Abstract Random Snow Art
#   Copyright 2009 Travis Crowder
#   travis.crowder@spechal.com
#   Published under the MIT License
#
import pygame, sys, random
from pygame.color import THECOLORS

pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([0,0,0])

for i in range(1, 2500):
    top = random.randint(2, 478)
    left = random.randint(2, 638)
    color_name = random.choice(THECOLORS.keys())
    color = THECOLORS[color_name]
    pygame.draw.rect(screen, color, [left, top, 1, 1], 1)
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
Posted in Python | Tagged , , | Leave a comment

Python Abstract Box Art using PyGame

#
#   Abstract Random Box Art
#   Copyright 2009 Travis Crowder
#   travis.crowder@spechal.com
#   Published under the MIT License
#
import pygame, sys, random
from pygame.color import THECOLORS

pygame.init()

screen = pygame.display.set_mode([640,480])
screen.fill([0,0,0])

for i in range(1, 100):
    width = random.randint(0, 250)
    height = random.randint(0, 250)
    top = random.randint(5, 435)
    left = random.randint(5, 375)
    color_name = random.choice(THECOLORS.keys())
    color = THECOLORS[color_name]
    line_width = random.randint(1,3)
    pygame.draw.rect(screen, color, [left, top, width, height], line_width)
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
Posted in Python | Tagged , , | Leave a comment