Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
verktoy
PyMetric
Commits
49f4dad4
Commit
49f4dad4
authored
Dec 19, 2018
by
Morten Knutsen
Browse files
Conversions and adjustments for Python3.
parent
b32bdde8
Changes
8
Expand all
Hide whitespace changes
Inline
Side-by-side
command.py
View file @
49f4dad4
This diff is collapsed.
Click to expand it.
metrics.py
View file @
49f4dad4
...
...
@@ -31,7 +31,7 @@ if __name__ == '__main__':
import
sys
if
not
len
(
sys
.
argv
)
>
1
:
print
"Please specify topology-file"
print
(
"Please specify topology-file"
)
sys
.
exit
(
1
)
infile
=
sys
.
argv
[
1
]
...
...
@@ -48,7 +48,7 @@ if __name__ == '__main__':
outpng
=
None
if
sys
.
argv
[
2
]
==
"-s"
:
if
not
len
(
sys
.
argv
)
==
4
:
print
"-s requires a script-file"
print
(
"-s requires a script-file"
)
sys
.
exit
(
1
)
else
:
scriptFile
=
sys
.
argv
[
3
]
...
...
model.py
View file @
49f4dad4
This diff is collapsed.
Click to expand it.
pajek.py
View file @
49f4dad4
...
...
@@ -37,7 +37,7 @@ def parse_pajek(lines):
directed
=
True
# assume this is a directed network for now
while
lines
:
try
:
l
=
lines
.
next
()
l
=
next
(
lines
)
l
=
l
.
lower
()
except
:
#EOF
break
...
...
@@ -51,11 +51,11 @@ def parse_pajek(lines):
l
,
nnodes
=
l
.
split
()
while
not
l
.
startswith
(
"*arcs"
):
if
l
.
startswith
(
'#'
):
l
=
lines
.
next
()
l
=
next
(
lines
)
l
=
l
.
lower
()
continue
if
l
.
startswith
(
'*'
):
l
=
lines
.
next
()
l
=
next
(
lines
)
l
=
l
.
lower
()
continue
splitline
=
shlex
.
split
(
l
)
...
...
@@ -67,10 +67,10 @@ def parse_pajek(lines):
if
len
(
splitline
)
>
2
:
id
,
label
,
x
,
y
=
splitline
[
0
:
4
]
G
.
node_attr
[
label
]
=
{
'id'
:
id
,
'x'
:
x
,
'y'
:
y
}
extra_attr
=
zip
(
splitline
[
4
::
2
],
splitline
[
5
::
2
])
extra_attr
=
list
(
zip
(
splitline
[
4
::
2
],
splitline
[
5
::
2
])
)
#print extra_attr
G
.
node_attr
[
label
].
update
(
extra_attr
)
l
=
lines
.
next
()
l
=
next
(
lines
)
l
=
l
.
lower
()
if
l
.
startswith
(
"*arcs"
):
for
l
in
lines
:
...
...
@@ -81,7 +81,7 @@ def parse_pajek(lines):
u
=
nodelabels
.
get
(
ui
,
ui
)
v
=
nodelabels
.
get
(
vi
,
vi
)
edge_data
=
{
'value'
:
float
(
w
)}
extra_attr
=
zip
(
splitline
[
3
::
2
],
splitline
[
4
::
2
])
extra_attr
=
list
(
zip
(
splitline
[
3
::
2
],
splitline
[
4
::
2
])
)
edge_data
.
update
(
extra_attr
)
if
G
.
has_edge
(
u
,
v
):
if
G
[
u
][
v
][
'value'
]
>
float
(
w
):
...
...
plotting.py
View file @
49f4dad4
...
...
@@ -248,7 +248,7 @@ class PlotUI:
edgewidths
=
[
PlotUI
.
edgewidths
[
edge_capa
[(
edges
[
i
][
0
],
edges
[
i
][
1
])]]
for
i
in
range
(
len
(
edges
))]
except
KeyError
:
print
"Error: Unknown or missing capacity information provided to plot command"
print
(
"Error: Unknown or missing capacity information provided to plot command"
)
return
self
.
edgec
=
nx
.
draw_networkx_edges
(
graph
,
pos
=
data
[
'pos'
],
...
...
scripting.py
View file @
49f4dad4
...
...
@@ -24,12 +24,12 @@ class ScriptEngine():
def
run
(
self
,
script
):
if
not
os
.
path
.
isfile
(
script
):
print
>>
sys
.
stderr
,
"No such file: %s"
%
script
print
(
"No such file: %s"
%
script
,
file
=
sys
.
stderr
)
return
1
try
:
fh
=
open
(
script
,
"r"
)
except
:
print
>>
sys
.
stderr
,
"Something went wrong when trying to open scriptfile"
print
(
"Something went wrong when trying to open scriptfile"
,
file
=
sys
.
stderr
)
return
1
self
.
state
=
self
.
STATE_OPEN
self
.
current_script
=
script
...
...
@@ -49,7 +49,7 @@ class ScriptEngine():
elif
line
.
startswith
(
'end'
):
self
.
_do_end
()
else
:
self
.
state
=
self
.
STATE_ERROR
print
>>
sys
.
stderr
,
"Syntax error at line %d: Unknown keyword"
%
self
.
current_line
print
(
"Syntax error at line %d: Unknown keyword"
%
self
.
current_line
,
file
=
sys
.
stderr
)
if
self
.
state
==
self
.
STATE_END
:
self
.
current_script
=
None
...
...
@@ -66,7 +66,7 @@ class ScriptEngine():
def
_do_begin
(
self
):
if
self
.
state
>
self
.
STATE_OPEN
:
print
>>
sys
.
stderr
,
"Syntax error at line %d: 'begin' allready specified"
%
self
.
current_line
print
(
"Syntax error at line %d: 'begin' allready specified"
%
self
.
current_line
,
file
=
sys
.
stderr
)
self
.
state
=
self
.
STATE_ERROR
return
self
.
cli
.
onecmd
(
"sim"
)
...
...
@@ -75,7 +75,7 @@ class ScriptEngine():
def
_do_reset
(
self
):
if
self
.
state
<=
self
.
STATE_BEGIN
:
print
>>
sys
.
stderr
,
"Warning: 'reset' without changes ignored (line %d)"
%
self
.
current_line
print
(
"Warning: 'reset' without changes ignored (line %d)"
%
self
.
current_line
,
file
=
sys
.
stderr
)
return
self
.
cli
.
onecmd
(
"stop"
)
assert
not
self
.
simulation
.
is_active
()
...
...
@@ -87,11 +87,11 @@ class ScriptEngine():
def
_do_linkfail
(
self
,
line
):
args
=
self
.
_get_args
(
line
)
if
not
len
(
args
)
==
2
:
print
>>
sys
.
stderr
,
"Syntax error at line %d: Wrong number of arguments for 'linkfail'"
%
self
.
current_line
print
(
"Syntax error at line %d: Wrong number of arguments for 'linkfail'"
%
self
.
current_line
,
file
=
sys
.
stderr
)
self
.
state
=
self
.
STATE_ERROR
return
if
not
self
.
state
>
self
.
STATE_OPEN
:
print
>>
sys
.
stderr
,
"Syntax error at line %d: Missing 'begin' statement before 'linkfail'"
%
self
.
current_line
print
(
"Syntax error at line %d: Missing 'begin' statement before 'linkfail'"
%
self
.
current_line
,
file
=
sys
.
stderr
)
self
.
state
=
self
.
STATE_ERROR
return
self
.
cli
.
onecmd
(
"linkfail %s %s"
%
(
args
[
0
],
args
[
1
]))
...
...
@@ -101,11 +101,11 @@ class ScriptEngine():
def
_do_assert
(
self
,
line
):
args
=
self
.
_get_args
(
line
)
if
not
len
(
args
)
>=
5
:
print
>>
sys
.
stderr
,
"Syntax error at line %d: Wrong number of arguments for 'assert'"
%
self
.
current_line
print
(
"Syntax error at line %d: Wrong number of arguments for 'assert'"
%
self
.
current_line
,
file
=
sys
.
stderr
)
self
.
state
=
self
.
STATE_ERROR
return
if
not
self
.
state
>
self
.
STATE_OPEN
:
print
>>
sys
.
stderr
,
"Syntax error at line %d: Missing 'begin' statement before 'assert'"
%
self
.
current_line
print
(
"Syntax error at line %d: Missing 'begin' statement before 'assert'"
%
self
.
current_line
,
file
=
sys
.
stderr
)
self
.
state
=
self
.
STATE_ERROR
return
#if not self.state == self.STATE_SIM:
...
...
@@ -115,48 +115,48 @@ class ScriptEngine():
self
.
loaddata
=
self
.
_read_savedata
()
link_key
=
"%s###%s"
%
(
args
[
1
],
args
[
2
])
if
not
self
.
loaddata
or
link_key
not
in
self
.
loaddata
:
print
>>
sys
.
stderr
,
"Warning: assertion not computed, no save data to compare with (line %d)"
%
self
.
current_line
print
(
"Warning: assertion not computed, no save data to compare with (line %d)"
%
self
.
current_line
,
file
=
sys
.
stderr
)
return
else
:
simpaths
=
self
.
simulation
.
path
(
args
[
1
],
args
[
2
])[
1
]
if
not
self
.
loaddata
[
link_key
]
==
simpaths
:
print
>>
sys
.
stderr
,
" FAIL: Assertion failed for %s -> %s (line %d)"
%
(
self
.
current_line
,
args
[
1
],
args
[
2
])
print
>>
sys
.
stderr
,
" Got: %s"
%
(
"
\n
"
.
join
(
map
(
str
,
simpaths
)))
print
>>
sys
.
stderr
,
" Expected: %s"
%
(
"
\n
"
.
join
(
map
(
str
,
self
.
loaddata
[
link_key
])))
print
(
" FAIL: Assertion failed for %s -> %s (line %d)"
%
(
self
.
current_line
,
args
[
1
],
args
[
2
])
,
file
=
sys
.
stderr
)
print
(
" Got: %s"
%
(
"
\n
"
.
join
(
map
(
str
,
simpaths
)))
,
file
=
sys
.
stderr
)
print
(
" Expected: %s"
%
(
"
\n
"
.
join
(
map
(
str
,
self
.
loaddata
[
link_key
])))
,
file
=
sys
.
stderr
)
self
.
state
=
self
.
STATE_FAIL
return
elif
args
[
3
]
==
'eq'
:
expected
=
eval
(
" "
.
join
(
args
[
4
:]))
simpaths
=
self
.
simulation
.
path
(
args
[
1
],
args
[
2
])[
1
]
if
not
expected
==
simpaths
:
print
>>
sys
.
stderr
,
" FAIL: Assertion failed (line %d)"
%
self
.
current_line
print
>>
sys
.
stderr
,
" Got: %s"
%
(
"
\n
"
.
join
(
map
(
str
,
simpaths
)))
print
>>
sys
.
stderr
,
" Expected: %s"
%
(
"
\n
"
.
join
(
map
(
str
,
expected
)))
print
(
" FAIL: Assertion failed (line %d)"
%
self
.
current_line
,
file
=
sys
.
stderr
)
print
(
" Got: %s"
%
(
"
\n
"
.
join
(
map
(
str
,
simpaths
)))
,
file
=
sys
.
stderr
)
print
(
" Expected: %s"
%
(
"
\n
"
.
join
(
map
(
str
,
expected
)))
,
file
=
sys
.
stderr
)
self
.
state
=
self
.
STATE_FAIL
return
else
:
print
>>
sys
.
stderr
,
"Error: Not implemented.... (line %d)"
%
(
self
.
current_line
)
print
(
"Error: Not implemented.... (line %d)"
%
(
self
.
current_line
)
,
file
=
sys
.
stderr
)
return
def
_do_save
(
self
,
line
):
args
=
self
.
_get_args
(
line
)
if
not
len
(
args
)
==
3
:
print
>>
sys
.
stderr
,
"Syntax error at line %d: Wrong number of arguments for 'save'"
%
self
.
current_line
print
(
"Syntax error at line %d: Wrong number of arguments for 'save'"
%
self
.
current_line
,
file
=
sys
.
stderr
)
self
.
state
=
self
.
STATE_ERROR
return
if
not
self
.
state
==
self
.
STATE_SIM
:
print
>>
sys
.
stderr
,
"Warning: Not saving (line %d)"
%
self
.
current_line
print
(
"Warning: Not saving (line %d)"
%
self
.
current_line
,
file
=
sys
.
stderr
)
return
simpaths
=
self
.
simulation
.
path
(
args
[
1
],
args
[
2
])[
1
]
if
not
simpaths
:
print
>>
sys
.
stderr
,
"Warning: Ignoring 'save' for non-existant path at line %d"
%
self
.
current_line
print
(
"Warning: Ignoring 'save' for non-existant path at line %d"
%
self
.
current_line
,
file
=
sys
.
stderr
)
return
link_key
=
"%s###%s"
%
(
args
[
1
],
args
[
2
])
self
.
savedata
[
link_key
]
=
simpaths
def
_do_end
(
self
):
if
not
self
.
state
>=
self
.
STATE_BEGIN
:
print
>>
sys
.
stderr
,
"Syntax error at line %d: Missing 'begin' before 'end'"
%
self
.
current_line
print
(
"Syntax error at line %d: Missing 'begin' before 'end'"
%
self
.
current_line
,
file
=
sys
.
stderr
)
self
.
state
=
self
.
STATE_ERROR
return
if
self
.
savedata
:
...
...
@@ -170,7 +170,7 @@ class ScriptEngine():
try
:
fh
=
open
(
self
.
current_script
+
".save"
,
"r"
)
except
IOError
:
print
>>
sys
.
stderr
,
"Warning: Could not open savefile"
print
(
"Warning: Could not open savefile"
,
file
=
sys
.
stderr
)
return
{}
retdata
=
{}
for
line
in
fh
.
readlines
():
...
...
@@ -183,7 +183,7 @@ class ScriptEngine():
try
:
fh
=
open
(
self
.
current_script
+
".save"
,
"w"
)
except
IOError
:
print
>>
sys
.
stderr
,
"Warning: Could not open savefile for writing"
print
(
"Warning: Could not open savefile for writing"
,
file
=
sys
.
stderr
)
return
for
key
in
self
.
savedata
:
fh
.
write
(
"%s %s
\n
"
%
(
key
,
self
.
savedata
[
key
].
__repr__
()))
...
...
termcolor.py
View file @
49f4dad4
...
...
@@ -24,7 +24,7 @@ __ALL__ = [ 'colored', 'colored2', 'colored3' ]
ATTRIBUTES
=
dict
(
zip
([
list
(
zip
([
'normal'
,
'bold'
,
'dark'
,
...
...
@@ -35,14 +35,14 @@ ATTRIBUTES = dict(
'reverse'
,
'concealed'
],
range
(
0
,
9
)
)
list
(
range
(
0
,
9
)
)
)
)
)
del
ATTRIBUTES
[
''
]
HIGHLIGHTS
=
dict
(
zip
([
list
(
zip
([
'on_grey'
,
'on_red'
,
'on_green'
,
...
...
@@ -52,13 +52,13 @@ HIGHLIGHTS = dict(
'on_cyan'
,
'on_white'
],
range
(
40
,
48
)
)
list
(
range
(
40
,
48
)
)
)
)
)
COLORS
=
dict
(
zip
([
list
(
zip
([
'grey'
,
'red'
,
'green'
,
...
...
@@ -68,8 +68,8 @@ COLORS = dict(
'cyan'
,
'white'
,
],
range
(
30
,
38
)
)
list
(
range
(
30
,
38
)
)
)
)
)
SAVE
=
'
\033
[s'
...
...
@@ -174,44 +174,44 @@ def colored3(text, color=None, on_color=None, attrs=None):
if
__name__
==
'__main__'
:
print
'Current terminal type: '
,
os
.
getenv
(
'TERM'
)
print
'Test basic colors:'
print
colored
(
'Grey color'
,
'grey'
)
print
colored
(
'Red color'
,
'red'
)
print
colored
(
'Green color'
,
'green'
)
print
colored
(
'Yellow color'
,
'yellow'
)
print
colored
(
'Blue color'
,
'blue'
)
print
colored
(
'Magenta color'
,
'magenta'
)
print
colored
(
'Cyan color'
,
'cyan'
)
print
colored
(
'White color'
,
'white'
)
print
'-'
*
78
print
'Test highlights:'
print
colored
(
'On grey color'
,
on_color
=
'on_grey'
)
print
colored
(
'On red color'
,
on_color
=
'on_red'
)
print
colored
(
'On green color'
,
on_color
=
'on_green'
)
print
colored
(
'On yellow color'
,
on_color
=
'on_yellow'
)
print
colored
(
'On blue color'
,
on_color
=
'on_blue'
)
print
colored
(
'On magenta color'
,
on_color
=
'on_magenta'
)
print
colored
(
'On cyan color'
,
on_color
=
'on_cyan'
)
print
colored
(
'On white color'
,
color
=
'grey'
,
on_color
=
'on_white'
)
print
'-'
*
78
print
'Test attributes:'
print
colored
(
'Bold grey color'
,
'grey'
,
attrs
=
[
'bold'
])
print
colored
(
'Dark red color'
,
'red'
,
attrs
=
[
'dark'
])
print
colored
(
'Underline green color'
,
'green'
,
attrs
=
[
'underline'
])
print
colored
(
'Blink yellow color'
,
'yellow'
,
attrs
=
[
'blink'
])
print
colored
(
'Reversed blue color'
,
'blue'
,
attrs
=
[
'reverse'
])
print
colored
(
'Concealed Magenta color'
,
'magenta'
,
attrs
=
[
'concealed'
])
print
colored
(
'Bold underline reverse cyan color'
,
'cyan'
,
attrs
=
[
'bold'
,
'underline'
,
'reverse'
])
print
colored
(
'Dark blink concealed white color'
,
'white'
,
attrs
=
[
'dark'
,
'blink'
,
'concealed'
])
print
'-'
*
78
print
'Test mixing:'
print
colored
(
'Underline red on grey color'
,
'red'
,
'on_grey'
,
[
'underline'
])
print
colored
(
'Reversed green on red color'
,
'green'
,
'on_red'
,
[
'reverse'
])
print
(
'Current terminal type: '
,
os
.
getenv
(
'TERM'
)
)
print
(
'Test basic colors:'
)
print
(
colored
(
'Grey color'
,
'grey'
)
)
print
(
colored
(
'Red color'
,
'red'
)
)
print
(
colored
(
'Green color'
,
'green'
)
)
print
(
colored
(
'Yellow color'
,
'yellow'
)
)
print
(
colored
(
'Blue color'
,
'blue'
)
)
print
(
colored
(
'Magenta color'
,
'magenta'
)
)
print
(
colored
(
'Cyan color'
,
'cyan'
)
)
print
(
colored
(
'White color'
,
'white'
)
)
print
(
'-'
*
78
)
print
(
'Test highlights:'
)
print
(
colored
(
'On grey color'
,
on_color
=
'on_grey'
)
)
print
(
colored
(
'On red color'
,
on_color
=
'on_red'
)
)
print
(
colored
(
'On green color'
,
on_color
=
'on_green'
)
)
print
(
colored
(
'On yellow color'
,
on_color
=
'on_yellow'
)
)
print
(
colored
(
'On blue color'
,
on_color
=
'on_blue'
)
)
print
(
colored
(
'On magenta color'
,
on_color
=
'on_magenta'
)
)
print
(
colored
(
'On cyan color'
,
on_color
=
'on_cyan'
)
)
print
(
colored
(
'On white color'
,
color
=
'grey'
,
on_color
=
'on_white'
)
)
print
(
'-'
*
78
)
print
(
'Test attributes:'
)
print
(
colored
(
'Bold grey color'
,
'grey'
,
attrs
=
[
'bold'
])
)
print
(
colored
(
'Dark red color'
,
'red'
,
attrs
=
[
'dark'
])
)
print
(
colored
(
'Underline green color'
,
'green'
,
attrs
=
[
'underline'
])
)
print
(
colored
(
'Blink yellow color'
,
'yellow'
,
attrs
=
[
'blink'
])
)
print
(
colored
(
'Reversed blue color'
,
'blue'
,
attrs
=
[
'reverse'
])
)
print
(
colored
(
'Concealed Magenta color'
,
'magenta'
,
attrs
=
[
'concealed'
])
)
print
(
colored
(
'Bold underline reverse cyan color'
,
'cyan'
,
attrs
=
[
'bold'
,
'underline'
,
'reverse'
])
)
print
(
colored
(
'Dark blink concealed white color'
,
'white'
,
attrs
=
[
'dark'
,
'blink'
,
'concealed'
])
)
print
(
'-'
*
78
)
print
(
'Test mixing:'
)
print
(
colored
(
'Underline red on grey color'
,
'red'
,
'on_grey'
,
[
'underline'
])
)
print
(
colored
(
'Reversed green on red color'
,
'green'
,
'on_red'
,
[
'reverse'
])
)
utils.py
View file @
49f4dad4
import
networkx
as
nx
import
http
lib
import
http
.client
def
reciprocity
(
G
):
"""Calculate reciprocity of graph, i.e. the ratio of the edges in
...
...
@@ -48,7 +48,7 @@ def edge_labels(edges, edgegroups=[], suppress_default=True):
try
:
assert
len
(
edges
)
==
len
(
labels
)
except
:
print
"Assertion fail: %s != %s"
%
(
len
(
edges
),
len
(
labels
))
print
(
"Assertion fail: %s != %s"
%
(
len
(
edges
),
len
(
labels
))
)
return
labels
...
...
@@ -73,7 +73,7 @@ def cap2str(capacity):
def
read_linkloads
(
G
,
host
,
url
):
conn
=
http
lib
.
HTTPConnection
(
host
)
conn
=
http
.
client
.
HTTPConnection
(
host
)
conn
.
request
(
"GET"
,
url
)
r1
=
conn
.
getresponse
()
if
r1
.
status
!=
200
:
...
...
@@ -141,8 +141,8 @@ def calc_ratio(G, loads, u, v, discard_inverse=False, no_diff=False, exclude_edg
return
0
ratio
=
totload
/
float
(
sum
)
if
ratio
<
0
:
print
"Assertion failed for ratio (%s, %s): %s"
\
%
(
u
,
v
,
ratio
)
print
(
"Assertion failed for ratio (%s, %s): %s"
\
%
(
u
,
v
,
ratio
)
)
if
ratio
>
1
:
ratio
=
1
return
ratio
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment