<div dir="ltr">Hi Vadim!<br><br>I'd like to inform you that I just added the to_dict() method, which can be useful for converting Pony objects to CSV, JSON or any other format. I want to show you how this method can be used.<br>
<br>At first, let's import a module from Pony ORM examples and get an object which we want to convert to dictionary:<br><br> >>> from pony.orm.examples.presentation import *<br> >>> populate_database()<br>
>>> s = Student[1]<br><br>Now we can convert this object to a dictionary by the following way:<br><br> >>> s.to_dict()<br><div> {'group': 101, 'name': u'John Smith', 'dob': datetime.date(1991, 3, 20), 'gpa': 3.0, 'tel': '123-456', 'id': 1}<br>
</div><br>If we want to extract specific fields only, we can specify the list of attributes:<br><br> >>> s.to_dict(['id', 'name', 'group'])<div> {'group': 101, 'id': 1, 'name': u'John Smith'}</div>
<br>or<br><br> >>> s.to_dict("id name group")<div> {'group': 101, 'id': 1, 'name': u'John Smith'}</div><br>By default the related objects are represented by its primary keys, but it is possible to get full objects by specifying `related_objects=True`. This way you can convert related objects recursively:<br>
<br><div> >>> s.to_dict(['id', 'name', 'group'], related_objects=True)</div><div> {'group': Group[101], 'id': 1, 'name': u'John Smith'}<br><br>By default, collections and lazy attributes (such as BLOBs) are not included to the to_dict() result. It can be changed by specifying `with_collections=True` and `with_lazy=True`.<br>
<br>Now I want to show how you can store database data to CSV format using standard python csv module and new to_dict() method. Let's store the content of the Student table to the CSV file:<br><br> from pony.orm.examples.presentation import Student<br>
import csv<br> fields = ['id', 'name', 'group', 'gpa', 'tel']<br> csv_file = open('students.csv', 'w')<br> writer = csv.DictWriter(csv_file, fields)<br>
writer.writeheader()<br> for s in Student.select():<br> writer.writerow(s.to_dict(fields))<br> csv_file.close()<br><br>If you want to export all entity attributes in the same order as they defined, you can write universal function like this:</div>
<br><div> def export_to_csv(entity, filename):</div><div> fields = [ <a href="http://attr.name">attr.name</a> for attr in entity._attrs_with_columns_ if not attr.lazy ]</div><div> with open(filename, 'wb') as f:</div>
<div> writer = csv.DictWriter(f, fields)</div><div> writer.writeheader()</div><div> for obj in entity.select():</div><div> writer.writerow(obj.to_dict(fields))</div><div><br>
Then you can use this function in the following way:<br><br> export_to_csv(Student, 'students.csv')<br></div><br>to_dict() method is available in Pony ORM development version which can be taken from GitHub:<br>
<a href="https://github.com/ponyorm/pony">https://github.com/ponyorm/pony</a><br><br><div>We plan to release the new version Pony ORM 0.5.2 on Monday which includes to_dict() method, and you will be able to install it from PyPI using<br>
pip install pony --upgrade<br><br><br>Regards,<br>Alexander<br><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Aug 4, 2014 at 6:53 PM, Вадим Бажов <span dir="ltr"><<a href="mailto:vadim@ideco.ru" target="_blank">vadim@ideco.ru</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi, folks !<br>
I created and filled an sqlite db with data using Pony ORM. It's clean and easy.<br>
Now i need to be able to convert the database to windows Excell-friendly representation for the hiegher autority.<br>
As i suppose, the csv is the most easy and suitable file format for that case.<br>
So, please, share your experiences with me what is the best way to do such an export with Pony? If pony hasn't such export ability (what i didn't find), tell me what tools do you usually use for that task?<br>
<br>
-- <br>
С уважением,<br>
Бажов Вадим,<br>
Инженер отдела технической поддержки,<br>
Компания Айдеко<br>
--<br>
Телефоны: +7 (495) 987-32-70; +7 (495) 662-87-34 (тех. поддержка); +7 (343) 345-15-75; Факс: +7 (343) 383-75-13<br>
<br>
Электронная почта:<br>
Вопросы по приобретению: <a href="mailto:sales@ideco.ru" target="_blank">sales@ideco.ru</a><br>
Технические вопросы: <a href="mailto:support@ideco.ru" target="_blank">support@ideco.ru</a><br>
Партнерство: <a href="mailto:partners@ideco.ru" target="_blank">partners@ideco.ru</a><br>
Общие вопросы: <a href="mailto:info@ideco.ru" target="_blank">info@ideco.ru</a><br>
<br>
Сайт: <a href="http://www.ideco.ru/" target="_blank">http://www.ideco.ru/</a><br>
Форум: <a href="http://www.ideco.ru/forum/" target="_blank">http://www.ideco.ru/forum/</a><br>
<br>
______________________________<u></u>_________________<br>
ponyorm-list mailing list<br>
<a href="mailto:ponyorm-list@ponyorm.org" target="_blank">ponyorm-list@ponyorm.org</a><br>
<a href="/ponyorm-list" target="_blank">https://mailman-mail5.<u></u>webfaction.com/listinfo/<u></u>ponyorm-list</a><br>
</blockquote></div><br></div>