Alice Community

Alice Community (http://www.alice.org/community/index.php)
-   Share Objects (http://www.alice.org/community/forumdisplay.php?f=8)
-   -   File I/O Tool (http://www.alice.org/community/showthread.php?t=6758)

arty-fishL 06-14-2011 05:15 PM

File I/O Tool
 
1 Attachment(s)
This is a neat file I/O tool I made for [URL="http://www.alice.org/community/member.php?u=18410"]TauTrumpsPi[/URL].
Import the object into the world you want to use it in.
Don't rename it and don't delete any of its variables, methods or events.

Set IOTool's variable named current_file to the file to be used. Change it when necessary.
Use any of its methods wherever required.
Some methods have basic help/info in comments inside them.
Any method that returns a value (eg read_all or get_lines) will write its value to IOTool's variable named returnString (or returnNumber for get_lines).

Update 1.1 - should work perfectly now.
Update 1.2 - now it actually definitely works; trialled and tested.
[B]
New Version Available - [URL="http://alice.org/community/showthread.php?p=45897"]Save Tool[/URL] - for specifically saving and loading variables[/B]



[B]Read below only if you require help:[/B]

[SIZE=1][I][B]get_value[/B][/I] - returns to [U][I]returnString[/I][/U] - parameter [U][I]value[/I][/U] is the name of the value key - returns info about the file (eg size).
[I][B]new_file[/B][/I] - returns [U]none[/U] - parameter [U][I]filename[/I][/U] is the complete folder, file,ext name (ie C:\Users\Me\Documents\blank.txt).
[I][B]new_folder[/B][/I] - returns [U]none[/U] - parameter [U][I]dirname[/I][/U] is the complete pathname of the directory (ie C:\Users\Me\Documents\folder).
[B]read_all[/B] - returns to [U][I]returnString[/I][/U] - [U]no[/U] parameters - reads the entire file.
[B]read_line[/B] - returns to [U][I]returnString[/I][/U] -parameter line is the line to read - reads only a given line from the file.
[B]get_lines[/B] - returns to [U][I]returnNumber[/I][/U] -[U]no[/U] parameters - returns the number of lines of text in the file.
[B]write[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with - writes over everything in the file.
[B]write_line[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with, [U][I]line[/I][/U] is the line to write over - writes over everything on only the specified line in the file.
[B]write_append[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with - writes onto the end of the file, retaining any text already in it.
[B]delete_file[/B] - returns [U]none[/U] - [U]no[/U] parameters - completely removes the file from disc.
[B]replace_file[/B] - returns [U]none[/U] - parameter [U][I]file_to_overwrite_with[/I][/U] is the complete filename of the file to write with - replaces the current file with the contents of another file, replaces in binary, so can replace with images etc.
[B]rename_file[/B] - returns [U]none[/U] - parameter [U][I]name[/I][/U] is the new name to give the file, set [U][I]full_path[/I][/U] to true to change complete filename; or to false just to change name relative to the director[/SIZE]y it is in - renames the file.

[SIZE=2][B]Support me by using [URL="http://adf.ly/8J02s"][B][SIZE=2]this DOWNLOAD link[/SIZE][/B][/URL][/B] (AdF.ly)[/SIZE]

David B 06-14-2011 05:44 PM

[QUOTE=arty-fishL;37453]This is a neat file I/O tool I made for [URL="http://www.alice.org/community/member.php?u=18410"]TauTrumpsPi[/URL].
Import the object into the world you want to use it in.
Don't rename it and don't delete any of its variables, methods or events.

Set IOTool's variable named current_file to the file to be used. Change it when necessary.
Use any of its methods wherever required.
Some methods have basic help/info in comments inside them.
Any method that returns a value (eg read_all or get_lines) will write its value to IOTool's variable named returnString (or returnNumber for get_lines).





[B]Read below only if you require help:[/B]

[SIZE=1][I][B]get_value[/B][/I] - returns to [U][I]returnString[/I][/U] - parameter [U][I]value[/I][/U] is the name of the value key - returns info about the file (eg size).
[I][B]new_file[/B][/I] - returns [U]none[/U] - parameter [U][I]filename[/I][/U] is the complete folder, file,ext name (ie C:\Users\Me\Documents\blank.txt).
[I][B]new_folder[/B][/I] - returns [U]none[/U] - parameter [U][I]dirname[/I][/U] is the complete pathname of the directory (ie C:\Users\Me\Documents\folder).
[B]read_all[/B] - returns to [U][I]returnString[/I][/U] - [U]no[/U] parameters - reads the entire file.
[B]read_line[/B] - returns to [U][I]returnString[/I][/U] -parameter line is the line to read - reads only a given line from the file.
[B]get_lines[/B] - returns to [U][I]returnNumber[/I][/U] -[U]no[/U] parameters - returns the number of lines of text in the file.
[B]write[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with - writes over everything in the file.
[B]write_line[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with, [U][I]line[/I][/U] is the line to write over - writes over everything on only the specified line in the file.
[B]write_append[/B] - returns [U]none[/U] - parameter [U][I]text[/I][/U] is the string to write with - writes onto the end of the file, retaining any text already in it.
[B]delete_file[/B] - returns [U]none[/U] - [U]no[/U] parameters - completely removes the file from disc.
[B]replace_file[/B] - returns [U]none[/U] - parameter [U][I]file_to_overwrite_with[/I][/U] is the complete filename of the file to write with - replaces the current file with the contents of another file, replaces in binary, so can replace with images etc.
[B]rename_file[/B] - returns [U]none[/U] - parameter [U][I]name[/I][/U] is the new name to give the file, set [U][I]full_path[/I][/U] to true to change complete filename; or to false just to change name relative to the director[/SIZE]y it is in - renames the file.[/QUOTE]

Cool! It looks interesting!

TauTrumpsPi 06-14-2011 06:46 PM

I can't get it to work... It seems like you're missing an integer variable of some sort

Here's the error

[code]
Error during simulation.




Alice version: 2.2 02/10/2011



Throwable that caused the error:

Traceback (innermost last):

File "edu.cmu.cs.stage3.alice.core.property.ScriptProperty[name=script,owner=edu.cmu.cs.stage3.alice.core.response.ScriptResponse[world.iOTool.read_all.__Unnamed0__]]", line 1, in ?

NameError: fileinfo


[/code]

DensetsuNoKaboom 06-15-2011 06:29 AM

My head is spinning, partly because Arty just made a new manly batch of awesome, but also partly because I don't know what to do with it.

Can you write new .txt files with it?

arty-fishL 06-15-2011 11:09 AM

[QUOTE=TauTrumpsPi;37467]I can't get it to work... It seems like you're missing an integer variable of some sort

Here's the error
[/QUOTE]Give it a second to start before you use it, don't use it on world start.
If that doesn't work, do you have an [I]"on world start" [/I]event under the IOTool heading? If you don't then there should be one.

[QUOTE=DensetsuNoKaboom;37494]Can you write new .txt files with it?[/QUOTE]Yes, and read them. (they don't have to end in .txt, but they should be plain text files ie. not images etc.)

TauTrumpsPi 06-15-2011 12:10 PM

I got it! Thank you!

Chao 06-15-2011 12:12 PM

Thanks! This will be very useful!

DensetsuNoKaboom 06-15-2011 05:03 PM

[quote=arty-fishl;37504]
yes, and read them. (they don't have to end in .txt, but they should be plain text files ie. Not images etc.)[/quote]
Hmmm...
...
...
...
...
...
...
...
...
... :D:D:D:D:D

EDIT: It's not working. It creates new files all right but when I try to make it write a string variable to line 1 of the created file, it gives me an error.
[quote]Error during simulation.




Alice version: 2.2 02/10/2011



Throwable that caused the error:

Traceback (innermost last):

File "edu.cmu.cs.stage3.alice.core.property.ScriptProperty[name=script,owner=edu.cmu.cs.stage3.alice.core.response.ScriptResponse[world.iOTool.write_line.__Unnamed0__]]", line 1, in ?

File "<string>", line 7, in __init__

AttributeError: class 'org.python.modules.os' has no attribute 'path'


[/quote]

Mr Kidnapper 06-15-2011 05:33 PM

Mr. Kidnapper does not understand the uses of this. Is it supposed to assist in creating save/load states?

TauTrumpsPi 06-15-2011 06:06 PM

That is the reason I originally asked for it, yes.

room14 06-16-2011 12:44 AM

Having the same error as densestu.:(
btw, you should add a method that converts strings into numbers:)

DensetsuNoKaboom 06-16-2011 06:20 AM

[QUOTE=room14;37562]Having the same error as densestu.:(
btw, you should add a method that converts strings into numbers:)[/QUOTE]

Yeah, so you can make it read a "number" that is in the file and change it to a number and set a variable to that number, so it wouldn't be just strings you can save!

David B 06-16-2011 08:12 AM

I haven't come up with anything to use it for yet, but it is sounds very cool!

arty-fishL 06-16-2011 01:17 PM

I updated it to a working version.
I'm considering the other suggested features.

DensetsuNoKaboom 06-16-2011 03:05 PM

[QUOTE=arty-fishL;37593]I updated it to a working version.
I'm considering the other suggested features.[/QUOTE]

Really now?

[quote]Error during simulation.




Alice version: 2.2 02/10/2011



Throwable that caused the error:

Traceback (innermost last):

File "edu.cmu.cs.stage3.alice.core.property.ScriptProperty[name=script,owner=edu.cmu.cs.stage3.alice.core.response.ScriptResponse[world.iOTool.event.__Unnamed0__]]", line 1, in ?

File "<string>", line 1, in ?

SyntaxError: ('future feature division is not defined', ('C:\\Documents and Settings\\*\\My Documents\\Alice22\\Required\\jython-2.1\\Lib\\javaos.py', 23, 6, ''))


[/quote]
Future feature division?

EDIT: I think you should also know that this error occurs when I start playing a world with the object in it.

Mr Kidnapper 06-16-2011 03:06 PM

If you want uses, then you could use it to save PoV locations, booleans, and other number variables constantly every second to, for example, make a game a bit like Harvest Moon or other example sim game. In fact, I would applaud you for making a persistent world sim game. Persistent World could be achieved by making the world relative to computer date time, if you didn't know how.

TauTrumpsPi 06-16-2011 04:02 PM

You know, I find it funny that I ask you to do this and then a week later I learn how to do it in C++. Odd coincedence.

arty-fishL 06-16-2011 04:58 PM

I worked out what was wrong, its fixed.
Its updated and definitely works now, I tested it on a non-modified Alice.

DensetsuNoKaboom 06-16-2011 05:59 PM

[QUOTE=arty-fishL;37628]I worked out what was wrong, its fixed.
Its updated and definitely works now, I tested it on a non-modified Alice.[/QUOTE]

[quote]Error during simulation.




Alice version: 2.2 02/10/2011



Throwable that caused the error:

Traceback (innermost last):

File "edu.cmu.cs.stage3.alice.core.property.ScriptProperty[name=script,owner=edu.cmu.cs.stage3.alice.core.response.ScriptResponse[world.iOTool.write_line.__Unnamed0__]]", line 1, in ?

File "<string>", line 153, in writeline

IndexError: index out of range: -1


[/quote]

Bbbbububbuubuubbubut...

At least this time, it didn't happen until I tried to write a variable to line 1.
EDIT: And hey, it works when I overwrite the entire file with text rather than one line! And it works when I load that file!
EDIT2: And this is my 600th post!

arty-fishL 06-16-2011 06:43 PM

No, that's just you using it wrong.

You cant write to line 1, because line 1 doesn't exist.
It is like a list, it starts at line 0.
If you want a new line at any time just add in [I]\n[/I] .
Once the line exists you can write to it.

DensetsuNoKaboom 06-17-2011 06:45 AM

[QUOTE=arty-fishL;37631]
If you want a new line at any time just add in [I]\n[/I] .
[/QUOTE]

Add it in where?

arty-fishL 06-17-2011 08:05 AM

Wherever you're writing just add [I]\n[/I] into the string for a new line. For example:

"hi there \n my name is Bob\nGreat!"

would produce

[CODE]hi there
my name is Bob
Great![/CODE]

Anyway, what you were looking for was writing to line 0, not line 1.

I could maybe update it to change writing line 0 to line 1 instead (so the numbers are shifted down one) and so on. I could also add a function to create a new line or set of lines; one for changing a string to a number; maybe even also a variable saving function.

DensetsuNoKaboom 06-17-2011 08:46 PM

It failed as well when I tried writing to line 0.

arty-fishL 06-18-2011 04:14 AM

[QUOTE=DensetsuNoKaboom;37684]It failed as well when I tried writing to line 0.[/QUOTE]Ensure the file exists first - use new_file with overwrite set to false. If writing to line 0 doesn't work then try using write. It should work, I've tested it lots.

Dj Tech42 06-29-2011 03:06 PM

Is this compatible with Mac OS X? I recognized it started with C:// so I changed it to the Mac filepath and when I ran the world it threw an error:


Error during simulation.


Alice version: 2.2 09/10/2010

Throwable that caused the error:
Traceback (innermost last):
File "edu.cmu.cs.stage3.alice.core.property.ScriptProperty[name=script,owner=edu.cmu.cs.stage3.alice.core.response.ScriptResponse[world.iOTool.event.__Unnamed0__]]", line 1, in ?
File "<string>", line 26, in ?
File "<string>", line 12, in patchOS
IOError: File not found - jython-2.1\Lib\javaos.py (No such file or directory)

arty-fishL 06-29-2011 04:56 PM

[QUOTE=Dj Tech42;38233]Is this compatible with Mac OS X? I recognized it started with C:// so I changed it to the Mac filepath and when I ran the world it threw an error:


Error during simulation.


Alice version: 2.2 09/10/2010

Throwable that caused the error:
Traceback (innermost last):
File "edu.cmu.cs.stage3.alice.core.property.ScriptProperty[name=script,owner=edu.cmu.cs.stage3.alice.core.response.ScriptResponse[world.iOTool.event.__Unnamed0__]]", line 1, in ?
File "<string>", line 26, in ?
File "<string>", line 12, in patchOS
IOError: File not found - jython-2.1\Lib\javaos.py (No such file or directory)[/QUOTE]Ehh, sorry, it seems your version (either your version is broken or Mac version is broken) of Alice has its file directory incorrectly referenced. You could try updating Alice, there's a newer version of 2.2 available, but I'm not sure that will help.

Dj Tech42 06-29-2011 05:20 PM

I already have the latest version. I think it has something to do with the file system being different on Mac OS X. I think this is a very important feature that needs to be implemented into Alice and I am sad that it isn't working :(

Dj Tech42 06-29-2011 05:58 PM

Is there a way I can change how Alice accesses the internal files? It can't find one of its own files, which is really annoying

arty-fishL 06-29-2011 06:14 PM

[QUOTE=Dj Tech42;38237]I already have the latest version. I think it has something to do with the file system being different on Mac OS X. I think this is a very important feature that needs to be implemented into Alice and I am sad that it isn't working :([/QUOTE]Oh, the latest Windows version is a 2011 version, youve got a 2010 (maybe Mac isn't as updated yet).

[QUOTE=Dj Tech42;38238]Is there a way I can change how Alice accesses the internal files? It can't find one of its own files, which is really annoying[/QUOTE]Elaborate on this error, I may be able to help. What file cant it find?

Dj Tech42 06-29-2011 06:46 PM

/Applications/Alice 2.2.app/Contents/Required/jython-2.1/Lib/javaos.py

javaos.py is the file it is looking for, and I know where it is ^. Is there some way I can show Alice where to look for the file?

And the Mac version was released September 2010.

Dj Tech42 07-06-2011 06:45 PM

It's still coming up with the error no matter what I do

arty-fishL 07-09-2011 04:36 AM

1 Attachment(s)
[QUOTE=Dj Tech42;38552]It's still coming up with the error no matter what I do[/QUOTE]Sorry about taking ages to respond. Try this world. Play it in Alice, wait for it to finish, then try restarting Alice.

Dj Tech42 07-09-2011 10:40 AM

It says patch failed :(

arty-fishL 07-10-2011 03:17 PM

1 Attachment(s)
[QUOTE=Dj Tech42;38746]It says patch failed :([/QUOTE]
OK, try the new one, even if it doesn't work, it will print out debug info so I can see what is wrong

Dj Tech42 07-10-2011 07:14 PM

Ok here's what it said:


START PATCH
Resource Transfer patch failed
Debug: File not found - resources\common\ResourceTransfer.py (No such file or directory)
Patch failed
END PATCH

Dj Tech42 07-10-2011 07:16 PM

The said file does exist, but it is under required/resources/common/

arty-fishL 07-21-2011 06:05 PM

[QUOTE=Dj Tech42;38857]The said file does exist, but it is under required/resources/common/[/QUOTE]Sorry I am late again, I've been busy elsewhere - and in thinking about this. I'm at a loss, if it can't even be patched then I don't know what to do.

You haven't by chance stuck Alice in the system programs directory instead of your local home directory have you? Because if you have, then I would try moving it to a folder on your local home directory instead, the error may lay with permissions or similar.

Dj Tech42 07-21-2011 06:17 PM

It's ok, you don't need to worry about it anymore. I can't even run Alice 2.2 on my computer now due to the code in Alice being too old.

Dj Tech42 08-04-2011 09:54 PM

Well, Alice 2.2 is running on my computer again, but still no luck with the plugin. :( I will probably have to try it on a different computer.

Dj Tech42 08-05-2011 07:31 PM

I think I might have fixed it! I enabled scripting on Alice and fixed the javaos.py path reference! I just need to try the actual methods now.

It worked! Thanks so much arty for creating such an awesome tool!


All times are GMT -5. The time now is 02:19 AM.

Copyright ©2020, Carnegie Mellon University
Alice 2.x 1999-2012, Alice 3.x 2008-2012, Carnegie Mellon University. All rights reserved.