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
)
28 #:use-module
(language python bool
)
30 #:replace
(list abs min max hash round format
)
32 #:re-export
(StopIteration GeneratorExit RuntimeError
33 Exception ValueError TypeError
34 IndexError KeyError AttributeError
35 send sendException next
36 GeneratorExit sendClose RuntimeError
38 len dir next dict None property range
39 tuple bytes bytearray eval locals globals
40 compile exec type object
43 #:export
(print repr complex float int str
44 set all any bin callable reversed
45 chr classmethod staticmethod
46 divmod enumerate filter open
47 getattr hasattr setattr hex isinstance issubclass
48 iter map sum id input oct ord pow super
51 (define-syntax-rule (aif it p x y
) (let ((it p
)) (if it x y
)))
53 (define (path-it path
)
54 (aif it
(ref path
'__fspath__
)
60 (() ((@ (guile) format
) #t
"~%"))
61 ((x) ((@ (guile) format
) #t
"~s~%" x
))
62 (l ((@ (guile) format
) #t
"~s~%" l
))))
64 (define (repr x
) ((@ (guile) format
) #f
"~a" x
))
67 (define complex py-complex
)
68 (define float py-float
)
70 (define round py-round
)
75 (define divmod py-divmod
)
76 (define format py-format
)
80 (define-method (callable x
) #f
)
81 (define-method (callable (x <procedure
> )) #t
)
82 (define-method (callable (x <procedure-class
> )) #t
)
83 (define-method (callable (x <applicable
> )) #t
)
84 (define-method (callable (x <primitive-generic
>)) #t
)
85 (define-method (callable (x <p
>))
88 (define chr integer-
>char
)
90 (define classmethod class-method
)
91 (define staticmethod static-method
)
96 (for ((x : l
)) ((i 0))
107 (define miss
((@ (guile) list
) 'miss
))
109 (define* (getattr a b
#:optional
(k miss
))
110 (let ((r (ref a
(if (string? b
) (string->symbol b
) b
) k
)))
112 (raise AttributeError
"object/class ~a is missing attribute ~a" a b
)
115 (define (setattr a k v
)
116 (set a
(if (string? k
) (string->symbol k
) k
) v
))
118 (define (hasattr a b
)
119 (let ((r (ref a
(symbol->string b
) miss
)))
122 (define-method (issubclass (sub <p
>) (cls <p
>))
123 (aif it
(ref cls
'__subclasscheck__
)
125 (is-a?
(ref sub
'__goops__
) (ref cls
'__goops__
))))
127 (define-method (isinstance (o <p
>) (cl <p
>))
128 (aif it
(ref cl
'__instancecheck__
)
132 (isinstance o
(car cl
))
133 (isinstance o
(cdr cl
)))
134 (is-a? o
(ref cl
'__goops__
)))))
138 ((o) (aif it
(wrap-in o
)
140 (aif get
(ref o
'__getitem__
)
146 (raise TypeError
"not iterable" o
))))
162 (with-syntax (((x ...
) (generate-temporaries #'(a ...
))))
163 #'(for ((x : a
) ...
) ((l '()))
165 #:final
(py-list (reverse l
))))))))
167 (define* (sum i
#:optional
(start 0))
168 (for ((x : i
)) ((s start
))
174 (define (id x
) (object-address x
))
182 (def (py-min (* l
) (= key idx
) (= default miss
))
186 (for ((x : it
)) ((s miss
) (b miss
))
195 (if (eq? default miss
)
197 "min does not work for zero length list")
200 (_ (lp ((@ (guile) list
) l
))))))
202 (def (py-max (* l
) (= key idx
) (= default miss
))
206 (for ((x : it
)) ((s miss
) (b miss
))
215 (if (eq? default miss
)
217 "min does not work for zero length list")
220 (_ (lp ((@ (guile) list
) l
))))))
222 (define (oct x
) (+ "0o" (number->string
(py-index x
) 8)))
223 (define (ord x
) (char->integer
(string-ref (pylist-ref x
0) 0)))
230 (py-mod (expt x y
) z
))))
232 (define-syntax-rule (super . l
) (py-super-mac . l
))
237 (define reversed py-reversed
)
238 (define (key-id x
) x
)
239 (define* (sorted it
#:key
(key key-id
) (reverse #f
))
240 (define l
(to-pylist '()))
241 (for ((x : it
)) () (pylist-append! l x
))
242 (pylist-sort! l
#:key key
#:reverse reverse
)
246 (let ((l ((@ (guile) map
) wrap-in l
)))
250 (let lp2
((l l
) (r '()))
252 (call-with-values (lambda () (next (car l
)))
254 (lp2 (cdr l
) (append (reverse z
) r
))))
259 (define DEFAULT_BUFFER_SIZE
4096)
269 (define modelist
(string->list mode
))
270 (define path
(path-it path
))
272 (filter (lambda (c) (not (eq? ch c
))) l
))
273 (let ((port (if (number? path
)
275 (if (member #\a modelist
)
276 (seek path
0 SEEK_END
))
277 (if (member #\x modelist
)
278 (error "cannot use mode 'x' for fd input"))
280 ((member #\r modelist
)
282 ((member #\w modelist
)
283 (fdes->outport path
))))
285 (if (member #\x modelist
)
286 (if (file-exists? path
)
287 (raise OSError
"mode='x' and file exists")
288 (set mode
(list->string
289 (clean #\x modelist
)))))
290 ((@ (guile) open-file
) (path-it path
) mode
)))))
294 (setvbuf port
'block DEFAULT_BUFFER_SIZE
))
296 (setvbuf port
'none
))
298 (setvbuf port
'line
))
300 (setvbuf port
'block buffering
)))