awesome_print

Awesome Print for Python
Download

awesome_print Ranking & Summary

Advertisement

  • Rating:
  • License:
  • MIT/X Consortium Lic...
  • Price:
  • FREE
  • Publisher Name:
  • Stan Mazhara
  • Publisher web site:
  • https://github.com/michaeldv/

awesome_print Tags


awesome_print Description

awesome_print is a clone of Awesome Print in Python.Awesome Print is a Ruby library that pretty prints Ruby objects in full color exposing their internal structure with proper indentation. Rails ActiveRecord objects and usage within Rails templates are supported via included mixins.Installation# Installing as Ruby gem gem install awesome_print# Cloning the repository git clone git://github.com/michaeldv/awesome_print.gitUsagerequire "awesome_print"ap object, options = {}Default options::indent => 4, # Indent using 4 spaces.:index => true, # Display array indices.:html => false, # Use ANSI color codes rather than HTML.:multiline => true, # Display in multiple lines.:plain => false, # Use colors.:sort_keys => false, # Do not sort hash keys.:limit => false, # Limit large output for arrays and hashes. Set to a boolean or integer.:color => { :args => :pale, :array => :white, :bigdecimal => :blue, :class => :yellow, :date => :greenish, :falseclass => :red, :fixnum => :blue, :float => :blue, :hash => :pale, :keyword => :cyan, :method => :purpleish, :nilclass => :red, :string => :yellowish, :struct => :pale, :symbol => :cyanish, :time => :greenish, :trueclass => :green, :variable => :cyanish}Supported color names::gray, :red, :green, :yellow, :blue, :purple, :cyan, :white:black, :redish, :greenish, :yellowish, :blueish, :purpleish, :cyanish, :paleExamples$ cat > 1.rbrequire "awesome_print"data = ap data^D$ ruby 1.rb false, 42, "forty", "two" ], { :class => Time < Object, :now => Fri Apr 02 19:55:53 -0700 2010, :distance => 4.2e+43 }]$ cat > 2.rbrequire "awesome_print"data = { :now => Time.now, :class => Time.now.class, :distance => 42e42 }ap data, :indent => -2 # < -- Left align hash keys.^D$ ruby 2.rb{ :class => Time < Object, :now => Fri Apr 02 19:55:53 -0700 2010, :distance => 4.2e+43}$ cat > 3.rbrequire "awesome_print"data = data false^D$ ruby 3.rb, ]$ cat > 4.rbrequire "awesome_print"class Hello def self.world(x, y, z = nil, &blk) endendap Hello.methods - Class.methods^D$ ruby 4.rb world(x, y, *z, &blk) Hello]$ cat > 5.rbrequire "awesome_print"ap (''.methods - Object.methods).grep(/!/)^D$ ruby 5.rb capitalize!() String chomp!(*arg1) String chop!() String delete!(*arg1) String downcase!() String encode!(*arg1) String gsub!(*arg1) String lstrip!() String next!() String reverse!() String rstrip!() String slice!(*arg1) String squeeze!(*arg1) String strip!() String sub!(*arg1) String succ!() String swapcase!() String tr!(arg1, arg2) String tr_s!(arg1, arg2) String upcase!() String]$ cat > 6.rbrequire "awesome_print"ap 42 == ap(42)^D$ ruby 6.rb42true$ cat 7.rbrequire "awesome_print"some_array = (1..1000).to_aap some_array, :limit => true^D$ ruby 7.rb 1, 2, 3, .. , 998, 999, 1000]$ cat 8.rbrequire "awesome_print"some_array = (1..1000).to_aap some_array, :limit => 5^D$ ruby 8.rb 1, 2, .. , 999, 1000]Example (Rails console) rails consolerails> require "awesome_print"rails> ap Account.all(:limit => 2) #< Account:0x1033220b8 > { :id => 1, :user_id => 5, :assigned_to => 7, :name => "Hayes-DuBuque", :access => "Public", :website => "http://www.hayesdubuque.com", :toll_free_phone => "1-800-932-6571", :phone => "(111)549-5002", :fax => "(349)415-2266", :deleted_at => nil, :created_at => Sat, 06 Mar 2010 09:46:10 UTC +00:00, :updated_at => Sat, 06 Mar 2010 16:33:10 UTC +00:00, :email => "info@hayesdubuque.com", :background_info => nil }, #< Account:0x103321ff0 > { :id => 2, :user_id => 4, :assigned_to => 4, :name => "Ziemann-Streich", :access => "Public", :website => "http://www.ziemannstreich.com", :toll_free_phone => "1-800-871-0619", :phone => "(042)056-1534", :fax => "(106)017-8792", :deleted_at => nil, :created_at => Tue, 09 Feb 2010 13:32:10 UTC +00:00, :updated_at => Tue, 09 Feb 2010 20:05:01 UTC +00:00, :email => "info@ziemannstreich.com", :background_info => nil }]rails> ap Accountclass Account < ActiveRecord::Base { :id => :integer, :user_id => :integer, :assigned_to => :integer, :name => :string, :access => :string, :website => :string, :toll_free_phone => :string, :phone => :string, :fax => :string, :deleted_at => :datetime, :created_at => :datetime, :updated_at => :datetime, :email => :string, :background_info => :string}rails >IRB integrationTo use awesome_print as default formatter in irb and Rails console add the following code to your ~/.irbrc file:require "rubygems"require "awesome_print"unless IRB.version.include?('DietRB') IRB::Irb.class_eval do def output_value ap @context.last_value end endelse # MacRuby IRB.formatter = Class.new(IRB::Formatter) do def inspect_object(object) object.ai end end.newendPRY integrationIf you miss awesome_print's way of formatting output, here's how you can use it in place of the formatting which comes with pry. Add the following code to your ~/.pryrc:require "rubygems"require "awesome_print"Pry.print = proc { |output, value| output.puts value.ai }Logger Convenience Methodawesome_print adds the 'ap' method to the Logger and ActiveSupport::BufferedLogger classes letting you call:logger.ap objectBy default, this logs at the :debug level. You can override that globally with::log_level => :infoin the custom defaults (see below). You can also override on a per call basis with:logger.ap object, :warnActionView Convenience Methodawesome_print adds the 'ap' method to the ActionView::Base class making it available within Rails templates. For example:< %= ap @accounts.first % > # ERB!= ap @accounts.first # HAMLWith other web frameworks (ex: in Sinatra templates) you can explicitly request HTML formatting:< %= ap @accounts.first, :html => true % >Setting Custom DefaultsYou can set your own default options by creating .aprc file in your home directory. Within that file assign your defaults to AwesomePrint.defaults. For example:# ~/.aprc file.AwesomePrint.defaults = { :indent => -2, :color => { :hash => :pale, :class => :white }}Running Specs gem install rspec # RSpec 2.x is the requirement. rake spec # Run the entire spec suite. rspec spec/logger_spec.rb # Run individual spec file.Product's homepage


awesome_print Related Software