Skip to content

Commit

Permalink
refactoring refs #12
Browse files Browse the repository at this point in the history
  • Loading branch information
namdre committed Feb 22, 2024
1 parent 3aab714 commit 7229457
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
18 changes: 18 additions & 0 deletions tools/sumolib/miscutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,21 @@ def openz(fileOrURL, mode="r", **kwargs):
if "b" in mode:
return io.open(fileOrURL, mode=mode)
return io.open(fileOrURL, mode=mode, encoding=encoding)


def short_names(filenames, noEmpty):
if len(filenames) == 1:
return filenames
reversedNames = [''.join(reversed(f)) for f in filenames]
prefix = os.path.commonprefix(filenames)
suffix = os.path.commonprefix(reversedNames)
prefixLen = len(prefix)
suffixLen = len(suffix)
shortened = [f[prefixLen:-suffixLen] for f in filenames]
if noEmpty and any([not f for f in shortened]):
# make longer to avoid empty file names
base = os.path.basename(prefix)
shortened = [base + f for f in shortened]
return shortened


18 changes: 1 addition & 17 deletions tools/visualization/plotXMLAttributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

sys.path.append(os.path.join(os.path.dirname(sys.argv[0]), '..'))
from sumolib import openz # noqa
from sumolib.miscutils import uMin, uMax, parseTime # noqa
from sumolib.miscutils import uMin, uMax, parseTime, short_names # noqa
from sumolib.options import ArgumentParser, RawDescriptionHelpFormatter # noqa
import sumolib.visualization.helpers # noqa

Expand Down Expand Up @@ -208,22 +208,6 @@ def write_csv(data, fname):
f.write('\n\n')


def short_names(filenames, noEmpty):
if len(filenames) == 1:
return filenames
reversedNames = [''.join(reversed(f)) for f in filenames]
prefix = os.path.commonprefix(filenames)
suffix = os.path.commonprefix(reversedNames)
prefixLen = len(prefix)
suffixLen = len(suffix)
shortened = [f[prefixLen:-suffixLen] for f in filenames]
if noEmpty and any([not f for f in shortened]):
# make longer to avoid empty file names
base = os.path.basename(prefix)
shortened = [base + f for f in shortened]
return shortened


def onpick(event):
mevent = event.mouseevent
print("dataID=%s x=%d y=%d" % (event.artist.get_label(), mevent.xdata, mevent.ydata))
Expand Down

0 comments on commit 7229457

Please sign in to comment.