summaryrefslogtreecommitdiff
path: root/.emacs.d/snippets/python-mode.statnett
diff options
context:
space:
mode:
authorSimeon Simeonov2024-01-10 21:49:17 +0100
committerSimeon Simeonov2024-01-10 21:49:17 +0100
commitff160687915804b109fe626a5c7fbe949f227106 (patch)
tree9838ea050edf4288234a16206aecef7c9825c62f /.emacs.d/snippets/python-mode.statnett
parent7d08994f744685d6237ae3b4defde3eb2441fa42 (diff)
Update markdown-mode and yasnippet. Add some new Python snippets
Diffstat (limited to '.emacs.d/snippets/python-mode.statnett')
-rw-r--r--.emacs.d/snippets/python-mode.statnett/.yas-setup.el31
-rw-r--r--.emacs.d/snippets/python-mode.statnett/__call__7
-rw-r--r--.emacs.d/snippets/python-mode.statnett/__repr__7
-rw-r--r--.emacs.d/snippets/python-mode.statnett/__str__7
-rw-r--r--.emacs.d/snippets/python-mode.statnett/script3
-rw-r--r--.emacs.d/snippets/python-mode.statnett/with-open6
6 files changed, 47 insertions, 14 deletions
diff --git a/.emacs.d/snippets/python-mode.statnett/.yas-setup.el b/.emacs.d/snippets/python-mode.statnett/.yas-setup.el
index 76af532..8b29215 100644
--- a/.emacs.d/snippets/python-mode.statnett/.yas-setup.el
+++ b/.emacs.d/snippets/python-mode.statnett/.yas-setup.el
@@ -1,23 +1,33 @@
1(require 'yasnippet) 1(require 'yasnippet)
2(defvar yas-text) 2(defvar yas-text)
3 3
4(defvar python-split-arg-arg-regex
5"\\([[:alnum:]*]+\\)\\(:[[:blank:]]*[[:alpha:]]*\\)?\\([[:blank:]]*=[[:blank:]]*[[:alnum:]]*\\)?"
6"Regular expression matching an argument of a python function.
7First group should give the argument name.")
8
9(defvar python-split-arg-separator
10"[[:space:]]*,[[:space:]]*"
11"Regular expression matching the separator in a list of argument.")
12
4(defun python-split-args (arg-string) 13(defun python-split-args (arg-string)
5 "Split a python argument string into ((name, default)..) tuples" 14 "Split a python argument string ARG-STRING into a tuple of argument names."
6 (mapcar (lambda (x) 15 (mapcar (lambda (x)
7 (split-string x "[[:blank:]]*=[[:blank:]]*" t)) 16 (when (string-match python-split-arg-arg-regex x)
8 (split-string arg-string "[[:blank:]]*,[[:blank:]]*" t))) 17 (match-string-no-properties 1 x)))
18 (split-string arg-string python-split-arg-separator t)))
9 19
10(defun python-args-to-docstring () 20(defun python-args-to-docstring ()
11 "return docstring format for the python arguments in yas-text" 21 "Return docstring format for the python arguments in yas-text."
12 (let* ((indent (concat "\n" (make-string (current-column) 32))) 22 (let* ((indent (concat "\n" (make-string (current-column) 32)))
13 (args (python-split-args yas-text)) 23 (args (python-split-args yas-text))
14 (max-len (if args (apply 'max (mapcar (lambda (x) (length (nth 0 x))) args)) 0)) 24 (max-len (if args (apply 'max (mapcar (lambda (x) (length (nth 0 x))) args)) 0))
15 (formatted-args (mapconcat 25 (formatted-args (mapconcat
16 (lambda (x) 26 (lambda (x)
17 (concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- " 27 (concat (nth 0 x) (make-string (- max-len (length (nth 0 x))) ? ) " -- "
18 (if (nth 1 x) (concat "\(default " (nth 1 x) "\)")))) 28 (if (nth 1 x) (concat "\(default " (nth 1 x) "\)"))))
19 args 29 args
20 indent))) 30 indent)))
21 (unless (string= formatted-args "") 31 (unless (string= formatted-args "")
22 (mapconcat 'identity (list "Keyword Arguments:" formatted-args) indent)))) 32 (mapconcat 'identity (list "Keyword Arguments:" formatted-args) indent))))
23 33
@@ -33,6 +43,3 @@
33 (list "\nParameters\n----------" formatted-params 43 (list "\nParameters\n----------" formatted-params
34 "\nReturns\n-------" formatted-ret) 44 "\nReturns\n-------" formatted-ret)
35 "\n")))) 45 "\n"))))
36
37
38(add-hook 'python-mode-hook #'yasnippet-snippets--fixed-indent)
diff --git a/.emacs.d/snippets/python-mode.statnett/__call__ b/.emacs.d/snippets/python-mode.statnett/__call__
new file mode 100644
index 0000000..e807be5
--- /dev/null
+++ b/.emacs.d/snippets/python-mode.statnett/__call__
@@ -0,0 +1,7 @@
1# -*- mode: snippet -*-
2# name: __call__
3# key: _call
4# group: Special methods
5# --
6def __call__(self, ${1:*args}):
7 return $0
diff --git a/.emacs.d/snippets/python-mode.statnett/__repr__ b/.emacs.d/snippets/python-mode.statnett/__repr__
new file mode 100644
index 0000000..85cb80f
--- /dev/null
+++ b/.emacs.d/snippets/python-mode.statnett/__repr__
@@ -0,0 +1,7 @@
1# -*- mode: snippet -*-
2# name: __repr__
3# key: _repr
4# group: Special methods
5# --
6def __repr__(self):
7 return $0
diff --git a/.emacs.d/snippets/python-mode.statnett/__str__ b/.emacs.d/snippets/python-mode.statnett/__str__
new file mode 100644
index 0000000..f623df7
--- /dev/null
+++ b/.emacs.d/snippets/python-mode.statnett/__str__
@@ -0,0 +1,7 @@
1# -*- mode: snippet -*-
2# name: __str__
3# key: _str
4# group: Special methods
5# --
6def __str__(self):
7 return $0
diff --git a/.emacs.d/snippets/python-mode.statnett/script b/.emacs.d/snippets/python-mode.statnett/script
index 2e8cc35..4490e07 100644
--- a/.emacs.d/snippets/python-mode.statnett/script
+++ b/.emacs.d/snippets/python-mode.statnett/script
@@ -3,12 +3,11 @@
3# key: script 3# key: script
4# -- 4# --
5#!/usr/bin/env python 5#!/usr/bin/env python
6# -*- coding: utf-8 -*-
7 6
8 7
9def main(inargs=None): 8def main(inargs=None):
10 """main entry point""" 9 """main entry point"""
11 $0 10 $0
12 11
13if __name__ == '__main__': 12if __name__ == "__main__":
14 main() 13 main()
diff --git a/.emacs.d/snippets/python-mode.statnett/with-open b/.emacs.d/snippets/python-mode.statnett/with-open
new file mode 100644
index 0000000..77c7210
--- /dev/null
+++ b/.emacs.d/snippets/python-mode.statnett/with-open
@@ -0,0 +1,6 @@
1# -*- mode: snippet -*-
2# name: with-open
3# key: wo
4# --
5with open(${1:"filename"}${2:, encoding="${3:utf-8}"}${4:, mode="${5:w}"}) as ${6:myfile}:
6 $0 \ No newline at end of file