Skip to content

Total Time

Francisco Maria Calisto edited this page Jul 24, 2018 · 4 revisions

First of all, welcome to this Wiki. This sample consists in getting a dataset folder, counting the total number of time summing the time of all files. The first task you need to do is to follow the next steps.

Basics

1.1. Install Python;

1.2. Create a root folder for your Git repositories:

mkdir Git

1.3. Go inside the Git root folder:

cd Git

1.4. Clone both dataset-samples and dataset-counters repositories with the same root folder:

git clone https://github.com/MIMBCD-UI/dataset-samples
git clone https://github.com/MIMBCD-UI/dataset-counters

1.5. Get inside the dataset-counters repository and at the src/ folder:

cd dataset-counters
cd src

1.6. Run the source code sample:

python3 totalTime.py

Coding

Now let's get deeper. The following tasks will promote your understanding to develop coding skills. Just follow the steps.

2.1. Create a new myTotalTime.py file:

touch myTotalTime.py

2.2. Inside the file, let's put some imports:

import os, json
from pprint import pprint

2.3. Now try to read a simple JSON file:

import os, json
from pprint import pprint

path_to_json = '../../dataset-samples/counters/1948.json'

with open(path_to_json) as f:
    data = json.load(f)

pprint(data)

2.4. The last tasks just consume one, and just one, JSON file while we want all of it, so let's develop that:

import os, json
from pprint import pprint

path_to_json = '../../dataset-samples/counters/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

pprint(json_files)

2.5. Next, let's print each time variable:

import os, json
from pprint import pprint

timeCounter = 0;
i = 0;
filesNum = 12;
totalTime = 0;

path_to_json = '../../dataset-samples/counters/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

while i < filesNum:
  with open(path_to_json + json_files[i]) as f:
    data = json.load(f)
  i = i + 1
  pprint(data["rawData"]["time"])

2.6. Finally, we need to sum recursively the total time number:

import os, json
from pprint import pprint

timeCounter = 0;
i = 0;
filesNum = 12;
totalTime = 0;

path_to_json = '../../dataset-samples/counters/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

while i < filesNum:
  with open(path_to_json + json_files[i]) as f:
    data = json.load(f)
  i = i + 1
  totalTime = totalTime + data["rawData"]["time"]

print(totalTime)
Clone this wiki locally