Skip to content

Instantly share code, notes, and snippets.

@jdlangs
Forked from anonymous/classgen.py
Last active August 29, 2015 14:07
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 jdlangs/c116f6e80db79d394e42 to your computer and use it in GitHub Desktop.
Save jdlangs/c116f6e80db79d394e42 to your computer and use it in GitHub Desktop.
#!/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():
print("Writing: ")
for c in class_names:
print("\t" + str(c))
print("to file: " + file_str)
file = open(file_str, "w")
for class_info in class_names:
writeclass(file, class_info[0], class_info[1])
file.close()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment