The Professional Steve

Tutorials, developer resources and inspiration.

Arithmetic In Python

Ok, I’ve avoided this long enough. It’s time to get back to my Python tutorial series. Next up: simple arithmetic.

Let’s add two numbers:

print 1 + 1

Run that sucker and you’re bound to get the answer: 2.

Similarly, I bet you can guess what these lines of code will get you:

print 5 - 2
print 5 * 8
print 10 / 2

That’s right, 3, 40, and 5.

Here’s a bit of a gotcha:

print 10 / 3

That gives you 3! What the heck?

If you want a decimal answer you’re going to have to tell Python that by including at least one decimal:

print 10.0 / 3

The above will get you 3.33333333333.

The order of operations in Python is pretty much what you would expect in any math class, but I tend to make liberal use of parentheses just to make everything clear:

print 3 * (4 + 8) - (22 * 7)

The above gets you -118.

Of course, if you want to do more than just simple stuff like the above there’s always the math module:

import math

print math.sqrt(math.pow(5 - 1, 2) + math.pow(1 - -2, 2))

If you’re interested, got the above equation from a distance formula example at Purplemath.

Using RequireJS

I may have already made mention of this neat JavaScript library called RequireJS, it allows you to modularize your javascript code.

Let’s say I had a simple page that uses JavaScript to make a button behavior:

HTML:

<!DOCTYPE html>
<html>
    <head>
        <script src="jquery-1.11.2.js" defer></script>
        <script src="norequire.js" defer></script>
    </head>
    <body>
        <h1>No requirejs</h1>
        <p class="some_paragraph">This is a paragraph</p>
        <button class="some_button" type="button">click it</button>
    </body>
</html>

JavaScript:

$(".some_button").click(function() {
    $(".some_paragraph").html("You changed the text!"); });

This example is pretty simple, but it already has two script tags at the top. In a large project, keeping track of a large number of script tags, as well as the order they need to be placed (since the script referred to by one tag can often depend on the script in one of the other tags) can quickly become too much to manage.

All the code from the above and below examples are at
https://github.com/NerdcoreSteve/requrejs_examples
by the way :)

Here’s something a little better:

<!DOCTYPE html>
<html>
    <head>
        <script data-main="scripts/main" src="scripts/libraries/require.js"></script>
    </head>
    <body>
        <h1>No requirejs</h1>
        <p class="some_paragraph">This is a paragraph</p>
        <button class="some_button" type="button">click it</button>
    </body>
</html>

Now all we need is one script tag that tells us where require.js is and where our main.js is.

Have a look at the directory structure in the “require” folder of that github project. main.js is in our “scripts” folder:

require(["scripts/libraries/jquery-1.11.2.js"], function() {
    $(".some_button").click(function() { $(".some_paragraph").html("You changed the text!"); });
});

The previous code is now put into a function that itself is a parameter in a call of the require function. The first parameter of the require function is a string array detailing a list of dependencies for the code. In our case the only dependency is JQuery, which lives in a “libraries” folder in the project.

The upshot is that the code won’t run until the library or libraries it needs are loaded.

In the lazyload directory of this github project we see how we handle a library’s dependency on other libraries. In this case, the lazy-load JQuery plugin.

To handle this kind of dependency you need to call RequireJS’s config function:

requirejs.config({
    shim: {
        'jquery': [],
        'jquery.lazyload': ['jquery']
    },

    paths: {
        'jquery': 'libraries/jquery-1.11.2.min',
        'jquery.lazyload': 'libraries/jquery.lazyload.min'
    }
});

require(['jquery.lazyload'], function() {
    $(function() {
        $('img.lazy').lazyload();
    });
});

The object I’m passing to requirejs.config has path information for my libraries in the paths sub-object, and dependency information in the shim sub-object. That’s all there is too it!

MongoDB And Java

MongoDB is pretty great. It’s easy to use and has none of the complications of SQL. It has a nice and understandable Javascripty syntax. I prefer to use Node with Mongo, but if you have to, here’s how you include it in your java project (using maven):

If you’re using Maven just stick this in your dependency list:

<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>2.12.5</version>
</dependency>

And now you’re ready to interact with the MongoDB driver. Here’s the MongoDB project’s getting started page and here’s another tutorial I found helpful.

Cloud Log 3

Soooooo……

Well….

I just want to say that JAX-RS sucks.

Now that I’m using Node.js with Express, things are moving sooooo much faster.

I did finish the Twitter-like app using Tomcat and JAX-RS, but after a lot of headaches trying to adapt that application to my Dropbox-like app I finally made the switch to something more sensible.

