Advertisement
uopspop

Untitled

Oct 24th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.65 KB | None | 0 0
  1.     # -*- coding: utf-8 -*-
  2.     """
  3.    Created on Thu Oct 25 00:30:39 2018
  4.    
  5.    @author: sam
  6.    """
  7.    
  8.     """This module contains a code example related to
  9.    
  10.    Think Python, 2nd Edition
  11.    by Allen Downey
  12.    http://thinkpython2.com
  13.    
  14.    Copyright 2015 Allen Downey
  15.    
  16.    License: http://creativecommons.org/licenses/by/4.0/
  17.    """
  18.    
  19.     #from __future__ import print_function, division
  20.    
  21.     """
  22.    This module provides one function, structshape(), which takes
  23.    an object of any type and returns a string that summarizes the
  24.    "shape" of the data structure; that is, the type, size and
  25.    composition.
  26.    """
  27.    
  28.     def structshape(ds):
  29.         """Returns a string that describes the shape of a data structure.
  30.    
  31.        ds: any Python object
  32.    
  33.        Returns: string
  34.        """
  35.         typename = type(ds).__name__
  36.    
  37.         # handle sequences
  38.         sequence = (list, tuple, set, type(iter('')))
  39.         if isinstance(ds, sequence):
  40.             t = []
  41.             for i, x in enumerate(ds):
  42.                 t.append(structshape(x))
  43.             rep = '%s of %s' % (typename, listrep(t))
  44.             return rep
  45.    
  46.         # handle dictionaries
  47.         elif isinstance(ds, dict):
  48.             keys = set()
  49.             vals = set()
  50.             for k, v in ds.items():
  51.                 keys.add(structshape(k))
  52.                 vals.add(structshape(v))
  53.             rep = '%s of %d %s->%s' % (typename, len(ds),
  54.                                        setrep(keys), setrep(vals))
  55.             return rep
  56.    
  57.         # handle other types
  58.         else:
  59.             if hasattr(ds, '__class__'):
  60.                 return ds.__class__.__name__
  61.             else:
  62.                 return typename
  63.    
  64.    
  65.     def listrep(t):
  66.         """Returns a string representation of a list of type strings.
  67.    
  68.        t: list of strings
  69.    
  70.        Returns: string
  71.        """
  72.         current = t[0]
  73.         count = 0
  74.         res = []
  75.         for x in t:
  76.             if x == current:
  77.                 count += 1
  78.             else:
  79.                 append(res, current, count)
  80.                 current = x
  81.                 count = 1
  82.         append(res, current, count)
  83.         return setrep(res)
  84.    
  85.    
  86.     def setrep(s):
  87.         """Returns a string representation of a set of type strings.
  88.    
  89.        s: set of strings
  90.    
  91.        Returns: string
  92.        """
  93.         rep = ', '.join(s)
  94.         if len(s) == 1:
  95.             return rep
  96.         else:
  97.             return '(' + rep + ')'
  98.         return
  99.    
  100.    
  101.     def append(res, typestr, count):
  102.         """Adds a new element to a list of type strings.
  103.    
  104.        Modifies res.
  105.    
  106.        res: list of type strings
  107.        typestr: the new type string
  108.        count: how many of the new type there are
  109.    
  110.        Returns: None
  111.        """
  112.         if count == 1:
  113.             rep = typestr
  114.         else:
  115.             rep = '%d %s' % (count, typestr)
  116.         res.append(rep)
  117.    
  118.    
  119.     if __name__ == '__main__':
  120.    
  121.         t = [1, 2, 3]
  122.         print(structshape(t))
  123.    
  124.         t2 = [[1, 2], [3, 4], [5, 6]]
  125.         print(structshape(t2))
  126.    
  127.         t3 = [1, 2, 3, 4.0, '5', '6', [7], [8], 9]
  128.         print(structshape(t3))
  129.    
  130.         class Point:
  131.             """trivial object type"""
  132.    
  133.         t4 = [Point(), Point()]
  134.         print(structshape(t4))
  135.    
  136.         s = set('abc')
  137.         print(structshape(s))
  138.    
  139.         lt = zip(t, s)
  140.         print(structshape(lt))
  141.    
  142.         d = dict(lt)        
  143.         print(structshape(d))
  144.    
  145.         it = iter('abc')
  146.         print(structshape(it))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement