Files
ppit/utils.py

52 lines
1.3 KiB
Python

import os
import sys
import sqlite3
import logging
def get_file_path(file_name):
return os.path.join(os.path.dirname(os.path.realpath(__file__)), file_name)
def get_pits_path():
return os.path.basename(sys.argv[0])
def strstr(haystack, needle):
"""
Return a sub-string of haystack, going from first occurence of needle until the end.
Return None if not found.
Arguments:
haystack {string} -- the entry string
needle {string} -- the part of string searched
Returns:
string -- sub-string of haystack, starting from needle. None if not exist.
Examples:
email = 'name@example.com'
domain = strstr(email, '@');
print(domain) // Display : @example.com
"""
pos = haystack.find(needle)
if pos < 0: # not found
return None
return haystack[pos:]
def create_connection(db_filename):
db_path = get_file_path(db_filename)
logging.debug("Try to connect to {}".format(db_path))
try:
conn = sqlite3.connect(db_path)
return conn
except sqlite3.Error as sqlite3_error:
print(sqlite3_error)
return None
def count_object(conn, table_name):
query = "SELECT count(*) FROM " + table_name
cursor = conn.cursor()
cursor.execute(query)
return cursor.fetchone()[0]