Alphaios Blog

This is the official blog of Alphaios.net. When I feel like rambling I turn here.

11/25/09

Python String-Inferred Names (working title)

UPDATE:

As it turns out, an idea that is similar (though not quite the same) to this one has already been proposed to the appropriate people--and rejected.
See PEP 363 for details.

Also, the recent PEP 3003 says that no more changes can be made to the Python syntax until more than a year from now in order to allow the various Python implementations to catch up to the current spec.

It looks like this idea won't see the light of day for quite some time, if ever.


I have an idea for a new feature to the Python programming language, but I have no idea how to go about getting it implemented, so I'm just going to describe it here.

This isn't a major feature--it's not even a very original one--and it will not make anything particularly new or exciting possible which is not already possible. However, I believe that a feature such as this would make certain tasks much easier to perform, and would make the code for such tasks easier to read and understand by others. This feature would in no way interfere with existing code as it relies on the use of an operator which the python interpreter currently does not recognize, the dollar sign ($), except in the rare and unusual circumstance that someone's code relies on a SyntaxError occurring. It would, however, make code using this new feature incompatible with older Python code.

So, what is the feature? Essentially, it is a way to combine the built-in functions getattr, setattr, and delattr into a single concise symbol. It is best illustrated with the following Python psuedocode:


>>> class Foo(object):
... def __init__(self):
... self.myattr = "hello"
>>>
>>> f = Foo()
>>> attrname = "myattr"
>>>
>>> # Here it is in use:
>>> f.$attrname = "goodbye"
>>>
>>> # Here is the result:
>>> f.myattr
"goodbye"


What this illustrates is that the syntax obj.$attr = value is effectively an alternate way of writing setattr(obj, attr, value).

Likewise,

myvar = obj.$attr

...is equivalent to...

myvar = getattr(obj, attr_expr)

...and...

del obj.$attr

...is the same as...

delattr(obj, attr)

As you can see, this syntax merely makes an existing feature of Python more concise rather than makes anything possible that is not already possible...unless you extend it to allow access to names in the current scope rather than names of a particular object, as illustrated below:


>>> name = "somevar"
>>> $name = 123456
>>> somevar
123456


Effectively what this does is that it takes the value "somevar" of the variable in name and uses it as an actual name in the current scope, then assigns the value 123456 to that name.

Although an assignment like the one above is currently impossible, it actually is possible to emulate a reference/return using the built-in eval function. For example, the expression $name is basically the same as eval(name, globals(), locals()).

So, why should anyone bother to implement this new syntax? Why add yet another syntactical element to the already feature-rich Python language? My primary reason has to do with metaprogramming.

