Package: pytagged
pytagged: Auto Comment CLIWhat is it?pytagged is a simple CLI utlity written in python that helps you comment out "tagged" code. For a simple example, this might be a common pattern in your code.defproduction_code():whileTrue:expensive_debug_code()# debugprod_code()While this fine for most cases, it's a wasted instruction/call every iteration of the loop,
and this can get expensive fast. With pytagged you can do thispytag production.py -t debug, and the above code would become:defproduction_code():whileTrue:# expensive_debug_code()  # debugprod_code()Fairly straight forward, just comment out lines that end with a "tag", in this case:
'# debug'. pytagged can also do this with "tagged blocks", turning this:defproduction_code():whileTrue:# block: debugexpensive_debug_code_1()expensive_debug_code_2()...expensive_debug_code_n()# endprod_code()Into this:defproduction_code():whileTrue:# block: debug# expensive_debug_code_1()# expensive_debug_code_2()# ...# expensive_debug_code_n()# endprod_code()While these example are fairly trivial, pytagged is flexible and lets you define your own "tags" to support more complex use cases.Usage & exampleInstallationSimply use:pip install pytagged, or you can clone this directory and install pytagged directly by runningsetup.py.Note that the source code (dev version) is a little different from the pypi distribution. Functionally, they're the same, the dev version just prints out more stuffs for my debugging purposes.Multiple filespytagged accepts its first argument as a path, if the path is a directory, it recursively
scans for all '.py' files within that directory (and the subdirectories). By default, pytagged works on the cwd. So if your project looks like this:.
├── requirements.txt
├── src
│   ├── __init__.py
│   └── main.py
└── test
    ├── test.dat
    └── test_main.pyRunningpytag -t debugwill affect the .py files in./src/and in./test.Multiple tagsPytaggd accepts multiple tags. You just need to specify them using the -t flag. For example, runningpytag ./hello.py -t debug skip slowon a file like this.if__name__=="__main__":print("Hello world")print("Hello debug world")# debugprint("Hello skip world")# skipprint("hello slow world")# slowassert0# debug# block: slowassert__debug__# skipwhileTrue:print("Debug")1+12+2s="somestr"# end# block: skipassert1# endwill turn it to:if__name__=="__main__":print("Hello world")# print("Hello debug world")  # debug# print("Hello skip world")  # skip# print("hello slow world")  # slow# assert 0    # debug# block: slow# assert __debug__    # skip# while True:# print("Debug")# 1 + 1# 2 + 2# s = "somestr"# end# block: skip# assert 1# endBehaviorspytagged ignores certain cases to avoid any unecessary modifications.Already commented linespytagged ignores already commented lines, even if they are inlined tagged or block tagged. Take this code.defsome_fn():# print("already commented ") # slow# print("same") # debugprint("debug")# debugThe second and third lines will be ignored, even if you runpytag file.py -t debug slow,
resulting in:defsome_fn():# print("already commented ") # slow# print("same") # debug# print("debug")    # debugOpened blocksAn opened block starts with# block: tagbut does not have a closing# end. pytagged
ignores cases like this. For example, runningpytag file.py -t debugon this:defopened_block():# block: debugprint("This is an opened block")print("Inlined tag, this would still work")# debug# block: debugprint("This is a proper block")print("Below is the end block tag")# endreturn0would result in:defopened_block():# block: debugprint("This is an opened block")# print("Inlined tag, this would still work")     # debug# block: debug# print("This is a proper block")# print("Below is the end block tag")# endreturn0Triple quoted string/docstringTriple quotes are either'''or"""Triple quoted block, that is a block whose line starts with"""and ends with a line
with only"""is ignored.
Lines that have triple quoted strings in them, but do not start with"""can be commented
out with block tags, but not inlined tags.Runningpytag file.py -t debugon:deffn_with_docstring():'''This should not be commented out         # debugwe don't do anything in a triple quote      # debugblock if the start of that block is the     # debugstart of a line.                            # debug'''some_str="this should be commented out normally"# debug# block: debugtriple_quote_str="""This however would be commented outif it's block tagged"""# endtriple_quote_str="""But triple quote strings can't be commented   # debugout using inline tags"""# block: debug"""This should not be commented out, even if it's block tagged"""# endreturntriple_quote_strwould result in:deffn_with_docstring():'''This should not be commented out         # debugwe don't do anything in a triple quote      # debugblock if the start of that block is the     # debugstart of a line.                            # debug'''# some_str = "this should be commented out normally"  # debug# block: debug# triple_quote_str = """This however would be commented out# if it's block tagged"""# endtriple_quote_str="""But triple quote strings can't be commented   # debugout using inline tags"""# block: debug"""This should not be commented out, even if it's block tagged"""# endreturntriple_quote_strMore advanced usageExclude patternsBy default, pytagged ignore files/directories that match against any of these patterns:".svn", "CVS", ".bzr", ".hg", ".git", "__pycache__", ".tox", ".eggs", "*.egg".You can override this through the -x/--exclude flag. For example:pytag . -t debug -x envwill ignore any files  or directories (along with all the subdirectories and files in them) with the name 'env'. This is useful for things like ignoring python packages in a virtual environment.Another option is to use the -xt/--extend-exclude flag to add more patterns to the excluded patterns like thispytag . -t debug --xt env. This will add 'env' to the default excluded patterns. If you use both  -x and --xt at the same time, the resulting excluded patterns will be the union set of the two.Config fileEvery command line flag (except for -cf/--config) can be configured in a .ini format compliant file like this:[pytagged]

path = pytagged
tags =
    debug,
    develop
# tags = debug, develop     this also works
exclude = env, test*To tell pytagged to use the config file you can use the -cf/--config flag:pytag -cf [config_path], or you just usepytagand name your file 'pytagged.ini' and place it at the current directory, which is the default location that pytagged looks for the config file when it's run with no command line arguments. Note that you have to have the options declared under thepytaggedheader/session as in the example, as pytagged will only read configurations from there.You can also use command line arguments in combination with a config file: take the file 'pytagged.ini' from the previous example, and:pytag--configpytagged.ini-xtbuildRunning this is equivalent to:pytagpytagged-tdebugdevelop-xenvtest*-xtbuildIn this scenario, thepath, tags, excludeoptions are provided by the config file, while the-xtflag from the command line provides argument for theextend-excludeoption. Note that the arguments from the cli take precedence over options from the config file.By default, if no config path is provided, pytagged looks for the defaultpytagged.inifile. If one exists, pytagged will use the options provided there as a base and update/override them with options from the command line as appropriate.MiscModesThere are 3 modes that pytagged runs in:Default: No outputPrintonly: does NOT modify files, but instead print the raw string output of what the modified files would beBenchmark: Performs a benchmark of n runs (defaults to 100, configurable through cli or config file), and prints out performance statistics of the phases in processing the files.Note: You can also use the -v/--verbose flag to print out some more info.