Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
Separating detailed data code from the main application - Ruby example

In almost every appication, you'll be writing a number of pieces of code that use the same data type. It's tempting initially to write all the detail of the individual fields and attributes of the data into a main program - especially if the application's not very long and you're in a hurry:

  fh = File.new "ac_20160516"
  
  counter = {}
  
  fh.each do |record|
    parts = record.split " "
    if parts[1] == "option247.uk"
      page = parts[6]
  
      if counter[page].nil?
        counter[page] = 1
      else
        counter[page] += 1
      end
      end
    end
  
  pages = counter.keys
  pages.sort!  {|this,that| this.length <=> that.length}
  
  pages.each do |page|
    puts "#{page} #{counter[page]}"
    end
  


Which works well enough, but ties the program down to a specific data format and implements the algorithms individually. If your data records are sometime available in a different way, you need to rewrite all the code, and if you want to use the counter technique elsewhere, you'll have to recode that too. Better to write the code a little longer and clearer and abstact the algorithms and data formats into separate classes.


  class Counter
    def initialize()
      @internal = {}
    end
  
    def count(item)
      @internal[item] = @internal[item].nil? ? 1 : @internal[item]+1
    end
  
    def getKeys()
      return @internal.keys.sort { |this,that| @internal[this] <=> @internal[that]}
    end
  
    def getKey(which)
      return @internal[which]
    end
  end
  class Hit
    def initialize(record)
      @fields = record.chomp.split(" ")
    end
    def issite?(which)
      return @fields[1] == which
    end
    def url
      return @fields[6]
    end
  end
  # ----------------------
  
  if __FILE__ == $0
    fh = File.new "ac_20160516"
    counter = Counter.new
  
    fh.each do |record|
      hit = Hit.new(record)
      parts = record.split " "
      if hit.issite?("option247.uk")
        counter.count(hit.url)
        end
      end
  
    pages = counter.getKeys
    pages.each do |page|
      puts "#{page} #{counter.getKey(page)}"
      end
  
    end


Complete source code of this example - the before [here] and the after [here]. From the Ruby Course I am presenting this week. The datafile is available [here].
(written 2016-05-16, updated 2016-05-17)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q908 - Object Orientation and General technical topics - Object Orientation: Design Patterns
  [485] North, Norther and Northest - PHP 5 Objects - (2005-11-04)
  [1224] Object Relation Mapping (ORM) - (2007-06-09)
  [2322] Looking for a practical standards course - (2009-08-05)
  [2741] What is a factory? - (2010-04-26)
  [2977] What is a factory method and why use one? - Example in Ruby - (2010-09-30)
  [3608] Design Patterns - what are they? Why use them? - (2012-02-12)
  [3716] Learning C++ - a design pattern for your first class - (2012-05-02)
  [3810] Reading files, and using factories to create vectors of objects from the data in C++ - (2012-07-21)
  [3843] Caching Design Patterns - (2012-08-20)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [4096] Perl design patterns example - (2013-05-20)
  [4098] Using object orientation for non-physical objects - (2013-05-22)
  [4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
  [4330] Java - factory method, encapsulation, hashmap example - (2014-11-27)
  [4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
  [4359] How to avoid too many recalculations within an object - (2014-12-21)
  [4377] Designing a base class and subclasses, and their extension, in C++ - (2015-01-01)
  [4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
  [4421] How healthy are the stars of stage and screen? - (2015-02-09)
  [4581] Thin application, thick objects - keep you main code simple. Example in Ruby - (2015-11-21)
  [4626] Singleton design pattern - examples and uses - (2016-01-20)
  [4663] Easy data to object mapping (csv and Python) - (2016-03-24)

R102 - Hello Ruby Objects
  [2287] Learning to program in Ruby - examples of the programming basics - (2009-07-15)
  [2603] Ruby objects - a primer - (2010-01-29)
  [4009] Clear, concise examples - Ruby classes and objects. - (2013-02-17)
  [4369] Ruby - the second rung of learning the language - (2014-12-28)
  [4580] Easy example - data record to object and object to data record mapping in Ruby - (2015-11-21)


Back to
Annual review of Melksham Matters for Chamber of Commerce AGM
Previous and next
or
Horse's mouth home
Forward to
Alternating valuses / flip-flop / toggle - example in Ruby
Some other Articles
Swindon, Chippenham and Melksham - day trips by train to Weymouth start for 2016
Running shell (operating system) commands from within Ruby
Exceptions in Ruby - throwing, catching and using
Alternating valuses / flip-flop / toggle - example in Ruby
Separating detailed data code from the main application - Ruby example
Annual review of Melksham Matters for Chamber of Commerce AGM
Ruby training, half a world away
The end, or the start?
Buses - how did we get into the situation we are in?
Sorting a dict in Python
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4673_Sep ... ample.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb