Re: [julia-users] Libz with an unknown file

2015-12-06 Thread Erik Schnetter
This form

```
contents = open(fn, "r") do io
do_sth(io)
end
```

returns the result of `do_sth`; the close is implicit.

Otherwise, you'll have to introduce a local variable, as in

```
try
io = open(fn)
res = do_sth(io)
close(io)
res
end

-erik


On Sun, Dec 6, 2015 at 9:30 PM, Seth  wrote:

> This gets more complicated because the return value from the try and catch
> blocks will be the close() statement, when I really want the output from
> do_sth. How do you work around that?
>
> On Sunday, December 6, 2015 at 6:02:21 PM UTC-8, Erik Schnetter wrote:
>>
>> I would separate the two independent access methods:
>> ```
>> try
>> io = zlibopen
>> do_sth(io)
>> close(io)
>> catch
>> io = open
>> do_sth(io)
>> close(io)
>> end
>> ```
>>
>> If you want another try block to ensure the file is closed, I'd open
>> secondary try blocks within this one. I think you're trying to do two
>> things at once -- see whether zlib is needed, and also check for I/O errors.
>>
>> Incidentally, you don't need a `finally` clause for the regular `open`
>> function; you can instead write
>>
>> ```
>> contents = open(fn, "r") do io
>> do_sth(io)
>> end
>> ```
>>
>> which ensures that `io` will be closed no matter what.
>>
>> -erik
>>
>>
>> On Sun, Dec 6, 2015 at 8:16 PM, Seth  wrote:
>>
>>> Hi,
>>>
>>> I'm moving from Gzip.jl to Libz.jl, and am running into a problem. Gzip
>>> used to allow a file to be opened using its methods even if the file was
>>> not encrypted. Libz doesn't allow that.
>>>
>>> The problem I'm having is that I can't figure out a try/catch/finally
>>> that works. Basically, I want this (pseudocode):
>>>
>>> io = ZlibInflateInputStream(open(fn,"r"))   # this will succeed if fn
>>> exists, even if fn isn't compressed
>>> contents = try
>>>do_something_with_io(io)   # this will error if fn isn't compressed
>>> catch
>>>   io = open(fn,"r")   # so we try to open it as an uncompressed file
>>>   do_something_with_io(io)
>>> finally
>>>   close(io)
>>> end
>>>
>>> but this doesn't work (the finally statement fails, for one).
>>>
>>> What's the accepted way of doing this?
>>>
>>
>>
>>
>> --
>> Erik Schnetter 
>> http://www.perimeterinstitute.ca/personal/eschnetter/
>>
>


-- 
Erik Schnetter 
http://www.perimeterinstitute.ca/personal/eschnetter/


[julia-users] Libz with an unknown file

2015-12-06 Thread Seth
Hi,

I'm moving from Gzip.jl to Libz.jl, and am running into a problem. Gzip 
used to allow a file to be opened using its methods even if the file was 
not encrypted. Libz doesn't allow that.

The problem I'm having is that I can't figure out a try/catch/finally that 
works. Basically, I want this (pseudocode):

io = ZlibInflateInputStream(open(fn,"r"))   # this will succeed if fn 
exists, even if fn isn't compressed
contents = try
   do_something_with_io(io)   # this will error if fn isn't compressed
catch
  io = open(fn,"r")   # so we try to open it as an uncompressed file
  do_something_with_io(io)
finally
  close(io)
end

but this doesn't work (the finally statement fails, for one).

What's the accepted way of doing this?


Re: [julia-users] Libz with an unknown file

2015-12-06 Thread Erik Schnetter
I would separate the two independent access methods:
```
try
io = zlibopen
do_sth(io)
close(io)
catch
io = open
do_sth(io)
close(io)
end
```

If you want another try block to ensure the file is closed, I'd open
secondary try blocks within this one. I think you're trying to do two
things at once -- see whether zlib is needed, and also check for I/O errors.

Incidentally, you don't need a `finally` clause for the regular `open`
function; you can instead write

```
contents = open(fn, "r") do io
do_sth(io)
end
```

which ensures that `io` will be closed no matter what.

-erik


On Sun, Dec 6, 2015 at 8:16 PM, Seth  wrote:

> Hi,
>
> I'm moving from Gzip.jl to Libz.jl, and am running into a problem. Gzip
> used to allow a file to be opened using its methods even if the file was
> not encrypted. Libz doesn't allow that.
>
> The problem I'm having is that I can't figure out a try/catch/finally that
> works. Basically, I want this (pseudocode):
>
> io = ZlibInflateInputStream(open(fn,"r"))   # this will succeed if fn
> exists, even if fn isn't compressed
> contents = try
>do_something_with_io(io)   # this will error if fn isn't compressed
> catch
>   io = open(fn,"r")   # so we try to open it as an uncompressed file
>   do_something_with_io(io)
> finally
>   close(io)
> end
>
> but this doesn't work (the finally statement fails, for one).
>
> What's the accepted way of doing this?
>



-- 
Erik Schnetter 
http://www.perimeterinstitute.ca/personal/eschnetter/


Re: [julia-users] Libz with an unknown file

2015-12-06 Thread Seth
This gets more complicated because the return value from the try and catch 
blocks will be the close() statement, when I really want the output from 
do_sth. How do you work around that?

On Sunday, December 6, 2015 at 6:02:21 PM UTC-8, Erik Schnetter wrote:
>
> I would separate the two independent access methods:
> ```
> try
> io = zlibopen
> do_sth(io)
> close(io)
> catch
> io = open
> do_sth(io)
> close(io)
> end
> ```
>
> If you want another try block to ensure the file is closed, I'd open 
> secondary try blocks within this one. I think you're trying to do two 
> things at once -- see whether zlib is needed, and also check for I/O errors.
>
> Incidentally, you don't need a `finally` clause for the regular `open` 
> function; you can instead write
>
> ```
> contents = open(fn, "r") do io
> do_sth(io)
> end
> ```
>
> which ensures that `io` will be closed no matter what.
>
> -erik
>
>
> On Sun, Dec 6, 2015 at 8:16 PM, Seth  > wrote:
>
>> Hi,
>>
>> I'm moving from Gzip.jl to Libz.jl, and am running into a problem. Gzip 
>> used to allow a file to be opened using its methods even if the file was 
>> not encrypted. Libz doesn't allow that.
>>
>> The problem I'm having is that I can't figure out a try/catch/finally 
>> that works. Basically, I want this (pseudocode):
>>
>> io = ZlibInflateInputStream(open(fn,"r"))   # this will succeed if fn 
>> exists, even if fn isn't compressed
>> contents = try
>>do_something_with_io(io)   # this will error if fn isn't compressed
>> catch
>>   io = open(fn,"r")   # so we try to open it as an uncompressed file
>>   do_something_with_io(io)
>> finally
>>   close(io)
>> end
>>
>> but this doesn't work (the finally statement fails, for one).
>>
>> What's the accepted way of doing this?
>>
>
>
>
> -- 
> Erik Schnetter  
> http://www.perimeterinstitute.ca/personal/eschnetter/
>