Progress while copying an img file to/from an SD card with OSX/Linux

I’m setting up some SD cards for my Raspberry Pi computers. Since it takes forever to backup and/or restore even a 4GB card, I wanted to see the progress of the copy. The dd command is not very informative in this regard, so I looked around for a solution. These instructions are for a raw .img file and not for fancier formats such as .dmg or .iso.

To copy to/from an SD card from OSX/Linux, make sure the SD card is not mounted. There is some infomation at elinux.org. The following commands assume you know the correct device files to unmount and copy to/from. The references to /dev/diskN will need to be updated to point to appropriate files on your computer.

Normal Unix method (usually works on Mac OSX):

sudo umount /dev/diskN

The Mac OSX way:

diskutil unmount /dev/diskN

In order to see the progress while copying to/from the SD card, you can use pv to send the source file to dd. pv will then show the progress as dd writes to the target location. I used homebrew to install pv on my mac.

To copy an image to the SD card:
WARNING: Using dd to copy to a disk is dangerous! Be extremely careful that the device you are writing to is in fact your SD card and not, for example, your boot drive (or any other drive you don’t want to completely erase and replace with the contents of your image file.)

pv -tpreb /path/to/image.img | sudo dd of=/dev/diskN bs=1m

To copy an SD card to an image:

sudo pv -tpreb /dev/diskN | sudo dd of=/path/to/image.img bs=1m

AppleScript for sending stuff to Xpenser with voice response…

Open the AppleScript Editor on your Mac, paste this in to a new script:

property theUsername : "your username"
property thePassword : "your password"
property theURL : "https://www.xpenser.com/api/v1.0/expense/"
property theQueryTimeout : 10
property theDefaultQuery : "Lunch 12.14 ModMarket with Marcus #clientX"

on postExpense(theQuery)
    set XpenserUpdate to "/usr/bin/curl -s -S -m " & theQueryTimeout & ¬
        " -u " & quoted form of (theUsername & ":" & thePassword) & ¬
        " -d " & quoted form of ("q=" & theQuery) & ¬
        " " & quoted form of theURL
    set XpenserResponse to do shell script XpenserUpdate
    if XpenserResponse contains "\"status\": 1" then
        return "Your expense was saved."
    else
        return "There was a problem saving your expense!"
    end if
end postExpense

set theResult to display dialog "Add A New Expense" default answer theDefaultQuery

set theQuery to text returned of theResult
set theButton to button returned of theResult
if theButton is equal to "OK" and theQuery is not equal to theDefaultQuery then
    set theResponse to postExpense(theQuery)
    say theResponse
else
    -- do nothing
end if

Then update your username and password and save as an application in your applications folder (I called it SendToXpenser).

SendToXpenser-AppleScript-Editor.png

Now you can type apple-space to open Spotlight type SendToXpenser and press return.

SendToXpenser-Spotlight.png

You are prompted for quick entry text and you get voice confirmation if the expense was added or not.

SendToXpenser-1.png

Using namespace qualified XML with a Flex DataGrid

I’ve been working on an Adobe AIR application that supports the administration of some XML content for a client. The source XML has a namespace assigned to it. And thus begins some more Flex/ActionScript shenanigans. I’ve searched the net for solutions to this and not found anything useful, so I hope this will help others.

For this post I’m simply trying to provide a viewer/editor for a list of authors. Here’s some sample XML:

<authors xmlns="http://www.example.com">
  <author>
    <honorific>Mr.</honorific>
    <lname>McGoo</lname>
    <fname>Jim</fname>
    <mname>S.</mname>
    <suffix>Esq.</suffix>
  </author>
  <author>
    <honorific>Ms.</honorific>
    <lname>McGoo</lname>
    <fname>Jane</fname>
    <mname>M.</mname>
    <suffix></suffix>
  </author>
  <author>
    <honorific>Mr.</honorific>
    <lname>McGoo</lname>
    <fname>Buddy</fname>
    <mname>L.</mname>
    <suffix>Jr</suffix>
  </author>
</authors>

In my project I have an XMLListCollection with this XML content in it called authors. What I want to do is have one row per author and one column per author name element. Following is a rough example of where I started:

<AuthorGrid id="dg" width="100%" height="100%" 
			allowMultipleSelection="false" editable="true"
			dataProvider="{authors}">
	<columns>
		<mx:DataGridColumn id="honorific" headerText="Title"       dataField="honorific" />
		<mx:DataGridColumn id="fname"     headerText="First Name"  dataField="fname"     />
		<mx:DataGridColumn id="mname"     headerText="Middle Name" dataField="mname"     />
		<mx:DataGridColumn id="lname"     headerText="Last Name"   dataField="lname"     />
		<mx:DataGridColumn id="suffix"    headerText="Suffix"      dataField="suffix"    />
	</columns>
</AuthorGrid>

Unfortunately this does not work because ActionScript is namespace aware, but lots of Flex controls are not and we defined a non-standard default namespace on our root element. This namespace was then applied to each descendant as would be expected. I’ve found some samples on the net that define a separate labelFunction for each column. While this may be useful in some cases, for me it was a big waste, what I ended up doing was adding a labelFunction to the whole grid. This function simply adds a namespace to the dataField already defined in the grid above:

protected namespace ns = "http://www.example.com";
protected function labelFunction(item:Object, column:DataGridColumn):String {
  use namespace ns;
  return item[column.dataField].toString();
}

OK, so now I have a grid that shows my authors, unfortunately I want the grid to be editable, notice the editable=”true” in the grid definition. While the grid goes into edit mode any values typed into cells do not get back into the source XML. The best solution I’ve found for this is to subclass DataGrid, there are a couple of methods that handle marshaling information into and out-of edit mode for cells, we need to override these methods and make sure they are aware of our namespace:

package
{
  import mx.controls.DataGrid;
  
  public class AuthorGrid extends DataGrid
  {
    protected namespace ns = "http://www.example.com";
    
    public function AuthorGrid()
    {
      super();
    }
    
    //default implementations of these two methods, intended for subclassing
    //not checking if it really is a complex value here as the performance hit of doing this here is negligible
    //compared with every display
    override protected function getCurrentDataValue( data:Object, property:String ):String
    {
      use namespace ns;
      // use namespace does not reach into a call to super so copied parents code
      if ( !isComplexColumn( property ) )
        return data[ property ];
      
      var complexFieldNameComponents:Array = property.split( "." );
      var obj:Object = deriveComplexFieldReference( data, complexFieldNameComponents );
      
      return String( obj );
    }
    
    //Passing all of these parameters as it basically allows everything you would need to subclass for all sorts of fun implementations
    override protected function setNewValue( data:Object, property:String, value:Object, columnIndex:int ):Boolean 
    {
      use namespace ns;
      // use namespace does not reach into a call to super so copied parents code
      if ( !isComplexColumn( property ) )
      {
        data[ property ] = value;
      } 
      else 
      {
        var complexFieldNameComponents:Array = property.split( "." );
        var lastProp:String = complexFieldNameComponents.pop();
        var parent:Object = deriveComplexFieldReference( data, complexFieldNameComponents );
        parent[ lastProp ] = value;
      }
      
      //The value they typed in is always converted to a string, but is the value actually a string in the dataprovider?
      //unknown as it is cast by datagridcolumn before datagrid ever gets to know...
      //control if this really causes an update in subclass
      return true;
    }
  }  
}

One last thing I wanted was for the honorific/title column to provide a list of possible values rather than the default TextInput when edit mode is invoked (The schema for my document requires one of a fixed set of values to be used.) The solution here is to update the definition for the honorific/title column as follows:

<mx:DataGridColumn id="honorific" headerText="Title" dataField="honorific"  >
  <mx:itemEditor>
    <mx:Component>
      <mx:ComboBox alpha="100" creationComplete="init()">
        <mx:Script>
        <![CDATA[
public function init():void {
  var ns:Namespace = new Namespace("", "http://www.example.com");
  var nm:XML = data as XML;
  var ho:XML = nm.ns::honorific[0];
  this.selectedItem = ho.toString();
}
        ]]>
        </mx:Script>
        <mx:String>Prof.</mx:String>
        <mx:String>Dr.</mx:String>
        <mx:String>Mr.</mx:String>
        <mx:String>Mrs.</mx:String>
        <mx:String>Ms.</mx:String>
        <mx:String>Miss</mx:String>
      </mx:ComboBox>
    </mx:Component>
  </mx:itemEditor>
</mx:DataGridColumn>

Well that sums up what it took to create an editable DataGrid bound to namespace qualified XML for our project. Not all that difficult in the end but it took quite a bit of digging to get here.

E4X/hasOwnProperty/XML and namespaces in Flex

If you’ve done very much work with e4x at all, you have probably run into situations where you want to determine if an XML variable contains a particular element or attribute. It is a common practice to use the hasOwnProperty() method to make such a check:

xmlVar.hasOwnProperty("element");
xmlVar.hasOwnProperty("@attribute");

