(password)
| 315 | |
| 316 | |
| 317 | def password_score(password): # type: (str) -> int |
| 318 | score = 0 |
| 319 | if not password: |
| 320 | return score |
| 321 | if not isinstance(password, str): |
| 322 | return score |
| 323 | |
| 324 | total = len(password) |
| 325 | if total > 50: |
| 326 | # this password score implementation hangs if password is too long |
| 327 | password = password[:50] |
| 328 | total = 50 |
| 329 | uppers = 0 |
| 330 | lowers = 0 |
| 331 | digits = 0 |
| 332 | symbols = 0 |
| 333 | for x in password: |
| 334 | if x.isupper(): |
| 335 | uppers += 1 |
| 336 | elif x.islower(): |
| 337 | lowers += 1 |
| 338 | elif x.isdecimal(): |
| 339 | digits += 1 |
| 340 | else: |
| 341 | symbols += 1 |
| 342 | |
| 343 | ds = digits + symbols |
| 344 | if not password[0].isalpha(): |
| 345 | ds -= 1 |
| 346 | if not password[-1].isalpha(): |
| 347 | ds -= 1 |
| 348 | if ds < 0: |
| 349 | ds = 0 |
| 350 | |
| 351 | score += total * 4 |
| 352 | if uppers > 0: |
| 353 | score += (total-uppers) * 2 |
| 354 | if lowers > 0: |
| 355 | score += (total-lowers) * 2 |
| 356 | if digits > 0: |
| 357 | score += digits * 4 |
| 358 | score += symbols * 6 |
| 359 | score += ds * 2 |
| 360 | |
| 361 | variance = 0 |
| 362 | if uppers > 0: |
| 363 | variance += 1 |
| 364 | if lowers > 0: |
| 365 | variance += 1 |
| 366 | if digits > 0: |
| 367 | variance += 1 |
| 368 | if symbols > 0: |
| 369 | variance += 1 |
| 370 | if total >= 8 and variance >= 3: |
| 371 | score += (variance + 1) * 2 |
| 372 | |
| 373 | if digits + symbols == 0: |
| 374 | score -= total |
no test coverage detected