salvoventura / pypexels

An open source Python wrapper for the Pexels REST API https://www.pexels.com/api/.
MIT License
73 stars 15 forks source link

added hash and eq magic methods for comparing photos/videos by its id #9

Closed kz159 closed 4 years ago

kz159 commented 4 years ago

With two methods you can create set of unique photo/video classes This change will help a lot when using random() method

codecov-commenter commented 4 years ago

Codecov Report

Merging #9 into master will decrease coverage by 0.27%. The diff coverage is 50.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master       #9      +/-   ##
==========================================
- Coverage   83.96%   83.69%   -0.28%     
==========================================
  Files          21       21              
  Lines         499      503       +4     
==========================================
+ Hits          419      421       +2     
- Misses         80       82       +2     
Impacted Files Coverage Δ
pypexels/src/models.py 53.84% <50.00%> (-0.70%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update e4683e6...2bed795. Read the comment docs.

salvoventura commented 4 years ago

Hello, thanks for the PR. The use case seems quite interesting, I don't see a problem in pulling this in. Do you happen to have a code snippet that exemplifies the use case? This would simplify my documentation work.

Cheers, Salvatore

kz159 commented 4 years ago

Glad to hear it! Here's the example(don' forget to add API variable to your environment) This code will print out '1 2' because set is collection of unique elements

from os import getenv
import pypexels

API = getenv('API')

papi = pypexels.PyPexels(API)

first = papi.single_photo(7777)
second = papi.single_photo(7777)

s = set()
l = []

s.add(first)
s.add(second)

l.append(first)
l.append(second)

print(len(s), len(l))

Here's my use case of this commit:

wo_clones_entries = []
for i in entries: # entries is a list from papi.random(per_page=50).entries
    if i.id not in [l.id for l in wo_clones_entries]: # i need a for cycle for adding unique photos
        wo_clones_entries.append(i)

Upper code will transform to just: set(papi.random(per_page=50).entries)