Category: programming

Convert to Roman Numerals in Emacs Lisp

Sorry I can’t get the indents to export in org2blog. Maybe a configuration issue…

Just open it in Emacs, select the whole buffer, and intent-region

(defun ba-romani-numeri (number)
  "Provide the roman numeral for a number less than 4999"
(interactive)
(let* ((thousands (truncate (/ number 1000)))
       (thousands-remainder (- number (* 1000 thousands)))
       (roman-thousands (cond ((= 4 thousands) "MMMM")
                              ((= 3 thousands) "MMM")
                              ((= 2 thousands) "MM")
                              ((= 1 thousands) "M")
                              ((= 0 thousands) "")))
       (hundreds (truncate (/ thousands-remainder 100)))
       (hundreds-remainder (- thousands-remainder (* 100 hundreds)))
       (roman-hundreds (cond ((= 9 hundreds) "XM")
                             ((= 8 hundreds) "DCCC")
                             ((= 7 hundreds) "DCC")
                             ((= 6 hundreds) "DC")
                             ((= 5 hundreds) "D")
                             ((= 4 hundreds) "CD")
                             ((= 3 hundreds) "CCC")
                             ((= 2 hundreds) "CC")
                             ((= 1 hundreds) "C")
                             ((= 0 hundreds) "")))
       (tens (truncate (/ hundreds-remainder 10)))
       (tens-remainder (- hundreds-remainder (* 10 tens)))
       (roman-tens (cond ((= 9 tens) "XC")
                         ((= 8 tens) "LXXX")
                         ((= 7 tens) "LXX")
                         ((= 6 tens) "LX")
                         ((= 5 tens) "L")
                         ((= 4 tens) "XL")
                         ((= 3 tens) "XXX")
                         ((= 2 tens) "XX")
                         ((= 1 tens) "X")
                         ((= 0 tens) "")))
       (ones  tens-remainder)
       (roman-ones (cond ((= 9 ones) "IX")
                         ((= 8 ones) "VIII")
                         ((= 7 ones) "VII")
                         ((= 6 ones) "VI")
                         ((= 5 ones) "V")
                         ((= 4 ones) "IV")
                         ((= 3 ones) "III")
                         ((= 2 ones) "II")
                         ((= 1 ones) "I")
                         ((= 0 ones) ""))))
  (concat roman-thousands roman-hundreds roman-tens roman-ones)))
(ba-romani-numeri 2213)
;; "MMCCXIII"

(ba-romani-numeri 2013)
;; "MMXIII"

(ba-romani-numeri 999)
;; "XMXCIX"

(ba-romani-numeri 299)
;; "CCXCIX"

(ba-romani-numeri 99)
;; "XCIX"

(ba-romani-numeri 55)
;; "LV"

(ba-romani-numeri 35)
;; "XXXV"

(ba-romani-numeri 17)
;; "XVII"

(ba-romani-numeri 15)
;; "XV"

(ba-romani-numeri 5)
;; "V"
Advertisements

Compojure Web Framework

Compojure is a Web Framework written in Clojure. I’m interested in trying it out because Clojure is a a Lisp and currently lisp is the the only language I like to use for programming. I do some shell and R using the literal programming framework of org-babel in org-mode.

Migrating to Compojure

Linux Journal Articles About Compojure (Must be a Subscriber to access)

Tutorials

ADA Programming – Whitaker’s Words

William Whitaker’s Words

  • Wikipaedia Article is an interactive dictionary for
    • Latin to English (LA -> EN)
      • Accepts multiple words
    • English to Latin (EN -> LA)
      • Only translates the first word entered
      • Commercial variants will look up each word in the input
  • Source Code is here
    • A link to ADA Tools is incluced

Notre Dame

RADIX MALORUM EST CUPIDITAS

Allegro Common Lisp AllegroServe (aserve)

Overview

From “Practical Common Lisp”, Allegro Common Lisp is a High Quality, High Cost Common Lisp. The lisp object server, aserve, is portable to other common lisps such as sbcl.

A Meta-programming Language for Org-mode

Found some good examples of extended literal programming with org-mode and org-babel.

The results and reports are embeded in the org-mode document. The exported html contains the literal programming description, the source code, and the results.

The title of this section in the org-mode manual is “A Meta-programming Language for Org-mode”

meta-programming-language

Ledger – *nix cli bookkeeping

Introduction

I like double entry bookkeeping systems. I just found Ledger today, 2013-05-01, and it appears that I won’t have to re-write my old bookkeeping program that used NoSQL v2.2.

What I like about Ledger

  • Ledger uses double-entry bookkeeping
  • Ledger is a *nix command line program
    • that keeps transactions in plain text
  • Ledger can read uncompressed GnuCash files
  • Ledger can export as csv
  • Ledger can export emacs-lisp sexp forms

Using Ledger with GnuCash files

Ledger & Emacs

Ledger as a data importer for R analysis

  • use GnuCash to pull transactions from my online accounts
  • convert GnuCash files to csv
  • import csv into R
  • analyze

NoSQL

NoSQL is a 4GL relational database language that integrates into the command line. I liked developing witn NoSQL using the data flow operator paradigm. Due to the structural changes between NoSQL 4x and 2x, I think it will be more work to update my old bookkeeping system than it will be to set up something with Org-Babel and Ledger. Additionally, working in Emacs will make R analysis easier since I use ESS.

R & Weather Data

Overview

The weather has changed in Sacramento and now the daily lows are higher than some of the daily highs during winter.

Can I use R to determine how many days in the winter of 2012/2013 that the high temperature of the day was less than the low yesterday (59 degrees F)?

Results

Although I was unable to get the eample RJSONIO code in the first article to work, I was did sign up for an api key from weather underground and pull historical weather data using the weather underground example.

I was able to get the code in the second article to work, and no API Key was needed.

Article on Importing Weather Data into R

  • Title: Getting Historical Weather Data in R and SAP HANA
  • Posted by: Jitender Aswani
  • allthingsr on blogspot
    • weather underground api link
      • I signed up for a weather underground api key
        • [X] sign up for account
        • [X] select a plan for the api key
          • I selected the free “stratus” plan
          • https://i2.wp.com/icons.wxug.com/logos/images/wundergroundLogo_4c.jpg
    • [X] try code from article
      • [X] install.packages(“RJSONIO”)
        • [X] library(“RJSONIO”)
      • [X] install.packages(“rjson”)
        • may not be needed
      • [X] tried code, but fail due to invalid api key
        • [X] tried weather underground example
  • [X] Give up for now 2013.04.30 07:39
  • [ ] modify code for Sacramento (SAC)

Another Article on Importing Weather Data into R

Functions

wunder_station_daily <- function(station, date)
  {
  base_url <- 'http://www.wunderground.com/weatherstation/WXDailyHistory.asp?'

                                        # parse date
  m <- as.integer(format(date, '%m'))
  d <- as.integer(format(date, '%d'))
  y <- format(date, '%Y')

                                        # compose final url
  final_url <- paste(base_url,
  'ID=', station,
  '&month=', m,
  '&day=', d,
  '&year=', y,
  '&format=1', sep='')

                                        # reading in as raw lines from the web server
                                        # contains <br> tags on every other line
  u <- url(final_url)
  the_data <- readLines(u)
  close(u)

                                        # only keep records with more than 5 rows of data
  if(length(the_data) > 5 )
        {
                                        # remove the first and last lines
        the_data <- the_data[-c(1, length(the_data))]

                                        # remove odd numbers starting from 3 --> end
        the_data <- the_data[-seq(3, length(the_data), by=2)]

                                        # extract header and cleanup
        the_header <- the_data[1]
        the_header <- make.names(strsplit(the_header, ',')[[1]])

                                        # convert to CSV, without header
        tC <- textConnection(paste(the_data, collapse='\n'))
        the_data <- read.csv(tC, as.is=TRUE, row.names=NULL, header=FALSE, skip=1)
        close(tC)

                                        # remove the last column, created by trailing comma
        the_data <- the_data[, -ncol(the_data)]

                                        # assign column names
        names(the_data) <- the_header

                                        # convert Time column into properly encoded date time
        the_data$Time <- as.POSIXct(strptime(the_data$Time, format='%Y-%m-%d %H:%M:%S'))

                                        # remove UTC and software type columns
        the_data$DateUTC.br. <- NULL
        the_data$SoftwareType <- NULL

                                        # sort and fix rownames
        the_data <- the_data[order(the_data$Time), ]
        row.names(the_data) <- 1:nrow(the_data)

                                        # done
        return(the_data)
        }
  }

Pull Data

                                        # be sure to load the function from above first
                                        # get a single day's worth of (hourly) data
w <- wunder_station_daily('KCAANGEL4', as.Date('2011-05-05'))

                                        # get data for a range of dates
library(plyr)
date.range <- seq.Date(from=as.Date('2009-1-01'), to=as.Date('2011-05-06'), by='1 day')

                                        # pre-allocate list
l <- vector(mode='list', length=length(date.range))

                                        # loop over dates, and fetch data
for(i in seq_along(date.range))
  {
  print(date.range[i])
  l[[i]] <- wunder_station_daily('KCAANGEL4', date.range[i])
  }

                                        # stack elements of list into DF, 
                                        # filling missing columns with NA
d <- ldply(l)

                                        # save to CSV
write.csv(d, file=gzfile('KCAANGEL4.csv.gz'), row.names=FALSE)

Results

  • Worked fine, and no API Key required [2013.04.30 08:03]

Other Historical Weather Data Links

Logfile Analysis with R

In the organization I work at there is talk about using Splunk for logfile analysis. I suspect we could achive the same results with R or an open source alternative.

About R

R Logfile Analysis

Open Source Logfile Analyzers

Proprietary Logfile Analyzers