pyggi.atomic_operator module¶
This module contains AtomicOperator class which is an abstact base class, and several classes inherit the AtomicOperator class.
-
class
pyggi.atomic_operator.
AtomicOperator
[source]¶ Bases:
object
PYGGI-defined Atomic Operator: User can generate the own custom edit operators which can be converted into a list of atomic operators. For example, MOVE x -> y operator can be represented as [LineReplacement(x, None),LineInsertion(x, y)]
Available List
- LineReplacement
- LineInsertion
- StmtReplacement
- StmtInsertion
-
apply
(program, new_contents, modification_points)[source]¶ ” Apply the operator to the contents of program :param program: The original program instance :type program:
Program
:param new_contents: The new contents of program to which the edit will be applied :type new_contents: dict(str, list(?)) :param modification_points: The original modification points :type modification_points: list(?) :return: success or not :rtype: bool
-
atomic_operators
¶ Returns: [self]
, the list that only contains the AtomicOperator instance itself.Return type: list( atomic_operator.AtomicOperator
)
-
classmethod
create
()[source]¶ Returns: The operator instance with randomly-selected properties. Return type: atomic_operator.AtomicOperator
-
is_valid_for
(program)[source]¶ Parameters: program ( Program
) – The program instance to which this edit will be appliedReturns: Whether the edit is able to be applied to the program Return type: bool
-
modification_point
¶
-
class
pyggi.atomic_operator.
LineInsertion
(line, ingredient, direction='before')[source]¶ Bases:
pyggi.atomic_operator.AtomicOperator
Note
- LineInsertion(([file_path], 4), ([file_path], 2))
Before After 0 0 1 1 2 2 3 3 4 2 … 4 Parameters: - line (tuple(str, int)) – The file path and position of line which is a target of modification
- ingredient (tuple(str, int)) – The file path and index of code line which is an ingredient
- direction (str) – ‘before’ or ‘after’
-
apply
(program, new_contents, modification_points)[source]¶ ” Apply the operator to the contents of program :param program: The original program instance :type program:
Program
:param new_contents: The new contents of program to which the edit will be applied :type new_contents: dict(str, list(str)) :param modification_points: The original modification points :type modification_points: list(int) :return: success or not :rtype: bool
-
classmethod
create
(program, line_file=None, ingr_file=None, direction='before', method='random')[source]¶ Parameters: - program (
Program
) – The program instance to which the random edit will be applied. - line_file (str) – Line means the modification point of the edit. If line_file is specified, the line will be chosen within the file.
- ingr_file (str) – Ingredient is the line to be copied. If ingr_file is specified, the target line will be chosen within the file.
- method (str) – The way of choosing the modification point. ‘random’ or ‘weighted’
Returns: The LineInsertion instance with the randomly-selected properties: line and ingredient.
Return type: - program (
-
modification_point
¶
-
class
pyggi.atomic_operator.
LineReplacement
(line, ingredient=None)[source]¶ Bases:
pyggi.atomic_operator.AtomicOperator
Note
- LineReplacement(([file_path], 3), ([file_path], 2))
Before After 0 0 1 1 2 2 3 2 4 4 - LineReplacement(([file_path], 3), None)
Before After 0 0 1 1 2 2 3 4 4 Parameters: - line (tuple(str, int)) – The file path and index of line which should be replaced
- ingredient (None or tuple(str, int)) – The file path and index of code line which is an ingredient
-
apply
(program, new_contents, modification_points)[source]¶ ” Apply the operator to the contents of program :param program: The original program instance :type program:
Program
:param new_contents: The new contents of program to which the edit will be applied :type new_contents: dict(str, list(str)) :param modification_points: The original modification points :type modification_points: list(int) :return: success or not :rtype: bool
-
classmethod
create
(program, line_file=None, ingr_file=None, del_rate=0, method='random')[source]¶ Parameters: - program (
Program
) – The program instance to which the random edit will be applied. - line_file (str) – Line is the target line to delete. If line_file is specified, the target line will be chosen within the file.
- ingr_file (str) – Ingredient is the line to be copied. If ingr_file is specified, the target line will be chosen within the file.
- del_rate (float) – The probability of that line is deleted instead of replaced with another line
- method (str) – The way of choosing the modification point. ‘random’ or ‘weighted’
Returns: The LineReplacement instance with the randomly-selected properties: line and ingredient.
Return type: - program (
-
modification_point
¶
-
class
pyggi.atomic_operator.
StmtInsertion
(stmt, ingredient, direction='before')[source]¶ Bases:
pyggi.atomic_operator.AtomicOperator
Parameters: - stmt (tuple(str, list(tuple(str, int)))) – The file path and position of statement which is a target of modification
- ingredient (None or tuple(str, list(tuple(str, int)))) – The file path and the position of statement which will be inserted
- direction (str) – ‘before’ or ‘after’
-
apply
(program, new_contents, modification_points)[source]¶ Apply the operator to the contents of program
Parameters: - program (
Program
) – The original program instance - new_contents (dict(str, ?)) – The new contents of program to which the edit will be applied
- modification_points (list(int, )) – The original modification points
Returns: success or not
Return type: bool
- program (
-
classmethod
create
(program, stmt_file=None, ingr_file=None, direction='before', method='random')[source]¶ Parameters: - program (
Program
) – The program instance to which the random edit will be applied. - line_file (str) – stmt means the modification point of the edit. If stmt_file is specified, the stmt will be chosen within that file.
- ingr_file (str) – Ingredient is the stmt to be copied. If ingr_file is specified, the target stmt will be chosen within that file.
- method (str) – The way of choosing the modification point. ‘random’ or ‘weighted’
Returns: The StmtInsertion instance with the randomly-selected properties: stmt and ingredient.
Return type: - program (
-
modification_point
¶
-
class
pyggi.atomic_operator.
StmtReplacement
(stmt, ingredient=None)[source]¶ Bases:
pyggi.atomic_operator.AtomicOperator
Parameters: - stmt (tuple(str, int)) – The file path and the node # of statement which should be replaced
- ingredient (None or tuple(str, int)) – The file path and the node # of statement which is an ingredient
-
apply
(program, new_contents, modification_points)[source]¶ ” Apply the operator to the contents of program :param program: The original program instance :type program:
Program
:param new_contents: The new contents of program to which the edit will be applied :type new_contents: dict(str, ?) :param modification_points: The original modification points :type modification_points: list(int, ) :return: success or not :rtype: bool
-
classmethod
create
(program, stmt_file=None, ingr_file=None, del_rate=0, method='random')[source]¶ Parameters: - program (
Program
) – The program instance to which the random edit will be applied. - stmt_file (str) – stmt is the target statement to delete. If stmt_file is specified, the target statement will be chosen within that file.
- ingr_file (str) – Ingredient is the statement to be copied. If ingr_file is specified, the ingredient statement will be chosen within that file.
- del_rate (float) – The probability of ingredient will be None. ([0,1])
- method (str) – The way of choosing the modification point. ‘random’ or ‘weighted’
Returns: The StmtReplacement instance with the randomly-selected properties: stmt and ingredient.
Return type: - program (
-
modification_point
¶