index.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <!doctype html>
  2. <title>CodeMirror: Python mode</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../../doc/docs.css">
  5. <link rel="stylesheet" href="../../lib/codemirror.css">
  6. <script src="../../lib/codemirror.js"></script>
  7. <script src="../../addon/edit/matchbrackets.js"></script>
  8. <script src="python.js"></script>
  9. <style type="text/css">.CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}</style>
  10. <div id=nav>
  11. <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../../doc/logo.png"></a>
  12. <ul>
  13. <li><a href="../../index.html">Home</a>
  14. <li><a href="../../doc/manual.html">Manual</a>
  15. <li><a href="https://github.com/codemirror/codemirror">Code</a>
  16. </ul>
  17. <ul>
  18. <li><a href="../index.html">Language modes</a>
  19. <li><a class=active href="#">Python</a>
  20. </ul>
  21. </div>
  22. <article>
  23. <h2>Python mode</h2>
  24. <div><textarea id="code" name="code">
  25. # Literals
  26. 1234
  27. 0.0e101
  28. .123
  29. 0b01010011100
  30. 0o01234567
  31. 0x0987654321abcdef
  32. 7
  33. 2147483647
  34. 3L
  35. 79228162514264337593543950336L
  36. 0x100000000L
  37. 79228162514264337593543950336
  38. 0xdeadbeef
  39. 3.14j
  40. 10.j
  41. 10j
  42. .001j
  43. 1e100j
  44. 3.14e-10j
  45. # String Literals
  46. 'For\''
  47. "God\""
  48. """so loved
  49. the world"""
  50. '''that he gave
  51. his only begotten\' '''
  52. 'that whosoever believeth \
  53. in him'
  54. ''
  55. # Identifiers
  56. __a__
  57. a.b
  58. a.b.c
  59. #Unicode identifiers on Python3
  60. # a = x\ddot
  61. a⃗ = ẍ
  62. # a = v\dot
  63. a⃗ = v̇
  64. #F\vec = m \cdot a\vec
  65. F⃗ = m•a⃗
  66. # Operators
  67. + - * / % & | ^ ~ < >
  68. == != <= >= <> << >> // **
  69. and or not in is
  70. #infix matrix multiplication operator (PEP 465)
  71. A @ B
  72. # Delimiters
  73. () [] {} , : ` = ; @ . # Note that @ and . require the proper context on Python 2.
  74. += -= *= /= %= &= |= ^=
  75. //= >>= <<= **=
  76. # Keywords
  77. as assert break class continue def del elif else except
  78. finally for from global if import lambda pass raise
  79. return try while with yield
  80. # Python 2 Keywords (otherwise Identifiers)
  81. exec print
  82. # Python 3 Keywords (otherwise Identifiers)
  83. nonlocal
  84. # Types
  85. bool classmethod complex dict enumerate float frozenset int list object
  86. property reversed set slice staticmethod str super tuple type
  87. # Python 2 Types (otherwise Identifiers)
  88. basestring buffer file long unicode xrange
  89. # Python 3 Types (otherwise Identifiers)
  90. bytearray bytes filter map memoryview open range zip
  91. # Some Example code
  92. import os
  93. from package import ParentClass
  94. @nonsenseDecorator
  95. def doesNothing():
  96. pass
  97. class ExampleClass(ParentClass):
  98. @staticmethod
  99. def example(inputStr):
  100. a = list(inputStr)
  101. a.reverse()
  102. return ''.join(a)
  103. def __init__(self, mixin = 'Hello'):
  104. self.mixin = mixin
  105. # Python 3.6 f-strings (https://www.python.org/dev/peps/pep-0498/)
  106. f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
  107. f'He said his name is {name!r}.'
  108. f"""He said his name is {name!r}."""
  109. f'{"quoted string"}'
  110. f'{{ {4*10} }}'
  111. f'This is an error }'
  112. f'This is ok }}'
  113. fr'x={4*10}\n'
  114. </textarea></div>
  115. <h2>Cython mode</h2>
  116. <div><textarea id="code-cython" name="code-cython">
  117. import numpy as np
  118. cimport cython
  119. from libc.math cimport sqrt
  120. @cython.boundscheck(False)
  121. @cython.wraparound(False)
  122. def pairwise_cython(double[:, ::1] X):
  123. cdef int M = X.shape[0]
  124. cdef int N = X.shape[1]
  125. cdef double tmp, d
  126. cdef double[:, ::1] D = np.empty((M, M), dtype=np.float64)
  127. for i in range(M):
  128. for j in range(M):
  129. d = 0.0
  130. for k in range(N):
  131. tmp = X[i, k] - X[j, k]
  132. d += tmp * tmp
  133. D[i, j] = sqrt(d)
  134. return np.asarray(D)
  135. </textarea></div>
  136. <script>
  137. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  138. mode: {name: "python",
  139. version: 3,
  140. singleLineStringErrors: false},
  141. lineNumbers: true,
  142. indentUnit: 4,
  143. matchBrackets: true
  144. });
  145. CodeMirror.fromTextArea(document.getElementById("code-cython"), {
  146. mode: {name: "text/x-cython",
  147. version: 2,
  148. singleLineStringErrors: false},
  149. lineNumbers: true,
  150. indentUnit: 4,
  151. matchBrackets: true
  152. });
  153. </script>
  154. <h2>Configuration Options for Python mode:</h2>
  155. <ul>
  156. <li>version - 2/3 - The version of Python to recognize. Default is 3.</li>
  157. <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>
  158. <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>
  159. </ul>
  160. <h2>Advanced Configuration Options:</h2>
  161. <p>Usefull for superset of python syntax like Enthought enaml, IPython magics and questionmark help</p>
  162. <ul>
  163. <li>singleOperators - RegEx - Regular Expression for single operator matching, default : <pre>^[\\+\\-\\*/%&amp;|\\^~&lt;&gt;!]</pre> including <pre>@</pre> on Python 3</li>
  164. <li>singleDelimiters - RegEx - Regular Expression for single delimiter matching, default : <pre>^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]</pre></li>
  165. <li>doubleOperators - RegEx - Regular Expression for double operators matching, default : <pre>^((==)|(!=)|(&lt;=)|(&gt;=)|(&lt;&gt;)|(&lt;&lt;)|(&gt;&gt;)|(//)|(\\*\\*))</pre></li>
  166. <li>doubleDelimiters - RegEx - Regular Expression for double delimiters matching, default : <pre>^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&amp;=)|(\\|=)|(\\^=))</pre></li>
  167. <li>tripleDelimiters - RegEx - Regular Expression for triple delimiters matching, default : <pre>^((//=)|(&gt;&gt;=)|(&lt;&lt;=)|(\\*\\*=))</pre></li>
  168. <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>
  169. <li>extra_keywords - list of string - List of extra words ton consider as keywords</li>
  170. <li>extra_builtins - list of string - List of extra words ton consider as builtins</li>
  171. </ul>
  172. <p><strong>MIME types defined:</strong> <code>text/x-python</code> and <code>text/x-cython</code>.</p>
  173. </article>