How to retrieve information about Python errors in a C extension

Python posted 16 days ago by christian

   1  result = PyEval_CallObject(tmp_callback, args);
   2      // result == NULL means an error occured
   3      if (PyErr_Occurred()) {
   4          PyObject* ptype;
   5          PyObject* pvalue;
   6          PyObject* ptraceback;
   7          PyErr_Fetch(&ptype, &pvalue, &ptraceback);
   8          printf("Error occurred on line: %d", ((PyTracebackObject*)ptraceback)->tb_lineno);
   9          // Restore exception instead of disposing of it
  10          PyErr_Restore(ptype, pvalue, ptraceback);
  11          PyErr_Print();
  12  
  13          Py_XDECREF(ptype);
  14          Py_XDECREF(pvalue);
  15          Py_XDECREF(ptraceback);
  16      }

via http://www.ragestorm.net/tutorial?id=21

Tagged python, pyeval_callobject, pyerr_fetch

How to view a Git repository in your browser with "git instaweb"

Shell Script (Bash) posted 21 days ago by christian

git instaweb -d webrick:

   1  % git instaweb -d webrick      
   2  [2010-02-18 17:27:01] INFO  WEBrick 1.3.1
   3  [2010-02-18 17:27:01] INFO  ruby 1.8.7 (2008-08-11) [i686-darwin10.0.0]
   4  No known browser available.
   5  http://127.0.0.1:1234

Now open http://127.0.0.1:1234 in your browser.

You can also add this this to ./gitconfig:

   1  [instaweb]
   2  httpd=webrick

Tagged git, instaweb, browser

A simple HTTP client for PHP

PHP posted 22 days ago by christian

A simple HTTP client for PHP that uses curl:

   1  class HTTP {
   2      static function get($url, $headers=null) {
   3          $ch = curl_init();
   4          curl_setopt($ch, CURLOPT_URL, $url);
   5          curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
   6          curl_setopt($ch, CURLOPT_HEADER, true);
   7  
   8          if($headers) {
   9              curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  10          }
  11  
  12          // Execute request
  13          $response = curl_exec($ch);
  14  
  15          // Split headers and body
  16          list($header_text, $body) = explode("\r\n\r\n", $response, 2);
  17          $header_lines = explode("\r\n", $header_text);
  18  
  19          // Extract HTTP status
  20          $header_line = array_shift($header_lines);
  21          if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@', $header_line, $matches)) {
  22              $status = $matches[1];
  23          }
  24  
  25          // Extract HTTP headers
  26          $headers = array();
  27          foreach ($header_lines as $header_line) {
  28              list($header, $value) = explode(': ', $header_line, 2);
  29              $headers[$header] = $value;
  30          }
  31  
  32          $info = curl_getinfo($ch); 
  33  
  34          curl_close($ch);    
  35  
  36          $status = $info['http_code'];
  37  
  38          return new Response($status, $body, $headers, $info);
  39      }
  40  }

Even supports reading response headers.

   1  class Response  {
   2      public function Response($code, $body, $headers, $info=null) {
   3          $this->code = $code;
   4          $this->body = $body;
   5          $this->headers = $headers;
   6          $this->info = $info;
   7      }
   8  }

Tagged http, client, php

How to use a Python decorator wrapper to get a reference to the calling class instance

Python posted 22 days ago by christian

   1  def requires_authentication(method):
   2      """
   3      self points to a SheisseController instance instead of the decorator function.
   4      """
   5      def wrapper(self, *args, **kwargs):
   6          if self._requires_authentication == True and self._authenticated == False:
   7              return response('403 Forbidden or whatever')
   8  
   9          return method(self, *args, **kwargs)
  10      return wrapper
  11  
  12  class SheisseController:
  13    @requires_authentication
  14    def index(self):
  15  

Tagged python, decorator, self

How to use dual-purpose accessors in Ruby to create a DSL

Ruby posted 28 days ago by christian

Instead of this:

   1  Sitemap('public/sitemap.xml') do
   2    self.stylesheet = 'public/sitemap.xls'  
   3    self.ping = ['http://www.google.com', 'http://www.google.com']  
   4  end

You could write this:

   1  Sitemap('public/sitemap.xml') do
   2    stylesheet 'public/sitemap.xls'  
   3    ping ['http://www.google.com', 'http://www.google.com']  
   4  end

Using dual-purpose accessors:

   1  class Sitemap
   2    def stylesheet(path = nil) 
   3      return @path unless path
   4     @path = path
   5    end
   6    alias_method :stylesheet=, :stylesheet 
   7    ...
   8  end

Tagged ruby, dsl