swdata/island/matches.py
2018-06-16 21:37:40 +08:00

38 lines
1.1 KiB
Python

from pathlib import Path
from .match import Match
import json
import csv
class Matches:
def __init__(self, source, from_list=False):
self.data = []
self.names = []
if from_list:
for l in source:
self.data.append(Match.read_from_json(l))
self.names.append(Path(l).stem)
else:
for f in Path(source).iterdir():
if Path(f).suffix == '.json':
self.data.append(Match.read_from_json(str(f)))
self.names.append(Path(f).stem)
@staticmethod
def from_profile(pname, ppos='profile.csv'):
source = []
with open(ppos, 'r', encoding='utf-8') as f:
for row in csv.reader(f):
if pname in row:
source.append(row[0])
return Matches(source, True)
@staticmethod
def from_profile_expr(expr, ppos='profile.csv'):
source = []
with open(ppos, 'r', encoding='utf-8') as f:
for row in csv.reader(f):
if expr(row):
source.append(row[0])
return Matches(source, True)