Object relational mapping from csv file. This module will load csv to memory and treat it like Object. This module main purpose is to ease up csv usage in small application, mainly in use in sandboxing. It's highly ineficient and doesn't support multiple instance.
CSV RM provide basic orm utility such as :
- Load cscv to object
- Create new record
- Update existing record
- Delete records
- Search records
What not supported yet:
- Transaction
- Concurrent usage
Just install it directly using pip. No dependecies needed.
pip install csvrm
Or you can just copy entire source code.
- Define model structure Defining new model by inheriting Model class. Put csv file location inside _filename variable. Define each fields you want to extract by specifiy it as object.
from csvrm import models, fields
class Person(models.Model):
_filename = '[filepath]'
id = fields.Integer()
name = fields.String()
- Create records
p = Person(load = true)
p.create({
'id': 1,
'name': 'person name'
})
p.save()
- Seach records
p = Person(load = true)
result = p.search(lambda x: x.name == 'person name')
for res in result:
print(res.name)
- Update records Updating record can be done using either update methods or directly change properties
p = Person(load = true)
result = p.search(lambda x: x.name == 'person name')
for res in result:
res.name = 'new person name'
p.save()
# OR
p.update(
domain=(lambda x: x.id == 10),
values= {'name': 'new person name'}
)
p.save()
- Delete records
p = Person(load = true)
p.unlink(
domain=(lambda x: x.id == 10)
)
p.save()
FOR FULL DETAIL, READ THE CODE
Fell free to port or contribute this project