Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help!!! Define a function named pnamedtuple in a module named pcollections.py (that is the only name defined in the module, but this function can

Please help!!!

  • Define a function namedpnamedtuplein a module namedpcollections.py(that is the only name defined in the module, but this function can define local functions: I wroteshow_listingand 5 other short ones). Its header is
  • def pnamedtuple(type_name, field_names, mutable=False, defaults={}):
  • an example call to this function is
  • Point = pnamedtuple('Point', ['x','y'], mutable=False)
  • which is equivalent to writingPoint = pnamedtuple('Point', 'x y')orPoint = pnamedtuple('Point', 'x,y'). Once we have definedPointin this way, we can then write code likeorigin = Point(0,0).
  • Generally, apnamedtuplecan have an arbitrary number of field names;the order of these field names is important and should be retained in the later code(for example see the header of__init__below). SoPoint = pnamedtuple('Point', 'x,y')has a different meaning thanPoint = pnamedtuple('Point', 'y,x')even though both have the same field names: their order is different, and some methods depend on this order (again, see the header of__init__below, which would bedef __init__(self, y, x)in the second case).
  • Alegal namefor the "type" and "fields" must start with a letter which can be followed by 0 or more letters, digits, or underscore characters (hint: I used a simple regular expression to verify legal names); also it must not be a Python keyword. Hint: the namekwlistis importable from thekeywordmodule: it is bound to a list of all Python keywords.
  • The parameters must have the following structure.
  • type_namemust be a legal name (see above).
  • field_namesmust be a list of legal names (see above), or a string in which spaces or commas (or some mixture of the two) separate legal names. So, we can specifyfield_nameslike['x','y']or'x y', or'x, y'. If a name is duplicated, just ignore all but its first appearance (hint: I used theuniquegenerator to filter out duplicates, which is written in the course notes).
  • If any of the names are not legal, raise aSyntaxErrorwith an appropriate message.
  • defaultscan specify a dictionary offield_namesand their default values: the connection between these two features will be described further below in the definition of__init__. Meanwhile, if any keys in thedefaultsdictionary do not appear asfield_names, raise aSyntaxErrorwith an appropriate message.

def pnamedtuple(type_name, field_names, mutable= False, defaults= {}): def show_listing(s): for line_number, line_text in enumerate(s.split(' '),1): print(f' {line_number: >3} {line_text.rstrip()}') # put your code here 

kwlistmodule:

https://pastebin.com/rfdct70L

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions

Question

\f

Answered: 1 week ago

Question

Explain about operations on Data Structure?

Answered: 1 week ago