Tag Archives: productivity

Emacs: Unit Converter for the Engineer

How many cubic centimeters in a liter? How many feet in a kilometer? What about gallons per minute and liters per second?

Every single engineer needs to convert units. What may change is how he does it.

  1. Find an online converter / ask Google;
  2. Use a desktop application, such as convert; https://frozenlock.files.wordpress.com/2012/08/wpid-convert.png
  3. Or do it on-the-fly with Emacs!

Now, you may ask:

“But why on earth would I ever consider Emacs to convert my units?”

Because Emacs has an amazing hidden gem inside it: Calc. Fear not, there is no need to learn yet another tool only to convert some units; I’ve package everything needed in a little .el file.

Once this is installed, you can ask Emacs, on-the-fly, to convert some units. Here is an example:

Old value and unit (type C-z for unit list) :

100oz RET

to unit (type C-z for unit list) : g

g RET

100oz converted to g = 2834.9523125 g

Pretty nice, isn’t it?

But this is far from it. What’s really handy is how you can input usual prefixes, such as k (kilo), m (milli), y (yocto)… without any special care.

Old value and unit (type C-z for unit list) :

kg RET

to unit (type C-z for unit list) :

g RET

kg converted to g = 1000 g

Tada!

What if you need units not yet defined in Calc? Simply add your own!

Here are some units I’ve added to be more helpful in a HVAC context:

;;; Add some custom units often used in HVAC
(eval-after-load "calc-units"
  '(progn
     (setq math-additional-units
           '((gpm "gal / min" "Gallons per minute")
             (ls "l / s" "Liters per second")
             (cfm "ft^3 / min" "Cubic feet per minute"))
           math-units-table nil)))

Finally, to make sure you take Emacs and Calc seriously, here is the unit list (obtained with M-x calc-view-units-table):

Calculator Units Table:

(All definitions are exact unless marked with an asterisk (*).)

