本文整理汇总了Python中pygame.compat.as_bytes函数的典型用法代码示例。如果您正苦于以下问题:Python as_bytes函数的具体用法?Python as_bytes怎么用?Python as_bytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了as_bytes函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_scrap_put_text
def test_scrap_put_text (self):
scrap.put (pygame.SCRAP_TEXT, as_bytes("Hello world"))
self.assertEquals (scrap.get (pygame.SCRAP_TEXT),
as_bytes("Hello world"))
scrap.put (pygame.SCRAP_TEXT, as_bytes("Another String"))
self.assertEquals (scrap.get (pygame.SCRAP_TEXT),
as_bytes("Another String"))
开发者ID:SantoKo,项目名称:RPI3-Desktop,代码行数:8,代码来源:scrap_test.py
示例2: 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
示例3: test_as_bytes
def test_as_bytes(self):
ords = [0, 1, 0x7F, 0x80, 0xC3, 0x20, 0xC3, 0xB6, 0xFF]
s = ''.join([chr(i) for i in ords])
self.failUnlessEqual(len(s), len(ords))
b = compat.as_bytes(s)
self.failUnless(isinstance(b, compat.bytes_))
self.failUnlessEqual([compat.ord_(i) for i in b], ords)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:7,代码来源:compat_test.py
示例4: test_get_raw_more
def test_get_raw_more(self):
""" test the array interface a bit better.
"""
import platform
IS_PYPY = 'PyPy' == platform.python_implementation()
if IS_PYPY:
return
from ctypes import pythonapi, c_void_p, py_object
try:
Bytes_FromString = pythonapi.PyBytes_FromString
except:
Bytes_FromString = pythonapi.PyString_FromString
Bytes_FromString.restype = c_void_p
Bytes_FromString.argtypes = [py_object]
mixer.init()
try:
samples = as_bytes('abcdefgh') # keep byte size a multiple of 4
snd = mixer.Sound(buffer=samples)
raw = snd.get_raw()
self.assertTrue(isinstance(raw, bytes_))
self.assertNotEqual(snd._samples_address, Bytes_FromString(samples))
self.assertEqual(raw, samples)
finally:
mixer.quit()
开发者ID:Yarrcad,项目名称:CPSC-386-Pong,代码行数:26,代码来源:mixer_test.py
示例5: test_refcount
def test_refcount(self):
bpath = as_bytes(" This is a string that is not cached.")[1:]
upath = bpath.decode('ascii')
before = getrefcount(bpath)
bpath = encode_string(bpath)
self.assertEqual(getrefcount(bpath), before)
bpath = encode_string(upath)
self.assertEqual(getrefcount(bpath), before)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:8,代码来源:rwobject_test.py
示例6: test_get_raw
def test_get_raw(self):
mixer.init()
try:
samples = as_bytes('abcdefgh') # keep byte size a multiple of 4
snd = mixer.Sound(buffer=samples)
raw = snd.get_raw()
self.assertTrue(isinstance(raw, bytes_))
self.assertEqual(raw, samples)
finally:
mixer.quit()
开发者ID:Yarrcad,项目名称:CPSC-386-Pong,代码行数:11,代码来源:mixer_test.py
示例7: test_array_interface
def test_array_interface(self):
mixer.init(22050, -16, 1)
try:
snd = mixer.Sound(as_bytes('\x00\x7f') * 20)
d = snd.__array_interface__
self.assertTrue(isinstance(d, dict))
if pygame.get_sdl_byteorder() == pygame.LIL_ENDIAN:
typestr = '<i2'
else:
typestr = '>i2'
self.assertEqual(d['typestr'], typestr)
self.assertEqual(d['shape'], (20,))
self.assertEqual(d['strides'], (2,))
self.assertEqual(d['data'], (snd._samples_address, False))
finally:
mixer.quit()
开发者ID:Yarrcad,项目名称:CPSC-386-Pong,代码行数:16,代码来源:mixer_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_get_raw
def test_get_raw(self):
from ctypes import pythonapi, c_void_p, py_object
try:
Bytes_FromString = pythonapi.PyBytes_FromString
except:
Bytes_FromString = pythonapi.PyString_FromString
Bytes_FromString.restype = c_void_p
Bytes_FromString.argtypes = [py_object]
mixer.init()
try:
samples = as_bytes('abcdefgh') # keep byte size a multiple of 4
snd = mixer.Sound(buffer=samples)
raw = snd.get_raw()
self.assertTrue(isinstance(raw, bytes_))
self.assertNotEqual(snd._samples_address, Bytes_FromString(samples))
self.assertEqual(raw, samples)
finally:
mixer.quit()
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:19,代码来源:mixer_test.py
示例10: test_samples
def test_samples(self):
if not arraytype:
self.fail("no array package installed")
null_byte = as_bytes('\x00')
def check_sample(size, channels, test_data):
try:
pygame.mixer.init(22050, size, channels)
except pygame.error:
# Not all sizes are supported on all systems.
return
try:
__, sz, __ = pygame.mixer.get_init()
if sz == size:
zeroed = null_byte * ((abs(size) // 8) *
len(test_data) *
channels)
snd = pygame.mixer.Sound(buffer=zeroed)
samples = pygame.sndarray.samples(snd)
self._assert_compatible(samples, size)
##print ('X %s' % (samples.shape,))
##print ('Y %s' % (test_data,))
samples[...] = test_data
arr = pygame.sndarray.array(snd)
self.failUnless(alltrue(samples == arr),
"size: %i\n%s\n%s" %
(size, arr, test_data))
finally:
pygame.mixer.quit()
check_sample(8, 1, [0, 0x0f, 0xf0, 0xff])
check_sample(8, 2,
[[0, 0x80], [0x2D, 0x41], [0x64, 0xA1], [0xff, 0x40]])
check_sample(16, 1, [0, 0x00ff, 0xff00, 0xffff])
check_sample(16, 2, [[0, 0xffff], [0xffff, 0],
[0x00ff, 0xff00], [0x0f0f, 0xf0f0]])
check_sample(-8, 1, [0, -0x80, 0x7f, 0x64])
check_sample(-8, 2,
[[0, -0x80], [-0x64, 0x64], [0x25, -0x50], [0xff, 0]])
check_sample(-16, 1, [0, 0x7fff, -0x7fff, -1])
check_sample(-16, 2, [[0, -0x7fff], [-0x7fff, 0],
[0x7fff, 0], [0, 0x7fff]])
开发者ID:maaverik,项目名称:pygame-scripts,代码行数:42,代码来源:sndarray_test.py
示例11: 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
示例12: test_string_with_null_bytes
def test_string_with_null_bytes(self):
b = as_bytes("a\x00b\x00c")
self.assert_(encode_string(b, etype=SyntaxError) is b)
u = b.decode()
self.assert_(encode_string(u, 'ascii', 'strict') == b)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:5,代码来源:rwobject_test.py
示例13: test_path_with_null_bytes
def test_path_with_null_bytes(self):
b = as_bytes("a\x00b\x00c")
self.assert_(encode_file_path(b) is None)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:3,代码来源:rwobject_test.py
示例14: print
print ("Type %s : None" % (t,))
else:
print ("Type %s : '%s'" % (t, r.decode('ascii', 'ignore')))
if "image" in t:
namehint = t.split("/")[1]
if namehint in ['bmp', 'png', 'jpg']:
f = BytesIO(r)
loaded_surf = pygame.image.load(f, "." + namehint)
screen.blit(loaded_surf, (0,0))
elif e.type == KEYDOWN and e.key == K_p:
# Place some text into the selection.
print ("Placing clipboard text.")
scrap.put (SCRAP_TEXT,
as_bytes("Hello. This is a message from scrap."))
elif e.type == KEYDOWN and e.key == K_a:
# Get all available types.
print ("Getting the available types from the clipboard.")
types = scrap.get_types ()
print (types)
if len (types) > 0:
print ("Contains %s: %s" %
(types[0], scrap.contains (types[0])))
print ("Contains _INVALID_: ", scrap.contains ("_INVALID_"))
elif e.type == KEYDOWN and e.key == K_i:
print ("Putting image into the clipboard.")
scrap.set_mode (SCRAP_CLIPBOARD)
fp = open (os.path.join(main_dir, 'data', 'liquid.bmp'), 'rb')
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:31,代码来源:scrap_clipboard.py
示例15: test_get_BytesIO
def test_get_BytesIO(self):
BytesIO = compat.get_BytesIO()
b1 = compat.as_bytes("\x00\xffabc")
b2 = BytesIO(b1).read()
self.failUnless(isinstance(b2, compat.bytes_))
self.failUnlessEqual(b2, b1)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:6,代码来源:compat_test.py
示例16: test_etype
def test_etype(self):
b = as_bytes("a\x00b\x00c")
self.assertRaises(TypeError, encode_file_path, b, TypeError)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:3,代码来源:rwobject_test.py
示例17: test_sound_args
def test_sound_args(self):
def get_bytes(snd):
return snd.get_buffer().raw
mixer.init()
try:
sample = as_bytes('\x00\xff') * 24
wave_path = example_path(os.path.join('data', 'house_lo.wav'))
uwave_path = unicode_(wave_path)
bwave_path = uwave_path.encode(sys.getfilesystemencoding())
snd = mixer.Sound(file=wave_path)
self.assert_(snd.get_length() > 0.5)
snd_bytes = get_bytes(snd)
self.assert_(len(snd_bytes) > 1000)
self.assert_(get_bytes(mixer.Sound(wave_path)) == snd_bytes)
self.assert_(get_bytes(mixer.Sound(file=uwave_path)) == snd_bytes)
self.assert_(get_bytes(mixer.Sound(uwave_path)) == snd_bytes)
arg_emsg = 'Sound takes either 1 positional or 1 keyword argument'
try:
mixer.Sound()
except TypeError:
self.assertEqual(str(geterror()), arg_emsg)
else:
self.fail("no exception")
try:
mixer.Sound(wave_path, buffer=sample)
except TypeError:
self.assertEqual(str(geterror()), arg_emsg)
else:
self.fail("no exception")
try:
mixer.Sound(sample, file=wave_path)
except TypeError:
self.assertEqual(str(geterror()), arg_emsg)
else:
self.fail("no exception")
try:
mixer.Sound(buffer=sample, file=wave_path)
except TypeError:
self.assertEqual(str(geterror()), arg_emsg)
else:
self.fail("no exception")
try:
mixer.Sound(foobar=sample)
except TypeError:
emsg = "Unrecognized keyword argument 'foobar'"
self.assertEqual(str(geterror()), emsg)
else:
self.fail("no exception")
snd = mixer.Sound(wave_path, **{})
self.assertEqual(get_bytes(snd), snd_bytes)
snd = mixer.Sound(*[], **{'file': wave_path})
try:
snd = mixer.Sound([])
except TypeError:
emsg = 'Unrecognized argument (type list)'
self.assertEqual(str(geterror()), emsg)
else:
self.fail("no exception")
try:
snd = mixer.Sound(buffer=[])
except TypeError:
emsg = 'Expected object with buffer interface: got a list'
self.assertEqual(str(geterror()), emsg)
else:
self.fail("no exception")
ufake_path = unicode_('12345678')
self.assertRaises(pygame.error, mixer.Sound, ufake_path)
try:
mixer.Sound(buffer=unicode_('something'))
except TypeError:
emsg = 'Unicode object not allowed as buffer object'
self.assertEqual(str(geterror()), emsg)
else:
self.fail("no exception")
self.assertEqual(get_bytes(mixer.Sound(buffer=sample)), sample)
self.assertEqual(get_bytes(mixer.Sound(sample)), sample)
self.assertEqual(get_bytes(mixer.Sound(file=bwave_path)), snd_bytes)
self.assertEqual(get_bytes(mixer.Sound(bwave_path)), snd_bytes)
snd = mixer.Sound(wave_path)
try:
mixer.Sound(wave_path, array=snd)
except TypeError:
self.assertEqual(str(geterror()), arg_emsg)
else:
self.fail("no exception")
try:
mixer.Sound(buffer=sample, array=snd)
except TypeError:
self.assertEqual(str(geterror()), arg_emsg)
else:
self.fail("no exception")
snd2 = mixer.Sound(array=snd)
self.assertEqual(snd.get_buffer().raw, snd2.get_buffer().raw)
finally:
mixer.quit()
开发者ID:IuryAlves,项目名称:pygame,代码行数:98,代码来源:mixer_test.py
示例18: test_obj_bytes
def test_obj_bytes(self):
b = as_bytes("encyclop\xE6dia")
self.assert_(encode_string(b, 'ascii', 'strict') is b)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:3,代码来源:rwobject_test.py
示例19: test_put
def test_put (self):
scrap.put ("arbitrary buffer", as_bytes("buf"))
r = scrap.get ("arbitrary buffer")
self.assertEquals (r, as_bytes("buf"))
开发者ID:SantoKo,项目名称:RPI3-Desktop,代码行数:4,代码来源:scrap_test.py
示例20: OLDBUF_test_oldbuf_arg
def OLDBUF_test_oldbuf_arg(self):
from pygame.bufferproxy import (get_segcount, get_read_buffer,
get_write_buffer)
content = as_bytes('\x01\x00\x00\x02') * 12
memory = ctypes.create_string_buffer(content)
memaddr = ctypes.addressof(memory)
def raise_exception(o):
raise ValueError("An exception")
bf = BufferProxy({'shape': (len(content),),
'typestr': '|u1',
'data': (memaddr, False),
'strides': (1,)})
seglen, segaddr = get_read_buffer(bf, 0)
self.assertEqual(segaddr, 0)
self.assertEqual(seglen, 0)
seglen, segaddr = get_write_buffer(bf, 0)
self.assertEqual(segaddr, 0)
self.assertEqual(seglen, 0)
segcount, buflen = get_segcount(bf)
self.assertEqual(segcount, 1)
self.assertEqual(buflen, len(content))
seglen, segaddr = get_read_buffer(bf, 0)
self.assertEqual(segaddr, memaddr)
self.assertEqual(seglen, len(content))
seglen, segaddr = get_write_buffer(bf, 0)
self.assertEqual(segaddr, memaddr)
self.assertEqual(seglen, len(content))
bf = BufferProxy({'shape': (len(content),),
'typestr': '|u1',
'data': (memaddr, True),
'strides': (1,)})
segcount, buflen = get_segcount(bf)
self.assertEqual(segcount, 1)
self.assertEqual(buflen, len(content))
seglen, segaddr = get_read_buffer(bf, 0)
self.assertEqual(segaddr, memaddr)
self.assertEqual(seglen, len(content))
self.assertRaises(ValueError, get_write_buffer, bf, 0)
bf = BufferProxy({'shape': (len(content),),
'typestr': '|u1',
'data': (memaddr, True),
'strides': (1,),
'before': raise_exception})
segcount, buflen = get_segcount(bf)
self.assertEqual(segcount, 0)
self.assertEqual(buflen, 0)
bf = BufferProxy({'shape': (3, 4),
'typestr': '|u4',
'data': (memaddr, True),
'strides': (12, 4)})
segcount, buflen = get_segcount(bf)
self.assertEqual(segcount, 3 * 4)
self.assertEqual(buflen, 3 * 4 * 4)
for i in range(0, 4):
seglen, segaddr = get_read_buffer(bf, i)
self.assertEqual(segaddr, memaddr + i * 4)
self.assertEqual(seglen, 4)
开发者ID:AjithPanneerselvam,项目名称:Gamepy,代码行数:62,代码来源:bufferproxy_test.py
注:本文中的pygame.compat.as_bytes函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论