posit-dev / positron

Positron, a next-generation data science IDE
https://positron.posit.co
Other
2.68k stars 80 forks source link

Python: Add basic snippets for Python #1437

Open petetronic opened 1 year ago

petetronic commented 1 year ago

RStudio included a small set of useful snippets when writing Python scripts. We should review those snippets and select the set that we think will be useful for Positron users.

petetronic commented 1 year ago

The raw set from RStudio to review is:

{
  "#!": {
    "prefix": "#!",
    "body": "#!/usr/bin/env python"
  },
  "imp": {
    "prefix": "imp",
    "body": "import ${1:module}"
  },
  "from": {
    "prefix": "from",
    "body": [
      "from ${1:package} import ${2:module}",
      "# Module Docstring"
    ]
  },
  "docs": {
    "prefix": "docs",
    "body": [
      "'''",
      "File: ${1:FILENAME:file_name}",
      "Author: ${2:author}",
      "Description: ${3}",
      "'''"
    ]
  },
  "wh": {
    "prefix": "wh",
    "body": [
      "while ${1:condition}:",
      "\t${2:# TODO: write code...}",
      "# dowh - does the same as do...while in other languages"
    ]
  },
  "dowh": {
    "prefix": "dowh",
    "body": [
      "while True:",
      "\t${1:# TODO: write code...}",
      "\tif ${2:condition}:",
      "\t\tbreak"
    ]
  },
  "with": {
    "prefix": "with",
    "body": [
      "with ${1:expr} as ${2:var}:",
      "\t${3:# TODO: write code...}",
      "# New Class"
    ]
  },
  "cl": {
    "prefix": "cl",
    "body": [
      "class ${1:ClassName}(${2:object}):",
      "\t\"\"\"${3:docstring for $1}\"\"\"",
      "\tdef __init__(self, ${4:arg}):",
      "\t\t${5:super($1, self).__init__()}",
      "\t\tself.$4 = $4",
      "\t\t${6}",
      "# New Function"
    ]
  },
  "def": {
    "prefix": "def",
    "body": [
      "def ${1:fname}(${2:`indent('.') ? 'self' : ''`}):",
      "\t\"\"\"${3:docstring for $1}\"\"\"",
      "\t${4:# TODO: write code...}"
    ]
  },
  "deff": {
    "prefix": "deff",
    "body": [
      "def ${1:fname}(${2:`indent('.') ? 'self' : ''`}):",
      "\t${3:# TODO: write code...}",
      "# New Method"
    ]
  },
  "defs": {
    "prefix": "defs",
    "body": [
      "def ${1:mname}(self, ${2:arg}):",
      "\t${3:# TODO: write code...}",
      "# New Property"
    ]
  },
  "property": {
    "prefix": "property",
    "body": [
      "def ${1:foo}():",
      "\tdoc = \"${2:The $1 property.}\"",
      "\tdef fget(self):",
      "\t\t${3:return self._$1}",
      "\tdef fset(self, value):",
      "\t\t${4:self._$1 = value}",
      "# Ifs"
    ]
  },
  "if": {
    "prefix": "if",
    "body": [
      "if ${1:condition}:",
      "\t${2:# TODO: write code...}"
    ]
  },
  "el": {
    "prefix": "el",
    "body": [
      "else:",
      "\t${1:# TODO: write code...}"
    ]
  },
  "ei": {
    "prefix": "ei",
    "body": [
      "elif ${1:condition}:",
      "\t${2:# TODO: write code...}",
      "# For"
    ]
  },
  "for": {
    "prefix": "for",
    "body": [
      "for ${1:item} in ${2:items}:",
      "\t${3:# TODO: write code...}",
      "# Encodes"
    ]
  },
  "cutf8": {
    "prefix": "cutf8",
    "body": "# -*- coding: utf-8 -*-"
  },
  "clatin1": {
    "prefix": "clatin1",
    "body": "# -*- coding: latin-1 -*-"
  },
  "cascii": {
    "prefix": "cascii",
    "body": [
      "# -*- coding: ascii -*-",
      "# Lambda"
    ]
  },
  "ld": {
    "prefix": "ld",
    "body": "${1:var} = lambda ${2:vars} : ${3:action}"
  },
  ".": {
    "prefix": ".",
    "body": "self."
  },
  "try": {
    "prefix": "try",
    "body": [
      "try:",
      "\t${1:# TODO: write code...}",
      "except ${2:Exception}, ${3:e}:",
      "\t${4:raise $3}"
    ]
  },
  "try.1": {
    "prefix": "try",
    "body": [
      "try:",
      "\t${1:# TODO: write code...}",
      "except ${2:Exception}, ${3:e}:",
      "\t${4:raise $3}",
      "else:",
      "\t${5:# TODO: write code...}"
    ]
  },
  "try.2": {
    "prefix": "try",
    "body": [
      "try:",
      "\t${1:# TODO: write code...}",
      "except ${2:Exception}, ${3:e}:",
      "\t${4:raise $3}",
      "finally:",
      "\t${5:# TODO: write code...}"
    ]
  },
  "try.3": {
    "prefix": "try",
    "body": [
      "try:",
      "\t${1:# TODO: write code...}",
      "except ${2:Exception}, ${3:e}:",
      "\t${4:raise $3}",
      "else:",
      "\t${5:# TODO: write code...}",
      "finally:",
      "\t${6:# TODO: write code...}",
      "# if __name__ == '__main__':"
    ]
  },
  "ifmain": {
    "prefix": "ifmain",
    "body": [
      "if __name__ == '__main__':",
      "\t${1:main()}",
      "# __magic__"
    ]
  },
  "_": {
    "prefix": "_",
    "body": [
      "__${1:init}__${2}",
      "# python debugger (pdb)"
    ]
  },
  "pdb": {
    "prefix": "pdb",
    "body": [
      "import pdb; pdb.set_trace()",
      "# ipython debugger (ipdb)"
    ]
  },
  "ipdb": {
    "prefix": "ipdb",
    "body": [
      "import ipdb; ipdb.set_trace()",
      "# ipython debugger (pdbbb)"
    ]
  },
  "pdbbb": {
    "prefix": "pdbbb",
    "body": "import pdbpp; pdbpp.set_trace()"
  },
  "pprint": {
    "prefix": "pprint",
    "body": "import pprint; pprint.pprint(${1})${2}"
  },
  "\"": {
    "prefix": "\"",
    "body": [
      "\"\"\"",
      "${1:doc}",
      "\"\"\"",
      "# test function/method"
    ]
  },
  "test": {
    "prefix": "test",
    "body": [
      "def test_${1:description}(${2:self}):",
      "\t${3:# TODO: write code...}",
      "# test case"
    ]
  },
  "testcase": {
    "prefix": "testcase",
    "body": [
      "class ${1:ExampleCase}(unittest.TestCase):",
      "\t",
      "\tdef test_${2:description}(self):",
      "\t\t${3:# TODO: write code...}"
    ]
  },
  "fut": {
    "prefix": "fut",
    "body": [
      "from __future__ import ${1}",
      "#getopt"
    ]
  },
  "getopt": {
    "prefix": "getopt",
    "body": [
      "try:",
      "\t# Short option syntax: \"hv:\"",
      "\t# Long option syntax: \"help\" or \"verbose=\"",
      "\topts, args = getopt.getopt(sys.argv[1:], \"${1:short_options}\", [${2:long_options}])",
      "",
      "except getopt.GetoptError, err:",
      "\t# Print debug info",
      "\tprint str(err)",
      "\t${3:error_action}",
      "",
      "for option, argument in opts:",
      "\tif option in (\"-h\", \"--help\"):",
      "\t\t${4}",
      "\telif option in (\"-v\", \"--verbose\"):",
      "\t\tverbose = argument"
    ]
  }
}
petetronic commented 1 year ago

Currently, #! won't suggest completions as it is in a comment.

jthomasmock commented 1 year ago

There are some from an extension as well, but they're probably not that interesting for DataSci: https://github.com/cstrap/python-snippets/tree/master/snippets

jthomasmock commented 1 year ago

x-posting the equivalent request for R: https://github.com/posit-dev/positron/issues/1438

kevinushey commented 1 year ago

IIRC the snippets from RStudio were just the default Python snippets we inherited from Ace. IMHO we should start over and choose only the snippets we think will be universally relevant / helpful.