index.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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="http://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. </textarea></div>
  106. <h2>Cython mode</h2>
  107. <div><textarea id="code-cython" name="code-cython">
  108. import numpy as np
  109. cimport cython
  110. from libc.math cimport sqrt
  111. @cython.boundscheck(False)
  112. @cython.wraparound(False)
  113. def pairwise_cython(double[:, ::1] X):
  114. cdef int M = X.shape[0]
  115. cdef int N = X.shape[1]
  116. cdef double tmp, d
  117. cdef double[:, ::1] D = np.empty((M, M), dtype=np.float64)
  118. for i in range(M):
  119. for j in range(M):
  120. d = 0.0
  121. for k in range(N):
  122. tmp = X[i, k] - X[j, k]
  123. d += tmp * tmp
  124. D[i, j] = sqrt(d)
  125. return np.asarray(D)
  126. </textarea></div>
  127. <script>
  128. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  129. mode: {name: "python",
  130. version: 3,
  131. singleLineStringErrors: false},
  132. lineNumbers: true,
  133. indentUnit: 4,
  134. matchBrackets: true
  135. });
  136. CodeMirror.fromTextArea(document.getElementById("code-cython"), {
  137. mode: {name: "text/x-cython",
  138. version: 2,
  139. singleLineStringErrors: false},
  140. lineNumbers: true,
  141. indentUnit: 4,
  142. matchBrackets: true
  143. });
  144. </script>
  145. <h2>Configuration Options for Python mode:</h2>
  146. <ul>
  147. <li>version - 2/3 - The version of Python to recognize. Default is 2.</li>
  148. <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>
  149. <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>
  150. </ul>
  151. <h2>Advanced Configuration Options:</h2>
  152. <p>Usefull for superset of python syntax like Enthought enaml, IPython magics and questionmark help</p>
  153. <ul>
  154. <li>singleOperators - RegEx - Regular Expression for single operator matching, default : <pre>^[\\+\\-\\*/%&amp;|\\^~&lt;&gt;!]</pre> including <pre>@</pre> on Python 3</li>
  155. <li>singleDelimiters - RegEx - Regular Expression for single delimiter matching, default : <pre>^[\\(\\)\\[\\]\\{\\}@,:`=;\\.]</pre></li>
  156. <li>doubleOperators - RegEx - Regular Expression for double operators matching, default : <pre>^((==)|(!=)|(&lt;=)|(&gt;=)|(&lt;&gt;)|(&lt;&lt;)|(&gt;&gt;)|(//)|(\\*\\*))</pre></li>
  157. <li>doubleDelimiters - RegEx - Regular Expression for double delimiters matching, default : <pre>^((\\+=)|(\\-=)|(\\*=)|(%=)|(/=)|(&amp;=)|(\\|=)|(\\^=))</pre></li>
  158. <li>tripleDelimiters - RegEx - Regular Expression for triple delimiters matching, default : <pre>^((//=)|(&gt;&gt;=)|(&lt;&lt;=)|(\\*\\*=))</pre></li>
  159. <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>
  160. <li>extra_keywords - list of string - List of extra words ton consider as keywords</li>
  161. <li>extra_builtins - list of string - List of extra words ton consider as builtins</li>
  162. </ul>
  163. <p><strong>MIME types defined:</strong> <code>text/x-python</code> and <code>text/x-cython</code>.</p>
  164. </article>