Skip to content

Commit 8df8612

Browse files
committed
rename some methods to a more user-friendly syntax and add order method
1 parent 39a9b52 commit 8df8612

3 files changed

Lines changed: 39 additions & 11 deletions

File tree

README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ Retrieving entities from dandelion is easy, just instantiate a Datagem and itera
4444

4545
### Select fields
4646

47-
If you want to reduce the network load, you can retrieve only the fields you will actually use with `values`:
47+
If you want to reduce the network load, you can retrieve only the fields you will actually use with `select`:
4848

4949
>>> from dandelionimport Datagem
5050
>>> d = Datagem('datagem-slug')
51-
>>> for obj in d.objects.values('acheneID')[:10]:
51+
>>> for obj in d.objects.select('acheneID')[:10]:
5252
... print obj
5353
...
5454
{u'acheneID': u'http://dandelion.eu/resource/158dce3ea58cebda9a14a911cd7574f204f9de72'}
@@ -64,7 +64,7 @@ If you want to reduce the network load, you can retrieve only the fields you wil
6464

6565
It is also possible to rename fields if you need to:
6666

67-
>>> for obj in d.objects.values('acheneID', 'municipality.name AS "name"')[:10]:
67+
>>> for obj in d.objects.select('acheneID', 'municipality.name AS "name"')[:10]:
6868
... print obj
6969
...
7070
{u'acheneID': u'http://dandelion.eu/resource/158dce3ea58cebda9a14a911cd7574f204f9de72', u'name': u'Dresano'}
@@ -81,9 +81,9 @@ It is also possible to rename fields if you need to:
8181

8282
### Filter elements
8383

84-
You can powerfully filter the result set with the `filter` method:
84+
You can powerfully filter the result set with the `where` method:
8585

86-
>>> for obj in d.objects.filter(level=60)[:10]:
86+
>>> for obj in d.objects.where(level=60)[:10]:
8787
... print obj['acheneID'], obj['level']
8888
...
8989
http://dandelion.eu/resource/a7d36ba742d54e8a97f187cf3d33a3beb6043057 60
@@ -99,7 +99,7 @@ You can powerfully filter the result set with the `filter` method:
9999

100100
More complex filters can be achieved with the `__` (dunder) operator:
101101

102-
>>> for obj in d.objects.filter(level__lt=30)[:10]:
102+
>>> for obj in d.objects.where(level__lt=30)[:10]:
103103
... print obj['acheneID'], obj['country']['name']
104104
...
105105
http://dandelion.eu/resource/3058697c2d534d13b48f20e1d530dea1794d58b2 FRANCE
@@ -120,3 +120,27 @@ Available comparators are:
120120
* gte (greater-than-equals, `>=`)
121121
* gt (greater-than, `>`)
122122
* not (not, `<>`)
123+
124+
125+
### Sort elements
126+
127+
Sorting is easy as everything else, with the `sort` method:
128+
129+
>>> for obj in d.objects.select('acheneID').order('acheneID')[:5]:
130+
... print obj['acheneID']
131+
...
132+
http://dandelion.eu/resource/00017329d07750b26ffc0efb08c3a862b1898a0d
133+
http://dandelion.eu/resource/00026440c06c9774bfbb08e770167c9ad09124a1
134+
http://dandelion.eu/resource/0002abf01d8bdfcb13e5d01625c806ad2b3a06f3
135+
http://dandelion.eu/resource/0002e35e46df47106976c746606cc7188d1a039e
136+
http://dandelion.eu/resource/0003080a25b86105ba3f538b5d857b6a31b6646d
137+
138+
139+
>>> for obj in d.objects.select('acheneID').order('acheneID DESC')[:5]:
140+
... print obj['acheneID']
141+
...
142+
http://dandelion.eu/resource/ffff40dff5241feb1ecc394e79bfd820d54d43d8
143+
http://dandelion.eu/resource/ffff31c2ec23ff3a18da4804cdbd869c11120ca7
144+
http://dandelion.eu/resource/fffb71883dbeaf8511f58a6d5260c5cb1c52be74
145+
http://dandelion.eu/resource/fffb636f2df4a5be30e3d56da3df2dc388a534a5
146+
http://dandelion.eu/resource/fffacd9ff1bdaca8107642a1048be2bef5796a53

dandelion/datagem.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def __init__(self, datagem):
3737
self._step = 1
3838
self._stop = None
3939

40-
def filter(self, **kwargs):
40+
def where(self, **kwargs):
4141
new_filter = ' AND '.join(
4242
self._parse_single_filter(key, value)
4343
for key, value in kwargs.items()
@@ -52,12 +52,16 @@ def filter(self, **kwargs):
5252
return self
5353

5454
def get(self, **kwargs):
55-
return self.filter(**kwargs).__iter__().next()
55+
return self.where(**kwargs).__iter__().next()
5656

57-
def values(self, *args):
57+
def select(self, *args):
5858
self.params['$select'] = ','.join(args)
5959
return self
6060

61+
def order(self, *args):
62+
self.params['$order'] = ','.join(args)
63+
return self
64+
6165
def __iter__(self):
6266
offset = self.params.get('$offset', 0)
6367
returned = 0

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
setup(
77
name = 'dandelion-eu',
88
packages = ['dandelion'],
9-
version = '0.1.1',
9+
version = '0.1.2',
1010
description = 'Connect to the dandelion.eu API in a very pythonic way!',
1111
author = 'Stefano Parmesan',
1212
author_email = 'parmesan@spaziodati.eu',
1313
url = 'https://github.com/armisael/python-dandelion',
14-
download_url = 'https://github.com/armisael/python-dandelion/tarball/0.1.1',
14+
download_url = 'https://github.com/armisael/python-dandelion/tarball/0.1.2',
1515
keywords = ['api', 'dandelion'],
1616
install_requires=[
1717
'requests',

0 commit comments

Comments
 (0)