Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ArrayList(object):
- """Mimics the functioning of a SP ArrayList, use it to send as parameter.
- Creates a dynamic global cell array. While slower than a normal array,
- it can be used globally AND dynamically, which is otherwise impossible.\n
- You can send a list as parameter, make sure it meets
- the rules of a SP Arraylist. Send it at your own risk.
- """
- def __init__(self, array: list = []):
- if type(array) is not list:
- raise TypeError('array must be list type')
- self.__items = array.copy()
- @property
- def Length(self) -> int:
- return len(self.__items)
- def Clear(self):
- """Clears an array of all entries. This is the same as Resize(0)."""
- del self.__items[0:]
- def Resize(self, size: int):
- """Resizes an array. If the size is smaller than the current size, the
- array is truncated. If the size is larger than the current size,
- the data at the additional indexes will be None.
- """
- if type(size) is not int:
- raise TypeError('size must be int type')
- if size > len(self.__items):
- self.__items += [None for x in range(size - len(self.__items))]
- elif size < len(self.__items):
- del self.__items[size:]
- def Push(self, val: (int, float, bool, str)) -> int:
- """Pushes a value onto the end of an array, adding a new index.\n
- return value: int Index of the new entry.
- """
- if type(val) not in (int, float, bool, str):
- raise TypeError('val must be int, float, bool or str len 1 type')
- if type(val) is str and len(val) > 1:
- raise TypeError('val must be int, float, bool or str len 1 type')
- self.__items.append(val)
- return len(self.__items) - 1
- def PushString(self, string: str) -> int:
- """Pushes a string onto the end of an array.\nreturn value: int index of the new entry."""
- if type(string) is not str:
- raise TypeError('string must be str type')
- self.__items.append(string)
- return len(self.__items) - 1
- def PushArray(self, array: list) -> int:
- """Pushes an array of cells onto the end of an array. The cells
- are pushed as a block (i.e. the entire array sits at the index),
- rather than pushing each cell individually.\n
- return value: int Index of the new entry.
- """
- if type(array) is not list:
- raise TypeError('array must be list type')
- if type(array[0]) not in (int, float, str, bool):
- raise TypeError('all values in list must be int, float, bool or str len 1 type')
- if not all(type(array[0]) == type(x) for x in array):
- raise TypeError('all values in list must have the same type')
- if type(array[0]) == str:
- if not all(len(x) == 1 for x in array):
- raise TypeError('all values in list must be int, float, bool or str len 1 type')
- self.__items.append(array)
- return len(self.__items) - 1
- def Get(self, index: int, block: int = None) -> (int, float, bool, str):
- """Retrieves a cell value from an array."""
- if type(index) is not int:
- raise TypeError('index must be int type')
- if block is not None and type(block) is not int:
- raise TypeError('block must be int type')
- if block is None:
- if type(self.__items[index]) is list:
- raise TypeError('value at index ' + str(index) + ' in ArrayList is not a cell')
- return self.__items[index]
- else:
- if type(self.__items[index][block]) is list:
- raise TypeError('value at index ' + str(index) + ', block '+ str(block) + ' in ArrayList is not a cell')
- return self.__items[index][block]
- def GetString(self, index: int) -> str:
- """Retrieves a string value from an array."""
- if type(self.__items[index]) is not str:
- raise TypeError('value at index ' + str(index) + ' in ArrayList is not a string')
- return self.__items[index]
- def GetArray(self, index: int) -> list:
- """Retrieves an array of cells from an array."""
- if type(self.__items[index]) is not list:
- raise TypeError('value at index ' + str(index) + ' in ArrayList is not an array')
- return self.__items[index]
- def Set(self, index: int, val: (int, float, bool, str), block: int = None):
- """Sets a cell value in an array."""
- if type(index) is not int:
- raise TypeError('index must be int type')
- if block is not None and type(block) is not int:
- raise TypeError('block must be int type')
- if type(val) not in (int, float, bool, str):
- raise TypeError('val must be int, float, bool or str len 1 type')
- if type(val) is str and len(val) > 1:
- raise TypeError('val must be int, float, bool or str len 1 type')
- if block is None:
- self.__items[index] = val
- else:
- self.__items[index][block] = val
- def SetString(self, index: int, string: str):
- """Sets a string value in an array."""
- if type(index) is not int:
- raise TypeError('index must be int type')
- if type(string) is not str:
- raise TypeError('string must be str type')
- self.__items[index] = string
- def SetArray(self, index: int, array: list):
- """Sets an array of cells in an array."""
- if type(index) is not int:
- raise TypeError('index must be int type')
- if type(array) is not list:
- raise TypeError('array must be list type')
- if type(array[0]) not in (int, float, str, bool):
- raise TypeError('all values in list must be int, float, bool or str len 1 type')
- if not all(type(array[0]) == type(x) for x in array):
- raise TypeError('all values in list must have the same type')
- if type(array[0]) is str:
- if not all(len(x) == 1 for x in array):
- raise TypeError('all values in list must be int, float, bool or str len 1 type')
- self.__items[index] = array
- def ShiftUp(self, index: int):
- """Shifts an array up. All array contents after and including the given
- index are shifted up by one, and the given index is then "free."
- After shifting, the contents of the given index is None.
- """
- if type(index) is not int:
- raise TypeError('index must be int type')
- self.__items.insert(index, None)
- def Erase(self, index: int):
- """Removes an array index, shifting the entire array down from that position on."""
- if type(index) is not int:
- raise TypeError('index must be int type')
- del self.__items[index]
- def SwapAt(self, index1: int, index2: int):
- """Swaps two items in the array."""
- if type(index1) is not int:
- raise TypeError('index1 must be int type')
- if type(index2) is not int:
- raise TypeError('index2 must be int type')
- self.__items[index1], self.__items[index2] = self.__items[index2], self.__items[index1]
- def FindString(self, string: str) -> int:
- """Returns the index for the first occurrence of the provided string.
- If the string cannot be located, -1 will be returned.
- """
- if type(string) is not str:
- raise TypeError('string must be str type')
- try:
- pos = self.__items.index(string)
- return pos
- except ValueError:
- return -1
- def FindValue(self, val: (int, float, bool, str), block: int = 0) -> int:
- """Returns the index for the first occurrence of the provided value.
- If the value cannot be located, -1 will be returned.
- """
- if type(val) not in (int, float, bool, str):
- raise TypeError('val must be int, float, bool or str len 1 type')
- if type(block) is not int:
- raise TypeError('block must be int type')
- try:
- pos = self.__items.index(val)
- return pos
- except ValueError:
- return -1
- def _get_list(self) -> list:
- """Returns the internal object list"""
- return self.__items
- def _set_list(self, array: list):
- """Set the internal object list"""
- self.__items = array
- def __str__(self):
- return 'ArrayList(' + str(self.__items) + ')'
- def __len__(self):
- return len(self.__items)
- def copy(self):
- return ArrayList(self.__items)
- class DataPack(object):
- """Mimics the functioning of a SP DataPack, use it to send as parameter to SP.
- Creates a new data pack.\n
- You can send a list and a position as parameters, make sure it meets
- the rules of a SP DataPack. Send it at your own risk.
- """
- def __init__(self, array: list = [], position: int = 0):
- self.__items = array.copy()
- self.Position = position
- def WriteCell(self, val: (int, bool, str), insert: bool = False):
- """Packs a normal cell into a data pack."""
- if type(val) not in (int, bool, str):
- raise TypeError('val must be int, float, bool or str len 1 type')
- if type(val) is str and len(val) > 1:
- raise TypeError('val must be int, float, bool or str len 1 type')
- if len(self.__items) == self.Position:
- self.__items.append(val)
- else:
- if insert:
- self.__items.insert(self.Position, val)
- else:
- self.__items[self.Position] = val
- self.Position += 1
- def WriteFloat(self, val: float, insert: bool = False):
- """Packs a float into a data pack."""
- if type(val) is not float:
- raise TypeError('val must be float type')
- if len(self.__items) == self.Position:
- self.__items.append(val)
- else:
- if insert:
- self.__items.insert(self.Position, val)
- else:
- self.__items[self.Position] = val
- self.Position += 1
- def WriteString(self, val: str, insert: bool = False):
- """Packs a float into a data pack."""
- if type(val) is not str:
- raise TypeError('val must be str type')
- if len(self.__items) == self.Position:
- self.__items.append(val)
- else:
- if insert:
- self.__items.insert(self.Position, val)
- else:
- self.__items[self.Position] = val
- self.Position += 1
- def WriteFunction(self, function, insert: bool = False):
- """Packs a function pointer into a data pack."""
- if len(self.__items) == self.Position:
- self.__items.append(function)
- else:
- if insert:
- self.__items.insert(self.Position, function)
- else:
- self.__items[self.Position] = function
- self.Position += 1
- def WriteCellArray(self, array: list, insert: bool = False):
- """Packs an array of cells into a data pack."""
- if type(array) is not list:
- raise TypeError('array must be list type')
- if type(array[0]) not in (int, str, bool):
- raise TypeError('all values in list must be int, bool or str len 1 type')
- if not all(type(array[0]) == type(x) for x in array):
- raise TypeError('all values in list must have the same type')
- if type(array[0]) == str:
- if not all(len(x) == 1 for x in array):
- raise TypeError('all values in list must be int, bool or str len 1 type')
- if len(self.__items) == self.Position:
- self.__items.append(array)
- else:
- if insert:
- self.__items.insert(self.Position, array)
- else:
- self.__items[self.Position] = array
- self.Position += 1
- def WriteFloatArray(self, array: list, insert: bool = False):
- """Packs an array of floats into a data pack."""
- if type(array) is not list:
- raise TypeError('array must be list type')
- if not all(type(x) == float for x in array):
- raise TypeError('all values in list must be float type')
- if len(self.__items) == self.Position:
- self.__items.append(array)
- else:
- if insert:
- self.__items.insert(self.Position, array)
- else:
- self.__items[self.Position] = array
- self.Position += 1
- def ReadCell(self) -> (int, bool, str):
- """Reads a cell from a data pack."""
- if type(self.__items[self.Position]) not in (int, bool, str):
- raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not int, bool or str 1 len type')
- if type(self.__items[self.Position]) is str and len(self.__items[self.Position]) > 1:
- raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not int, bool or str 1 len type')
- self.Position += 1
- return self.__items[self.Position - 1]
- def ReadFloat(self) -> float:
- """Reads a float from a data pack."""
- if type(self.__items[self.Position]) is not float:
- raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not float type')
- self.Position += 1
- return self.__items[self.Position - 1]
- def ReadString(self) -> str:
- """Reads a string from a data pack."""
- if type(self.__items[self.Position]) is not str:
- raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not str type')
- self.Position += 1
- return self.__items[self.Position - 1]
- def ReadFunction(self):
- """Reads a function pointer from a data pack."""
- self.Position += 1
- return self.__items[self.Position - 1]
- def ReadCellArray(self) -> list:
- """Reads an array of cells from a data pack."""
- if type(self.__items[self.Position]) is not list:
- raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not cell array type')
- if not all(type(x) is not float for x in self.__items[self.Position]):
- raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not cell array type')
- self.Position += 1
- return self.__items[self.Position - 1]
- def ReadFloatArray(self) -> list:
- """Reads an array of floats from a data pack."""
- if type(self.__items[self.Position]) is not list:
- raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not float array type')
- if not all(type(x) is float for x in self.__items[self.Position]):
- raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not float array type')
- self.Position += 1
- return self.__items[self.Position - 1]
- def Reset(self, clear: bool = False):
- """Resets the position in a data pack."""
- self.Position = 0
- if clear:
- self.__items = list()
- def _get_list(self) -> list:
- """Returns the internal object list"""
- return self.__items
- def _set_list(self, array: list):
- """Set the internal object list"""
- self.__items = array
- def __str__(self):
- return 'DataPack(' + str(self.__items) + ')'
- def __len__(self):
- return len(self.__items)
- def copy(self):
- return DataPack(self.__items, self.Position)
- class StringMap(object):
- """Mimics the functioning of a SP StringMap, use it to send as parameter.
- Creates a hash map. A hash map is a container that can map strings (called
- "keys") to arbitrary values (cells, arrays, or strings). Keys in a hash map
- are unique. That is, there is at most one entry in the map for a given key.\n
- You can send a dictionary parameter, make sure it meets
- the rules of a SP StringMap. Send it at your own risk.
- """
- def __init__(self, hashmap: dict = {}):
- self.__items = hashmap.copy()
- @property
- def Size(self) -> int:
- return len(self.__items)
- def SetValue(self, key: str, val: (int, float, bool, str), replace: bool = True) -> bool:
- """Sets a value in a hash map, either inserting a new entry or replacing an old one.\n
- return value: bool True on success, false on failure.
- """
- if type(key) is not str:
- raise TypeError('key must be str type')
- if type(val) not in (int, float, bool, str):
- raise TypeError('val must be int, float, bool or str len 1 type')
- if type(val) is str and len(val) > 1:
- raise TypeError('val must be int, float, bool or str len 1 type')
- if replace:
- self.__items[key] = val
- return True
- else:
- if key in self.__items:
- return False
- else:
- self.__items[key] = val
- return True
- def SetArray(self, key: str, array: list, replace: bool = True) -> bool:
- """Sets an array value in a Map, either inserting a new entry or replacing an old one.\n
- return value: bool True on success, false on failure.
- """
- if type(key) is not str:
- raise TypeError('key must be str type')
- if type(array) is not list:
- raise TypeError('array must be list type')
- if type(array[0]) not in (int, float, str, bool):
- raise TypeError('all values in list must be int, float, bool or str len 1 type')
- if not all(type(array[0]) == type(x) for x in array):
- raise TypeError('all values in list must have the same type')
- if type(array[0]) == str:
- if not all(len(x) == 1 for x in array):
- raise TypeError('all values in list must be int, float, bool or str len 1 type')
- if replace:
- self.__items[key] = array
- return True
- else:
- if key in self.__items:
- return False
- else:
- self.__items[key] = array
- return True
- def SetString(self, key: str, string: str, replace: bool = True) -> bool:
- """Sets a string value in a Map, either inserting a new entry or replacing an old one.\n
- return value: bool True on success, false on failure.
- """
- if type(key) is not str:
- raise TypeError('key must be str type')
- if type(string) is not str:
- raise TypeError('string must be str type')
- if replace:
- self.__items[key] = string
- return True
- else:
- if key in self.__items:
- return False
- else:
- self.__items[key] = string
- return True
- def GetValue(self, key: str):
- """Retrieves a value in a Map.
- return value: any value and bool True on success, 0 and false on failure.
- """
- if type(key) is not str:
- raise TypeError('key must be str type')
- if key not in self.__items:
- return 0, False
- if type(self.__items[key]) in (list, str):
- TypeError('value at key \'' + key + '\' in StringMap is not a cell')
- return self.__items[key], True
- def GetArray(self, key: str):
- """Retrieves an array in a Map.
- return value: array and bool True on success, [] and false on failure.
- """
- if type(key) is not str:
- raise TypeError('key must be str type')
- if key not in self.__items:
- return [], False
- if type(self.__items[key]) is not list:
- raise TypeError('value at key \'' + key + '\' in StringMap is not an array')
- return self.__items[key], True
- def GetString(self, key: str):
- """Retrieves a string in a Map.
- return value: string and bool True on success, 0 and false on failure.
- """
- if type(key) is not str:
- raise TypeError('key must be str type')
- if key not in self.__items:
- return 0, False
- if type(self.__items[key]) is not str:
- TypeError('value at key \'' + key + '\' in StringMap is not a string')
- return self.__items[key], True
- def Remove(self, key: str) -> bool:
- """Removes a key entry from a Map.
- return value: bool True on success, false if the value was never set.
- """
- if type(key) is not str:
- raise TypeError('key must be str type')
- if key not in self.__items:
- return False
- else:
- del self.__items[key]
- def Snapshot(self) -> list:
- """Create a snapshot of the map's keys. See StringMapSnapshot."""
- return list(self.__items.keys())
- def Clear(self):
- """Clears all entries from a Map."""
- self.__items.clear()
- def _get_dict(self) -> dict:
- """Returns the internal object dict"""
- return self.__items
- def _set_dict(self, array: dict):
- """Set the internal object dict"""
- self.__items = array
- def __str__(self):
- return 'StringMap(' + str(self.__items) + ')'
- def __len__(self):
- return len(self.__items)
- def copy(self):
- return StringMap(self.__items)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement