Sunday, April 26, 2009

School is nearly over!

Well, I'm now on study leave for a week before my exams start. By the end of May I'll have graduated high school. So if the next update is a little delayed (day or two maybe) you guys know why.

These days I've been bothered by my temperamental laptop again. The charger is starting to die (it's the THIRD charger for this HP DV6545eg laptop in the last year), and I'm tired of replacing it, so I've decided to replace the laptop after I graduate. I've decided this mainly because the battery life of the HP laptop is terrible (about and hour and a half, maybe a bit longer), and the hard drive seems to be starting to die (I give it another month or two before it bites the dust). Seeing as the total cost of replacements would be around about 200€ I decided I may as well buy a new laptop for twice as much and get better battery life and mobility (my laptop is 15.4"). I've decided I want to get the Samsung NC10 netbook, which has a rumoured battery life of up to 7 hours (even if it only gets to 4 or 5 hours I'd be ecstatic), and is 10.2", which means it'll be nice and portable for me. Not sure if I'll go with Ubuntu 9.04 netbook edition, ArchLinux, or a normal install of Ubuntu. If anyone has any comments on compatibility (according to Ubuntu wiki only suspend/resume and sound recording doesn't work), please post a comment.

On a bit of an ironic note...the Samsung NC10 has an integrated graphics card with more shared memory than my laptop. Current laptop: Nvidia 7150m chipset with 128MB shared memory, NC10: Intel GMA 950 with max. 384 MB SharedMemory. Sure, the NC10 only has 1GB of RAM, but I can easily increase that, though my current PC and laptop set ups only require about 300MB of RAM for everything I do. Actually...looking at these specifications...the NC10 has bluetooth and all that, seems it is a better deal than my laptop (for half the price too). How times change.

I'm also hoping to get another Acer x223w monitor (22") for my PC, which will require yet another re-organizing of my desk.

The latest program I'm working on is a command-line tool for googling using python. So far the only thing it does is take supplied search terms and open the google results in firefox, since the SOAP API from google was dropped, and I've not got the time yet to try to implement the AJAX API in some way or form. I'm doing this mainly for practice so that I can get some hands-on experience with formatting large chunks of input in python, and so I can work with urls a bit.

Also, I noticed something interesting this morning. I have a follower! First one too, it was a bit of a surprise. I thought to myself "has that function always been there?". It's nice to know someone reads these articles (besides people I know personally).

One final note: I'm open to any suggestions if anyone would like a how-to written on a specific linux problem, or anything like that. Just drop a comment with your request and I will do my best to write a how-to on it.

Sunday, April 12, 2009

Some Python scripts

Well, as I mentioned in the last post I made, I plan on updating this blog every other Sunday (besides when I'm on vacation). This is the first of that series of updates (on time too!). I've been using Python a lot for small scripts lately, because a) it's good practice, and b) it's efficient. Below are a few scripts I wrote and an explanation of what they do:

Sorry, but the whitespaces seem to be ignored, so I'll post a link to a pastebin for each one as well. (For those who don't know, whitespace is significant in Python since each indentation level denotes a "block" of code, similar to the function of braces "{}" in Java or most other programming languages).

FileCondenser
http://lswest.pastebin.com/f21a592a4
#!/usr/bin/env python
#Script to condense the multiple files of a project into one for easy printing/copying
#Author: lswest
import os
import optparse

def main():
usage="usage: %prog [options] args"
p = optparse.OptionParser()
p.add_option('--origin', '-o', help="The location of the files to be read in.", default="")
p.add_option('--output', '-t', help="The location of the file to which the condensed output will be written.", default="");
p.add_option('--extension', '-e', help="The extension of the files to condense.", default="");
options, arguments = p.parse_args()
if options.origin == "" or options.output == "" or options.extension == "":
p.print_help()
else:
print "Input file: %(o)s*%(e)s \nOutput file: %(t)s" % {'o' : options.origin, 't' : options.output, 'e' : options.extension}
ff=open(os.path.join(options.output), "wt")
for root, dirs, files in os.walk(os.path.join(options.origin), "true", "none", "true"):
for infile in [f for f in files if f.endswith(options.extension)]:
fh=open(os.path.abspath(os.path.join(root,infile)))
for line in fh:
ff.write(line,)
fh.close()
ff.close()

if __name__ == '__main__':
main()
This is a small script that can be run using the arguments ("FileCondenser --help" to see the help information), and is used to take all the files within a directory tree ending in the specified extension, and then copy each line of those files into the output file. I used it for my Computer Science coursework, since I had to print a hard-copy of my code (weird, I know).

