scripts/CodeMirror/mode/python/index.html (view raw)
1<!doctype html>
2
3<title>CodeMirror: Python mode</title>
4<meta charset="utf-8"/>
5<link rel=stylesheet href="../../doc/docs.css">
6
7<link rel="stylesheet" href="../../lib/codemirror.css">
8<script src="../../lib/codemirror.js"></script>
9<script src="../../addon/edit/matchbrackets.js"></script>
10<script src="python.js"></script>
11<style>.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
12<div id=nav>
13 <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png" alt=""></a>
14
15 <ul>
16 <li><a href="../../index.html">Home</a>
17 <li><a href="../../doc/manual.html">Manual</a>
18 <li><a href="https://github.com/codemirror/codemirror">Code</a>
19 </ul>
20 <ul>
21 <li><a href="../index.html">Language modes</a>
22 <li><a class=active href="#">Python</a>
23 </ul>
24</div>
25
26<article>
27<h2>Python mode</h2>
28
29 <div><textarea id="code" name="code">
30# Literals
311234
320.0e101
33.123
340b01010011100
350o01234567
360x0987654321abcdef
377
382147483647
393L
4079228162514264337593543950336L
410x100000000L
4279228162514264337593543950336
430xdeadbeef
443.14j
4510.j
4610j
47.001j
481e100j
493.14e-10j
50
51
52# String Literals
53'For\''
54"God\""
55"""so loved
56the world"""
57'''that he gave
58his only begotten\' '''
59'that whosoever believeth \
60in him'
61''
62
63# Identifiers
64__a__
65a.b
66a.b.c
67
68#Unicode identifiers on Python3
69# a = x\ddot
70a⃗ = ẍ
71# a = v\dot
72a⃗ = v̇
73
74#F\vec = m \cdot a\vec
75F⃗ = m•a⃗
76
77# Operators
78+ - * / % & | ^ ~ < >
79== != <= >= <> << >> // **
80and or not in is
81
82#infix matrix multiplication operator (PEP 465)
83A @ B
84
85# Delimiters
86() [] {} , : ` = ; @ . # Note that @ and . require the proper context on Python 2.
87+= -= *= /= %= &= |= ^=
88//= >>= <<= **=
89
90# Keywords
91as assert break class continue def del elif else except
92finally for from global if import lambda pass raise
93return try while with yield
94
95# Python 2 Keywords (otherwise Identifiers)
96exec print
97
98# Python 3 Keywords (otherwise Identifiers)
99nonlocal
100
101# Types
102bool classmethod complex dict enumerate float frozenset int list object
103property reversed set slice staticmethod str super tuple type
104
105# Python 2 Types (otherwise Identifiers)
106basestring buffer file long unicode xrange
107
108# Python 3 Types (otherwise Identifiers)
109bytearray bytes filter map memoryview open range zip
110
111# Some Example code
112import os
113from package import ParentClass
114
115@nonsenseDecorator
116def doesNothing():
117 pass
118
119class ExampleClass(ParentClass):
120 @staticmethod
121 def example(inputStr):
122 a = list(inputStr)
123 a.reverse()
124 return ''.join(a)
125
126 def __init__(self, mixin = 'Hello'):
127 self.mixin = mixin
128
129# Python 3.6 f-strings (https://www.python.org/dev/peps/pep-0498/)
130f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
131f'He said his name is {name!r}.'
132f"""He said his name is {name!r}."""
133f'{"quoted string"}'
134f'{{ {4*10} }}'
135f'This is an error }'
136f'This is ok }}'
137fr'x={4*10}\n'
138</textarea></div>
139
140
141<h2>Cython mode</h2>
142
143<div><textarea id="code-cython" name="code-cython">
144
145import numpy as np
146cimport cython
147from libc.math cimport sqrt
148
149@cython.boundscheck(False)
150@cython.wraparound(False)
151def pairwise_cython(double[:, ::1] X):
152 cdef int M = X.shape[0]
153 cdef int N = X.shape[1]
154 cdef double tmp, d
155 cdef double[:, ::1] D = np.empty((M, M), dtype=np.float64)
156 for i in range(M):
157 for j in range(M):
158 d = 0.0
159 for k in range(N):
160 tmp = X[i, k] - X[j, k]
161 d += tmp * tmp
162 D[i, j] = sqrt(d)
163 return np.asarray(D)
164
165</textarea></div>
166
167 <script>
168 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
169 mode: {name: "python",
170 version: 3,
171 singleLineStringErrors: false},
172 lineNumbers: true,
173 indentUnit: 4,
174 matchBrackets: true
175 });
176
177 CodeMirror.fromTextArea(document.getElementById("code-cython"), {
178 mode: {name: "text/x-cython",
179 version: 2,
180 singleLineStringErrors: false},
181 lineNumbers: true,
182 indentUnit: 4,
183 matchBrackets: true
184 });
185 </script>
186 <h2>Configuration Options for Python mode:</h2>
187 <ul>
188 <li>version - 2/3 - The version of Python to recognize. Default is 3.</li>
189 <li>singleLineStringErrors - true/false - If you have a single-line string that is not terminated at the end of the line, this will show subsequent lines as errors if true, otherwise it will consider the newline as the end of the string. Default is false.</li>
190 <li>hangingIndent - int - If you want to write long arguments to a function starting on a new line, how much that line should be indented. Defaults to one normal indentation unit.</li>
191 </ul>
192 <h2>Advanced Configuration Options:</h2>
193 <p>Usefull for superset of python syntax like Enthought enaml, IPython magics and questionmark help</p>
194 <ul>
195 <li>singleOperators - RegEx - Regular Expression for single operator matching, default : <pre>^[\\+\\-\\*/%&|\\^~<>!]</pre> including <pre>@</pre> on Python 3</li>
196 <li>singleDelimiters - RegEx - Regular Expression for single delimiter matching, default : <pre>^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]</pre></li>
197 <li>doubleOperators - RegEx - Regular Expression for double operators matching, default : <pre>^((==)|(!=)|(<=)|(>=)|(<>)|(<<)|(>>)|(//)|(\\*\\*))</pre></li>
198 <li>doubleDelimiters - RegEx - Regular Expression for double delimiters matching, default : <pre>^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&=)|(\\|=)|(\\^=))</pre></li>
199 <li>tripleDelimiters - RegEx - Regular Expression for triple delimiters matching, default : <pre>^((//=)|(>>=)|(<<=)|(\\*\\*=))</pre></li>
200 <li>identifiers - RegEx - Regular Expression for identifier, default : <pre>^[_A-Za-z][_A-Za-z0-9]*</pre> on Python 2 and <pre>^[_A-Za-z\u00A1-\uFFFF][_A-Za-z0-9\u00A1-\uFFFF]*</pre> on Python 3.</li>
201 <li>extra_keywords - list of string - List of extra words ton consider as keywords</li>
202 <li>extra_builtins - list of string - List of extra words ton consider as builtins</li>
203 </ul>
204
205
206 <p><strong>MIME types defined:</strong> <code>text/x-python</code> and <code>text/x-cython</code>.</p>
207 </article>