Skip to content

Commit 797b2dc

Browse files
committed
Initial release - v1.0.0
- Common helpers: random_str, empty, is_true, is_false, ErrHelpParser - Decorators: retry_on_err - Django helpers: handle_error, is_database_synchronized, model_to_dict, to_json - Net helpers: asn_to_name, ip_is_v4, ip_is_v6 - Basic usage information in README.md
0 parents  commit 797b2dc

File tree

11 files changed

+986
-0
lines changed

11 files changed

+986
-0
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
*.log
2+
*.swp
3+
__pycache__
4+
.idea
5+
.vscode
6+
7+
# PyPi build files
8+
build
9+
dist
10+
*.egg-info
11+

LICENSE

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
+===================================================+
2+
| © 2019 Privex Inc. |
3+
| https://www.privex.io |
4+
+===================================================+
5+
| |
6+
| Privex's Python Helpers |
7+
| License: X11/MIT |
8+
| |
9+
| Core Developer(s): |
10+
| |
11+
| (+) Chris (@someguy123) [Privex] |
12+
| (+) Kale (@kryogenic) [Privex] |
13+
| |
14+
+===================================================+
15+
16+
Privex's Python Helpers - a variety of python functions and classes that are useful in many projects
17+
Copyright (c) 2019 Privex Inc. ( https://www.privex.io )
18+
19+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
20+
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
21+
modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
22+
Software is furnished to do so, subject to the following conditions:
23+
24+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of
25+
the Software.
26+
27+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
28+
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
29+
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
30+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31+
32+
Except as contained in this notice, the name(s) of the above copyright holders shall not be used in advertising or
33+
otherwise to promote the sale, use or other dealings in this Software without prior written authorization.

README.md

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# Privex's Python Helpers
2+
3+
This small Python 3 module is comprised of various small functions and classes that were often
4+
copied and pasted across our projects.
5+
6+
Each of these "helper" functions, decorators or classes are otherwise too small to be independantly
7+
packaged, and so we've amalgamated them into this PyPi package, `privex-helpers`.
8+
9+
10+
```
11+
+===================================================+
12+
| © 2019 Privex Inc. |
13+
| https://www.privex.io |
14+
+===================================================+
15+
| |
16+
| Originally Developed by Privex Inc. |
17+
| |
18+
| Core Developer(s): |
19+
| |
20+
| (+) Chris (@someguy123) [Privex] |
21+
| (+) Kale (@kryogenic) [Privex] |
22+
| |
23+
+===================================================+
24+
```
25+
26+
# Install
27+
28+
### Download and install from PyPi using pip (recommended)
29+
30+
```sh
31+
pip3 install privex-helpers
32+
```
33+
34+
### (Alternative) Manual install from Git
35+
36+
**Option 1 - Use pip to install straight from Github**
37+
38+
```sh
39+
pip3 install git+https://github.com/Privex/python-helpers
40+
```
41+
42+
**Option 2 - Clone and install manually**
43+
44+
```bash
45+
# Clone the repository from Github
46+
git clone https://github.com/Privex/python-helpers
47+
cd python-helpers
48+
49+
# RECOMMENDED MANUAL INSTALL METHOD
50+
# Use pip to install the source code
51+
pip3 install .
52+
53+
# ALTERNATIVE MANUAL INSTALL METHOD
54+
# If you don't have pip, or have issues with installing using it, then you can use setuptools instead.
55+
python3 setup.py install
56+
```
57+
58+
# License
59+
60+
**Python Log Helper** was created by [Privex Inc. of Belize City](https://www.privex.io), and licensed under the X11/MIT License.
61+
See the file [LICENSE](https://github.com/Privex/python-loghelper/blob/master/LICENSE) for the license text.
62+
63+
**TL;DR; license:**
64+
65+
We offer no warranty. You can copy it, modify it, use it in projects with a different license, and even in commercial (paid for) software.
66+
67+
The most important rule is - you **MUST** keep the original license text visible (see `LICENSE`) in any copies.
68+
69+
70+
71+
# Example uses
72+
73+
We export all of the submodule's contents in `privex/helpers/__init__.py`, so you can import any
74+
function/class/attribute straight from `privex.helper` without needing several import lines.
75+
76+
Here are some of the most useful examples (part of our `.common` module, no dependencies)
77+
78+
```python
79+
from privex.helpers import empty, is_true, random_str, ip_is_v4, ip_is_v6
80+
81+
####
82+
# Our empty() helper is very convenient and easy to remember. It allows you to quick check if a variable is "empty"
83+
# (a blank string, None, zero, or an empty list/dict/tuple).
84+
#
85+
# empty(v, zero: bool = False, itr: bool = False) -> bool
86+
#
87+
# For safety, it only returns True for empty iterables / integer zero (0) if you enable `zero` and/or `itr` respectively.
88+
####
89+
90+
x = ''
91+
if empty(x):
92+
print('Var x is empty: either None or empty string')
93+
94+
y = []
95+
if empty(y, itr=True):
96+
print('Var y is empty: either None, empty string, or empty iterable')
97+
98+
####
99+
# Our is_true() / is_false() helpers are designed to ease checking boolean values from plain text config files
100+
# such as .env files, or values passed in an API call
101+
####
102+
103+
# The strings 'true' / 'y' / 'yes' / '1' are all considered truthy, plus int 1 / bool True
104+
enable_x = 'YES' # String values are automatically cast to lowercase, so even 'YeS' and 'TrUe' are fine.
105+
if is_true(enable_x):
106+
print('Enabling feature X')
107+
108+
####
109+
# Need to generate a random alphanumeric string for a password / API key? Try random_str(), which uses SystemRandom()
110+
# for cryptographically secure randomness, and by default uses our SAFE_CHARS character set, removing look-alike
111+
# characters such as 1 and l, or o and 0
112+
####
113+
114+
# Default random string - 50 character alphanum without easily mistaken chars
115+
random_str() # outputs: 'MrCWLYMYtT9A7bHc5ZNE4hn7PxHPmsWaT9GpfCkmZASK7ApN8r'
116+
117+
# Customised random string - 12 characters using only the characters `abcdef12345`
118+
random_str(12, chars='abcdef12345') # outputs: 'aba4cc14a43d'
119+
120+
####
121+
# As a server hosting provider, we deal with IP addresses a lot :)
122+
# The helper functions ip_is_v4 and ip_is_v6 do exactly as their name says, they return a boolean
123+
# if an IP is IPv4 or IPv6 respectively.
124+
####
125+
126+
ip_is_v4('192.168.1.1') # True
127+
ip_is_v4('2a07:e00::1') # False
128+
129+
ip_is_v6('192.168.1.1') # False
130+
ip_is_v6('2a07:e00::1') # True
131+
132+
```
133+
134+
# Minimal dependencies
135+
136+
Most of our helper code is independant, and does not result in any extra dependencies being installed.
137+
138+
Some of our helpers are dependant on external libraries or frameworks, such as Django or Flask. To avoid
139+
large Python packages such as Django being installed needlessly, we programatically enable/disable some
140+
of the helpers based on whether you have the required dependency installed.
141+
142+
This package only requires (and automatically installs if needed) a single dependency - our
143+
[privex-loghelper](https://github.com/Privex/python-loghelper) package, which itself is lightweight
144+
and dependency free.
145+
146+
147+
Optional requirements (just `pip3 install` them depending on the helpers you require):
148+
149+
```
150+
# For all Django-specific helpers in privex.helpers.django
151+
Django
152+
# For certain DNS dependant helpers in privex.helpers.net
153+
dnspython>=1.16.0
154+
```
155+
156+
# Contributing
157+
158+
We're happy to accept pull requests, no matter how small.
159+
160+
Please make sure any changes you make meet these basic requirements:
161+
162+
- No additional dependencies. We want our helper package to be lightweight and painless to install.
163+
- Any code taken from other projects should be compatible with the MIT License
164+
- This is a new project, and as such, supporting Python versions prior to 3.4 is very low priority.
165+
- However, we're happy to accept PRs to improve compatibility with older versions of Python, as long as it doesn't:
166+
- drastically increase the complexity of the code
167+
- OR cause problems for those on newer versions of Python.
168+
169+
**Legal Disclaimer for Contributions**
170+
171+
Nobody wants to read a long document filled with legal text, so we've summed up the important parts here.
172+
173+
If you contribute content that you've created/own to projects that are created/owned by Privex, such as code or
174+
documentation, then you might automatically grant us unrestricted usage of your content, regardless of the open source
175+
license that applies to our project.
176+
177+
If you don't want to grant us unlimited usage of your content, you should make sure to place your content
178+
in a separate file, making sure that the license of your content is clearly displayed at the start of the file
179+
(e.g. code comments), or inside of it's containing folder (e.g. a file named LICENSE).
180+
181+
You should let us know in your pull request or issue that you've included files which are licensed
182+
separately, so that we can make sure there's no license conflicts that might stop us being able
183+
to accept your contribution.
184+
185+
If you'd rather read the whole legal text, it should be included as `privex_contribution_agreement.txt`.
186+
187+
188+
# Thanks for reading!
189+
190+
**If this project has helped you, consider [grabbing a VPS or Dedicated Server from Privex](https://www.privex.io) - prices start at as little as US$8/mo (we take cryptocurrency!)**

privex/__init__.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
__path__ = __import__('pkgutil').extend_path(__path__, __name__)
2+
3+
"""
4+
+===================================================+
5+
| © 2019 Privex Inc. |
6+
| https://www.privex.io |
7+
+===================================================+
8+
| |
9+
| Originally Developed by Privex Inc. |
10+
| |
11+
| Core Developer(s): |
12+
| |
13+
| (+) Chris (@someguy123) [Privex] |
14+
| (+) Kale (@kryogenic) [Privex] |
15+
| |
16+
+===================================================+
17+
18+
Copyright 2019 Privex Inc. ( https://www.privex.io )
19+
20+
Permission is hereby granted, free of charge, to any person obtaining a copy of
21+
this software and associated documentation files (the "Software"), to deal in
22+
the Software without restriction, including without limitation the rights to use,
23+
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
24+
Software, and to permit persons to whom the Software is furnished to do so,
25+
subject to the following conditions:
26+
27+
The above copyright notice and this permission notice shall be included in all
28+
copies or substantial portions of the Software.
29+
30+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
31+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
32+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
35+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36+
"""

privex/helpers/__init__.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
+===================================================+
3+
| © 2019 Privex Inc. |
4+
| https://www.privex.io |
5+
+===================================================+
6+
| |
7+
| Originally Developed by Privex Inc. |
8+
| |
9+
| Core Developer(s): |
10+
| |
11+
| (+) Chris (@someguy123) [Privex] |
12+
| (+) Kale (@kryogenic) [Privex] |
13+
| |
14+
+===================================================+
15+
16+
Copyright 2019 Privex Inc. ( https://www.privex.io )
17+
18+
Permission is hereby granted, free of charge, to any person obtaining a copy of
19+
this software and associated documentation files (the "Software"), to deal in
20+
the Software without restriction, including without limitation the rights to use,
21+
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
22+
Software, and to permit persons to whom the Software is furnished to do so,
23+
subject to the following conditions:
24+
25+
The above copyright notice and this permission notice shall be included in all
26+
copies or substantial portions of the Software.
27+
28+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
29+
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
30+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
31+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
32+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
33+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
34+
"""
35+
36+
import logging
37+
from privex.loghelper import LogHelper
38+
39+
# Set up logging for the entire module ``privex.helpers`` . Since this is a package, we don't add any
40+
# console or file logging handlers, we purely just set our minimum logging level to WARNING to avoid
41+
# spamming the logs of any application importing it.
42+
def _setup_logging(level=logging.WARNING):
43+
lh = LogHelper(__name__, level=level)
44+
return lh.get_logger()
45+
46+
log = _setup_logging()
47+
48+
name = 'helpers'
49+
50+
# Only import the Django functions if Django is actually installed
51+
try:
52+
import django
53+
from privex.helpers.django import *
54+
except ImportError:
55+
log.debug('privex.helpers __init__ failed to import "django", not loading django helpers')
56+
pass
57+
58+
59+
from privex.helpers.common import *
60+
from privex.helpers.decorators import *
61+
from privex.helpers.net import *

0 commit comments

Comments
 (0)