Skip to content

Commit e678cac

Browse files
committed
v1.4.0 (BREAKING) - Added inject_items + cleanup
Notable Changes =============== **New Features** - Added `common.inject_items` - a small function for injecting a list into another list at an arbitrary position. **General Improvements/Fixes** - Fixed `net.asn_to_name` so that it correctly raises `KeyError` or returns `'Unknown ASN'` when dnspython raises `NoAnswer` - Added `plugin.HAS_DNSPYTHON` attribute, for checking whether the dnspython dependent functions/classes were loaded or not - (POTENTIALLY BREAKING CHANGE) `__init__.py` no longer loads the `django` module, as it was causing endless issues related to Django not being configured, or not being ready for accessing certain modules. **Documentation** - Fixed small typo in `cache.RedisCache` pydoc block - Re-organised Unit Tests section to make it more readable - Re-named the cache module into "Cache Abstraction Layer", as the module page contains a lot of hand written documentation about it. - Re-organised the code documentation to be top level - Added **Usage Examples** section - Fleshed out the index page of the docs **Unit Test updates** - Extracted tests from `tests.py` into individual files in `tests` folder, as tests.py was close to 500 lines long and growing... - Added unit tests for `net.asn_to_name` - Added unit tests for `common.inject_items`
1 parent bf1f649 commit e678cac

44 files changed