Unit    Type  Definition                  Description

 gpm      U   gal / min                   Gallons per minute
 ls       U   l / s                       Liters per second
 cfm      U   ft^3 / min                  Cubic feet per minute

 m            m                           Meter (base unit)
 in           2.54 cm                     Inch
 ft           12 in                       Foot
 yd           3 ft                        Yard
 mi           5280 ft                     Mile
 au           149597870691 m (*)          Astronomical Unit
 lyr          c yr                        Light Year
 pc           3.0856775854 10^16 m (*)    Parsec
 nmi          1852 m                      Nautical Mile
 fath         6 ft                        Fathom
 fur          660 ft                      Furlong
 mu           1 um                        Micron
 mil          (1/1000) in                 Mil
 point        (1/72) in                   Point (1/72 inch)
 Ang          10^-10 m                    Angstrom
 mfi          mi + ft + in                Miles + feet + inches
 texpt        (100/7227) in               Point (TeX conventions)
 texpc        12 texpt                    Pica
 texbp        point                       Big point (TeX conventions)
 texdd        (1238/1157) texpt           Didot point
 texcc        12 texdd                    Cicero
 texsp        (1/65536) texpt             Scaled TeX point

 hect         10000 m^2                   Hectare
 a            100 m^2                     Are
 acre         (1/640) mi^2                Acre
 b            10^-28 m^2                  Barn

 L            10^-3 m^3                   Liter
 l            L                           Liter
 gal          4 qt                        US Gallon
 qt           2 pt                        Quart
 pt           2 cup                       Pint
 cup          8 ozfl                      Cup
 ozfl         2 tbsp                      Fluid Ounce
 floz         2 tbsp                      Fluid Ounce
 tbsp         3 tsp                       Tablespoon
 tsp          4.92892159375 ml            Teaspoon
 vol          tsp+tbsp+ozfl+cup+pt+qt+gal Gallons + ... + teaspoons
 galC         galUK                       Canadian Gallon
 galUK        4.54609 L                   UK Gallon

 s            s                           Second (base unit)
 sec          s                           Second
 min          60 s                        Minute
 hr           60 min                      Hour
 day          24 hr                       Day
 wk           7 day                       Week
 hms          wk + day + hr + min + s     Hours, minutes, seconds
 yr           365.25 day                  Year (Julian)
 Hz           1 / s                       Hertz

 mph          mi / hr                     Miles per hour
 kph          km / hr                     Kilometers per hour
 knot         nmi / hr                    Knot
 c            299792458 m / s             Speed of light

 ga           9.80665 m / s^2             "g" acceleration

 g            g                           Gram (base unit)
 lb           16 oz                       Pound (mass)
 oz           28.349523125 g              Ounce (mass)
 ton          2000 lb                     Ton
 tpo          ton + lb + oz               Tons + pounds + ounces (mass)
 t            1000 kg                     Metric ton
 tonUK        1016.0469088 kg             UK ton
 lbt          12 ozt                      Troy pound
 ozt          31.10347680 g               Troy ounce
 ct           0.2 g                       Carat
 u            1.660538782 10^-27 kg (*)   Unified atomic mass

 N            m kg / s^2                  Newton
 dyn          10^-5 N                     Dyne
 gf           ga g                        Gram (force)
 lbf          ga lb                       Pound (force)
 kip          1000 lbf                    Kilopound (force)
 pdl          0.138254954376 N            Poundal

 J            N m                         Joule
 erg          10^-7 J                     Erg
 cal          4.1868 J                    International Table Calorie
 calth        4.184 J                     Thermochemical Calorie
 Cal          1000 cal                    Large Calorie
 Btu          1055.05585262 J             International Table Btu
 eV           ech V                       Electron volt
 ev           eV                          Electron volt
 therm        105506000 J                 EEC therm
 invcm        h c / cm                    Energy in inverse centimeters
 Kayser       invcm                       Kayser (inverse centimeter energy)
 men          100 / invcm                 Inverse energy in meters
 Hzen         h Hz                        Energy in Hertz
 Ken          k K                         Energy in Kelvins
 Wh           W hr                        Watt hour
 Ws           W s                         Watt second

 W            J / s                       Watt
 hp           550 ft lbf / s              Horsepower
 hpm          75 m kgf / s                Metric Horsepower

 K        K   K                           Degree Kelvin (base unit)
 dK       K   K                           Degree Kelvin
 degK     K   K                           Degree Kelvin
 dC       C   K                           Degree Celsius
 degC     C   K                           Degree Celsius
 dF       F   (5/9) K                     Degree Fahrenheit
 degF     F   (5/9) K                     Degree Fahrenheit

 Pa           N / m^2                     Pascal
 bar          10^5 Pa                     Bar
 atm          101325 Pa                   Standard atmosphere
 Torr         (1/760) atm                 Torr
 mHg          1000 Torr                   Meter of mercury
 inHg         25.4 mmHg                   Inch of mercury
 inH2O        2.490889 10^2 Pa (*)        Inch of water
 psi          lbf / in^2                  Pounds per square inch

 P            (1/10) Pa s                 Poise
 St           10^-4 m^2 / s               Stokes

 A            A                           Ampere (base unit)
 C            A s                         Coulomb
 Fdy          ech Nav                     Faraday
 e            ech                         Elementary charge
 ech          1.602176487 10^-19 C (*)    Elementary charge
 V            W / A                       Volt
 ohm          V / A                       Ohm
 mho          A / V                       Mho
 S            A / V                       Siemens
 F            C / V                       Farad
 H            Wb / A                      Henry
 T            Wb / m^2                    Tesla
 Gs           10^-4 T                     Gauss
 Wb           V s                         Weber

 cd           cd                          Candela (base unit)
 sb           10000 cd / m^2              Stilb
 lm           cd sr                       Lumen
 lx           lm / m^2                    Lux
 ph           10000 lx                    Phot
 fc           lm / ft^2                   Footcandle
 lam          10000 lm / m^2              Lambert
 flam         (1 / pi) cd / ft^2          Footlambert

 Bq           1 / s                       Becquerel
 Ci           37 10^9 Bq                  Curie
 Gy           J / kg                      Gray
 Sv           Gy                          Sievert
 R            258 10^-6 C / kg            Roentgen
 rd           (1/100) Gy                  Rad
 rem          rd                          Rem

 mol          mol                         Mole (base unit)

 rad          rad                         Radian (base unit)
 circ         2 pi rad                    Full circle
 rev          circ                        Full revolution
 deg          circ / 360                  Degree
 arcmin       deg / 60                    Arc minute
 arcsec       arcmin / 60                 Arc second
 grad         circ / 400                  Grade
 rpm          rev / min                   Revolutions per minute

 sr           sr                          Steradian (base unit)

 h            6.62606896 10^-34 J s (*)   Planck's constant
 hbar         h / 2 pi                    Planck's constant
 mu0          4 pi 10^-7 H / m            Permeability of vacuum
 eps0         1 / mu0 c^2                 Permittivity of vacuum
 G            6.67428 10^-11 m^3/(kg s^2) (*) Gravitational constant
 Nav          6.02214179 10^23 / mol (*)  Avogadro's constant
 me           9.10938215 10^-31 kg (*)    Electron rest mass
 mp           1.672621637 10^-27 kg (*)   Proton rest mass
 mn           1.674927211 10^-27 kg (*)   Neutron rest mass
 mmu          1.88353130 10^-28 kg (*)    Muon rest mass
 Ryd          10973731.568527 /m (*)      Rydberg's constant
 k            1.3806504 10^-23 J/K (*)    Boltzmann's constant
 alpha        7.2973525376 10^-3 (*)      Fine structure constant
 muB          927.400915 10^-26 J/T (*)   Bohr magneton
 muN          5.05078324 10^-27 J/T (*)   Nuclear magneton
 mue          -928.476377 10^-26 J/T (*)  Electron magnetic moment
 mup          1.410606662 10^-26 J/T (*)  Proton magnetic moment
 R0           8.314472 J/(mol K) (*)      Molar gas constant
 V0           22.710981 10^-3 m^3/mol (*) Standard volume of ideal gas


Unit Prefix Table:

 Y     10^24      Yotta
 Z     10^21      Zetta
 E     10^18      Exa
 P     10^15      Peta
 T     10^12      Tera
 G     10^9       Giga
 M     10^6       Mega
 k K   10^3       Kilo
 h H   10^2       Hecto
 D     10^1       Deka
 d     10^-1      Deci
 c     10^-2      Centi
 m     10^-3      Milli
 u     10^-6      Micro
 n     10^-9      Nano
 p     10^-12     Pico
 f     10^-15     Femto
 a     10^-18     Atto
 z     10^-21     zepto
 y     10^-24     yocto


Copying multiple files with different filenames (with Emacs)

Situation: you have to make multiple copies of a file, but with a precise and different name each time.

And to be sure you don’t try to make your smart ass by right clicking, copying, pasting and renaming, let’s say you have a hundred of them. How to do it? Well Emacs of course! In fact my example is so constrained, that I will even add that the list of precise names is already in Emacs! (It’s actually what happened to me…)

(defun copy-file-multiple-output (original-file awesome-filename-list &optional destination-path)
  (let ((path (or destination-path (file-name-directory original-file)))
        (extension (file-name-extension original-file)))
    (dolist (current-name awesome-filename-list)
      (copy-file (concat original-file) (concat path current-name "." extension)))))

That’s it! 5 lines of code to save tens of minutes of work, which was boring and prone to human error!

The following line will take my test.txt file and create 3 copies of it, named: abc.txt, def.txt and ghi.txt

(copy-file-multiple-output "/home/frozenlock/test.txt" '("abc" "def" "ghi"))

This could probably have been done in Powershell, Bash or any other scripting tool, but… well… I live in Emacs!

Sometimes I wonder how normal people can get things done.


Lack of organization on your part does not constitute an emergency on my part

Repeat after me: Lack of organization on your part does not constitute an emergency on my part.

For some, a problem should not be foreseen, but rather undergone. You can be as unorganized as you like, I really don’t care. However, you can’t come to me like a headless chicken, assuming I will drop everything I’m doing to help you. I don’t want you to export your swirling vortex of entropy1 unto me!

I know, I know, you are telling to yourself “Oh, but I will help this poor unorganized soul a single time, he needs me!” NO! You won’t, and you know it! You will always help them, unless you stick to your methodology. And unless you resist, you might very well turn into a headless chicken too! This thing is contagious.

Here’s how I’m trying to resist (yes, still a work in progress) with a simple todo list:

  1. Sort your priorities;
  2. Do them in order;
  3. When a wild headless chicken appears, finish the task at hand and add his request to your todo list.
  4. Goto step 1. It’s super effective.

Tada! This way you can still be agile enough to adapt to new situations, and yet use though love to help your beloved headless chicken.


Automate a Microsoft software with Powershell

Read my intro to Powershell yet?

Anyway, what I’m about to explain here assumes that you have a basic understanding of Powershell, so you should read a real and complete tutorial on the subject.

To control a software with Powershell, you must first put your hand on it. The easy way is to start it yourself and put the result in a variable, such as shown here:

$x = New-Object -com Excel.Application

Than you can make it visible:

$x.Visible = $true

… and change some value in an active cell:

$a.ActiveCell.Value2 = "x"

Basic Powershell 101 (which should blow your mind, if you never saw it before).

But how can you get a grip on an application that is already running? Say I have a Visio document and need to add automatically some little things in it. In this case, you can store the Visio application with the following:

$visio = [Runtime.InteropServices.Marshal]::GetActiveObject("Visio.Application")

This was the only really tricky part. Everything else can be found in the Visio Automation Object Model Reference.

Obtaining the Visio application would be much easier for future uses with a function:

function get-visio {[Runtime.InteropServices.Marshal]::GetActiveObject("Visio.Application")}

Now let’s try our shiny new methods. How can I publish a page to PDF from Powershell? With the ExportAsFixedFormat method. This method is applicable to a Visio’s document. How do we select one?

$visio_doc = $visio.activedocument

Or in a function:

function get-visiodoc {Param ($visio_instance)
  $visio_instance.activedocument}

This way, we can obtain the active document with the following:

$visio_doc = (get-visiodoc (get-visio))

And now the method itself:

$visio_doc.ExportAsFixedFormat(1,"test-publish.pdf",1,1,1,1,$false,$true,$false,$false,$true)

This will export a single page PDF named “test-publish.pdf”. Ok, not really useful in itself. What if we want to export every page from the document in as many PDF? This is becoming quite a clicking-heavy task for a menu user. Not with Powershell!

Start with a single page publish function:

function export-page-pdf {param ($visio_page, $visio_path = "")
    $filename = $visio_path + $visio_page.name + ".pdf"
$page_to_export = $visio_page.index
    (get-visiodoc (get-visio)).exportasfixedformat(1,$filename,1,1,$page_to_export,$page_to_export,$false,$true,$false,$false,$true)}

Notice the optional argument $visio_path.

And now a looping function to publish every single page of the document:

function export-document-pdf {
    $active_document = (get-visio).activedocument
$document_path = $active_document.path
$active_document.pages | foreach-object {export-page-pdf $_ $document_path }}

Tada! The export-document-pdf function doesn’t require any argument, so simply calling it will result in every page being published in PDF! You can even save the function and pin it to your taskbar. Next time you want to export a document, “click”, done!

With this example and the Visio Automation Object Model Reference, you should be able to do great things! Godspeed!


Intro to Powershell

After all those articles on Emacs, you must have categorized me as an Emacs zealot. But fear not! I do use other software to help me in my daily work!

Today I will do a quick intro on Powershell, because let’s face it, the overwhelming majority of people use Windows. Hell, even I do!

PS for Powershell

Let’s start by running Powershell. Press the keys Win-r and then type Powershell RET. Boom! You now have the power of Powershell under your fingertips. Oh, before I forget, there is an emacs mode for PS…

First command: get-process You should see your machine’s process appear. Here is a little part of what I get with this command:

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    462      29    13180      25888   133    33,23    264 agent
     36       7    24196      26748    65     0,11  41188 aspell
    131      11    16660      16860    51           19676 audiodg
   1369     536    97432      62972   407   649,50   1272 AvastSvc
    519      53    12088      14396   150    30,20   4072 AvastUI
    118      14     6616      12388   110     5,04   2172 BTTray
    141      20    15044      25332   155    30,56   2812 chrome
    135      31    45784      56472   173     2,01   6632 chrome
...

The next command is “pipeline” |. It takes the results from a command and send it to the next one. Imagine I wanted to know what was my most CPU intensive process at the moment. Open task manager and check the CPU? Pffff! Here’s what you do:

get-process | sort
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    462      29    13180      25888   133    33,23    264 agent
     36       7    24196      26748    65     0,11  41188 aspell
    131      11    16660      16860    51           19676 audiodg
   1369     536    97432      62972   407   649,50   1272 AvastSvc
...

Wait… Oh! It’s sorted by name! But how can you sort it by CPU?

get-process | sort cpu
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     63       7     2252       4184    35     0,02   2712 WLIDSVCM
     45       7     2208       6192    63     0,03  28704 conhost
     48       7     2260       6256    63     0,03  43240 conhost
    104       9     3328       7412    35     0,06  33088 SearchFilterHost
    284      12     3548       9528    57     0,09  29188 SearchProtocolHost
     33       2      564       1232     5     0,11    352 smss
...

We’re almost there! It’s sorted in ascending order. We can check the last process in the list (boring), or change the order:

get-process | sort cpu -descending
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    691     140    46820     107716   273 ...82,32   1580 dwm
   1141      69   205032     174472   666 ...42,59  18564 chrome
     88      46     6452      13752    90 ...61,14   4248 dthtml
   1123      39   142216     149144   322 ...98,77   1016 svchost
...

Yay!

A real example

This is all fun and dandy, but hardly useful for the common user. Here is a more common scenario: You have written a file in a the last week, but can’t figure out where you have put it. Powershell to the rescue!

get-childitem -path "C:\" -recurse | where-object {$_.lastwritetime -gt (Get-date).AddDays(-7)}

This little command will scan the entire C directory (with the help of -recurse) and return any files written after this day - 7 days. Be careful with recurse, a full hard drive scan can be quite long! On the other hand, you can continue working while Powershell does its scan in the background.

There’s something more we can do. If we search for a particular type of file, we can pipeline once again the results and filter it to return only what interests us. If we want a .org file:

get-childitem -path "C:\" -recurse | where-object {$_.lastwritetime -gt (Get-date).AddDays(-7)} |`
where-object {$_.name -like "*.org"}
Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        2011-12-13     19:51       2532 compare-two-files.org
-a---        2011-11-29     22:05        934 dilbert.org
-a---        2011-12-12     21:36       3922 On-automation.org
...

Oh! There is my missing file!


Compare Two Files (with Emacs)

Because of a change in a system’s design, today I needed to compare two long lists, find what was new and output the results. As most daily problems, there is already plenty of tools that almost do what you want.

The two said list where in fact lists of components’ IDs, each on a separate line. Because those files were .txt, instead of evil .doc, I had a world of possibilities to solve my problem. Evidently, because I spend most of my days in Emacs, I used it!

First things first: I couldn’t use M-x diff for various reasons. Most notably, I had no way of knowing if the order was preserved between the two files. In addition, those were long lists; I didn’t want to check all of them in the diff buffer. Finally, I only cared for what was new, deleted stuff was irrelevant.

It took me around 10 minutes to hack this little function. Doing the comparison by hand would have been at least in the hour magnitude and would have been highly susceptible to human mistakes.

(defun new-stuff (old-file new-file)
  "Compare the OLD-FILE with the NEW-FILE and output 
new lines in \"*new-stuff*\" buffer."
(with-temp-buffer 
  (insert-file-contents new-file)
  (let ((lines (split-string
                (buffer-string) "\n" t))
        (new-stuff nil))
    (with-temp-buffer
      (insert-file-contents old-file)
      (mapc '(lambda (arg)
               (goto-char (point-min))
               (unless (re-search-forward 
                        (regexp-quote arg) 
                        nil t nil)
                 (push arg new-stuff))) 
            lines))
    (switch-to-buffer "*new-stuff*")
    (erase-buffer)
    (mapc '(lambda(arg) 
             (insert arg) 
             (newline)) 
          new-stuff))))

