How I Find Flow

At the suggestion of a friend, I watched a TED talk called Mihaly Csikszentmihalyi: Flow, the secret to happiness | TED Talk …. I have read his book, Flow, and so the ideas presented in the talk weren’t new to me. What I was reminded of was the necessity of a high degree of challenge combined with a high degree of skill. As I pondered that it occurred to me that in so many of the cases of flow that I have read about or experienced myself, one of the key factors was a level of familiarity with ones tools that was so intuitive that you don’t consciously think about them.

Take writing for instance. I have developed my typing skills to the point where I don’t think about spelling words anymore. I just think the sentences that I intend to write and they appear on the page. It feels almost like magic.

Similarly, when I write a program, I build it a piece at a time. I prefer interpreted languages, like Lisp or Python, because I can experiment with them as I think about the problem. The IPython environment is especially good for that kind of development as it records every expression you enter and every response that it returns and labels it with an index number (see the screenshot below).

You can also step through the history by using the up-arrow and down-arrow keys. You can edit the expression and then execute it again. Typos are not nearly as frustrating when you don’t have to type in the entire expression again. Notice I refer to the previous output with the _ character. The line that starts with the prompt: In [2]:  computes the square of 42 ( _**2) .

There are many other useful features of IPython. I am still exploring them. The nice thing about them is that they are optional. You can learn about them a little bit at a time and they don’t get in your way if you don’t know about them. This helps you stay in flow by not distracting you.

There are other nifty tools to help you write Python code. I have been using one called Spyder that is a multi-pane Integrated Development Environment similar to Xcode, Eclipse, or Visual Studio, depending on your preference as a programmer. Each of these other IDEs have add on software that helps you develop Python.

I’ve wandered a bit away from the topic but the point that I am making is that before you can experience flow while doing a task, you have to be comfortable doing it. Pick a tool, learn it until it disappears. When you are creating, the tool should become invisible to you. Your focus should be on the code that you’re writing, not the tool that you are writing it with. Furthermore, you should be thinking about solving the problem, not how you get the language to do what you want. That takes a good bit of practice to achieve as well.

To round out my examples by noting another activity where I experience flow. When I’m playing guitar, I experience flow. Especially when I am trying to play a piece that I’ve never tried before. Or when I try to learn a new song on the mandolin. It challenges me and demands skills that I often have to learn to get into the zone. It’s worth it though.

Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

Fiddling with Blog Formatting

WARNING: This post is primarily an experiment in how to format code in-line in a blog post. If you don’t understand this post, it’s probably because I’m doing a lousy job of explaining myself. I am concentrating on form instead of content. Tune in tomorrow and I’ll probably be back to making some kind of sense. And now, back to your regularly scheduled blog post, already in progress.

I found that there is a style pulldown in WordPress that lets me put preformatted source code in a blog post. Surprisingly enough, it is called ‘Preformatted’.  There is an example of how it looks in the box below.

def hello():
    print('Hello, world!')

if __name__ == '__main__':

There are better looking ways to embed source code in a blog post but this is the simplest one. Some of the other ways provide syntax highlighting to make the keywords of the programming language stand out. Most of them provide line numbers to make it easier to talk about the various lines in the program. I will investigate those tools and report back on them as I learn how to use them.

Since I have a piece of Python code to talk about, I will attempt to explain what the program in the box above actually does. When loaded interactively into a Python interpreter, it defines a function called hello() that prints the string Hello,world! on the screen.

If the file containing this code is run from the command line, the variable name is given the value ‘main‘. The code after the if statement at the bottom of the box checks to see if name is equal to ‘main‘ and if so, the function hello() is called in the body of the if statement.

If the code is imported as a library, on the other hand, name will not be equal to ‘main‘, so the body of the if statement will not be executed. The code that imports the file can then call the hello() function at its own discretion.

If you know Python, this post has probably been boring. If you don’t know Python, it has probably been confusing. This post has been more of an exercise in how to format code in a blog post and less of an actual tutorial on Python.

If you are interested in a tutorial on Python, look here. I will probably write more about Python in the future. I will make the code prettier next time. I will make the prose more illuminating. But there is just so much time available to blog each night and I didn’t have time to experiment with formatting and write riveting content in the same post.

Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

Fibonacci Fun with Python

I’ve been playing with numbers with Python. I wrote a function that returns the nth Fibonacci number. Fibonacci numbers are generated by taking zero and one as the first two numbers, then adding the previous two Fibonacci numbers together to yield the next one. The next Fibonacci number after 0 and 1 is also 1 since 0+1=1. The next one is 2. The next one is 3. The next one is 5. And so on. Strictly speaking, the first Fibonacci number is 1 since there is another trait that Fibonacci numbers exhibit and 0 can play no part in that. Given two successive Fibonacci numbers, the ratio of the greater to the lesser of the two will tend toward the golden mean or phi.

