Kristian
Similar problems arise with e.g. datetime.datetime.
Romet
if you're using the Decimal type you really want to use strings
Romet
there's no point in it otherwise, you'll be missing out on precision which is kind of its entire point
Kristian
Well, in JSON it's all a string, right?
{ 'd': Decimal('10.000000000000000001') }
-> '{ "d": "10.000000000000000001" }' or
-> '{ "d": 10.000000000000000001}'
In both ways, no precision is lost. I would assume it will only be lost when a default decoder does
'{ "d": 10.000000000000000001}' -> {'d': float(10.)}
but not when I give an object_hook to json.JSONDecoder which converts numbers.
Kristian
But you have a point: At least in Postgres Pony uses jsonb, so '{ "d": 10.000000000000000001}' will probably loose precision in the Database, as the database will interpret it as float.
Alexander
Pony allows to query JSON fields in queries, like
select(obj for obj in MyObject
if obj.json_field[x][y] > z)
In such queries we expect that JSON items have the same type in Python and in the database. If values can be converted to some other type when loading from the database it may be surprising and non-intuitive.
Maybe, it is better to perform such conversion explicitly. Say, you know that some fields are stored as strings, but actually datetime or decimal values, and convert them to that type when you read that JSON value
Kristian
Hm, ok, I see. I never understood the magic that translates these generators to sql. Thanks anyway, I will use the explicit way, converting them manually.
Anonymous
Hey, sorry to interrupt can someone help me with creating ERD on a database?
Anonymous
This is some part of python code in a project of me:
Anonymous
https://pastebin.com/raw/MFf3GzFf
Anonymous
Any idea how to make this code shorter? Not including the json inside each function?
Anonymous
It's not that related to PONY but the community here is that nice, so I ask...
stsouko
if request.form['token'] != config.secret_token: return False else: return True
Replace with
return request.form['token'] == config.secret_token
Anonymous
Okay...thought I could include this check with lower lines of code, but thanks
Jordi
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
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 ;"""
]
Alexander
for command in commands:
db.execute(command)
Jordi
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
Try without DELIMITER command
Alexander
Maybe it is unnecessary
Jordi
I couldn't make it work, but I see it should be unnecessary. https://github.com/PyMySQL/mysqlclient-python/issues/64
Anonymous
Any way to get the "query" and check into a one line if statement? https://pastebin.grey.pw/?6e32153ecdd11688#x+z65JcDdm9E32e2JeUUJP8lAfNKzQSr0WHASwb+nmA=
Alexander
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:
Anonymous
It's NOT the primary key
Alexander
Then you can write:
if Server.get(interna_id=3).is_busy:
But your current code looks more clear
Lucky
Anonymous
Thanks I prefer short code :)
Anonymous
How to get the inserted object JSON serialized? https://pastebin.com/raw/VuuWXaYi
Alexander
Try to use to_dict method: https://docs.ponyorm.com/api_reference.html?highlight=to_dict#Entity.to_dict
Anonymous
Okay, much easier :) http://stackoverflow.com/a/34765354/7878100
Anonymous
Since I need to pass the "data" somehow to celery
Anonymous
Any idea where things go wrong? https://puu.sh/vHuZi/63827b7051.png
Anonymous
I've changed from Optional back to Required in the meantime.
Anonymous
Of course it has... https://puu.sh/vHvDZ/cee6006e46.png
Alexander
I think you have circular import. When entity definition processed, config module is not fully loaded yet
Alexander
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)
Anonymous
So just to make sure I understand, this will make python load the value before it's used?
Alexander
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
Anonymous
By the way, are python related questions allowed too, like best practices?
Lucky
I wouldn't mind
Lucky
Just do it
Can be deleted anyway xD
Anonymous
Well here we go
Anonymous
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?
Lucky
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
Anonymous
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).
Anonymous
Any links for this?
Lucky
As I am on the bike right now, and it starts to rain, probably you googling makes mire sense xD
Anonymous
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 :)
Lucky
There must be a linux one too
Lucky
Let me DuckDuckGo that for you:
🔎 python linux listen for file system changes
Lucky
Pyinotify?
Anonymous
found it too atm, seems like it's perfect for my need.
Romet
tail -f file.txt | grep Success?😄
Anonymous
@Romet since the update command itself takes some time, using this wouldn't be helpful
Anonymous
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.
Max
Alexsander hi, I checked ponyjs... Is it main realization of rest? Will you plan to use KO into library or not?
Max
hey, all
Alexander
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.
Max
Thanks, for fast anwser :)
Lucky
Alexander
With PonyJS we select objects on the backend side using standard Pony queries
Lucky
So it is a `text in <field>` query?
How is that field indexed?
Alexander
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)"
))
Alexander
https://www.postgresql.org/docs/current/static/textsearch.html
Lucky
Uh, cool.
Lucky
Somehow still didn't had the speed I needed
Anonymous
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
Anonymous
But when running my app I get: raise exc
pony.orm.core.ERDiagramError: Entity definition CounterStrike_Server_Go_Maps was not found
Anonymous
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
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
omg
Rozen
pony for javascript
Rozen
sorry it took me a while to read that
Anonymous
@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|
Anonymous
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.
Anonymous
Pony always creates accessors in both directions on a relation.
Anonymous
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?
Anonymous
Like this: https://editor.ponyorm.com/user/ptmcg/Server_to_map
Anonymous
screenshot from designer of creating 1-many relation