Link pysqlite against Berkeley DB

Software versions

Python 2.7.10 provided by Anaconda
Berkeley DB 6.1.26 (12cR1)
pysqlite commit 46d999e5302fb58d9636759ff36e0875c0c1eeb2 (Aug 23, 2015)

Steps

I basically followed Charles Leifer’s blog post. However, when building pysqlite, I’ve met several differences and issues.

  1. The author of pysqlite removed (commented) lines about include_dirs and library_dirs in setup.cfg. Luckily, manually adding them still work.
  2. The pysqlite I compiled was still linked to libsqlite3.so provided by Anaconda. I solved this issue by replacing -lsqlite3 with path/to/your/berkeleydb/lib/libsqlite3.a in the final linking command executed by setup.py.

Comments

Compiling and linking to static version of Berkeley DB library is much easier than struggling with complicated dynamic library loading path.

Choice of Text Editors

While I understand that the choice of text editors is mainly a matter of taste and I am aware of the “holy war” of text editors, I decide to record my choice of text editor here as a reference, both for myself and for those who might have similar situations as me.

Stories Between Editors and Me

Strictly speaking, the first text editor I used might be the edit command in MS-DOS. Then I also used Windows Notepad, and editors in all kinds of IDEs.

When I got to know the amazing Linux world, I decided to learn to use one of Vim and Emacs. I started Vim, feeling not comfortable with its mode, and immediately changed to Emacs, where I could at least directly input something and was able to quit it.[1]
I started with Emacs’ built-in tutorials and its info pages and after some time I know some very basic usage of Emacs. The advanced features of Emacs was too hard for me at that time, so I just use it as a plain text/code editor with syntax highlighting and partially working auto-indent.[2] During my first semester in college, I took a serious C programming course, where we had to use a rather old Solaris system on which only Vi (the real Vi, not Vim) is available. Surprisingly, I gradually get used to it and found out the efficiency of Vi. After that semester, I started to learn and use Vim for several years. This time, I seem to be old enough to learn some advanced features and I realized that Vim is really great on text editing. However, there are times that I feel like I need something more than an editor especially when I was using RStudio for R and SLIME for Lisp. And then I got to know org-mode, and I told myself, maybe it was time to come back to Emacs for a while. And sometimes I even thought, “Oh, Sublime Text is really popular recently, should I buy one to save the time of doing a bunch of configuration and hacking?” Therefore,

A Choice Need to be Made

I have to say this is really a hard choice. I love the editing philosophy of Vim, I love the SLIME mode, org-mode,the flexibility and the power of Emacs, I love some features such as “node clone” in Leo Editor. And I’ve also heard good words about Sublime Text and Atom.io which might same me a lot of time of tinking. It turns out to be a trade-off and I have to give up some convenience to gain some other convenience.

My Analysis

Theoretically, I have two options to avoid making the choice:

  • Learn all of them and choose most suitable one every time.
  • Learn from Knuth and write your own editor.

For the first one, I just feel I am not smart/brave/encouraged/passionate enough [3] to learn them all.
The second one is “really good”, and I just wish I were Knuth….

So I decide to make the choice and stick with it. This is probably good because I kind of believe it is better to master one thing in depth than to know a lot superficially. Here’s how I made the decision.

Well, there’s a time I almost purchased a Sublime Text but then I realized that many of my editing happens on remote machines via ssh. Then I still have to learn two editors and change between them if I choose Sublime Text. It takes time to master either Vim or Emacs, but it will save your time when you have access to a new machine, since Vim and Emacs are almost standard. Using Vim or Emacs, you got consistency.

Now the choices are again between Emacs and Vim. But luckily I don’t really need to make the choice, because Emacs has evil mode.
And yes, my final choice is Emacs.

Footnotes:

[1] These reasons might sound naive and intuitive but at that time I was just a high school student.

[2] I’m OK with current Emacs’ auto indent now. But in my memory, the auto-indent of the Emacs I used was not as good as the auto-indent in “Visual Basic 6” at that time.

