Skip to main content

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

Metadata

Release files for rkpython 0.0.25

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rkpython 0.0.25
File Size Uploaded Provenance Actions
rkpython-0.0.25.tar.gz 6.9 kB Without provenance Details for rkpython-0.0.25.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for rkpython 0.0.25
File Interpreter ABI Platform Actions
rkpython-0.0.25-py3-none-any.whl Python 3 none any Details for rkpython-0.0.25-py3-none-any.whl

Total release size: 14.2 kB

Release files / rkpython-0.0.25.tar.gz

Download URL rkpython-0.0.25.tar.gz
Size 6.9 kB
Tags Source
SHA-256 checksum
How to use checksums
63be3abc9c3b226baa310c13c147462a75e103f5e2dc91728d65e3621b92eea4
BLAKE2b-256 checksum
How to use checksums
37942ed1cc3c97b4fb24a2762d5d3315d49fee754289ca7b67f12fe5b9ffa7ec
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / rkpython-0.0.25-py3-none-any.whl

Download URL rkpython-0.0.25-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ecb937c0a3e3e90a98c6e05d73b1daac13bca9e649df9ef01b55a96724c4a49
BLAKE2b-256 checksum
How to use checksums
d117e5f7439f9139c6fad7bf869d5de4e6ebb299dd9c775f40d966d2b09ab82e
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for rkpython 0.0.25

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page