@ponyorm

Страница 9 из 75
Angel
02.12.2016
16:55:02
I intend to read your project source. Any recommended reading before that?

Alexey
02.12.2016
16:56:32
Alexander
02.12.2016
16:57:49
Do you mean sources of PonyORM?

Angel
02.12.2016
16:58:02
yes sir

Google
Angel
02.12.2016
16:58:16
Alexander
02.12.2016
17:00:23
Maybe documentation?

You may be shocked while reading PonyORM sources, because currently they are not following PEP8 standard

I used my own convention, which was convenient to me

We plan to format it according to PEP8 before release 1.0

Luckydonald
02.12.2016
17:04:28
Okey, now this is a tough one. Context: One of my telegram bots, @StickerTagBot. This is the inline query search. I have StickerMessages representing a sticker someone sent. I want to filter them by user_id, and search the query_text. Now the seach query should look in the .sticker.emoji field and the .string of any of the Tags (Sticker.tags, which is StickerMessage.sticker.tags[].string). I got that working so far: sticker_messages = orm.select( st for st in StickerMessage if st.user.id == user_id for t in st.sticker.tags if query_text.lower() in t.string.lower() or query_text == st.sticker.emoji ).order_by(orm.desc(StickerMessage.date)).limit(50, offset=offset) Now sticker_messages is a list of StickerMessage rows. How can I make StickerMessage.sticker.file_id distinct here? In other words, modify the query, so I get each StickerMessage.sticker.file_id only once (the newest, StickerMessage.date)? This is my workaround: known_ids = [] # is limited to 50 anyway. for sticker in sticker_messages: file_id = sticker.sticker.file_id if file_id in known_ids: continue # end if known_ids.append(file_id) # do something with file_id

Oh, and offset is a for pagination, it will be increased by 50, and that function called later again.

Alexander
02.12.2016
17:18:25
Pony should add distinct by default if the result contains repeatable data. So, if you write: select(st.sticker.file_id for st in StickerMessage if ...) You should get distinct values

Luckydonald
02.12.2016
17:21:50
Sorry. My assumption was wrong. That it is in there 3 times because of 3 tags, but actually because it was sent already 3 times.

Alexander
02.12.2016
17:22:46
If you select st.sticker.file_id instead of st you should see it once, I think

Luckydonald
02.12.2016
17:28:31
Huh. Can it be that simple? I'll try.

Alexander
02.12.2016
17:28:48
...but because of pagination you can get the same file_id on different pages. I'm not sure it is good to use pagination in this case.

Luckydonald
02.12.2016
17:29:20
I have to, I can only send 50 entries at once.

Google
Luckydonald
02.12.2016
17:29:29
NotImplementedError: Ordering by attributes is limited to queries which return simple list of objects. Try use other forms of ordering (by tuple element numbers or by full-blown lambda expr).

Hmm...

Alexander
02.12.2016
17:30:15
.order_by(lambda: orm.desc(st.date))

The IDE can warn you that st variable is not found, but don't worry about that

Luckydonald
02.12.2016
17:33:02
pony.orm.dbapiprovider.ProgrammingError: for SELECT DISTINCT, ORDER BY expressions must appear in select list LINE 7: ORDER BY "st"."date" DESC

Uh, this is really hard. I'm sorry...

Alexander
02.12.2016
17:39:15
Ok, then PostgreSQL does not allow to combine ORDER BY x and SELECT DISTINCT y in the same query. Then maybe you can do DISTINCT part in Python: messages = select(st for st in StickerMessage if ...).order_by(...) file_ids = set(m.sticker.file_id for m in messages)

Luckydonald
02.12.2016
17:40:24
Hm. Postgres SQL has a SELECT DISCTINCT(column1), column2 FROM table

Because with the one above, I'll have to load all id's into memory first, and do cutting it to 50 for pagination later.

Let me try to come up with a raw sql query.

Alexander
02.12.2016
17:41:58
> SELECT DISCTINCT(column1), column2 FROM table what does it do?

Luckydonald
02.12.2016
17:43:43
You can specify one (or possible multible?) of the colums you select to be distinct

Another link, to the official Docs: https://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-DISTINCT

Sorry. SELECT DISCTINCT(column1) column1, column2 FROM table SELECT DISCTINCT(column1) * FROM table

Alexander
02.12.2016
17:49:09
I'm not sure it can be useful in your case

I think the correct query is pairs = select( (st.sticker.file_id, max(st.date)) for st in StickerMessage if st.user.id == user_id for t in st.sticker.tags if query_text.lower() in t.string.lower() or query_text == st.sticker.emoji ).order_by(-2, 1).limit(50, offset=offset) file_ids = [ file_id for file_id, date in pairs ]

Luckydonald
02.12.2016
17:52:33
This is looking promissing, yeah

Yep. Thats it. Wonderfully working! Can I write .order_by(desc(2), 1) instead of .order_by(-2, 1)?

Does desc(2) make a -2?

Alexander
02.12.2016
17:58:53
I think yes

Google
Alexander
02.12.2016
18:00:49
Cool

Luckydonald
02.12.2016
19:29:22
What is the difference between Optional and Nullable?

Марк ☢
02.12.2016
19:29:38
Megaquestion

One of things that I can't understand

Luckydonald
02.12.2016
19:30:11
Yay, I'm not alone!

Марк ☢
02.12.2016
19:30:41
Moreover, behaviour is different depending on the data type of the column!!

Alexander
02.12.2016
19:34:01
The difference is for string attributes, which by default are optional, but not nullable. Otherwise it may be very annoying to search for empty string values, half of which is '' and another half is NULL

Марк ☢
02.12.2016
19:34:43
And what is the difference for nonstrings ?

Alexander
02.12.2016
19:35:19
For non-strings, all Optional attributes are always nullable

Luckydonald
02.12.2016
19:36:36
Okey, I was thinking Optional means you can leave it out when createing the object or something

Romet
02.12.2016
19:36:40
so like blank in django orm?

Alexander
02.12.2016
19:36:56
Yes

Okey, I was thinking Optional means you can leave it out when createing the object or something
Yes, but also you can leave out Required attributes which have `default` option specified

Alexey
02.12.2016
19:42:29
We described it here https://docs.ponyorm.com/api_reference.html?highlight=nullable#optional-string-attributes

Does it explain enough?

Chris
02.12.2016
21:43:23
I think I've found another bug but want to confirm here: If I have an entity with a volatile attribute, and I perform a select, then an update, then a to_dict() call, I get a KeyError inside a pony library function, A basic example is here: https://gist.github.com/chrisshroba/deb3bbff34261c88e1572a19b06123c3

Alexander
03.12.2016
01:03:25
Thanks, fixed

Mikki
05.12.2016
06:30:23
Welcome! :)

Colin
05.12.2016
08:22:25
Hello guys :)

Swadhin Sangram
05.12.2016
08:23:23
Hello

Google
Luckydonald
05.12.2016
09:36:45
Hey

Mikki
05.12.2016
14:33:25
Welcome :)

Angel
05.12.2016
17:20:54
welcome!

Luckydonald
05.12.2016
19:52:14
Hi!

I realized I probably just could replace (reassign) the .state :D Not sure why that above happens, tho.

Alexander
06.12.2016
10:23:09
The real operation is postponed until commit or flush. I'm not sure it is a good idea to delete a row and right after that create a new row with the same primary key. Maybe it is better to assign new state field to a existing row. And even better is probably to have state field directly inside the User object

Luckydonald
06.12.2016
10:26:21
The real operation is postponed until commit or flush. I'm not sure it is a good idea to delete a row and right after that create a new row with the same primary key. Maybe it is better to assign new state field to a existing row. And even better is probably to have state field directly inside the User object
Yeah, probably :/ Anyway this makes state cleanup a bit easier (drop table), and can also be extendet to chat groups later. But you are probably right, and I am just a bad programmer, still sticking to this ?

Alexander
06.12.2016
10:31:01
I think you are overmodest :) If each object type (User, Chat, Message, etc.) has corresponding type attribute, the cleanup may be relatively simple too, something like that: @db_session def do_cleanup() db.execute('update "user" set "state" = 0') db.execute('update "message" set "state" = 0') db.execute('update "chat" set "state" = 0')

Mikki
06.12.2016
21:38:11
Welcome!

Gilbert
06.12.2016
21:43:05
Hey everyone!

Just join the group

starting to learn pony

Angel
06.12.2016
21:47:51
welcome

on a non related question, in: def send_message(text, chat_id=169582347, parse_mode=None):

def send_message(text, chat_id=169582347, parse_mode=None):

which are *arg and **kwargs

Rozen
06.12.2016
22:37:22
mmm if i were you i'd ask here: https://telegram.me/pythontelegrambotgroup

Luckydonald
06.12.2016
22:56:58
on a non related question, in: def send_message(text, chat_id=169582347, parse_mode=None):
This could be pytgbot actually. In that case I can help you https://telegram.me/pytg_group

which are *arg and **kwargs
Arg are the positional arguments, kwargs are the keyword arguments (with foo=value)

Google
Angel
06.12.2016
22:58:16
I realize my mistake thanks it is decided on the call not the definition

Страница 9 из 75