Unfortunately if your XML makes use of namespaces it is not abundantly clear how to use the hasOwnProperty() method (esp. since the argument is listed as being of type String) to this end.

After much trial and error, I discovered that given the following XML variable:

var xmlVar:XML = 
<foo xmlns="http://example.com/uri1" bar="baz">
  <bing>value</bing>
</foo>

You can pass QName() instances into hasOwnProperty() as follows:

var nsDefault:Namespace = new Namespace("*"); // match default ns
var nsURI1:Namespace = new Namespace("http://example.com/uri1");
var nsURI2:Namespace = new Namespace("http://example.com/uri2");

xmlVar.hasOwnProperty( new QName(nsDefault, "bing") ); // returns true
xmlVar.hasOwnProperty( new QName(nsURI1, "bing") ); // returns true
xmlVar.hasOwnProperty( new QName(nsURI2, "bing") ); // returns false!
xmlVar.hasOwnProperty( new QName(nsDefault, "@bar") ); // returns true
xmlVar.hasOwnProperty( new QName(nsURI1, "@bar") ); // returns true
xmlVar.hasOwnProperty( new QName(nsURI2, "@bar") ); // returns false

And there you have it, QName() derives from Object and not String, and yet you are allowed to pass it into hasOwnProperty() and it allows us to check for elements and attributes that have a namespace attached to them.

I thought that the “*” namespace could be used to match any namespace, however it only appears to work with a default (no alias provided) namespace. As such the following XML variable:

xmlVar =
<foo xmlns:ns="http://example.com/uri1" ns:bar="baz">
  <ns:bing>value</ns:bing>
</foo>

Works as follows:

xmlVar.hasOwnProperty( new QName(nsDefault, "bing") ); // returns false
xmlVar.hasOwnProperty( new QName(nsURI1, "bing") ); // returns true
xmlVar.hasOwnProperty( new QName(nsURI2, "bing") ); // returns false
xmlVar.hasOwnProperty( new QName(nsDefault, "@bar") ); // returns false
xmlVar.hasOwnProperty( new QName(nsURI1, "@bar") ); // returns true
xmlVar.hasOwnProperty( new QName(nsURI2, "@bar") ); // returns false

As you would expect passing element names or attribute names that don’t exist into hasOwnProperty() via QName() always returns false.

UPDATE:

Many thanks to Dan at our office, he mentioned a much more elegant solution to this problem that doesn’t appear to be widely known:

if( xmlVar.nsURI1::bing != undefined ) trace( "we have the element" );
if( xmlVar.@nsURI1::bar != undefined) trace( "we have the attribute" );

Thanks again Dan!

Validating initialization of nonvisual components in Flex 3

WARNING: Turns out that this is probably not as good of an idea as it first appeared to be, I’m adding some details at the bottom…

I’m working on a really cool Flex 3 project at work. One of the things we’ve done architecturally is to wrap up major portions of the server communication and business logic into manager classes (think of these as MVC controllers.) We use the IMXMLObject interface for these managers so that they can be instantiated in our main Application MXML and we pass managers to other managers explicitly rather than having components reaching up to the application to obtain these references. One of the main drivers for passing this stuff in explicitly is that we’d like the managers to be testable (with FlexUnit) without having to setup loads of application state that isn’t well defined.

Unfortunately when instantiating a class via MXML there is no way that I’m aware of to pass constructor parameters to the object. You can of course set a bunch of properties, but unfortunately there also does not appear to be a way to indicate that certain properties are required prior to a component being used (yeah I realize it’s hard to define when the component is being used.)

I’ve been noodling on this in the back of my mind for a while. Last night I did some searching and didn’t come up with anything. Then I had this idea that we could put a set of conditions into the initialized() method of these components that checks if all of the required properties have been set. If they have, all is good, if they have not, we throw an exception. Yes I know this is simple and probably obvious, but since I couldn’t find references to this after searching for some time, I figured I’d post about it.

Unfortunately this doesn’t allow us to catch a missing dependency at compile time, but the code simply won’t work if a component hasn’t had all of its required attributes set… whereas previously the component could attempt to work and fail in weird and hard to quantify ways. The other downside of this is that we have to add an explicit call to initialized() in all of our unit tests that construct these managers (or their surrogates.) But all-in-all, I feel more comfortable that someone won’t accidentally use one of these managers without first providing all of the required input properties and that even if this is done in Unit tests, the code will fail quickly with a message that will allow folks to provide the missing dependency and move on.

UPDATE:

