Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!