Skip to content

Add option for exclusive creation to write#77

Merged
bastibe merged 1 commit intomasterfrom
issue-77
Jan 8, 2015
Merged

Add option for exclusive creation to write#77
bastibe merged 1 commit intomasterfrom
issue-77

Conversation

@mgeier
Copy link
Contributor

@mgeier mgeier commented Nov 27, 2014

Write currently always opens the file as mode='w'. mode='x' would be interesting as well.

@mgeier
Copy link
Contributor

mgeier commented Oct 14, 2014

I also thought of this and I like the idea, but I fear it has some subtleties.

I would very much prefer the default to be overwrite=False, but the problem is that this isn't possible in Python 2.

The default value overwrite=True is possible in all versions but it's kind of meaningless to have a feature for safety, that has to be switched on explicitly. Where's the safety in this?

Another possibility would be to have opposite default values depending on the Python version, which sounds quite dubious to me (but may be the way to go?).

I feel this is a bit of a conundrum ...

Or am I missing something?

@bastibe
Copy link
Owner Author

bastibe commented Oct 16, 2014

Since write is a non-standard function, we can choose our own API. I like the idea of having an overwrite=False option, which does the right thing.

@mgeier mgeier modified the milestone: 0.6.0 Oct 26, 2014
@mgeier
Copy link
Contributor

mgeier commented Nov 16, 2014

But how should we handle the discrepancy between Python 2 and 3 behavior?

@bastibe
Copy link
Owner Author

bastibe commented Nov 17, 2014

We could explicitly check if the file exists and throw an error if it does in Python 2.

@mgeier
Copy link
Contributor

mgeier commented Nov 22, 2014

I just realized that there might be a conceptual problem with calling the option overwrite, because users could think that an existing file is opened in 'r+' mode and parts of it will be overwritten by sf.write(..., overwrite=True).
This will get even more confusing if we will ever add an overwrite() function as proposed in #86.

How else could we call this argument?

The explanation for 'x' in the Python docs is "open for exclusive creation, failing if the file already exists".
Some brainstorming: exclusive=True, exclusive_creation=True, fail_on_existing=True, truncate=False, replace=False ...
It wouldn't matter if the argument name is verbose, because it will be typed seldom and its main purpose would be to explicitly describe the behavior of write().

Another possibility would be to add a mode='x' argument, but that would also be strange, because only 2 modes would be allowed (the other one being mode='w') and the optional 'b' would make things even more complicated.

Or should we call it overwrite=False anyway and agree to never add an overwrite() function?

@bastibe
Copy link
Owner Author

bastibe commented Nov 24, 2014

Intuitively, I'd say exclusive_creation. It is a bit verbose, but at least it's unambiguous.

@bastibe bastibe modified the milestones: 0.6.x, 0.6.0 Nov 24, 2014
@mgeier
Copy link
Contributor

mgeier commented Nov 27, 2014

#94 will allow mode='x' for all Python versions.

When specifying exclusive_creation=False, mode='w' is used.

See #77.
@mgeier mgeier modified the milestones: 0.6.0, 0.6.x Nov 27, 2014
bastibe added a commit that referenced this pull request Jan 8, 2015
Add option for exclusive creation to `write`
@bastibe bastibe merged commit e2d61a6 into master Jan 8, 2015
@mgeier mgeier deleted the issue-77 branch January 9, 2015 09:49
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.

2 participants