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

C++ Read from a file using fstream

/**
  Read from a file (version 1)
  Copyright 2009 Travis Crowder
  travis.crowder@spechal.com
  Published under the MIT License
*/
#include <iostream>
#include <fstream>
#include <string>

int main(int argc, char* argv[]){

  // create the file handle, opening the file
  std::fstream myFile("text.txt", std::ios::in);

  // create a string to hold the line
  std::string line;

  if(myFile.is_open()){
    while(!myFile.eof()){
      getline(myFile, line);
      std::cout << line << std::endl;
    }
  } else {
    std::cout << "Could not open text.txt for reading." << std::endl;
    return -1;
  }

  return 0;
}
Posted in C++ | Tagged | Leave a comment

C++ Write to a file using fstream

/**
  Write to a file (version 1)
  Copyright 2009 Travis Crowder
  travis.crowder@spechal.com
  Published under the MIT License
*/
#include <iostream>
#include <fstream>

int main(int argc, char* argv[]){

  // create the file handle, opening the file as well
  std::fstream myFile("text.txt", std::ios::out);

  if(myFile.is_open()){
    myFile << "I am just some text in some file.";
    myFile.close();
    std::cout << "File written to." << std::endl;
  } else {
    // could not create/write to file
    std::cout << "Could not write to file." << std::endl;
    return -1;
  }

  return 0;
}
Posted in C++ | Leave a comment

C++ Palindrome Checker

/**
  Palindrome Checker
  Copyright 2009 Travis Crowder
  Published under the MIT License
*/

#include <iostream>
#include <algorithm>
#include <string>

bool isPalindrome(const std::string theWord);

int main(int argc, char* argv[]){

  std::string theWord;

  if(!argv[1]){
    std::cout << "Enter the word to check: ";
    std::cin >> theWord;
  } else {
    theWord = argv[1];
  }
  std::cout << std::endl;

  /**
    Make all of the characters lower case
  */
  int i = 0;
  while(theWord[i]){
    if(isupper(theWord[i]))
      theWord[i] = tolower(theWord[i]);
    i++;
  }

  std::cout << theWord << " is ";
  if(!isPalindrome(theWord))
    std::cout << "NOT ";
  std::cout << "a palindrome." << std::endl;
  return 0;
}

bool isPalindrome(const std::string theWord){
  std::string tmp = theWord;
  reverse(tmp.begin(), tmp.end());
  if(tmp == theWord)
    return true;
  return false;
}
Posted in C++ | Tagged | Leave a comment