コード例 #1
0
ファイル: redisb.py プロジェクト: abulte/python-stdnet
 def load_related(self, meta, fname, data, fields, encoding):
     '''Parse data for related objects.'''
     field = meta.dfields[fname]
     if field in meta.multifields:
         fmeta = field.structure_class()._meta
         if fmeta.name in ('hashtable', 'zset'):
             return ((native_str(id, encoding),
                      pairs_to_dict(fdata, encoding)) for \
                     id, fdata in data)
         else:
             return ((native_str(id, encoding), fdata) for id, fdata in data)
     else:
         # this is data for stdmodel instances
         return self.build(data, meta, fields, fields, encoding)
コード例 #2
0
 def load_related(self, meta, fname, data, fields, encoding):
     '''Parse data for related objects.'''
     field = meta.dfields[fname]
     if field in meta.multifields:
         fmeta = field.structure_class()._meta
         if fmeta.name in ('hashtable', 'zset'):
             return ((native_str(id,
                                 encoding), pairs_to_dict(fdata, encoding))
                     for id, fdata in data)
         else:
             return ((native_str(id, encoding), fdata)
                     for id, fdata in data)
     else:
         # this is data for stdmodel instances
         return self.build(data, meta, fields, fields, encoding)
コード例 #3
0
ファイル: __init__.py プロジェクト: AlecTaylor/python-stdnet
 def load_query(self, response, backend, meta, get=None, fields=None,
                fields_attributes=None, redis_client=None, **options):
     if get:
         tpy = meta.dfields.get(get).to_python
         return [tpy(v, backend) for v in response]
     else:
         data, related = response
         encoding = redis_client.encoding
         data = self.build(data, meta, fields, fields_attributes, encoding)
         related_fields = {}
         if related:
             for fname, rdata, fields in related:
                 fname = native_str(fname, encoding)
                 fields = tuple(native_str(f, encoding) for f in fields)
                 related_fields[fname] =\
                     self.load_related(meta, fname, rdata, fields, encoding)
         return backend.objects_from_db(meta, data, related_fields)
コード例 #4
0
def model_iterator(application, include_related=True, exclude=None):
    '''A generator of :class:`StdModel` classes found in *application*.

:parameter application: A python dotted path or an iterable over python
    dotted-paths where models are defined.

Only models defined in these paths are considered.

For example::

    from stdnet.odm import model_iterator

    APPS = ('stdnet.contrib.searchengine',
            'stdnet.contrib.timeseries')

    for model in model_iterator(APPS):
        ...

'''
    if exclude is None:
        exclude = set()
    application = native_str(application)
    if ismodule(application) or isinstance(application, str):
        if ismodule(application):
            mod, application = application, application.__name__
        else:
            try:
                mod = import_module(application)
            except ImportError:
                # the module is not there
                mod = None
        if mod:
            label = application.split('.')[-1]
            try:
                mod_models = import_module('.models', application)
            except ImportError:
                mod_models = mod
            label = getattr(mod_models, 'app_label', label)
            models = set()
            for name in dir(mod_models):
                value = getattr(mod_models, name)
                meta = getattr(value, '_meta', None)
                if isinstance(value, ModelType) and meta:
                    for model in models_from_model(
                            value, include_related=include_related,
                            exclude=exclude):
                        if (model._meta.app_label == label
                                and model not in models):
                            models.add(model)
                            yield model
    else:
        for app in application:
            for m in model_iterator(app):
                yield m
コード例 #5
0
 def load_query(self,
                response,
                backend,
                meta,
                get=None,
                fields=None,
                fields_attributes=None,
                redis_client=None,
                **options):
     if get:
         tpy = meta.dfields.get(get).to_python
         return [tpy(v, backend) for v in response]
     else:
         data, related = response
         encoding = redis_client.encoding
         data = self.build(data, meta, fields, fields_attributes, encoding)
         related_fields = {}
         if related:
             for fname, rdata, fields in related:
                 fname = native_str(fname, encoding)
                 fields = tuple(native_str(f, encoding) for f in fields)
                 related_fields[fname] =\
                     self.load_related(meta, fname, rdata, fields, encoding)
         return backend.objects_from_db(meta, data, related_fields)
コード例 #6
0
ファイル: client.py プロジェクト: abulte/python-stdnet
    def brpop(self, keys, timeout=0, **options):
        """
        RPOP a value off of the first non-empty list
        named in the ``keys`` list.

        If none of the lists in ``keys`` has a value to LPOP, then block
        for ``timeout`` seconds, or until a value gets pushed on to one
        of the lists.

        If timeout is 0, then block indefinitely.
        """
        keys = native_str(keys)
        if isinstance(keys, str):
            keys = [keys]
        else:
            keys = list(keys)
        keys.append(timeout)
        return self.execute_command('BRPOP', *keys, **options)