Re: [julia-users] string version of expressions not parseable

2016-04-02 Thread vishesh
Ah, thank you for that example.


On Saturday, April 2, 2016 at 11:02:07 AM UTC-7, Yichao Yu wrote:
>
> On Sat, Apr 2, 2016 at 1:02 PM,   
> wrote: 
> > I seem to be unable to get this to work 
> > 
> >>serialize(STDOUT, :(x + 1)) 
> > =b+1 
> > 
> >>deserialize(IOBuffer("=b+1")) 
> > :call 
> > 
> > which is only form 1 out of 4. 
>
> deserializing and serializing do no use a plain text format. 
>
> julia> io = IOBuffer() 
> IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, 
> append=false, size=0, maxsize=Inf, ptr=1, mark=-1) 
>
> julia> serialize(io, :(x + 1)) 
>
> julia> seekstart(io) 
> IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true, 
> append=false, size=9, maxsize=Inf, ptr=1, mark=-1) 
>
> julia> deserialize(io) 
> :(x + 1) 
>
>
> > 
> > Also, could you give me an example of a situation where the aliasing is 
> an 
> > issue? I'm unclear when that pops up. 
>


Re: [julia-users] string version of expressions not parseable

2016-04-02 Thread Yichao Yu
On Sat, Apr 2, 2016 at 1:02 PM,   wrote:
> I seem to be unable to get this to work
>
>>serialize(STDOUT, :(x + 1))
> =b+1
>
>>deserialize(IOBuffer("=b+1"))
> :call
>
> which is only form 1 out of 4.

deserializing and serializing do no use a plain text format.

julia> io = IOBuffer()
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true,
append=false, size=0, maxsize=Inf, ptr=1, mark=-1)

julia> serialize(io, :(x + 1))

julia> seekstart(io)
IOBuffer(data=UInt8[...], readable=true, writable=true, seekable=true,
append=false, size=9, maxsize=Inf, ptr=1, mark=-1)

julia> deserialize(io)
:(x + 1)


>
> Also, could you give me an example of a situation where the aliasing is an
> issue? I'm unclear when that pops up.


Re: [julia-users] string version of expressions not parseable

2016-04-02 Thread vishesh
I seem to be unable to get this to work

>serialize(STDOUT, :(x + 1))
=b+1

>deserialize(IOBuffer("=b+1"))
:call

which is only form 1 out of 4.

Also, could you give me an example of a situation where the aliasing is an 
issue? I'm unclear when that pops up.


Re: [julia-users] string version of expressions not parseable

2016-04-01 Thread Erik Schnetter
Vishesh

I would use `serialize` and `deserialize` to ensure that you get
exactly the same expression back that you wrote out initially. There
are several caveats that would be very difficult to get correct in a
string representation, e.g. if objects are aliased (if the same object
appears multiple times in the expression).

-erik


On Fri, Apr 1, 2016 at 7:36 PM,   wrote:
> basically, if I do:
>
>> e = Expr(:quote, :(:x)) # (quoting a symbol)
>> show(e)
>
> :($(Expr(:quote, :(:x
>
>> print(e)
>
> $(Expr(:quote, :(:x)))
>
>> string(e)
>
> "\$(Expr(:quote, :(:x)))"
>
>> parse(string(e))
>
> :($(Expr(:$, :(Expr(:quote,$(Expr(:quote, :(:x
>
>
> that's not the same expression as represented by the string. Then when I go
> to eval it (which should still work, semantically speaking)
>
> ERROR: unsupported or misplaced expression $
>
>
> Is there a way around this? I'd like to make an expression a string, print
> it to a file, then slurp it up later and eval it.
>
> At the very least, there should be a consistent way to stringify and
> reparse/eval expression objects, even if they look ugly like
> :($(Expr(:quote, :(:x. I'd hate to have to reimplement the whole
> printing of every expression ever in order to make this work (there doesn't
> seem to be a way to extend a method, like print or show, to only work on
> expressions with a particular head?)
>
>
>
> Vishesh



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


[julia-users] string version of expressions not parseable

2016-04-01 Thread vishesh
basically, if I do:

> e = Expr(:quote, :(:x)) # (quoting a symbol)
> show(e)

*:($(Expr(:quote, :(:x*

> print(e)

*$(Expr(:quote, :(:x)))*

> string(e)

*"\$(Expr(:quote, :(:x)))"*

> parse(string(e))

*:($(Expr(:$, :(Expr(:quote,$(Expr(:quote, :(:x*


that's not the same expression as represented by the string. Then when I go 
to eval it (which should still work, semantically speaking)

*ERROR: unsupported or misplaced expression $*


Is there a way around this? I'd like to make an expression a string, print 
it to a file, then slurp it up later and eval it.

At the very least, there should be a consistent way to stringify and 
reparse/eval expression objects, even if they look ugly like 
:($(Expr(:quote, :(:x. I'd hate to have to reimplement the whole 
printing of every expression ever in order to make this work (there doesn't 
seem to be a way to extend a method, like print or show, to only work on 
expressions with a particular head?)



Vishesh