Пример #1
0
def test_isinstance_str():
    assert pkcompat.isinstance_str(u'ab'), \
        'unicode is str'
    if type(b'') == str:
        assert pkcompat.isinstance_str(b'ab'), \
            'bytes is a str in PY2'
    else:
        assert not pkcompat.isinstance_str(b'ab'), \
            'bytes are not a str'
    assert pkcompat.isinstance_str('ab'), \
        'str is a str'
Пример #2
0
def report_parameters_hash(data):
    """Compute a hash of the parameters for his report.

    Only needs to be unique relative to the report, not globally unique
    so MD5 is adequate. Long and cryptographic hashes make the
    cache checks slower.

    Args:
        data (dict): report and related models
    Returns:
        str: url safe encoded hash
    """
    if not 'reportParametersHash' in data:
        models = sirepo.template.import_module(data).models_related_to_report(
            data)
        res = hashlib.md5()
        dm = data['models']
        for m in models:
            if pkcompat.isinstance_str(m):
                name, field = m.split('.') if '.' in m else (m, None)
                value = dm[name][field] if field else dm[name]
            else:
                value = m
            res.update(
                json.dumps(value, sort_keys=True, allow_nan=False).encode())
        data['reportParametersHash'] = res.hexdigest()
    return data['reportParametersHash']
Пример #3
0
    def from_anything(cls, value):
        """Convert value (str, int, obj) to instance of `cls`

        Args:
            value (object): may be str, int, or obj
        Returns:
            enum.Enum: returns instance
        """
        if isinstance(value, cls):
            return value
        if isinstance(value, (int,float)):
            return cls(value)
        if pkcompat.isinstance_str(value):
            return cls[value.upper()]
        raise AssertionError('{}: is not an instance of {}'.format(value, cls))
Пример #4
0
 def _children(self, decl):
     if 'children' not in decl:
         return None
     res = pkcollections.OrderedMapping()
     for c in decl['children']:
         if pkcompat.isinstance_str(c):
             d = c
             n = c
         else:
             d = Declaration(c, self.qualified_name)
             n = d.name
         assert n not in res, \
             '{}: duplicate key in {}'.format(n, self.name)
         res[n] = d
     return res