Source code for ts2.utils

#   Copyright (C) 2008-2015 by Nicolas Piganeau
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   GNU General Public License for more details.
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the
#   Free Software Foundation, Inc.,
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

import random

from Qt import QtCore
import simplejson

import ts2.xobjects.xsettings
settings = ts2.xobjects.xsettings.XSettings()
"""Settings instance"""

[docs]class Context: """Different context's""" GAME = 10 """Game""" EDITORS = [20, 21, 22, 23, 24, 25] """Editor Modes list """ EDITOR_GENERAL = 20 """Editor General""" EDITOR_SCENERY = 21 """Editor Scenery""" EDITOR_ROUTES = 22 """Editor Routes""" EDITOR_TRAINTYPES = 23 """Editor TrainTypes""" EDITOR_SERVICES = 24 """Editor Services""" EDITOR_TRAINS = 25 """Editor Trains"""
[docs]class FormatException(Exception): """File format exception."""
[docs] def __init__(self, arg): """Constructor of the FormatException class.""" super().__init__(arg)
[docs]class MissingDependencyException(Exception): """Exception raised when a dependency is missing (e.g. TSL file)."""
[docs] def __init__(self, arg): """Constructor of the MissingDependencyException class.""" super().__init__(arg)
[docs]def cumsum(lis): """cumulated sum of lis :return: a list with the """ summ = 0 for x in lis: summ += x yield summ
""" def _getUserDataDirectory(): homeDir = os.path.expanduser("~") if os.path.commonprefix((homeDir, os.getcwd())): return os.getcwd() else: os.makedirs(os.path.join(homeDir, ".ts2", "data"), exist_ok=True) os.makedirs(os.path.join(homeDir, ".ts2", "simulations"), exist_ok=True) return os.path.join(homeDir, ".ts2") simulationsDirectory = os.path.join(_getUserDataDirectory(), "simulations") userDataDirectory = os.path.join(_getUserDataDirectory(), "data") """
[docs]class DurationProba(QtCore.QObject): """A DurationProba is a probability distribution for a duration in seconds."""
[docs] def __init__(self, data): """Constructor for the DurationProba class.""" super().__init__() self._probaList = None if isinstance(data, str): try: self._probaList = eval(data) except SyntaxError: pass else: self._probaList = data
[docs] def __str__(self): """Returns the string representation of the DurationProba.""" return str(self._probaList)
[docs] def isNull(self): """Returns true if the DurationProba instance has no data.""" return self._probaList is None
[docs] def yieldValue(self): """Returns a random value in the bounds and probabilities given by this DurationProba instance.""" try: probas = list(cumsum([t[2] for t in self._probaList])) probas.insert(0, 0) except TypeError: return self._probaList except Exception as err: QtCore.qDebug(str(err)) return None r0 = 100 * random.random() seg = 0 for i in range(len(probas) - 1): if probas[i] < r0 < probas[i+1]: break seg += 1 else: # Out of range: returns max value return self._probaList[-1][1] r1 = random.random() low, high, prob = self._probaList[seg] return r1 * (high - low) + low
[docs]def to_json(data): """Serialize data to a json string .. important:: Its advised to use this function as its is indented and sorted and therefore a consistent output. This is for git and versioning reasons, ie less deltas. """ return simplejson.dumps(data, indent=4, sort_keys=True)
[docs]def from_json(json_str): """Load data from a json string""" return simplejson.loads(json_str)