Skip to content

Commit

Permalink
Merge pull request #12 from Jenselme/find-all
Browse files Browse the repository at this point in the history
Add a way to find all layers matching an attribute
  • Loading branch information
geographika committed Apr 5, 2017
2 parents 28796b1 + 31e1599 commit f98ea6c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
11 changes: 11 additions & 0 deletions docs/examples/search.py
@@ -0,0 +1,11 @@
import mappyfile

# load will accept a filename (loads will accept a string)
mapfile = mappyfile.load("./docs/examples/raster.map")

# Search of a layer by its name
mappyfile.find(mapfile['layers'], 'name', 'my_layer')

# Search for all layers of a group
for layer in mappyfile.findall(mapfile['layers'], 'group', 'my_group'):
print(layer['name'])
6 changes: 6 additions & 0 deletions docs/index.rst
Expand Up @@ -101,6 +101,12 @@ Accessing Values

.. literalinclude:: examples/accessing_values.py
:language: python

Query
+++++

.. literalinclude:: examples/search.py
:language: python

Modifying Values
++++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion mappyfile/__init__.py
@@ -1,2 +1,2 @@
# allow high-level functions to be accessed directly from the mappyfile module
from mappyfile.utils import load, loads, find, dumps, write
from mappyfile.utils import load, loads, find, findall, dumps, write
5 changes: 5 additions & 0 deletions mappyfile/utils.py
Expand Up @@ -50,6 +50,11 @@ def find(lst, key, value):
def __find__(lst, key, value):
return next((item for item in lst if item[key.lower()] == value), None)

def findall(lst, key, value):
possible_values = ("'%s'" % value, '"%s"' % value)

return (item for item in lst if item[key.lower()] in possible_values)

def _save(output_file, map_string):

with codecs.open(output_file, "w", encoding="utf-8") as f:
Expand Down

0 comments on commit f98ea6c

Please sign in to comment.