Harsh J

Memoirs of a QWERTY Keyboard

Archive for the ‘Blog’ Category

Clearing all log files in /var/log

leave a comment

Stupid things you end up writing when you worry about filling disk spaces at 3 AM in the morning of the day right before your exams begin. The following will delete all your log files in a safe manner, by simply rewriting them to null.

cd /var/log
for file in `find .`
do
	if [ -f $file ];
	then
		cat /dev/null > $file
	fi
done

Suggest better methods if known, and thank you!

Optionally, get in there and delete those now-stagnant .gz files with the following:

find /var/log -name "*.gz" | xargs rm

P.s. So much for BS code highlight plugins printing angled brackets in HTML notations when asked to do bash.

Written by Harsh

May 19th, 2009 at 3:31 am

Posted in Blog,Linux,Personal

Tagged with , ,

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

Twitter or This post had to come sooner or later

6 comments

@having fun with microblogging, status-blogging, whatever twitter is.

Though am not totally addicted to it, its good to post short thoughts or replies at Twitter. I still find the place a bit weird to browse through but I’m beginning to get a hang of it, unfortunately not while I’m mobile. I think that’s the best point of Twitter, a scribble pad to post upon as you travel.

Twitter - Cause birds tweet?

Twitter - Cause birds "tweet"?

I’ve had many nice ideas, thoughts and such while travelling but never have I cultivated the so-said good habit of carrying around a small notebook to scribble them upon. I find that quite odd to do, non-wash-proof kind of odd. Twitter, however, with the advance of the mobile devices into every man’s pockets, promises to help cultivating the same habit in a digital way. I need to setup the texting feature and give it a spin, and also try some gprs methods while am at it.

On my desktop I use Ping.fm for “tweeting”, as they like to call it, and I use it along with Bitlbee. Nice integration into one single irssi IRC interface. Ok I got a bit off the topic there, admitted. I’m turning quite the jargon guy here I notice, but I can’t really help changing that back, but yes I can dumb stuff down a bit maybe.

So, more tiny updates at my Twitter account – QwertyManiac

Written by Harsh

August 21st, 2008 at 10:28 pm

Open Source Games

one comment

I’m sure there has been a lot of incidents where you have cursed your most recently acquired game, be it anything from a silly to annoying bug, the lack of a feature that would have made playing more heavenly for you or even something as small as better detail on some object on screen.

Now what if the game were open source, to the fullest extent rather than just the “Extend”. You don’t like the character’s tail, you contribute an idea in form of a feature-request or by getting into the development yourself. So do million others. Together you can, over time, form a completely different game than the one you were being unsatisfied with!

Yo Frankie! - An open source game in active development.

Yo Frankie! - An open source game in active development.

The usual way of extending a game would be to add in map and skin packs while letting the functionality of the entire game remain constant. What if the game evolves with time, adding in new moves, more pick-ups and extras, and more elements to the game, such as weapons or collectibles?

Though these are present in some of the games alive today, it would be greater if the game were or went open-source, opening the game’s code to a world of never-before imagined possibilities and also getting in tons of optimization over time. Of course, building a game big enough like the heavyweights we have today requires the work of a large active team behind it, but once done with its release, getting in additions to the code would we amazingly easy were it supported by a good version-tracking system!

Playing such a game would mean endless hours of fun, repeated every couple of months for a completely different experience while still remaining the old charms that a player liked. New innovative concepts implemented by avid code-enabled gamers would add in more and more goodies, much like how Compiz-Fusion is today, an idea being born a day and implemented almost as fast.

Yes there would be a point where it would hit a roadblock and this is where the next game development has to begin. Ideas found in the older version could be tried out across a multitude of other genres, and what a learning experience would it be for creating that “ultimate” game many always dreamed about!

I hope some biggie in gaming gets onto this OSS bandwagon, with atleast a half-baked title cracked open, and pretty soon, since the game market is losing out on ideas really, and thats cause ideas are limited to them in the closed confines they live in.

You’d say what of the OSS games we have today, and I’d point at their evergrowing success till date. No game has ever been given a cold shoulder and kicked out of existence, right from Nethack to Nezuiz. Though this post was inspired by a particular game known as Yo Frankie!.

Written by Harsh

July 17th, 2008 at 7:00 pm

Managing Tag and Tags

3 comments

This new Tags feature in WordPress 2.3 is confusing me. While Categories are linked as URL/tag/name, the tags are linked as URL/tags/name. And it makes NO difference at all. :?

What is it all about anyway?

Written by Harsh

November 28th, 2007 at 11:30 pm

Posted in Blog,Personal

Tagged with , ,