Experiencing with iPython

March 5th, 2008 mysurface

What is iPython? Is it something to do with Apple Macintosh? as stylish products like ipod and iphone? If you are enjoy coding python like me, yes ipython is a stylish and very interactive python interpreter. Auto Complete ipython support auto complete, which is the very first reason I use it. With auto complete features, [...]

Posted in Developer, ipython, python | Hits: 64175 | 3 Comments »

Python: How to run a command line within python?

March 4th, 2008 mysurface

I always got this question in my mind. How to run a command line within python, get the output and manipulates it. Before I learn python, I was doing bash scripts all the while to helps me manipulates text which I get it from log files, or pipes out from some certain command line. To [...]

Posted in Developer, python | Hits: 201633 | 15 Comments »

python: convey the exception traceback into log file

February 27th, 2008 mysurface

Python is the interpreter language, you do not need to compile your code, and also you have no ways to check for your syntax error until you run your python script. Either syntax error or runtime error will be throw to standard output through python exception handler by default. Python throw the exception with traceback [...]

Posted in Developer, python | Hits: 49194 | 7 Comments »

Python: Manipulate Date and Time variables

February 17th, 2008 mysurface

When comes to data time related calculations, we usually calculate for time difference, for example How long is the down time for a particular service? How many days or hours are used to finish a task? etc. With python datetime and time class, it makes calculation simple. Let us look at some simple examples of [...]

Posted in Developer, python | Hits: 42877 | 1 Comment »

python: user define sorting with callback

January 8th, 2008 mysurface

Theres a lots of people treated python as scripting language like bash, but I am going to tell you, python is real programming language. Python support callback like c/c++, and this feature is really God-like, because it is so simple to implement callback in python. What is Callback? In computer programming, a callback is executable [...]

Posted in Developer, python | Hits: 39644 | 4 Comments »

Python: Careful with equal sign

December 8th, 2007 mysurface

To store data, object reference to a variable is very easy in python. For example, to assign an integer 5 to a a=5 Same thing goes to string str=”hello world” Assign data to a data structured variable, let say list, L=[1,2,3,4,5] Even assign a function to a variable. import sys wstderr=sys.stderr.write wstderr(“hello world\n”) Observed that [...]

Posted in python | Hits: 69296 | 6 Comments »

How to redirect output to a file as well as display it out

December 6th, 2007 mysurface

To redirect standard output to a file is easy, you just need to use the redirection symbol, for example: echo “hello world” > test.txt But what if I want to display it out as well as store into a file? Answer: tee echo “hello world” | tee test.txt Okay it seems very easy, how about [...]

Posted in pipeline, python, tee, Text Manipulation | Hits: 283783 | 40 Comments »

Python: How to access ssh with pexpect?

November 25th, 2007 mysurface

I have research on how to access ssh using scripting language for quite sometimes. For python, there are three libs is in my list: Twisted – an event-driven networking engine written in Python. Paramiko – implements the SSH2 protocol for secure connections to remote machines. Pexpect – spawn a child application and control it as [...]

Posted in python | Hits: 111787 | 15 Comments »

python: handle string from pipelines and list of param

November 13th, 2007 mysurface

While I was searching ways to implement pipeline input for my python apps, I read an article from linuxjournal.com that mention about how easy that python can works with pipeline. Here is the code quote from that article. #! /usr/local/bin/python import sys sys.stdout.write(sys.stdin.read()) Well, I understand that served as a simple example to show how [...]

Posted in Developer, python | Hits: 63746 | 10 Comments »

python: how to identify the type of your variable

November 10th, 2007 mysurface

In python, every single variable is an object, every object must have a type, it is either data structure or class instances. Python’s variable can be dynamically change easily during runtime, for example >>> d={1:’one’,2:’two’} >>> print d {1: ‘one’, 2: ‘two’} >>> d=['one','two'] >>> print d ['one', 'two'] First line d is declare as [...]

Posted in Developer, python | Hits: 287195 | 19 Comments »