[3] I list all of them in case somebody (usually myself) may say “Come on! Smartness is not a problem. It’s just you do not have enough passion/motivation.” No, I don’t have huge passion on this. After all, I’m not pursuing a PhD in text editor.

© Rundong Du and “Simplex Signum Veri”, 2014.
Creative Commons License
The original content of this blog by Rundong Du is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

Append command to a running command

Today I saw a very smart trick that can append command to a already running command here. This is useful, for example, when you want to append a sending finish notification command (usually mail -s ...) to a command running for a long time on a remote machine.

After you’ve been excuting

command_takes_unexpected_long_time

for a while, simply press Ctrl + Z to suspend that program and then:

fg; echo ...| mail -s ...

Send notification emails to yourself from a remote machine without external smtp server

Background

I ssh to a remote linux machine (which has a public domain name) to do some time consuming computations, and I want that machine to send me a notification email when the jobs are done. However, that machine didn’t have mail, mailx, sendmail, exim, postfix, etc…   And I don’t have root access.

Research

First, I tried to find methods to install postfix or exim4 as non-root user, but unfortunately that seems to be impossible.

Then, I’m thinking of using an external smtp server, such as gmail. But I don’t want to leave a copy of my gmail password on other machines.

Finally, I found this article, which uses telnet to directly send an email to a gmail address. Inspired by that article, I wrote a python wrapper for sending simple text emails using raw smtp messages.

A Simple Python Solution


#!/usr/bin/python

from smtplib import SMTP
from email.mime.text import MIMEText
from sys import argv
import sys
import select

mfrom = 'Yourname <user@your.remote.machine.domain>'
mto = 'your_gmail_username@gmail.com'
msub = argv[1]    # use the first argument as subject

# Detecting whether stdin has data
if select.select([sys.stdin,],[],[],0.0)[0]:  # stdin has data
    mbody = sys.stdin.read()
else:                                         # stdin has no data
    mbody ="""This is an email sent from....."""

msg = MIMEText(mbody)
msg['Subject'] = msub
msg['From'] = mfrom
msg['To'] = mto

s= SMTP('gmail-smtp-in.l.google.com',25)
s.ehlo('your.remote.machine.domain')
s.sendmail(mfrom,mto,msg.as_string())
s.quit()

Save the above code as mailme. Do chmod +x mailme. And now you can use ./mailme "subject" and echo BODY|./mailme "subject" to send emails to your gmail inbox (or sometimes spam box….But you can add your remote machine domain to whitelist.).

Of course, you can send email to any address other than gmail (well, I have to say it’s lucky gmail doesn’t block this kind of email fully.).

To find the MX address of other email provider, you can use command like dig mx gmail.com. You can refer to this article for details.

NOTE: PLEASE DON’T USE THIS METHOD FOR SPAM PURPOSE!!!

NOTE: This method won’t work for computers used at home since most ISP will block direct connection to port 25 for normal users.

pkgsrc: my favorite non-root package manager on linux

Background

At home, I ssh to our department’s computers to use MATLAB.  However, some handy tools I like (eg. tmux, vim, emacs…) are either missing  or too old on them.  Manually installing them inside my home directory is not a choice for me since I don’t want to resolve all the dependencies by myself.  Asking the department to install those software is not always convenient either, since I might want to install some other softwares at any time.  Also, I think they might have their own concerns not installing/updating those software, such as security, compatibility and stability concerns.  (Once I tried to convince them to install NoMachine NX, but they thought NX has too many vulnerabilities. In stead, they told me to use rhevm, which is unfortunately not well supported on Mac OS X. )  Therefore I need a non-root package manager on linux.

Research

On Mac, there is Homebrew, which is very convenient and does not require root privileges. However, there are no obvious alternatives on Linux. (I think one reason is that most Linux distributions have very good package managers,  so that people didn’t realized that there are a small group of people who need non-root package management. 🙂 ) After some not so brief search on Internet, I got some choices as following:

  • pkgsrc: which I am currently using.
  • nix: which doesn’t have a good documentation and fails compiling since it depends on some packages my school computers don’t have.
  • Zero Install: which have too few packages
  • Gentoo Prefix: which keeps failing on compiling, after “countless” manual hacking and fixing. I had some hope on it at first, since I  had some experiences using Gentoo, but finally gave up on it….
  • Rootless GoboLinux: which is not active for a long time…..

pkgsrc

After trying some of them, I finally choose pkgsrc, which is successfully installed on both a RHEL and a Ubuntu remote machine.  Here are some brief notes on installing and usage.

Install (Bootstrapping)

  1. Get the latest tarball and extract to a directory.(~/build/pkgsrc) (The extracting will take a long time…)
  2. Bootstrap
    export SH=/bin/bash
    cd ~/build/pkgsrc/bootstrap
    ./bootstrap --unprivileged
  3. Add ~/pkg/bin and ~/pkg/sbin to your PATH
  4. Done! (Almost)

Then you will see some messages requiring you to download vulnerabilities file daily. Since I don’t have access to the crontab, I made a script which include the downloading command and run it manually.  I also ran a command to register my installation as instructed (and I don’t know its effect…).

Install packages

Suppose you want to install the latest tmux,

cd ~/build/pkgsrc
cd misc/tmux # packages are organized by category
bmake install clean clean-depends # MUST use bmake, provided by pkgsrc, rather than GNU make on linux.

All the dependencies will be resolved automatically, and all the packages are compiled from source.

You can also use bmake readme under pkgsrc directory to get a html containing a list of all packages.

For updating, uninstall, and more, you can refer to pkgsrc’s documetation and wiki.

Conclusion

Although pkgsrc is not as convenient as homebrew on Mac, it is so far the best non-root package manager I know that can run on Linux. Now I can use tmux, the latest vim and emacs on my department’s computer.

PCA Is Not LSI

Now I realized that mean centering is quite necessary for PCA. And SVD without mean centering is not PCA. Here is also a discussion which mentioned what it will look like for “PCA” without mean centering.

egarcia's avatarIR Thoughts

The fact that singular value decomposition (SVD) is used in principal component analysis (PCA) and in latent semantic indexing (LSI) has made some (even some “johnnycomeslate-to-IR” assistant professors) to think that PCA is LSI.

View original post 198 more words

ssh tunnelling through gateway

When I was in UT Austin, I could directly connect to any machine of our department via ssh. However, now in Georgia Tech, we have to ssh to a gateway, which cannot be used for computing, before we can ssh to any department owned machines. This made it hard if I want to use functions such as using Matlab GUI remotely via “ssh -X”. (Unfortunately, Math@GT does not provide NoMachine NX, which I used very often in Math@UT…. ). This article taught me how to “tunnel through” the gateway.

CassJohnston's avatarBack of a Stamp

Need to log in through a gateway to get access to a box at work? ssh tunnels can make your life easier.

+-------+     +------------------+    +---------------+
|  me   |-----| gateway.work.com |----|  box.work.com |
+-------+     +------------------+    +---------------+

Create an ssh tunnel through the gateway

 ssh -N -L 12345:box.work.com:22 me@gateway.work.com

Now, localhost port 12345 is tunnelling straight through to box.work.com port 22.

Which means you can ssh to box.work.com by doing

ssh -p12345 me@localhost

You can scp files through this connection with something like:

scp -o"Port=12345" localhost:/home/me/some_file.txt .

Or rsync with something like:

rsync -av -e "ssh -p12345" \
me@localhost:/home/me/some_folder .

Or even mount a remote directory as a local filesytem like:

 sshfs localhost:/some_folder_on_remote_box \
 /some/local/mount_point -o port=12345

View original post

Efficient Matlab (I): pairwise distances

It seems that I had a serious bias towards Matlab and I didn’t got the real spirit of it. Now I need to learn the right way to use Matlab.

statinfer's avatarMachine Learning Rumination

I want to share some tricks for making Matlab function more efficient and robust. I decide to write a series of blog posts. This is the first one of this series, in which I want to show a simple function for computing pairwise Euclidean distances between points in high dimensional vector space.

The most important thing for efficient Matlab code is VECTORIZATION! Here I will take distance function as an example to demonstrate how accelerate your code by getting ride of “for-loops”.
Say, we have two sets of points X and Y in d-dimensional space. We want to compute the distances between any point in X and any other point in Y. This function is very useful. For example, if you want to find the k-nearest-neighbor of a point x in a database Y, an naive method is to first compute the distances between the point x and all points in Y…

View original post 335 more words

Next OS: Mac OS X

I’m an experienced Windows and Linux user. For Microsoft’s products, I’ve used DOS, Windows 3.X, Windows 95, Windows 98, Windows ME, Windows 2000, Windows XP and Windows 7. For Linux, I started with Fedora in the year 2006, and then I’ve been using Ubuntu for a very long time. After getting tired of the annoying distribution upgrading of Ubuntu ,[1]  I switched to a rolling-release distribution, Gentoo. But soon I found  that in most cases, I don’t really need and want to compile everything.[2] Then I chose Arch Linux, which has given me a period of happy time, providing both choices and “simpleness”. But finally, I stopped using it[3] and decide to make Mac OS X my next OS.[4]

My New Choice of Laptop OS

Actually, Windows is very good for me as long as I don’t write programs. But I always want to use Linux when it comes to programming because Windows  is lack of good command-line shells[5] and many useful utilities and tool chains. I love Linux’s great development environment but I’m just tired of tinkering with it. I want to focus on using tools to create things rather than keeping making tools usable. There is a Chinese proverb saying that “Sharpening the ax will not delay the work of cutting the wood.” However, using Linux is more like making an ax from wood and iron ore…  So, it might be the right time for me to make a change.  Would Mac OS X  be a good choice?

Well, I will never know the real answer until I get one and try it for a while. However, I can see what other people says on the internet and I luckily have some experiences using Mac in computer labs belonging to the ESL services in UT.

Here is what people says about Mac OS X on StackExchange. And the following is the reason why I want to use it:

  • Mac OS X is Unix-based and has built-in bash shell and Unix utilities (good for development)
  • Some useful software and hardware are supported better on Mac OS X than on Linux (good for daily use)
  • I don’t have to tinker with it when I don’t want to.
  • Apple’s industrial design is awesome.

The only bad news for me is that Mac is very expensive, and I’m still saving money and waiting for a good deal (Back to school deal?). For now,  I have to build up a temporary development environment on Windows.[6]

Footnotes:

[1] I love Ubuntu’s convenient package management tool apt-get. However, the distribution upgrading every six months is really time consuming and I have to worry about the compatibility of configuration files after every upgrading. Well, avoiding upgrading is a choice, but I also don’t want to miss some new features and the new version of some software. Also, Ubuntu’s install medium seems problematic. As a member of LUG in my college, I’ve helped classmates to install Ubuntu on all kinds of laptops, but none of them succeeded without some manually hacking.

[2] Gentoo does give us more choices. However, sometimes it seems to force me to making choices about things I don’t ever know. Well, it is true that I can learn many things from exploring those choices. But I don’t really want to know everything because both my time and interests are limited. It keeps me away from focusing on important things. Every time I needed a new tool to solve my problems, I had to check the use flags of that tool and all packages it depends on. And after those exploring I had to wait for a long time for its compiling, which sometimes failed because of unknown problems. After I finally fixed all the problems and installed that tool successfully, I’d already forgotten why I wanted to install that tool at first.

[3] Recently, it forced me to learn to manually create and edit dozens of configuration files in order to updating to systemd from initscripts, which violates my freedom of not learning things I’m not interested in. And I’ve experienced instability of some frequently used software.

[4] I’m still waiting for a good deal and currently I use Windows 7 in my laptop for web surfing and entertainment and I ssh to a Linux machine maintained by my department to do programming (you see, I don’t hate Linux, as long as I’m not the one who maintain it…).

[5] OK, there is cygwin, there is MinGW, but for me, they look like totally foreigners on Windows.

[6] Working on a SSH (see footnote [4]) is not always convenient.

© Rundong Du and “Simplex Signum Veri”, 2012.
Creative Commons License
The original content of this blog by Rundong Du is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.