Return a function which accepts a row and returns the first non-missing
    value from the specified fields. Intended for use with
    :func:`petl.transform.basics.addfield`.
348174
def coalesce(*fields, **kwargs):
    
    missing = kwargs.get('missing', None)
    default = kwargs.get('default', None)

    def _coalesce(row):
        for f in fields:
            v = row[f]
            if v is not missing:
                return v
        return default

    return _coalesce
850635
def _select_deep(v, field, depth, record):
    
    if hasattr(field.value, "__call__"):
        try:
            record[field.name] = field.value(wrap(v))
        except Exception as e:
            record[field.name] = None
        return 0, None

    for i, f in enumerate(field.value[depth : len(field.value) - 1 :]):
        v = v.get(f)
        if v is None:
            return 0, None
        if is_list(v):
            return depth + i + 1, v

    f = field.value.last()
    try:
        if not f:  # NO NAME FIELD INDICATES SELECT VALUE
            record[field.name] = v
        else:
            record[field.name] = v.get(f)
    except Exception as e:
        Log.error(
            "{{value}} does not have {{field}} property", value=v, field=f, cause=e
        )
    return 0, None
99941
def field_default(colx, table_name, tables_dict):
  ""
  if colx.coltp.type.lower() == 'serial':
    x = sqparse2.parse('select coalesce(max(%s),-1)+1 from %s' % (colx.name, table_name))
    return sqex.run_select(x, tables_dict, Table)[0]
  elif colx.not_null: raise NotImplementedError('todo: not_null error')
  else: return toliteral(colx.default)
1268569
def _wrap_row(cls, row):
        """"""
        doc = row.get('doc')
        if doc is not None:
            return cls.wrap(doc)
        data = row['value']
        data['_id'] = row['id']
        return cls.wrap(data)
1063464
def first(self, **kwargs):
        
        result = yield self.get(**kwargs)

        if not result['rows']:
            raise exceptions.NotFound()

        raise Return(result['rows'][0])
1132714
def _make_record(self,row):
        """"""
        res = dict(zip(['__id__']+[f for f in self.fields],row))
        for field_name in self.conv_func:
            res[field_name] = self.conv_func[field_name](res[field_name])
        return res
1006106
def NewRow(self, value=""):
        
        newrow = self.row_class()
        newrow.row = self.size + 1
        newrow.table = self
        headers = self._Header()
        for header in headers:
            newrow[header] = value
        return newrow
456611
def groupby_field(records, field_name, skip_none=True):
    
    return apply_groupby(
        records,
        lambda obj: getattr(obj, field_name),
        skip_none=skip_none)
311777
def first(self, rows: List[Row]) -> List[Row]:
        
        if not rows:
            logger.warning("Trying to get first row from an empty list")
            return []
        return [rows[0]]
443502
def from_data(data):
    
    if len(data) == 0:  # pragma: no cover
        return None
    else:
        ptable = PrettyTable()
        ptable.field_names = data[0].keys()
        for row in data:
            ptable.add_row(row)
        return ptable
681965
def __update(self, row):
        
        expr = self.__table.update().values(row)
        for key in self.__update_keys:
            expr = expr.where(getattr(self.__table.c, key) == row[key])
        if self.__autoincrement:
            expr = expr.returning(getattr(self.__table.c, self.__autoincrement))
        res = expr.execute()
        if res.rowcount > 0:
            if self.__autoincrement:
                first = next(iter(res))
                last_row_id = first[0]
                return last_row_id
            return 0
        return None
72654
def SimpleRowColumn(field, *args, **kwargs):
    
    if isinstance(field, basestring):
        field = Field(field, *args, **kwargs)
    return Row(
        Column(field),
    )
678065
def first(self, limit=1, columns=None):
        
        if not columns:
            columns = ["*"]

        return self.take(limit).get(columns).first()
339967
def add_field(self, field):
        
        field = FieldFactory(
            field,
        )
        field.set_table(self)

        # make sure field is not already added
        field_name = field.get_name()
        for existing_field in self.fields:
            if existing_field.get_name() == field_name:
                return None

        self.before_add_field(field)
        field.before_add()

        if field.ignore is False:
            self.fields.append(field)

        return field
280299
def transform(fields, function, *tables):
    ""

    new_table = Table(fields=fields)

    for table in tables:
        for row in filter(bool, map(lambda row: function(row, table), table)):
            new_table.append(row)

    return new_table
187671
def row(self, fields):
        """"""

        d = self.dict

        row = [None] * len(fields)

        for i, f in enumerate(fields):
            if f in d:
                row[i] = d[f]

        return row
876265
def wherenotin(self, fieldname, value):
        
        return self.wherein(fieldname, value, negate=True)