fagfunksjoner.prodsone package¶
Submodules¶
fagfunksjoner.prodsone.check_env module¶
To write “environment-aware code”, we need to know where we are.
This module extracts information from the current environment, and can help differentiate between the different places we develop code.
- check_env(raise_err=True)¶
Check if you are on Dapla or in prodsone.
- Parameters:
raise_err (bool) – Set to False if you don’t want the code to raise an error on an unrecognized environment.
- Returns:
“DAPLA” if on Dapla, “PROD” if in prodsone, otherwise “UNKNOWN”.
- Return type:
str
- Raises:
OSError – If no environment indications match (Dapla or Prod), and raise_err is set to True.
- linux_shortcuts(insert_environ=False)¶
Manually load the “linux-forkortelser” in as dict.
If the function can find the file they are shared in.
- Parameters:
insert_environ (
bool
) – Set to True if you want the dict to be inserted into the environment variables (os.environ).- Returns:
The “linux-forkortelser” as a dict
- Return type:
dict[str, str]
- Raises:
ValueError – If the stamme_variabel file is wrongly formatted.
fagfunksjoner.prodsone.dynarev module¶
- dynarev_uttrekk(delreg_nr, skjema, dublettsjekk=False, sfu_cols=None)¶
Fetches and processes data from the Oracle database using the Oracle class for connection management.
- Parameters:
delreg_nr (
str
) – Delregisternummer.skjema (
str
) – Skjemanavn.dublettsjekk (
bool
) – If True, checks for and returns duplicates.sfu_cols (
str
|list
[str
] |bool
|None
) – Specify a list of columns for SFU data, or a single column as a string. If True picks all columns. If None, skips getting sfu-data.
- Returns:
A dataframe, or tuple of dataframes if you wanted sfu-data / dupe-check.
- Return type:
pd.DataFrame | tuple[pd.DataFrame]
- Raises:
ValueError – If the sfu_cols parameter does not fit expectations.
fagfunksjoner.prodsone.oradb module¶
- class Oracle(db, pw=None)¶
Bases:
object
Class for working with an Oracle database with most common queries.
This class supports the most used SQL queries to a table in a database. It gives us the possibilities to query multiple times since the class has the credentials needed stored until the user closes the connection.
Note
User must remember to call the close method after final use.
- Parameters:
db (str)
pw (str | None)
- user¶
user id
- Type:
str
- db¶
database name
- Type:
str
- pw¶
user password
- Type:
str
- conn¶
database connection if using context manager
- Type:
oracledb.Connection
- cur¶
database cursor if using context manager
- Type:
oracledb.Cursor
- close()¶
Close connection and delete the class attribute values.
- Return type:
None
- select(sql)¶
Get data from Oracle database with fetchall method.
Method for normal select SQL query. It will do a fetchall procedure. If it is too much data, then use the select_many method.
- Parameters:
sql (
str
) – the SQL query statement- Returns:
A list of dictionaries of every record, column names as keys.
- Return type:
list[dict[str, Any]]
- Raises:
error – If the connection returns an error.
- select_many(sql, batchsize)¶
Get data from Oracle database in batches with fetchmany method.
Method for normal select SQL query. It will do a fetchmany procedure, which is preferred when there is a lot of data that needs to be fetched.
- Parameters:
sql (
str
) – the SQL query statement.batchsize (
int
) – the size of rows per batch.
- Returns:
A list of dictionaries of every record, column names as keys.
- Return type:
list[dict[str, Any]]
- Raises:
error – If the connection returns an error.
- update_or_insert(sql, update)¶
Update data or insert new data to Oracle database.
Method to do either update or insert SQL query. It is important that the SQL query statement and the data column names and values come in correct order to each other.
- Parameters:
sql (
str
) – SQL query statement, insert or update.update (
list
[tuple
[Any
,...
]]) – list of record values to insert or update.
- Raises:
error – If the connection returns an error.
- Return type:
None
fagfunksjoner.prodsone.saspy_ssb module¶
Simplifications of saspy package for SSB use.
Helps you store password in prodsone. Sets libnames automatically for you when just wanting to open a file, or convert it.
- cp(from_path, to_path)¶
Use saspy and sas-server to copy files.
- Parameters:
from_path (
str
) – The path for the source file to copyto_path (
str
) – The path to place the copy on
- Returns:
A key for if it succeded, and a key for holding the log as string.
- Return type:
dict[str, Any]
- df_from_sasfile(path)¶
Return a pandas dataframe from the path to a sasfile, using saspy.
Creates saspy-session, create a libref, gets the dataframe, terminates the connection to saspy cleanly, and returns the dataframe.
- Parameters:
path (
str
) – The full path to the sasfile you want to open with sas.- Returns:
The raw content of the sasfile straight from saspy
- Return type:
pandas.DataFrame
- df_to_sasfile(df, outpath)¶
Store a pandas dataframe as a sas7bdat on disk.
- Parameters:
df (
DataFrame
) – The dataframe to store.outpath (
str
) – The path to store the dataset on.
- Returns:
The outpath you sent in, maybe you can use it…
- Return type:
str
- sasfile_to_parquet(path_str, out_path_str='', gzip=False)¶
Convert a sasfile directly to a parquetfile, using saspy and pandas.
- Parameters:
path_str (
str
) – The path to the in-sas-file.out_path_str (
str
) – The path to place the parquet-file ongzip (
bool
) – If you want the parquetfile gzipped or not.
- Returns:
- In case you want to use the content for something else.
I mean, we already read it into memory…
- Return type:
pandas.DataFrame
- saspy_df_from_path(path)¶
Use df_from_sasfile instead, this is the old (bad) name for the function.
- Parameters:
path (
str
) – The full path to the sasfile you want to open with sas.- Returns:
The raw content of the sasfile straight from saspy
- Return type:
pandas.DataFrame
- saspy_session()¶
Get an initialized saspy.SASsession object.
Use the default config, getting your password if you’ve set one.
- Returns:
An initialized saspy-session
- Return type:
saspy.SASsession
- set_libref(path, sas, librefname='sasdata')¶
Create a libref, return the librefname and filename, which is what sas uses to refer to a sasfile.
- Parameters:
path (
str
) – The full path to the sasfile.sas (
SASsession
) – An initialized saspy-session.librefname (
str
) – The name to use for the libref. Defaults to “sasdata”.
- Returns:
the librefname and the filename
- Return type:
tuple[str, str]
- set_password(encrypted_password)¶
Pass into this function, an encrypted version of your password.
Get the encrypted password in SAS EG, running the following code (swap MY PASSWORD for your actual common-password):
proc pwencode in='MY PASSWORD' method=sas004; run;
In the log-window in SAS EG you should then recieve an encrypted version of your password, that looks something like this {SAS004}C598BA0A77F74607464634566CCD0D7BB8EBDEEA4B73C440 Send this as the parameter into this function.
- Parameters:
encrypted_password (
str
) – Your password encrypted using SAS EG- Return type:
None
- split_path_for_sas(path)¶
Split a path in three parts, mainly for having a name for the libname.
- Parameters:
path (
Path
) – The full path to be split- Returns:
The three parts the path has been split into.
- Return type:
tuple[str]
- swap_server(new_server)¶
Swap between the sas-servers you connect to with saspy.
- Parameters:
new_server (
int
) – The server number to switch to.- Return type:
None
Module contents¶
This module contains code to be run in SSBs “prodsone”.