One of the big uses for metaclasses is the ability to dynamically generate new properties and methods for classes. Through metaclassing, it is possible for all sorts of interesting, useful and time-saving feature sets to be added to classes with relative ease. Creating such metaclasses themselves, however, can be quite messy as it tends to rely on many calls to the getattr and setattr functions. Using these functions a lot in the same chunk of code tends to make it hard to read: Firstly, it draws out the normally trivial tasks of variable assignment and referencing into function calls, and secondly, it makes the object-oriented nature of the code less obvious. (I don't think this is a good thing. Do you?)

Using $ syntax, the getattr, setattr, and delattr needn't be used at all. We can reference attributes and assign to them with a syntax that more closely resembles what is actually going on conceptually.

I know one of the big arguments against a feature like this is that we don't really need it. Well guess what? It's true. We don't need it. But we don't really "need" a lot of the neat little features that exist in Python, like decorators, iterators, generators, list comprehensions, ... but the fact of the matter is that we like them, and they are very useful, and they serve to make the language much easier to use and, many times, easier to understand. A feature like the one I am proposing, although perhaps not AS useful as some of the existing features that I just mentioned, would certainly make the meaning of code clearer and would therefore be useful enough to be implemented.

Another complaint I might here is that a feature such as this may lead to bad code. And to a small extent, I guess I agree; it could be used for bad code. But although it could, it doesn't have to. Just like nearly any feature of nearly any language, it could be abused, yet these features exist and are well used all the time. In other words, I don't really see how the possibility to misuse a feature could be interpreted as a reason not to implement it. Like any feature, simply use common sense and learn from your mistakes, and you will be able to determine when to use the feature and when not to.

That's all I have to say about it for now. I may revise later.

Labels: , , ,

8/5/09

One more song uploaded:

0085 - If I Knew (revision 1)

This is a revision--almost a remix, but not quite--of "If I knew" that uses "real" instruments instead of the chiptune-sounding ones that the original used. Also, there is a small melodic addition at the end.

As usual, its in the music section, and is pending approval to The Mod Archive.

Labels: , , , ,

8/3/09

OMG GUESS WHAT my modules have been uploaded to The Mod Archive! You can view my artist page here.

Also, I've changed the naming system for new modules. It is now as follows:

[artist-name]_[mod-number]_[title][_r][revision-number].[ext]

- [artist-name] -> name of the mod artist in lowercase and with no spaces. (Exmple: themusicguy)
- [mod-number] -> number of the artist's composition in four digits, zero-padded. (Example: 0089 for composition number 89.)
- [title] -> The title of the module, usually the same one indicated by the internal title text in lowercase and with no spaces. (Example: "My Big Song" would become "mybigsong").
- [_r][revision-number] -> If this module is actually a revision of another previous module, a revision number is affixed to the end. (Example: The first revisions would appear as "_r1" at the end of the file name, "_r2" for the second revision, and so on.) Note, however, that a revision is NOT the same as a remix. Revisions are for minor changes to the existing file, such as a slightly altered tempo, better tuning, or even a better set of instruments.
- [ext] -> The file extension.

Finally, I've uploaded a handful of new songs, all of which are available from the music section. They are also pending upload to The Mod Archive.

#80 - Broken Chip Precursor *
#81 - Sleeping on the Sand
#82 - Winter Walker (revision 1) **
#83 - Good Beginnings
#84 - The Sky Rider

* A remix of Broken Chip
** Technically not a new song, but a new file at least.

Labels: , ,

2/15/09

New Song: "Portal to the Past"

I welcome you to song #79, "Portal to the Past".

Download: [XM] or [MP3]

(Note: Despite my best efforts to make the module as player-compatible as possible, some players don't seem handle the stereo effects properly. If you want to make sure that it sounds right, download the MP3 version and then find yourself a player that will play the XM the same way as the MP3 sounds.)

This is a remix of a song I did a long time ago. Actually, this is the second remix of that song...both the original and the first remix can be downloaded here:

Original: #12, Retro 01 (XM format)
First Remix: #42, The Dark in the Light (IT format)

It seems that the IT file for #42 above has been corrupted somehow. If you want to hear what it was supposed to sound like, check out the MP3 version (available from the music page).

Labels: , , , , , ,

1/10/09

ScrollBack Code Repository Now Active

I'm not sure who's aware of this, but for a very long time now I have been working on a project called "ScrollBack." It is a software development kit for making 80's and 90's style games targeted at people who are new to game making or programming in general.

Up until now, I have been pretty quiet about this project and haven't made much info available. However, I have just created a code repository for the project at Google Code. It can be accessed at the following URL:

http://code.google.com/p/scrollback/

If you are not familiar with Google Code, it's basically Google's version of Source Forge, only much easier to use, or so I am told. (If you don't know what Source Forge is, I strongly suggest looking it up on Wikipedia.com right now! ^_^) In any case, you can go to that page to view and download the source code for ScrollBack, or, if you have a user account, make comments. There is also a wiki and an issue tracker, but I'm not sure how much I will use those.

I am open to comments about this project, but I am not currently looking for project members from the general public. If I happen to know you personally and feel I can trust you to lend a hand, I may give you permission to commit to the repo, but don't get your hopes up.

When winter break ends (January 20th) I will probably become very slow with updates to the code, but don't assume the project has died when that happens. Right now, this is my biggest and most loved project, so I seriously doubt I will drop it anytime soon. Nonetheless, I will soon come to a point where I may have to postpone all development on it for a long while, and I will be very sad. :(

Labels: , , , ,

1/5/09

Django is Delayed?

Getting Django up may take longer than I thoght.

After spending a while in #python I became convinced that using mod_python would definitely be a mistake unless it were absolutely the only option. The preferred alternative is to use mod_wsgi instead. Unfortunately, my host makes no mention of support for mod_wsgi, and I'm too broke to switch to a host that does (besides that, I'm really tired of host-hopping).

I'm consulted my host about the problem and am still awaiting a reply. With any luck they may be able to get mod_wsgi up and running for my account. If not, I guess I'll just have to suck it up and use mod_python, or POSSIBLY go with a non-pythonic solution. *sigh*

Labels: , , , , ,

12/17/08

Django Is Coming

After a couple of hours of work I managed to get a working installation of Django up and running. You can see it here:

http://alphaios.net/beta/

As you can see, it's far from being anything special, but it means I can finally use my favorite scripting language to code my site. I hope I can soon merge this blog, my other blog, all the various projects and sections of my website, and whatever else into this new Python-powered system and then at last say goodbye to plain-text HTML as well as the limitations of PHP...yuck.