Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Python divides modules

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report