Skip to content

Commit 1d4c5ec

Browse files
committed
Black formatting
1 parent 1603c2a commit 1d4c5ec

41 files changed

Lines changed: 500 additions & 307 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

preflibtools/instances/convert.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" This module presents procedures to convert instances from one type to another.
22
"""
3+
34
from preflibtools.properties.pairwisecomparisons import pairwise_scores
45

56

@@ -23,11 +24,15 @@ def concatenate_elements_with_delimiter(*args, delimiter=","):
2324
num_unique = 0
2425
for alt_name1, score_dict in scores.items():
2526
for alt_name2, score in score_dict.items():
26-
pairwise_relation += concatenate_elements_with_delimiter(score, alt_name1, alt_name2)
27+
pairwise_relation += concatenate_elements_with_delimiter(
28+
score, alt_name1, alt_name2
29+
)
2730
num_unique += 1
2831
sum_vote_count += score
2932
pairwise_relation = pairwise_relation[:-1]
3033

31-
count_line = concatenate_elements_with_delimiter(instance.num_voters, sum_vote_count, num_unique)
34+
count_line = concatenate_elements_with_delimiter(
35+
instance.num_voters, sum_vote_count, num_unique
36+
)
3237

3338
return header + count_line + pairwise_relation

preflibtools/instances/dataset.py

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
def read_info_file(info_file_path):
22
tag_mapping = {
3-
'Name:': 'name',
4-
'Abbreviation:': 'abb',
5-
'Tags:': 'tags',
6-
'Series Number:': 'series',
7-
'Publication Date:': 'publication_date',
8-
'Description:': 'description',
9-
'Required Citations:': 'citations',
10-
'Selected Studies:': 'studies'
3+
"Name:": "name",
4+
"Abbreviation:": "abb",
5+
"Tags:": "tags",
6+
"Series Number:": "series",
7+
"Publication Date:": "publication_date",
8+
"Description:": "description",
9+
"Required Citations:": "citations",
10+
"Selected Studies:": "studies",
1111
}
1212

13-
infos = {'files': {}}
13+
infos = {"files": {}}
1414

15-
with open(info_file_path, 'r', encoding="utf-8") as file:
15+
with open(info_file_path, "r", encoding="utf-8") as file:
1616
line_cnt = 0
1717
lines = file.readlines()
1818
for line in lines:
@@ -22,22 +22,24 @@ def read_info_file(info_file_path):
2222

2323
for tag, key in tag_mapping.items():
2424
if line.startswith(tag):
25-
value = line[len(tag):].strip()
26-
if key == 'tags':
27-
infos[key] = [tag.strip() for tag in value.split(',')]
25+
value = line[len(tag) :].strip()
26+
if key == "tags":
27+
infos[key] = [tag.strip() for tag in value.split(",")]
2828
else:
2929
infos[key] = value
3030
break
3131

32-
if line.startswith('file_name, modification_type, relates_to, title, description, publication_date'):
32+
if line.startswith(
33+
"file_name, modification_type, relates_to, title, description, publication_date"
34+
):
3335
break
3436

3537
for line in lines[line_cnt:]:
3638
line = line.strip()
3739
if line:
38-
split_line = [part.strip() for part in line.split(',')]
40+
split_line = [part.strip() for part in line.split(",")]
3941
new_split_line = []
40-
tmp_split = ''
42+
tmp_split = ""
4143
inside_quotes = False
4244

4345
for split in split_line:
@@ -47,7 +49,7 @@ def read_info_file(info_file_path):
4749
new_split_line.append(tmp_split[:-3])
4850
inside_quotes = False
4951
else:
50-
tmp_split += ', '
52+
tmp_split += ", "
5153
else:
5254
if split.startswith('"""') and len(split) > 3:
5355
if split.endswith('"""'):
@@ -58,16 +60,18 @@ def read_info_file(info_file_path):
5860
else:
5961
new_split_line.append(split)
6062
new_file_info = {
61-
'file_name': new_split_line[0],
62-
'modification_type': new_split_line[1],
63-
'relates_to': new_split_line[2],
64-
'title': new_split_line[3],
65-
'description': new_split_line[4],
66-
'publication_date': new_split_line[5]
63+
"file_name": new_split_line[0],
64+
"modification_type": new_split_line[1],
65+
"relates_to": new_split_line[2],
66+
"title": new_split_line[3],
67+
"description": new_split_line[4],
68+
"publication_date": new_split_line[5],
6769
}
6870

6971
file_name = new_split_line[0].strip()
70-
infos['files'][file_name] = {key: value.strip() for key, value in new_file_info.items()}
72+
infos["files"][file_name] = {
73+
key: value.strip() for key, value in new_file_info.items()
74+
}
7175

7276
return infos
7377

@@ -76,10 +80,10 @@ def file_description_line(file_dict, headers):
7680
values = []
7781
for h in headers:
7882
value = str(file_dict[h])
79-
if ',' in value:
83+
if "," in value:
8084
value = f'"""{value}"""'
8185
values.append(value)
82-
return ', '.join(values)
86+
return ", ".join(values)
8387

