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

Grantham/new methods flytefile and flytedirectory #2842

Closed

Conversation

granthamtaylor
Copy link
Contributor

@granthamtaylor granthamtaylor commented Oct 21, 2024

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 21, 2024

Codecov Report

Attention: Patch coverage is 45.16129% with 34 lines in your changes missing coverage. Please review.

Project coverage is 45.55%. Comparing base (194300f) to head (6511968).
Report is 10 commits behind head on master.

Files with missing lines Patch % Lines
flytekit/remote/remote.py 38.09% 13 Missing ⚠️
flytekit/types/directory/types.py 30.00% 7 Missing ⚠️
flytekit/types/file/file.py 28.57% 5 Missing ⚠️
flytekit/tools/fast_registration.py 0.00% 4 Missing ⚠️
flytekit/core/context_manager.py 83.33% 0 Missing and 2 partials ⚠️
flytekit/types/structured/structured_dataset.py 0.00% 2 Missing ⚠️
flytekit/core/base_task.py 50.00% 1 Missing ⚠️

❗ There is a different number of reports uploaded between BASE (194300f) and HEAD (6511968). Click for more details.

HEAD has 4 uploads less than BASE
Flag BASE (194300f) HEAD (6511968)
5 1
Additional details and impacted files
@@             Coverage Diff             @@
##           master    #2842       +/-   ##
===========================================
- Coverage   95.61%   45.55%   -50.07%     
===========================================
  Files          31      196      +165     
  Lines        1094    20384    +19290     
  Branches        0     2633     +2633     
===========================================
+ Hits         1046     9285     +8239     
- Misses         48    10640    +10592     
- Partials        0      459      +459     

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

granthamtaylor and others added 7 commits October 21, 2024 15:05
* Catch mistake in structured dataset (#2834)

Signed-off-by: Yee Hing Tong <[email protected]>

* Small change to clean up unit test. (#2835)

* delete=true

Signed-off-by: Yee Hing Tong <[email protected]>

* close but don't delete on close

Signed-off-by: Yee Hing Tong <[email protected]>

* skip for 3.9

Signed-off-by: Yee Hing Tong <[email protected]>

* 10

Signed-off-by: Yee Hing Tong <[email protected]>

---------

Signed-off-by: Yee Hing Tong <[email protected]>

* Fix tree printing (#2837)

Signed-off-by: Yee Hing Tong <[email protected]>

* handle case where error may not have args (#2831)

Signed-off-by: Blake Jackson <[email protected]>
Co-authored-by: Blake Jackson <[email protected]>

* Bump pyspark from 3.3.1 to 3.3.2 in /plugins/flytekit-greatexpectations (#2818)

Bumps [pyspark](https://github.com/apache/spark) from 3.3.1 to 3.3.2.
- [Commits](apache/spark@v3.3.1...v3.3.2)

---
updated-dependencies:
- dependency-name: pyspark
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Pull secrets from environment when running locally (#2800)

Signed-off-by: Thomas J. Fan <[email protected]>

* Support executing launchplans from CLI (#2839)

Signed-off-by: Ketan Umare <[email protected]>
Co-authored-by: Ketan Umare <[email protected]>

* Add top-level access to FlyteRemote, FlyteFile, and FlyteDirectory and convenience class methods for FlyteRemote (#2836)

---------

Signed-off-by: Yee Hing Tong <[email protected]>
Signed-off-by: Blake Jackson <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: Thomas J. Fan <[email protected]>
Signed-off-by: Ketan Umare <[email protected]>
Co-authored-by: Yee Hing Tong <[email protected]>
Co-authored-by: Blake Jackson <[email protected]>
Co-authored-by: Blake Jackson <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Thomas J. Fan <[email protected]>
Co-authored-by: Ketan Umare <[email protected]>
Co-authored-by: Ketan Umare <[email protected]>
* Catch mistake in structured dataset (#2834)

Signed-off-by: Yee Hing Tong <[email protected]>

* Small change to clean up unit test. (#2835)

* delete=true

Signed-off-by: Yee Hing Tong <[email protected]>

* close but don't delete on close

Signed-off-by: Yee Hing Tong <[email protected]>

* skip for 3.9

Signed-off-by: Yee Hing Tong <[email protected]>

* 10

Signed-off-by: Yee Hing Tong <[email protected]>

---------

Signed-off-by: Yee Hing Tong <[email protected]>

* Fix tree printing (#2837)

Signed-off-by: Yee Hing Tong <[email protected]>

* handle case where error may not have args (#2831)

Signed-off-by: Blake Jackson <[email protected]>
Co-authored-by: Blake Jackson <[email protected]>

* Bump pyspark from 3.3.1 to 3.3.2 in /plugins/flytekit-greatexpectations (#2818)

Bumps [pyspark](https://github.com/apache/spark) from 3.3.1 to 3.3.2.
- [Commits](apache/spark@v3.3.1...v3.3.2)

---
updated-dependencies:
- dependency-name: pyspark
  dependency-type: direct:production
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Pull secrets from environment when running locally (#2800)

Signed-off-by: Thomas J. Fan <[email protected]>

* Support executing launchplans from CLI (#2839)

Signed-off-by: Ketan Umare <[email protected]>
Co-authored-by: Ketan Umare <[email protected]>

* Add top-level access to FlyteRemote, FlyteFile, and FlyteDirectory and convenience class methods for FlyteRemote (#2836)

* Config for_endpoint doesn't respect config file (#2843)

Signed-off-by: Yee Hing Tong <[email protected]>

* Union/enum handling (#2845)

* temp

Signed-off-by: Yee Hing Tong <[email protected]>

* delete test

Signed-off-by: Yee Hing Tong <[email protected]>

* allow strings

Signed-off-by: Yee Hing Tong <[email protected]>

* remove enum transformer change and tests

Signed-off-by: Yee Hing Tong <[email protected]>

* reset a test

Signed-off-by: Yee Hing Tong <[email protected]>

* lint

Signed-off-by: Yee Hing Tong <[email protected]>

* use .value for enums to handle dynamic case

Signed-off-by: Yee Hing Tong <[email protected]>

* use prior logic for 3.9

Signed-off-by: Yee Hing Tong <[email protected]>

* add 3.9 testing

Signed-off-by: Yee Hing Tong <[email protected]>

* should always be a union type

Signed-off-by: Yee Hing Tong <[email protected]>

---------

Signed-off-by: Yee Hing Tong <[email protected]>

* update docs for FlyteRemote (#2847)

---------

Signed-off-by: Yee Hing Tong <[email protected]>
Signed-off-by: Blake Jackson <[email protected]>
Signed-off-by: dependabot[bot] <[email protected]>
Signed-off-by: Thomas J. Fan <[email protected]>
Signed-off-by: Ketan Umare <[email protected]>
Co-authored-by: Yee Hing Tong <[email protected]>
Co-authored-by: Blake Jackson <[email protected]>
Co-authored-by: Blake Jackson <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Thomas J. Fan <[email protected]>
Co-authored-by: Ketan Umare <[email protected]>
Co-authored-by: Ketan Umare <[email protected]>
@granthamtaylor granthamtaylor deleted the grantham/new-methods-flytefile-and-flytedirectory 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.

1 participant