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
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;    ...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text...
Define a function called 'filterWords, which takes a parameter. The first parameter, text" is an nltk.text.Text object. The function definition code stub is given in the editor. Perform the given operation for the 'text' object and print the results: • Filter the words whose length is greater than 15 from the complete set of 'text', and store into "large_words' variable as a list
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT