Ejemplo n.º 1
0
    def test_knotted(self):
        # Verify .isrecursive() and .isreadable() w/ recursion
        # Tie a knot.
        self.b[67] = self.a
        # Messy dict.
        self.d = {}
        self.d[0] = self.d[1] = self.d[2] = self.d

        verify = self.assert_
        pp = pprint.PrettyPrinter()

        for icky in self.a, self.b, self.d, (self.d, self.d):
            verify(pprint.isrecursive(icky), "expected isrecursive")
            verify(not pprint.isreadable(icky), "expected not isreadable")
            verify(pp.isrecursive(icky), "expected isrecursive")
            verify(not pp.isreadable(icky), "expected not isreadable")

        # Break the cycles.
        self.d.clear()
        del self.a[:]
        del self.b[:]

        for safe in self.a, self.b, self.d, (self.d, self.d):
            # module-level convenience functions
            verify(not pprint.isrecursive(safe),
                   "expected not isrecursive for %r" % (safe, ))
            verify(pprint.isreadable(safe),
                   "expected isreadable for %r" % (safe, ))
            # PrettyPrinter methods
            verify(not pp.isrecursive(safe),
                   "expected not isrecursive for %r" % (safe, ))
            verify(pp.isreadable(safe),
                   "expected isreadable for %r" % (safe, ))
Ejemplo n.º 2
0
def display_data(city_file):
    '''Displays five lines of data if the user specifies that they would like to.
    After displaying five lines, ask the user if they would like to see five more,
    continuing asking until they say stop.

    Args:
        none.
    Returns:
        (list) returns five lines and keep adding five more data if user specifies yes
    '''
    display = input('Would you like to view individual trip data?'
                    'Type \'yes\' or \'no\'. ')
    # TODO: handle raw input and complete function
    start_line=0
    end_line=5
    while display == "yes" :
        print(city_file[start_line:end_line])
        pprint.isreadable(city_file[start_line:end_line])
        if (input('Would you like to view individual trip data?'
                        'Type \'yes\' or \'no\'. ') =='yes'):
            start_line+=5
            end_line+=5
            continue
        return
    else :
        return
Ejemplo n.º 3
0
    def test_knotted(self) -> None:
        # Verify .isrecursive() and .isreadable() w/ recursion
        # Tie a knot.
        self.b[67] = self.a
        # Messy dict.
        self.d = {}  # type: Dict[int, dict]
        self.d[0] = self.d[1] = self.d[2] = self.d

        pp = pprint.PrettyPrinter()

        for icky in self.a, self.b, self.d, (self.d, self.d):
            self.assertTrue(pprint.isrecursive(icky), "expected isrecursive")
            self.assertFalse(pprint.isreadable(icky),
                             "expected not isreadable")
            self.assertTrue(pp.isrecursive(icky), "expected isrecursive")
            self.assertFalse(pp.isreadable(icky), "expected not isreadable")

        # Break the cycles.
        self.d.clear()
        del self.a[:]
        del self.b[:]

        for safe in self.a, self.b, self.d, (self.d, self.d):
            # module-level convenience functions
            self.assertFalse(pprint.isrecursive(safe),
                             "expected not isrecursive for %r" % (safe, ))
            self.assertTrue(pprint.isreadable(safe),
                            "expected isreadable for %r" % (safe, ))
            # PrettyPrinter methods
            self.assertFalse(pp.isrecursive(safe),
                             "expected not isrecursive for %r" % (safe, ))
            self.assertTrue(pp.isreadable(safe),
                            "expected isreadable for %r" % (safe, ))
Ejemplo n.º 4
0
    def test_knotted(self):
        # Verify .isrecursive() and .isreadable() w/ recursion
        # Tie a knot.
        self.b[67] = self.a
        # Messy dict.
        self.d = {}
        self.d[0] = self.d[1] = self.d[2] = self.d

        verify = self.assertTrue
        pp = pprint.PrettyPrinter()

        for icky in self.a, self.b, self.d, (self.d, self.d):
            verify(pprint.isrecursive(icky), "expected isrecursive")
            verify(not pprint.isreadable(icky),  "expected not isreadable")
            verify(pp.isrecursive(icky), "expected isrecursive")
            verify(not pp.isreadable(icky),  "expected not isreadable")

        # Break the cycles.
        self.d.clear()
        del self.a[:]
        del self.b[:]

        for safe in self.a, self.b, self.d, (self.d, self.d):
            # module-level convenience functions
            verify(not pprint.isrecursive(safe),
                   "expected not isrecursive for %r" % (safe,))
            verify(pprint.isreadable(safe),
                   "expected isreadable for %r" % (safe,))
            # PrettyPrinter methods
            verify(not pp.isrecursive(safe),
                   "expected not isrecursive for %r" % (safe,))
            verify(pp.isreadable(safe),
                   "expected isreadable for %r" % (safe,))
Ejemplo n.º 5
0
    def create_files(self):
        config_lines = []
        unreadable_data = {}

        pp = pprint.PrettyPrinter(indent=2, width=150)
        for k, v in sorted(self.pwg_config.items()):
            if pprint.isreadable(v):
                config_lines.append("'%s': %s," % (k, pp.pformat(v)))
            else:
                unreadable_data[k] = v

        if len(unreadable_data) > 0:
            assert False, "unreadable data"

        with open(tk.uncached_path(self.config_file), 'wt',
                  encoding='utf-8') as f:
            f.write("{\n")
            f.write('\n'.join(config_lines))
            f.write("}\n")
        with open("run.sh", "wt") as f:
            f.write("#!/bin/bash\n")
            f.write("export PYTHONPATH=%s\n" % self.pwg_src_root)
            train_call = self.build_train_call()
            f.write(' '.join(train_call) + "\n")

        os.chmod(
            'run.sh', stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH | stat.S_IWUSR
            | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)
Ejemplo n.º 6
0
def pkdpretty(obj):
    """Return pretty print the object.

    If `obj` is JSON, parse and print it. If it is a regular python
    data structure, pretty print that. Any exceptions are caught,
    and the return value will be `obj`.

    Args:
        obj (object): JSON string or python object

    Returns:
        str: pretty printed string
    """
    try:
        if isinstance(obj, six.string_types):
            try:
                obj = json.loads(obj)
            except Exception:
                pass
        # try to dump as JSON else dump as Python
        try:
            return json.dumps(
                obj,
                sort_keys=True,
                indent=4,
                separators=(',', ': '),
            ) + '\n'
        except Exception as e:
            pass
        if pprint.isreadable(obj):
            return pprint.pformat(obj, indent=4) + '\n'
    except Exception:
        pass
    return obj
Ejemplo n.º 7
0
def receive_data(data):
	"""
	python-xbee Documentation, Release 2.1.0
	This method is called whenever data is received
	from the associated XBee device. Its first and
	only argument is the data contained within the
	frame.

	purpose of receive_data is to take incoming data and add it to a queue or queues
	to be processed elsewhere. 
	only exception is to print incoming data and maybe have a logging function to save incoming data directly to a text file
	"""
	
	
	t0 = time.time()	
	
	if pprint.isreadable(data): #not sure if every message has been getting printed
		pprint.pprint(data)
	else:
		print "couldn't pretty print. here is raw data:"
		print(data)
		
	
	global msg_queue
	msg_queue.append(data) #process incoming messages in a separate thread
	
	t1 = time.time()
	print t1 - t0
	print #blank line so display starts after >>> line
Ejemplo n.º 8
0
  def write(self, path):
    config = self.config
    config.update(self.post_config)

    config = instanciate_vars(config)

    config_lines = []
    unreadable_data = {}

    pp = pprint.PrettyPrinter(indent=2, width=150)
    for k, v in sorted(config.items()):
      if pprint.isreadable(v):
        config_lines.append('%s = %s' % (k, pp.pformat(v)))
      elif isinstance(v, tk.Path):
        unreadable_data[k] = v

    if len(unreadable_data) > 0:
      config_lines.append('import json')
      json_data = json.dumps(unreadable_data).replace('"', '\\"')
      config_lines.append('config = json.loads("%s")' % json_data)
    else:
      config_lines.append('config = {}')

    python_code = string.Template(self.PYTHON_CODE).substitute({ 'REGULAR_CONFIG' : '\n'.join(config_lines),
                                                                 'EXTRA_PYTHON_CODE' : self.extra_python_code })
    with open(path, 'wt', encoding='utf-8') as f:
      f.write(python_code)
Ejemplo n.º 9
0
def receive_data(data):
    """
	python-xbee Documentation, Release 2.1.0
	This method is called whenever data is received
	from the associated XBee device. Its first and
	only argument is the data contained within the
	frame.

	purpose of receive_data is to take incoming data and add it to a queue or queues
	to be processed elsewhere. 
	only exception is to print incoming data and maybe have a logging function to save incoming data directly to a text file
	"""

    t0 = time.time()

    if pprint.isreadable(
            data):  #not sure if every message has been getting printed
        pprint.pprint(data)
    else:
        print "couldn't pretty print. here is raw data:"
        print(data)

    global msg_queue
    msg_queue.append(data)  #process incoming messages in a separate thread

    t1 = time.time()
    print t1 - t0
    print  #blank line so display starts after >>> line
Ejemplo n.º 10
0
def make_pretty(data):
    '''
    :param data: ugly data
    :return str: pretty data formatted using **pprint.pformat**,
        or ``None`` if data was too ugly
    '''
    if isreadable(data):
        return pformat(data)
Ejemplo n.º 11
0
 def test_unreadable(self):
     """Not recursive but not readable anyway."""
     verify = self.assert_
     for unreadable in type(3), pprint, pprint.isrecursive:
         verify(not pprint.isrecursive(unreadable),
                "expected not isrecursive for " + `unreadable`)
         verify(not pprint.isreadable(unreadable),
                "expected not isreadable for " + `unreadable`)
Ejemplo n.º 12
0
def make_pretty(data):
    """
    :param data: ugly data
    :return str: pretty data formatted using **pprint.pformat**,
        or ``None`` if data was too ugly
    """
    if isreadable(data):
        return pformat(data)
Ejemplo n.º 13
0
 def test_unreadable(self):
     """Not recursive but not readable anyway."""
     verify = self.assert_
     for unreadable in type(3), pprint, pprint.isrecursive:
         verify(not pprint.isrecursive(unreadable),
                "expected not isrecursive for " + ` unreadable `)
         verify(not pprint.isreadable(unreadable),
                "expected not isreadable for " + ` unreadable `)
Ejemplo n.º 14
0
    def set_option(self, name, value):
        if not isreadable(value):
            raise TypeError("The option '%s' (current value: '%s') can't be " "dumped" % (name, value))

        self.module.attributes[name] = value

        if name in __builtin__.__dict__:
            self.emit("log", "The option '%s' overwrites the builtin of the " "same name" % name, level="warning")
Ejemplo n.º 15
0
def unpickle(s):
    s = s.encode("UTF-8") # s is unicode, switch to string
    s = s.replace('\\n', '\n') # replace \n character pair with a real newline character
    data = pickle.loads(s)
    if not pprint.isreadable(data):
        print "Data cannot be loaded as it is not yield a good representation of pickle. (isreadable false)"
        return data
    pretty_data = pprint.pformat(data, indent=INDENT_SIZE)
    return pretty_data
Ejemplo n.º 16
0
 def test_basic(self):
     """Verify .isrecursive() and .isreadable() w/o recursion."""
     verify = self.assert_
     for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
                  self.a, self.b):
         verify(not pprint.isrecursive(safe),
                "expected not isrecursive for " + `safe`)
         verify(pprint.isreadable(safe),
                "expected isreadable for " + `safe`)
Ejemplo n.º 17
0
 def test_basic(self):
     """Verify .isrecursive() and .isreadable() w/o recursion."""
     verify = self.assert_
     for safe in (2, 2.0, 2j, "abc", [3], (2, 2), {
             3: 3
     }, uni("yaddayadda"), self.a, self.b):
         verify(not pprint.isrecursive(safe),
                "expected not isrecursive for " + ` safe `)
         verify(pprint.isreadable(safe),
                "expected isreadable for " + ` safe `)
