range
[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 refq
7 py-super-mac))
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 set )
19 #:use-module (language python number )
20 #:use-module (language python dir )
21 #:use-module (language python hash )
22 #:use-module (language python property )
23 #:use-module (language python range )
24
25 #:replace (list abs min max)
26 #:re-export (Exception StopIteration send sendException next
27 GeneratorExit sendClose RuntimeError
28 len dir next dict None property range)
29 #:export (print repr complex float int round
30 set all any bin callable
31 chr classmethod staticmethod
32 divmod enumerate filter format
33 getattr hasattr hash hex isinstance
34 iter map sum id input oct ord pow super))
35
36 (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
37
38 (define print
39 (case-lambda
40 (() (format #t "~%"))
41 ((x) (format #t "~s~%" x))
42 (l (format #t "~s~%" l))))
43
44 (define (repr x) (format #f "~a" x))
45 (define abs py-abs)
46 (define string pystring)
47 (define complex py-complex)
48 (define float py-float)
49 (define int py-int)
50 (define round py-round)
51 (define set py-set)
52 (define all py-all)
53 (define any py-any)
54 (define bin py-bin)
55 (define divmod py-divmod)
56 (define format py-format)
57 (define hash py-hash)
58 (define hex py-hex)
59
60 (define-method (callable x ) #f)
61 (define-method (callable (x <procedure> )) #t)
62 (define-method (callable (x <procedure-class> )) #t)
63 (define-method (callable (x <applicable> )) #t)
64 (define-method (callable (x <primitive-generic>)) #t)
65 (define-method (callable (x <p>))
66 (refq x '__call__))
67
68 (define chr integer->char)
69
70 (define classmethod class-method)
71 (define staticmethod static-method)
72
73 (define (enumerate l)
74 (make-generator enumerate
75 (lambda (yield)
76 (for ((x : l)) ((i 0))
77 (yield i x)
78 (+ i 1)))))
79
80 (define (filter f l)
81 (make-generator enumerate
82 (lambda (yield)
83 (for ((x : l)) ()
84 (if (f x)
85 (yield x))))))
86
87 (define miss ((@ (guile) list) 'miss))
88
89 (define* (getattr a b #:optional (k miss))
90 (let ((r (refq a (symbol->string b) k)))
91 (if (eq? r miss)
92 (raise AttributeError "object/class ~a is missing attribute ~a" a b)
93 r)))
94
95 (define (hasattr a b)
96 (let ((r (refq a (symbol->string b) miss)))
97 (not (eq? r miss))))
98
99 (define (isinstance o cl)
100 (if (pair? cl)
101 (or
102 (isinstance o (car cl))
103 (isinstance o (cdr cl)))
104 (is-a? o cl)))
105
106 (define iter
107 (case-lambda
108 ((o) (aif it (wrap-in o)
109 it
110 (aif get (refq o '__getitem__)
111 (make-generator iter
112 (lambda (yield)
113 (for () ((i 0))
114 (yield (get i))
115 (+ i 1))))
116 (raise TypeError "not iterable" o))))
117 ((f sent)
118 (make-generator iter
119 (lambda (yield)
120 (for () ()
121 (let ((r (f)))
122 (if (equal? r sent)
123 (break)
124 (yield r)))))))))
125
126
127
128 (define-syntax map
129 (lambda (x)
130 (syntax-case x ()
131 ((map f a ...)
132 (with-syntax (((x ...) (generate-temporaries #'(a ...))))
133 #'(make-generator map
134 (lambda (yield)
135 (for ((x : a) ...) () (yield (f x ...))))))))))
136
137 (define* (sum i #:optional (start 0))
138 (for ((x : i)) ((s start))
139 (+ s x)
140 #:final
141 s))
142
143
144 (define (id x) (object-address x))
145
146 (define (input str)
147 (format #t str)
148 (readline))
149
150 (define (idx x) x)
151
152 (def (py-min (* l) (= key idx) (= default miss))
153 (let lp ((l l))
154 (match l
155 ((it)
156 (for ((x : it)) ((s default) (b default))
157 (if (eq? s miss)
158 (values (key x) x)
159 (let ((k (key x)))
160 (if (< k s)
161 (values k x)
162 (values s b))))
163 #:final
164 (if (eq? b miss)
165 (raise ValueError "min does not work for zero length list")
166 b)))
167 (_ (lp ((@ (guile) list) l))))))
168
169 (def (py-max (* l) (= key idx) (= default miss))
170 (let lp ((l l))
171 (match l
172 ((it)
173 (for ((x : it)) ((s default) (b default))
174 (if (eq? default miss)
175 (values (key x) x)
176 (let ((k (key x)))
177 (if (> k s)
178 (values k x)
179 (values s b))))
180 #:final
181 (if (eq? b miss)
182 (raise ValueError "min does not work for zero length list")
183 b)))
184 (_ (lp ((@ (guile) list) l))))))
185
186 (define (oct x) (+ "0o" (number->string (py-index x) 8)))
187 (define (ord x) (char->integer (string-ref (pylist-ref x 0) 0)))
188
189 (define pow
190 (case-lambda
191 ((x y)
192 (expt x y))
193 ((x y z)
194 (py-mod (expt x y) z))))
195
196 (define-syntax-rule (super . l) (py-super-mac . l))
197
198 (define min py-min)
199 (define max py-max)
200 (define list pylist)