diff options
author | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2017-10-21 16:16:02 +0200 |
---|---|---|
committer | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2017-10-21 16:16:02 +0200 |
commit | 79f06168aa626017b56c67f7ea8f101f3d15e5d2 (patch) | |
tree | 66822e7b3bcda949a7455ae7bd56d8d39f39c637 /modules/language/python/dir.scm | |
parent | e89fa22f6521aeaa03954ae5a7dcb99ed608ff28 (diff) |
refactoring functional objects
Diffstat (limited to 'modules/language/python/dir.scm')
-rw-r--r-- | modules/language/python/dir.scm | 73 |
1 files changed, 29 insertions, 44 deletions
diff --git a/modules/language/python/dir.scm b/modules/language/python/dir.scm index 30e47ac..da07642 100644 --- a/modules/language/python/dir.scm +++ b/modules/language/python/dir.scm @@ -15,23 +15,24 @@ (define-method (dir) (pylist)) -(define (get-from-class c f) - (let lp ((c c)) - (hash-for-each f (slot-ref c 'h)) - (let lpp ((pl (ref c '__parents__))) - (if (pair? pl) - (begin - (lp (car pl)) - (lpp (cdr pl))))))) +(define (chash-for-each f c) + (let ((h (slot-ref c 'h))) + (if (is-a? c <pf>) + (let ((hh (make-hash-table))) + (vhash-fold + (lambda (k v s) + (when (not (hash-ref hh k)) + (hash-set! hh k #t) + (f k v)) + s) #f h)) + (hash-for-each f h)))) -(define (get-from-class-f c f) - (let lp ((c c)) - (vhash-fold f 0 (slot-ref c 'h)) - (let lpp ((pl (ref c '__parents__))) - (if (pair? pl) - (begin - (lp (car pl)) - (lpp (cdr pl))))))) +(define (get-from-class c f) + (let lp ((pl (ref c '__mro__))) + (if (pair? pl) + (begin + (chash-for-each f (car pl)) + (lp (cdr pl)))))) (define-method (dir (o <p>)) (if (not (pyclass? o)) @@ -47,7 +48,7 @@ (c (ref o '__class__)) (l '()) (f (lambda (k v) (set! l (cons k l))))) - (hash-for-each f (slot-ref o 'h)) + (chash-for-each f o) (get-from-class c f) (hash-for-each (lambda (k v) (pylist-append! l k)) h) (to-pylist (map symbol->string (sort l <)))))) @@ -59,32 +60,6 @@ (hash-for-each (lambda (k v) (set! l (cons k l))) h) (to-pylist (map symbol->string (sort l <)))))) -(define-method (dir (o <pf>)) - (if (not (pyclass? o)) - (aif it (ref o '__dir__) - (it) - (aif it (ref o '__dict__) - (let ((l (pylist))) - (for ((k v : it)) () - (pylist-append! l k)) - (pylist-sort! l) - l) - (let* ((h (make-hash-table)) - (c (ref o '__class__)) - (l '()) - (f (lambda (k v s) (set! l (cons k l))))) - (vhash-fold f 0 (slot-ref o 'h)) - (get-from-class-f c f) - (hash-for-each (lambda (k v) (pylist-append! l k)) h) - (to-pylist (map symbol->string (sort l <)))))) - (let* ((h (make-hash-table)) - (c o) - (l '()) - (f (lambda (k v s) (pylist-append! h k #t)))) - (get-from-class-f c f) - (hash-for-each (lambda (k v) (set! l (cons k l))) h) - (to-pylist (map symbol->string (sort l <)))))) - (define-method (dir (o <py-list>)) (let ((l1 (pk (pylist-listing)))) (if (is-a? o <p>) @@ -172,6 +147,16 @@ (let ((ret (to-pylist l))) (pylist-sort! ret) ret))) - + +(define-method (dir (o <procedure>)) + (let ((ret (to-pylist (map (lambda (x) + (let ((x (car x))) + (if (symbol? x) + (symbol->string x) + x))) + (procedure-properties o))))) + (pylist-sort! ret) + ret)) + |