예제 #1
0
 def __init__(self, shouldPack=True):
     """
     Class representation of the C{IMAGE_DATA_DIRECTORY} structure. 
     @see: U{http://msdn.microsoft.com/es-es/library/windows/desktop/ms680305%28v=vs.85%29.aspx}
     
     @type shouldPack: bool
     @param shouldPack: If set to C{True} the L{Directory} object will be packed. If set to C{False} the object won't be packed.
     """
     self.name = datatypes.String("")
     self.rva = datatypes.DWORD(0)  #: L{DWORD} rva.
     self.size = datatypes.DWORD(0)  #: L{DWORD} size.
     self.info = None  #: This variable holds the information of the directory.
     self.shouldPack = shouldPack
예제 #2
0
    def P(self):
        if self.current.kind == "ID":
            if self.lookahead.string == "(":
                return self.function_call()
            else:
                return self.V()

        if self.current.string == "-":
            return self.unary()

        if self.current.kind == "DIGITS":
            return self.number()

        if self.current.kind == "STRING":
            string = self.expect_kinds("STRING")
            stripped_string = string[1:-1]
            return ParseNode("STRING",
                             string=datatypes.String(stripped_string))

        if self.check_strings("("):
            node = self.expression()
            self.confirm_strings(")")
            return node
예제 #3
0
파일: ast.py 프로젝트: f4str/neetspeak
 def __init__(self, value):
     self.value = types.String(value[1:-1])
예제 #4
0
def set_classifier_interface_params(spec,
                                    features,
                                    class_labels,
                                    model_accessor_for_class_labels,
                                    output_features=None):
    """
    Common utilities to set the regression interface params.
    """
    # Normalize the features list.
    features = _fm.process_or_validate_features(features)

    if class_labels is None:
        raise ValueError("List of class labels must be provided.")

    n_classes = len(class_labels)

    output_features = _fm.process_or_validate_classifier_output_features(
        output_features, class_labels)

    if len(output_features) == 1:
        predicted_class_output, pred_cl_type = output_features[0]
        score_output = None
    elif len(output_features) == 2:
        predicted_class_output, pred_cl_type = output_features[0]
        score_output, score_output_type = output_features[1]
    else:
        raise ValueError(
            "Provided output classes for a classifier must be "
            "a list of features, predicted class and (optionally) class_score."
        )

    spec.description.predictedFeatureName = predicted_class_output

    # Are they out of order?
    if not (pred_cl_type == datatypes.Int64()
            or pred_cl_type == datatypes.String()):
        raise ValueError(
            "Provided predicted class output type not Int64 or String (%s)." %
            repr(pred_cl_type))

    if score_output is not None:
        if not isinstance(score_output_type, datatypes.Dictionary):
            raise ValueError(
                "Provided class score output type not a Dictionary (%s)." %
                repr(score_output_type))

        if score_output_type.key_type != pred_cl_type:
            raise ValueError(
                ("Provided class score output (%s) key_type (%s) does not "
                 "match type of class prediction (%s).") %
                (score_output, repr(
                    score_output_type.key_type), repr(pred_cl_type)))

        spec.description.predictedProbabilitiesName = score_output

    # add input
    for index, (cur_input_name, input_type) in enumerate(features):
        input_ = spec.description.input.add()
        input_.name = cur_input_name
        datatypes._set_datatype(input_.type, input_type)

    # add output
    for index, (cur_output_name, output_type) in enumerate(output_features):
        output_ = spec.description.output.add()
        output_.name = cur_output_name
        datatypes._set_datatype(output_.type, output_type)

    # Worry about the class labels
    if pred_cl_type == datatypes.String():
        for c in class_labels:
            getattr(spec, model_accessor_for_class_labels
                    ).stringClassLabels.vector.append(str(c))
    else:
        for c in class_labels:
            conv_error = False
            try:
                if not (int(c) == c):
                    conv_error = True
            except:
                conv_error = True

            if conv_error:
                raise TypeError(
                    ("Cannot cast '%s' class to an int type " % str(c)) +
                    "(class type determined by type of first class).")

            getattr(spec, model_accessor_for_class_labels
                    ).int64ClassLabels.vector.append(int(c))

    # And we are done!
    return spec
def process_or_validate_classifier_output_features(output_features,
                                                   class_labels,
                                                   supports_class_scores=True):
    """
    Given a list of class labels and a list of output_features, validate the 
    list and return a valid version of output_features with all the correct 
    data type information included.
    """
    def raise_error(msg):

        raise ValueError("Classifier error: %s" % msg)

    class_labels = list(class_labels)

    # First, we need to determine the type of the classes.
    _int_types = (bool, int, long, _np.bool_, _np.int32, _np.int64)

    _str_types = (str, unicode)

    if all(isinstance(cl, _int_types) for cl in class_labels):
        output_class_type = datatypes.Int64()

    elif all(isinstance(cl, _str_types) for cl in class_labels):
        output_class_type = datatypes.String()

    else:
        raise ValueError(
            'Class labels must be all of type int or all of type string.')

    if output_features is None:

        out = [("classLabel", output_class_type)]

        if supports_class_scores:
            out += [("classProbability",
                     datatypes.Dictionary(output_class_type))]

    elif isinstance(output_features, (str, unicode)):

        out = [(output_features, output_class_type)]

        if supports_class_scores:
            out += [("classProbability",
                     datatypes.Dictionary(output_class_type))]

    elif (isinstance(output_features, (list, tuple))
          and all(isinstance(fn, (str, unicode)) for fn in output_features)
          and len(output_features) == 2):

        if supports_class_scores:
            out = [(output_features[0], output_class_types),
                   (output_features[1],
                    datatypes.Dictionary(output_class_type))]
        else:
            raise ValueError(
                "Classifier model (as trained) does not support output scores for classes."
            )

    elif is_valid_feature_list(output_features):

        output_features = [(k, datatypes._normalize_datatype(dt))
                           for k, dt in output_features]

        if len(output_features) == 1 or not supports_class_scores:
            if not output_features[0][1] == output_class_type:
                raise ValueError(
                    "Type of output class feature does not match type of class labels."
                )

        else:
            # Make sure the first two output features specified give the output
            # class field and the output class scores dictionary field
            if (isinstance(output_features[0][1], datatypes.Dictionary)
                    and isinstance(output_features[1][1], output_class_type)):

                output_features[0], output_features[1] = output_features[
                    1], output_features[0]

            if not isinstance(output_features[1][1], datatypes.Dictionary):
                raise_error(
                    "Output features class scores should be dictionary type.")

            if output_features[1][1].key_type != output_class_type:
                raise_error(
                    "Class scores dictionary key type does not match type of class labels."
                )

            if output_features[0][1] != output_class_type:
                raise_error(
                    "Specified type of output class does not match type of class labels."
                )

        # NOTE: We are intentionally allowing the case where additional fields are allowed
        # beyond the original two features.

        out = output_features

    else:
        raise_error("Form of output features not recognized")

    return out
예제 #6
0
 def execute(self, env):
     env.qframe.append(datatypes.String(input()))
예제 #7
0
 def test_string_equality(self):
     self.assertEqual(visp.String('hello'), datatypes.String('hello'))
     self.assertNotEqual(visp.String('hello'), 'hello')