def data_received(self, data): conn = self._connection parser = conn.parser parser.feed(data) response = parser.get() request = self._request if len(request) == 2: if response is not False: if not isinstance(response, Exception): cmnd = request[0][0] response = self.parse_response(response, cmnd, request[1]) else: response = ResponseError(response) self.finished(response) else: # pipeline commands, raise_on_error, responses = request error = None while response is not False: if (isinstance(response, Exception) and raise_on_error and not error): error = response responses.append(response) response = parser.get() if len(responses) == len(commands): response = [] for cmds, resp in zip(commands[1:-1], responses[-1]): args, options = cmds if isinstance(resp, Exception) and not error: error = resp resp = self.parse_response(resp, args[0], options) response.append(resp) if error and raise_on_error: response = ResponseError(error) self.finished(response)
def execute_transaction(self, transaction): '''Execute a ``transaction`` ''' updates = [] models = [] for command in transaction.commands: action = command.action if not action: raise NotImplementedError else: model = command.args updates.append(dict(self.model_data(model, action))) models.append(model) # if updates: executed = yield self.update_documents(self._database, updates) errors = [] for doc, model in zip(executed, models): if doc.get('ok'): model['id'] = doc['id'] model['_rev'] = doc['rev'] model._modified.clear() elif doc.get('error'): errors.append(CouchDbError(doc['error'], doc['reason'])) if errors: raise errors[0] coroutine_return(models)
def execute_transaction(self, transaction): '''Execute a ``transaction`` ''' updates = [] models = [] for command in transaction.commands: action = command.action if not action: raise NotImplementedError else: model = command.args updates.append(dict(self.model_data(model, action))) models.append(model) # if updates: executed = yield self.update_documents(self._database, updates) errors = [] for doc, model in zip(executed, models): if doc.get('ok'): model['id'] = doc['id'] model['_rev'] = doc['rev'] model._modified.clear() elif doc.get('error'): errors.append(CouchDbError(doc['error'], doc['reason'])) if errors: raise errors[0]
def sort_return_tuples(response, groups=None, **options): """ If ``groups`` is specified, return the response as a list of n-element tuples with n being the value found in options['groups'] """ if not response or not groups: return response return list(zip(*[response[i::groups] for i in range(groups)]))
def pubsub_callback(response, subcommand=None): if subcommand == 'numsub': it = iter(response) return dict(((k, int(v)) for k, v in zip(it, it))) return pairs_to_object(response) elif subcommand == 'numpat': return int(response) else: return response
def parse_response(self, response, command_name, **options): if self.transaction: if command_name != 'EXEC': return response else: data = [] callbacks = self.response_callbacks for r, cmd in zip(response, self.command_stack): if not isinstance(r, Exception): args, opt = cmd command_name = args[0] if command_name in callbacks: r = callbacks[command_name](r, **opt) data.append(r) return data else: callbacks = self.response_callbacks if command_name in callbacks: response = callbacks[command_name](response, **options) if command_name in self.UNWATCH_COMMANDS: self.watching = False elif command_name == 'WATCH': self.watching = True return response
def values_to_zset(response, withscores=False, **kw): if withscores: it = iter(response) return Zset(((float(score), value) for value, score in zip(it, it))) else: return response
def pairs_to_object(response, factory=None): it = iter(response) return (factory or dict)(zip(it, it))
def values_to_object(response, fields=None, factory=None): if fields is not None: return (factory or dict)(zip(fields, response)) else: return response
def random(self, size=100, score_min=-10, score_max=10): scores = populate('float', size, score_min, score_max) data = populate('string', size, min_len=5, max_len=10) return self.skiplist(zip(scores, data))
def json(self): '''An ordered dictionary representation of this :class:`Column`. Used when serialising to json.''' return OrderedDict(((name, value) for name, value in zip(self._fields, self) if value is not None))