{"id":15400511,"url":"https://github.com/whisperity/py-singleline","last_synced_at":"2026-05-17T03:44:19.560Z","repository":{"id":62577011,"uuid":"338629503","full_name":"whisperity/py-singleline","owner":"whisperity","description":"Quick and dirty Python code execution in command-line pipes without manually dealing with newlines and indentation","archived":false,"fork":false,"pushed_at":"2023-09-21T07:44:04.000Z","size":98,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-02T02:27:54.811Z","etag":null,"topics":["command-line","command-line-tool","compiler-tool","pipe","pipeline","python","python-package","python-script","shell","single-line","single-line-input","transpiler","transpiler-for-casual-use"],"latest_commit_sha":null,"homepage":"http://pypi.org/project/py-singleline","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/whisperity.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2021-02-13T17:30:39.000Z","updated_at":"2023-09-21T07:44:49.000Z","dependencies_parsed_at":"2023-09-21T08:27:01.335Z","dependency_job_id":null,"html_url":"https://github.com/whisperity/py-singleline","commit_stats":null,"previous_names":["whisperity/py-singleline","whisperity/lpython"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whisperity%2Fpy-singleline","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whisperity%2Fpy-singleline/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whisperity%2Fpy-singleline/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/whisperity%2Fpy-singleline/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/whisperity","download_url":"https://codeload.github.com/whisperity/py-singleline/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245949254,"owners_count":20698911,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["command-line","command-line-tool","compiler-tool","pipe","pipeline","python","python-package","python-script","shell","single-line","single-line-input","transpiler","transpiler-for-casual-use"],"created_at":"2024-10-01T15:54:04.886Z","updated_at":"2025-10-21T06:28:01.711Z","avatar_url":"https://github.com/whisperity.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Arbitrarily complex single-line Python scripts\n==============================================\n\n**`pysln`** allows executing \"Python\"-ish scripts in the command line as parts of a pipeline.\n\n_Why?_ Normally, the Python binary allows executing Python script specified in the command-line, but you have to write a proper Python script:\n\n~~~~\npython3 -c 'import json\nprint(json.dumps({0: 1}))\n'\n~~~~\n\nThis may make shell history unreadable, hard to edit, etc.\n\nIn addition, accessing information in a pipe, common with most command-line tools, is convoluted.\nWhile you can easily say `some-command-generating-data | grep Foo | awk '{ print $2; }'`, doing a similar thing for data processing in Python is really hard, requiring you to open an editor, save a script file.\n\n**`pysln`** takes this need off for **quick and dirty** command-line data processing.\n\nInstallation\n------------\n\nInstall from [PyPI](http://pypi.org/project/pysln/).\nThe `pysln` entry point will be made available.\n\n~~~\n$ pip3 install pysln\n$ pysln -h\nusage: pysln [-h] [...]\n~~~\n\nOverview\n--------\n\nUse `pysln` just like you would use `sed` or `awk` in a pipe.\nAfter the optional flags, specify the code to execute:\n\n~~~~\n$ seq 1 5 | pysln 'print(int(LINE) * 2);'\n2\n4\n6\n8\n10\n~~~~\n\n_Py-SingleLine_ works by first *transcoding* the input code to real Python syntax, then *injecting* this input into a context, forming a full source code, and then running this code.\n\n### Optional arguments\n\nHelp about individual modes is printed if no code is specified:\n\n~~~~\n$ pysln -t lines\nHelp for the 'lines' mode ...\n~~~~\n\n * **`-n`**: Show the result of the transformed code, but do not execute.\n * **`-b`**: Show the result of the transformed and injected code, but do not execute.\n * **`-t XXX`**: use _`XXX`_ mode.\n\n### Passing command-line arguments\n\nCommand-line arguments to `pysln` can be passed to the running script with the **`-X`** optional argument.\nThe argument vector (list) of the invocation is available inside the script as `ARGS`.\n\n~~~~\n$ pysln -X \"username\" -X \"$(date)\" 'print(ARGS[1], ARGS[2])'\nusername \"Sun 14 Feb 2021 14:02:33\"\n~~~~\n\n\nUsage modes\n-----------\n\n### `bare` mode\n\nThe bare mode does not perform any pre-parsing or business logic.\nThis is the default mode when the standard input is a terminal, and not a pipe.\nThe variables `STDIN`, `STDOUT`, and `STDERR` alias `sys.stdin`, `sys.stdout`, and `sys.stderr`, respectively.\n\n### `lines` mode\n\nLines mode allows handling each line of the standard input.\nThis is the default mode if the standard input comes from a pipe.\nThe values are available through the `LINE` variable.\n\nThe functions `OUT` and `ERR` print the arguments verbatim to the standard output and error respectively, without adding an additional trailing newline.\n\n#### FizzBuzz\n\n~~~~\n$ seq 1 15 | pysln 'if int(LINE) % 15 == 0: print(\"Fizzbuzz\"); ' \\\n    'elif int(LINE) % 3 == 0: print(\"Fizz\");' \\\n    'elif int(LINE) % 5 == 0: print(\"Buzz\");' \\\n    'else: print(LINE); endif'\n1\n2\nFizz\n4\nBuzz\nFizz\n7\n8\nFizz\nBuzz\n11\nFizz\n13\n14\nFizzbuzz\n~~~~\n\n### `csv` mode\n\nThe input stream is loaded and parsed as a [CSV](http://docs.python.org/3.6/library/csv.html) file, with each row made available as `ROW`.\nThe `HEADER()` function returns `True` if the first row is in `ROW`.\nAfter the user's code is executed and the rows are transformed one by one, the resulting CSV is printed to the standard output.\n\n~~~~\n$ echo -e \"Foo,Bar,Baz\\n1,2,3\\n4,5,6\" | \\\n    pysln -t csv 'for idx, elem in enumerate(ROW): ' \\\n        'if not HEADER(): ROW[idx] = int(elem) * 10; endif; endfor;'\nFoo,Bar,Baz\n10,20,30\n40,50,60\n~~~~\n\n### `json` mode\n\nThe input stream is loaded and parsed as a [JSON](http://json.org), and made available as `DATA`.\nThis mode is aimed at implementing JSON filters and transformers.\nAfter the user's code executed, the JSON is formatted and printed to the standard output.\n\n#### JSON filter\n\n~~~~\n$ echo '[{\"Timezone\": \"Europe/London\"}, {\"Timezone\": \"America/New York\"}]' | \\\n    pysln -t json 'for rec in DATA: for k, v in dict(rec).items(): ' \\\n        'if k == \"Timezone\": split = v.split(\"/\"); ' \\\n        'rec[\"Country\"] = split[0]; rec[\"City\"] = split[1]; del rec[\"Timezone\"]; ' \\\n        'endif; endfor; endfor;'\n[{\"Country\": \"Europe\", \"City\": \"London\"}, {\"Country\": \"America\", \"City\": \"New York\"}]\n~~~~\n\nThe JSON mode also offers the `PRETTY()` function, which turns out formatted JSON output:\n\n~~~~\n$ echo '[{\"Timezone\": \"Europe/London\"}, {\"Timezone\": \"America/New York\"}]' | \\\n    pysln -t json 'for rec in DATA: for k, v in dict(rec).items(): ' \\\n        'if k == \"Timezone\": split = v.split(\"/\"); ' \\\n        'rec[\"Country\"] = split[0]; rec[\"City\"] = split[1]; del rec[\"Timezone\"]; ' \\\n        'endif; endfor; endfor; ' \\\n        'PRETTY();'\n[\n    {\n        \"City\": \"London\",\n        \"Country\": \"Europe\"\n    },\n    {\n        \"City\": \"New York\",\n        \"Country\": \"America\"\n    }\n]\n~~~~\n\n\nSyntax\n------\n\nThe code given to `pysln` is generally the same as normal Python code, except for a few key differences:\n\n * **Lines are terminated by `;` (semicolon)**, instead of a newline. Newlines still work, but the entire idea is to not deal with newlines.\n * Due to not dealing with newlines and whitespace, the indentation-based \"scoping\" is also side-stepped:\n    * Everything that would begin a scope and require indented code is instead closed with an `end___` keyword.\n    * For example: `if X: print(X); endif;`, `while True: pass; endwhile;`, `def identity(a): return a; enddef`.\n\nEverything else in-between is expected to behave as it would in Python.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhisperity%2Fpy-singleline","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwhisperity%2Fpy-singleline","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwhisperity%2Fpy-singleline/lists"}