add snippets keys_only + values_only + test bp

This commit is contained in:
Rob-Rychs
2018-04-01 13:56:31 -07:00
parent 7a296dafa2
commit 57c3f5f1ab
6 changed files with 63 additions and 0 deletions

23
snippets/keys_only.md Normal file
View File

@ -0,0 +1,23 @@
### keys_only
Function which accepts a dictionary of key value pairs and returns a new flat list of only the keys.
Uses the .items() function with a for loop on the dictionary to track both the key and value and returns a new list by appending the keys to it. Best used on 1 level-deep key:value pair dictionaries and not nested data-structures.
``` python
def keys_only(dict):
lst = []
for k, v in dict.items():
lst.append(k)
return lst
```
``` python
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
}
keys_only(ages) # result
['Peter', 'Isabel', 'Anna']
```

23
snippets/values_only.md Normal file
View File

@ -0,0 +1,23 @@
### values_only
Function which accepts a dictionary of key value pairs and returns a new flat list of only the values.
Uses the .items() function with a for loop on the dictionary to track both the key and value of the iteration and returns a new list by appending the values to it. Best used on 1 level-deep key:value pair dictionaries and not nested data-structures.
``` python
def values_only(dict):
lst = []
for k, v in dict.items():
lst.append(v)
return lst
```
``` python
ages = {
"Peter": 10,
"Isabel": 11,
"Anna": 9,
}
values_only(ages) # result
[10, 11, 9]
```

View File

@ -0,0 +1,6 @@
import types,functools
from pytape import test
from keys_only import keys_only
def keys_only_test(t):
t.true(isinstance(keys_only, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'keys_only is a function')
test('Testing keys_only',keys_only_test)

View File

View File

@ -0,0 +1,5 @@
def values_only(dict):
lst = []
for k, v in dict.items():
lst.append(v)
return lst

View File

@ -0,0 +1,6 @@
import types,functools
from pytape import test
from values_only import values_only
def values_only_test(t):
t.true(isinstance(values_only, (types.BuiltinFunctionType, types.FunctionType, functools.partial)),'values_only is a function')
test('Testing values_only',values_only_test)