Skip to content

Created unique plugin#1678

Merged
cvium merged 1 commit intoFlexget:developfrom
BackSlasher:uniques-plugin
Feb 18, 2017
Merged

Created unique plugin#1678
cvium merged 1 commit intoFlexget:developfrom
BackSlasher:uniques-plugin

Conversation

@BackSlasher
Copy link
Copy Markdown
Contributor

Takes action on all duplicates, except for the first

Motivation for changes:

Redoing #1231 in a separate plugin
Allowing to create unique lists by rejecting second+ items based on a list of fields.
Also created tests

Detailed changes:

New plugin "unique":

unique:
  field: imdb_id

unique:
  field: 
  - series_episode
  - series_name

Addressed issues:

(no tickets)
Personal usecase - select only one entry of each kind (e.g. only one torrent for a movie)

Config usage if relevant (new plugin or updated schema):

New plugin as described above.
Nothing else

Log and/or tests output (preferably both):

nitz@mars:~/projects/flexget/virtualenv (master *+%)$ bin/py.test ~/projects/flexget/flexget/flexget/tests/test_unique.py 
=========================================================================================== test session starts ============================================================================================
platform linux2 -- Python 2.7.11+, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: /home/nitz/projects/flexget/flexget, inifile: setup.cfg
plugins: xdist-1.14, cov-2.2.1, capturelog-0.7
collected 6 items 

../flexget/flexget/tests/test_unique.py ......

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use setdefault() instead

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the linebreak?

Copy link
Copy Markdown
Contributor Author

@BackSlasher BackSlasher Feb 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line seemed too long, wanted to simplify reading it. Will move to one line

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this function only used in one place? Might make more sense to scrap the function then... It only makes it harder to read.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not done in prepare_config?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why save it in a variable? I don't anything is gained by it

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of if one-liners. Just move continue down.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing happens if field is not specified?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defaults to the entire entity.


    def extract_fields(self, entry, field_names):
        if field_names:
            pass
            return {field: entry.get(field) for field in field_names}
        else:
            return entry

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I document it somewhere?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wonder if it ever happens that two entries are identical and wouldn't you want to ensure uniqueness on specific parameters? I'm not sure I see the "general" case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this behavior from the "duplicates" plugin. I think it's a good behavior for ensuring uniqueness of simple entities.
Would you have required specifying field?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The duplicates plugin requires action and field.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I've misremembered.
I'm feeling like if you say "I want these entities to be unique" and you're not saying based on which criteria, it's legit to compare the entire entity.
If you feel like this is a weird behavior, I'll remove it and mandate the field

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to mirror duplicates plugin and require field.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are all these imports for?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't any(entry_fields) the same as entry_fields? You can also short-circuit this if by checking entry_fields first ie. if entry_fields and entry_fields == prospect_fields.

@BackSlasher
Copy link
Copy Markdown
Contributor Author

Thanks for the review! Fixed issues you commented about

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used anywhere

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is really needed.

@BackSlasher BackSlasher force-pushed the uniques-plugin branch 3 times, most recently from 85a7257 to a19e789 Compare February 9, 2017 19:02
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this import is used

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

task.entries only contains entries that are accepted or undecided btw

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd like to keep the skips, in case they're rejected / accepted mid-plugin

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't make sense to check this after you've extracted the fields :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Won't you potentially end up with a bunch of None values?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could, but if that's what the user wants to determine uniqueness by...
I'd prefer that than failing the run because of a missing field. Not sure what's the flexget norms on that

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it makes much sense to compare the entries if they don't have all the fields present.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ack

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need these two either.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems wrong :) Beware of copy/pasting. You make a lot of errors.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I hope my colleagues will never see this :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought this was autoconverting to a list. Apparently not. Oh well

@BackSlasher BackSlasher force-pushed the uniques-plugin branch 3 times, most recently from b0b0f27 to a1b1b37 Compare February 9, 2017 19:49
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is also performed after field extraction

@BackSlasher
Copy link
Copy Markdown
Contributor Author

No rush whatsoever, but I think I cleared all of the review issues. Would appreciate more feedback!

@cvium
Copy link
Copy Markdown
Contributor

cvium commented Feb 10, 2017

I still think it'd be best to skip entries that do not have all specified fields present.

@BackSlasher
Copy link
Copy Markdown
Contributor Author

Right now it's failing for these entities, like duplicate:
https://github.com/flexget/Flexget/blob/master/flexget/plugins/filter/duplicates.py#L38
What do you mean "skip"?

@BackSlasher
Copy link
Copy Markdown
Contributor Author

ping @cvium
also @paranoidi
thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will crash if the field is not present.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: https://github.com/flexget/Flexget/blob/master/flexget/plugins/filter/duplicates.py#L38
Think we should skip entries/prospects without the required fields instead?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Duplicates should not be changed in this PR though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R. Done and added testing for it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to use EAFP in this case. Just have extract_fields assume all fields are present and catch the KeyError here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

R, done

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to change this back?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right you are

Takes action on all duplicates, except for the first
@cvium
Copy link
Copy Markdown
Contributor

cvium commented Feb 18, 2017

Hmm, this plugin is starting to look a lot like duplicates. Maybe it would be better to just change duplicates?

Aren't the ideas similar?

@BackSlasher
Copy link
Copy Markdown
Contributor Author

from #1231:
@paranoidi:

It is semantically very weird to use duplicates plugin to get first unique match. I would make a separate plugin for that purpose which is more suitably named (ie. unique). I was hesitant to even add the action support for this plugin originally. I'm declining this due that concern. I would be willing to accept this refined as a separate plugin though :)

@cvium
Copy link
Copy Markdown
Contributor

cvium commented Feb 18, 2017

I see.

@cvium cvium merged commit 77dbb6a into Flexget:develop Feb 18, 2017
@cvium
Copy link
Copy Markdown
Contributor

cvium commented Feb 18, 2017

Wanna update the wiki?

@BackSlasher
Copy link
Copy Markdown
Contributor Author

sure, how do I

@cvium
Copy link
Copy Markdown
Contributor

cvium commented Feb 18, 2017

@BackSlasher
Copy link
Copy Markdown
Contributor Author

Great. Will do soon

@BackSlasher
Copy link
Copy Markdown
Contributor Author

Done
https://flexget.com/Plugins/unique
@cvium thanks for reviewing this! I appreciate the effort.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants