Best Python code snippet using locust
automate_cron.py
Source:automate_cron.py
...60 pre_comment=True,61 )62 entered_time = enter_run_time(job)63 if not entered_time:64 get_run_time(job)65 print()66 print_text('Your cron job is:')67 print_text(job)68 cron.write()69 print()70 print_text('Done! Please double check your job for any typos using "crontab -l".')71def copy_crontab(billopsuser):72 exists = os.path.exists('/usr/local/rbm/home_dirs/' + billopsuser + '/bin/cron_backups')73 if not exists:74 os.mkdir('/usr/local/rbm/home_dirs/' + billopsuser + '/bin/cron_backups')75 todays_date = datetime.date.today().strftime("%Y%m%d")76 copy_exists = os.path.exists('/usr/local/rbm/home_dirs/' + billopsuser + '/bin/cron_backups/crontab.' + todays_date)77 if copy_exists:78 print_text('A copy of crontab from today already exists at:')79 print_text('/usr/local/rbm/home_dirs/' + billopsuser + '/bin/cron_backups/crontab.' + todays_date)80 else:81 os.system('crontab -l > /usr/local/rbm/home_dirs/' + billopsuser + '/bin/cron_backups/crontab.' + todays_date)82 print_text('Copied crontab to /usr/local/rbm/home_dirs/' + billopsuser + '/bin/cron_backups/crontab.' + todays_date)83def get_directory_name():84 print_text('Please enter the $BIN directory that your script exists in (no slashes or path, just the name):')85 directory_name = input()86 check_for_spaces = directory_name.split(' ')87 if len(check_for_spaces) > 1:88 print_text("Please don't include any spaces in the directory name.")89 directory_name = get_directory_name()90 elif len(directory_name) == 0:91 print_text('Directory name cannot be empty.')92 directory_name = get_directory_name()93 elif directory_name[0] == '/':94 directory_name = directory_name[1:]95 return directory_name96def get_title_name():97 print_text("""Enter the title you would like to be at the top of your cron job i.e. #===== <title>:""")98 title_name = input()99 if len(title_name) == 0:100 print_text('Title cannot be empty.')101 title_name = get_title_name()102 return title_name103def get_ksh_name():104 print_text('Enter name of your .ksh file:')105 ksh_name = input().split('.')106 if len(ksh_name) == 0:107 print_text('Name cannot be empty.')108 ksh_name = get_ksh_name()109 elif len(ksh_name) > 2:110 print_text('Name cannot have more than 1 dot in name.')111 ksh_name = get_ksh_name()112 final_name = ksh_name[0] + '.ksh'113 return final_name114def enter_run_time(job):115 print_text("Do you want to enter the cron schedule using cron's syntax?")116 print_text('Or do you want to be walked through it? (cron or walk-through) c|w')117 choice = input().lower()118 if choice == 'c':119 print_text('Enter the schedule in the form (without brackets) <* * * * *>')120 schedule = input().rstrip()121 job.setall(schedule)122 return True123 elif choice == 'w':124 return False125def get_run_time(job):126 print_text("Enter 'restart' at any point to restart the walk-through.")127 while True:128 day_of_week = False129 week_interval = False130 job.every().minute() # If restarted, this line will reset time to * * * * *131 print_text('Will this job run every minute? y|n')132 c = input().lower()133 if c == 'y':134 pass135 elif c == 'n':136 print_text('When called, will this job run at only one minute of the hour? y|n')137 choice = input().lower()138 if choice == 'y':139 print_text('What minute of the hour will it run? 0-59')140 minute_ = input()141 if minute_ == 'restart':142 print_text('Starting over.')143 get_run_time(job)144 break145 minute = int(minute_)146 job.minute.on(minute)147 elif choice == 'n':148 print_text('Will it run on an [i]nterval (every __ days)? Or at [s]pecific minutes? i|s')149 setting = input().lower()150 if setting == 'i':151 print_text('Every how many minutes do you want this to run? 0-59')152 num = input()153 if num == 'restart':154 print_text('Starting over.')155 get_run_time(job)156 break157 job.minute.every(int(num))158 elif setting == 's':159 print_text('What minutes do you want it to run at? (comma delimited, no spaces) 1-60')160 minutes = input().split(',')161 if minutes == ['restart']:162 print_text('Starting over.')163 get_run_time(job)164 break165 job.minute.on(int(minutes[0]))166 for minute in minutes[1:]:167 job.minute.also.on(int(minute))168 elif setting == 'restart':169 print_text('Starting over.')170 get_run_time(job)171 break172 else:173 print_text('Your options are [i], [s], or [restart]. Restarting due to bad input.')174 get_run_time(job)175 break176 elif choice == 'restart':177 print_text('Starting over.')178 get_run_time(job)179 break180 else:181 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')182 get_run_time(job)183 break184 elif c == 'restart':185 print_text('Starting over.')186 get_run_time(job)187 break188 else:189 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')190 get_run_time(job)191 break192 print_text('Will this job run every hour of the day? y|n')193 c = input().lower()194 if c == 'y':195 pass196 elif c == 'n':197 print_text('Will this job run at only one hour of the day? y|n')198 choice = input().lower()199 if choice == 'y':200 print_text('What hour of the day will this job run? 0-23')201 hour = input()202 if hour == 'restart':203 print_text('Starting over.')204 get_run_time(job)205 break206 job.hour.on(int(hour))207 elif choice == 'n':208 print_text('Will this job run in an hourly [i]nterval (every __ hours) or at [s]pecified hours of the day? i|s')209 period = input().lower()210 if period == 'i':211 print_text('Every how many hours do you want this to run? 0-23')212 interval = input()213 if interval == 'restart':214 print_text('Starting over.')215 get_run_time(job)216 break217 job.hour.every(int(interval))218 elif period == 's':219 print_text('What specific hours of the day do you want this to run? (comma delimited, no spaces) 0-23')220 hours = input().split(',')221 if hours == ['restart']:222 print_text('Starting over.')223 get_run_time(job)224 break225 job.hour.on(int(hours[0]))226 for hour in hours[1:]:227 job.hour.also.on(int(hour))228 elif period == 'restart':229 print_text('Starting over.')230 get_run_time(job)231 break232 else:233 print_text('Your options are [i], [s], or [restart]. Restarting due to bad input.')234 get_run_time(job)235 break236 elif choice == 'restart':237 print_text('Starting over.')238 get_run_time(job)239 break240 else:241 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')242 get_run_time(job)243 break244 elif c == 'restart':245 print_text('Starting over.')246 get_run_time(job)247 break248 else:249 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')250 get_run_time(job)251 break252 print_text('Will this job run every day of the week? y|n')253 c = input().lower()254 if c == 'y':255 pass256 elif c == 'n':257 day_of_week = True258 print_text('Does this job run only one day of the week? y|n')259 choice = input()260 if choice == 'y':261 print_text('What day of the week will it run? (0=Sun, 1=Mon, etc...) 0-6')262 week_day = input()263 if week_day == 'restart':264 print_text('Starting over.')265 get_run_time(job)266 break267 job.dow.on(int(week_day))268 elif choice == 'n':269 print_text('Will this job run on an [i]nterval (every <n-th> day per week) or on [s]pecific days of the week? i|s')270 period = input().lower()271 if period == 'i':272 week_interval = True273 print_text('Every which day each week do you want this to run (every nth day)? 0-6')274 interval = input()275 if interval == 'restart':276 print_text('Starting over.')277 get_run_time(job)278 break279 job.dow.every(int(interval))280 elif period == 's':281 print_text('What days of the week will it run? (comma delimited, no spaces) 0-6')282 week_days = input().split(',')283 if week_days == ['restart']:284 print_text('Starting over.')285 get_run_time(job)286 break287 job.dow.on(int(week_days[0]))288 for day in week_days[1:]:289 job.dow.also.on(int(day))290 elif period == 'restart':291 print_text('Starting over.')292 get_run_time(job)293 break294 else:295 print_text('Your options are [i], [s], or [restart]. Restarting due to bad input.')296 get_run_time(job)297 break298 elif choice == 'restart':299 print_text('Starting over.')300 get_run_time(job)301 break302 else:303 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')304 get_run_time(job)305 break306 elif c == 'restart':307 print_text('Starting over.')308 get_run_time(job)309 break310 else:311 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')312 get_run_time(job)313 break314 if day_of_week:315 print_text('Since you have specified a per-week schedule, do you want this to ONLY run on a per-week schedule?')316 print_text('(as opposed to a per-week AND per-month schedule) y|n')317 if week_interval:318 print_text('Keep in mind that since you have specified an interval of the week for the job to run,')319 print_text('choosing a per-month period to run will make the job called only when the schedule')320 print_text('matches BOTH your weekly interval AND your monthly scheduled time. For example: Running')321 print_text('every Friday if it is ALSO every 5th day of the month.')322 else:323 print_text('Will this job run every day of the month? y|n')324 c = input().lower()325 if c == 'y':326 pass327 elif c == 'n':328 print_text('Will this job run only once a month? y|n')329 choice = input().lower()330 if choice == 'y':331 print_text('What day of the month will it run on? 1-31')332 if day_of_week:333 if week_interval:334 print_text('Since you have specified a week-interval for this to run, this will run on both')335 print_text('the day/days of the week you chose IF it is AlSO the day of the month you choose now.')336 else:337 print_text('Since you have specified day/days of the week for this to run, this will run on both')338 print_text('the day/days of the week you chose AND the day of the month you choose now.')339 day = input()340 if day == 'restart':341 print_text('Starting over.')342 get_run_time(job)343 break344 job.dom.on(int(day))345 elif choice == 'n':346 print_text('Will this job run on an [i]nterval (every __ days per month) or on [s]pecified days each month? i|s')347 period = input().lower()348 if period == 'i':349 print_text('Every how many days per month will this to run? 0-31')350 if day_of_week:351 print_text('Since you have specified day/days of the week for this to run, this will run on')352 print_text('the day/days of the week you chose IF it ALSO aligns with the interval that you')353 print_text('specify now.')354 interval = input()355 if interval == 'restart':356 print_text('Starting over.')357 get_run_time(job)358 break359 job.dom.every(int(interval))360 elif period == 's':361 print_text('What specific days of the month will this job run? (comma delimited, no spaces) 0-31')362 if day_of_week:363 print_text('Since you have specified day/days of the week for this to run, this will run on')364 print_text('both the day/days of the week you chose AND the days of the month you choose now.')365 specific = input().split(',')366 if specific == ['restart']:367 print_text('Starting over.')368 get_run_time(job)369 break370 job.dom.on(int(specific[0]))371 for day in specific[1:]:372 job.dom.also.on(int(day))373 elif period == 'restart':374 print_text('Starting over.')375 get_run_time(job)376 break377 else:378 print_text('Your options are [i], [s], or [restart]. Restarting due to bad input.')379 get_run_time(job)380 break381 elif choice == 'restart':382 print_text('Starting over.')383 get_run_time(job)384 break385 else:386 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')387 get_run_time(job)388 break389 elif c == 'restart':390 print_text('Starting over.')391 get_run_time(job)392 break393 else:394 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')395 print_text('Will this job run every month of the year? y|n')396 c = input().lower()397 if c == 'y':398 pass399 elif c == 'n':400 print_text('Will this job run only one month of the year? y|n')401 choice = input().lower()402 if choice == 'y':403 print_text('What month will this run? 1-12')404 month = input()405 if month == 'restart':406 print_text('Starting over.')407 get_run_time(job)408 break409 job.month.on(int(month))410 elif choice == 'n':411 print_text('Will this job run on an [i]nterval (every __ months per year) or on [s]pecific months? i|s')412 period = input().lower()413 if period == 'i':414 print_text('Every how many months will this run? 1-12')415 interval = input()416 if interval == 'restart':417 print_text('Starting over.')418 get_run_time(job)419 break420 job.month.every(int(interval))421 elif period == 's':422 print_text('What specific months will this job run? (comma delimited, no spaces) 1-12')423 months = input().split(',')424 if months == ['restart']:425 print_text('Starting over.')426 get_run_time(job)427 break428 job.month.on(int(months[0]))429 for month in months[1:]:430 job.month.also.on(int(month))431 elif period == 'restart':432 print_text('Starting over.')433 get_run_time(job)434 break435 else:436 print_text('Your options are [i], [s], or [restart]. Restarting due to bad input.')437 get_run_time(job)438 break439 elif choice == 'restart':440 print_text('Starting over.')441 get_run_time(job)442 break443 else:444 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')445 get_run_time(job)446 break447 elif c == 'restart':448 print_text('Starting over.')449 get_run_time(job)450 break451 else:452 print_text('Your options are [y], [n], or [restart]. Restarting due to bad input.')453 break454def print_text(text):455 text = text456 for i in str(text):457 sys.stdout.write(i)458 sys.stdout.flush()459 time.sleep(0.01)460 print()461if __name__ == '__main__':...
07-装饰器带参数.py
Source:07-装饰器带参数.py
1"""2éæ±:åæ°ä¼ å
¥0 å¸ææ¶é´ç¨æ´æ°æ¾ç¤º,åæ°ä¼ å
¥1 ç¨æµ®ç¹æ°æ¾ç¤º3"""4import time5def get_run_time(flag):6 """è£
饰å¨å·¥åå½æ°"""7 def get_time(func):8 """è£
饰å¨å½æ°:对å½æ°è¿è¡æ¶é´è¿è¡ç»è®¡"""9 print('in get_time')10 def inner(*args, **kwargs):11 t1 = time.time()12 res = func(*args, **kwargs)13 t2 = time.time()14 if flag == 0:15 print('è¿è¡äº%d s' % (t2 - t1))16 else:17 print('è¿è¡äº%f s' % (t2 - t1))18 return res19 return inner20 return get_time21# è£
饰å¨å·¥åå½æ°çä½ç¨:22# 1.>æ¥æ¶è£
饰å¨å½æ°æéè¦ä½æ¯åä¸è½ç´æ¥æ¥åçåæ°---->æ¥æ¶åæ°23# 2.>ç产è£
饰å¨å¯¹è±¡---->产çè£
饰å¨å½æ°24# å
³è: è£
饰å¨å·¥åå
é¨æ¯è£
饰å¨å½æ°25# çæ£æ§è¡è¿ç¨:26# 1.> get_time = get_run_time(åæ°)27# 2.> @get_time 对func1å½æ°è¿è¡è£
饰 func1 = get_time(func1)28@get_run_time(0) # è¿å¥è¯çè¿åå¼æ¯get_timeè¿ä¸ªå½æ°å29def func1(num, age=18):30 for i in range(3):31 time.sleep(1)32 print('in func', num, age)33# @get_run_time(1) 注æ:è¿ä¸ªè¦åå¼ç f1 = get_run_time(1) è¿æ¯ä¸ä¸ªæ´ä½,å½æ°è°ç¨,ç¶åè¿åä¸ä¸ªå¼34# @f1 è¿ææ¯çæ£çè£
饰å¨å½æ°å¼å§äº35func1(89)36"""37é®é¢:381.>è£
饰å¨å·¥åå½æ°æ¯è£
饰å¨å½æ°å?39ä¸æ¯,å·¥åå
é¨å®ä¹äºè£
饰å¨å½æ°,并ä¸returnè£
饰å¨å½æ°çå¼ç¨402.>è£
饰å¨å·¥åå½æ°åè£
饰å¨çå
³ç³»?41å·¥åçè¿åå¼æ¯è£
饰å¨å½æ°çå¼ç¨.å®çä½ç¨å°±æ¯å建ä¸ä¸ªè£
饰å¨å½æ°ç对象<æè
å«å°åå¼ç¨æ´åé>...
example02.py
Source:example02.py
2"""3éæ±:flag åæ°ä¼ å
¥0 å¸ææ¶é´ç¨æ´æ°æ¾ç¤º,åæ°ä¼ å
¥é0, ç¨æµ®ç¹æ°æ¾ç¤º4"""5import time6def get_run_time(flag):7 """è£
饰å¨å·¥åå½æ°"""8 def get_time(func):9 """è£
饰å¨å½æ°:对å½æ°è¿è¡æ¶é´è¿è¡ç»è®¡"""10 print('in get_time')11 def inner(*args, **kwargs):12 t1 = time.time()13 res = func(*args, **kwargs)14 t2 = time.time()15 if flag == 0:16 print('è¿è¡äº%d s' % (t2 - t1))17 else:18 print('è¿è¡äº%f s' % (t2 - t1))19 return res20 return inner21 return get_time22# è£
饰å¨å·¥åå½æ°çä½ç¨:23# 1.>æ¥æ¶è£
饰å¨å½æ°æéè¦ä½æ¯åä¸è½ç´æ¥æ¥åçåæ°---->æ¥æ¶åæ°24# 2.>ç产è£
饰å¨å¯¹è±¡---->产çè£
饰å¨å½æ°25# å
³è: è£
饰å¨å·¥åå
é¨æ¯è£
饰å¨å½æ°26# çæ£æ§è¡è¿ç¨:27# 1.> get_time = get_run_time(åæ°)28# 2.> @get_time 对func1å½æ°è¿è¡è£
饰 func1 = get_time(func1)29@get_run_time(1) # è¿å¥è¯çè¿åå¼æ¯get_timeè¿ä¸ªå½æ°å30def func1(num, age=18):31 for i in range(3):32 time.sleep(1)33 print('in func', num, age)34# @get_run_time(1) 注æ:è¿ä¸ªè¦åå¼ç f1 = get_run_time(1) è¿æ¯ä¸ä¸ªæ´ä½,å½æ°è°ç¨,ç¶åè¿åä¸ä¸ªå¼35# @f1 è¿ææ¯çæ£çè£
饰å¨å½æ°å¼å§äº...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!