Jorge
Jorge
I tried setting db_session(strict=True) and ussing the context manager (with db_session)
Alexander
Transactional ORMs doesnt work good with async. Pony does not support async.
Alexander
Thats good
Alexander
I think
Bloo
Works great for everything I've needed thus far. With the DB code all in it's own private thread, it has nothing to contend with for access, and no deadlocks of any kind, thanks to the magic of zeromq messaging.
Bloo
Just send off commands to the DB thread, and await responses. All very straightforward.
Alexander
Might be a good practice. Maybe we should write some article about all these things.
Bloo
I've never been against more helpful documentation bein' added to the web. :)
Bloo
In truth, I have plans to add myself a proper blog to the web soon documenting exactly such fun things I've learned while exploring Python. :)
Bloo
Python's prolly the most enjoyable programming language I've ever worked with, so it's been a lotta fun discovering the various toys I have available to me. Pony ORM's been one of the better ones, and ZeroMQ too. Still wrapping my brain around all the new async stuff tho.
Bloo
Async programming requires a different way of planning one's code than I'd been previously used to.
Alexey
Bloo
Surely will do. Prolly gonna use Nikola (https://getnikola.com/) to generate the site/blog. Big fan of static HTML. :)
Jorge
Matthew
I googled and couldn’t find much: What’s the suggested way to use pony with type annotations? How about mypy?
Alexander
We didn't think much about type annotation support yet
Alexander
Maybe we need now
Matthew
Running mypy on a file with some pony models gives a bunch of errors about imports, which currently I think means any project with pony in it won’t work with mypy
Matthew
in general types seem neat, and will probably make python code a lot more solid
Alexander
Ok, thanks
Alexander
Doesnt it conflict with py2?
Lucky
Maybe we need now
Yeah, to keep it "pythonic" as you claim, that's really be needed.
Alexander
I mean that pony supports also python2 and it uses the same code as python 3.
Alexander
So probably we’ll add type annotations after we dump Python2 support. At this moment it probably still used in some scientific code
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
👋🏻
It's possible to create an object with its sub-objects?
for example:
Command (name = "new_command", rules = [Rule (text = "rule")], messages = [Message (text = "msg")])
Alexander
yes
Alexander
But actually in this code Rule or Message are created before Command, not at the same time.
If Rule.command or Message.command is required attributes, you need to create Command before and these objects later
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Alexander
if Rule.command and Message.command are optional, you can create objects in any order. You show two options, and the third option is
cmd = Command(...)
Rule(..., command=cmd)
Message(..., command=cmd)
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Ok, I'll just change to optional
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
🙏🏻
Alexander
If it really is required, it is better to leave it as required
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Right.
Another question, is there a one-way relationship?
Alexander
No, but on the other side you can add some Set attribute with arbitrary name
(Also, in my experience, this "unnecessary" attribute may turn out to be quite useful to express queries in a simple and compact way)
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Alexander
Like, you think that you want to have one-way relationship Person->House. You cannot do it in Pony (right now), so you need to add reverse attribute to House too. In most cases it wil be Set attribute, but sometimes it may be Optional one.
so you need to define
class Person(db.Entity):
name = Required(str)
house = Optional("House")
class House(db.Entity):
address = Required(str)
persons = Set("Person")
If you are not going to actually use House.persons attribute it may be named arbitrarily. But it may be useful for many kinds of queries, like instead of
select(p for p in Person if p.house == h)
you can just use h.persons
Alexander
Note that House.persons is a "virtual" attribute which does not have corresponding table column
{°•.~.•°🇦 🇻 🇮 °•.~.•°}
Ok, thanks!😀
Anonymous
Hi! Been looking into alternatives for peewee, as I was having issues with it when the database connection drops. How does pony handle that situation? I try execute a query, but the database is not available at the moment. In peewee, this would always happen eventually - the connection would drop, even if it can be immediately reestablished and any queries would fail.
Can I set pony up to automatically reconnect and retry? Maybe even keep retrying periodically until a timeout is reached?
Alexander
Hi! Pony automatically reconnects under the hood if a database connection closes unexpectedly. Pony checks the state of the current transaction. If the transaction is just started, or up to this point you performed only SELECTs, Pony will reconnect silently. But if at least one UPDATE was executed, then an exception is raised instead, because we lost the state of transaction after the connection was closed
Anonymous
Sounds sensible to me. Very nice to hear :)
Alexander
You are welcome :)
Anonymous
Just to confirm: This is only within a transaction, right? If I do individual statements outside a transaction or start a new transaction the "update was performed" thing is irrelevant?
Alexander
If dropped connection had some uncommitted inserts/updates/deletes, then we cannot reconnect silently. Otherwise, we can.
If you work in interactive mode outside of db_session, transaction is still started on first insert/update/delete, and will continue until explicit commit()/rollback()
Anonymous
Hmm, I guess if it's wrapped in a transaction a failed query would, at the very least, cause the commit to throw an exception?
To be fair, my main concern isn't with failed queries mid-transaction, but I'm curious now.
Alexander
with db_session:
s = Student[123] #1
dept = s.group.department #2
s.scholarship += 100 #3
some_students = select(s for s in Student if s.scholarship > 500) #4
some_students[0].group = Group[456] #5
#1 This is the first SELECT in this db_session, if we are trying to execute it and see broken connection, we can reconnect silently without any problem
#2 Additional SELECT to fetch department. If we are trying to execute it and see broken connection, we still can reconnect silently
#3 We perform some updates in memory, but these updates are not saved to database yet. Pony doesn't talk with database here
#4 When Pony sees that query, it also sees there are some unsaved changes. So it tries to save changes before issuing SELECT in order to get correct result from the query
#4.1 If Pony tries to do first UPDATE and sees broken connection, it still can reconnect silently
#4.2 after that point (on any subsequent UPDATE/SELECT) Pony cannot reconnect silently, because the first UPDATE was rolled back by database when connection was dropped, and a new connection will not have that first UPDATE applied
#5 the same as 4.2, it is too late to restore connection silently, Pony needs to throw an exception here, because some updates were lost
Alexander
It is seldom that connection drops in the middle of transaction, usually it happens between transactions, and then the connection will be restored at the start of the next transaction
Anonymous
Hence my comment that I'm not too worried about it dropping mid-transaction. You say it cannot fail silently, that means it will just throw an exception, right?
A failure in one transaction will not affect any subsequent transactions?
Alexander
yes, after we start a new transaction after exception was raised Pony will open a new connection instead of dropped one
Anonymous
Awesome. I am really liking this. I'm gonna give pony a go, I think. It will definitely beat peewee, which I used previously and where I was told that queries failing due to a dropped connection (between transactions) is intentional...
Muhammed
Hi, is there an alternative of CREATE TYPE in Pony ORM?
Alexander
Hi, what type do you want to create?
Anonymous
hi, any way to define a custom dump/load method for a json column?
Alexander
For what purpose?
Anonymous
I have json columns on tables where i save info from dicts
Anonymous
currently i'm using rapidjson, with some specific way to match Dateeimes/decimal/uuids fields
Anonymous
if i can specifc my custom loader/dumps i can save double conversion
Alexander
> currently i'm using rapidjson
is it for c++?
Anonymous
yep, there is a bind for python
Anonymous
python-rapidjson
Alexander
Do you write from c++ directly to database and then read from Python or what?
Anonymous
https://github.com/python-rapidjson/python-rapidjson
Anonymous
i'm using this because is very strict/configurable with json conversion, a part of very perfomance
Anonymous
Currently'm sanitazing the information in this way:
try:
dat_str = rapidjson.dumps(stint.get('datum'), uuid_mode=rapidjson.UM_CANONICAL, number_mode=rapidjson.NM_DECIMAL, datetime_mode=rapidjson.DM_ISO8601)
dat_dict = rapidjson.loads(dat_str)
except:
self.logger.exception("Error converting close datum: {}".format(stint.get('datum')))
dat_dict = { 'error': 'Error converting close datum' }
finally:
update_current_act['datum'] = dat_dict
Anonymous
but at the end, i'm parsing and reparsing the json
Anonymous
update_current_act is the db.Entity and datum is a json Column
Alexander
What database are you using?
Anonymous
postgresql
Alexander
Right now it is not customizable, you can open an issue to allow specifying JSON serializer
Alexander
Hi guys, we just released PonyORM 0.7.12
## Features
* CockroachDB support added
* CI testing for SQLite, PostgreSQL & CockroachDB
(see test result statuses here: https://github.com/ponyorm/pony/blob/orm/README.md)
## Bugfixes
* Fix translation of getting array items with negative indexes
* Fix string getitem translation for slices and negative indexes
* PostgreSQL DISTINCT bug fixed for queries with ORDER BY clause
* Fix date difference syntax in PostgreSQL
* Fix casting json to dobule in PostgreSQL
* Fix count by several columns in PostgreSQL
* Fix PostgreSQL MIN and MAX expressions on boolean columns
* Fix determination of interactive mode in PyCharm
* Fix column definition when sql_default is specified: DEFAULT should be before NOT NULL
* Relax checks on updating in-memory cache indexes (don't throw CacheIndexError on valid cases)
* Fix deduplication logic for attribute values
Alexander
MySQL and Oracle tests builds will be published as well.
Muhammed
Hi, what type do you want to create?
Sorry, I was not interested in messages.
For example in this link
CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
CREATE TABLE bug (
id serial,
description text,
status bug_status
);
Alexander
At this moment it is not supported, instead you can use int values with names defined in Python
class bug_status:
new = 1
open = 2
closed = 3
class Bug(db.Entity):
id = PrimaryKey(int, auto=True)
desctiption = Required(str)
status = Required(int, default=bug_status.new)
I hope we can add Enum support soon
Muhammed
Ok, thank you
Free Sneed | Sneed Ban of 2019 | The Simpsons General Massacre
Hello guys, I am sorry, but I will ask the question right from the start - is there any way of going from pony code to diagram? or only from diagram to code?