A few more bits about shelve vs json. shelve is actually not
a serialization format specifically, but rather a key-value store that
serializes its values using pickle. It stores like a database and has the
added benefit of letting you modify in-memory, piecemeal over time, until
it gets flushed to disk. Basically it acts just like optionVar, by being
accessible like a dictionary, as opposed to needing to dump your entire
structure to and from disk with each change.
And also, json has a "dump" variant:
with open(fname, 'w') as f:
# writes directly to your file
json.dump(data, f)
If you use Qt at all, there is also a really nice class called
QSettings<http://srinikom.github.io/pyside-docs/PySide/QtCore/QSettings.html>
which
acts similar to the shelve module, but also has the benefit of knowing the
cross-platform location to store user preferences, and is completely safe
for concurrent reads/writes between threads/processes.
On Tue, Feb 4, 2014 at 8:46 PM, Marcus Ottosson <[email protected]>wrote:
> I take it you are looking to store data from code onto disk for retrieval
> later with libraries external to Maya, or even outside of Maya.
>
> This is usually a two-step process:
> 1. Serialization <http://en.wikipedia.org/wiki/Serialization>, which
> means to make a set of data - in this case, internal to Python - writeable
> to disk and retrievable later.
> 2. Writing to disk.
>
> Depending on your needs, you may not need serialization and can simply
> write data structures directly to disk; e.g. if you are looking to store
> text.
>
> >> with open(file, 'w') as f:
> >> f.write(my_text)
> >>
> >> with open(file, 'r') as f:
> >> retrieved_text = f.read()
>
> Serialization comes in many flavours and are relevant if you're looking to
> store more complex types, such as list, dict or arbitrary data structures
> of your own or Python's making.
>
> The shelve <http://docs.python.org/2/library/shelve.html> module as
> suggested above is appropriate if the data you're storing involves any
> objects other than the plain-old-data types, such as string, int, booletc.
>
> If you're needs are simpler, an alternative to shelve is
> json<http://docs.python.org/2/library/json.html>,
> also included in the standard library, but capable of storing only
> plain-old-data.
>
> Both shelve and json come with their own writing mechanism, but in the
> case of json, the resulting serialized data is of type string and can be
> passed around and modified in code before being written out by other means.
>
> >> serialized_data = json.dumps(my_data)
> >> with open(file, 'w') as f:
> >> f.write(serialized_data)
>
> Which brings me to the next important topic of which json and shelve are
> good examples.
>
> One results in human-readable data and the other does not. Generally,
> human-readable data lacks the performance of binary equivalents but make up
> for it in that they can be inspected and edited by humans. It all depends
> on your needs, really.
>
> See here for a summary of what Python has to offer in this aspect
> http://docs.python.org/2/library/persistence.html
>
>
>
> On 4 February 2014 05:48, Chad Dombrova <[email protected]> wrote:
>
>> I *think* that Glom was looking for something similar to optionVars for
>> use outside of Maya, but I could be wrong.
>> —
>> Sent from Mailbox <https://www.dropbox.com/mailbox> for iPhone
>>
>>
>> On Mon, Feb 3, 2014 at 8:43 PM, Count Zer0 <[email protected]> wrote:
>>
>>> PyMel gives you easy access to them too:
>>>
>>>
>>> http://download.autodesk.com/global/docs/maya2013/en_us/PyMel/generated/classes/pymel.core.language/pymel.core.language.OptionVarDict.html
>>>
>>> And you should be using PyMel anyway if you're moving past MEL anyway.
>>>
>>> -jason
>>>
>>> On Monday, February 3, 2014 9:21:44 AM UTC-8, Glom De wrote:
>>>>
>>>> hi guys
>>>> as you know ,maya command "optionVar" can store variables in disk,
>>>> are there any functions or Module of python can do same thing like Maya
>>>> cmd "optionVar"
>>>>
>>>> googled , but did not find any clue。。。。。
>>>> anyone can give me some info about that, thanks a lot!!!
>>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Python Programming for Autodesk Maya" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/python_inside_maya/1cf36bfc-4066-4a6c-acd0-860ecd33b765%40googlegroups.com
>>> .
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Python Programming for Autodesk Maya" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/python_inside_maya/1391492891930.317b607f%40Nodemailer
>> .
>>
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>
>
>
> --
> *Marcus Ottosson*
> [email protected]
>
> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAAepsjw_wDVgOF5yDbP5Mn9MOtUrVtNMFNktbzQ_cAcw%40mail.gmail.com
> .
>
> For more options, visit https://groups.google.com/groups/opt_out.
>
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA0NAU4q53EJydpwbuHxc-UrCZ%2BJGChcykVoBKLdrOioMw%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.