python: new syntax PY-QUALIFIED-FROM, so you can import eg __builtin__:map and not have it clash with factors map

db4
Björn Lindqvist 2014-10-23 18:43:36 +02:00 committed by John Benediktsson
parent dce2e90839
commit ba564d1b78
2 changed files with 30 additions and 9 deletions

View File

@ -16,6 +16,16 @@ HELP: PY-FROM:
} }
} ; } ;
HELP: PY-QUALIFIED-FROM:
{ $syntax "PY-QUALIFIED-FROM: module => name-effects ;" }
{ $values
{ "module" "fully qualified name of a python module" }
{ "name-effects" "pairs of names and effect declarations of bindings to import" }
}
{ $description
"Like " { $link \ PY-FROM: } " except all words are created with module as the given prefix."
} ;
HELP: PY-METHODS: HELP: PY-METHODS:
{ $syntax "PY-METHODS: class => name-effects ;" } { $syntax "PY-METHODS: class => name-effects ;" }
{ $values { $values
@ -36,6 +46,7 @@ HELP: PY-METHODS:
ARTICLE: "python.syntax" "Syntax for python calls from factor" ARTICLE: "python.syntax" "Syntax for python calls from factor"
"The " { $vocab-link "python.syntax" } " vocab adds syntax to factor to make calls from factor to python natural and intuitive." "The " { $vocab-link "python.syntax" } " vocab adds syntax to factor to make calls from factor to python natural and intuitive."
{ $subsections \ PY-FROM: \ PY-QUALIFIED-FROM: \ PY-METHODS: }
$nl $nl
{ $examples "Here is how you bind and call a method namelist on a ZipFile instance created by importing the zipfile module:" { $examples "Here is how you bind and call a method namelist on a ZipFile instance created by importing the zipfile module:"
{ $code { $code

View File

@ -33,16 +33,20 @@ SYMBOL: current-context
[ in>> gather-args-quot ] [ out>> unpack-value-quot ] bi [ in>> gather-args-quot ] [ out>> unpack-value-quot ] bi
swapd '[ @ _ -rot call-object-full @ ] ; swapd '[ @ _ -rot call-object-full @ ] ;
: function-callable ( name alien effect -- ) : make-factor-words ( module name prefix? -- call-word obj-word )
[ create-in ] 2dip [ make-function-quot ] keep define-inline ; inline [ [ ":" glue ] [ ":$" glue ] 2bi ] [ nip dup "$" prepend ] if
[ create-in ] bi@ ;
: function-object ( name alien -- ) : import-getattr ( module name -- alien )
[ "$" prepend create-in ] [ '[ _ ] ] bi* [ py-import ] dip getattr ;
{ } { "obj" } <effect> define-inline ; inline
: add-function ( name effect -- ) : make-creator-quots ( alien effect -- call-quot obj-quot )
[ dup current-context get py-import swap getattr 2dup ] dip [ '[ _ _ [ make-function-quot ] keep define-inline ] ]
function-callable function-object ; inline [ drop '[ [ _ ] { } { "obj" } <effect> define-inline ] ] 2bi ; inline
: add-function ( effect module name prefix? -- )
[ make-factor-words ] [ drop import-getattr ] 3bi [ rot ] dip swap
make-creator-quots bi* ;
: make-method-quot ( name effect -- quot ) : make-method-quot ( name effect -- quot )
[ in>> 1 tail gather-args-quot ] [ out>> unpack-value-quot ] bi swapd [ in>> 1 tail gather-args-quot ] [ out>> unpack-value-quot ] bi swapd
@ -60,6 +64,12 @@ SYMBOL: current-context
PRIVATE> PRIVATE>
SYNTAX: PY-FROM: [ add-function ] scan-definitions ; inline SYNTAX: PY-FROM: [
current-context get rot f add-function
] scan-definitions ; inline
SYNTAX: PY-QUALIFIED-FROM: [
current-context get rot t add-function
] scan-definitions ; inline
SYNTAX: PY-METHODS: [ add-method ] scan-definitions ; inline SYNTAX: PY-METHODS: [ add-method ] scan-definitions ; inline