-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileMaker.py
171 lines (140 loc) · 4.94 KB
/
fileMaker.py
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
"""
Cameron Padua
Group Project
fileMaker
This script will create different network shapes files. Currently it supports
star, mesh, fully connected, line, and tree. If you would like to create an
individual file, you call the method responsible for creating it. By default,
each method will create a network with 10 nodes that are bidirectional. If you
pass in an integer value to the method, you can vary the size of the number of
nodes. Additionally, you can call the main method to create every type of network
file. By default, they will all have 10 nodes, but if you pass a integer value,
you can vary the size.
Note: Code written for Python 3.6
Notes to Use:
import fileMaker
call individual methods
Example
fileMaker.star(n)
IGNORE, ONLY USE IF YOU UN-COMMENT EVAL LINE AT THE BOTTOM
if you are in a linux enviroment, make sure to chmod +x the file
run the script by using ./fileMaker 'method()'
for example ./fileMaker 'main(14)' or ./fileMaker 'star(14)'
if you are using Windows, you will need to do something like (I think)
pyhton fileMaker.py 'method(10)'
"""
import numpy as np
import random
import sys
def line(maxNode):
if(maxNode < 2):
raise Exception('Cannot create shape with 1 node')
#fileName = "line"+str(maxNode)+".txt"
fileName = "line.txt"
File = open(fileName,"w")
for x in range(1,maxNode):
writePairs(File, x, x+1)
File.close()
print("Line File made. File is named ", fileName)
def fullConnected(maxNode = 10):
if(maxNode < 2):
raise Exception('Cannot create shape with 1 node')
record = np.zeros((maxNode+1,maxNode+1),dtype=bool)
#fileName = "fullyConnected"+str(maxNode)+".txt"
fileName = "fullconnect.txt"
File = open(fileName,"w")
for x in range(1,maxNode):
#create ring of values:
writePairs(File, x, x+1)
record[x, x+1] = True
record[x+1, x] = True
record[x, x] = True
record[maxNode, maxNode] = True
for y in range(1,maxNode+1):
for x in range(1,maxNode+1):
if(record[y, x] == False):
writePairs(File, x, y)
record[y, x] = True
record[x, y] = True
File.close()
print("Fully connected File made. File is named ", fileName)
def mesh(maxNode = 10):
if(maxNode < 2):
raise Exception('Cannot create shape with 1 node')
record = np.zeros((maxNode+1,maxNode+1),dtype=bool)
#fileName = "mesh"+str(maxNode)+".txt"
fileName = "mesh.txt"
File = open(fileName,"w")
for x in range(1,maxNode):
#remove self values from mix
record[x, x] = True
record[maxNode, maxNode] = True
for y in range(1,maxNode+1):
for x in range(1,maxNode+1):
if random.random() > .8:
if(record[y, x] == False):
writePairs(File, x, y)
record[y, x] = True
record[x, y] = True
File.close()
# print("Mesh File made. File is named ", fileName)
def ring(maxNode = 10):
if(maxNode < 2):
raise Exception('Cannot create shape with 1 node')
#fileName = "ring"+str(maxNode)+".txt"
fileName = "ring.txt"
File = open(fileName,"w")
for x in range(1,maxNode):
writePairs(File, x, x+1)
writePairs(File, maxNode, 1)
File.close()
print("Ring File made. File is named ", fileName)
def star(maxNode = 10):
if(maxNode < 2):
raise Exception('Cannot create shape with 1 node')
#fileName = "star"+str(maxNode)+".txt"
fileName = "star.txt"
File = open(fileName,"w")
for x in range(1,maxNode):
writePairs(File, 1, x+1)
File.close()
print("Star File made. File is named ", fileName)
def tree(maxNode = 10):
if(maxNode < 2):
raise Exception('Cannot create shape with 1 node')
#fileName = "tree"+str(maxNode)+".txt"
fileName = "tree.txt"
File = open(fileName,"w")
currentNode = 1
currentMaxNode = 2
while(currentMaxNode <= maxNode):
if currentNode*2 <= maxNode:
writePairs(File, currentNode, currentMaxNode)
currentMaxNode+=1
if currentNode*2 +1 <= maxNode:
writePairs(File, currentNode, currentMaxNode)
currentMaxNode+=1
currentNode+=1
File.close()
print("Tree File made. File is named ", fileName)
def writePairs(File, val1, val2):
"""
Writes a pair of integers to a file.
Input: File: a file object to write to.
val1: an integer to write to the file
va12: an integer to write to the file
Return: Nothing
"""
File.write(str(val1))
File.write("\t")
File.write(str(val2))
File.write("\n")
def main(maxNumber = 10):
line(maxNumber)
ring(maxNumber)
star(maxNumber)
tree(maxNumber)
fullConnected(maxNumber)
mesh(maxNumber)
#eval(sys.argv[1])