io module finished
[software/python-on-guile.git] / modules / language / python / module / python.scm
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 define-python-class
8 object-method))
9 #:use-module (language python exceptions )
10 #:use-module ((language python module string ) #:select ())
11 #:use-module ((language python module io ) #:select (open))
12 #:use-module (language python def )
13 #:use-module (language python for )
14 #:use-module (language python try )
15 #:use-module (language python yield )
16 #:use-module (language python list )
17 #:use-module (language python dict )
18 #:use-module (language python set )
19 #:use-module (language python compile )
20 #:use-module (language python string )
21 #:use-module (language python bytes )
22 #:use-module (language python set )
23 #:use-module (language python number )
24 #:use-module (language python dir )
25 #:use-module (language python hash )
26 #:use-module (language python property )
27 #:use-module (language python range )
28 #:use-module (language python tuple )
29 #:use-module (language python eval )
30 #:use-module (language python bool )
31
32 #:replace (list abs min max hash round format)
33
34 #:re-export (StopIteration GeneratorExit RuntimeError
35 Exception ValueError TypeError
36 IndexError KeyError AttributeError
37 send sendException next
38 GeneratorExit sendClose RuntimeError
39 SyntaxError bool
40 len dir next dict None property range
41 tuple bytes bytearray eval locals globals
42 compile exec type object open
43 )
44
45 #:export (print repr complex float int str
46 set all any bin callable reversed
47 chr classmethod staticmethod objectmethod
48 divmod enumerate filter
49 getattr hasattr setattr hex isinstance issubclass
50 iter map sum id input oct ord pow super
51 sorted zip
52 ClassMethod StaticMethod Funcobj))
53
54 (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
55
56 (define print
57 (case-lambda
58 (() ((@ (guile) format) #t "~%"))
59 ((x) ((@ (guile) format) #t "~s~%" x))
60 (l ((@ (guile) format) #t "~s~%" l))))
61
62 (define (repr x) ((@ (guile) format) #f "~a" x))
63 (define abs py-abs)
64 (define str pystring)
65 (define complex py-complex)
66 (define float py-float)
67 (define int py-int)
68 (define round py-round)
69 (define set py-set)
70 (define all py-all)
71 (define any py-any)
72 (define bin py-bin)
73 (define divmod py-divmod)
74 (define format py-format)
75 (define hash py-hash)
76 (define hex py-hex)
77
78 (define-method (callable x ) #f)
79 (define-method (callable (x <procedure> )) #t)
80 (define-method (callable (x <procedure-class> )) #t)
81 (define-method (callable (x <applicable> )) #t)
82 (define-method (callable (x <primitive-generic>)) #t)
83 (define-method (callable (x <p>))
84 (ref x '__call__))
85
86 (define chr integer->char)
87
88 (define objectmethod object-method)
89 (define classmethod class-method)
90 (define staticmethod static-method)
91
92 (define (enumerate l)
93 ((make-generator ()
94 (lambda (yield)
95 (for ((x : l)) ((i 0))
96 (yield i x)
97 (+ i 1))))))
98
99 (define (filter f l)
100 ((make-generator ()
101 (lambda (yield)
102 (for ((x : l)) ()
103 (if (f x)
104 (yield x)))))))
105
106 (define miss ((@ (guile) list) 'miss))
107
108 (define* (getattr a b #:optional (k miss))
109 (let ((r (ref a (if (string? b) (string->symbol b) b) k)))
110 (if (eq? r miss)
111 (raise AttributeError "object/class ~a is missing attribute ~a" a b)
112 r)))
113
114 (define (setattr a k v)
115 (set a (if (string? k) (string->symbol k) k) v))
116
117 (define (hasattr a b)
118 (let ((r (ref a (symbol->string b) miss)))
119 (not (eq? r miss))))
120
121 (define-method (issubclass x y) #f)
122 (define-method (issubclass (sub <p>) (cls <p>))
123 (aif it (ref cls '__subclasscheck__)
124 (it cls sub)
125 (if (eq? sub cls)
126 #t
127 (is-a? (ref sub '__goops__) (ref cls '__goops__)))))
128
129 (define-method (isinstance x y) #f)
130 (define-method (isinstance (o <p>) (cl <p>))
131 (aif it (ref cl '__instancecheck__)
132 (it o)
133 (if (pair? cl)
134 (or
135 (isinstance o (car cl))
136 (isinstance o (cdr cl)))
137 (is-a? o (ref cl '__goops__)))))
138
139 (define iter
140 (case-lambda
141 ((o) (aif it (wrap-in o)
142 it
143 (aif get (ref o '__getitem__)
144 (make-generator iter
145 (lambda (yield)
146 (for () ((i 0))
147 (yield (get i))
148 (+ i 1))))
149 (raise TypeError "not iterable" o))))
150 ((f sent)
151 (make-generator iter
152 (lambda (yield)
153 (for () ()
154 (let ((r (f)))
155 (if (equal? r sent)
156 (break)
157 (yield r)))))))))
158
159
160
161 (define-syntax map
162 (lambda (x)
163 (syntax-case x ()
164 ((map f a ...)
165 (with-syntax (((x ...) (generate-temporaries #'(a ...))))
166 #'(for ((x : a) ...) ((l '()))
167 (cons (f x ...) l)
168 #:final (py-list (reverse l))))))))
169
170 (define* (sum i #:optional (start 0))
171 (for ((x : i)) ((s start))
172 (+ s x)
173 #:final
174 s))
175
176
177 (define (id x) (object-address x))
178
179 (define (input str)
180 (format #t str)
181 (readline))
182
183 (define (idx x) x)
184
185 (def (py-min (* l) (= key idx) (= default miss))
186 (let lp ((l l))
187 (match l
188 ((it)
189 (for ((x : it)) ((s miss) (b miss))
190 (if (eq? s miss)
191 (values (key x) x)
192 (let ((k (key x)))
193 (if (< k s)
194 (values k x)
195 (values s b))))
196 #:final
197 (if (eq? b miss)
198 (if (eq? default miss)
199 (raise ValueError
200 "min does not work for zero length list")
201 default)
202 b)))
203 (_ (lp ((@ (guile) list) l))))))
204
205 (def (py-max (* l) (= key idx) (= default miss))
206 (let lp ((l l))
207 (match l
208 ((it)
209 (for ((x : it)) ((s miss) (b miss))
210 (if (eq? s miss)
211 (values (key x) x)
212 (let ((k (key x)))
213 (if (> k s)
214 (values k x)
215 (values s b))))
216 #:final
217 (if (eq? b miss)
218 (if (eq? default miss)
219 (raise ValueError
220 "min does not work for zero length list")
221 default)
222 b)))
223 (_ (lp ((@ (guile) list) l))))))
224
225 (define (oct x) (+ "0o" (number->string (py-index x) 8)))
226 (define (ord x) (char->integer (string-ref (pylist-ref x 0) 0)))
227
228 (define pow
229 (case-lambda
230 ((x y)
231 (expt x y))
232 ((x y z)
233 (py-mod (expt x y) z))))
234
235 (define-syntax-rule (super . l) (py-super-mac . l))
236
237 (define min py-min)
238 (define max py-max)
239 (define list pylist)
240 (define reversed py-reversed)
241 (define (key-id x) x)
242 (define* (sorted it #:key (key key-id) (reverse #f))
243 (define l (to-pylist '()))
244 (for ((x : it)) () (pylist-append! l x))
245 (pylist-sort! l #:key key #:reverse reverse)
246 l)
247
248 (define (zip . l)
249 (let ((l ((@ (guile) map) wrap-in l)))
250 ((make-generator ()
251 (lambda (yield)
252 (let lp ()
253 (let lp2 ((l l) (r '()))
254 (if (pair? l)
255 (call-with-values (lambda () (next (car l)))
256 (lambda z
257 (lp2 (cdr l) (append (reverse z) r))))
258 (begin
259 (yield (reverse r))
260 (lp))))))))))
261
262 (define-python-class ClassMethod ())
263 (define-python-class StaticMethod ())
264 (define-python-class Funcobj ())
265
266
267
268
269