Archive for November, 2007

Fake SMTPd

Tuesday, November 27th, 2007

So I wrote a fake SMTP server for testing DrProject. Check it out.

Thanks,
Dave

Decorative Python

Wednesday, November 21st, 2007

In my code for DrProject/REST, I rather like a trivial 2 lines of Python that ever so thinly wraps around property and is used for decoration.


def Property(func):
   return property(doc=func.__doc__, *func())

If it’s not immediately clear what this does, you’re new to Python decorators. Here’s an silly, useless, but otherwise insightful example:


class Test(object):

   def __init__(self, value):
      self.__data = value

   @Property
   def data():
      '''Here is my docstring'''
      def fget(self):
         return self.__data
      def fset(self, value):
         self.__data = value
      def fdel(self):
         raise NotImplementedError, "Cannot delete"
      return fget, fset, fdel

That’s all,
Dave

AJAX Persistent Request Objects API

Wednesday, November 14th, 2007

reqobj.js - AJAX (XMLHttpRequst) Persistent Request Objects API.
Used for Café Chess (http://play.cafechess.com/js/reqobj.js)

(Note: All functions/variables with an underscore before them are private)

Variables:

unsupported_redirect (string)
Set this to the URL you’d like the script to redirect to if an XMLHttpRequest() object can not be created.

Example:

 

var unsupported_redirect = 'http://www.example.com/nosupport.html';

Functions:

do_req(postdata, url, callback, timeout_interval) (boolean)
Does a single HTTP POST call. Returns true if the call goes through, false if there is already a call in progress.

  • postdata (string) - The data to send.
  • url (string) - The url to post to (should be a relative path or most browsers will throw an error).
  • callback (string) - Just the name (ie: no brackets) of a function that will accept one input parameter (either false or a string) to be called when the request has gone through. The request will call this function with the returned data upon success or false upon failure.
  • timeout_interval (int) - The amount of time (in milliseconds) that the request should wait for a response. After this time, the callback function will be called with false.

Example:

 

function handle_request(data) {

  if (data)

    document.write(data);

  else

    document.write('An error occurred.');

}var response = do_req('post this', 'post.php', 'handle_request', 5000);

if (!reponse)

  alert('Request could not be sent');

p_do_req(callforpostdata, url, callback, interval, timeout_interval) (boolean)
Creates a persistent HTTP POST object that runs in the background. Returns true if the object could be created, false if there has already been an object created.

  • callforpostdata (string) - Just the name (ie: no brackets) of a function that accepts no input parameters and will return a string to use as post data for each call.
  • url (string) - The url to post to (should be a relative path or most browsers will throw an error).
  • callback (string) - Just the name (ie: no brackets) of a function that will accept one input parameter (either false or a string) to be called when the each request has gone through. The object will call this function with the returned data upon success or false upon failure.
  • interval (int) - The amount of time (in milliseconds) that the object will wait after each call (upon success or failure) until the next call.
  • timeout_interval (int) - The amount of time (in milliseconds) that the object should wait for each response. After this time, the callback function will be called with false and the next call queued.

Example:

 

function handle_request(data) {

  if (data)

    document.write(data + '<br>');

  else

    document.write('An error occurred.' + '<br>');

}function get_post_data() {

  return 'post_this';

}

var response = p_do_req('get_post_data', 'post.php', 'handle_request', 1250, 5000);

if (!reponse)

  alert('Object could not be created');

p_kill_req() (boolean)
Attempts to destroy an already active persistent request object. Returns true if one exists and it is destroyed, returns false is there isn’t one to destroy.

Now that’s that,
Dave

(Note that this was imported from my old blog. If the formatting is messy let me know so I can change it.)