Best Python code snippet using behave
json.format.py
Source:json.format.py  
...60        outfile.write(contents2)61        outfile.close()62        console.warn("%s OK", message)63        return 1 #< OK64def json_formatall(filenames, indent=DEFAULT_INDENT_SIZE, dry_run=False):65    """66    Format/Beautify a JSON file.67    :param filenames:  Format one or more JSON files.68    :param indent:     Number of chars to indent per level (default: 4).69    :returns:  0, if successful. Otherwise, number of errors.70    """71    errors = 072    console = logging.getLogger("console")73    for filename in filenames:74        try:75            result = json_format(filename, indent=indent, console=console,76                                 dry_run=dry_run)77            if not result:78                errors += 179#        except json.decoder.JSONDecodeError, e:80#            console.error("ERROR: %s (filename: %s)", e, filename)81#            errors += 182        except Exception as e:83            console.error("ERROR %s: %s (filename: %s)",84                          e.__class__.__name__, e, filename)85            errors += 186    return errors87# ----------------------------------------------------------------------------88# MAIN FUNCTION:89# ----------------------------------------------------------------------------90def main(args=None):91    """Boilerplate for this script."""92    if args is None:93        args = sys.argv[1:]94    usage_ = """%prog [OPTIONS] JsonFile [MoreJsonFiles...]95Format/Beautify one or more JSON file(s)."""96    parser = OptionParser(usage=usage_, version=VERSION)97    parser.add_option("-i", "--indent", dest="indent_size",98                default=DEFAULT_INDENT_SIZE, type="int",99                help="Indent size to use (default: %default).")100    parser.add_option("-c", "--compact", dest="compact",101                action="store_true", default=False,102                help="Use compact format (default: %default).")103    parser.add_option("-n", "--dry-run", dest="dry_run",104                action="store_true", default=False,105                help="Check only if JSON is well-formed (default: %default).")106    options, filenames = parser.parse_args(args)    #< pylint: disable=W0612107    if not filenames:108        parser.error("OOPS, no filenames provided.")109    if options.compact:110        options.indent_size = None111    # -- STEP: Init logging subsystem.112    format_ = "json.format: %(message)s"113    logging.basicConfig(level=logging.WARN, format=format_)114    console = logging.getLogger("console")115    # -- DOS-SHELL SUPPORT: Perform filename globbing w/ wildcards.116    skipped = 0117    filenames2 = []118    for filename in filenames:119        if "*" in filenames:120            files = glob.glob(filename)121            filenames2.extend(files)122        elif os.path.isdir(filename):123            # -- CONVENIENCE-SHORTCUT: Use DIR as shortcut for JSON files.124            files = glob.glob(os.path.join(filename, "*.json"))125            filenames2.extend(files)126            if not files:127                console.info("SKIP %s, no JSON files found in dir.", filename)128                skipped += 1129        elif not os.path.exists(filename):130            console.warn("SKIP %s, file not found.", filename)131            skipped += 1132            continue133        else:134            assert os.path.exists(filename)135            filenames2.append(filename)136    filenames = filenames2137    # -- NORMAL PROCESSING:138    errors  = json_formatall(filenames, options.indent_size,139                             dry_run=options.dry_run)140    console.error("Processed %d files (%d with errors, skipped=%d).",141                  len(filenames), errors, skipped)142    if not filenames:143        errors += 1144    return errors145# ----------------------------------------------------------------------------146# AUTO-MAIN:147# ----------------------------------------------------------------------------148if __name__ == "__main__":...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
