public static PythonResultDto <string> ExecutePythonScript(PythonRequestDto request) { var cmd = request.Command; // "understats2.py"; var workingDirectory = Utility.GetApplicationSetting <string>("WorkingDirectory"); var pathToPythonExecutable = Utility.GetApplicationSetting <string>("PathTopythonExe"); var pythonResult = new PythonResultDto <string> { Status = false }; ProcessStartInfo start = new ProcessStartInfo(); start.WorkingDirectory = workingDirectory; start.FileName = pathToPythonExecutable; start.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\"", cmd, request.Params[0], request.Params[1]); start.UseShellExecute = false; // Do not use OS shell start.CreateNoWindow = true; // We don't need new window start.RedirectStandardOutput = true; // Any output, generated by application will be redirected back start.RedirectStandardError = true; // Any error in standard output will be redirected back (for example exceptions) using (Process process = Process.Start(start)) { using (StreamReader reader = process.StandardOutput) { string stderr = process.StandardError.ReadToEnd(); // Here are the exceptions from our Python script string result = reader.ReadToEnd(); // Here is the result of StdOut(for example: print "test") pythonResult.Status = stderr == ""; pythonResult.ErrorMessage = stderr; pythonResult.Output = result.StripLineBreaks(); } } return(pythonResult); }
public BaseResultDto <List <SeasonTeamDto> > GetTeams(IEnumerable <SeasonDto> seasons) { var result = new BaseResultDto <List <SeasonTeamDto> >(); result.DataObject = new List <SeasonTeamDto>(); var dataPath = Utility.GetApplicationSetting <string>("PathToDataFiles"); foreach (var season in seasons) { var pythonRequest = new PythonRequestDto { Command = Utility.GetApplicationSetting <string>("TeamScript"), Params = new string[] { season.StartYear.ToString(), season.League.Name } }; var pythonResult = PythonUtility.ExecutePythonScript(pythonRequest); if (pythonResult.Status) { var cleanSheetsFile = Utility.GetFileData(dataPath + "team_Fpl.csv"); foreach (var line in Utility.GetFileData(dataPath + pythonResult.Output).Skip(1)) { var lineAsList = Utility.CsvRowToList(line); int position; int matches; int won; int drawn; int lost; int goals; int goalsAgainst; int points; double xG; double xGAgainst; double xPoints; var status = int.TryParse(lineAsList[0], out position); status &= int.TryParse(lineAsList[2], out matches); status &= int.TryParse(lineAsList[3], out won); status &= int.TryParse(lineAsList[4], out drawn); status &= int.TryParse(lineAsList[5], out lost); status &= int.TryParse(lineAsList[6], out goals); status &= int.TryParse(lineAsList[7], out goalsAgainst); status &= int.TryParse(lineAsList[8], out points); status &= double.TryParse(lineAsList[9].StripXValues().Replace(".", ","), out xG); status &= double.TryParse(lineAsList[10].StripXValues().Replace(".", ","), out xGAgainst); status &= double.TryParse(lineAsList[11].StripXValues().Replace(".", ","), out xPoints); if (status) { var seasonTeam = new SeasonTeamDto { Season = season, Team = new TeamDto { Name = ConvertDoubleTeam(lineAsList[1]) }, Drawn = drawn, GoalsAgainst = goalsAgainst, GoalsFor = goals, Lost = lost, GamesPlayed = matches, Points = points, Position = position, Won = won, XGAgainst = xGAgainst, XGFor = xG, XPoints = xPoints }; result.DataObject.Add(seasonTeam); } } var nextPosition = true; var nextTeam = false; var nextCS = false; SeasonTeamDto st = null; foreach (var line in cleanSheetsFile) { if (nextPosition) { nextPosition = false; nextTeam = true; nextCS = false; continue; } int cs; if (nextTeam) { st = result.DataObject.FirstOrDefault(r => r.Team.Name.Equals(line) && r.Season.Id == season.Id); if (st == null) { var tt = 1; } nextPosition = false; nextTeam = false; nextCS = true; continue; } var status = int.TryParse(line, out cs); if (!status || st == null) { throw new Exception(); } st.CleanSheets = cs; st = null; nextPosition = true; nextTeam = false; nextCS = false; } //foreach (var line in cleanSheetsFile) //{ // var csLineAsList = Utility.CsvRowToList(line); // int cs; // var st = result.DataObject.FirstOrDefault(r => r.Team.Name.Equals(csLineAsList[1]) // && r.Season.Id == season.Id); // var status = int.TryParse(csLineAsList[2], out cs); // if (!status || st == null) // { // throw new Exception(); // } // st.CleanSheets = cs; //} } else { var e = new Exception(pythonResult.ErrorMessage); _logger.Error(e, "Could not parse team data"); throw e; } } //result.Status = result.DataObject.Count/seasons.Count() == 20; result.Status = true; return(result); }