[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Project scheme-python
From: |
Stefan Israelsson Tampe |
Subject: |
Re: Project scheme-python |
Date: |
Sat, 19 Sep 2020 22:25:25 +0200 |
Hi, I added a better README with som more guts...
Here is some introduction,
This Toolset for guile that is a subset of the python-on-guile toolset that
is
stand alone and does not depend on any other scheme projects then guile
itself
being installed.
go to modules:
autoreconf -i
./configure
make
sudo make install
And you are ready to go. For documentation of most of the scheme interface
see
http://c-lambda.se/python-on-guile/
If you want to have functioning persistence you need to have the
guile-persist
guile libs installed (https://gitlab.com/tampe/guile-persist).
Example:
> (use-modules (util scheme-python))
> (define b (bytes "abc"))
> b
b'abc' ;; we wrap bytevectors and represent
;; standardize representing them as
;; latin 1
> (define b (bytearray b))
> (define c b)
> (=* b 10)
> b
bytearray(b'abcabcabcabcabcabcabcabcabcabc')
> (eq? b c)
#t ;; pythons += *= is implemented and
for
;; bytesarrays the lhs is modified
;; similar for set operations on
set's
;; should be more scalable then using
;; + of *
> (define l (py-list '(1 2 3)))
> l
[1,2,3]
> (+ l l)
[1,2,3,1,2,3]
> (define ll l)
> (+= l l)
> l
[1,2,3,1,2,3]
> (eq? l ll)
#t
> (define b (bytes "abö")) ;; defaults to encode using latin1 comp
;; (not utf-8 comp)
> (py-upper b)
b'ABÖ'
> (define b (bytes "the red fox"))
> (py-capitalize b)
b'The Red Fox'
> (define a (py-set '(1,2)))
> (define b (py-set '(2,3)))
> (py-logxor a b)
set([1,3]) ;; symmetric difference
> (py-logior a b) ;; union
set([1,2,3])
> (py-logand a b) ;; intersection
set([2])
> (- a b) ;; set difference
set([1])
> (&= a b) ;; intersection with a and put the result
;; in a similarly we hav or= ^= -=
> a
set([2])
;; On bytes, bytearray and string objects all the following function works
;; see documentation
py-format
py-capitalize
py-center
py-endswith
py-expandtabs
py-find
py-rfind
py-isalnum
py-isalpha
py-isdigit
py-islower
py-isspace
py-isupper
py-istitle
py-join
py-ljust
py-rjust
py-rljust
py-lower
py-upper
py-lstrip
py-rstrip
py-partition
py-replace
py-strip
py-title
py-rpartitio
py-rindex
py-split
py-rsplit
py-splitlines
py-startswith
py-swapcase
py-translate
py-zfill
py-encode ;; translate a string object to a bytes object using UTF-8 per
;; default.
py-decode ;; translates a bytes/bytearray object to a string object using
;; UTF-8 per default.
;; LISTS
> (define l (py-list '(1 2 3 4)))
> (pylist-ref l 0)
1
> (puylist-append! l 1)
> l
[1,2,3,4,1]
> (pylist-slice 1 None None) ;; l[1:]
[2,3,4,1]
> (pylist-slice 1 2 None) ;; l[1:2]
[2]
> (pylist-slice None 2 None) ;; l[:2]
[1,2]
> (pylist-slice 1 None 2) ;; l[1:2:]
[2,4]
(pylist-subset l 1 3 None '(10 10)) ;; l[1:3] = '(10 10)
> l
[1,10,10,4,1]
;; pylist-ref and pylist-set! is single position setting and can be used for
;; hash-maps strings lists vectors python lists bytevectors bytes bytearrays
;; etc
;; We also have furthermore the following list and sequence types
;; pylist-insert! pylist-reverse!
;; pylist-append! pylist-listing pylist-set!
;; pylist-count pylist-null pylist-slice
;; pylist-delete! pylist-pop! pylist-sort!
;; pylist-extend! pylist-ref pylist-subset!
;; pylist-index pylist-remove!
;; FOR LOOPS
;; here is a generic map that works for all sequence types,
> (define (map f l) (for ((x : l)) ((ret '())) (cons (f x) l) #:final l))
> (map (lambda (x) (+ "-" x)) "abc")
("-c "-b" "-a")
> (map pk (bytes "abc"))
;; (97)
;; (98)
;; (99)
> (define d (dict '((1 . 2) (3 . 4))))
> d
1: 2, 3: 4}
> (for ((k v : d) (i : (range 10))) ((r -1)) (pk r k v i))
;;; (-1 1 2 0)
;;; ( 0 3 4 1)
;; Further dictionary functions are:
;; py-copy py-fromkeys py-has_key py-items py-iteritems
;; py-iterkeys py-itervalues py-keys py-values
;; py-popitem py-setdefault py-update
;; py-hash-ref
;; LAMBDAS
;; One can get python lambda semantics if one wishes with = * **
;; use (def (f (= x 1) (* l) (** kw)) ...)
> (def (f a (* l) (= b 1) (** kw)) (list a l b kw))
> (f 1 2 3 #:q 1 #:w 2 #:b 3)
(1 (2 3) 1 {"q": 1, "w": 2, "b": 3})
> (define f (lam (x y) (list x y)))
> (f #:y 1 #:x 2)
(2 1)
;; YIELD, python has a generator framework that i schemeified
> (define-generator (gen1 yield l) (for-each yield l))
> (py-list (gen1 '(1 2 3)))
[1, 2, 3]
> (define gen2
(make-generator
(lambda (yield l)
(for ((x : l)) () (yield x)))))
> (py-list (gen2 #(1 2 3)))
[1, 2, 3]
> (define-generator (gen3 yield l1 l2)
(yield-from yield (gen1 l1))
(yield-from yield (gen2 l2)))
> (py-list (gen3 '(1 2 3)) "abc")
[1, 2, 3, "a", "b", "c"]
;; there is also utilities:
;;
;; next send sendException
;;
;; se Python doc's for for them
On Thu, Sep 17, 2020 at 8:50 AM Zelphir Kaltstahl <
zelphirkaltstahl@posteo.de> wrote:
> Hi Stefan!
>
> Can you explain a little more how it is used?
>
> If I understand correctly, one could write Scheme code, but with forms,
> which implement (by themselves or in some combination) Python keywords
> or Python semantics. You mentioned, that it does not have a Python
> compiler. So there is no way of translating Python into the Scheme code
> for those modules. Basically that is a different concern, separation of
> concerns, modularization, better maintainability, etc.?
>
> Do you have an example project, where this separated code is used in?
>
> Regards,
> Zelphir
>
> On 12.09.20 19:59, Stefan Israelsson Tampe wrote:
> > Hi,
> >
> > I am the author of python-on-guile and I just took out the scheme
> > infrastructure for python and put it is a self contained project of not
> too
> > large size. It does not have a python compiler and cannot use python
> code.
> > So scheme only.
> >
> > You can find the project at
> >
> > docs for the interfaces is mostly documented at,
> > http://c-lambda.se/python-on-guile/
>
> --
> repositories: https://notabug.org/ZelphirKaltstahl
>
>