klieret / AnkiPandas

Analyze and manipulate your Anki collection using pandas! 🌠🐼
https://ankipandas.rtfd.io/
MIT License
131 stars 18 forks source link

AttributeError with ankipandas.Collection().notes #38

Closed brownbat closed 4 years ago

brownbat commented 4 years ago

Was just kicking the tires, col.revs and col.cards worked as expected, but ran into an issue when calling col.notes:

(with col = ankipandas.Collection(), which it seemed to find fine.)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/thomas/.local/lib/python3.8/site-packages/ankipandas/collection.py", line 90, in notes
    return self._get_item("notes")
  File "/home/thomas/.local/lib/python3.8/site-packages/ankipandas/collection.py", line 83, in _get_item
    r = self._get_original_item(item).copy(True)
  File "/home/thomas/.local/lib/python3.8/site-packages/ankipandas/collection.py", line 76, in _get_original_item
    r = AnkiDataFrame.init_with_table(self, item)
  File "/home/thomas/.local/lib/python3.8/site-packages/ankipandas/ankidf.py", line 103, in init_with_table
    new._get_table(col, table, empty=empty)
  File "/home/thomas/.local/lib/python3.8/site-packages/ankipandas/ankidf.py", line 98, in _get_table
    self.normalize(inplace=True)
  File "/home/thomas/.local/lib/python3.8/site-packages/ankipandas/ankidf.py", line 1028, in normalize
    self["nmodel"] = self["mid"].map(raw.get_mid2model(self.db))
  File "/home/thomas/.local/lib/python3.8/site-packages/ankipandas/raw.py", line 347, in get_mid2model
    minfo = get_model_info(db)
  File "/home/thomas/.local/lib/python3.8/site-packages/ankipandas/raw.py", line 334, in get_model_info
    return {int(key): value for key, value in get_info(db)["models"].items()}
AttributeError: 'str' object has no attribute 'items'
klieret commented 4 years ago

Thanks a lot for letting me know!

Hmm, that's strange. Perhaps that's a similar error to https://github.com/klieret/AnkiPandas/issues/36 and has to do with changes introduced by the Anki 2.1.26/2.1.28beta.

What Anki version are you using? And are you running this on a completely empty collection or are there already some cards/notes/models?

And could you run the following snippet:

import ankipandas
from ankipandas.raw import get_info

col = ankipandas.Collection()
model_info = get_info(col.db)["models"]
print(type(model_info))
print(model_info)

and send me the output?

This would help me to track down what's happening there.

(I'm on holidays though, so I might be a bit slow to reply)

brownbat commented 4 years ago

I've only tried building the latest build of Anki on my VM, so I've used the collection with v.1.3.1+ here. It's not an empty or new deck, it was exported from a deck originally used in Anki 2.1.26.

Looks like models is getting an empty string, not sure why.

thomas@thomas-VirtualBox:~/PycharmProjects/anki$ python3

Python 3.8.2 (default, Jul 16 2020, 14:00:26) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ankipandas
>>> from ankipandas.raw import get_info
>>> col = ankipandas.Collection()
INFO: Searching for database. This might take some time. You can speed this up by specifying a search path or directly entering the path to your database.
WARNING: The search will stop at the first hit, so please verify that the result is correct (for example in case there might be morethan one Anki installations)
INFO: Loaded db from /home/thomas/.local/share/Anki2/User 1/collection.anki2
>>> model_info = get_info(col.db)["models"]
>>> print(type(model_info))
<class 'str'>
>>> print(model_info)

>>> 

That's a one line gap there, doesn't appear to contain a space or anything. After that, to confirm the collection has something in it, I ran col.cards just to be sure, here's the first line:

>>> col.cards
                         nid  cord        cmod  cusn  ... cleft codue  cdeck  codeck
cid                                                   ...                           
1589630886942  1589220255215     2  1597756833    -1  ...     0     0 
...

No rush for my benefit. I think you're right on my other comment, I need some other way to generate a set of test decks, maybe genanki.

Good luck with this one, let me know if there's anything else I can do to help test.

klieret commented 4 years ago

Ah, I've updated to the newest stable release and see what's going on there. Basically the way anki saved the configuration in the database has changed slightly, but it should be possible to adapt.

klieret commented 4 years ago

The new version of ankipandas should now support the new db format.

If you want, you could help by testing if it works for you.

To update ankipandas, do

pip3 install --user --upgrade ankipandas
brownbat commented 4 years ago

col.notes now works like a charm.

Your earlier testing recommendations don't survive, but maybe you know that already.

>>> get_info(col.db)
{1: {'crt': 1597392000, 'mod': 1598225343106, 'scm': 1598220616405, 'ver': 15, 'dty': 0, 'usn': 0, 'ls': 0, 'conf': '', 'models': '', 'decks': '', 'dconf': '', 'tags': ''}}

So I have to subscript that get_info call with [1]["models"] instead of just "models", and it ends up empty.

But col.notes and col.cards have integers in nmod and cmod, and col.notes has strings in nmodel that track to my note types, so there are models floating around in there. Maybe that means everything's working as expected.

brownbat commented 4 years ago

Output on col.cards and col.notes, for the curious.

>>> col.cards
                         nid  cord        cmod  cusn     ctype cqueue   cdue  civl  cfactor  creps  clapses  cleft  codue                             cdeck                          codeck
cid                                                                                                                                                                                        
1589630886942  1589220255215     2  1597756833    -1  learning    new    718     0     2500      0        0      0      0    Korean3 Transcription1 Vocab                                
1589630886944  1589220255216     2  1597756833    -1  learning    new   2046     0     2500      0        0      0      0    Korean3 Transcription1 Vocab                                
1589630886946  1589220255217     2  1597756833    -1  learning    new   1459     0     2500      0        0      0      0    Korean3 Transcription1 Vocab                                
1589630886950  1589220255220     2  1597756832    -1   relearn    due -99971    32     2900      3        0      0     19                   Filtered Deck 1  Korean3 Transcription1 Vocab
1589630886952  1589220255221     2  1597756833    -1  learning    new    965     0     2500      0        0      0      0    Korean3 Transcription1 Vocab                                
...                      ...   ...         ...   ...       ...    ...    ...   ...      ...    ...      ...    ...    ...                               ...                             ...
1597361145241  1597361136294     2  1597756833    -1  learning    new   1729     0        0      0        0      0      0  Korean3 Transcription2 Phrases                                
1597361145244  1597361136295     2  1597756833    -1  learning    new    691     0        0      0        0      0      0  Korean3 Transcription2 Phrases                                
1597361145247  1597361136296     2  1597756833    -1  learning    new   2983     0        0      0        0      0      0  Korean3 Transcription2 Phrases                                
1597361145250  1597361136297     2  1597756833    -1  learning    new   2513     0        0      0        0      0      0  Korean3 Transcription2 Phrases                                
1597361145253  1597361136298     2  1597756833    -1  learning    new   1061     0        0      0        0      0      0  Korean3 Transcription2 Phrases                                

[5330 rows x 15 columns]
>>> col.notes
                    nguid        nmod  nusn                  ntags                                              nflds       nmodel
nid                                                                                                                               
1589220255215  u>q;4An(mn  1595335887    -1             [modified]  [ν•œκ΅­, Korea, The formal name of the country is ...   HTSK Vocab
1589220255216  gx^T0_ayJs  1595335887    -1             [modified]  [λ„μ‹œ, city, , λŒ€λ„μ‹œ = a large city<br>μ‹ λ„μ‹œ = a new...   HTSK Vocab
1589220255217  Aua_I=*8L(  1595335887    -1  [duplicate, modified]  [이름, name, , 이름이 λ­μ˜ˆμš”? = What is your name?<br>...   HTSK Vocab
1589220255220  sB5I5CNw:t  1595335887    -1  [duplicate, modified]  [λ‚¨μž, man, , μž˜μƒκΈ΄ λ‚¨μž = handsome man<br>κ°•ν•œ λ‚¨μž = s...   HTSK Vocab
1589220255221  Hd>]fb:J&@  1595335887    -1  [duplicate, modified]  [μ—¬μž, woman, , 예쁜 μ—¬μž = pretty girl<br>μ•„λ¦„λ‹€μš΄ μ—¬μž =...   HTSK Vocab
...                   ...         ...   ...                    ...                                                ...          ...
1597361136294  Or~GRLb0h4  1597361133    -1                     []                      [μ›ƒμŒ, laughter, a smile, , , ]  HTSK Usages
1597361136295  P~hT^`}adj  1597361133    -1                     []            [μ›ƒκ²Œ ν•΄ μ£Όλ‹€, to make somebody laugh, , , ]  HTSK Usages
1597361136296  xVwInN0/GS  1597361133    -1                     []                            [건강에 μ’‹λ‹€, healthy, , , ]  HTSK Usages
1597361136297  O[y=E>>ogO  1597361133    -1                     []                     [건강 검진, health check up, , , ]  HTSK Usages
1597361136298  FYBq5<JyXC  1597361133    -1                     []                           [ν˜„μž¬κΉŒμ§€λŠ”, until now, , , ]  HTSK Usages

[5330 rows x 6 columns]
>>> get_info(col.db)
klieret commented 4 years ago

col.notes now works like a charm.

Your earlier testing recommendations don't survive, but maybe you know that already.

>>> get_info(col.db)
{1: {'crt': 1597392000, 'mod': 1598225343106, 'scm': 1598220616405, 'ver': 15, 'dty': 0, 'usn': 0, 'ls': 0, 'conf': '', 'models': '', 'decks': '', 'dconf': '', 'tags': ''}}

So I have to subscript that get_info call with [1]["models"] instead of just "models", and it ends up empty.

But col.notes and col.cards have integers in nmod and cmod, and col.notes has strings in nmodel that track to my note types, so there are models floating around in there. Maybe that means everything's working as expected.

Right, you now need to use ankipandas.raw.get_model_info to retrieve the information.

Perhaps get_info should be renamed to get_collection_info as it now only includes the kind of "global" information. This is a change that was introduced in anki.

klieret commented 4 years ago

I'd say this looks like it's working, so I'll close this issue, but feel free to write more or re-open if anything should arise :)

klieret commented 3 years ago

@allcontributors please add @brownbat for bug

allcontributors[bot] commented 3 years ago

@klieret

I've put up a pull request to add @brownbat! :tada:

klieret commented 3 years ago

hmm that didn't work as intended. @allcontributors please add @brownbat for bug

allcontributors[bot] commented 3 years ago

@klieret

I've put up a pull request to add @brownbat! :tada: