Python types.UnicodeType() Examples

The following are 30 code examples of types.UnicodeType(). 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 types , or try the search function .
Example #1
Source File: urllib2.py    From jawfish with MIT License 6 votes vote down vote up
def open(self, fullurl, data=None):
        # accept a URL or a Request object
        if isinstance(fullurl, (types.StringType, types.UnicodeType)):
            req = Request(fullurl, data)
        else:
            req = fullurl
            if data is not None:
                req.add_data(data)
        assert isinstance(req, Request) # really only care about interface

        result = self._call_chain(self.handle_open, 'default',
                                  'default_open', req)
        if result:
            return result

        type_ = req.get_type()
        result = self._call_chain(self.handle_open, type_, type_ + \
                                  '_open', req)
        if result:
            return result

        return self._call_chain(self.handle_open, 'unknown',
                                'unknown_open', req) 
Example #2
Source File: __init__.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def emit(self, record):
        """
        Emit a record.

        If a formatter is specified, it is used to format the record.
        The record is then written to the stream with a trailing newline
        [N.B. this may be removed depending on feedback]. If exception
        information is present, it is formatted using
        traceback.print_exception and appended to the stream.
        """
        try:
            msg = self.format(record)
            fs = "%s\n"
            if not hasattr(types, "UnicodeType"): #if no unicode support...
                self.stream.write(fs % msg)
            else:
                try:
                    self.stream.write(fs % msg)
                except UnicodeError:
                    self.stream.write(fs % msg.encode("UTF-8"))
            self.flush()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record) 
Example #3
Source File: __init__.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def getMessage(self):
        """
        Return the message for this LogRecord.

        Return the message for this LogRecord after merging any user-supplied
        arguments with the message.
        """
        if not hasattr(types, "UnicodeType"): #if no unicode support...
            msg = str(self.msg)
        else:
            msg = self.msg
            if type(msg) not in (types.UnicodeType, types.StringType):
                try:
                    msg = str(self.msg)
                except UnicodeError:
                    msg = self.msg      #Defer encoding till later
        if self.args:
            msg = msg % self.args
        return msg 
Example #4
Source File: test_StringIO.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
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) 
Example #5
Source File: test_StringIO.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
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) 
Example #6
Source File: __init__.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def normalize_encoding(encoding):

    """ Normalize an encoding name.

        Normalization works as follows: all non-alphanumeric
        characters except the dot used for Python package names are
        collapsed and replaced with a single underscore, e.g. '  -;#'
        becomes '_'. Leading and trailing underscores are removed.

        Note that encoding names should be ASCII only; if they do use
        non-ASCII characters, these must be Latin-1 compatible.

    """
    # Make sure we have an 8-bit string, because .translate() works
    # differently for Unicode strings.
    if hasattr(types, "UnicodeType") and type(encoding) is types.UnicodeType:
        # Note that .encode('latin-1') does *not* use the codec
        # registry, so this call doesn't recurse. (See unicodeobject.c
        # PyUnicode_AsEncodedString() for details)
        encoding = encoding.encode('latin-1')
    return '_'.join(encoding.translate(_norm_encoding_map).split()) 
Example #7
Source File: microdom.py    From python-for-android with Apache License 2.0 6 votes vote down vote up
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) 
Example #8
Source File: test_StringIO.py    From BinderFilter with MIT License 6 votes vote down vote up
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) 
Example #9
Source File: PyShell.py    From BinderFilter with MIT License 6 votes vote down vote up
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 
Example #10
Source File: PyShell.py    From oss-ftp with MIT License 6 votes vote down vote up
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 
Example #11
Source File: _beautifulsoup.py    From BruteXSS with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #12
Source File: interface.py    From bt-manager with GNU General Public License v3.0 6 votes vote down vote up
def translate_to_dbus_type(typeof, value):
    """
    Helper function to map values from their native Python types
    to Dbus types.

    :param type typeof: Target for type conversion e.g., 'dbus.Dictionary'
    :param value: Value to assign using type 'typeof'
    :return: 'value' converted to type 'typeof'
    :rtype: typeof
    """
    if ((isinstance(value, types.UnicodeType) or
         isinstance(value, str)) and typeof is not dbus.String):
        # FIXME: This is potentially dangerous since it evaluates
        # a string in-situ
        return typeof(eval(value))
    else:
        return typeof(value) 
Example #13
Source File: config.py    From twitter-for-bigquery with Apache License 2.0 6 votes vote down vote up
def __init__(self, streamOrFile=None, parent=None):
        """
        Initializes an instance.

        @param streamOrFile: If specified, causes this instance to be loaded
        from the stream (by calling L{load}). If a string is provided, it is
        passed to L{streamOpener} to open a stream. Otherwise, the passed
        value is assumed to be a stream and used as is.
        @type streamOrFile: A readable stream (file-like object) or a name.
        @param parent: If specified, this becomes the parent of this instance
        in the configuration hierarchy.
        @type parent: a L{Container} instance.
        """
        Mapping.__init__(self, parent)
        object.__setattr__(self, 'reader', ConfigReader(self))
        object.__setattr__(self, 'namespaces', [Config.Namespace()])
        object.__setattr__(self, 'resolving', set())
        if streamOrFile is not None:
            if isinstance(streamOrFile, StringType) or isinstance(streamOrFile, UnicodeType):
                global streamOpener
                if streamOpener is None:
                    streamOpener = defaultStreamOpener
                streamOrFile = streamOpener(streamOrFile)
            load = object.__getattribute__(self, "load")
            load(streamOrFile) 
