@ponyorm

Страница 31 из 75
Janis
05.05.2017
08:31:33
Any idea how to make this code shorter? Not including the json inside each function?

It's not that related to PONY but the community here is that nice, so I ask...

stsouko
05.05.2017
10:11:54
if request.form['token'] != config.secret_token: return False else: return True Replace with return request.form['token'] == config.secret_token

Janis
05.05.2017
10:14:08
Okay...thought I could include this check with lower lines of code, but thanks

Google
Jordi
05.05.2017
10:34:17
Hello guys, I'm using Jinja + Pony to create and run sql scripts. Actually, the generated script runs properly if executed from the shell on MySQL, but it never finishes when I run the db.execute(<script>) command. Is there any character limitation? Example: DROP TRIGGER IF EXISTS test_trigger_update; DELIMITER @@ CREATE TRIGGER test_trigger_update BEFORE INSERT, UPDATE ON test FOR EACH ROW BEGIN SET NEW.updated = CURRENT_TIMESTAMP; END @@ DELIMITER ;

Alexander
05.05.2017
10:38:20
I think you need to execute four separate commands commands = [ """DROP TRIGGER IF EXISTS test_trigger_update;""", """DELIMITER @@""", """CREATE TRIGGER test_trigger_update BEFORE INSERT, UPDATE ON test FOR EACH ROW BEGIN SET NEW.updated = CURRENT_TIMESTAMP; END @@""", """DELIMITER ;""" ]

for command in commands: db.execute(command)

Jordi
05.05.2017
10:57:45
I have removed the first command (the drop trigger) + run it as suggested and now I get an error when I run the DELIMITER command, so I think the DELIMITER is not supported.

Alexander
05.05.2017
10:58:28
Try without DELIMITER command

Maybe it is unnecessary

Jordi
05.05.2017
11:26:17
I couldn't make it work, but I see it should be unnecessary. https://github.com/PyMySQL/mysqlclient-python/issues/64

Janis
05.05.2017
15:46:14
Any way to get the "query" and check into a one line if statement? https://pastebin.grey.pw/?6e32153ecdd11688#x+z65JcDdm9E32e2JeUUJP8lAfNKzQSr0WHASwb+nmA=

Alexander
05.05.2017
16:23:41
if interna_id is primary key and you are sure that the object with this id exists in the database, you can write if Server[3].is_busy:

Janis
05.05.2017
16:24:12
It's NOT the primary key

Alexander
05.05.2017
16:25:29
Then you can write: if Server.get(interna_id=3).is_busy: But your current code looks more clear

Janis
05.05.2017
18:26:51
Thanks I prefer short code :)

Google
Janis
06.05.2017
13:15:18
How to get the inserted object JSON serialized? https://pastebin.com/raw/VuuWXaYi

Alexander
06.05.2017
14:07:03
Try to use to_dict method: https://docs.ponyorm.com/api_reference.html?highlight=to_dict#Entity.to_dict

Janis
06.05.2017
14:07:34
Okay, much easier :) http://stackoverflow.com/a/34765354/7878100

Since I need to pass the "data" somehow to celery

Any idea where things go wrong? https://puu.sh/vHuZi/63827b7051.png

I've changed from Optional back to Required in the meantime.

Alexander
06.05.2017
15:28:17
Any idea where things go wrong? https://puu.sh/vHuZi/63827b7051.png
Well, I think that config module does not have daemon_id variable :)

Janis
06.05.2017
15:29:06
Of course it has... https://puu.sh/vHvDZ/cee6006e46.png

Alexander
06.05.2017
15:29:54
I think you have circular import. When entity definition processed, config module is not fully loaded yet

Also, right now you specified a constant (which is not defined yet). You can use callable instead, which will retrieve actual value of daemon_id later, at the moment of object creation. It will help with curcular import, and also may be useful if daemon_id value can be changed dynamically later daemon_id = Optional(int, default=lambda: config.daemon_id)

Janis
06.05.2017
15:55:26
So just to make sure I understand, this will make python load the value before it's used?

