def getOriginFromWikidata(name: str):
    toReturn = Optional.empty()
    placeOfBirth = None

    page = wptools.page(name)
    try:
        page.get_wikidata()
    except:
        print("Couldn't get wikidata from name.")

    try:
        placeOfBirth = page.data['wikidata']['place of birth (P19)']
    except:
        None

    try:
        placeOfBirth = page.data['wikidata']['location of formation (P740)']
    except:
        None

    try:
        index = placeOfBirth.index("(")
        placeOfBirth = placeOfBirth[0:index]
    except:
        None

    if placeOfBirth != None:
        toReturn = Optional.of(placeOfBirth)

    return toReturn
Exemple #2
0
    def handle(self, request: Request, params: Dict):
        if not request:
            raise RuntimeError("Missing request")

        logger.info(f"Updating expense {params['expense_id']}")
        expense_id = Optional.of(params['expense_id'])
        category = Optional.of(params['category'])
        hide = Optional.of(params['hide'])

        if expense_id.is_empty():
            raise RuntimeError("Expense id should not be empty")

        try:
            self.db.update_by_key(expense_id.get(),
                                  category=category,
                                  hide=hide)
            return """
            <html>
                <head>
                    <title>Some HTML in here</title>
                </head>
                <body>
                    <h1>Successful </h1>
                </body>
            </html>
            """
        except Exception as err:
            logger.error(f"Unable to update expense id: {expense_id} - {err}")
            raise RuntimeError("Update failed")
Exemple #3
0
    def test_get_or_raise_on_a_populated_optional_returns_value(self):
        optional = Optional.of("thing")

        class RandomDomainException(Exception):
            pass

        assert optional.get_or_raise(RandomDomainException()) == optional.get()
Exemple #4
0
    def test_raises_if_flat_map_function_returns_non_optional(self):
        def does_not_return_optional(thing):
            return "PANTS"

        optional = Optional.of("thing")
        with pytest.raises(FlatMapFunctionDoesNotReturnOptionalException):
            optional.flat_map(does_not_return_optional)
Exemple #5
0
    def test_get_or_raise_on_an_empty_optional_throws_wrapped_exception(self):
        optional = Optional.empty()

        class RandomDomainException(Exception):
            pass

        with pytest.raises(RandomDomainException):
            optional.get_or_raise(RandomDomainException())
Exemple #6
0
    def test_flat_map_returns_unwrapped_value_with_map_result(self):
        def maps_stuff(thing):
            return Optional.of(thing + "PANTS")

        optional = Optional.of("thing")
        res = optional.flat_map(maps_stuff)
        assert res.is_present()
        assert res.get() == "thingPANTS"
Exemple #7
0
    def test_will_run_consumer_if_present(self):
        scope = {'seen': False}

        def some_thing_consumer(thing):
            scope['seen'] = True

        optional = Optional.of("thing")
        optional.if_present(some_thing_consumer)
        assert scope['seen']
Exemple #8
0
    def test_will_raise_on_or_else_raise_from_if_present_when_not_present(
            self):
        class TestException(Exception):
            pass

        optional = Optional.empty()
        with pytest.raises(TestException):
            optional.if_present(lambda x: x).or_else_raise(
                TestException("Something"))
Exemple #9
0
    def test_will_not_run_consumer_if_not_present(self):
        scope = {'seen': False}

        def some_thing_consumer(thing):
            scope['seen'] = True

        optional = Optional.empty()
        optional.if_present(some_thing_consumer)
        assert not scope['seen']
Exemple #10
0
    def test_wont_raise_on_or_else_raise_from_if_present_when_present(self):
        class ShouldNotHappenException(Exception):
            pass

        optional = Optional.of("thing")
        scope = {'seen': False}

        def some_thing_consumer(thing):
            scope['seen'] = True

        optional.if_present(some_thing_consumer).or_else_raise(
            ShouldNotHappenException)
        assert scope['seen']
Exemple #11
0
    def test_will_run_or_else_from_if_present_when_not_present(self):
        scope = {
            'if_seen': False,
            'else_seen': False,
        }

        def some_thing_consumer(thing):
            scope['if_seen'] = True

        def or_else_procedure():
            scope['else_seen'] = True

        optional = Optional.empty()
        optional.if_present(some_thing_consumer).or_else(or_else_procedure)
        assert not scope['if_seen']
        assert scope['else_seen']
def getArtistOriginFromScraping(name: str):
    result = Optional.empty()

    if result.is_empty():
        result = getOriginFromMusicbrainz(name)

    if result.is_empty():
        result = getOriginFromWikipedia(name + " Musician")

    if result.is_empty():
        result = getOriginFromWikipedia(name + " Band")

    if result.is_empty():
        result = getOriginFromWikipedia(name)

    if result.is_empty():
        result = getOriginFromWikidata(name)

    return result
Exemple #13
0
 def test_empty_optional_not_equal_non_empty_optional(self):
     assert Optional.empty() != Optional.of("thing")
Exemple #14
0
 def test_can_get_when_present_and_have_checked(self):
     optional = Optional.of("thing")
     assert optional.is_present()
     assert optional.get() == "thing"
Exemple #15
0
 def test_cannot_get_from_empty_even_after_checking(self):
     optional = Optional.empty()
     assert optional.is_empty()
     with pytest.raises(OptionalAccessOfEmptyException):
         optional.get()
Exemple #16
0
 def test_is_not_present_with_empty(self):
     optional = Optional.of(None)
     assert not optional.is_present()
Exemple #17
0
 def test_is_present_with_content(self):
     optional = Optional.of("thing")
     assert optional.is_present()
Exemple #18
0
 def test_empty_optionals_are_falsy(self):
     assert not Optional.empty()
Exemple #19
0
 def test_can_instantiate_an_empty_optional_via_the_zero_arity_of(self):
     assert Optional.of() == Optional.empty()
Exemple #20
0
 def test_populated_optionals_are_truthy(self):
     assert Optional.of('foo')
Exemple #21
0
 def test_non_empty_optionals_with_non_equal_content_are_not_equal(self):
     assert Optional.of("PANTS") != Optional.of("thing")
Exemple #22
0
 def test_non_empty_optionals_with_equal_content_are_equal(self):
     assert Optional.of("PANTS") == Optional.of("PANTS")
Exemple #23
0
 def test_can_eval_the_representation_of_an_empty_optional(self):
     optional = Optional.empty()
     assert eval(repr(optional)) == optional
Exemple #24
0
 def get_open_class(self, period: int, subject: Subject) -> Optional.of(Class):
     newest_class = self.matrix[period][subject.id][-1]
     if newest_class.is_open():
         return Optional.of(newest_class)
     return Optional.empty()
Exemple #25
0
 def test_instantiate_with_none(self):
     optional = Optional.of(None)
     assert optional.is_empty()
Exemple #26
0
 def test_populated_optionals_are_truthy_even_if_their_value_is_falsy(self):
     assert Optional.of(False)
Exemple #27
0
 def test_get_or_default_on_a_populated_optional_ignores_default_value(
         self):
     optional = Optional.of("thing")
     assert optional.get_or_default("pants") == optional.get()
Exemple #28
0
 def test_can_eval_the_representation_of_a_populated_optional(self):
     optional = Optional.of('23')
     assert eval(repr(optional)) == optional
Exemple #29
0
 def test_can_instantiate_with_any_other_value(self):
     assert Something(value=23, optional=Optional) == Optional.of(23)
Exemple #30
0
 def test_get_or_default_on_an_empty_optional_returns_default_value(self):
     optional = Optional.empty()
     assert optional.get_or_default("pants") == "pants"