LSRC Name Picker (#129)

On Jun 24, 2007, at 12:40 PM, Morton G. wrote:

  1. The app automatically runs each time it is started up and it
    picks one winner. To pick another, the app is relaunched.

Which behavior is wanted?

I meant each time your application is started (#2). I can see where
#1 might be attractive for something like a web application, but I
have a hard time imagining that the program will run non-stop for two
days.

James Edward G. II

On Jun 24, 2007, at 12:28 AM, Ruby Q. wrote:

  • Each time your application is run, it should select and display
    a single name from the list. Your application should remember
    previously selected names and not choose them a second time.

There are two ways to interpret ‘run’:

  1. The app has a Pick button and a new winner is selected each time
    it is clicked.

  2. The app automatically runs each time it is started up and it picks
    one winner. To pick another, the app is relaunched.

Which behavior is wanted? I ask because remembering previous winners
between launches makes the quiz somewhat more difficult. Extra points
for the 1st behavior combined with the winner list persistence of the
2nd?

Regards, Morton

James Edward G. II wrote:

  1. The app has a Pick button and a new winner is selected each time it

James Edward G. II

Yeah … and typically from the two Ruby conferences I’ve been to, it
may not even run on the same laptop twice in a row. That’s one more
organizational detail. :slight_smile:

On 6/24/07, Morton G. [email protected] wrote:

Regards, Morton
Honestly I am rude Morton, because I am jealous ;). I have read your
recent quiz submissions, they are lightyears above my level, what are
you asking here?
Just solve the problem :wink:

Seriously now:
I read the Quiz in a way that winner persistence is explicitly asked
for and I really like this part.

Cheers
Robert

On Mon, Jun 25, 2007 at 01:16:38AM +0900, James Edward G. II wrote:

On Jun 23, 2007, at 11:29 PM, James Edward G. II wrote:

Sorry the quiz was late this week.
[…]
It’s my fault this week’s quiz was two days late, so it will run for
an extra week to give people the time they need.

Sorry for the inconveniences.

So, er, when does the spoiler moratorium period end this time? Next
Sunday?

James Edward G. II
–Greg

On Jun 24, 2007, at 7:22 PM, Gregory S. wrote:

Sunday?
48 hours from the time on the quiz message, just like always.

James Edward G. II

On 6/24/07, Morton G. [email protected] wrote:

On Jun 24, 2007, at 4:51 PM, Robert D. wrote:

On 6/24/07, Morton G. [email protected] wrote:
My extra points idea was to allow an arbitrary number of drawings to
be made at one time then sometime later allow more to be mode.
Psst, already implemented – well with certain Plugins :slight_smile:

Robert

On Jun 24, 2007, at 4:51 PM, Robert D. wrote:

for the 1st behavior combined with the winner list persistence of the
2nd?

Regards, Morton

Seriously now:
I read the Quiz in a way that winner persistence is explicitly asked
for and I really like this part.

I just thought that they might award all the prizes in one session
with multiple drawings, not one at a time with fairly long intervals
between drawings. But, as James has replied, your interpretation is
the correct one.

My extra points idea was to allow an arbitrary number of drawings to
be made at one time then sometime later allow more to be mode.

Regards, Morton

I assume the 48 hour deadline is over. I made a fun little camping
app, however its 300k zipped up (has a couple images). I assume it
would be poor form to post it to the list, but I currently don’t have
a publicly facing server where I can put it. What shall I do?

Carl P.

My solution uses SQLite to store a DB of names, and RMagick to display
chosen
names. Consider the display part a first draft - I think I’ll redo it,
maybe
in a completely different way - and submit again later, though the
backend
shouldn’t change.

There are 4 files that roughly follow MVC:

names_db.rb: Wrapper around a SQLite::Database (M)
pick.rb: Main script (C)
pickerface.rb: Interface stuff (V)
name_image.rb: RMagick stuff (V)

The DB stores just one thing: names. When a name is chosen, its simply
deleted.
I thought of marking the names as either picked or unpicked, or storing
chosen
names in another table, but I’m not sure if that would be used, and in
any
event it would be easy enough to add if needed.

When pick.rb is run without a command option, it either creates a DB and
starts
adding names if the DB doesn’t exist, or picks and displays a name if it
does.
See the top of pick.rb, or run pick.rb --help for a full usage message.

For all text-mode-only operations, names can be Unicode, but RMagick
doesn’t
seem to like that, so they can’t be displayed nicely.
Magick::ImageList.animate
is used for displaying, so it won’t work for any name on Windows.

I bracket chosen names in stars taken from:

http://lonestarrubyconf.com/stylesheets/lsrc_2007/images/LSRC_big_title.jpg

I’m not sure this list will accept messages containing graphics, so
you’ll
have to fetch this image and place it in the same directory as my code:

http://www.jessemerriman.com/images/ruby_quiz/129_lsrc_name_picker/star_small.png

On Jun 26, 2007, at 3:40 AM, Carl P. wrote:

I assume the 48 hour deadline is over. I made a fun little camping
app, however its 300k zipped up (has a couple images). I assume it
would be poor form to post it to the list, but I currently don’t have
a publicly facing server where I can put it. What shall I do?

You may email it to me off-list and I’ll host it for you.

James Edward G. II

On 6/24/07, Ruby Q. [email protected] wrote:

The LSRC organizers would like us to build name picking applications they can
choose from. The functionality is very basic:

My design goals were:

  • What can I do in an hour?
  • How can I avoid graphics design, which I suck at?
  • How can I minimize (to zero, if possible) gems, add-ons, etc?

My solution is a dead simple WEBrick servlet thingy that poaches the
stylesheet and images from the conference website. It uses a YAML
input file of names and attributes, and an ERB template to display the
result. Every time you reload the page you get a new name.

It keeps track of the names that have been served (saved to a file, so
it survives a restart of the app). I wasn’t sure what to do when it
ran out of names, so I’m currently just clearing the save file and
restarting the serving of names (if there are more door prizes than
attendees?).

Here’s the app: Parked at Loopia (save as name_picker.rb)

Here’s a sample data file: Parked at Loopia (save as
names.yml)

Run the app: ruby name_picker.rb names.yml

Point your browser to http://localhost:4242/

Hi,

my minimalistic approach uses the main.rb as data storage with the
END keyword. So I don’t need any 3rd party libs doing the
persistence stuff for me. I also tried to build a GUI but shattered a
whole day on wxRuby.

Features:

  • persitence + test suite

  • multiple picks

    ruby main.rb --pick 2

  • eats everything, not just names, just needs TSV with header (see
    example.tsv)

  • no sql, no extra files

  • reads Tab-Separated-Values from stdin or file

    cat example.tsv | ruby main.rb --list unlucky
    ruby main.rb --list unlucky --from example.tsv

What so ever, have a try… :smiley:

Cheers
Florian

Florian Aßmann schrieb:

ruby main.rb --pick 2

What so ever, have a try… :smiley:

Cheers
Florian

I found a flaw in lucky_filter.rb when processing empty values, just
replace at lucky_filter.rb#20:

  •    @attendees[ key ] = row.map { |value| value.strip }
    
  •    @attendees[ key ] = row.map { |value| value.to_s.strip }
    

God bless duck typing :smiley:

Sincerely
Florian

Hi Jesse,

definitely a very nice idea to doing the r magic stuff :smiley:

Regards
Florian

Jesse M. schrieb:

Hi James,

I refactored my code a little, so the e-mail I sent directly to the ml
should be the richt (better) one. :smiley:

Thanks for hosting the code :slight_smile:

Regards
Florian

This is a simple camping app that uses RMagick to generate an animated
gif for the picked names.

To get going, place names (one per line) in the names file (there is
sample data there for you already).
Run “camping name_picker.rb” at the command line.
Go to http://localhost:3301/ in your browser and be appalled.

It can be found at:
http://rubyquiz.com/hosted_solutions/129/carl/name_picker.zip

Carl P.

On Jun 26, 6:21 am, James Edward G. II [email protected]

I tried to submit my solution but the mailing list rejected it as too
big (216 KB). What do I do now? Anybody got a suggestion for a work-
around?

Regards, Morton

P.S. The reason it’s so big is because it has a GUI that uses several
of GIF files to do a little animation.

On Jun 29, 2007, at 8:19 PM, Morton G. wrote:

I tried to submit my solution but the mailing list rejected it as
too big (216 KB). What do I do now? Anybody got a suggestion for a
work-around?

You may send it to me off-list. I will host it from the Ruby Q. site.

James Edward G. II

On Jun 23, 11:28 pm, Ruby Q. [email protected] wrote:

The three rules of Ruby Q.:

Here is my sollution, using Tk. It is not very fancy, as uses very
basic GUI, but is what I had time for. I will probably make it fancy,
I hope I can get it on time. If someone wants to add some eye’candy,
go on (as long as it credits the original). After all, it’s for a good
cause for Ruby ;D.


#! /usr/bin/ruby

Ruby Q. 129 - Name Picker

Author: Ruben M. [email protected]

require ‘csv’

def read_names_csv( filename )
table = CSV::parse(File.read(filename)) rescue table =
CSV::parse( filename )
titles = table.shift
return titles, table
end

def get_name( data_row )

Override if the format is different.

data_row[0…1].join(’ ')
end

class TkRoulette

require ‘tk’

attr_accessor :prizes
attr_accessor :contestants

COLORS = [:blue, :red, :green, :orange, :yellow, :pink]

def initialize( contestants = [], prizes = nil )
@contestants = contestants
@prizes = prizes
@winners = []
initialize_gui
Tk.mainloop
end

def initialize_gui
@root = TkRoot.new {
title “Ruby Q. 129 - Name Picker”
}
@name = TkLabel.new {
text ‘Press PLAY to start’
relief :groove
width 100
height 10
font :size => 60, :weight => :bold
pack :side => :top, :expand => true, :fill => :x
}
@play = TkButton.new {
text ‘PLAY!’
width 100
height 3
font :size => 30, :weight => :bold
pack :side => :bottom, :fill => :x
}.command method(:play).to_proc
end

def play( category = nil )
if @contestants.empty?
show_message( “No more contestants” )
return
end
@tick_time = 200
pick_winner
end

def pick_winner
if @tick_time <= 0
winner = @contestants.delete_at(rand(@contestants.size))
@winners << winner
show_name( "Winner is: " + get_name(winner) + “!!” )
return
end
Tk.after(200 - @tick_time, method(:pick_winner).to_proc )
@tick_time -= 5
show_name( get_name( @contestants[rand(@contestants.size)]) )
end

def show_name( name )
@name.fg COLORS[rand(COLORS.size)]
@name.text name
end

def show_message( message )
@name.fg :black
@name.text message
end

end

titles, data = read_names_csv( <<-CSV )
“First name”,“Last name”,“Organization”,“Mail”
“Fred”,“Flinstone”,“Slate Rock and Gravel
Company”,“[email protected]
“Homer”,“Simpson”,“Sprinfield’s Nuclear Power Plant”,“[email protected]
“George”,“Jetson”,“Spacely’s Space
Sprockets”,“[email protected]
“Elmer”,“Food”,“Acme”,“[email protected]
CSV
#read_names_csv(‘attendants.csv’)

if FILE == $0
roulette = TkRoulette.new(data)
end