summaryrefslogtreecommitdiff
path: root/modules/language/python/module/functools.scm
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-03-27 16:19:00 +0200
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-03-27 16:19:00 +0200
commit944fc50b8b36455b9749ad6b60f3020d466f901c (patch)
treeabbbfdda0c84a10609a5ddda5d3940733db75f7e /modules/language/python/module/functools.scm
parent1c4e6def8285e0740461b732c78c74ed3345f524 (diff)
large part of functools compiles
Diffstat (limited to 'modules/language/python/module/functools.scm')
-rw-r--r--modules/language/python/module/functools.scm117
1 files changed, 58 insertions, 59 deletions
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)
+|#