Exporting Object Memory

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Exporting Object Memory

Chris Cunnington-5

If I had data in object memory, because I've saved answers from a user into
objects, then can I file out that information? If I had to send it to
somebody as an csv file, how could I do that easily?

I know if I could find an object with the inspector I could print data in
the Transcript and cut and paste it into another file, but is there another
way?

It seems to me that filing out gives you the application, but what if I
wanted information that had been entered by the users, the end user data?

Chris
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Exporting Object Memory

Herbert König
Hello Chris,


CC> If I had data in object memory, because I've saved answers from a user into
CC> objects, then can I file out that information?

if you mean file out as a counterpart of file in, you just use
ReferenceStreams. I use the two attached methods to file out a
dictionary but it works with any object.

CC> If I had to send it to
CC> somebody as an csv file, how could I do that easily?

Don't know of a universal method to do this and wrote one myself for
some special objects of fixed structure. Not smalltalkish.

CC> It seems to me that filing out gives you the application, but what if I
CC> wanted information that had been entered by the users, the end user data?

Other places to search are ImageSegment and SmartRefStream. Be aware
that there are problems if your objects contain blocks.

Cheers,

Herbert                            mailto:[hidden email]
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

Kommentarbersetzer class-writeKurzformbersetzer.st (588 bytes) Download Attachment
Kommentarbersetzer class-readKurzformbersetzer.st (588 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Exporting Object Memory

cdavidshaffer
In reply to this post by Chris Cunnington-5
Chris Cunnington wrote:

> If I had data in object memory, because I've saved answers from a user into
> objects, then can I file out that information? If I had to send it to
> somebody as an csv file, how could I do that easily?
>
> I know if I could find an object with the inspector I could print data in
> the Transcript and cut and paste it into another file, but is there another
> way?
>
> It seems to me that filing out gives you the application, but what if I
> wanted information that had been entered by the users, the end user data?
>
> Chris
>
>  
The end-user data is presumable stored in your application's object
model.  Normally for me that is some kind of "database" class.  Let's
call it "Database" and assume it understands #default (which returns the
only Database instance).

There are lots of ways to answer your question so let me mention a
couple in increasing order of work involved:

    You could export your data as XML using SIXX (load from SqueakMap).
    You could export your data in binary format using ImageSegments
    You could write your data to CSV

Using SIXX you would just do this:

    s := SixxWriteStream newFileNamed: 'myData.sixx'.
    s nextPut: Database default.
    s close.

That's it.  You can read your data back in using a SixxReadStream.

Now, while SIXX is the easiest that I can think of you mentioned CSV so
I'll assume you want it in that format for some reason.  In any case, if
XML is good enough just write your data to a file with:

Let's further suppose that the database instance understands #data which
answers a collection of data items that all understand #printCSVOn:.  
Then I'd write that database to a file with:

fileStream := FileDirectory current newFileNamed: 'someFile.csv'.
[Database default data do: [:dataItem |
    dataItem printCSVOn: fileStream.
    fileStream cr]]
        ensure: [fileStream close].

Notice that I make sure that the fileStream get's closed even if there
is an error during the writing.  Now, of course, the real work is in
printCSVOn: which obviously depends on your data.  Suppose you have an
Account class which has i-vars for accountNumber, ownerName,
ownerPhoneNumber.  You could write them out with:

Account>>printCSVOn: aStream
    aStream nextPutAll: '"' , accountNumber , '",'.
    aStream nextPutAll: '"' , ownerName ,'",'.
    aStream nextPutAll: '"' , ownerPhoneNumber ,'"'.


Of course CSV will probably be useless if your database has more than
one type of thing in it (each line will have different columns).  You
could modify this example so that each different kind of thing stored in
your database is written to a different file.

HTH,

David

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Exporting Object Memory

Chris Cunnington-5
Hi David and Herbert,

Thank you very much for your answers. They are excellent, and there's lots
for me to explore and dig into. As often happens these days, as soon as I've
asked a question, I realize I'm looking at things in a strange way for a
Smalltalker. It's something about the Smalltalk learning curve, I think.
I've spent, oh, a year punching away at this stuff. Now I ask questions  and
-- pop -- all that past effort wasn't so futile. What I mean is, I wrote
that question, and went to Starbucks and had a coffee. When I come back,
read your emails, I realized that my question was oblique. The real areas I
need to explore now are Collections and Streams. Daw. But see, I seem to be
in a strange space, where it only takes a tincture for all my supposedly
past futile efforts to chemically change, and I can see to the bottom of the
pot. It's super weird. But after so much frustration, I don't mind one bit.

Thanks for your help,

Chris
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Exporting Object Memory

Edgar J. De Cleene
In reply to this post by Chris Cunnington-5



El 12/17/07 12:57 PM, "Chris Cunnington" <[hidden email]> escribió:

> If I had data in object memory, because I've saved answers from a user into
> objects, then can I file out that information? If I had to send it to
> somebody as an csv file, how could I do that easily?

To Herbert and David advice I add:

You could export any object as

anObject saveOnFile (you could inspect any object and do self saveOnFile
from inside Inspector)

And read again

| inputStream anObject |
inputStream _ FileStream oldFileNamed: 'Preferences.obj'. (I use this for
have Peferences in MinimalMorphic "
anObject _ inputStream fileInObjectAndCode.
inputStream close.

But for this, the original .image and the target .image should be
compatible, for complicated objects like Morphs.

You can't save from 3.10 and read back on 3.9 , as example.

You should play safe with same version Squeak.

Edgar


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Exporting Object Memory

David T. Lewis
In reply to this post by Chris Cunnington-5
On Mon, Dec 17, 2007 at 10:57:05AM -0500, Chris Cunnington wrote:

>
> If I had data in object memory, because I've saved answers from a user into
> objects, then can I file out that information? If I had to send it to
> somebody as an csv file, how could I do that easily?
>
> I know if I could find an object with the inspector I could print data in
> the Transcript and cut and paste it into another file, but is there another
> way?
>
> It seems to me that filing out gives you the application, but what if I
> wanted information that had been entered by the users, the end user data?

Chris,

Take a look at class Object, and the methods in method category "printing".
In particular, look at the methods #printOn:, #printString, #storeOn:, and
#storeString. These are the basic methods used for converting objects
into human-readable or computer-readable strings (or for writing them
onto streams, such as file streams, in the case of #printOn: and #storeOn:).
They are overridden by many classes in the image, so the methods that
you see in class Object are the defaults.

One simple and useful way to convert objects into CSV is just to make
up your own methods called #printCsvOn: and #printCsvString (or something
like that). Write the code to spit out the fields in your objects with
commas in between, and viola you have a CSV file writer. This is fairly
easy to do, and it makes a handy way to dump your data to a file that
can be imported by a spreadsheet. If your data might include comma characters,
you can try writing it out in tab-separated format (i.e. #printTsvOn:)
instead of comma-separated fields.

It's not fancy but it works fine.

Dave

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners