Posts

Unit Test in Python

Python Unit Test is something you will encounter in your intermediate term of python journey, Its good practice to follow unit test as it allows you to test a single unit of logic. I am not going into too much depth of advantage and disadvantages of unit test and why you should use it, you can find many more posts on the topic. My focus here to show you some real code quickly which helps you to build a quick understanding of the topic. Following is the simple program we will be using to see unit test in reality. Just go through it and I will provide a quick explanation of steps. Detailed step by step explanation exist at the end of this post.
#!/usr/bin/env python class Calculate:     def add(self, x, y):         return x + y
if __name__ == '__main__':     calc = Calculate()     result = calc.add(2,3)     print result

Unit test Part: First import unit test libraries import unittest
Then import your program: from calc import Calculator
Now you need to create a class say and override with `uni…

Linux Command line tips and Bash stuff

Linux Command Line Tips and Many More

Here is a selection of command-line tips that I've found useful when working on Linux. The emphasis is on somewhat less-known techniques that are generally important or useful to technical users. It's a bit long, and users certainly don't need to know all of them, but I've done my best to review that each item is worth reading in terms of projected time savings, if you use Linux heavily.

To get more information on a command mentioned, first try "man <command name>". In some cases, you must install a package for this to work -- try aptitude or yum. If that fails, Google it.

Basics

Learn basicBash. Actually, read the whole bash man page; it's pretty easy to follow and not that long. Alternate shells can be nice, but bash is powerful and always available (learning mainly zsh or tcsh restricts you in many situations).Learn vim. There's really no competition for random Linux editing (even if you use Emacs or Eclipse …

BASH TRICKS

Image
Here are some awesome bash trick which will make you more productive on bash:

History expansion
    !! = previous command
    !$ = last word of previous command
    !-n = nth previous command
    !#$ = last word of current line
    !<start of command> will execute the command from history starting with letters after "!"

Brace Expansion:
    {a..b} = numbers a to b in order.
    {a,b,c} = words a, b, c. Useful for paths: touch /tmp/{foo,bar,baz}

Parameter Expansion:
Suppose that foo=/usr/local/blah.txt
    ${variable#word} = removes word from the beginning of variable. For example, ${foo#*/} = usr/local/blah.txt
    ${variable##word} = same thing, but removes longest pattern matching word. For example, ${foo##*/} = blah.txt
    ${variable%word} = removes word from end of variable. For example: ${foo%.txt} = /usr/local/blah
    ${variable%%word} = same thing but longest matching suffix

Process Substitution:
    <(command) = treats the output of command as a file. diff -u <(ssh web{…

Simple Python WebServer: Share/View your Linux Filesystem files over Http Server

Image
A quick tip for Linux lovers.

Python allows you to start a quick http server by a single command.


Suppose you want to share files and directory of current working directory over http. No need to set up a complete web server for that. Python  can do this magic in a Single command.

 [root@clone-1 /]# python -m SimpleHTTPServer

Serving HTTP on 0.0.0.0 port 8000 ...
localhost.localdomain - - [02/Dec/2013 10:15:18] "GET / HTTP/1.1" 200 -
localhost.localdomain - - [02/Dec/2013 10:15:22] "GET /boot/ HTTP/1.1" 200 -
localhost.localdomain - - [02/Dec/2013 10:15:24] "GET /boot/symvers-2.6.18-164.el5.gz HTTP/1.1" 200 -
localhost.localdomain - - [02/Dec/2013 10:15:32] "GET /home/ HTTP/1.1" 200 -
localhost.localdomain - - [02/Dec/2013 10:15:34] "GET /home/yogi/ HTTP/1.1" 200 -
192.168.91.1 - - [02/Dec/2013 10:20:12] "GET / HTTP/1.1" 200 -
192.168.91.1 - - [02/Dec/2013 10:20:12] code 404, message File not found
192.168.91.1 - - [02/Dec/2013 …

Using MTS MBlaze on Mint Linux 14 / Ubuntu 12.04

Yes, Indeed ubuntu and Mint Linux both provides inbuilt support for MTS MBlaze , But sometime network GUI may not recognize your MTS dongle. You can configure "wvdial" to get online.


Install wvdial using
sudo apt-get install wvdial

Yes, you are right you should install it in advance ;).

You will also need usb-modeswitch and usb-modeswitch-data.this might come preinstalled with your distribution, You can check this under Synaptics Package Manager.

Then you need to modify /etc/wvdial.conf configuration file. This might not present on your system by default, But no worries You can generate it by using wvdialconf command

Yogi-Lenovo ~ # wvdialconf
Editing `/etc/wvdial.conf'.

Scanning your serial ports for a modem.

Modem Port Scan<*1>: S0   S1   S2   S3   S4   S5   S6   S7  
Modem Port Scan<*1>: S8   S9   S10  S11  S12  S13  S14  S15 
Modem Port Scan<*1>: S16  S17  S18  S19  S20  S21  S22  S23 
Modem Port Scan<*1>: S24  S25  S26  S27  S28  S29  S30  S31 
ttyUS…

Linux Bash : Remove only numbers or patterns from a long list using SED and more

Image
Q: I want to remove only numbers/specific patterns from a long list ?


You can use "sed" for the above task. Below is the command.
#cat filename | sed 's|[0-9]||g'Or if you want to KEEP ONLY numbers.

# cat filename | sed 's|[^0-9]||g'
Or anything you wish to skip in whole text. for eg "-" and "." in below example

#cat filename | sed 's|[-.-]||g'
A more BASH specific way can be ,if you do not wish to use sed:

while read line;do \ echo ${line//[0-9]/}; \ done< filename
Well only grep can also perform same stuff:

#cat test | grep -o '[^0-9-]*'

Hope you this little tricks helpful.






Related articlesUse multiple CPU Cores with your Linux commands -- awk, sed, bzip2, grep, wc, etc.In bash how to replace string variable, and set it into sed command

Check RPM installed recently on RedHat/CentOS

Image
Q: How do I check that which rpm's have installed recently on my RedHat/CentOS machine ?

RPM provide various ways to manage, update, installation, uninstall of packages.
To check what rpm's installed recently we can use following command.

[root@linuxskool]# rpm -qa --last
perl-rrdtool-1.4.7-1.el5.rf                   Thu 11 Jul 2013 05:16:36 PM IST
rrdtool-1.4.7-1.el5.rf                          Thu 11 Jul 2013 05:16:35 PM IST
ruby-1.8.5-29.el5_9                            Thu 11 Jul 2013 05:16:34 PM IST
libdbi-0.8.1-2.1                                   Thu 11 Jul 2013 05:16:34 PM IST

ruby-libs-1.8.5-29.el5_9                      Thu 11 Jul 2013 05:16:33 PM IST

or more clearly

[root@linuxskool]# rpm -qa --last | less


Hope you find this quick tip useful.