Reusable codes
O'Reilly's textbook in Chapter 2.3:
Can write down source codes from IDLE by menu: File -> New window
This can be saved as a file with extension ".py".
Then execute via menu: Run -> Run Module
Defining Functions: Already experienced in previous sections more or less.
>>> def plural(word): ... if word.endswith('y'): ... return word[:-1] + 'ies' ... elif word[-1] in 'sx' or word[-2:] in ['sh', 'ch']: ... return word + 'es' ... else: ... return word + 's' ... >>> plural('fairy') 'fairies' >>> plural('woman') 'women'
How about this?
>>> plural('tooth') 'tooths'
I got a result as same as I expected... No logic is included in the code for foot/feet or tooth/teeth patterns.
Moving forward. Copy the code and save as textproc.py under /Documents/workspace/NLTK Learning. But I felt the place could be slightly uncomfortable. Then I created a new folder scripts under NLTK learning.
Try to import, but ....
>>> from textproc import * Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named textproc
That could be PATH is not set to the current location (scripts). Let's check the sys.path.
>>> import sys >>> print sys.path ['/Users/xxx/Documents/workspace/NLTK Learning', '/Users/xxx/Documents', '/Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg', '/Library/Python/2.7/site-packages/pip-1.3.1-py2.7.egg', '/Library/Python/2.7/site-packages/ipython-0.13.2-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages'] >>>
It seems the original location (NLTK learning) is set to the PATH. However, I have moved the file one level lower by Finder. After I thought for a second. I opened the file (textproc.py) from IDLE side then show sys.path again.
>>> import sys >>> print sys.path ['/Users/xxx/Documents/workspace/NLTK Learning/scripts', '/Users/xxx/Documents', '/Library/Python/2.7/site-packages/setuptools-0.6c11-py2.7.egg', '/Library/Python/2.7/site-packages/pip-1.3.1-py2.7.egg', '/Library/Python/2.7/site-packages/ipython-0.13.2-py2.7.egg', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages'] >>>
Yes, the PATH has been updated. Now the system should be able to recognise the new location.
>>> from textproc import plural >>> >>> plural('wish') 'wishes' >>> plural('fan') 'fans' >>>