Skip to content

Commit b1230a8

Browse files
committed
Small improvements to a few utility classes and functions.
1 parent 5497e76 commit b1230a8

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

sc2reader/utils.py

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -397,45 +397,54 @@ def __setattr__(self, name, value):
397397
self[name] = value
398398

399399
def copy(self):
400-
return AttributeDict(super(AttributeDict,self).copy())
400+
return AttributeDict(self.items())
401401

402-
class Color(AttributeDict):
402+
class Color(object):
403403
"""
404-
Stores the string and rgba representation of a color. Individual components
405-
of the color can be retrieved with the dot operator::
404+
Stores a color name and rgba representation of a color. Individual
405+
color components can be retrieved with the dot operator::
406406
407407
color = Color(r=255, g=0, b=0, a=75)
408-
tuple(color.r,color.g, color.b, color.a) = color.rgba
408+
tuple(color.r,color.g, color.b, color.a) == color.rgba
409409
410-
Because Color is an implementation of an AttributeDict you must specify
411-
each component by name in the constructor.
410+
You can also create a color by name.
412411
413-
Can print the string representation with str(Color)
414-
"""
412+
color = Color('Red')
415413
416-
@property
417-
def rgba(self):
418-
"""Tuple containing the (r,g,b,a) representation of the color"""
419-
if 'r' not in self or 'g' not in self or 'b' not in self:
420-
hexstr = self.hex
414+
Only standard Starcraft colors are supported. ValueErrors will be thrown
415+
on invalid names or hex values.
416+
"""
417+
def __init__(self, name=None, r=0, g=0, b=0, a=255):
418+
if name:
419+
if name not in COLOR_CODES_INV:
420+
raise ValueError("Invalid color name: "+name)
421+
hexstr = COLOR_CODES_INV[name]
421422
self.r = int(hexstr[0:2],16)
422423
self.g = int(hexstr[2:4],16)
423424
self.b = int(hexstr[4:6],16)
424425
self.a = 255
426+
self.name = name
427+
else:
428+
self.r = r
429+
self.g = g
430+
self.b = b
431+
self.a = a
432+
if self.hex in COLOR_CODES:
433+
self.name = COLOR_CODES[self.hex]
434+
else:
435+
raise ValueError("Invalid color hex code: "+self.hex)
436+
437+
@property
438+
def rgba(self):
439+
""" Returns a tuple containing the color's (r,g,b,a) """
425440
return (self.r,self.g,self.b,self.a)
426441

427442
@property
428443
def hex(self):
429444
"""The hexadecimal representation of the color"""
430-
if 'name' in self:
431-
return COLOR_CODES_INV.get(self.name)
432-
else:
433-
return "{0.r:02X}{0.g:02X}{0.b:02X}".format(self)
434-
445+
return "{0.r:02X}{0.g:02X}{0.b:02X}".format(self)
435446

436447
def __str__(self):
437-
if not hasattr(self,'name'):
438-
self.name = COLOR_CODES[self.hex]
439448
return self.name
440449

441450
def open_archive(replay_file):
@@ -514,20 +523,16 @@ def merged_dict(a, b):
514523
c.update(b)
515524
return c
516525

517-
def extension_filter(filename, extension):
518-
name, ext = os.path.splitext(filename)
519-
return ext.lower()[1:] == extension.lower()
520-
521526
def get_files(path, exclude=list(), depth=-1, followlinks=False, extension=None, **extras):
522527
# os.walk and os.path.isfile fail silently. We want to be loud!
523528
if not os.path.exists(path):
524529
raise ValueError("Location `{0}` does not exist".format(path))
525530

526531
# If an extension is supplied, use it to do a type check
527-
if extension == None:
528-
type_check = lambda n: True
532+
if extension:
533+
type_check = lambda path: os.path.splitext(path)[1][1:].lower() == extension.lower()
529534
else:
530-
type_check = functools.partial(extension_filter, extension=extension)
535+
type_check = lambda n: True
531536

532537
# os.walk can't handle file paths, only directories
533538
if os.path.isfile(path):
@@ -551,24 +556,23 @@ def get_files(path, exclude=list(), depth=-1, followlinks=False, extension=None,
551556

552557

553558
class Length(timedelta):
554-
"""
555-
Extends the builtin timedelta class. See python docs for more info on
559+
""" Extends the builtin timedelta class. See python docs for more info on
556560
what capabilities this gives you.
557561
"""
558562

559563
@property
560564
def hours(self):
561-
"""The number of hours in represented."""
565+
""" The number of hours in represented. """
562566
return self.seconds/3600
563567

564568
@property
565569
def mins(self):
566-
"""The number of minutes in excess of the hours."""
570+
""" The number of minutes in excess of the hours. """
567571
return (self.seconds/60)%60
568572

569573
@property
570574
def secs(self):
571-
"""The number of seconds in excess of the minutes."""
575+
""" The number of seconds in excess of the minutes. """
572576
return self.seconds%60
573577

574578
def __str__(self):

0 commit comments

Comments
 (0)