extern crate pythonic; use pythonic::{Argument, Assignment, Block, Class, Function, KeywordArgument, Statement}; fn main() { let mut program = Block::new(); let mut class = Class::new("Greeter", &program); class.add_docstring("Greeter is a class to say Hello!"); let arguments = vec![ Argument::new("self", None, None, None), Argument::new("name", Some("\"World\""), Some("str"), Some("The name to greet")), ]; let mut init = Function::new("__init__", arguments, &class.block); init.add_docstring("Set up name variable for the Greeter"); init.add_statement( Statement::Assignment( Assignment::new( Argument::variable("self.name"), Statement::variable("name")))); class.add_statement(Statement::Function(init)); let arguments = vec![ Argument::new("self", None, None, None), ]; let mut hello = Function::new("hello", arguments, &class.block); hello.add_docstring("Say hello to self.name"); let print = Statement::function_call( "print", vec![ Statement::unnamed_variable("Hello"), Statement::variable("self.name"), Statement::unnamed_variable("!"), ]); hello.add_statement(print); class.add_statement(Statement::Function(hello)); program.add_statement(Statement::Class(class)); program.add_statement( Statement::Assignment(Assignment::new( Argument::variable("g"), Statement::function_call( "Greeter", vec![ Statement::KeywordArgument(KeywordArgument::new("name", "\"Chris\"")) ] ) ))); let function_call = Statement::function_call( "g.hello", vec![]); program.add_statement(function_call); let expected = r#"class Greeter: """ Greeter is a class to say Hello! """ def __init__(self, name="World"): """ Set up name variable for the Greeter :param name: str The name to greet """ self.name = name def hello(self): """ Say hello to self.name """ print("Hello", self.name, "!") g = Greeter(name="Chris") g.hello() "#; let actual = format!("{}", program); assert_eq!(expected, actual); println!("{}", actual); }