Tech Blog

All technical-related stuff.

Java Applets: drawing lines on .png graphics

Example allowing to draw on a .PNG files. This java applet effectively
loads .PNG file as a background and allows for drawing straight lines.
Tiny functionality but it is especially useful to review equities.
Below code is reworked code from Java Applet tutorial: http://profs.etsmtl.ca/mmcguffin/learn/java
I had to write it quickly when I was reviewing lots of charts of
companies listed on Warsaw Stock Exchange.

How to use it? Put test2.class together with view.htm file.
In browser open: view.htm?fname=name_of_file.png

Ruby on Rails: controller/view/passing args

I went through examples based on Ruby on Rails
book by dr Steve Holzner and below are some interesting notes:

Below controller describes three views: lunch, work and zzz;
Even if work is called at 2pm (http://127.0.0.1:3000/do/work) - it
will render and output lunch. If lunch is called
(http://127.0.0.1:3000/do/lunch) - it will be always rendered
properly. There will be no HTTP redirection or anything like that.
Finally when zzz is called (http://127.0.0.1:3000/do/zzz)- it
will render a file that is in app/views/do/test1.rhtml;

class DoController < ApplicationController

Ruby on Rails: first project notes

Following creates project, controller and run HTTP server (default port: 3000)

rails first
cd first
ruby script/generate controller Somecontroller
ruby script/server

Edit Somecontroller controller responses:

vi app/controllers/Somecontroller_controller.rb

Add views (there.rhtml) for Somecontroller:

class HelloController < ApplicationController
++ def there
++ end
end

Creating view for Somecontroller and running view in webbrowser:

cd apps/views
echo "<html>Hello first test</html>" >> there.rhtml

Ruby: various[tricks]

Problem: Displaying number of companies that starts with same
letter at one page can cause problems - sometimes too much data on one
page are not displayed properly. It is good to have companies starting
with one letter on one page; So requirement is to:
- figure out letter on which most companies will start; name it as MAX
- MAX must be a maximum of companies displayed on one page
- one page should contain as many companies as it is possible in alphabetical order, but not more than MAX number of companies on that page

- So e.g. MAX is 47 [47 companies starting with letter P]

Bash: Arrays, resizing files

Basics of arrays

Lists all files into array a

a=( $( ls . ) )

Prints 5th element of array a (5th file):
echo ${a[5]}

To list all bash components

echo ${a[@]}

To iterate through whole components:

for i in "${a[@]}"
do
  echo $i
done

Arrays and folders

To iterate through all files in folder:

for i in $( ls ); do echo $i; done

Cropping and resizing files in folder

To crop all .png files in a folder:

for i in $( ls *.png );2
do

Ruby: Unzipping files

This short example download file and unzips all to data folder:
Note that it requires zip gem

gem install zip

Example source:

require 'rubygems'
require 'zip/zip'
require 'open-uri'
 
zip_file = "http://any_server.com/some.zip"
 
puts "Retrieving data..."
File.open("mstall.zip", "w") { |f| f.write(open(zip_file).read) }
 
puts "Unpacking data..."
Zip::ZipFile.open("mstall.zip") { |zip_file|
  zip_file.each { |f| 
    puts f.name
    zip_file.extract(f, "data/"+f.name) { true } # true = overwrite if file exists
  }
}

Python: WIG20 on a taskbar

I really didnt like concept of this abstract language
... till I managed to write program showing Warsaw's stock market index [WIG20].

It took me maybe 30 minutes during Python Course, overall
Grabs the data from http://gielda.onet.pl, strips the tags and shows in very tiny box boss wouldn't see...
This is how it looks:



Double click opens webpage with current graph.

And code for it:

import threading

Python: Graphic libraries

I came across two libraries recommended on StockOverflow for creating graphs and charts:
Most recommended was Cairo, but I could not really get it from ubuntu repository (strange??)
so without digging it deeper - I turned quickly to matplotlib - one well known.

Installation of python is realtively straight-forward and there is zillions of examples
on the internet. As I already had python on my system - I used following to install ipython
and matplotlib itself. ipython is an interactive python, which provides you important

Ruby: begining

To collect data from web I wanted a relatively simply
tool that can process easily retrieved HTML. Initially
bash curl and other U*NX tools such as awk or
sed can be enough to process data, but question is
always about flexibility: Bash does not provide
scalable objects that could be used as containers for
data structures; additionally extracting data from HTML
using regular expression is not an easy approach. My
experience with Python showed me that processing HTML
had some issues: it wasn't fault-tolerant.

Ruby: CSV to HTML

Quickly written code for conversion CSV to HTML.
It looks that using awk is a little bit more efficient,
however on the other hand - below code uses opens file
to write and handles input from command line (ARGV).

Following line:

line.strip.gsub(/\"/,"").gsub(/,$/,"").gsub(/,/,"</td><td>")

Drops whitespaces, ", and last commas of every line. Then replaces
commas with </td><td>.

Source code

if ARGV.count < 1
  puts "Usage:"
  puts "  csv2html.rb <csv filename> [output filename]"
  exit
end
 
ifname = ARGV[0]

Syndicate content