Sunday, September 21, 2014

Pygame on Android - What to Do When Your Android App Dies Soon After Startup

You can use adb to get console error messages and print statement output from your pygame app on Android phones.  One of the most frustrating thing for beginners to pygame on Android is when the app runs fine on their desktop, but dies soon after startup on their Android phone.  Usually, you click on the app, the splash screen comes up, then the Android app silently dies.  Unless you're using adb, corrently you could be stuck.

With the usb cable connected between your phone and desktop, run 

adb logcat |grep python

Here's the output I got today.

I/python  (  580): 
Opening APK '/data/app/org.pychildren.surfsc-2.apk'I/python  (  580): 
Traceback (most recent call last):I/python  (  580):   
File "main.py", line 323, in <module>I/python  (  580):     
main()I/python  (  580):   
File "main.py", line 296, in mainI/python  (  580):     
pprint.pprint(weather.w_dict)I/python  (  580): 
NameError: global name 'pprint' is not definedI/python  (  580): 
Python for android ended.I/ActivityManager( 7163): Process org.pychildren.surfsc:python (pid 580) has died.

It is clear that there's a problem with the pprint.pprint statement that I was using to display the Python dictionary of weather data.  It even gives me the line number in my source code.  I simply commented out the line and the app started working again on my Android phone.



I've added basic weather information to my version.  My son's version has a cleaner interface and better colors.


Tuesday, September 16, 2014

Sphinx Documentation System

I've started to encourage my son to focus on code that is easier to read by humans.  There are three parts to this:

  1. choose variable, class, and function names that are easy to understand;
  2. break the code into functions and classes instead of one long block and use local variables;
  3. comment the code
For the last part, I started off using pydoc -w filename to extract the docstrings into a document.  This didn't give me as nice a document as I wanted.  I ended up using Sphinx with the autodoc extension

$ sphinx-quickstart

I took all the defaults except for autodoc.
In conf.py, I edited the sys.path

# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
sys.path.insert(0, os.path.abspath('.'))
I just uncommented the line above.

I then edited index.rst
.. automodule:: main   :members:
I then edited the docstrings in my python main.py file.




Monday, September 15, 2014

Overview of Teaching Python to Graph Output from Cloud API - Spitcast Surf


I've been working with my son to build a mobile app that displays graphs of tide charts and tables of surf forecasts for the Santa Cruz region.  The app runs on his Android Motorola Moto-G phone and my Samsung Galaxy Note 2.  He grabs the data using the Spitcast API and displays the output using Pygame.


The tide has a huge influence on the quality of surf in Eastside Santa Cruz.  The optimal tide conditions at my favorite spot is a tide that is rising from 2 feet to 3.5 feet.  The swell height and direction also plays a big role.  In the future, we'll use the API from OpenWeatherMap to pull the sunrise, sunset, and wind.

The steps are:
  1. Pull data from Spitcast using urllib2
  2. Read JSON data from API into program and then convert to a list of Python dictionaries using the json library


The lesson gets off to an exciting start. The student will realize the potential of accessing cloud-based APIs. Assuming the student has a basic understanding of Python data structures lists and dictionaries, they'll also understand how easy it is to parse the data. The simple idea to convert the tide values of feet into pixels is just to multiply feet by a constant number. In this case, I multiply the height of the wave in feet by 50 to get a pixel value. For example, a wave height of 5 feet will correspond to a screen height of 250 pixels. To generate the y coordinate for the Pygame screen, I subtract the pixel height from 550. Remember that a y value of 0 is the top of the screen. I set my screen height on the phone to 500 pixels. A wave height of 1 foot will equal screen height of 50 pixels. Subtracting this from 550 will yield a y position of 500.
  1. Build start and end points for a series of 23 vertical lines spaced evenly apart to create the x axis grid. 
  2.  Create a point list that will be used by pygame.draw.lines in a future section.



At this point, the student will have a list of points that they can then draw with pygame.draw.lines for the tide graph or pygame.draw.line x, y axis grids.
Here's the basic algorithms with a bit more bells and whistles.

In the main while loop, I have this code.
This is the android-presplash.jpg screen of the application.


In the future, we'll merge the features from my son's Weather App project into Santa Cruz Surf.





Below is a shot of the app developer benefiting from the application.




Installing POSIX Man Pages on Ubuntu

I was fairly surprised to find that $ man jobs didn't work on Ubuntu 14.04.  When I looked at the installed man pages, I realized that the sections 1, 6, and 8 of the UNIX manual pages were missing.  These correspond to the user commands, games, and system admin commands.  I was able to install the man pages with:

 $ sudo apt-get install manpages-posix

This made me wonder if the current group of Linux users were using basic UNIX commands like fg, bg, jobs.  I then realized that the full set of document was available with info.  Many years ago, the man pages were in better shape then the info pages.  I now see that there's more documentation in info.  Despite being a heavy user of Emacs, I've never really warmed to the navigation commands in info and prefer the interface for less and man.  I'll give info another try over the next few months.


Saturday, September 13, 2014

Fixing Emacs ctrl-space (set mark) on Ubuntu 14.04 Trusty Tahr

The Emacs ctrl-space stopped working for me.  I'm not sure if it was related to my upgrade to Ubuntu 14.04 or if it was related to my switching from Unity to LXDE.   I fixed the problem by running ibus-setup from the command line, then changing the default keymap for input method.  The main problem is that the Intelligent Input Bus or ibus, takes over the keymap for ctrl-space.


I've found LXDE to be faster than Unity on my Lenovo laptop with 4 GB of RAM and a Celeron processor.

I've had some problems with Xfce in the past, primarily related to the HDMI port coming out of sleep mode.  

Saturday, June 21, 2014

Code Rush Documentary - Netscape / Mozilla / Silicon Valley Ups and Downs


As my son worked on file I/O in Python, I decided to watch Code Rush, a documentary that covers the amazing story of Netscape and its path to open source their core product Navigator as Mozilla.

The Code Rush story is filled with millionaires, epic stories of how people changed the world, intense human bonding, and an amazing legacy.  There was also a high human cost, with strained marriages and emotional suffering. 

The film resonated with me.  I feel that it accurately portrayed the hectic culture of the time, the belief in the Internet to radically change society, and the hope that open source with its implied philosophy of freedom and sharing was about to improve our lives. 
  
The documentary covers the time period from March 1998 to April 1999, a formative time in my life.  While the team at Netscape was changing the future of the Internet, I was on my own adventure.  During that time, I sold my Internet company, an online community in Tokyo, to PSINet, moved to Silicon Valley to kickstart the US office for Turbolinux, helped the CEO raise a ton of money, and most importantly and magically, had a child.

I started this blog to chronicle my childrens' progress to learn to program and to understand the culture of their home, Silicon Valley.  Part of what I hope to teach them is to find their own balance in a town filled with extremes.  If they are lucky, they will each have at least 10 jobs in their lives, ranging from a baker, a designer, a landscaper, therapist, a commercial fisherman, or even a programmer.  Technology could be an enabler in their future.  Or, they could never use it.

As you watch Code Rush, I encourage you to think about your own strategy to define your goals and find balance.  It's a wonderful window into the past that could help you plan your future.


Saturday, May 31, 2014

Still Keeping to Pygame Despite Great New Kivy O'Reilly Book (for now)


Since I wrote about Kivy six months ago, my son and daughter have continued to make strong progress with Pygame.  For IDEs, they've both moved to PyCharm with its awesome code completion and easy integration with GitHub.  My son is making progress with object oriented programming and recursion.

Kivy is much more powerful than Pygame.  It is also harder to use.  Although the documentation is comprehensive, only the tutorials on Pong and Paint are geared toward beginners.  It also not designed to teach programming concepts to new programmers.  Kivy is geared more toward intermediate to advance programmers that want to get a mobile app up quickly. The new O'Reilly book, Creating Apps in Kivy,  by Dusty Phillips is great.  However, most people new to programming will get stalled around chapter 4.  Although the text is easy to follow and the code examples are clear, the concepts are dense.  Just as an example, in chapter 2 of this thin book, Dusty rips into list comprehensions.  While the example is easy to understand, a new programmer is better off dealing with for loops and list iteration.

There's many other places where Dusty teaches us a valuable concept, but I think it is too much, too soon for young developers.  This said, I've been gearing up for my son''s summer by going through the Kivy book and I plan to introduce him to Kivy later in June.

Kivy makes use of a Python file, which works great in PyCharm, and a kv language file, which does not work great by default.  To get the kv language syntax highlighting and tabs working properly in PyCharm, you need to install the kv settings file.  This makes the development process much easier.  I could never get the kv language settings working properly in Emacs.    After 20 years of using Emacs, I find myself grabbing PyCharm more and more.  It's amazing to me that even at my age, I find myself moving to new tools.  I've even moved off of the Emacs key mappings and am using the default PyCharm keymaps.  JetBrains has done a nice job.

I've also gone through the Packt Publishing book, Kivy: Interactive Applications in Python.  The O'Reilly book is much better, clearer, easier to read, with less errors in grammar.  It's also newer.  The O'Reilly book does focus on Python 3, which I don't think is a good thing since most Kivy development is still done with Python 2.7.  Though, I haven't run into any problems following the Python 3 examples.