def zadd(self, name, *args, **kwargs): """ Set any number of score, element-name pairs to the key ``name``. Pairs can be specified in two ways: As ``*args``, in the form of:: score1, name1, score2, name2, ... or as ``**kwargs``, in the form of:: name1=score1, name2=score2, ... The following example would add four values to the 'my-key' key:: client.zadd('my-key', 1.1, 'name1', 2.2, 'name2', name3=3.3, name4=4.4) """ pieces = [] if args: if len(args) % 2 != 0: raise ValueError("ZADD requires an equal number of " "values and scores") pieces.extend(args) for pair in iteritems(kwargs): pieces.append(pair[1]) pieces.append(pair[0]) return self.execute_command('ZADD', name, *pieces)
def __call__(self, html, fields, context): html.data("fields", len(fields)) for name, value in iteritems(fields): if is_string(value): html.append(Html("div", value, field=name)) else: html.append(Html("div", field=name, value=value))
def _auth_header(self, type, **options): """Convert the stored values into a WWW-Authenticate header.""" return '%s %s' % (type.title(), ', '.join( ('%s=%s' % (key, quote_header_value( value, allow_token=key not in _require_quoting)) for key, value in iteritems(options))))
def stream_mapping(value, request=None): result = {} for key, value in iteritems(value): if isinstance(value, AsyncString): value = value.render(request) result[key] = value return multi_async(result)
def _auth_header(self, type, **options): """Convert the stored values into a WWW-Authenticate header.""" return '%s %s' % (type.title(), ', '.join(( '%s=%s' % (key, quote_header_value( value, allow_token=key not in _require_quoting)) for key, value in iteritems(options) )))
def stream(self, whitespace=''): '''This function convert the :class:`css` element into a string.''' # First we execute mixins if self.rendered: raise RuntimeError('%s already rendered' % self) self.rendered = True children = self._children self._children = OrderedDict() for tag, clist in iteritems(children): for c in clist: c._parent = None s = c.set_parent(self) if s: # the child (mixin) has return a string, added it. yield (None, s) data = [] for k, v in self._attributes: v = as_value(v) if v is not None: data.append('%s %s: %s;' % (whitespace, k, v)) if data: yield (self.tag, '\n'.join(data)) # yield Mixins and children for child_list in itervalues(self._children): if isinstance(child_list, list): child = child_list[0] for c in child_list[1:]: child.extend(c) for s in child.stream(whitespace): yield s else: yield None, child_list
def __call__(self, html, fields, context): html.data('fields', len(fields)) for name, value in iteritems(fields): if is_string(value): html.append(Html('div', value, field=name)) else: html.append(Html('div', field=name, value=value))
def iterdata(stream, start=0): '''Iterate over a stream which is either a dictionary or a list. This iterator is over key-value pairs for a dictionary, and index-value pairs for a list.''' if isinstance(stream, Mapping): return iteritems(stream) else: return enumerate(stream, start)
def apply_content(self, elem, content): elem.data({'id': content.id, 'content_type': content.content_type}) for name, value in chain( (('title', content.title), ('keywords', content.keywords)), iteritems(content.data)): if not is_string(value): elem.data(name, value) elif value: elem.append(Html('div', value, field=name))
def _(): for data, tags in iteritems(od): if tags: yield ',\n'.join(('%s%s' % (whitespace, t) for t in tags) ) + ' {' yield data yield whitespace + '}\n' else: yield data
def apply_content(self, elem, content): elem.data({'id': content.id, 'content_type': content.content_type}) for name, value in chain((('title', content.title), ('keywords', content.keywords)), iteritems(content.data)): if not is_string(value): elem.data(name, value) elif value: elem.append(Html('div', value, field=name))
def _spawn_actor(cls, monitor, cfg=None, name=None, aid=None, **kw): # Internal function which spawns a new Actor and return its # ActorProxyMonitor. # *cls* is the Actor class # *monitor* can be either the ariber or a monitor kind = None if issubclass(cls, PoolMixin): kind = 'monitor' if monitor: params = monitor.actorparams() name = params.pop('name', name) aid = params.pop('aid', aid) cfg = params.pop('cfg', cfg) # get config if not available if cfg is None: if monitor: cfg = monitor.cfg.copy() else: cfg = Config() if not monitor: # monitor not available, this is the arbiter if kind != 'monitor': raise TypeError('class %s not a valid monitor' % cls) kind = 'arbiter' params = {} if not cfg.exc_id: if not aid: aid = gen_unique_id()[:8] cfg.set('exc_id', aid) # for key, value in iteritems(kw): if key in cfg.settings: cfg.set(key, value) else: params[key] = value # if monitor: if not kind: if not issubclass(cls, Actor): raise TypeError('Class %s not a valid actor.' % cls) kind = cfg.concurrency if not kind: raise TypeError('Cannot spawn class %s. not a valid concurrency.' % cls) actor_proxy = concurrency(kind, cls, monitor, cfg, name=name, aid=aid, **params) # Add to the list of managed actors if this is a remote actor if isinstance(actor_proxy, Actor): return actor_proxy else: actor_proxy.monitor = monitor monitor.managed_actors[actor_proxy.aid] = actor_proxy future = actor_proxy_future(actor_proxy) actor_proxy.start() return future
def stream_mapping(value, request=None): result = {} async = False for key, value in iteritems(value): if isinstance(value, AsyncString): value = value.content(request) value = maybe_async(value) async = async or isinstance(value, Deferred) result[key] = value return multi_async(result) if async else result
def manage_actors(self, stop=False): '''Remove :class:`Actor` which are not alive from the :class:`PoolMixin.managed_actors` and return the number of actors still alive. :parameter stop: if ``True`` stops all alive actor. ''' alive = 0 if self.managed_actors: for aid, actor in list(iteritems(self.managed_actors)): alive += self.manage_actor(actor, stop) return alive
def initials(cls): '''Iterator over initial field values. Check the :attr:`Field.initial` attribute for more information. This class method can be useful when using forms outside web applications. ''' for name, field in iteritems(cls.base_fields): initial = field.get_initial(cls) if initial is not None: yield name, initial
def poll(self, timeout=None): readable, writeable, errors = _select( self.read_fds, self.write_fds, self.error_fds, timeout) events = {} for fd in readable: events[fd] = events.get(fd, 0) | READ for fd in writeable: events[fd] = events.get(fd, 0) | WRITE for fd in errors: events[fd] = events.get(fd, 0) | ERROR return list(iteritems(events))
def _spawn_actor(cls, monitor, cfg=None, name=None, aid=None, **kw): # Internal function which spawns a new Actor and return its # ActorProxyMonitor. # *cls* is the Actor class # *monitor* can be either the ariber or a monitor kind = None if issubclass(cls, PoolMixin): kind = 'monitor' if cfg is None: if monitor: cfg = monitor.cfg.copy() else: cfg = pulsar.Config() if monitor: params = monitor.actorparams() name = params.pop('name', name) aid = params.pop('aid', aid) else: # monitor not available, this is the arbiter if kind != 'monitor': raise TypeError('class %s not a valid monitor' % cls) kind = 'arbiter' params = {} for key, value in iteritems(kw): if key in cfg.settings: cfg.set(key, value) else: params[key] = value # if monitor: if not kind: if not issubclass(cls, Actor): raise TypeError('Class %s not a valid actor.' % cls) kind = cfg.concurrency if not kind: raise TypeError('Cannot spawn class %s. not a valid concurrency.' % cls) actor_proxy = concurrency(kind, cls, monitor, cfg, name=name, aid=aid, **params) # Add to the list of managed actors if this is a remote actor if isinstance(actor_proxy, Actor): return actor_proxy else: actor_proxy.monitor = monitor monitor.managed_actors[actor_proxy.aid] = actor_proxy deferred = proxy.ActorProxyDeferred(actor_proxy) actor_proxy.start() return deferred
def update(self, *args, **kwargs): if len(args) == 1: iterable = args[0] if isinstance(iterable, Mapping): iterable = iteritems(iterable) super(Model, self).update(((mstr(k), v) for k, v in iterable)) self._modified = 1 elif args: raise TypeError('expected at most 1 arguments, got %s' % len(args)) if kwargs: super(Model, self).update(**kwargs) self._modified = 1
def data(self, *args): '''Add or retrieve data values for this :class:`Html`.''' data = self._data if not args: return data or {} result, adding = self._attrdata('data', *args) if adding: if data is None: self._extra['data'] = {} add = self._visitor.add_data for key, value in iteritems(result): add(self, key, value) return self else: return result
def commit(self): '''Commit the transaction. This method can be invoked once only otherwise an :class:`.InvalidOperation` occurs. :return: a :class:`~asyncio.Future` which results in this transaction ''' if self._executed is None: fut = multi_async((store.execute_transaction(commands) for store, commands in iteritems(self._commands))) self._executed = fut return self._executed else: raise InvalidOperation('Transaction already executed.')
def copy_many_times_events(self, other): '''Copy :ref:`many times events <many-times-event>` from ``other``. All many times events of ``other`` are copied to this handler provided the events handlers already exist. ''' if isinstance(other, EventHandler): events = self._events for name, event in iteritems(other._events): if isinstance(event, Event) and event._handlers: ev = events.get(name) # If the event is available add it if ev: for callback in event._handlers: ev.bind(callback)
def pubsub_publish(request, id, channel, message): monitor = request.actor if not channel or channel == '*': matched = ((c, reg[1]) for c, reg in iteritems(_get_pubsub_channels(monitor))) else: matched = _channel_groups(monitor, channel) clients = set() # loop over matched channels for channel, group in matched: for aid, pid in group: # if the id is matched we have a client if pid == id: clients.add(aid) monitor.send(aid, 'pubsub_broadcast', id, channel, message) return len(clients)
def _setup(self, cn=None, attr=None, css=None, data=None, type=None, **params): self._visitor = html_visitor(self._tag) self.addClass(cn) self.data(data) self.attr(attr) self.css(css) attributes = self.available_attributes if type and 'type' in attributes: self.attr('type', type) attributes = self.available_attributes for name, value in iteritems(params): if name in attributes: self.attr(name, value) else: self.data(name, value)
def commit(self): """Commit the transaction. This method can be invoked once only otherwise an :class:`.InvalidOperation` occurs. :return: a :class:`~asyncio.Future` which results in the list of transaction """ if self._executed is None: executed = dict( ((store, store.execute_transaction(commands)) for store, commands in iteritems(self._commands)) ) self._executed = multi_async(executed, loop=self._loop) return self._executed else: raise InvalidOperation("Transaction already executed.")
def css(self, tag, *components, **attributes): '''A child :class:`Css` elements.''' if tag: elems = [Css(t) for t in alltags(tag)] else: elems = [Css(tag)] for clone, css in enumerate(elems): for name, value in iteritems(attributes): css[name] = value css.set_parent(self) # Loop over components to add them to self for cl in components: if not isinstance(cl, list): cl = (cl,) for c in cl: css.add(c.clone() if clone else c) return elems[0] if len(elems) == 1 else elems
def _spawn_actor(kind, monitor, cfg=None, name=None, aid=None, **kw): # Internal function which spawns a new Actor and return its # ActorProxyMonitor. # *cls* is the Actor class # *monitor* can be either the arbiter or a monitor if monitor: params = monitor.actorparams() name = params.pop('name', name) aid = params.pop('aid', aid) cfg = params.pop('cfg', cfg) # get config if not available if cfg is None: if monitor: cfg = monitor.cfg.copy() else: cfg = Config() if not aid: aid = create_aid() if not monitor: # monitor not available, this is the arbiter assert kind == 'arbiter' name = kind params = {} if not cfg.exc_id: cfg.set('exc_id', aid) # for key, value in iteritems(kw): if key in cfg.settings: cfg.set(key, value) else: params[key] = value # if monitor: kind = kind or cfg.concurrency if not kind: raise TypeError('Cannot spawn') maker = concurrency_models.get(kind) if maker: c = maker() return c.make(kind, cfg, name, aid, monitor=monitor, **params) else: raise ValueError('Concurrency %s not supported in pulsar' % kind)
def _setup(self, cn=None, attr=None, css=None, data=None, type=None, content_type=None, **params): self.charset = params.get('charset') or 'utf-8' self._content_type = content_type or self._default_content_type self._visitor = html_visitor(self._tag) self.addClass(cn) self.data(data) self.attr(attr) self.css(css) attributes = self.available_attributes if type and 'type' in attributes: self.attr('type', type) attributes = self.available_attributes for name, value in iteritems(params): if name in attributes: self.attr(name, value) elif name != 'charset': self.data(name, value)
def attr(self, *args): '''Add the specific attribute to the attribute dictionary with key ``name`` and value ``value`` and return ``self``.''' attr = self._attr if not args: return attr or {} result, adding = self._attrdata('attr', *args) if adding: if attr is None: self._extra['attr'] = attr = {} available_attributes = self.available_attributes for name, value in iteritems(result): if value is not None: if name in available_attributes: attr[name] = value elif name is 'value': self.append(value) result = self return result
def _fill_initial(self): # Fill the initial dictionary with data from fields and from # the instance if available old_initial = self.initial self.initial = initial = {} instance = self.instance instance_id = instance.id if instance else None for name, field in iteritems(self.base_fields): if name in old_initial: value = old_initial[name] else: value = field.get_initial(self) # Instance with id can override the initial value if instance_id: try: # First try the field method value = field.value_from_instance(instance) except ValueError: value = self.value_from_instance(instance, name, value) if value is not None: initial[name] = value
def aggregate(self, kwargs): '''Aggregate lookup parameters.''' meta = self._meta store = self._store fields = meta.dfields field_lookups = {} for name, value in iteritems(kwargs): bits = name.split(JSPLITTER) field_name = bits.pop(0) if field_name not in fields: raise QueryError(('Could not filter on model "%s". Field ' '"%s" does not exist.' % (meta, field_name))) field = fields[field_name] store_name = field.store_name lookup = None if bits: bits = [n.lower() for n in bits] if bits[-1] == 'in': bits.pop() elif bits[-1] in range_lookups: lookup = bits.pop() remaining = JSPLITTER.join(bits) if lookup: # this is a range lookup store_name, nested = field.get_lookup( remaining, QueryError) lookups = get_lookups(store_name, field_lookups) lookups.append(lookup_value(lookup, (value, nested))) continue elif remaining: # Not a range lookup, must be a nested filter value = field.filter(self.session, remaining, value) lookups = get_lookups(store_name, field_lookups) if not isinstance(value, (list, tuple, set)): value = (value, ) for v in value: if isinstance(v, Query): v = lookup_value('query', v.compiled()) else: v = lookup_value('value', field.to_store(v, store)) lookups.append(v) return field_lookups
def aggregate(self, kwargs): '''Aggregate lookup parameters.''' meta = self._meta store = self._store fields = meta.dfields field_lookups = {} for name, value in iteritems(kwargs): bits = name.split(JSPLITTER) field_name = bits.pop(0) if field_name not in fields: raise QueryError(('Could not filter on model "%s". Field ' '"%s" does not exist.' % (meta, field_name))) field = fields[field_name] store_name = field.store_name lookup = None if bits: bits = [n.lower() for n in bits] if bits[-1] == 'in': bits.pop() elif bits[-1] in range_lookups: lookup = bits.pop() remaining = JSPLITTER.join(bits) if lookup: # this is a range lookup store_name, nested = field.get_lookup(remaining, QueryError) lookups = get_lookups(store_name, field_lookups) lookups.append(lookup_value(lookup, (value, nested))) continue elif remaining: # Not a range lookup, must be a nested filter value = field.filter(self.session, remaining, value) lookups = get_lookups(store_name, field_lookups) if not isinstance(value, (list, tuple, set)): value = (value,) for v in value: if isinstance(v, Query): v = lookup_value('query', v.compiled()) else: v = lookup_value('value', field.to_store(v, store)) lookups.append(v) return field_lookups
def _setup(self, cn=None, attr=None, css=None, data=None, type=None, **params): self.charset = params.get('charset') or 'utf-8' self._visitor = html_visitor(self._tag) self.addClass(cn) self.data(data) self.attr(attr) self.css(css) attributes = self.available_attributes if type and 'type' in attributes: self.attr('type', type) attributes = self.available_attributes for name, value in iteritems(params): if name in attributes: self.attr(name, value) elif name != 'charset': self.data(name, value)
def _clean_fields(self, rawdata): loop = self._loop files = self._files self._data = data = {} self._fields = fields = [] self._fields_dict = dfields = {} initial = self.initial is_bound = self.is_bound # Loop over form fields for name, field in iteritems(self.base_fields): bfield = BoundField(self, field, name, self.prefix) key = bfield.html_name fields.append(bfield) dfields[name] = bfield field_value = None if is_bound: field_value = field.value_from_datadict(rawdata, files, key) yield async(bfield.clean(field_value), loop) if field_value != initial.get(name): self.changed = True data[name] = field_value elif name in initial: data[name] = field_value = initial[name] bfield.value = field_value
def __init__(self, data=None, loop=None, type=None, raise_on_error=True): super(MultiFuture, self).__init__(loop=loop) self._futures = {} self._failures = [] self._raise_on_error = raise_on_error if data is not None: type = type or data.__class__ if issubclass(type, Mapping): data = iteritems(data) else: type = list data = enumerate(data) else: type = list data = () self._stream = type() for key, value in data: value = self._get_set_item(key, maybe_async(value, loop)) if isfuture(value): self._futures[key] = value value.add_done_callback(partial(self._future_done, key)) elif self.done(): break self._check()
def _update_modify(self, iterable): if isinstance(iterable, Mapping): iterable = iteritems(iterable) for field, value in iterable: yield self._get_field_value(field, value)
def _commit(self): # Run this method in the event loop so that it is thread safe executed = dict(((store, store.execute_transaction(commands)) for store, commands in iteritems(self._commands))) return multi_async(executed, loop=self._loop)
def filter_types(self, type): """Return a generator of all tasks of a specific type.""" return ((job_name, job) for job_name, job in iteritems(self) if job.type == type)
def _to_store(self, store): for key, value in iteritems(self): if key in self._meta.dfields: value = self._meta.dfields[key].to_store(value, store) if value is not None: yield key, value