Project-OMOTES / simulator-core

Core library for NWN simulator
GNU General Public License v3.0
1 stars 0 forks source link

Simplify import tree for simulator-core package #58

Open samvanderzwan opened 11 months ago

samvanderzwan commented 11 months ago

When do we only import required classes and function and when do we import entire module. To be discussed with @MichielTukker and @TNO-SlaFleur

lfse-slafleur commented 11 months ago

Entire modules force you to then write it like:

import somemodule

somemodule.somefunction('hello!')

if you import the entire function:

from somemodule import somefunction

somefunction('hello!')

The first case is useful when you expect name clashes or when it makes the code clearer. The second case is useful for brevity but does not explain where it comes from on the same line it is used. This downside is usually lessed by the use of IDE's but it does give the impression that the function is defined in the current module and not imported.

Either is fine but beware of the up- & downsides and adjust accordingly.

Use the first case to describe where a function comes from when necessary:

import influxdb_timeseries_writer

influxdb_timeseries_writer.write('This is definitely a timeseries') # Note that this 'write' function is non-descriptive without the module name in front of it.

Use the second case where brevity is required and the function/class name is already self-explanatory:

from influxdb_timeseries_writer import InfluxdbConnection

with InfluxdbConnection('some host', 'some port') as conn:
   # Do something with the conn
lfse-slafleur commented 11 months ago

And as a bonus tip:

# Python stdlib stuff
from dataclasses import dataclass
from pathlib import Path

# 3rd party dependencies
from pandapipes import some_function_I_dont_know

# Your own imports
from simulator_core.influxdb_writer import InfluxdbConnection
vanmeerkerk commented 7 months ago

@samvanderzwan I believe this issue can be closed? Or does this issue relate to the import in init.py?

vanmeerkerk commented 7 months ago

45 is original/associated issue.

samvanderzwan commented 7 months ago

Remove import in solver from other parts of the codes, this is mainly for the solver_pipe where the defaults are imported

samvanderzwan commented 7 months ago

pydaps, to create a graph of class diagram of your code