• No results found

Creating your own module

In document Learn Python in a Day (Page 103-109)

You now know that a module is simply a Python file having classes, functions and statements inside, and you also know that you can import a module or its features into a Python script or an interactive session.

As you get better with Python and start developing relatively long scripts that you want to reuse inside other scripts, it might be a good idea to import your script or its

features as a module in another script.

Let's take an example to illustrate the idea. Consider the class we created in the previous chapter: class Property:

def __init__(self, address, prop_type): self.address = address self.prop_type = prop_type print("Generates information about properties") def getaddress(self): print("Address: %s" % self.address) def gettype(self): print("Property type: %s" % self.prop_type)

To use it as a module, first of all, we need to store it inside a Python .py script, and store it among the other modules in the Python installation directory which could be something similar to C:\Python34\Lib.

Tip: To store some code in a .py script, you can either create an empty file in the

modules location, paste the code inside the file, and name the file under a .py

extension, or you can create a new file through the Python interactive IDLE and save it in the modules location.

Once you have saved the script under a name such as property.py, you can open a new interactive session, and import the new module as follows: import property

As you see, we used the name of the file to refer to the model. Once you have imported it, you can use all its functionalities by referring to the model name. Here is an

example: property.Property("An address", "Apartment").getaddress()

And that would produce this output: Generates information about properties Address: An address

And of course, you can also import only the feature that you want from the module. Let's import the Property class only: from property import Property

That would import only the Property class and its components from the module. As you see, Python is highly flexible so that it can suit various custom needs.

Summary

In this chapter you learned about modules and packages. You were able to understand the use of modules and packages in your programs. You were also explained the difference between a module and a package. However, you saw that using packages and modules in your scripts follows the same importing and accessing rules.

Furthermore, you learned how to install external Python modules and packages that didn’t come automatically installed with Python. You were also introduced to code introspection by learning how to get relevant information about Python classes, functions, and keywords.

Lastly, you were able to create your own Python module and use its features inside Python.

Assignment

Exercise 1

1. Which of the following statements about modules is not true?

1. A module can be imported into a script or an interactive session just like a package does.

2. A module is a .py file containing Python packages.

3. A module is a .py file containing classes, functions and statements. 4. A module provides a good technique for keeping scripts organized. 2. Which of the following statements about packages is not true?

1. Packages are imported just like modules. 2. Packages can be collection of modules.

3. Some packages do not come with the Python standard installation. 4. Packages are a must for Python to work.

3. What is not true about the following code? from base import element

1. It imports only the element feature from the base and no other element more.

2. It waives the use of base.element in the code that will come after that line.

3. It works with modules, but not with packages. 4. element belongs to base.

Exercise 2

Create a module that contains the following code and name the module assignment.py. intro = "Enjoy using our module!"

def area (x): a = x**2 return a

def email_generator (name, surname, email, extension): address = "{0}{1}@{2}{3}".format (name, surname, email, extension) return address

Once you have created the module and placed it along the other Python modules, perform the actions on the left column of the table and write the code that you used to perform those actions in the right column.

Action Code

Import the module into the interactive session.

Display "Enjoy using our module!"

Output the area of a square with a side length of 6 units using the area function.

Remove the assignment module from the session.

Import only the email_generator function to the session.

Import the area function following the same technique.

Solution for Exercise 1

Correct answers: 1: b

2: d 3: c

Solution for Exercise 2

Action Code

Import the module into the interactive session. import assignemnt

Display "Enjoy using our module!" print(assignment.intro)

of 6 units using the area function.

Remove the assignment module from the session.

del assignment

Import only the email_generator function to the session.

from assignment import email_generator

Import the area function following the same technique.

from assignment import email_generator

Chapter 9: File Handling

Chapter objectives:

You will learn how to open an existing text file in Python and read its content.

You will also learn how to write new content to an existing file.

You will be able to create new text files and type text in them from within Python.

You will also be able to open existing binary files and create new ones essentially using the same techniques as with text files.

You will learn a very robust way of handling files in Python using the with keyword.

You will also be introduced to the os module which is a built-in module used to interact with directories and perform actions such as creating new directories, changing the current working directory, listing directory content, and deleting folders and files as well.

In document Learn Python in a Day (Page 103-109)