# Tools by Ouroboros Coding
[![pypi version](https://img.shields.io/pypi/v/Tools-OC.svg)](https://pypi.org/project/Tools-OC) ![MIT License](https://img.shields.io/pypi/l/Tools-OC.svg)
A set of functions for common python problems
## Requires
Tools-OC requires python 3.10 or higher
## Installation
```bash
pip install Tools-OC
```
## Functions
### clone
`clone` is used to make a complete copy of a dictionary from top to bottom. It follows keys that are either dictionaries or lists and clones them as well, but copies anything else as is. Great for copying raw data like JSON, not great for complex structures containing class instances.
```python
>>> from tools import clone
>>> a = {'hello': 'their'}
>>> b = a
>>> a['hello'] = 'there'
>>> b['hello']
'there'
>>> b = clone(a)
>>> a['hello'] = 'fruit loops'
>>> b['hello']
'there'
```
### combine
`combine` is used to generate a new dictionary by cloning the first one passed, then by merging the second into it, and returning it
```python
>>> from tools import combine
>>> a = { 'one': 1 }
>>> b = { 'two': 2 }
>>> c = combine(a, b)
>>> c
{'one': 1, 'two': 2}
>>> d = combine(c, { 'one': 'une' })
>>> d
{'one': 'une', 'two': 2}
```
### compare
`compare` is used to compare any two values. It will compare dicts and lists by traversing them, but will check any other value one to one. Like clone, it is very useful for raw data like JSON, but not great for anything with complex data like class instances unless they take care of overloading \_\_eq\_\_
```python
>>> from tools import compare
>>> compare({'one': 1, 'two': 2}, {'two': 2, 'one': 1})
True
>>> compare([1, 2, 3], [3, 2, 1])
False
>>> compare([{'one': 1}], [{'one': 1}])
True
```
### evaluate
`evaluate` is used to evaluate if a dictionary contains the keys it requires, without a lot of complicated configuration. It's meant for simple dictionaries, but can also check keys of keys
```python
>>> from tools import evaluate
>>> evaluate({'one': 1, 'two': 2}, ['one', 'two', 'three'])
ValueError: three
```
### get_client_ip
Used to get the actual IP address of the client by using the provided dictionary of environment variables
```python
>>> from tools import get_client_ip
>>> from bottle import request
>>> get_client_ip(request.environ)
'195.201.123.59'
```
### keys_to_ints
Traverses a dictionary and converts any keys from strings to integers. Helpful for processing data like JSON that won't allow keys as anything other than strings
```python
>>> from tools import keys_to_ints
>>> keys_to_ints({'1': 'one', '2': 'two'})
{1: 'one', 2: 'two'}
```
### lfindi
Steps through the given list of dictionaries looking for one with a key that matches the value, and returns the index of that dictionary in the list, else -1 for no dictionary found.
```python
>>> from tools import lfindi
>>> l = [
... {'name': 'Bob', 'job': 'Accountant'},
... {'name': 'Frank', 'job': 'Salesman'}
... ]
>>> lfindi(l, 'name', 'Frank')
1
>>> lfindi(l, 'name', 'Stan')
-1
```
### lfindd
Works exactly the same as lfindi, but returns the dictionary instead of its index
```python
>>> from tools import lfindd
>>> l = [
... {'name': 'Bob', 'job': 'Accountant'},
... {'name': 'Frank', 'job': 'Salesman'}
... ]
>>> lfindd(l, 'name', 'Stan') # Returns None, which does not display
>>> lfindd(l, 'name', 'Frank')
{'name': 'Frank', 'job': 'Salesman'}
```
### merge
Works exactly the same as the `combine` function, but instead of creating a new dict by cloning the first one, that step is skipped and the second dict is simple merged with the first and returned altered
```python
>>> from tools import merge
>>> a = { 'one': 1, 'three': { 'four': 4 } }
>>> b = { 'two': 2, 'three': { 'four': 'quatre' }}
>>> merge(a, b)
{'one': 1, 'three': {'four': 'quatre'}, 'two': 2}
>>> a
{'one': 1, 'three': {'four': 'quatre'}, 'two': 2}
```
`merge` contains an optional third parameter called `return_changes` that will return the differences found while merging the second dict over the first.
```python
>>> from tools import merge
>>> a = { 'one': 1, 'three': { 'four': 4 } }
>>> b = { 'two': 2, 'three': { 'four': 'quatre' }}
>>> merge(a, b, True)
{'two': 2, 'three': {'four': 'quatre'}}
>>> merge(a, {'one': 1, 'three': { 'four': 4 }}, True)
{'three': {'four': 4}}
```
### without
`without` is used to strip out one or more keys from a dictionary, or a list of dictionaries
```python
>>> from tools import without
>>> l = [
... {'one': 'one', 'two': 'two', 'three': 'three', 'four': 'four'},
... {'one': 'une', 'two': 'deux', 'three': 'trois', 'four': 'quatre'},
... {'one': 'uno', 'two': 'dos', 'three': 'tres', 'four': 'cuatro'}
... ]
>>> without(l, ['three', 'four'])
[{'one': 'one', 'two': 'two'}, {'one': 'une', 'two': 'deux'}, {'one': 'uno', 'two': 'dos'}]
>>> without(l[2], 'four')
{'one': 'uno', 'two': 'dos', 'three': 'tres'}
```
Raw data
{
"_id": null,
"home_page": "https://github.com/ouroboroscoding/tools-python",
"name": "Tools-OC",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": "",
"keywords": "tools,clone,merge,combine",
"author": "Chris Nasr - Ouroboros Coding Inc.",
"author_email": "chris@ouroboroscoding.com",
"download_url": "https://files.pythonhosted.org/packages/1b/fb/e38762c095a10dad356f6ffff3e0656455003fe769bc014f6a2b91c5db9f/Tools-OC-1.2.1.tar.gz",
"platform": null,
"description": "# Tools by Ouroboros Coding\n[![pypi version](https://img.shields.io/pypi/v/Tools-OC.svg)](https://pypi.org/project/Tools-OC) ![MIT License](https://img.shields.io/pypi/l/Tools-OC.svg)\n\nA set of functions for common python problems\n\n## Requires\nTools-OC requires python 3.10 or higher\n\n## Installation\n```bash\npip install Tools-OC\n```\n\n## Functions\n\n### clone\n`clone` is used to make a complete copy of a dictionary from top to bottom. It follows keys that are either dictionaries or lists and clones them as well, but copies anything else as is. Great for copying raw data like JSON, not great for complex structures containing class instances.\n```python\n>>> from tools import clone\n>>> a = {'hello': 'their'}\n>>> b = a\n>>> a['hello'] = 'there'\n>>> b['hello']\n'there'\n>>> b = clone(a)\n>>> a['hello'] = 'fruit loops'\n>>> b['hello']\n'there'\n```\n\n### combine\n`combine` is used to generate a new dictionary by cloning the first one passed, then by merging the second into it, and returning it\n```python\n>>> from tools import combine\n>>> a = { 'one': 1 }\n>>> b = { 'two': 2 }\n>>> c = combine(a, b)\n>>> c\n{'one': 1, 'two': 2}\n>>> d = combine(c, { 'one': 'une' })\n>>> d\n{'one': 'une', 'two': 2}\n```\n\n### compare\n`compare` is used to compare any two values. It will compare dicts and lists by traversing them, but will check any other value one to one. Like clone, it is very useful for raw data like JSON, but not great for anything with complex data like class instances unless they take care of overloading \\_\\_eq\\_\\_\n```python\n>>> from tools import compare\n>>> compare({'one': 1, 'two': 2}, {'two': 2, 'one': 1})\nTrue\n>>> compare([1, 2, 3], [3, 2, 1])\nFalse\n>>> compare([{'one': 1}], [{'one': 1}])\nTrue\n```\n\n### evaluate\n`evaluate` is used to evaluate if a dictionary contains the keys it requires, without a lot of complicated configuration. It's meant for simple dictionaries, but can also check keys of keys\n```python\n>>> from tools import evaluate\n>>> evaluate({'one': 1, 'two': 2}, ['one', 'two', 'three'])\nValueError: three\n```\n\n### get_client_ip\nUsed to get the actual IP address of the client by using the provided dictionary of environment variables\n```python\n>>> from tools import get_client_ip\n>>> from bottle import request\n>>> get_client_ip(request.environ)\n'195.201.123.59'\n```\n\n### keys_to_ints\nTraverses a dictionary and converts any keys from strings to integers. Helpful for processing data like JSON that won't allow keys as anything other than strings\n```python\n>>> from tools import keys_to_ints\n>>> keys_to_ints({'1': 'one', '2': 'two'})\n{1: 'one', 2: 'two'}\n```\n\n### lfindi\nSteps through the given list of dictionaries looking for one with a key that matches the value, and returns the index of that dictionary in the list, else -1 for no dictionary found.\n```python\n>>> from tools import lfindi\n>>> l = [\n... {'name': 'Bob', 'job': 'Accountant'},\n... {'name': 'Frank', 'job': 'Salesman'}\n... ]\n>>> lfindi(l, 'name', 'Frank')\n1\n>>> lfindi(l, 'name', 'Stan')\n-1\n```\n\n### lfindd\nWorks exactly the same as lfindi, but returns the dictionary instead of its index\n```python\n>>> from tools import lfindd\n>>> l = [\n... {'name': 'Bob', 'job': 'Accountant'},\n... {'name': 'Frank', 'job': 'Salesman'}\n... ]\n>>> lfindd(l, 'name', 'Stan') # Returns None, which does not display\n>>> lfindd(l, 'name', 'Frank')\n{'name': 'Frank', 'job': 'Salesman'}\n```\n\n### merge\nWorks exactly the same as the `combine` function, but instead of creating a new dict by cloning the first one, that step is skipped and the second dict is simple merged with the first and returned altered\n```python\n>>> from tools import merge\n>>> a = { 'one': 1, 'three': { 'four': 4 } }\n>>> b = { 'two': 2, 'three': { 'four': 'quatre' }}\n>>> merge(a, b)\n{'one': 1, 'three': {'four': 'quatre'}, 'two': 2}\n>>> a\n{'one': 1, 'three': {'four': 'quatre'}, 'two': 2}\n```\n`merge` contains an optional third parameter called `return_changes` that will return the differences found while merging the second dict over the first.\n```python\n>>> from tools import merge\n>>> a = { 'one': 1, 'three': { 'four': 4 } }\n>>> b = { 'two': 2, 'three': { 'four': 'quatre' }}\n>>> merge(a, b, True)\n{'two': 2, 'three': {'four': 'quatre'}}\n>>> merge(a, {'one': 1, 'three': { 'four': 4 }}, True)\n{'three': {'four': 4}}\n```\n\n### without\n`without` is used to strip out one or more keys from a dictionary, or a list of dictionaries\n```python\n>>> from tools import without\n>>> l = [\n... {'one': 'one', 'two': 'two', 'three': 'three', 'four': 'four'},\n... {'one': 'une', 'two': 'deux', 'three': 'trois', 'four': 'quatre'},\n... {'one': 'uno', 'two': 'dos', 'three': 'tres', 'four': 'cuatro'}\n... ]\n>>> without(l, ['three', 'four'])\n[{'one': 'one', 'two': 'two'}, {'one': 'une', 'two': 'deux'}, {'one': 'uno', 'two': 'dos'}]\n>>> without(l[2], 'four')\n{'one': 'uno', 'two': 'dos', 'three': 'tres'}\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A set of tools for common python problems",
"version": "1.2.1",
"project_urls": {
"Homepage": "https://github.com/ouroboroscoding/tools-python",
"Source": "https://github.com/ouroboroscoding/tools-python",
"Tracker": "https://github.com/ouroboroscoding/tools-python/issues"
},
"split_keywords": [
"tools",
"clone",
"merge",
"combine"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1bfbe38762c095a10dad356f6ffff3e0656455003fe769bc014f6a2b91c5db9f",
"md5": "fecbcfc58c0650d2923be0d4893c7017",
"sha256": "978e582ab1d926094507f05ef35e72721a79da8221d1afc84e6693e3f405e6a0"
},
"downloads": -1,
"filename": "Tools-OC-1.2.1.tar.gz",
"has_sig": false,
"md5_digest": "fecbcfc58c0650d2923be0d4893c7017",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 7926,
"upload_time": "2023-08-24T11:12:11",
"upload_time_iso_8601": "2023-08-24T11:12:11.087577Z",
"url": "https://files.pythonhosted.org/packages/1b/fb/e38762c095a10dad356f6ffff3e0656455003fe769bc014f6a2b91c5db9f/Tools-OC-1.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-24 11:12:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ouroboroscoding",
"github_project": "tools-python",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "tools-oc"
}