Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add class methods, unit tests for flytefile and flytedirectory #2852

Merged
merged 1 commit into from
Oct 23, 2024

Conversation

granthamtaylor
Copy link
Contributor

I feel like it is verbose to get the working directory (flytekit.current_context().working_directory) in a task. I find myself doing this very, very frequently, primarily to write files. I use the working directory so that files won’t get written to my local working directory during local execution.

I had thought about this for a while, and the more I thought about what exactly I wanted to accomplish, I feel like it should be accomplished with a few new classmethod for FlyteFile / FlyteDirectory...


This task...

@task
def my_task() -> FlyteFile:
    local_path = os.path.join(current_context().working_directory, "data.txt")
    with open(local_path, mode="w") as file:
        file.write("Here is some sample data.")
    return FlyteFile(path=local_path)

Should ideally be replaced with something like this:

@task
def my_task() -> FlyteFile:

    ff = FlyteFile.new('myfile.txt')

    with open(ff, mode="w") as file:
        file.write("Here is some sample data.")

    return ff

In this case, FlyteFile.new will create a FlyteFile with a path of the filename inside of the working directory (current_context().working_directory).


The same goes for FlyteDirectory...

@task
def my_task() -> FlyteDirectory:

    p = os.path.join(current_context().working_directory, "my_new_directory")

    os.makedirs(p)

    with open(os.path.join(p, "file_1.txt"), 'w') as file:
        file.write("This is file 1.")

    return FlyteDirectory(p)

Should be replaced with:

@task
def my_task() -> FlyteDirectory:

    dir = FlyteDirectory.new('my_new_directory')

    with open(dir / "file_1.txt", 'w') as file:
        file.write("This is file 1.")

    return dir

In this case, FlyteDirectory.new will create the directory my_new_directory under current_context().working_directory, and ensure that it exists (os.makedirs(dir.path))

Additionally, I am overloading the __truedivide__ operator to FlyteDirectory for convenience:

class FlyteDirectory:

    ...

    def __truedivide__(self, other: str | os.PathLike) -> Path:
        """
        This is a convenience method to allow for easy concatenation of paths.
        """
        return Path(self.path) / other

Copy link

codecov bot commented Oct 23, 2024

Codecov Report

Attention: Patch coverage is 76.47059% with 4 lines in your changes missing coverage. Please review.

Project coverage is 73.03%. Comparing base (3fc51af) to head (ba9b4b8).
Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
flytekit/types/directory/types.py 80.00% 1 Missing and 1 partial ⚠️
flytekit/types/file/file.py 71.42% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #2852       +/-   ##
===========================================
+ Coverage   45.53%   73.03%   +27.50%     
===========================================
  Files         196      196               
  Lines       20418    20471       +53     
  Branches     2647     2650        +3     
===========================================
+ Hits         9298    14952     +5654     
+ Misses      10658     4798     -5860     
- Partials      462      721      +259     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.


path = os.path.join(ctx.user_space_params.working_directory, dirname)

os.makedirs(path, exist_ok=False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the exist_ok=False.

@granthamtaylor granthamtaylor merged commit 5a8941b into master Oct 23, 2024
106 checks passed
@granthamtaylor granthamtaylor deleted the grantham/easier-flyte-file-directory-init branch October 23, 2024 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants