text
stringlengths
0
828
1279,"def attr(cls, iterable, attr_name):
""""""
Applies #getattr() on all elements of *iterable*.
""""""
return cls(getattr(x, attr_name) for x in iterable)"
1280,"def of_type(cls, iterable, types):
""""""
Filters using #isinstance().
""""""
return cls(x for x in iterable if isinstance(x, types))"
1281,"def partition(cls, iterable, pred):
""""""
Use a predicate to partition items into false and true entries.
""""""
t1, t2 = itertools.tee(iterable)
return cls(itertools.filterfalse(pred, t1), filter(pred, t2))"
1282,"def count(cls, iterable):
""""""
Returns the number of items in an iterable.
""""""
iterable = iter(iterable)
count = 0
while True:
try:
next(iterable)
except StopIteration:
break
count += 1
return count"
1283,"def column_names(self, table):
""""""An iterable of column names, for a particular table or
view.""""""
table_info = self.execute(
u'PRAGMA table_info(%s)' % quote(table))
return (column['name'] for column in table_info)"
1284,"def execute(self, sql, *args, **kwargs):
'''
Run raw SQL on the database, and receive relaxing output.
This is sort of the foundational method that most of the
others build on.
'''
try:
self.cursor.execute(sql, *args)
except self.sqlite3.InterfaceError, msg:
raise self.sqlite3.InterfaceError(unicode(msg) + '\nTry converting types or pickling.')
rows = self.cursor.fetchall()
self.__commit_if_necessary(kwargs)
if None == self.cursor.description:
return None
else:
colnames = [d[0].decode('utf-8') for d in self.cursor.description]
rawdata = [OrderedDict(zip(colnames,row)) for row in rows]
return rawdata"
1285,"def create_index(self, columns, table_name, if_not_exists = True, unique = False, **kwargs):
'Create a unique index on the column(s) passed.'
index_name = simplify(table_name) + u'_' + u'_'.join(map(simplify, columns))
if unique:
sql = u'CREATE UNIQUE INDEX %s ON %s (%s)'
else:
sql = u'CREATE INDEX %s ON %s (%s)'
first_param = u'IF NOT EXISTS ' + index_name if if_not_exists else index_name
params = (first_param, quote(table_name), ','.join(map(quote, columns)))
self.execute(sql % params, **kwargs)"
1286,"def create_table(self, data, table_name, error_if_exists = False, **kwargs):
'Create a table based on the data, but don\'t insert anything.'
converted_data = convert(data)
if len(converted_data) == 0 or converted_data[0] == []:
raise ValueError(u'You passed no sample values, or all the values you passed were null.')
else:
startdata = OrderedDict(converted_data[0])
# Select a non-null item
for k, v in startdata.items():
if v != None:
break
else:
v = None
if_not_exists = u'' if error_if_exists else u'IF NOT EXISTS'
# Do nothing if all items are null.
if v != None:
try:
# This is vulnerable to injection.
sql = u'''
CREATE TABLE %s %s (
%s %s
);''' % (if_not_exists, quote(table_name), quote(k), get_column_type(startdata[k]))
self.execute(sql, commit = False)
except: