数値をカンマ付き数字に変換(commify)
http://pleac.sourceforge.net/pleac_python/numbers.html
から。
def commify(amount): amount = str(amount) firstcomma = len(amount)%3 or 3 # set to 3 if would make a leading comma first, rest = amount[:firstcomma], amount[firstcomma:] segments = [first] + [rest[i:i+3] for i in range(0, len(rest), 3)] return ",".join(segments)
検索したところ、正規表現版が多い。
ジェネレータ初めて使ってみた
SQLを実行して、その行を1つずつyieldする。
こんな使い方であっているかな。
def search_tags(self, tags): query = ' and '.join(['tag = ?' for w in tags]) self.cur.execute('SELECT id, fullpath, filetitle, ext, size, sha512, mtime FROM files WHERE id IN (SELECT file_id FROM file_tag WHERE tag_id IN (SELECT id FROM tags WHERE %s))' % query, tags) for row in self.cur: yield {'id': row[0], 'fullpath': row[1], 'filetitle': row[2], 'ext': row[3], 'size': row[4], 'sha512': row[5], 'mtime': row[6] }