On Wed, Dec 21, 2016 at 9:17 AM likage <[email protected]> wrote:
> Thanks, it works!
>
> I have a few questions..
>
> 1. `all_data[sel] = {}`, why can't I wrote it as `all_data[sel] = None`? I
> tried and I got an error that states ''NoneType' object does not support
> item assignment'
> I asked this because since I have no values to append in the first place,
> wouldn't using None be more practical? Or, do correct me if I am wrong,
> since we are dealing with dictionaries, initializing an empty dict is then
> the correct way?
>
To me, it does not seem practical to initialize the key with a None value,
because you are going to have to just replace that later with a dictionary
if you end up wanting to assign anything.
all_data[sel] = None
for ...
all_data[sel][frame] = data
That would crash because the value of all_data[sel] is None, and you can't
treat None like a dictionary. It just makes sense to me to start with an
empty dictionary and fill it. Otherwise, you could switch to a defaultdict
and populate it on demand. I just went for the easiest example to get your
functional.
> 2. Do I still need to have `attr_data = {}` still needs to be placed under
> the `for time ... end_frame +1))` line? Would that not cause it to be loop
> continuously? P.S, Again, I tried placing it outside of the `for` statement
> as initially seen in my post, and it works too..
>
Yes, as far as I can tell from reading your code, you need to initialize a
new attr_data at the start of each iteration of a frame. While it is not
going to cause a crash if you initialize it once outside all of the for
loops, what it is doing is corrupting your data. Look carefully at how you
are using it. In each loop you replace the same keys (the attributes) with
new values, and then assign that attr_data to all_data. It would seem you
think that the assignment will deep copy the dictionary each time, but it
does not. You are simply corrupting values from the previous loops.
Consider this smaller example:
http://ideone.com/X09EGs
copy <http://ideone.com/X09EGs#>
1. def test(broken=False):
2. all_data = {}
3. attr_data = {}
4.
5. for i in xrange(5):
6.
7. if not broken:
8. attr_data = {}
9.
10. for j, attr in enumerate(['a','b','c']):
11. attr_data[attr] = i + j
12.
13. all_data[i] = attr_data
14.
15. print all_data
If you don't create a new attr_data for each loop, your output data will be
only the values of the last loop, for all of the keys, because you are
always updating the same reference to the same dictionary.
> 3. This is slightly unrelated but what can I do to refine writing logic
> process? There are times in which I seem to get it (but it is in a big
> chunk where it can be further refine), and times like this, where I am
> unable to..
>
I'm not clear on what you mean by this. But if you are asking how to make
it easier to reason about building data structures processing lots of
values and logic, then maybe what you want to do is break it up into
smaller functions and then compose those functions in another. This may
help you by having to look at less context at one time. That is, maybe you
have a function that can just produce a new attr_data dict, given some
input. And then you just assign that output to another data structure as
needed.
Justin
>
>
> --
> 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/ec6429bb-8cad-4509-8485-83974dfa3e87%40googlegroups.com
> <https://groups.google.com/d/msgid/python_inside_maya/ec6429bb-8cad-4509-8485-83974dfa3e87%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
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/CAPGFgA3v51mgizwzd0toaS%3D-vVfsRJTy6Fy8px1Vd3qZvpUdWA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.