Code Monkey home page Code Monkey logo

Comments (2)

citrusvanilla avatar citrusvanilla commented on May 29, 2024 1

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.

linecker avatar linecker commented on May 29, 2024 1

Thank you so much for your fast reply and the python lesson - I really tricked myself into this one!

from tinyflux.

Related Issues (14)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.