Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Thursday, September 4, 2008

Emacs usecases 1 - Python

Previous in series: 0 - Intro
Next in series: 2 - Blogging

Disclaimer: This is pretty basic and verbose stuff. If you use Emacs to develop Python regularly, take a look at the links below. Most of the rest is probably old news to you.

Update: I forgot to mention that you can run an instance of pdb (Python Debugger) inside Emacs with M-x pdb. It lets you step through the code and keeps the cursor in the code buffer at the current line being executed. You can also use pdb to execute arbitrary Python statements while the program is paused.

There's a recent post on this blog on how VIM is the ultimate Python IDE. I have to say, after two weeks of using Emacs for editing exclusively, that Emacs is just way too feature-rich compared to VIM. There's a dedicated Python major-mode (yup, you guessed it, python-mode), provided in python.el. Then there's the inferior-python-mode, for running the Python interpreter inside Emacs. And of course you still have all the power of Emacs (some of which I probably still don't know about).

The Emacs wiki lists some possible problems, but I found the python-mode distributed with Emacs 22 to work flawlessly. Some other links you may find useful:

python-mode

Indentation in Python is very important, so it's worth noting that Emacs handles Python indentation extremely gracefully. It's worth to note that the tab character means eight spaces to Python. However, python-mode defaults to spaces-only, 4 spaces per level indentation, the Python standard. When you ask Emacs to make a new, indented line, it checks what level of indentation you really need. In most cases that's just the same as on the current line, but lines ending with a colon are indented one level deeper, while a return reduces the depth. Of course you have smart tabs, meaning that deleting an indentation doesn't remove one space, but a whole level of indentation. Additionally the Tab key doesn't increase indentation, and doesn't insert a tab character, but instead cycles the current line through the available indentation levels, showing information about the level in the minibuffer.

There are several useful shortcuts available when editing Python code. You can explore most of them via the menu *shudder* so I'm not going to list them all here. Other than the usual C-j the only one I use regularly is C-c C-c, which compiles the current buffer - evaluates it, to be accurate. It does so in an inferior Python process, which takes us to...

inferior-python-mode

Having the ability to run a Python interpreter inside Emacs is pretty useful in itself - you don't have to switch between windows to test that new piece of code. But it's better than just that. When you evaluate a buffer with C-c C-c (or a region, for that matter), it gets sent straight to the inferior Python process. You can directly access everything in the evaluated file in the interpreter. That's great for testing new functionality.
When things break, they break gracefully: if there's a problem during the evaluation, you get the same messages you would get when running the code. If you want hyperlinks to the position of the error, you can compile with mode-compile (see the intro for setting it up, my-generic.el in particular).

That's it for today. Watch this space for an Emacs + LaTeX post soon :)

Thursday, July 24, 2008

And the ultimate Python IDE is... VIM?

I just came across a great post detailing how to make VIM even better suited for writing Python at sontek

New commands

Thew new keystrokes and commands provided by the tweaks in the above post:
  • Ctrl+LeftArrow, Ctrl+RightArrow: Jump to the method/property under the cursor in the system libraries and back (after making and loading the tags file)
  • Ctrl+Space: code completion
  • <Leader>pw and <Leader>pW: Show the pydoc for the expression under the cursor (default <Leader> is '\') after installing pydoc.vim
  • ALT+LeftArrow, ALT+RightArrow: move between open tabs
  • F7, Shift+F7: Add/remove breakpoint.
  • !python % (% is the current file, it can be replaced with another filename)

Filesystem encoding

Intro

I recently had to make a Python application parse some command-line arguments. No problem, sys.argv, right? Well, kind of. One of the arguments is a file name. Which is fine as long as it's pure ASCII. Or in the encoding Python expects. However, that's not always the case. I have a Hungarian Windows XP for running py2exe, and while testing I found that my app dies with a 'ASCII codec couldn't decode...' kind of error when there are non-ASCII characters passed in as arguments. Unfortunately that's exactly what happens when one opens a file associated to the application (and there are special characters in the name.)

Solution

The point is that we have to somehow turn the filename into something Python can handle. It's documented alright, but took me some time to find. The function we want:

sys.getfilesystemencoding()
This, according to pydoc, returns the encoding used to convert Unicode filenames in operating system filenames. See Effbot for some more details.

Monday, July 7, 2008

[HUN] Toldalékoló program

The topic of this post is a library addressing UI problems arising from the Hungarian grammar system, so I find it appropriate to write the post in Hungarian. Sorry :)

Felhasználói felületek létrehozásakor gyakran előforduló probléma a toldalékok, sőt a helyes névelő eldöntése - tulajdonképpen tényleg egyszerűbb 'a(z)'-t írni, és még senki sem panaszkodott. Ez szerintem mégis probléma. Ezért kezdtem el írni egy kis csomagot, ami megoldja ezt a problémát (természetesen Pythonban :) ).
A határozóragokat a tesztjeim alapján jól kezeli. Persze vannak hiányosságok:

  • Az összetett szavakat (természetesen) egy szónak tekinti.
  • A tárgyrag régi szavaknál nem követi a hagyományos használatot (pl. hót, nem havat)
  • A többesszám gyakorlatilag használhatatlan

Link rá a címben. Van benne egy teszt is, ami egyébként bemutatja a használatát. Új toldalékok hozzáadása triviális.
A library szabadon felhasználható bármilyen célra (amire alkalmas). Ha hasznosnak találod, szívesen vennék egy kommentet/levelet :)

Saturday, June 21, 2008

Students Dictionary

I recently finished rewriting my little dictionary app, AbDict. It's one of those programs that let you create your own dictionaries then test if you know the words. Quite useful when learning new languages. Link in the title.

Features (?)

This application is quite basic. You can create your own dictionary and name the languages whatever you want - thinking about it, you could use it for learning historical dates or anything similar.

You can of course add words - the program checks whether they're already in the dictionary. One word can have several translations - you can create one-to-one, one-to-many and many-to-many relationships. A word can be part of any number of such lists (for the tech guys: each list is treated as a separate entity - this is not a reference-based wordlist, maybe in a later version, if there's demand).

The testing part is the soul of such a program, and I think this one has quite a nice soul. You can tell it how many times to ask new words and words you missed. There's also a "strict testing" option: this means that when there's a list of meanings for a word, the answer is only accepted if you write all the translations. They must be separated by the word separator character, which you can also set from a list. Of course, if any of them is wrong, the whole answer is considered wrong.

Also, the application itself is internationalized, meaning that the interface can be translated. Right now it includes an English and a Hungarian translation, but it's really simple to add new ones (it uses wxPython for this, which uses the gettext fileformat, in case you're interested).

AbDict started out as a project for learning Python, but I found it useful enough to rewrite properly. It's now at version 0.2, and all bugs I found in it are squashed. It's free software, licensed under GPL2. I hope you find it useful.

Comments, suggestions, bug reports and translations are more than welcome.