Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ Create a star pattern pyramid
calculate compound interest

## Script 14 - Mouse mover
Moves your mouse every 15 seconds
Moves your mouse every 15 seconds
## Script 15 - JSON to YAML converter
Converts JSON file to YAML files. A sample JSON is included for testing.
26 changes: 26 additions & 0 deletions json_2_yaml/json_to_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# script to convert json to yaml
import json
import os
import sys
import yaml
if len(sys.argv) > 1:
if os.path.exists(sys.argv[1]):
sourcefile = open(sys.argv[1], "r")
contentsource = json.load(sourcefile)
sourcefile.close()
else:
print("ERROR: " + sys.argv[1] + " not found")
exit(1)
else:
print("Usage: json2yaml.py <sourcefile.json> [target_file.yaml]")
outs = yaml.dump(contentsource)
if len(sys.argv) < 3:
print(outs)
elif os.path.exists(sys.argv[2]):
print("ERROR: " + sys.argv[2] + " already exists")
exit(1)
else:
target_file = open(sys.argv[2], "w")
target_file.write(outs)
target_file.close()
##end
28 changes: 28 additions & 0 deletions json_2_yaml/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"quiz": {
"sport": {
"q1": {
"question": "Which one is correct team name in NBA?",
"options": [
"New York Bulls",
"Los Angeles Kings",
"Golden State Warriros",
"Huston Rocket"
],
"answer": "Huston Rocket"
}
},
"maths": {
"q1": {
"question": "5 + 7 = ?",
"options": ["10", "11", "12", "13"],
"answer": "12"
},
"q2": {
"question": "12 - 8 = ?",
"options": ["1", "2", "3", "4"],
"answer": "4"
}
}
}
}
27 changes: 27 additions & 0 deletions json_2_yaml/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
quiz:
maths:
q1:
answer: '12'
options:
- '10'
- '11'
- '12'
- '13'
question: 5 + 7 = ?
q2:
answer: '4'
options:
- '1'
- '2'
- '3'
- '4'
question: 12 - 8 = ?
sport:
q1:
answer: Huston Rocket
options:
- New York Bulls
- Los Angeles Kings
- Golden State Warriros
- Huston Rocket
question: Which one is correct team name in NBA?