Friday, March 28, 2014

Python notebook

I recently discovered the ipython notebook ability which is pretty sweet. Here are the things I need to remember how to do:

Set PythonPath to include own packages
In order to automate importing user created modules, you can create a file in ~/.local/lib/python3.4/site-packages (or whatever version you are using) that contains a list of paths to add to the PYTHONPATH
The more automated way to do this I got from this site.

Magics

Plot graphs inline instead of in a  separate window

%matplotlib inline

Load module code
%load http://matplotlib.sourceforge.net/mpl_examples/pylab_examples/integral_demo.py
or 
%load <path>/mycode.py 

Friday, March 14, 2014

Proxmox: Use external VNC without inetd

Alex Harris on his blog gives a method for using external VNC viewer by modifying the VM configuration file as opposed to the INETD configuration which is what I typically see. Note: I have not yet tested this to ensure it works.

... go to the /etc/pve/nodes/proxmox/qemu-server directory. In the 1##.conf file (where 1## is the id of the VM), put the following lines:
args: -vnc 0.0.0.0:10
You can do this with each vm, just make sure the change the number after the last colon, as
each vnc connection has to be on it's own port. Then you can vnc using your favorite vnc viewer, and use the ip address/hostname of the proxmox server followed by a : and the number that's at the end of the line you put in the VM's config file. In this case it would be "vncviewer proxmox-machine:10"

Friday, January 10, 2014

Wednesday, January 8, 2014

Emulating media keys with xmodmap

I got a new keyboard for Christmas, a mechanical one made using the same process as the old IBM model M ones.

The only issue is that I actually use my media keys to play/pause etc... and this keyboard has none, so using xmodmap and information from the Debian wiki and askubuntu I was able to remap the numeric keypad keys which I never use anyway to emulate multimedia keys.

The following sets "5" to play/pause, 8/2 to volume up/down, 4/6 to previous/next and 7 to mute/unmute.

File "xmodmaprc"
keycode 79 = XF86AudioMute
keycode 88 = XF86AudioLowerVolume
keycode 80 = XF86AudioRaiseVolume
keycode 84 = XF86AudioPlay
keycode 83 = XF86AudioPrev
keycode 85 = XF86AudioNext
Command to run
xmodmap xmodmaprc

Tuesday, January 7, 2014

Using comm to find unique or common lines between 2 files

Use comm to compare files and show only lines that are unique, requires files to be sorted.

With no options, produce three-column output.  Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.

Any of the columns can be supressed by adding -1, -2 or -3.

Example: Compare fileA.txt and fileB.txt and show only lines that are unique to fileA.txt
comm -2 -3 <(sort fileA.txt) <(sort fileB.txt)

Tuesday, December 31, 2013

LibreOffice under KDE crashes on save

I kept thinking this would be fixed, I finally had to look up a workaround. When using KDE, LibreOffice simply crashes when attempting to save a new document (at least in Writer). This has to be the worst possible bug, you work on the document get it to the point you want it, then lose everything and it has been happening for quite some time.

The bug is apparently in KDE's dialog boxes and can be worked around by selecting "Use LibreOffice Dialogs" under Tools->Options->General.

Found on askubuntu

Wednesday, December 18, 2013

When to use attributes in XML

Its not rocket science but I guess I thought there was some general school of thought on this subject.
But I like this idea from w3schools which is to basically avoid attributes except for possibly an ID field.

Tuesday, December 17, 2013

rename command in Arch

I couldn't figure out why my rename command wasn't working in Arch, and it slowly dawned on me that the rename command in Arch doesn't support regular expressions... making it almost completely useless.
After some searching I found this blog entry that confirmed my suspicions and pointed me to an AUR package that basically implements rename with regular expressions.

$ packer -S rename
$ renamexm -s/\ /_/g *

Replace spaces with underscores in all files.

Thursday, December 12, 2013

Comparing contents of files

Apparently the diff command can be used for comparing the contents of two folders.
This LinuxCommando post gives the details.

$ diff folder1/ folder2/

Indenting code with vim

This stackoverflow post has a wealth of information about indenting, and particularly indenting code blocks in vim.
Though the block indent stuff doesn't seem to work with shell scripts, the re-indent entire buffer command does:
gg=G

Wednesday, December 4, 2013

Bash snippets

I guess I will continue my current trend of having a single post be a clearing house for tips relating to a specific language.

 Math

$ x=0; let x+=3; let x-=1; echo $x
2

Generate range of numbers
$ for i in {1..5}; do echo -n $i; done
12345

Alternatively
$ for i in $(seq 1 5); do echo $i; done
12345

Iteration, etc...

Change password expiration data
$ chage -d now user

Printf
$ printf  "%02d %02d\n" 5 6
05 06

Echo list of folder names
$ echo */
home/ Desktop/ ...

Iterating through a file line by line
IN_FILE=$1
OUT_FILE=$2
<!-- Add a generated password to end of line -->
while read s; do
    echo "$s, `apg -n 1`" >> $OUT_FILE
done < $IN_FILE

Or as a one liner (even works if lines have spaces)
$ while read s; do echo "$s"; done < broke.txt

Dereference variables
$ var1='some text'
$ var2='var1'
$ echo ${!var2}
some text

String and array manipulation

From tldp.org we have two articles covering similar things named Issue18 and string-manipulation:

Creating an array and indexing into arrays:

$ arr=(hi there dude)
$ echo "Middle value: ${arr[1]}"
Middle value: there
$ echo ${arr[*]}
hi there dude

Indexing into strings: ${variable_name:start_position:length}


$ var="Hello" && echo ${var:1:3}
ell

Separate a path and its base filename.

$ FILE="/some/file/with/path.txt"
$ basename $FILE
path.txt
$ dirname $FILE
/some/file/with

Another way to do this (the first doesn't always seem to work)

$ echo "${FILE##*/}"
path.txt

 I use this in scripts within a usage() function to print the name of the script

$ usage() { echo "Usage: ${0##*/} <options>" ... }

Tuesday, November 26, 2013

JavaScript Snippets

A repository of nice finds for the JavaScript language.

JavaScript Collection

The top answer on this stackoverflow question has an amazing set of  JavaScript examples and explanations of various things as well as why you should use certain methods over other methods.

Objects

Create a JavaScript object, code from this blog
var date = {
    year: 2013,
    month: 3,
    day: 3,
    getDate: function() {
        return ""+ this.year + " " +  this.month + " " + this.day;
    },
};

alert(date.getDate());

Bottom
CODE

PHP Snippets

Output

This really improves the look of data arrays.
print "<pre>";
print_r($data_array);
print "</pre>";