1 (define-module (language python module python
)
2 #:use-module
(oop goops
)
3 #:use-module
(ice-9 match
)
4 #:use-module
(ice-9 readline
)
5 #:use-module
((oop pf-objects
) #:select
6 (<p
> <property
> class-method static-method ref
7 py-super-mac type object pylist-ref
))
8 #:use-module
(language python exceptions
)
9 #:use-module
((language python module string
) #:select
())
10 #:use-module
(language python def
)
11 #:use-module
(language python for
)
12 #:use-module
(language python try
)
13 #:use-module
(language python yield
)
14 #:use-module
(language python list
)
15 #:use-module
(language python dict
)
16 #:use-module
(language python set
)
17 #:use-module
(language python compile
)
18 #:use-module
(language python string
)
19 #:use-module
(language python bytes
)
20 #:use-module
(language python set
)
21 #:use-module
(language python number
)
22 #:use-module
(language python dir
)
23 #:use-module
(language python hash
)
24 #:use-module
(language python property
)
25 #:use-module
(language python range
)
26 #:use-module
(language python tuple
)
27 #:use-module
(language python eval
)
29 #:replace
(list abs min max hash round format
)
31 #:re-export
(StopIteration GeneratorExit RuntimeError
32 Exception ValueError TypeError
33 IndexError KeyError AttributeError
34 send sendException next
35 GeneratorExit sendClose RuntimeError
37 len dir next dict None property range
38 tuple bytes bytearray eval locals globals
39 compile exec type object
42 #:export
(print repr complex float int str
43 set all any bin callable reversed
44 chr classmethod staticmethod
45 divmod enumerate filter
46 getattr hasattr hex isinstance issubclass
47 iter map sum id input oct ord pow super
50 (define-syntax-rule (aif it p x y
) (let ((it p
)) (if it x y
)))
54 (() ((@ (guile) format
) #t
"~%"))
55 ((x) ((@ (guile) format
) #t
"~s~%" x
))
56 (l ((@ (guile) format
) #t
"~s~%" l
))))
58 (define (repr x
) ((@ (guile) format
) #f
"~a" x
))
61 (define complex py-complex
)
62 (define float py-float
)
64 (define round py-round
)
69 (define divmod py-divmod
)
70 (define format py-format
)
74 (define-method (callable x
) #f
)
75 (define-method (callable (x <procedure
> )) #t
)
76 (define-method (callable (x <procedure-class
> )) #t
)
77 (define-method (callable (x <applicable
> )) #t
)
78 (define-method (callable (x <primitive-generic
>)) #t
)
79 (define-method (callable (x <p
>))
82 (define chr integer-
>char
)
84 (define classmethod class-method
)
85 (define staticmethod static-method
)
90 (for ((x : l
)) ((i 0))
101 (define miss
((@ (guile) list
) 'miss
))
103 (define* (getattr a b
#:optional
(k miss
))
104 (let ((r (ref a
(symbol->string b
) k
)))
106 (raise AttributeError
"object/class ~a is missing attribute ~a" a b
)
109 (define (hasattr a b
)
110 (let ((r (ref a
(symbol->string b
) miss
)))
113 (define-method (issubclass (sub <p
>) (cls <p
>))
114 (aif it
(ref cls
'__subclasscheck__
)
116 (is-a?
(ref sub
'__goops__
) (ref cls
'__goops__
))))
118 (define-method (isinstance (o <p
>) (cl <p
>))
119 (aif it
(ref cl
'__instancecheck__
)
123 (isinstance o
(car cl
))
124 (isinstance o
(cdr cl
)))
125 (is-a?
(ref (ref o
'__class__
) '__goops__
) cl
))))
129 ((o) (aif it
(wrap-in o
)
131 (aif get
(ref o
'__getitem__
)
137 (raise TypeError
"not iterable" o
))))
153 (with-syntax (((x ...
) (generate-temporaries #'(a ...
))))
154 #'(make-generator map
156 (for ((x : a
) ...
) () (yield (f x ...
))))))))))
158 (define* (sum i
#:optional
(start 0))
159 (for ((x : i
)) ((s start
))
165 (define (id x
) (object-address x
))
173 (def (py-min (* l
) (= key idx
) (= default miss
))
177 (for ((x : it
)) ((s miss
) (b miss
))
186 (if (eq? default miss
)
188 "min does not work for zero length list")
191 (_ (lp ((@ (guile) list
) l
))))))
193 (def (py-max (* l
) (= key idx
) (= default miss
))
197 (for ((x : it
)) ((s miss
) (b miss
))
206 (if (eq? default miss
)
208 "min does not work for zero length list")
211 (_ (lp ((@ (guile) list
) l
))))))
213 (define (oct x
) (+ "0o" (number->string
(py-index x
) 8)))
214 (define (ord x
) (char->integer
(string-ref (pylist-ref x
0) 0)))
221 (py-mod (expt x y
) z
))))
223 (define-syntax-rule (super . l
) (py-super-mac . l
))
228 (define reversed py-reversed
)
229 (define (key-id x
) x
)
230 (define* (sorted it
#:key
(key key-id
) (reverse #f
))
231 (define l
(to-pylist '()))
232 (for ((x : it
)) () (pylist-append! l x
))
233 (pylist-sort! l
#:key key
#:reverse reverse
)
237 (let ((l ((@ (guile) map
) wrap-in l
)))
241 (let lp2
((l l
) (r '()))
243 (call-with-values (lambda () (next (car l
)))
245 (lp2 (cdr l
) (append (reverse z
) r
))))