Erik’s Blog

Erik’s Blog

Archive for October 2008

PHP atomic write function

with 2 comments

Motivation:

An atomic write function for those who appreciate the convenience of file_put_contents(), but need an atomic operation.  To be clear, this function does not perform all its actions in a single operation, but the replacement of the target file’s contents is atomic.  Please leave a comment if you have a more convenient and/or efficient solution.

Source code:


function atomic_put_contents($filename, $data)
{
    //define quasi-unique name of file to write tmp data to
    $tmp_path = dirname($filename).'/'.microtime(true);

    //file_put_contents fails if file doesn't exist
    if(!is_file($tmp_path))
    {
        touch($tmp_path);
    }

    //file put contents is non-atomic, so write to tmp
    file_put_contents($tmp_path, $data);

    //mv is an atomic operation
    exec("mv -f $tmp_path $filename");
}

Example usage:


$content = 'some content';

atomic_put_contents('path/file.ext', $content);

//creates file called path/file.ext containing 'some content'

Written by Erik

October 26, 2008 at 1:58 am

Posted in code

Tagged with

PHP class to manage read-only vars and arrays

without comments

Source code:


class Constants
{
    private $var_cache;

    public function set($key, $val)
    {
        if(isset($this->var_cache[$key]))
        {
            throw(new Exception("Constants->get() error: var $key has already been defined"));
        }
        else
        {
            $this->var_cache[$key] = $val;
        }
    }

    public function get($key)
    {
        return $this->var_cache[$key];
    }
}

Example usage:

$constants = new Constants();

$constants->set('base_url', 'www.domain.com/path');

...

$img_src_url = $constants->get('base_url').'/img/file.jpg';

echo "<img src='$img_src_url'/>";

Written by Erik

October 26, 2008 at 12:04 am

Posted in code

Tagged with

Everything

without comments

Must everything be important?

My favorite color is

the golden green produced by

sunlight shining through a leaf,

particularly those of fig trees and grape vines.

Sitting in the shade of a grape arbor,

I am awash in my favorite tone.

It’s calming;

my eyes are soothed.

I feel supported by the clay and dried grass beneath me.

I can imagine the water flowing

through the roots of this plant,

to transpire from its leaves.

I am home.

My favorite color is important to me.

Written by Erik

October 25, 2008 at 10:57 pm

Posted in poetry

Tagged with

sap of life

without comments

the sap of life

flows

from fingers to toes

and yet

is also still

like the air

we breath it in

and move in it

it is made of us

and we of it

Written by Erik

October 25, 2008 at 10:42 pm

Posted in poetry

Tagged with

a rock ledge

without comments

a rock ledge

far from the sounds of leaf blowers, garbage trucks, and trains

close to the nature of things

insignificant

below the snow

above a river wrapped in green

rust-colored

ancient

exposed to air and unabated starlight

Written by Erik

October 25, 2008 at 10:39 pm

Posted in poetry

Tagged with

Mother & baby by Os Gemeos

without comments



Mother & baby by Os Gemeos, originally uploaded by server pics.

Amazing art by Os Gemeos

Written by Erik

October 23, 2008 at 12:23 am

Posted in Uncategorized

Beautiful graffiti by Daim

without comments



Daim, originally uploaded by trashisfesch.

Written by Erik

October 23, 2008 at 12:22 am

Posted in Uncategorized

Test-driven JavaScript development with YUI

without comments

Notes from “Test Driven Development with YUI Test” presentation given by Nicholas Zakas

  • Flow:
    1. Define requirements
    2. Write tests to verify requirements are met
    3. Write code to test
    4. Run tests
    5. If test succeed, repeat from step 1 until project is finished. If tests fail, repeat from step 3 until tests pass, and then repeat from step 1 until project is finished
  • Motivations:
    • Even small changes can break a complex app.  Use unit tests and suites of tests to ensure app integrity after each change to the code base.
    • Tasks become well-defined using this methodology
    • We are best positioned to perform QA because we know our code best
  • Tips & tricks:
    • Use a test template to speed up the process
    • Use setUp and tearDown functions to prepare and clean environment, respectively, after each test is run
    • Develop the habit of creating files for code and tests for the code in pairs, e.g. code.js & test_code.js
    • In general, each method gets a test and each object gets a test case
    • In your build process, define a “test” state that inserts YUI test code into the app code base
  • References:

Written by Erik

October 14, 2008 at 6:07 am

Posted in Uncategorized

building an RSS consolidator w/ Yahoo! Pipes

without comments

Goal:

Consolidate the feeds from my Flickr, WordPress, Del.icio.us, etc accounts

Solution:

Use Yahoo! Pipes

Intended Audience:

Pipes newbies

Steps:

Create a pipe

  1. Go to http://pipes.yahoo.com and log in, or create an account if you don’t already have one
  2. Click the “Create a pipe” button
  3. In the editor, click and drag a “Fetch Feed” object onto the stage
  4. Open a new browser tab or window, head over to http://flickr.com/, and log in, or create an account
  5. Click on the “Your photostream” link to view your photostream
  6. Scroll to the bottom of the page and copy the “Latest” RSS feed URL
  7. Switch back to the Pipes editor and paste the Flickr URL into the text field in the “Fetch Feed” object on the stage
  8. On the bottom on the “Fetch Feed” object, there is a circular attachment point.  Click on it an drag to start forming a new pipe
  9. Drag the pipe to the attachment point on top of the “Pipe Output” object at the bottom of the stage.
  10. Release the pipe when the “Pipe Output” attachment point glows.
  11. Click the “Save” button to name and save your new pipe
  12. Click the “Back to my Pipes” link next to the save button
  13. Click on the name of your pipe in the list of your pipes.  This will display the output of your pipe.
  14. Click the “More Options” link and then “Get as RSS” in the drop-down.  This will open the RSS feed in your browser if you’re using Safari or Firefox.
Build page
  1. Following an example from Rasmus Lerdorf’s Open Hack talk (http://talks.php.net/show/hack08/8), use simplexml to parse the RSS feed:
<?php
$url = '<URL for Pipes output>';
//e.g. http://pipes.yahoo.com/pipes/pipe.run?_id=gIBkHK_V3RGpmv5oBRNMsA&_render=rss
$xml = simplexml_load_file($url);
$num_items = count($xml->channel->item);
for($i = 0; $i < $num_items; $i++)
{
    $title = $xml->channel->item[$i]->title;
    $date = $xml->channel->item[$i]->pubDate;
    $desc = $xml->channel->item[$i]->description;
    $link = $xml->channel->item[$i]->link;

   echo "
<div>";
   echo "
<h3><a href='$link'>$title</a></h3>
";
   echo "$date";
   echo "$desc";
   echo "</div>
";
}
  1. Go back to the browser tab displaying the output from your pipe and copy the URL
  2. Paste this URL into your index.php file as the value for the variable “$url”
  3. Change the beginning of the URL from ‘feed://…’ to ‘http://…’
  4. Load your page and you should see the output defined above.  Note: var_dump the ‘$xml’ object to see the other available fields.
Extend pipe
  1. Back in the Pipes editor, drag another “Fetch Feed” object onto the stage
  2. Copy and paste another RSS feed into it
  3. From under the “Operators” heading in the list on the left, drag out a “Union” object
  4. Run the pipes from the two “Fetch Feed” objects into the union and pipe the output to the “Pipe Output” object
  5. To add more feeds, drag out additional “Fetch Feed” objects and connect them to the union

Written by Erik

October 9, 2008 at 7:25 am

Posted in Uncategorized

Tagged with , , ,