<commit_before>package org.cafebabepy.runtime.module.builtins;

import org.cafebabepy.annotation.DefineCafeBabePyFunction;
import org.cafebabepy.annotation.DefineCafeBabePyModule;
import org.cafebabepy.runtime.PyObject;
import org.cafebabepy.runtime.Python;
import org.cafebabepy.runtime.module.AbstractCafeBabePyModule;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by yotchang4s on 2017/05/12.
 */
@DefineCafeBabePyModule(name = "builtins")
public class PyBuiltinsModule extends AbstractCafeBabePyModule {
    public PyBuiltinsModule(Python runtime) {
        super(runtime);
    }

    @DefineCafeBabePyFunction(name = "issubclass")
    public PyObject isSubClass(PyObject clazz, PyObject classInfo) {
        if (clazz == null || !clazz.isType()) {
            throw this.runtime.newRaiseTypeError(
                    "issubclass() arg 1 must be a class");
        }

        Set<PyObject> classInfoSet = new HashSet<>();
        appendIntoClassInfoSet(classInfoSet, classInfo);

        if (classInfoSet.contains(classInfo)) {
            return this.runtime.True();
        } else {
            return this.runtime.False();
        }
    }

    private void appendIntoClassInfoSet(Set<PyObject> classInfoSet, PyObject classinfo) {
        if (classinfo == null || !classinfo.isType()) {
            throw this.runtime.newRaiseTypeError(
                    "issubclass() arg 2 must be a class or tuple of classes");

        } else if (classinfo.getType() instanceof PyTupleType) {
            this.runtime.iter(classinfo, c -> appendIntoClassInfoSet(classInfoSet, c));

        } else {
            classInfoSet.add(classinfo);
        }
    }
}<commit_msg>Rename oops!! issubclass to isinstance<commit_after>package org.cafebabepy.runtime.module.builtins;

import org.cafebabepy.annotation.DefineCafeBabePyFunction;
import org.cafebabepy.annotation.DefineCafeBabePyModule;
import org.cafebabepy.runtime.PyObject;
import org.cafebabepy.runtime.Python;
import org.cafebabepy.runtime.module.AbstractCafeBabePyModule;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by yotchang4s on 2017/05/12.
 */
@DefineCafeBabePyModule(name = "builtins")
public class PyBuiltinsModule extends AbstractCafeBabePyModule {
    public PyBuiltinsModule(Python runtime) {
        super(runtime);
    }

    @DefineCafeBabePyFunction(name = "isinstance")
    public PyObject isInstance(PyObject object, PyObject classInfo) {
        if (classInfo == null || (!classInfo.isType() && classInfo instanceof PyTupleType)) {
            throw this.runtime.newRaiseTypeError(
                    "isinstance() arg 2 must be a type or tuple of types");
        }

        Set<PyObject> objectTypeSet = new HashSet<>();
        appendIntoObjectTypeSet(objectTypeSet, object);

        if (objectTypeSet.contains(object)) {
            return this.runtime.True();
        }

        return this.runtime.False();
    }

    private void appendIntoObjectTypeSet(Set<PyObject> objectTypeSet, PyObject type) {
        objectTypeSet.add(type);

        if (type instanceof PyTypeType) {
            if (objectTypeSet.contains(type)) {
                return;
            }

        } else if (type instanceof PyObjectType) {
            if (objectTypeSet.contains(type)) {
                return;
            }

        } else if (type instanceof PyTupleType) {
            this.runtime.iter(type, t -> appendIntoObjectTypeSet(objectTypeSet, t));
        }

        type.getSuperTypes().forEach(t -> appendIntoObjectTypeSet(objectTypeSet, t));
    }
}
{'fraction_non_alphanumeric': 0.10177115546809108, 'fraction_numerical': 0.005903851560303626, 'mean_word_length': 2.948945615982242, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 12, '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': '3874861', 'n_tokens_mistral': 1107, 'n_tokens_neox': 1050, 'n_words': 210}