def stdin(sys_argv): """ Imports Kafka & Cassandra parameters. """ # Sets sensitive variables from ENV file try: path_home = os.getcwd() os.chdir(r"../../util/settings") settings = dc.Config(dc.RepositoryEnv(".env")) os.chdir(path_home) except: raise OSError("Cannot import ENV settings. Check path for ENV.") # Imports terminal input for simulation & Kafka settings try: p = {} p["spark_name"] = settings.get("SPARK_NAME") p["cassandra"] = settings.get("CASSANDRA_MASTER", cast=dc.Csv()) p["cassandra_key"] = settings.get("CASSANDRA_KEYSPACE") p["kafka_brokers"] = settings.get("KAFKA_BROKERS") p["kafka_topic"] = settings.get("KAFKA_TOPIC", cast=dc.Csv()) except: raise ValueError("Cannot interpret external settings. Check ENV file.") return p
def stdin(sys_argv): """ Imports simulation & Kafka parameters, then assigns battery parameters. """ # Sets sensitive variables from ENV file try: path_home = os.getcwd() os.chdir(r"../../util/settings") settings = dc.Config(dc.RepositoryEnv(".env")) os.chdir(path_home) except: raise OSError("Cannot import ENV settings. Check path for ENV.") # Imports terminal input for simulation & Kafka settings try: p = {} p["id"] = uu.uuid4() p["cycles"] = range(abs(int(sys_argv[2]))) p["current"] = abs(float(sys_argv[3])) p["v_min"] = float(sys_argv[4]) p["v_range"] = float(sys_argv[5]) - p["v_min"] p["kafka_brokers"] = settings.get("KAFKA_BROKERS", cast=dc.Csv()) p["kafka_topic"] = settings.get("KAFKA_TOPIC") p["initial_time"] = dt.datetime.now() except: raise ValueError("Cannot interpret parameters. Check terminal input.") # Imports models from serialized models file try: os.chdir(r"../../util/models") with open("models_charge_discharge.pk", "rb") as pickled_models: p["models"] = pk.load(pickled_models) except: raise OSError("Cannot import models. Check path for models file.") # Generates cathode and initial capacity pseudo-randomly p["cathode"] = nprnd.choice([ "W", "X", "Y", "Z", ]) if p["cathode"] == "W": p["capacity"] = nprnd.choice(range(5000, 6001)) elif p["cathode"] == "X": p["capacity"] = nprnd.choice(range(9500, 12001)) elif p["cathode"] == "Y": p["capacity"] = nprnd.choice(range(2000, 8001)) else: p["capacity"] = nprnd.choice(range(4500, 9001)) return p
# Media & Static # set 's3_folder_storage.s3.DefaultStorage' in settings.ini for production DEFAULT_FILE_STORAGE = decouple.config( 'DEFAULT_FILE_STORAGE', default='django.core.files.storage.FileSystemStorage') # set 's3_folder_storage.s3.StaticStorage' in settings.ini for production STATICFILES_STORAGE = decouple.config( 'STATICFILES_STORAGE', default='django.contrib.staticfiles.storage.StaticFilesStorage') STATIC_S3_PATH = "static" DEFAULT_S3_PATH = "media" PIPELINE_LESS_BINARY = decouple.config('PIPELINE_LESS_BINARY', cast=decouple.Csv(), default='') PIPELINE_COMPILERS = decouple.config( 'PIPELINE_COMPILERS', cast=decouple.Csv(), default='pipeline.compilers.less.LessCompiler, ' 'pipeline.compilers.stylus.StylusCompiler', ) # You've installed lessc, right? if decouple.config('USE_PRECOMPILERS', cast=bool, default=False): COMPRESS_PRECOMPILERS = ( ('text/less', '/opt/local/lib/node_modules/less/bin/lessc {infile} {outfile}'), ) COMPRESS_OUTPUT_DIR = decouple.config('COMPRESS_OUTPUT_DIR', default='CACHE')
def main(): # noqa: C901 """ Entrypoint for command line execution only. """ _initialize_logging() logger.info('[%s] Starting...', datetime.datetime.now()) # Settings. DATALOGGER_TIMEOUT = decouple.config('DATALOGGER_TIMEOUT', default=20, cast=float) DATALOGGER_SLEEP = decouple.config('DATALOGGER_SLEEP', default=0.5, cast=float) DATALOGGER_INPUT_METHOD = decouple.config('DATALOGGER_INPUT_METHOD') DATALOGGER_API_HOSTS = decouple.config( 'DATALOGGER_API_HOSTS', cast=decouple.Csv(post_process=tuple)) DATALOGGER_API_KEYS = decouple.config( 'DATALOGGER_API_KEYS', cast=decouple.Csv(post_process=tuple)) DATALOGGER_MIN_SLEEP_FOR_RECONNECT = decouple.config( 'DATALOGGER_MIN_SLEEP_FOR_RECONNECT', default=1.0, cast=float) if not DATALOGGER_API_HOSTS or not DATALOGGER_API_KEYS: raise RuntimeError('API_HOSTS or API_KEYS not set') if len(DATALOGGER_API_HOSTS) != len(DATALOGGER_API_KEYS): raise RuntimeError( 'The number of API_HOSTS and API_KEYS given do not match each other' ) serial_kwargs = dict(telegram_timeout=DATALOGGER_TIMEOUT, ) if DATALOGGER_INPUT_METHOD == 'serial': serial_kwargs.update( dict( url_or_port=decouple.config('DATALOGGER_SERIAL_PORT'), baudrate=decouple.config('DATALOGGER_SERIAL_BAUDRATE', cast=int, default=115200), bytesize=decouple.config('DATALOGGER_SERIAL_BYTESIZE', cast=int, default=serial.EIGHTBITS), parity=decouple.config('DATALOGGER_SERIAL_PARITY', cast=str, default=serial.PARITY_NONE), stopbits=serial.STOPBITS_ONE, xonxoff=1, rtscts=0, )) elif DATALOGGER_INPUT_METHOD == 'ipv4': serial_kwargs.update( dict(url_or_port='socket://{}:{}'.format( decouple.config('DATALOGGER_NETWORK_HOST'), decouple.config('DATALOGGER_NETWORK_PORT', cast=int), ))) else: raise RuntimeError('Unsupported DATALOGGER_INPUT_METHOD') datasource = None while True: if not datasource: datasource = read_telegram(**serial_kwargs) telegram = next(datasource) # Do not persist connections when the sleep is too high. if DATALOGGER_SLEEP >= DATALOGGER_MIN_SLEEP_FOR_RECONNECT: datasource = None logger.info("[%s] Telegram read", datetime.datetime.now()) for current_server_index in range(len(DATALOGGER_API_HOSTS)): current_api_host = DATALOGGER_API_HOSTS[current_server_index] current_api_url = '{}/api/v1/datalogger/dsmrreading'.format( current_api_host) current_api_key = DATALOGGER_API_KEYS[current_server_index] try: _send_telegram_to_remote_dsmrreader( telegram=telegram, api_url=current_api_url, api_key=current_api_key, timeout=DATALOGGER_TIMEOUT, ) except Exception as error: logger.exception(error) logger.debug("[%s] Sleeping for %s second(s)", datetime.datetime.now(), DATALOGGER_SLEEP) time.sleep(DATALOGGER_SLEEP)
def maybe(s: t.Optional[str]) -> t.Optional[SomeType]: return cast(s) if s else None return maybe cast_bool = decouple.Config(None)._cast_boolean default_casts = { int: int, float: float, complex: complex, bool: cast_bool, str: str, bytes: str.encode, t.List[int]: decouple.Csv(int), t.List[float]: decouple.Csv(float), t.List[complex]: decouple.Csv(complex), t.List[bool]: decouple.Csv(cast_bool), t.List[str]: decouple.Csv(str), t.List[bytes]: decouple.Csv(str.encode), t.Optional[int]: optional(int), t.Optional[float]: optional(float), t.Optional[complex]: optional(complex), t.Optional[bool]: optional(cast_bool), t.Optional[str]: optional(str), t.Optional[bytes]: optional(str.encode), t.Optional[t.List[int]]: optional(decouple.Csv(int)), t.Optional[t.List[float]]: optional(decouple.Csv(float)), t.Optional[t.List[complex]]: optional(decouple.Csv(complex)), t.Optional[t.List[bool]]: optional(decouple.Csv(cast_bool)),
import decouple import os from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = decouple.config('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = decouple.config('DEBUG', default=False, cast=bool) ALLOWED_HOSTS = decouple.config('ALLOWED_HOSTS', cast=decouple.Csv()) # Application definition INSTALLED_APPS = [ 'gallery.apps.GalleryConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sites', 'django.contrib.flatpages', 'imagekit', 'widget_tweaks', ]
import decouple from dj_database_url import parse as database_url # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = decouple.config("SECRET_KEY") # SECURITY WARNING: don't run with debug turned on in production! DEBUG = decouple.config("DEBUG", default=False, cast=bool) ALLOWED_HOSTS = decouple.config( "ALLOWED_HOSTS", default=[], cast=decouple.Csv() ) INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'bootstrapform', 'ckeditor', 'blog.apps.BlogConfig', 'accounts.apps.AccountsConfig', 'social.apps.SocialConfig', 'contact.apps.ContactConfig',
import os import decouple BASE_DIR = os.path.dirname(__file__) DATABASE = decouple.config("DATABASE") ADMINS = decouple.config("ADMINS", cast=decouple.Csv(int)) DEFAULT_LANGUAGE = "pt_BR" DEFAULT_PROJECT = "python" AVAILABLE_LANGUAGES = { "pt_BR": "Brazilian Portuguese", "es": "Spanish", } AVAILABLE_PROJECTS = { "python": "Python", "jupyter": "Jupyter", } SENTRY_DSN = decouple.config("SENTRY_DSN", default="")
import os import decouple BASE_DIR = os.path.dirname(__file__) TELEGRAM_TOKEN = decouple.config("TELEGRAM_TOKEN") TRANSIFEX_TOKEN = decouple.config("TRANSIFEX_TOKEN") DATABASE = decouple.config("DATABASE") CACHE_URL = decouple.config("CACHE_URL", default="memory://") ADMINS = decouple.config("ADMINS", default="", cast=decouple.Csv(int)) BETA_USERS = decouple.config("BETA_USERS", default="", cast=decouple.Csv(int)) DEFAULT_LANGUAGE = "pt_BR" DEFAULT_PROJECT = "python" AVAILABLE_LANGUAGES = { "pt_BR": "Brazilian Portuguese", "es": "Spanish", } AVAILABLE_PROJECTS = { "python": "Python", "jupyter": "Jupyter", } SENTRY_DSN = decouple.config("SENTRY_DSN", default="")
_CORS_EXPOSE_HEADERS_DEFAULT = [ "X-Pagination-Count", "X-Pagination-Num-Pages", "X-Pagination-Page", "X-Pagination-Page-Size", "X-Request-ID", "X-Api-Version", "X-RateLimit-Reset", "X-RateLimit-Remaining", "X-RateLimit-Limit", "Retry-After", ] CORS_EXPOSE_HEADERS = config( "CORS_EXPOSE_HEADERS", default=",".join(_CORS_EXPOSE_HEADERS_DEFAULT), cast=decouple.Csv(), ) BASIC_AUTH_USERNAME = config("BASIC_AUTH_USERNAME", default="admin") BASIC_AUTH_PASSWORD = config("BASIC_AUTH_PASSWORD", default="admin") ADMIN_EMAIL = config("ADMIN_EMAIL", default="*****@*****.**") ADMIN_PASSWORD = config("ADMIN_PASSWORD", default="admin") MAIL_DEBUG = config("MAIL_DEBUG", default=DEBUG, cast=bool) MAIL_SERVER = config("MAIL_SERVER", default="sendria.local") MAIL_PORT = config("MAIL_PORT", default=62000, cast=int) MAIL_USERNAME = config("MAIL_USERNAME", default="") MAIL_PASSWORD = config("MAIL_PASSWORD", default="") MAIL_USE_SSL = config("MAIL_USE_SSL", default=False, cast=bool) MAIL_USE_TLS = config("MAIL_USE_TLS", default=False, cast=bool)
import dash import dash_core_components as dcc import dash_html_components as html import dash_table_experiments as dte import decouple as dc import os import pandas as pd import plotly.graph_objs as go ## GLOBAL DEFINITIONS # Sets Cassandra database parameters path_home = os.getcwd() os.chdir(r"../../util/settings") settings = dc.Config(dc.RepositoryEnv(".env")) os.chdir(path_home) p_cassandra = settings.get("CASSANDRA_MASTER", cast=dc.Csv()) db_session = Cluster(p_cassandra).connect() # Sets Table and dropdown options all_groups = ["W", "X", "Y", "Z"] all_cycles = [str(x) for x in range(1000)] table_order = ["id", "group", "cycle", "energy", "percent deviation"] # Sets Dash application parameters app = dash.Dash("Charge_Tracker", external_stylesheets=[\ "https://codepen.io/chriddyp/pen/bWLwgP.css"]) server = app.server app.layout = html.Div([ html.Div( [