Harsh J

Memoirs of a QWERTY Keyboard

Archive for the ‘python’ tag

The new print function in Python 3

5 comments

The print statement has gone. A function replaces it in Python 3.0. Apart from the other changes, this one probably needs the most mind and finger retraining, cause its something I use a lot while fiddling with the interpreter.

So far, till Python 2.6

So this is how you all have been happily typing away in Python 2.x:

print "Print this and print a newline"
print "Print this, but not a newline",

Now, if you had a collection of elements you wanted to print with certain formatting, say with punctuation, here’s how you would do it:

basket = ('Apple', 'Oranges', 'Banana')
print ", ".join(basket) + "."

# Which produces the output:
"Apple, Oranges, Banana."
About the print() function in Python 3.0

About the print() function in Python 3.0

Some would even prefer to use a logical loop instead, for readability’s sake. But this method does require a string operation to happen, and loops probably would take up lines, or worse, in certain cases.

Now, in Python 3.0 onwards

The print statement has gone for good and the print() function comes in.

This decision is well explained in the PEP (Python Enhancement Proposals) and the specific paper on it can be found here (Numbered 3105)

Digging into this function’s documentation would reveal all about it in simple text (If you are an avid reader of the Python Documentation). Of what’s continued here is all just for newbies-only, with some demonstrations.

A simple demo equivalent to the first as above:

print ("Print this line, and print a newline")
print ("Print this line, but not a newline", end="")

You might notice that its got a little complex here, with a keyword argument being supplied instead of an ending comma as before.

But lets see the full power of this new print() function by doing the same punctuation to the fruit basket as before:

basket = ('Apple', 'Oranges', 'Banana')

print (*basket, sep=", ", end=".\n")

# Which produces the same output as desired:

"Apple, Oranges, Banana."

We do the same thing as before, except that we don’t require a loop, nor string operations. The function’s two keyword arguments sep and end handle the complex jobs for us. Basically, this is what they mean:

  • sep – Seperator string – Defines the string that is to be placed between every two values printed.
  • end – Ender string – Defines the string to be printed at the end of the print function.

By default, sep has a space (‘ ‘) and end is a newline (‘ \n ‘). So a simple signature of this new print function would be like:

print ( [object(s)], sep=' ', end='\n' )

And finally, the Star of this show

The one last addition to the print function made by the Python team was the file keyword argument. That’s right, one more keyword argument.

  • file – Object Name – Specify file/object to print to.

This one is a really cool addition, and it defaults to sys.stdout, (i.e.) your terminal. Thus, a more complete print() signature is:

print ( [object(s)], sep=' ', end='\n' , file=sys.stdout)
# No, you obviously don't have to import sys for this.
# Its just to describe what file its printing to.
# (/dev/stdout in UNIX's case)

All the file argument needs is an object that supports any write(string) method.

Now lets try printing the fruit basket to a file than to the terminal as default:

fruits = open("fruitsfile.txt", "w")
basket = ('Apple', 'Oranges', 'Banana')
print (*basket, sep=", ", end=".\n", file=fruits)

# Effectively prints the punctuated line,
# to the file named fruitsfile.txt

This could have a great use, especially with logging! And since it supports any object with a write method, the possibilities could be endless.

Now tell me if you still hate that its got parentheses? Look at the power of this new function!

Written by Harsh

December 9th, 2008 at 11:06 pm

Random stuff yet again

2 comments

Discovered that PowerISO has a free Linux CLI tool for opening .DAA (Direct Access Archive) files and allowing conversion to other formats as well. Can be downloaded and installed from the .tar.gz package available at their website here or click here for a direct download! :)

College’s been pretty hectic, or I instead have been very lazy since my last blog post (27th January). :P

Other stuff am trying these days are problems, both old and new, from IIT-K’s Bitwise (Of course, in Python!). I did try the still-alive Krypton but I got fed up of it at the 6th level, lost my touch I guess. :evil:

Also trying to learn nasm bit by bit (Pun intended) but I can’t be an avid user of it any time soon, its hell mighty! .Net is going into my brains too, as my College’s Lab projects require it. IMO .Net’s pretty good to use, with its heavy abstraction, its very easy to build stuff with it. Strictly NO for efficient applications though. Its a dependency hell (sort of) too but thats okay with me.

Well thats all for now, more on what movies, etc I’ve seen, later! Darn records.

Written by Harsh

February 26th, 2008 at 5:58 pm

Convert CHM files to PDF in Linux

31 comments

I’ve tried a lot of ways to do this thing in Linux (Ubuntu) and finally I found the easiest one to use.

Called simply as ‘chm2pdf‘, this is a small script written in Python and it uses a few dependencies you might have to install first.

Now to install it, simply follow this guide:

1. Install dependencies by running in the Terminal:

sudo apt-get install htmldoc libchm-bin python-chm

2. Download chm2pdf by clicking this.

3. Now extract the downloaded file.

4. Open your Terminal and browse to the folder you extracted and run:

sudo python setup.py install

5. You’re done!

Now to use it is very simple:

In Terminal, type:

chm2pdf --book filename.chm
# (Where filename is obviously your required file to be converted.)

And surprise surprise, a PDF copy will be waiting for you just next to your CHM file!

Written by Harsh

December 6th, 2007 at 12:08 pm

Posted in Linux,Software

Tagged with , , , , ,

The best Regular Expression HOWTO

leave a comment

Yep, the best resource for learning RegEx (Regular Expressions) you will ever find on the Internet would be this one:

Regular Expression HOWTO

Seriously, all I needed was an hour in the evening!

Written by Harsh

November 30th, 2007 at 9:48 am

Look and Say Sequence

7 comments

Have no idea why it took near 2 hours for me to write this. Maybe it was the stupid OOP paper I gave today, I’ve been in a bad mood after writing a 40-pager and yet feeling unsatisfactory about it. :?

So back to the post’s topic: The Look and Say Sequence, which I found today while solving the level 10 of The Python Challenge, is a very odd sequence of numbers which are formed by counting and spelling out the digits sequentially.

The series looks like the following:

1 11 21 1211 111221 and so on …
How this is formed:
See the first digit 1. Spell it as One One (One in quantity that is). This is the general idea.
Thus:
1 – One One: 11
11 – Two One(s): 21
21 – One Two and One One: 1211
1211 – One One, One Two and Two One(s): 111221
And on and on and on …

And now, for the Python Code.

( Took me over 80 minutes with lots of scrapping and frustration to write. Bad day :( )

Written by Harsh

November 29th, 2007 at 1:17 am