Example #1
0
 def test_constructor(self):
     color = PlayerColor.BLUE
     card = CardType.CARD_APOLOGIES
     history = History("action", color, card)
     assert history.action == "action"
     assert history.color is color
     assert history.card is card
     assert history.timestamp <= DateTime.utcnow()
Example #2
0
def get_last_record(last_record_cursor: DateTime,
                    form_id: str = "form1") -> str:
    metadata = {"referer": f"http://134/{form_id}"} if form_id else {}
    return {
        Responses.cursor_field:
        last_record_cursor.format(Responses.date_format),
        "metadata": metadata
    }
Example #3
0
 def test_track_no_player(self):
     game = Game(4)
     game.track("action")
     assert game.history[0].action == "action"
     assert game.history[0].color is None
     assert game.history[0].card is None
     assert game.history[0].timestamp <= DateTime.utcnow()
     assert game.players[PlayerColor.RED].turns == 0
     assert game.players[PlayerColor.YELLOW].turns == 0
     assert game.players[PlayerColor.BLUE].turns == 0
     assert game.players[PlayerColor.GREEN].turns == 0
Example #4
0
 def test_track_with_color(self):
     game = Game(4)
     player = MagicMock(color=PlayerColor.RED)
     card = MagicMock(cardtype=CardType.CARD_12)
     game.track("action", player, card)
     assert game.history[0].action == "action"
     assert game.history[0].color is PlayerColor.RED
     assert game.history[0].card == CardType.CARD_12
     assert game.history[0].timestamp <= DateTime.utcnow()
     assert game.players[PlayerColor.RED].turns == 1
     assert game.players[PlayerColor.YELLOW].turns == 0
     assert game.players[PlayerColor.BLUE].turns == 0
     assert game.players[PlayerColor.GREEN].turns == 0
Example #5
0
def get_recent_episodes(recent_episodes_date: DateTime,
                        exec_date: DateTime) -> List[Dict]:
    number_re = re.compile("^\d+")

    tree = etree.parse(
        urllib.request.urlopen(
            "https://talkpython.fm/episodes/rss_full_history"))
    root = tree.getroot()
    recent_episodes = []
    for item in root.iter("item"):
        tag_to_text = {}
        for child in item:
            # print("%s - %s" % (child.tag, child.text))
            tag_to_text[child.tag] = child.text
        id_ = tag_to_text[
            "{http://www.itunes.com/dtds/podcast-1.0.dtd}episode"].strip()
        assert len(id_) > 0
        id_ = "TalkPythonToMe:" + id_

        title = tag_to_text[
            "{http://www.itunes.com/dtds/podcast-1.0.dtd}title"].strip()
        assert len(title) > 0
        summary = tag_to_text[
            "{http://www.itunes.com/dtds/podcast-1.0.dtd}summary"]
        if summary is None:
            summary = title
        else:
            summary = summary.strip()
        author = tag_to_text[
            "{http://www.itunes.com/dtds/podcast-1.0.dtd}author"].strip()
        assert len(author) > 0
        episode_number = tag_to_text[
            "{http://www.itunes.com/dtds/podcast-1.0.dtd}episode"].strip()
        episode_number = int(episode_number)
        d = tag_to_text[
            "{http://www.itunes.com/dtds/podcast-1.0.dtd}duration"].strip()
        d = [int(x.strip()) for x in d.split(":")]
        if len(d) == 2:
            duration_in_seconds = d[0] * 60 + d[1]
        elif len(d) == 3:
            duration_in_seconds = d[0] * 3600 + d[1] * 60 + d[2]
        else:
            raise ValueError(d)
        keywords = tag_to_text[
            "{http://www.itunes.com/dtds/podcast-1.0.dtd}keywords"].strip()
        keywords = [x.strip() for x in keywords.split(",")]
        raw_text = tag_to_text["description"].strip()
        assert len(raw_text) > 0
        ## possible but not necessary here:
        ## ensure that we separate some html elements as newlines
        # raw_text = raw_text.replace("</div>", "\n")
        # raw_text = raw_text.replace("</code>", "\n")
        # raw_text = raw_text.replace("</li>", "\n")
        # raw_text = raw_text.replace("</p>", "\n")
        parsed_text = BeautifulSoup(raw_text, "html.parser").get_text()
        url = tag_to_text["link"].strip()
        publication_date = tag_to_text["pubDate"]
        publication_date = arrow.get(publication_date,
                                     "D MMM YYYY").format("YYYY-MM-DD")

        if pendulum.parse(publication_date) < recent_episodes_date:
            # episode is too old and since episodes are sorted by publication date, we can stop here
            break

        # append transcript to both parsed and raw text
        repo_api_url = "https://api.github.com/repos/mikeckennedy/talk-python-transcripts/git/trees/master"
        transcript = python_bytes.fetch_transcript(episode_number, number_re,
                                                   repo_api_url)
        # Skip episode for now as we only want episodes with transcripts
        if transcript is None:
            continue
        transcript = "\nEpisode transcript:\n" + transcript
        raw_text += transcript
        parsed_text += transcript

        doc_dict = {
            "id": id_,
            "version": "1",
            "source": "Talk Python To Me",
            "title": title,
            "document_type": "Podcast episode",
            "authors": [author],
            "publication_date": publication_date,
            "update_date": exec_date.to_date_string(),
            "urls": [url],
            "summary": summary,
            "raw_text": raw_text,
            "raw_text_format": "HTML",
            "parsed_text": parsed_text,
            "language": "English",
            "keywords": keywords,
            "extra": {
                "duration_in_seconds": duration_in_seconds,
                "episode_number": episode_number,
                "has_transcript": True,
            },
        }
        recent_episodes.append(doc_dict)
    return recent_episodes
Example #6
0
def test_fileds_none_dump_handle(ma_app: Flask, pendulum_field_schema: Schema) -> None:
    with ma_app.app_context():
        data = {"time": None}
        res = pendulum_field_schema.dump(data)
        assert res["time"] is None


@pytest.mark.parametrize(
    "data, result",
    [
        (
            {"created_date": "2019-04-03", "modified_date": "2019-04-03"},
            {
                "created__between": (
                    DateTime(2019, 4, 2, 16, tzinfo=utc),
                    DateTime(2019, 4, 3, 16, tzinfo=utc),
                ),
                "modified__between": (
                    DateTime(2019, 4, 2, 16, tzinfo=utc),
                    DateTime(2019, 4, 3, 16, tzinfo=utc),
                ),
            },
        ),
        (
            {"created_date": "2019-04-05"},
            {
                "created__between": (
                    DateTime(2019, 4, 4, 16, tzinfo=utc),
                    DateTime(2019, 4, 5, 16, tzinfo=utc),
                )
Example #7
0
def expand_datetime(dt: DateTime, ) -> Tuple[DateTime, DateTime]:
    return dt, dt.add(days=1)