Just this morning I figured out how to grab POST request data with the Express body-parser module.

Now I just need to finish up writing the api calls. They’re stupid simple to write:

app.post('/users/register', function (request, response) {
    response.send('Got a POST /users/register for username ' + request.body.username
             + ' with password ' + request.body.password);
})

Not exactly the most secure code, I know, but this is a school assignment after all.

Here’s the github for this final project:
https://github.com/NerdcoreSteve/CloudDrive

Starting A Software Business

I have a few irons in the fire (the most important one being my new job). Another one of involves starting an online business.

Well, I guess that last iron isn’t in the fire until I finish my Cloud Computing assignments, but it’s near the fire. It’s leaning against whatever it is that the fire is in. It might even be getting slightly warm due to it’s proximity to said fire. I’ll be putting it in that fire on March 15th, when my Cloud assignments are due.

What with my new job, my commute, things around the house I need to take care of, my classwork, and all that stuff I keep in and around a metaphorical fire, I don’t have a lot of free time. I do, however, have time to listen to podcasts while I drive and do chores.

One of those is The Smart Passive Income Podcast hosted by Pat Flynn. It’s one of the resources I’ve found in my search for information about creating online business that have little to no sleaze factor.

So far Flynn is sleaze free.

Most of his content revolves around creating sites that provide valuable information for free, and then, for those who want to dig deeper, offering more in-depth products and services for a fair price.

That approach is exactly what I’m doing with my Python tutorials. Those tutorial posts will always be free, but I will use them as a kind of first draft for an ebook and online course once I’ve finished them. It’s also my hope that the blog will be useful to people in its own right, and that a smaller set of those people will find my future ebooks and courses valuable enough to pay for them.

But there’s a different and more general approach to software businesses (and perhaps all businesses) that two of Flynn’s podcasts describe. The core of the idea is something I first came across in Seth Godin’s blog. I wish I could find the quote, but I’ll paraphrase it here:

Look for the pain points and provide solutions to those problems.

In episodes 46 and 149 of The Smart Passive Income Podcast, Flynn and his guest Dane Maxwell discuss specific methods for just how you find the pain in any industry.

They go into detail, but the basic idea is that you simply ask people what their problems are. Now, you’ve probably guessed that it’s not as simple as that, and it’s not. But the process of actually finding out what the true difficulties a person is grappling with is not rocket science. You have to ask probing questions, get inside the other person’s head, and try to think about things from their point of view.

That’s a useful skill to learn for all sorts of reasons, but in this context, once you’ve found some pain points you can use software to provide a solution, often in the form of a web application that customers pay a monthly fee for. If the application truly solves their solution and is reasonably priced, customers will save money by using your product and will be happy to pay for it.

I strongly recommend listening to these episodes. I know I’ll be listening to them a few times more at least.

Yes, I’m still alive

But I got a job! I am now, officially, a software engineer. I’m still getting used to being an exempt employee, and to my commute. Also, I’ve still got a cloud computing class to complete before the 15th. So I don’t think I’ll be doing updates every weekday. But I will try to keep do a post a week.

Installing and running MongoDB The Mac

MongoDB is a modern NoSQL database. It’s syntax is very like JavaScript and it is a key-value. That means if you want to store something in the database all you need to do is give whatever it is you want to store a name. Then when you want to retrieve that thing you use the name you gave it. That’s basically it. I’m really looking forward to using this in my current Cloud Computing assignment. The MongoDB site, for your convenience, has instructions on how to install it and how to get started using it.

Cloud Log 2: Getting the database up and running

So, the api (users, friends, followers, tweets, retweets, etc) now works in memory but now I need to get this thing connected to a database. I’m going to try to use MongoDB because SQL seem like overkill. I don’t need the headaches of relational databases, writing queries, etc. I just want to store user info and tweets. Simple.

Installing Java With Homebrew

I may have already mentioned it, but I thought I’d make a separate post about it for easy access.

Here’s how you install Java with Homebrew.

Homebrew: The Best Package Manager For Your Mac

I may be just a little biased. It’s not like I’ve done some big survey but, hey, this is my blog, I’ll recommend what I think is the best.

Homebrew is a package manager much like apt-get or yum in Linux. If you’re not familiar with them, all you need to know is that Homebrew is a program that, if you’re going to do software development on your Mac, will save you a lot of headaches. I’ve already done several posts about how to install this or that program/framework/language using Homebrew. Really, it’s just the best way.

Go install Homebrew.

Follow

Get every new post delivered to your Inbox.

Join 206 other followers