Example #14
Source File: test_StringIO.py    From gcblue with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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) 
Example #15
Source File: microdom.py    From Safejumper-for-Desktop with GNU General Public License v2.0 6 votes vote down vote up
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) 
Example #16
Source File: para.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def simpleJustifyAlign(line, currentLength, maxLength):
    "simple justification with only strings"

    strings = []
    for x in line[:-1]:
        if type(x) in (StringType, UnicodeType):
            strings.append(x)
    nspaces = len(strings)-1
    slack = maxLength-currentLength
    text = ' '.join(strings)
    if nspaces>0 and slack>0:
        wordspacing = slack/float(nspaces)
        result = [("wordSpacing", wordspacing), text, maxLength, ("wordSpacing", 0)]
    else:
        result = [text, currentLength, ("nextLine", 0)]
    nextlinemark = ("nextLine", 0)
    if line and line[-1]==nextlinemark:
        result.append( nextlinemark )
    return result 
Example #17
Source File: para.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def compile_ul(self, attdict, content, extra, program, tagname="ul"):
        # by transformation
        #print "compile", tagname, attdict
        atts = attdict.copy()
        bulletmaker = bulletMaker(tagname, atts, self.context)
        # now do each element as a separate paragraph
        for e in content:
            te = type(e)
            if te in (StringType, UnicodeType):
                if e.strip():
                    raise ValueError, "don't expect CDATA between list elements"
            elif te is TupleType:
                (tagname, attdict1, content1, extra) = e
                if tagname!="li":
                    raise ValueError, "don't expect %s inside list" % repr(tagname)
                newatts = atts.copy()
                if attdict1:
                    newatts.update(attdict1)
                bulletmaker.makeBullet(newatts)
                self.compile_para(newatts, content1, extra, program) 
Example #18
Source File: test_StringIO.py    From oss-ftp with MIT License 6 votes vote down vote up
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) 
Example #19
Source File: optparse.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def isbasestring(x):
        return isinstance(x, (types.StringType, types.UnicodeType)) 
Example #20
Source File: views.py    From esproxy with MIT License 5 votes vote down vote up
def _fix_encoding(x):
    if type(x) is types.UnicodeType:
        return x.encode('utf-8');
    return x 
Example #21
Source File: test_optparse.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def assertHelpEquals(self, expected_output):
        if type(expected_output) is types.UnicodeType:
            encoding = self.parser._get_encoding(sys.stdout)
            expected_output = expected_output.encode(encoding, "replace")

        save_argv = sys.argv[:]
        try:
            # Make optparse believe bar.py is being executed.
            sys.argv[0] = os.path.join("foo", "bar.py")
            self.assertOutput(["-h"], expected_output)
        finally:
            sys.argv[:] = save_argv 
Example #22
Source File: para.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def insertShift(self, line, shift):
        # insert shift just before first visible element in line
        result = []
        first = 1
        for e in line:
            te = type(e)
            if first and (te in (StringType, UnicodeType, InstanceType)):
                result.append(shift)
                first = 0
            result.append(e)
        return result 
Example #23
Source File: logconfig.py    From listen1 with MIT License 5 votes vote down vote up
def format(self, record):
        t = RemoteAddressFormatter.format(self, record)
        if type(t) in [types.UnicodeType]:
            t = t.encode(self.encoding, 'replace')
        return t 
Example #24
Source File: androconf.py    From MARA_Framework with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #25
Source File: androconf.py    From aurasium with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #26
Source File: logconfig.py    From listen1 with MIT License 5 votes vote down vote up
def format(self, record):
        t = RemoteAddressFormatter.format(self, record)
        if type(t) in [types.UnicodeType]:
            t = t.encode(self.encoding, 'replace')
        return t 
Example #27
Source File: androconf.py    From MARA_Framework with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #28
Source File: androconf.py    From MARA_Framework with GNU Lesser General Public License v3.0 5 votes vote down vote up
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 
Example #29
Source File: _clientcookie.py    From BruteXSS with GNU General Public License v3.0 5 votes vote down vote up
def escape_path(path):
    """Escape any invalid characters in HTTP URL, and uppercase all escapes."""
    # There's no knowing what character encoding was used to create URLs
    # containing %-escapes, but since we have to pick one to escape invalid
    # path characters, we pick UTF-8, as recommended in the HTML 4.0
    # specification:
    # http://www.w3.org/TR/REC-html40/appendix/notes.html#h-B.2.1
    # And here, kind of: draft-fielding-uri-rfc2396bis-03
    # (And in draft IRI specification: draft-duerst-iri-05)
    # (And here, for new URI schemes: RFC 2718)
    if isinstance(path, types.UnicodeType):
        path = path.encode("utf-8")
    path = urllib.quote(path, HTTP_PATH_SAFE)
    path = ESCAPED_CHAR_RE.sub(uppercase_escaped_char, path)
    return path 
Example #30
Source File: androconf.py    From MARA_Framework with GNU Lesser General Public License v3.0 5 votes vote down vote up
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