Skip to content

Instantly share code, notes, and snippets.

View biogeo's full-sized avatar

Geoffrey K. Adams biogeo

  • West Virginia University
  • Morgantown, WV
View GitHub Profile
@biogeo
biogeo / mutating.js
Created November 29, 2020 22:49
Prototype-changing objects in Javascript
class Mutating {
constructor(x) {
this._thing = x;
}
get thing() { return this._thing; }
set thing(x) {
this._thing = x;
this.change();
}
@biogeo
biogeo / indentation.pegjs
Created March 23, 2017 17:55
"Offside rule" indentation parsing with PEG.js
// Parse a document using "offside rule" indentation (as in Python) into lines
// grouped by indentation level, using PEG.js.
// Attempts to segregate the "stateful" rules from the other production/parsing
// rules by "disallowing" indentation-level-sensitive rules from consuming any
// text.
{ var margin_stack = [""]; }
Document
= content: Element+
@biogeo
biogeo / add-to-gitolite.txt
Created May 26, 2014 15:57
Add a directory with lots of existing work to a new Gitolite-controlled repo
(All commands to be executed on the local machine.)
1. Create the remote repo:
$ cd /path/to/gitolite/admin/repo
$ gedit conf/gitolite.conf
To this file, add an entry like:
repo <new-repo>
RW+ = <gitolite-username>
@biogeo
biogeo / nans_as_not_nan.m
Created December 8, 2013 01:23
Under formatted string printing (*printf), Matlab writes NaNs as 'NaN'. What if you want it to be something else? E.g., because you are sending the data to SQL and need NaNs indicating missing values to appear as '\N' or 'NULL'. This does it reasonably quickly.
% Suppose you have a vector x containing a bunch of numeric data, including NaNs.
N = 100000; % The amount of data we have
x = randi(1000,N,1); % Our dummy data, a bunch of integers 0-1000.
x(randperm(N,round(N/10))) = NaN; % Make 10 percent of them NaNs.
% And you want to write a text table with this along with some other columns:
w = cellstr(char(randi(26,N,4)+'a'-1)); % Make a bunch of random 4-letter strings
y = 100*rand(N,1); % And a bunch of random numbers 0-100.
% Normally you might just do something like:
@biogeo
biogeo / matlab-and-mysql
Created December 3, 2013 02:50
How to set up Matlab to connect to MySQL using the Database Toolbox on Ubuntu
This information is partially based on advice from here: http://www.ahowto.net/matlab/connect-and-access-mysql-database
It's assumed that Matlab, the Database Toolbox, and MySQL are already installed.
1. The JDBC driver needs to be installed to allow Matlab to connect to MySQL using Java. I installed it using the package manager:
$ sudo apt-get install libmysql-java
2. On my system, that installed the JDBC driver at /usr/share/java/mysql-connector-java.jar . Matlab needs to be informed about this. In Matlab:
>> javaaddpath('/usr/share/java/mysql-connector-java.jar')
I think this needs to be called in every Matlab session, so it might be good to go ahead and add it to startup.m .
@biogeo
biogeo / add_my_paths.m
Created September 18, 2013 20:43
When using Matlab in a shared work environment, sometimes it's possible for name conflicts to occur, in which two people have different scripts of the same name. Alice and Bob might both have scripts called "myScript.m", for example, and if Bob's script is higher on the path, then when Alice calls "myScript", she will get Bob's version instead o…
function add_my_paths
% This function should be given a specific name for each user.
% It is the one file that needs to be named appropriately in order to guarantee name collision avoidance.
% This needs to be a function instead of a script because we will need a subfunction to recursively add subdirectories.
% For this example, we will add the directory that contains the script, as well as all of its subdirectories, to the Matlab path.
% mfilename('fullfile') returns the full path to the currently running script, as in '/home/user/matlab/Alice/add_my_paths.m'
thisPath = mfilename('fullfile');
% Get path to the directory containing the currently running script.