本文整理汇总了Python中json.encoder.JSONEncoder.encode方法的典型用法代码示例。如果您正苦于以下问题:Python JSONEncoder.encode方法的具体用法?Python JSONEncoder.encode怎么用?Python JSONEncoder.encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类json.encoder.JSONEncoder
的用法示例。
在下文中一共展示了JSONEncoder.encode方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __input_loop
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
def __input_loop(self, protocol, base):
"""
:type protocol server.WebSocketServerProtocol
:param base:
:return:
"""
json_encoder = JSONEncoder()
yield from protocol.send(json_encoder.encode({"message": _GET_NICK}))
nick = yield from protocol.recv()
self._chat_api.enter_chat(nick)
yield from protocol.send(
json_encoder.encode({"message": _ADD, "text": ",".join(self._chat_api.nicks_in_chat)}))
while self._chat_api.connection_opened:
message = yield from protocol.recv()
self._chat_api.say_to_chat(message)
示例2: __output_loop
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
def __output_loop(self, protocol, base):
"""
:type protocol server.WebSocketServerProtocol
:param base:
:return:
"""
room_queue = self._chat_api.subscribe_to_chat()
json_encoder = JSONEncoder()
current_people = self._chat_api.nicks_in_chat
while self._chat_api.connection_opened:
message = yield from room_queue.get()
yield from protocol.send(json_encoder.encode({"message": _TEXT, "text": message}))
if current_people != self._chat_api.nicks_in_chat:
current_people = self._chat_api.nicks_in_chat
yield from protocol.send(
json_encoder.encode({"message": _ADD, "text": ",".join(self._chat_api.nicks_in_chat)}))
示例3: JSONSerializer
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
class JSONSerializer(CustomizableSerializer):
"""
Serializes objects using JSON (JavaScript Object Notation).
See the :mod:`json` module documentation in the standard library for more information on
available options.
Certain options can resolve references to objects:
* ``encoder_options['default']``
* ``decoder_options['object_hook']``
* ``decoder_options['object_pairs_hook']``
:param encoder_options: keyword arguments passed to :class:`~json.JSONEncoder`
:param decoder_options: keyword arguments passed to :class:`~json.JSONDecoder`
:param encoding: the text encoding to use for converting to and from bytes
:param custom_type_codec: wrapper to use to wrap custom types after marshalling
"""
__slots__ = ('encoder_options', 'decoder_options', 'encoding', 'custom_type_codec',
'_encoder', '_decoder', '_marshallers', '_unmarshallers')
def __init__(self, encoder_options: Dict[str, Any] = None,
decoder_options: Dict[str, Any] = None, encoding: str = 'utf-8',
custom_type_codec: Union[JSONTypeCodec, str] = None) -> None:
assert check_argument_types()
super().__init__(resolve_reference(custom_type_codec) or JSONTypeCodec())
self.encoding = encoding
self.encoder_options = encoder_options or {}
self.encoder_options['default'] = resolve_reference(self.encoder_options.get('default'))
self._encoder = JSONEncoder(**self.encoder_options)
self.decoder_options = decoder_options or {}
self.decoder_options['object_hook'] = resolve_reference(
self.decoder_options.get('object_hook'))
self.decoder_options['object_pairs_hook'] = resolve_reference(
self.decoder_options.get('object_pairs_hook'))
self._decoder = JSONDecoder(**self.decoder_options)
def serialize(self, obj) -> bytes:
return self._encoder.encode(obj).encode(self.encoding)
def deserialize(self, payload: bytes):
text_payload = payload.decode(self.encoding)
return self._decoder.decode(text_payload)
@property
def mimetype(self):
return 'application/json'
示例4: encode_json
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
def encode_json(obj, inline=False, **kwargs):
"""Encode the given object as json.
Supports objects that follow the `_asdict` protocol. See `parse_json` for more information.
:param obj: A serializable object.
:param bool inline: `True` to inline all resolvable objects as nested JSON objects, `False` to
serialize those objects' addresses instead; `False` by default.
:param **kwargs: Any kwargs accepted by :class:`json.JSONEncoder` besides `encoding` and
`default`.
:returns: A UTF-8 json encoded blob representing the object.
:rtype: string
:raises: :class:`ParseError` if there were any problems encoding the given `obj` in json.
"""
encoder = JSONEncoder(encoding='UTF-8',
default=functools.partial(_object_encoder, inline=inline),
**kwargs)
return encoder.encode(obj)
示例5: index
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
def index():
if request.method == "POST":
site_hash = sha1(request.form["url"]).hexdigest()
zipf_profile = r.get(site_hash)
if zipf_profile:
return zipf_profile
else:
try:
corpus = WebCorpus(request.form["url"])
except:
abort(404)
e = JSONEncoder()
zipf_profile = e.encode(corpus.freq_list)
r.set(site_hash, zipf_profile)
r.expire(site_hash, 120)
resp = make_response(zipf_profile)
return resp
elif request.method == "GET":
return render_template("index.html")
示例6: BaseAsyncTasks
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
class BaseAsyncTasks(object):
def __init__(self, priority=DEFAULT_JOB_PRIORITY, delay=0, ttr=DEFAULT_JOB_TTR):
self._priority = priority
self._delay = delay
self._ttr = ttr
self._jsonEncoder = JSONEncoder()
'''
@function -> the reference to the annotated asynchronous business logic function
@args -> a json serializable list of parameters
@priority -> you can set a priority for your task, so the consumers can process it accordingly
@delay -> you can set a delay, the default is 0
@ttr -> ttr value. see: http://github.com/kr/beanstalkd/wiki/faq
'''
def run(self, function, args):
logger = logging.getLogger('BaseAsyncTasks.run')
if settings.BEANSTALK_ENABLED :
beanstalkClient = BeanstalkClient()
module = function.__module__.__self__.app
# Add more metadata to the args
args['__uuid__'] = str(uuid4()).replace('-', '')
func = ".".join([module, function.__name__])
body = self._jsonEncoder.encode(args)
pid = beanstalkClient.call(func, body,
priority=self._priority,
delay=self._delay,
ttr=self._ttr)
try :
if not pid >= 0:
logger.critical("Failed to execute task: " + str(function) + " " + str(args))
JobRecord.objects.create(jid=pid, tube=func, body=body, uuid=args['__uuid__'], status=JobStatus.FAILED_TO_REGISTER)
else:
JobRecord.objects.create(jid=pid, tube=func, body=body, uuid=args['__uuid__'], status=JobStatus.READY)
except Exception, ex:
logger.exception('Error while persisting Job object. %s' % str(ex))
else :
示例7: __shutdown
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
def __shutdown(self):
beanstalkClient = BeanstalkClient()
jsonEncoder = JSONEncoder()
args = {'timestamp' : time.time()}
pid = beanstalkClient.call("shutdown", jsonEncoder.encode(args), priority=0) #high pri job
示例8: Trainer
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
class Trainer(object):
categories = {}
def __init__(self):
self._jsonDecoder = JSONDecoder()
self._jsonEncoder = JSONEncoder()
self.__featureExtractor = FeatureExtractor()
def setFeatureExtractor(self, featureExtractor):
self.__featuredExtractor = featureExtractor
def __isNumeric(self, feature):
isNumeric = False
try:
float(feature)
isNumeric = True
except ValueError:
pass
return isNumeric
"""
Given a list of yes category names, retrieves the yes/no category hash that the trainer needs
"""
def __getCategoriesFromNames(self, yesTagNames, noTagNames):
finalCategories = []
# create the categories if they don't already exist
for tagName in yesTagNames:
if tagName:
categoryYes, _ = ClassifierCategory.objects.get_or_create(categoryName=tagName, yes=True)
categoryNo, _ = ClassifierCategory.objects.get_or_create(categoryName=tagName, yes=False)
finalCategories.append(categoryYes)
for tagName in noTagNames:
if tagName:
categoryYes, _ = ClassifierCategory.objects.get_or_create(categoryName=tagName, yes=True)
categoryNo, _ = ClassifierCategory.objects.get_or_create(categoryName=tagName, yes=False)
finalCategories.append(categoryNo)
return finalCategories
"""
Trains a corpus of data.
"""
@transaction.commit_manually
def train(self, corpus="", yesTagNames=None, noTagNames=None):
logger = logging.getLogger("Trainer.train")
success = False
categories = []
try:
document = Document.getDocumentByCorpus(corpus)
if not document:
features = self.__featuredExtractor.getFeatures(corpus)
categories = self.__getCategoriesFromNames(yesTagNames, noTagNames)
document = Document(corpus=corpus)
document.save()
documentCounts = {}
for category in categories:
self.__incrementCategoryCount(documentCounts, category)
DocumentCategoryCounts(document=document, countData=self._jsonEncoder.encode(documentCounts)).save()
for feature in features:
featureCount, _ = FeatureCounts.objects.get_or_create(featureName=feature)
counts = self._jsonDecoder.decode(featureCount.countData) if featureCount.countData else {}
for category in categories:
self.__incrementCategoryCount(counts, category)
featureCount.countData = self._jsonEncoder.encode(counts)
featureCount.save()
# We keep an index of category document counts for faster classification later on
catDocCountIndex = CategoryDocumentCountIndex.getCountIndex()
index = self._jsonDecoder.decode(catDocCountIndex.countData) if catDocCountIndex.countData else {}
for category in categories:
self.__incrementCategoryCount(index, category)
catDocCountIndex.countData = self._jsonEncoder.encode(index)
catDocCountIndex.save()
success = True
transaction.commit()
else:
logger.info("Document already exists: " + str(document.id) + " - " + document.corpusHash)
success = True
except Exception, ex:
logger.info("Bad data:%s" % corpus)
logger.exception("Failed to save the trained data: " + str(ex))
transaction.rollback()
return success
示例9: len
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
# load original language file
if len(sys.argv) > 1:
language_path = os.path.dirname(sys.argv[1])
language_file = xml.parse(sys.argv[1])
else:
language_path = os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), 'site', 'data')
language_file = xml.parse(os.path.join(language_path, 'language.xml'))
# get all languages
output = {}
for language in language_file.getElementsByTagName('language'):
code = language.attributes['short'].value
output[code] = {}
for constant in language.getElementsByTagName('constant'):
constant_name = constant.attributes['name'].value
if constant.firstChild:
output[code][constant_name] = constant.firstChild.data
else:
output[code][constant_name] = ''
# save output files
encoder = JSONEncoder(**encoder_options)
for code, language in output.items():
with open(os.path.join(language_path, 'language_{0}.json'.format(code)), 'w') as raw_file:
raw_file.write(encoder.encode(language).encode('utf8'))
示例10: encode
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
def encode(self, o):
pickle_bytes = pickle.dumps(o)
pickle_str = binascii.b2a_qp(pickle_bytes).decode(encoding='utf8')
o = {JSONCodec._obj: pickle_str,
JSONCodec._ver_key: JSONCodec._ver}
return JSONEncoder.encode(self, o)
示例11: __init__
# 需要导入模块: from json.encoder import JSONEncoder [as 别名]
# 或者: from json.encoder.JSONEncoder import encode [as 别名]
class Connector:
"""description of class"""
def __init__(self, api_key):
self.key = api_key
self.host = "api.projectoxford.ai"
self.base_url = "/face/v1.0/{}"
self.encoder = JSONEncoder()
self.decoder = JSONDecoder()
def encode_json(self, dictionary):
"""
encodes dictionaries to json to send to API
"""
return self.encoder.encode(dictionary)
def decode_json(self, json):
"""
decodes json to a dictionary
"""
return self.decoder.decode(json)
def send_request(self, method, url, qs_args=None, headers=None, body=None):
"""
Sends a request to the API.
"""
# Because having a dictionary as default value is dangerous
if qs_args is None:
qs_args = {}
if headers is None:
headers = {}
# Check what content type header to include in the HTTP message
if hasattr(body, "read") or isinstance(body, bytes):
headers["Content-Type"] = "application/octet-stream"
else:
body = self.encode_json(body)
headers["Content-Type"] = "application/json"
connection = HTTPSConnection(self.host)
# Format the url
url = self.base_url.format(url)
if len(qs_args) > 0:
url += "?{}".format(urlencode(qs_args))
# Add api-key to the headers
headers["Ocp-Apim-Subscription-Key"] = self.key
# Send the request
connection.request(method, url, headers=headers, body=body)
# Read the response and try to decode JSON
response = connection.getresponse()
data_bytes = response.read()
data = data_bytes.decode()
# TODO: Except data that is not JSON
if len(data) > 0:
data = self.decode_json(data)
return data, response