Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ google-auth-httplib2>=0.0.3
google_api_python_client>=1.6.7
google_auth_oauthlib>=0.2.0
google-cloud-bigquery>=1.24.0
googleads>=42.0.0
googleads>=49.0.0
isort==4.3.9
lxml>=4.6.5
mysql-connector-python==9.1.0
numpy>=1.16.2
pandas>=0.24.0
pandas>=2.0.0
pyarrow>=0.11.1
qds_sdk>=1.10.1
requests>=2.20
Expand Down
5 changes: 3 additions & 2 deletions sroka/api/athena/athena_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
from botocore.exceptions import ClientError, EndpointConnectionError

import sroka.config.config as config
from sroka.api.athena.athena_api_helpers import poll_status, download_file, return_on_exception, \
input_check
from sroka.api.athena.athena_api_helpers import (download_file, input_check,
poll_status,
return_on_exception)


def query_athena(query, filename=None):
Expand Down
2 changes: 1 addition & 1 deletion sroka/api/athena/athena_api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def input_check(input_to_check, expected_types):
for expected_type in expected_types:
if type(input_to_check) == expected_type:
if isinstance(input_to_check, expected_type):
if expected_type == str and len(input_to_check) == 0:
print('Function input must be a nonempty string.')
return False
Expand Down
21 changes: 10 additions & 11 deletions sroka/api/google_ad_manager/gam_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,44 +22,44 @@

def dict_type_checker(dict_argument, argument_name, mandatory=True):
if mandatory:
if type(dict_argument) != dict:
if not isinstance(dict_argument, dict):
print(f"""{argument_name} needs to be a dict""")
return "Incorrect type"
else:
if dict_argument and type(dict_argument) != dict:
if dict_argument and not isinstance(dict_argument, dict):
print(f"""{argument_name} needs to be a dict""")
return "Incorrect type"


def int_type_checker(int_argument, argument_name, mandatory=True):
if mandatory:
if type(int_argument) != int:
if not isinstance(int_argument, int):
print(f"""{argument_name} needs to be an integer""")
return "Incorrect type"
else:
if int_argument and type(int_argument) != int:
if int_argument and not isinstance(int_argument, int):
print(f"""{argument_name} needs to be an integer""")
return "Incorrect type"


def list_type_checker(list_argument, argument_name, mandatory=True):
if mandatory:
if type(list_argument) != list:
if not isinstance(list_argument, list):
print(f"""{argument_name} needs to be a list""")
return "Incorrect type"
else:
if list_argument and type(list_argument) != list:
if list_argument and not isinstance(list_argument, list):
print(f"""{argument_name} needs to be a list""")
return "Incorrect type"


def str_type_checker(str_argument, argument_name, mandatory=True):
if mandatory:
if type(str_argument) != str:
if not isinstance(str_argument, str):
print(f"""{argument_name} needs to be a string""")
return "Incorrect type"
else:
if str_argument and type(str_argument) != str:
if str_argument and not isinstance(str_argument, str):
print(f"""{argument_name} needs to be a string""")
return "Incorrect type"

Expand Down Expand Up @@ -194,7 +194,7 @@ def get_users_from_admanager(query, dimensions, network_code=None):
print('Failed to generate user list. Incorrect dimension: {}'.format(e))
return

user_df = user_df.append(dimensions_df, sort=False)
user_df = pd.concat([user_df, dimensions_df], sort=False)
statement.offset += statement.limit
else:
break
Expand Down Expand Up @@ -250,7 +250,7 @@ def get_companies_from_admanager(query, dimensions, network_code=None):
print('Failed to generate company list. Incorrect dimension: {}'.format(e))
return

company_df = company_df.append(dimensions_df, sort=False)
company_df = pd.concat([company_df, dimensions_df], sort=False)
statement.offset += statement.limit
else:
break
Expand Down Expand Up @@ -404,7 +404,6 @@ def get_service_data_from_admanager(
print("No more items found.")
break


print(
f"Successfully fetched a total of {len(all_items)} '{service}' items.\n"
)
Expand Down
16 changes: 8 additions & 8 deletions sroka/api/google_bigquery/bigquery_api.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import pandas as pd
from google.api_core.exceptions import BadRequest, Forbidden, NotFound
from google.cloud import bigquery
import sroka.config.config as config
from google.api_core.exceptions import Forbidden, NotFound, BadRequest

import sroka.config.config as config

KEY_FILE = config.get_file_path('google_bigquery')


def query_bigquery(input_query, filename=None):

if filename:
if type(filename) != str:
if not isinstance(filename, str):
print('filename needs to be a string')
return None

if type(input_query) != str:
if not isinstance(input_query, str):
print('input_query needs to be a string')
return None

Expand All @@ -26,7 +26,7 @@ def query_bigquery(input_query, filename=None):
return None

else:
if type(input_query) != str:
if not isinstance(input_query, str):
print('input_query needs to be a string')
return pd.DataFrame([])

Expand Down Expand Up @@ -55,11 +55,11 @@ def query_bigquery(input_query, filename=None):
def done_bigquery(job_id, filename=None):

if filename:
if type(filename) != str:
if not isinstance(filename, str):
print('filename needs to be a string')
return None

if type(job_id) != str:
if not isinstance(job_id, str):
print('input_query needs to be a string')
return None

Expand All @@ -71,7 +71,7 @@ def done_bigquery(job_id, filename=None):
return None

else:
if type(job_id) != str:
if not isinstance(job_id, str):
print('input_query needs to be a string')
return pd.DataFrame([])

Expand Down
19 changes: 10 additions & 9 deletions sroka/api/google_drive/google_drive_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import numpy as np
import pandas as pd

from googleapiclient.errors import HttpError
from sroka.api.google_drive.google_drive_helpers import is_valid_email, service_builder

from sroka.api.google_drive.google_drive_helpers import (is_valid_email,
service_builder)


def google_drive_sheets_read(sheetname_id: str, sheet_range: str, first_row_columns=False):
Expand Down Expand Up @@ -231,7 +232,7 @@ def google_drive_sheets_delete_tab(spreadsheet_id: str, tab_name: str):

Args:
spreadsheet_id (str): The ID of the spreadsheet.
tab_name (str): The name of the tab to delete.
tab_name (str): The name of the tab to delete.
Returns:
str: The ID of the spreadsheet.
"""
Expand Down Expand Up @@ -274,10 +275,10 @@ def google_drive_sheets_delete_tab(spreadsheet_id: str, tab_name: str):
def google_drive_get_file_parents(file_id: str):
"""
Retrieves the parent folder IDs for a specified file on Google Drive.

Args:
file_id (str): The ID of the file whose parent folders are to be retrieved.

Returns:
list: A list of string IDs for the parent folder(s) of the file. Returns an empty list on failure.
"""
Expand Down Expand Up @@ -330,7 +331,7 @@ def google_drive_transfer_ownership(file_id: str, new_owner_email: str):

# Create the permission, triggering the ownership transfer
# pylint: disable=E1101
permission = service.permissions().create(
service.permissions().create(
fileId=file_id,
body=permission_body,
transferOwnership=True,
Expand Down Expand Up @@ -373,7 +374,7 @@ def google_drive_change_file_permission(file_id: str, user_email: str, role: str
except ValueError as er:
print(f"An incorrect role has been used in the function - {er}")
return False

try:
if is_valid_email(user_email) is False:
raise ValueError(f'The {user_email} is incorrect.')
Expand All @@ -390,10 +391,10 @@ def google_drive_change_file_permission(file_id: str, user_email: str, role: str
'role': role.lower(),
'emailAddress': user_email.lower()
}

# Insert the new permission
# pylint: disable=E1101
permission = service.permissions().create(
service.permissions().create(
fileId=file_id,
body=permission_body,
fields='id'
Expand Down
9 changes: 6 additions & 3 deletions sroka/api/google_drive/google_drive_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import re

from googleapiclient.discovery import build

import sroka.config.config as config


Expand All @@ -18,6 +20,7 @@ def is_valid_email(email_string: str):
return True
return False


def service_builder(service_type: int, version: str):
"""
Creates a service with proper credentials for a selected build from the service_type variable: 'sheets' or 'drive'
Expand All @@ -38,15 +41,15 @@ def service_builder(service_type: int, version: str):
try:
valid_service_type = [1, 2]
if service_type not in valid_service_type:
raise ValueError('Available services: [1] sheets, [2] drive')
raise ValueError('Available services: [1] sheets, [2] drive')
except ValueError as e:
print(f"An incorrect role has been used in the function - {e}")
return False

try:
valid_version = ['v2', 'v3', 'v4']
if version.lower() not in valid_version:
raise ValueError('Available versions: v2, v3, v4')
raise ValueError('Available versions: v2, v3, v4')
except ValueError as er:
print(f"An incorrect version has been used in the function - {er}")
return False
Expand Down
2 changes: 1 addition & 1 deletion sroka/api/moat/moat_api_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def validate_input_dict(moat_dict):
print('{} is not defined'.format(column))
return False

if type(moat_dict['columns']) != list or moat_dict['columns'] == []:
if not isinstance(moat_dict['columns'], list) or moat_dict['columns'] == []:
print('columns are not defined correctly')
return False

Expand Down
12 changes: 8 additions & 4 deletions sroka/api/mysql/mysql.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import os
import mysql.connector
import pandas as pd
from configparser import NoSectionError
from pathlib import Path
from mysql.connector.errors import DatabaseError, OperationalError, InternalError

import mysql.connector
import pandas as pd
from mysql.connector.errors import (DatabaseError, InternalError,
OperationalError)
from retrying import retry
from sroka.api.mysql.mysql_helpers import validate_options, get_options_from_config

from sroka.api.mysql.mysql_helpers import (get_options_from_config,
validate_options)


@retry(stop_max_attempt_number=1,
Expand Down
3 changes: 2 additions & 1 deletion sroka/api/mysql/mysql_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sroka.config.config as config
from configparser import NoOptionError

import sroka.config.config as config


def get_options_from_config():
# Set the options in a dictionary, in order to pass only the
Expand Down
7 changes: 3 additions & 4 deletions sroka/api/neo4j/neo4j_api.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import pandas as pd
from py2neo import Graph
from py2neo import ClientError
from py2neo import ClientError, Graph

import sroka.config.config as config


def neo4j_query_data(cypher, parameters=None, **kwparameters):

if type(cypher) != str:
if not isinstance(cypher, str):
print('Cypher query needs to be a string')
return pd.DataFrame([])

if len(cypher) == 0:
print('Cypher query cannot be empty')
return pd.DataFrame([])

if parameters and type(parameters) != dict:
if parameters and not isinstance(parameters, dict):
print('Parameters need to be a dictionary')
return pd.DataFrame([])

Expand Down
8 changes: 2 additions & 6 deletions sroka/api/qubole/qubole_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,8 @@

import pandas as pd
from qds_sdk.commands import Command, HiveCommand, PrestoCommand
from qds_sdk.exception import (
ForbiddenAccess,
ResourceInvalid,
ResourceNotFound,
UnauthorizedAccess,
)
from qds_sdk.exception import (ForbiddenAccess, ResourceInvalid,
ResourceNotFound, UnauthorizedAccess)
from qds_sdk.qubole import Qubole

import sroka.config.config as config
Expand Down
12 changes: 6 additions & 6 deletions sroka/api/s3_connection/s3_connection_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
from io import BytesIO, StringIO

import boto3
import pandas as pd
import numpy as np
import pandas as pd
import pyarrow.parquet as pq
from botocore.exceptions import ClientError, ParamValidationError
from pandas.errors import EmptyDataError
Expand Down Expand Up @@ -89,7 +89,7 @@ def s3_download_data(s3_filename, prefix=False, output_file=None, sep=',', skip_

key_prefix = match.group(2)

if type(sep) == str and len(sep) == 1:
if isinstance(sep, str) and len(sep) == 1:

data = _download_data(key_prefix, s3, bucket_name, prefix, sep, skip_empty_files,
first_row_columns)
Expand All @@ -111,15 +111,15 @@ def s3_upload_data(data, bucket, path, sep=','):
aws_secret_access_key=access_key
)

if type(sep) == str and len(sep) == 1:
if isinstance(sep, str) and len(sep) == 1:

csv_buffer = StringIO()

if type(data) == pd.core.frame.DataFrame or type(data) == np.ndarray:
if isinstance(data, pd.core.frame.DataFrame) or isinstance(data, np.ndarray):

if type(data) == pd.core.frame.DataFrame:
if isinstance(data, pd.core.frame.DataFrame):
data.to_csv(csv_buffer, sep=sep)
elif type(data) == np.ndarray:
elif isinstance(data, np.ndarray):
np.savetxt(csv_buffer, data, delimiter=sep, fmt='%s')

s3 = session.resource('s3')
Expand Down