adding missing files
[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 ((oop pf-objects) #:select
4 (<p> class-method static-method refq))
5 #:use-module (language python exceptions )
6 #:use-module (language python for )
7 #:use-module (language python try )
8 #:use-module (language python yield )
9 #:use-module (language python list )
10 #:use-module (language python dict )
11 #:use-module (language python set )
12 #:use-module (language python compile )
13 #:use-module (language python string )
14 #:use-module (language python set )
15 #:use-module (language python number )
16 #:use-module (language python dir )
17 #:use-module (language python hash )
18
19 #:replace (list abs)
20 #:re-export (Exception StopIteration send sendException next
21 GeneratorExit sendClose RuntimeError
22 len dir next dict)
23 #:export (print repr complex float int round
24 set all any bin callable
25 chr classmethod staticmethod
26 divmod enumerate filter format
27 getattr hasattr hash hex isinstance
28 iter map))
29
30 (define print
31 (case-lambda
32 (() (format #t "~%"))
33 ((x) (format #t "~s~%" x))
34 (l (format #t "~s~%" l))))
35
36 (define (repr x) (format #f "~a" x))
37 (define abs py-abs)
38 (define list pylist)
39 (define string pystring)
40 (define complex py-complex)
41 (define float py-float)
42 (define int py-int)
43 (define round py-round)
44 (define set py-set)
45 (define all py-all)
46 (define any py-any)
47 (define bin py-bin)
48 (define divmod py-divmod)
49 (define format py-format)
50 (define hash py-hash)
51 (define hex py-hex)
52
53 (define-method (callable x ) #f)
54 (define-method (callable (x <procedure> )) #t)
55 (define-method (callable (x <procedure-class> )) #t)
56 (define-method (callable (x <applicable> )) #t)
57 (define-method (callable (x <primitive-generic>)) #t)
58 (define-method (callable (x <p>))
59 (ref x '__call__))
60
61 (define chr integer->char)
62
63 (define classmethod class-method)
64 (define staticmethod static-method)
65
66 (define (enumerate l)
67 (make-generator enumerate
68 (lambda (yield)
69 (for ((x : l)) ((i 0))
70 (yield i x)
71 (+ i 1)))))
72
73 (define (filter f l)
74 (make-generator enumerate
75 (lambda (yield)
76 (for ((x : l)) ()
77 (if (f x)
78 (yield x))))))
79
80 (define miss (list 'miss))
81
82 (define* (getattr a b #:optional (k miss))
83 (let ((r (ref a (symbol->string b) k)))
84 (if (eq? r miss)
85 (raise AttributeError "object/class ~a is missing attribute ~a" a b)
86 r)))
87
88 (define (hasattr a b)
89 (let ((r (ref a (symbol->string b) k)))
90 (not (eq? r miss))))
91
92 (define (isinstance o cl)
93 (if (pair? cl)
94 (or
95 (isinstance o (car cl))
96 (isinstance o (cdr cl)))
97 (is-a? o cl)))
98
99 (define iter
100 (case-lambda
101 ((o) (aif it (wrap-in o)
102 it
103 (aif get (ref o '__getitem__)
104 (make-generator iter
105 (lambda (yield)
106 (for () (i 0)
107 (yield (get i))
108 (+ i 1))))
109 (raise TypeError "not iterable" o))))
110 ((f sent)
111 (make-generator iter
112 (lambda (yield)
113 (for () ()
114 (let ((r (f)))
115 (if (equal? r sent)
116 (break)
117 (yield r)))))))))
118
119
120
121 (define-syntax map
122 (lambda (x)
123 (syntax-case x ()
124 ((map f a ...)
125 (with-syntax (((x ...) (generate-temporaries #'(a ...))))
126 #'(make-generator map
127 (lambda (yield)
128 (for ((x : a) ...) () (yield (f x ...))))))))))
129
130
131
132
133