From ee85425cc79172cdc19519c0fd52f56d97ae2fe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Wed, 11 Jun 2025 00:46:34 +0200 Subject: [PATCH] Big changes in Cython --- README.md | 26 ++++++++++++-------------- index.html | 29 ++++++++++++++--------------- parse.js | 25 ++++++++++++------------- 3 files changed, 38 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index 84eaf52..4569717 100644 --- a/README.md +++ b/README.md @@ -379,7 +379,7 @@ import re ```python = .group() # Returns the whole match. Also group(0). = .group(1) # Returns part inside the first brackets. - = .groups() # Returns all bracketed parts. + = .groups() # Returns all bracketed parts as strings. = .start() # Returns start index of the match. = .end() # Returns exclusive end index of the match. ``` @@ -3513,29 +3513,27 @@ Appendix ```python # $ pip3 install cython -import pyximport; pyximport.install() # Module that runs imported Cython scripts. -import # Script must be saved with '.pyx' extension. +import pyximport; pyximport.install() # Module that runs Cython scripts. +import # Script must have '.pyx' extension. ``` #### All `'cdef'` definitions are optional, but they contribute to the speed-up: - ```python -cdef [*] [= ] -cdef [n_items] [= ] -cdef * = < *> malloc(n_items * sizeof()) -cdef ( [*]): ... +cdef [= ] # Either Python or C type variable. +cdef * [= &] # Use [0] to get the value. +cdef [size] [= ] # Also `from cpython cimport array`. +cdef * [= ] # Also `< *> malloc(n_bytes)`. ``` ```python -cdef class : - cdef public [*] - def __init__(self, [*]): - self. = +cdef ( [*]): ... # Omitted types default to `object`. ``` ```python -cdef struct : - [*] +cdef class : # Also `cdef struct :`. + cdef public [*] # Also `... [*]`. + def __init__(self, ): # Also `cdef __dealloc__(self):`. + self. = # Also `... free()`. ``` ### Virtual Environments diff --git a/index.html b/index.html index f5cf53e..a785cb1 100644 --- a/index.html +++ b/index.html @@ -56,7 +56,7 @@
- +
@@ -358,7 +358,7 @@ Point(x=1, y=2

Match Object

<str>   = <Match>.group()                         # Returns the whole match. Also group(0).
 <str>   = <Match>.group(1)                        # Returns part inside the first brackets.
-<tuple> = <Match>.groups()                        # Returns all bracketed parts.
+<tuple> = <Match>.groups()                        # Returns all bracketed parts as strings.
 <int>   = <Match>.start()                         # Returns start index of the match.
 <int>   = <Match>.end()                           # Returns exclusive end index of the match.
 
@@ -2874,25 +2874,24 @@ px.line(df, x='Date', y=#Appendix

Cython

Library that compiles Python-like code into C.

# $ pip3 install cython
-import pyximport; pyximport.install()  # Module that runs imported Cython scripts.
-import <cython_script>                 # Script must be saved with '.pyx' extension.
+import pyximport; pyximport.install()               # Module that runs Cython scripts.
+import <cython_script>                              # Script must have '.pyx' extension.
 
-

All 'cdef' definitions are optional, but they contribute to the speed-up:

cdef <ctype/type> [*]<var_name> [= <object>]
-cdef <ctype>[n_items] <array_name> [= <coll_of_nums/structs>]
-cdef <ctype> *<array_name> = <<ctype> *> malloc(n_items * sizeof(<ctype>))
-cdef <ctype/type/void> <func_name>(<ctype/type> [*]<arg_name>): ...
+

All 'cdef' definitions are optional, but they contribute to the speed-up:

cdef <type> <var_name> [= <obj/var>]                # Either Python or C type variable.
+cdef <ctype> *<pointer_name> [= &<var>]             # Use <pointer>[0] to get the value.
+cdef <ctype>[size] <array_name> [= <coll/array>]    # Also `from cpython cimport array`.
+cdef <ctype> *<array_name> [= <coll/array>]         # Also `<<ctype> *> malloc(n_bytes)`.
 
-
cdef class <class_name>:
-    cdef public <ctype/type> [*]<attr_name>
-    def __init__(self, <ctype/type> [*]<arg_name>):
-        self.<attr_name> = <arg_name>
+
cdef <type> <func_name>(<type> [*]<arg_name>): ...  # Omitted types default to `object`.
 
-
cdef struct <struct_name>:
-    <ctype> [*]<field_name>
+
cdef class <class_name>:                            # Also `cdef struct <struct_name>:`.
+    cdef public <type> [*]<attr_name>               # Also `... <ctype> [*]<field_name>`.
+    def __init__(self, <type> <arg_name>):          # Also `cdef __dealloc__(self):`.
+        self.<attr_name> = <arg_name>               # Also `... free(<pointer/array>)`.
 

Virtual Environments

System for installing libraries directly into project's directory.

$ python3 -m venv NAME      # Creates virtual environment in current directory.
 $ source NAME/bin/activate  # Activates it. On Windows run `NAME\Scripts\activate`.
@@ -2939,7 +2938,7 @@ $ deactivate                # Deactivates the active
  
 
   
 
diff --git a/parse.js b/parse.js
index 6e5fb30..3f27730 100755
--- a/parse.js
+++ b/parse.js
@@ -326,20 +326,19 @@ const GROUPBY =
   '6  11  13';
 
 const CYTHON_1 =
-  'cdef <ctype/type> [*]<var_name> [= <object>]\n' +
-  'cdef <ctype>[n_items] <array_name> [= <coll_of_nums/structs>]\n' +
-  'cdef <ctype> *<array_name> = <<ctype> *> malloc(n_items * sizeof(<ctype>))\n' +
-  'cdef <ctype/type/void> <func_name>(<ctype/type> [*]<arg_name>): ...\n';
+  'cdef <type> <var_name> [= <obj/var>]                # Either Python or C type variable.\n' +
+  'cdef <ctype> *<pointer_name> [= &<var>]             # Use <pointer>[0] to get the value.\n' +
+  'cdef <ctype>[size] <array_name> [= <coll/array>]    # Also `from cpython cimport array`.\n' +
+  'cdef <ctype> *<array_name> [= <coll/array>]         # Also `<<ctype> *> malloc(n_bytes)`.\n';
 
 const CYTHON_2 =
-  'cdef class <class_name>:\n' +
-  '    cdef public <ctype/type> [*]<attr_name>\n' +
-  '    def __init__(self, <ctype/type> [*]<arg_name>):\n' +
-  '        self.<attr_name> = <arg_name>\n';
+  'cdef <type> <func_name>(<type> [*]<arg_name>): ...  # Omitted types default to `object`.\n';
 
 const CYTHON_3 =
-  'cdef struct <struct_name>:\n' +
-  '    <ctype> [*]<field_name>\n';
+  'cdef class <class_name>:                            # Also `cdef struct <struct_name>:`.\n' +
+  '    cdef public <type> [*]<attr_name>               # Also `... <ctype> [*]<field_name>`.\n' +
+  '    def __init__(self, <type> <arg_name>):          # Also `cdef __dealloc__(self):`.\n' +
+  '        self.<attr_name> = <arg_name>               # Also `... free(<pointer/array>)`.\n';
 
 const INDEX =
   '
  • Ctrl+F / ⌘F is usually sufficient.
  • \n' + @@ -942,9 +941,9 @@ function fixHighlights() { $(`code:contains(samples_f = (sin(i *)`).html(AUDIO_2); $(`code:contains(collections, dataclasses, enum, io, itertools)`).html(MARIO); $(`code:contains(>>> gb = df.groupby)`).html(GROUPBY); - $(`code:contains(cdef [*] [= ])`).html(CYTHON_1); - $(`code:contains(cdef class :)`).html(CYTHON_2); - $(`code:contains(cdef struct :)`).html(CYTHON_3); + $(`code:contains(cdef [= ])`).html(CYTHON_1); + $(`code:contains(cdef ( [*]): ...)`).html(CYTHON_2); + $(`code:contains(cdef class :)`).html(CYTHON_3); $(`ul:contains(Ctrl+F / ⌘F is usually sufficient.)`).html(INDEX); }