Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to generate python code

class CodeGenerator:
    def __init__(self, indentation='	'):
        self.indentation = indentation
        self.level = 0
        self.code = ''

    def indent(self):
        self.level += 1

    def dedent(self):
        if self.level > 0:
            self.level -= 1

    def __add__(self, value):
        temp = CodeGenerator(indentation=self.indentation)
        temp.level = self.level
        temp.code = str(self) + ''.join([self.indentation for i in range(0, self.level)]) + str(value)
        return temp

    def __str__(self):
        return str(self.code)

a = CodeGenerator()
a += 'for a in range(1, 3):
'
a.indent()
a += 'for b in range(4, 6):
'
a.indent()
a += 'print(a * b)
'
a.dedent()
a += '# pointless comment
'
print(a)
Comment

PREVIOUS NEXT
Code Example
Python :: python MAX_INT 
Python :: double for in loop python 
Python :: fast api template syntax 
Python :: django import could not be resolved 
Python :: How to perform topological sort of a group of jobs, in Python? 
Python :: convert hex rgb to matplotlib color 
Python :: convert series to dataframe pandas 
Python :: attributes in python 
Python :: search and replace in python 
Python :: py array contains 
Python :: pandas sub dataframe 
Python :: docstring in python 
Python :: python calendar table view 
Python :: Data Structure tree in python 
Python :: how to count all files on linux 
Python :: negative slicing in python 
Python :: speech enhancement techniques 
Python :: how to make loops in python 
Python :: python power of e 
Python :: space complexity python 
Python :: include app in django project 
Python :: how to append data in django queryset 
Python :: fibonacci sequence 
Python :: shape of variable python 
Python :: typecasting python 
Python :: merge list elements python 
Python :: Python NumPy split Function Syntax 
Python :: python json 
Python :: python 2 
Python :: argparse one argument or without argument 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =