<commit_before>'''
Copyright (C) 2015 Dato, Inc.
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.
'''
import commands
import json
import logging
import os
import re
import tempfile
import unittest
import pandas
import sys

from ..connect import main as glconnect
from .. import sys_util as _sys_util
from .. import util
from .. import SGraph, Model, SFrame, load_graph, load_model, load_sframe
from pandas.util.testing import assert_frame_equal

restricted_place = '/root'
if sys.platform == 'win32':
    restricted_place = 'C:/Windows/System32/config/RegBack'
elif sys.platform == 'darwin':
    restricted_place = '/System'


def _test_save_load_object_helper(testcase, obj, path):
    """
    Helper function to test save and load a server side object to a given url.
    """
    def cleanup(url):
        """
        Remove the saved file from temp directory.
        """
        tempdir = tempfile.gettempdir()
        pattern = path + ".*"
        for f in os.listdir(tempdir):
            if re.search(pattern, f):
                os.remove(os.path.join(tempdir, f))

    if isinstance(obj, SGraph):
        obj.save(path + ".graph")
        newobj = load_graph(path + ".graph")
        testcase.assertItemsEqual(obj.get_fields(), newobj.get_fields())
        testcase.assertDictEqual(obj.summary(), newobj.summary())
    elif isinstance(obj, Model):
        obj.save(path + ".model")
        newobj = load_model(path + ".model")
        testcase.assertItemsEqual(obj.list_fields(), newobj.list_fields())
        testcase.assertEqual(type(obj), type(newobj))
    elif isinstance(obj, SFrame):
        obj.save(path + ".frame_idx")
        newobj = load_sframe(path + ".frame_idx")
        testcase.assertEqual(obj.shape, newobj.shape)
        testcase.assertEqual(obj.column_names(), newobj.column_names())
        testcase.assertEqual(obj.column_types(), newobj.column_types())
        assert_frame_equal(obj.head(obj.num_rows()).to_dataframe(),
                           newobj.head(newobj.num_rows()).to_dataframe())
    else:<commit_msg>Fixed path issue in test_io.<commit_after>'''
Copyright (C) 2015 Dato, Inc.
All rights reserved.

This software may be modified and distributed under the terms
of the BSD license. See the LICENSE file for details.
'''
import commands
import json
import logging
import os
import fnmatch
import tempfile
import unittest
import pandas
import sys

from ..connect import main as glconnect
from .. import sys_util as _sys_util
from .. import util
from .. import SGraph, Model, SFrame, load_graph, load_model, load_sframe
from pandas.util.testing import assert_frame_equal

restricted_place = '/root'
if sys.platform == 'win32':
    restricted_place = 'C:/Windows/System32/config/RegBack'
elif sys.platform == 'darwin':
    restricted_place = '/System'


def _test_save_load_object_helper(testcase, obj, path):
    """
    Helper function to test save and load a server side object to a given url.
    """
    def cleanup(url):
        """
        Remove the saved file from temp directory.
        """
        tempdir = tempfile.gettempdir()
        pattern = path + "*"
        for f in os.listdir(tempdir):
            if fnmatch.fnmatch(pattern, f):
                os.remove(os.path.join(tempdir, f))

    if isinstance(obj, SGraph):
        obj.save(path + ".graph")
        newobj = load_graph(path + ".graph")
        testcase.assertItemsEqual(obj.get_fields(), newobj.get_fields())
        testcase.assertDictEqual(obj.summary(), newobj.summary())
    elif isinstance(obj, Model):
        obj.save(path + ".model")
        newobj = load_model(path + ".model")
        testcase.assertItemsEqual(obj.list_fields(), newobj.list_fields())
        testcase.assertEqual(type(obj), type(newobj))
    elif isinstance(obj, SFrame):
        obj.save(path + ".frame_idx")
        newobj = load_sframe(path + ".frame_idx")
        testcase.assertEqual(obj.shape, newobj.shape)
        testcase.assertEqual(obj.column_names(), newobj.column_names())
        testcase.assertEqual(obj.column_types(), newobj.column_types())
        assert_frame_equal(obj.head(obj.num_rows()).to_dataframe(),
                           newobj.head(newobj.num_rows()).to_dataframe())
    else:
{'fraction_non_alphanumeric': 0.11537566876017678, 'fraction_numerical': 0.0037217957664573155, 'mean_word_length': 3.1306436119116237, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4544348', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1276, 'n_words': 337}