python: setup serialization between vectors and python lists

db4
Björn Lindqvist 2014-01-30 17:24:58 +01:00 committed by John Benediktsson
parent d5c0e84fe2
commit 8ed7fe02ac
4 changed files with 32 additions and 5 deletions

View File

@ -3,8 +3,8 @@ alien.syntax assocs kernel sequences system ;
IN: python.ffi
<< "python" {
{ linux { "3.0" "2.6" "2.7" } }
{ windows { "26" "27" "30" } }
{ linux { "3.0" "2.7" "2.6" } }
{ windows { "30" "27" "26" } }
} os of [
"python" prepend find-library
] map-find drop cdecl add-library >>
@ -56,7 +56,11 @@ FUNCTION: int PyTuple_Size ( PyObject* t ) ;
! Lists
! Borrowed reference
FUNCTION: PyObject* PyList_GetItem ( PyObject* l, int pos ) ;
FUNCTION: int PyList_Size ( PyObject* t ) ;
! New reference
FUNCTION: PyObject* PyList_New ( int len ) ;
FUNCTION: int PyList_Size ( PyObject* l ) ;
! Steals the reference
FUNCTION: int PyList_SetItem ( PyObject* l, int pos, PyObject* o ) ;
! Modules

View File

@ -32,6 +32,9 @@ py-initialize
{ "year" "month" "day" } [ getattr >factor ] with map
first3 0 0 0 instant <timestamp> ;
! Lists
[ t ] [ V{ 4 8 15 16 23 42 } dup >py >factor = ] py-test
! ! Datetimes
[ t ] [
[ py-date>factor ] "date" py-type-dispatch get set-at

View File

@ -1,5 +1,5 @@
USING: accessors alien alien.c-types alien.data arrays assocs fry grouping
hashtables kernel namespaces python.ffi sequences strings words ;
hashtables kernel namespaces python.ffi sequences strings vectors words ;
IN: python
QUALIFIED: math
@ -76,12 +76,18 @@ ERROR: python-error type message ;
PyDict_Size ;
! Lists
: <py-list> ( length -- list )
PyList_New check-return ;
: py-list-size ( list -- len )
PyList_Size ;
: py-list-get-item ( obj pos -- val )
PyList_GetItem dup Py_IncRef check-return ;
: py-list-set-item ( obj pos val -- )
dup Py_IncRef PyList_SetItem check-return-code ;
! Unicodes
: py-ucs-size ( -- n )
"maxunicode" PySys_GetObject PyInt_AsLong 0xffff = 2 4 ? ;
@ -102,6 +108,10 @@ ERROR: python-error type message ;
[ length <py-tuple> dup ] keep
[ rot py-tuple-set-item ] with each-index ;
: vector>py-list ( vec -- py-list )
[ length <py-list> dup ] keep
[ rot py-list-set-item ] with each-index ;
: py-tuple>array ( py-tuple -- arr )
dup py-tuple-size iota [ py-tuple-get-item ] with map ;
@ -114,6 +124,8 @@ M: hashtable (>py)
<py-dict> swap dupd [
swapd [ (>py) ] [ (>py) ] bi* py-dict-set-item
] with assoc-each ;
M: vector (>py)
[ (>py) ] map vector>py-list ;
M: word (>py) name>> (>py) ;
@ -132,7 +144,7 @@ DEFER: >factor
{ "dict" [ PyDict_Items (check-return) >factor >hashtable ] }
{ "int" [ PyInt_AsLong ] }
{ "list" [
dup py-list-size iota [ py-list-get-item >factor ] with map
dup py-list-size iota [ py-list-get-item >factor ] with V{ } map-as
] }
{ "long" [ PyLong_AsLong ] }
{ "str" [ PyString_AsString (check-return) ] }

View File

@ -85,6 +85,7 @@ PY-METHODS: file =>
] py-test
PY-METHODS: str =>
lower ( self -- self' )
partition ( self sep -- bef sep aft )
startswith ( self str -- ? )
title ( self -- self' )
@ -117,3 +118,10 @@ PY-METHODS: list =>
$path "test" >py [ append ] [ drop >factor ] [ remove ] 2tri
"test" swap in?
] py-test
! setattr doesn't affect which objects $words are referencing.
PY-FROM: sys => platform ( -- x ) ;
[ t ] [
$platform "sys" import "platform" "tjaba" >py setattr $platform =
] py-test