Comments (2)
Hi @linecker thanks for the reproducible example. Hope TinyFlux is working well for you.
To answer your question- no, this is not the intended behavior, the module API should perform the same irrespective of the choice of Storage
instance.
There's a couple of things going on here, but basically it boils down to the line data["tags"]["id"] = "not"
- you are changing the value of data["tags"]["id"]
which is also the reference that is stored in p1.tags
. Take a look at the following minimum example:
>>> tf = TinyFlux(storage=storages.MemoryStorage)
>>> data = {"my_tag_key": "my_tag_value"}
>>> p1 = Point(tags=data)
>>> p1.tags
{"my_tag_key": "my_tag_value"}
>>> data["my_tag_key"] = "some_new_value" # this is where you are changing the value of the reference
>>> p1.tags
{'my_tag_key': 'some_new_value'}
See the issue? p1.tags
is just a reference to the dict pointed to by data
and when you change the value of data
you are also changing what p1.tags
evaluates to. If you are new to programming this can certainly be confusing.
The CSVStorage
does not have this issue because the value of Point.tags
is written to the CSV file and the dict
reference ceases to exist, whereas in the MemoryStorage
instance, the dict reference is kept around forever.
To fix, you should not be changing the values of the source data that your Points come from in order to make new Points. You need to create new source data for each point. The best way to do this is to just pass the values into the Point constructor directly. If you can't do that, make a new dict for each point. Example:
Making new dicts for each Point:
tiny_flux = TinyFlux(storage=storages.MemoryStorage)
data = {
"measurement": "sma",
"tags": {
"id": "should",
},
"time": "0",
"fields": {
"value": 1.0,
},
}
p1 = Point(
time=datetime.datetime.now(),
measurement=data["measurement"],
fields=data["fields"],
tags=data["tags"],
)
tiny_flux.insert(p1)
data2 = {
"measurement": "sma",
"tags": {
"id": "not",
},
"time": "0",
"fields": {
"value": 1.0,
},
}
p2 = Point(
time=datetime.datetime.now(),
measurement=data2["measurement"],
fields=data2["fields"],
tags=data2["tags"],
)
tiny_flux.insert(p2)
tags = TagQuery()
result = tiny_flux.search(tags.id == "should")
print(result)
prints
[Point(time=2023-05-15T19:46:07.812680+00:00, measurement=sma, tags=id:should, fields=value:1.0)]
Or better yet:
tiny_flux = TinyFlux(storage=storages.MemoryStorage)
p1 = Point(
time=datetime.datetime.now(),
measurement="sma",
fields={"value": 1.0},
tags={"id": "should"},
)
tiny_flux.insert(p1)
p2 = Point(
time=datetime.datetime.now(),
measurement="sma",
fields={"value": 1.0},
tags={"id": "not"},
)
tiny_flux.insert(p2)
tags = TagQuery()
result = tiny_flux.search(tags.id == "should")
print(result)
prints the same.
from tinyflux.
Thank you so much for your fast reply and the python lesson - I really tricked myself into this one!
from tinyflux.
Related Issues (14)
- "Line contains NUL" Exception after editing and saving a TinyFlux CSV in Excel HOT 5
- CSVStorage append blank lines on Windows HOT 1
- Field values of 0.0 are serialized to the DB as "_none" HOT 1
- Improve typing using mypy strict mode HOT 1
- Add ability to remove individual tags and fields from Points HOT 5
- Updating a Point with CSVStorage throws a TypeError
- Make package PEP 561 compliant HOT 1
- Compact storage with shorter tag and field key prefixes HOT 4
- Empty string tag values are read from storage as "_none" HOT 1
- Tag and Field keys should not be able to be removed in update callables
- Data synchronisation from TinyFlux to InfluxDB or other HOT 1
- Python 3.12 support
- Retrieving Points with the max timestamp from the db HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tinyflux.