2014-03-12 23:01:11 -04:00
|
|
|
USING: arrays assocs destructors fry io.files.temp kernel math
|
|
|
|
namespaces python python.ffi python.objects sequences sets
|
2014-02-07 11:10:10 -05:00
|
|
|
splitting tools.test unicode.categories ;
|
2014-03-12 23:01:11 -04:00
|
|
|
IN: python.syntax
|
|
|
|
|
|
|
|
: py-test ( result quot -- )
|
|
|
|
'[ _ with-destructors ] unit-test ; inline
|
2014-01-28 13:19:57 -05:00
|
|
|
|
2014-03-04 12:39:02 -05:00
|
|
|
! py-importing functions
|
2014-01-28 13:19:57 -05:00
|
|
|
PY-FROM: os =>
|
|
|
|
getpid ( -- y )
|
|
|
|
system ( x -- y ) ;
|
|
|
|
|
2014-03-04 12:39:02 -05:00
|
|
|
[ t ] [ getpid py> 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-03-04 12:39:02 -05:00
|
|
|
[ "hello.doc" ] [ "/some/path/hello.doc" >py basename py> ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
2014-01-28 18:31:43 -05:00
|
|
|
[ { "hello" ".doc" } ] [
|
2014-03-04 12:39:02 -05:00
|
|
|
"hello.doc" >py splitext 2array [ py> ] map
|
2014-01-28 18:31:43 -05:00
|
|
|
] 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-02-03 05:28:43 -05:00
|
|
|
PY-FROM: sys => path ( -- seq ) argv ( -- seq ) ;
|
2014-01-28 13:19:57 -05:00
|
|
|
|
2014-03-04 12:39:02 -05:00
|
|
|
[ t ] [ $path py> 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-03-04 12:39:02 -05:00
|
|
|
[ t ] [ $path len int py> 5 > ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
2014-03-04 12:39:02 -05:00
|
|
|
[ 10 ] [ 10 >py range len py> ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
|
|
|
! Callables
|
|
|
|
[ t ] [
|
2014-03-04 12:39:02 -05:00
|
|
|
"os" py-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-03-04 12:39:02 -05:00
|
|
|
[ 2 ] [ 3 <py-tuple> getrefcount py> ] unit-test
|
2014-01-28 13:19:57 -05:00
|
|
|
|
|
|
|
[ -2 ] [
|
|
|
|
H{ { "foo" 33 } { "bar" 44 } } >py
|
2014-03-04 12:39:02 -05:00
|
|
|
[ "foo" py-dict-get-item-string getrefcount py> ]
|
2014-01-28 13:19:57 -05:00
|
|
|
[
|
|
|
|
'[
|
|
|
|
500 [ _ "foo" py-dict-get-item-string drop ] times
|
|
|
|
] with-destructors
|
|
|
|
]
|
2014-03-04 12:39:02 -05:00
|
|
|
[ "foo" py-dict-get-item-string getrefcount py> ] tri -
|
2014-01-28 13:19:57 -05:00
|
|
|
] py-test
|
|
|
|
|
|
|
|
[ -2 ] [
|
|
|
|
"abcd" >py <1py-tuple>
|
2014-03-04 12:39:02 -05:00
|
|
|
[ 0 py-tuple-get-item getrefcount py> ]
|
2014-01-28 13:19:57 -05:00
|
|
|
[
|
|
|
|
[ 100 [ swap 0 py-tuple-get-item drop ] with times ] with-destructors
|
|
|
|
]
|
2014-03-04 12:39:02 -05:00
|
|
|
[ 0 py-tuple-get-item getrefcount py> ] tri -
|
2014-01-28 18:31:43 -05:00
|
|
|
] py-test
|
|
|
|
|
|
|
|
PY-METHODS: file =>
|
|
|
|
close ( self -- )
|
|
|
|
fileno ( self -- n )
|
|
|
|
tell ( self -- n ) ;
|
|
|
|
|
|
|
|
[ t ] [
|
2014-02-07 11:10:10 -05:00
|
|
|
"python-file" temp-file >py "wb" >py open
|
2014-01-29 12:19:07 -05:00
|
|
|
[ tell ] [ fileno ] [ close ] tri
|
2014-03-04 12:39:02 -05:00
|
|
|
[ py> integer? ] bi@ and
|
2014-01-28 18:31:43 -05:00
|
|
|
] 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-03-04 12:39:02 -05:00
|
|
|
"hello there" >py title 20 >py zfill "00" >py startswith py>
|
2014-01-29 12:19:07 -05:00
|
|
|
] py-test
|
|
|
|
|
|
|
|
[ { "hello" "=" "there" } ] [
|
2014-03-04 12:39:02 -05:00
|
|
|
"hello=there" >py "=" >py partition 3array [ py> ] map
|
2014-01-29 12:19:07 -05:00
|
|
|
] py-test
|
|
|
|
|
|
|
|
! Introspection
|
|
|
|
PY-METHODS: func =>
|
|
|
|
func_code ( func -- code ) ;
|
|
|
|
|
|
|
|
PY-METHODS: code =>
|
|
|
|
co_argcount ( code -- n ) ;
|
|
|
|
|
2014-03-04 12:39:02 -05:00
|
|
|
[ 1 ] [ $splitext $func_code $co_argcount py> ] py-test
|
2014-01-29 12:19:07 -05:00
|
|
|
|
|
|
|
! Change sys.path
|
|
|
|
PY-METHODS: list =>
|
|
|
|
append ( list obj -- )
|
|
|
|
remove ( list obj -- ) ;
|
|
|
|
|
|
|
|
[ t ] [
|
2014-03-04 12:39:02 -05:00
|
|
|
$path "test" >py [ append ] [ drop py> ] [ remove ] 2tri
|
2014-01-29 12:19:07 -05:00
|
|
|
"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 ] [
|
2014-03-04 12:39:02 -05:00
|
|
|
$platform "sys" py-import "platform" "tjaba" >py setattr $platform =
|
2014-01-30 11:24:58 -05:00
|
|
|
] py-test
|
2014-01-31 11:00:26 -05:00
|
|
|
|
|
|
|
! Support for kwargs
|
|
|
|
PY-FROM: datetime => timedelta ( ** -- timedelta ) ;
|
|
|
|
|
|
|
|
[ "datetime.timedelta(4, 10800)" ] [
|
2014-03-04 12:39:02 -05:00
|
|
|
H{ { "hours" 99 } } >py timedelta repr py>
|
2014-01-31 11:00:26 -05:00
|
|
|
] py-test
|
2014-02-03 05:28:43 -05:00
|
|
|
|
|
|
|
! Kwargs in methods
|
|
|
|
PY-FROM: argparse => ArgumentParser ( -- self ) ;
|
|
|
|
PY-METHODS: ArgumentParser =>
|
|
|
|
add_argument ( self name ** -- )
|
|
|
|
format_help ( self -- str ) ;
|
|
|
|
|
|
|
|
[ t ] [
|
|
|
|
[
|
|
|
|
ArgumentParser dup
|
|
|
|
"--foo" >py H{ { "help" "badger" } } >py add_argument
|
2014-03-04 12:39:02 -05:00
|
|
|
format_help py>
|
2014-02-03 05:28:43 -05:00
|
|
|
] with-destructors [ blank? ] trim " " split "badger" swap in?
|
|
|
|
] py-test
|
2014-02-07 11:10:10 -05:00
|
|
|
|
|
|
|
! Can you pass a callback written in factor to a python function?
|
|
|
|
PY-FROM: wsgiref.simple_server => make_server ( iface port callback -- httpd ) ;
|