Question

In: Computer Science

Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It...

Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It "pretty prints" the contents of the namedtuple, including both the names of its fields and their values. This is subject to a few rules.

  • Each field and its value are displayed on one line, with the name of the field appearing first, followed by a colon and a space, followed by the value of the field converted to a string.
  • The fields should be displayed in the same order that they were specified when the namedtuple's type was created.
  • The field names should have "padding" (i.e., empty space) in front of them, so that the colons all line up vertically; only the fields that have names longer than all of the others should have no spaces in front of their names.

A couple of examples follow.

>>> from collections import namedtuple
>>> Point = namedtuple('Point', ['x', 'y', 'z'])
>>> pt = Point(z = 5, x = 9, y = 13)
>>> pretty_print(pt)
x: 9
y: 13
z: 5
>>> Person = namedtuple('Person', ['name', 'age', 'favorite'])
>>> instructor = Person(name = 'Alex', age = 45, favorite = 'Boo')
>>> pretty_print(instructor)
    name: Alex
     age: 45
favorite: Boo

Solutions

Expert Solution

Code:

from collections import namedtuple
def pretty_print(pt):
for i in range(len(pt)):#this loop iterates untill the len of pt
print(pt._fields[i],":",pt[i])#here we print the output in the format

  
Point = namedtuple('Point', ['x', 'y', 'z'])
pt = Point(z = 5, x = 9, y = 13)
pretty_print(pt)

Person = namedtuple('Person', ['name', 'age', 'favorite'])
instructor = Person(name = 'Alex', age = 45, favorite = 'Boo')
pretty_print(instructor)

Output:



Related Solutions

Write a function which takes one parameter int num, and prints out a countdown timer with...
Write a function which takes one parameter int num, and prints out a countdown timer with minutes and seconds separated by a colon (:). It should print out one line for each second elapsed and then pause one second before printing out the next line. A few things to note: - You can assume that calling the function usleep(1000000) makes the program pause for one second - It should count down from num minutes:zero seconds to zero minutes:zero seconds -...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the...
Python Programming 1. Write a function name split_name which takes one parameter called name. If the name parameter value is a name in the form LastName, FirstName(e.g., ”Grounds, Nic”) then the function should return a list of two elements where FirstName is the first element and LastName is the second element. If the name parameter value is a name in the form FirstName LastName(e.g., ”Nic Grounds”) then the function should return a list of two elements where FirstName is the...
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
C++ Write a toVector function that takes in a C-style pointer of any type and a...
C++ Write a toVector function that takes in a C-style pointer of any type and a size (int) and returns a vector of the same size containing a copy of the elements in the input array using a template. You may assume that the array elements have a valid copy constructor. Please explain every line of code to me
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
C++.how to write a selection sort to sort it. read_book_data() This member function takes one parameter,...
C++.how to write a selection sort to sort it. read_book_data() This member function takes one parameter, a string that contains the name of a file. This string parameter can be a C++ string or a C string (your choice). The function returns nothing. This constructor should do the following: Declare and open an input file stream variable using the file name string passed in as a parameter. Check to make sure the file was opened successfully. If not, print an...
Which of the types listed below can be the type of a function output parameter? unsigned...
Which of the types listed below can be the type of a function output parameter? unsigned long * double unsigned long char * Void Consider this function that takes a fraction (with arguments for numerator and denominator) and inverts it in place: void invert_fraction(type num, type denom) {    int tmp = *num;    if (*num == 0 || *denom == 0) {        fprintf(stderr, "ERROR: cannot invert %d/%d.\n", *num, *denom);        exit(1);     }     *num = *denom;    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT