本文整理汇总了Python中pyamf.get_encoder函数的典型用法代码示例。如果您正苦于以下问题:Python get_encoder函数的具体用法?Python get_encoder怎么用?Python get_encoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_encoder函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_expando
def test_expando(self):
# 'borrowed' from http://code.google.com/appengine/docs/datastore/entitiesandmodels.html
class Pet(db.Expando):
name = db.StringProperty(required=True)
type = db.StringProperty(required=True, choices=set(["cat", "dog", "bird"]))
birthdate = db.DateProperty()
weight_in_pounds = db.IntegerProperty()
spayed_or_neutered = db.BooleanProperty()
jessica = Pet(name='Jessica', type='cat')
jessica.birthdate = datetime.date(1986, 10, 2)
jessica.weight_in_pounds = 5
jessica.spayed_or_neutered = False
jessica.foo = 'bar'
encoder = pyamf.get_encoder(pyamf.AMF0)
encoder.writeElement(jessica)
self.assertEquals(encoder.stream.getvalue(),
'\x03\x00\x04name\x02\x00\x07Jessica\x00\tbirthdate\x0bB^\xc4\xae'
'\xaa\x00\x00\x00\x00\x00\x00\x10weight_in_pounds\[email protected]\x14\x00'
'\x00\x00\x00\x00\x00\x00\x03foo\x02\x00\x03bar\x00\x04type\x02'
'\x00\x03cat\x00\x12spayed_or_neutered\x01\x00\x00\x00\t')
encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeElement(jessica)
self.assertEquals(encoder.stream.getvalue(),
'\n\x0b\x01\tname\x06\x0fJessica\x13birthdate\x08\x01B^\xc4\xae'
'\xaa\x00\x00\x00!weight_in_pounds\x04\x05\x07foo\x06\x07bar\t'
'type\x06\x07cat%spayed_or_neutered\x02\x01')
示例2: test_expando
def test_expando(self):
class Author(db.Expando):
name = db.StringProperty()
class Novel(db.Expando):
title = db.StringProperty()
author = db.ReferenceProperty(Author)
a = Author(name='Jane Austen')
a.put()
k = str(a.key())
b = Novel(title='Sense and Sensibility', author=a)
self.assertEquals(b.author, a)
try:
encoder = pyamf.get_encoder(pyamf.AMF0)
encoder.writeElement(b)
self.assertEquals(encoder.stream.getvalue(),
'\x03\x00\x04_key\x05\x00\x06author\x03\x00\x04_key\x02%s%s'
'\x00\x04name\x02\x00\x0bJane Austen\x00\x00\t\x00\x05title'
'\x02\x00\x15Sense and Sensibility\x00\x00\t' % (
struct.pack('>H', len(k)), k))
encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeElement(b)
self.assertEquals(encoder.stream.getvalue(),
'\n;\x01\t_key\rauthor\x0btitle\x01\n+\x01\x00\tname\x06%s%s'
'\x06\x17Jane Austen\x01\x06+Sense and Sensibility\x01' % (
amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k))
# now test with aliases ..
pyamf.register_class(Author, 'Author')
pyamf.register_class(Novel, 'Novel')
encoder = pyamf.get_encoder(pyamf.AMF0)
encoder.writeElement(b)
self.assertEquals(encoder.stream.getvalue(), '\x10\x00\x05Novel'
'\x00\x04_key\x05\x00\x06author\x10\x00\x06Author\x00\x04_key'
'\x02%s%s\x00\x04name\x02\x00\x0bJane Austen\x00\x00\t\x00'
'\x05title\x02\x00\x15Sense and Sensibility\x00\x00\t' % (
struct.pack('>H', len(k)), k))
encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeElement(b)
self.assertEquals(encoder.stream.getvalue(),
'\n;\x0bNovel\t_key\rauthor\x0btitle\x01\n+\rAuthor\x02\tname'
'\x06%s%s\x06\x17Jane Austen\x01\x06+Sense and Sensibility'
'\x01' % (amf3._encode_int(len(k) << 1 | amf3.REFERENCE_BIT), k))
except:
a.delete()
raise
a.delete()
示例3: test_objects_all
def test_objects_all(self):
encoder = pyamf.get_encoder(pyamf.AMF0)
encoder.writeElement(models.SimplestModel.objects.all())
self.assertEqual(encoder.stream.getvalue(), '\n\x00\x00\x00\x00')
encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeElement(models.SimplestModel.objects.all())
self.assertEqual(encoder.stream.getvalue(), '\t\x01\x01')
示例4: test_NOT_PROVIDED
def test_NOT_PROVIDED(self):
from django.db.models import fields
encoder = pyamf.get_encoder(pyamf.AMF0)
encoder.writeElement(fields.NOT_PROVIDED)
self.assertEquals(encoder.stream.getvalue(), '\x06')
encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeElement(fields.NOT_PROVIDED)
self.assertEquals(encoder.stream.getvalue(), '\x00')
示例5: test_cached_reference_properties
def test_cached_reference_properties(self):
class Author(db.Model):
name = db.StringProperty()
class Novel(db.Model):
title = db.StringProperty()
author = db.ReferenceProperty(Author)
a = Author(name="Jane Austen")
a.put()
k = str(a.key())
b = Novel(title="Sense and Sensibility", author=a)
b.put()
c = Novel(title="Pride and Prejudice", author=a)
c.put()
try:
s, p = Novel.all().order("-title").fetch(2)
encoder = pyamf.get_encoder(pyamf.AMF3)
stream = encoder.stream
context = encoder.context
self.assertFalse(hasattr(context, "gae_objects"))
encoder.writeElement(s)
self.assertTrue(hasattr(context, "gae_objects"))
self.assertEquals(context.gae_objects, {Novel: {str(s.key()): s}, Author: {k: a}})
encoder.writeElement(p)
self.assertEquals(context.gae_objects, {Novel: {str(s.key()): s, str(p.key()): p}, Author: {k: a}})
finally:
a.delete()
b.delete()
c.delete()
c = Novel(title="Pride and Prejudice", author=None)
c.put()
encoder = pyamf.get_encoder(encoding=pyamf.AMF3)
alias = adapter_db.DataStoreClassAlias(Novel, None)
self.assertEquals(
alias.getAttributes(c, codec=encoder),
({"_key": str(c.key()), "title": "Pride and Prejudice", "author": None}, {}),
)
示例6: test_objects_all
def test_objects_all(self):
from django.db import models
class Spam(models.Model):
pass
self.resetDB()
encoder = pyamf.get_encoder(pyamf.AMF0)
encoder.writeElement(Spam.objects.all())
self.assertEquals(encoder.stream.getvalue(), '\n\x00\x00\x00\x00')
encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeElement(Spam.objects.all())
self.assertEquals(encoder.stream.getvalue(), '\t\x01\x01')
示例7: pyamfEncode
def pyamfEncode(self, obj, amf3=False, use_proxies=False):
if amf3 is True:
context = pyamf.get_context(pyamf.AMF3)
else:
context = pyamf.get_context(pyamf.AMF0)
stream = BufferedByteStream()
if amf3 is True:
pyamf_encoder = pyamf.get_encoder(pyamf.AMF3, stream=stream, context=context)
else:
pyamf_encoder = pyamf.get_encoder(pyamf.AMF0, stream=stream, context=context)
pyamf_encoder.writeElement(obj)
return pyamf_encoder.stream.getvalue()
示例8: test_objects_all
def test_objects_all(self):
class Spam(models.Model):
pass
cursor = connection.cursor()
cursor.execute('CREATE TABLE gateway_spam (id INTEGER PRIMARY KEY)')
encoder = pyamf.get_encoder(pyamf.AMF0)
encoder.writeElement(Spam.objects.all())
self.assertEquals(encoder.stream.getvalue(), '\n\x00\x00\x00\x00')
encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeElement(Spam.objects.all())
self.assertEquals(encoder.stream.getvalue(), '\t\x01\x01')
cursor.execute('DROP TABLE gateway_spam')
示例9: send
def send(self, message):
self.sends.append(message)
# tell server we started listening
logging.debug('send: request: %s' % message.__repr__())
encoder = pyamf.get_encoder(object_encoding)
encoder.writeElement(message)
message = encoder.stream.getvalue()
encoder.stream.truncate()
logging.debug('send: encoded request: %s' % message.__repr__())
send_log = encoder.stream.tell().__repr__() \
+ encoder.stream.__repr__()
logging.debug('send; ' + send_log)
decoder = pyamf.get_decoder(object_encoding, message)
#if not isinstance(stream, util.BufferedByteStream):
# stream = util.BufferedByteStream(stream)
logging.debug('send: '
+ decoder.stream.tell().__repr__()
+ decoder.stream.__repr__())
data = decoder.readElement()
logging.debug('send: decoded %s' % data.__repr__())
try:
#total_sent = 0
#while total_sent < len(message):
sent = self.sock.send(message)
if sent == 0:
raise RuntimeError, \
"socket connection broken"
# total_sent += sent
except socket.error, e:
raise Exception("Can't connect: %s" % e[1])
示例10: test_amf3
def test_amf3(self):
encoder = pyamf.get_encoder(pyamf.AMF3)
context = encoder.context
stream = encoder.stream
s = str(self.jessica.key())
self.assertFalse(hasattr(context, "gae_objects"))
encoder.writeObject(self.jessica)
self.assertTrue(hasattr(context, "gae_objects"))
self.assertEquals(context.gae_objects, {PetModel: {s: self.jessica}})
self.assertEquals(
stream.getvalue(),
"\nk\x01\t_key!weight_in_pounds"
"\ttype\tname\x13birthdate%%spayed_or_neutered\x06%s%s\x04\x05\x06"
"\x07cat\x06\x0fJessica\x08\x01B^\xc4\xae\xaa"
"\x00\x00\x00\x02\x01" % (amf3._encode_int(len(s) << 1 | amf3.REFERENCE_BIT), s),
)
stream.truncate()
encoder.writeObject(self.jessica2)
self.assertTrue(hasattr(context, "gae_objects"))
self.assertEquals(context.gae_objects, {PetModel: {s: self.jessica}})
self.assertEquals(stream.getvalue(), "\n\x00")
示例11: __init__
def __init__( self ):
# Prepare the encoder and decoder
self.encoder = pyamf.get_encoder( self.encoding )
self.ostream = self.encoder.stream
self.decoder = pyamf.get_decoder( self.encoding )
self.istream = self.decoder.stream
self.ipos = 0
示例12: test_amf0
def test_amf0(self):
encoder = pyamf.get_encoder(pyamf.AMF0)
context = encoder.context
stream = encoder.stream
s = str(self.jessica.key())
self.assertFalse(hasattr(context, 'gae_objects'))
encoder.writeObject(self.jessica)
self.assertTrue(hasattr(context, 'gae_objects'))
self.assertEquals(context.gae_objects, {PetModel: {s: self.jessica}})
self.assertEquals(stream.getvalue(), '\x03\x00\x04_key\x02%s%s\x00'
'\tbirthdate\x0bB^\xc4\xae\xaa\x00\x00\x00\x00\x00\x00\x04name'
'\x02\x00\x07Jessica\x00\x12spayed_or_neutered\x01\x00\x00\x04'
'type\x02\x00\x03cat\x00\x10weight_in_pounds\[email protected]\x14\x00\x00\x00'
'\x00\x00\x00\x00\x00\t' % (struct.pack('>H', len(s)), s))
stream.truncate()
encoder.writeObject(self.jessica2)
self.assertTrue(hasattr(context, 'gae_objects'))
self.assertEquals(context.gae_objects, {PetModel: {s: self.jessica}})
self.assertEquals(stream.getvalue(), '\x07\x00\x00')
stream.truncate()
# check a non referenced object
toby = PetModel(name='Toby', type='cat')
toby.put()
try:
encoder.writeObject(toby)
finally:
toby.delete()
示例13: test_amf3
def test_amf3(self):
encoder = pyamf.get_encoder(pyamf.AMF3)
encoder.writeElement(self.jessica)
self.assertEquals(encoder.stream.getvalue(),
'\nk\x01\t_key\x13birthdate\tname%spayed_or_neutered\ttype!'
'weight_in_pounds\x01\x08\x01B^\xc4\xae\xaa\x00\x00\x00\x06\x0f'
'Jessica\x02\x06\x07cat\x04\x05\x01')
示例14: test_proxy
def test_proxy(self):
from pyamf import flex
c = pyamf.get_encoder(pyamf.AMF3)
self.alias.proxy_attrs = ('foo', 'bar')
self.alias.compile()
self.assertEqual(self.alias.proxy_attrs, ['bar', 'foo'])
self.obj.foo = ['bar', 'baz']
self.obj.bar = {'foo': 'gak'}
attrs = self.alias.getEncodableAttributes(self.obj, c)
k = attrs.keys()
k.sort()
self.assertEqual(k, ['bar', 'foo'])
self.assertTrue(isinstance(attrs['foo'], flex.ArrayCollection))
self.assertEqual(attrs['foo'], ['bar', 'baz'])
self.assertTrue(isinstance(attrs['bar'], flex.ObjectProxy))
self.assertEqual(attrs['bar']._amf_object, {'foo': 'gak'})
示例15: __init__
def __init__(self):
self.started = False
self.encoder = pyamf.get_encoder(self.encoding)
self.stream = self.encoder.stream
self.context= self.encoder.context
pyamf.register_class(Screen, "Screen")
amf3.use_proxies_default = True