Sublime Text 2 build system for PROCESSING

I just threw together a quick PROCESSING build system for Sublime Text 2

Build PROCESSING pdes inside of Sublime Text 2

PROCESSING build system for Sublime Text 2 WINDOWS

Here is the same build system for Sublime Text 2 on OSX

Build PROCESSING using Sublime Text 2 - osx

PROCESSING build system for Sublime Text 2 OSX

{
  "cmd": ["killall -v java  ; processing-java  --sketch=$file_path --output=$file_path/build --force --build --run "],
  "shell": true
}
Advertisements

Text to speech PROCESSING example for OSX

If you’re on OSX, you have access to a text to speech command named say.  This command typically lives at /usr/bin/say and it lets you read text using a robot voice.  If you’re unfamiliar with say, open up Terminal and type the following:
“say you are the master”
Image
Assuming your speakers aren’t muted, you should here a robot voice say “you are my master”.  The OSX say command has many voices built in which you can see by running this command:
“say -v ?”
And now for the magic.  Say you want your Processing sketch to speak.  No problem, here’s the source code to a simple sample of how to do this.
// Begin Processing Sketch
void setup() {
  size(200, 200);
  text(“Turn up your sound”, 5, 100);
  text(“and then click me”, 5, 120);
}
void draw() {
  // draw() must be present for mousePressed() to work
}
void mousePressed() {
String[] cmd = { “/usr/bin/say”, “What’s shaking bacon?” };
exec(cmd);
println(“mousePressed “);
}

// end sketch

My first animated gif made with Processing

Ah yeah, I made my first animated gif with Processing today.  Here’s my gif

this gif made with PROCESSING and gifAnimation lib

this gif made with PROCESSING and gifAnimation lib

I made this with Processing and the gifAnimation library in about 10 minutes.  Here is how.

  1. Download and install Processing from processing.org
  2. Fire up Processing and create a quick sketch ( this step forces you to setup your sketchbook folder )
  3. Download the gifAnimation library for processing and unzip to a temp folder
  4. Inside your sketchbook folder ( from step 2 ), create a new folder named libraries
  5. Copy the folder named gifAnimation from the temp folder in step #3 into your  libraries folder inside your main sketchbook folder
  6. Start a new sketch and import gifAnimation.*;
  7. Then get coding

( or just download the full PROCESSING sample of using gifAnimation library   )

You will find full processing samples in the gifAnimation zip file you downloaded in step #3.  Also, this page has a great example under the section title EXPORTING A GIF ANIMATION.

I haven’t dug any deeper into the gifAnimation library because it just worked.  I hope creating animated gifs from Processing will be as smooth for you as it was for me.  If not, please let me know in the comments section.  If you have any tips or tricks with the gifAnimation lib or animating gifs via Processing, I’d like to hear those as well.

Now to make animated fire.

noLoop();

McDonalds’ Chicken Nuggets are NOT boneless

UPDATE 3.24.11- The owner of the McDonalds called and sounded very sincere on the phone, while having no idea how his restaraunt was being run. We were offered more free food, but politely declined. McDonalds’ insurance has now served Tyson Foods Inc with a bone notice.

Tyson Foods Inc bone notice

Tyson Foods make McDonalds Chicken McNuggets, and some have bones

“The chicken mcnuggets already come packaged and prepared to our insured location and they are not altered in any way; therefore Tyson Foods Inc. may be liable for said incident.”

Wow, my image of McDonalds is going right down the tubes. They don’t actually make their food, they just heat it up and put it in a bag? I’m loving it!

—————————————————————————–

I’m not a litigious guy, but I feel like something needs to be said about McDonalds and the bones in their Chicken McNuggets. Tonight is Friday, so the wife and I took the kids to McDonalds so they could play with their friends on the playground. Tonight was mostly like every other Friday, except that Fletch received two surprises in his happy meal.

  1. A Superman action figure ( nicely packed in it’s own plastic bag )
  2. A chicken bone ( hidden inside of a nugget )

The chicken bone came as quite a shocker. Fletch was eating slow as usual and got really weird, then started spitting out his nugget onto the bench. At first I thought he had a cough and was trying not to choke. After I cleaned all the mush off the bench I found this CHICKEN BONE!!!

This bone is on a nugget only for size reference

Holding the chicken bone from Fletch's Chicken McNugget

The manager of this location showed zero sympathy that my three year old just bit into a chicken bone and said we have to fill out an incident report. Notice the form is folded in half. The manager gave my wife Jenn the form folded in half and said to fill out our name and phone number. He will fill out the remainder of the form. He also said this isn’t the first time this has happened, and that McDonalds will just recall the batch of chicken that our bone filled McNugget came from.

Jenn filling out the McDonalds incident report for finding a chicken bone in a Chicken McNugget

Something else I’d like to point out is McDonalds says on their website their chicken McNuggets are boneless.

McDonalds lies about their Chicken McNuggets being boneless. My son bit into a chicken bone in a Chicken McNugget 2 hours ago, they are not boneless

While most chicken McNuggets are probably boneless, I consider this a straight up lie. How can big corporations like McDonalds get away with bold face lies and nobody does anything about it? In the past I didn’t care because I knew their food was shit, so I didn’t eat there ( even though I did grow up on McDonalds ). Now that I’m a parent, my wife and I are no match for the marketing power of McDonalds, their toys, and their Playplaces.

So, that’s all I know to do right now, make some noise. McDonalds will continue selling their mostly boneless chicken McNuggets and silencing anybody who finds the golden nuggets with bones.


If you think I’m crazy, you can ask Google about bones in Chicken McNuggets. If you’re into Facebook, there is a group you can join called Mc Donalds chicken nuggets DO contain bones!!

An aggregate may not appear in the set list of an UPDATE statement

Ever seen the error “An aggregate may not appear in the set list of an UPDATE statement” when working with SQL Server?  I ran into this one recently after trying to put a COUNT in an UPDATE statement.  I was rewriting some legacy code to use a stored procedure, and it turned out to be the perfect case for a Temporary Table.

Instead of boring you with a work scenario, let’s take a simpler one that uses the AdventureWorks database.  This example will create a list of sales people, total order count for each person, and store this list a single table variable to be used as the final data table.

Should be three simple steps right?

1. Create @Table variable

[sql]
DECLARE @SalesPeople TABLE
(
EmployeeID int NOT NULL,
SalesPersonID int NOT NULL,
FullName varchar(200) NOT NULL,
Title varchar(200) NOT NULL,
sales_count int NULL default 0
)
[/sql]

2. INSERT sales people into @Table

[sql]
— HACKISH : Match SalesPersonID to EmployeeID, and fill @SalesPeople
INSERT INTO @SalesPeople
( EmployeeID, SalesPersonID, FullName, Title )
SELECT e.EmployeeID, sp.SalesPersonID,
c.FirstName + ‘ ‘ + c.LastName as FullName,
e.Title
FROM Sales.SalesPerson sp,
HumanResources.Employee e,
Person.Contact c
WHERE sp.SalesPersonID = e.EmployeeID
AND e.ContactID = c.ContactID
[/sql]

3. UPDATE @Table with COUNT

[sql]
UPDATE @SalesPeople
SET
sales_count = COUNT( soh.SalesOrderID )
FROM @SalesPeople sp, Sales.SalesOrderHeader soh
WHERE EXISTS (
SELECT DISTINCT SalesPersonID FROM @SalesPeople WHERE SalesPersonID = soh.SalesPersonID
)
AND sp.SalesPersonID = soh.SalesPersonID
[/sql]

Not COUNT allowed in an UPDATE SET statement

The third step is where the original error comes in, so let’s update this to four steps and see how a Table Variable gets through this.

1 & 2 – Repeat from above

3. Create Table Variable of order counts

[sql]
SELECT soh.SalesPersonID, COUNT( soh.SalesOrderID ) AS sales_count
INTO #SalesOrderCounts
FROM Sales.SalesOrderHeader soh
WHERE EXISTS (
SELECT DISTINCT SalesPersonID FROM @SalesPeople WHERE SalesPersonID = soh.SalesPersonID
)
GROUP BY soh.SalesPersonID
[/sql]

4. Update @Table with order counts

[sql]
UPDATE @SalesPeople
SET sales_count = tmp.sales_count
FROM @SalesPeople sp, #SalesOrderCounts tmp
WHERE sp.SalesPersonID = tmp.SalesPersonID
[/sql]

And here’s the full script from start to finish with the table variable in use.

[sql]
— Master table of sales people
DECLARE @SalesPeople TABLE
(
EmployeeID int NOT NULL,
SalesPersonID int NOT NULL,
FullName varchar(200) NOT NULL,
Title varchar(200) NOT NULL,
sales_count int NULL default 0
)

— Match SalesPersonID to EmployeeID, and fill @SalesPeople
INSERT INTO @SalesPeople
( EmployeeID, SalesPersonID, FullName, Title )
SELECT e.EmployeeID, sp.SalesPersonID,
c.FirstName + ‘ ‘ + c.LastName as FullName,
e.Title
FROM Sales.SalesPerson sp, HumanResources.Employee e, Person.Contact c
WHERE sp.SalesPersonID = e.EmployeeID
AND e.ContactID = c.ContactID

— put sales counts into the other kind of #tableVariable
SELECT soh.SalesPersonID, COUNT( soh.SalesOrderID ) AS sales_count
INTO #SalesOrderCounts
FROM Sales.SalesOrderHeader soh
WHERE EXISTS (
SELECT DISTINCT SalesPersonID FROM @SalesPeople WHERE SalesPersonID = soh.SalesPersonID
)
GROUP BY soh.SalesPersonID

— Update our master @table with data from #tableVariable
UPDATE @SalesPeople
SET sales_count = tmp.sales_count
FROM @SalesPeople sp, #SalesOrderCounts tmp
WHERE sp.SalesPersonID = tmp.SalesPersonID

— dump the results
SELECT FullName, Title, sales_count
FROM @SalesPeople

— cleanup
drop table #SalesOrderCounts
[/sql]

Eleven Coldfusion-ish tips from the field

I’ve had this running list of Coldfusion tips on my wall for the last few years and it’s time to get these online.  All of the items in this list came from Coldfusion projects over the last few years, but a good portion of these could easily be considered tips for server programmers.  I definitely run into the same items when programming Asp.NET.

There is no rhyme or reason here, just some things I felt need to be repeated.

1. PreserveSingleQuotes()

This one came in really handy on a project requiring large text files to be imported into a MySQL database.  I used Coldfusion to upload and read the files into large INSERT chunks using MySQL’s multi – row INSERT syntax. Code built the VALUES portion of the SQL, then I just fed the data into a function for insertion.

[sql]
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
[/sql]
[coldfusion]
<cfquery name="insert_data" result="insert_result" DATASOURCE="#request.dsn#" USERNAME="#request.dbuser#" PASSWORD="#request.dbpswd#">
INSERT INTO table
( column1, column2, column3, column4, column5, column6, column7 )
VALUES
#PreserveSingleQuotes( insert_values )#
</cfquery>
[/coldfusion]

2. What if GENERATED_KEY doesn’t work?

If you’re using MySQL and the GENERATED_KEY property of your cfquery objects isn’t populating, you can use LAST_INSERT_ID instead.

[sql]SELECT LAST_INSERT_ID();[/sql]

3. Use ArrayAppend when building strings

Classic performance tuning tip for just about any programming language.  Here I’ll give a Coldfusion example and keep it dead simple.  If you ever have to concatenate strings in code, user ArrayAppend instead.  Here are two loops that do the same thing.  If you run this code, you should notice loop1 takes forever, and loop2 is smoking fast.

slow…..

[coldfusion]
<cfscript>
xx = 100000;
insertString = "";

// do the loop
while( xx > 0 ) {
insertString &= xx & " ";
xx–;
}

WriteOutput( insertString);
</cfscript>
[/coldfusion]

FAST!

[coldfusion]
<cfscript>
xx = 1000000;
insertArray = ArrayNew(1);
// do the loop
while( xx > 0 ) {
ArrayAppend( insertArray, xx & " " );
xx–;
}

WriteOutput( ArrayToList( insertArray, " " ) );
ArrayClear( insertArray );
</cfscript>
[/coldfusion]

4. If CSV, then CHR

This one is simple, if you find yourself creating CSV or any other text file, use special characters when dealing with single and double quotes, etc.

  • chr(9) = Tab
  • chr(34) = ” double quote
  • chr(39) = ‘ single quote

And if you’re not sure of the correct code for the character you’re looking to use, just wrap that character in ASC() and WriteOutput to the page.

5. Use CFMail with GMail

This is a no brainer, but with how difficult sending email can be with other languages, I’m mentioning it here.

Application.cfc

[coldfusion]
<cfscript>
APPLICATION.mail.server = "smtp.gmail.com";
APPLICATION.mail.port = "465";
APPLICATION.mail.ssl = true;
APPLICATION.mail.user = "gmailAccount";
APPLICATION.mail.pswd = "gmailPasssword";
</cfscript>
[/coldfusion]

Emailer.cfm

[coldfusion]
<cfmail to="work@ericfickes.com"
bcc=""
from="web@master.com"
subject="sending mail is easy with Coldfusion"
server="#application.mail.server#"
useSSL="#application.mail.ssl#"
port="#application.mail.port#"
username="#application.mail.user#"
password="#application.mail.pswd#">
#emailBody#
</cfmail>
[/coldfusion]

6. CFSCRIPT doesn’t know NULL?

Another tip from the land of importing and exporting data.  While working with query objects in CFScript, for some reason I could never accurately detect for NULL values.  I tried all sorts of detection schemes and ended up just writing a hacky fail safe.  Please, if you have a better suggestion for *easy* NULL detection in CFSCript, add it in the comments below.

utils.cfc

[coldfusion]
<!— Simple value getter with try / catch to get around NULL values
This function originated in a script where we always needed a " " even if
the value from the database was null.
—>
<cffunction name="qryGetString" access="public" returntype="string">

<cfargument name="data" type="string">

<cftry>
<cfscript>
return #data# & " ";
</cfscript>

<cfcatch type="Any">
<cfreturn " "/>
</cfcatch>
</cftry>

</cffunction>
[/coldfusion]

Exporter.cfm

