Tidbits @ Kassemi

A collection of opinions, thoughts, tricks and misc. information.

Thursday, December 15, 2005


Getting more interested in Python

What's up everyone? I've been reading up a little more on Python... I knew enough to quickly debug runtime errors in python stuff that I downloaded, but never really got into programming with the language. When I decided that I didn't like bash scripting, and needed to get a job done, I turned to CLI PHP... Which is definitely not fun to program with.

I love PHP for web scripting. It's extremely simple to get a quick site off of the ground, and comes available with almost every hosting plan you'd ever look into, but let's face it, PHP is NOT ready for more traditional, single-computer based languages. I read up a little on PHP GTK, which looked interesting, but a little too complicated for my taste. And, by the way, why do I want to rely on a language that's just recently gained good CLI capabilities... I want a full-featured language that I can use for anything. And that's what got me interested enough in python to put some effort into learning it.

So I picked up the Python in a Nutshell book, and started reading. This really is a nice little language. I've written some very simple scripts thus far... This one, my most recent, shows my lack of understanding thus far, but works:

#! /usr/bin/python

# Simple script for raising, lowering, or directly setting the processor throttling state...
# Takes a single argument, either n, an integer smaller than max_state and larger than min_state,
# or +, which increments the state, or -, which decrements the state.

import os,sys

throttling_file = "/proc/acpi/processor/CPU1/throttling"
max_state = 7
min_state = 0

def changestate(state="0"):
global throttling_file,max_state,min_state

def getcurrentstate():
"Get the processor's current throttle state... "

file = open(throttling_file, "r")
while 1:
str = file.readline()
if str == "": return None
if str.find("*") > -1:
start = str.find("*") + 2
level = str[start:start+1]
return int(level)
return None

if state == "+":
state = str(getcurrentstate() + 1)
elif state == "-":
state = str(getcurrentstate() -1)

if int(state) > max_state or int(state) < min_state:
print "Cannot set specified state. State remains at %i" % getcurrentstate()
print "Setting processor throttling to level %s" % state
os.system("echo -n %s > %s" % (state,throttling_file))

if len(sys.argv) <> 2:
print "Setting processor throttling to default, state 0. Only call this script with one argument."
state = "0"
state = sys.argv[1]


All it does is set my processor's throttling... I can pass a throttling level, or a + / -, and it will adjust the processor level accordingly... I could add quite a bit more, but it does what I need. I've bound it to keyboard shortcuts, and can throttle my processor as I feel fit... Good stuff... I'll be back,


Comments: Post a Comment

<< Home


August 2005   September 2005   October 2005   November 2005   December 2005   January 2006   February 2006   March 2006   April 2006   June 2006   July 2006   August 2006   September 2006   October 2006   November 2006  

This page is powered by Blogger. Isn't yours?