Skip to content

Commit

Permalink
Handled unicode values in ObjectSelector.get_range
Browse files Browse the repository at this point in the history
  • Loading branch information
philippjfr committed Feb 13, 2017
1 parent 5523389 commit fcc75c4
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"""

import os.path
import sys
import glob

from .parameterized import Parameterized, Parameter, String, \
Expand All @@ -43,6 +44,8 @@
except:
__version__ = '1.4.2-unknown'

if sys.version_info.major >= 3:
unicode = str

#: Top-level object to allow messaging not tied to a particular
#: Parameterized object, as in 'param.main.warning("Invalid option")'.
Expand All @@ -66,6 +69,34 @@ def produce_value(value_obj):
return value_obj


def as_unicode(obj):
"""
Safely casts any object to unicode including regular string
(i.e. bytes) types in python 2.
"""
if sys.version_info.major < 3 and isinstance(obj, str):
obj = obj.decode('utf-8')
return unicode(obj)


def named_objs(objlist):
"""
Given a list of objects, returns a dictionary mapping from
string name for the object to the object itself.
"""
objs = OrderedDict()
for obj in objlist:
if hasattr(obj, "name"):
k = obj.name
elif hasattr(obj, '__name__'):
k = obj.__name__
else:
k = as_unicode(obj)
objs[k] = obj
return objs



class Infinity(object):
"""
An instance of this class represents an infinite value. Unlike
Expand Down Expand Up @@ -1004,9 +1035,7 @@ def get_range(self):
(Returns the dictionary {object.name:object}.)
"""
return OrderedDict((obj.name if hasattr(obj,"name") \
else obj.__name__ if hasattr(obj,"__name__") \
else str(obj), obj) for obj in self.objects)
return named_objs(self.objects)


class ClassSelector(Selector):
Expand Down

0 comments on commit fcc75c4

Please sign in to comment.