We pass managers to each other like so: <primarymanager secondaryManager=”{secondaryManagerId}”/>. This is the only way I’m aware of passing other instances of MXML objects to each other. As I’m sure you are aware the squiggly-brace syntax is turned into binding in the background. What you may or may not know is that initialized is called before your binding expressions are initialized!

My solution to this is to throw up my hands in disgust and instantiate my manager objects in the constructor for the code-behind for our application; I can use regular constructor parameters now. I really don’t like this because the object instantiation is no-longer visible to the user working with the application MXML; i.e. they need to know about the manager instances.

What we have here is one more catch-22, can’t pass instances to properties without using binding, and binding doesn’t get called prior to initialized() being called on objects that implement IMXMLObject. I sure wish Adobe would just give us a [Requierd] annotation for properties, also wish they’d allow for parameter initialization with object instances without requiring the overhead of Binding. I only set these things once, don’t need to watch for changes, etc.

If anyone figures out an elegant solution for setting up an initialization contract for MXML components, I’d sure appreciate a comment/link.

Vitamix: Intro & A Couple Lessons Learned.

We recently purchased a Vitamix 5200 (which is a super powerful blender) in order to help us prepare food at home more regularly. The Vitamix 5200 comes with a great ring bound recipe book that contains all sorts of things you can make:

  • Smoothies (green and otherwise)
  • Juice (a little tricky see comments further down)
  • Fresh Fruit Margaritas
  • Salsa
  • Guacamole
  • Humus
  • Soups hot (it boils liquid from room temperature in about 6 minutes with just friction) and cold
  • Sorbet/Ice-Cream
  • Nut Butter (Cashew is GRRRREEAT)
  • Nut Milk
  • Savory sauces to put on other dishes (Sour-cream/Dill for fish is wonderful)

We’ve tried variations on most of these, other things you can make (stuff we haven’t tried):

  • Scrambled Eggs (I wanted too, but sounds like nasty cleanup)
  • Salad Dressings
  • Emulsions
  • Crushed Ice
  • Chop a quarter of a cabbage for slaw in about 1 second!
  • Mayo
  • Marinades
  • Pesto
  • Butter (from heavy cream)
  • Cold compost

Seriously the list just keeps going on and on, you can checkout loads of recipe’s, video’s etc on the Vitamix website. Oh and with the exception of the Nut Butter, it’s seriously easy to clean (rinse, half fill with warm water, one drop of dish soap and blend for 15-30 seconds, then rinse again.) I’ve seen posts from others that if you leave it for even an hour or two after decanting your creation, cleaning can become seriously painful.

We’ve gone from maybe cooking at home 2 nights a week to eating out about 2 nights a week. The main thing we make is smoothies, although these vary from something approaching fresh and delicious V8 to something approaching blended tropical fruit cocktail and even mixtures of both (yes fruit and veg and even spices taste great together) so in a way it’s hard to consider all of these delicious concoctions under the one uber title of smoothie…

We make the fruit margaritas described in the getting started guide pretty regularly on a Friday evening after work. I generally am not a drinker, but this drink is to-die-for (we use a little more fruit and a little more sugar than is called for, but even the original recipe is fab.)

I’m a bit sensitive to milk so one of the things I tried the first day was to make some almond milk. The recipe from Vitamix says to load up 1 cup of chopped raw almonds and 3 cups of water and mix for about 2 minutes.. Unfortunately, this mixture had a noticeable fine grit to it (to be frank it felt a bit like drinking super fine sand paper.) I strained some through a couple of paper towels, which improved things but it was still a bit harsh and also a bit watery. After doing some research on the net and obtaining a nut milk bag (actually just a nylon bag used to filter stuff when canning) I had the most amazing second experience making alternate milk today. I put 1 cup of chopped raw almonds into our 32oz container, added 3 cups of water, a small splash of vanilla extract (most folks suggest using real vanilla, but I don’t have any) and let stand for an hour or two. Then mixed for 2 minutes, strained through nut milk bag, put back into freshly rinsed Vitamix container with a few dates and whizzed around for 30 seconds or so… Comes out warm, frothy and delicious. No more sand paper and oh my god it tastes amazing!

Unlike a juicer, the Vitamix leaves all the fiber of your ingredients in your concoction. In many cases this is great, but if you want an actual juice (orange, applet, etc) you can end up with a rather too thick liquid for some folks tastes… I rather enjoyed the orange mouse we got from whizzing up a few oranges with pith… well at least I liked the idea of it… but it was pretty weird in reality. Again after some research I discovered that the pith, while extremely good for you, can make your recipe very foamy, adding ice/water and getting rid of most of the pith helps. Today I tried straining some apple juice (was the consistency of very watery apple sauce) using the nut milk bag and got a lovely glass of regular apple juice sans thickener. Based on net searches, I believe this will also work for OJ.

Overall I’d say this little machine has proven to be a life changing force in our lives. I’m thrilled to have it and expect we’ll be using it for very many years to come (did I mention it comes with a 7 year warranty?) Yes it’s expensive and may not have quite as good technical specs as it’s main similarly priced competitor, but I believe (again based on research) that this is the finest blender you can buy. The Vitamix marketing is all focused on making delicious food rather than silly stuff like blending iPods/Golf-Clubs/etc… for some reason this makes a huge difference to me… After all, I’m looking for a blender to make tasty and simple to prepare food with!

Flex:Actionscript:E4X – Bug in implicit xml namespace handling

Just posted the following bug report to the adobe bugs site.

Steps to reproduce:
Construct an XML variable with no explicit namespaces and any xml: attribute such as xml:lang or xml:id. E4X will correctly construct an implicit namespace for xml:, but will not assign the xml prefix to it, as such you end up with a default namespace which now applies to anything in your XML that doesn’t explicitly have another namespace associated with it.

Here’s a sample flex app that demonstrates the issue.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
<mx:Script>
<![CDATA[
private function init():void
{
var x:XML = <root xml:lang="en"><element/></root>
ta.text = x.toXMLString();
}
]]>
</mx:Script>
<mx:TextArea id="ta" width="100%" height="100%"/>
</mx:Application>

Actual Results:

<root lang="en" xmlns="http://www.w3.org/XML/1998/namespace">
<element/>
</root>

Notice that the root and element elements now incorrectly exist in the http://www.w3.org/XML/1998/namespace namespace.

Expected Results:

<root someprefix:lang="en" xmlns:someprefix="http://www.w3.org/XML/1998/namespace">
<element/>
</root>

Where someprefix is probably xml…

Workaround (if any):

  • The xml:lang element was being added by our XML database and is not actually used by our app, so we simply removed it.
  • You can specify xmlns:xml=”http://www.w3.org/XML/1998/namespace&#8221; in the root element and then the correct prefix is applied.

Bug in how Flex handles Tree data binding

We discovered a couple of very strange bugs in how Flex trees handle data binding today. Our tree has showRoot=”false”. We are using e4x to bind to a sub-element of some source XML. Here is a sample MXML file:

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml&#8221; layout=”vertical” creationComplete=”init()”>
<mx:Script>
<![CDATA[
public function onAdd():void
{
x2.structure.appendChild(<entry label=”dynamic”/>);
}
]]>
</mx:Script>
<mx:XML id=”x1″ xmlns=””>
<project label=”root”>
<metadata label=”metadata root”>
<entry label=”1st metadata entry”/>
<entry label=”2nd metadata entry”/>
</metadata>
<structure label=”content”>
<entry label=”one”>
<entry label=”one.one”/>
</entry>
<entry label=”two”/>
</structure>
</project>
</mx:XML>
<mx:XML id=”x2″ xmlns=””>
<project label=”root”>
<metadata label=”metadata root”>
<entry label=”1st metadata entry”/>
<entry label=”2nd metadata entry”/>
</metadata>
<structure label=”structure”>
</structure>
</project>
</mx:XML>

<mx:Tree id=”t1″ dataProvider=”{x1.structure}” showRoot=”false” labelField=”@label” width=”100″ height=”100″/>
<mx:Tree id=”t2″ dataProvider=”{x2.structure}” showRoot=”false” labelField=”@label” width=”100″ height=”100″/>
<mx:Button label=”Add” click=”onAdd()”/>

</mx:Application>

Second Tree Should Be Empty

Second Tree Should Be Empty

After Adding An Entry To An Empty Container The Tree Renders From The DataProviders Parent

After Adding An Entry To An Empty Container The Tree Renders From The DataProviders Parent

The first thing you notice when you run this is that while the second tree (on the left) should be empty it is in fact not! The showRoot=”false” setting does not appear to be honored if the root element doesn’t have any children. This appears to be written up at Adobe as Bug SDK-15083.

The second extremly strange behavior occurs when we add an item to the structure container that’s mapped to the second tree (we do this by clicking the Add button). Instead of ending up with a tree with our new entry, we end up with a tree that has a node for the metadata element and a node for the structure element… It appears that the databinding has been pushed up a level somehow (see picture on right.)

We were able to workaround the first bug using the workaround described in the SDK-15083 writeup. We had to force a full refresh of the data binding for the tree when adding a first item in order to resolve the second issue.