def add(self, other, data=None, select=False): if other not in self.data: for x in other.get_parents_until(): self._lookup[x].add(other) if other not in self.data or \ size(data) > size(self.data[other]): self.data[other] = data if isinstance(select, bool) and select: self._select.add(other)
def save(self): import pickle as pkl from sys import getsizeof as size pickling_on = open("meta.zms", "wb") pkl.dump(self, pickling_on) pickling_on.close() print("[Data stored]", size(self) / 1024, "kb") pass
def _call_parallel(self, atoms, coords, map_switch_bytes = 50000, chunk_size = 100): from sys import getsizeof as size if self._pool is None: self._pool = mp.Pool() if size(coords[0])<map_switch_bytes: res = self._pool.map(self.f, coords, chunk_size) else: res = self._pool.imap(self.f, coords, chunk_size) return np.array(res)
def __sizeof__(self): return size(self.times_chosen) +\ size(self.times_chosen_since_new) +\ size(self.times_seen) +\ size(self.score) +\ size(self.ram) +\ size(self.reward) +\ size(self.length)
def generate(self, seed=0, shift=5): """""" if (seed): self.num = seed else: bitMax = size(self.num) * 8 - 1 self.num ^= (self.num << (shift % bitMax + 1)) | (self.num >> (bitMax - shift % bitMax)) self.num += 0x9595 self.num %= pow(2, 8 * 12) return self.num
def __sizeof__(self): total = 0 stack = [self.parent, self.action, self.children] while stack: object = stack.pop() if isinstance(object, TreeNode): stack.extend( [id(object.parent), object.action, object.children]) elif isinstance(object, dict): for key, value in object.items(): stack.extend([key, value]) else: total += size(object) return total
def _send_post(self, post_url, post_body=None): """ :param post_url: :param post_body: :return: the received reply if request is successful """ """Send data to server. data (list): node and values (eg: '[node,val1,val2,...]') time (int): timestamp, time when sample was recorded return True if data sent correctly """ reply = "" request = urllib2.Request(post_url, post_body) request.add_header('Content-Type', 'application/json') if self._settings["compression"]: request.add_header('Content-Encoding', 'gzip') if self._settings['use_binary']: request.add_header('Content-Type', 'application/x-msgpack') self._data_size = self._data_size + size(post_body) try: response = urllib2.urlopen(request, timeout=60) except urllib2.HTTPError as e: self._log.warning(self.name + " couldn't send to server, HTTPError: " + str(e.code)) except urllib2.URLError as e: self._log.warning(self.name + " couldn't send to server, URLError: " + str(e.reason)) except httplib.HTTPException: self._log.warning(self.name + " couldn't send to server, HTTPException") except Exception: import traceback self._log.warning(self.name + " couldn't send to server, Exception: " + traceback.format_exc()) else: reply = response.read() finally: self._log.debug("amount of data sent is %s" % self._data_size) return reply
def user_output(self, FORMAT): print('[OUTPUT] Engaged') while self.working_status: if not self.tasks_done.empty(): connection, text = self.tasks_done.get() text_size = str(size(text)) while len(text_size) != self.message_length: text_size += 'X' try: connection.send(text_size.encode(FORMAT)) connection.send(text.encode(FORMAT)) except BrokenPipeError: continue self.tasks_done.task_done() print('[OUTPUT] Terminating')
def publish(self, message: str, message_id: int) -> None: """ Publishes payload to broker :param message: string :param message_id: integer. :return: mqtt.MQTTMessageInfo """ payload: str = self._create_payload(message, message_id) max_payload_bytes = 268435455 if size(payload) > max_payload_bytes: msg = Message.status_message('Message too large.') self.client.queue.put(msg) return return_value: mqtt.MQTTMessageInfo = self.client.publish(self.client.topic, payload, qos=2) if return_value.rc == 0: # Publication successful return else: raise SubscriptionError(f'MQTTMessageInfo error code: {return_value.rc}')
def main(): args = argparse.ArgumentParser() args.add_argument("task", nargs="?", default="run", help=base.helpTask) args.add_argument("--path", "-p", nargs="?", default=base.path, help=base.helpPath) args.add_argument("--topics", "-t", nargs="?", default=base.numberTopics, help=base.helpTopics) args.add_argument("--nwords", "-w", nargs="?", default=base.nWords, help=base.helpnWords) args.add_argument("--distro", "-d", nargs="?", default=base.defaultDistro, help=base.helpDistro) pargs = args.parse_args() ndoc, docs = countFiles(pargs.path) if pargs.task == "run": print(base.run) if os.path.exists("meta.zms"): os.remove("meta.zms") distributions = heuristics(path=pargs.path, numberTopics=int(pargs.topics)) distributions.save() pass elif pargs.task == "test": print(base.run) if os.path.exists("meta.zms"): os.remove("meta.zms") distributions = heuristics(path=pargs.path, numberTopics=int(pargs.topics)) distributions.get_doc_topic_distrib(docs) distributions.get_topic_word_distrib(int(pargs.nwords)) distributions.get_doc_word_distrib(docs, int(pargs.nwords)) distributions.get_vocabulary(docs, int(pargs.nwords)) pass elif pargs.task == "display": if os.path.exists("meta.zms"): pass else: choice = input(base.choice) if choice == "y": print(base.run) distributions = heuristics(path=pargs.path, numberTopics=int(pargs.topics)) distributions.save() else: print("Taking it as a no.") return pickling_on = open("meta.zms", "rb") distributions = pkl.load(pickling_on) if pargs.distro == "dt": distributions.get_doc_topic_distrib(docs) elif pargs.distro == "tw": distributions.get_topic_word_distrib(int(pargs.nwords)) elif pargs.distro == "dw": distributions.get_doc_word_distrib(docs, int(pargs.nwords)) elif pargs.distro == "voc": distributions.get_vocabulary(docs, int(pargs.nwords)) elif pargs.distro == "all": distributions.get_doc_topic_distrib(docs) distributions.get_topic_word_distrib(int(pargs.nwords)) distributions.get_doc_word_distrib(docs, int(pargs.nwords)) distributions.get_vocabulary(docs, int(pargs.nwords)) pass elif pargs.task == "delete": pickling_on = open("meta.zms", "rb") from sys import getsizeof as size obj = pkl.load(pickling_on) os.remove("meta.zms") print(base.delete, size(obj) / 1024, "kb freed") pass elif pargs.task == "man": USAGE = open("MAN.txt", "r") print(USAGE.read()) pass else: print("[Invalid argument]") pass
from sys import getsizeof as size lst = [24, 12, 57, 42] size_of_list_object = size(lst) # only green box size_of_elements = len(lst) * size(lst[0]) # 24, 12, 57, 42 total_list_size = size_of_list_object + size_of_elements print("Size without the size of the elements: ", size_of_list_object) print("Size of all the elements: ", size_of_elements) print("Total size of list, including elements: ", total_list_size) lst = [] print("Emtpy list size: ", size(lst))
import numpy as np from sys import getsizeof as size #delaring list A = [1, 2, 3, 4, 5, 6] print(A) print(type(A)) #converting to array B = np.array(A) print(type(B)) #size of each variable(bits) print(size(A)) #36+6*4 print(size(B)) #48+6*4 #size of empty array and empty list(bits) print(size([])) print(size(np.array([]))) #In array or list each element takes 4 bits #data type of each element in array print(B.dtype) #optimising the memory allocted to int 16,32,64(changing size) B = np.array(A, np.int8) #only for arrays not list print(size(B)) #comparing operations of arrays and list C = B / 2 #arrays print(C) #D=A/2 #list throws error #print(D) D = list(i / 2 for i in A) print(D)
import numpy as np # Um den Speicherverbrauch einer Liste zu berechnen, werden wir die Funktion # "getsizeof" aus dem Modul "sys" benutzen. from sys import getsizeof as size import time lst = [24, 12, 57] size_of_list_object = size(lst) # nur die Liste, ohne Elemente size_of_elements = len(lst) * size(lst[0]) # 24, 12, 57 total_list_size = size_of_list_object + size_of_elements print("Größe ohne Größe der Elemente: ", size_of_list_object) print("Größe aller Elemente: ", size_of_elements) print("Gesamtgröße der Liste: ", total_list_size) print("**********************************************") lst = [24, 12, 57, 42] size_of_list_object = size(lst) size_of_elements = len(lst) * size(lst[0]) # 24, 12, 57, 42 total_list_size = size_of_list_object + size_of_elements print("Größe ohne Größe der Elemente: ", size_of_list_object) print("Größe aller Elemente: ", size_of_elements) print("Gesamtgröße der Liste: ", total_list_size) print("**********************************************") lst = [] print("Speicherbedarf einer leeren Liste: ", size(lst))
from sys import getsizeof as size import numpy as np lst = [24, 12, 57] size_of_list_object = size(lst) # only green box size_of_elements = len(lst) * size(lst[0]) # 24, 12, 57 total_list_size = size_of_list_object + size_of_elements print("Size without the size of the elements: ", size_of_list_object) print("Size of all the elements: ", size_of_elements) print("Total size of list, including elements: ", total_list_size) a = np.array([24, 12, 57]) print("Size of array = ", size(a)) e = np.array([]) print("Size of an empty array = ", size(e)) a = np.array([24, 12, 57], np.int8) print("Size of array with data type int8 = ", size(a) - 96) # size(a)-96 because 96 is the size of empty array a = np.array([24, 12, 57], np.int16) print("Size of array with data type int16 = ",size(a) - 96) a = np.array([24, 12, 57], np.int32) print("Size of array with data type int32 = ",size(a) - 96) a = np.array([24, 12, 57], np.int64) print("Size of array with data type int64 = ",size(a) - 96)
def __sizeof__(self): total = 0 for code, cell in self.items(): total += size(code) + size(cell) return total
import numpy as np from sys import getsizeof as size #declare list A = [1, 2, 3, 4, 5, 7, 8, 9] print(type(A)) #convert to array B = np.array(A) print(B) print(type(B)) #size of each variable print("A=", size(A)) print("B=", size(B)) #finding size of an empty array and empty list\memory consumed print("empty list size", size([])) #64+8 (list size) print("empty array size", size(np.array([]))) #96+8 (size of array) #optimizing memory my changing int to 16,32,64 B = np.array(A, np.int8) print("after optimizing B=", size(B)) #comparing operations in lists and arrays C = B / 2 print(C) #array #D = A/2 #print(D) #list error D = list(i / 2 for i in A) print(D)
def __sizeof__(self): return size(self.root)
"""# NumPy Array v/s List Reasons to choose numpy array v/s Python lists: 1. Less Memory Consumption 2. Fast 2. Convinient """ #Size Comparison between numpy array and list import numpy as np from sys import getsizeof as size lst = [1, 2, 3] size_of_elements = len(lst) * size(0) size_of_list_object = size(lst) total_list_size = size_of_list_object + size_of_elements print("Size without the size of the elements: ", size_of_list_object) print("Size of all the elements: ", size_of_elements) print("Total size of list, including elements: ", total_list_size) print() arr = np.array([1, 2, 3]) size_of_elements = arr.itemsize * arr.size size_of_array_object = size(arr) - size_of_elements total_list_size = size(arr) print("Size without the size of the elements : ", size_of_array_object) print("Size of all the elements: ", size_of_elements) print("Total size of list, including elements: ", total_list_size)
def test_numpy_vs_list_1(): # We will turn our list "cvalues" into a one-dimensional numpy array: C = np.array(cvalues) # C is an instance of the class numpy.ndarray" print(C) print(C * 9 / 5 + 32) print(C) #array C has not been changed by this expression: fvalues = [ x*9/5 + 32 for x in cvalues] print(fvalues) print(type(C)) # <class 'numpy.ndarray'> print(type(fvalues)) # <class 'list'> # import matplotlib.pyplot as plt # plt.plot(C) # plt.show() lst = [24, 12, 57] size_of_list_object = size(lst) # only green box(list object) size_of_elements = len(lst) * size(lst[0]) # 24, 12, 57 total_list_size = size_of_list_object + size_of_elements print("Size without the size of the elements: ", size_of_list_object) #88 print("Size of all the elements: ", size_of_elements) #84 print("Total size of list, including elements: ", total_list_size) #172 # Add a new element lst = [24, 12, 57, 42] size_of_list_object = size(lst) # only green box size_of_elements = len(lst) * size(lst[0]) # 24, 12, 57, 42 total_list_size = size_of_list_object + size_of_elements print("Size without the size of the elements: ", size_of_list_object) print("Size of all the elements: ", size_of_elements) print("Total size of list, including elements: ", total_list_size) lst = [] print("Emtpy list size: ", size(lst)) a = np.array([24, 12, 57]) print(size(a)) e = np.array([]) print(size(e)) # numpy automatically chooses a fixed integer size. - 3, 6, 12, 24 a = np.array([24, 12, 57], np.int8) print(size(a) - 96) a = np.array([24, 12, 57], np.int16) print(size(a) - 96) a = np.array([24, 12, 57], np.int32) print(size(a) - 96) a = np.array([24, 12, 57], np.int64) print(size(a) - 96)
import numpy as np from sys import getsizeof as size A = np.array([[1, 2], [4, 5]]) B = np.array([[6, 7], [8, 9]], np.int8) print(A) print(B) print(A.ndim) print(B.ndim) print("shape A=", A.shape) print("shape B=", B.shape) print("size A", size(A)) #default size=int32 print("size B", size(B)) #elementwise arthimetic operations add = np.add(A, B) print("addition\n", add) sub = np.subtract(A, B) print("sub\n", sub) mul = np.multiply(A, B) print("mul\n", mul) div = np.divide(A, B) print("div\n", div) #identity matrix I = np.identity(4) print("identity matrix\n", I) #null matrix N = np.zeros((3, 4)) print("null\n", N) # matrix with ones O = np.ones((3, 2)) print("all ones\n", O) #matrix multiplication
import numpy as np from sys import getsizeof as size X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Y = np.array(X, np.int8) print(size([])) print(size(np.array([]))) print(size(X) - 56) print(size(Y) - 96) Z = Y / 2 print(Z) for i in X: W = i / 2 print(W)
import numpy as np from sys import getsizeof as size #multi dimensional arrays A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9], [41, 65, 12]]) X = np.array([1, 2, 3, 4]) print(A.ndim) print(A) #shape of 2D array print(A.shape) print(A.dtype) print(size(np.array([]))) print(size(A)) print \ """#chaniging size of elements B=np.array(A,np.int8) print(B.dtype) print(size(np.array([]))) print(size(B)""" #accessing elements in array print(A[2, 1]) print(A[3, 2]) #slicing(first two rows) print(A[:, :]) print(A[:2, :3]) print(A[2:, 1:3])
from sys import getsizeof as size lst = [24, 12, 57] a = 10 print(size(a)) print(size(lst[1])) print(len(lst)) e = len(lst) * size(lst[0]) print(e) siz_of_list_object = size(lst) print(siz_of_list_object)
>>> I = int >>> >>> P1 = CP(L=1, I=5) # initialized with values >>> print(P1) CP(L=1, I=5) >>> P2 = CP() # default initialization >>> print(P2) CP(L=0, I=0) >>> print(P1.id, P2.id) # instance's ids 0 1 >>> # look for object by instance's ids >>> print(CP.get_instance(0), CP.get_instance(1)) CP(L=1, I=5) CP(L=0, I=0) >>> P2.L += 1; P2.I += 10 # assignment, fields are mutable >>> print(P2) CP(L=1, I=10) >>> # field/param types are not constrained, so be careful! >>> P2.L = 'something' >>> print(P2) CP(L='something', I=10) """ def __init__(self, **kwargs): pass if __name__ == "__main__": # for debugging from sys import getsizeof as size size(ClusterStructure)
def packet_bytes(c, u, m): print(size(c) + size(u) + size(m)) return size(c) + size(u) + size(m)