Skip to content

Instantly share code, notes, and snippets.

@thehappydinoa
Last active October 22, 2019 22:12
Show Gist options
  • Save thehappydinoa/f84324c15d2fd0c6e2c16476490cb7dd to your computer and use it in GitHub Desktop.
Save thehappydinoa/f84324c15d2fd0c6e2c16476490cb7dd to your computer and use it in GitHub Desktop.
Basically saves me so much time re-arranging all the simulated circuit devices to look nice

Simcir Rearranger

Usage

usage: simcir_rearranger.py [-h] JSON_FILE

SHOW ME WHAT YOU GOT

positional arguments:
  JSON_FILE   the path to the json file

optional arguments:
  -h, --help  show this help message and exit

Screenshot

screenshot

#!/usr/bin/env python
# coding: utf-8
"""
Simulated Circuit Rearranger
Basically saves me so much time rearranging all the devices to look nice
usage: simcir_rearranger.py [-h] JSON_FILE
SHOW ME WHAT YOU GOT
positional arguments:
JSON_FILE the path to the json file
optional arguments:
-h, --help show this help message and exit
"""
import argparse
import json
import os
try:
# pylint: disable-msg=W0622,C0103
input = raw_input
except NameError:
pass
TITLE = "Simulated Circuit"
DESCRIPTION = ""
HTML = """<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<script type="text/javascript" src="jquery-2.1.1.min.js"></script>
<script type="text/javascript" src="simcir.js"></script>
<link rel="stylesheet" type="text/css" href="simcir.css" />
<script type="text/javascript" src="simcir-basicset.js"></script>
<link rel="stylesheet" type="text/css" href="simcir-basicset.css" />
<script type="text/javascript" src="simcir-library.js"></script>
<title>{title}</title>
</head>
<body>
<h1>{title}</h1>
<p>{description}</p>
<div class="simcir">
{simcir}
</div>
</body>
</html>"""
def copy_to_clipboard(string):
"""Copies to clipboard in *nix"""
os.system("echo '%s' | pbcopy" % string)
def make_number_normal(integer, base=25, make_bigger=0):
"""Makes number normal as they should be"""
integer += make_bigger
return int(base * round(integer / base))
def rearranger(simcir_json, base=25, html=True, title=TITLE, description=DESCRIPTION):
"""Transforms json values"""
# Loads json from input
# simcir_input = input("[?] SHOW ME WHAT YOU GOT: ")
# simcir_json = json.loads(simcir_input)
# Loads json from file
simcir_json = json.load(open("simcir.json", "r"))
# Get devices
devices = simcir_json.get("devices")
# Initializes lists
x_values = list()
y_values = list()
new_devices = list()
for device in devices:
# Mades duplicate of old device so I can modify values
new_device = device
# Gets values from new device
new_x = new_device.get("x")
new_y = new_device.get("y")
# Keeps values for max
x_values.append(new_x)
y_values.append(new_y)
# Updates values
new_device["x"] = new_x
new_device["y"] = new_y
# Adds new device to list
new_devices.append(new_device)
# Updates devices with new normal values
simcir_json["devices"] = new_devices
# Finds the max values and sets width and height it should be
simcir_json["width"] = make_number_normal(
max(x_values), base=base, make_bigger=base * 4)
simcir_json["height"] = make_number_normal(
max(y_values), base=base, make_bigger=base * 4)
# Hides toolbox
simcir_json["showToolbox"] = False
# Prints width and height to user
print("[UwU] I got the width of {} and the height of {} ♥w♥".format(
simcir_json.get("width"), simcir_json.get("height")))
# Dumps json to legible garbage
json_dump = json.dumps(simcir_json)
# Sets results to json
results = json_dump
if html:
# Sets results to html
results = HTML.format(
title=title, description=description, simcir=json_dump)
return results
def main():
"""Loads and transforms json values """
# Loads json from input
# simcir_input = input("[?] SHOW ME WHAT YOU GOT: ")
# simcir_json = json.loads(simcir_input)
# Parses args
parser = argparse.ArgumentParser(description="SHOW ME WHAT YOU GOT")
parser.add_argument("json", metavar="JSON_FILE",
help="the path to the json file")
args = parser.parse_args()
# Loads json from file
with open(args.json) as json_file:
simcir_json = json.load(json_file)
# Fixes json
fixed = rearranger(simcir_json)
# Prints html file to user
print("[!] I LIKE WHAT YOU GOT. GOOD JOB.\n\n\n")
# Prints results
print(fixed)
# Copies results
copy_to_clipboard(fixed)
# Prints html file to user
print("\n\n\n[!] BUT IN THE END, THERE CAN ONLY BE ONE.")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment