sys.getsizeof

Here are the examples of the python api sys.getsizeof taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

116 Examples 7

Example 1

Project: KMCLib Source File: XYZTrajectory.py
Function: buffer_size
    def _bufferSize(self):
        """
        Calculate and return the buffer size.
        """
        size =  sys.getsizeof(self.__atom_id_coordinates)
        size +=  sys.getsizeof(self.__atom_id_coordinates[0])*len(self.__atom_id_coordinates)
        size += sys.getsizeof(self.__atom_id_types)
        size += sys.getsizeof(self.__atom_id_types[0])*len(self.__atom_id_types)
        size += sys.getsizeof(self.__time)
        size += sys.getsizeof(self.__time[0])*len(self.__time)
        size += sys.getsizeof(self.__step)
        size += sys.getsizeof(self.__step[0])*len(self.__step)

        return size

Example 2

Project: grr Source File: fake_data_store.py
Function: size
  def Size(self):
    total_size = sys.getsizeof(self.subjects)
    for subject, record in self.subjects.iteritems():
      total_size += sys.getsizeof(subject)
      total_size += sys.getsizeof(record)
      for attribute, values in record.iteritems():
        total_size += sys.getsizeof(attribute)
        total_size += sys.getsizeof(values)
        for value, timestamp in values:
          total_size += sys.getsizeof(value)
          total_size += sys.getsizeof(timestamp)
    return total_size

Example 3

Project: leap_mail Source File: size.py
Function: get_size
def _get_size(item, seen):
    known_types = {dict: lambda d: chain.from_iterable(d.items())}
    default_size = getsizeof(0)

    def size_walk(item):
        if id(item) in seen:
            return 0
        seen.add(id(item))
        s = getsizeof(item, default_size)
        for _type, fun in known_types.iteritems():
            if isinstance(item, _type):
                s += sum(map(size_walk, fun(item)))
                break
        return s

    return size_walk(item)

Example 4

Project: dask Source File: cache.py
    def _posttask(self, key, value, dsk, state, id):
        duration = default_timer() - self.starttimes[key]
        deps = state['dependencies'][key]
        if deps:
            duration += max(self.durations.get(k, 0) for k in deps)
        self.durations[key] = duration
        nb = self._nbytes(value) + overhead + sys.getsizeof(key) * 4
        self.cache.put(key, value, cost=duration / nb / 1e9, nbytes=nb)

Example 5

Project: ndstore Source File: cshlTest.py
Function: cache_test
def cache_test( token, hostname, slicenumber, resolution ):
  """ Carry's out the test on a given number of tiles """
  totalsize = 0.0

  for X in range(0,20):
    for Y in range(0,20):
      
      try:
        url = 'http://{}/ocpcatmaid/tilecache/{}/{}/{}_{}_{}.png'.format(hostname, token, slicenumber, X, Y, resolution )
        req = urllib2.Request( url )
        response = urllib2.urlopen(req)
        totalsize += sys.getsizeof(response)
      except urllib2.URLError, e:
        print "Failed URL {}".format(url)
        print "Error {}".format(e)

  print "Size in kilobytes {}".format(totalsize/1024)

Example 6

Project: mysql-tools Source File: http_handlers.py
def HandlePyHeaps(unused_request, response):
  """Renders /pyheaps page with Python object memory usage."""
  response.headers.add_header('Content-Type', 'text/plain')
  response.start_response(200)

  count = {}
  mem_used = {}
  for obj in gc.get_objects():
    count[type(obj)] = count.get(type(obj), 0) + 1
    mem_used[type(obj)] = mem_used.get(type(obj), 0) + sys.getsizeof(obj)

  response.out.write('Python objects (total bytes, count, type):\n\n')
  for total_size, type_ in sorted(((v, k) for k, v in mem_used.iteritems()),
                                  reverse=True):
    response.out.write('%12s %12s  %s\n' % (total_size, count[type_], type_))

  response.end_response()