Ejemplo n.º 18
0
 def recursive_eval(self, o: object) -> str:
     ret: object
     if pprint.isreadable(o):
         try:
             ret = self.recursive_eval(eval(o))
         except Exception:
             ret = o
     else:
         ret = o
     return o
Ejemplo n.º 19
0
 def test_unreadable(self):
     # Not recursive but not readable anyway
     pp = pprint.PrettyPrinter()
     for unreadable in type(3), pprint, pprint.isrecursive:
         # module-level convenience functions
         self.assertFalse(pprint.isrecursive(unreadable), "expected not isrecursive for %r" % (unreadable,))
         self.assertFalse(pprint.isreadable(unreadable), "expected not isreadable for %r" % (unreadable,))
         # PrettyPrinter methods
         self.assertFalse(pp.isrecursive(unreadable), "expected not isrecursive for %r" % (unreadable,))
         self.assertFalse(pp.isreadable(unreadable), "expected not isreadable for %r" % (unreadable,))
Ejemplo n.º 20
0
 def test_basic(self):
     # Verify .isrecursive() and .isreadable() w/o recursion
     pp = pprint.PrettyPrinter()
     for safe in (2, 2.0, 2j, "abc", [3], (2, 2), {3: 3}, "yaddayadda", self.a, self.b):
         # module-level convenience functions
         self.assertFalse(pprint.isrecursive(safe), "expected not isrecursive for %r" % (safe,))
         self.assertTrue(pprint.isreadable(safe), "expected isreadable for %r" % (safe,))
         # PrettyPrinter methods
         self.assertFalse(pp.isrecursive(safe), "expected not isrecursive for %r" % (safe,))
         self.assertTrue(pp.isreadable(safe), "expected isreadable for %r" % (safe,))
Ejemplo n.º 21
0
def unpickle(s):
    s = s.encode("UTF-8")  # s is unicode, switch to string
    s = s.replace(
        '\\n', '\n')  # replace \n character pair with a real newline character
    data = pickle.loads(s)
    if not pprint.isreadable(data):
        print "Data cannot be loaded as it is not yield a good representation of pickle. (isreadable false)"
        return data
    pretty_data = pprint.pformat(data, indent=INDENT_SIZE)
    return pretty_data
Ejemplo n.º 22
0
 def test_unreadable(self):
     pp = pprint.PrettyPrinter()
     for unreadable in (type(3), pprint, pprint.isrecursive):
         self.assertFalse(pprint.isrecursive(unreadable), 
             'expected not isrecursive for %r' % (unreadable,))
         self.assertFalse(pprint.isreadable(unreadable), 
             'expected not isreadable for %r' % (unreadable,))
         self.assertFalse(pp.isrecursive(unreadable), 
             'expected not isrecursive for %r' % (unreadable,))
         self.assertFalse(pp.isreadable(unreadable), 
             'expected not isreadable for %r' % (unreadable,))
Ejemplo n.º 23
0
	def save(self):
		"Save non-default option values into a dictionary."

		optDict = {}
		for name, opt in self._options.items():
			value = opt.savedValue(forPrefSave=1)
			if value == opt.defaultValue():
				continue
			if not pprint.isreadable(value):
				continue
			optDict[name] = value
		return optDict
Ejemplo n.º 24
0
 def test_basic(self):
     pp = pprint.PrettyPrinter()
     for safe in (2, 2.0, 2j, 'abc', [3], (2, 2), {(3): 3}, b'def',
         bytearray(b'ghi'), True, False, None, ..., self.a, self.b):
         self.assertFalse(pprint.isrecursive(safe), 
             'expected not isrecursive for %r' % (safe,))
         self.assertTrue(pprint.isreadable(safe), 
             'expected isreadable for %r' % (safe,))
         self.assertFalse(pp.isrecursive(safe), 
             'expected not isrecursive for %r' % (safe,))
         self.assertTrue(pp.isreadable(safe), 
             'expected isreadable for %r' % (safe,))
Ejemplo n.º 25
0
    def save(self):
        "Save non-default option values into a dictionary."

        optDict = {}
        for name, opt in self._options.items():
            value = opt.savedValue(forPrefSave=1)
            if value == opt.defaultValue():
                continue
            if not pprint.isreadable(value):
                continue
            optDict[name] = value
        return optDict
Ejemplo n.º 26
0
    def serialize(self):
        self.check_consistency()
        config = self.config
        config.update(self.post_config)

        config = instanciate_delayed(config)

        config_lines = []
        unreadable_data = {}

        pp = pprint.PrettyPrinter(indent=2, width=150, **self.pprint_kwargs)
        for k, v in sorted(config.items()):
            if pprint.isreadable(v):
                config_lines.append("%s = %s" % (k, pp.pformat(v)))
            else:
                unreadable_data[k] = v

        if len(unreadable_data) > 0:
            config_lines.append("import json")
            json_data = json.dumps(unreadable_data).replace('"', '\\"')
            config_lines.append('config = json.loads("%s")' % json_data)
        else:
            config_lines.append("config = {}")

        python_prolog_code = self._parse_python(self.python_prolog)
        python_epilog_code = self._parse_python(self.python_epilog)

        if self.staged_network_dict:
            get_network_string = "\ndef get_network(epoch, **kwargs):\n"
            get_network_string += "  from networks import networks_dict\n"
            get_network_string += "  while(True):\n"
            get_network_string += "    if epoch in networks_dict:\n"
            get_network_string += "      return networks_dict[epoch]\n"
            get_network_string += "    else:\n"
            get_network_string += "      epoch -= 1\n"
            get_network_string += "      assert epoch > 0, \"Error, no networks found\"\n"

            config_lines.append(get_network_string)

            python_prolog_code = (
                "import os\nimport sys\nsys.path.insert(0, os.path.dirname(__file__))\n\n"
                + python_prolog_code)

        python_code = string.Template(self.PYTHON_CODE).substitute({
            "PROLOG":
            python_prolog_code,
            "REGULAR_CONFIG":
            "\n".join(config_lines),
            "EPILOG":
            python_epilog_code,
        })
        return python_code
Ejemplo n.º 27
0
 def test_knotted(self):
     self.b[67] = self.a
     self.d = {}
     self.d[0] = self.d[1] = self.d[2] = self.d
     pp = pprint.PrettyPrinter()
     for icky in (self.a, self.b, self.d, (self.d, self.d)):
         self.assertTrue(pprint.isrecursive(icky), 'expected isrecursive')
         self.assertFalse(pprint.isreadable(icky), 'expected not isreadable'
             )
         self.assertTrue(pp.isrecursive(icky), 'expected isrecursive')
         self.assertFalse(pp.isreadable(icky), 'expected not isreadable')
     self.d.clear()
     del self.a[:]
     del self.b[:]
     for safe in (self.a, self.b, self.d, (self.d, self.d)):
         self.assertFalse(pprint.isrecursive(safe), 
             'expected not isrecursive for %r' % (safe,))
         self.assertTrue(pprint.isreadable(safe), 
             'expected isreadable for %r' % (safe,))
         self.assertFalse(pp.isrecursive(safe), 
             'expected not isrecursive for %r' % (safe,))
         self.assertTrue(pp.isreadable(safe), 
             'expected isreadable for %r' % (safe,))
Ejemplo n.º 28
0
	def save(self):
		"Return the option dictionary as what needs to be save."

		optDict = {}
		for name, value in self._options.items():
			try:
				if value == self._defaultOptions[name]:
					continue
			except KeyError:
				pass
			if not pprint.isreadable(value):
				continue
			optDict[name] = value
		return optDict
Ejemplo n.º 29
0
    def save(self):
        "Return the option dictionary as what needs to be save."

        optDict = {}
        for name, value in self._options.items():
            try:
                if value == self._defaultOptions[name]:
                    continue
            except KeyError:
                pass
            if not pprint.isreadable(value):
                continue
            optDict[name] = value
        return optDict
