Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How Python divides modules

Shulou Source: shulou.com Published: 2022-06-01 11:43:36 09月25日 Update

This article introduces the relevant knowledge of "how to divide the module of Python". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First step, split rule

Copy the code from the rule section to a new file: rule.py, and comment or delete the original code. For comparison here, DE8UG commented on file 08, see the last code.

# rule.py

# author: DE8UG

# PROJECT: rengleme

Rule = {

"Wet garbage": ["vegetable leaves", "orange peel", "spring onions", "biscuits"]

"dry garbage": ["old bathtub", "basin", "sponge", "toilet paper"]

}

The second step is to split the core code for dealing with garbage

Create a new file: handle_laji.py, and copy the function for dealing with garbage sorting here.

# handle_laji.py

# author: DE8UG

# PROJECT: rengleme

Def reng_laji (rule_k, laji, ljt):

If rule_k = = ljt ['name']:

Ljt ['data'] .append (laji)

Def fenlei (laji, rule, ljt):

For k, v in rule.items ():

Print (k, v)

If laji in v:

Print ('garbage found:', laji, k)

Reng_laji (k, laji, ljt)

The third step is to process files, load and save garbage

Create a new file: handle_file.py, and copy the function for processing the file here.

# handle_file.py

# author: DE8UG

# PROJECT: rengleme

Import json

# print ('load file:', ljt_gan)

Def load_data (filename):

With open (filename) as f:

Data = json.load (f)

Return data

# encapsulate the code for writing files with functions

Def save_to_file (filename, data):

With open (filename,'w') as f:

Json.dump (data, f)

The fourth step, improve the code, test

Delete the removed code from the 08 file (I've only commented here for comparison), and then import the previous new py files first.

Note: these files need to be in one directory to avoid the situation that files can not be found in different paths.

After import imports the corresponding module, the subsequent function needs to use the module. Called by the name of the function. Please add the required function name yourself. Then conduct a code test. If there is no problem, our new transformation will be completed today.

# author: DE8UG

# create trash cans and trash

# Rule: KRV, a k corresponds to a v, and the code is divided by colons.

# input

# judgment

# Save

# function

# Synthesis

# 8 divide the module and transfer the code

# import json

# Import self-built modules

Import rule

Import handle_file, handle_laji

# rule = {

# "Wet garbage": ["vegetable leaves", "orange peel", "spring onions", "biscuits"]

# "dry garbage": ["old bathtub", "basin", "sponge", "toilet paper"]

#}

# # print ('load file:', ljt_gan)

# def load_data (filename):

# with open (filename) as f:

# data = json.load (f)

# return data

# def reng_laji (rule_k, laji, ljt):

# if rule_k = = ljt ['name']:

# ljt ['data'] .append (laji)

# def fenlei (laji, rule, ljt):

# for k, v in rule.items ():

# print (k, v)

# if laji in v:

# print ('garbage found:', laji, k)

# reng_laji (k, laji, ljt)

# reng_laji (k, laji, ljt_shi)

# encapsulate the code for writing files with functions

# def save_to_file (filename, data):

# with open (filename,'w') as f:

# json.dump (data, f)

Def main ():

# define trash cans

Ljt_shi = {

Name': "wet rubbish"

'data': []

}

Ljt_gan = {

Name': "dry trash"

'data': []

}

# load existing garbage

Ljt_gan = handle_file.load_data ('gan.json')

Ljt_shi = handle_file.load_data ('shi.json')

Print ('load file:', ljt_gan, ljt_shi)

# throw out the trash

Laji = input ("enter garbage to throw:")

Print ("junk:", laji)

# Classification

Handle_laji.fenlei (laji, rule, ljt_gan)

Handle_laji.fenlei (laji, rule, ljt_shi)

Print ('- * 20)

Print (ljt_shi)

Print (ljt_gan)

# call the function and save to the specific trash file

Handle_file.save_to_file ('gan.json', ljt_gan)

Handle_file.save_to_file ('shi.json', ljt_shi)

If _ name__ = = "_ _ main__":

Main ()

This is the end of the content of "how to divide Python into modules". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Garbage files code functions modules processing trash cans comments rules hygiene content toilet paper situation more bathtub sponge basin knowledge vegetable leaves biscuits Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology vpn MySQL Shulou Tech Info Huawei