Example 7

Project: EmPyre Source File: helpers.py
Function: get_file_size
def get_file_size(file):
    """
    Returns a string with the file size and highest rating.
    """
    byte_size = sys.getsizeof(file)
    kb_size = byte_size / 1024
    if kb_size == 0:
        byte_size = "%s Bytes" % (byte_size)
        return byte_size
    mb_size = kb_size / 1024
    if mb_size == 0:
        kb_size = "%s KB" % (kb_size)
        return kb_size
    gb_size = mb_size / 1024 % (mb_size)
    if gb_size == 0:
        mb_size = "%s MB" %(mb_size)
        return mb_size
    return "%s GB" % (gb_size)

Example 8

Project: flask-mongoengine Source File: operation_tracker.py
Function: unpack_response
@functools.wraps(_original_methods['_unpack_response'])
def _unpack_response(response, *args, **kwargs):

    result = _original_methods['_unpack_response'](
        response,
        *args,
        **kwargs
    )
    response_sizes.append(sys.getsizeof(response, len(response)) / 1024.0)
    return result

Example 9

Project: tilequeue Source File: process.py
Function: sizeof
def _sizeof(val):
    size = 0

    if isinstance(val, dict):
        for k, v in val.items():
            size += len(k) + _sizeof(v)
    elif isinstance(val, list):
        for v in val:
            size += _sizeof(v)
    elif isinstance(val, (str, bytes, unicode)):
        size += len(val)
    else:
        size += getsizeof(val)

    return size

Example 10

Project: openstates Source File: jsindex.py
    def as_json(self, showsizes=False):
        data = self.jsondata()
        if showsizes:
            from utils import humanize_bytes
            for k, v in data.items():
                js = json.dumps(v, cls=JSONDateEncoder)
                print 'size of', k, humanize_bytes(sys.getsizeof(js))
        return data

Example 11

Project: TrustRouter Source File: test_sys.py
Function: check_sizeof
    def check_sizeof(self, o, size):
        result = sys.getsizeof(o)
        # add GC header size
        if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
           ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
            size += self.gc_headsize
        msg = 'wrong size for %s: got %d, expected %d' \
              % (type(o), result, size)
        self.assertEqual(result, size, msg)

Example 12

Project: django-scarface Source File: platform_strategy.py
    def trim_message(self, message):
        import sys
        trim_length = SCARFACE_DEFAULT_MESSAGE_TRIM_LENGTH
        if hasattr(settings, 'SCARFACE_MESSAGE_TRIM_LENGTH'):
            trim_length = settings.SCARFACE_MESSAGE_TRIM_LENGTH

        if sys.getsizeof(message) > trim_length:
            while sys.getsizeof(message) > trim_length:
                message = message[:-3]
            message += '...'
        return message

Example 13

Project: tortik Source File: __init__.py
    def finish_with_debug(self):
        self.set_header('Content-Type', 'text/html; charset=utf-8')
        if self.debug_type == _DEBUG_ALL:
            self.set_status(200)

        self.finish(RequestHandler.debug_loader.get_template('debug.html').render(
            data=self.log.get_debug_info(),
            output_data=self.get_data(),
            size=sys.getsizeof,
            get_params=lambda x: urlparse.parse_qs(x, keep_blank_values=True),
            pretty_json=lambda x: json.dumps(x, sort_keys=True, indent=4, ensure_ascii=False),
            pretty_xml=lambda x: to_unicode(tostring(x.getroot() if hasattr(x, 'getroot') else x,
                                                     pretty_print=True, encoding='UTF-8')),
            to_unicode=to_unicode,
            dumper=dump,
            format_exception=lambda x: "".join(traceback.format_exception(*x))
        ))

Example 14

Project: pympler Source File: test_summary.py
    def test_summarize(self):
        """Test summarize method. """
        objects = [1, 'a', 'b', 'a', 5, [], {}]
        expected = [[summary._repr(''), 3, 3*getsizeof('a')],\
                    [summary._repr(1), 2, 2*getsizeof(1)],\
                    [summary._repr([]), 1, getsizeof([])],\
                    [summary._repr({}), 1, getsizeof({})]]
        res = summary.summarize(objects)
        for row_e in res:
            self.assert_(row_e in expected)

Example 15

Project: jinjabread Source File: filehandler.py
def _safe_size(payload, limit):
    ''' if file size limit return false '''
    
    limit = limit * 1024
    size = sys.getsizeof(payload)
    
    if size > limit:
        return False
    return True

Example 16

Project: python-future Source File: misc.py
Function: sizeof
    def __sizeof__(self):
        sizeof = sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 17

Project: incubator-airflow Source File: gcs_download_operator.py
    def execute(self, context):
        logging.info('Executing download: %s, %s, %s', self.bucket, self.object, self.filename)
        hook = GoogleCloudStorageHook(google_cloud_storage_conn_id=self.google_cloud_storage_conn_id,
                                      delegate_to=self.delegate_to)
        file_bytes = hook.download(self.bucket, self.object, self.filename)
        if self.store_to_xcom_key:
            if sys.getsizeof(file_bytes) < 48000:
                context['ti'].xcom_push(key=self.store_to_xcom_key, value=file_bytes)
            else:
                raise RuntimeError('The size of the downloaded file is too large to push to XCom!')
        logging.info(file_bytes)

Example 18

Project: Perspectives-Server Source File: pycache.py
Function: init
	def __init__(self, key, data, expiry):
		"""Create new cache entry."""

		if (expiry < 1):
			raise ValueError("CacheEntry expiry values must be positive")

		now = int(time.time())

		self.key = key
		self.data = data
		self.expiry = now + expiry
		self.memory_used = sys.getsizeof(data)

		# count the key as having been requested just now, so it is not immediately removed.
		# this is usually correct, as the caller will likely have just retrieved or calculated
		# the data before calling us to store it.
		# this also prevents thrashing so new entries are not rapidly added and then removed from the heap.
		self.last_requested = now

Example 19

Project: iot-utilities Source File: test_memoryio.py
Function: test_sizeof
    @support.cpython_only
    def test_sizeof(self):
        basesize = support.calcobjsize('P2n2Pn')
        check = self.check_sizeof
        self.assertEqual(object.__sizeof__(io.BytesIO()), basesize)
        check(io.BytesIO(), basesize )
        check(io.BytesIO(b'a' * 1000), basesize + sys.getsizeof(b'a' * 1000))

Example 20

Project: pympler Source File: test_muppy.py
    def test_filter_by_size(self):
        """Test that only elements within the specified size boundaries
        are returned.
        Also verify that if minimum is larger than maximum an exception is
        raised."""
        minimum = 42
        maximum = 958
        objects = []
        for i in range(1000):
            rand = random.randint(0,1000)
            objects.append(' ' * rand)
        objects = muppy.filter(objects, min=minimum, max=maximum)
        for o in objects:
            self.assert_(minimum <= getsizeof(o) <= maximum)

        self.assertRaises(ValueError, muppy.filter, objects, min=17, max=16)

Example 21

Project: yaql Source File: utils.py
def limit_memory_usage(quota_or_engine, *args):
    if isinstance(quota_or_engine, int):
        quota = quota_or_engine
    else:
        quota = get_memory_quota(quota_or_engine)
    if quota <= 0:
        return

    total = 0
    for t in args:
        total += t[0] * sys.getsizeof(t[1], 0)
        if total > quota:
            raise exceptions.MemoryQuotaExceededException()

Example 22

Project: dpark Source File: shuffle.py
Function: merge
    def merge(self, items):
        Merger.merge(self, items)

        self.merged += 1
        if self.max_merge is None:
            current_mem = max(self.get_used_memory() - self.base_memory,
                              sys.getsizeof(self.combined) >> 20)
            if current_mem > self.mem:
                logger.warning(
                    'Too much memory for shuffle, using disk-based shuffle')
                self.max_merge = self.merged

        if self.max_merge is not None and self.merged >= self.max_merge:
            self.rotate()
            self.merged = 0

Example 23

Project: dpark Source File: table.py
Function: left_outer_join
    @table_join
    def leftOuterJoin(self, other, left_keys=None, right_keys=None):
        o = other.indexBy(right_keys).collectAsMap()
        r = self.indexBy(left_keys).map(lambda (k,v):(k,(v,o.get(k))))
        r.mem += (sys.getsizeof(o) * 10) >> 20 # memory used by broadcast obj
        return r

Example 24

Project: pympler Source File: test_muppy.py
Function: test_get_size
    def test_get_size(self):
        """Test that the return value is the sum of the size of all objects."""
        (o1, o2, o3, o4, o5) = (1, 'a', 'b', 4, 5)
        list = [o1, o2, o3, o4, o5]
        expected = 0
        for o in list:
            expected += getsizeof(o)

        self.assertEqual(muppy.get_size(list), expected)

Example 25

Project: ete Source File: db.py
def zencode(x, data_id):
    pdata = six.moves.cPickle.dumps(x)
    if sys.getsizeof(pdata) > MAX_SQLITE_SIZE:
        # using protocol 2 fails because of the integer overflow python bug
        # i.e. http://bugs.python.org/issue13555
        six.moves.cPickle.dump(x, open(pjoin(GLOBALS['db_dir'], data_id+".pkl"), "wb"), protocol=1)
        return "__DBDIR__:%s" %data_id
    else:
        return base64.encodestring(zlib.compress(pdata))

Example 26

Project: TrustRouter Source File: test_sys.py
Function: test_gc_head_size
    def test_gc_head_size(self):
        # Check that the gc header size is added to objects tracked by the gc.
        h = self.header
        vh = self.vheader
        size = self.calcsize
        gc_header_size = self.gc_headsize
        # bool objects are not gc tracked
        self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
        # but lists are
        self.assertEqual(sys.getsizeof([]), size(vh + 'PP') + gc_header_size)

Example 27

Project: plenum Source File: util.py
Function: get_size
def get_size(obj, seen=None):
    """Recursively finds size of objects"""
    size = sys.getsizeof(obj)
    if seen is None:
        seen = set()
    obj_id = id(obj)
    if obj_id in seen:
        return 0
    # Important mark as seen *before* entering recursion to gracefully handle
    # self-referential objects
    seen.add(obj_id)
    if isinstance(obj, dict):
        size += sum([get_size(v, seen) for v in obj.values()])
        size += sum([get_size(k, seen) for k in obj.keys()])
    elif hasattr(obj, '__dict__'):
        size += get_size(obj.__dict__, seen)
    elif hasattr(obj, '__iter__') and not isinstance(obj, (str, bytes, bytearray)):
        size += sum([get_size(i, seen) for i in obj])
    return size

Example 28

Project: bluepass Source File: perf_skiplist.py
    def perf_memory_overhead(self):
        """Test memory overhead."""
        for logN in range(3, 6):
            items = 10**logN
            sl = self._create_skiplist(items)
            node = sl._head[2]
            extrasize = 0
            while node is not sl._tail:
                extrasize += sys.getsizeof(node)
                if  len(node) > 3:
                    extrasize += sys.getsizeof(node[-1])
                node = node[2]
            overhead = extrasize / len(sl)
            self.add_result(overhead, params={'logN': logN})

Example 29

Project: INGInious Source File: tasks_code_boxes.py
    def input_is_consistent(self, taskInput, default_allowed_extension, default_max_size):
        if not BasicBox.input_is_consistent(self, taskInput, default_allowed_extension, default_max_size):
            return False

        try:
            if not taskInput[self.get_complete_id()]["filename"].endswith(tuple(self._allowed_exts or default_allowed_extension)):
                return False

            if sys.getsizeof(taskInput[self.get_complete_id()]["value"]) > (self._max_size or default_max_size):
                return False
        except:
            return False
        return True

Example 30

Project: pympler Source File: summary.py
Function: subtract
def _subtract(summary, o):
    """Remove object o from the summary by subtracting it's size."""
    found = False
    row = [_repr(o), 1, getsizeof(o)]
    for r in summary:
        if r[0] == row[0]:
            (r[1], r[2]) = (r[1] - row[1], r[2] - row[2])
            found = True
    if not found:
        summary.append([row[0], -row[1], -row[2]])
    return summary

Example 31

Project: itools Source File: utils.py
def get_sizeof(obj):
    """Return the size of an object and all objects refered by it.
    """
    size = 0
    done = set()
    todo = {id(obj): obj}
    while todo:
        obj_id, obj = todo.popitem()
        size += getsizeof(obj)
        done.add(obj_id)
        done.add(id(obj.__class__)) # Do not count the class
        for obj in get_referents(obj):
            obj_id = id(obj)
            if obj_id not in done:
                todo[obj_id] = obj

    return size

Example 32

Project: brython Source File: test_sys.py
Function: test_gc_head_size
    def test_gc_head_size(self):
        # Check that the gc header size is added to objects tracked by the gc.
        vsize = test.support.calcvobjsize
        gc_header_size = self.gc_headsize
        # bool objects are not gc tracked
        self.assertEqual(sys.getsizeof(True), vsize('') + self.longdigit)
        # but lists are
        self.assertEqual(sys.getsizeof([]), vsize('Pn') + gc_header_size)

Example 33

Project: pymo Source File: test_sys.py
Function: test_gc_head_size
    def test_gc_head_size(self):
        # Check that the gc header size is added to objects tracked by the gc.
        h = self.header
        size = self.calcsize
        gc_header_size = self.gc_headsize
        # bool objects are not gc tracked
        self.assertEqual(sys.getsizeof(True), size(h + 'l'))
        # but lists are
        self.assertEqual(sys.getsizeof([]), size(h + 'P PP') + gc_header_size)

Example 34

Project: datafari Source File: test_mmap.py
Function: test_sizeof
    @cpython_only
    @unittest.skipUnless(os.name == 'nt', 'requires Windows')
    def test_sizeof(self):
        m1 = mmap.mmap(-1, 100)
        tagname = "foo"
        m2 = mmap.mmap(-1, 100, tagname=tagname)
        self.assertEqual(sys.getsizeof(m2),
                         sys.getsizeof(m1) + len(tagname) + 1)

Example 35

Project: proficiency-metric Source File: util.py
Function: sizeof
def sizeof (obj, types = None):
    ret = sys.getsizeof(obj)
    if types is not None:
        types[type(obj).__name__] += 1
    if (isinstance(obj,list) or isinstance(obj,tuple) or
        isinstance(obj,set) or isinstance(obj,frozenset)):
        for x in obj:
            ret += sizeof(x, types = types)
        return ret
    if isinstance(obj,dict):
        for k,v in obj.iteritems():
            ret += sizeof(k, types = types) + sizeof(v, types = types)
        return ret
    return ret

Example 36

Project: pydoop Source File: pipes.py
Function: emit
    def emit(self, key, value):
        self.used_bytes += sys.getsizeof(key)
        self.used_bytes += sys.getsizeof(value)
        if not self.fast_combiner:
            key = self.__defensive_copy(key)
            value = self.__defensive_copy(value)
        self.data.setdefault(key, []).append(value)
        self.ctx.increment_counter(self.in_rec_counter, 1)
        if self.used_bytes >= self.spill_bytes:
            self.spill_all()

Example 37

Project: AWS-Lambda-ML-Microservice-Skeleton Source File: test_unicode.py
Function: buffer_length
    def buffer_length(arr):
        if isinstance(arr, unicode):
            arr = str(arr)
            return (sys.getsizeof(arr+"a") - sys.getsizeof(arr)) * len(arr)
        v = memoryview(arr)
        if v.shape is None:
            return len(v) * v.itemsize
        else:
            return np.prod(v.shape) * v.itemsize

Example 38

Project: nocrack Source File: trie.py
Function: sizeof
    def __sizeof__(self):
        T = self.Tree
        s = 0;
        for k in T:
            s += getsizeof(T[k]) + sys.getsizeof(k)
        return s;

Example 39

Project: chest Source File: core.py
Function: nbytes
def nbytes(o):
    """ Number of bytes of an object

    >>> nbytes(123)  # doctest: +SKIP
    24

    >>> nbytes('Hello, world!')  # doctest: +SKIP
    50

    >>> import numpy as np
    >>> nbytes(np.ones(1000, dtype='i4'))
    4000
    """
    if hasattr(o, 'nbytes'):
        return o.nbytes
    if hasattr(o, 'values') and hasattr(o, 'index'):
        return o.values.nbytes + o.index.nbytes  # pragma: no cover
    else:
        return sys.getsizeof(o)

Example 40

Project: cachey Source File: nbytes.py
Function: array
def _array(x):
    if x.dtype == 'O':
        return sys.getsizeof('0'*100) * x.size
    elif str(x.dtype) == 'category':
        return _array(x.codes) + _array(x.categories)
    else:
        return x.nbytes

Example 41

Project: iot-utilities Source File: test_pickle.py
Function: test_pickler
        def test_pickler(self):
            basesize = support.calcobjsize('5P2n3i2n3iP')
            p = _pickle.Pickler(io.BytesIO())
            self.assertEqual(object.__sizeof__(p), basesize)
            MT_size = struct.calcsize('3nP0n')
            ME_size = struct.calcsize('Pn0P')
            check = self.check_sizeof
            check(p, basesize +
                MT_size + 8 * ME_size +  # Minimal memo table size.
                sys.getsizeof(b'x'*4096))  # Minimal write buffer size.
            for i in range(6):
                p.dump(chr(i))
            check(p, basesize +
                MT_size + 32 * ME_size +  # Size of memo table required to
                                          # save references to 6 objects.
                0)  # Write buffer is cleared after every dump().

Example 42

Project: pymo Source File: test_sys.py
Function: check_sizeof
    def check_sizeof(self, o, size):
        result = sys.getsizeof(o)
        if ((type(o) == type) and (o.__flags__ & self.TPFLAGS_HEAPTYPE) or\
           ((type(o) != type) and (type(o).__flags__ & self.TPFLAGS_HAVE_GC))):
            size += self.gc_headsize
        msg = 'wrong size for %s: got %d, expected %d' \
                % (type(o), result, size)
        self.assertEqual(result, size, msg)

Example 43

Project: mistral Source File: __init__.py
def cut_by_kb(data, kilobytes):
    if kilobytes <= 0:
        return cut(data)

    bytes_per_char = sys.getsizeof('s') - sys.getsizeof('')
    length = int(kilobytes * 1024 / bytes_per_char)

    return cut(data, length)

Example 44

Project: dpark Source File: serialize.py
Function: dump_obj
def dump_obj(f, name, obj):
    if obj is f:
        # Prevent infinite recursion when dumping a recursive function
        return dumps(RECURSIVE_FUNCTION_PLACEHOLDER)

    try:
        if sys.getsizeof(obj) > OBJECT_SIZE_LIMIT:
            obj = create_broadcast(name, obj, f.__name__)
    except TypeError:
        pass

    b = dumps(obj)
    if len(b) > OBJECT_SIZE_LIMIT:
        b = dumps(create_broadcast(name, obj, f.__name__))
    if len(b) > OBJECT_SIZE_LIMIT:
        logger.warning("broadcast of %s obj too large", type(obj))
    return b

Example 45

Project: TrustRouter Source File: collections.py
Function: sizeof
    def __sizeof__(self):
        sizeof = _sys.getsizeof
        n = len(self) + 1                       # number of links including root
        size = sizeof(self.__dict__)            # instance dictionary
        size += sizeof(self.__map) * 2          # internal dict and inherited dict
        size += sizeof(self.__hardroot) * n     # link objects
        size += sizeof(self.__root) * n         # proxy objects
        return size

Example 46

Project: pympler Source File: muppy.py
Function: get_size
def get_size(objects):
    """Compute the total size of all elements in objects."""
    res = 0
    for o in objects:
        try:
            res += getsizeof(o)
        except AttributeError:
            print("IGNORING: type=%s; o=%s" % (str(type(o)), str(o)))
    return res

Example 47

Project: pympler Source File: test_muppy.py
Function: test_sort
    def test_sort(self):
        """Test that objects are sorted by size."""
        objects = ['', 'a', 'ab', 'ab', 'abc', '0']
        objects = muppy.sort(objects)
        while len(objects) > 1:
            prev_o = objects.pop(0)
            self.assert_(getsizeof(objects[0]) >= getsizeof(prev_o),\
                 "The previous element appears to be larger than the " +\
                 "current: %s<%s" % (prev_o, objects[0]))

Example 48

Project: TrustRouter Source File: test_sys.py
Function: test_default
    def test_default(self):
        h = self.header
        vh = self.vheader
        size = self.calcsize
        self.assertEqual(sys.getsizeof(True), size(vh) + self.longdigit)
        self.assertEqual(sys.getsizeof(True, -1), size(vh) + self.longdigit)

Example 49

Project: datafari Source File: test_sys.py
    def test_gc_head_size(self):
        # Check that the gc header size is added to objects tracked by the gc.
        size = test.test_support.calcobjsize
        gc_header_size = self.gc_headsize
        # bool objects are not gc tracked
        self.assertEqual(sys.getsizeof(True), size('l'))
        # but lists are
        self.assertEqual(sys.getsizeof([]), size('P PP') + gc_header_size)

Example 50