LineCounter
http://lswest.pastebin.com/f6c52aaa0
#!/usr/bin/env python
#Program to tally the lines in a file
#Author: lswest
import os
import os
import optparse

def main():
usage="usage: %prog [options] args"
p = optparse.OptionParser()
p.add_option('--file', '-f', help="The path to the file to count.", default="")
options, arguments = p.parse_args()
if options.file == "":
p.print_help()
else:
print "Input file: %s" % options.file
count=0
ff=open(os.path.join(options.file))
for x in ff:
count+=1
values={'name': os.path.join(options.file), 'count' : count}
print "The file %(name)s contains %(count)s lines." % values

if __name__ == '__main__':
main()
This script basically just tallies the number of lines within the specified files, and was done for practice (no real practical reason), but I still thought I'd include it.

updateCheck
http://lswest.pastebin.com/f4d33eb3d
#!/usr/bin/env python
#A program to check if there are any updates available for Arch
#Author: lswest
from subprocess import Popen,PIPE
import os

def main():
p=Popen("pacman -Qu|grep Targets|cut --delimiter=\" \" -f 2|sed -e 's/(//' -e 's/)://'",shell=True,stdout=PIPE)
x=p.stdout.read()
if x != "":
tally=int(x)
if tally == "1":
print "1 package to update"
else:
print "%s packages to update" % str(tally)
else:
print "No packages to update"

if __name__ == '__main__':
main()
I use this script in my Conky on both my PC and laptop, in combination with a cronjob of "pacman -Sy" to update the database, to display the number of available updates for my system.

To Buy.py
http://lswest.pastebin.com/f4b4c9849
#!/usr/bin/env python
## A script to print out my "to buy" list
#Author: lswest
import os

home=os.path.expanduser("~")

for root, dirs, files in os.walk(os.path.join(home,"To Buy")):
for infile in [f for f in files]:
if(infile.endswith("~")!=True):
fh=open(os.path.abspath(os.path.join(root,infile)))
for line in fh:
print "- "+line,
fh.close()
This is a small script that takes all the text files in my To Buy folder, and prints it out, which I use in Conky. I create the text files with just echo "something to buy" > To\ Buy/something.

TO DO.py
http://lswest.pastebin.com/f18550d93
#!/usr/bin/env python
#A script to print out my To Do list
#Author: lswest
import os

home=os.path.expanduser("~")

for root, dirs, files in os.walk(os.path.join(home,"Reminders")):
for infile in [f for f in files]:
if(infile.endswith("~")!=True):
fh=open(os.path.abspath(os.path.join(root,infile)))
for line in fh:
print "- "+line,
fh.close()
Basically the same as above, besides the fact that it's a to-do list.

Well, that's all the scripts I've written so far that are half-way useful. I'm thinking about making a github repository for scripts of mine, so if you think it's a good idea, drop a comment and I'll post an update on the blog with the link if I do it.

*edit* I went ahead and made the github repository: http://github.com/lswest/scripts/tree/master

Saturday, April 4, 2009

An update (very delayed, sorry!)

Sorry about the slow updates on my blog here, I've been kinda swamped with school work, writing articles for Full Circle, and such that I haven't been able to think of things to write when I did have time (or I just simply didn't have time). I am now, however, setting aside every other Sunday of the month (second and last sunday of each month) to updating this blog (short of when I have exams, or when I'm off on Summer vacation, but I'll leave a notice as to how long I'll be gone). Over Summer vacation I'll probably be writing a lot, and so I'll just be unable to post them until I return.

A few things I'm working on these days:
- A website about Linux along with a wordpress blog, and once it's finished I may find hosting for it and use that instead, but I will see how it turns out first.
- Learning Python thoroughly
- Studying for exams
- Planning my gap year out
- Writing for Full Circle

Changes to my computers:
- Both PC and Laptop are now running ArchLinux 64bit with Awesome 3.1
- PC will soon be running with 4GB of RAM

Things I plan on doing:
- LPIC-1 during my gap year
- Getting a second 22" monitor for my PC
- Getting a graphics tablet for my PC

Favourite programs these days:
- MOC (command-line music player)
- My ipod Touch (for checking emails and such without having to pull out my old clunky laptop)
- Conky (I added a to-do list and a shopping list to it using python and an assortment of text files)

That's all I can think of posting for this time. Anyone is free to post suggestions for articles, how-tos, and so forth and I will cover as many as I can. Also, I would like to get a rough estimate of how many people read this blog, so if you feel like just leaving a comment, please do so!
Lswest