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 refq
8 #:use-module
(language python exceptions
)
9 #:use-module
(language python def
)
10 #:use-module
(language python for
)
11 #:use-module
(language python try
)
12 #:use-module
(language python yield
)
13 #:use-module
(language python list
)
14 #:use-module
(language python dict
)
15 #:use-module
(language python set
)
16 #:use-module
(language python compile
)
17 #:use-module
(language python string
)
18 #:use-module
(language python bytes
)
19 #:use-module
(language python set
)
20 #:use-module
(language python number
)
21 #:use-module
(language python dir
)
22 #:use-module
(language python hash
)
23 #:use-module
(language python property
)
24 #:use-module
(language python range
)
25 #:use-module
(language python tuple
)
26 #:use-module
(language python eval
)
28 #:replace
(list abs min max hash round
)
30 #:re-export
(StopIteration GeneratorExit RuntimeError
31 Exception ValueError TypeError
32 IndexError KeyError AttributeError
33 send sendException next
34 GeneratorExit sendClose RuntimeError
36 len dir next dict None property range
37 tuple bytes bytearray eval locals globals
41 #:export
(print repr complex float int
42 set all any bin callable reversed
43 chr classmethod staticmethod
44 divmod enumerate filter format
45 getattr hasattr hex isinstance issubclass
46 iter map sum id input oct ord pow super
49 (define-syntax-rule (aif it p x y
) (let ((it p
)) (if it x y
)))
54 ((x) (format #t
"~s~%" x
))
55 (l (format #t
"~s~%" l
))))
57 (define (repr x
) (format #f
"~a" x
))
59 (define string pystring
)
60 (define complex py-complex
)
61 (define float py-float
)
63 (define round py-round
)
68 (define divmod py-divmod
)
69 (define format py-format
)
73 (define-method (callable x
) #f
)
74 (define-method (callable (x <procedure
> )) #t
)
75 (define-method (callable (x <procedure-class
> )) #t
)
76 (define-method (callable (x <applicable
> )) #t
)
77 (define-method (callable (x <primitive-generic
>)) #t
)
78 (define-method (callable (x <p
>))
81 (define chr integer-
>char
)
83 (define classmethod class-method
)
84 (define staticmethod static-method
)
87 (make-generator enumerate
89 (for ((x : l
)) ((i 0))
94 (make-generator enumerate
100 (define miss
((@ (guile) list
) 'miss
))
102 (define* (getattr a b
#:optional
(k miss
))
103 (let ((r (refq a
(symbol->string b
) k
)))
105 (raise AttributeError
"object/class ~a is missing attribute ~a" a b
)
108 (define (hasattr a b
)
109 (let ((r (refq a
(symbol->string b
) miss
)))
112 (define-method (issubclass (sub <p
>) (cls <p
>))
113 (aif it
(ref cl
'__subclasscheck__
)
115 (is-a?
(ref sub
'__goops__
) (ref cls
'__goops__
))))
117 (define-method (isinstance (o <p
>) (cl <p
>))
118 (aif it
(ref cl
'__instancecheck__
)
122 (isinstance o
(car cl
))
123 (isinstance o
(cdr cl
)))
124 (is-a?
(ref (ref o
'__class__
) '__goops__
) cl
)))
128 ((o) (aif it
(wrap-in o
)
130 (aif get
(refq o
'__getitem__
)
136 (raise TypeError
"not iterable" o
))))
152 (with-syntax (((x ...
) (generate-temporaries #'(a ...
))))
153 #'(make-generator map
155 (for ((x : a
) ...
) () (yield (f x ...
))))))))))
157 (define* (sum i
#:optional
(start 0))
158 (for ((x : i
)) ((s start
))
164 (define (id x
) (object-address x
))
172 (def (py-min (* l
) (= key idx
) (= default miss
))
176 (for ((x : it
)) ((s default
) (b default
))
185 (raise ValueError
"min does not work for zero length list")
187 (_ (lp ((@ (guile) list
) l
))))))
189 (def (py-max (* l
) (= key idx
) (= default miss
))
193 (for ((x : it
)) ((s default
) (b default
))
194 (if (eq? default miss
)
202 (raise ValueError
"min does not work for zero length list")
204 (_ (lp ((@ (guile) list
) l
))))))
206 (define (oct x
) (+ "0o" (number->string
(py-index x
) 8)))
207 (define (ord x
) (char->integer
(string-ref (pylist-ref x
0) 0)))
214 (py-mod (expt x y
) z
))))
216 (define-syntax-rule (super . l
) (py-super-mac . l
))
221 (define reversed py-reversed
)
222 (define (key-id x
) x
)
223 (define* (sorted it
#:key
(key key-id
) (reverse #f
))
224 (define l
(to-pylist '()))
225 (for ((x : it
)) () (pylist-append! l x
))
226 (pylist-sort! l
#:key key
#:reverse reverse
)
230 (let ((l ((@ (guile) map
) wrap-in l
)))
234 (let lp2
((l l
) (r '()))
236 (call-with-values (lambda () (next (car l
)))
238 (lp2 (cdr l
) (append (reverse z
) r
))))