2014-01-28 18:31:43 -05:00
|
|
|
USING: arrays assocs destructors fry kernel math namespaces python python.ffi
|
2014-01-31 09:59:04 -05:00
|
|
|
python.objects python.syntax python.tests sequences sets tools.test ;
|
2014-01-28 13:19:57 -05:00
|
|
|
IN: python.syntax.tests
|
|
|
|
|
|
|
|
! Importing functions
|
|
|
|
PY-FROM: os =>
|
|
|
|
getpid ( -- y )
|
|
|
|
system ( x -- y ) ;
|
|
|
|
|
2014-01-28 18:31:43 -05:00
|
|
|
[ t ] [ getpid >factor integer? ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
2014-01-28 18:31:43 -05:00
|
|
|
! ! Automatic tuple unpacking
|
2014-01-28 13:19:57 -05:00
|
|
|
PY-FROM: os.path =>
|
|
|
|
basename ( x -- x' )
|
|
|
|
splitext ( x -- base ext ) ;
|
|
|
|
|
2014-01-28 18:31:43 -05:00
|
|
|
[ "hello.doc" ] [ "/some/path/hello.doc" >py basename >factor ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
2014-01-28 18:31:43 -05:00
|
|
|
[ { "hello" ".doc" } ] [
|
|
|
|
"hello.doc" >py splitext 2array [ >factor ] map
|
|
|
|
] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
|
|
|
PY-FROM: time => sleep ( n -- ) ;
|
|
|
|
|
2014-01-28 18:31:43 -05:00
|
|
|
[ ] [ 0 >py sleep ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
2014-01-29 12:19:07 -05:00
|
|
|
! Module variables are bound as zero-arg functions
|
2014-01-28 13:19:57 -05:00
|
|
|
PY-FROM: sys => path ( -- seq ) ;
|
|
|
|
|
2014-01-29 12:19:07 -05:00
|
|
|
[ t ] [ $path >factor sequence? ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
|
|
|
PY-FROM: __builtin__ =>
|
|
|
|
callable ( obj -- ? )
|
2014-01-29 12:19:07 -05:00
|
|
|
dir ( obj -- seq )
|
2014-01-28 13:19:57 -05:00
|
|
|
int ( val -- s )
|
|
|
|
len ( seq -- n )
|
2014-01-29 12:19:07 -05:00
|
|
|
open ( name mode -- file )
|
|
|
|
range ( n -- seq )
|
|
|
|
repr ( obj -- str ) ;
|
2014-01-28 13:19:57 -05:00
|
|
|
|
2014-01-29 12:19:07 -05:00
|
|
|
[ t ] [ $path len int >factor 5 > ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
2014-01-28 18:31:43 -05:00
|
|
|
[ 10 ] [ 10 >py range len >factor ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
|
|
|
! Callables
|
|
|
|
[ t ] [
|
|
|
|
"os" import "getpid" getattr
|
2014-01-28 18:31:43 -05:00
|
|
|
[ callable ] [ PyCallable_Check 1 = ] bi and
|
|
|
|
] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
|
|
|
! Reference counting
|
|
|
|
PY-FROM: sys => getrefcount ( obj -- n ) ;
|
|
|
|
|
2014-01-28 18:31:43 -05:00
|
|
|
[ 2 ] [ 3 <py-tuple> getrefcount >factor ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
|
|
|
[ -2 ] [
|
|
|
|
H{ { "foo" 33 } { "bar" 44 } } >py
|
2014-01-28 18:31:43 -05:00
|
|
|
[ "foo" py-dict-get-item-string getrefcount >factor ]
|
2014-01-28 13:19:57 -05:00
|
|
|
[
|
|
|
|
'[
|
|
|
|
500 [ _ "foo" py-dict-get-item-string drop ] times
|
|
|
|
] with-destructors
|
|
|
|
]
|
2014-01-28 18:31:43 -05:00
|
|
|
[ "foo" py-dict-get-item-string getrefcount >factor ] tri -
|
2014-01-28 13:19:57 -05:00
|
|
|
] py-test
|
|
|
|
|
|
|
|
[ -2 ] [
|
|
|
|
"abcd" >py <1py-tuple>
|
2014-01-28 18:31:43 -05:00
|
|
|
[ 0 py-tuple-get-item getrefcount >factor ]
|
2014-01-28 13:19:57 -05:00
|
|
|
[
|
|
|
|
[ 100 [ swap 0 py-tuple-get-item drop ] with times ] with-destructors
|
|
|
|
]
|
2014-01-28 18:31:43 -05:00
|
|
|
[ 0 py-tuple-get-item getrefcount >factor ] tri -
|
|
|
|
] py-test
|
|
|
|
|
|
|
|
PY-METHODS: file =>
|
|
|
|
close ( self -- )
|
|
|
|
fileno ( self -- n )
|
|
|
|
tell ( self -- n ) ;
|
|
|
|
|
|
|
|
[ t ] [
|
|
|
|
"testfile" >py "wb" >py open
|
2014-01-29 12:19:07 -05:00
|
|
|
[ tell ] [ fileno ] [ close ] tri
|
2014-01-28 18:31:43 -05:00
|
|
|
[ >factor integer? ] bi@ and
|
|
|
|
] py-test
|
|
|
|
|
|
|
|
PY-METHODS: str =>
|
2014-01-30 11:24:58 -05:00
|
|
|
lower ( self -- self' )
|
2014-01-29 12:19:07 -05:00
|
|
|
partition ( self sep -- bef sep aft )
|
2014-01-28 18:31:43 -05:00
|
|
|
startswith ( self str -- ? )
|
2014-01-29 12:19:07 -05:00
|
|
|
title ( self -- self' )
|
2014-01-28 18:31:43 -05:00
|
|
|
zfill ( self n -- str' ) ;
|
|
|
|
|
|
|
|
! Method chaining
|
|
|
|
[ t ] [
|
2014-01-29 12:19:07 -05:00
|
|
|
"hello there" >py title 20 >py zfill "00" >py startswith >factor
|
|
|
|
] py-test
|
|
|
|
|
|
|
|
[ { "hello" "=" "there" } ] [
|
|
|
|
"hello=there" >py "=" >py partition 3array [ >factor ] map
|
|
|
|
] py-test
|
|
|
|
|
|
|
|
! Introspection
|
|
|
|
PY-METHODS: func =>
|
|
|
|
func_code ( func -- code ) ;
|
|
|
|
|
|
|
|
PY-METHODS: code =>
|
|
|
|
co_argcount ( code -- n ) ;
|
|
|
|
|
|
|
|
[ 1 ] [ $splitext $func_code $co_argcount >factor ] py-test
|
|
|
|
|
|
|
|
! Change sys.path
|
|
|
|
PY-METHODS: list =>
|
|
|
|
append ( list obj -- )
|
|
|
|
remove ( list obj -- ) ;
|
|
|
|
|
|
|
|
[ t ] [
|
|
|
|
$path "test" >py [ append ] [ drop >factor ] [ remove ] 2tri
|
|
|
|
"test" swap in?
|
2014-01-28 13:19:57 -05:00
|
|
|
] py-test
|
2014-01-30 11:24:58 -05:00
|
|
|
|
|
|
|
! 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
|