@ponyorm

Страница 63 из 75
Alexander
13.06.2018
12:07:42
I'm not sure SERIALIZABLE can solve this

I think you can solve this by using PostgreSQL INSERT ... ON CONFLICT (...) DO UPDATE ... statement https://www.postgresql.org/docs/current/static/sql-insert.html#SQL-ON-CONFLICT Pony does not support it yet, but you can write raw sql query db.execute("insert ...")

Etienne
14.06.2018
07:31:09
Hey guys, I'm wondering what's the best practice for password management when connecting to a database with pony? Leaving it in clear in the code seems dangerous, how do you guys deal with this?

stsouko
14.06.2018
07:32:16
Use config files

Google
Etienne
14.06.2018
07:35:35
Is that the standard way? It doesn't feel great security wise, I feel like one of my users could accidentally share theirs.

Jim
14.06.2018
07:39:20
use python-dotenv

stsouko
14.06.2018
07:42:59
U can set file attr 640 in unix

Etienne
14.06.2018
07:44:59
Oh that's great, didn't think to store them as environment variables, I'm gonna do that thank you both =)!

Roman
14.06.2018
21:33:55
Hi guys. Can you recommend good swagger library for flask? Most of them seems abandoned.

stsouko
15.06.2018
05:31:17
Flask swagger 2

Alexey
15.06.2018
10:10:36
do you mean this one? https://github.com/soerface/flask-restful-swagger-2.0

Permalink Bot
15.06.2018
10:10:59
do you mean this one? https://github.com/soerface/flask-restful-swagger-2.0
Permanent link to the soerface/flask-restful-swagger-2.0 project you mentioned. (?)

stsouko
15.06.2018
10:11:19
Yes

Roman
15.06.2018
10:14:57
this project is not maintained

Terrence
15.06.2018
12:25:59
Hello, I typed up my question about PonyORM here - https://stackoverflow.com/questions/50875270/schema-migrations-in-ponyorm And there is an old, unanswered question here: https://stackoverflow.com/questions/50016739/mock-pony-orm-entity

Alexander
15.06.2018
12:35:02
Hi Terrence! Thanks for poining out, I'll answer the questions

Google
Alexander
15.06.2018
12:51:43
Done

[Anonymous]
16.06.2018
19:50:21
Как дел?)

Alexander
16.06.2018
19:52:02
Good )

Jim
22.06.2018
08:17:44
Hi. I want to save 3 files en disk, they correspond to a Document entity. The thing is for each file I wanna be sure that writting to disk ANd to database succeeds. before_insert or after_insert can't do that. I was thinking a bout a context manager. Do you see another way to do it ?

this what I want to accomplish : python @db_session def save_document(path): some_files = [...] new_entity =[] for file in some_files: try: d = Document(*args) d.flush() savefile except orm.OrmError: raise someother error except IOError: rollback raise someerror else: new_entity .append(d) return new_entity

Matthew
22.06.2018
09:05:18
So you want it to be one atomic transaction?

Jim
22.06.2018
09:10:08
Not necessarily. I do not want save file if database fails and i don't want save to database if file save fails. I don't want every thing fail, if only one file fails. the thing is that the underlying function is already inside a db_session

if flush() succeeds, does it mean that commit() will ?

Matthew
22.06.2018
09:27:51
I think the problem fundamentally is that either system (filesystem / database) can fail independently

You could write to database, then write to file, and then if writing to file fails, remove your database record?

or have a flag for file written in the database table

stsouko
22.06.2018
09:32:58
Flush can be roll backed

If exception raised

Jim
22.06.2018
09:33:19
this is why I propose flush better than commit

Alexander
22.06.2018
09:50:08
I think it may be something like that: def save_file(filename, saved_files, ...): <do save> saved_files.append(filename) def save_document(path): files_to_save = [...] saved_files = [] new_entities = [] try: with db_session: for file in files_to_save: d = Document(*args) new_entities.append(d) d.flush() save_file(file, saved_files, ...) except: for filename in saved_files: remove_file(filename) raise return new_entities

Jim
22.06.2018
11:22:13
is it a problem if save_document is already decorated by @db_session ?

Alexander
22.06.2018
11:42:05
In that case inner db_session will be ignored, so commit to the database happens too late. You can do manual commit: def save_file(filename, saved_files, ...): <do save> saved_files.append(filename) @db_session def save_document(path): files_to_save = [...] saved_files = [] new_entities = [] try: for file in files_to_save: d = Document(*args) new_entities.append(d) d.flush() save_file(file, saved_files, ...) commit() except: for filename in saved_files: remove_file(filename) raise return new_entities

Jim
22.06.2018
16:14:31
thank you guys

Alexander
22.06.2018
19:41:35
Nice. "Hot chicks" join our comminity, not a spam for sure.

Alexander
22.06.2018
19:45:23
Alexander, It's a sexism. It may be a real developer, why do you now. Btw, some time ago your avatar was an anime girl with panties on her head. So, not be so fast with conclusions )

Google
Henri
22.06.2018
19:49:54
Hmm one after the other and all written in upper case. For sure spam.

Alexander
22.06.2018
19:50:24
Shame on me

Grigori
23.06.2018
18:22:51
Hi, everyone!

I'm trying to use buffer as PrimaryKey. PonyORM allows this, but select always fails afterwards.

from pony import orm db = orm.Database() class Bla(db.Entity): smt = orm.PrimaryKey(buffer, auto=False) db.bind(provider='sqlite', filename=':memory:') db.generate_mapping(create_tables=True) a = Bla(smt=str(0x01)*20) q = orm.select(p for p in Bla)[:]

Python 2.7 mumbles something about TypeError: writable buffers are not hashable

Does anyone have some thoughts about this problem?

stsouko
23.06.2018
18:29:15
Don't use mutable types as pk

Grigori
23.06.2018
18:30:08
but how I can use binary type as pk then?

stsouko
23.06.2018
18:31:11
Str in 2.7 is bytes string

In python 3 u can use bytes

Grigori
23.06.2018
18:33:56
I tried string, but I get 'can't convert to unicode' error

ValueError: Value for attribute MetadataGossip.sig cannot be converted to unicode: '8\x13D\xb2\xcd3\xf62\xa2\xbe\x83\xa3ca<3\xb3u\xe5M\xb9n\td\x9d}\xf8K\x8do\xad\xea+\x94\xfc\x90Tl...

i.e. I can't create the entry

but that's another problem

stsouko
23.06.2018
18:37:22
Are you using python 2.7

Grigori
23.06.2018
18:37:50
Python 2.7.14 (default, Jan 17 2018, 14:37:42) [GCC 6.3.0 20170406] on linux2

stsouko
23.06.2018
18:40:02
Try to set attr as bytes

I'm using bytes type for binary data on python 3

Grigori
23.06.2018
18:42:37
still cannot be converted to unicode

Google
Grigori
23.06.2018
18:42:40
hm

stsouko
23.06.2018
18:43:25
Error looks like python try to convert bytes to Unicode with invalid encodings

Grigori
23.06.2018
18:50:38
funny thing, PonyORM seems to expect the value to be unicode. Therefore, It is impossible to store true binary data in str with Pony.

```

``` from pony import orm db = orm.Database() class Bla(db.Entity): smt = orm.PrimaryKey(str, auto=False) db.bind(provider='sqlite', filename=':memory:') db.generate_mapping(create_tables=True) a = Bla(smt=str('\x13D\xb2\xcd3\xf62\xa2\xbe')) q = orm.select(p for p in Bla)[:] ```

triggers it

stsouko
23.06.2018
18:54:08
PrimaryKey(bytes, auto=False)

Try this

Grigori
23.06.2018
18:54:41
same result

'cant convert'

Henri
23.06.2018
19:02:48
From the docs: > Starting with the Pony Release 0.6 the attributes of str type in Python 2 behave as if they were declared as unicode attributes. There is no difference now if you specify str or unicode as the attribute type – you will have unicode string in Python and in the database.

Grigori
23.06.2018
19:07:20
yep. Unfortunately, the docs don't tell that its impossible to use buffer as PrimaryKey

Alexander
23.06.2018
19:33:32
Hi Grigori! At this moment, Pony cannot use binary attribute as a primary key, because, if I understand correctly, most databases don't allow it. In SQL binary values are typically stored using BLOB datatype, and BLOB column cannot be indexed. In some databases there are other binary types besides BLOB, like VARBINARY for small binary data. Maybe in the future we can allow primary key VARBINARY columns, if it is supported by the database. Right now an attempt to make binary attribute a primary key leads to obscure error, we need to fix the error text and make it less obscure Regarding Python types, Pony treat both str and unicode as the same unicode string type, for easier porting of applications to Python3: you can define attribute as str in Python2 application and it will work without any changes after porting application to Python3 For binary data, you can use buffer in Python2 and bytes in Python3. Unfortunately Python2 define bytes as an alias to str in a misguided attempt to make porting to Python3 easier. So you need to use buffer type in Python 2.7 for binary type

Grigori
23.06.2018
20:22:11
Alexander, thanks a lot for making it clear to me!

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