Ejemplo n.º 1
0
def main():
    """Read file of programming language details, save as objects, display."""
    languages = []
    # open the file for reading
    in_file = open('languages.csv', 'r')

    # file format is like: Language,Typing,Reflection,Year
    # 'consume' the first line (header) - we don't need its contents
    in_file.readline()

    # all other lines are language data
    for line in in_file:
        # print(repr(line))  # debugging

        # strip newline from end and split it into parts (CSV)
        parts = line.strip().split(',')
        # print(parts)  # debugging

        # reflection is stored as a string (Yes/No) and we want a Boolean
        reflection = parts[2] == "Yes"

        # construct a ProgrammingLanguage object using the elements
        # year should be an int
        language = ProgrammingLanguage(parts[0], parts[1], reflection,
                                       int(parts[3]))

        # add the language we've just constructed to the list
        languages.append(language)

    # close the file as soon as we've finished reading it
    in_file.close()

    # loop through and display all languages (using their str method)
    for language in languages:
        print(language)
Ejemplo n.º 2
0
from prac_08.programming_language import ProgrammingLanguage

# Create some instances
ruby = ProgrammingLanguage("Ruby", "Dynamic", True, 1995, False)
python = ProgrammingLanguage("Python", "Dynamic", True, 1991, False)
visual_basic = ProgrammingLanguage("Visual Basic", "Static", False, 1991,
                                   False)

print(ruby)

# Create array
languages = [ruby, python, visual_basic]

# Filter language by is_dynamic()
dynamically_typed = [
    language for language in languages if language.is_dynamic()
]

print("The dynamically typed languages are: ")
for typed in dynamically_typed:
    print(typed.name)