alien.syntax assocs kernel sequences system ;
IN: python.ffi
-<< "python" {
- { linux { "3.0" "2.7" "2.6" } }
- { windows { "30" "27" "26" } }
-} os of [
- "python" prepend find-library
-] map-find drop cdecl add-library >>
+<< "python" { "3.0" "2.7" "2.6" }
+os windows? [ [ [ CHAR: . = not ] filter ] map ] when
+[ "python" prepend find-library ] map-find drop
+cdecl add-library >>
! Functions that return borrowed references needs to be called like this:
! Py_Func dup Py_IncRef &Py_DecRef
{ $examples
{ $example
"USING: python ;"
- "10 iota >array >py >factor ."
+ "10 iota >array >py py> ."
"{ 0 1 2 3 4 5 6 7 8 9 }"
}
}
-{ $see-also >factor } ;
+{ $see-also py> } ;
ARTICLE: "python" "Python binding"
"The " { $vocab-link "python" } " vocab and its subvocabs implements a simple binding for libpython, allowing factor code to call native python."
"Initialization and finalization:"
{ $subsections py-initialize py-finalize }
"Module management:"
-{ $subsections import }
+{ $subsections py-import }
"The vocab " { $vocab-link "python.syntax" } " implements a higher level factorific interface on top of the lower-level constructs in this vocab. Prefer to use that vocab most of the time."
{ $notes "Sometimes the embedded python interpreter can't find or finds the wrong load path to it's module library. To counteract that problem it is recommended that the " { $snippet "PYTHONHOME" } " environment variable is set before " { $link py-initialize } " is called. E.g:" }
{ $code "\"C:/python27-64bit/\" \"PYTHONHOME\" set-os-env" } ;
[ t ] [ Py_GetVersion string? ] unit-test
-[ "os" ] [ "os" import PyModule_GetName ] py-test
+[ "os" ] [ "os" py-import PyModule_GetName ] py-test
[ t ] [
- "os" import "getpid" getattr
- { } >py call-object >factor 0 >
+ "os" py-import "getpid" getattr
+ { } >py call-object py> 0 >
] py-test
[ t ] [ Py_IsInitialized ] py-test
-! Importing
+! py-importing
[ { "ImportError" "No module named kolobi" } ] [
- [ "kolobi" import ] [ [ type>> ] [ message>> ] bi 2array ] recover
+ [ "kolobi" py-import ] [ [ type>> ] [ message>> ] bi 2array ] recover
] py-test
! setattr
[ 73 ] [
- "sys" import "testit" [ 73 >py setattr ] [ getattr >factor ] 2bi
+ "sys" py-import "testit" [ 73 >py setattr ] [ getattr py> ] 2bi
] py-test
! Tuples
[ 2 ] [ 2 <py-tuple> py-tuple-size ] py-test
-: py-date>factor ( py-obj -- timestamp )
- { "year" "month" "day" } [ getattr >factor ] with map
+: py-datepy> ( py-obj -- timestamp )
+ { "year" "month" "day" } [ getattr py> ] with map
first3 0 0 0 instant <timestamp> ;
! Lists
-[ t ] [ V{ 4 8 15 16 23 42 } dup >py >factor = ] py-test
+[ t ] [ V{ 4 8 15 16 23 42 } dup >py py> = ] py-test
! ! Datetimes
[ t ] [
- [ py-date>factor ] "date" py-type-dispatch get set-at
- "datetime" import "date" getattr "today" getattr
- { } >py call-object >factor
+ [ py-datepy> ] "date" py-type-dispatch get set-at
+ "datetime" py-import "date" getattr "today" getattr
+ { } >py call-object py>
today instant >>gmt-offset =
] py-test
! Unicode
[ "غثههح" ] [
- "os.path" import "basename" getattr { "غثههح" } >py call-object >factor
+ "os.path" py-import "basename" getattr { "غثههح" } >py call-object py>
] py-test
! Instance variables
[ 7 ] [
- "datetime" import "timedelta" getattr
- { 7 } >py call-object "days" getattr >factor
+ "datetime" py-import "timedelta" getattr
+ { 7 } >py call-object "days" getattr py>
] py-test
! Create a dictonary
[ 33 ] [
<py-dict> "tjaba"
[ 33 >py py-dict-set-item-string ]
- [ py-dict-get-item-string >factor ] 2bi
+ [ py-dict-get-item-string py> ] 2bi
] py-test
! Nest dicts
] py-test
! Round tripping!
-[ { "foo" { 99 77 } } ] [ { "foo" { 99 77 } } >py >factor ] py-test
+[ { "foo" { 99 77 } } ] [ { "foo" { 99 77 } } >py py> ] py-test
[ H{ { "foo" "bar" } { 3 4 } } ] [
- H{ { "foo" "bar" } { 3 4 } } >py >factor
+ H{ { "foo" "bar" } { 3 4 } } >py py>
] py-test
! Kwargs
[ 2014 10 22 ] [
- "datetime" import "date" getattr
+ "datetime" py-import "date" getattr
{ } >py H{ { "year" 2014 } { "month" 10 } { "day" 22 } } >py
- call-object-full >factor
+ call-object-full py>
[ year>> ] [ month>> ] [ day>> ] tri
] py-test
! Modules
[ t ] [
- "os" import PyModule_GetDict dup Py_IncRef &Py_DecRef py-dict-size 100 >
+ "os" py-import PyModule_GetDict dup Py_IncRef &Py_DecRef py-dict-size 100 >
] py-test
Py_IsInitialized [ Py_Finalize ] when ;
! Importing
-: import ( str -- module )
+: py-import ( str -- module )
PyImport_ImportModule check-new-ref ;
! Unicodes
M: array (>py) [ (>py) ] map array>py-tuple ;
M: hashtable (>py)
<py-dict> swap dupd [
- swapd [ (>py) ] [ (>py) ] bi* py-dict-set-item
+ swapd [ (>py) ] bi@ py-dict-set-item
] with assoc-each ;
M: vector (>py)
[ (>py) ] map vector>py-list ;
! Data marshalling to Factor
SYMBOL: py-type-dispatch
-DEFER: >factor
+DEFER: py>
: init-py-type-dispatch ( -- table )
H{
{ "NoneType" [ drop f ] }
{ "bool" [ PyObject_IsTrue 1 = ] }
- { "dict" [ PyDict_Items (check-ref) >factor >hashtable ] }
+ { "dict" [ PyDict_Items (check-ref) py> >hashtable ] }
{ "int" [ PyInt_AsLong ] }
- { "list" [ py-list>vector [ >factor ] map ] }
+ { "list" [ py-list>vector [ py> ] map ] }
{ "long" [ PyLong_AsLong ] }
{ "str" [ PyString_AsString (check-ref) ] }
- { "tuple" [ py-tuple>array [ >factor ] map ] }
+ { "tuple" [ py-tuple>array [ py> ] map ] }
{ "unicode" [ py-unicode>utf8 ] }
} clone ;
ERROR: missing-type type ;
-: >factor ( py-obj -- obj )
+: py> ( py-obj -- obj )
dup "__class__" getattr "__name__" getattr PyString_AsString
py-type-dispatch get ?at [ call( x -- x ) ] [ missing-type ] if ;
"PY-FROM: zipfile => ZipFile ( name mode -- file ) ;"
"PY-METHODS: ZipFile => namelist ( self -- names ) ;"
"! Then use the declarations like this"
- "\"name-of-zip.zip\" >py \"r\" >py ZipFile namelist >factor"
+ "\"name-of-zip.zip\" >py \"r\" >py ZipFile namelist py>"
}
} ;
"PY-FROM: zipfile => ZipFile ( name mode -- file ) ;"
"PY-METHODS: ZipFile => namelist ( self -- names ) ;"
"! Then use the declarations like this"
- "\"name-of-zip.zip\" >py \"r\" >py ZipFile namelist >factor"
+ "\"name-of-zip.zip\" >py \"r\" >py ZipFile namelist py>"
}
"In python, a method or function takes keyword arguments if its last parameter starts with \"**\". If the name of the last argument to a declared function is \"**\" then a " { $link hashtable } " can be sent to the function:"
{ $code
"PY-FROM: datetime => timedelta ( ** -- timedelta ) ;"
"PY-METHODS: timedelta => seconds ( self -- n ) ;"
- "H{ { \"hours\" 99 } { \"minutes\" 33 } } >py timedelta $seconds >factor ."
+ "H{ { \"hours\" 99 } { \"minutes\" 33 } } >py timedelta $seconds py> ."
"12780"
}
} ;
splitting tools.test unicode.categories ;
IN: python.syntax.tests
-! Importing functions
+! py-importing functions
PY-FROM: os =>
getpid ( -- y )
system ( x -- y ) ;
-[ t ] [ getpid >factor integer? ] unit-test
+[ t ] [ getpid py> integer? ] unit-test
! ! Automatic tuple unpacking
PY-FROM: os.path =>
basename ( x -- x' )
splitext ( x -- base ext ) ;
-[ "hello.doc" ] [ "/some/path/hello.doc" >py basename >factor ] unit-test
+[ "hello.doc" ] [ "/some/path/hello.doc" >py basename py> ] unit-test
[ { "hello" ".doc" } ] [
- "hello.doc" >py splitext 2array [ >factor ] map
+ "hello.doc" >py splitext 2array [ py> ] map
] unit-test
PY-FROM: time => sleep ( n -- ) ;
! Module variables are bound as zero-arg functions
PY-FROM: sys => path ( -- seq ) argv ( -- seq ) ;
-[ t ] [ $path >factor sequence? ] unit-test
+[ t ] [ $path py> sequence? ] unit-test
PY-FROM: __builtin__ =>
callable ( obj -- ? )
range ( n -- seq )
repr ( obj -- str ) ;
-[ t ] [ $path len int >factor 5 > ] unit-test
+[ t ] [ $path len int py> 5 > ] unit-test
-[ 10 ] [ 10 >py range len >factor ] unit-test
+[ 10 ] [ 10 >py range len py> ] unit-test
! Callables
[ t ] [
- "os" import "getpid" getattr
+ "os" py-import "getpid" getattr
[ callable ] [ PyCallable_Check 1 = ] bi and
] unit-test
! Reference counting
PY-FROM: sys => getrefcount ( obj -- n ) ;
-[ 2 ] [ 3 <py-tuple> getrefcount >factor ] unit-test
+[ 2 ] [ 3 <py-tuple> getrefcount py> ] unit-test
[ -2 ] [
H{ { "foo" 33 } { "bar" 44 } } >py
- [ "foo" py-dict-get-item-string getrefcount >factor ]
+ [ "foo" py-dict-get-item-string getrefcount py> ]
[
'[
500 [ _ "foo" py-dict-get-item-string drop ] times
] with-destructors
]
- [ "foo" py-dict-get-item-string getrefcount >factor ] tri -
+ [ "foo" py-dict-get-item-string getrefcount py> ] tri -
] py-test
[ -2 ] [
"abcd" >py <1py-tuple>
- [ 0 py-tuple-get-item getrefcount >factor ]
+ [ 0 py-tuple-get-item getrefcount py> ]
[
[ 100 [ swap 0 py-tuple-get-item drop ] with times ] with-destructors
]
- [ 0 py-tuple-get-item getrefcount >factor ] tri -
+ [ 0 py-tuple-get-item getrefcount py> ] tri -
] py-test
PY-METHODS: file =>
[ t ] [
"python-file" temp-file >py "wb" >py open
[ tell ] [ fileno ] [ close ] tri
- [ >factor integer? ] bi@ and
+ [ py> integer? ] bi@ and
] py-test
PY-METHODS: str =>
! Method chaining
[ t ] [
- "hello there" >py title 20 >py zfill "00" >py startswith >factor
+ "hello there" >py title 20 >py zfill "00" >py startswith py>
] py-test
[ { "hello" "=" "there" } ] [
- "hello=there" >py "=" >py partition 3array [ >factor ] map
+ "hello=there" >py "=" >py partition 3array [ py> ] map
] py-test
! Introspection
PY-METHODS: code =>
co_argcount ( code -- n ) ;
-[ 1 ] [ $splitext $func_code $co_argcount >factor ] py-test
+[ 1 ] [ $splitext $func_code $co_argcount py> ] py-test
! Change sys.path
PY-METHODS: list =>
remove ( list obj -- ) ;
[ t ] [
- $path "test" >py [ append ] [ drop >factor ] [ remove ] 2tri
+ $path "test" >py [ append ] [ drop py> ] [ remove ] 2tri
"test" swap in?
] py-test
PY-FROM: sys => platform ( -- x ) ;
[ t ] [
- $platform "sys" import "platform" "tjaba" >py setattr $platform =
+ $platform "sys" py-import "platform" "tjaba" >py setattr $platform =
] py-test
! Support for kwargs
PY-FROM: datetime => timedelta ( ** -- timedelta ) ;
[ "datetime.timedelta(4, 10800)" ] [
- H{ { "hours" 99 } } >py timedelta repr >factor
+ H{ { "hours" 99 } } >py timedelta repr py>
] py-test
! Kwargs in methods
[
ArgumentParser dup
"--foo" >py H{ { "help" "badger" } } >py add_argument
- format_help >factor
+ format_help py>
] with-destructors [ blank? ] trim " " split "badger" swap in?
] py-test