Пример #1
0
def test_funny_project_names():
    NAMES = [
        "Powerلُلُصّبُلُلصّبُررً ॣ ॣh ॣ ॣ冗",
        "Roses are red, violets are blue. Hope you enjoy terminal hue",
        "True",
        "None",
        "1.0/0.0",
        "0xabad1dea",
        "!@#$%^&*()`~",
        "<>?:'{}|_+",
        r",./;'[]\-=",
        "Ω≈ç√∫˜µ≤≥÷",
        "田中さんにあげて下さい",
        "`ィ(´∀`∩",
        "👾 🙇 💁 🙅 🙆 🙋 🙎 🙍 ",
        "הָיְתָהtestالصفحات التّحول",
        " ",
    ]
    error_found = False
    for name in NAMES:
        try:
            projects.set_current(name)
            assert [x for x in os.listdir(projects.dir)]
            print("This is OK:", name)
        except:
            print("This is not OK:", name)
            error_found = True
    if error_found:
        raise ValueError("Oops")
Пример #2
0
def restore_project_directory(fp):
    """Restore backup created using ``backup_project_directory``.

    Raises an error is the project already exists.

    ``fp`` is the filepath of the backup archive.

    Returns the name of the newly created project."""
    def get_project_name(fp):
        reader = codecs.getreader("utf-8")
        with tarfile.open(fp, "r|gz") as tar:
            for member in tar:
                if member.name[-17:] == "project-name.json":
                    return json.load(reader(tar.extractfile(member)))["name"]
            raise ValueError("Couldn't find project name file in archive")

    assert os.path.isfile(fp), "Can't find file at path: {}".format(fp)
    print(
        "Restoring project backup archive - this could take a few minutes...")
    project_name = get_project_name(fp)

    with tarfile.open(fp, "r|gz") as tar:
        tar.extractall(projects._base_data_dir)

    _current = projects.current
    projects.set_current(project_name, update=False)
    projects.set_current(_current)
    return project_name
Пример #3
0
def test_delete_project_keep_directory():
    projects.set_current("foo")
    foo_dir = projects.dir
    projects.set_current("bar")
    projects.delete_project("foo")
    assert os.path.isdir(foo_dir)
    assert "foo" not in projects
    assert projects.current == 'bar'
Пример #4
0
def test_delete_project_remove_directory():
    projects.set_current("foo")
    foo_dir = projects.dir
    projects.set_current("bar")
    projects.delete_project("foo", delete_dir=True)
    assert not os.path.isdir(foo_dir)
    assert "foo" not in projects
    assert projects.current == "bar"
Пример #5
0
def bw2test(wrapped, instance, args, kwargs):
    config.dont_warn = True
    config.is_test = True
    config.cache = {}
    projects._use_temp_directory()
    projects.set_current("default")
    geocollections.__init__()
    intersections.__init__()
    loadings.__init__()
    return wrapped(*args, **kwargs)
Пример #6
0
def check_database(project_name, database):

    if project_name in projects:
        current = projects.current
        projects.set_current(project_name)
        check = database in databases
        projects.set_current(current)

        return check

    else:
        return False
Пример #7
0
def random_project(wrapped, instance, args, kwargs):
    config.is_test = True
    projects._restore_orig_directory()
    string = random_string()
    while string in projects:
        string = random_string()
    projects.set_current(string)
    build_databases()
    result = wrapped(*args, **kwargs)
    projects.set_current("default", writable=False)
    projects.delete_project(name=string, delete_dir=True)
    return result
Пример #8
0
def test_copy_project():
    ds = ProjectDataset.get(ProjectDataset.name == projects.current)
    ds.data["this"] = "that"
    ds.save()

    databases["foo"] = "bar"
    projects.copy_project("another one", False)
    assert "another one" in projects

    ds = ProjectDataset.get(ProjectDataset.name == "another one")
    assert ds.data["this"] == "that"

    projects.set_current("another one")
    assert databases["foo"] == "bar"
Пример #9
0
def test_create_lock_file():
    projects.set_current("foo")
    config.p['lockable'] = True
    projects.set_current("foo", writable=False)
    assert not os.path.isfile(os.path.join(projects.dir, "write-lock"))
    projects.set_current("bar")
    assert not os.path.isfile(os.path.join(projects.dir, "write-lock"))
    config.p['lockable'] = True
    projects.set_current("bar")
    assert os.path.isfile(os.path.join(projects.dir, "write-lock"))
Пример #10
0
    def write_database(self, project, name, overwrite=False):

        if project not in projects:
            warn("The project '{}' doesn't exist, it will be created".format(
                project))

        if projects.current != project:
            projects.set_current(project)

        if 'biosphere3' not in databases:
            bw2setup()

        if name in databases:
            if not overwrite:
                assert 0, 'Database already exists, either use overwrite=True, or use another name'
            else:
                print('Deleting existing database {}'.format(name))
                del databases[name]

        w.write_brightway2_database(self.db, name)
Пример #11
0
    def extract_bw2_database(self, project_name, database_name):

        if isinstance(database_name, str):
            database_names = [database_name]
        assert project_name in projects, "That project doesn't exist"
        assert isinstance(
            self.database_names,
            (list, tuple, set)), "Must pass list of database names"

        self.database_names.extend(database_names)

        projects.set_current(project_name)

        input_db = w.extract_brightway2_databases(database_names)

        fix_unset_technosphere_and_production_exchange_locations(input_db)
        #remove_nones(input_db)

        self.db.extend(input_db)
        print(self.db)
Пример #12
0
def multi_worker(args):
    """Calculate a single Monte Carlo iteration for many demands.

    ``args`` are in order:
        * ``project``: Name of project
        * ``demands``: List of demand dictionaries
        * ``method``: LCIA method

    Returns a list of results: ``[(demand dictionary, result)]``

    """
    project, demands, method = args
    projects.set_current(project, writable=False)
    mc = MonteCarloLCA(demands[0], method)
    next(mc)
    results = []
    for demand in demands:
        mc.redo_lcia(demand)
        results.append((demand, mc.score))
    return results
Пример #13
0
def test_switch_project_correctly_switches_database_objects():
    database = DatabaseChooser("testy")
    data = {
        ("testy", "A"): {},
        ("testy", "C"): {"type": "biosphere"},
        ("testy", "B"): {
            "exchanges": [
                {"input": ("testy", "A"), "amount": 1, "type": "technosphere"},
                {"input": ("testy", "B"), "amount": 1, "type": "production"},
                {"input": ("testy", "C"), "amount": 1, "type": "biosphere"},
            ]
        },
    }
    database.write(data)

    table = db._tables[0]
    current_db_location = copy(db.db.database)
    assert table.select().count()

    projects.set_current("new one")
    assert not table.select().count()
    assert current_db_location != db.db.database
Пример #14
0
def test_switch_project_correctly_switches_database_objects():
    database = DatabaseChooser("testy")
    data = {
        ("testy", "A"): {},
        ("testy", "C"): {
            'type': 'biosphere'
        },
        ("testy", "B"): {
            'exchanges': [
                {
                    'input': ("testy", "A"),
                    'amount': 1,
                    'type': 'technosphere'
                },
                {
                    'input': ("testy", "B"),
                    'amount': 1,
                    'type': 'production'
                },
                {
                    'input': ("testy", "C"),
                    'amount': 1,
                    'type': 'biosphere'
                },
            ]
        },
    }
    database.write(data)

    table = db._tables[0]
    current_db_location = copy(db.db.database)
    assert table.select().count()

    projects.set_current("new one")
    assert not table.select().count()
    assert current_db_location != db.db.database
Пример #15
0
def direct_solving_worker(args):
    project, demand, method, iterations = args
    projects.set_current(project, writable=False)
    mc = DirectSolvingMonteCarloLCA(demand=demand, method=method)
    return [next(mc) for x in range(iterations)]
Пример #16
0
def test_project_directories():
    projects.set_current("foo")
    for dirname in projects._basic_directories:
        assert os.path.isdir(os.path.join(projects.dir, dirname))
Пример #17
0
def test_copy_project():
    databases['foo'] = 'bar'
    projects.copy_project('another one', False)
    assert 'another one' in projects
    projects.set_current('another one')
    assert databases['foo'] == 'bar'
Пример #18
0
def test_read_only_cant_write():
    projects.set_current("foo")
    config.p['lockable'] = True
    projects.set_current("foo", writable=False)
    with pytest.raises(ReadOnlyProject):
        databases['foo'] = 'bar'
Пример #19
0
def test_iterating_over_projects_no_error():
    projects.set_current("foo")
Пример #20
0
def test_iterating_over_projects_no_error():
    projects.set_current("foo")
    projects.set_current("bar")
    projects.set_current("baz")
    for x in projects:
        projects.set_current(x.name)
Пример #21
0
def test_delete_project():
    projects.set_current("foo")
    projects.set_current("bar")
    projects.delete_project("foo")
    assert "foo" not in projects
    assert projects.current == 'bar'
Пример #22
0
def test_set_readonly_project_first_time():
    projects.set_current("foo", writable=False)
    assert not projects.read_only
Пример #23
0
def test_set_readonly_project():
    projects.set_current("foo")
    assert not projects.read_only
    config.p['lockable'] = True
    projects.set_current("foo", writable=False)
    assert projects.read_only
Пример #24
0
def test_set_project():
    projects.set_current("foo")
    assert projects.current == "foo"
Пример #25
0
def test_set_project_creates_new_project():
    other_num = len(projects)
    projects.set_current("foo")
    assert len(projects) == other_num + 1
Пример #26
0
def test_delete_current_project_no_name():
    projects.set_current("foo")
    projects.delete_project()
    assert "foo" not in projects
    assert projects.current != "foo"
