From de3463223b29fbd2b7589461731e0e2799c0b910 Mon Sep 17 00:00:00 2001 From: Subrahmanya Gaonkar Date: Fri, 26 Jul 2024 10:28:42 +0530 Subject: [PATCH 1/9] Successfully added addtype method under MimeTypes class --- Doc/library/mimetypes.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst index 91e8c30f8607b3..577a82eb6e573a 100644 --- a/Doc/library/mimetypes.rst +++ b/Doc/library/mimetypes.rst @@ -295,3 +295,13 @@ than one MIME-type database; it provides an interface similar to the one of the types, else to the list of non-standard types. .. versionadded:: 3.2 + + + .. method:: MimeTypes.add_type(type, ext, strict=True) + + Add a mapping from the MIME type *type* to the extension *ext*. When the + extension is already known, the new type will replace the old one. When the type + is already known the extension will be added to the list of known extensions. + + When *strict* is ``True`` (the default), the mapping will be added to the + official MIME types, otherwise to the non-standard ones. \ No newline at end of file From 89207d157a203e9b7be796dc3dc81b46773640fc Mon Sep 17 00:00:00 2001 From: Subrahmanya Gaonkar Date: Fri, 26 Jul 2024 11:39:25 +0530 Subject: [PATCH 2/9] added a new blank line --- Doc/library/mimetypes.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst index 577a82eb6e573a..89f537e0173e18 100644 --- a/Doc/library/mimetypes.rst +++ b/Doc/library/mimetypes.rst @@ -304,4 +304,5 @@ than one MIME-type database; it provides an interface similar to the one of the is already known the extension will be added to the list of known extensions. When *strict* is ``True`` (the default), the mapping will be added to the - official MIME types, otherwise to the non-standard ones. \ No newline at end of file + official MIME types, otherwise to the non-standard ones. + \ No newline at end of file From b03a832fd81211fc21e81469fed8c54bde349f1c Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+AA-Turner@users.noreply.github.com> Date: Fri, 26 Jul 2024 22:38:15 +0100 Subject: [PATCH 3/9] Remove whitespace --- Doc/library/mimetypes.rst | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst index 89f537e0173e18..8241a99eabe964 100644 --- a/Doc/library/mimetypes.rst +++ b/Doc/library/mimetypes.rst @@ -296,7 +296,6 @@ than one MIME-type database; it provides an interface similar to the one of the .. versionadded:: 3.2 - .. method:: MimeTypes.add_type(type, ext, strict=True) Add a mapping from the MIME type *type* to the extension *ext*. When the @@ -304,5 +303,5 @@ than one MIME-type database; it provides an interface similar to the one of the is already known the extension will be added to the list of known extensions. When *strict* is ``True`` (the default), the mapping will be added to the - official MIME types, otherwise to the non-standard ones. + official MIME types, otherwise to the non-standard ones. \ No newline at end of file From 248058861e5173ed2403b29f3a214cfe407357d8 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Fri, 26 Jul 2024 22:40:27 +0100 Subject: [PATCH 4/9] Remove whitespace (again) --- Doc/library/mimetypes.rst | 1 - 1 file changed, 1 deletion(-) diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst index 8241a99eabe964..54352c5f0e123d 100644 --- a/Doc/library/mimetypes.rst +++ b/Doc/library/mimetypes.rst @@ -304,4 +304,3 @@ than one MIME-type database; it provides an interface similar to the one of the When *strict* is ``True`` (the default), the mapping will be added to the official MIME types, otherwise to the non-standard ones. - \ No newline at end of file From e41ea05c599d3f3cedbbad392348d5b53462f210 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Fri, 26 Jul 2024 22:44:08 +0100 Subject: [PATCH 5/9] Add whitespace --- Doc/library/mimetypes.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Doc/library/mimetypes.rst b/Doc/library/mimetypes.rst index 54352c5f0e123d..8ad4850584a7e1 100644 --- a/Doc/library/mimetypes.rst +++ b/Doc/library/mimetypes.rst @@ -296,6 +296,7 @@ than one MIME-type database; it provides an interface similar to the one of the .. versionadded:: 3.2 + .. method:: MimeTypes.add_type(type, ext, strict=True) Add a mapping from the MIME type *type* to the extension *ext*. When the From ba2e3ed406155804fd58d8748a4615a3ac8b9a5e Mon Sep 17 00:00:00 2001 From: Subrahmanya Gaonkar Date: Sat, 27 Jul 2024 09:50:26 +0530 Subject: [PATCH 6/9] added Binary(data) function to Module functions --- Doc/library/sqlite3.rst | 137 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 5dc22a7e431ae4..9102d3d3acfb31 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -364,6 +364,143 @@ Module functions and *uri* is deprecated. They will become keyword-only parameters in Python 3.15. +.. function:: Binary(data) + + Create a Binary object to handle binary data in SQLite. + + :param data: + The binary data to be encapsulated. This can be any object supporting the + Python buffer protocol, such as bytes, bytearray, or memoryview. + :type data: buffer-compatible object + + The :func:`Binary` function encapsulates binary data to ensure proper handling + by SQLite. It is used to signal to the sqlite3 module that the data should be + treated as a BLOB (Binary Large Object) rather than text. + + **Purpose and Functionality:** + + - **Data Encapsulation**: Ensures the data is treated as binary and not as text. + - **Buffer Protocol Support**: Accepts objects that support the Python buffer protocol. + - **Type Signaling**: Explicitly signals that the data should be treated as binary. + + **Internal Workings:** + + - Creates an instance of the Binary class, a subclass of bytes. + - Flags the data for special handling as binary by the sqlite3 module. + - Ensures data is bound as a BLOB in SQL queries. + + **Use Cases:** + + - **Storing Raw Binary Data**: Ideal for files, images, or any raw binary data. + - **Preserving Exact Byte Sequences**: Prevents encoding/decoding errors. + - **Working with Non-Text Data**: Ensures data isn't subjected to text-based operations. + + **Behavior in Queries:** + + - **INSERT or UPDATE**: Stores Binary objects as BLOB data. + - **SELECT**: Retrieves BLOB data as Binary objects, convertible to bytes. + + **Performance Considerations:** + + - Using Binary() for large data can be memory-intensive. + - For very large objects, consider using SQLite's incremental or BLOB I/O interfaces. + + **Compatibility and Portability:** + + - Ensures consistent behavior across Python implementations and SQLite versions. + - Helps in writing portable code for binary data handling. + + **Error Handling:** + + - Raises :exc:`TypeError` if input doesn't support the buffer protocol. + - Handle potential :exc:`MemoryError` for very large binary objects. + + **Best Practices:** + + - Use Binary() to explicitly mark data as binary. + - Be mindful of memory usage for large binary objects. + - Consider using with prepared statements for efficiency. + + By using :func:`Binary`, developers can ensure robust handling of binary data in SQLite, + preventing data corruption and enhancing application reliability. + + **Examples:** + + Basic example of storing and retrieving binary data: + + .. code-block:: python + + import sqlite3 + + # Create a connection and cursor + conn = sqlite3.connect(':memory:') + cursor = conn.cursor() + + # Create a table + cursor.execute(''' + CREATE TABLE files ( + id INTEGER PRIMARY KEY, + name TEXT, + data BLOB + ) + ''') + + # Insert binary data + with open('example.png', 'rb') as file: + binary_data = file.read() + cursor.execute('INSERT INTO files (name, data) VALUES (?, ?)', ('example.png', sqlite3.Binary(binary_data))) + + conn.commit() + + # Retrieve binary data + cursor.execute('SELECT data FROM files WHERE name=?', ('example.png',)) + blob_data = cursor.fetchone()[0] + + # Save retrieved data to a file + with open('retrieved_example.png', 'wb') as file: + file.write(blob_data) + + # Clean up + conn.close() + + Example with buffer-compatible objects: + + .. code-block:: python + + import sqlite3 + import numpy as np + + # Create a connection and cursor + conn = sqlite3.connect(':memory:') + cursor = conn.cursor() + + # Create a table + cursor.execute(''' + CREATE TABLE binary_data ( + id INTEGER PRIMARY KEY, + data BLOB + ) + ''') + + # Use a memoryview object + data_array = np.array([1, 2, 3, 4, 5], dtype=np.uint8) + memoryview_data = memoryview(data_array) + + # Insert memoryview data + cursor.execute('INSERT INTO binary_data (data) VALUES (?)', (sqlite3.Binary(memoryview_data),)) + + conn.commit() + + # Retrieve memoryview data + cursor.execute('SELECT data FROM binary_data WHERE id=1') + blob_data = cursor.fetchone()[0] + + # Convert retrieved data back to a numpy array + retrieved_array = np.frombuffer(blob_data, dtype=np.uint8) + + # Clean up + conn.close() + .. function:: complete_statement(statement) Return ``True`` if the string *statement* appears to contain From c8ebedd45d840c9cab035b5736f78a6056b0df7d Mon Sep 17 00:00:00 2001 From: Subrahmanya Gaonkar Date: Sat, 27 Jul 2024 19:06:17 +0530 Subject: [PATCH 7/9] did everything asked, removed numpy example, etc.... --- Doc/library/sqlite3.rst | 105 ++++------------------------------------ 1 file changed, 9 insertions(+), 96 deletions(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 9102d3d3acfb31..53a91a2260900b 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -369,58 +369,13 @@ Module functions Create a Binary object to handle binary data in SQLite. :param data: - The binary data to be encapsulated. This can be any object supporting the - Python buffer protocol, such as bytes, bytearray, or memoryview. - :type data: buffer-compatible object + The binary data to be encapsulated. This should be a + :term:`bytes-like object`. The :func:`Binary` function encapsulates binary data to ensure proper handling by SQLite. It is used to signal to the sqlite3 module that the data should be treated as a BLOB (Binary Large Object) rather than text. - **Purpose and Functionality:** - - - **Data Encapsulation**: Ensures the data is treated as binary and not as text. - - **Buffer Protocol Support**: Accepts objects that support the Python buffer protocol. - - **Type Signaling**: Explicitly signals that the data should be treated as binary. - - **Internal Workings:** - - - Creates an instance of the Binary class, a subclass of bytes. - - Flags the data for special handling as binary by the sqlite3 module. - - Ensures data is bound as a BLOB in SQL queries. - - **Use Cases:** - - - **Storing Raw Binary Data**: Ideal for files, images, or any raw binary data. - - **Preserving Exact Byte Sequences**: Prevents encoding/decoding errors. - - **Working with Non-Text Data**: Ensures data isn't subjected to text-based operations. - - **Behavior in Queries:** - - - **INSERT or UPDATE**: Stores Binary objects as BLOB data. - - **SELECT**: Retrieves BLOB data as Binary objects, convertible to bytes. - - **Performance Considerations:** - - - Using Binary() for large data can be memory-intensive. - - For very large objects, consider using SQLite's incremental or BLOB I/O interfaces. - - **Compatibility and Portability:** - - - Ensures consistent behavior across Python implementations and SQLite versions. - - Helps in writing portable code for binary data handling. - - **Error Handling:** - - - Raises :exc:`TypeError` if input doesn't support the buffer protocol. - - Handle potential :exc:`MemoryError` for very large binary objects. - - **Best Practices:** - - - Use Binary() to explicitly mark data as binary. - - Be mindful of memory usage for large binary objects. - - Consider using with prepared statements for efficiency. - By using :func:`Binary`, developers can ensure robust handling of binary data in SQLite, preventing data corruption and enhancing application reliability. @@ -439,67 +394,25 @@ Module functions # Create a table cursor.execute(''' CREATE TABLE files ( - id INTEGER PRIMARY KEY, name TEXT, data BLOB ) ''') # Insert binary data - with open('example.png', 'rb') as file: - binary_data = file.read() - cursor.execute('INSERT INTO files (name, data) VALUES (?, ?)', ('example.png', sqlite3.Binary(binary_data))) + binary_data = bytes("abc", "utf-8") + query = 'INSERT INTO files (name, data) VALUES (?, ?)' + values = ('example.txt', sqlite3.Binary(binary_data)) + cursor.execute(query, values) conn.commit() # Retrieve binary data - cursor.execute('SELECT data FROM files WHERE name=?', ('example.png',)) - blob_data = cursor.fetchone()[0] - - # Save retrieved data to a file - with open('retrieved_example.png', 'wb') as file: - file.write(blob_data) - - # Clean up - conn.close() - - Example with buffer-compatible objects: - - .. code-block:: python - - import sqlite3 - import numpy as np - - # Create a connection and cursor - conn = sqlite3.connect(':memory:') - cursor = conn.cursor() - - # Create a table - cursor.execute(''' - CREATE TABLE binary_data ( - id INTEGER PRIMARY KEY, - data BLOB - ) - ''') - - # Use a memoryview object - data_array = np.array([1, 2, 3, 4, 5], dtype=np.uint8) - memoryview_data = memoryview(data_array) - - # Insert memoryview data - cursor.execute('INSERT INTO binary_data (data) VALUES (?)', (sqlite3.Binary(memoryview_data),)) - - conn.commit() - - # Retrieve memoryview data - cursor.execute('SELECT data FROM binary_data WHERE id=1') + cursor.execute('SELECT data FROM files WHERE name=?', ('example.txt',)) blob_data = cursor.fetchone()[0] - # Convert retrieved data back to a numpy array - retrieved_array = np.frombuffer(blob_data, dtype=np.uint8) - - # Clean up - conn.close() + with open('retrieved_example.txt', 'wb') as file: + file.write(blob_data) .. function:: complete_statement(statement) From 2d25094995fbf24ca974d30362dc69a73fdf925d Mon Sep 17 00:00:00 2001 From: Subrahmanya Gaonkar Date: Sat, 27 Jul 2024 19:47:08 +0530 Subject: [PATCH 8/9] tried my best --- Doc/library/sqlite3.rst | 68 ++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index 53a91a2260900b..ec1503b7255d12 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -369,7 +369,7 @@ Module functions Create a Binary object to handle binary data in SQLite. :param data: - The binary data to be encapsulated. This should be a + The binary data to be encapsulated. This should be a :term:`bytes-like object`. The :func:`Binary` function encapsulates binary data to ensure proper handling @@ -379,40 +379,44 @@ Module functions By using :func:`Binary`, developers can ensure robust handling of binary data in SQLite, preventing data corruption and enhancing application reliability. - **Examples:** - - Basic example of storing and retrieving binary data: + **Basic example of storing and retrieving binary data:** .. code-block:: python - import sqlite3 - - # Create a connection and cursor - conn = sqlite3.connect(':memory:') - cursor = conn.cursor() - - # Create a table - cursor.execute(''' - CREATE TABLE files ( - name TEXT, - data BLOB - ) - ''') - - # Insert binary data - binary_data = bytes("abc", "utf-8") - query = 'INSERT INTO files (name, data) VALUES (?, ?)' - values = ('example.txt', sqlite3.Binary(binary_data)) - cursor.execute(query, values) - - conn.commit() - - # Retrieve binary data - cursor.execute('SELECT data FROM files WHERE name=?', ('example.txt',)) - blob_data = cursor.fetchone()[0] - - with open('retrieved_example.txt', 'wb') as file: - file.write(blob_data) + import sqlite3 + import pickle + + # Create a connection and cursor + with sqlite3.connect(':memory:') as conn: + cursor = conn.cursor() + + # Create a table + cursor.execute(''' + CREATE TABLE files ( + id INTEGER PRIMARY KEY, + name TEXT, + data BLOB) + ''') + + # Insert binary data + binary_data = pickle.dumps({'foo': 42, 'bar': 1337}) + query = 'INSERT INTO files (name, data) VALUES (?, ?)' + values = ('example.pkl', sqlite3.Binary(binary_data)) + cursor.execute(query, values) + + # Retrieve binary data + cursor.execute('SELECT data FROM files WHERE name=?', ('example.pkl',)) + blob_data = cursor.fetchone()[0] + + # Deserialize the binary data using pickle + retrieved_data = pickle.loads(blob_data) + print(retrieved_data) # This should print: {'foo': 42, 'bar': 1337} + + # Save retrieved data to a file + with open('retrieved_example.txt', 'wb') as file: + file.write(blob_data) + + conn.close() .. function:: complete_statement(statement) From 2a637ff12c88f0d07c35dc19557066943f0606b0 Mon Sep 17 00:00:00 2001 From: Subrahmanya Gaonkar Date: Sat, 27 Jul 2024 20:19:59 +0530 Subject: [PATCH 9/9] tried again --- Doc/library/sqlite3.rst | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/Doc/library/sqlite3.rst b/Doc/library/sqlite3.rst index ec1503b7255d12..d588b3655c5c0d 100644 --- a/Doc/library/sqlite3.rst +++ b/Doc/library/sqlite3.rst @@ -369,7 +369,7 @@ Module functions Create a Binary object to handle binary data in SQLite. :param data: - The binary data to be encapsulated. This should be a + The binary data to be encapsulated. This should be a :term:`bytes-like object`. The :func:`Binary` function encapsulates binary data to ensure proper handling @@ -379,7 +379,7 @@ Module functions By using :func:`Binary`, developers can ensure robust handling of binary data in SQLite, preventing data corruption and enhancing application reliability. - **Basic example of storing and retrieving binary data:** + .. rubric:: Basic example of storing and retrieving binary data .. code-block:: python @@ -387,8 +387,8 @@ Module functions import pickle # Create a connection and cursor - with sqlite3.connect(':memory:') as conn: - cursor = conn.cursor() + conn = sqlite3.connect(':memory:') + cursor = conn.cursor() # Create a table cursor.execute(''' @@ -400,24 +400,19 @@ Module functions # Insert binary data binary_data = pickle.dumps({'foo': 42, 'bar': 1337}) - query = 'INSERT INTO files (name, data) VALUES (?, ?)' values = ('example.pkl', sqlite3.Binary(binary_data)) - cursor.execute(query, values) + cursor.execute('INSERT INTO files (name, data) VALUES (?, ?)', values) + conn.commit() # Retrieve binary data cursor.execute('SELECT data FROM files WHERE name=?', ('example.pkl',)) blob_data = cursor.fetchone()[0] + conn.close() # Deserialize the binary data using pickle retrieved_data = pickle.loads(blob_data) print(retrieved_data) # This should print: {'foo': 42, 'bar': 1337} - # Save retrieved data to a file - with open('retrieved_example.txt', 'wb') as file: - file.write(blob_data) - - conn.close() - .. function:: complete_statement(statement) Return ``True`` if the string *statement* appears to contain pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy