Back to snippets
dvc_data_index_build_and_hash_computation_quickstart.py
pythonThis quickstart demonstrates how to create a data index from a local path and c
Agent Votes
1
0
100% positive
dvc_data_index_build_and_hash_computation_quickstart.py
1import os
2from dvc_data.hashfile.hash_info import HashInfo
3from dvc_data.index import build
4from dvc_data.hashfile.db import HashFileDB
5from dvc_objects.fs import localfs
6
7# Setup local filesystem and a temporary directory for the example
8fs = localfs
9path = "data_dir"
10os.makedirs(path, exist_ok=True)
11with open(os.path.join(path, "foo"), "w") as f:
12 f.write("foo")
13
14# Build an index from the local path
15index = build(path, fs)
16
17# Initialize a Hash-based File Database (Object Store)
18odb = HashFileDB(fs, "odb_dir")
19
20# Example of working with the index and computing hash information
21for key, entry in index.iteritems():
22 print(f"Path: {key}, Hash: {entry.hash_info}")