Python past.builtins.unicode() Examples
The following are 22
code examples of past.builtins.unicode().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
past.builtins
, or try the search function
.
Example #1
Source File: encoder.py From go2mapillary with GNU General Public License v3.0 | 5 votes |
def _handle_attr(self, layer, feature, props): for k, v in props.items(): if self._can_handle_attr(k, v): if not PY3 and isinstance(k, str): k = k.decode('utf-8') if k not in self.seen_keys_idx: layer.keys.append(k) self.seen_keys_idx[k] = self.key_idx self.key_idx += 1 feature.tags.append(self.seen_keys_idx[k]) if v not in self.seen_values_idx: self.seen_values_idx[v] = self.val_idx self.val_idx += 1 val = layer.values.add() if isinstance(v, bool): val.bool_value = v elif isinstance(v, str): if PY3: val.string_value = v else: val.string_value = unicode(v, 'utf-8') elif isinstance(v, unicode): val.string_value = v elif isinstance(v, (int, long)): val.int_value = v elif isinstance(v, float): val.double_value = v feature.tags.append(self.seen_values_idx[v])
Example #2
Source File: helper.py From resilient-community-apps with MIT License | 5 votes |
def dict_to_json_str(d): """Function that converts a dictionary into a JSON string. Supports types: basestring, bool, int and nested dicts. Does not support lists. If the value is None, it sets it to False.""" json_entry = u'"{0}":{1}' json_entry_str = u'"{0}":"{1}"' entries = [] for entry in d: key = entry value = d[entry] if value is None: value = False if isinstance(value, list): dummy = {} if isinstance(value, basestring): value = value.replace(u'"', u'\\"') entries.append(json_entry_str.format(unicode(key), unicode(value))) elif isinstance(value, unicode): entries.append(json_entry.format(unicode(key), unicode(value))) elif isinstance(value, bool): value = 'true' if value == True else 'false' entries.append(json_entry.format(key, value)) elif isinstance(value, int): entries.append(json_entry.format(unicode(key), value)) elif isinstance(value, dict): entries.append(json_entry.format(key, dict_to_json_str(value))) else: dummy = {} return u'{0} {1} {2}'.format(u'{', ','.join(entries), u'}')
Example #3
Source File: mfg_event_converter.py From openhtf with Apache License 2.0 | 5 votes |
def multidim_measurement_to_attachment(name, measurement): """Convert a multi-dim measurement to an `openhtf.test_record.Attachment`.""" dimensions = list(measurement.dimensions) if measurement.units: dimensions.append( measurements.Dimension.from_unit_descriptor(measurement.units)) dims = [] for d in dimensions: if d.suffix is None: suffix = u'' # Ensure that the suffix is unicode. It's typically str/bytes because # units.py looks them up against str/bytes. elif isinstance(d.suffix, unicode): suffix = d.suffix else: suffix = d.suffix.decode('utf8') dims.append({ 'uom_suffix': suffix, 'uom_code': d.code, 'name': d.name, }) # Refer to the module docstring for the expected schema. dimensioned_measured_value = measurement.measured_value value = (sorted(dimensioned_measured_value.value, key=lambda x: x[0]) if dimensioned_measured_value.is_value_set else None) outcome_str = MEASUREMENT_OUTCOME_TO_TEST_RUN_STATUS_NAME[measurement.outcome] data = _convert_object_to_json({ 'outcome': outcome_str, 'name': name, 'dimensions': dims, 'value': value, }) attachment = htf_test_record.Attachment(data, test_runs_pb2.MULTIDIM_JSON) return attachment
Example #4
Source File: mfg_event_converter.py From openhtf with Apache License 2.0 | 5 votes |
def _convert_object_to_json(obj): # Since there will be parts of this that may have unicode, either as # measurement or in the logs, we have to be careful and convert everything # to unicode, merge, then encode to UTF-8 to put it into the proto. json_encoder = json.JSONEncoder(sort_keys=True, indent=2, ensure_ascii=False) pieces = [] for piece in json_encoder.iterencode(obj): if isinstance(piece, bytes): pieces.append(unicode(piece, errors='replace')) else: pieces.append(piece) return (u''.join(pieces)).encode('utf8', errors='replace')
Example #5
Source File: test_nbmolviz.py From notebook-molecular-visualization with Apache License 2.0 | 5 votes |
def test_generating_cubefile_works(wfn_viewer): wfn_viewer.numpoints = 64 grid, values = wfn_viewer._calc_orb_grid(wfn_viewer.mol.wfn.orbitals.canonical[1]) cb = wfn_viewer._grid_to_cube(grid, values) assert isinstance(cb, unicode)
Example #6
Source File: utils.py From amplpy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def toVariantArray(lst): va = amplpython.VariantArray(len(lst)) for i in range(len(lst)): if isinstance(lst[i], unicode): va[i] = amplpython.Variant(str(lst[i])) # FIXME: This is just a workaround for issue amplapi#332 # The real fix requires a new release of amplapi else: va[i] = amplpython.Variant(lst[i]) return va
Example #7
Source File: quark_runtime.py From quark with Apache License 2.0 | 5 votes |
def toBase64(self, buffer, offset, length): return unicode(base64.b64encode(buffer.data[offset:offset+length]), "ascii")
Example #8
Source File: quark_runtime.py From quark with Apache License 2.0 | 5 votes |
def getType(self): if isinstance(self.value, dict): return 'object' elif isinstance(self.value, (list, tuple)): return 'list' elif isinstance(self.value, (str, unicode)): return 'string' elif isinstance(self.value, (int,float,long)): return 'number' elif isinstance(self.value, bool): return 'bool' elif self.value is None: return 'null' else: raise TypeError("Unknown JSONObject type " + str(type(self.value)))
Example #9
Source File: quark_runtime_logging.py From quark with Apache License 2.0 | 5 votes |
def parseLevel(lvl): if isinstance(lvl, (str,unicode)): lvl = str(lvl).lower() return levelNames.get(lvl, logging.INFO)
Example #10
Source File: quark_threaded_runtime.py From quark with Apache License 2.0 | 5 votes |
def received_message(self, message): if message.is_text: self.runtime.events.put((self.handler.onWSMessage, (self.ws, unicode(message)), {})) else: self.runtime.events.put((self.handler.onWSBinary, (self.ws, Buffer(message.data)), {}))
Example #11
Source File: encoder.py From go2mapillary with GNU General Public License v3.0 | 5 votes |
def _can_handle_val(self, v): if isinstance(v, (str, unicode)): return True elif isinstance(v, bool): return True elif isinstance(v, (int, long)): return True elif isinstance(v, float): return True return False
Example #12
Source File: encoder.py From go2mapillary with GNU General Public License v3.0 | 5 votes |
def _can_handle_key(self, k): return isinstance(k, (str, unicode))
Example #13
Source File: encoder.py From mapbox-vector-tile with MIT License | 5 votes |
def _handle_attr(self, layer, feature, props): for k, v in props.items(): if self._can_handle_attr(k, v): if not PY3 and isinstance(k, str): k = k.decode('utf-8') if k not in self.seen_keys_idx: layer.keys.append(k) self.seen_keys_idx[k] = self.key_idx self.key_idx += 1 feature.tags.append(self.seen_keys_idx[k]) if isinstance(v, bool): values_idx = self.seen_values_bool_idx else: values_idx = self.seen_values_idx if v not in values_idx: values_idx[v] = self.val_idx self.val_idx += 1 val = layer.values.add() if isinstance(v, bool): val.bool_value = v elif isinstance(v, str): if PY3: val.string_value = v else: val.string_value = unicode(v, 'utf-8') elif isinstance(v, unicode): val.string_value = v elif isinstance(v, (int, long)): val.int_value = v elif isinstance(v, float): val.double_value = v feature.tags.append(values_idx[v])
Example #14
Source File: encoder.py From mapbox-vector-tile with MIT License | 5 votes |
def _can_handle_val(self, v): if isinstance(v, (str, unicode)): return True elif isinstance(v, bool): return True elif isinstance(v, (int, long)): return True elif isinstance(v, float): return True return False
Example #15
Source File: encoder.py From mapbox-vector-tile with MIT License | 5 votes |
def _can_handle_key(self, k): return isinstance(k, (str, unicode))
Example #16
Source File: test_futurize.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def test_open(self): """ In conservative mode, futurize would not import io.open because this changes the default return type from bytes to text. """ before = """ filename = 'temp_file_open.test' contents = 'Temporary file contents. Delete me.' with open(filename, 'w') as f: f.write(contents) with open(filename, 'r') as f: data = f.read() assert isinstance(data, str) assert data == contents """ after = """ from past.builtins import open, str as oldbytes, unicode filename = oldbytes(b'temp_file_open.test') contents = oldbytes(b'Temporary file contents. Delete me.') with open(filename, oldbytes(b'w')) as f: f.write(contents) with open(filename, oldbytes(b'r')) as f: data = f.read() assert isinstance(data, oldbytes) assert data == contents assert isinstance(oldbytes(b'hello'), basestring) assert isinstance(unicode(u'hello'), basestring) assert isinstance(oldbytes(b'hello'), basestring) """ self.convert_check(before, after, conservative=True)
Example #17
Source File: test_futurize.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def test_Py2_StringIO_module(self): """ This requires that the argument to io.StringIO be made a unicode string explicitly if we're not using unicode_literals: Ideally, there would be a fixer for this. For now: TODO: add the Py3 equivalent for this to the docs. Also add back a test for the unicode_literals case. """ before = """ import cStringIO import StringIO s1 = cStringIO.StringIO('my string') s2 = StringIO.StringIO('my other string') assert isinstance(s1, cStringIO.InputType) """ # There is no io.InputType in Python 3. futurize should change this to # something like this. But note that the input to io.StringIO # must be a unicode string on both Py2 and Py3. after = """ import io import io s1 = io.StringIO(u'my string') s2 = io.StringIO(u'my other string') assert isinstance(s1, io.StringIO) """ self.convert_check(before, after)
Example #18
Source File: test_futurize.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def test_literal_prefixes_are_not_stripped(self): """ Tests to ensure that the u'' and b'' prefixes on unicode strings and byte strings are not removed by the futurize script. Removing the prefixes on Py3.3+ is unnecessary and loses some information -- namely, that the strings have explicitly been marked as unicode or bytes, rather than just e.g. a guess by some automated tool about what they are. """ code = ''' s = u'unicode string' b = b'byte string' ''' self.unchanged(code)
Example #19
Source File: test_futurize.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def test_source_coding_utf8(self): """ Tests to ensure that the source coding line is not corrupted or removed. It must be left as the first line in the file (including before any __future__ imports). Also tests whether the unicode characters in this encoding are parsed correctly and left alone. """ code = """ # -*- coding: utf-8 -*- icons = [u"◐", u"◓", u"◑", u"◒"] """ self.unchanged(code)
Example #20
Source File: test_translation.py From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 | 5 votes |
def test_import_builtin_types(self): code = """ s1 = 'abcd' s2 = u'abcd' b1 = b'abcd' b2 = s2.encode('utf-8') d1 = {} d2 = dict((i, i**2) for i in range(10)) i1 = 1923482349324234L i2 = 1923482349324234 """ module = self.write_and_import(code, 'test_builtin_types') self.assertTrue(isinstance(module.s1, oldstr)) self.assertTrue(isinstance(module.s2, unicode)) self.assertTrue(isinstance(module.b1, oldstr))
Example #21
Source File: test_mdk.py From mdk with Apache License 2.0 | 4 votes |
def test_nestedInteractions(self, values): """ Nested interactions operate independently of parent interactions. :param values: a two-tuple composed of: - a recursive list of unicode and other recursive lists - list start means begin interaction, string means node resolve, list end means finish interaction. - list of False/True; True means failed interaction """ requested_interactions, failures = values failures = iter(failures) assume(not isinstance(requested_interactions, unicode)) self.init() ws_actor = self.connector.expectSocket() self.connector.connect(ws_actor) failures = iter(failures) created_services = {} expected_success_nodes = Counter() expected_failed_nodes = Counter() def run_interaction(children): should_fail = next(failures) failed = [] succeeded = [] self.session.start_interaction() for child in children: if isinstance(child, unicode): # Make sure disco knows about the node: if child in created_services: node = created_services[child] else: node = create_node(child, child) created_services[child] = node self.disco.onMessage(None, NodeActive(node)) # Make sure the child Node is resolved in the interaction self.session.resolve(node.service, "1.0") if should_fail: expected_failed_nodes[node] += 1 failed.append(node) else: expected_success_nodes[node] += 1 succeeded.append(node) else: run_interaction(child) if should_fail: self.session.fail_interaction("OHNO") self.session.finish_interaction() self.connector.advance_time(5.0) # Make sure interaction is sent ws_actor.swallowLogMessages() self.connector.expectInteraction( self, ws_actor, self.session, failed, succeeded) run_interaction(requested_interactions) for node in set(expected_failed_nodes) | set(expected_success_nodes): policy = self.disco.failurePolicy(node) self.assertEqual((policy.successes, policy.failures), (expected_success_nodes[node], expected_failed_nodes[node]))
Example #22
Source File: mfg_event_converter.py From openhtf with Apache License 2.0 | 4 votes |
def _copy_unidimensional_measurement( self, phase, name, measurement, mfg_event): """Copy uni-dimensional measurements to the MfgEvent.""" mfg_measurement = mfg_event.measurement.add() # Copy basic measurement fields. mfg_measurement.name = name if measurement.docstring: mfg_measurement.description = measurement.docstring mfg_measurement.parameter_tag.append(phase.name) if (measurement.units and measurement.units.code in test_runs_converter.UOM_CODE_MAP): mfg_measurement.unit_code = ( test_runs_converter.UOM_CODE_MAP[measurement.units.code]) # Copy failed measurements as failure_codes. This happens early to include # unset measurements. if (measurement.outcome != measurements.Outcome.PASS and phase.outcome != htf_test_record.PhaseOutcome.SKIP): failure_code = mfg_event.failure_codes.add() failure_code.code = name failure_code.details = '\n'.join(str(v) for v in measurement.validators) # Copy measurement value. measured_value = measurement.measured_value status_str = MEASUREMENT_OUTCOME_TO_TEST_RUN_STATUS_NAME[ measurement.outcome] mfg_measurement.status = test_runs_pb2.Status.Value(status_str) if not measured_value.is_value_set: return value = measured_value.value if isinstance(value, numbers.Number): mfg_measurement.numeric_value = float(value) elif isinstance(value, bytes): # text_value expects unicode or ascii-compatible strings, so we must # 'decode' it, even if it's actually just garbage bytestring data. mfg_measurement.text_value = unicode(value, errors='replace') elif isinstance(value, unicode): # Don't waste time and potential errors decoding unicode. mfg_measurement.text_value = value else: # Coercing to string. mfg_measurement.text_value = str(value) # Copy measurement validators. for validator in measurement.validators: if isinstance(validator, validators.RangeValidatorBase): if validator.minimum is not None: mfg_measurement.numeric_minimum = float(validator.minimum) if validator.maximum is not None: mfg_measurement.numeric_maximum = float(validator.maximum) elif isinstance(validator, validators.RegexMatcher): mfg_measurement.expected_text = validator.regex else: mfg_measurement.description += '\nValidator: ' + str(validator)