Esempio n. 1
0
 def _make_udt_tuple_type(cls, name, field_names):
     # fallback to positional named, then unnamed tuples
     # for CQL identifiers that aren't valid in Python,
     try:
         t = namedtuple(name, field_names)
     except ValueError:
         try:
             t = namedtuple(name, util._positional_rename_invalid_identifiers(field_names))
             log.warn("could not create a namedtuple for '%s' because one or more field names are not valid Python identifiers (%s); " \
                      "returning positionally-named fields" % (name, field_names))
         except ValueError:
             t = None
             log.warn("could not create a namedtuple for '%s' because the name is not a valid Python identifier; " \
                      "will return tuples in its place" % (name,))
     return t
Esempio n. 2
0
 def _make_udt_tuple_type(cls, name, field_names):
     # fallback to positional named, then unnamed tuples
     # for CQL identifiers that aren't valid in Python,
     try:
         t = namedtuple(name, field_names)
     except ValueError:
         try:
             t = namedtuple(name, util._positional_rename_invalid_identifiers(field_names))
             log.warn("could not create a namedtuple for '%s' because one or more field names are not valid Python identifiers (%s); " \
                      "returning positionally-named fields" % (name, field_names))
         except ValueError:
             t = None
             log.warn("could not create a namedtuple for '%s' because the name is not a valid Python identifier; " \
                      "will return tuples in its place" % (name,))
     return t
Esempio n. 3
0
def named_tuple_factory(colnames, rows):
    """
    Returns each row as a `namedtuple <https://docs.python.org/2/library/collections.html#collections.namedtuple>`_.
    This is the default row factory.

    Example::

        >>> from cassandra.query import named_tuple_factory
        >>> session = cluster.connect('mykeyspace')
        >>> session.row_factory = named_tuple_factory
        >>> rows = session.execute("SELECT name, age FROM users LIMIT 1")
        >>> user = rows[0]

        >>> # you can access field by their name:
        >>> print "name: %s, age: %d" % (user.name, user.age)
        name: Bob, age: 42

        >>> # or you can access fields by their position (like a tuple)
        >>> name, age = user
        >>> print "name: %s, age: %d" % (name, age)
        name: Bob, age: 42
        >>> name = user[0]
        >>> age = user[1]
        >>> print "name: %s, age: %d" % (name, age)
        name: Bob, age: 42

    .. versionchanged:: 2.0.0
        moved from ``cassandra.decoder`` to ``cassandra.query``
    """
    clean_column_names = map(_clean_column_name, colnames)
    try:
        Row = namedtuple('Row', clean_column_names)
    except Exception:
        log.warning(
            "Failed creating named tuple for results with column names %s (cleaned: %s) "
            "(see Python 'namedtuple' documentation for details on name rules). "
            "Results will be returned with positional names. "
            "Avoid this by choosing different names, using SELECT \"<col name>\" AS aliases, "
            "or specifying a different row_factory on your Session" %
            (colnames, clean_column_names))
        Row = namedtuple(
            'Row', _positional_rename_invalid_identifiers(clean_column_names))

    return [Row(*row) for row in rows]
Esempio n. 4
0
def named_tuple_factory(colnames, rows):
    """
    Returns each row as a `namedtuple <https://docs.python.org/2/library/collections.html#collections.namedtuple>`_.
    This is the default row factory.

    Example::

        >>> from cassandra.query import named_tuple_factory
        >>> session = cluster.connect('mykeyspace')
        >>> session.row_factory = named_tuple_factory
        >>> rows = session.execute("SELECT name, age FROM users LIMIT 1")
        >>> user = rows[0]

        >>> # you can access field by their name:
        >>> print "name: %s, age: %d" % (user.name, user.age)
        name: Bob, age: 42

        >>> # or you can access fields by their position (like a tuple)
        >>> name, age = user
        >>> print "name: %s, age: %d" % (name, age)
        name: Bob, age: 42
        >>> name = user[0]
        >>> age = user[1]
        >>> print "name: %s, age: %d" % (name, age)
        name: Bob, age: 42

    .. versionchanged:: 2.0.0
        moved from ``cassandra.decoder`` to ``cassandra.query``
    """
    clean_column_names = map(_clean_column_name, colnames)
    try:
        Row = namedtuple("Row", clean_column_names)
    except Exception:
        log.warning(
            "Failed creating named tuple for results with column names %s (cleaned: %s) "
            "(see Python 'namedtuple' documentation for details on name rules). "
            "Results will be returned with positional names. "
            'Avoid this by choosing different names, using SELECT "<col name>" AS aliases, '
            "or specifying a different row_factory on your Session" % (colnames, clean_column_names)
        )
        Row = namedtuple("Row", _positional_rename_invalid_identifiers(clean_column_names))

    return [Row(*row) for row in rows]