Posts Tagged ‘tips’

Python common mistakes

Saturday, May 23rd, 2009

When starting with Python or just when you haven’t read PEP 8 you usually fall into some frequent mistakes. Mostly because you take your experience in other languages to Python and your mind gets confused with the new conventions (or something like that).

Some of these errors and their explanation:

  • if a == None:
    Really common. None is a singleton, so you can (well, actually, have) to compare to it like ‘if a is None‘ or ‘if a is not None‘.
  • if (condition):
    This is not really pythonic, if doesn’t take () around the condition in python, () purpose is grouping, you can use them in an if but only to clarify a really complex condition or similar stuff. Please don’t clutter code with unneeded characters.
  • if len(list):
    This is a misunderstanding of Python way of things, any empty sequence (string, list, tuple) is False. So if you want to make sure a list is empty, just check if it’s False. Same for tuples, and in some ocassions for strings.
  • if type(obj) is type(1):
    This is killing kittens, there’s a builtin function for this purpose, it’s called -surprise- isinstance(object, type). For example isinstance(123, int)

Read more about this and other conventions in PEP 8.

How to know the return value of a command

Sunday, May 18th, 2008

Just do:

$ echo $?

and that’s it.

Thinkpad R50e suspend and resume

Thursday, May 15th, 2008

So I have a loyal R50e, thing is that resuming from suspend breaks graphics, I had that working back in the days but recent upgrades of drivers and stuff broke it for some months, I didn’t care to fix it since I didn’t have battery anyway.

But now I fixed it again, trick is to edit /usr/share/acpi-support/IBM.config:

# R50e
1834*|1842*|2670*)
ACPI_SLEEP=true;
SAVE_VIDEO_PCI_STATE=true;
# toggled false ->; true
SAVE_VBE_STATE=true;
POST_VIDEO=true;
;;

There! You must make those two vars be true. Maybe some tweaking to /etc/defaults/acpi-support would be needed. Most of time this should be all you need to do.