Skip to main content

This repository contains a set of libraries that i use in other projects

Project description

python3-utils

This repository contains a set of libraries that I use in other projects

Installation

Installation can be done through the pip3 command:

pip3 install procamora-utils --user

You can also update the library with:

python3 -m pip install --user --upgrade procamora-utils

Libraries

  • logger
  • interface_sqlite
  • ip
  • ping

logger

Library to run the logging library with colors according to the type of log.

Basic Usage

To use this class the first thing to do is import the library:

import logging
from procamora_utils.logger import get_logging

logger: logging = get_logging(verbose=False, name='test')

logger.debug('hi')
logger.info('hi')
logger.warning('hi')
logger.error('hi')
logger.critical('hi')

interface_sqlite

This library provides an easy way to manage a sqlite database. To do this, use the sqlite3 library to connect to the database and the logging library to display information about errors and debugging.

Basic Usage

To use this class the first thing to do is import the library:

from procamora_utils.logger import get_logging
from procamora_utils.interface_sqlite import *

The interface_sqlite file when doing an import * we are importing three functions, these are:

__all__ = ['conection_sqlite', 'execute_script_sqlite', 'dump_database']

conection_sqlite

This function is responsible for carrying out the main SQL operations, such as: SELECT, INSERT, UPDATE or DELETE.

An example of some of these functions would be:

from pathlib import Path
from typing import List, Dict, Text, Any
from procamora_utils.interface_sqlite import *

db: Path = Path('database.db')
def select_all_hosts() -> List[Dict[Text, Any]]:
    query: Text = "SELECT * FROM Hosts"
    response_query: List[Dict[Text, Any]] = conection_sqlite(db, query, is_dict=True)
    return response_query

def update_host_offline(date: Text):
    query: Text = f"UPDATE Hosts SET active=0 WHERE date <> '{date}';"
    conection_sqlite(db, query)
Parameterized query

This library allows the Parameterized of sql queries, here is an example of how to use it

from pathlib import Path
from typing import List, Dict, Text, Any, Tuple
from procamora_utils.interface_sqlite import *
db: Path = Path('database.db')

query: Text = "SELECT * FROM table1 WHERE value=?"
params: Tuple = ('Python',)
response_query: List[Dict[Text, Any]] = conection_sqlite(db, query, query_params=params, is_dict=True)

execute_script_sqlite

This function allows you to run a script or dump that you receive in string format. With this function, databases could be created.

from pathlib import Path
from typing import Text
from procamora_utils.interface_sqlite import *

db: Path = Path('database.db')
dump: Text = '''INSERT INTO() VALUES();
INSERT INTO() VALUES();'''
execute_script_sqlite(db, dump)

dump_database

This function allows you to perform a complete dump of the database.

from pathlib import Path
from procamora_utils.interface_sqlite import *

db: Path = Path('database.db')
response = dump_database(db)

ip

This library provides a high-level abstraction for storing an IP address or FQDN. In case of entering a wrong value it will raise an exception.

Basic Usage

import logging
from typing import Text

from procamora_utils.ip import IP
from procamora_utils.logger import get_logging
logger: logging = get_logging(False, 'ip')


fqdn: Text = 'google.es'
ip: IP = IP(fqdn=fqdn)
logger.info(ip.get_addr())

ipv4: Text = '192.168.1.1'
ip: IP = IP(ip=ipv4)
logger.info(ip.get_addr())

ping

This library provides an easy way to ping using the ping command provided by the operating system. The reason for using the operating system command is so that you do not need to be root to send an ICMP packet.

Basic Usage

To use this class the first thing to do is import the library:

import logging
from typing import List
from procamora_utils.ip import IP
from procamora_utils.ping import ping
from procamora_utils.logger import get_logging
logger: logging = get_logging(False, 'ping')

ips: List[IP] = [IP(ip="127.0.0.1"), IP(ip="192.168.0.103"), IP(fqdn="google.es"), IP(fqdn="google.false")]
for ip in ips:
    texto: bool = ping(ip)
    if texto:
        logger.info("{} up".format(ip.get_addr()))
    else:
        logger.info("{} down".format(ip.get_addr()))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

procamora-utils-1.5.tar.gz (20.6 kB view details)

Uploaded Source

File details

Details for the file procamora-utils-1.5.tar.gz.

File metadata

  • Download URL: procamora-utils-1.5.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for procamora-utils-1.5.tar.gz
Algorithm Hash digest
SHA256 09d0e9a03ecd4266d27300b9e2e6347124408642d288b678c7f07b10373e1e4d
MD5 1eb15de831ef29691b7b22ba33099167
BLAKE2b-256 37385ee42d3a1d60efb99958ffb589a8c8d212ecfa67f64406cbf57b02a0c70f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page