Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

โมดูล pprint (Pretty Printer ข้อมูล)

โมดูล pprint (lib/pprint.py) เป็นส่วนหนึ่งของไลบรารีมาตรฐานของ Python ซึ่งกระจายพร้อมกับการกระจาย Python มาตรฐาน ชื่อ pprint ย่อมาจาก pretty printer ฟังก์ชันของโมดูล pprint ทำให้โครงสร้างข้อมูล Python มีลักษณะที่ดูดีและสวยงาม โครงสร้างข้อมูลใดๆ ที่ตัวแปล Python แยกวิเคราะห์ได้ถูกต้อง จะถูกจัดรูปแบบอย่างสง่างาม นิพจน์ที่จัดรูปแบบแล้วถูกเก็บไว้ในบรรทัดเดียวเท่าที่เป็นไปได้ แต่จะแบ่งเป็นหลายบรรทัดหากความยาวเกินพารามิเตอร์ width ของการจัดรูปแบบ คุณสมบัติเฉพาะของเอาต์พุท pprint คือ ดิกชันนารีถูกจัดเรียงอัตโนมัติ ก่อนที่การแสดงผลจะถูกจัดรูปแบบ

โมดูล pprint มีการกำหนดคลาส PrettyPrinter คอนสตรัคเตอร์รับรูปแบบดังนี้:

pprint.PrettyPrinter(indent, width, depth, stream, compact)

พารามิเตอร์ indent กำหนดการเยื้องที่เพิ่มขึ้นในแต่ละระดับ recursive ค่าเริ่มต้นคือ 1

พารามิเตอร์ width ค่าเริ่มต้นคือ 80 เอาต์พุทที่ต้องการถูกจำกัดโดยค่านี้ หากความยาวมากกว่า width จะถูกแบ่งเป็นหลายบรรทัด

พารามิเตอร์ depth ควบคุมจำนวนระดับที่จะพิมพ์

พารามิเตอร์ stream ค่าเริ่มต้นคือ std.out - อุปกรณ์เอาต์พุทเริ่มต้น สามารถรับอ็อบเจ็กต์สตรีมใดๆ เช่น ไฟล์

พารามิเตอร์ compact ตั้งค่าเป็น False ตามค่าเริ่มต้น หากเป็น True จะแสดงเฉพาะข้อมูลที่ปรับเข้ากับ width ได้

คลาส PrettyPrinter กำหนดเมธอดต่อไปนี้:

pprint() - พิมพ์การแสดงผลที่จัดรูปแบบของอ็อบเจ็กต์ PrettyPrinter

pformat() - คืนค่าการแสดงผลที่จัดรูปแบบของอ็อบเจ็กต์ ตามพารามิเตอร์ของคอนสตรัคเตอร์

ตัวอย่างต่อไปนี้สาธิตการใช้งานง่ายของคลาส PrettyPrinter:

import pprint

students = {
    "Dilip": ["English", "Maths", "Science"],
    "Raju": {"English": 50, "Maths": 60, "Science": 70},
    "Kalpana": (50, 60, 70)
}

pp = pprint.PrettyPrinter()
print("normal print output")
print(students)
print("----")
print("pprint output")
pp.pprint(students)

เอาต์พุทแสดงการแสดงผลปกติและ pretty print:

normal print output
{'Dilip': ['English', 'Maths', 'Science'], 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}, 'Kalpana': (50, 60, 70)}
----
pprint output
{'Dilip': ['English', 'Maths', 'Science'],
 'Kalpana': (50, 60, 70),
 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}

โมดูล pprint ยังกำหนดฟังก์ชันสะดวก pprint() และ pformat() ที่สอดคล้องกับเมธอดของ PrettyPrinter ตัวอย่างด้านล่างใช้ฟังก์ชัน pprint():

from pprint import pprint

students = {
    "Dilip": ["English", "Maths", "Science"],
    "Raju": {"English": 50, "Maths": 60, "Science": 70},
    "Kalpana": (50, 60, 70)
}

print("normal print output")
print(students)
print("----")
print("pprint output")
pprint(students)

ตัวอย่างถัดไปใช้เมธอด pformat() และฟังก์ชัน pformat() เพื่อใช้เมธอด pformat() ต้องตั้งค่าอ็อบเจ็กต์ PrettyPrinter ก่อน ในทั้งสองกรณี การแสดงผลที่จัดรูปแบบจะถูกแสดงโดยฟังก์ชัน print() ปกติ:

import pprint

students = {
    "Dilip": ["English", "Maths", "Science"],
    "Raju": {"English": 50, "Maths": 60, "Science": 70},
    "Kalpana": (50, 60, 70)
}

print("using pformat method")
pp = pprint.PrettyPrinter()
string = pp.pformat(students)
print(string)
print('------')
print("using pformat function")
string = pprint.pformat(students)
print(string)

นี่คือเอาต์พุทของโค้ดข้างต้น:

using pformat method
{'Dilip': ['English', 'Maths', 'Science'],
 'Kalpana': (50, 60, 70),
 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}
------
using pformat function
{'Dilip': ['English', 'Maths', 'Science'],
 'Kalpana': (50, 60, 70),
 'Raju': {'English': 50, 'Maths': 60, 'Science': 70}}

Pretty printer ยังสามารถใช้กับคลาสที่กำหนดเองได้ ภายในคลาส เมธอด __repr__() ถูกโอเวอร์ไรด์ เมธอด __repr__() ถูกเรียกเมื่อใช้ฟังก์ชัน repr() นี่คือการแสดงสตริงอย่างเป็นทางการของอ็อบเจ็กต์ Python เมื่อเราใช้อ็อบเจ็กต์เป็นพารามิเตอร์ของฟังก์ชัน print() มันจะพิมพ์ค่าที่คืนจากฟังก์ชัน repr()

ในตัวอย่างต่อไปนี้ เมธอด __repr__() คืนค่าการแสดงสตริงของอ็อบเจ็กต์ player:

import pprint

class player:
    def __init__(self, name, formats=[], runs=[]):
        self.name = name
        self.formats = formats
        self.runs = runs

    def __repr__(self):
        dct = {}
        dct[self.name] = dict(zip(self.formats, self.runs))
        return (repr(dct))

l1 = ['Tests', 'ODI', 'T20']
l2 = [[140, 45, 39], [15, 122, 36, 67, 100, 49], [78, 44, 12, 0, 23, 75]]
p1 = player("virat", l1, l2)
pp = pprint.PrettyPrinter()
pp.pprint(p1)

เอาต์พุทของโค้ดข้างต้นคือ:

{'virat': {'Tests': [140, 45, 39], 'ODI': [15, 122, 36, 67, 100, 49], 'T20': [78, 44, 12, 0, 23, 75]}}

โครงสร้างข้อมูลเรียกซ้ำ (Recursive) กับ pprint

เมื่อเราพยายามพิมพ์อ็อบเจ็กต์เรียกซ้ำด้วย pprint จะแสดงเพียงการแสดงผลครั้งแรก และสำหรับการเรียกซ้ำต่อไป จะพิมพ์เพียงอ้างอิงของมันเท่านั้น:

>>> import pprint
>>> numbers = list(range(1, 6))
>>> numbers.append(numbers)
>>> print(numbers)
[1, 2, 3, 4, 5, [...]]
>>> pprint.pprint(numbers)
[1, 2, 3, 4, 5, ]

การจำกัดความกว้างของเอาต์พุท

หากเปลี่ยนพารามิเตอร์ width จากค่าเริ่มต้น 80 เป็นค่าอื่น เอาต์พุทจะถูกจัดรูปแบบให้แสดงหลายบรรทัดพร้อมดูแลไม่ให้ละเมิดไวยากรณ์:

import pprint

students = {
    "Dilip": ["English", "Maths", "Science"],
    "Raju": {"English": 50, "Maths": 60, "Science": 70},
    "Kalpana": (50, 60, 70)
}

pp = pprint.PrettyPrinter(width=20)
pp.pprint(students)

โค้ดคล้ายตัวอย่างแรกในบทความนี้ แต่สร้างอ็อบเจ็กต์ PrettyPrinter ด้วยพารามิเตอร์ width เป็น 20 ดังนั้นเอาต์พุท pprint จึงถูกจัดรูปแบบตามนั้น:

{'Dilip': ['English',
            'Maths',
            'Science'],
 'Kalpana': (50,
             60,
             70),
 'Raju': {'English': 50,
          'Maths': 60,
          'Science': 70}}