"programmingblog" entries

Dos and don’ts in JavaScript

A few best practices for when you're learning the language


With every programming language, there’s a list of do’s and don’ts and JavaScript is no exception. Some of these best practices are there for your protection (like always always always using semi-colons!); some to make your code more readable and less error-prone; and some to increase the efficiency of your code. Read more…

More than enough Arel

When ActiveRecord just isn’t enough

In Just Enough Arel, we explored a bit into how the Arel library transforms our Ruby code into SQL to be executed by the database. To do so, we discovered that Arel abstracts database tables and the fields therein as objects, which in turn receive messages not normally available in ActiveRecord queries. Wrapping up the article, we also looked at arguments for using Arel over falling back to SQL.

As alluded at the end of the previous article, Arel can do much more than merely provide a handful of comparison operators. In this post, we’ll look at how we can call native database functions, construct unions and intersects, and we’ll wrap things up by explicitly building joins with Arel.

Read more…

5 reasons to learn D3

D3 doesn’t stand for data-design dictator

Designers and developers making data visualizations on the web are buzzing about d3.js. But why? Read more…

I just slipped on a banana peel named “this”

Keeping track of this in your JavaScript code

In JavaScript, the special variable this is used to refer an object. But which object this refers too depends on the code you’re executing and how this is used. So, a common problem for those learning JavaScript is keeping track of the value of this in different situations. You can be happily testing your code, and then – bam! Suddenly, things stop working, and you’re wondering what happened, not realizing that you’re assuming this is set to one value, when in fact, it’s an entirely different value. And, bugs caused by confusion about this are notoriously difficult to track down.
Read more…

Building RESTful APIs with Flask in PyCharm

Quickly make your API accessible in just a few lines of code.

Building APIs can be easier than you think. Say you’ve developed a product or service and would like to provide developer access via a RESTful API quickly, with minimal effort and overhead. The lightweight Flask Python Web framework lets you easily build extendible APIs fast, without the bloat and ceremony of similar tools. Add to this the comprehensive workflow of a modern integrated development environment like PyCharm and you’ll be up and running posthaste.

We’ll start with installing a few prerequisites and set up our working environment so that testing, debugging, and extending the API can happen without too much hopping around. Afterward, we’ll move on to connecting to our database with SQLAlchemy, a Python SQL toolkit and object-relational mapper. We’ll finish up by creating the RESTful API with Flask-Restless, a Flask extension that provides the simple generation of RESTful JSON APIs for database models defined by SQLAlchemy.

Read more…

Theming in Kivy

Adding consistency to Kivy's Python UI tools

Kivy has a wonderful set of built-in widgets that can be extended in numerous ways. They have very useful behaviors, but their look and feel may not integrate well with your App or the platforms you are targeting. Kivy doesn’t support theming out of the box right now, but if you poke around enough, there are a range of options you can use to customize the default look of widgets without having to define your own inherited versions of them.

I’ll first introduce you to Kivy’s image atlases, which are less mysterious than they sound, and are important groundwork for understanding theming in Kivy. Then you’ll learn two different ways to do manual theming in Kivy, with an eye to future automation.

Introducing Atlases

To understand theming, you must first understand atlases. An atlas is essentially a collection of distinct images combined into a single image file for loading efficiency. A JSON file describes the location of the separate images inside that master image file so that Kivy can access them directly. If you’ve ever worked with CSS sprites, you know exactly what I’m talking about. If you haven’t, the following example should explain everything.

Read more…

What is that upside-down tree doing in my browser?

Start using JavaScript to create dynamic web pages by updating the DOM.

The secret to getting your web pages to do your bidding with code is to use JavaScript to manipulate the Document Object Model, or DOM. The DOM is an upside-down tree-like structure that the browser uses to represent your web page internally, and it’s by getting and setting values in the DOM that you can modify your web page in response to users doing things like clicking a button, moving the mouse, or dragging an element around.

Getting started with the DOM is easy once you understand how the browser translates your HTML into this internal structure made of objects. Once these objects are created, then you can manipulate them using a wide variety of properties and methods, to change the content of an element, to add a style to an element, or even remove an element from the page completely.

Read more…

Why Ruby blocks exist, part III

Never forget to clean up again!

Previous posts in this series used Ruby blocks to iterate over the items in a collection. We’re going to wrap up by showing a very different side of blocks – resource allocation and cleanup.

Close the door!

Here we have a Ruby class representing a refrigerator. Prior to accessing a refrigerator object’s contents via its `contents` method, you have to call its `open` method to open the door. (Sensible enough.) Read more…

Who holds your keys?

DRM makes a mash of security and privacy.

Put your books, movies, and music on a gleaming shelf. Close the door to keep the dust off. Lock the door, so no one can take it, and hand me the key. I’ll let you have the key when you need it, if you promise not to share these with anyone else.

I might keep track of when you borrow the keys, and what you check in and out. You understand, of course, that it’s just data I need to collect and aggregate to keep my costs down, right? I wouldn’t want to have to charge you very much for my key-keeping service.

It’s the Deal of the Century!

Or at least it will be if some kinds of content publishers and distributors get their way. Terrified by the sudden collapse in the cost of duplication and distribution, locking everyone’s shelves down seems like the only way to maintain their balance (sheets). Worse, products from beyond publishing are appearing with the new key-management practices built in, including cars, coffee, and of course printer cartridges.

Read more…

9 things to consider before deploying sensors

Defining application requirements for IoT networking standards.

This article is part of a series exploring the role of networking in the Internet of Things.

Each networking technology has very different attributes and capabilities. When evaluating protocols and standards for your IoT project, you’ll need to understand all of the technical and financial requirements underlying your application in order to effectively choose a technology to implement. Let’s take a look at the typical networking requirements in designing solutions for the Internet of Things.

To provide a concrete illustration of the requirements analysis, I will describe a hypothetical building energy management application and outline a comprehensive list of its wireless network requirements. This list of requirements will form a framework for future discussion of the networking technology standards currently on the market.

Read more…