Skip to main content

Python module containing various functions

Project description

rkpython

Description

Module containing some general purpose functions.

Installation

Use

pip install rkpython

Then import it in Python with

import rkpython as rk

Functions

rk.to_txt(l, path = 'text_file.txt', overwrite = False, verbose = False)

Saves a list to a .txt file.

--------------

l : list, default is None
    List object to be saved.  

path : str, default is 'text_file.txt'
    File path (with file name) you want to save to.  

overwrite : bool, default is False  
    Overwrites the file if it exists.

verbose : boolean, default is False
    Prints out a message if the operation was succesful

--------------

Examples :

>>> rk.to_txt(var_list, './documents/vars.txt', verbose = True)
File successfully written to ./documents/vars.txt

rk.read_txt(path = 'text_file.txt', verbose = False)

Reads from a text file, saving the result as a list, where each line is one item.

--------------

path : str, default = 'text_file.txt'
    File path (with file name) you want to read from. Can be any type of file (.txt, .csv...)  

verbose : boolean, default is False
    Prints out a message if the operation was succesful

--------------

Examples :

>>> var_list = rk.read_txt('./documents/vars.txt', verbose = True)
File successfully read from ./documents/vars.txt

rk.h_size(size)

Converts a size in bytes to a humanly readable size, with 1 decimal number. Input an integer, returns a string.

--------------

size : float, int
    Size of the object you want to convert, in bytes.

--------------

Examples :

>>>  h_size(67108864)
'64.0 Mb'

rk.get_mem(nb_objects = 10)

Warning : this function can't access the global variables so doesn't currently work. You can however copy the function from the source code and define it in your python session so that it will access the right variables.

Prints out a list of the largest objects stored in memory, as well as the total memory usage of global variables. Returns a string.

--------------

nb_objects : int, default =  10
    Maximum number of items to be printed out.

--------------

Examples :

>>> get_mem(5)
Total usage : 25.3 Gb

 5 largest objects :
 _477  :  1.7 Gb
 _529  :  1.7 Gb
 _437  :  1.4 Gb
 _412  :  1.3 Gb
 _415  :  1.3 Gb

rk.csv_info(file)

Returns information about a csv or text file, such as the encoding and separators infered using csv's Sniffer() function.

--------------

file : str
    Path to the file you want to read.

--------------

csv_info().size :
    Returns the size of the file as a string.

csv_info().separator :
    Returns the infered separator as a string.

csv_info().quotechar :
    Returns the infered quote character as a string, defaults to ["].

csv_info().encoding :
    Returns the infered encoding using chardet. Defaults to ascii.

csv_info().rawdata :
    Returns a 8192 byte sample of the file, unencoded.

csv_info().rows :
    Returns the number of rows in the csv file.        

csv_info().columns :
    Returns the columns of the csv file.

csv_info().parameters :
    Returns the separator, quotechar and encoding of the file to plug them in pandas or dask.        

csv_info().info() :
    Prints out the main characteristics of the file.

--------------

Examples :

>>> csv_info("table.csv").encoding
'utf-8'

>>> sep, quotechar, encoding = csv_info("table.csv").parameters
>>> df = pandas.read_csv("table.csv", sep=sep, quotechar=quotechar, encoding=encoding)
>>> print(sep, quotechar, encoding)
; " utf-8

rk.sql_dict(con, db_filter = '.*', table_filter = '.*', col_filter = '.*', strict=False)

Creates a dictionary listing all databases, tables and columns of a MySql server. Results can be filtered using regex.

--------------

con : SQLAlchemy connectable (engine/connection) or database string URI or DBAPI2 connection (fallback mode)

db_filter : str or list of str, default is '.*'
    Filters on databases containing the specified regular expression(s).

table_filter : str or list of str, default is '.*'
    Filters on tables containing the specified regular expression(s).

column_filter : str or list of str, default is '.*'
    Filters on columns containing the specified regular expression(s).

strict : boolean, default is False
    Returns only strict matches instead of partial matches.

--------------

Examples :

>>> temp_dict = sql_dict(con = f'mysql://{user}:{pw}@{host}:{port}', col_filter = 'price')
>>> temp_dict.keys()
dict_keys(['products'])
>>> temp_dict['products'].keys()
dict_keys(['phones', 'laptops'])
>>> temp_dict['products']['phones']
['price_without_tax',
 'price_with_tax']

rk.humantime(ms)

Converts timespan in milliseconds to a humanly readable string.

--------------

ms : int or float
    Time in milliseconds

--------------

Examples :

>>> humantime(194159)
'3 min, 14 sec, 159 ms'

rk.timer

Class for measuring elapsed time.
Contains the rk.timer.start() function to save the current time, and the rk.timer.end() to print out the elapsed time.

--------------

Example :
>>> rk.timer.start()
>>> time.sleep(2)
>>> rk.timer.end("Sleep time")
[Sleep time] : 2 sec, 2 ms

rk.timer.start() & rk.timer.end(step = "Time")

rk.timer.start() saves the current time as a global variable, then prints out the time elapsed with rk.timer.end()

--------------

step : str or int, default "Time"

--------------

Example :
>>> rk.timer.start()
>>> time.sleep(2)
>>> rk.timer.end("Sleep time")
[Sleep time] : 2 sec, 2 ms

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

rkpython-0.0.25.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

rkpython-0.0.25-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file rkpython-0.0.25.tar.gz.

File metadata

  • Download URL: rkpython-0.0.25.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.4

File hashes

Hashes for rkpython-0.0.25.tar.gz
Algorithm Hash digest
SHA256 63be3abc9c3b226baa310c13c147462a75e103f5e2dc91728d65e3621b92eea4
MD5 369c76c85342cd6b7392a40b0ca557cd
BLAKE2b-256 37942ed1cc3c97b4fb24a2762d5d3315d49fee754289ca7b67f12fe5b9ffa7ec

See more details on using hashes here.

File details

Details for the file rkpython-0.0.25-py3-none-any.whl.

File metadata

  • Download URL: rkpython-0.0.25-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/39.2.0 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.6.4

File hashes

Hashes for rkpython-0.0.25-py3-none-any.whl
Algorithm Hash digest
SHA256 1ecb937c0a3e3e90a98c6e05d73b1daac13bca9e649df9ef01b55a96724c4a49
MD5 34c5e10f18c285d2ff8faf4828b37664
BLAKE2b-256 d117e5f7439f9139c6fad7bf869d5de4e6ebb299dd9c775f40d966d2b09ab82e

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