Ejemplo n.º 30
0
 def test_unreadable(self):
     # Not recursive but not readable anyway
     pp = pprint.PrettyPrinter()
     for unreadable in type(3), pprint, pprint.isrecursive:
         # module-level convenience functions
         self.assertFalse(pprint.isrecursive(unreadable),
                          "expected not isrecursive for %r" % (unreadable,))
         self.assertFalse(pprint.isreadable(unreadable),
                          "expected not isreadable for %r" % (unreadable,))
         # PrettyPrinter methods
         self.assertFalse(pp.isrecursive(unreadable),
                          "expected not isrecursive for %r" % (unreadable,))
         self.assertFalse(pp.isreadable(unreadable),
                          "expected not isreadable for %r" % (unreadable,))
Ejemplo n.º 31
0
    def test_knotted(self):
        """Verify .isrecursive() and .isreadable() w/ recursion."""
        # Tie a knot.
        self.b[67] = self.a
        # Messy dict.
        self.d = {}
        self.d[0] = self.d[1] = self.d[2] = self.d

        verify = self.assert_

        for icky in self.a, self.b, self.d, (self.d, self.d):
            verify(pprint.isrecursive(icky), "expected isrecursive")
            verify(not pprint.isreadable(icky), "expected not isreadable")

        # Break the cycles.
        self.d.clear()
        del self.a[:]
        del self.b[:]

        for safe in self.a, self.b, self.d, (self.d, self.d):
            verify(not pprint.isrecursive(safe),
                   "expected not isrecursive for " + ` safe `)
            verify(pprint.isreadable(safe),
                   "expected isreadable for " + ` safe `)
Ejemplo n.º 32
0
 def test_unreadable(self):
     # Not recursive but not readable anyway
     verify = self.assert_
     pp = pprint.PrettyPrinter()
     for unreadable in type(3), pprint, pprint.isrecursive:
         # module-level convenience functions
         verify(not pprint.isrecursive(unreadable),
                "expected not isrecursive for " + `unreadable`)
         verify(not pprint.isreadable(unreadable),
                "expected not isreadable for " + `unreadable`)
         # PrettyPrinter methods
         verify(not pp.isrecursive(unreadable),
                "expected not isrecursive for " + `unreadable`)
         verify(not pp.isreadable(unreadable),
                "expected not isreadable for " + `unreadable`)
Ejemplo n.º 33
0
    def test_knotted(self):
        """Verify .isrecursive() and .isreadable() w/ recursion."""
        # Tie a knot.
        self.b[67] = self.a
        # Messy dict.
        self.d = {}
        self.d[0] = self.d[1] = self.d[2] = self.d

        verify = self.assert_

        for icky in self.a, self.b, self.d, (self.d, self.d):
            verify(pprint.isrecursive(icky), "expected isrecursive")
            verify(not pprint.isreadable(icky),  "expected not isreadable")

        # Break the cycles.
        self.d.clear()
        del self.a[:]
        del self.b[:]

        for safe in self.a, self.b, self.d, (self.d, self.d):
            verify(not pprint.isrecursive(safe),
                   "expected not isrecursive for " + `safe`)
            verify(pprint.isreadable(safe),
                   "expected isreadable for " + `safe`)
Ejemplo n.º 34
0
 def test_basic(self):
     # Verify .isrecursive() and .isreadable() w/o recursion
     pp = pprint.PrettyPrinter()
     for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
                  self.a, self.b):
         # module-level convenience functions
         self.assertFalse(pprint.isrecursive(safe),
                          "expected not isrecursive for %r" % (safe,))
         self.assertTrue(pprint.isreadable(safe),
                         "expected isreadable for %r" % (safe,))
         # PrettyPrinter methods
         self.assertFalse(pp.isrecursive(safe),
                          "expected not isrecursive for %r" % (safe,))
         self.assertTrue(pp.isreadable(safe),
                         "expected isreadable for %r" % (safe,))
Ejemplo n.º 35
0
def store_info(filename, data):
    r'''Pretty print roundtrippable data into filename

    >>> f = mktemp()
    >>> store_info(f, dict(a='hello', b=[1, 2, 3]))
    >>> open(f).read()
    "{'a': 'hello', 'b': [1, 2, 3]}\n"
    '''
    if not isreadable(data):
        raise Exception('Recursive or nonserializable data')

    f = open(filename, 'w')
    try:
        pprint(data, f)
    finally:
        f.close()
