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

Have code run conditionally on load() vs. command line #572

Closed
pao opened this issue Mar 12, 2012 · 11 comments
Closed

Have code run conditionally on load() vs. command line #572

pao opened this issue Mar 12, 2012 · 11 comments
Labels
speculative Whether the change will be implemented is speculative

Comments

@pao
Copy link
Member

pao commented Mar 12, 2012

To make interactive testing of Julia scripts easier and improve the ability to reuse components, it would be nice if there were an equivalent to Python's:

if __name__ == "__main__":
    #things

or Ruby's

if $0 == __FILE__
    # things
end
@Keno
Copy link
Member

Keno commented Mar 12, 2012

Could we just have a global variable that is set by the REPL to indicate whether it's the REPL or not?

@pao
Copy link
Member Author

pao commented Mar 12, 2012

The Python and Ruby approaches are variations on that (with __name__ and __FILE__ being the special variables). @raggi is working on something already.

@JeffBezanson
Copy link
Member

One way to tell is if ARGS[1] is the name of the source file.

@pao
Copy link
Member Author

pao commented Mar 13, 2012

That's the Ruby approach, but unless we provide a special variable which returns the name of the file you are in (or if that already exists, in which case this just becomes documentation) you have to hardcode the file name. That would be sufficient to make this not painful.

@StefanKarpinski
Copy link
Member

One way to tell is if ARGS[1] is the name of the source file.

Actually, that's a bug. ARGS should only be the arguments to the program, not the program name. That should be provided via another global variable.

I'm not an expert on the Python thing, but isn't this generally considered to be a misfeature that's just been rampantly abused? I've never really wanted this in Perl or Ruby — a script is a script, a module is a module.

@pao
Copy link
Member Author

pao commented Mar 15, 2012

And interactive debugging from the REPL or a test harness requires that you be able to do both. Let's say I have a script foo.jl with a bar() function--I might like to test bar() independently of any side effects of running other parts of julia foo.jl.

@StefanKarpinski
Copy link
Member

Doesn't that seem to indicate that you should move the definition of bar into a separate file from the script that uses it? I mean, if you have a test suite, I think you're project is big enough to make that separation, and it's kind of good practice, no? I'm not saying there can't be any way to check this, but I'm just unsure that it's good practice. The idea that something is both a module and a script has always struck me as bizarre and kind of wrong.

@pao
Copy link
Member Author

pao commented Mar 15, 2012

I have a script. That script has repeated code which I want to make a function out of, but which is still quite specific to that script. I really don't want to put that in a separate file just so I can test that out from the REPL.

Also "bigness" is a relative term in technical computing. I've written a lot of throwaway MATLAB that I still need to quickly check before using--forcing me into a heavyweight, multiple-file workflow for that isn't going to make me happy.

@StefanKarpinski
Copy link
Member

Ok, fair enough. We can add something like this. Not sure what it should look like. Maybe reflective access to the options that julia was invoked with?

@JeffBezanson
Copy link
Member

I can see the case of wanting to load a script in the repl to test its functions without calling main. So maybe just a way to test if we are in interactive mode, like @loladiro suggested?

@pao
Copy link
Member Author

pao commented May 21, 2012

Sounds good to me.

Keno pushed a commit that referenced this issue Oct 9, 2023
dkarrasch pushed a commit that referenced this issue Nov 4, 2024
8c84b8c (#56431)

Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: release-1.10
Julia branch: backports-release-1.10
Old commit: 279b363
New commit: 8c84b8c
Julia version: 1.10.6
SparseArrays version: 1.10.0(Does not match)
Bump invoked by: @IanButterworth
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaSparse/SparseArrays.jl@279b363...8c84b8c

```
$ git log --oneline 279b363..8c84b8c
8c84b8c Merge pull request #572 from JuliaSparse/backports-release-1.10
ec38631 Update ci.yml with more architectures
46c8f7e Merge branch 'release-1.10' into backports-release-1.10
2d762b3 Manual commit for PR #550 to backport to 1.10 (#577)
5c37298 Add versions to include arch
b539588 Update CI
fa49620 Disable nested dissection
d2a80a6 Change default QR tolerance to match SPQR (#557)
9b8cd14 SparseMatrixCSC constructor with a Tuple of Integers (#523)
546be18 Fix docs conflict when building as part of full Julia docs (#430)
30fbfc6 Test suite: activate a temp project if we need to install Aqua.jl during the test suite (#425)
91b0aa5 doc: move solvers doc to `src\solvers.md` (#576)
5d3724a Inline sparse-times-dense in-place multiplication
```

Co-authored-by: Dilum Aluthge <[email protected]>
N5N3 pushed a commit that referenced this issue Nov 11, 2024
8c84b8c (#56431)

Stdlib: SparseArrays
URL: https://github.com/JuliaSparse/SparseArrays.jl.git
Stdlib branch: release-1.10
Julia branch: backports-release-1.10
Old commit: 279b363
New commit: 8c84b8c
Julia version: 1.10.6
SparseArrays version: 1.10.0(Does not match)
Bump invoked by: @IanButterworth
Powered by:
[BumpStdlibs.jl](https://github.com/JuliaLang/BumpStdlibs.jl)

Diff:
JuliaSparse/SparseArrays.jl@279b363...8c84b8c

```
$ git log --oneline 279b363..8c84b8c
8c84b8c Merge pull request #572 from JuliaSparse/backports-release-1.10
ec38631 Update ci.yml with more architectures
46c8f7e Merge branch 'release-1.10' into backports-release-1.10
2d762b3 Manual commit for PR #550 to backport to 1.10 (#577)
5c37298 Add versions to include arch
b539588 Update CI
fa49620 Disable nested dissection
d2a80a6 Change default QR tolerance to match SPQR (#557)
9b8cd14 SparseMatrixCSC constructor with a Tuple of Integers (#523)
546be18 Fix docs conflict when building as part of full Julia docs (#430)
30fbfc6 Test suite: activate a temp project if we need to install Aqua.jl during the test suite (#425)
91b0aa5 doc: move solvers doc to `src\solvers.md` (#576)
5d3724a Inline sparse-times-dense in-place multiplication
```

Co-authored-by: Dilum Aluthge <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

4 participants