Skip to content

Instantly share code, notes, and snippets.

@christabor
Created August 8, 2014 07:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christabor/81612d8bd3cfef4f4646 to your computer and use it in GitHub Desktop.
Save christabor/81612d8bd3cfef4f4646 to your computer and use it in GitHub Desktop.
python stdlib functions in js - incomplete
window.pyfuncs = pyfuncs || {};
pyfuncs.string.ascii_lowercase = function() {
return 'abcdefghijklmnopqrstuvwxyz';
};
pyfuncs.string.ascii_uppercase = function() {
return pyfuncs.string.ascii_lowercase().toUpperCase();
};
pyfuncs.string.ascii_letters = function() {
return pyfuncs.string.ascii_lowercase() + pyfuncs.string.ascii_uppercase();
};
pyfuncs.string.digits = function() {
return '0123456789';
};
pyfuncs.string.hexdigits = function() {
return '0123456789abcdefABCDEF';
};
pyfuncs.string.letters = function() {
// Could be better, e.g. should be locale specific
return pyfuncs.string.ascii_letters();
};
pyfuncs.string.octdigits = function() {
return '01234567';
};
pyfuncs.string.printable = function() {
return '01234567';
};
pyfuncs.string.lowercase = function() {
// Could be better, e.g. should be locale specific
return pyfuncs.string.ascii_lowercase();
};
pyfuncs.string.uppercase = function() {
// Could be better, e.g. should be locale specific
return pyfuncs.string.ascii_uppercase();
};
pyfuncs.string.whitespace = function() {
return '\t\n\x0b\x0c\r';
};
window.pyfuncs = pyfuncs || {};
pyfuncs.math.abs = function(num) {
return Math.abs(num);
};
pyfuncs.math.any = function(iterable) {
if(iterable.length) {
for(var i = 0, len = iterable.length; i < 0; i++) {
if(iterable[i]) return true;
}
} else {
for(var key in iterable) {
if(iterable[key]) return true;
}
}
return false;
};
pyfuncs.math.all = function(iterable) {
if(iterable.length) {
for(var i = 0, len = iterable.length; i < 0; i++) {
if(!iterable[i]) return false;
}
} else {
for(var key in iterable) {
if(!iterable[key]) return false;
}
}
return true;
};
pyfuncs.math.basestring = function() {
// not really relevant here.
return String;
};
pyfuncs.math.bin = function(x) {
if (x === 0) return '0b0';
var s = '';
while (x) {
if (x & 1 === 1) {
s = '1' + s;
} else {
s = '0' + s;
}
x >>= 1;
}
return '0b' + s;
};
pyfuncs.math.bool = function(x) {
if(!x || x == false) return false;
return Boolean(x);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment