本文整理汇总了Python中Kernel.Utils类的典型用法代码示例。如果您正苦于以下问题:Python Utils类的具体用法?Python Utils怎么用?Python Utils使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Utils类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_true_guard_statements
def add_true_guard_statements(obj,n_true_guard_statements_min_left=1,n_true_guard_statements_max_left=2,
n_true_guard_statements_min_right=1,n_true_guard_statements_max_right=2):
for i in xrange(len(obj)):
n_guard_statements_left = randint(min(n_true_guard_statements_min_left,n_true_guard_statements_max_left),
max(n_true_guard_statements_min_left,n_true_guard_statements_max_left))
n_guard_statements_right = randint(min(n_true_guard_statements_min_right,n_true_guard_statements_max_right),
max(n_true_guard_statements_min_right,n_true_guard_statements_max_right))
is_if = ex.is_if(obj[i])
is_else_if = ex.is_else_if(obj[i])
is_while = ex.is_while(obj[i])
is_until = ex.is_until(obj[i])
if is_if or is_else_if or is_while or is_until:
true_guard_statements_left = [Utils.generate_true_statement(n_min_value=100,n_max_value=300) for j in xrange(n_guard_statements_left)]
true_guard_statements_right = [Utils.generate_true_statement(n_min_value=100,n_max_value=300) for j in xrange(n_guard_statements_right)]
if is_if or is_else_if: obj[i] = add_true_guard_statements_if(obj[i],n_guard_statements_left,
true_guard_statements_left,
n_guard_statements_right,
true_guard_statements_right)
if is_while or is_until: obj[i] = add_true_guard_statements_while(obj[i],n_guard_statements_left,
true_guard_statements_left,
n_guard_statements_right,
true_guard_statements_right)
return obj
示例2: simple_block
def simple_block(n_statements_min=10,n_statements_max=20):
n_statements = randint(min(n_statements_min,n_statements_max),max(n_statements_min,n_statements_max))
variables = [variable() for i in xrange(n_statements)]
values = [value() for i in xrange(n_statements)]
res = ""
for i in xrange(n_statements): res += assign(variables[i],values[i])+"\n"
#res += enum(1,3,5,10) + "\n"
#elif rnd==2: res += ternary_operation(1,3)
for i in xrange(n_statements/2):
""" -3 -> ternary
-2 -> enum
-1 -> arithmetic assign
0 -> f arity 0
1 -> f arity 1
2 -> f arity 2
"""
arity = randint(-1,2)
if arity==-1:
r,rp = randint(0,len(variables)-1),randint(0,len(variables)-1)
res += variables[r]+" "+arithmetic_assign()+" "+variables[rp]+"\n"
elif arity==0: res += Utils.low_up_string(choice(Config.FUNCTIONS_ZERO_ARITY)) + "()\n"
elif arity==1:
r,rf,rp = randint(0,len(variables)-1),randint(0,len(Config.FUNCTIONS_ONE_ARITY)-1),randint(0,len(variables)-1)
res += variables[r]+" "+arithmetic_assign()+" "+Utils.low_up_string(Config.FUNCTIONS_ONE_ARITY[rf])+"("+variables[rp]+")\n"
elif arity==2:
r,rf,rp,rt = randint(0,len(variables)-1),randint(0,len(Config.FUNCTIONS_TWO_ARITY)-1),randint(0,len(variables)-1),randint(0,len(variables)-1)
res += variables[r]+" "+arithmetic_assign()+" "+Utils.low_up_string(Config.FUNCTIONS_TWO_ARITY[rf])+"("+variables[rp]+","+variables[rt]+")\n"
return res
示例3: block_with
def block_with(deep_act,n_statements_min=10,n_statements_max=20,n_guard_statements_min=1,n_guard_statements_max=5,n_else_if_min=1,n_else_if_max=5,deep_max=5,case_values_min=-1000,case_values_max=1000):
v = variable()
declaration = Utils.generate_random_declarator()+v+' = ObjCreate("shell.application") \n'
res = Utils.low_up_string(" With ") + v + "\n" + \
block(deep_act+1,n_statements_min,n_statements_max,n_guard_statements_min,
n_guard_statements_max,n_else_if_min,n_else_if_max,deep_max,case_values_min,
case_values_max)+Utils.low_up_string("\n EndWith\n")
return declaration+res
示例4: hide_variable_names
def hide_variable_names(obj):
identifiers = list(ex.extract_defined_variables_from_obj(obj)) # Extraer solo las que se definen en el script #
replaces = Utils.mod_names_hash(identifiers)
boundary = "\n"+Utils.generate_random_string(20,30)+"\n"
obj = boundary.join(obj)
for i in xrange(len(identifiers)):
if identifiers[i]: obj = sub(r"\$"+identifiers[i][1:]+r"\b","$"+replaces[i]+" ",obj)
return obj.split(boundary)
示例5: block_func
def block_func(arity_min=1,arity_max=4,identifiers_length_min=5,identifiers_length_max=10,n_statements_min=10,n_statements_max=20,n_guard_statements_min=1,n_guard_statements_max=5,n_else_if_min=1,n_else_if_max=5,deep_max=5,case_values_min=-1000,case_values_max=1000):
id_func,arity = Utils.generate_identifier(identifiers_length_min,identifiers_length_max),randint(min(arity_min,arity_max),max(arity_min,arity_max))
Config.TDS_FUNCTIONS[id_func]=arity # ¿Sobra?
res = Utils.low_up_string("Func ")+id_func+"("
for i in xrange(arity):
if i<arity-1: res += variable()+","
else: res += variable()
res += ")\n\n\n"+block(0,n_statements_min,n_statements_max,n_guard_statements_min,n_guard_statements_max,n_else_if_min,n_else_if_max,deep_max,case_values_min,case_values_max)+Utils.low_up_string("\n EndFunc\n")
Globals.arity_new_functions.append(arity)
return res
示例6: hide_function_parameters
def hide_function_parameters(obj):
identifiers = list(ex.extract_parameters_from_obj(obj))
replaces = Utils.mod_names_hash(identifiers)
boundary = "\n"+Utils.generate_random_string(20,30)+"\n"
obj = boundary.join(obj)
for i in xrange(len(identifiers)):
try:
if identifiers[i]: obj = sub(r"\$"+identifiers[i][1:]+r"\b","$"+replaces[i]+" ",obj)
except: continue
return obj.split(boundary)
示例7: enum
def enum(n_enumeration_min=1,n_enumeration_max=3,length_min=5,length_max=10):
rnd_enum = randint(n_enumeration_min,n_enumeration_max)
r = Utils.low_up_string("Local Enum ")
rnd_step = randint(0,1)
if rnd_step==1: r += Utils.low_up_string("Step ")+enum_stepval()+" "
for i in xrange(rnd_enum):
var = variable(length_min,length_max)
val = value()
r += var_definition(var,val)
if i != rnd_enum-1: r += ", "
else: r += " "
return r
示例8: block_switch
def block_switch(deep_act,n_statements_min=10,n_statements_max=20,n_guard_statements_min=1,n_guard_statements_max=5,n_else_if_min=1,n_else_if_max=5,deep_max=5,case_values_min=-1000,case_values_max=1000):
v = variable()
declaration = Utils.generate_random_declarator()+assign_by_var_name(v)+"\n"
n_cases = randint(min(n_else_if_min,n_else_if_max),max(n_else_if_min,n_else_if_max))
res = Utils.low_up_string("Switch ")+v+"\n"
for i in xrange(n_cases):
a,b = integer(min(case_values_min,case_values_max),max(case_values_min,case_values_max)),integer(min(case_values_min,case_values_max),max(case_values_min,case_values_max))
a,b = min(a,b),max(a,b)
res += Utils.low_up_string("Case ")+a+Utils.low_up_string(" To ")+b+"\n"+block(deep_act,n_statements_min,n_statements_max,n_guard_statements_min,n_guard_statements_max,n_else_if_min,n_else_if_max,deep_max,case_values_min,case_values_max)
res += "\nCase Else\n"+block(deep_act,n_statements_min,n_statements_max,n_guard_statements_min,n_guard_statements_max,n_else_if_min,n_else_if_max,deep_max,case_values_min,case_values_max)
res += Utils.low_up_string("\nEndSwitch\n")
return declaration+res
示例9: ternary_operation
def ternary_operation(n_guard_statements_min=1,n_guard_statements_max=3):
res = ""
declarations = ""
var = variable()
n_guard_statements = randint(min(n_guard_statements_min,n_guard_statements_max),
min(n_guard_statements_min,n_guard_statements_max))
log_statements = logical_statements(n_guard_statements)
logic_variables = ExtractKeywords.extract_variables(log_statements)
res += Utils.generate_random_declarator()+var+"\n"
for i in logic_variables: declarations += Utils.generate_random_declarator()+assign_by_var_name(i)+"\n"
res += var+" = (" + log_statements + ") ? " + value() + " : " + value()
return declarations+res+"\n"
示例10: add_true_guard_statements_while
def add_true_guard_statements_while(line,n_guard_statements_left,true_guard_statements_left,
n_guard_statements_right,true_guard_statements_right):
line = line.strip()
if line[-1]!="_":
splitted = split("\s",line)
line = splitted[0] + " "
if n_guard_statements_left>0:
line += Utils.low_up_string(" And ").join(true_guard_statements_left) + Utils.low_up_string(" And ")
line += " ".join(splitted[1:])
if n_guard_statements_right>0:
line += Utils.low_up_string(" And ") + Utils.low_up_string(" And ").join(true_guard_statements_right)
return line
示例11: block_for
def block_for(deep_act,n_statements_min=10,n_statements_max=20,n_guard_statements_min=1,n_guard_statements_max=5,n_else_if_min=1,n_else_if_max=5,deep_max=5,case_values_min=-1000,case_values_max=1000):
a,b = integer(),integer()
a,b = min(a,b),max(a,b)
v = variable()
declaration = Utils.generate_random_declarator()+assign_by_var_name(v)+"\n\n"
res = Utils.low_up_string("For ")+assign_full(v,a)+Utils.low_up_string(" To ")+b
rnd_step = randint(0,1)
if rnd_step==1:
res += Utils.low_up_string(" Step ")+str(randint(1,10))+" \n"
else: res += "\n"
res += block(deep_act+1,n_statements_min,n_statements_max,n_guard_statements_min,
n_guard_statements_max,n_else_if_min,n_else_if_max,deep_max,case_values_min,
case_values_max)+Utils.low_up_string("\n Next\n")
return declaration+res
示例12: add_hardcoded_funcs
def add_hardcoded_funcs(obj,add_calls=False,functions=Globals.defined_new_functions,arity=Globals.arity_new_functions,n_funcs_min=3,n_funcs_max=5):
n_funcs = randint(min(n_funcs_min,n_funcs_max),max(n_funcs_min,n_funcs_max))
shuffle(hp.HARDCODED_PROGRAMS)
funcs = hp.HARDCODED_PROGRAMS[:n_funcs]
for i in xrange(n_funcs):
function_code = funcs[i]()
obj.insert(0,function_code+"\n\n")
if add_calls:
arity = Globals.arity_new_functions[-1]
identifier = Globals.defined_new_functions[-1]
params = ",".join([g.value() for i in xrange(arity)])
obj.insert(0,Utils.low_up_string(Utils.generate_random_declarator()) + \
g.variable() + " = " + identifier + "("+params+")\n\n")
return obj
示例13: block_while
def block_while(deep_act,n_statements_min=10,n_statements_max=20,n_guard_statements_min=1,n_guard_statements_max=5,n_else_if_min=1,n_else_if_max=5,deep_max=5,case_values_min=-1000,case_values_max=1000):
n_guard_statements = randint(min(n_guard_statements_min,n_guard_statements_max),
max(n_guard_statements_min,n_guard_statements_max))
vars_one = []
vars_two = []
vals_one = []
vals_two = []
rel_ops = []
declarations = []
for i in xrange(n_guard_statements):
vars_one.append(variable())
vars_two.append(variable())
vals_one.append(integer(100,1000))
vals_two.append(integer(100,1000))
rel_ops.append(choice([">","<",">=","<=","="]))
declarations.append(Utils.generate_random_declarator() + vars_one[i] + " = " + vals_one[i])
declarations.append(Utils.generate_random_declarator() + vars_two[i] + " = " + vals_two[i])
res = Utils.low_up_string(" While ")
for i in xrange(n_guard_statements):
res += vars_one[i] + " " + rel_ops[i] + " "+ vars_two[i]
if i<n_guard_statements-1: res += Utils.low_up_string(" Or ")
res += "\n"
res += block(deep_act+1,n_statements_min,n_statements_max,n_guard_statements_min,
n_guard_statements_max,n_else_if_min,n_else_if_max,deep_max,case_values_min,
case_values_max)+"\n"
for i in xrange(n_guard_statements):
if rel_ops[i]==">":
if vals_one[i]>vals_two[i]:
res += choice([vars_one[i] + " -= 1\n", vars_two[i] + " += 1\n"])
elif rel_ops[i]=="<":
if vals_one[i]<vals_two[i]: res += choice([vars_one[i] + " += 1\n",vars_two[i] + " -= 1\n"])
elif rel_ops[i]==">=":
if vals_one[i]>=vals_two[i]:
res += choice([vars_one[i] + " -= 1\n", vars_two[i] + " += 1\n"])
elif rel_ops[i]=="<=":
if vals_one[i]<=vals_two[i]: res += choice([vars_one[i] + " += 1\n",vars_two[i] + " -= 1\n"])
elif rel_ops[i]=="=":
if vals_one[i]==vals_two[i]: res += choice([vars_one[i] + " += 1\n",vars_two[i] + " -= 1\n" ,
vars_one[i] + " -= 1\n",vars_two[i] + " += 1\n"])
res += Utils.low_up_string("\nWEnd\n")
return "\n".join(declarations)+"\n"+res
示例14: replace_includes
def replace_includes(obj,directories_files):
for i in xrange(len(obj)):
include = ex.extract_includes(obj[i])
if include:
path_content = directories_files[include] if include in directories_files else None
if path_content!=None: obj[i] = Utils.read_file(path_content)
return obj
示例15: hide_function_names
def hide_function_names(obj):
identifiers = list(ex.extract_func_names_from_obj(obj))
replaces = Utils.mod_names_identifier(identifiers)
obj = "\n".join(obj)
for i in xrange(len(identifiers)):
if identifiers[i]: obj = sub(r""+identifiers[i]+r"\s*\(",replaces[i]+"(",obj)
return obj.split("\n")