I recently ran an experiment and ran into a problem:
Because the experimental data set is more, every time you run a data set you need to manually change the file path, and then transfer the file to the server, and then run the experiment, in this case, the efficiency is very low, you have to specialize in looking at the experiment, when you run out of manually modified to run the next experiment. I personally can not stand this low efficiency, I want to be able to have any solution.
Our expected solution to this problem is to pass the parameter on the command line, because the next step is to write shell scripts to run the experiments in bulk, and it seems too clumsy to use typed statements.
While writing the experimental code, I centralized all the parameters in a py file so that it is easy to maintain at a later stage, and now the problem is that I need to change some of the values in that file by passing parameters on the command line.
Python provides a module called argparse for passing parameters. This module makes it possible to pass parameters from the command line very quickly.
Here's an example:
file
import argparse from argparse import RawTextHelpFormatter parse = (description="The parameters for the feature select method", formatter_class=RawTextHelpFormatter) parse.add_argument('name', type = str) args = parse.parse_args() name =
file
import test1 str = + 'asdasdasd'
file
import test3 print
Running:
We can change the value of this name by changing the parameters on the command line. It's actually relatively easy to understand about this, we can think of it as python splicing these three pieces of code together and executing them together.
This fulfills the function I just wanted.
Supplementary: python call module pass parameter error solution
First a Login module is defined
There are two parameters: username,password.
#coding=utf-8 def login(self,username,password): driver= .implicitly_wait(30)#Without the wait there will be an 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= driver.find_element_by_link_text(u"Exit.").click() ()
Call the module in:
#coding=utf-8 from selenium import webdriver import unittest,time from public import Login class TestLogin(): def setUp(self): =() .implicitly_wait(30) self.base_url = "http:///" = [] def test_login(self): driver= (self.base_url) (self,"jinbian3333","jinbian76")# The passing parameters are these two text=driver.find_element_by_id("spnUid").text (text,"jinbian3333@") (self) def tearDown(self): () ([],) if __name__ == '__main__': ()
But after running it, I found an error:
Ming Chuan-Shen is"jinbian3333", "jinbian76", why is the login info in the picture?
Going back to the called module, the
The way I understand it is that by adding " " it's not a variable, it's a constant, and when you call the module, even if you pass the parameter, it's still going to run with the module's own settings: "username", "password", and so on.
The above is a personal experience, I hope it can give you a reference, and I hope you can support me more. If there is any mistake or something that has not been fully considered, please do not hesitate to give me advice.