The golden mean is approximately 1.61803398875 although, like pi, it is an irrational quantity that has no exact expression. It can be written as the expression (1 + square_root(5))/2. It is the square_root(5) term that makes the number irrational. But, if you will notice, 1/1 is 1, 2/1 is 2, 3/2 is 1.5, 5/3 is approximately 1.667 and as you continue to compute the ratio with larger and larger Fibonacci numbers, the result gets closer and closer to phi.

Phi is an interesting quantity. It shows up in many places in nature. Some of the claims about it, for instance that it is evident in the logarithmic curve formed by a Nautilus shell, have been disproven by empirical study. Others, like the fact that the ratio of the length of a diagonal of a regular pentagon to the length of one of its sides is phi, are on sound footing. In any case, computing Fibonacci numbers and phi can be an interesting pastime.

I found a simple function in Python that yields the nth Fibonacci number in relatively short order without having the unfortunate side effect of consuming the entire stack.

This function takes an integer argument n. It initially sets a to 0 and b to 1. Python allows you to set multiple values with one assignment statement as shown in line 2 above. Then, for  a variable x assigned to every value between 0 and n-1, which is to say n times, you set a to b and b to (a+b). When you are done, you return a because it will be the nth Fibonacci number.

I learned several things while writing this blog post. First, when you are computing Fibonacci numbers, the values get large very fast. For example, the 100th Fibonacci number is 21 digits long. The 1000th Fibonacci number is 209 digits long. Another thing I learned is that the square root symbol does not render as such in a standard web page. I am somewhat baffled by that as it renders fine in the WordPress editor. Perhaps I will discover a way around this.

I also learned how to use GitHub to create a gist, that short block of code above. A gist allows you to display the program formatted as it would be in a code editor instead of formatted as prose as it would be if typed directly into the blog post.

I learned other things tonight but I don’t intend to belabor this post with a lengthy explanation of everything I learned. Perhaps another night.

Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.

Python For The Win

Today I remembered what I love about programming. It’s that feeling that you get when you know what you want to accomplish. You have an idea of how to start realizing your vision. You are comfortable with your tools, that is the language and the editor that you are using to write your opus. With these elements in place you are set to experience flow. Flow is a state of mind, studied and characterized by Mihály Csíkszentmihályi, where the task at hand is difficult enough to be engaging but not so difficult that you get stuck trying to accomplish it. As you finish one task, the next one becomes apparent to you. It is an exhilarating feeling.

What happened today is that I found a Python package called openpyxl that allows you to read and write Excel files from Python. This was the solution that I was looking for to my problem with analyzing large data sets stored in an Excel spreadsheet. I had been struggling to do it with VBA (Visual Basic for Applications) but I found it to be difficult to use. I found plenty written on it but nothing that spoke to my problem domain. If anything, there were too many mediocre books targeted at spreadsheet power users that were novice programmers. I am the opposite, a casual spreadsheet user that is an expert programmer.

I figured out after I thought about it for a while that experienced programmers just weren’t going to put up with a language as shabby as VBA. Not only was it a bad language but it had no standard definition and was proprietary to Microsoft. Consequently, it was subject to Microsoft’s favorite ploy of making radical changes to the behavior of the language from one version of the product to the next. It made no sense to me to invest any significant amount of effort into crafting a solution only to have to entirely redo it at the next release of Excel.

To summarize, I made more progress today than I would have in a week with VBA. And as a bonus benefit, my code is readable to the large and ever growing cadre of Python programmers. It was a brilliant idea and I’m still floating on air over having figured it out.

Basic was a good first cut at a first language but we have learned a lot about how computer languages should work since it was first released in 1964. Bill Gates and Paul Allen started Microsoft with Microsoft Basic in 1975 and it was a clever implementation that made the limited resources of the early personal computers more approachable by total computer novices. In the intervening 42 years, we have developed a number of languages that fill the niche that Basic originally targeted much better.

Python has become very popular for both casual scripting and implementation of large projects. It has the inertia associated with a large user community and a responsive team of implementors. There is a clear definition of the language and a carefully controlled process governing the evolution of the language. I can develop in it with confidence that my work won’t be rendered obsolete by the next release of the language.

Sweet dreams, don’t forget to tell the ones you love that you love them, and most important of all, be kind.