Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from pathlib import Path
- from random import choice
- import pickle
- def load_state():
- state_file = Path('state.json')
- if not state_file.exists():
- pictures = list(Path('.').glob('**/*.jpg'))
- save_state(pictures)
- return pictures
- else:
- with state_file.open('rb') as fd:
- return pickle.load(fd)
- def save_state(pictures):
- state_file = Path('state.json')
- with state_file.open('wb') as fd:
- pickle.dump(pictures, fd)
- def pop_random_picture(pictures):
- """
- This function mutates pictures.
- It returns a random picture from the set
- and removes this picture from the set.
- """
- if pictures:
- picture = choice(pictures)
- pictures.remove(picture)
- return picture
- return None
- pictures = load_state()
- print(f'Number of pics: {len(pictures)}')
- print(pop_random_picture(pictures))
- save_state(pictures)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement