Skip to content

Commit

Permalink
Execute files relative to their directory
Browse files Browse the repository at this point in the history
Fixes #305
  • Loading branch information
saulshanabrook committed Oct 18, 2021
1 parent 0df726c commit 464466f
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
6 changes: 6 additions & 0 deletions lineapy/cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import pathlib

import click
Expand Down Expand Up @@ -83,6 +84,11 @@ def linea_cli(
"Source code", rich.syntax.Syntax(code, "python")
)
)

# Change the working directory to that of the script,
# To pick up relative data paths
os.chdir(file_name.parent)

tracer = Tracer(db, SessionType.SCRIPT)
transform(code, file_name, tracer)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
assets = pd.read_csv("tests/ames_train_cleaned.csv")
assets = pd.read_csv("ames_train_cleaned.csv")
def is_new(col):
return col > 1970
assets["is_new"] = is_new(assets["Year_Built"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
alt.data_transformers.enable("json")
alt.renderers.enable("mimetype")
assets = pd.read_csv("tests/ames_train_cleaned.csv")
assets = pd.read_csv("ames_train_cleaned.csv")
sns.relplot(data=assets, x="Year_Built", y="SalePrice", size="Lot_Area")
Expand Down Expand Up @@ -164,7 +164,7 @@ def is_new(col):
lineno=11,
col_offset=9,
end_lineno=11,
end_col_offset=52,
end_col_offset=46,
source_code=source_1.id,
),
function_id=CallNode(
Expand Down Expand Up @@ -202,10 +202,10 @@ def is_new(col):
lineno=11,
col_offset=21,
end_lineno=11,
end_col_offset=51,
end_col_offset=45,
source_code=source_1.id,
),
value="tests/ames_train_cleaned.csv",
value="ames_train_cleaned.csv",
).id
],
)
Expand Down
2 changes: 1 addition & 1 deletion tests/housing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
alt.data_transformers.enable("json")
alt.renderers.enable("mimetype")

assets = pd.read_csv("tests/ames_train_cleaned.csv")
assets = pd.read_csv("ames_train_cleaned.csv")

sns.relplot(data=assets, x="Year_Built", y="SalePrice", size="Lot_Area")

Expand Down
8 changes: 7 additions & 1 deletion tests/test_end_to_end.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
import os
from pathlib import Path
from tempfile import NamedTemporaryFile

Expand Down Expand Up @@ -435,7 +436,12 @@ def test_assignment_destructuring(self, execute):
assert res.values["b"] == 2

def test_housing(self, execute, python_snapshot):
code = (Path(__file__).parent / "housing.py").read_text()
tests_dir = Path(__file__).parent

# Change directory to tests dir before executing
os.chdir(tests_dir)

code = (tests_dir / "housing.py").read_text()
res = execute(code)
assert res.slice("p value") == python_snapshot

Expand Down
6 changes: 2 additions & 4 deletions tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,11 @@ def test_export_slice_housing():


def test_kaggle_example1():
os.chdir("examples")

subprocess.check_call(
[
"lineapy",
"kaggle_example1.py",
"examples/kaggle_example1.py",
"--slice",
"mushroom feature importance",
]
Expand All @@ -51,12 +50,11 @@ def test_kaggle_example1():

@pytest.mark.xfail(reason="lambdas aren't supported")
def test_kaggle_example2():
os.chdir("examples")

subprocess.check_call(
[
"lineapy",
"kaggle_example2.py",
"examples/kaggle_example2.py",
"--slice",
"nn for diabetes",
]
Expand Down

0 comments on commit 464466f

Please sign in to comment.