Ejemplo n.º 36
0
 def test_basic(self):
     # Verify .isrecursive() and .isreadable() w/o recursion
     verify = self.assert_
     pp = pprint.PrettyPrinter()
     for safe in (2, 2.0, 2j, "abc", [3], (2,2), {3: 3}, uni("yaddayadda"),
                  self.a, self.b):
         # module-level convenience functions
         verify(not pprint.isrecursive(safe),
                "expected not isrecursive for " + `safe`)
         verify(pprint.isreadable(safe),
                "expected isreadable for " + `safe`)
         # PrettyPrinter methods
         verify(not pp.isrecursive(safe),
                "expected not isrecursive for " + `safe`)
         verify(pp.isreadable(safe),
                "expected isreadable for " + `safe`)
Ejemplo n.º 37
0
Archivo: util.py Proyecto: davidk01/bpt
def store_info(filename, data):
    r'''Pretty print roundtrippable data into filename
    
    >>> f = mktemp()
    >>> store_info(f, dict(a='hello', b=[1, 2, 3]))
    >>> open(f).read()
    "{'a': 'hello', 'b': [1, 2, 3]}\n"
    '''
    if not isreadable(data):
        raise Exception('Recursive or nonserializable data')
    
    f = open(filename, 'w')
    try:
        pprint(data, f)
    finally:
        f.close()
Ejemplo n.º 38
0
def check_obj(obj, title):
    if pprint.isreadable(obj):
        logging.info(title + " is readable")
    else:
        logging.info(title + " is NOT readable")
    try:
        json.dumps(obj)
        logging.info(title + " is JSON serializable")
    except TypeError as err:
        logging.error(title + " is not JSON serializable")
        logging.error(err)
    try:
        s = pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
        pickle.loads(s)
        logging.info(title + " can be pickled")
    except:
        logging.error(title + " could not be picked and unpickled")
Ejemplo n.º 39
0
def check_obj(obj, title):
    if pprint.isreadable(obj):
        logging.info(title + " is readable")
    else:
        logging.info(title + " is NOT readable")
    try:
        json.dumps(obj)
        logging.info(title + " is JSON serializable")
    except TypeError as err:
        logging.error(title + " is not JSON serializable")
        logging.error(err)
    try:
        s=pickle.dumps(obj,pickle.HIGHEST_PROTOCOL)
        pickle.loads(s)
        logging.info(title + " can be pickled")
    except:
        logging.error(title + " could not be picked and unpickled")
Ejemplo n.º 40
0
    def _serialize(self):
        """
        Serialize the main config
        :return:
        """
        self.check_consistency()
        config = self.config
        config.update(self.post_config)

        config = instanciate_delayed(config)

        config_lines = []
        unreadable_data = {}

        pp = pprint.PrettyPrinter(indent=2, width=150, **self.pprint_kwargs)
        for k, v in sorted(config.items()):
            if pprint.isreadable(v):
                config_lines.append("%s = %s" % (k, pp.pformat(v)))
            else:
                unreadable_data[k] = v

        if len(unreadable_data) > 0:
            config_lines.append("import json")
            json_data = json.dumps(unreadable_data).replace('"', '\\"')
            config_lines.append('config = json.loads("%s")' % json_data)
        else:
            config_lines.append("config = {}")

        python_prolog_code = self.__parse_python(self.python_prolog)
        python_epilog_code = self.__parse_python(self.python_epilog)

        support_code = ""
        if self.staged_network_dict:
            support_code += self.GET_NETWORK_CODE

        python_code = string.Template(self.PYTHON_CODE).substitute({
            "SUPPORT_CODE":
            support_code,
            "PROLOG":
            python_prolog_code,
            "REGULAR_CONFIG":
            "\n".join(config_lines),
            "EPILOG":
            python_epilog_code,
        })
        return python_code
Ejemplo n.º 41
0
def save():
    """ Save configuration to CONFIG_FILE. """
    global current, CONFIG_FILE

    if not current:
        raise EnvironmentError(_('No configuration loaded.'))
    
    # Get a config file we can write to.
    target = ensure_file(CONFIG_FILE, writable=True)
    if not target:
        raise EnvironmentError(_('Could not get a writable configuration file.'))

    with open(target, 'wb') as f:
        f.write('# Generated by {n}.\n'.format(n=michiru.__name__).encode('utf-8'))

        # Pretty print all config items...
        for k, v in current.items():
            # ... but only if we can actually read them back again.
            if pprint.isreadable(v):
                f.write('{} = {}\n'.format(k, pprint.pformat(v)).encode('utf-8'))
Ejemplo n.º 42
0
    def make_recog_config(self, job, file_path, import_models):
        import json
        import pprint
        import string
        import textwrap

        config = job.returnn_config
        config.update(job.returnn_post_config)
        config.update(import_models)
        config["num_outputs"]["classes"] = [util.get_val(job.num_classes), 1]

        config_lines = []
        unreadable_data = {}

        pp = pprint.PrettyPrinter(indent=2, width=150)
        for k, v in sorted(config.items()):
            if pprint.isreadable(v):
                config_lines.append("%s = %s" % (k, pp.pformat(v)))
            else:
                unreadable_data[k] = v

        if len(unreadable_data) > 0:
            config_lines.append("import json")
            print(unreadable_data)
            json_data = json.dumps(unreadable_data).replace('"', '\\"')
            config_lines.append('config = json.loads("%s")' % json_data)
        else:
            config_lines.append("config = {}")

        python_code = string.Template(job.PYTHON_CODE).substitute({
            "PROLOG":
            job.returnn_config.python_prolog,
            "REGULAR_CONFIG":
            "\n".join(config_lines),
            "EPILOG":
            job.returnn_config.python_epilog,
        })
        # with open(file_path, 'wt', encoding='utf-8') as f:
        #    f.write(python_code)
        return python_code
Ejemplo n.º 43
0
def pkdpretty(obj):
    """Return pretty print the object.

    If `obj` is JSON, parse and print it. If it is a regular python
    data structure, pretty print that. Any exceptions are caught,
    and the return value will be `obj`.

    Args:
        obj (object): JSON string or python object

    Returns:
        str: pretty printed string
    """
    try:
        import json
        if isinstance(obj, six.string_types):
            try:
                obj = json.loads(obj)
            except Exception:
                pass
        # try to dump as JSON else dump as Python
        try:
            return json.dumps(
                obj,
                sort_keys=True,
                indent=4,
                separators=(',', ': '),
            ) + '\n'
        except Exception:
            pass
        import pprint
        if pprint.isreadable(obj):
            return pprint.pformat(obj, indent=4) + '\n'
    except Exception:
        pass
    return obj
 def test_readable(self):
     assert isreadable(Matrix(2, 4, range(8)))
Ejemplo n.º 45
0
#! /usr/bin/env python
# coding:utf-8

# https://docs.python.org/2/library/pprint.html

import pprint

stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
stuff.insert(0, stuff[:])

print pprint.isreadable(stuff)
print pprint.saferepr(stuff)

pprint.pprint(stuff)

pp = pprint.PrettyPrinter(indent=4)
pp.pprint(stuff)

tup = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',))))))))
pp = pprint.PrettyPrinter(depth=6)
pp.pprint(tup)

Ejemplo n.º 46
0
import pprint
Ejemplo n.º 47
0
import pprint
Ejemplo n.º 48
0
pp = pprint.PrettyPrinter(width=41, compact=True)
pp.pprint(stuff)

tup = ('spam', ('eggs', ('lumberjack',
                         ('knights', ('ni', ('dead', ('parrot',
                                                      ('fresh fruit', ))))))))
pp = pprint.PrettyPrinter(depth=6)
pp.pprint(tup)

print(pprint.pformat(tup, depth=6))

# pprint.pp(tup) # 3.8

pprint.pprint(tup, depth=6)

print(pprint.isreadable(tup))
print(pprint.isrecursive(tup))

print("-----------------")

import json
import pprint
from urllib.request import urlopen

with urlopen('https://pypi.org/pypi/sampleproject/json') as resp:
    project_info = json.load(resp)['info']

pprint.pprint(project_info)

pprint.pprint(project_info, depth=1, width=60)
Ejemplo n.º 49
0
 def isreadable(self, object):
     return isreadable(object)