Lucky
AttributeError: 'Database' object has no attribute 'permissions_for'
Alexander
Sorry, it's set_perms_for
Lucky
>>> with db.set_perms_for(Sticker): ... orm.core.can_view(POSTGRES_USER, Sticker) ... False
Lucky
Oh never mind
Lucky
I'm stupid :D
Lucky
not my postgres user anymore.
Lucky
>>> with db.set_perms_for(Sticker): ... orm.allow('view', group='anybody') ... Traceback (most recent call last): File "<stdin>", line 2, in <module> AttributeError: module 'pony.orm' has no attribute 'allow'
Lucky
Can't find that method either.
Alexander
Try this: with db.set_perms_for(Sticker): orm.perm('view', group='anybody')
Lucky
Seems working: <pony.orm.core.AccessRule object at 0x7f46437e41d0>
Lucky
And db._get_schema_dict() too
Alexander
cool
Lucky
What's the difference of ._attrs_ and ._new_attrs_?
Alexander
If one entity inherits from another entity, _new_attrs_ contains new attributes only
Lucky
Ah, thanks. Also, where can I find the size attribute of an int type Attribute? (Or something else to be able to distinguish between long, int, short, etc., and float, double)
Lucky
Got it. >>> Class.field.kwargs['size'] 64
Alexander
I recommend to use the following: attr.converters[0].size
Micaiah
What would be the best way to catch for different transaction errors and turn them to JSON
Micaiah
For example: with db_session: try: return TestEntity.get(id=id).to_dict() except Exception as e: # what to do with e?
Alexander
may be return jsonify({'status': 'error', 'class': type(e).__name__, 'msg': str(e)}) ?
Lucky
How do I do unit tests with Pony? is there a way to use it without actual database?
Alexander
You can have a separate test database, and call db.bind(...) in test setup with parameters of that database. If you don't use any logic specific to your main database engine, you can use SQLite in-memory database for tests
Anonymous
what does pony seems to be fit in web frameworks with?
Anonymous
does it only flask which gives a nice integrated ?
Micaiah
what does pony seems to be fit in web frameworks with?
You can use Pony with anything, but you'll find better support with Flask than Django. Honestly, other than Django I'd pick Pony for my ORM
Anonymous
What about Odoo or ERP5?Have you tried to those frameworks?
Lucky
You can use Pony with anything, but you'll find better support with Flask than Django. Honestly, other than Django I'd pick Pony for my ORM
The good thing with Pony is, it isn't bound to anything else. Unlike Django's ORM which only comes with Django.
Anonymous
Yeap I knew it..I hope Pony will have tool to migrate data
Max
Alexander, do u plan to extend lambda syntax in Pony? I hope in the future pony will support all types of operations == without lambdas...Where is Russian telegram :)
Anonymous
Pony has give big improvement some years ago, and I can't wait for another one....😘Love pony
Alexander
I realy hope we will finish migration tool soon
Alexander
Maxim, what do you mean by extending lambda syntax, can you give an example?
Max
Sorry for the late answer. I do not remember. I will check old projects and send more specific request. I only rembeber what i did not create some things with lambdas. I checked full docs. Looked at maillist and other community resources, but i did not find how to do it. Because I had a similar question as a speaker in the community. Answer: not using lambdas (Model definition). Thank you. Lambdas - is not correct... Model difinition - worked only with Model.... model properties and methods and etc... Perfecly like as LINQ(collection IQueryable) c#, Peewee, asq, pythonql... Be more simple...I hope pony will be smartest python orm in the world))
stsouko
Hello! DB structure: class A(db.Entity): a = Set('B') class B(db.Entity): b = Set('A') def before_delete(self): for x in self.b: x.delete() then I try to use this: B[1].delete() I get OperationWithDeletedObjectError: B[1] was marked to delete
stsouko
I think hooks priority should be greater.
Lucky
Talking about Hooks, Is there something to hook into create/update/delete on PonyORM side? Context is the Pony --> Elasticsearch tool I'm writing. Currently it would look like
Lucky
from database import SomeObject, db obj = SomeObject["id123"] Syncer.generate_index(db, "SomeObject") Syncer.store(obj)
Lucky
So the idea would be to hook into Pony somewhere, to automatically have it updated.
Lucky
pony.orm.core.TransactionError: create_tables() cannot be called inside of db_session Why that?
Alexander
In many databases it is not possible to intermix create and insert/update commands. As create_tables command already have @db_session wrapper, it typically have a little sense to wrap it in yet another db_session
stsouko
@akozlovsky what about before delete hook?
stsouko
Or hooks don't designed for this
Alexander
It looks like a bug, I need to reproduce it
stsouko
I used latest pypi version
Lucky
I probably something old... lemme check
Lucky
Mine is 0.7
Alexander
@nougmanoff, can you provide a full traceback?
Alexander
@luckydonald, data can be modified inside the database without using Pony, in that case the hook will not work
Lucky
I would be happy enough with that limitation. What do I need to do?
Alexander
Probably just override after_update / after_delete hooks and make corresponding update to elasticsearch
stsouko
Ok.
stsouko
--------------------------------------------------------------------------- OperationWithDeletedObjectError Traceback (most recent call last) <ipython-input-40-000b29768406> in <module>() ----> 1 commit() /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in commit() /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in cut_traceback(func, *args, **kwargs) 73 if tb.tb_frame.f_globals.get('__name__') == 'pony.utils' and tb.tb_frame.f_code.co_name == 'throw': 74 reraise(exc_type, exc, last_pony_tb) ---> 75 raise exc # Set "pony.options.CUT_TRACEBACK = False" to see full traceback 76 finally: 77 del exc, tb, last_pony_tb /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in cut_traceback(func, *args, **kwargs) 58 return func(*args, **kwargs) 59 ---> 60 try: return func(*args, **kwargs) 61 except AssertionError: raise 62 except Exception: /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in commit() 291 try: 292 for cache in caches: --> 293 cache.flush() 294 except: 295 rollback() /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in flush(cache) 1600 with cache.flush_disabled(): 1601 for obj in cache.objects_to_save: # can grow during iteration -> 1602 if obj is not None: obj._before_save_() 1603 1604 cache.query_results.clear() /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in _before_save_(obj) 4836 if status == 'created': obj.before_insert() 4837 elif status == 'modified': obj.before_update() -> 4838 elif status == 'marked_to_delete': obj.before_delete() 4839 def before_insert(obj): 4840 pass <ipython-input-23-c9e3c6eedbcc> in before_delete(self) 2 b = Set('A') 3 def before_delete(self): ----> 4 for x in self.b: 5 x.delete() 6 /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in __get__(attr, obj, cls) /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in cut_traceback(func, *args, **kwargs) 73 if tb.tb_frame.f_globals.get('__name__') == 'pony.utils' and tb.tb_frame.f_code.co_name == 'throw': 74 reraise(exc_type, exc, last_pony_tb) ---> 75 raise exc # Set "pony.options.CUT_TRACEBACK = False" to see full traceback 76 finally: 77 del exc, tb, last_pony_tb /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in cut_traceback(func, *args, **kwargs) 58 return func(*args, **kwargs) 59 ---> 60 try: return func(*args, **kwargs) 61 except AssertionError: raise 62 except Exception: /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in __get__(attr, obj, cls) 2651 def __get__(attr, obj, cls=None): 2652 if obj is None: return attr -> 2653 if obj._status_ in del_statuses: throw_object_was_deleted(obj) 2654 rentity = attr.py_type 2655 wrapper_class = rentity._get_set_wrapper_subclass_() /home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/orm/core.py in throw_object_was_deleted(obj) 4134 assert obj._status_ in del_statuses 4135 throw(OperationWithDeletedObjectError, '%s was %s' -> 4136 % (safe_repr(obj), obj._status_.replace('_', ' '))) 4137 4138 def unpickle_entity(d):
stsouko
/home/stsouko/bydlocoding/predictor/env/lib/python3.4/site-packages/pony/utils/utils.py in throw(exc_type, *args, **kwargs) 96 raise exc 97 else: ---> 98 raise exc # Set "pony.options.CUT_TRACEBACK = False" to see full traceback 99 finally: del exc 100 OperationWithDeletedObjectError: B[1] was marked to delete
stsouko
oh sheee
stsouko
In [39]: B[1].delete() In [40]: commit()
Alexander
Ok, I reproduced it, thanks. Give me a few minutes
Alexander
I agree that this behavior is counter-intuitive. I need to think how to fix it. Probably we need to allow cascade_delete=True options for many-to-many attributes
stsouko
Yes. Previously I try to set cascade delete for m2m.
stsouko
This is good feature: unsimmetric delete. Imho
Alexander
agree
Святослав
bydlocoding folder, lol
Lucky
What is the efficient way to just get all elements of a table? In terms of memory and speed, I mean
Lucky
Probably just override after_update / after_delete hooks and make corresponding update to elasticsearch
Oh, per entity class. https://docs.ponyorm.com/api_reference.html?highlight=after_#entity-hooks So probably best with an Mixin class to be applied?
stsouko
m.b. .page() method usefull?
Alexander
page_size = 1000 page_num = 1 while True: with db_session: objects = MyObject.select(). \ order_by(MyObject.id). \ page(page_num, page_size) process_objects(objects) if len(objects) < page_size: break page_num += 1
Alexander
Oh, per entity class. https://docs.ponyorm.com/api_reference.html?highlight=after_#entity-hooks So probably best with an Mixin class to be applied?
You can use mixins, but when declaring an entity class you probably need to specify mixin before entity: class MyMixin(object): def before_update(self): ... def before_delete(self): ... class MyObject(MyMixin, db.Entity): # correct ... class MyObject(db.Entity, MyMixin): # wrong ...
Jordi
Hello guys. I'm trying to write a function that lets me create the classes dinamically
Jordi
def table_class(cls_name, table_name, **kwargs): # reset current mapping db = Database() class A(db.Entity): _table_ = table_name A.__name__ = cls_name for attr, attr_def in kwargs.iteritems(): a_def, a_type, a_name = attr_def setattr(A, attr, a_def(a_type, column=a_name)) # mapping new class db.bind('mysql', host=None, user=None, passwd=None, db=None) db.generate_mapping(create_tables=False) globals()[cls_name] = A return A # —- definition = { u'dataId': (PrimaryKey, unicode, 'dataId'), u'owner': (Required, unicode, 'owner'), # + some other fields ... } table_class('Data', 'data', **definition)
Jordi
but the code above fails to create the class properly (no defined PrimaryKey), any idea on how I could achive it?
Alexander
In simple cases (if you don't use composite keys) you can do something like that: from pony import orm from pony.orm import core def define_entity(db, entity_name, attrs): return core.EntityMeta(entity_name, (db.Entity,), attrs) db = orm.Database() entity = define_entity(db, 'Person', dict( name=orm.Required(str), age=orm.Optional(int) )) orm.sql_debug(True) db.bind('sqlite', ':memory:') db.generate_mapping(create_tables=True)
Jordi
Many thanks Alexander.
Anonymous
what if my code suddenly dead / crash during processing ? How does pony able to handle ?
Anonymous
I meant, Do the database would be alright?
Alexander
The database should be alright, Pony use transaction for each db_session and perform rollback if an exception occurred. If Python process crashes, then the database itself should perform rollback
Total-Trainwrec
hai
Alexander
Welcome
Total-Trainwrec
^^