Saturday, November 27, 2010
Tuesday, October 19, 2010
Getting started with Python
Links
Official web site: http://python.org/
This tutorial is really cool: http://docs.python.org/tutorial
Reference: http://python.org/doc/
A dynamic language. Interpreted not compiled. Sort of C-like syntax, close enough that certainly I felt comfortable in it. Also used as a scripting language. Easy to learn.
Like C .... except the whitespace is significant in front of expressions: so no braces { } Don't mix tabs and spaces before statements because you'll get it wrong. Intent consistently and you'll be fine. It appears most people love the idea (everything is correctly formatted!). Then hate it when they start. Then love it once they've been doing it for a while.
Here is some Python 2.x code (they are currently working on Python 2.x and 3.x in parallel).
Summary: Define a function. Print hello. Call the function. Notice the tab/spaces before print.
Other features:
Why?
We are using Python 2.x because that's what one of the interpreters that comes by default with Mac OS X, although for the work here you can use any operating system.
We are using to run a couple of things for our current weather program, which talks to hardware weather stations.
http://lightsoft.co.uk/LWC/
Specially, I'm writing a file converter - from other formats to LWC format.
(We've also got web page scrapers, called 'Decoders' written in Python. They need to be easy to change, hence Python.)
The Python Interpreter
On Mac OS X you'll have Python installed. On Windows you'll need to install it: http://python.org/download/
Some Linux and BSD distribution have it installed - otherwise that link will do it as well.
MacOSX Version = Python Version
10.3 = 2.3.0
10.4 = 2.3.5
10.5 = 2.5.1
10.6 = 2.6.1 (typing python2.5 gets you python2.5.4)
For MacOSX, if you go to Terminal (that's in Application/Utilities if it's not in your doc) and type 'python --version' then it will show you the Python version installed. You can do a similar thing in Windows from the Command Prompt.
I've installed Python 2.6.6 for reasons I can't remember. It's unlikely that this is critically important and probably relates to some unrelated work I was doing. Yeah, I think it was PyGame stuff.
The Editor and Execution environment - ECLIPSE
You can edit python files (.py) in anything - they are straight text files. For instance on Windows Visual Studio works (but it doesn't know how to chroma code it by default...) and on MacOSX Xcode knows how to chroma code Python files.
You can run the code in the Terminal by typing 'python file.py' or 'file.py' (depending on various things).
However, both of these, whilst functional, suck slightly in various ways.
So I've started editing with Eclipse. http://www.eclipse.org/
This started life as a Java development environment, but I've used it elsewhere for embedded C/C++ and I use it also for Python development.
Get it here: http://www.eclipse.org/downloads/
It doesn't really matter what version you download - I have the 'Eclipse IDE for C/C++ Developers' installed (for various reasons), so I'd suggest that one.
Once you boot up Eclipse, you'll need to create a workspace. Create this somewhere like your Document folder. A workspace is a place to work (duh!) that contains, usually, multiple related projects and the settings for the whole environment (for that particular workspace). The actual project may or may not be inside the workspace folder.
PyDev
There are a few ways of getting Python to work with Eclipse. I'm using http://pydev.org/
The basic instruction are, from within Eclipse... (see http://pydev.org/manual_101_install.html for screenshots of this process)
1. Go to the Help Menu
2. Go to 'Install New Software...'
3. Click Add next to 'Work with' .... add 'PyDev' and 'http://pydev.org/updates'
4. Follow the Instructions on that link above... (yeah, tired of typing :-D)
Create a Python Project
File -> New -> Pydev Project
Project name: test_project
you might need to configure a python interpreter...
Auto config should work...
If not ... New... Python2.5 then (on MacOSX) browse to /System/Library/Frameworks/Python.framework/Versions/2.x/Resources/Python.app/Contents/MacOS/Python and on Windows it will be in your Program Files folder.
Create '.py' file inside the project directory.
Type this into it....
Right click on the file Run As... Python Run.
It should show 'Hello' in the Console window down the bottom.
You should also be able to put Debug As... Python Run.
Might help if you set a break-point.
(If you go into debugger view, you can click the 'PyDev' button on the top-right to switch back to editor mode.)
Summary
Phew!
Hopefully at this point you'll have a working Python, Eclipse IDE and PyDev. You can create Python projects, run them and debug them.
Official web site: http://python.org/
This tutorial is really cool: http://docs.python.org/tutorial
Reference: http://python.org/doc/
A dynamic language. Interpreted not compiled. Sort of C-like syntax, close enough that certainly I felt comfortable in it. Also used as a scripting language. Easy to learn.
Like C .... except the whitespace is significant in front of expressions: so no braces { } Don't mix tabs and spaces before statements because you'll get it wrong. Intent consistently and you'll be fine. It appears most people love the idea (everything is correctly formatted!). Then hate it when they start. Then love it once they've been doing it for a while.
Here is some Python 2.x code (they are currently working on Python 2.x and 3.x in parallel).
def hello():
print "hello"
hello()
Summary: Define a function. Print hello. Call the function. Notice the tab/spaces before print.
Other features:
- Lists are sort of like C arrays, but more powerful. They use array-style syntax. These are the general purpose container of Python and get used all over the place to store stuff. Pretty efficient (e.g. faster to add characters to a list than a string). sid[3] = 5
- There is a big library that does pretty much everything. Send emails, get web pages, regex, web servers, etc. etc.
- Types are inferred rather than specified explicitly. 'Duck' typing. Strong typing.
- It's an OO language. Naturally. But also supports imperative (procedural) and to a lesser extent functional programming.
- Automatic memory management
- There are dictionary which do map-style key-value look up. Used occasionally.
- It's in the same class of languages (dynamic, interpreted, scripting) as Perl, Ruby, Tcl, PHP, etc.
- In my opinion: Fast to program. As fast (or faster) than BASIC, and probably > 4x faster to program than C or C++ for the general case.
- Interpreted = sometimes slow to run. But we have fast computers, so that doesn't matter.
Why?
We are using Python 2.x because that's what one of the interpreters that comes by default with Mac OS X, although for the work here you can use any operating system.
We are using to run a couple of things for our current weather program, which talks to hardware weather stations.
http://lightsoft.co.uk/LWC/
Specially, I'm writing a file converter - from other formats to LWC format.
(We've also got web page scrapers, called 'Decoders' written in Python. They need to be easy to change, hence Python.)
The Python Interpreter
On Mac OS X you'll have Python installed. On Windows you'll need to install it: http://python.org/download/
Some Linux and BSD distribution have it installed - otherwise that link will do it as well.
MacOSX Version = Python Version
10.3 = 2.3.0
10.4 = 2.3.5
10.5 = 2.5.1
10.6 = 2.6.1 (typing python2.5 gets you python2.5.4)
For MacOSX, if you go to Terminal (that's in Application/Utilities if it's not in your doc) and type 'python --version' then it will show you the Python version installed. You can do a similar thing in Windows from the Command Prompt.
I've installed Python 2.6.6 for reasons I can't remember. It's unlikely that this is critically important and probably relates to some unrelated work I was doing. Yeah, I think it was PyGame stuff.
The Editor and Execution environment - ECLIPSE
You can edit python files (.py) in anything - they are straight text files. For instance on Windows Visual Studio works (but it doesn't know how to chroma code it by default...) and on MacOSX Xcode knows how to chroma code Python files.
You can run the code in the Terminal by typing 'python file.py' or 'file.py' (depending on various things).
However, both of these, whilst functional, suck slightly in various ways.
So I've started editing with Eclipse. http://www.eclipse.org/
This started life as a Java development environment, but I've used it elsewhere for embedded C/C++ and I use it also for Python development.
Get it here: http://www.eclipse.org/downloads/
It doesn't really matter what version you download - I have the 'Eclipse IDE for C/C++ Developers' installed (for various reasons), so I'd suggest that one.
Once you boot up Eclipse, you'll need to create a workspace. Create this somewhere like your Document folder. A workspace is a place to work (duh!) that contains, usually, multiple related projects and the settings for the whole environment (for that particular workspace). The actual project may or may not be inside the workspace folder.
PyDev
There are a few ways of getting Python to work with Eclipse. I'm using http://pydev.org/
The basic instruction are, from within Eclipse... (see http://pydev.org/manual_101_install.html for screenshots of this process)
1. Go to the Help Menu
2. Go to 'Install New Software...'
3. Click Add next to 'Work with' .... add 'PyDev' and 'http://pydev.org/updates'
4. Follow the Instructions on that link above... (yeah, tired of typing :-D)
Create a Python Project
File -> New -> Pydev Project
Project name: test_project
you might need to configure a python interpreter...
Auto config should work...
If not ... New... Python2.5 then (on MacOSX) browse to /System/Library/Frameworks/Python.framework/Versions/2.x/Resources/Python.app/Contents/MacOS/Python and on Windows it will be in your Program Files folder.
Create '.py' file inside the project directory.
Type this into it....
def main():
print "Hello"
main()
Right click on the file Run As... Python Run.
It should show 'Hello' in the Console window down the bottom.
You should also be able to put Debug As... Python Run.
Might help if you set a break-point.
(If you go into debugger view, you can click the 'PyDev' button on the top-right to switch back to editor mode.)
Summary
Phew!
Hopefully at this point you'll have a working Python, Eclipse IDE and PyDev. You can create Python projects, run them and debug them.
Saturday, July 24, 2010
A story about IDE problems
So making changes to source files. Discover I need to set a Python file to UTF-8 from MacRoman. Look at nested settings ... workbench settings, project settings, file settings. Make some changes.
Try to edit a file an Eclipse hangs. Force quit and launch ... look at settings - all seem ok ... try to edit... Eclipse hangs. Launch eclipse ... try to save the settings ... waiting on something else....
The user operation is waiting for background work to complete
Saving workbench state.
Model provider updating pythonpath
Initializing Java Tooling
Configuring classpath containers
OK ... try searching for last two items in Google, with various combinations of additional phrases. No real info. Check Eclipse log files, etc. Nothing interesting. Tried a few things.
Ok, so this is not happening. Check the .project and .pydevproject with their baseline. Hmmm .... no major changes. Wish I'd booked my workbench into Subversion...
So ... create another workbench. Import Python project into that new workbench. First thing; No Python interpreters configured in that workbench. Ok, bit of a PITA navigating to the actual python binary - it's in a bundle a long way down ...
/System/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python
Ok ... so do that for 2.5 as well.
But now colours in Python source files are all wrong! Hmmm... oh, why didn't I book it into Subversion.
Go for coffee and talk away from the computer for a minute. One coffee turns into two. Hang on... I HAVE got a backup ... duh! In Time machine!
Make a copy of the old workbench folder just in case....
Had to go back a few times in time machine before I got one that worked... Then had to add new debug configurations (I added 2.6 a few days ago).
And we are back to doing what I was meant to be doing...
P.S. I also notice the second release candidate of Eudora OSE, version 1.0rc2 has just been released. Haven't tried Eudora OSE at all yet. Might have to give it a go this weekend .... http://www.eudora.com/betas/
Try to edit a file an Eclipse hangs. Force quit and launch ... look at settings - all seem ok ... try to edit... Eclipse hangs. Launch eclipse ... try to save the settings ... waiting on something else....
The user operation is waiting for background work to complete
Saving workbench state.
Model provider updating pythonpath
Initializing Java Tooling
Configuring classpath containers
OK ... try searching for last two items in Google, with various combinations of additional phrases. No real info. Check Eclipse log files, etc. Nothing interesting. Tried a few things.
Ok, so this is not happening. Check the .project and .pydevproject with their baseline. Hmmm .... no major changes. Wish I'd booked my workbench into Subversion...
So ... create another workbench. Import Python project into that new workbench. First thing; No Python interpreters configured in that workbench. Ok, bit of a PITA navigating to the actual python binary - it's in a bundle a long way down ...
/System/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python
Ok ... so do that for 2.5 as well.
But now colours in Python source files are all wrong! Hmmm... oh, why didn't I book it into Subversion.
Go for coffee and talk away from the computer for a minute. One coffee turns into two. Hang on... I HAVE got a backup ... duh! In Time machine!
Make a copy of the old workbench folder just in case....
Had to go back a few times in time machine before I got one that worked... Then had to add new debug configurations (I added 2.6 a few days ago).
And we are back to doing what I was meant to be doing...
P.S. I also notice the second release candidate of Eudora OSE, version 1.0rc2 has just been released. Haven't tried Eudora OSE at all yet. Might have to give it a go this weekend .... http://www.eudora.com/betas/
Saturday, March 06, 2010
Programming Quotes
There are quite a few programming quotes. These are a few I like.
Computer programming is tremendous fun. Like music, it is a skill that derives from an unknown blend of innate talent and constant practice. Like drawing, it can be shaped to a variety of ends – commercial, artistic, and pure entertainment. Programmers have a well-deserved reputation for working long hours, but are rarely credited with being driven by creative fevers. Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination, but because their imagination reveals worlds that others cannot see.
-- Larry O'Brien and Bruce Eckel in Thinking in C#
"With good program architecture debugging is a breeze, because bugs will be where they should be."
-- David May
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
-- Brian Kernighan
As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.
-- Maurice Wilkes discovers debugging, 1949
Why do we never have time to do it right, but always have time to do it over?
-- Anonymous Code Monkey
Computer programming is tremendous fun. Like music, it is a skill that derives from an unknown blend of innate talent and constant practice. Like drawing, it can be shaped to a variety of ends – commercial, artistic, and pure entertainment. Programmers have a well-deserved reputation for working long hours, but are rarely credited with being driven by creative fevers. Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination, but because their imagination reveals worlds that others cannot see.
-- Larry O'Brien and Bruce Eckel in Thinking in C#
"With good program architecture debugging is a breeze, because bugs will be where they should be."
-- David May
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
-- Brian Kernighan
As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs.
-- Maurice Wilkes discovers debugging, 1949
Why do we never have time to do it right, but always have time to do it over?
-- Anonymous Code Monkey
Labels: computers, debugging, programming
Monday, February 22, 2010
iPad & Flash
I know Steve has mentioned his voiced reasons - and I'm not going to say what I think of Flash itself except to say that, like all technologies, there are advantages and disadvantages.
Several people are saying that the decision to not support the Flash on the iPad is to protect the App Store. I've this heard from several places.
But I'm not sure it holds up as an argument.
Two thoughts:
1. There are apps developed for straight html web sites already - html 5 will only extend the reach.
2. Additionally, unless I remember wrongly, native apps weren't there at the release of the iphone - web apps were initially touted as the initial approach.
Several people are saying that the decision to not support the Flash on the iPad is to protect the App Store. I've this heard from several places.
But I'm not sure it holds up as an argument.
Two thoughts:
1. There are apps developed for straight html web sites already - html 5 will only extend the reach.
2. Additionally, unless I remember wrongly, native apps weren't there at the release of the iphone - web apps were initially touted as the initial approach.
Monday, December 14, 2009
SVN, Keyboards and mad, mad projects (z80!)
"This client is too old to work with working copy" --- some recent OS upgrade seems to have wiped my upgraded Subversion and replaced with 1.4.4 :-( Oh well, it's a quick fix to install new one. Must have only happened over the last few weeks...
I'm sure you've seen this keyboard before http://www.thinkgeek.com/computing/keyboards-mice/9836/. I like the look of it, although since I don't look at my keyboard whilst typing think it would be a bit wasted. Very pretty though. I wonder what the key action is like?
Anyway, been working on the Robot a few weeks ago and my game project last week. Although when say 'game project', actually I was continuing an asm to C/C++ converter I was writing on holiday and decided to spend a few hours on last week. To check the integrity of the data-set, I decided to write a machine code output ... effectively creating an assembler ... for Z80. Yeah, I know. I must be mad. Not finished yet, but other things need doing.
Anyway this week it's back onto the data importer for LWC, after I've checked-in the work I've been doing over the last few days into our Subversion server. Hence the hitting the Subversion client problem...
I've also got some work to do for another project which really needs to be finished before next week if I'm going to meet promised goals... going to be a busy week.
Anyway, blogging about it isn't going to get anything done...
I'm sure you've seen this keyboard before http://www.thinkgeek.com/computing/keyboards-mice/9836/. I like the look of it, although since I don't look at my keyboard whilst typing think it would be a bit wasted. Very pretty though. I wonder what the key action is like?
Anyway, been working on the Robot a few weeks ago and my game project last week. Although when say 'game project', actually I was continuing an asm to C/C++ converter I was writing on holiday and decided to spend a few hours on last week. To check the integrity of the data-set, I decided to write a machine code output ... effectively creating an assembler ... for Z80. Yeah, I know. I must be mad. Not finished yet, but other things need doing.
Anyway this week it's back onto the data importer for LWC, after I've checked-in the work I've been doing over the last few days into our Subversion server. Hence the hitting the Subversion client problem...
I've also got some work to do for another project which really needs to be finished before next week if I'm going to meet promised goals... going to be a busy week.
Anyway, blogging about it isn't going to get anything done...
