How to use a python function from another module
Question: I trying to import a module and use a function from that module in my current
python file.

I run the nosetests on the parser_tests.py file but it fails with "name
'parse_subject' not defined"

e.g its not finding the parse_subject function which is clearly defined in the
parsrer.py file

This is the parsrer file:

    
    
    def peek(word_list):
    if word_list:
        word = word_list[0]
        return word[0]
    else:
        return None
    
    #Confirms that the expected word is the right type,
    

def match(word_list, expecting): if word_list: word = word_list.pop(0)

    
    
        if word[0] == expecting:
            return word
        else:
            return None
    else:
        return None
    

def skip(word_list, word_type): while peek(word_list) == word_type:
match(word_list, word_type)

def parse_verb(word_list): skip(word_list, 'stop')

    
    
    if peek(word_list) == 'verb':
        return match(word_list, 'verb')
    else:
        raise ParserError("Expected a verb next.")
    

def parse_object(word_list): skip(word_list, 'stop') next_word =
peek(word_list)

    
    
    if next_word == 'noun':
        return match(word_list, 'noun')
    elif next_word == 'direction':
        return match(word_list, 'direction')
    else:
        raise ParserError("Expected a noun or direction next.")
    

def parse_subject(word_list): skip(word_list, 'stop') next_word =
peek(word_list)

    
    
    if next_word == 'noun':
        return match(word_list, 'noun')
    elif next_word == 'verb':
        return ('noun', 'player')
    else:
        raise ParserError("Expected a verb next.")
    

def parse_sentence(word_list): subj = parse_subject(word_list) verb =
parse_verb(word_list) obj = parse_object(word_list)

    
    
    return Sentence(subj, verb, obj)
    

This is my tests file

    
    
    from nose.tools import *
    

from nose.tools import assert_equals import sys
sys.path.append("h:/projects/projectx48/ex48")

import parsrer

def test_subject(): word_list = [('noun', 'bear'), ('verb', 'eat'), ('stop',
'the'), ('noun', 'honey')] assert_equals(parse_subject(word_list),
('noun','bear'))


Answer: # Import Module

You can either import the whole module or you can specifically import that
specific function using **from an example keyword**.

For example:

    
    
    from parser_tests import parse_subject
    
    # and then you can invoke the function. 
    
    parse_subject() 
    

