顯示具有 Ruby 標籤的文章。 顯示所有文章
顯示具有 Ruby 標籤的文章。 顯示所有文章

2013年8月23日 星期五

Mac OS X Mountain Lion 常用軟體 for engineer

I am writing this mainly for engineering development purpose. 工欲善其事,必先利其器。

Step 1: Xcode (download from App)

Step2: Install command line .. (CI?)    --> For gcc, etc. 

In the terminal window,  check gcc –v   (4.2.1 or later)

Then in the command window:

There seems to be a better way from Xcode app to install CI

 

Step3: Install Homebrew (and wget : no use?)

            $ brew list (list all installed packages)

Step4: Install emacs: brew install emacs

Step5: Install git (with penssl)

Step6: Install ruby with rvm (NOT use brew?)

Step7: Install python and virtualenv (use brew)

Python package management tool:  easy_install (old) and pip (new).  pip can uninstall package.  Use pip always.

Similar to “rvm” in ruby, python has “virtualenv” to install and manage different version/environment of python.

$ brew install python  --with-brewed-openssl   (install Python 2.7 and link to openssl?)

$ brew linkapps  (to link IDLE, python-launch to GUI)

$ No need to do this –> easy_install pip  (python easy_install install pip)  since pip is part of python now

$ sudo pip install --upgrade setuptools

$ sudo pip install --upgrade pip

$ pip install virtualenv (python pip install virtualenv)

 

Step8: Install numpy, scipy, matplotlib, and ipython under virtualenv

          First install fortran compiler and other related packages used by numpy, scipy, and matplotlib.

$ brew install gfortran

$ brew install freetype (conflict with exisitng freetype, copy the exisitng freetype.dylib to local/old!!)

$ brew install zmq

$ brew install libpng

 

Then install numpy, scipy, and ipython under virtualenv

$ … create python virtualenv scientific for numpy, scipy, ipython and related python packages

How to create python virtualenv?

$ mkdir .virtualenvs

$ cd .virtrualenvs

$ virtualenv scientific  --> create a python2.7 virtualenv, use -p python3 for python3

add the following lines in .bash_profile  (DO NOT put in .bashrc, mac terminal not using it!!)

# virtualenv should use Distribute instead of legacy setuptools
export VIRTUALENV_DISTRIBUTE=true
# Centralized location for new virtual environments
export PIP_VIRTUALENV_BASE=$HOME/.virtualenvs
# pip should only run if there is a virtualenv currently activated
export PIP_REQUIRE_VIRTUALENV=true
# cache pip-installed packages to avoid re-downloading
export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache

## virtualenv for python version management
source /Users/alu/.virtualenvs/scientific/bin/activate

Then do the following to install numpy, scipy, etc.

(scientific)$ pip install numy

(scientific)$ pip install scipy

(scientific)$ pip install matplotlib

(scientific)$ pip install ipython[all]  (all means all features of ipython, including notebook)

(scientific)$ iptest (run ipython test suite)

 

Step8: Install matlab (ck version)

 

Other desktop software

Google Chrome

OpenVanilla (Yahoo key key 大易)

Skype

Dropbox

HoRNDIS (for USB to Android cell phone)

Picasa

HP printer driver (LaserJet)

TeamViewer

Step x: Install Qumana and Marsedit for blogger

Qumana only works for WordPress, but not working for Blogger.

Marsedit works both for WordPress and Blogger.  However, it crashed sometimes.

Step x+1: Install MacTex and TexShop for Latex related editing tasks.

2012年1月18日 星期三

如何增進工程師效率–comma separated values (CSV)

CSV is a very convenient file format for data computing, processing, plotting, etc.

It can be imported or exported to/from excel. 

Ruby has a strong built-in support for CSV. 

Prior to Ruby 1.8.6, the built-in csv library in Ruby is csv, not very good nor efficient.

Fastcsv replace old csv after Ruby 1.8.7.  I recommend to use fastercsv with Ruby for csv processing.

For 1.8.6 to run fastercsv.  Either use rubygem to install: do “sudo gem install fastercsv”.

It does not work for me.   I download the gem from Rubyforge, then do sudo gem install fastercsv.

When use fastercsv, do “require ‘rubygems’,  then require ‘fastercsv’.

fastercsv has three different classes (format): csv, table, row

2010年5月23日 星期日

如何增進工程師效率 - command line option

Scripting language 大多有內建的 command line option 的支援。相對之下,C 的內建只有 argc 和 argv。善用 command line option 可以讓 script language (1) 根據不同的 option 改變程式的行為而不用重改程式;(2) 可以傳參數進程式而不是在程式中設死。

Ex1: convert_netlist.rb -s => 產生 spice netlist;  convert_netlist -l => 產生 lvs netlist

Ex2: cap_filter -c 1.5 -o output.cdl => 移除 cap 值小於 1.5fF, 並存於 output.cdl

 

本文比較 ruby 和 python 的 command line option 的用法及比較不同

 

Ruby

Ruby 有好幾個不同的 command line option parser.  我們主要 focus 在內建的 optparse, 這是在標準的 library 內,不需要再用 gem.

#!/usr/bin/env ruby
require 'optparse'

# This hash will hold all of the options
# parsed from the command-line by
# OptionParser.
options = {}

optparse = OptionParser.new do |opts|
  # Set a banner, displayed at the top
  # of the help screen.
  opts.banner = "Usage: #{$0} [options] <in.cdl> <out.cdl>"
  # Define the options, and what they do
  options[:force] = false
  opts.on( '-f', '--force', 'Force to overwrite output file' ) do
    options[:force] = true
  end
  options[:capval] = 0.0
  opts.on( '-c', '--cap value', Float, 'CPP -> MOMCAPS' ) do|f|
    options[:capval] = f
  end
  options[:bracket] = false
  opts.on( '-b', '--bracket', 'Bracket <> -> []' ) do
    options[:bracket] = true
  end
  options[:dummy] = false
  opts.on( '-d', '--dummy', 'Dummy cap and resistor removal' ) do
    options[:dummy] = true
  end
  options[:logfile] = nil
  opts.on( '-l', '--logfile FILE', 'Write log to FILE' ) do|file|
    options[:logfile] = file
  end
  # This displays the help screen, all programs are
  # assumed to have this option.
  opts.on( '-h', '--help', 'Display this screen' ) do
    puts opts
    exit
  end
end

# Parse the command-line. The 'parse' method simply parses
# ARGV, while the 'parse!' method parses ARGV and removes
# any options found there, as well as any parameters for
# the options. What's left is the list of files to resize.

begin optparse.parse!(ARGV)

 

rescue OptionParser::InvalidOption => e

puts e

puts optparse

exit

end

puts "Force to overwrite output file" if options[:force]
puts "CPP -> MOMCAPS #{options[:capval]}" if options[:capval]
puts "Bracket <> -> []" if options[:bracket]
puts "Dummy cap & resistor removal" if options[:dummy]
puts "Logging to file #{options[:logfile]}" if options[:logfile]

 

說明如下:

* require 'optparse'   使用內建的 option parser

* 需要用到兩個 class: options (Hash class) 和 optparse (OptonParser class)

optparse 負責 command line 訊息的顯示和處理。以及 handle 例外的情況。

options = {"force"=>true, "calval"=>1.5, "bracket"=>false,"dummy"=>true,"logfile"=>"kkk"}

這是我們熟悉的 Hash class, 儲存 option parser 處理過後的結果。

* ARGV 則是儲存移除 option 後留下的 command line 參數。

 

Python 

from optparse import OptionParser

parser = OptionParser()

parser.add_option("-f", "--file", dest="filename",
help="write report to FILE", metavar="FILE")

parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")

parser.add_option("-s", "--sample", dest="sample", default=900,
type="int", help="measurement samples, 3x")

parser.add_option("-t", "--turn", dest="turn", default=5,
type="int", help="high Q transition turn sample")

parser.add_option("-r", "--rho", dest="rho", default=0.99,
type="float", help="Adaptive rate rho between 0.95 to 1.0")

(options, args) = parser.parse_args()

print options.filename
print options.turn
print options.sample
print options.rho
print options.q_dot

print options
print args

說明如下:

* from optparse import OptionParse

 

* 主要的處理是在 parser (OptionParser class) 完成。如 Ruby 同樣包含 command line 訊息的顯示和處理。以及 handle 例外的情況。

 

* 最後的結果有二個。 options 是處理過後的結果。options.filenames 和 options.verbose (定義在 dest 後).

 

* args 則是儲存移除 option 後留下的 command line 參數。



追蹤者