|
| 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!)** |
0 commit comments