Lines changed: 1505 additions & 1694 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ __pycache__
99
build
1010
dist
1111
*.egg-info
12+
venv/
1213

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ python:
1313
install:
1414
- pip install -r docs/requirements.txt
1515
- pip install .
16-
script: pytest --cov=./privex tests.py
16+
script: pytest --cov=./privex -v
1717
after_success:
1818
- codecov

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Documentation Status](https://readthedocs.org/projects/python-helpers/badge/?version=latest)](https://python-helpers.readthedocs.io/en/latest/?badge=latest)
44
[![Build Status](https://travis-ci.com/Privex/python-helpers.svg?branch=master)](https://travis-ci.com/Privex/python-helpers)
5-
[![Codecov](https://img.shields.io/codecov/c/github/Privex/python-helpers)](https://codecov.io/gh/Privex/python-helpers)
5+
[![Codecov](https://img.shields.io/codecov/c/github/Privex/python-helpers)](https://codecov.io/gh/Privex/python-helpers)
66
[![PyPi Version](https://img.shields.io/pypi/v/privex-helpers.svg)](https://pypi.org/project/privex-helpers/)
77
![License Button](https://img.shields.io/pypi/l/privex-helpers)
88
![PyPI - Downloads](https://img.shields.io/pypi/dm/privex-helpers)
@@ -12,7 +12,7 @@
1212
This small Python 3 module is comprised of various small functions and classes that were often
1313
copied and pasted across our projects.
1414

15-
Each of these "helper" functions, decorators or classes are otherwise too small to be independantly
15+
Each of these "helper" functions, decorators or classes are otherwise too small to be independently
1616
packaged, and so we've amalgamated them into this PyPi package, `privex-helpers`.
1717

1818

@@ -34,7 +34,15 @@ packaged, and so we've amalgamated them into this PyPi package, `privex-helpers`
3434

3535
# Install
3636

37-
### Download and install from PyPi using pip (recommended)
37+
### Download and install from PyPi
38+
39+
**Using [Pipenv](https://pipenv.kennethreitz.org/en/latest/) (recommended)**
40+
41+
```sh
42+
pipenv install privex-helpers
43+
```
44+
45+
**Using standard Python pip**
3846

3947
```sh
4048
pip3 install privex-helpers

docs/source/examples.rst

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
##############
2+
Example Usages
3+
##############
4+
5+
Boolean testing
6+
===============
7+
8+
The ``empty`` function
9+
-----------------------
10+
11+
The :func:`empty` function in our opinion, is one of most useful functions in this library. It allows for a clean,
12+
readable method of checking if a variable is "empty", e.g. when checking keyword arguments to a function.
13+
14+
With a single argument, it simply tests if a variable is ``""`` (empty string) or ``None``.
15+
16+
The argument ``itr`` can be set to ``True`` if you consider an empty iterable such as ``[]`` or ``{}`` as "empty". This
17+
functionality also supports objects which implement ``__len__``, and also checks to ensure ``__len__`` is available,
18+
avoiding an exception if an object doesn't support it.
19+
20+
The argument ``zero`` can be set to ``True`` if you want to consider ``0`` (integer) and ``'0'`` (string) as "empty".
21+
22+
.. code-block:: python
23+
24+
from privex.helpers import empty
25+
26+
x, y = "", None
27+
z, a = [], 0
28+
29+
empty(x) # True
30+
empty(y) # True
31+
empty(z) # False
32+
empty(z, itr=True) # True
33+
empty(a) # False
34+
empty(a, zero=True) # True
35+
36+
37+
The ``is_true`` and ``is_false`` functions
38+
------------------------------------------
39+
40+
When handling user input, whether from an environment file (``.env``), or from data passed to a web API, it can be
41+
a pain attempting to check for booleans.
42+
43+
A boolean ``True`` could be represented as the string ``'true'``, ``'1'``, ``'YES'``, as an integer ``1``, or even
44+
an actual boolean ``True``. Trying to test for all of those cases requires a rather long ``if`` statement...
45+
46+
Thus :func:`.is_true` and :func:`.is_false` were created.
47+
48+
.. code-block:: python
49+
50+
from privex.helpers import is_true, is_false
51+
52+
is_true(0) # False
53+
is_true(1) # True
54+
is_true('1') # True
55+
is_true('true') # True
56+
is_true('false') # False
57+
is_true('orange') # False
58+
is_true('YeS') # True
59+
60+
is_false(0) # True
61+
is_false('false') # True
62+
is_false('true') # False
63+
is_false(False) # True
64+
65+
66+
Handling environmental variables in different formats
67+
=====================================================
68+
69+
Using ``env_csv`` to support lists contained within an env var
70+
---------------------------------------------------------------
71+
72+
The function :func:`.env_csv` parses a CSV-like environment variable into a list
73+
74+
.. code-block:: python
75+
76+
from privex.helpers import env_csv
77+
import os
78+
os.environ['EXAMPLE'] = "this, is, an,example "
79+
80+
env_csv('EXAMPLE', ['error'])
81+
# returns: ['this', 'is', 'an', 'example']
82+
env_csv('NOEXIST', ['non-existent'])
83+
# returns: ['non-existent']
84+
85+
Using ``env_keyval`` to support dictionaries contained within an env var
86+
------------------------------------------------------------------------
87+
88+
The function :func:`.env_keyval` parses an environment variable into a ordered list of tuple pairs, which can be
89+
easily converted into a dictionary using ``dict()``.
90+
91+
.. code-block:: python
92+
93+
from privex.helpers import env_keyval
94+
import os
95+
os.environ['EXAMPLE'] = "John: Doe , Jane : Doe, Aaron:Smith"
96+
97+
env_keyval('EXAMPLE')
98+
# returns: [('John', 'Doe'), ('Jane', 'Doe'), ('Aaron', 'Smith')]
99+
env_keyval('NOEXIST', {})
100+
# returns: {}
101+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Dictable
2+
========
3+
4+
.. currentmodule:: privex.helpers.common
5+
6+
.. autoclass:: Dictable
7+
8+
9+
.. automethod:: __init__
10+
11+
12+
.. rubric:: Methods
13+
14+
.. autosummary::
15+
16+
~Dictable.__init__
17+
~Dictable.from_dict
18+
19+
20+
21+
22+
23+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
ErrHelpParser
2+
=============
3+
4+
.. currentmodule:: privex.helpers.common
5+
6+
.. autoclass:: ErrHelpParser
7+
8+
9+
.. automethod:: __init__
10+
11+
12+
.. rubric:: Methods
13+
14+
.. autosummary::
15+
16+
~ErrHelpParser.__init__
17+
~ErrHelpParser.add_argument
18+
~ErrHelpParser.add_argument_group
19+
~ErrHelpParser.add_mutually_exclusive_group
20+
~ErrHelpParser.add_subparsers
21+
~ErrHelpParser.convert_arg_line_to_args
22+
~ErrHelpParser.error
23+
~ErrHelpParser.exit
24+
~ErrHelpParser.format_help
25+
~ErrHelpParser.format_usage
26+
~ErrHelpParser.get_default
27+
~ErrHelpParser.parse_args
28+
~ErrHelpParser.parse_intermixed_args
29+
~ErrHelpParser.parse_known_args
30+
~ErrHelpParser.parse_known_intermixed_args
31+
~ErrHelpParser.print_help
32+
~ErrHelpParser.print_usage
33+
~ErrHelpParser.register
34+
~ErrHelpParser.set_defaults
35+
36+
37+
38+
39+
40+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
inject\_items
2+
=============
3+
4+
.. currentmodule:: privex.helpers.common
5+
6+
.. autofunction:: inject_items

docs/source/helpers/index.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Code Docs for each helper module
2-
=================================
31

42
.. autosummary::
53
:toctree:
@@ -14,10 +12,4 @@ Code Docs for each helper module
1412
privex.helpers.plugin
1513
privex.helpers.settings
1614

17-
Unit Tests
18-
==========
1915

20-
.. autosummary::
21-
:toctree:
22-
23-
tests

docs/source/helpers/modules.rst

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/source/helpers/privex.helpers.cache.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
privex.helpers.cache
2-
====================
1+
Cache Abstraction Layer
2+
=======================
3+
4+
**Module:** ``privex.helpers.cache``
5+
6+
Classes
7+
^^^^^^^
38

49
.. autosummary::
510
:toctree: cache
@@ -10,7 +15,7 @@ privex.helpers.cache
1015

1116

1217
Functions
13-
---------
18+
^^^^^^^^^
1419

1520
.. automodule:: privex.helpers.cache
1621

0 commit comments

Comments
 (0)