#!/usr/bin/env python

from __future__ import print_function
import sys
from community import best_partition, load_binary, modularity


def main():
    """Main function to mimic C++ version behavior"""
    try:
        filename = sys.argv[1]
        graph_file = load_binary(filename)
        partition = best_partition(graph_file)
        print(str(modularity(partition, graph_file)), file=sys.stderr)
        for elem, part in partition.items():
            print(str(elem) + " " + str(part))
    except (IndexError, IOError):
        print("Usage : ./community filename")
        print("find the communities in graph filename "
              "and display the dendrogram")
        print("Parameters:")
        print("filename is a binary file as generated by the ")
        print("convert utility distributed with the C implementation")

if __name__ == '__main__':
    main()
