# Buggy code
def generate_sample_ep(multi=None, abd=False, sports=False, anime_type=None):
    # make a fake episode object
    ep = TVEpisode(2, 3, 3, "Ep Name")

    # pylint: disable=protected-access
    ep.status = DOWNLOADED
    ep.quality = Quality.HDTV
    ep.airdate = datetime.date(2011, 3, 9)

    if abd:
        ep.release_name = "Show.Name.2011.03.09.HDTV.x264-RLSGROUP"
        ep.series.air_by_date = 1
    elif sports:
        ep.release_name = "Show.Name.2011.03.09.HDTV.x264-RLSGROUP"
        ep.series.sports = 1
    else:
        if anime_type != 3:
            ep.series.anime = 1
            ep.release_name = "Show.Name.003.HDTV.x264-RLSGROUP"
        else:
            ep.release_name = "Show.Name.S02E03.HDTV.x264-RLSGROUP"

    if multi is not None:
        ep.name = "Ep Name (1)"

        if anime_type != 3:
            ep.series.anime = 1

            ep.release_name = "Show.Name.003-004.HDTV.x264-RLSGROUP"

            secondEp = TVEpisode(2, 4, 4, "Ep Name (2)")
            secondEp.status = DOWNLOADED
            secondEp.quality = Quality.HDTV
            secondEp.release_name = ep.release_name

            ep.related_episodes.append(secondEp)
        else:
            ep.release_name = "Show.Name.S02E03E04E05.HDTV.x264-RLSGROUP"

            secondEp = TVEpisode(2, 4, 4, "Ep Name (2)")
            secondEp.status = DOWNLOADED
            secondEp.quality = Quality.HDTV
            secondEp.release_name = ep.release_name

            thirdEp = TVEpisode(2, 5, 5, "Ep Name (3)")
            thirdEp.status = DOWNLOADED
            thirdEp.quality = Quality.HDTV
            thirdEp.release_name = ep.release_name

            ep.related_episodes.append(secondEp)
            ep.related_episodes.append(thirdEp)

    return ep


# Traceback (AttributeError)
2019-02-22 03:02:57 DEBUG    Thread_6 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.018264055252075195, season: None, quality: Unknown, total_time: 0.018332958221435547
2019-02-22 03:02:57 DEBUG    Thread_6 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.027241230010986328, season: None, quality: Unknown, total_time: 0.02730083465576172
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.024758100509643555, season: None, quality: Unknown, total_time: 0.02481818199157715
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.04372978210449219, season: None, quality: Unknown, total_time: 0.04378795623779297
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.024518966674804688, season: None, quality: Unknown, total_time: 0.024566173553466797
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 ERROR    Thread_6 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 130, in testNaming
    result = naming.test_name(pattern, multi, abd, sports, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 298, in test_name
    return {'name': ep.formatted_filename(pattern, multi, anime_type), 'dir': ep.formatted_dir(pattern, multi)}
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.02576303482055664, absolute_episode: [], quality: Unknown, total_time: 0.02585601806640625
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.036277055740356445, absolute_episode: [], quality: Unknown, total_time: 0.03634905815124512
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.030246973037719727, absolute_episode: [], quality: Unknown, total_time: 0.030331134796142578
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.02576303482055664, absolute_episode: [], quality: Unknown, total_time: 0.02585601806640625
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.036277055740356445, absolute_episode: [], quality: Unknown, total_time: 0.03634905815124512
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.016936779022216797, season: None, quality: Unknown, total_time: 0.01699090003967285
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.030246973037719727, absolute_episode: [], quality: Unknown, total_time: 0.030331134796142578
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Checking whether the pattern Season %S/%SN S%0SE%0E is valid for a multi episode
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.013588905334472656, season: None, quality: Unknown, total_time: 0.013649940490722656
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Show.Name.003-004.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.027174949645996094, season: None, quality: Unknown, total_time: 0.027235031127929688
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Show.Name.003-004.HDTV.x264-RLSGROUP into title: Show Name, absolute_episode: [3, 4], episode: [], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.016008853912353516, season: None, quality: Unknown, total_time: 0.016068220138549805
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.015719890594482422, absolute_episode: [], quality: Unknown, total_time: 0.0158078670501709
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.015719890594482422, absolute_episode: [], quality: Unknown, total_time: 0.0158078670501709
2019-02-22 03:02:57 ERROR    Thread_1 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 160, in isNamingValid
    is_valid = naming.check_valid_naming(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 151, in check_valid_naming
    valid = valid and validate_name(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 201, in validate_name
    new_name = ep.formatted_filename(pattern, multi, anime_type) + '.ext'
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Checking whether the pattern Season %S/%SN S%0SE%0E is valid for a multi episode
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.014174938201904297, season: None, quality: Unknown, total_time: 0.014236927032470703
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003-004.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.013724088668823242, season: None, quality: Unknown, total_time: 0.013782978057861328
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003-004.HDTV.x264-RLSGROUP into title: Show Name, absolute_episode: [3, 4], episode: [], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.015986919403076172, season: None, quality: Unknown, total_time: 0.016047000885009766
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:58 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:58 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:58 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:58 DEBUG    Thread_39 :: [ee861d0] Trying to parse Show Name S02E03.ext
2019-02-22 03:02:58 DEBUG    Thread_3 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:58 DEBUG    Thread_0 :: [ee861d0] Trying to parse Show Name S02E03.ext
2019-02-22 03:02:58 ERROR    Thread_3 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 160, in isNamingValid
    is_valid = naming.check_valid_naming(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 151, in check_valid_naming
    valid = valid and validate_name(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 201, in validate_name
    new_name = ep.formatted_filename(pattern, multi, anime_type) + '.ext'
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'

# Fixed code
def generate_sample_ep(multi=None, abd=False, sports=False, anime_type=None):
    series = Series(indexer=1, indexerid=12345, lang="en")
    series.name = "Show Name"
    series.genre = "Comedy"
    if anime_type:
        series.anime = 1

    # make a fake episode object
    ep = Episode(series=series, season=2, episode=3)

    ep.status = DOWNLOADED
    ep.quality = Quality.HDTV
    ep.airdate = datetime.date(2011, 3, 9)
    ep.name = "Ep Name"
    ep.absolute_number = 13
    ep.release_name = "Show.Name.S02E03.HDTV.x264-RLSGROUP"
    ep.is_proper = True

    if abd:
        ep.release_name = "Show.Name.2011.03.09.HDTV.x264-RLSGROUP"
        ep.series.air_by_date = 1
    elif sports:
        ep.release_name = "Show.Name.2011.03.09.HDTV.x264-RLSGROUP"
        ep.series.sports = 1
    elif anime_type:
        ep.release_name = "Show.Name.013.HDTV.x264-RLSGROUP"

    if multi is not None:
        ep.name = "Ep Name (1)"
        ep.release_name = "Show.Name.S02E03E04E05.HDTV.x264-RLSGROUP"
        if anime_type:
            ep.release_name = "Show.Name.013-015.HDTV.x264-RLSGROUP"

        second_ep = Episode(series, 2, 4)
        second_ep.name = "Ep Name (2)"
        second_ep.status = DOWNLOADED
        second_ep.quality = Quality.HDTV
        second_ep.absolute_number = 14
        second_ep.release_name = ep.release_name

        third_ep = Episode(series, 2, 5)
        third_ep.name = "Ep Name (3)"
        third_ep.status = DOWNLOADED
        third_ep.quality = Quality.HDTV
        third_ep.absolute_number = 15
        third_ep.release_name = ep.release_name

        ep.related_episodes.append(second_ep)
        ep.related_episodes.append(third_ep)

    return ep