项目作者: varadchoudhari

项目描述 :
A simple Python implementation to pretty print dictionaries
高级语言: Python
项目地址: git://github.com/varadchoudhari/Neat-Dictionary.git
创建时间: 2017-11-17T04:36:20Z
项目社区:https://github.com/varadchoudhari/Neat-Dictionary

开源协议:MIT License

下载


Neat-Dictionary

A simple Python implementation to pretty print dictionaries

Supports

  • Python 2
  • Python 3

Features

  • Set column width
  • Support for multi-line dictionaries
  • Sort by column

What’s inside

  1. neat-dictionary-fixed-column-width.py
    • neat_printer
    • neat_writer
  2. neat-dictionary-custom-column-width.py
    • neat_printer
    • neat_writer
  3. neat-multiline-custom-width.py
    • multiline_printer
    • multiline_writer
  4. neat-multiline-fixed-width.py
    • multiline_printer
    • multiline_writer
  5. neat-sorted-custom-width.py
    • neat_printer
    • neat_writer

How to use

neat-dictionary-fixed-column-width.py

neat_printer(dictionary_variable, [list_of_titles], column_width)

  1. dictionary = {egg: ["Qty. 1", 1],
  2. bread: ["Qty. 2", 2]}
  1. neat_printer(dictionary,["Item","Quantity","Price"],10)

OUTPUT

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. bread Qty. 2 2
  1. neat_printer(dictionary,["Item","Quantity","Price"],20)

OUTPUT when width == 20:

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. bread Qty. 2 2

neat_writer(dictionary_variable, [list_of_titles], column_width, output_file)

  1. dictionary = {egg: ["Qty. 1", 1],
  2. bread: ["Qty. 2", 2]}
  1. file = open("neat_writing.txt", "w")
  2. neat_writer(dictionary, ["Item","Quantity","Price"], 10, file)

OUTPUT FILE:

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. bread Qty. 2 2

neat-dictionary-custom-column-width.py

neat_printer(dictionary_variable, [list_of_titles], [list_of_column_width])

  1. dictionary = {egg: ["Qty. 1", 1],
  2. bread: ["Qty. 2", 2]}
  1. neat_printer(dictionary,["Item","Quantity","Price"],[6,10,10])

OUTPUT

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. bread Qty. 2 2

neat_writer(dictionary_variable, [list_of_titles], [list_of_column_width], file)

  1. dictionary = {egg: ["Qty. 1", 1],
  2. bread: ["Qty. 2", 2]}
  1. file = open("neat_writing.txt", "w")
  2. neat_writer(dictionary, ["Item","Quantity","Price"], [6,10,10], file)

OUTPUT FILE:

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. bread Qty. 2 2

neat-multiline-custom-width.py

multiline_printer(dictionary_variable, [list_of_titles], [list_of_column_width])

  1. dictionary = {"egg":[["Qty. 1",1], ["Qty. 2",2], ["Qty. 4",3]],
  2. "bread":[["Qty. 2",2], ["Qty. 3",3]]}
  1. multiline_printer(dictionary,["Item","Quantity","Price"],[6,10,10])

OUTPUT

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. egg Qty. 2 2
  4. egg Qty. 4 3
  5. bread Qty. 2 2
  6. bread Qty. 3 3

multiline_writer(dictionary_variable, [list_of_titles], [list_of_column_width], output_file)

  1. dictionary = {"egg":[["Qty. 1",1], ["Qty. 2",2], ["Qty. 4",3]],
  2. "bread":[["Qty. 2",2], ["Qty. 3",3]]}
  1. file = open("multiline_writing.txt", "w")
  2. multiline_writer(dictionary,["Item","Quantity","Price"],[6,10,10],file)

OUTPUT FILE

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. egg Qty. 2 2
  4. egg Qty. 4 3
  5. bread Qty. 2 2
  6. bread Qty. 3 3

neat-multiline-fixed-width.py

multiline_printer(dictionary_variable, [list_of_titles], column_width)

  1. dictionary = {"egg":[["Qty. 1",1], ["Qty. 2",2], ["Qty. 4",3]],
  2. "bread":[["Qty. 2",2], ["Qty. 3",3]]}
  1. multiline_printer(dictionary,["Item","Quantity","Price"],10)

OUTPUT

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. egg Qty. 2 2
  4. egg Qty. 4 3
  5. bread Qty. 2 2
  6. bread Qty. 3 3

multiline_writer(dictionary_variable, [list_of_titles], column_width, output_file)

  1. dictionary = {"egg":[["Qty. 1",1], ["Qty. 2",2], ["Qty. 4",3]],
  2. "bread":[["Qty. 2",2], ["Qty. 3",3]]}
  1. file = open("multiline_writing.txt", "w")
  2. multiline_writer(dictionary,["Item","Quantity","Price"],10,file)

OUTPUT

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. egg Qty. 2 2
  4. egg Qty. 4 3
  5. bread Qty. 2 2
  6. bread Qty. 3 3

neat-sorted-custom-width.py

neat_printer(dictionary_variable, [list_of_titles], [list_of_column_width], title_to_sort_by)

  1. dictionary = {egg: ["Qty. 1", 1],
  2. bread: ["Qty. 2", 2]}
  1. neat_printer(dictionary,["Item","Quantity","Price"],[6,10,10],"Item")

OUTPUT

  1. Item Quantity Price
  2. bread Qty. 2 2
  3. egg Qty. 1 1
  1. neat_printer(dictionary,["Item","Quantity","Price"],[6,10,10],"Price")

OUTPUT

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. bread Qty. 2 2

neat_writer(dictionary_variable, [list_of_titles], [list_of_column_width], title_to_sort_by)

  1. dictionary = {egg: ["Qty. 1", 1],
  2. bread: ["Qty. 2", 2]}
  1. file = open("neat_writer.txt", "w")
  2. neat_writer(dictionary,["Item","Quantity","Price"],[6,10,10],file,"Item")

OUTPUT FILE

  1. Item Quantity Price
  2. bread Qty. 2 2
  3. egg Qty. 1 1
  1. file = open("neat_writer.txt", "w")
  2. neat_printer(dictionary,["Item","Quantity","Price"],[6,10,10],file,"Price")

OUTPUT FILE

  1. Item Quantity Price
  2. egg Qty. 1 1
  3. bread Qty. 2 2