Skip to content

Commit 23c16bb

Browse files
committed
3.0.0 - Overhaul 'net', new object cleaner, class generation/mocking + more
**Key Additions and Changes** - `privex.helpers.common` - Added `strip_null` - very simple helper function to strip both `\00` and white space from a string - with 2 cycles for good measure. - `privex.helpers.types` - Added `AUTO` / `AUTOMATIC` / `AUTO_DETECTED` dummy type, for use as the default value of function/method parameters, signalling to users that a parameter is auto-populated from another data source (e.g. instance/class attribute) if not specified. - `privex.helpers.collections` - Added `copy_class_simple` (alternative to `copy_class`) - Added `copy_func` for copying functions, methods, and classmethods - Improved `_q_copy` to handle copying functions, methods and classmethods - Added `generate_class` + `generate_class_kw` - Added `Mocker.make_mock_module` - Added `Mocker.add_mock_modules` - Added `Mocker.__dir__` to track the available mock attributes and modules - Added `dataclasses_mock` - a `Mocker` instance which emulates `dataclasses` as a drop-in partially functional dummy for Python 3.6 when the `dataclasses` backport package isn't installed. - Various changes to `Mocker.make_mock_class` - potentially breaking, see the **BREAKING CHANGES** section. - Added `DictObject.__dir__` + `OrderedDictObject.__dir__` to enable proper tracking of dictionary keys as attributes - `privex.helpers.net` - This module has now been converted into a folder-based module. Imports in `__init__.py` have been carefully setup to ensure that existing import statements should still work as normal - Added new `SocketWrapper` and `AsyncSocketWrapper` classes, which are powerful wrapper classes for working with Python `socket.socket` objects, including support for SSL/TLS, partial support for running socket servers, and\ making basic HTTP requests - **Many, many new functions and classes!** There's too many to list, and due to the conversion into a module folder instead of a singular file, it's difficult to track which functions/classes are new, and which existed before. If you really want to know what's new, just take a look around the `privex/helpers/net` module. - `privex.helpers.converters` - Added `clean_obj` - which is a function that recursively "cleans" any arbitrary object, as to make it safe to convert into JSON and other common serialisation formats. It supports `dict`'s, `list`'s, [attrs](https://attrs.org) objects, native Python `dataclass`'s, `Decimal`, and many other types of objects. - Added `clean_dict` (used by `clean_obj`, usually no need to call it directly) - Added `clean_list` (used by `clean_obj`, usually no need to call it directly) - Added `privex.helpers.mockers` module, which contains pre-made `Mocker` objects that are designed to stand-in for certain libraries / classes as partially functional dummies, if the real module(s) are unavailable for whatever reason. - **And probably some other small additions / changes** **BREAKING CHANGES** - Both `_copy_class_dict` and `_copy_class_slotted` now check each attribute name against a blacklist (default: `COPY_CLASS_BLACKLIST`), and the default blacklist contains `__dict__`, `__slots__` and `__weakref__`, as the first 2 can't be directly copied (but we copy their contents by iteration), and weakref simply can't be deep copied (and it probably isn't a good idea to copy it anyway). - `_copy_class_dict` (used by `copy_class`) no longer breaks the attribute copy loop if `deep_copy=False` - `Mocker.make_mock_class` now returns a cloned `Mocker` class or instance by default, instead of a barebones class / instance of a barebones class. This was done simply because a Mocker class/instance is designed to handle being instantiated with any combination of constructor arguments, and have arbitrary attributes be retrieved / methods called without raising errors. If you absolutely require a plain, simple, empty class to be generated, you may pass the parameter `simple=True` to generate a bare class instead of a clone of Mocker (similar to the old behaviour). Unlike the old version of this method, you can now specify attributes as a dictionary to make your barebones mock class act similar to the class it's mocking. - Many things in `privex.helpers.net` such as `check_host` / `check_host_async` have been improved in various ways, however there may be some breaking changes with certain `privex.helpers.net` functions/classes in certain usecases. - Due to the high risk of bugs with certain networking functions that have been completely revamped, the older, simpler versions of various networking functions are available under `privex.helpers.net.base` with their original names. Because of the naming conflicts, to use the legacy functions/classes from `base`, you must import them directly from `privex.helpers.net.base` like so: ``` # Option 1: import the base module itself, with an alias to prevent naming conflicts (and make it more # clear what you're referencing) from privex.helpers.net import base as netbase if netbase.check_host('google.com', 80): print('google.com is up') # Option 2: import the required legacy functions directly (optionally, you can alias them as needed) # You could also alias the newer overhauled functions while testing them in small portions # of your application. from privex.helpers.net.base import check_host from privex.helpers.net import check_host as new_check_host if check_host('www.privex.io', 443, http_test=True, use_ssl=True): print('[old check_host] https://www.privex.io is up') if new_check_host('files.privex.io', 443, http_test=True, use_ssl=True): print('[new check_host] https://files.privex.io is up') ```
1 parent 88946d2 commit 23c16bb

22 files changed

+3892
-444
lines changed

CHANGELOG.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,108 @@
11
-----------------------------------------------------------------------------------------------------------------------
22

3+
3.0.0 - Overhauled net module, new object cleaner for easier serialisation, improved class generation/mocking + more
4+
====================================================================================================================
5+
6+
-----------------------------------------------------------------------------------------------------------------------
7+
8+
- `privex.helpers.common`
9+
- Added `strip_null` - very simple helper function to strip both `\00` and white space
10+
from a string - with 2 cycles for good measure.
11+
12+
- `privex.helpers.types`
13+
- Added `AUTO` / `AUTOMATIC` / `AUTO_DETECTED` dummy type, for use as the default value
14+
of function/method parameters, signalling to users that a parameter is auto-populated
15+
from another data source (e.g. instance/class attribute) if not specified.
16+
17+
- `privex.helpers.collections`
18+
- Added `copy_class_simple` (alternative to `copy_class`)
19+
- Added `copy_func` for copying functions, methods, and classmethods
20+
- Improved `_q_copy` to handle copying functions, methods and classmethods
21+
- Added `generate_class` + `generate_class_kw`
22+
- Added `Mocker.make_mock_module`
23+
- Added `Mocker.add_mock_modules`
24+
- Added `Mocker.__dir__` to track the available mock attributes and modules
25+
- Added `dataclasses_mock` - a `Mocker` instance which emulates `dataclasses` as a drop-in
26+
partially functional dummy for Python 3.6 when the `dataclasses` backport package isn't installed.
27+
- Various changes to `Mocker.make_mock_class` - potentially breaking, see
28+
the **BREAKING CHANGES** section.
29+
- Added `DictObject.__dir__` + `OrderedDictObject.__dir__` to enable proper tracking of dictionary keys as attributes
30+
31+
- `privex.helpers.net`
32+
- This module has now been converted into a folder-based module. Imports in `__init__.py` have been carefully
33+
setup to ensure that existing import statements should still work as normal
34+
- Added new `SocketWrapper` and `AsyncSocketWrapper` classes, which are powerful wrapper classes for working with
35+
Python `socket.socket` objects, including support for SSL/TLS, partial support for running socket servers, and\
36+
making basic HTTP requests
37+
- **Many, many new functions and classes!** There's too many to list, and due to the conversion into a module folder
38+
instead of a singular file, it's difficult to track which functions/classes are new, and which existed before.
39+
40+
If you really want to know what's new, just take a look around the `privex/helpers/net` module.
41+
42+
- `privex.helpers.converters`
43+
- Added `clean_obj` - which is a function that recursively "cleans" any arbitrary object, as to make it safe to convert
44+
into JSON and other common serialisation formats. It supports `dict`'s, `list`'s, [attrs](https://attrs.org)
45+
objects, native Python `dataclass`'s, `Decimal`, and many other types of objects.
46+
- Added `clean_dict` (used by `clean_obj`, usually no need to call it directly)
47+
- Added `clean_list` (used by `clean_obj`, usually no need to call it directly)
48+
49+
- Added `privex.helpers.mockers` module, which contains pre-made `Mocker` objects that are designed to stand-in
50+
for certain libraries / classes as partially functional dummies, if the real module(s) are unavailable for whatever reason.
51+
52+
- **And probably some other small additions / changes**
53+
54+
55+
**BREAKING CHANGES**
56+
57+
- Both `_copy_class_dict` and `_copy_class_slotted` now check each attribute name
58+
against a blacklist (default: `COPY_CLASS_BLACKLIST`), and the default blacklist
59+
contains `__dict__`, `__slots__` and `__weakref__`, as the first 2 can't be directly
60+
copied (but we copy their contents by iteration), and weakref simply can't be deep copied
61+
(and it probably isn't a good idea to copy it anyway).
62+
- `_copy_class_dict` (used by `copy_class`) no longer breaks the attribute copy loop if `deep_copy=False`
63+
64+
- `Mocker.make_mock_class` now returns a cloned `Mocker` class or instance by default, instead of
65+
a barebones class / instance of a barebones class.
66+
67+
This was done simply because a Mocker class/instance is designed to handle being
68+
instantiated with any combination of constructor arguments, and have arbitrary
69+
attributes be retrieved / methods called without raising errors.
70+
71+
If you absolutely require a plain, simple, empty class to be generated, you may
72+
pass the parameter `simple=True` to generate a bare class instead of a clone of Mocker
73+
(similar to the old behaviour). Unlike the old version of this method, you can now specify attributes
74+
as a dictionary to make your barebones mock class act similar to the class it's mocking.
75+
76+
- Many things in `privex.helpers.net` such as `check_host` / `check_host_async` have been improved in various ways, however
77+
there may be some breaking changes with certain `privex.helpers.net` functions/classes in certain usecases.
78+
- Due to the high risk of bugs with certain networking functions that have been completely revamped, the
79+
older, simpler versions of various networking functions are available under `privex.helpers.net.base`
80+
with their original names.
81+
82+
Because of the naming conflicts, to use the legacy functions/classes from `base`, you must import them
83+
directly from `privex.helpers.net.base` like so:
84+
85+
```
86+
# Option 1: import the base module itself, with an alias to prevent naming conflicts (and make it more
87+
# clear what you're referencing)
88+
from privex.helpers.net import base as netbase
89+
if netbase.check_host('google.com', 80):
90+
print('google.com is up')
91+
# Option 2: import the required legacy functions directly (optionally, you can alias them as needed)
92+
# You could also alias the newer overhauled functions while testing them in small portions
93+
# of your application.
94+
from privex.helpers.net.base import check_host
95+
from privex.helpers.net import check_host as new_check_host
96+
97+
if check_host('www.privex.io', 443, http_test=True, use_ssl=True):
98+
print('[old check_host] https://www.privex.io is up')
99+
if new_check_host('files.privex.io', 443, http_test=True, use_ssl=True):
100+
print('[new check_host] https://files.privex.io is up')
101+
```
102+
103+
104+
-----------------------------------------------------------------------------------------------------------------------
105+
3106
2.8.0 - Refactoring, bug fixes + new loop_run function
4107
===============================================================================================
5108

privex/helpers/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def _setup_logging(level=logging.WARNING):
148148
log = _setup_logging()
149149
name = 'helpers'
150150

151-
VERSION = '2.19.0'
151+
VERSION = '3.0.0rc1'
152152

153153

154154

0 commit comments

Comments
 (0)