Skip to content

Commit

Permalink
Merge pull request #1359 from CloudSlang/cs_functions_int_param
Browse files Browse the repository at this point in the history
Python string processing methods refactor
  • Loading branch information
HorvatMaria authored May 24, 2022
2 parents 26baaaa + 1c020a3 commit 85bebfa
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 20 deletions.
77 changes: 69 additions & 8 deletions cloudslang-runtime/src/main/resources/scripts/cs_extract_number.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,73 @@
import re
def cs_extract_number(string, count = 1):
if type(string).__name__ != 'str' and type(string).__name__ != 'unicode':
raise Exception("Expected a string for parameter 'string', got " + str(string))

result = None
numbers = re.findall("[0-9]*\.?[0-9]+", string)
def cs_extract_number(string, count=1):
if type(string).__name__ != "str" and type(string).__name__ != "unicode":
raise Exception("Expected a string for parameter 'string', got " + str(string))

if len(numbers) >= count & count >= 1:
result = numbers[count - 1]
result = None
numbers = re.findall("[0-9]*\.?[0-9]+", string)

return result
count_int_value = validate_input(count, "count", 1)

if len(numbers) >= count_int_value & count_int_value >= 1:
result = numbers[count_int_value - 1]

return result


def validate_input(input_value, parameter_name, default_value):
if type(input_value).__name__ == "NoneType":
return default_value
elif type(input_value).__name__ == "int":
return input_value
elif type(input_value).__name__ == "float" or type(input_value).__name__ == "bool":
return int(input_value)
elif type(input_value).__name__ == "str":
return convert_string_to_int(input_value, parameter_name, default_value)
elif type(input_value).__name__ == "bytes":
return convert_string_to_int(input_value.decode(), parameter_name, default_value)
else:
raise Exception("Type of parameter '" + parameter_name + "' is not valid, got " + str(input_value)
+ " of type " + str(type(input_value).__name__))


def convert_string_to_int(input_value, parameter_name, default_value):
if is_string_of_type_int(input_value):
return int(input_value)
elif is_string_of_type_float(input_value):
return int(float(input_value))
elif is_string_of_type_none(input_value):
return default_value
else:
return check_bool_in_string(input_value, parameter_name)


def is_string_of_type_int(input_value):
try:
int(input_value)
return True
except ValueError:
return False


def is_string_of_type_float(input_value):
try:
float(input_value)
return True
except ValueError:
return False

def is_string_of_type_none(input_value):
if input_value.lower() == 'none':
return True
else:
return False


def check_bool_in_string(input_value, parameter_name):
if input_value.lower() == 'true':
return int(True)
elif input_value.lower() == 'false':
return int(False)
else:
raise Exception("Value of the parameter '" + parameter_name + "' is not valid, got " + str(input_value))
80 changes: 72 additions & 8 deletions cloudslang-runtime/src/main/resources/scripts/cs_replace.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,76 @@
def cs_replace(string, old_val, new_val, count = -1):
def cs_replace(string, old_val, new_val, count=-1):

if type(string).__name__ != 'str' and type(string).__name__ != 'unicode':
raise Exception("Expected a string for parameter 'string', got " + str(string))
if type(string).__name__ != "str" and type(string).__name__ != "unicode":
raise Exception("Expected a string for parameter 'string', got " + str(string))

if type(old_val).__name__ != 'str' and type(old_val).__name__ != 'unicode':
raise Exception("Expected a string for parameter 'old_val', got " + str(old_val))
if type(old_val).__name__ != "str" and type(old_val).__name__ != "unicode":
raise Exception("Expected a string for parameter 'old_val', got " + str(old_val))

if type(new_val).__name__ != 'str' and type(new_val).__name__ != 'unicode':
raise Exception("Expected a string for parameter 'new_val', got " + str(new_val))
if type(new_val).__name__ != "str" and type(new_val).__name__ != "unicode":
raise Exception("Expected a string for parameter 'new_val', got " + str(new_val))

return string.replace(old_val, new_val) if count < 0 else string.replace(old_val, new_val, count)
count_int_value = validate_input(count, "count", -1)

return (
string.replace(old_val, new_val)
if count_int_value < 0
else string.replace(old_val, new_val, count_int_value)
)


def validate_input(input_value, parameter_name, default_value):
if type(input_value).__name__ == "NoneType":
return default_value
elif type(input_value).__name__ == "int":
return input_value
elif type(input_value).__name__ == "float" or type(input_value).__name__ == "bool":
return int(input_value)
elif type(input_value).__name__ == "str":
return convert_string_to_int(input_value, parameter_name, default_value)
elif type(input_value).__name__ == "bytes":
return convert_string_to_int(input_value.decode(), parameter_name, default_value)
else:
raise Exception("Type of parameter '" + parameter_name + "' is not valid, got " + str(input_value)
+ " of type " + str(type(input_value).__name__))


def convert_string_to_int(input_value, parameter_name, default_value):
if is_string_of_type_int(input_value):
return int(input_value)
elif is_string_of_type_float(input_value):
return int(float(input_value))
elif is_string_of_type_none(input_value):
return default_value
else:
return check_bool_in_string(input_value, parameter_name)


def is_string_of_type_int(input_value):
try:
int(input_value)
return True
except ValueError:
return False


def is_string_of_type_float(input_value):
try:
float(input_value)
return True
except ValueError:
return False

def is_string_of_type_none(input_value):
if input_value.lower() == 'none':
return True
else:
return False


def check_bool_in_string(input_value, parameter_name):
if input_value.lower() == 'true':
return int(True)
elif input_value.lower() == 'false':
return int(False)
else:
raise Exception("Value of the parameter '" + parameter_name + "' is not valid, got " + str(input_value))
76 changes: 72 additions & 4 deletions cloudslang-runtime/src/main/resources/scripts/cs_substring.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
def cs_substring(string, start, end = None):
if type(string).__name__ != 'str' and type(string).__name__ != 'unicode':
raise Exception("Expected a string for parameter 'string', got " + str(string))
def cs_substring(string, start, end=None):
if type(string).__name__ != "str" and type(string).__name__ != "unicode":
raise Exception("Expected a string for parameter 'string', got " + str(string))

return string[start: end]
start_int_value = validate_input(start, "start", None)
end_int_value = validate_input(end, "end", None)

if (start_int_value is None) and (end_int_value is None):
return string
elif end_int_value is None:
return string[start_int_value:]
elif start_int_value is None:
return string[:end_int_value]
else:
return string[start_int_value:end_int_value]


def validate_input(input_value, parameter_name, default_value):
if type(input_value).__name__ == "NoneType":
return default_value
elif type(input_value).__name__ == "int":
return input_value
elif type(input_value).__name__ == "float" or type(input_value).__name__ == "bool":
return int(input_value)
elif type(input_value).__name__ == "str":
return convert_string_to_int(input_value, parameter_name, default_value)
elif type(input_value).__name__ == "bytes":
return convert_string_to_int(input_value.decode(), parameter_name, default_value)
else:
raise Exception("Type of parameter '" + parameter_name + "' is not valid, got " + str(input_value)
+ " of type " + str(type(input_value).__name__))


def convert_string_to_int(input_value, parameter_name, default_value):
if is_string_of_type_int(input_value):
return int(input_value)
elif is_string_of_type_float(input_value):
return int(float(input_value))
elif is_string_of_type_none(input_value):
return default_value
else:
return check_bool_in_string(input_value, parameter_name)


def is_string_of_type_int(input_value):
try:
int(input_value)
return True
except ValueError:
return False


def is_string_of_type_float(input_value):
try:
float(input_value)
return True
except ValueError:
return False

def is_string_of_type_none(input_value):
if input_value.lower() == 'none':
return True
else:
return False


def check_bool_in_string(input_value, parameter_name):
if input_value.lower() == 'true':
return int(True)
elif input_value.lower() == 'false':
return int(False)
else:
raise Exception("Value of the parameter '" + parameter_name + "' is not valid, got " + str(input_value))

0 comments on commit 85bebfa

Please sign in to comment.