Alexander
06.05.2017
15:56:29
Without lambda it will remember the value at the moment of entity definition. With lambda the default value will be calculated at the moment of object instance creation

Janis
08.05.2017
07:39:57
By the way, are python related questions allowed too, like best practices?

Luckydonald
08.05.2017
08:37:48
I wouldn't mind

Just do it Can be deleted anyway xD

Janis
08.05.2017
08:38:18
Well here we go

Any suggestion how to monitor a file for changes using Python? I'm using console commands to update some software. The output of this software is written to a file. Now I start a worker process (using celery), this worker now needs to "monitor" the file and check if it contains "Success", how to do so using Python?

Luckydonald
08.05.2017
08:39:40
There actually are some approaches to monitor file changes, some relying on the system used, the operating system which they are more efficient as polling would be

Janis
08.05.2017
08:39:42
So the worker code needs to check the file again and again for changes, only when the "Success" part is found it will stop (of course I can include a timer, waiting 5 minutes, if still no "success" in file, it will notify me about failed updated).

Any links for this?

Google
Luckydonald
08.05.2017
08:54:34
As I am on the bike right now, and it starts to rain, probably you googling makes mire sense xD

Janis
08.05.2017
08:56:26
Already done, all I've found until now is related to windows by hooking into win32 API functions, but since I run my code on LINUX doesn't make sense to me :)

Luckydonald
08.05.2017
08:57:35
There must be a linux one too

Let me DuckDuckGo that for you: ? python linux listen for file system changes

Pyinotify?

Janis
08.05.2017
09:09:03
found it too atm, seems like it's perfect for my need.

Romet
08.05.2017
09:09:54
tail -f file.txt | grep Success??

Janis
08.05.2017
12:28:13
@Romet since the update command itself takes some time, using this wouldn't be helpful

Sometimes it takes 5 minutes, in case of good bandwith speed it may take 1 minute until the textfile contains "Success", so I need somekind of "watchdog" to monitor the file.

Maxim
10.05.2017
12:17:31
Alexsander hi, I checked ponyjs... Is it main realization of rest? Will you plan to use KO into library or not?

hey, all

Alexander
10.05.2017
20:24:27
Hi Maxim, currently we use PonyJS on fineartbiblio.com, but didn't release it officially yet. At this moment PonyJS uses KnockoutJS observables. As it turns out, KnockoutJS has some drawbacks - its templates are hard to maintain, and Knockout observables are slow. But we like the idea of observables in general, and want to re-implement PonyJS using another implementation of observables. PonyJS is not REST-like, it allows to serialize multiple objects into a single data packet, use it on JS side, and then send changes back to Python.

Maxim
10.05.2017
21:00:21
Thanks, for fast anwser :)

Alexander
10.05.2017
21:06:41
With PonyJS we select objects on the backend side using standard Pony queries

Luckydonald
10.05.2017
21:10:34
So it is a `text in <field>` query? How is that field indexed?

Alexander
10.05.2017
21:18:15
You mean full text search? We use standard PostgreSQL search capabilities for that. Table has additional indexed column of tsvector type which value is calculated by trigger as a concatenation of all other fields. And then we add raw_sql fragment to the query in the way similar to that: search_text = request.args.get("search_text") query = query.filter(lambda: raw_sql( "obj.fts_column @@ plainto_tsquery($search_text)" ))

https://www.postgresql.org/docs/current/static/textsearch.html

Luckydonald
10.05.2017
21:33:56
Uh, cool.

Somehow still didn't had the speed I needed

Google
Janis
14.05.2017
17:47:33
First time I use relations inside pony. Each server has a "default_map" attribute, with the id of the map. Now how to link them? I tried this: https://puu.sh/vPSZD/b7e1f993e4.png

But when running my app I get: raise exc pony.orm.core.ERDiagramError: Entity definition CounterStrike_Server_Go_Maps was not found

Get a little further, but using the generated code from the Pony Web based generator now I get: https://puu.sh/vPUXC/c7172a7922.png

