• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python compat.as_unicode函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中pygame.compat.as_unicode函数的典型用法代码示例。如果您正苦于以下问题:Python as_unicode函数的具体用法?Python as_unicode怎么用?Python as_unicode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了as_unicode函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: test_render

    def test_render(self):
        """ 
        """

        f = pygame_font.Font(None, 20)
        s = f.render("foo", True, [0, 0, 0], [255, 255, 255])
        s = f.render("xxx", True, [0, 0, 0], [255, 255, 255])
        s = f.render("", True, [0, 0, 0], [255, 255, 255])
        s = f.render("foo", False, [0, 0, 0], [255, 255, 255])
        s = f.render("xxx", False, [0, 0, 0], [255, 255, 255])
        s = f.render("xxx", False, [0, 0, 0])
        s = f.render("   ", False, [0, 0, 0])
        s = f.render("   ", False, [0, 0, 0], [255, 255, 255])
        # null text should be 1 pixel wide.
        s = f.render("", False, [0, 0, 0], [255, 255, 255])
        self.assertEqual(s.get_size()[0], 1)
        # None text should be 1 pixel wide.
        s = f.render(None, False, [0, 0, 0], [255, 255, 255])
        self.assertEqual(s.get_size()[0], 1)
        # Non-text should raise a TypeError.
        self.assertRaises(TypeError, f.render,
                          [], False, [0, 0, 0], [255, 255, 255])
        self.assertRaises(TypeError, f.render,
                          1, False, [0, 0, 0], [255, 255, 255])
        # is background transparent for antialiasing?
        s = f.render(".", True, [255, 255, 255])
        self.failUnlessEqual(s.get_at((0, 0))[3], 0)
        # is Unicode and bytes encoding correct?
        # Cannot really test if the correct characters are rendered, but
        # at least can assert the encodings differ.
        su = f.render(as_unicode("."), False, [0, 0, 0], [255, 255, 255])
        sb = f.render(as_bytes("."), False, [0, 0, 0], [255, 255, 255])
        self.assert_(equal_images(su, sb))
        u = as_unicode(r"\u212A")
        b = u.encode("UTF-16")[2:] # Keep byte order consistent. [2:] skips BOM
        sb = f.render(b, False, [0, 0, 0], [255, 255, 255])
        try:
            su = f.render(u, False, [0, 0, 0], [255, 255, 255])
        except pygame.error:
            pass
        else:
            self.assert_(not equal_images(su, sb))

        # If the font module is SDL_ttf based, then it can only supports  UCS-2;
        # it will raise an exception for an out-of-range UCS-4 code point.
        if UCS_4 and not hasattr(f, 'ucs4'):
            ucs_2 = as_unicode(r"\uFFEE")
            s = f.render(ucs_2, False, [0, 0, 0], [255, 255, 255])
            ucs_4 = as_unicode(r"\U00010000")
            self.assertRaises(UnicodeError, f.render,
                              ucs_4, False, [0, 0, 0], [255, 255, 255])

        b = as_bytes("ab\x00cd")
        self.assertRaises(ValueError, f.render, b, 0, [0, 0, 0])
        u = as_unicode("ab\x00cd")
        self.assertRaises(ValueError, f.render, b, 0, [0, 0, 0])
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:56,代码来源:font_test.py


示例2: test_load

    def test_load(self):
        # __doc__ (as of 2008-07-13) for pygame.mixer_music.load:
        
          # pygame.mixer.music.load(filename): return None
          # Load a music file for playback

        data_fname = example_path('data')
        pygame.mixer.init()

        # The mp3 test file can crash smpeg on some systems.
        ## formats = ['mp3', 'ogg', 'wav']
        formats = ['ogg', 'wav']

        for f in formats:
            path = os.path.join(data_fname, 'house_lo.%s' % f)
            if os.sep == '\\':
                path = path.replace('\\', '\\\\')
            umusfn = as_unicode(path)
            bmusfn = filesystem_encode(umusfn) 
    
            pygame.mixer.music.load(umusfn)
            pygame.mixer.music.load(bmusfn)

            #NOTE: TODO: loading from filelikes are disabled...
            # because as of writing it only works in SDL_mixer svn.
            #pygame.mixer.music.load(open(musfn))
            #musf = open(musfn)
            #pygame.mixer.music.load(musf)
        pygame.mixer.quit()
开发者ID:Anugrahaa,项目名称:anagrammatic,代码行数:29,代码来源:mixer_music_test.py


示例3: test_as_unicode

 def test_as_unicode(self):
     r = r'Bo\u00F6tes'
     ords = [ord('B'), ord('o'), 0xF6, ord('t'), ord('e'), ord('s')]
     self.failUnlessEqual(len(r), 11)
     u = compat.as_unicode(r)
     self.failUnless(isinstance(u, compat.unicode_))
     self.failUnlessEqual([ord(c) for c in u], ords)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:7,代码来源:compat_test.py


示例4: test_load_from_file_unicode

 def test_load_from_file_unicode(self):
     base_dir = os.path.dirname(pygame.__file__)
     font_path = os.path.join(base_dir, pygame_font.get_default_font())
     if os.path.sep == '\\':
         font_path = font_path.replace('\\', '\\\\')
     ufont_path = as_unicode(font_path)
     f = pygame_font.Font(ufont_path, 20)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:7,代码来源:font_test.py


示例5: test_freetype_Font_get_size

    def test_freetype_Font_get_size(self):

        font = self._TEST_FONTS['sans']

        def test_size(s):
            self.assertTrue(isinstance(s, tuple))
            self.assertEqual(len(s), 2)
            self.assertTrue(isinstance(s[0], int))
            self.assertTrue(isinstance(s[1], int))

        size_default = font.get_size("ABCDabcd", ptsize=24)
        test_size(size_default)
        self.assertTrue(size_default > (0, 0))
        self.assertTrue(size_default[0] > size_default[1])

        size_bigger = font.get_size("ABCDabcd", ptsize=32)
        test_size(size_bigger)
        self.assertTrue(size_bigger > size_default)

        size_bolden = font.get_size("ABCDabcd", ptsize=24, style=ft.STYLE_BOLD)
        test_size(size_bolden)
        self.assertTrue(size_bolden > size_default)

        font.vertical = True
        size_vert = font.get_size("ABCDabcd", ptsize=24)
        test_size(size_vert)
        self.assertTrue(size_vert[0] < size_vert[1])
        font.vertical = False

        size_italic = font.get_size("ABCDabcd", ptsize=24, style=ft.STYLE_ITALIC)
        test_size(size_italic)
        self.assertTrue(size_italic[0] > size_default[0])
        self.assertTrue(size_italic[1] == size_default[1])

        size_under = font.get_size("ABCDabcd", ptsize=24, style=ft.STYLE_UNDERLINE)
        test_size(size_under)
        self.assertTrue(size_under[0] == size_default[0])
        self.assertTrue(size_under[1] > size_default[1])

        size_utf32 = font.get_size(as_unicode(r'\U000130A7'), ptsize=24)
        size_utf16 = font.get_size(as_unicode(r'\uD80C\uDCA7'), ptsize=24)
        size_utf16 = font.get_size(as_unicode(r'\uD80C\uDCA7'), ptsize=24,
                                   surrogates=False)
        self.assertNotEqual(size_utf16[0], size_utf32[0]);
        
        self.assertRaises(RuntimeError,
                          nullfont().get_size, 'a', ptsize=24)
开发者ID:JasonQSong,项目名称:Sinners,代码行数:47,代码来源:freetype_font_test.py


示例6: test_load_from_file_unicode

 def test_load_from_file_unicode(self):
     base_dir = os.path.split(pygame.__file__)[0]
     sep = os.path.sep
     if sep == '\\':
         sep = '\\\\'
     font_path = base_dir + sep + pygame_font.get_default_font()
     ufont_path = as_unicode(font_path)
     f = pygame_font.Font(ufont_path, 20)
开发者ID:LiangXunfly,项目名称:play_pong,代码行数:8,代码来源:font_test.py


示例7: test_as_str

 def test_as_str(self):
     # Bug reported on Pygame mailing list July 24, 2011:
     # For Python 3.x str(event) to raises an UnicodeEncodeError when
     # an event attribute is a string with a non-ascii character.
     try:
         str(pygame.event.Event(1, a=as_unicode(r"\xed")))
     except UnicodeEncodeError:
         self.fail("Event object raised exception for non-ascii character")
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:8,代码来源:event_test.py


示例8: test_metrics

    def test_metrics(self):
        # Ensure bytes decoding works correctly. Can only compare results
        # with unicode for now.
        f = pygame_font.Font(None, 20);
        um = f.metrics(as_unicode("."))
        bm = f.metrics(as_bytes("."))
        self.assert_(len(um) == 1)
        self.assert_(len(bm) == 1)
        self.assert_(um[0] is not None)
        self.assert_(um == bm)
        u = as_unicode(r"\u212A")
        b = u.encode("UTF-16")[2:] # Keep byte order consistent. [2:] skips BOM
        bm = f.metrics(b)
        self.assert_(len(bm) == 2)
        try:
            um = f.metrics(u)
        except pygame.error:
            pass
        else:
            self.assert_(len(um) == 1)
            self.assert_(bm[0] != um[0])
            self.assert_(bm[1] != um[0])

        if UCS_4:
            u = as_unicode(r"\U00013000")
            bm = f.metrics(u)
            self.assert_(len(bm) == 1 and bm[0] is None)
    
        return # unfinished
        # The documentation is useless here. How large a list?
        # How do list positions relate to character codes?
        # What about unicode characters?

        # __doc__ (as of 2008-08-02) for pygame_font.Font.metrics:

          # Font.metrics(text): return list
          # Gets the metrics for each character in the pased string.
          # 
          # The list contains tuples for each character, which contain the
          # minimum X offset, the maximum X offset, the minimum Y offset, the
          # maximum Y offset and the advance offset (bearing plus width) of the
          # character. [(minx, maxx, miny, maxy, advance), (minx, maxx, miny,
          # maxy, advance), ...]

        self.fail() 
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:45,代码来源:font_test.py


示例9: test_size

 def test_size(self):
     f = pygame_font.Font(None, 20)
     text = as_unicode("Xg")
     size = f.size(text)
     w, h = size
     self.assert_(isinstance(w, int) and isinstance(h, int))
     s = f.render(text, False, (255, 255, 255))
     self.assert_(size == s.get_size())
     btext = text.encode("ascii")
     self.assert_(f.size(btext) == size)
     text = as_unicode(r"\u212A")
     btext = text.encode("UTF-16")[2:] # Keep the byte order consistent.
     bsize = f.size(btext)
     try:
         size = f.size(text)
     except pygame.error:
         pass
     else:
         self.assert_(size != bsize)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:19,代码来源:font_test.py


示例10: test_freetype_Face_render

    def test_freetype_Face_render(self):

        face = self._TEST_FONTS["sans"]

        surf = pygame.Surface((800, 600))
        color = pygame.Color(0, 0, 0)

        # make sure we always have a valid fg color
        self.assertRaises(TypeError, face.render, "FoobarBaz")
        self.assertRaises(TypeError, face.render, "FoobarBaz", None)

        rend = face.render("FoobarBaz", pygame.Color(0, 0, 0), None, ptsize=24)
        self.assertTrue(isinstance(rend, tuple))
        self.assertEqual(len(rend), 2)
        self.assertTrue(isinstance(rend[0], pygame.Surface))
        self.assertTrue(isinstance(rend[1], pygame.Rect))
        self.assertEqual(rend[0].get_rect().size, rend[1].size)
        s, r = face.render("", pygame.Color(0, 0, 0), None, ptsize=24)
        self.assertEqual(r.width, 1)
        self.assertEqual(r.height, face.get_sized_height(24))
        self.assertEqual(s.get_size(), r.size)
        self.assertEqual(s.get_bitsize(), 32)

        # misc parameter test
        self.assertRaises(ValueError, face.render, "foobar", color)
        self.assertRaises(TypeError, face.render, "foobar", color, "", ptsize=24)
        self.assertRaises(ValueError, face.render, "foobar", color, None, style=42, ptsize=24)
        self.assertRaises(TypeError, face.render, "foobar", color, None, style=None, ptsize=24)
        self.assertRaises(ValueError, face.render, "foobar", color, None, style=97, ptsize=24)

        # valid surrogate pairs
        #        rend1 = face.render(None, as_unicode(r'\uD800\uDC00'), color, ptsize=24)
        #        rend1 = face.render(None, as_unicode(r'\uDBFF\uDFFF'), color, ptsize=24)
        #        rend1 = face.render(None, as_unicode(r'\uD80C\uDCA7'), color, ptsize=24)
        #        rend2 = face.render(None, as_unicode(r'\U000130A7'), color, ptsize=24)
        #        self.assertEqual(rend1[1], rend2[1])
        #        face.utf16_surrogates = False
        #        try:
        #            rend1 = face.render(None, as_unicode(r'\uD80C\uDCA7'),
        #                                color, ptsize=24)
        #        finally:
        #            face.utf16_surrogates = True
        #        self.assertNotEqual(rend1[1], rend2[1])

        # malformed surrogate pairs
        self.assertRaises(UnicodeEncodeError, face.render, as_unicode(r"\uD80C"), color, ptsize=24)
        self.assertRaises(UnicodeEncodeError, face.render, as_unicode(r"\uDCA7"), color, ptsize=24)
        self.assertRaises(UnicodeEncodeError, face.render, as_unicode(r"\uD7FF\uDCA7"), color, ptsize=24)
        self.assertRaises(UnicodeEncodeError, face.render, as_unicode(r"\uDC00\uDCA7"), color, ptsize=24)
        self.assertRaises(UnicodeEncodeError, face.render, as_unicode(r"\uD80C\uDBFF"), color, ptsize=24)
        self.assertRaises(UnicodeEncodeError, face.render, as_unicode(r"\uD80C\uE000"), color, ptsize=24)

        # raises exception when uninitalized
        self.assertRaises(RuntimeError, nullface().render, "a", (0, 0, 0), ptsize=24)
开发者ID:paulbalaji,项目名称:A2_Project_SHM,代码行数:54,代码来源:freetype_test.py


示例11: test_load_bad_filename

 def test_load_bad_filename(self):
     u = as_unicode(r"a\x00b\x00c.png")
     self.assertRaises(pygame.error, load_extended, u)
开发者ID:GertBurger,项目名称:pygame_cffi,代码行数:3,代码来源:imageext_test.py


示例12: test_returns_bytes

 def test_returns_bytes(self):
     u = as_unicode(r"Hello")
     self.assert_(isinstance(encode_string(u), bytes_))
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:3,代码来源:rwobject_test.py


示例13: test_encoding

 def test_encoding(self):
     u = as_unicode(r"Hello")
     self.assert_(isinstance(encode_file_path(u), bytes_))
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:3,代码来源:rwobject_test.py


示例14: test_save_bad_filename

 def test_save_bad_filename(self):
     im = pygame.Surface((10, 10), 0, 32)
     u = as_unicode(r"a\x00b\x00c.png")
     self.assertRaises(pygame.error, save_extended, im, u)
开发者ID:GertBurger,项目名称:pygame_cffi,代码行数:4,代码来源:imageext_test.py


示例15: test_etype

 def test_etype(self):
     u = as_unicode(r"a\x80b")
     self.assertRaises(SyntaxError, encode_string,
                       u, 'ascii', 'strict', SyntaxError)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:4,代码来源:rwobject_test.py


示例16: test_smp

 def test_smp(self):
     utf_8 = as_bytes("a\xF0\x93\x82\xA7b")
     u = as_unicode(r"a\U000130A7b")
     b = encode_string(u, 'utf-8', 'strict', AssertionError)
     self.assertEqual(b, utf_8)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:5,代码来源:rwobject_test.py


示例17: test_check_defaults

 def test_check_defaults(self):
     u = as_unicode(r"a\u01F7b")
     b = u.encode("unicode_escape", "backslashreplace") 
     self.assert_(encode_string(u) == b)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:4,代码来源:rwobject_test.py


示例18: test_encoding_error

 def test_encoding_error(self):
     u = as_unicode(r"a\x80b")
     self.assert_(encode_string(u, 'ascii', 'strict') is None)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:3,代码来源:rwobject_test.py


示例19: test_errors

 def test_errors(self):
     s = r"abc\u0109defg\u011Dh\u0125ij\u0135klmnoprs\u015Dtu\u016Dvz"
     u = as_unicode(s)
     b = u.encode('ascii', 'ignore')
     self.assertEqual(encode_string(u, 'ascii', 'ignore'), b)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:5,代码来源:rwobject_test.py


示例20: test_filesystem_encode

 def test_filesystem_encode(self):
     upath = compat.as_unicode(r"ab\u212Acd")
     self.assertEqual(compat.filesystem_encode(upath),
                      encode_file_path(upath))
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:4,代码来源:compat_test.py



注:本文中的pygame.compat.as_unicode函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python compat.geterror函数代码示例发布时间:2022-05-25
下一篇:
Python compat.as_bytes函数代码示例发布时间:2022-05-25
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap