Advertisement
misdocumeno

Untitled

Nov 25th, 2020
646
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 21.38 KB | None | 0 0
  1. class ArrayList(object):
  2.     """Mimics the functioning of a SP ArrayList, use it to send as parameter.
  3.    Creates a dynamic global cell array. While slower than a normal array,
  4.    it can be used globally AND dynamically, which is otherwise impossible.\n
  5.    You can send a list as parameter, make sure it meets
  6.    the rules of a SP Arraylist. Send it at your own risk.
  7.    """
  8.     def __init__(self, array: list = []):
  9.         if type(array) is not list:
  10.             raise TypeError('array must be list type')
  11.         self.__items = array.copy()
  12.  
  13.     @property
  14.     def Length(self) -> int:
  15.         return len(self.__items)
  16.  
  17.     def Clear(self):
  18.         """Clears an array of all entries. This is the same as Resize(0)."""
  19.         del self.__items[0:]
  20.  
  21.     def Resize(self, size: int):
  22.         """Resizes an array. If the size is smaller than the current size, the
  23.        array is truncated. If the size is larger than the current size,
  24.        the data at the additional indexes will be None.
  25.        """
  26.         if type(size) is not int:
  27.             raise TypeError('size must be int type')
  28.         if size > len(self.__items):
  29.             self.__items += [None for x in range(size - len(self.__items))]
  30.         elif size < len(self.__items):
  31.             del self.__items[size:]
  32.  
  33.     def Push(self, val: (int, float, bool, str)) -> int:
  34.         """Pushes a value onto the end of an array, adding a new index.\n
  35.        return value: int Index of the new entry.
  36.        """
  37.         if type(val) not in (int, float, bool, str):
  38.             raise TypeError('val must be int, float, bool or str len 1 type')
  39.         if type(val) is str and len(val) > 1:
  40.             raise TypeError('val must be int, float, bool or str len 1 type')
  41.         self.__items.append(val)
  42.         return len(self.__items) - 1
  43.  
  44.     def PushString(self, string: str) -> int:
  45.         """Pushes a string onto the end of an array.\nreturn value: int index of the new entry."""
  46.         if type(string) is not str:
  47.             raise TypeError('string must be str type')
  48.         self.__items.append(string)
  49.         return len(self.__items) - 1
  50.  
  51.     def PushArray(self, array: list) -> int:
  52.         """Pushes an array of cells onto the end of an array.  The cells
  53.        are pushed as a block (i.e. the entire array sits at the index),
  54.        rather than pushing each cell individually.\n
  55.        return value: int Index of the new entry.
  56.        """
  57.         if type(array) is not list:
  58.             raise TypeError('array must be list type')
  59.         if type(array[0]) not in (int, float, str, bool):
  60.             raise TypeError('all values in list must be int, float, bool or str len 1 type')
  61.         if not all(type(array[0]) == type(x) for x in array):
  62.             raise TypeError('all values in list must have the same type')
  63.         if type(array[0]) == str:
  64.             if not all(len(x) == 1 for x in array):
  65.                 raise TypeError('all values in list must be int, float, bool or str len 1 type')
  66.         self.__items.append(array)
  67.         return len(self.__items) - 1
  68.  
  69.     def Get(self, index: int, block: int = None) -> (int, float, bool, str):
  70.         """Retrieves a cell value from an array."""
  71.         if type(index) is not int:
  72.             raise TypeError('index must be int type')
  73.         if block is not None and type(block) is not int:
  74.             raise TypeError('block must be int type')
  75.         if block is None:
  76.             if type(self.__items[index]) is list:
  77.                 raise TypeError('value at index ' + str(index) + ' in ArrayList is not a cell')
  78.             return self.__items[index]
  79.         else:
  80.             if type(self.__items[index][block]) is list:
  81.                 raise TypeError('value at index ' + str(index) + ', block '+ str(block) + ' in ArrayList is not a cell')
  82.             return self.__items[index][block]
  83.  
  84.     def GetString(self, index: int) -> str:
  85.         """Retrieves a string value from an array."""
  86.         if type(self.__items[index]) is not str:
  87.             raise TypeError('value at index ' + str(index) + ' in ArrayList is not a string')
  88.         return self.__items[index]
  89.  
  90.     def GetArray(self, index: int) -> list:
  91.         """Retrieves an array of cells from an array."""
  92.         if type(self.__items[index]) is not list:
  93.             raise TypeError('value at index ' + str(index) + ' in ArrayList is not an array')
  94.         return self.__items[index]
  95.  
  96.     def Set(self, index: int, val: (int, float, bool, str), block: int = None):
  97.         """Sets a cell value in an array."""
  98.         if type(index) is not int:
  99.             raise TypeError('index must be int type')
  100.         if block is not None and type(block) is not int:
  101.             raise TypeError('block must be int type')
  102.         if type(val) not in (int, float, bool, str):
  103.             raise TypeError('val must be int, float, bool or str len 1 type')
  104.         if type(val) is str and len(val) > 1:
  105.             raise TypeError('val must be int, float, bool or str len 1 type')
  106.         if block is None:
  107.             self.__items[index] = val
  108.         else:
  109.             self.__items[index][block] = val
  110.  
  111.     def SetString(self, index: int, string: str):
  112.         """Sets a string value in an array."""
  113.         if type(index) is not int:
  114.             raise TypeError('index must be int type')
  115.         if type(string) is not str:
  116.             raise TypeError('string must be str type')
  117.         self.__items[index] = string
  118.  
  119.     def SetArray(self, index: int, array: list):
  120.         """Sets an array of cells in an array."""
  121.         if type(index) is not int:
  122.             raise TypeError('index must be int type')
  123.         if type(array) is not list:
  124.             raise TypeError('array must be list type')
  125.         if type(array[0]) not in (int, float, str, bool):
  126.             raise TypeError('all values in list must be int, float, bool or str len 1 type')
  127.         if not all(type(array[0]) == type(x) for x in array):
  128.             raise TypeError('all values in list must have the same type')
  129.         if type(array[0]) is str:
  130.             if not all(len(x) == 1 for x in array):
  131.                 raise TypeError('all values in list must be int, float, bool or str len 1 type')
  132.         self.__items[index] = array
  133.  
  134.     def ShiftUp(self, index: int):
  135.         """Shifts an array up. All array contents after and including the given
  136.        index are shifted up by one, and the given index is then "free."
  137.        After shifting, the contents of the given index is None.
  138.        """
  139.         if type(index) is not int:
  140.             raise TypeError('index must be int type')
  141.         self.__items.insert(index, None)
  142.  
  143.     def Erase(self, index: int):
  144.         """Removes an array index, shifting the entire array down from that position on."""
  145.         if type(index) is not int:
  146.             raise TypeError('index must be int type')
  147.         del self.__items[index]
  148.  
  149.     def SwapAt(self, index1: int, index2: int):
  150.         """Swaps two items in the array."""
  151.         if type(index1) is not int:
  152.             raise TypeError('index1 must be int type')
  153.         if type(index2) is not int:
  154.             raise TypeError('index2 must be int type')
  155.         self.__items[index1], self.__items[index2] = self.__items[index2], self.__items[index1]
  156.  
  157.     def FindString(self, string: str) -> int:
  158.         """Returns the index for the first occurrence of the provided string.
  159.        If the string cannot be located, -1 will be returned.
  160.        """
  161.         if type(string) is not str:
  162.             raise TypeError('string must be str type')
  163.         try:
  164.             pos = self.__items.index(string)
  165.             return pos
  166.         except ValueError:
  167.             return -1
  168.  
  169.     def FindValue(self, val: (int, float, bool, str), block: int = 0) -> int:
  170.         """Returns the index for the first occurrence of the provided value.
  171.        If the value cannot be located, -1 will be returned.
  172.        """
  173.         if type(val) not in (int, float, bool, str):
  174.             raise TypeError('val must be int, float, bool or str len 1 type')
  175.         if type(block) is not int:
  176.             raise TypeError('block must be int type')
  177.         try:
  178.             pos = self.__items.index(val)
  179.             return pos
  180.         except ValueError:
  181.             return -1
  182.  
  183.     def _get_list(self) -> list:
  184.         """Returns the internal object list"""
  185.         return self.__items
  186.  
  187.     def _set_list(self, array: list):
  188.         """Set the internal object list"""
  189.         self.__items = array
  190.  
  191.     def __str__(self):
  192.         return 'ArrayList(' + str(self.__items) + ')'
  193.  
  194.     def __len__(self):
  195.         return len(self.__items)
  196.  
  197.     def copy(self):
  198.         return ArrayList(self.__items)
  199.  
  200.  
  201. class DataPack(object):
  202.     """Mimics the functioning of a SP DataPack, use it to send as parameter to SP.
  203.    Creates a new data pack.\n
  204.    You can send a list and a position as parameters, make sure it meets
  205.    the rules of a SP DataPack. Send it at your own risk.
  206.    """
  207.     def __init__(self, array: list = [], position: int = 0):
  208.         self.__items = array.copy()
  209.         self.Position = position
  210.  
  211.     def WriteCell(self, val: (int, bool, str), insert: bool = False):
  212.         """Packs a normal cell into a data pack."""
  213.         if type(val) not in (int, bool, str):
  214.             raise TypeError('val must be int, float, bool or str len 1 type')
  215.         if type(val) is str and len(val) > 1:
  216.             raise TypeError('val must be int, float, bool or str len 1 type')
  217.         if len(self.__items) == self.Position:
  218.             self.__items.append(val)
  219.         else:
  220.             if insert:
  221.                 self.__items.insert(self.Position, val)
  222.             else:
  223.                 self.__items[self.Position] = val
  224.         self.Position += 1
  225.  
  226.     def WriteFloat(self, val: float, insert: bool = False):
  227.         """Packs a float into a data pack."""
  228.         if type(val) is not float:
  229.             raise TypeError('val must be float type')
  230.         if len(self.__items) == self.Position:
  231.             self.__items.append(val)
  232.         else:
  233.             if insert:
  234.                 self.__items.insert(self.Position, val)
  235.             else:
  236.                 self.__items[self.Position] = val
  237.         self.Position += 1
  238.  
  239.     def WriteString(self, val: str, insert: bool = False):
  240.         """Packs a float into a data pack."""
  241.         if type(val) is not str:
  242.             raise TypeError('val must be str type')
  243.         if len(self.__items) == self.Position:
  244.             self.__items.append(val)
  245.         else:
  246.             if insert:
  247.                 self.__items.insert(self.Position, val)
  248.             else:
  249.                 self.__items[self.Position] = val
  250.         self.Position += 1
  251.  
  252.     def WriteFunction(self, function, insert: bool = False):
  253.         """Packs a function pointer into a data pack."""
  254.         if len(self.__items) == self.Position:
  255.             self.__items.append(function)
  256.         else:
  257.             if insert:
  258.                 self.__items.insert(self.Position, function)
  259.             else:
  260.                 self.__items[self.Position] = function
  261.         self.Position += 1
  262.  
  263.     def WriteCellArray(self, array: list, insert: bool = False):
  264.         """Packs an array of cells into a data pack."""
  265.         if type(array) is not list:
  266.             raise TypeError('array must be list type')
  267.         if type(array[0]) not in (int, str, bool):
  268.             raise TypeError('all values in list must be int, bool or str len 1 type')
  269.         if not all(type(array[0]) == type(x) for x in array):
  270.             raise TypeError('all values in list must have the same type')
  271.         if type(array[0]) == str:
  272.             if not all(len(x) == 1 for x in array):
  273.                 raise TypeError('all values in list must be int, bool or str len 1 type')
  274.         if len(self.__items) == self.Position:
  275.             self.__items.append(array)
  276.         else:
  277.             if insert:
  278.                 self.__items.insert(self.Position, array)
  279.             else:
  280.                 self.__items[self.Position] = array
  281.         self.Position += 1
  282.  
  283.     def WriteFloatArray(self, array: list, insert: bool = False):
  284.         """Packs an array of floats into a data pack."""
  285.         if type(array) is not list:
  286.             raise TypeError('array must be list type')
  287.         if not all(type(x) == float for x in array):
  288.             raise TypeError('all values in list must be float type')
  289.         if len(self.__items) == self.Position:
  290.             self.__items.append(array)
  291.         else:
  292.             if insert:
  293.                 self.__items.insert(self.Position, array)
  294.             else:
  295.                 self.__items[self.Position] = array
  296.         self.Position += 1
  297.  
  298.     def ReadCell(self) -> (int, bool, str):
  299.         """Reads a cell from a data pack."""
  300.         if type(self.__items[self.Position]) not in (int, bool, str):
  301.             raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not int, bool or str 1 len type')
  302.         if type(self.__items[self.Position]) is str and len(self.__items[self.Position]) > 1:
  303.             raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not int, bool or str 1 len type')
  304.         self.Position += 1
  305.         return self.__items[self.Position - 1]
  306.  
  307.     def ReadFloat(self) -> float:
  308.         """Reads a float from a data pack."""
  309.         if type(self.__items[self.Position]) is not float:
  310.             raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not float type')
  311.         self.Position += 1
  312.         return self.__items[self.Position - 1]
  313.  
  314.     def ReadString(self) -> str:
  315.         """Reads a string from a data pack."""
  316.         if type(self.__items[self.Position]) is not str:
  317.             raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not str type')
  318.         self.Position += 1
  319.         return self.__items[self.Position - 1]
  320.  
  321.     def ReadFunction(self):
  322.         """Reads a function pointer from a data pack."""
  323.         self.Position += 1
  324.         return self.__items[self.Position - 1]
  325.  
  326.     def ReadCellArray(self) -> list:
  327.         """Reads an array of cells from a data pack."""
  328.         if type(self.__items[self.Position]) is not list:
  329.             raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not cell array type')
  330.         if not all(type(x) is not float for x in self.__items[self.Position]):
  331.             raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not cell array type')
  332.         self.Position += 1
  333.         return self.__items[self.Position - 1]
  334.  
  335.     def ReadFloatArray(self) -> list:
  336.         """Reads an array of floats from a data pack."""
  337.         if type(self.__items[self.Position]) is not list:
  338.             raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not float array type')
  339.         if not all(type(x) is float for x in self.__items[self.Position]):
  340.             raise TypeError('value at position ' + str(self.Position) + ' in DataPack is not float array type')
  341.         self.Position += 1
  342.         return self.__items[self.Position - 1]
  343.  
  344.     def Reset(self, clear: bool = False):
  345.         """Resets the position in a data pack."""
  346.         self.Position = 0
  347.         if clear:
  348.             self.__items = list()
  349.  
  350.     def _get_list(self) -> list:
  351.         """Returns the internal object list"""
  352.         return self.__items
  353.  
  354.     def _set_list(self, array: list):
  355.         """Set the internal object list"""
  356.         self.__items = array
  357.  
  358.     def __str__(self):
  359.         return 'DataPack(' + str(self.__items) + ')'
  360.  
  361.     def __len__(self):
  362.         return len(self.__items)
  363.  
  364.     def copy(self):
  365.         return DataPack(self.__items, self.Position)
  366.  
  367.  
  368. class StringMap(object):
  369.     """Mimics the functioning of a SP StringMap, use it to send as parameter.
  370.    Creates a hash map. A hash map is a container that can map strings (called
  371.    "keys") to arbitrary values (cells, arrays, or strings). Keys in a hash map
  372.    are unique. That is, there is at most one entry in the map for a given key.\n
  373.    You can send a dictionary parameter, make sure it meets
  374.    the rules of a SP StringMap. Send it at your own risk.
  375.    """
  376.     def __init__(self, hashmap: dict = {}):
  377.         self.__items = hashmap.copy()
  378.  
  379.     @property
  380.     def Size(self) -> int:
  381.         return len(self.__items)
  382.  
  383.     def SetValue(self, key: str, val: (int, float, bool, str), replace: bool = True) -> bool:
  384.         """Sets a value in a hash map, either inserting a new entry or replacing an old one.\n
  385.        return value: bool True on success, false on failure.
  386.        """
  387.         if type(key) is not str:
  388.             raise TypeError('key must be str type')
  389.         if type(val) not in (int, float, bool, str):
  390.             raise TypeError('val must be int, float, bool or str len 1 type')
  391.         if type(val) is str and len(val) > 1:
  392.             raise TypeError('val must be int, float, bool or str len 1 type')
  393.         if replace:
  394.             self.__items[key] = val
  395.             return True
  396.         else:
  397.             if key in self.__items:
  398.                 return False
  399.             else:
  400.                 self.__items[key] = val
  401.                 return True
  402.  
  403.     def SetArray(self, key: str, array: list, replace: bool = True) -> bool:
  404.         """Sets an array value in a Map, either inserting a new entry or replacing an old one.\n
  405.        return value: bool True on success, false on failure.
  406.        """
  407.         if type(key) is not str:
  408.             raise TypeError('key must be str type')
  409.         if type(array) is not list:
  410.             raise TypeError('array must be list type')
  411.         if type(array[0]) not in (int, float, str, bool):
  412.             raise TypeError('all values in list must be int, float, bool or str len 1 type')
  413.         if not all(type(array[0]) == type(x) for x in array):
  414.             raise TypeError('all values in list must have the same type')
  415.         if type(array[0]) == str:
  416.             if not all(len(x) == 1 for x in array):
  417.                 raise TypeError('all values in list must be int, float, bool or str len 1 type')
  418.         if replace:
  419.             self.__items[key] = array
  420.             return True
  421.         else:
  422.             if key in self.__items:
  423.                 return False
  424.             else:
  425.                 self.__items[key] = array
  426.                 return True
  427.  
  428.     def SetString(self, key: str, string: str, replace: bool = True) -> bool:
  429.         """Sets a string value in a Map, either inserting a new entry or replacing an old one.\n
  430.        return value: bool True on success, false on failure.
  431.        """
  432.         if type(key) is not str:
  433.             raise TypeError('key must be str type')
  434.         if type(string) is not str:
  435.             raise TypeError('string must be str type')
  436.         if replace:
  437.             self.__items[key] = string
  438.             return True
  439.         else:
  440.             if key in self.__items:
  441.                 return False
  442.             else:
  443.                 self.__items[key] = string
  444.                 return True
  445.  
  446.     def GetValue(self, key: str):
  447.         """Retrieves a value in a Map.
  448.        return value: any value and bool True on success, 0 and false on failure.
  449.        """
  450.         if type(key) is not str:
  451.             raise TypeError('key must be str type')
  452.         if key not in self.__items:
  453.             return 0, False
  454.         if type(self.__items[key]) in (list, str):
  455.             TypeError('value at key \'' + key + '\' in StringMap is not a cell')
  456.         return self.__items[key], True
  457.  
  458.     def GetArray(self, key: str):
  459.         """Retrieves an array in a Map.
  460.        return value: array and bool True on success, [] and false on failure.
  461.        """
  462.         if type(key) is not str:
  463.             raise TypeError('key must be str type')
  464.         if key not in self.__items:
  465.             return [], False
  466.         if type(self.__items[key]) is not list:
  467.             raise TypeError('value at key \'' + key + '\' in StringMap is not an array')
  468.         return self.__items[key], True
  469.  
  470.     def GetString(self, key: str):
  471.         """Retrieves a string in a Map.
  472.        return value: string and bool True on success, 0 and false on failure.
  473.        """
  474.         if type(key) is not str:
  475.             raise TypeError('key must be str type')
  476.         if key not in self.__items:
  477.             return 0, False
  478.         if type(self.__items[key]) is not str:
  479.             TypeError('value at key \'' + key + '\' in StringMap is not a string')
  480.         return self.__items[key], True
  481.  
  482.     def Remove(self, key: str) -> bool:
  483.         """Removes a key entry from a Map.
  484.        return value: bool True on success, false if the value was never set.
  485.        """
  486.         if type(key) is not str:
  487.             raise TypeError('key must be str type')
  488.         if key not in self.__items:
  489.             return False
  490.         else:
  491.             del self.__items[key]
  492.  
  493.     def Snapshot(self) -> list:
  494.         """Create a snapshot of the map's keys. See StringMapSnapshot."""
  495.         return list(self.__items.keys())
  496.  
  497.     def Clear(self):
  498.         """Clears all entries from a Map."""
  499.         self.__items.clear()
  500.  
  501.     def _get_dict(self) -> dict:
  502.         """Returns the internal object dict"""
  503.         return self.__items
  504.  
  505.     def _set_dict(self, array: dict):
  506.         """Set the internal object dict"""
  507.         self.__items = array
  508.  
  509.     def __str__(self):
  510.         return 'StringMap(' + str(self.__items) + ')'
  511.  
  512.     def __len__(self):
  513.         return len(self.__items)
  514.  
  515.     def copy(self):
  516.         return StringMap(self.__items)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement