Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Tuesday, November 12, 2013

How to import functions in another file to your current file in python

If you are working on a code (sample.py) that uses functions of another file (simpleops.py) in the same directory, you can simply call "import simpleops" and then use the functions in that file as "simpleops.()" as shown in the below example.

Example: 

Your current program (sample.py) :

#!/usr/bin/env python
import simpleops

print simpleops.sum(5,3)
print simpleops.mult(5,3)



If the another file is in different directory compared to your current file, then you should first import its directory path and then import the file like: "sys.path.insert(0, FILEPATH) import simpleops" as shown in the below example. Note that the value '0' in sys.path.insert means it first searches the modules in the current directory first from the program has been called. 

Example: 

Your current program (sample.py):

#!/usr/bin/env python
import sys
sys.path.insert(0, FILEPATH)
import simpleops

print simpleops.sum(5,3)
print simpleops.mult(5,3)



For example lets say, your sample code (simpleops.py) looks like this:

#!/usr/bin/env python

def mult(a,b):
        return(a*b)

def sum(a,b):
        return(a+b)


Monday, November 11, 2013

How to install maven on linux?

Step-1: Download the maven binary package from http://maven.apache.org/download.cgi

Step-2: Extract the tar file: tar -xvzf apache-maven-3.1.1-bin.tar.gz

Step-3: Create a folder in /usr/local with the name apache-maven and extract the above tar file in this directory which gives the file structure as: /usr/local/apache-maven/apache-maven-3.1.1

Step-4: Set your path variables as below:
export M2_HOME=/usr/local/apache-maven/apache-maven-3.1.1
export M2=$M2_HOME/bin
export PATH=$M2:$PATH

Step-5: To check if maven is successfully installed: mvn --version


Wednesday, September 18, 2013

Problem with tagger and chunkers of NLTK on linux

I installed NLTK using the commands which were found here: http://nltk.org/install.html

Everything got installed successfully. So when I started using this nltk package like this:

python
>>> import nltk
>>> text = nltk.word_tokenize("And now for something completely different")
>>> text
['And', 'now', 'for', 'something', 'completely', 'different']

But when I try to tag these tokens, there are some errors as shown below.

>>>  nltk.pos_tag(text)
  File "", line 1
    nltk.pos_tag(text)
    ^
IndentationError: unexpected indent
>>> nltk.pos_tag(text)
Traceback (most recent call last):
  File "", line 1, in
  File "/usr/lib/python2.6/site-packages/nltk/tag/__init__.py", line 99, in pos_tag
    tagger = load(_POS_TAGGER)
  File "/usr/lib/python2.6/site-packages/nltk/data.py", line 605, in load
    resource_val = pickle.load(_open(resource_url))
  File "/usr/lib/python2.6/site-packages/nltk/data.py", line 686, in _open
    return find(path).open()
  File "/usr/lib/python2.6/site-packages/nltk/data.py", line 467, in find
    raise LookupError(resource_not_found)
LookupError:
**********************************************************************
  Resource 'taggers/maxent_treebank_pos_tagger/english.pickle' not
  found.  Please use the NLTK Downloader to obtain the resource:
  >>> nltk.download()
  Searched in:
    - '/root/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
**********************************************************************


The way you can fix it is: Run this command which downloads the tagger:
>>> nltk.download('maxent_treebank_pos_tagger')

It downloads this package to some directory on your machine. For me it downloaded here: /root/nltk_data...

And all the system related files with regard to NLTK are in this directory: /usr/lib/python2.6/site-packages/nltk
So, I created a new directory called 'taggers' and copy this 'maxent_treebank_pos_tagger' to this new directory named: /usr/lib/python2.6//site-packages/nltk/taggers/

Now when you run this command:
>>> tagged = nltk.pos_tag(tokens)
[('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'), ('Thursday', 'NNP'), ('morning', 'NN')]

Similarly when you try to do this:
>>> entities = nltk.chunk.ne_chunk(tagged)
You will see similar results that you have to download chunker and corpora, follow the similar procedure and execute these two commands:

>>> nltk.download('maxent_ne_chunker')
>>> nltk.download('words')

This can help you install the chunkers package and when you execute this command, you can get the parse tree
>>> entities = nltk.chunk.ne_chunk(tagged)
>>> entities
Tree('S', [('At', 'IN'), ('eight', 'CD'), ("o'clock", 'JJ'), ('on', 'IN'), ('Thursday', 'NNP'), ('morning', 'NN'), Tree('PERSON', [('Arthur', 'NNP')]), ('did', 'VBD'), ("n't", 'RB'), ('feel', 'VB'), ('very', 'RB'), ('good', 'JJ'), ('.', '.')])

Hope this helps!


Monday, June 28, 2010

using SCREEN command in Linux

There are times when you want to log off your system, but you want the programs to be executed on the server machine which you've initiated from your system. You can use screen command which serves this purpose. It starts a session within the session that you have logged in. Even if you logout from your system, the screen will keep on running until the server stops or you forcefully exit.

Here are the commands which you should use.

$screen //starts a new session in the logged session

$./a.out > output //some command which you want to get it executed

$ CTRL + a + d //closes the screen and brings you back to the original session

$screen -r //displays the screens running on your machine

Hope this is helpful :-)

Friday, June 11, 2010

Identify the ports in linux

For socket programming, you need to use the port. If you want to identify the ports which are active, you can use the following commands. Generally in linux, the ports from 0 to 1023 are reserved and can be accessed only as a root user.

For checking the information about the port, you can use netstat

If you want to know more about this command, you can check its man page
$ man netstat

But for quick reference, you can use any of these for the appropriate purpose.

For finding the open ports:
$ netstat --listen

For finding the open ports and established TCP connections:
$ netstat -vatn

For checking information about a port,
$ netstat -anp | grep <port>

For identifying the ports already in use:
$ netstat -atnp | grep LISTEN

Tuesday, March 9, 2010

How to mount Windows[NTFS] from Linux

Initially login as the root user.

Then type this command on the console:-

$ fdisk -l

This command shows what are the different disks present on our machine. They generally follow this particular format: /dev/sda[number]

$ mount -t ntfs /dev/sda[number of the disk which you want to mount] [directory on the root from which you will access the mounted disk files say for ex: mountedDir]

Then for viewing the mounted files just type as usual,

$ cd mountedDir

You will be able to access the files of the disk which was mounted.

For unmounting the disk, just simply type this on the console:

$ unmount mountedDir

Hope this will be helpful.

Finding the system information in Linux

These days am using Fedora more than Windows so trying to learn more about that. For finding out the details related to your system, you can use the following commands.

First login in to the root account.

Then, you can run these commands:

% /usr/sbin/dmidecode -t baseboard

This will give details of the motherboard


% /usr/sbin/dmidecode -t processor

This will dump the information about the processor.

Other keywords which are available which can be used as options for ‘t’ are

  1. bios
  2. system
  3. chassis
  4. memory
  5. cache
  6. connector
  7. slot
A easy method to find all the information abuot your cpu, you can execute this command:

$ cat /proc/cpuinfo

Hope this will be helpful.