mirror of
https://github.com/SpiceBot/SpiceBot.git
synced 2026-11-13 15:09:54 -05:00
first attempt
This commit is contained in:
parent
e82b75c616
commit
98c1203307
@ -1,6 +1,5 @@
|
|||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
"""Top-level package for spicemanip."""
|
"""Top-level package for spicemanip."""
|
||||||
|
|
||||||
__author__ = """Sam Zick"""
|
__author__ = """Sam Zick"""
|
||||||
@ -11,15 +10,20 @@ import random
|
|||||||
import collections
|
import collections
|
||||||
|
|
||||||
|
|
||||||
class Spicemanip():
|
# TODO 'this*that' or '1*that' replace either all strings matching, or an index value
|
||||||
|
# TODO reverse sort z.sort(reverse = True)
|
||||||
|
# list.extend adds lists to eachother
|
||||||
|
|
||||||
|
|
||||||
|
class Spicemanip():
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def __call__(self, inputs, task, output_type='default'):
|
def __call__(self, inputs, task, output_type='default'):
|
||||||
|
|
||||||
if hasattr(self, task):
|
if hasattr(self, task):
|
||||||
raise Exception(str(self.task) + " is not a valid spicemanip function.")
|
raise Exception(
|
||||||
|
str(self.task) + " is not a valid spicemanip function.")
|
||||||
|
|
||||||
# convert input into list
|
# convert input into list
|
||||||
self.inputs = inputs
|
self.inputs = inputs
|
||||||
@ -64,10 +68,6 @@ class Spicemanip():
|
|||||||
self.outputs = self.input_list
|
self.outputs = self.input_list
|
||||||
return
|
return
|
||||||
|
|
||||||
"""
|
|
||||||
returnvalue = eval('spicemanip_' + outputtask + '(inputs, outputtask, mainoutputtask, suboutputtask)')
|
|
||||||
"""
|
|
||||||
|
|
||||||
if len(self.input_list):
|
if len(self.input_list):
|
||||||
|
|
||||||
if self.task in [0, 'complete']:
|
if self.task in [0, 'complete']:
|
||||||
@ -75,7 +75,8 @@ class Spicemanip():
|
|||||||
return
|
return
|
||||||
|
|
||||||
elif self.task in ['index']:
|
elif self.task in ['index']:
|
||||||
self.outputs = self.index(self.input_list[0], self.input_list[1], self.input_list[2])
|
self.outputs = self.index(
|
||||||
|
self.input_list[0], self.input_list[1], self.input_list[2])
|
||||||
|
|
||||||
elif self.is_digit(self.task):
|
elif self.is_digit(self.task):
|
||||||
self.outputs = self.number(self.input_list, int(self.task))
|
self.outputs = self.number(self.input_list, int(self.task))
|
||||||
@ -85,31 +86,44 @@ class Spicemanip():
|
|||||||
|
|
||||||
elif "^" in str(self.task):
|
elif "^" in str(self.task):
|
||||||
range_start, range_end = str(self.task).split("^", 1).sort()
|
range_start, range_end = str(self.task).split("^", 1).sort()
|
||||||
self.outputs = self.rangebetween(self.input_list, range_start, range_end)
|
self.outputs = self.rangebetween(self.input_list, range_start,
|
||||||
|
range_end)
|
||||||
|
|
||||||
elif str(self.task).startswith("split_"):
|
elif str(self.task).startswith("split_"):
|
||||||
self.outputs = self.split(self.input_list, str(self.task).replace("split_", ""))
|
self.outputs = self.split(self.input_list,
|
||||||
|
str(self.task).replace("split_", ""))
|
||||||
|
|
||||||
elif str(self.task).endswith("!"):
|
elif str(self.task).endswith("!"):
|
||||||
self.outputs = self.exclude(self.input_list, str(self.task).replace("!", ""))
|
self.outputs = self.exclude(self.input_list,
|
||||||
|
str(self.task).replace("!", ""))
|
||||||
|
|
||||||
elif str(self.task).endswith("+"):
|
elif str(self.task).endswith("+"):
|
||||||
self.outputs = self.incrange_plus(self.input_list, str(self.task).replace("+", ""))
|
self.outputs = self.incrange_plus(
|
||||||
|
self.input_list,
|
||||||
|
str(self.task).replace("+", ""))
|
||||||
|
|
||||||
elif str(self.task).endswith("-"):
|
elif str(self.task).endswith("-"):
|
||||||
self.outputs = self.incrange_minus(self.input_list, str(self.task).replace("-", ""))
|
self.outputs = self.incrange_minus(
|
||||||
|
self.input_list,
|
||||||
|
str(self.task).replace("-", ""))
|
||||||
|
|
||||||
elif str(self.task).endswith(">"):
|
elif str(self.task).endswith(">"):
|
||||||
self.outputs = self.excrange_plus(self.input_list, str(self.task).replace(">", ""))
|
self.outputs = self.excrange_plus(
|
||||||
|
self.input_list,
|
||||||
|
str(self.task).replace(">", ""))
|
||||||
|
|
||||||
elif str(self.task).endswith("<"):
|
elif str(self.task).endswith("<"):
|
||||||
self.outputs = self.excrange_minus(self.input_list, str(self.task).replace("<", ""))
|
self.outputs = self.excrange_minus(
|
||||||
|
self.input_list,
|
||||||
|
str(self.task).replace("<", ""))
|
||||||
|
|
||||||
elif hasattr(self, self.task) and not str(self.task).startswith("core_"):
|
elif hasattr(self,
|
||||||
|
self.task) and not str(self.task).startswith("core_"):
|
||||||
self.outputs = eval('self.' + str(self.task) + '(inputs)')
|
self.outputs = eval('self.' + str(self.task) + '(inputs)')
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise Exception(str(self.task) + " is not a valid spicemanip function.")
|
raise Exception(
|
||||||
|
str(self.task) + " is not a valid spicemanip function.")
|
||||||
|
|
||||||
def core_output_handler(self):
|
def core_output_handler(self):
|
||||||
if not self.output_type:
|
if not self.output_type:
|
||||||
@ -120,7 +134,8 @@ class Spicemanip():
|
|||||||
if self.output_type == 'default':
|
if self.output_type == 'default':
|
||||||
if self.task in [
|
if self.task in [
|
||||||
'string', 'number', 'rangebetween', 'exclude', 'random',
|
'string', 'number', 'rangebetween', 'exclude', 'random',
|
||||||
'incrange_plus', 'incrange_minus', 'excrange_plus', 'excrange_minus'
|
'incrange_plus', 'incrange_minus', 'excrange_plus',
|
||||||
|
'excrange_minus'
|
||||||
]:
|
]:
|
||||||
self.output_type = 'string'
|
self.output_type = 'string'
|
||||||
elif self.task in ['count']:
|
elif self.task in ['count']:
|
||||||
@ -251,7 +266,9 @@ class Spicemanip():
|
|||||||
randomselectlist = list(inputs)
|
randomselectlist = list(inputs)
|
||||||
while len(randomselectlist) > 1:
|
while len(randomselectlist) > 1:
|
||||||
random.shuffle(randomselectlist)
|
random.shuffle(randomselectlist)
|
||||||
randomselect = randomselectlist[random.randint(0, len(randomselectlist) - 1)]
|
randomselect = randomselectlist[random.randint(
|
||||||
|
0,
|
||||||
|
len(randomselectlist) - 1)]
|
||||||
randomselectlist.remove(randomselect)
|
randomselectlist.remove(randomselect)
|
||||||
randomselect = randomselectlist[0]
|
randomselect = randomselectlist[0]
|
||||||
return randomselect
|
return randomselect
|
||||||
@ -304,15 +321,11 @@ class Spicemanip():
|
|||||||
return ', '.join(str(x) for x in inputs)
|
return ', '.join(str(x) for x in inputs)
|
||||||
|
|
||||||
|
|
||||||
core = Spicemanip()
|
main = Spicemanip()
|
||||||
|
|
||||||
|
|
||||||
# Hub
|
# Hub
|
||||||
def main(inputs, outputtask, output_type='default'):
|
def core(inputs, outputtask, output_type='default'):
|
||||||
|
|
||||||
# TODO 'this*that' or '1*that' replace either all strings matching, or an index value
|
|
||||||
# TODO reverse sort z.sort(reverse = True)
|
|
||||||
# list.extend adds lists to eachother
|
|
||||||
|
|
||||||
mainoutputtask, suboutputtask = None, None
|
mainoutputtask, suboutputtask = None, None
|
||||||
|
|
||||||
@ -376,13 +389,16 @@ def main(inputs, outputtask, output_type='default'):
|
|||||||
if outputtask == 'string':
|
if outputtask == 'string':
|
||||||
returnvalue = inputs
|
returnvalue = inputs
|
||||||
else:
|
else:
|
||||||
returnvalue = eval('spicemanip_' + outputtask + '(inputs, outputtask, mainoutputtask, suboutputtask)')
|
returnvalue = eval(
|
||||||
|
'spicemanip_' + outputtask +
|
||||||
|
'(inputs, outputtask, mainoutputtask, suboutputtask)')
|
||||||
|
|
||||||
# default return if not specified
|
# default return if not specified
|
||||||
if output_type == 'default':
|
if output_type == 'default':
|
||||||
if outputtask in [
|
if outputtask in [
|
||||||
'string', 'number', 'rangebetween', 'exclude', 'random',
|
'string', 'number', 'rangebetween', 'exclude', 'random',
|
||||||
'incrange_plus', 'incrange_minus', 'excrange_plus', 'excrange_minus'
|
'incrange_plus', 'incrange_minus', 'excrange_plus',
|
||||||
|
'excrange_minus'
|
||||||
]:
|
]:
|
||||||
output_type = 'string'
|
output_type = 'string'
|
||||||
elif outputtask in ['count']:
|
elif outputtask in ['count']:
|
||||||
@ -474,7 +490,9 @@ def spicemanip_random(inputs, outputtask, mainoutputtask, suboutputtask):
|
|||||||
randomselectlist.append(temppart)
|
randomselectlist.append(temppart)
|
||||||
while len(randomselectlist) > 1:
|
while len(randomselectlist) > 1:
|
||||||
random.shuffle(randomselectlist)
|
random.shuffle(randomselectlist)
|
||||||
randomselect = randomselectlist[random.randint(0, len(randomselectlist) - 1)]
|
randomselect = randomselectlist[random.randint(
|
||||||
|
0,
|
||||||
|
len(randomselectlist) - 1)]
|
||||||
randomselectlist.remove(randomselect)
|
randomselectlist.remove(randomselect)
|
||||||
randomselect = randomselectlist[0]
|
randomselect = randomselectlist[0]
|
||||||
return randomselect
|
return randomselect
|
||||||
@ -484,7 +502,8 @@ def spicemanip_random(inputs, outputtask, mainoutputtask, suboutputtask):
|
|||||||
def spicemanip_exrandom(inputs, outputtask, mainoutputtask, suboutputtask):
|
def spicemanip_exrandom(inputs, outputtask, mainoutputtask, suboutputtask):
|
||||||
if not len(inputs):
|
if not len(inputs):
|
||||||
return []
|
return []
|
||||||
randremove = spicemanip_random(inputs, outputtask, mainoutputtask, suboutputtask)
|
randremove = spicemanip_random(inputs, outputtask, mainoutputtask,
|
||||||
|
suboutputtask)
|
||||||
inputs.remove(randremove)
|
inputs.remove(randremove)
|
||||||
return inputs
|
return inputs
|
||||||
|
|
||||||
@ -600,7 +619,8 @@ def spicemanip_rangebetween(inputs, outputtask, mainoutputtask, suboutputtask):
|
|||||||
return ''
|
return ''
|
||||||
mainoutputtask, suboutputtask = int(mainoutputtask), int(suboutputtask)
|
mainoutputtask, suboutputtask = int(mainoutputtask), int(suboutputtask)
|
||||||
if suboutputtask == mainoutputtask:
|
if suboutputtask == mainoutputtask:
|
||||||
return spicemanip_number(inputs, outputtask, mainoutputtask, suboutputtask)
|
return spicemanip_number(inputs, outputtask, mainoutputtask,
|
||||||
|
suboutputtask)
|
||||||
if suboutputtask < mainoutputtask:
|
if suboutputtask < mainoutputtask:
|
||||||
return []
|
return []
|
||||||
if mainoutputtask < 0:
|
if mainoutputtask < 0:
|
||||||
@ -609,35 +629,43 @@ def spicemanip_rangebetween(inputs, outputtask, mainoutputtask, suboutputtask):
|
|||||||
suboutputtask = len(inputs)
|
suboutputtask = len(inputs)
|
||||||
newlist = []
|
newlist = []
|
||||||
for i in range(mainoutputtask, suboutputtask + 1):
|
for i in range(mainoutputtask, suboutputtask + 1):
|
||||||
newlist.append(str(spicemanip_number(inputs, outputtask, i, suboutputtask)))
|
newlist.append(
|
||||||
|
str(spicemanip_number(inputs, outputtask, i, suboutputtask)))
|
||||||
if newlist == []:
|
if newlist == []:
|
||||||
return ''
|
return ''
|
||||||
return ' '.join(newlist)
|
return ' '.join(newlist)
|
||||||
|
|
||||||
|
|
||||||
# Forward Range includes index number
|
# Forward Range includes index number
|
||||||
def spicemanip_incrange_plus(inputs, outputtask, mainoutputtask, suboutputtask):
|
def spicemanip_incrange_plus(inputs, outputtask, mainoutputtask,
|
||||||
|
suboutputtask):
|
||||||
if not len(inputs):
|
if not len(inputs):
|
||||||
return ''
|
return ''
|
||||||
return spicemanip_rangebetween(inputs, outputtask, int(mainoutputtask), len(inputs))
|
return spicemanip_rangebetween(inputs, outputtask, int(mainoutputtask),
|
||||||
|
len(inputs))
|
||||||
|
|
||||||
|
|
||||||
# Reverse Range includes index number
|
# Reverse Range includes index number
|
||||||
def spicemanip_incrange_minus(inputs, outputtask, mainoutputtask, suboutputtask):
|
def spicemanip_incrange_minus(inputs, outputtask, mainoutputtask,
|
||||||
|
suboutputtask):
|
||||||
if not len(inputs):
|
if not len(inputs):
|
||||||
return ''
|
return ''
|
||||||
return spicemanip_rangebetween(inputs, outputtask, 1, int(mainoutputtask))
|
return spicemanip_rangebetween(inputs, outputtask, 1, int(mainoutputtask))
|
||||||
|
|
||||||
|
|
||||||
# Forward Range excludes index number
|
# Forward Range excludes index number
|
||||||
def spicemanip_excrange_plus(inputs, outputtask, mainoutputtask, suboutputtask):
|
def spicemanip_excrange_plus(inputs, outputtask, mainoutputtask,
|
||||||
|
suboutputtask):
|
||||||
if not len(inputs):
|
if not len(inputs):
|
||||||
return ''
|
return ''
|
||||||
return spicemanip_rangebetween(inputs, outputtask, int(mainoutputtask) + 1, len(inputs))
|
return spicemanip_rangebetween(inputs, outputtask,
|
||||||
|
int(mainoutputtask) + 1, len(inputs))
|
||||||
|
|
||||||
|
|
||||||
# Reverse Range excludes index number
|
# Reverse Range excludes index number
|
||||||
def spicemanip_excrange_minus(inputs, outputtask, mainoutputtask, suboutputtask):
|
def spicemanip_excrange_minus(inputs, outputtask, mainoutputtask,
|
||||||
|
suboutputtask):
|
||||||
if not len(inputs):
|
if not len(inputs):
|
||||||
return ''
|
return ''
|
||||||
return spicemanip_rangebetween(inputs, outputtask, 1, int(mainoutputtask) - 1)
|
return spicemanip_rangebetween(inputs, outputtask, 1,
|
||||||
|
int(mainoutputtask) - 1)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user