opentraveldata / geobases

Data services and visualization
http://opentraveldata.github.com/geobases/
Other
193 stars 41 forks source link

Save changes from Python API #14

Closed alexprengere closed 11 years ago

alexprengere commented 11 years ago

Idea

From the Python API, be able to save changes made with set, in the initial loaded file (if any).

Example for a normal base

from GeoBases import GeoBase

g = GeoBase('ori_por', verbose=False)
g.set('ORY', name='Orly')
g.set('new_por', name='new_name')

g.save()

This way the ori_por file would be updated with the new ORY name, and would have a new line for the entry new_por.

Example for a base without initial file

The mechanism can be a bit tricky for bases without regular initial file like feed, so we should be able to do something like this, where we explicitely define everything:

from GeoBases import GeoBase

g = GeoBase('feed')
g.set('key', name='name')

g.save(path='file.csv', headers=['name'])

Since g.fields are not automatically updated when data is set, this may be a good opportunity to use g.syncFields() to synchronise g.fields with the underlying data. If we resume he previous example:

g.fields # []
g.syncFields()
g.fields # ['__dup__', '__gar__', '__key__', '__lno__', '__par__', 'name']

# You may want to remove the __special__ fields
g.save(path='file.csv', headers=g.fields)

Conclusion

I implemented a draft of this specification in the develop branch. Comments/ideas are welcome!

da115115 commented 11 years ago

That sounds a very good idea :)

Please, do not forget that ori_por is a generated file. Hence, the modifications will be lost at the next update from OpenTravelData.

I would suggest, in a first easy step, just to create a patch file, which would then be sent by mail, and/or attached to an automatically created issue. Indeed, Geonames does not allow updates through its Web Services (WS), which would have been very convenient otherwise...

alexprengere commented 11 years ago

With 5f031638d67eb56daaad1b9cce58fb012cfafcd1, this works fine with delimited fields. This means that if you change the value of a delimited field (a tuple), then save the results, the file will be updated. Sub-delimiters should be consistent with those used during data loading.

Note that if you modify a raw version of a delimited field, like tvl_por_list@raw, changes will not be saved, since these fields are not dumped by default.