Wordpress pain

Well, I’ve been working off and on for a few days on a Processing applet. The only reason I like Processing better than NodeBox is because I can embed the results in an applet on my web site. At least, I could if WordPress didn’t keep stripping out the embedding markup.

[Update: I did finally figure out how to disable the auto-conversion. What a PITA. Now I just have to figure out what I've broken by doing that.]

Frustrations like this are why I was writing my own blogging software. The main holdup on writing my own blogging software was the fact that it is damn difficult to get anything but PHP to run persistently on Dreamhost. Actually, even WordPress runs as a CGI, so maybe its hard to run PHP persistently too. Anyway, it would take a lot of effort to move all my sites to a host that lets me run WSGI apps, but at moments like this, I think it would be worth it. WordPress has sucked yet more hours of my life away.

Going to bed, sick with loathing for WordPress. Again.

from vanpyz import cannon

The March Vancouver Python and Zope User Group meeting snuck up on me.  It is tomorrow night, March 4th, in the conference room of the Bentall Five building.  Same location for three months in a row, that’s got to be some kind of a record!  For anyone who has not attended recently and would like to, there is a map on the VanPyZ website. Tomorrow’s talk will be by Brett Cannon:

The once and future Import: How import works in Python 2.6 and beyond, and what it means for you.  

This should be interesting. The import mechanism has some interesting hooks, and Brett has been re-working it for some time now. Also, the recent meetings have had a great turnout with lots of good conversation, both during the meeting itself, and afterwards at the pub. So come on out, if you’re in Vancouver.  And if you’re not in Vancouver, well from whereeveryouare import yourself.  See you there!

Programming Problems

Ruins

André Roberge has started a project to embed programming challenges in his Crunchy project. Crunchy is a way to embed both Python code and an interpreter into a web page, making it great for Python education projects. I like the idea of collecting both sources of programming challenges, and solutions to them in Python, as a way of spurring interest. Discussion around alternative solutions could get pretty interesting. Anyway, rather than try to coordinate this through the comments on André’s blog, or via email, I created a page on the Python wiki to help organize this: Problem Sets. I listed the 99 Prolog Problems that Andé pointed to originally, and a couple more sources of programming challenges I was aware of. Hopefully more people will fill in both the sources, and some solutions. I’ve started to put up a few solutions, but this is definitely something that would benefit from more participation.

If you know of a good source of programming problems (or want to create one), or want to provide some solutions of your own, or even just kibbitz, please dive in!

Simple text adventure

Stairs down

My daughter was working on her XO and said something like, “Dad, you promised to help me with Python.” It turns out she was working on her first python program, using the bare-bones IDE for the XO, Pippy. (Aside: If anyone has a suggestion for a better environment than Pippy, that runs on the XO, I’m all ears.) I helped her with the syntax of an if statement and explained how else works, and how to quote the strings she was testing against. Looking over her shoulder I see that she’s got a minimal version of a text adventure game shaping up. I thought this would be a good opportunity to introduce her to lists and dicts, so I put together an email for her, so she would have it to refer back to later. Here is what I wrote:

In Python there are some powerful data structures for organizing your
code and information. You can also build your own, and I’ll show you
how to do that soon, but it is amazing what you can do just with what
is built in. Three of the key structures are lists, dicts (short for
dictionaries), and tuples. Tuples are similar enough to lists that I
will only talk about lists and dicts for now, and we can talk about
tuples later. Also, it is important to know that when I talk about
“strings” I mean text, which is a string of characters, generally
surrounded by either single quotes ‘like this’ or double quotes “like
this”.

Dictionaries

Dicts are a lot like real dictionaries: there is something you use to
look something else up. With a real dictionary you use the spelling
of a word to look up its definition. With python dictionaries, you
can use a wide variety of objects to look up other objects. The
objects you look up can be anything: strings, functions, even lists or
other dictionaries. The objects you use to look up with are required
to be things that cannot change, but usually they are strings.

For convenience we call the things we use to look up with “keys” and
the things that are looked up “values”. In the case of a real
dictionary, the key is the spelling of a word, and the value is the
definition.

There are two ways to create a dict and assign it to a variable, in
this example the variable is “my_lookup”. There is the dict()
constructor function, and the literal notation.

my_lookup = dict(one=1, two=2, three='xyzzy') # dict constructor
my_lookup = {'one': 1, 'two': 2, 'three': 'xyzzy')

Once we have a dict, we can change the value of the keys, add new key/
value pairs, remove key/value pairs, or loop through all the keys:

print my_lookup['one'] # prints ‘1′

my_lookup['one'] = ‘flugelhorn’ # the key ‘one’ is now associated with the value ‘flugelhorn’

my_lookup['four'] = ‘this is the value of the key “four”‘ # add a new key, my_lookup now has four key/value pairs

del my_lookup['one'] # remove a key/value pair, my_lookup now has keys ‘two’, ‘three’, and ‘four’

for key in my_lookup:
print key, my_lookup[key] # will print each key with its value, in random order

The last point is important: dicts do not keep any specific order for
their keys. If you need to have the keys in order, you need to sort
them first. Usually this is not a problem.

For more information on dicts and things you can do with them: http://docs.python.org/lib/typesmapping.html

If you do need ordering, then you would generally use lists. Lists
keep what you put into them in order. They can grow or shrink as you
put things in or take them out, and you can put pretty much anything
into a list, including dicts or other lists. Getting or putting an
item into a list looks a lot like getting or putting an item into a
dict, except the keys are always integers. By now you probably know,
or can guess, that the first item in a list is 0, followed by 1, then
2, etc. You can also count down in a list: the last item is -1, the
next-to-last is -2, etc.


my_list = list(1,2,3,4)
my_list = [1,2,3,4] # create a literal list

So far my_list may not be what you expect. What is the value of
my_list[1]?

print my_list[1] # prints 2 because the indexes (like keys) of the list start with 0
my_list = [4,8,16,32]
print my_list[1] # prints 8

You can change any value in a list

my_list[2] = "new value of two"

You can add new values to the end of a list

my_list.append(64)

And you can go through all the values of a list:

for value in my_list:
    print value

OK, so what does all of this have to do with games?

Here is a simple example game. See if you can extend it to do some
more things, like dropping an item (or all items), or the ‘look’
command to show the long description and the room’s contents. What
else would be good to add?

'''
Simple dungeon game
'''

character = {'inventory': [], 'location': 'west room'}

dungeon = {
    'west room': {
        'short description': 'west room',
        'long description': 'a sloping north-south passage of barren rock',
        'contents': ['pail of water', 'dragon tooth'],
        'exits': {'east': 'centre room'}
    },
    'east room': {
        'short description': 'east room',
        'long description': 'a room of finished stone with high arched ceiling and soaring columns',
        'contents': [],
        'exits': {'west': 'centre room'}
    },
    'centre room': {
        'short description': 'centre room',
        'long description': 'the very heart of the dungeon, a windowless chamber lit only by the eerie light of glowing fungi high above',
        'contents': ['golden key', 'spiral hourglass'],
        'exits': {'east': 'east room', 'west': 'west room'}
    }
}

while True:
    room = dungeon[character['location']]
    command = raw_input(room['short description'] + ' > ')
    command_parts = command.split(None, 1)
    verb = command_parts[0]
    obj = command_parts[-1] # if the user only types one word, both verb and obj will be the same
    if verb in ['east', 'west', 'north', 'south', 'up', 'down', 'in', 'out']:
        if verb in room['exits']:
            character['location'] = room['exits'][verb]
            room = dungeon[character['location']]
            print 'You are in', room['long description']
            for item in room['contents']:
                print 'There is a', item, 'here'
        else:
            print 'You cannot go that way'
    if verb == 'inventory':
        print 'You are carrying:'
        for item in character['inventory']:
            print '   ', item
    if verb == 'quit':
        print 'Goodbye'
        break
    if verb == 'take':
        if obj == 'all':
            if room['contents']:
                for item in room['contents'][:]: # this part: [:] makes a copy of the list so removing items works
                    print 'You pick up the', item
                    character['inventory'].append(item)
                    room['contents'].remove(item)
            else:
                print 'There is nothing to take!'
        else:
            for item in room['contents']:
                if obj in item: # does the word in obj match any part of the text of item?
                    print 'You pick up the', item
                    character['inventory'].append(item)
                    room['contents'].remove(item)

That’s it for now. I hope this was clear. Feel free to ask me
question.

Love,

–Dad

Apologies

Mandala

I wanted to apologize for spamming Planet Python (and anyone else that happens to subscribe) with old posts recently. After spending many of my free hours for the past year noodling around with my weblog software (and the myriad of problems getting anything to run persistently on Dreamhost), I finally gave up and switched to WordPress. I’ve imported my old blogs for the sake of continuity (from Blogger and my own custom weblog, but not going back to Manila yet), and mapped the old URLs with redirects, so things should be fairly transparent to the casual observer.

A couple of good things came from my time wrestling with my own software. I learned a lot more about WSGI. I got to evaluate TurboGears, Django, web.py, Pylons, and raw WSGI. I learned more about Atom syndication and AtomPub. And while I was doing all that, WordPress got semi-decent Atom 1.0 support and also began to support tagging: two of the reasons I was rolling my own. It’s still dog-slow, but I think that may have more to do with how DreamHost is running it (as a CGI) than WordPress itself. And since I’m running several other WordPress blogs for family members and friends, at least I only have one system to fight with rather than two.

Another good thing that came out of switching to WordPress, and one of the main reasons I had ripped my old blogging system apart to rebuild it it as a server-side tool, is that my blog supports comments now. I have spent the past year doing a bit more than just twiddling with blogging software, and I hope to have some of my more interesting experiments and some tutorials to post soon. Let me know what you think of it.

« Previous Page« Previous entries « Previous Page · Next Page » Next entries »Next Page »

google

google

asus