<commit_before>#!/usr/bin/python
"""
Script used to connect to the edX MongoDB or take in valid MongoDB database
dump and produce valid couse represented in JSON format.
"""
import argparse
import json


class Block(object):
    def __init__(self, id, name, type, children):
        self.id = id
        self.name = name if name else id
        self.type = type
        self.children = children

    def __str__(self):
        return '%s (%s)' % (self.name, self.type)


def read_blocks(course_dict):
    """Read in the course dictionary into Block objects."""
    blocks = {}
    for item in course_dict['blocks']:
        blocks[item['block_id']] = Block(item['block_id'], item['block_name'],
                                         item['block_type'], item['children'])

    for id, block in blocks.iteritems():
        list = []
        for child in block.children:
            list.append(blocks[child['child_id']])
        block.children = list

    return blocks


def main(filename):
    """Read in a file and print JSON of the course content."""
    with open(filename, 'r') as my_file:
        course_dict = json.load(my_file)

    blocks = read_blocks(course_dict)

    print '//',
    print '-' * 58
    print '// Course: %s (%s blocks)' % (course_dict['course_id'],
                                         len(course_dict['blocks']))
    print '//',
    print '-' * 58

    print '\n// printing nodes'
    for id, block in blocks.iteritems():
        print 'g.setNode("%s", { label: "%s" });' % (block.id, block.name)

    print '\n// printing edges'
    for id, block in blocks.iteritems():
        for child in block.children:
            print 'g.setEdge("%s", "%s");' % (block.id, child.id)


if __name__ == "__main__":<commit_msg>actually pushing new version of edx map that prints edges and nodes by chapter.<commit_after>#!/usr/bin/python
"""
Script used to connect to the edX MongoDB or take in valid MongoDB database
dump and produce valid couse represented in JSON format.
"""
import argparse
import json


class Block(object):
    def __init__(self, id, name, type):
        self.id = id
        self.name = name if name else id
        self.type = type
        self.children = []

    def __str__(self):
        return '%s, %s' % (self.type, self.name)


class Node(object):
    def __init__(self, id, label, type):
        self.id = id
        self.label = label
        self.type = type

    def __str__(self):
        return '%s %s' % (self.id.encode('utf-8'), self.label.encode('utf-8'))


class Edge(object):
    def __init__(self, parent, child):
        self.parent = parent
        self.child = child

    def __str__(self):
        return '%s ---> %s' % (self.parent.encode('utf-8'), self.child.encode('utf-8'))


def get_children(block, nodes, edges):
    """Recursive function to produce the nodes and edges for a particular block."""
    for child in block.children:
        nodes.append(Node(child.id, child.name, child.type))
        edges.append(Edge(block.id, child.id))
        get_children(child, nodes, edges)


def create_chapter_maps(block):
    """Given a single block, go build all the nodes and edges."""
    nodes = []
    edges = []

    nodes.append(Node(block.id, block.name, block.type))
    get_children(block, nodes, edges)

    return nodes, edges


def read_blocks(course_dict):
    """Read entire course dictionary into Block objects and set children."""
    blocks = {}
    for item in course_dict['blocks']:
        blocks[item['block_id']] = Block(item['block_id'], item['block_name'], item['block_type'])

    # now go through and add refrences to each parent to the children blocks
    for item in course_dict['blocks']:
        parent_id = item['block_id']
        for child in item['children']:
            blocks[parent_id].children.append(blocks[child['child_id']])

    return blocks.values()


def main(filename):
    """Read in a file and print each course by chapter."""
    with open(filename, 'r') as my_file:
        course_dict = json.load(my_file)

    blocks = read_blocks(course_dict)
    for block in blocks:
        if block.type == 'chapter':
            nodes, edges = create_chapter_maps(block)


if __name__ == "__main__":
{'fraction_non_alphanumeric': 0.13209788548348778, 'fraction_numerical': 0.001900688999762414, 'mean_word_length': 2.6832895888014, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5592323', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1228, 'n_words': 396}