Anonymous
(which is checked for in line 228)
Anonymous
{it could of course also be me who is an idiot, as I perhaps should use absolute paths instead...}
Valery
I've also faced with that magic in attempts to pass is_path parameter to sqlite
Valery
(it can be used to get some extended features of in-memory databases like shared cache)
Anonymous
Hmmm.... I'm not too fond of that. In all cases the write should be to os.getcwd(). Not to an import.
Alexander
Hi, this is long-standing behavior. The reason for it is mod_wsgi. Initially mod_wsgi was the most popular way to use Pony with "real" web-server. And in mod_wsgi the current working directory is set to some obscure path. So, the typical error was, the user creates a sample application to use from mod_wsgi, and specifyes in it db = Database('sqlite', 'mydatabase.sqlite', create_db=True) When the script is running locally, using CherryPy or Flask, it creates a database file in a current working directory, that is, typically the same directory where the script placed. A user populate the database with some example data, and then run the application under mod_wsgi. But then he encounters a strange bug: the database is empty! Although, the user sees that the populated database lies right here.
Alexander
So relative filenames were just unusable for that reason. And because of this, some frameworks, like Django, just forbid to use relative path in settings: https://stackoverflow.com/questions/28635405/why-absolute-paths-in-django-settings
Alexander
We had that option too, it was possible to raise an error when the user provide a relative path to the database, in order to prevent that obscure error. But then we thought, if relative path in its usual meaning cannot be used, why not allow them, but with the different meaning, "relative to the file where the database object is defined". Sometimes it is useful.
Alexander
And, you always can specify absolute path to be exact.
Alexander
Maybe it is possible to change that behavior now, but (1) it breaks backward compatibility, (2) "real" relative path can still be unreliable, for example if you have multiple scripts placed in different directories which all import the same db object, and (3) "relative to db object" semantics is quite handy sometimes
Anonymous
Hi - Sorry for the silence: My team has settled on using absolute paths for good. ๐Ÿ‘ (bug report cancelled ๐Ÿ˜Š๐Ÿ‘)
Anonymous
Is there a way to add a computed property to an entity that will be included by default in to_dict?
Anonymous
I've just overridden to_dict in my entity to take care of it, but I was wondering if there was a better way.
Anonymous
I think sqlalchemy calls this "hybrid attributes"...
Alexander
Hi, we want to add hybrid attributes soon, but this task turns out to be surprisingly difficult. Right now you need to override to_dict
Anonymous
Okay, thanks!
Anonymous
Hi all
Anonymous
I ve using Pony for my small project and I am very happy to use it especially because the syntax is very pythonic way.
Alexander
You asked info about lastest release, idk why did you delete your message but if you care... Here you can read info about it https://github.com/ponyorm/pony/releases/tag/0.7.3
Anonymous
I am going to check it right now , thanks..
Alexander
Or here https://blog.ponyorm.com/2017/10/23/pony-orm-release-0-7-3/
Anonymous
Thanks for your fast response
Alexander
No problem
Ghulam
hi.. im new to pony, now im doing validation before inserting data into the database, what your suggestion for doing the validation, is it before the insert using entity_hook, or plain validation before initiating new entity instance?
Matthew
What kind of an application is it? If it's a web app, I validate in my Forms (WTForms or Django Forms)
Matthew
In general, I validate outside of the model
Matthew
Sometimes after validation it won't make sense to make an instance of the model, for example if a required piece of data is missing or invalid
Ghulam
an API, using falcon..
Matthew
so do it in the code before creating the model instance
Ghulam
Ok
Ghulam
and thanks..
Matthew
no problem
Valery
Not sure if it's a good idea but you can use class method of model to create instance. Class method allows you keep validation code not so far from model and don't initialize instance if data is wrong.
Ghulam
yes, I'veโ€‹ been thinking something like that, but haven't really know how to do it properly, since I use schematics library to do the validations duplicating pony entities into schemas is not ideal I think.. Maybe I might try that one..
Valentin
https://github.com/ponyorm/pony/issues/315 hmmm
Henri
Have joined the framework benchmark with Morepath using Pony (on Python 3).
Henri
https://www.techempower.com/benchmarks/previews/round15/#section=data-r15&hw=ph&test=db&l=zijzen
Henri
Difficult to compare as Morepath is the only framework which uses Pony. But looks promising. Especially the tests using the database where Pony is involved.
Alexander
Hmm, I see Morepath, but I don't see Pony in this list. Do you mean that all Morepath results are with Pony?
Henri
Yes at least the ones which uses the database.
Alexander
So, if I read correctly, Morepath+Pony is approximately two times faster than Django
Henri
Depending which test but yeah a big difference. BTW json and plain text tests doesn't use the database. The others do.
Henri
Especially when you take into account that it's Python 3 and compare it with the PY3 versions of the other frameworks it looks quite amazing.
Alexey
That's great!
Alexander
Yeah. In fact, in terms of speed, I think Pony still has some room for growth ) The current code was written just from "theoretical" point of view, and profiling on real application may find some local bottlenecks which can be optimized even more
Alexander
But even now it looks quite good
Anonymous
great to hear thta\
Henri
BTW the source code of the benchmark is here : https://github.com/TechEmpower/FrameworkBenchmarks/tree/master/frameworks/Python/morepath
Bulat
Hi! I am new to Pony, and got stuck with MySQL. I have a table of users, and datetime field in it, and pony's mysql connector raises exception during parsing '0000-00-00 00:00:00', which is returned for NULL datetime fields lib/python2.7/site-packages/pony/orm/dbproviders/mysql.py", line 334, in str2datetime return datetime(*imap(int, s.split())) ValueError: year is out of range
Bulat
How can I make Pony treat this value as None ?
stsouko
Use custom datetime type class
Juan
Hello everybody, anyone know how to make a custom autoincrement like primary key? E.g. RCRD_001, RCRD_002, RCRD_003, etc. Thank you so much.
Alexander
Hi Juan. What database do you use?
Angel
/effect@bonbot
Angel
/eduvote@bonbot
Angel
/whereami@bonbot
Angel
/whereami@bonbot
ichux
/whereami@bonbot
Alexander
I think it is better to have integer primary key, because it is smaller and more efficient. But if you really need a string key, you can probalby declare it as a secondary key using nullable UNIQUE column and initialize it using an AFTER INSERT trigger
Alexander
With pony you can have a string primary key, but if it includes some "counter" you need to process it somewhere. You can store the counter value in a separate table, and use to construct primary key in Pony: class Counter(db.Entity): entity_name = PrimaryKey(str) value = Required(int, default=0) class MyEntity(db.Entity): number = PrimaryKey(str) foo = Required(str) bar = Required(str) @classmethod def make(cls, **kwargs): cls_name = cls.__name__ counter = Counter.get(entity_name=cls_name) \ or Counter(entity_name=cls_name) counter.value += 1 number = 'RCRD_%d' % counter.value return cls(number=number, **kwargs) ... with db_session: obj = MyEntity.make(foo='foo', bar='bar') But it will be less efficient then using integer autoincrement primary keys
ichux
@bonbot Who are you?
Bulat
Use custom datetime type class
Sorry, I'm not sure if this will fix the problem? str2datetime is assigned into the conv, which is converters I guess, like this conv[FIELD_TYPE.TIMESTAMP] = str2datetime conv[FIELD_TYPE.DATETIME] = str2datetime I can't see if custom datetime class can somehow affect this I've even tried using str, instead of datetime class, but didn't succeed. It raises exception way before getting to my object
Alexander
Hi Bulat, thanks for reporting, I think we need to fix it
Bulat
That would be great!
Anonymous
Good morning - Just FYI: We're using pony with SQLite for unittesting and postgresql in production. It's awesome. 10 x ๐Ÿ‘ hashtag @dematic.com
Anonymous
Of mere curiosity: Are anyone working on implementing database triggers into db.Entity?
Alexander
I'm glad you like it ๐Ÿ˜Ž
Alexander
At this moment we don't working on database triggers. But you can create them manually and use them with pony
Alexander
The problem with triggers is they may change data in the database and pony will not know about that, so object already loaded into memory may become out of date. It is possible in pony to mark attribute as volatile, that means it can be changed by trigger, so pony re-fetch attribute value after the object was changed
Anonymous
@ifindam We decided about 3 months ago to use ponyorm for all database interaction - including the database definition - which is stored in a "model.py" that is importable for all other modules.
Anonymous
By starting in pony, we can create unittests with: from model import get_db where: db = get_db(as_test=True) leads to the loading of an sqlite db with necessary test data loaded into memory.
Anonymous
db = get_db() leads to loading of the postgres production defaults.
Anonymous
We use nosetests' general principles with: def setup(): db global db = get_db(as_test=True) def teardown(): pass # drops the db from memory all code inbetween can then run with the "in memory" db variable shared.