我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用types.UnicodeType()。
def writexml(self, stream, indent='', addindent='', newl='', strip=0, nsprefixes={}, namespace=''): if self.raw: val = self.nodeValue if not isinstance(val, StringTypes): val = str(self.nodeValue) else: v = self.nodeValue if not isinstance(v, StringTypes): v = str(v) if strip: v = ' '.join(v.split()) val = escape(v) if isinstance(val, UnicodeType): val = val.encode('utf8') stream.write(val)
def __init__(self): """SecurityOptions() Initialize. """ # I don't believe any of these types can ever pose a security hazard, # except perhaps "reference"... self.allowedTypes = {"None": 1, "bool": 1, "boolean": 1, "string": 1, "str": 1, "int": 1, "float": 1, "datetime": 1, "time": 1, "date": 1, "timedelta": 1, "NoneType": 1} if hasattr(types, 'UnicodeType'): self.allowedTypes['unicode'] = 1 self.allowedModules = {} self.allowedClasses = {}
def WriteXML(self, file): """ Dump non-empty contents to the output file, in XML format. """ if not self.loc: return out = SITEURL_XML_PREFIX for attribute in self.__slots__: value = getattr(self, attribute) if value: if type(value) == types.UnicodeType: value = encoder.NarrowText(value, None) elif type(value) != types.StringType: value = str(value) value = xml.sax.saxutils.escape(value) out = out + (' <%s>%s</%s>\n' % (attribute, value, attribute)) out = out + SITEURL_XML_SUFFIX file.write(out) #end def WriteXML #end class URL
def fmtstr(self, *fmt): str = '' encoding = 'utf8'#??utf8?? for i in fmt: if not type(i) in [types.UnicodeType, types.StringTypes, types.StringType]: s= repr(i) else: s = i if type(s) == type(u''): str += s.encode(encoding) else: str += s str += '.' #str += '/n' #print 'fmtstr:'+str return str
def test_unicode(self): if not test_support.have_unicode: return # The StringIO module also supports concatenating Unicode # snippets to larger Unicode strings. This is tested by this # method. Note that cStringIO does not support this extension. f = self.MODULE.StringIO() f.write(self._line[:6]) f.seek(3) f.write(unicode(self._line[20:26])) f.write(unicode(self._line[52])) s = f.getvalue() self.assertEqual(s, unicode('abcuvwxyz!')) self.assertEqual(type(s), types.UnicodeType)
def XMLTreeRecursion(node,handlers,res): '''This is the dispatcher for a recursive walk through the XMLTree. It is first called by "ProcessXMLTree" with the root of the tree as "tree" and can in turn be called by handler routines to work on subtrees.''' # node can be string node, a unicode node or a true node if type(node) == types.StringType: handlers.handleString(node,res) # default behaviour is a res.write elif type(node) == types.UnicodeType: handlers.handleString(node.encode('ISO-8859-1','replace'),res) # note that we ignore encoding problems here! # Now "node" must be a tuple: elif node[0] == '<![CDATA[': # a CDATA section node handlers.handleCDATA(node,res) elif node[0] == '<!--': # a comment node handlers.handleComment(node,res) elif node[0] == '<?': # a processing instruction handlers.handlePI(node,res) elif hasattr(handlers,'handle_'+node[0]): method = getattr(handlers,'handle_'+node[0]) method(node,res) else: handlers.handleDefault(node,res) # For the processing of XML trees in memory we define a class:
def renderContents(self, showStructureIndent=None, needUnicode=None): """Renders the contents of this tag as a (possibly Unicode) string.""" s=[] for c in self: text = None if isinstance(c, NavigableUnicodeString) or type(c) == types.UnicodeType: text = unicode(c) elif isinstance(c, Tag): s.append(c.__str__(needUnicode, showStructureIndent)) elif needUnicode: text = unicode(c) else: text = str(c) if text: if showStructureIndent != None: if text[-1] == '\n': text = text[:-1] s.append(text) return ''.join(s) #Soup methods
def endData(self): currentData = ''.join(self.currentData) if currentData: if not currentData.strip(): if '\n' in currentData: currentData = '\n' else: currentData = ' ' c = NavigableString if type(currentData) == types.UnicodeType: c = NavigableUnicodeString o = c(currentData) o.setup(self.currentTag, self.previous) if self.previous: self.previous.next = o self.previous = o self.currentTag.contents.append(o) self.currentData = []
def main(args): rf = RecentFeed(args) if args: articles = [] for arg in args.urls[0]: if args.verbose: print arg rf.get(arg) rf.parse() articles.append(rf.recently()) for article in articles[0]: if args.output == 'html': if type(article['title']) is types.UnicodeType: article['title'] = article['title'].encode('utf-8', 'replace') print '<li><a href="{0}">{1}</a></li>'.format(article['id'], article['title']) elif args.output == 'json': json.dumps({'title': article['title'], 'url': article['id']})
def _escape_json(json): """Escapes all string fields of JSON data. Operates recursively.""" t = type(json) if t == types.StringType or t == types.UnicodeType: return cgi.escape(json) elif t == types.IntType: return json elif t == types.FloatType: return json elif t == types.DictType: result = {} for f in json.keys(): result[f] = _escape_json(json[f]) return result elif t == types.ListType: result = [] for f in json: result.append(_escape_json(f)) return result else: raise RuntimeError, "Unsupported type: %s" % str(t)
def GetValue(self, row, col): if row < self.GetNumberRows(): if col == 0: return self.data[row].Number colname = self.GetColLabelValue(col, False) if colname == "Initial Value": colname = "InitialValue" value = getattr(self.data[row], colname, "") if colname == "Type" and isinstance(value, TupleType): if value[0] == "array": return "ARRAY [%s] OF %s" % (",".join(map(lambda x: "..".join(x), value[2])), value[1]) if not isinstance(value, (StringType, UnicodeType)): value = str(value) if colname in ["Class", "Option"]: return _(value) return value
def runsource(self, source): "Extend base class method: Stuff the source in the line cache first" filename = self.stuffsource(source) self.more = 0 self.save_warnings_filters = warnings.filters[:] warnings.filterwarnings(action="error", category=SyntaxWarning) if isinstance(source, types.UnicodeType): from idlelib import IOBinding try: source = source.encode(IOBinding.encoding) except UnicodeError: self.tkconsole.resetoutput() self.write("Unsupported characters in input\n") return try: # InteractiveInterpreter.runsource() calls its runcode() method, # which is overridden (see below) return InteractiveInterpreter.runsource(self, source, filename) finally: if self.save_warnings_filters is not None: warnings.filters[:] = self.save_warnings_filters self.save_warnings_filters = None
def __call__(self, message, verbosity): """Log message that has verbosity importance message can be a string, which is logged as-is, or a function, which is then called and should return the string to be logged. We do it this way in case producing the string would take a significant amount of CPU. """ if verbosity > self.verbosity and verbosity > self.term_verbosity: return if not (type(message) is types.StringType or type(message) is types.UnicodeType): assert type(message) is types.FunctionType message = message() if verbosity <= self.verbosity: self.log_to_file(message) if verbosity <= self.term_verbosity: self.log_to_term(message, verbosity)
def alert_count(conf): import types # convert string handle to Conf object automatically if isinstance(conf, types.UnicodeType): conf = Conf.objects.get(handle=conf) qs = conf.alerts.filter(seen=False) unread = len(qs) if unread: severity = max([x.severity for x in qs]) css = { 0: 'badge-info', 1: 'badge-warning', 2: 'badge-important' } css_class = css.get(severity) else: css_class = 'badge-default' return u'<span class="badge %s">%d</span>' % (css_class, unread)
def apply_zen21467_patch(self): """Patch cause of ZEN-21467 issue. The problem is that zenpacklib sets string property values to unicode strings instead of regular strings. There's a platform bug that prevents unicode values from being serialized to be used by zenjmx. This means that JMX datasources won't work without this patch. """ try: from Products.ZenHub.XmlRpcService import XmlRpcService if types.UnicodeType not in XmlRpcService.PRIMITIVES: XmlRpcService.PRIMITIVES.append(types.UnicodeType) except Exception: # The above may become wrong in future platform versions. pass
def setUp(self): class OtherInfo: __metaclass__ = autoxml.autoxml t_BirthDate = [types.StringType, autoxml.mandatory] t_Interest = [types.StringType, autoxml.optional] t_CodesWith = [ [types.UnicodeType], autoxml.optional, 'CodesWith/Person'] class A(xmlfile.XmlFile): __metaclass__ = autoxml.autoxml t_Name = [types.UnicodeType, autoxml.mandatory] t_Description = [autoxml.LocalText, autoxml.mandatory] t_Number = [types.IntType, autoxml.optional] t_Email = [types.StringType, autoxml.optional] a_href = [types.StringType, autoxml.mandatory] t_Projects = [ [types.StringType], autoxml.mandatory, 'Project'] t_OtherInfo = [ OtherInfo, autoxml.optional ] s_Comment = [ autoxml.Text, autoxml.mandatory] self.A = A
def isbasestring(x): return isinstance(x, (types.StringType, types.UnicodeType))
def writexml(self, stream, indent='', addindent='', newl='', strip=0, nsprefixes={}, namespace=''): val=self.data if isinstance(val, UnicodeType): val=val.encode('utf8') stream.write("<!--%s-->" % val)
def parseString(st, *args, **kw): if isinstance(st, UnicodeType): # this isn't particularly ideal, but it does work. return parse(StringIO(st.encode('UTF-16')), *args, **kw) return parse(StringIO(st), *args, **kw)
def _unjelly_unicode(self, exp): if UnicodeType: return unicode(exp[0], "UTF-8") else: return Unpersistable(exp[0])
def recode(self, utext): if type(utext) is types.UnicodeType: return utext.encode(self.options.charset, 'replace') return utext
def _MungeLocationListIntoFiles(self, urllist): """Given a list of URLs, munge them into our self._pathlist property. We do this by assuming all the files live in the same directory as the first file in the existing pathlist. That is, we assume a Sitemap index points to Sitemaps only in the same directory. This is not true in general, but will be true for any output produced by this script. """ assert self._pathlist path = self._pathlist[0] path = os.path.normpath(path) dir = os.path.dirname(path) wide = False if type(path) == types.UnicodeType: wide = True for url in urllist: url = URL.Canonicalize(url) output.Log('Index points to Sitemap file at: %s' % url, 2) (scheme, netloc, path, query, frag) = urlparse.urlsplit(url) file = os.path.basename(path) file = urllib.unquote(file) if wide: file = encoder.WidenText(file) if dir: file = dir + os.sep + file if file: self._pathlist.append(file) output.Log('Will attempt to read Sitemap file: %s' % file, 1) #end def _MungeLocationListIntoFiles
def str2long(s): """Convert a string to a long integer.""" if type(s) not in (types.StringType, types.UnicodeType): raise ValueError, 'the input must be a string' l = 0L for i in s: l <<= 8 l |= ord(i) return l
def __init__(self, URI): _checkInit() # init required if type(URI) in (StringType,UnicodeType): URI=processStringURI(URI) self.URI = URI self.objectID = URI.objectID # Delay adapter binding to enable transporting of proxies. # We just create an adapter, and don't connect it... self.adapter = Pyro.protocol.getProtocolAdapter(self.URI.protocol) # ---- don't forget to register local vars with DynamicProxyWithAttrs, see below
def _process_result(self, method, url, response, request_class, response_class, query_params, path_params): cls = response_class and response_class or request_class if cls in [types.StringType, types.UnicodeType]: # if we expect string type result, no need for further processing return response.text else: # prepare context to hold url and data along with method to navigate pages context = { 'method': method, 'url': url, 'query_params': query_params, 'response_class': response_class, 'func': self._get_all, 'request_class': request_class, 'path_params': path_params, 'response_data': response.text } return self._json_object_to_object(json.loads(response.text), cls, context=context) # first load JSON to a Python list or dict
def unicode_line(self, line): try: if type(line) is types.UnicodeType: return line else: return unicode(line, errors='ignore') except Exception as e: print("unicode err:%r" % e) print("line can't decode:%s" % line) print("Except stack:%s" % traceback.format_exc()) return ""