Project: BitXBay Source File: caltxs.py
def unspent4sentmerch(namount, buyertxs, msig, msig2,addressbuyer, addressmerchant, buyerchange):
    namount2 = float(namount)*0.05
    if namount2 < 0.0005:
        namount2 = 0.0005
    ad0 = {}
    fraud = False
    #here check and break if buyer sent merchant's unspent.
    try:
        listunspent = conn.listunspent(0)
        listunspent = sorted(listunspent, key=lambda k: k['amount'])
    except:
        listunspent = []
        fraud = True
    allmine = False
    try:
        for el in listunspent:
            for buyert in buyertxs:
                if el["txid"] in buyert["txid"]:
                    fraud = True
        #specialy if you create deal with yourself check is all addresses is your
        allmine = True
        try:
            try:
                a2 = conn.validateaddress(str(msig))
                if a2.ismine == False:
                    allmine = False
            except:
                error=""
                allmine = False
            try:
                a2 = conn.validateaddress(str(msig2))
                if a2.ismine == False:
                    allmine = False
            except:
                error=""
                allmine = False

            try:
                a2 = conn.validateaddress(str(addressbuyer))
                if a2.ismine == False:
                    allmine = False
            except:
                error=""
                allmine = False

            print allmine

            try:
                a2 = conn.validateaddress(str(addressmerchant))
                if a2.ismine == False:
                    allmine = False
            except:
                error=""
                allmine = False
        except:
            allmine =False

        if allmine == True:
            fraud = False
    except:
        allmine = False
        fraud = True
        listunspent = []
    #here is if all checks is passed, need hard audit this checks
    if fraud == False:
        amount = 0
        if allmine:
            amount = buyerchange + namount2 + namount
        fee = 0.0001
        adlist = buyertxs

        #create tx with 2 payments form buyer and 1 from merchant and get back change
        for el in listunspent:
            print '10'
            if {"txid":el["txid"], "vout":el["vout"]} in adlist:
                if allmine != True:
                    adlist=[]
                    addr = []
                    break
            if amount >= (namount+namount2*2+fee):
                #this code only for deal with yourself
                change = amount - namount - namount2*2 - fee
                if change > 0:
                    m = conn.validateaddress(addressbuyer)
                    if m.ismine:
                        addr = {msig:namount2*2, msig2:namount, addressbuyer:change}
                        try:
                            a = conn.createrawtransactionlist(adlist, addr)
                        except:
                            a = ""
                        try:
                            b = conn.signrawtransaction(a)
                        except:
                            b = ""
                        try:
                            bsz = sys.getsizeof(b["hex"])/1000
                        except:
                            bsz = 16
                        if bsz > 1 and bsz < 15:
                            d = bsz
                            fee = fee + 0.0001 * d
                            change = amount - namount2*2 -namount - fee

                            if change > 0:
                                addr[addressbuyer] = change
                                a = conn.createrawtransactionlist(adlist, addr)
                                b = conn.signrawtransaction(a)
                                return b
                            elif change == 0:
                                del addr[addressbuyer]
                                a = conn.createrawtransactionlist(adlist, addr)
                                b = conn.signrawtransaction(a)
                                return b
                            else:
                                continue
                        else:
                            return b
                elif change == 0:
                    m = conn.validateaddress(addressbuyer)
                    if m.ismine:
                        addr = {msig:namount2*2, msig2:namount}
                        try:
                            a = conn.createrawtransactionlist(adlist, addr)
                        except:
                            a = ""
                        try:
                            b = conn.signrawtransaction(a)
                        except:
                            b = ""
                        try:
                            bsz = sys.getsizeof(b["hex"])/1000
                        except:
                            bsz = 16
                        if bsz > 1 and bsz < 15:
                            d = bsz
                            fee = fee + 0.0001 * d
                            continue
                        else:
                            return b
                else:
                    continue
            elif {"txid":el["txid"], "vout":el["vout"]} not in adlist:
                adlist.append({"txid":el["txid"], "vout":el["vout"]})
                amount = amount + el["amount"]
            if amount >= namount2 + fee and allmine == False:
                if buyerchange > 0:
                    if amount - namount2 -fee > 0:
                        change = amount - fee - namount2
                        addr = {msig:namount2*2, msig2:namount, addressbuyer:buyerchange, addressmerchant:change}
                    else:
                        addr = {msig:namount2*2, msig2:namount, addressbuyer:buyerchange}
                else:
                    if amount - namount2 -fee > 0:
                        change = amount - fee - namount2
                        addr = {msig:namount2*2, msig2:namount,addressmerchant:change}
                    else:
                        addr = {msig:namount2*2, msig2:namount}
                try:
                    a = conn.createrawtransactionlist(adlist, addr)
                except:
                    a = ""
                try:
                    b = conn.signrawtransaction(a)
                except:
                    b = ""
                try:
                    bsz = sys.getsizeof(b["hex"])/1000
                except:
                    bsz = 16
                #buyer pay 0.0002 commission and marchant 0.0001 if size of tx not more then 1000
                if bsz > 1 and bsz < 15:
                    d = bsz
                    fee = fee + 0.0001 * d
                    change = amount - namount2 - fee
                    if change > 0:
                        addr[addressmerchant] = change
                        a = conn.createrawtransactionlist(adlist, addr)
                        b = conn.signrawtransaction(a)
                        return b
                    elif change == 0:
                        del addr[addressmerchant]
                        a = conn.createrawtransactionlist(adlist, addr)
                        b = conn.signrawtransaction(a)
                        return b
                    else:
                        continue
                elif bsz == 0:
                    return b
                else:
                    error=""
                return b
    return ad0
See More Examples - Go to Next Page
Page 1 Selected Page 2 Page 3