}
} ;
+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
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
[ 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
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