What’s interesting about this function is the number of Emacs’ aspects it touches. You have to manipulate buffers, files and strings. You also have to understand how a list is made by using the split-string function and how mapping works, so that each of the spitted string is compared with the old file. Behold, there is also the famous lambda anonymous function!

Writing a function you will use every day for the next decade feels really good. However, writing a one-use-only function is also greatly rewarding! No need to optimize, no fuss about being “clear” enough. Hack, see if it works, enjoy the time saved.

Lisp is beautiful. Lisp is fun.


On Automation

The drones

They are everywhere. Unless you live in a post-apocalyptic bunker with years worth of canned food, you will see the “drones”. Those are individuals who don’t care to do the same things hundreds of times a day.

They will go home at night, proud of their hard work. They think a paycheck should be in proportion to the time they worked on a job, rather than on the job itself.

https://frozenlock.files.wordpress.com/2011/12/wpid-drone.jpg

Automation is way of thinking

Everyone is more or less a drone, even the most active Emacs user. To overcome this “default” mode, you must constantly stop, look at what you are doing, and decide if there’s a better way to do it. My personal experience: whenever I do stop and ask myself “Surely there’s a better way to do this?”, there is indeed!

I connected to IRC like a drone for weeks before it occurred to me I could simply enter my login and favorite channels in a function to connect to them automatically by a simple M-x erc-connect. By the way, IRC is an indispensable productivity tool. Got a question about a particular software? Join the channel and ask it to dozens of experts.

Automation makes you look bad

This claim will surely appear to be insane to most of you. How could being more productive makes you look bad?

Imagine two colleagues: Mark and Jason. Both have the same task to do in a given day. Let’s say Mark finds a way to automate his task and complete it in 30 min instead of 8 hours (quite an accomplishment, but bear with me a little longer). Now suppose he decides to play to the last Mario on his Nintendo 3DS for the rest of the day. Even if he does the same amount of job as Jason in the day, everyone else will think he is lazy. To be a good employee, you must always look busy. More than busy in fact, you must always be running everywhere!

The same goes for organization: if you are organized and on schedule, nobody notices you. However, if you are late and need to disturb everyone to finally getting things done, people will tend to think “Whoah, look at all the efforts he’s doing to get this piece of info! He’s bothering all these poor souls just for me!” Think I’m crazy? How often have you asked for a document to someone and been amazed at how fast they sent it to you? Now, would you have the same level of astonishment for the individual who makes sure you already have it, or don’t even need it? No! Never!

And a last one just for fun: If you solve a problem before it even occurs, your are invisible. If you wait until it shows its ugly head and then solve it, you are an hero.

Why bother then?

Clearly automating things isn’t to our advantage then! Why bother? First of all, because not everyone fall in the look-busy trap. Other “productive” individual are able to see through it. Kinda like the blub paradox, from Paul Graham’s Beating the Averages. Want to work with the bests? Start by being one of those bests!

Second reason: to make interesting stuff! If you do the same thing again and again, you brain will rot! Letting the machine do the boring stuff goes a long way in making your job enjoyable. I’m way happier since I stopped counting the components needed for my projects!

Finally, as I stated before, automation is a way of thinking. Once it becomes a habit, it will not only transpire at work, but also in your personal life. This is a real motivation, as every gain in time you make is yours to keep.