GithubHelp home page GithubHelp logo

varadchoudhari / neat-dictionary Goto Github PK

View Code? Open in Web Editor NEW
4.0 2.0 1.0 31 KB

A simple Python implementation to pretty print dictionaries

License: MIT License

Python 100.00%
python dictionary pretty-print pretty-printer

neat-dictionary's Introduction

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)

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

OUTPUT

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

OUTPUT when width == 20:

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

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

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

OUTPUT FILE:

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

neat-dictionary-custom-column-width.py

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

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

OUTPUT

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

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

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

OUTPUT FILE:

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

neat-multiline-custom-width.py

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

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

OUTPUT

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

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

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

OUTPUT FILE

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

neat-multiline-fixed-width.py

multiline_printer(dictionary_variable, [list_of_titles], column_width)

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

OUTPUT

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

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

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

OUTPUT

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

neat-sorted-custom-width.py

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

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

OUTPUT

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

OUTPUT

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

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

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

OUTPUT FILE

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

OUTPUT FILE

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.