Commit 043c30b5 by Shirley He

polish up file

parent a0a511b3
#!/usr/bin/env python #!/usr/bin/env python
# other comment about how this file works, plus probably an example of implementation #
# Create a file of course permutations based on permutations.json file configuration. A max
# of 3 fields can be chosen to build permutations on, and only the fields under
# "permutation_data" are eligible. No field input pulls from "default_data"
#
# ./generate_permutations.py --seed_data_file permutations.json --fields field1 field2 field3
import json import json
import argparse import argparse
...@@ -17,13 +22,14 @@ def parse_field_arguments(): ...@@ -17,13 +22,14 @@ def parse_field_arguments():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('--fields', nargs="*", action="append", default=None, parser.add_argument('--fields', nargs="*", action="append", default=None,
help="Specify which fields to generate permutations on") help="Specify which fields to generate permutations on")
parser.add_argument('filename') parser.add_argument('--seed_data_file', help="Input a permutation configuration file")
num_args = parser.parse_args() num_args = parser.parse_args()
file = open(num_args.filename) file = open(num_args.seed_data_file)
file_data = json.load(file) file_data = json.load(file)
default_data = file_data["default_data"] default_data = file_data["default_data"]
permutation_data = file_data["permutation_data"] permutation_data = file_data["permutation_data"]
...@@ -31,7 +37,7 @@ def parse_field_arguments(): ...@@ -31,7 +37,7 @@ def parse_field_arguments():
fields = {} fields = {}
# if no field arguments are given, just print out default data # if no field arguments are given, just return default data
if not num_args.fields: if not num_args.fields:
default_permutation = file_data["default_data"] default_permutation = file_data["default_data"]
fields = default_permutation fields = default_permutation
...@@ -39,16 +45,16 @@ def parse_field_arguments(): ...@@ -39,16 +45,16 @@ def parse_field_arguments():
field_length = len(num_args.fields[0]) field_length = len(num_args.fields[0])
if (field_length > 3): if (field_length > 3):
raise argparse.ArgumentTypeError("--fields can only take a max of 3 values") raise argparse.ArgumentTypeError("Only a max of 3 fields allowed")
# add each command line field to fields dict
for permutation_choices in num_args.fields: for permutation_choices in num_args.fields:
for i in range(0, field_length): for i in range(0, field_length):
fields[permutation_choices[i]] = permutation_data[permutation_choices[i]] fields[permutation_choices[i]] = permutation_data[permutation_choices[i]]
# the difference btwn all possible fields and the permutation ones # the difference btwn all possible fields and the chosen permutation ones
default_fields = list(set(default_data_keys) - set(num_args.fields[0])) default_fields = list(set(default_data_keys) - set(num_args.fields[0]))
# add non permutation fields # add non permutation fields to dict
for j in range(0, len(default_fields)): for j in range(0, len(default_fields)):
fields[default_fields[j]] = default_data[default_fields[j]] fields[default_fields[j]] = default_data[default_fields[j]]
...@@ -58,12 +64,11 @@ def parse_field_arguments(): ...@@ -58,12 +64,11 @@ def parse_field_arguments():
def generate_permutations(fields, index, results, current, fields_dict): def generate_permutations(fields, index, results, current, fields_dict):
all_permutations_keys = fields.keys() all_permutations_keys = fields.keys()
permutation_option = all_permutations_keys[index] permutation_option = all_permutations_keys[index]
permutations_values = fields[permutation_option] permutations_values = fields[permutation_option]
for i in range(len(permutations_values)): for i in range(len(permutations_values)):
# add other required default fields to dict # add other required default fields to dict
current["number"] = "123" # will be generated automatically by course creation script current["number"] = None # will be generated automatically by course creation script
current["organization"] = "RITX" current["organization"] = "RITX"
current["run"] = "3T2017" current["run"] = "3T2017"
current["user"] = "edx@example.com" current["user"] = "edx@example.com"
...@@ -73,10 +78,10 @@ def generate_permutations(fields, index, results, current, fields_dict): ...@@ -73,10 +78,10 @@ def generate_permutations(fields, index, results, current, fields_dict):
enrollment_dict = {} enrollment_dict = {}
enrollment_dict["credit"] = False enrollment_dict["credit"] = False
enrollment_dict["credit_provider"] = "test-credit-provider" enrollment_dict["credit_provider"] = "test-credit-provider"
# enrollment_dict["audit"] = True
# add permutation fields to dict # add permutation fields to dict
fields_dict[permutation_option] = permutations_values[i] fields_dict[permutation_option] = permutations_values[i]
# generate dates
now = datetime.datetime.now(pytz.UTC) now = datetime.datetime.now(pytz.UTC)
if permutations_values[i] == "future": if permutations_values[i] == "future":
future = str(now + datetime.timedelta(days=365)) future = str(now + datetime.timedelta(days=365))
...@@ -86,31 +91,22 @@ def generate_permutations(fields, index, results, current, fields_dict): ...@@ -86,31 +91,22 @@ def generate_permutations(fields, index, results, current, fields_dict):
fields_dict[permutation_option] = past fields_dict[permutation_option] = past
if permutations_values[i] == None: if permutations_values[i] == None:
fields_dict[permutation_option] = None fields_dict[permutation_option] = None
# add audit and verify fields to dict
if all_permutations_keys[i] == "audit" and permutations_values[i] == True: if all_permutations_keys[i] == "audit" and permutations_values[i] == True:
enrollment_dict["audit"] = permutations_values[i] enrollment_dict["audit"] = permutations_values[i]
if all_permutations_keys[i] == "verify" and permutations_values[i] == True: if all_permutations_keys[i] == "verify" and permutations_values[i] == True:
enrollment_dict["verify"] = True enrollment_dict["verify"] = True
if index + 1 < len(all_permutations_keys): if index + 1 < len(all_permutations_keys):
generate_permutations(fields, index + 1, results, current, fields_dict) generate_permutations(fields, index + 1, results, current, fields_dict)
current["enrollment"] = enrollment_dict current["enrollment"] = enrollment_dict
current["fields"] = fields_dict.copy() current["fields"] = fields_dict.copy()
results.append(current.copy()) results.append(current.copy())
# results["courses"] = current.copy()
wrapper_courses_dict = {}
wrapper_courses_dict = {} # needed to match course input file creation
wrapper_courses_dict["courses"] = results wrapper_courses_dict["courses"] = results
with open("test_courses.json", "w") as outfile: with open("test_courses.json", "w") as outfile:
json.dump(wrapper_courses_dict, outfile) json.dump(wrapper_courses_dict, outfile)
......
...@@ -26,7 +26,6 @@ ...@@ -26,7 +26,6 @@
false, false,
null null
], ],
"mobile_available": [ "mobile_available": [
true, true,
false, false,
...@@ -43,8 +42,12 @@ ...@@ -43,8 +42,12 @@
"display_name": [ "display_name": [
"International Project Management" "International Project Management"
], ],
"audit": [true], "audit": [
"verified": [true], true
],
"verified": [
true
],
"mobile_available": [ "mobile_available": [
true true
] ]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment