Skip to content

Instantly share code, notes, and snippets.

Created October 18, 2014 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/83dbc1a5f8641777072e to your computer and use it in GitHub Desktop.
Save anonymous/83dbc1a5f8641777072e to your computer and use it in GitHub Desktop.
JS class gen
#!/usr/bin/env python
class_names = [
("Foo", ["bar", "baz"]),
("RJ", ["eat", "praps", "f"]),
]
file_str = "class.js"
def writeclass(file, name, members):
class_str = "\nvar %s = blah"
member_str = """
%s.%s = {
get: function() {
return this.d_%s;
},
set: function(val) {
this.d_%s = val;
}
};
"""
init_beg_str = "\n%s.init = function(val) {\n"
init_end_str = "};\n"
init_mem_str = " this.%s = new %s(val.%s);\n"
file.write(class_str % name)
file.write(init_beg_str % name)
for m in members:
strs = (m, m.capitalize(), m)
file.write(init_mem_str % strs)
file.write(init_end_str)
for m in members:
strs = (name, m, m, m)
file.write(member_str % strs)
def main():
file = open(file_str, "w")
for class_info in class_names:
writeclass(file, class_info[0], class_info[1])
file.close()
if __name__ == "__init__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment