Best Python code snippet using green
pwsettings.py
Source:pwsettings.py
...77 cur_max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24))78 except Exception, e:79 raise CommandError("Could not retrieve password properties!", e)80 if subcommand == "show":81 self.message("Password informations for domain '%s'" % domain_dn)82 self.message("")83 if pwd_props & DOMAIN_PASSWORD_COMPLEX != 0:84 self.message("Password complexity: on")85 else:86 self.message("Password complexity: off")87 if pwd_props & DOMAIN_PASSWORD_STORE_CLEARTEXT != 0:88 self.message("Store plaintext passwords: on")89 else:90 self.message("Store plaintext passwords: off")91 self.message("Password history length: %d" % pwd_hist_len)92 self.message("Minimum password length: %d" % cur_min_pwd_len)93 self.message("Minimum password age (days): %d" % cur_min_pwd_age)94 self.message("Maximum password age (days): %d" % cur_max_pwd_age)95 elif subcommand == "set":96 msgs = []97 m = ldb.Message()98 m.dn = ldb.Dn(samdb, domain_dn)99 if complexity is not None:100 if complexity == "on" or complexity == "default":101 pwd_props = pwd_props | DOMAIN_PASSWORD_COMPLEX102 msgs.append("Password complexity activated!")103 elif complexity == "off":104 pwd_props = pwd_props & (~DOMAIN_PASSWORD_COMPLEX)105 msgs.append("Password complexity deactivated!")106 if store_plaintext is not None:107 if store_plaintext == "on" or store_plaintext == "default":108 pwd_props = pwd_props | DOMAIN_PASSWORD_STORE_CLEARTEXT109 msgs.append("Plaintext password storage for changed passwords activated!")110 elif store_plaintext == "off":111 pwd_props = pwd_props & (~DOMAIN_PASSWORD_STORE_CLEARTEXT)112 msgs.append("Plaintext password storage for changed passwords deactivated!")113 if complexity is not None or store_plaintext is not None:114 m["pwdProperties"] = ldb.MessageElement(str(pwd_props),115 ldb.FLAG_MOD_REPLACE, "pwdProperties")116 if history_length is not None:117 if history_length == "default":118 pwd_hist_len = 24119 else:120 pwd_hist_len = int(history_length)121 if pwd_hist_len < 0 or pwd_hist_len > 24:122 raise CommandError("Password history length must be in the range of 0 to 24!")123 m["pwdHistoryLength"] = ldb.MessageElement(str(pwd_hist_len),124 ldb.FLAG_MOD_REPLACE, "pwdHistoryLength")125 msgs.append("Password history length changed!")126 if min_pwd_length is not None:127 if min_pwd_length == "default":128 min_pwd_len = 7129 else:130 min_pwd_len = int(min_pwd_length)131 if min_pwd_len < 0 or min_pwd_len > 14:132 raise CommandError("Minimum password length must be in the range of 0 to 14!")133 m["minPwdLength"] = ldb.MessageElement(str(min_pwd_len),134 ldb.FLAG_MOD_REPLACE, "minPwdLength")135 msgs.append("Minimum password length changed!")136 if min_pwd_age is not None:137 if min_pwd_age == "default":138 min_pwd_age = 1139 else:140 min_pwd_age = int(min_pwd_age)141 if min_pwd_age < 0 or min_pwd_age > 998:142 raise CommandError("Minimum password age must be in the range of 0 to 998!")143 # days -> ticks144 min_pwd_age_ticks = -int(min_pwd_age * (24 * 60 * 60 * 1e7))145 m["minPwdAge"] = ldb.MessageElement(str(min_pwd_age_ticks),146 ldb.FLAG_MOD_REPLACE, "minPwdAge")147 msgs.append("Minimum password age changed!")148 if max_pwd_age is not None:149 if max_pwd_age == "default":150 max_pwd_age = 43151 else:152 max_pwd_age = int(max_pwd_age)153 if max_pwd_age < 0 or max_pwd_age > 999:154 raise CommandError("Maximum password age must be in the range of 0 to 999!")155 # days -> ticks156 max_pwd_age_ticks = -int(max_pwd_age * (24 * 60 * 60 * 1e7))157 m["maxPwdAge"] = ldb.MessageElement(str(max_pwd_age_ticks),158 ldb.FLAG_MOD_REPLACE, "maxPwdAge")159 msgs.append("Maximum password age changed!")160 if max_pwd_age > 0 and min_pwd_age >= max_pwd_age:161 raise CommandError("Maximum password age (%d) must be greater than minimum password age (%d)!" % (max_pwd_age, min_pwd_age))162 samdb.modify(m)163 msgs.append("All changes applied successfully!")164 self.message("\n".join(msgs))165 else:...
pwsettings
Source:pwsettings
...49opts, args = parser.parse_args()50#51# print a message if quiet is not set52#53def message(text):54 if not opts.quiet:55 print text56if len(args) == 0:57 parser.print_usage()58 sys.exit(1)59lp = sambaopts.get_loadparm()60creds = credopts.get_credentials(lp)61if opts.H is not None:62 url = opts.H63else:64 url = lp.get("sam database")65samdb = SamDB(url=url, session_info=system_session(), credentials=creds, lp=lp)66domain_dn = SamDB.domain_dn(samdb)67res = samdb.search(domain_dn, scope=ldb.SCOPE_BASE,68 attrs=["pwdProperties", "pwdHistoryLength", "minPwdLength", "minPwdAge",69 "maxPwdAge"])70assert(len(res) == 1)71try:72 pwd_props = int(res[0]["pwdProperties"][0])73 pwd_hist_len = int(res[0]["pwdHistoryLength"][0])74 min_pwd_len = int(res[0]["minPwdLength"][0])75 # ticks -> days76 min_pwd_age = int(abs(int(res[0]["minPwdAge"][0])) / (1e7 * 60 * 60 * 24))77 max_pwd_age = int(abs(int(res[0]["maxPwdAge"][0])) / (1e7 * 60 * 60 * 24))78except:79 print "ERROR: Could not retrieve password properties!"80 if args[0] == "show":81 print "So no settings can be displayed!"82 sys.exit(1)83if args[0] == "show":84 message("Password informations for domain '" + domain_dn + "'")85 message("")86 if pwd_props & DOMAIN_PASSWORD_COMPLEX != 0:87 message("Password complexity: on")88 else:89 message("Password complexity: off")90 message("Password history length: " + str(pwd_hist_len))91 message("Minimum password length: " + str(min_pwd_len))92 message("Minimum password age (days): " + str(min_pwd_age))93 message("Maximum password age (days): " + str(max_pwd_age))94elif args[0] == "set":95 msgs = []96 m = ldb.Message()97 m.dn = ldb.Dn(samdb, domain_dn)98 if opts.complexity is not None:99 if opts.complexity == "on" or opts.complexity == "default":100 pwd_props = pwd_props | DOMAIN_PASSWORD_COMPLEX101 msgs.append("Password complexity activated!")102 elif opts.complexity == "off":103 pwd_props = pwd_props & (~DOMAIN_PASSWORD_COMPLEX)104 msgs.append("Password complexity deactivated!")105 else:106 print "ERROR: Wrong argument '" + opts.complexity + "'!"107 sys.exit(1)108 m["pwdProperties"] = ldb.MessageElement(str(pwd_props),109 ldb.FLAG_MOD_REPLACE, "pwdProperties")110 if opts.history_length is not None:111 if opts.history_length == "default":112 pwd_hist_len = 24113 else:114 pwd_hist_len = int(opts.history_length)115 if pwd_hist_len < 0 or pwd_hist_len > 24:116 print "ERROR: Password history length must be in the range of 0 to 24!"117 sys.exit(1)118 m["pwdHistoryLength"] = ldb.MessageElement(str(pwd_hist_len),119 ldb.FLAG_MOD_REPLACE, "pwdHistoryLength")120 msgs.append("Password history length changed!")121 if opts.min_pwd_length is not None:122 if opts.min_pwd_length == "default":123 min_pwd_len = 7124 else:125 min_pwd_len = int(opts.min_pwd_length)126 if min_pwd_len < 0 or min_pwd_len > 14:127 print "ERROR: Minimum password length must be in the range of 0 to 14!"128 sys.exit(1)129 m["minPwdLength"] = ldb.MessageElement(str(min_pwd_len),130 ldb.FLAG_MOD_REPLACE, "minPwdLength")131 msgs.append("Minimum password length changed!")132 if opts.min_pwd_age is not None:133 if opts.min_pwd_age == "default":134 min_pwd_age = 0135 else:136 min_pwd_age = int(opts.min_pwd_age)137 if min_pwd_age < 0 or min_pwd_age > 998:138 print "ERROR: Minimum password age must be in the range of 0 to 998!"139 sys.exit(1)140 # days -> ticks141 min_pwd_age_ticks = -int(min_pwd_age * (24 * 60 * 60 * 1e7))142 m["minPwdAge"] = ldb.MessageElement(str(min_pwd_age_ticks),143 ldb.FLAG_MOD_REPLACE, "minPwdAge")144 msgs.append("Minimum password age changed!")145 if opts.max_pwd_age is not None:146 if opts.max_pwd_age == "default":147 max_pwd_age = 43148 else:149 max_pwd_age = int(opts.max_pwd_age)150 if max_pwd_age < 0 or max_pwd_age > 999:151 print "ERROR: Maximum password age must be in the range of 0 to 999!"152 sys.exit(1)153 # days -> ticks154 max_pwd_age_ticks = -int(max_pwd_age * (24 * 60 * 60 * 1e7))155 m["maxPwdAge"] = ldb.MessageElement(str(max_pwd_age_ticks),156 ldb.FLAG_MOD_REPLACE, "maxPwdAge")157 msgs.append("Maximum password age changed!")158 if max_pwd_age > 0 and min_pwd_age >= max_pwd_age:159 print "ERROR: Maximum password age (%d) must be greater than minimum password age (%d)!" % (max_pwd_age, min_pwd_age)160 sys.exit(1)161 samdb.modify(m)162 msgs.append("All changes applied successfully!")163 message("\n".join(msgs))164else:165 print "ERROR: Wrong argument '" + args[0] + "'!"...
03_count.py
Source:03_count.py
1from collections import defaultdict2import math3user2demo = {}4with open("../../data/screenname_agegroup_race_gender_NY.txt") as fi:5 for line_cnt, line in enumerate(fi):6 screen_name, age, age_range, age_group, race, race_conf, gender, gender_conf = [term.strip() for term in line.split("\t")]7 user2demo[screen_name] = [age_group, gender, race, age]8print "len(user2demo) = ", len(user2demo)9cnt_type = defaultdict(int)10cnt_matched = defaultdict(int)11age_confusion = defaultdict(lambda: defaultdict(int))12gender_confusion = defaultdict(lambda: defaultdict(int))13new_cnt = defaultdict(int)14with open("./pattern_matched_users_2.txt") as fi, open("validation_age_diff.txt", "w") as output:15 for line_cnt, line in enumerate(fi):16 demotype, myvalue, screen_name = [term.strip() for term in line.split("\t")]17 if demotype == "age":18 age = int(myvalue)19 int_age = int(myvalue)20 myagegroup = ""21 if age <= 17:22 myagegroup = "age-17"23 elif age >= 18 and age <= 24:24 myagegroup = "age18-24"25 elif age >= 25 and age <= 34:26 myagegroup = "age25-34"27 elif age >= 35 and age <= 44:28 myagegroup = "age35-44"29 elif age >= 45 and age <= 54:30 myagegroup = "age45-54"31 elif age >= 55:32 myagegroup = "age55-"33 myvalue = myagegroup34 cnt_type[myvalue] += 135 # if screen_name in user2demo:36 age_group, gender, race, age = [term.strip() for term in user2demo[screen_name]]37 if demotype == "gender":38 if myvalue == gender:39 cnt_matched[myvalue] += 140 gender_confusion[gender][myvalue] += 141 new_cnt[gender] += 142 if demotype == "age":43 if myvalue == age_group:44 cnt_matched[myvalue] += 145 age_confusion[age_group][myvalue] += 146 new_cnt[age_group] += 147 diff_age = int(math.fabs(int_age - int(age)))48 output.write(str(diff_age)+"\n")49 50for group in sorted(cnt_matched):51 print group, cnt_type[group], cnt_matched[group]52myheader = [""]53for group in sorted(gender_confusion):54 group = group.replace("age","")55 myheader.append("\\textbf{%s}" % (group))56myheader.append("\\textbf{%s}" % ("Total"))57print " & ".join(myheader), "\\\\"58print "\\midrule"59for face_age in sorted(gender_confusion):60 mytotal = 061 myresult = ["\\textbf{%s}" % (face_age)]62 for bio_age in sorted(gender_confusion[face_age]):63 myresult.append(str(gender_confusion[face_age][bio_age]))64 mytotal += gender_confusion[face_age][bio_age]65 myresult.append(str(mytotal))66 print " & ".join(myresult), "\\\\"67 # print ", ".join(myresult)68total_2 = 069myresult = ["\\textbf{%s}" % ("Total")]70for bio_age in sorted(gender_confusion):71 myresult.append(str(cnt_type[bio_age]))72 total_2 += cnt_type[bio_age]73myresult.append(str(total_2))74print "\\midrule"75print " & ".join(myresult), "\\\\"76precisions = {}77recalls = {}78for face_age in sorted(gender_confusion):79 for bio_age in sorted(gender_confusion[face_age]):80 value = gender_confusion[face_age][bio_age]81 if face_age == bio_age:82 tp = value83 totalpredicted = new_cnt[face_age]84 totalgoldlable = cnt_type[face_age]85 86 recall = float(tp)/float(totalgoldlable)87 precision = float(tp)/float(totalpredicted)88 precisions[face_age] = "%.2f" % (precision)89 recalls[face_age] = "%.2f" % (recall)90myresult = [""]91for face_age in sorted(precisions):92 myresult.append("\\textbf{%s}" % (face_age))93print " & ".join(myresult), "\\\\"94print "\\midrule"95myresult = ["\\textbf{%s}" % ("Precision")]96for face_age in sorted(precisions):97 myresult.append(precisions[face_age])98print " & ".join(myresult), "\\\\"99myresult = ["\\textbf{%s}" % ("Recall")] 100for face_age in sorted(recalls):101 myresult.append(recalls[face_age])102print " & ".join(myresult), "\\\\"103myheader = [""]104for group in sorted(age_confusion):105 group = group.replace("age","")106 myheader.append("\\textbf{%s}" % (group))107myheader.append("\\textbf{%s}" % ("Total"))108print " & ".join(myheader), "\\\\"109print "\\midrule"110for face_age in sorted(age_confusion):111 mytotal = 0112 myresult = ["\\textbf{%s}" % (face_age)]113 for bio_age in sorted(age_confusion[face_age]):114 myresult.append(str(age_confusion[face_age][bio_age]))115 mytotal += age_confusion[face_age][bio_age]116 myresult.append(str(mytotal))117 print " & ".join(myresult), "\\\\"118 # print ", ".join(myresult)119total_2 = 0120myresult = ["\\textbf{%s}" % ("Total")]121for bio_age in sorted(age_confusion):122 myresult.append(str(cnt_type[bio_age]))123 total_2 += cnt_type[bio_age]124myresult.append(str(total_2))125print "\\midrule"126print " & ".join(myresult), "\\\\"127precisions = {}128recalls = {}129for face_age in sorted(age_confusion):130 for bio_age in sorted(age_confusion[face_age]):131 value = age_confusion[face_age][bio_age]132 if face_age == bio_age:133 tp = value134 totalpredicted = new_cnt[face_age]135 totalgoldlable = cnt_type[face_age]136 137 recall = float(tp)/float(totalgoldlable)138 precision = float(tp)/float(totalpredicted)139 precisions[face_age] = "%.2f" % (precision)140 recalls[face_age] = "%.2f" % (recall)141myresult = [""]142for face_age in sorted(precisions):143 myresult.append("\\textbf{%s}" % (face_age))144print " & ".join(myresult), "\\\\"145print "\\midrule"146myresult = ["\\textbf{%s}" % ("Precision")]147for face_age in sorted(precisions):148 myresult.append(precisions[face_age])149print " & ".join(myresult), "\\\\"150myresult = ["\\textbf{%s}" % ("Recall")] 151for face_age in sorted(recalls):152 myresult.append(recalls[face_age])...
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!!