8488

8589
def write_info_file(file_path, info_dict):
@@ -94,8 +98,14 @@ def write_info_file(file_path, info_dict):
9498
f.write(f"Selected Studies: {info_dict['studies']}\n\n")
9599

96100
if info_dict["files"]:
97-
headers = ["file_name", "modification_type", "relates_to", "title", "description", "publication_date"]
98-
f.write(', '.join(headers) + "\n")
101+
headers = [
102+
"file_name",
103+
"modification_type",
104+
"relates_to",
105+
"title",
106+
"description",
107+
"publication_date",
108+
]
109+
f.write(", ".join(headers) + "\n")
99110
for file_descr in sorted(info_dict["files"], key=lambda x: x["file_name"]):
100111
f.write(file_description_line(file_descr, headers) + "\n")
101-

preflibtools/instances/preflibinstance/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"get_parsed_instance",
1010
"CategoricalInstance",
1111
"MatchingInstance",
12-
"OrdinalInstance"
13-
]
12+
"OrdinalInstance",
13+
]

preflibtools/instances/preflibinstance/categorical.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,3 @@ def __str__(self):
326326
return "Categorical-Instance: {} <{},{}>".format(
327327
self.file_name, self.num_voters, self.num_alternatives
328328
)
329-

preflibtools/instances/preflibinstance/comparison.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,3 @@ class ComparisonInstance(PrefLibInstance):
66

77
def __init__(self):
88
PrefLibInstance.__init__(self)
9-

preflibtools/instances/preflibinstance/instance.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
""" This module describes the main class to deal with PrefLib instances..
22
"""
3+
34
import os.path
45
from os import path
56

@@ -104,7 +105,9 @@ def parse_file(self, filepath, autocorrect=False, header_only=False):
104105

105106
self.parse_lines(lines, autocorrect=autocorrect, header_only=header_only)
106107

107-
def parse_str(self, string, data_type, file_name="", autocorrect=False, header_only=False):
108+
def parse_str(
109+
self, string, data_type, file_name="", autocorrect=False, header_only=False
110+
):
108111
"""Parses the string provided as argument and populates the PreflibInstance object accordingly.
109112
The parser to be used is deduced from the file extension passed as argument.
110113
@@ -125,7 +128,9 @@ def parse_str(self, string, data_type, file_name="", autocorrect=False, header_o
125128
self.file_name = file_name
126129
self.data_type = data_type
127130

128-
self.parse_lines(string.splitlines(), autocorrect=autocorrect, header_only=header_only)
131+
self.parse_lines(
132+
string.splitlines(), autocorrect=autocorrect, header_only=header_only
133+
)
129134

130135
def parse_url(self, url, autocorrect=False, header_only=False):
131136
"""Parses the file located at the provided URL and populates the PreflibInstance object accordingly.

preflibtools/instances/preflibinstance/matching.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def __str__(self):
8383

8484

8585
class MatchingInstance(PrefLibInstance, WeightedDiGraph):
86-
"""This is the class representing a PrefLib instance for matching preferences. It basically
86+
"""This is the class representing a PrefLib instance for matching preferences. It basically
8787
contains the data and information written within a PrefLib file.
8888
8989
"""

preflibtools/instances/preflibinstance/ordinal.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,9 @@ def flatten_strict(self):
256256
res = []
257257
for order in self.orders:
258258
if len(order) != self.num_alternatives:
259-
warnings.warn("You are flattening a non-strict order, information may be lost.")
259+
warnings.warn(
260+
"You are flattening a non-strict order, information may be lost."
261+
)
260262
res.append(
261263
(
262264
tuple(indif_class[0] for indif_class in order),
@@ -485,4 +487,3 @@ def __str__(self):
485487
return "Ordinal-Instance: {} <{},{}>".format(
486488
self.file_name, self.num_voters, self.num_alternatives
487489
)
488-

preflibtools/instances/preflibinstance/utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ def get_parsed_instance(file_path, autocorrect=False, header_only=False):
2828
elif extension == ".wmd":
2929
instance = MatchingInstance()
3030
else:
31-
raise TypeError(f"The file extension {extension} corresponds to no known file extension "
32-
f"of PrefLib data file.")
31+
raise TypeError(
32+
f"The file extension {extension} corresponds to no known file extension "
33+
f"of PrefLib data file."
34+
)
3335
instance.parse_file(file_path, autocorrect=autocorrect, header_only=header_only)
3436
return instance

preflibtools/instances/sampling.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
Note that this has been updated to use the samplers provided in the
33
`prefsampling <https://github.com/COMSOC-Community/prefsampling>`_ package.
44
"""
5+
56
import math
67

78
from prefsampling.ordinal import mallows, urn, impartial, impartial_anonymous

0 commit comments

Comments
 (0)