Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Showing posts with label UTF-8. Show all posts
Showing posts with label UTF-8. Show all posts

Thursday, 18 December 2014

Reading and writing to a csv file ruby-processing (version two)

I find it really surprising that one of most popular posts for ruby-processing is the original version here is an alternative slightly cleaner version:-
#
# Loading Tabular Data
# after Daniel Shiffman, by Martin Prout.
#
# This example demonstrates how to use CSV
# to retrieve data from a CSV file and make objects
# from that data.
#
# Here is what the CSV looks like:
#
#   x,y,diameter,name
#   160,103,43.19838,Happy
#   372,137,52.42526,Sad
#   273,235,61.14072,Joyous
#   121,179,44.758068,Melancholy
#
require 'csv'

load_library 'bubble'

attr_reader :bubbles, :data

def setup
  size(640, 360)
  load_data
end

def draw
  background(255)
  # Display all bubbles
  bubbles.run
  text_align(LEFT)
  fill(0)
  text('Click to add bubbles.', 10, height - 10)
end

def load_data
  # Load CSV file into an Array of Hash objects
  # headers: option indicates the file has a header row
  @bubbles = BubbleData.new
  CSV.foreach('data/data.csv', headers: true) do |row|
    x = row['x'].to_f
    y = row['y'].to_f
    d = row['diameter'].to_f
    n = row['name']
    # Make a Bubble object out of the data read
    bubbles << Bubble.new(x, y, d, n)
  end
end

def mouse_pressed
  bubbles << Bubble.new(mouse_x, mouse_y, rand(40..80), 'blah')
  # If there are more than 10 bubbles delete the oldest bubble
  bubbles.shift if bubbles.size > 10
  # Writing the csv data back to the same file, (also specify UTF-8 format)
  CSV.open('data/data.csv', 'w:UTF-8') do |csv|
    csv << %w(x y diameter name) # create csv headers
    bubbles.each do |bubble|
      csv << bubble.to_a       # write back bubble data
    end
  end
  # And reloading it
  load_data
end

# A run module
module Runnable
  def run
    each(&:display)
    each { |item| item.rollover(mouse_x, mouse_y) }
  end
end

# Enumerable class holds bubble data
class BubbleData
  extend Forwardable
  def_delegators(:@bubbles, :each, :<<, :size, :shift)
  include Enumerable, Runnable

  def initialize
    @bubbles = []
  end
end

Wednesday, 18 September 2013

Reading and writing to a csv file ruby-processing

Whilst I was on a roll I thought I would rubify the load_table processing example, there really is no need to use processings convenience method load_table, we can do it all in pure ruby:-
#
# Loading Tabular Data
# after Daniel Shiffman, by Martin Prout.  
# 
# This example demonstrates how to use CSV
# to retrieve data from a CSV file and make objects 
# from that data.
#
# Here is what the CSV looks like:
#
#   x,y,diameter,name
#   160,103,43.19838,Happy
#   372,137,52.42526,Sad
#   273,235,61.14072,Joyous
#   121,179,44.758068,Melancholy
#
require 'csv'

load_library 'bubble'

attr_reader :bubbles, :data

def setup
  size(640, 360)
  load_data
end

def draw
  background(255)
  # Display all bubbles
  bubbles.each do |b|
    b.display
    b.rollover(mouse_x, mouse_y)
  end

  text_align(LEFT)
  fill(0)
  text('Click to add bubbles.', 10, height - 10)
end

def load_data
  # Load CSV file into an Array of Hash objects
  # :headers option indicates the file has a header row
  @data = CSV.read('data/data.csv', :headers => true).map{|row| row.to_hash}

  # The size of the array of Bubble objects is determined by the total number of 'rows' in the CSV
  @bubbles = []

  data.each do |row|
    # You access the values via their column name (set by using headers option above)
    x = row['x'].to_f
    y = row['y'].to_f
    d = row['diameter'].to_f
    n = row['name']
    # Make a Bubble object out of the data read
    bubbles << Bubble.new(x, y, d, n)
  end

end

def mouse_pressed
  # Create a new 'row' hash
  row = {'x' => mouse_x.to_s, 'y' => mouse_y.to_s, 'diameter' => rand(40..80).to_s, 'name' => 'Blah'}
  # add the row to the existing data array
  data << row
  # If the table has more than 10 rows 
  data.shift if (data.size > 10) # Delete the oldest row
  # read column names from data, and generate csv 'string' that can be written to file  
  column_names = data.first.keys
  s = CSV.generate do |csv|
    csv << column_names
    data.each do |row|
      csv << row.values
    end
  end
  # Writing the csv data back to the same file, (also specify UTF-8 format)
  File.open('data/data.csv', 'w:UTF-8') { |file| file.write(s)}
  # And reloading it
  load_data
end

NB: For a more upto date version see JRubyArt version

Followers

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2