The seventh chapter of Web development--Data Modeling (ebook 247-250) __python

Source: Internet
Author: User
Data Modeling

Words: is to solve how the original text form of the data file, converted into a predefined class of an instance of the method, so as to facilitate user invocation, processing.

The image above is from head top Python P247

The image above comes from head top Python P249 Implementation--data storage and access

Data storage and access is implemented in two functions:
Put_to_store () function
Get_from_store () function. Put_to_store () function

return value: Athletelist dictionary containing athlete object; write athletelist to Pickle;

Code:

def put_to_store (files_list):
    all_athletes = {}
    #定义字典, for storing athlete objects for
    each_file in files_list:
        #get_ Coach_data (each_file) opens every file in the file list,
        #并将其转换成AthleteList对象.
        #最后, the Athletelist object is deposited in the All_athletes dictionary.
        ath = get_coach_data (each_file)
        all_athletes[ath.name] = Ath
    try:
        #打开athletes. pickle file, the dictionary is All_ Athletes deposited therein.
        with open (' Athletes.pickle ', ' WB ') as ATHF:
            pickle.dump (All_athletes, ATHF)
    except IOError as Ioerr:
        print (' File error (Put_to_store): ' + str (IOERR)) return
    (all_athletes)
Get_from_store () function

return value: Athletelist dictionary containing athlete objects;

Code:

Def get_from_store ():
    all_athletes = {}
    try:
        #打开athletes. Pickle file, which is a dictionary
        #然后 and assigns it to the dictionary all_athletes.
        with open (' Athletes.pickle ', ' RB ') as ATHF:
            all_athletes = Pickle.load (ATHF)
    except IOError as Ioerr:
        Print (' File error (Get_from_store): ' + str (IOERR)) return
    (all_athletes)
validation--data storage and access

The code module (athletemodel.py) that is responsible for storing and accessing data is as follows:

#-*-Coding:utf-8-' Created on 2017-1-3 @author: "Lenovo" ' Import pickle from athletelist import athletelist def g Et_coach_data (filename): Try:with open (filename) as jaf:data=jaf.readline () Templ = data . Strip (). Split (', ') return (athletelist (templ.pop (0), Templ.pop (0), Templ) except IOError as Ioerr:prin
    T (' File Error: ' +str (Ioerr)) return (None) def put_to_store (files_list): All_athletes = {} #定义字典, for storing athlete objects
        For each_file in Files_list:ath = Get_coach_data (each_file) all_athletes[ath.name] = Ath Try:
        With open (' Athletes.pickle ', ' WB ') as Athf:pickle.dump (All_athletes, ATHF) except IOError as Ioerr: Print (' File error (Put_to_store): ' + str (IOERR)) return (all_athletes) def get_from_store (): All_athletes = {} Try:with open (' Athletes.picke ', ' RB ') as Athf:all_athletes = Pickle.load (ATHF) except Ror as Ioerr:prInt (' File error (Get_from_store): ' + str (IOERR)) return (all_athletes) 

Ok, to verify.

In idle, open athletemodel.py, press F5 to run, and then, enter Command Dir () and you will see:

>>> dir ()
[' athletelist ', ' __builtins__ ', ' __doc__ ', ' __file__ ', ' __name__ ', ' __package__ ', ' Get_coach_ Data ', ' get_from_store ', ' pickle ', ' put_to_store ']

What is the role of Dir ()?

"The Dir () function built into Python lists an identifier for a defined object. For example, for a module, include functions, classes, and variables defined in the module. When you give Dir () a module name, it returns a list of the names defined in that module. When a parameter is not supplied, it returns a list of variables, properties, and methods defined in the current module. ”

So, we can find that by entering Dir () it returns a list of the methods, properties, and variables defined in the current module.

Ok, with the tools, we set the files to read and store:

>>> the_files = [' Sarah2.txt ', ' james2.txt ', ' mikey2.txt ', ' julie2.txt ']

Storing data:

>>> data = Put_to_store (the_files)

Check this out:

>>> data
{' James Lee ': [' 2-34 ', ' 3:21 ', ' 2.34 ', ' 2.45 ', ' 3.01 ', ' 2:01 ', ' 2:01 ', ' 3:10 ', ' 2-22 ', ' 2-01 ', ' 2.01 ' ', ' 2:16 '], ' Sarah Sweeney ': [' 2:58 ', ' 2.58 ', ' 2:39 ', ' 2-25 ', ' 2-55 ', ' 2:54 ', ' 2.18 ', ' 2:55 ', ' 2:55 ', ' 2:22 ', ' 2-21 ', ' 2.2 2 '], ' Julie Jones ': [' 2.59 ', ' 2.11 ', ' 2:11 ', ' 2:23 ', ' 3-10 ', ' 2-23 ', ' 3:10 ', ' 3.21 ', ' 3-21 ', ' 3.01 ', ' 3.02 ', ' 2:59 '], ' Mik EY McManus ': [' 2:22 ', ' 3.01 ', ' 3:01 ', ' 3.02 ', ' 3:02 ', ' 3.02 ', ' 3:22 ', ' 2.49 ', ' 2:38 ', ' 2:40 ', ' 2.22 ', ' 2-31 ']}

Sure enough, after calling the function Put_to_store (the_files), the text file was converted into a dictionary.

The data is stored (converted to instances), and you can access it below:

Calling the Get_from_store () function

>>> data_copy = Get_from_store ()

The function returns a dictionary to Data_copy, which can then be accessed by:

>>> data_copy = Get_from_store ()
>>> for Each_athlete in data_copy:
    print (Data_copy[each_ Athlete].name + ' + data_copy[each_athlete].dob '
    #注意调用的格式, every data item in the dictionary is athletelist object


James Lee 2002-3-14
Sarah Sweeney 2002-6-17
Julie Jones 2002-8-17
Mikey McManus 2002-2-24

Ok, so far, the method for storing and reading data is over. Another, the first time to write a document with Markdown, it is very simple, very easy to use; Contrast word in the need to repeatedly adjust the format, really is too convenient, thanks to developers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.