Becoming a free software developer, part III: Programming for the impatient

Becoming a free software developer, part III: Programming for the impatient


I finally began learning python. I wrote my last program in the 80s in Apple Basic, and here I am again starting to learn a new language. I can already guess what my biggest problem will be. I am incredibly impatient. How can I learn to program when I refuse to read the documentation all the way through? Will I succeed in writing a program or am I doomed to give up? No need worrying about it. I type python on the command-line, and start.

Python is already loaded on my computer, so typing python on the command line gives me the python prompt which looks like three greater than signs in a row. I decide to follow the tutorials by typing into the program and seeing what it returns.

Python.org has got a nice list of beginning tutorials on a page called Beginner's Guide to Python. I picked a tutorial to start with called Learning to Program.

I Breezed through the first couple of pages stopping when I found the Hello World program. You aren't really learning to program until you do Hello World. The tutorial actually said to write print 'Hello there!', but I am a traditionalist. At the prompt I wrote print 'Hello world!' and Hello world! popped up. I was on my way!

In a previous blog, I decided to write a program called period tracker(PT) that counts how many days since a person's last menstrual period. First, I opened a text file and started to think out what logically I had to do to make the program work.

The program requires me to subtract dates from dates. It takes more than simple math to subtract September 27 from October 12. Each month has a different number of days, and the program had to know that. Would I need to construct a table so the program knew how many days were in each month? Would I have to make a calendar counting each day from January first and converting the input dates?

But then I thought, surely someone has done this before. Python has modules that do stuff like this. I just needed to find out which modules controlled these calendar functions. I asked my local python expert (read husband) and he showed me where to find the documentation for python on my computer (mine was in a file at /usr/share/doc/python2.4/html/index.html). I tried to tell him that I had found the documentation on a website but he poo pooed me for trying to use offsite documentation.

Anyway, there were two modules that looked like they might be useful for this program. Calendar for general calendar-related functions and datetime for basic date and time types. Looking through the documentation, I saw that datetime has a class object called date that can add and subtract dates. My first goal was to figure out how to import and run a module that would subtract a date from a date and return a number.

To load a module I typed import and the module name. It explained this in the calendar documentation, but how do I use the class object date? I better go back to my tutorial and see if it has anything on modules.

I searched the table of contents and found the page on functions and modules. Having skipped a few pages I found it a bit hard to read, but "Faith favors the fearless" or "hope helps the helpless" or something like that. I'm sure that there is some aphorism for blindly going where angels fear to tread. Anyway, it says that you can use the sys module to exit from python. Cool! I had not even thought about how to leave the program yet.

I skimmed through, and not finding what I was looking for I tried typing a few commands. Just guessing really. Mostly I got back error messages.

At times like these I must pause. I realize that blindly typing things into the computer can have consequences. I realize that doing such things has caused me problems in the past such as the time I reformatted my program disk by mistake. I think that I should be a good girl and calmly read the directions. But the fact is I am not a good girl. I am a nervous, flighty, distractable, impatient woman who wanted to know how to do this yesterday and who has no patience for incompetence. I want it to work, and I want it to work now. Once I have a quick and dirty outline of a program I figure that I can sculpt it into something neater.

When I read the official documentation, I'm annoyed that there aren't more examples. Not understanding the syntax, I type in things that sound reasonable, but are wrong. Perhaps blindly guessing may not be the best way to learn how to program. Who knew?

So I slowly go back and begin reading everything that applies to python modules from the top of the page, then in my python window I type:print dir(datetime) and it returns a valid answer this time instead of an error. ['MAXYEAR', 'MINYEAR', '__doc__', '__file__', '__name__', 'date', 'datetime', 'time', 'timedelta', 'tzinfo']These are the classes under the module datetime.

Reading the documentation page, I find that the difference between one date and another is called a timedelta. This timedelta number can show me the number of days since the last menstrual period which is the first piece of data that my program will compute. Python modules can do it. I can make this work. I step back and plan the program again.

Period tracker will have a number of functions.

  1. I ask for a list of dates of first day of each menstrual period.
  2. Then I subtract them finding the timedelta between each of them.
  3. Next I can average all of them and find the average length of a menstrual period.
  4. Then I add the timedelta to the last date shown to predict the date the next period will start. I might do this to the end of the year.
  5. I should also do a range and compute the percent error in the calculation.
  6. I should have some part of the program that looks for missed periods and either inserts a period where there should have been, or removes the data so that it doesn't mess up the average, or flashes a pregnancy alert.

OK, Maybe I'm going a bit too far on the last one. I still need to figure out how to subtract one date from another, and how to write a program in a text file. So far I have only used the command line. But at least I have started now. When you are first learning, you should take baby steps, but sometimes it's hard not to try to run before I can walk.

Category: 

Comments

Anonymous visitor's picture
Submitted by Anonymous visitor (not verified) on

Sorry, my non-PC side got the better of me there.

I'm totally confident you can achieve all the steps you listed for your program. As long as you break everything down to simple steps (and it looks like that's what you're doing) you can't go wrong.
Sometimes it can help to do a little mini project like a proof of concept or something on the side, so jou don't get bogged down too much. I know it helps me when working on long projects. Nothing motivates like success.

Dennis

Anonymous visitor's picture
Submitted by Anonymous visitor (not verified) on

that was something! I would have to wish you luck. I know that you will get to succeed on this. Just be patient enough and you will perfect everything .
http://www.ayboston.com

Anonymous visitor's picture
Submitted by Anonymous visitor (not verified) on

Setting yourself challenges is a great way to approach life in general. You will be amazed at what you can achieve. When you try to learn a computer language you will also learn a lot of other things. For one thing, you will gain a deeper understanding of how your computer works. It will also help you to think problems through in a logical manner.

I didn't know how to do this either, so you inspired me to experiment. Here is my result.

import datetime

k = datetime.date(2000, 3, 17)
l = datetime.date(2002, 7, 4)
print l - k

Now, how about a program that calculates how many shopping days until Christmas? Happy programming!

Anonymous visitor's picture
Submitted by Anonymous visitor (not verified) on

You might wish to look into converting to/from Julian date. Julian date is a day numbering (starts at noon officially, because it was invented by an astronomer :), so that you don't have to worry about month boundaries...just subtract.

admin's picture
Submitted by admin on

Unfortunately, about 13 comments on this blog entry were lost due to a bug in Drupal. The bug is currently being investigated.

If you commented on this post yesterday and cannot see your comment now, please accept out apologies and post your comment again.

Apologies also to Rosalyn.

Thanks

- FSM Staff

Anonymous visitor's picture
Submitted by Anonymous visitor (not verified) on

I've never met a female programmer. I feel I'm in it alone. I'm not patient at all and hate to read documentation. So I can completely relate. I want to learn it all....Perl/CGI, PHP, C++, .Net (Lord knows why...I just know I need to keep up with everything and not just stick with unix based languages). I've gotten side tracked due to work but I'm in the position now that I may have a lot of free time on my hands during part of the week. I have a few complicated projects lined up. Nothing that I need to rush on....just projects that I want to see if I can code. Trying to decide on the best language.....thinking of perl.

Good luck with this.

Tonya

Anonymous visitor's picture
Submitted by Anonymous visitor (not verified) on

I don't understand this "hate to read documentation" mentality. Documentation is vital, if only because there's no way you can hold in your head all the information you need.

For instance, while doing any Python programming, I have two pages permanently open in Firefox: the Python Library Reference and the Language Reference. From here I can hop immediately to a relevant page that explains further some point about some code I'm working on.

If you're not doing this, then chances are you're not using the language to its full effect. This can manifest itself in code written in a long-winded, roundabout fashion, that could be rephrased much more concisely. In Python, at least, the concise version is often easier to understand and easier to maintain, as well.

Lawrence D'Oliveiro

Anonymous visitor's picture
Submitted by Anonymous visitor (not verified) on

> Trying to decide on the best language.....thinking of perl.

If there was the slightest hint that you were joking I'd let that go, but as you seem to be serious about it... FOR THE LOVE OF GOD DON'T DO IT! Perl is a horrible hack that happens to work mostly ok for one-liners or throwaway little scripts, but will suck your brain and make you regret the moment you decided to write your first over-hundred-lines program. You know, programs are read far more often than written and perl has been fairly described as "write-only" language. Trust me on this, you don't want to decipher a non-trivial 3-months-old perl script, even if you wrote it (let alone someone else).

Same holds at a lesser degree with PHP since it's more or less a dumbed-down version of perl aimed at web "programming". It's the easiest to learn compared to the alternatives if you want to hack a few dynamic pages here and there but as a language is inherently flawed, ignoring all modern language design principles and leaving with you a mess of program logic entangled with presentation (html).

If you're looking for a language that keeps a healthy balance between elegance and practicality, learn Python and/or Ruby. Both languages are fairly high level and have more commonalities than differences; most people's preference to one or the other usually comes down to personal taste. Ruby seems to be somewhat more orthogonal and elegant, Python is currently faster and has much larger standard and 3rd party libraries (though the gap may shrink in the coming years after the hype around Ruby on Rails). My personal preference is python (love the clean syntax and the included batteries) but I'd certainly consider Ruby as a reasonable alternative should I decide to leave python behind sometime in the future.

Hope this helps,
George

Rosalyn Hunter's picture

Is it possible that only posts by annonymous cowards are being allowed here? I watched my husband type in a comment that never appeared. Let's get that Drupal bug fixed!!

-Rosalyn

Dave Guard's picture
Submitted by Dave Guard on

Hi Rosalyn,

Your husband's comment was probably one of those lost when the bug did its work. Not very many people bother logging in to comment. It seems even people who have registered can't be bothered logging in before commenting even though they can win the book competition if they do. Still I'd rather anonymous comments than none at all.

I hope this improves in the coming months.

Bye

Anonymous visitor's picture
Submitted by Anonymous visitor (not verified) on

Not only do you have to calculate the average interval, but you should also calculate the standard deviation as well. This is because biology does not run on clockwork (who am I to tell you this? you know it already), so there will be some drift, and your predictions will get less and less accurate further out.

It would also help if, instead of calculating the average from all your records right back to whenever, that you only kept a "running average", going back say, a year or two at most. That way your calculation will adapt better to drift, and your prediction for a particular date in the future will become more accurate as it gets closer.

Lawrence D'Oliveiro

Anonymous visitor's picture
Submitted by Anonymous visitor (not verified) on

Hi there. Over at ShowMeDo.com we have a growing set of (free) educational videos, mostly for programming (34 for Python right now with more to come). You might like Jerol's introduction to Python objects, and our IDE sets may give you a few pointers towards new development environments. My founding partner Kyran has a set on GUI programming with wxPython which might help if want to add a user-interface?

We're moving towards larger, more useful tutorial sets. John Montgomery has a set on writing Java applets, it'll be a 10-part set when finished (but using Java, so not so useful to you) - but a great example of where we're heading. We are always looking for feedback on how we could improve, if you have thoughts?

Ian Ozsvald (ian@showmedo.com - one of the founders of ShowMeDo.com)

Seun's picture
Submitted by Seun on

Thats how it works. Just keep learning, don't bite more than you can chew or you'll want to quit. Don't hop to another language just yet, continue with python, when its time to learn something new you'll know.
To complete your current project successfully I suggest you play around with modules you intend to use in your program so you'll understand exactly how to implement them in your program.
Just be steady in your learning and you're there.

.s.I..s.ra....el

Terry Hancock's picture

Okay, I'm going to try posting this again:

You complain about a lack of examples in the Python documentation, and you are not the first to notice this. There are, however, a number of third party sites that attempt to fill this void. Two of the best:

Useless Python
A collection of python snippets that demonstrate various tricks and usages. As the name implies, this is not production code, just fun demos.

ActiveState Developer Network
Active State maintains a python distribution, and a website with lots of programmer distributions (I think they work with other languages too, based on the site). Anyway, there are a lot of nice "cookbook" examples on this site.

And here's yet another python tutorial, you may like:

Python as a First Programming Language for Everyone

Happy hacking!

jonnyb13's picture
Submitted by jonnyb13 on

Good luck! When you do perfect this, be sure to get a load of questions from me. I am a beginner and have very little knowledge about this. When I do learn enough, I'll be looking towards you for help! Where can I learn more about htis python?

Author information

Rosalyn Hunter's picture

Biography

Rosalyn Hunter has been on the internet since before the web was created. Born into a family of instructors, she has made it her life's goal to teach others about the important things in life, such as how to type kill -9 when a process is dead. She lives in a little house on the prairie in the American West with her husband, her three beautiful children, a cat and a dog.