Online Book Reader

Home Category

Learning Python - Mark Lutz [594]

By Root 1898 0
rest:% python

>>> from mymod import *

>>> countChars("mymod.py")

291

__main__. If you code it properly, it works in either mode (program run or module import):def countLines(name):

file = open(name)

return len(file.readlines())

def countChars(name):

return len(open(name).read())

def test(name): # Or pass file object

return countLines(name), countChars(name) # Or return a dictionary

if __name__ == '__main__':

print(test('mymod.py'))

% python mymod.py

(13, 346)

This is where I would probably begin to consider using command-line arguments or user input to provide the filename to be counted, instead of hardcoding it in the script (see Chapter 24 for more on sys.argv, and Chapter 10 for more on input):if __name__ == '__main__':

print(test(input('Enter file name:'))

if __name__ == '__main__':

import sys

print(test(sys.argv[1]))

Nested imports. Here is my solution (file myclient.py):from mymod import countLines, countChars

print(countLines('mymod.py'), countChars('mymod.py'))

% python myclient.py

13 346

As for the rest of this one, mymod’s functions are accessible (that is, importable) from the top level of myclient, since from simply assigns to names in the importer (it works as if mymod’s defs appeared in myclient). For example, another file can say:import myclient

myclient.countLines(...)

from myclient import countChars

countChars(...)

If myclient used import instead of from, you’d need to use a path to get to the functions in mymod through myclient:import myclient

myclient.mymod.countLines(...)

from myclient import mymod

mymod.countChars(...)

In general, you can define collector modules that import all the names from other modules so they’re available in a single convenience module. Using the following code, you get three different copies of the name somename (mod1.somename, collector.somename, and __main__.somename); all three share the same integer object initially, and only the name somename exists at the interactive prompt as is:# File mod1.py

somename = 42

# File collector.py

from mod1 import * # Collect lots of names here

from mod2 import * # from assigns to my names

from mod3 import *

>>> from collector import somename

Package imports. For this, I put the mymod.py solution file listed for exercise 3 into a directory package. The following is what I did to set up the directory and its required __init__.py file in a Windows console interface; you’ll need to interpolate for other platforms (e.g., use mv and vi instead of move and edit). This works in any directory (I just happened to run my commands in Python’s install directory), and you can do some of this from a file explorer GUI, too.

When I was done, I had a mypkg subdirectory that contained the files __init__.py and mymod.py. You need an __init__.py in the mypkg directory, but not in its parent; mypkg is located in the home directory component of the module search path. Notice how a print statement coded in the directory’s initialization file fires only the first time it is imported, not the second:C:\python30> mkdir mypkg

C:\Python30> move mymod.py mypkg\mymod.py

C:\Python30> edit mypkg\__init__.py

...coded a print statement...

C:\Python30> python

>>> import mypkg.mymod

initializing mypkg

>>> mypkg.mymod.countLines('mypkg\mymod.py')

13

>>> from mypkg.mymod import countChars

>>> countChars('mypkg\mymod.py')

346

Reloads. This exercise just asks you to experiment with changing the changer.py example in the book, so there’s nothing to show here.

Circular imports. The short story is that importing recur2 first works because the recursive import then happens at the import in recur1, not at a from in recur2.

The long story goes like this: importing recur2 first works because the recursive import from recur1 to recur2 fetches recur2 as a whole, instead of getting specific names. recur2 is incomplete when it’s imported from recur1, but because it uses import instead of from, you’re safe: Python finds and returns the already created recur2 module object and continues to

Return Main Page Previous Page Next Page

®Online Book Reader