Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Building Custom Paragraphs Is Painful #27

Closed
jrg94 opened this issue Jul 20, 2021 · 2 comments · Fixed by #28
Closed

Building Custom Paragraphs Is Painful #27

jrg94 opened this issue Jul 20, 2021 · 2 comments · Fixed by #28
Assignees
Labels
bug Something isn't working

Comments

@jrg94
Copy link
Member

jrg94 commented Jul 20, 2021

Here's the current chunk of code I'm trying to write:

def _generate_program_list(language: LanguageCollection) -> MDList:
    """
    A helper function which generates a list of programs for the README.
    :param language: a language collection
    :return: a list of sample programs list items
    """
    list_items = list()
    for program in language.sample_programs:
        program_line = Paragraph([])
        readable_name = program.normalized_name.replace("-", " ").title()
        program_name = f"{readable_name} in {language.get_readable_name()}"
        program_link = InlineText(program_name, url=program.sample_program_doc_url)
        if not program_link.verify_url():
            program_line.add(":warning: ")
            program_link = InlineText(program_name, url=program.sample_program_issue_url)
        else:
            program_line.add(":white_check_mark: ")
        program_line.add(program_link)
        program_line.add(" [Requirements]")
        program_line.insert_link("Requirements", program.sample_program_req_url)
        list_items.append(program_line)
    return MDList(list_items)

All of this to just generate a couple of sentences. The main trouble I'm running into pertains to #19. But even beyond that, it's really hard to tell what this code is even supposed to do. Part of me thinks that the pattern should be to avoid InlineText at all costs. I feel like this would be a lot more readable if the user was able to construct their text first. That way, we could leverage various paragraph functions to modify the text after the fact. One idea I currently have is having a parameter which allows the user to test if their link is valid as they insert it. Right now, insert_link returns a Paragraph, so you can chain the calls. Maybe we need a function that checks all links in a paragraph...

@jrg94 jrg94 self-assigned this Jul 20, 2021
@jrg94 jrg94 added the enhancement New feature or request label Jul 20, 2021
@jrg94
Copy link
Member Author

jrg94 commented Jul 20, 2021

Here's what I'd love this code to look like:

def _generate_program_list(language: LanguageCollection) -> MDList:
    """
    A helper function which generates a list of programs for the README.
    :param language: a language collection
    :return: a list of sample programs list items
    """
    list_items = list()
    for program in language.sample_programs:
        program_line = Paragraph([f":white_check_mark:  {readable_name} in {language.get_readable_name()} [Requirements]"]) \
            .insert_link(f"{readable_name} in {language.get_readable_name()}", program.sample_program_doc_url) \
            .insert_link("Requirements", program.sample_program_req_url)
        if program_line.verify_urls()[program.sample_program_doc_url]: # -> dict[str: bool]
            program_line.replace(":white_check_mark:", ":warning:")
            program_line.insert_link(f"{readable_name} in {language.get_readable_name()}", program.sample_program_issue_url)
        list_items.append(program_line)
    return MDList(list_items)

To me, this seems infinitely cleaner. For one, I know roughly what the output string is going to look like before we even do anything. I can also quickly tell which portions of the paragraph will have links. Then, we can bulk retrieve URL states and check the string accordingly.

@jrg94 jrg94 added bug Something isn't working and removed enhancement New feature or request labels Jul 20, 2021
@jrg94
Copy link
Member Author

jrg94 commented Jul 21, 2021

I added the replace and verify URLs methods to paragraph. Going to merge them in now and call it a night!

@jrg94 jrg94 linked a pull request Jul 21, 2021 that will close this issue
@jrg94 jrg94 closed this as completed in #28 Jul 21, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant