Skip to content

Commit

Permalink
Merge pull request #67 from eumiro/sorted-lists
Browse files Browse the repository at this point in the history
Simplify collections manipulation in code
  • Loading branch information
klieret committed Dec 19, 2020
2 parents 35ebb71 + 1c07344 commit 4d394b7
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 106 deletions.
10 changes: 4 additions & 6 deletions ankipandas/_columns.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@
#: nid.
table2index = {"cards": "cid", "notes": "nid", "revs": "rid"}

our_tables = sorted(list(tables_ours2anki.keys()))
our_tables = sorted(tables_ours2anki)
our_columns = {
table: sorted(
list(
fields_df[(fields_df["Table"] == table) & fields_df["Default"]][
"Column"
].unique()
)
fields_df[(fields_df["Table"] == table) & fields_df["Default"]][
"Column"
].unique()
)
for table in our_tables
}
Expand Down
64 changes: 28 additions & 36 deletions ankipandas/ankidf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import ankipandas.util.dataframe
from ankipandas.util.dataframe import replace_df_inplace
import ankipandas._columns as _columns
from ankipandas.util.misc import invert_dict, flatten_list_list
from ankipandas.util.misc import invert_dict
from ankipandas.util.log import log
from ankipandas.util.checksum import field_checksum
from ankipandas.util.guid import guid as generate_guid
Expand Down Expand Up @@ -650,11 +650,9 @@ def list_tags(self) -> List[str]:
)
else:
return sorted(
list(
{
item for lst in self["ntags"].tolist() for item in lst
}
)
{
item for lst in self["ntags"].tolist() for item in lst
}
)

def list_decks(self) -> List[str]:
Expand All @@ -665,7 +663,7 @@ def list_decks(self) -> List[str]:
"or merge it into your table."
)
else:
decks = sorted(list(self["cdeck"].unique()))
decks = sorted(self["cdeck"].unique())
if "" in decks:
decks.remove("")
return decks
Expand All @@ -677,7 +675,7 @@ def list_models(self):
"Model column 'nmodel' not present. Either use the notes table"
" or merge it into your table."
)
return sorted(list(self["nmodel"].unique()))
return sorted(self["nmodel"].unique())

def has_tag(self, tags: Optional[Union[Iterable[str], str]] = None):
""" Checks whether row has a certain tag ('ntags' column).
Expand Down Expand Up @@ -773,7 +771,7 @@ def add_tag(self, tags: Union[Iterable[str], str], inplace=False):
return

def _add_tags(other):
return other + sorted(list(set(tags) - set(other)))
return other + sorted(set(tags) - set(other))

self["ntags"] = self["ntags"].apply(_add_tags)

Expand Down Expand Up @@ -838,9 +836,7 @@ def was_modified(
if self._fields_format == "columns":
self_sf = self.fields_as_list(inplace=False, force=_force)

cols = sorted(
list(set(self_sf.columns).intersection(set(other.columns)))
)
cols = sorted(set(self_sf.columns).intersection(set(other.columns)))

other_nids = set(other.index)
inters = set(self_sf.index).intersection(other_nids)
Expand Down Expand Up @@ -881,7 +877,7 @@ def modified_columns(
inters = inters.intersection(
self[self.was_modified(other=other, _force=_force)].index
)
inters = sorted(list(inters))
inters = sorted(inters)
return pd.DataFrame(
self.loc[inters, cols].values != other.loc[inters, cols].values,
index=self.loc[inters].index,
Expand Down Expand Up @@ -935,7 +931,7 @@ def was_deleted(
other_ids = set(self.col._get_original_item(self._anki_table).id)

deleted_indices = other_ids - set(self.index)
return sorted(list(deleted_indices))
return sorted(deleted_indices)

# Update modification stamps and similar
# ==========================================================================
Expand Down Expand Up @@ -1394,7 +1390,7 @@ def add_cards(
# --- Ord ---

nid2mid = raw.get_nid2mid(self.db)
missing_nids = sorted(list(set(nid) - set(nid2mid.keys())))
missing_nids = sorted(set(nid) - set(nid2mid))
if missing_nids:
raise ValueError(
"The following note IDs (nid) can't be found in the notes "
Expand All @@ -1404,14 +1400,14 @@ def add_cards(
)
)

mids = list(set(map(lambda x: nid2mid[x], nid)))
mids = {nid2mid[x] for x in nid}
if len(mids) >= 2:
raise ValueError(
"It is only supported to add cards for notes of the same model"
", but you're trying to add cards for notes of "
"models: {}".format(", ".join(map(str, mids)))
)
mid = mids[0]
mid = mids.pop()

# fixme: should use function from ankipandas.raw
available_ords = raw.get_mid2templateords(self.db)[mid]
Expand All @@ -1425,7 +1421,7 @@ def add_cards(
raise ValueError(
"Unknown type for cord specifiation: {}".format(type(cord))
)
not_available = sorted(list(set(cord) - set(available_ords)))
not_available = sorted(set(cord) - set(available_ords))
if not_available:
raise ValueError(
"The following templates are not available for notes of "
Expand All @@ -1446,7 +1442,7 @@ def add_cards(
else:
raise ValueError("Unknown format for cdeck: {}".format(type(cdeck)))
unknown_decks = sorted(
list(set(cdeck) - set(raw.get_did2deck(self.db).values()))
set(cdeck) - set(raw.get_did2deck(self.db).values())
)
if unknown_decks:
raise ValueError(
Expand Down Expand Up @@ -1476,7 +1472,7 @@ def _handle_input(inpt, name, default, typ, options=None):
)
)
if options is not None:
invalid = sorted(list(set(inpt) - set(options)))
invalid = sorted(set(inpt) - set(options))
if invalid:
raise ValueError(
"The following values are no valid "
Expand Down Expand Up @@ -1646,7 +1642,7 @@ def add_notes(
# --- Model ---

model2mid = raw.get_model2mid(self.db)
if nmodel not in model2mid.keys():
if nmodel not in model2mid:
raise ValueError(
f"No model of with name '{nmodel}' exists."
)
Expand All @@ -1656,20 +1652,18 @@ def add_notes(

if is_list_dict_like(nflds):
n_notes = len(nflds)
specified_fields = set(
flatten_list_list(list(map(lambda d: list(d.keys()), nflds)))
)
unknown_fields = sorted(list(specified_fields - set(field_keys)))
specified_fields = {key for nfld in nflds for key in nfld}
unknown_fields = sorted(specified_fields - set(field_keys))
if unknown_fields:
raise ValueError(
"Unknown fields: {}".format(", ".join(unknown_fields))
)
field_key2field = {
key: list(map(lambda d: d.get(key), nflds))
key: [d.get(key) for d in nflds]
for key in field_keys
}
elif is_list_list_like(nflds):
n_fields = list(set(map(len, nflds)))
n_fields = list({len(x) for x in nflds})
n_notes = len(nflds)
if not (len(n_fields) == 1 and n_fields[0] == len(field_keys)):
raise ValueError(
Expand All @@ -1680,19 +1674,19 @@ def add_notes(
)
)
field_key2field = {
field_keys[i]: list(map(lambda x: x[i], nflds))
for i in range(len(field_keys))
field_key: [x[i] for x in nflds]
for i, field_key in enumerate(field_keys)
}
elif is_dict_list_like(nflds):
lengths = list(set(map(len, nflds.values())))
lengths = {len(x) for x in nflds.values()}
if len(lengths) >= 2:
raise ValueError(
"Inconsistent number of "
"fields: {}".format(", ".join(map(str, lengths)))
)
elif len(lengths) == 0:
elif not lengths:
raise ValueError("Are you trying to add zero notes?")
n_notes = lengths[0]
n_notes = lengths.pop()
field_key2field = copy.deepcopy(nflds)
for key in field_keys:
if key not in field_key2field:
Expand Down Expand Up @@ -1722,7 +1716,7 @@ def add_notes(
else:
nid = self._get_ids(n=n_notes)

already_present = sorted(list(set(nid).intersection(set(self.index))))
already_present = sorted(set(nid).intersection(set(self.index)))
if already_present:
raise ValueError(
"The following note IDs (nid) are "
Expand Down Expand Up @@ -1756,9 +1750,7 @@ def add_notes(
else:
nguid = [generate_guid() for _ in range(n_notes)]

existing_guids = sorted(
list(set(nguid).intersection(self["nguid"].unique()))
)
existing_guids = sorted(set(nguid) & set(self["nguid"].unique()))
if existing_guids:
raise ValueError(
"The following globally unique IDs (guid) are already"
Expand Down
6 changes: 3 additions & 3 deletions ankipandas/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ def _get_and_update_info(self) -> Dict[str, Any]:
)
if raw.get_db_version(self.db) == 0:
for key in info_updates:
assert key in info.keys()
assert key in info
info.update(info_updates)
elif raw.get_db_version(self.db) == 1:
assert len(list(info.keys())) == 1
first_key = list(info.keys())[0]
assert len(info) == 1
first_key = list(info)[0]
info[first_key].update(info_updates)
# fixme: this currently doesn't work. In the new db structure there's
# a tags table instead of a field, but it doesn't seem to be
Expand Down
8 changes: 4 additions & 4 deletions ankipandas/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,18 @@ def find_db(
)
found = found[user]
else:
if len(found.keys()) >= 2:
if len(found) >= 2:
raise ValueError(
"Found databases for more than one user: {}. Please specify "
"the user.".format(", ".join(found.keys()))
"the user.".format(", ".join(found))
)
elif len(found.keys()) == 0:
elif not found:
raise ValueError(
"No database found. You might increase the search depth or "
"specify search paths to find more."
)
else:
found = list(found.values())[0]
found = found.popitem()[1]
if len(found) >= 2:
raise ValueError(
"Found more than one database belonging to user {} at {}".format(
Expand Down
2 changes: 1 addition & 1 deletion ankipandas/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def get_empty_table(table: str) -> pd.DataFrame:


def _interpret_json_val(val):
if isinstance(val, str) and len(val) >= 1:
if isinstance(val, str) and val:
try:
return json.loads(val)
except json.decoder.JSONDecodeError:
Expand Down

0 comments on commit 4d394b7

Please sign in to comment.