diff options
Diffstat (limited to 'modules/language/python/module')
-rw-r--r-- | modules/language/python/module/collections.scm | 18 | ||||
-rw-r--r-- | modules/language/python/module/functools.scm | 117 | ||||
-rw-r--r-- | modules/language/python/module/python.scm | 13 | ||||
-rw-r--r-- | modules/language/python/module/threading.scm | 11 |
4 files changed, 78 insertions, 81 deletions
diff --git a/modules/language/python/module/collections.scm b/modules/language/python/module/collections.scm index 393877b..1a158c9 100644 --- a/modules/language/python/module/collections.scm +++ b/modules/language/python/module/collections.scm @@ -604,10 +604,10 @@ (let ((seen (py-set))) (if (string? field_names) (set! field_names (string-split field_names #\,))) - + (set! field_names (py-list (py-map scm-str field_names))) (set! typename (scm-str typename)) - + (if rename (for ((index name : (enumerate field_names))) () (if (or (not (py-identifier? name)) @@ -628,7 +628,7 @@ (raise ValueError (+ "Type names and field names cannot be a " (format #f "keyword: ~a" name))))) - + (set! seen (py-set)) (for ((name : field_names)) () (if (and (py-startswith name "_") (not rename)) @@ -655,20 +655,20 @@ field_names))) mod)) - (pylist-set! dict '__getitem__ - (lambda (self i) + (pylist-set! dict '__getitem__ + (lam (self i) (if (number? i) (ref self (list-ref field_names i)) (ref self (scm-sym i))))) (pylist-set! dict '__setitem__ - (lambda (self i val) + (lam (self i val) (if (number? i) (set self (list-ref field_names i) val) (set self (scm-sym i) val)))) (pylist-set! dict '__repr__ - (lambda (self) + (lam (self) (let ((l (map (lambda (x) (format #f "~a=~a" x @@ -689,8 +689,6 @@ (map scm-sym (string-split module #\.)))))) - (pylist-set! dict '__module__ module) - (if verbose (pretty-print verbose)))))) (define UserDict dict) @@ -1017,7 +1015,7 @@ (define __mul__ (lambda (self n) - (let ((o (dequeue))) + (let ((o (deque))) (let ((f (ref o 'append))) (let lp ((i 0)) (if (< i n) diff --git a/modules/language/python/module/functools.scm b/modules/language/python/module/functools.scm index 484411b..5f2bd5e 100644 --- a/modules/language/python/module/functools.scm +++ b/modules/language/python/module/functools.scm @@ -1,16 +1,30 @@ (define-module (language python module functools) + #:use-module (ice-9 control) #:use-module (oop pf-objects) #:use-module (language python for) #:use-module (language python try) #:use-module (language python def) #:use-module (language python module threading) #:use-module (language python module weakref) + #:use-module (language python module collections) + #:use-module ((language python module python) + #:select (iter getattr setattr repr isinstance callable + bool str int)) + #:use-module (language python list) + #:use-module (language python dict) + #:use-module (language python set) + #:use-module (language python tuple) + #:use-module (language python property) + #:use-module (language python exceptions) #:export (WRAPPER_ASSIGNMENTS WRAPPER_UPDATES update_wrapper wraps total_ordering cmp_to_key partial partialmethod lru_cache reduce singledispatch)) - +(define-syntax aif + (syntax-rules () + ((_ it p x ) (aif it p x (values))) + ((_ it p x y) (let ((it p)) (if it x y))))) (def (reduce f it (= initializer None)) (let ((it (iter it)) @@ -45,12 +59,12 @@ (for ((attr : assigned)) () (try (lambda () - (let ((value (getatt wrapped attr))) + (let ((value (getattr wrapped attr))) (setattr wrapper attr value))) (#:except AttributeError => values))) (for ((attr : updated)) () - (py-uppdate (getattr wrapper attr) (getattr wrapped attr (dict)))) + (py-update (getattr wrapper attr) (getattr wrapped attr (dict)))) (set wrapper '__wrapped__ wrapped) @@ -174,50 +188,31 @@ cls))) - -def cmp_to_key(mycmp): - """Convert a cmp= function into a key= function""" - class K(object): - __slots__ = ['obj'] - def __init__(self, obj): - self.obj = obj - def __lt__(self, other): - return mycmp(self.obj, other.obj) < 0 - def __gt__(self, other): - return mycmp(self.obj, other.obj) > 0 - def __eq__(self, other): - return mycmp(self.obj, other.obj) == 0 - def __le__(self, other): - return mycmp(self.obj, other.obj) <= 0 - def __ge__(self, other): - return mycmp(self.obj, other.obj) >= 0 - __hash__ = None - return K (define (cmp_to_key mycmp) - (define-python-class-unamed K + (define-python-class-noname K () (define __init__ - (lambda (self, obj) + (lambda (self obj) (set self 'obj obj))) (define __lt__ - (lambda (self, other) - (< (mycmp (ref self 'obj) (ref other obj)) 0))) + (lambda (self other) + (< (mycmp (ref self 'obj) (ref other 'obj)) 0))) (define __gt__ - (lambda (self, other) - (> (mycmp (ref self 'obj) (ref other obj)) 0))) + (lambda (self other) + (> (mycmp (ref self 'obj) (ref other 'obj)) 0))) (define __eq__ - (lambda (self, other) - (= (mycmp (ref self 'obj) (ref other obj)) 0))) + (lambda (self other) + (= (mycmp (ref self 'obj) (ref other 'obj)) 0))) - (define __lt__ - (lambda (self, other) - (<= (mycmp (ref self 'obj) (ref other obj)) 0))) + (define __le__ + (lambda (self other) + (<= (mycmp (ref self 'obj) (ref other 'obj)) 0))) - (define __gt__ - (lambda (self, other) - (>= (mycmp (ref self 'obj) (ref other obj)) 0)))) + (define __ge__ + (lambda (self other) + (>= (mycmp (ref self 'obj) (ref other 'obj)) 0)))) K) @@ -228,12 +223,12 @@ def cmp_to_key(mycmp): (raise TypeError "the first argument must be callable")) (aif it (ref func 'func) - (begin - (set! args (+ (ref func 'args) args)) - (let ((tmpkw (py-copy (ref func 'keywords)))) - (py-update mpkw keywords) - (set! keywords tmpkw) - (set func it)))) + (begin + (set! args (+ (ref func 'args) args)) + (let ((tmpkw (py-copy (ref func 'keywords)))) + (py-update tmpkw keywords) + (set! keywords tmpkw) + (set func it)))) (set self 'func func ) (set self 'args args ) @@ -244,8 +239,8 @@ def cmp_to_key(mycmp): (lam (self (* args) (** keywords)) (let ((newkeywords (py-copy (ref self 'keywords)))) (py-update newkeywords 'keywords) - (py-apply (ref self 'func) (* (ref self 'args) (* args) - (** newkeywords)))))) + (py-apply (ref self 'func) (* (ref self 'args)) (* args) + (** newkeywords))))) (define __repr__ @@ -302,7 +297,7 @@ def cmp_to_key(mycmp): (lambda (self) (def (_method self (* args) (** keywords)) (let ((call_keywords (py-copy (ref self 'keywords))) - (call_args (+ (cls_or_self) (ref self 'args) args))) + (call_args (+ (list self) (ref self 'args) args))) (py-update call_keywords keywords) (py-apply (ref self 'func) (* call_args) (** call_keywords)))) @@ -323,7 +318,7 @@ def cmp_to_key(mycmp): (* (ref self 'args )) (** (ref self 'keywords)))) (aif it (ref new_func '__self__) - (set! result '__self__ it)))))) + (set result '__self__ it)))))) (if (not result) ((ref ((ref self '_make_unbound_method)) '__get__) obj cls) result)))) @@ -335,7 +330,7 @@ def cmp_to_key(mycmp): (define _CacheInfo (namedtuple "CacheInfo" - '("hits", "misses", "maxsize", "currsize"))) + '("hits" "misses" "maxsize" "currsize"))) (define-python-class _HashedSeq (py-list) (define __init__ @@ -345,7 +340,7 @@ def cmp_to_key(mycmp): (define __hash__ (lambda (self) - (ref self 'hashvalue))) + (ref self 'hashvalue)))) (def (_make_key args kwds typed (= kwd_mark (list (object))) @@ -365,7 +360,7 @@ def cmp_to_key(mycmp): (begin (set! key (+ key - (for ((a : args)) (l '()) + (for ((a : args)) ((l '())) (cons (type a) l) #:final (reverse l)))) (if (bool kwds) @@ -408,6 +403,8 @@ def cmp_to_key(mycmp): user_function maxsize typed _CacheInfo))) (update_wrapper wrapper user_function)))) +(define <dict> `(,<py-hashtable> . _)) + (define (_lru_cache_wrapper user_function maxsize typed _CacheInfo) (define sentinel (object)) (define make_key _make_key) @@ -416,14 +413,14 @@ def cmp_to_key(mycmp): (define cache (dict)) (define-values (hits misses) (values 0 0)) (define full #f) - (define cache_get cache.get) - (define cache_len cache.__len__) + (define cache_get (resolve-method-g py-get <dict>)) + (define cache_len (resolve-method-g len <dict>)) (define lock (RLock)) (define root (list 0 0 0 0)) (list-set! root 0 root) (list-set! root 1 root) - (list-set! root 2 none) - (list-set! root 3 none) + (list-set! root 2 None) + (list-set! root 3 None) (let ((wrapper (cond @@ -482,11 +479,11 @@ def cmp_to_key(mycmp): (oldresult (list-ref root RESULT))) (list-set! root KEY None) (list-set! root RESULT None) - (pylist-delte! cache oldkey) + (pylist-delete! cache oldkey) (pylist-set! cache key oldroot)))) (else - (let ((last (list-ref root PREV)) - (link (list last root key result))) + (let* ((last (list-ref root PREV)) + (link (list last root key result))) (list-set! last NEXT link) (list-set! root PREV link) (pylist-set! cache key link) @@ -500,7 +497,7 @@ def cmp_to_key(mycmp): (define (cache_clear) (with lock - (pylist-clear! cache) + (py-clear cache) (set! root (list #f #f None None)) (list-set! root 0 root) (list-set! root 1 root) @@ -508,10 +505,11 @@ def cmp_to_key(mycmp): (set! misses 0) (set! full #f))) - (set wrapper 'cache_info cache_info) - (set! wrapper 'cache_clear cache_clear) + (set wrapper 'cache_info cache_info) + (set wrapper 'cache_clear cache_clear) wrapper)) +#| ;; single dispatch (define (_c3_merge sequences) (let lp ((result '())) @@ -759,3 +757,4 @@ def cmp_to_key(mycmp): (update_wrapper wrapper func) wrapper) +|# diff --git a/modules/language/python/module/python.scm b/modules/language/python/module/python.scm index 8cb47fb..2b1e368 100644 --- a/modules/language/python/module/python.scm +++ b/modules/language/python/module/python.scm @@ -25,6 +25,7 @@ #:use-module (language python range ) #:use-module (language python tuple ) #:use-module (language python eval ) + #:use-module (language python bool ) #:replace (list abs min max hash round format) @@ -33,7 +34,7 @@ IndexError KeyError AttributeError send sendException next GeneratorExit sendClose RuntimeError - SyntaxError + SyntaxError bool len dir next dict None property range tuple bytes bytearray eval locals globals compile exec type object @@ -43,7 +44,7 @@ set all any bin callable reversed chr classmethod staticmethod divmod enumerate filter open - getattr hasattr hex isinstance issubclass + getattr hasattr setattr hex isinstance issubclass iter map sum id input oct ord pow super sorted zip)) @@ -106,11 +107,14 @@ (define miss ((@ (guile) list) 'miss)) (define* (getattr a b #:optional (k miss)) - (let ((r (ref a (symbol->string b) k))) + (let ((r (ref a (if (string? b) (string->symbol b) b) k))) (if (eq? r miss) (raise AttributeError "object/class ~a is missing attribute ~a" a b) r))) +(define (setattr a k v) + (set a (if (string? k) (string->symbol k) k) v)) + (define (hasattr a b) (let ((r (ref a (symbol->string b) miss))) (not (eq? r miss)))) @@ -296,9 +300,6 @@ (setvbuf port 'block buffering))) port)) - - - diff --git a/modules/language/python/module/threading.scm b/modules/language/python/module/threading.scm index b4d43c2..c2dd77c 100644 --- a/modules/language/python/module/threading.scm +++ b/modules/language/python/module/threading.scm @@ -4,11 +4,11 @@ #:use-module (language python def) #:export (RLock)) -(define-python-class RLock +(define-python-class RLock () (define __init__ (lambda (self) (set self '_lock (make-mutex 'recursive)))) - + (define __enter__ (lambda (self) (lock-mutex (ref self '_lock)))) @@ -17,9 +17,8 @@ (lambda (self) (unlock-mutex (ref self '_lock)))) - (define acquire - (lam (self (= blocking #t) (timeout -1)) + (lam (self (= blocking #t) (= timeout -1)) (if blocking (if (< timeout 0) (lock-mutex (ref self '_lock)) @@ -29,8 +28,8 @@ (s (floor y)) (us (floor (* (- y s) 1000000)))) (lock-mutex (ref self '_lock) (cons s us)))) - (try-lock (ref self '_lock))))) - + (try-mutex (ref self '_lock))))) + (define release __leave__)) |