A general purpose template driven code generator

Overview

💩 Shit Mountain Generator

A general purpose template driven code generator
Contribute shits to your company's shit mountain more efficiently!

Quick Start

Install

pip3 install shit_mountain_generator

vscode Template Language Highlighting

The vscode extension for shit template language highlight support is now on marketplace! search for Shit Template Language in vscode extension tab to install

Create a generator script

from ShitMountainGenerator import Shitter

shitter = Shitter.from_template(path="/path/to/template.tmpl")
shits = shitter.shit(context={"your": "variable", "loop_contexts": [{"loop": "context"}, {"and": "more"}]})
print(shits)

Writing Templates

Currently, templates has the following supports:

  • variable
  • loop
  • Sub-template
  • Condition (Select-Case)

So, we will be starting with the main template:

your template content ">
<tmpl name="$main$">
  your template content
tmpl>

The name attribute can be omitted for the main template

Just relax and use indent to make it look neat. The first indent will be removed when parsing. However, please note that you should only use tab character, AKA \t, or 4 spaces to indent since the parser is assuming 4 spaces or a tab as one indent.

Now, maybe we need to replace some parts of the template with variables. So, we are going to use the variable syntax:

email = "{{ user_email }}" ">
<tmpl name="$main$">
  email = "{{ user_email }}"
tmpl>

{{ user_email }} represents the content of the variable user_email, which will be replaced later.

So, we have gone so far that we are able to generate some super simple codes.

Since the complexity of our business logic, we may need to loop over some list and generate corresponding code. And here is when sub-templates and loop operation come in.

Imagine that we are converting some data structure from a language to another, for instance, a go struct, to python data class. And we have the following structure:

type User struct {
  Id       string   `json:"id"`
  Name     string   `json:"name"`
  Password string   `json:"name"`
  Email    string   `json:"email"`
  Friends  []string `json:"friends"`
}

The above struct is exported and loaded into a parsing function of python, and we are getting the following result

context = {
  "struct_name": "User",
  "members": [
    {"name": "Id", "py_name": "id", "py_type": "str", "is_list": False},
    {"name": "Name", "py_name": "name", "py_type": "str", "is_list": False},
    {"name": "Password", "py_name": "password", "py_type": "str", "is_list": False},
    {"name": "Email", "py_name": "email", "py_type": "str", "is_list": False},
    {"name": "Friends", "py_name": "friends", "py_type": "List[str]", "is_list": True},
  ]
}

As you may noticed that we have mentioned about context in the quick-start section above. Right! We are putting this dict into the Shitter so that it shits the correct shit.

Now, we looking at some more complicated stuff. There are list of class members to generate and we apparent don't want to copy and paste these lines. So, we are gonna use the loop operation. But wait! We have got some variant for the "is_list" key! Luckily, we have condition rendering. Let's put these stuff together. And we will have the...

Complete Template Sample

[] "" {{ name }}: {{ py_type }} = {{ use(class_member_select) }} class {{ struct_name }}: {% class_member_tmpl <- members %} ">
<select name="class_member_select" var="is_list">
  <case test="True">
    []
  case>
  <case test="False">
  	""
  case>
select>

<tmpl name="class_member_tmpl">
  {{ name }}: {{ py_type }} = {{ use(class_member_select) }}
tmpl>

<tmpl name="$main$">
  class {{ struct_name }}:
  	{% class_member_tmpl <- members %}

In the above template, we have two sub-templates, class_member_tmpl and the main template and one select statement.

A select statement requires a name attribute to be referenced later in a template and a var attribute to specify which variable we are testing with. Inside the select statement, we can have unlimited numbers of case statements and one optional default statement. However, only the first matched case will be returned if we have more than one matching cases.

In the case statement, a test attribute is required. When comparing the variable and the test value, the test value will be automatically type converted to the target variable type. If a failure accurs while convering, it is assumed unequal.

If none of the specified cases are matched, the generator will go look for the default statement and an Exception will be thrown out if no defaults were found.

A select statement is referenced using the use() function with the select statement's name as argument

Sub-templates are used for loops. Use a {% %} tag to tell the generator to loop. The left side of the arrow is the sub-template to use and the right side is the context to loop over. The specified context must be an iterable and there must be Dict[str, Any] inside the context.

The sub-template is automatically fed with items of the list as context. So just go and use the variables inside that item.

LICENSE

This project is licenced under The MIT License

Feel free to use it for whatever you are working with

You might also like...
a general-purpose Transformer based vision backbone

Swin Transformer By Ze Liu*, Yutong Lin*, Yue Cao*, Han Hu*, Yixuan Wei, Zheng Zhang, Stephen Lin and Baining Guo. This repo is the official implement

ArrayFire: a general purpose GPU library.
ArrayFire: a general purpose GPU library.

ArrayFire is a general-purpose library that simplifies the process of developing software that targets parallel and massively-parallel architectures i

General purpose GPU compute framework for cross vendor graphics cards (AMD, Qualcomm, NVIDIA & friends). Blazing fast, mobile-enabled, asynchronous and optimized for advanced GPU data processing usecases.
General purpose GPU compute framework for cross vendor graphics cards (AMD, Qualcomm, NVIDIA & friends). Blazing fast, mobile-enabled, asynchronous and optimized for advanced GPU data processing usecases.

Vulkan Kompute The general purpose GPU compute framework for cross vendor graphics cards (AMD, Qualcomm, NVIDIA & friends). Blazing fast, mobile-enabl

Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything.

Retrying Retrying is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just

BYOL for Audio: Self-Supervised Learning for General-Purpose Audio Representation
BYOL for Audio: Self-Supervised Learning for General-Purpose Audio Representation

BYOL for Audio: Self-Supervised Learning for General-Purpose Audio Representation This is a demo implementation of BYOL for Audio (BYOL-A), a self-sup

Learning General Purpose Distributed Sentence Representations via Large Scale Multi-task Learning

GenSen Learning General Purpose Distributed Sentence Representations via Large Scale Multi-task Learning Sandeep Subramanian, Adam Trischler, Yoshua B

A general-purpose multi-agent training framework.

MALib A general-purpose multi-agent training framework. Installation step1: build environment conda create -n malib python==3.7 -y conda activate mali

ImVoxelNet: Image to Voxels Projection for Monocular and Multi-View General-Purpose 3D Object Detection
ImVoxelNet: Image to Voxels Projection for Monocular and Multi-View General-Purpose 3D Object Detection

ImVoxelNet: Image to Voxels Projection for Monocular and Multi-View General-Purpose 3D Object Detection This repository contains implementation of the

A task-agnostic vision-language architecture as a step towards General Purpose Vision
A task-agnostic vision-language architecture as a step towards General Purpose Vision

Towards General Purpose Vision Systems By Tanmay Gupta, Amita Kamath, Aniruddha Kembhavi, and Derek Hoiem Overview Welcome to the official code base f

ZSL-KG is a general-purpose zero-shot learning framework with a novel transformer graph convolutional network (TrGCN) to learn class representation from common sense knowledge graphs.
ZSL-KG is a general-purpose zero-shot learning framework with a novel transformer graph convolutional network (TrGCN) to learn class representation from common sense knowledge graphs.

ZSL-KG is a general-purpose zero-shot learning framework with a novel transformer graph convolutional network (TrGCN) to learn class representa

A general-purpose, flexible, and easy-to-use simulator alongside an OpenAI Gym trading environment for MetaTrader 5 trading platform (Approved by OpenAI Gym)
A general-purpose, flexible, and easy-to-use simulator alongside an OpenAI Gym trading environment for MetaTrader 5 trading platform (Approved by OpenAI Gym)

gym-mtsim: OpenAI Gym - MetaTrader 5 Simulator MtSim is a simulator for the MetaTrader 5 trading platform alongside an OpenAI Gym environment for rein

Mathics is a general-purpose computer algebra system (CAS). It is an open-source alternative to Mathematica
Mathics is a general-purpose computer algebra system (CAS). It is an open-source alternative to Mathematica

Mathics is a general-purpose computer algebra system (CAS). It is an open-source alternative to Mathematica. It is free both as in "free beer" and as in "freedom".

A repository containing several general purpose Python scripts to automate daily and common tasks.

General Purpose Scripts Introduction This repository holds a curated list of Python scripts which aim to help us automate daily and common tasks. You

Unofficial PyTorch implementation of MobileViT based on paper
Unofficial PyTorch implementation of MobileViT based on paper "MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer".

MobileViT RegNet Unofficial PyTorch implementation of MobileViT based on paper MOBILEVIT: LIGHT-WEIGHT, GENERAL-PURPOSE, AND MOBILE-FRIENDLY VISION TR

Neural text generators like the GPT models promise a general-purpose means of manipulating texts.

Boolean Prompting for Neural Text Generators Neural text generators like the GPT models promise a general-purpose means of manipulating texts. These m

General purpose GPU compute framework for cross vendor graphics cards (AMD, Qualcomm, NVIDIA & friends)
General purpose GPU compute framework for cross vendor graphics cards (AMD, Qualcomm, NVIDIA & friends)

General purpose GPU compute framework for cross vendor graphics cards (AMD, Qualcomm, NVIDIA & friends). Blazing fast, mobile-enabled, asynchronous and optimized for advanced GPU data processing usecases. Backed by the Linux Foundation.

Sodium is a general purpose programming language which is instruction-oriented
Sodium is a general purpose programming language which is instruction-oriented

Sodium is a general purpose programming language which is instruction-oriented (a new programming concept that we are developing and devising)

General Purpose Python Library by Techman

General Purpose Python Library by Techman

PyFlow is a general purpose visual scripting framework for python
PyFlow is a general purpose visual scripting framework for python

PyFlow is a general purpose visual scripting framework for python. State Base structure of program implemented, such things as packages disco

Releases(1.0.0)
Owner
Kelly
A frontend dev with the knowledge of backend / fullstack dev in common words... likes: Dart | VB.NET | Python | Typescript hates: Java | Javascript
Kelly
A simple, elegant Python based web templating engine (part of web.py).

Templator Simple, elegant Python based web templating (part of web.py). If you are familiar with Python, there is no new syntax to learn. This is a st

Dan 1 Dec 13, 2021
✈️ HTML Template engine for python. Supports XSS preventation and many more!

Htmotor HTML Template Engine for Python! Installation: Open your terminal and type pip install htmotor.

Penguen 3 Nov 06, 2022
A string template language hosted by Python3 runtime

A string template language hosted by Python3 runtime. Conventionally, the source code of this language is written in plain text with utf-8 encoding and stored in a file with extension ".meme".

6 Nov 02, 2022
Template Render Engine

Template Render Engine Why TRender? It is just another template render engine so why should one choose TRender? TRender was originally created for Sir

Cesbit 18 Jul 30, 2022
Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks.

Jinja Partials Simple reuse of partial HTML page templates in the Jinja template language for Python web frameworks. (There is also a Pyramid/Chameleo

Michael Kennedy 106 Dec 28, 2022
Use a docx as a jinja2 template

Use a docx as a jinja2 template

Eric Lapouyade 1.4k Jan 02, 2023
A general purpose template driven code generator

💩 Shit Mountain Generator A general purpose template driven code generator Contribute shits to your company's shit mountain more efficiently! Quick S

Kelly 14 Mar 09, 2022
HTML Template Linter and Formatter. Use with Django, Jinja, Nunjucks and Handlebars templates.

Find common formatting issues and reformat HTML templates. Django · Jinja · Nunjucks · Handlebars · Mustache · GoLang Ps, --check it out on other temp

Riverside Healthcare Analytics 263 Jan 01, 2023
Fast HTML/XML template engine for Python

Overview Chameleon is an HTML/XML template engine for Python. It uses the page templates language. You can use it in any Python web application with j

Malthe Borch 151 Dec 22, 2022
Mako Templates for Python

Mako Templates for Python Mako is a template library written in Python. It provides a familiar, non-XML syntax which compiles into Python modules for

mike bayer 173 Dec 22, 2022
Mako Templates for Python

Mako Templates for Python Mako is a template library written in Python. It provides a familiar, non-XML syntax which compiles into Python modules for

SQLAlchemy 233 Dec 21, 2022