Translate (.a.b.c x) to x.a.b.c(). (#1105)
Translate (.a.b.c x) to x.a.b.c(). This is useful for, e.g., calling the Series.str.lower method in pandas.
This commit is contained in:
parent
f60ed24c29
commit
2242097b6b
@ -2059,19 +2059,24 @@ class HyASTCompiler(object):
|
|||||||
|
|
||||||
if fn.startswith("."):
|
if fn.startswith("."):
|
||||||
# (.split "test test") -> "test test".split()
|
# (.split "test test") -> "test test".split()
|
||||||
|
# (.a.b.c x) -> (.c (. x a b)) -> x.a.b.c()
|
||||||
|
|
||||||
# Get the attribute name
|
# Get the method name (the last named attribute
|
||||||
ofn = fn
|
# in the chain of attributes)
|
||||||
fn = HySymbol(ofn[1:])
|
attrs = [HySymbol(a).replace(fn) for a in fn.split(".")[1:]]
|
||||||
fn.replace(ofn)
|
fn = attrs.pop()
|
||||||
|
|
||||||
# Get the object we want to take an attribute from
|
# Get the object we're calling the method on
|
||||||
|
# (extracted with the attribute access DSL)
|
||||||
if len(expression) < 2:
|
if len(expression) < 2:
|
||||||
raise HyTypeError(expression,
|
raise HyTypeError(expression,
|
||||||
"attribute access requires object")
|
"attribute access requires object")
|
||||||
func = self.compile(expression.pop(1))
|
|
||||||
|
|
||||||
# And get the attribute
|
func = self.compile(HyExpression(
|
||||||
|
[HySymbol(".").replace(fn), expression.pop(1)] +
|
||||||
|
attrs))
|
||||||
|
|
||||||
|
# And get the method
|
||||||
func += ast.Attribute(lineno=fn.start_line,
|
func += ast.Attribute(lineno=fn.start_line,
|
||||||
col_offset=fn.start_column,
|
col_offset=fn.start_column,
|
||||||
value=func.force_expr,
|
value=func.force_expr,
|
||||||
|
@ -399,7 +399,30 @@
|
|||||||
|
|
||||||
(defn test-dotted []
|
(defn test-dotted []
|
||||||
"NATIVE: test dotted invocation"
|
"NATIVE: test dotted invocation"
|
||||||
(assert (= (.join " " ["one" "two"]) "one two")))
|
(assert (= (.join " " ["one" "two"]) "one two"))
|
||||||
|
|
||||||
|
(defclass X [object] [])
|
||||||
|
(defclass M [object]
|
||||||
|
[meth (fn [self &rest args]
|
||||||
|
(.join " " (+ (, "meth") args)))])
|
||||||
|
|
||||||
|
(setv x (X))
|
||||||
|
(setv m (M))
|
||||||
|
|
||||||
|
(assert (= (.meth m) "meth"))
|
||||||
|
(assert (= (.meth m "foo" "bar") "meth foo bar"))
|
||||||
|
(assert (= (apply .meth [m "foo" "bar"]) "meth foo bar"))
|
||||||
|
|
||||||
|
(setv x.p m)
|
||||||
|
(assert (= (.p.meth x) "meth"))
|
||||||
|
(assert (= (.p.meth x "foo" "bar") "meth foo bar"))
|
||||||
|
(assert (= (apply .p.meth [x "foo" "bar"]) "meth foo bar"))
|
||||||
|
|
||||||
|
(setv x.a (X))
|
||||||
|
(setv x.a.b m)
|
||||||
|
(assert (= (.a.b.meth x) "meth"))
|
||||||
|
(assert (= (.a.b.meth x "foo" "bar") "meth foo bar"))
|
||||||
|
(assert (= (apply .a.b.meth [x "foo" "bar"]) "meth foo bar")))
|
||||||
|
|
||||||
|
|
||||||
(defn test-do []
|
(defn test-do []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user