2023-05-17 16:23:25 +02:00
---
description: >-
How to construct the beginnings of an awesome algorithm for C2D compute jobs
on datasets
---
# Make a Boss C2D Algorithm
2023-05-17 16:37:46 +02:00
{% embed url="https://media3.giphy.com/media/cub3pntkz8muQ/giphy.gif?cid=ecf05e47o9fdrqco4jqpeyh7899whqgw5tnd43elr023rykr& ep=v1_gifs_search& rid=giphy.gif" %}
2023-05-17 16:38:59 +02:00
The beginning of any great algorithm for Compute-to-Data first starts by referencing the dataset file correctly on the Docker container. Here is the code in both Python and Javascript for how to correctly reference the dataset file that's local on the Docker container for your compute job:
2023-05-17 16:30:38 +02:00
### Python
```
import csv
import json
import os
def get_input(local=False):
dids = os.getenv("DIDS", None)
if not dids:
print("No DIDs found in the environment. Aborting.")
return
dids = json.loads(dids)
for did in dids:
filename = f"data/inputs/{did}/0" # 0 for metadata service
print(f"Reading asset file {filename}.")
return filename
# Get the input filename using the get_input function
input_filename = get_input()
if not input_filename:
# No input filename returned
exit()
2023-05-17 16:51:18 +02:00
# Open the file & run your code
2023-05-17 16:30:38 +02:00
with open(input_filename, 'r') as file:
# Read the CSV file
csv_reader = csv.DictReader(file)
2023-05-17 16:37:46 +02:00
2023-05-17 16:50:45 +02:00
< YOUR CODE GOES HERE >
2023-05-17 16:30:38 +02:00
```
### Javascript
2023-05-17 16:50:18 +02:00
```
const fs = require("fs");
var input_folder = "/data/inputs";
var output_folder = "/data/outputs";
async function processfolder(Path) {
var files = fs.readdirSync(Path);
for (var i =0; i < files.length ; i + + ) {
var file = files[i];
var fullpath = Path + "/" + file;
if (fs.statSync(fullpath).isDirectory()) {
await processfolder(fullpath);
} else {
< YOUR CODE GOES HERE >
}
}
}
< YOUR CODE GOES HERE >
2023-05-17 16:51:18 +02:00
// Open the file & run your code
2023-05-17 16:50:18 +02:00
processfolder(input_folder);
```