Giter Club home page Giter Club logo

Comments (5)

aleksandra-kim avatar aleksandra-kim commented on June 24, 2024

Original comment by Benjamin W. Portner (Bitbucket: pommespapst, GitHub: pommespapst).


Better-formatted code:

#!python

oActivity = bw.Database('some_db').search("some_activity")[0] 
oExchange = list(oActivity.exchanges())[1] 
oExchange["amount"]
# 0
oExchange["amount"] = 1 
oExchange["amount"] 
# still 0

from brightway2.

aleksandra-kim avatar aleksandra-kim commented on June 24, 2024

Original comment by Chris Mutel (Bitbucket: cmutel, GitHub: cmutel).


Sorry, that is frustrating! I can't reproduce this:

#!python

In [6]: db = Database("ecoinvent 3.5 cutoff")

In [7]: exc = list(db.random().exchanges())[0]

In [8]: exc['amount']
Out[8]: 0.000277

In [9]: exc['amount'] = 1

In [10]: exc['amount']
Out[10]: 1

Some things to try:

  1. Make sure your version of bw2data is up to date - latest is 3.4.4.
  2. Make sure you are getting an actual activity and exchange (not sure how this wouldn't be the case, but check anyway)
  3. If you are loading the exchange in the second half, note that your changes will only be written to the database if you call oExchange.save() after you make your modifications.

from brightway2.

aleksandra-kim avatar aleksandra-kim commented on June 24, 2024

Original comment by Benjamin W. Portner (Bitbucket: pommespapst, GitHub: pommespapst).


Looks like the error occurs if the handle to the exchange is not saved:

#!python

oAct = bw.Database('ecoinvent 3.5 APOS').random()

# does not work
list(oAct.exchanges())[0]["amount"]
list(oAct.exchanges())[0]["amount"] = 3
list(oAct.exchanges())[0]["amount"]

# works
oExc = list(oAct.exchanges())[0]
oExc["amount"]
oExc["amount"] = 3
oExc["amount"]

from brightway2.

aleksandra-kim avatar aleksandra-kim commented on June 24, 2024

Original comment by Chris Mutel (Bitbucket: cmutel, GitHub: cmutel).


There are two important details here:

  1. list(oAct.exchanges())[0]["amount"] will retrieve the exchanges from the database each time, and there is no guarantee that they are in the same order (by default database queries have no defined order unless you specify one). I am not sure if Brightway gives a default order - by guess is yes - but in general SQL queries don't unless you specify ORDER BY.
  2. list(oAct.exchanges())[0]["amount"] also won't work the second time because you are recreating the object each time. The changes you make in the first instance are not saved, and so disappear. It is the same as:
#!python

In [1]: class A:
   ...:     pass
   ...:

In [2]: a = A()

In [3]: a.foo = "bar"

In [4]: a = A()

In [5]: a.foo
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-031223489f27> in <module>()
----> 1 a.foo

AttributeError: 'A' object has no attribute 'foo'

from brightway2.

aleksandra-kim avatar aleksandra-kim commented on June 24, 2024

Original comment by Benjamin W. Portner (Bitbucket: pommespapst, GitHub: pommespapst).


Thanks for the clarification!

Concerning the order of exchanges: it is changing every time indeed. Thanks for the hint!

from brightway2.

Related Issues (20)

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.