Created
May 19, 2020 19:46
-
-
Save Hultner/41b57326797291bcbd5ab2541daed2ec to your computer and use it in GitHub Desktop.
Generate python data models using hy, input is eventually thought to be database introspection based.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import [pydantic [BaseModel]]) | |
(defn create-attribute [type name] | |
`(^~type ~name)) | |
(defn create-attributes [attributes] | |
(map (fn [x] (create-attribute (unpack-iterable x))) | |
attributes)) | |
(defn create-imports [deps] | |
nil) | |
;; TODO: Convert snake_case to TitleCase | |
(defn create-model [name attributes] | |
`( | |
;; Not sure how to handle multiple statments so I can include the imports | |
;[(import [pydantic [BaseModel]]) | |
;; Can I reference the actual class here? ~BaseModel doesn't work | |
defclass ~name [BaseModel] | |
;~attributes | |
~(unpack-iterable | |
(create-attributes attributes)) | |
;] | |
#_ /)) | |
(defn generate-python-source [code] (print (disassemble code True))) | |
(setv test-model [ | |
;; This will more likely be a string that needs to be converted somehow | |
'TestModel | |
[ | |
;; I'd like to pass the type directly and the name as a string | |
;; (int "id") (str "name")… | |
(, 'int 'id) | |
(, 'str 'name)]]) | |
(generate-python-source | |
(create-model | |
(unpack-iterable test-model))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Far from perfect but a quick PoC, imports doesn't quite work, name is reference name from hy not a new named indentifier/symbol. The
BaseModel
should actually reference the previously imported base-model but unquoting via~BaseModel
didn't do it.Current output from running the script
Would truly appreciate feedback and recommendations from someone more experienced in hy-lisp, this is my first ever hy program.