In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian introduces in detail "how to import a module that needs to transfer parameters in python" with detailed contents, clear steps and proper handling of details. I hope that this article "how to import a module that needs to transfer parameters in python" can help you solve your doubts.
Recently, when I ran into the experiment, I encountered a problem:
Because of the large number of experimental data sets, you need to manually change the file path every time you run a dataset, then transfer the file to the server, and then run the experiment, so the efficiency is very low, so you have to watch this experiment specially. When you finish running, you need to manually modify and run the next experiment. Personally, I can't stand such inefficiency, so I wonder if there is any solution.
The desired solution is to pass parameters on the command line to solve this problem, because the next step is to write a shell script to run the experiment in batches, which is too clumsy to use input statements.
When writing the experimental code, I concentrated all the parameters into a py file for later maintenance. The problem now is that some values in the file need to be changed by passing parameters on the command line.
With regard to passing parameters, argparse is provided in python. Through this module, the function of transmitting parameters on the command line can be realized quickly.
Here's an example:
File test1.py
Import argparsefrom argparse import RawTextHelpFormatter parse = argparse.ArgumentParser (description= "The parameters for the feature select method", formatter_class=RawTextHelpFormatter) parse.add_argument ('name', type = str) args = parse.parse_args () name = args.name
File test3.py
Import test1 str = test1.name + 'asdasdasd'
File test2.py
Import test3 print test3.str
We can change the value of this name by changing the parameters on the command line, which is easier to understand. We can think of it as python splicing the three pieces of code together and then executing them together.
This achieves the function I just wanted.
Add: the solution to the error of passing parameters when python calls the module
First, a Login module is defined.
There are two parameters: username,password
# coding=utf-8def login (self,username Password): driver=self.driver self.driver.implicitly_wait (30) # error driver.switch_to.frame (0) driver.find_element_by_class_name ("dlemail"). Clear () driver.find_element_by_class_name ('dlemail'). Send_keys ("username") driver.find_element_by_class_name (' dlpwd'). Clear () driver.find_element_by_ Class_name ('dlpwd'). Send_keys ("password") driver.find_element_by_id ("dologin"). Click () def logout (self): driver=self.driver driver.find_element_by_link_text (u "exit"). Click () driver.quit () calls the module in Test.login: # coding=utf-8from selenium import webdriverimport unittest Timefrom public import Loginclass TestLogin (unittest.TestCase): def setUp (self): self.driver=webdriver.Chrome () self.driver.implicitly_wait (30) self.base_url = "http://www.126.com/" self.verificationErrors = [] def test_login (self): driver=self.driver driver.get (self.base_url) Login.login (self," jinbian3333 " "jinbian76") # pass parameters are these two text=driver.find_element_by_id ("spnUid") .text self.assertEqual (text, "jinbian3333@126.com") Login.logout (self) def tearDown (self): self.driver.quit () self.assertEqual ([], self.verificationErrors) if _ _ name__ = ='_ main__': unittest.main (), but an error is found after running:
Obviously, the parameters are "jinbian3333" and "jinbian76". Why is the login information in the picture?
Look back at the called module
As far as I understand it, adding "" is not a variable, but a constant. If you call the module, even if you pass parameters, it still runs according to the settings of the module itself: "username" and "password".
After reading this, the article "how to import a module that needs to transfer parameters in python" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.