[coldfusion]
// largeish loop
tab = chr(9);
for( xx = 1; xx <= queryObj.RecordCount; xx++ )
{
// start the row
this_row = utils_cfc.qryGetString( queryObj.FirstName[xx] ) & tab &
utils_cfc.qryGetString( queryObj.MiddleName[xx] ) & tab &
utils_cfc.qryGetString( queryObj.LastName[xx] ) & tab &
utils_cfc.qryGetString( queryObj.Suffix[xx] ) & tab &
utils_cfc.qryGetString( queryObj.MedicalTitle[xx] ) & tab &
utils_cfc.qryGetString( queryObj.email[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_phone[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_fax[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_name[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_address1[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_address2[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_city[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_state[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_zipcode[xx] ) & tab &
utils_cfc.qryGetString( queryObj.hospital_affiliation[xx] ) & tab;

// do stuff with the data
}
[/coldfusion]

7. How I find list items

Ever notice the different behavior in the Coldfusion ListFind commands?  I ended up writing my own ListHasValue function in order to find exact pattern matching in a list.  I had a list of role ids in a list, and just couldn’t get the built in functions to tell me when my id was in the list without also matching on other ids.  This one makes sense when you run some code.

The top of this sample as my custom ListHasValue() command, and the lower half does three simple loops counting from 1 to 100, and using ListFind, ListContains, and ListHasValue for number checking against the same list.

[coldfusion]
<!—
Use this to do an exact pattern check for a value in a list.
This is useful inside of a loop checking numbers against a number list.

EX :

list = "91, 92"

if you loop from 1 – 100, ListFind and related CF functions will match
on 1, 2, and 9. Not just 91 and 92

Use this when you’re looping and you ONLY want to match on 91, or 92

—>
<cffunction name="ListHasValue" access="public" returntype="boolean">

<cfargument name="list" required="yes" type="string">
<cfargument name="value" required="yes" type="any">

<cfscript>
// clean up to be safe
list = trim( toString( list ) );

// check to see if we have a *possible* match
position = ListContains( list, value ) ;

if( position > 0 )
{
// NOTE : KEEP THE TRIM AND TOSTRING
found_value = trim( toString( ListGetAt( list, position ) ) );

if( Compare( value, found_value ) == 0 )
{
return true;
}
}

// no match for you!
return false;
</cfscript>

</cffunction>

<cfscript>
list = "1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 99";
xx = 1;

WriteOutput( "List = " & list & "<hr>");

while( xx < 100 ) {
if( ListContains( list, xx ) > 0 )
{
WriteOutput( "ListContains found " & xx & "<br>" );
}
xx++;
}

WriteOutput("<hr />");

xx = 1;
while( xx < 100 ) {
// ListFind
if( ListFind( list, xx ) > 0 )
{
WriteOutput( "ListFind found " & xx & "<br>" );
}
xx++;
}

WriteOutput("<hr />");

xx = 1;
while( xx < 100 ) {
// Eric’s ListHasValue
if( ListHasValue( list, xx ) )
{
WriteOutput( "ListHasValue found " & xx & "<br>" );
}
xx++;
}
</cfscript>
[/coldfusion]

If you run this code on your Coldfusion server, you should notice the following results.  ListContains matches single digits from the loop that do not really exist in the list.  ListFind only finds the number 1?  And finally, my function does exactly what I needed it to do.  Tell me when a specific number exists in a list.

CF ListFind function comparison

Coldfusion ListFind functions don't always behave how I want them to


8. Make PDFs faster

This could easily be it’s own topic, but I’ll say one thing about making PDFs faster with CFDocument.  Only put final content between <cfdocument> and </cfdocument>.  That is, if you have any processing code, cfqueries, cfloops, inside of your cfdocument tag, your cfml page is running slower than it needs to be.  Here’s a simple example of one of my cfml pages that has only final content in the cfdocument tags.

The key to this example is moving all of my content creation code into an external file, then including at the top of my page. I always do a check for my main PDF_BODY variable, and then spit out my PDF document.

[coldfusion]
<cfinclude template="code/export_pdf_codefile.cfm">

<cfif PDF_BODY NEQ "">

<cfdocument name="provider_profile"
format="PDF"
pagetype="A4"
mimetype="application/pdf"
orientation="portrait"
margintop="0"
marginbottom="0.2"
marginleft="0.2"
marginright="0.2"
>
<cfoutput>#PDF_BODY#</cfoutput>
</cfdocument>

<!— send directly to client —>
<cfheader name="Content-Disposition" value="attachment; filename=#filename#">
<cfcontent type="application/pdf" variable="#provider_profile#">

<cfelse>
No PDF content found
</cfif>
[/coldfusion]

9. Use parameterized queries

This is a tip for all server side programmers whether you use Coldfusion, ASP, JSP, PBJ.  Use parameterized queries when doing any database interaction.  It’s too easy not to use, and you get protection from SQL Injection, as well as enforcing proper data types when speaking to your database.  This is something all server programmers should do regardless of your language, the sample below is for Coldfusion.

BAD

[coldfusion]
<cfquery name="tblInsert" datasource="myDb">
INSERT INTO myTable
( col1, col2, col3, col4 )
VALUES
( ‘#Form.field1#’, ‘#Form.field2#’, ‘#Form.field3#’ )
</cfquery>
[/coldfusion]

GOOD

[coldfusion]
<cfset val1 = Form.field1>
<cfset val2 = Form.field1>
<cfset val3 = Form.field1>

<cfquery name="tblInsert" datasource="myDb">
INSERT INTO myTable
( col1, col2, col3, col4 )
VALUES
(
<cfqueryparam cfsqltype="cf_sql_varchar" value="#val1#" />,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#val2#" />,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#val3#" />
)
</cfquery>
[/coldfusion]

10. Where’d the time go?

For the good programmers already using parameterized queries, ever insert a timestamp into your database and find out the date is correct, but the time is always 12:00:00?  Take a closer look at the cfsqltype in your cfqueryparam, I had this exact problem and here’s what happened.

Using cf_sql_date does not include the full date and timestamp, just the date.

[coldfusion]
<cfqueryparam cfsqltype="cf_sql_date" value="#paymentDate#" />
[/coldfusion]

 

Using cf_sql_timestamp includes the full date and timestamp I was looking for.

[coldfusion]
<cfqueryparam cfsqltype="cf_sql_timestamp" value="#paymentDate#" />
[/coldfusion]

11. Stored Procedures are a little different

This last one isn’t much of a tip, but more of a reminder to myself.  I do so much database work that stored procedures are just queries to me, but not so to Coldfusion and the CFQuery tag.  If you want to get data from a stored procedure, you need to use the CFStoredproc tag.  Here’s a sample of passing one argument into a stored procedure, and how to get the resulting data.

[coldfusion]
<cfstoredproc datasource="myDb" procedure="GetDataFaster">

<cfprocparam type="in" cfsqltype="cf_sql_integer" value="#inputVar#" />

<!— specify sproc result here, cfstoredproc res != returned recordset —>
<cfprocresult name = sprocResult>

</cfstoredproc>

<cfreturn #sprocResult.ColumnFromQuery#>
[/coldfusion]

Injecting javascript into asp.net via code

Microsoft has a great MSDN article on using javascript along asp.net, but they didn’t mention a technique I like to use, put it in a Literal control.  While there are many ways to add javascript to a page, I find putting the javascript in a literal much less stressful. Using a Literal control placeholder is also a good way to add messaging to a page after postback, but we’re just going to look at adding javascript.

Let’s take a simple example.  Say you’ve got a comment form that you want to auto close, or reload after the form was posted.  Below is a simple single file style asp.net page with a simple javascript function that reloads this page.

<%@ Page Language="C#" %>

<script runat="server">
/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {

    }
}

/////////////////////////////////////////////////////////////////////////////// 
/// Do stuff with the form data, then refresh page using javascript
protected void submitComments(object sender, EventArgs e)
{

    try
    {
	//
	// do stuff here
	//

	// set javascript timer to reload page afer 3 seconds
	js_target.Text = "setTimeout('reload()', 3000);";

    }
    catch (Exception exc)
    {
        Response.Write( "ERROR : " + exc.Message );
    }
}
</script>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
	<title>Comments</title>
	<script type="text/javascript">
	// page reload helper
	function reload() {
		document.location.replace( document.location );
	}
	</script>
</head>
<body>

<form id="form1" runat="server" method="post">

<script type="text/javascript"><asp:Literal runat="server" id="js_target" /></script>

	Comments
	<asp:TextBox runat="server" ID="comment_box" Width="200" />
	<br><br>

	Your name
	<asp:TextBox runat="server" ID="fullname" Width="200" />
	<br><br>
	<asp:Button runat="server" ID="submit_btn" onclick="submitComment" Text="submit" />

</form>
</body>
</html>

If you look just under the form tag you’ll see the key to this technique, an asp literal wrapped by an open and close script tag.

<script type="text/javascript"><asp:Literal runat="server" id="js_target" /></script>

When you load your page and view the source you’ll just see an empty script tag, so it shouldn’t interfere with the execution or rendering of your page.

The last part of this technique is simple, in your server code just set your Literal control’s .Text value to your javascript code. In this case when I post my comment form, after handling the input data I display a thank you message, then set some javascript to reload the page.

ltl_js.Text = "setTimeout('reload()', 3000);";

That’s all there is to it. Drop a literal in an empty script block and BAM!, you have an easy way to add javascript to your asp.net page.