Last active
November 21, 2016 09:08
-
-
Save Satak/3f780217f794075e66564f75e5e06c57 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'''Dynamic templates''' | |
# -*- coding: utf-8 -*- | |
import os | |
import locale | |
from string import Template | |
class Car: | |
'''Class Car''' | |
def __init__(self, brand="Tesla", model="Model S", year=2016, color="Black", price=100000.00): | |
self.brand = brand | |
self.model = model | |
self.year = year | |
self.color = color | |
locale.setlocale(locale.LC_ALL, 'fi') | |
self.price = locale.currency(price) | |
def __str__(self): | |
return new_template(self) | |
def new_template(obj): | |
'''Dynamically creates a string template from object attributes''' | |
temp_str = "" | |
for key in sorted(obj.__dict__): | |
temp_str += "{key}\t: ${value}\n".format( | |
key=key.capitalize(), | |
value=key | |
) | |
template = Template(temp_str) | |
return template.substitute(obj.__dict__) | |
def main(): | |
'''main method''' | |
os.system('cls') | |
car = Car() | |
print(car) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment