Viewing posts for the category development

Quick/Cheap website technology stack

Posted by: mhughes in development 1 year, 1 month ago

Are you looking to quickly make and host a small web app cheaply?  Here's the software stack that I use nowadays.

  • Python + Django 1.4 (https://www.djangoproject.com/)
  • Hosted on Heroku (http://www.heroku.com/)
  • A MySql database hosted on Amazon RDS (This is only cost-effective if you have more than 2 sites using the same DB, otherwise, use Heroku's DB)
  • Amazon S3 for static file serving
  • Twitter Bootstrap for a base CSS/JS stack
  • Stripe.com for payments
  • Desk.com for help desk
  • BitBucket.org for source control (GitHub for open source stuff)
  • Pusher.com if I need real-time / push capabilities

Using that stack I can get a new (small) site up and running for almost no incremental cost.

read more / 1 comment

Compiling FXP->SWC, a Catalyst workflow

Posted by: mhughes in development flex catalyst 3 years, 11 months ago

The other day, I blogged about an alternate workflow to Catalyst. It was somewhat confusing with a few steps involved. Towards the end of the post I had a couple wishes including:

  1. Let me select a package that components get created in.
  2. Export a swc with everything in it so we don’t have to go through most of this.
Last night I wrote a ruby script capable of doing exactly those two things.  I call it fxp2swc.rb.

Using the script is simple.  First open it up and set the two variables at the top.

@flex_dir = "/Applications/Adobe\ Flash\ Builder\ Beta/sdks/4.0.0";
@default_package = "com.roguedevelopment.catalyst";
The flex_dir just tells the script where your Flex SDK is. The default_package tells it what package you want your catalyst based components made in.

After you edit that, put the script in the same directory as your .fxp files. Run the script and you should see output like this:

$ ./fxp2swc.rb
=== FCTest.fxp
 Extracting to FCTest.fxp.tmp
 Creating directory FCTest.fxp.tmp/src/com
 Creating directory FCTest.fxp.tmp/src/com/roguedevelopment
 Creating directory FCTest.fxp.tmp/src/com/roguedevelopment/catalyst
 Creating directory FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest
 Copying Main.mxml
 Copying FCTest.fxp.tmp/src/components/Background.mxml -> FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest
 Copying FCTest.fxp.tmp/src/components/CustomComponent1.mxml -> FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest
 Copying FCTest.fxp.tmp/src/components/LoginButton.mxml -> FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest
 Copying FCTest.fxp.tmp/src/components/LoginForm.mxml -> FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest
 Copying FCTest.fxp.tmp/src/components/TextInput1.mxml -> FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest
 Removing old source locations
 Fixing package names in FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest/Background.mxml
 Fixing package names in FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest/CustomComponent1.mxml
 Fixing package names in FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest/LoginButton.mxml
 Fixing package names in FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest/LoginForm.mxml
 Fixing package names in FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest/Main.mxml
 Fixing package names in FCTest.fxp.tmp/src/com/roguedevelopment/catalyst/FCTest/TextInput1.mxml
 Creating swcconfig.xml
 Invoking compc -load-config+=swcconfig.xml
Loading configuration file /Applications/Adobe Flash Builder Beta/sdks/4.0.0/frameworks/flex-config.xml
Loading configuration file /Users/mhughes/Dropbox/fxg2swc/swcconfig.xml
/Users/mhughes/Dropbox/fxp2swc/FCTest.swc (1463574 bytes)
Deleting temp dir
Done!
You now have a one .swc for every .fxp file! As easy as that!

Take the swc, and plop it into the "libs" folder of you Flash Builder project, and you can start using the components from it. They'll be in a package that starts with your default_package variable and ends with the name of the .fxp you compiled. So for my example FCTest.fxp, the package is: com.roguedevelopment.catalyst.FCTest

Here's a quick example of what it looks like to use one of those components:

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
     xmlns:s="library://ns.adobe.com/flex/spark"
     xmlns:mx="library://ns.adobe.com/flex/halo"
     minWidth="1024" minHeight="768"
     viewSourceURL="srcview/index.html"
     xmlns:FCTest="com.roguedevelopment.catalyst.FCTest.*">

<FCTest:Background verticalCenter="0" horizontalCenter="0" />

</s:Application>

You can still use the composition or inheritance methods of using those components that I blogged about the other day. Here's an example showing that with view-source enabled.

I'm on OSX using Ruby v1.8.6 with this script. Your mileage may vary on other platforms. Two things that will likely need to change for Windows users:

  • The compc call should use compc.exe
  • It uses the unzip command line utility, not sure if that's a standard tool on Windows

read more / 7 comments

Reminder: Floats are not precise

Posted by: mhughes in development Actionscript 4 years ago

I was chatting with a couple friends the other day about an odd bug that was due to floating point precision.  Neither of them got it at first, and it reminded me that not everyone realizes how imprecise floating point math can be.  I know this, you know this, even the friends I was talking to knew it after they thought about it.  But it's so easy to ignore it since floating point math usually does what we want.  Here's three amazingly simple examples:

Actionscript:

var val:Number = 0.0;
for( var i:int = 0 ; i < 10 ; i++ )
{
    val += 0.1;
}
trace(val);
Java:
public class FloatTest {
    public static void main(String[] args) {
        float val = 0;
        for( int i = 0 ; i < 10 ; i++)
        {
            val += 0.1;
        }
        System.out.println( val );
    }
}
Ruby (I had to go a bit higher on the loop since it rounds differently):
val = 0
(1..100).each do
  val += 0.1
end
print val
What would you expect the output of those to be? Probably 1, 1, and 10 right?

Wrong.

The actionscript example comes out to 0.9999999999999999, java has 1.0000001, and ruby gets 9.99999999999998

Here's another actionscript example:

trace( (0.1 + 0.1 ) == (2/10) );
trace( (0.1 + 0.1 + 0.1) == (3/10) );

That traces out: true false

Baffling huh? A float should easily have the precision to represent a tenth, right? It should easily be able to add up ten tenths to get one, right?

Here's the problem. Floats really operate in binary (duh). In binary, you can't represent a lot of simple decimal values, such as 0.10 without a repeating pattern. 0.10 decimal works out to 0.000110011 in binary, with the last 4 digits repeating forever. So the 0.1 you see when you trace out 0.1 is really just a rounded off binary value. If you do math with those values, the error can accumulate until it's big enough to be seen despite the rounding.

So you should never use floats for anything like:

  • Money
  • "Real mathematics" (for instance, I work on educational math software where the numbers have to always actually add up)
  • Anything requiring a certain precision, like sending a probe to mars or calculating medication dosage

You should use floats for things where precision doesn't really matter.  Things, that if you're off by a little nobody will ever notice.  Games, animations, audio compression, etc.   Otherwise, the easiest solution is to stick to integer based math.  You can pick a unit of measure magnitudes higher than you need.  Example: for money use cents (or thenths of cents?) instead of dollars as your UoM.  Then when you display the value you divide by 100 to show dollars.  Or you could use a richer class that handles precise numbers like Java's BigDecimal, or ruby's Rational type.

But like I said, we already knew this, right?

Tags: as3 ruby java as3 ruby java as3 ruby java as3 ruby java as3 ruby java as3 ruby java as3 ruby java as3 ruby java as3 ruby java
read more / 64 comments

Creating nested objects with JSON in Rails

Posted by: mhughes in development 4 years ago

Did you know it's possible to send a single JSON based request to a stanard rails controller and have it create an object, plus an entire tree of children records?  I knew it was possible, but I was wracking my brain trying to figure out how to do it for several days. Imagine you have a data model something like this:

class Commute < ActiveRecord::Base
  has_many :locations
end

class Location < ActiveRecord::Base belongs_to :commute end

That's a pretty simple many-to-one relationship.  Now, if you have a regular old Rails controller, how do you pass that single JSON request to create both the commute object, as well as several location children?

The first step is to add the accepts_nested_attributes_for attribute to the commute model object.

class Commute < ActiveRecord::Base
  has_many :locations
  accepts_nested_attributes_for :locations, :allow_destroy => true
end
And... that's it on the server side!

To use this functionality, your client should send a PUT request with the following JSON in the body:

{
    "commute": {
        "minutes": 0,
        "startTime": "Wed May 06 22:14:12 EDT 2009",
        "locations_attributes": [
            {
                "latitude": "40.4220061",
                "longitude": "127.4220061"
            },
            {
                "latitude": "42.4220061",
                "longitude": "41.4220061"
            }
        ]
    }
}
minutes and starttime are two properties of the commute object. And likewise latitude and longitude are two properties of the location objects. The only thing strange there is instead of passing a locations array, you have to pass a locations_attributes array. The "_attributes" is the magic piece that I couldn't figure out.  This lets Rails know you want it to figure out how to create the children, and you're not simply passing those children in.

If you don't put the _attributes suffix in, you'll get errors like the following because Rails is creating a generic hash and trying to shove that into where a Location object should go.

ActiveRecord::AssociationTypeMismatch (Location(#18269790) expected, got HashWithIndifferentAccess(#2654720)):

This general approach should work with XML or HTTP params based requests as well.

Tags: development json ruby rails development json ruby rails development json ruby rails development json ruby rails development json ruby rails development json ruby rails development json ruby rails development json ruby rails development json ruby rails
read more / 1 comment

Bubbling events in data models

Posted by: mhughes in development flex Actionscript 4 years, 1 month ago

I've been using moccasin for a bit now on a smallish project.  One of the cool things it does is give you a sort of event bubbling in your datamodel.  While moccasin is a very specific framework for a very specific sort of application, the more I think about it, the more I like the bubbling-events in the data model part as a general solution to a wider variety of problems.

Let's look at a very simplified example that illustrates the general idea (actual mocasin implementation varies quite a bit from this).  Imagine this code...

public class PizzaModel
{
  [Bindable] public var sauce:PizzaSauce;
  [Bindalbe] public var size:Number;
}

...

[Bindable] public class PizzaSauce { public var name:String; public var displayColor:uint; }

...

public class PizzaView extends UIComponent { public function setPizza( pizza:PizzaModel ) { pizza.addEventListener(ModelChangeEvent.MODEL_CHANGE, onModelChange); }

protected function onModelChange(event:ModelChangeEvent):void { switch( event.property ) { case "size": // the pizza size changed, handle that.

case "sauce/displayColor": // the sauce color changed, handle that.

default: // some property that we can't incrementally // update changed, handle that...

} } }

We have some kind of model object, let's call it the PizzaModel.

That model has a property, that itself is some kind of complex object.  Lets call that property sauce, and the type will be called PizzaSauce

Now, we have some kind of view object.  Let's call it the PizzaView.

A normal way to implement something like this would be to have the pizza view listen to events on both the pizza object and it's sauce object. Or it could manually set up some change-watchers that watched the hierarchy to the required properties.

That's all fine and dandy until we get to more complex object hierarchies. What if we had an arraycollection of toppings and we needed to know when individual properties of those toppings changed? Now, we'd have to listen to yet another type of event.

And what happens of those toppings themselves had lists of ingredients we wanted to listen to, suddenly the event chain is getting pretty gnarly to manage.

If we had some kind of magical bubbling model event, we'd only have to listen at a single location, as shown above. Especially if there was an expressive way of identifying what changed.

How might something like this be implemented?

Mocassin does it by having a meta-data model that wraps the actual data model and provides this extra functionality.

Another way would be to force the data model objects to all extend from a common superclass (I believe earlier versions of Moccasin did just that)

Someday, I'd like to explore either creating a general purpose framework for this or extracting it from Moccasin. I'm blogging about it hoping someone might do it before I get a chance ;)

read more / 0 comments