<commit_before>import hashlib
import pickle
import os

from ..algorithms import librosa_analysis
from track import Track

class Song(Track):
    """A :py:class:`radiotool.composer.Track`
    subclass that wraps a music .wav file.
    Allows access to a musical analysis of the song.
    """

    def __init__(self, fn, name="Song name", cache_dir=None, refresh_cache=False):
        self._analysis = None
        self._checksum = None
        self.refresh_cache = refresh_cache
        self.cache_dir = cache_dir
        Track.__init__(self, fn, name)

    @property
    def analysis(self):
        """Get musical anaylsis of the song using the librosa library
        """
        if self._analysis is not None:
            return self._analysis

        if self.cache_dir is not None:
            path = os.path.join(self.cache_dir, self.checksum)
            try:
                if self.refresh_cache: raise IOError
                with open(path + '.pickle', 'rb') as pickle_file:
                    self._analysis = pickle.load(pickle_file)
            except IOError:
                self._analysis = librosa_analysis.analyze_frames(self.all_as_mono(), self.samplerate)
                with open(path + '.pickle', 'wb') as pickle_file:
                    pickle.dump(self._analysis, pickle_file, pickle.HIGHEST_PROTOCOL)
        else:
            self._analysis = librosa_analysis.analyze_frames(self.all_as_mono(), self.samplerate)
        return self._analysis

    @property
    def checksum(self):
        if self._checksum is not None:
            return self._checksum

        block_size = 65536
        hasher = hashlib.sha256()
        with open(self.filename, 'rb') as f:
            buf = f.read(block_size)
            while len(buf) > 0:
                hasher.update(buf)
                buf = f.read(block_size)
        self._checksum = hasher.digest()
        return self._checksum<commit_msg>using hexdigest instead of digest<commit_after>import hashlib
import pickle
import os

from ..algorithms import librosa_analysis
from track import Track

class Song(Track):
    """A :py:class:`radiotool.composer.Track`
    subclass that wraps a music .wav file.
    Allows access to a musical analysis of the song.
    """

    def __init__(self, fn, name="Song name", cache_dir=None, refresh_cache=False):
        self._analysis = None
        self._checksum = None
        self.refresh_cache = refresh_cache
        self.cache_dir = cache_dir
        Track.__init__(self, fn, name)

    @property
    def analysis(self):
        """Get musical anaylsis of the song using the librosa library
        """
        if self._analysis is not None:
            return self._analysis

        if self.cache_dir is not None:
            path = os.path.join(self.cache_dir, self.checksum)
            try:
                if self.refresh_cache: raise IOError
                with open(path + '.pickle', 'rb') as pickle_file:
                    self._analysis = pickle.load(pickle_file)
            except IOError:
                self._analysis = librosa_analysis.analyze_frames(self.all_as_mono(), self.samplerate)
                with open(path + '.pickle', 'wb') as pickle_file:
                    pickle.dump(self._analysis, pickle_file, pickle.HIGHEST_PROTOCOL)
        else:
            self._analysis = librosa_analysis.analyze_frames(self.all_as_mono(), self.samplerate)
        return self._analysis

    @property
    def checksum(self):
        if self._checksum is not None:
            return self._checksum

        block_size = 65536
        hasher = hashlib.sha256()
        with open(self.filename, 'rb') as f:
            buf = f.read(block_size)
            while len(buf) > 0:
                hasher.update(buf)
                buf = f.read(block_size)
        self._checksum = hasher.hexdigest()
        return self._checksum
{'fraction_non_alphanumeric': 0.08815141301529686, 'fraction_numerical': 0.00466683951257454, 'mean_word_length': 2.1087832393231265, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2236524', 'n_tokens_mistral': 1156, 'n_tokens_neox': 1110, 'n_words': 291}