Alexander
14.05.2017
19:51:48
1) All related entities should belong to the same database. If you want to split entity definitions betweens several files you can define db object in one module and then import it into another module. 2) When you define an attribute of Set type it will keep a collection of related entities. It is better to name that attribute in a plural form, like items, students, orders, etc., because when you retrieve a value of attribute it will be set of objects. 3) Any entity should have a primary key. If you don't define any primary key, Pony will create a default integer primary key with name id. In your entity you didn't define any primary key and the name id is already taken by another attribute. You should rename id attribute of Map entity to something like servers

Rozen
14.05.2017
19:58:46
omg

pony for javascript

sorry it took me a while to read that

Janis
15.05.2017
03:35:09
@akozlovsky 1.) Yes it's the same database, object is initialized in config (I store SQL params inside this file, just to make sure I can edit settibgs easier). 3.) I've already creates the SQL code before, so how to go now? My 'Counterstrike_server_go_maps' table should just look like this: |id|name|display_name|

All I try to setup is a simple relation, one server is related to exactly one map. one map is related to many servers, but the relation from map to server isn't needed, because I only query from the server model.

Paul
15.05.2017
06:03:33
Pony always creates accessors in both directions on a relation.

Janis
15.05.2017
06:09:40
So how to setup this by using 1:1 relation (one server is related to exactly one map) & (1:n -> 1 one map is related to many servers) by using the pkey of map as relation id?

Paul
15.05.2017
06:13:53
Like this: https://editor.ponyorm.com/user/ptmcg/Server_to_map



Janis
15.05.2017
06:14:51
Could you show complete python code for models?

Paul
15.05.2017
06:16:26
Eh? You define the model for yourself using the designer. I don't know what "servers" and "maps" are in your application.

Do you mean sample Python code for creating Server and Map instances?

Janis
15.05.2017
06:17:52
Yes and the attributes (server has attribute "default_map" which is the relation to the map)

map only has id and name :)

Paul
15.05.2017
06:19:39
Can you see the model using the link I posted a minute ago?

Janis
15.05.2017
06:20:10
Now i see yes

Google
Janis
15.05.2017
06:20:21
I thought the relation should be directly to the id of map

now "servers" attribute of map is only virtual or did this exists as attribute in mysql too?

Paul
15.05.2017
06:21:01
That may be how it is implemented

Janis
15.05.2017
06:21:49
I thought I can create relation directly between the "default_map" and the id (pk) of map

Paul
15.05.2017
06:22:30
If you look at the code generated for MySql, Postgres, or Oracle, you can see that that is exactly the constraint that is created

For MySQL: ALTER TABLE `server` ADD CONSTRAINT `fk_server__default_map` FOREIGN KEY (`default_map`) REFERENCES `map` (`id`)

The designer shows you the logical relationship, not necessarily the low-level implementation detail

And there is no 'servers' attribute in the Map table itself. That is a virtual Set that is created by the Pony ORM so that you can access mapA.servers instead of writing SQL "Select server,map where server.default_map=map.id"

If you are used to doing this kind of database coding yourself, you will have to mentally take a step back. Model the relations using the designer, and then let the ORM do the implementation details.

You *could* work this way. Use the design tool to give you the table and default index definitions, then write your own SQL code using the database access libraries - you wouldn't even need to do that in Python. But you would be ignoring some of the ease-of-coding benefits of using an ORM

Since you said that servers have exactly 1 map, then I modeled that as a required attribute. Here is code that would create a map, then create a server that references that map.

with db_session: map1 = Map(name='map_1') server1 = Server(name='Colossus', default_map=map1) # do a query print("server {} uses map {}".format(server1.name, server1.default_map.name))

And this prints out: server Colossus uses map map_1

A little better-looking query and output: print("server {!r} uses map {!r}".format(server1.name, server1.default_map.name)) prints server 'Colossus' uses map 'map_1'

I could have used map1.name for the second argument to format, but that would not have used the relation, it would have just echoed back the attribute of the object I already had.

Janis
15.05.2017
06:42:26
All fine, I know this from virtual attributes as accessors from other frameworks :)

Paul
15.05.2017
06:44:15
So what was your question then?

Janis
15.05.2017
06:44:33
Now've understand I need to use this virtual attributes :)

Thanks

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