我们从Python开源项目中,提取了以下7个代码示例,用于说明如何使用google.appengine.ext.db.model_to_protobuf()。
def _write_backup_info(self, backup_info): """Writes a backup_info_file. Args: backup_info: Required BackupInformation. Returns: Backup info filename. """ filename = self._generate_filename(backup_info, '.backup_info') backup_info.gs_handle = filename with GCSUtil.open(filename, 'w') as info_file: with records.RecordsWriter(info_file) as writer: writer.write('1') writer.write(db.model_to_protobuf(backup_info).SerializeToString()) for kind_files in backup_info.get_kind_backup_files(): writer.write(db.model_to_protobuf(kind_files).SerializeToString()) return filename
def _fetch_latest_from_datastore(app_version): """Get the latest configuration data for this app-version from the datastore. Args: app_version: the major version you want configuration data for. Side Effects: We populate memcache with whatever we find in the datastore. Returns: A config class instance for most recently set options or None if the query could not complete due to a datastore exception. """ rpc = db.create_rpc(deadline=DATASTORE_DEADLINE, read_policy=db.EVENTUAL_CONSISTENCY) key = _get_active_config_key(app_version) config = None try: config = Config.get(key, rpc=rpc) logging.debug('Loaded most recent conf data from datastore.') except: logging.warning('Tried but failed to fetch latest conf data from the ' 'datastore.') if config: memcache.set(app_version, db.model_to_protobuf(config).Encode(), namespace=NAMESPACE) logging.debug('Wrote most recent conf data into memcache.') return config
def match(document, topic=None, result_key=None, result_relative_url='/_ah/prospective_search', result_task_queue='default', result_batch_size=DEFAULT_RESULT_BATCH_SIZE, result_return_document=True): """Match document with all subscribed queries on specified topic. Args: document: instance of datastore.Entity or db.Model document. topic: required for datastore.Entity, optional for db.Model. Only subscriptions of this topic will be matched against this document. result_key: key to return in result, potentially to identify document. result_relative_url: url of taskqueue event handler for results. result_task_queue: name of taskqueue queue to put batched results on. result_batch_size: number of subscription ids per taskqueue task batch. result_return_document: returns document with match results if true. Raises: DocumentTypeError: document type is unsupported. TopicNotSpecified: raised for datastore.Entity if topic is not specified. apiproxy_errors.Error: match call failed. """ from google.appengine.ext import db request = prospective_search_pb.MatchRequest() if isinstance(document, db.Model): topic = _get_document_topic(document, topic) doc_pb = db.model_to_protobuf(document) if result_return_document: request.set_result_python_document_class(_doc_class.MODEL) elif isinstance(document, datastore.Entity): topic = _get_document_topic(document, topic) doc_pb = document.ToPb() if result_return_document: request.set_result_python_document_class(_doc_class.ENTITY) else: raise DocumentTypeError() request.set_topic(topic) request.mutable_document().CopyFrom(doc_pb) if result_key: request.set_result_key(result_key) request.set_result_relative_url(result_relative_url) request.set_result_task_queue(result_task_queue) request.set_result_batch_size(result_batch_size) response = prospective_search_pb.MatchResponse() _make_sync_call('matcher', 'Match', request, response)