Skip to content

Instantly share code, notes, and snippets.

@Satak
Last active November 21, 2016 09:08
Show Gist options
  • Save Satak/3f780217f794075e66564f75e5e06c57 to your computer and use it in GitHub Desktop.
Save Satak/3f780217f794075e66564f75e5e06c57 to your computer and use it in GitHub Desktop.
'''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