wintermute1974 in Toronto is doing 3 things including…

learn python

5 cheers

 

wintermute1974 has written 10 entries about this goal

Typo Yields Weirdness 4 years ago

Dictionaries are one of the built in types that I really like in Python. (I think of them as the equivalent of dt and dd in HTML, if that helps anyone.)

But I made a typo, and that typo works. Now the typo is playing with my head. Take a look as this history from the Python Interpreter:

>>> d={} 
>>> d['key']=d
>>> d
{'key': {...}}

Yes, if this is correct, Python knows that dictionary ‘d’ has a key named ‘key’ that contains dictionary ‘d’.

What makes this example particularly cool is the {...}, which shows Python knows that there is no bottom at the end of this dictionary.

This is so cool.

These examples work too:

>>> d['key']['key']
{'key': {...}}
>>> d['key']['key']['key']['key']['key']['key']
{'key': {...}}



pydoc 4 years ago

It took the afternoon, but I found the useful Python module that creates a web server so that you can browse installed modules from your web browser:

It’s called pydoc.



It's import(ant) 4 years ago

This works, bringing function() into your namespace:
   from module import function

And this works, but you will have to call module.function():
   import module

But this one doesn’t work:
   import function from module

Of course, the third way is the one I have been typing all morning.

I got my first module to run in WingIDE this morning. The entire code is:
%{color:green}if name==’main’:
   print ‘Hello World’%

Very nice. In WingIDE, I:
  • created a project
  • created a new text file
  • associated the text file with the project
  • ran the project.

In the DebugI/O window, it showed “Hello World.”



Mapped Out the Grand Plan 4 years ago

I spent about an hour or two tonight sitting at my kitchen table with pen and paper. Before hitting a single key of code in my pricing program, it helps to know what the goal is, so that I can get there.

Since Python treats everything like an object, the most natural way to create a pricing program is to map the real-world objects onto Python objects.

So far, I have a master object called the System. My program can have only one of them.

Each System will contain zero or more Components. Sample components include
  • rectifiers (that turn AC to DC),
  • inverters (that do the reverse)
  • batteries (that sit between rectifiers and inverters)
  • and sundry other components (such as diode droppers, blocking diodes, and best battery selectors).
Each Component will have a number of Attributes including:
  • input (voltage, current, etc)
  • output (the same)
  • settings
  • meters
  • alarms

The Attributes are the sticky part right now. Some attributes will be independent of one another, others will be mutually exclusive. Some will be dependent on one another.

This will probably take some time to thrash out, but I am glad I did it on paper first.



Python Project 4 years ago

Well, I most definitely have a Python project to work on now: Yesterday, I pitched a Pricing Tool to the Vice President of the company I work for.

Programming isn’t my job - at least it isn’t right now - but I offered to create a tool that salesmen and end-customers can use in the field.

Now all I have to do is create it. I told him that I would have a prototype ready in two months. It looks like my evenings and weekends just got a whole lot busier! October 24, here I come!



Trying WingIDE for Development 4 years ago

Up until now, my favorite programming language has been Visual Basic 5, and one of the main reasons is that it has a nice IDE.

Those people coming from a Unix background prefer the command line, and so the default installation of Python is probably just fine for them.

But for me, I want syntax highlighting and the ability to visually stop and step through a program that has spit up and died.

A cursory Google search shows that there are dozens. At first, I tried some of the free ones: IDLE (which came with my installation of Python), DrPython, and PyCrust and PyAlaMode.

I was hoping to find a free editor that would meet my needs, but so far, no luck.

So, after surfing the web for Python development environment reviews, I learned about Wingware’s WingIDE. Sadly, it is a commerical program, with licenses and costs. Thankfully, there’s a stripped down “Personal” version that does not cost too much.

Wingware is generous enough to give you a trial version that you can run for three 10-day periods, of which I am on my first. As my screenshot shows, I have not actually coded in this IDE yet, but have been going through the tutorial slowly and systematically.

So far, so good. If paying a few dollars for a good IDE will save me hours (or days) of frustration later, it will be money well spent.



Two Program Ideas 4 years ago

Now that I have experimented a little with Python, my mind is already considering which fun, sample programs I could write to learn the language. So far, I can think of two.

Idea #1: Create A Text Adventure
This would be a “fun with objects” learning experience. I could define a room object and an object object and populate a world with them.

I could create a stub of a def parser(): function, which would interpret text typed by the user.

In its own, tiny way, this would be my tribute to the Infocom games that I used to play.

If I am lucky, the game will be more exciting than my life at work:
  • Look

You are seated at an office desk. Behind you is a window. To your right is a photocopier. Exits are to the south and west.

  • Examine desk

You are sitting behind dual 17” displays at work. In the right display, the Microsoft Outlook Inbox is maximized, like it is every day. A message from your brother Carl is highlighted there. To the left of your keyboard is a small cup of coffee with cream and sugar. The cup is half empty.

  • Hit the reply button

You hit the monitor with your fist. Your knuckles hurt.

  • Press the reply button with the mouse.

A window appears in the left display. You can see happy little desktop icons in the background.

  • Drink coffee

You don’t see any coffee here!

  • Drink cup

You take a sip of coffee. It is lukewarm. Your stomach turns.

  • Type a reply using the keyboard

You place your fingers on the home row of the keyboard. The keys are slightly discoloured, possibly from the dirt from the factory floor beyond this room, possibly from one too many snacks eaten at this desk.

Idea #2: Create a Game of Life

Now, this game is notConway’s Game of Life, simply because I never enjoyed the stupid repeating patterns.

Instead, I would create a bug object. Each bug would have an x% and %{color:red}y position on a two-dimensional world.

Each bug would also have a energy level that would count down with every move. When the bug discovers food, its energy increases. When the bug moves, it expends energy. If it doesn’t eat frequently enough, it dies.

To make the game interesting, each bug could have its own genotype in a 3×3 matrix for each of the cardinal directions plus the middle element for no movement at all.

Two assumptions I have already made for now are that multiple bugs can inhabit the same x,y position on the screen peacefully, and that bugs are blind and move without sensing the world outside.



Session 1: Introductory Bits 4 years ago

I finally sat down and started reading the How to think like a computer scientist book, which I’ll just call my primer from now on.

So, what did I learn?

Well, for one thing, both functions and classes are ridiculously easy to create.

%{color:blue}# These next two lines will create a function.
def name(parameters):
   pass%

%{color:blue}# These next two lines will create a class.
class powerful:
   pass%

I also discovered two very handy built-in functions.
  • id(variable) will give you the unique ID number for any variable currently available. This is handy if you want to tell if two variables are pointing to the same location or to two different locations.
  • type(variable) will tell you what type the variable presently is, with useful names such as ‘classobj’ or ‘int’.

I found both of these functions so handy, that I created my own “info” function, that goes like this:
%{color:blue}def info(var):
   print ‘Value:’,var
   print ‘ID:’,id(var)
   print ‘Type:’,type(var)%
And that is that.

Incidentally, pass is a place holder for when you do not have any statements. You’ll need it because of the indented structure of the language.



Found My Python Primer 4 years ago

Finally, I have found my Python Primer.

It is called How to think like a computer scientist by Allen B. Downey and Jeffrey Elkner. Best of all, I can share it with all of you because it is a free document. I chose to download the free PDF version as I find PDFs display quite nicely on my monitor.

I have programmed before, so I do not need much hand holding there. But an introductory guide will give a good survey of the entire language, and sometimes it is nice to be lead by the nose.

If I happen to learn a few good coding practices in doing so, so much the better.



Why Python? 4 years ago

As a child in the 1980s, I was exposed to BASIC on 8-bit microcomputers. Over the years, I have had some success with C and assembly, but never really got serious about programming.

At my first real paying job, I discovered Visual Basic for Applications and later on the full-blown Visual Basic. I enjoyed using those tools, and truth be told, they’re still the ones I know best.

But Microsoft and I parted company when they started unrolling VB.net. By that time, I realized that Microsoft reinvents its tools every few years, and this did not appeal to me at all.

If there is a convincing reason to change development environments, fine, but just because some company wants to sell me their newest and greatest? No thanks.

So I surfed the web, looking for a free, open-source language which would not be held at the mercy of arbitrary upgrades to meet some company’s product cycle. There are many of them. I invite you to look at them for yourself.

Last year, I decided it was Python. I read the papers of Python’s creator, and in them, I learned that former VBers like myself were his target audience.

Since then, I have collected a few Python books. My first one was a bit of a disappointment, and it sort of put me off further studies. But no longer.

Now it is time to actually sit down and learn it.



wintermute1974 has gotten 5 cheers on this goal.

 

I want to:
43 Things Login