Пример #27
0
def get_static_forest_keys():
    """get the key of the forest sequestation processes in the set `forest`.
    Needed to consider forest sequestation dynamic in already installed dbs that 
    are static.
    When unit processes from other databases need to be evaluated dynamically is enough to add their name 
    to the set set `forest` inside the function.
    
    #Wrapped into a function otherwise it points to the default project if temporalis
    is imported before the current project is set """
    #they are all underbark, check if the bark CO2 is considered or not...if needed
    
    
    ei_22 = ["hardwood, Scandinavian, standing, under bark, in forest",
    "hardwood, standing, under bark, in forest",
    "eucalyptus ssp., standing, under bark, u=50%, in plantation",
    "meranti, standing, under bark, u=70%, in rainforest",
    "softwood, standing, under bark, in forest",
    "azobe (SFM), standing, under bark, in rain forest",
    "paraná pine, standing, under bark, in rain forest",
    "softwood, Scandinavian, standing, under bark, in forest"]

    ei_32_33 = [
    "softwood forestry, paraná pine, sustainable forest management",
     "hardwood forestry, mixed species, sustainable forest management",
     "hardwood forestry, beech, sustainable forest management",
     "softwood forestry, spruce, sustainable forest management",
     "hardwood forestry, meranti, sustainable forest management",
     "hardwood forestry, azobe, sustainable forest management",
     "softwood forestry, pine, sustainable forest management",
     #"import of roundwood, azobe from sustainable forest management, CM, debarked", # no
     #"import of roundwood, meranti from sustainable forest management, MY, debarked", # no
     "softwood forestry, mixed species, sustainable forest management",
     "hardwood forestry, oak, sustainable forest management",
     "hardwood forestry, birch, sustainable forest management",
     "hardwood forestry, eucalyptus ssp., sustainable forest management"
     ]
    
    #it should be the first one, but as it is not temporalis must be the second
    #~estelle=[
    #~'_Wood waste, sorted at panel plant, 20% water on dry mass basis/ EU U',
            #~'_Wood construction waste, 20% water on dry mass basis',
            #~]
    estelle=[
    #'_Allocation correction, spruce, 20% water on dry basis',
 #'_Aqueous microporous paint for wood, SIPEV/FR S_26082014',
 #'_Aqueous phase coating, SIPEV/FR S_20140527',
  #'_Polyurethane glue, Bostik, at plant/FR S_20140627',
 #'_Transport, lorry >28t, fleet average/CH S_litre',
 #'Polyethylene, HDPE, granulate, at plant/RER',

 '_Beech industrial roundwood, 80% water on dry mass basis, at forest road/FR S_20120830',
 '_Douglas-fir industrial roundwood, 65% water on dry mass basis, at forest road/FR S_20120830',
 '_Maritime pine industrial roundwood, 100% water on dry mass basis, at forest road/FR S_20120830',
 '_Oak industrial roundwood, 80% water on dry mass basis, at forest road/FR S_20120830',
 '_Scotch pine industrial roundwood, 104% water on dry mass basis, at forest road/FR S_20120830',
 '_Spruce industrial roundwood, 111% water on dry mass basis, at forest road/FR S_20120830',
 '_Wood waste, sorted at panel plant, 20% water on dry mass basis/ EU U',
 '_Wood construction waste, 20% water on dry mass basis',
 ]

            
            
    test=['co2bio_test']

    #dunno why this below returns skips some datasets
    #FORMIT_forest=[x['name'] for x in Database(db) if x['name'].split(',')[-1]==' NPP' for db in ['BAU0', 'BAU26', 'BAU45', 'BAU85', 'SCEN2_45', 'SCEN3_45', 'SCEN4_45', 'SCEN5_45', 'SCEN6_45']]

    #~FORMIT_forest=[]
    #~for db in ['BAU0', 'BAU26', 'BAU45', 'BAU85', 'SCEN2_45', 'SCEN3_45', 'SCEN4_45', 'SCEN5_45', 'SCEN6_45','trial']:
        #~FORMIT_forest.extend([x['name'] for x in Database(db) if x['name'].split(',')[-1]==' biogenic'])

    forest = set( ei_22 + ei_32_33 + estelle + test )# + FORMIT_forest)
    projects.set_current("{}".format(projects.current)) #need to do this otherwise uses default project if imported before setting the proj
    db = Database(config.biosphere)
    
    #search 'Carbon dioxide, in air' sequestered from processes in `forest` (empty set when biopshere not existing like in tests)
    return set() if not db else \
           set([x.output.key for x in db.get('cc6a1abb-b123-4ca6-8f16-38209df609be').upstream(kinds=None) if x.output.get('name') in forest])
Пример #28
0
def test_contains():
    assert "default" in projects
    projects.set_current("foo")
    assert "foo" in projects
Пример #29
0
def test_set_current_reset_metadata():
    databases['foo'] = 'bar'
    assert 'foo' in databases
    projects.set_current('foo')
    assert 'foo' not in databases
Пример #30
0
def test_len():
    assert len(projects) == 1
    projects.set_current("foo")
    assert len(projects) == 2