-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtracker.py
More file actions
60 lines (50 loc) · 1.77 KB
/
tracker.py
File metadata and controls
60 lines (50 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import os
import sqlite3
from typing import List
#
from tqdm import tqdm
class Tracker:
def __init__(
self,
output_dir: str = ".",
batch_size: int = 1,
):
self.batch_size = batch_size
self.conn = sqlite3.connect(os.path.join(output_dir, "progress.db"))
self.__create_db()
def add_samples(self, sample_filepaths: List[str]):
with self.conn:
for sample in tqdm(sample_filepaths, total=len(sample_filepaths), desc="Indexing samples"):
self.conn.execute(
"INSERT OR IGNORE INTO samples (path) VALUES (?)",
(sample,)
)
def get_batch(self) -> List[str]:
with self.conn:
cursor = self.conn.execute(
"SELECT path FROM samples WHERE status='pending' LIMIT ?",
(self.batch_size,)
)
return [row[0] for row in cursor.fetchall()]
def mark_done(self, path: str):
with self.conn:
self.conn.execute(
"UPDATE samples SET status='done' WHERE path=?",
(path,)
)
def pending_count(self) -> int:
with self.conn:
cursor = self.conn.execute("SELECT COUNT(*) FROM samples WHERE status='pending'")
return cursor.fetchone()[0]
def close(self):
self.conn.close()
def __create_db(self):
with self.conn:
cursor = self.conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS samples (
id INTEGER PRIMARY KEY AUTOINCREMENT,
path TEXT UNIQUE,
status TEXT CHECK(status IN ('pending', 'done', 'error')) DEFAULT 'pending'
)
""")