sumolib.miscutils
1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo 2# Copyright (C) 2012-2026 German Aerospace Center (DLR) and others. 3# This program and the accompanying materials are made available under the 4# terms of the Eclipse Public License 2.0 which is available at 5# https://www.eclipse.org/legal/epl-2.0/ 6# This Source Code may also be made available under the following Secondary 7# Licenses when the conditions for such availability set forth in the Eclipse 8# Public License 2.0 are satisfied: GNU General Public License, version 2 9# or later which is available at 10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html 11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later 12 13# @file miscutils.py 14# @author Jakob Erdmann 15# @author Michael Behrisch 16# @author Mirko Barthauer 17# @date 2012-05-08 18 19from __future__ import absolute_import 20from __future__ import print_function 21from __future__ import division 22import sys 23import time 24import builtins 25import os 26import math 27import colorsys 28import socket 29import random 30import gzip 31import codecs 32import io 33import atexit 34from functools import wraps 35from itertools import takewhile 36from pathlib import Path 37from types import ModuleType, FunctionType 38from gc import get_referents 39try: 40 from urllib.request import urlopen 41except ImportError: 42 from urllib import urlopen 43# needed for backward compatibility 44from .statistics import Statistics, geh, uMax, uMin, round # noqa 45 46PRACTICAL_INFINITY = 1e400 47_BLACKLIST = type, ModuleType, FunctionType 48 49 50def get_size(obj): 51 """sum size of object & members. 52 lifted from https://stackoverflow.com/a/30316760 53 """ 54 if isinstance(obj, (_BLACKLIST)): 55 raise TypeError('getsize() does not take argument of type: ' + str(type(obj))) 56 seen_ids = set() 57 size = 0 58 objects = [obj] 59 while objects: 60 need_referents = [] 61 for obj in objects: 62 if not isinstance(obj, _BLACKLIST) and id(obj) not in seen_ids: 63 seen_ids.add(id(obj)) 64 size += sys.getsizeof(obj) 65 need_referents.append(obj) 66 objects = get_referents(*need_referents) 67 return size 68 69 70def benchmark(func): 71 """ 72 decorator for timing a function 73 """ 74 @wraps(func) 75 def benchmark_wrapper(*args, **kwargs): 76 started = time.time() 77 now = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime()) 78 print('function %s called at %s' % (func.__name__, now)) 79 sys.stdout.flush() 80 result = func(*args, **kwargs) 81 print('function %s finished after %.6f seconds' % 82 (func.__name__, time.time() - started)) 83 sys.stdout.flush() 84 return result 85 return benchmark_wrapper 86 87 88def benchmark_cumulative(func): 89 """ 90 Decorator for timing a function and tracking execution statistics over repeated invocations. 91 Prints statistics at program end. 92 """ 93 @wraps(func) 94 def benchmark_wrapper(*args, **kwargs): 95 if not hasattr(benchmark_wrapper, 'call_count'): 96 benchmark_wrapper.call_count = 0 97 benchmark_wrapper.total_duration = 0.0 98 started = time.time() 99 result = func(*args, **kwargs) 100 elapsed = time.time() - started 101 benchmark_wrapper.call_count += 1 102 benchmark_wrapper.total_duration += elapsed 103 return result 104 105 def print_stats(): 106 count = getattr(benchmark_wrapper, 'call_count', 0) 107 if count > 0: 108 total = benchmark_wrapper.total_duration 109 print("\n--- Statistics for %s: calls=%s total(s)=%.6f avg(s)=%.6f ---" % 110 (func.__name__, count, total, total / count)) 111 sys.stdout.flush() 112 113 atexit.register(print_stats) 114 return benchmark_wrapper 115 116 117class Benchmarker: 118 """ 119 class for benchmarking a function using a "with"-statement. 120 Preferable over the "benchmark" function for the following use cases 121 - benchmarking a code block that isn't wrapped in a function 122 - benchmarking a function only in some calls 123 """ 124 125 def __init__(self, active, description): 126 self.active = active 127 self.description = description 128 129 def __enter__(self): 130 self.started = time.time() 131 132 def __exit__(self, *args): 133 if self.active: 134 duration = time.time() - self.started 135 print("%s finished after %s" % (self.description, humanReadableTime(duration))) 136 137 138class working_dir: 139 """ 140 temporarily change working directory using 'with' statement 141 """ 142 143 def __init__(self, dir): 144 self.dir = dir 145 self.origdir = os.getcwd() 146 147 def __enter__(self): 148 os.chdir(self.dir) 149 150 def __exit__(self, type, value, traceback): 151 os.chdir(self.origdir) 152 153 154class Colorgen: 155 DISTINCT = [ 156 (0.17, 1.0, 0.5), 157 (0.0, 0.9, 1.0), 158 (0.35, 0.67, 0.71), 159 (0.14, 0.9, 1.0), 160 (0.56, 1.0, 0.78), 161 (0.07, 0.8, 0.96), 162 (0.79, 0.83, 0.71), 163 (0.5, 0.71, 0.94), 164 (0.84, 0.79, 0.94), 165 (0.2, 0.76, 0.96), 166 (0.0, 0.24, 0.98), 167 (0.5, 1.0, 0.5), 168 (0.77, 0.25, 1.0), 169 (0.09, 0.76, 0.67), 170 (0.15, 0.22, 1.0), 171 (0.0, 1.0, 0.5), 172 (0.38, 0.33, 1.0), 173 (0.67, 1.0, 0.5), 174 ] 175 176 def __init__(self, hsv, cycleLength=10.67): 177 self.hsv = hsv 178 self.cycle = [int(random.random() * 256) for x in self.hsv] 179 self.cycleOffset = int(round(256 / cycleLength)) 180 self.distinctIndex = 0 181 182 def get_value(self, opt, index): 183 if opt == 'random': 184 return random.random() 185 if opt == 'cycle': 186 # the 255 below is intentional to get all color values when cycling long enough 187 self.cycle[index] = (self.cycle[index] + self.cycleOffset) % 255 188 return self.cycle[index] / 255.0 189 if opt == 'distinct': 190 if index == 0: 191 self.distinctIndex = (self.distinctIndex + 1) % len(self.DISTINCT) 192 return self.DISTINCT[self.distinctIndex][index] 193 return float(opt) 194 195 def floatTuple(self): 196 """return color as a tuple of floats each in [0,1]""" 197 return colorsys.hsv_to_rgb(*[self.get_value(o, i) for i, o in enumerate(self.hsv)]) 198 199 def byteTuple(self): 200 """return color as a tuple of bytes each in [0,255]""" 201 return tuple([int(round(255 * x)) for x in self.floatTuple()]) 202 203 def __call__(self): 204 """return constant or randomized rgb-color string""" 205 return ','.join(map(str, self.byteTuple())) 206 207 208class priorityDictionary(dict): 209 210 def __init__(self): 211 '''Initialize priorityDictionary by creating binary heap 212 of pairs (value,key). Note that changing or removing a dict entry will 213 not remove the old pair from the heap until it is found by smallest() or 214 until the heap is rebuilt.''' 215 self.__heap = [] 216 dict.__init__(self) 217 218 def smallest(self): 219 '''Find smallest item after removing deleted items from heap.''' 220 if len(self) == 0: 221 raise IndexError("smallest of empty priorityDictionary") 222 heap = self.__heap 223 while heap[0][1] not in self or self[heap[0][1]] != heap[0][0]: 224 lastItem = heap.pop() 225 insertionPoint = 0 226 while 1: 227 smallChild = 2 * insertionPoint + 1 228 if smallChild + 1 < len(heap) and \ 229 heap[smallChild][0] > heap[smallChild + 1][0]: 230 smallChild += 1 231 if smallChild >= len(heap) or lastItem <= heap[smallChild]: 232 heap[insertionPoint] = lastItem 233 break 234 heap[insertionPoint] = heap[smallChild] 235 insertionPoint = smallChild 236 return heap[0][1] 237 238 def __iter__(self): 239 '''Create destructive sorted iterator of priorityDictionary.''' 240 def iterfn(): 241 while len(self) > 0: 242 x = self.smallest() 243 yield x 244 del self[x] 245 return iterfn() 246 247 def __setitem__(self, key, val): 248 '''Change value stored in dictionary and add corresponding 249 pair to heap. Rebuilds the heap if the number of deleted items grows 250 too large, to avoid memory leakage.''' 251 dict.__setitem__(self, key, val) 252 heap = self.__heap 253 if len(heap) > 2 * len(self): 254 self.__heap = [(v, k) for k, v in self.items()] 255 self.__heap.sort() # builtin sort likely faster than O(n) heapify 256 else: 257 newPair = (val, key) 258 insertionPoint = len(heap) 259 heap.append(None) 260 while insertionPoint > 0 and val < heap[(insertionPoint - 1) // 2][0]: 261 heap[insertionPoint] = heap[(insertionPoint - 1) // 2] 262 insertionPoint = (insertionPoint - 1) // 2 263 heap[insertionPoint] = newPair 264 265 def setdefault(self, key, val): 266 '''Reimplement setdefault to call our customized __setitem__.''' 267 if key not in self: 268 self[key] = val 269 return self[key] 270 271 def update(self, other): 272 for key in other.keys(): 273 self[key] = other[key] 274 275 276def getFreeSocketPort(numTries=10): 277 for _ in range(numTries): 278 try: 279 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 280 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 281 s.bind(('', 0)) 282 p = s.getsockname()[1] 283 s.close() 284 return p 285 except socket.error: 286 pass 287 return None 288 289 290def getSocketStream(port, mode='rb'): 291 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 292 s.bind(("localhost", port)) 293 s.listen(1) 294 conn, _ = s.accept() 295 return conn.makefile(mode) 296 297 298# euclidean distance between two coordinates in the plane 299def euclidean(a, b): 300 return math.sqrt((a[0] - b[0]) ** 2 + (a[1] - b[1]) ** 2) 301 302 303def humanReadableTime(seconds): 304 result = "" 305 sign = '-' if seconds < 0 else '' 306 seconds = abs(seconds) 307 ds = 3600 * 24 308 if seconds > ds: 309 result = "%s:" % int(seconds / ds) 310 seconds = seconds % ds 311 result += "%02i:" % int(seconds / 3600) 312 seconds = seconds % 3600 313 result += "%02i:" % int(seconds / 60) 314 seconds = seconds % 60 315 if seconds == int(seconds): 316 seconds = int(seconds) 317 result += "%02i" % seconds 318 return sign + result 319 320 321SPECIAL_TIME_STRINGS = ["triggered", "containerTriggered", "split", "begin"] 322 323 324def parseTime(t, factor=1): 325 try: 326 return float(t) * factor 327 except ValueError: 328 pass 329 try: 330 # prepended zero is ignored if the date value already contains days 331 days, hours, minutes, seconds = ([0] + list(map(float, t.split(':'))))[-4:] 332 sign = -1 if t.strip()[0] == '-' else 1 333 return (3600 * 24 * days + 3600 * hours + 60 * minutes + seconds) * sign * factor 334 except ValueError: 335 if t in SPECIAL_TIME_STRINGS: 336 # signal special case but don't crash 337 return None 338 else: 339 raise 340 341 342def parseBool(val): 343 # see data/xsd/baseTypes:boolType 344 return val in ["true", "True", "x", "1", "yes", "on"] 345 346 347def getFlowNumber(flow): 348 """interpret number of vehicles from a flow parsed by sumolib.xml.parse""" 349 if flow.number is not None: 350 return int(flow.number) 351 if flow.end is not None: 352 duration = parseTime(flow.end) - parseTime(flow.begin) 353 period = 0 354 isFractional = False 355 if flow.period is not None: 356 if 'exp' in flow.period: 357 # use expected value 358 period = 1 / float(flow.period[4:-1]) 359 isFractional = True 360 else: 361 period = float(flow.period) 362 elif flow.probability is not None: 363 # use expected value 364 period = 1 / float(flow.probability) 365 isFractional = True 366 for attr in ['perHour', 'vehsPerHour']: 367 if flow.hasAttribute(attr): 368 period = 3600 / float(flow.getAttribute(attr)) 369 if period > 0: 370 count = duration / period 371 if isFractional: 372 return count 373 else: 374 # flows with a regular period always start at least one vehicle at the begin time 375 return math.ceil(count) 376 else: 377 return 1 378 379 380def intIfPossible(val, precision=None): 381 if int(val) == val: 382 return int(val) 383 elif precision is not None: 384 return builtins.round(val, precision) 385 else: 386 return val 387 388 389def openz(fileOrURL, mode="r", **kwargs): 390 """ 391 Opens transparently files, URLs and gzipped files for reading and writing. 392 Special file names "stdout" and "stderr" are handled as well. 393 Also enforces UTF8 on text output / input and should handle BOMs in input. 394 Should be compatible with python 2 and 3. 395 """ 396 encoding = kwargs.get("encoding", "utf8" if "w" in mode else "utf-8-sig") 397 try: 398 if fileOrURL.startswith("http://") or fileOrURL.startswith("https://"): 399 return io.BytesIO(urlopen(fileOrURL).read()) 400 if fileOrURL == "stdout": 401 return sys.stdout 402 if fileOrURL == "stderr": 403 return sys.stderr 404 if fileOrURL.endswith(".gz") and "w" in mode: 405 if "b" in mode: 406 return gzip.open(fileOrURL, mode="w") 407 return gzip.open(fileOrURL, mode="wt", encoding=encoding) 408 if kwargs.get("trySocket") and fileOrURL.isdigit(): 409 return getSocketStream(int(fileOrURL), mode) 410 if kwargs.get("tryGZip", True) and "r" in mode: 411 with gzip.open(fileOrURL) as fd: 412 fd.read(1) 413 if "b" in mode: 414 return gzip.open(fileOrURL) 415 if sys.version_info[0] < 3: 416 return codecs.getreader('utf-8')(gzip.open(fileOrURL)) 417 return gzip.open(fileOrURL, mode="rt", encoding=encoding) 418 except OSError as e: 419 if kwargs.get("printErrors"): 420 print(e, file=sys.stderr) 421 except IOError as e: 422 if kwargs.get("printErrors"): 423 print(e, file=sys.stderr) 424 if "b" in mode: 425 return io.open(fileOrURL, mode=mode) 426 return io.open(fileOrURL, mode=mode, encoding=encoding) 427 428 429def commonprefix(s): 430 """ 431 Return the longest substring all strings in s start with. 432 Reimplementation of os.path.commonprefix which is deprecated. 433 """ 434 return ''.join(ch[0] for ch in takewhile(lambda x: all(c == x[0] for c in x), zip(*s))) 435 436 437def short_names(filenames, noEmpty): 438 if len(filenames) == 1: 439 return filenames 440 reversedNames = [''.join(reversed(f)) for f in filenames] 441 prefix = commonprefix(filenames) 442 suffix = commonprefix(reversedNames) 443 prefixLen = len(prefix) 444 suffixLen = len(suffix) 445 shortened = [f[prefixLen:-suffixLen] for f in filenames] 446 if noEmpty and any([not f for f in shortened]): 447 # make longer to avoid empty file names 448 base = os.path.basename(prefix) 449 shortened = [base + f for f in shortened] 450 return shortened 451 452 453def getBaseName(filename): 454 """strip extensions such as .net.xml.gz""" 455 if filename[-11:] == ".net.xml.gz" and len(filename) > 11: 456 return filename[:-11] 457 elif filename[-8:] == ".net.xml" and len(filename) > 8: 458 return filename[:-8] 459 elif filename[-7:] == ".xml.gz" and len(filename) > 7: 460 return filename[:-7] 461 elif filename[-4:] == ".xml" and len(filename) > 4: 462 return filename[:-4] 463 else: 464 return filename 465 466 467def flattenPath(filename, sep='_'): 468 """create a filename that encodes the original directory structure""" 469 parts = Path(filename).parts 470 clean = [p for p in parts if p not in ('/', '\\') and ':' not in p] 471 return sep.join(clean)
51def get_size(obj): 52 """sum size of object & members. 53 lifted from https://stackoverflow.com/a/30316760 54 """ 55 if isinstance(obj, (_BLACKLIST)): 56 raise TypeError('getsize() does not take argument of type: ' + str(type(obj))) 57 seen_ids = set() 58 size = 0 59 objects = [obj] 60 while objects: 61 need_referents = [] 62 for obj in objects: 63 if not isinstance(obj, _BLACKLIST) and id(obj) not in seen_ids: 64 seen_ids.add(id(obj)) 65 size += sys.getsizeof(obj) 66 need_referents.append(obj) 67 objects = get_referents(*need_referents) 68 return size
sum size of object & members. lifted from https://stackoverflow.com/a/30316760
71def benchmark(func): 72 """ 73 decorator for timing a function 74 """ 75 @wraps(func) 76 def benchmark_wrapper(*args, **kwargs): 77 started = time.time() 78 now = time.strftime("%a, %d %b %Y %H:%M:%S +0000", time.localtime()) 79 print('function %s called at %s' % (func.__name__, now)) 80 sys.stdout.flush() 81 result = func(*args, **kwargs) 82 print('function %s finished after %.6f seconds' % 83 (func.__name__, time.time() - started)) 84 sys.stdout.flush() 85 return result 86 return benchmark_wrapper
decorator for timing a function
89def benchmark_cumulative(func): 90 """ 91 Decorator for timing a function and tracking execution statistics over repeated invocations. 92 Prints statistics at program end. 93 """ 94 @wraps(func) 95 def benchmark_wrapper(*args, **kwargs): 96 if not hasattr(benchmark_wrapper, 'call_count'): 97 benchmark_wrapper.call_count = 0 98 benchmark_wrapper.total_duration = 0.0 99 started = time.time() 100 result = func(*args, **kwargs) 101 elapsed = time.time() - started 102 benchmark_wrapper.call_count += 1 103 benchmark_wrapper.total_duration += elapsed 104 return result 105 106 def print_stats(): 107 count = getattr(benchmark_wrapper, 'call_count', 0) 108 if count > 0: 109 total = benchmark_wrapper.total_duration 110 print("\n--- Statistics for %s: calls=%s total(s)=%.6f avg(s)=%.6f ---" % 111 (func.__name__, count, total, total / count)) 112 sys.stdout.flush() 113 114 atexit.register(print_stats) 115 return benchmark_wrapper
Decorator for timing a function and tracking execution statistics over repeated invocations. Prints statistics at program end.
118class Benchmarker: 119 """ 120 class for benchmarking a function using a "with"-statement. 121 Preferable over the "benchmark" function for the following use cases 122 - benchmarking a code block that isn't wrapped in a function 123 - benchmarking a function only in some calls 124 """ 125 126 def __init__(self, active, description): 127 self.active = active 128 self.description = description 129 130 def __enter__(self): 131 self.started = time.time() 132 133 def __exit__(self, *args): 134 if self.active: 135 duration = time.time() - self.started 136 print("%s finished after %s" % (self.description, humanReadableTime(duration)))
class for benchmarking a function using a "with"-statement. Preferable over the "benchmark" function for the following use cases
- benchmarking a code block that isn't wrapped in a function
- benchmarking a function only in some calls
139class working_dir: 140 """ 141 temporarily change working directory using 'with' statement 142 """ 143 144 def __init__(self, dir): 145 self.dir = dir 146 self.origdir = os.getcwd() 147 148 def __enter__(self): 149 os.chdir(self.dir) 150 151 def __exit__(self, type, value, traceback): 152 os.chdir(self.origdir)
temporarily change working directory using 'with' statement
155class Colorgen: 156 DISTINCT = [ 157 (0.17, 1.0, 0.5), 158 (0.0, 0.9, 1.0), 159 (0.35, 0.67, 0.71), 160 (0.14, 0.9, 1.0), 161 (0.56, 1.0, 0.78), 162 (0.07, 0.8, 0.96), 163 (0.79, 0.83, 0.71), 164 (0.5, 0.71, 0.94), 165 (0.84, 0.79, 0.94), 166 (0.2, 0.76, 0.96), 167 (0.0, 0.24, 0.98), 168 (0.5, 1.0, 0.5), 169 (0.77, 0.25, 1.0), 170 (0.09, 0.76, 0.67), 171 (0.15, 0.22, 1.0), 172 (0.0, 1.0, 0.5), 173 (0.38, 0.33, 1.0), 174 (0.67, 1.0, 0.5), 175 ] 176 177 def __init__(self, hsv, cycleLength=10.67): 178 self.hsv = hsv 179 self.cycle = [int(random.random() * 256) for x in self.hsv] 180 self.cycleOffset = int(round(256 / cycleLength)) 181 self.distinctIndex = 0 182 183 def get_value(self, opt, index): 184 if opt == 'random': 185 return random.random() 186 if opt == 'cycle': 187 # the 255 below is intentional to get all color values when cycling long enough 188 self.cycle[index] = (self.cycle[index] + self.cycleOffset) % 255 189 return self.cycle[index] / 255.0 190 if opt == 'distinct': 191 if index == 0: 192 self.distinctIndex = (self.distinctIndex + 1) % len(self.DISTINCT) 193 return self.DISTINCT[self.distinctIndex][index] 194 return float(opt) 195 196 def floatTuple(self): 197 """return color as a tuple of floats each in [0,1]""" 198 return colorsys.hsv_to_rgb(*[self.get_value(o, i) for i, o in enumerate(self.hsv)]) 199 200 def byteTuple(self): 201 """return color as a tuple of bytes each in [0,255]""" 202 return tuple([int(round(255 * x)) for x in self.floatTuple()]) 203 204 def __call__(self): 205 """return constant or randomized rgb-color string""" 206 return ','.join(map(str, self.byteTuple()))
183 def get_value(self, opt, index): 184 if opt == 'random': 185 return random.random() 186 if opt == 'cycle': 187 # the 255 below is intentional to get all color values when cycling long enough 188 self.cycle[index] = (self.cycle[index] + self.cycleOffset) % 255 189 return self.cycle[index] / 255.0 190 if opt == 'distinct': 191 if index == 0: 192 self.distinctIndex = (self.distinctIndex + 1) % len(self.DISTINCT) 193 return self.DISTINCT[self.distinctIndex][index] 194 return float(opt)
209class priorityDictionary(dict): 210 211 def __init__(self): 212 '''Initialize priorityDictionary by creating binary heap 213 of pairs (value,key). Note that changing or removing a dict entry will 214 not remove the old pair from the heap until it is found by smallest() or 215 until the heap is rebuilt.''' 216 self.__heap = [] 217 dict.__init__(self) 218 219 def smallest(self): 220 '''Find smallest item after removing deleted items from heap.''' 221 if len(self) == 0: 222 raise IndexError("smallest of empty priorityDictionary") 223 heap = self.__heap 224 while heap[0][1] not in self or self[heap[0][1]] != heap[0][0]: 225 lastItem = heap.pop() 226 insertionPoint = 0 227 while 1: 228 smallChild = 2 * insertionPoint + 1 229 if smallChild + 1 < len(heap) and \ 230 heap[smallChild][0] > heap[smallChild + 1][0]: 231 smallChild += 1 232 if smallChild >= len(heap) or lastItem <= heap[smallChild]: 233 heap[insertionPoint] = lastItem 234 break 235 heap[insertionPoint] = heap[smallChild] 236 insertionPoint = smallChild 237 return heap[0][1] 238 239 def __iter__(self): 240 '''Create destructive sorted iterator of priorityDictionary.''' 241 def iterfn(): 242 while len(self) > 0: 243 x = self.smallest() 244 yield x 245 del self[x] 246 return iterfn() 247 248 def __setitem__(self, key, val): 249 '''Change value stored in dictionary and add corresponding 250 pair to heap. Rebuilds the heap if the number of deleted items grows 251 too large, to avoid memory leakage.''' 252 dict.__setitem__(self, key, val) 253 heap = self.__heap 254 if len(heap) > 2 * len(self): 255 self.__heap = [(v, k) for k, v in self.items()] 256 self.__heap.sort() # builtin sort likely faster than O(n) heapify 257 else: 258 newPair = (val, key) 259 insertionPoint = len(heap) 260 heap.append(None) 261 while insertionPoint > 0 and val < heap[(insertionPoint - 1) // 2][0]: 262 heap[insertionPoint] = heap[(insertionPoint - 1) // 2] 263 insertionPoint = (insertionPoint - 1) // 2 264 heap[insertionPoint] = newPair 265 266 def setdefault(self, key, val): 267 '''Reimplement setdefault to call our customized __setitem__.''' 268 if key not in self: 269 self[key] = val 270 return self[key] 271 272 def update(self, other): 273 for key in other.keys(): 274 self[key] = other[key]
211 def __init__(self): 212 '''Initialize priorityDictionary by creating binary heap 213 of pairs (value,key). Note that changing or removing a dict entry will 214 not remove the old pair from the heap until it is found by smallest() or 215 until the heap is rebuilt.''' 216 self.__heap = [] 217 dict.__init__(self)
Initialize priorityDictionary by creating binary heap of pairs (value,key). Note that changing or removing a dict entry will not remove the old pair from the heap until it is found by smallest() or until the heap is rebuilt.
219 def smallest(self): 220 '''Find smallest item after removing deleted items from heap.''' 221 if len(self) == 0: 222 raise IndexError("smallest of empty priorityDictionary") 223 heap = self.__heap 224 while heap[0][1] not in self or self[heap[0][1]] != heap[0][0]: 225 lastItem = heap.pop() 226 insertionPoint = 0 227 while 1: 228 smallChild = 2 * insertionPoint + 1 229 if smallChild + 1 < len(heap) and \ 230 heap[smallChild][0] > heap[smallChild + 1][0]: 231 smallChild += 1 232 if smallChild >= len(heap) or lastItem <= heap[smallChild]: 233 heap[insertionPoint] = lastItem 234 break 235 heap[insertionPoint] = heap[smallChild] 236 insertionPoint = smallChild 237 return heap[0][1]
Find smallest item after removing deleted items from heap.
266 def setdefault(self, key, val): 267 '''Reimplement setdefault to call our customized __setitem__.''' 268 if key not in self: 269 self[key] = val 270 return self[key]
Reimplement setdefault to call our customized __setitem__.
D.update([E, ]**F) -> None. Update D from dict/iterable E and F. If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
277def getFreeSocketPort(numTries=10): 278 for _ in range(numTries): 279 try: 280 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 281 s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 282 s.bind(('', 0)) 283 p = s.getsockname()[1] 284 s.close() 285 return p 286 except socket.error: 287 pass 288 return None
304def humanReadableTime(seconds): 305 result = "" 306 sign = '-' if seconds < 0 else '' 307 seconds = abs(seconds) 308 ds = 3600 * 24 309 if seconds > ds: 310 result = "%s:" % int(seconds / ds) 311 seconds = seconds % ds 312 result += "%02i:" % int(seconds / 3600) 313 seconds = seconds % 3600 314 result += "%02i:" % int(seconds / 60) 315 seconds = seconds % 60 316 if seconds == int(seconds): 317 seconds = int(seconds) 318 result += "%02i" % seconds 319 return sign + result
325def parseTime(t, factor=1): 326 try: 327 return float(t) * factor 328 except ValueError: 329 pass 330 try: 331 # prepended zero is ignored if the date value already contains days 332 days, hours, minutes, seconds = ([0] + list(map(float, t.split(':'))))[-4:] 333 sign = -1 if t.strip()[0] == '-' else 1 334 return (3600 * 24 * days + 3600 * hours + 60 * minutes + seconds) * sign * factor 335 except ValueError: 336 if t in SPECIAL_TIME_STRINGS: 337 # signal special case but don't crash 338 return None 339 else: 340 raise
348def getFlowNumber(flow): 349 """interpret number of vehicles from a flow parsed by sumolib.xml.parse""" 350 if flow.number is not None: 351 return int(flow.number) 352 if flow.end is not None: 353 duration = parseTime(flow.end) - parseTime(flow.begin) 354 period = 0 355 isFractional = False 356 if flow.period is not None: 357 if 'exp' in flow.period: 358 # use expected value 359 period = 1 / float(flow.period[4:-1]) 360 isFractional = True 361 else: 362 period = float(flow.period) 363 elif flow.probability is not None: 364 # use expected value 365 period = 1 / float(flow.probability) 366 isFractional = True 367 for attr in ['perHour', 'vehsPerHour']: 368 if flow.hasAttribute(attr): 369 period = 3600 / float(flow.getAttribute(attr)) 370 if period > 0: 371 count = duration / period 372 if isFractional: 373 return count 374 else: 375 # flows with a regular period always start at least one vehicle at the begin time 376 return math.ceil(count) 377 else: 378 return 1
interpret number of vehicles from a flow parsed by sumolib.xml.parse
390def openz(fileOrURL, mode="r", **kwargs): 391 """ 392 Opens transparently files, URLs and gzipped files for reading and writing. 393 Special file names "stdout" and "stderr" are handled as well. 394 Also enforces UTF8 on text output / input and should handle BOMs in input. 395 Should be compatible with python 2 and 3. 396 """ 397 encoding = kwargs.get("encoding", "utf8" if "w" in mode else "utf-8-sig") 398 try: 399 if fileOrURL.startswith("http://") or fileOrURL.startswith("https://"): 400 return io.BytesIO(urlopen(fileOrURL).read()) 401 if fileOrURL == "stdout": 402 return sys.stdout 403 if fileOrURL == "stderr": 404 return sys.stderr 405 if fileOrURL.endswith(".gz") and "w" in mode: 406 if "b" in mode: 407 return gzip.open(fileOrURL, mode="w") 408 return gzip.open(fileOrURL, mode="wt", encoding=encoding) 409 if kwargs.get("trySocket") and fileOrURL.isdigit(): 410 return getSocketStream(int(fileOrURL), mode) 411 if kwargs.get("tryGZip", True) and "r" in mode: 412 with gzip.open(fileOrURL) as fd: 413 fd.read(1) 414 if "b" in mode: 415 return gzip.open(fileOrURL) 416 if sys.version_info[0] < 3: 417 return codecs.getreader('utf-8')(gzip.open(fileOrURL)) 418 return gzip.open(fileOrURL, mode="rt", encoding=encoding) 419 except OSError as e: 420 if kwargs.get("printErrors"): 421 print(e, file=sys.stderr) 422 except IOError as e: 423 if kwargs.get("printErrors"): 424 print(e, file=sys.stderr) 425 if "b" in mode: 426 return io.open(fileOrURL, mode=mode) 427 return io.open(fileOrURL, mode=mode, encoding=encoding)
Opens transparently files, URLs and gzipped files for reading and writing. Special file names "stdout" and "stderr" are handled as well. Also enforces UTF8 on text output / input and should handle BOMs in input. Should be compatible with python 2 and 3.
430def commonprefix(s): 431 """ 432 Return the longest substring all strings in s start with. 433 Reimplementation of os.path.commonprefix which is deprecated. 434 """ 435 return ''.join(ch[0] for ch in takewhile(lambda x: all(c == x[0] for c in x), zip(*s)))
Return the longest substring all strings in s start with. Reimplementation of os.path.commonprefix which is deprecated.
438def short_names(filenames, noEmpty): 439 if len(filenames) == 1: 440 return filenames 441 reversedNames = [''.join(reversed(f)) for f in filenames] 442 prefix = commonprefix(filenames) 443 suffix = commonprefix(reversedNames) 444 prefixLen = len(prefix) 445 suffixLen = len(suffix) 446 shortened = [f[prefixLen:-suffixLen] for f in filenames] 447 if noEmpty and any([not f for f in shortened]): 448 # make longer to avoid empty file names 449 base = os.path.basename(prefix) 450 shortened = [base + f for f in shortened] 451 return shortened
454def getBaseName(filename): 455 """strip extensions such as .net.xml.gz""" 456 if filename[-11:] == ".net.xml.gz" and len(filename) > 11: 457 return filename[:-11] 458 elif filename[-8:] == ".net.xml" and len(filename) > 8: 459 return filename[:-8] 460 elif filename[-7:] == ".xml.gz" and len(filename) > 7: 461 return filename[:-7] 462 elif filename[-4:] == ".xml" and len(filename) > 4: 463 return filename[:-4] 464 else: 465 return filename
strip extensions such as .net.xml.gz
468def flattenPath(filename, sep='_'): 469 """create a filename that encodes the original directory structure""" 470 parts = Path(filename).parts 471 clean = [p for p in parts if p not in ('/', '\\') and ':' not in p] 472 return sep.join(clean)
create a filename that encodes the original directory structure