You searched for articles tagged with Python.

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!




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.




Using Perl Interactively Permalink

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

It's nice to be able to type:

$ python
Python 2.5.2 (r252:60911, Mar  1 2008, 13:52:45) 
[GCC 4.2.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 3 + 9
12
>>>

But what about Perl?

$ perl -de 0

Loading DB routines from perl5db.pl version 1.28
Editor support available.

Enter h or `h h' for help, or `man perldebug' for more help.

main::(-e:1):   0
  DB<1> print 6 + 18
24
  DB<2>

This opens the debugger for the expression '0' and then lets you evaluate perl expressions line-by-line like the interactive python (or ruby (irb)) sessions. It works OK. Unfortunately for multi-liners you need to put a backslash at the end of each line, like so:

  DB<4> if (1) {\
  cont:    print "OK\n";\
  cont: }
OK

An alternative is to use this gentleman's psh script: http://uazu.net/freeware/psh/




Python Pet Peeves Permalink

Python, Rant Added more than a year ago and last edited more than a year ago

The current Python (I do not know about Py3000) has some annoying features (annoying for me, not for you - you love them.)

How's this for counter-intuitive:

>>> a = ['f', 'a', 'i', 'l']
>>> a.join("")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'
>>> "".join(a)
'fail'

... the join method, to me, seems like it's in the wrong area. If I have an array (list), I want to join it together with a string. I don't want to get a string, then join it to an array.

More counter-intuitiveness:

>>> import re
>>> pat = re.compile('^\w\d+$')
>>> s = 'b45343'
>>> s.match(pat)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'str' object has no attribute 'match'
>>> pat.match(s)
<_sre.SRE_Match object at 0xb7c6e598>

... when programming and I have a string, I want to see if the string matches a pattern. I don't want to get a pattern and see if a string matches it. My suggestion could involve regex stuff being built into the string library - or not, depending on implementation. Either way is fine.

How about pypi, the place where Python packages live. Say I want a lib to do SSH, and don't know what the package is called. I ask pypi and I get a list including:

Contrast this to searching CPAN which is the place where Perl's libraries live. I get this:

CPAN is clearer.

Here's another thing: you can't do assignments in conditional tests. Observe:

>>> if (a = (1 + 1)): print "yay"
  File "<stdin>", line 1
    if (a = (1 + 1)): print "yay"
          ^
SyntaxError: invalid syntax

... compare to Perl:

$ perl -e 'if ($a = (1 + 1)) { print "yay\n" }'
yay



Blog Todo Permalink

Django, Python Added more than a year ago and last edited more than a year ago



(Nothing Older) ... (Nothing Newer)

Colophon

Django Python 960.gs Git Vim NetBSD Nginx

The Author

The author is a software engineer living in Australia. He sux at guitar, loves camping, doesn't like cake, does like coffee and is a lazy home brewer.

Meta

Help
Latest entries

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

Recent Entries

Perldoc Output
Yum
Possum
Git's Index
Jira Project Keys
The Coffee Shop
Git GUI
It is more important...
Questioning Unix (and Other) File Times
The Frog King Photo
Rain Cloud Photo
rsync
Timezone
utf8 in your Perl
Theatre Ceiling Photo
Some problems are so complex...
Colours in your PAGER
zsh vared
zsh magic-equals and double-star
Funny Tweets

Links

ChoppingBoard, Project365, RageQuit

♥ Actors/Artists/Characters