You searched for articles tagged with Python.

[ #241 ] Checking for exceptions in doctests Permalink

Python Added a day ago

You use the '...' magic:

http://stackoverflow.com/questions/12592/can-you-check-that-an-exception-is-thrown-with-doctest-in-python

Cached here:

>>> x
Traceback (most recent call last):
  ...
NameError: name 'x' is not defined



[ #238 ] retry in Python Permalink

Python Added a few days ago

If you want to wrap some code in a try/except block in Python, but you want it to be tried number of times before finally raising an exception, you can do something like this:

maxtries = 5
trynumber = 1
while 1:
    try:
        # your code here
    except WhateverException:
        if trynumber >= maxtries:
            # report your error
            raise SomeException
        # maybe sleep here
        trynumber += 1

This can be used for transient errors like network stuff...

This implements Ruby's begin/rescue/retry block (more messily of course.)




[ #234 ] Vim comment hints Permalink

Vim, Python Added about three weeks ago

If you need vim to interpret a file in a specific way (or set some values for a specific file only), you can put comment hints in the file.

For example, if you have a Python file that doesn't have a .py extension and also doesn't have a #! line indicating that it is indeed Python, then vim will not turn on the correct syntax highlighting for your file (because it thinks it is just plain text.) To force Python syntax highlighting, place a comment hint at the bottom of your file like this:

def eggs():
    pass
# vim: syntax=python



[ #232 ] Dish rotation Permalink

Python Added about a month ago

James over at Prog21 has an article on calculating the shortest rotation (in degrees) for a satellite dish - his point being that there is a case or two that he didn't immediately think about (i.e. that it's harder than it sounds.)

Here is my Python version of his function (with doc tests):

def angle_diff(begin, end):
    """
    >>> angle_diff(0, 50)
    50
    >>> angle_diff(37, 38)
    1
    >>> angle_diff(200, 10)
    170
    >>> angle_diff(50, 20)
    -30
    >>> angle_diff(0, 270)
    -90
    >>> angle_diff(270, 0)
    90
    >>> angle_diff(100, 100)
    0
    >>> angle_diff(0, 0)
    0
    """
    easyway = end - begin
    if abs(easyway) > 180:
        return (360 - abs(easyway)) * cmp(0, easyway)
        # get the shorter way ^
        #           in the opposite direction ^
    return easyway

Testing:

$ nosetests --with-doctest ad.py 
.
----------------------------------------------------------------------
Ran 1 test in 0.003s

OK



[ #225 ] Python - relative paths from within modules Permalink

Python Added about two months ago and last edited about a month ago

If your hierarchy looks like this:

script.py
data/file.txt
pythonlib/module.py

... and you want to refer to data/file.txt from within module.py, the best way of course is to pass around a basepath variable when you instantiate module, so that its methods can append file paths onto that base. However, if for some reason you needed to use a relative path within module.py, know that the path is relative to the calling script (script.py in our case) - not the module location.

Another option to refer to data/file.txt from within pythonlib/module.py is to use:

import os
os.path.dirname(__file__) + os.path.join('..', 'data', 'file.txt')



[ #213 ] Python - testing for a sys.exit Permalink

Python, Testing Added less than a year ago

Whenever a function of yours calls:

sys.exit(1)

... a SystemExit exception is raised. This can be tested for in the usual way:

assertRaises(SystemExit, yourfunction, arg1)



[ #212 ] Python Best Practice Link Dump Permalink

Python Added less than a year ago

http://eikke.com/how-not-to-write-python-code/

http://bayes.colorado.edu/PythonGuidelines.html

http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy

http://docs.python.org/py3k/howto/doanddont.html

http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html

http://www.python.org/dev/peps/pep-0008/

... and use pychecker and nose




[ #211 ] Python script names Permalink

Python Added less than a year ago

Don't put dashes in your script names. When you go to test and you have dashes, you will find that:

import your-script # fails

... because the parser thinks it's doing this: import 'your', subtract 'script'. You can get around it by doing this:

__import__('your-script')

... but the best advice is to forget about dashes.




[ #164 ] Blurring the Line between Modules and Scripts Permalink

Perl, Python Added less than a year ago and last edited less than a year ago

Perl modules are named .pm and Perl scripts are .pl. You are supposed to keep your packages in your .pm and your scripts that you run from the command line in your .pl. However - you can create a (number of) package(s) within a .pl... and you can also run a .pm from the command line.

If you use a module then your module must be a .pm. If you require a module it can have any extension.

You could also have a script that is simply named yourapp without any file extension, which is useful for when you want to include your modules and script in the one file - presumably for easier distribution.

Keeping your modules as .pms and your scripts as .pls is nice for clarity - so people know by looking at a directory listing which file is which.

I've found that I sometimes want to treat one like the other, so that's what today's trick is all about.

First some background about how Python handles this.

Python files are just .pys, and if they contain a class or functions but also should be run by themselves like a script, then this idiom is used:

class Yours:
    def something:
        ... code ...

def main:
    ... code ...

if __name__ == "__main__":
    main()

We can emulate this in Perl like so:

package Yours;

sub something {
    ... code ...
}

package main;

sub main {
    ... code ...
}

&main if ! caller();

1;

This file could have any extension. If it is run as a script then main will be run - but if it is used or required then it won't be!




[ #124 ] Booleans and Boolean Expressions... Permalink

Perl, Python, Ruby Added less than a year ago

It's funny how the little, basic things that you re-examine when you are trying to teach someone else a programming language can surprise you. I thought I knew how these different languages handled this topic but I was wrong. Here we go:

I want to show you some differences between Perl, Python and Ruby (the three of which I consider to be the main "scripting" languages at the moment) with respect to their handling of booleans and boolean expressions. I was taken by surprise with some of this behaviour, as I tend to mainly use booleans as the results of conditional expressions - and fairly explicitly when I do.

Perl

No boolean type.

$ perl -e 'if (100 > 1) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if (100) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if (1) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if (0) { print "true\n" } else { print "false\n" }'
false
$ perl -e 'if (-1) { print "true\n" } else { print "false\n" }'
true
$ perl -e 'if ("") { print "true\n" } else { print "false\n" }'
false
$ perl -e 'if ("hello") { print "true\n" } else { print "false\n" }'
true

Python

Has a specific boolean type.

>>> x = False

$ python -c 'print ("true" if 100 > 1 else "false")'
true
$ python -c 'print ("true" if 100 else "false")'
true
$ python -c 'print ("true" if 1 else "false")'
true
$ python -c 'print ("true" if 0 else "false")'
false
$ python -c 'print ("true" if -1 else "false")'
true
$ python -c 'print ("true" if "" else "false")'
false
$ python -c 'print ("true" if "hello" else "false")'
true

... ok so it's the same as Perl at this point, but note these following boolean comparisons and compare to the upcoming Ruby code:

>>> 0 == False
True
>>> 1 == True
True
>>> 100 == True
False
>>> -1 == True
False

Ruby

$ ruby -e 'if (100 > 1) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (100) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (1) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (0) then puts "true"; else puts "false"; end'
true
$ ruby -e 'if (-1) then puts "true"; else puts "false"; end'
true
$ ruby -e 'a = ""; if (a) then puts "true"; else puts "false"; end'
true
$ ruby -e 'a = "hello"; if (a) then puts "true"; else puts "false"; end'
true

... ok ... that's fairly different. That's because Ruby only counts something as false if it is specifically equal to 'false' or 'nil'. Interesting. What about boolean comparisons that aren't wrapped in conditional expressions?

(Python uses 'True' and 'False', Ruby uses 'true' and 'false')

irb(main):001:0> 0 == false
=> false
irb(main):002:0> 1 == true
=> false
irb(main):003:0> 100 == true
=> false
irb(main):004:0> -1 == true
=> false

That is considerably different to Python, right?

What do we know?

OK so this post wasn't exactly straightforward, but it shows how major languages can treat something as simple as booleans so differently.




Older Posts ... (Nothing Newer)

Colophon

Django Python 960.gs Git Vim NetBSD Nginx

The Author

This is the blog of Brad Willis, a software engineer living in Brisbane.

Meta

Help
Latest entries

*BSD Agile Apache Apple apt Athletics Best-Practice Censorship Comedy Cool Crosswords Deployment Django English Exim Firefox Git Hardcore Health irssi Javascript Jira Languages Linux Makefile Mathematics Mobile Broadband Mutt MySQL NetBSD nginx Nokia OpenVZ OSX Perl Privacy Python Rant Requirements rsync Ruby Shell Slackware SQL SQLite SSH Standards Subversion Television Testing ThisBlog Vim VMWare (Fusion) VPN X zsh

Recent Entries

Checking for exceptions in doctests
Homer's Curling Speech
retry in Python
Vim Makefile tabs
Centos (or RH) IPTables
Converting ssh2 public keys to openssh
Vim comment hints
Context managers in Perl
Dish rotation
Git - fixing commit user
apt stuff
Using shell variables in AWK
Linux - Too many open files
Tell gvim to save and quit... remotely
Vim - automatically remove whitespace at EOL
Python - relative paths from within modules
TV Aspect Ratios
Git - Which commits are in your branch only?
Subversion setup cheat sheet
Force detach a screen session
Modify sudo's use of environment variables
Install all Perl modules
Mutt - delete old messages
OpenVZ VPS and swap space
fail2ban on NetBSD for ssh
NetBSD - Using sup
Python - testing for a sys.exit
Python Best Practice Link Dump
Python script names
Perl - Using an expensive module
Speed of git clone
Perl Modules with Custom Prefix
Perl: tr vs. s
Brilliant sysadmin Reference
Why is GRUB better than LILO?
Why is swap space important?
Perldoc Output
Git's Index
Jira Project Keys
Git GUI

Links

ChoppingBoard, DaveMisc, Project365, RageQuit