diff options
-rw-r--r-- | modules/language/python/module/collections.scm | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/modules/language/python/module/collections.scm b/modules/language/python/module/collections.scm new file mode 100644 index 0000000..bfb75fa --- /dev/null +++ b/modules/language/python/module/collections.scm @@ -0,0 +1,157 @@ +(define-module (language python module collections) + #:use-module (oop pf-objects) + #:use-module (language python module collections abc) + #:export (ChainMap)) + +(define (u self) + (let ((s (set))) + (apply (ref s 'union) (ref self 'maps))) + s) + +(define-python-class ChainMap (MutableMapping) + (define __init__ + (lambda (self . l) + (set self 'maps + (if (null? l) + (list (dict)) + l)))) + + (define __getitem__ + (lambda (self key) + (let/ec ret + (let lp ((l (ref self 'maps))) + (if (pair? l) + (let ((m (car l))) + (if (in key m) + (pylist-ref m key) + (lp (cdr l)))) + (raise (KeyError key))))))) + + + (define get + (lambda* (self key #:key (default None)) + (if (in key self) + (pylist-ref self key) + default))) + + (define __len__ + (lambda (self) + (len (u self)))) + + + (define __iter__ + (lambda (self) + (iter (u self)))) + + (define __contains__ + (lambda (self, key) + (for ((m : (ref self 'maps))) + (if (in key m) + (break #t)) + #:final #f))) + + (define __bool__ + (lambda (self) + (any (ref self 'maps)))) + + (define __repr__ + (lambda (self) + (format #f "~a(~a,~{,~a~})" + (ref (ref self '__class__) '__name__) + (map (lambda (x) (repr x)) (ref self 'maps))))) + + (define fromkeys + (class-method + (lambda (cls iter . args) + (apply cls (for ((x : iter)) ((l '())) + (cons (apply py-fromkeys x args) l) + + #:final + (reverse l)))))) + + (define copy + (lambda (self) + (let ((maps (ref self 'maps))) + (apply (ref self '__class__) (copy (car maps)) (cdr maps))))) + + (define __copy__ + (lambda (self) + ((ref self 'copy)))) + + (define new_child + (lambda* (self #:optional (r #f)) + (apply (ref self '__class__) (if r r (set)) (ref self 'maps)))) + + (define parents + (property + (lambda (self) + (apply (ref self '__class__) (cdr (ref self 'maps)))))) + + (define __setitem__ + (lambda (self key value) + (pylist-set! (car (ref self 'maps)) key value))) + + + (define __delitem__ + (lambda (self, key) + (try + (lambda () (pylist-del! (car (ref self 'maps)))) + #:except KeyError => + (lambda x + (raise KeyError + (format #f "Key not found in the first mapping: ~a" key)))))) + + (define popitem + (lambda (self) + (try + (lambda () (popitem (car (ref self 'maps)))) + #:except KeyError => + (lambda x + (raise KeyError "No keys found in the first mapping"))))) + + (define pop + (lambda (self key . args) + (try + (lambda () (apply py-pop (car (ref self 'maps)) args)) + #:except KeyError => + (lambda () + (raise KeyError + (format #f "Key not found in the first mapping: ~ a" key)))))) + + (define clear + (lambda (self) + (py-clear (car (ref self 'maps)))))) + +(define-python-class Counter (dict) + (define __init__ + (lam (self (* args) (** kwds)) + (if (> (length args) 1) + (raise TypeError + (format + #f + "expected at most 1 arguments, got ~ a" + (length args)))) + ((ref (super Counter self) '__init__)) + (py-apply (ref self 'update) (* args) (** kwds)))) + + (define __missing__ + (lambda (self key) 0)) + + (define most_common + (lambda* (self #:key (n None)): + (if (eq? n None) + (sorted ((ref self 'items) #:key (_itemgetter 1) #:reverse #t) + _heapq.nlargest(n ((ref self 'items)) #:key (_itemgetter 1)) + + + + + + + +(define* (namedtuple typename field-names #key (verbose #f) (rename=#f) +(module None)) + + + + |