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:
{ $syntax "PY-METHODS: class => name-effects ;" }
{ $values
@ -36,6 +46,7 @@ HELP: PY-METHODS:
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."
{ $subsections \ PY-FROM: \ PY-QUALIFIED-FROM: \ PY-METHODS: }
$nl
{ $examples "Here is how you bind and call a method namelist on a ZipFile instance created by importing the zipfile module:"
{ $code

View File

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