Repository navigation
Expand file tree
/
Copy pathguess_password.py
More file actions
61 lines (44 loc) · 1.8 KB
/
Copy pathguess_password.py
File metadata and controls
61 lines (44 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import random
import string
import datetime
import unittest
import genetic_a
def get_fitness(genes, target):
return sum(1 for expected, actual in zip(target, genes)
if expected == actual)
def display_fitness(organism, start_time):
time_elapsed = datetime.datetime.now() - start_time
print(f"Organism: {''.join(organism.Genes)}\t"
f"Fitness: {organism.Fitness}\t"
f"Time Elapsed: {str(time_elapsed)}")
class GuessPasswordTests(unittest.TestCase):
gene_pool = string.ascii_lowercase + string.ascii_uppercase + " !.,"
def guess_password(self, target):
start_time = datetime.datetime.now()
def runGetFitness(genes):
return get_fitness(genes, target)
def runDisplayFitness(candidate):
display_fitness(candidate, start_time)
optimal_fitness = len(target)
best = genetic_a.get_best(runGetFitness, len(target), optimal_fitness,
self.gene_pool, runDisplayFitness)
self.assertEqual(''.join(best.Genes), target)
def test_Hello_World(self):
print("\nInitiating Hello World Unit Test")
target = "Hello World!"
self.guess_password(target)
def test_extremely_complex_password(self):
print("\nInitiating Extremely Complex Password Unit Test")
target = "ExtreMEly ComplEX PasSwoRd!..."
self.guess_password(target)
def test_Random(self):
print("\nInitiating Long Password Unit Test")
length = 150
target = ''.join(random.choice(self.gene_pool)
for _ in range(length))
self.guess_password(target)
def test_benchmark(self):
print("\nInitiating Benchmark Test")
genetic_a.Benchmark.run(self.test_Random)
if __name__ == '__main__':
unittest.main()