Skip to main content

HaoTian's Python Util

Project description

htutil

HaoTian's Python Util

version downloads format implementation pyversions license

Install

pip install htutil

Usage

file

from htutil import file

Refer to C# System.IO.File API, very simple to use.

    s = 'hello'
    write_all_text('1.txt', s)
    # hello in 1.txt
    append_all_text('1.txt', 'world')
    # helloworld in 1.txt
    s = read_all_text('1.txt')
    print(s)  # helloworld

    s = ['hello', 'world']
    write_all_lines('1.txt', s)
    # hello\nworld in 1.txt
    append_all_lines('1.txt',['\npython'])
    # hello\nworld\npython in 1.txt
    s = read_all_lines('1.txt')
    print(s)  # ['hello', 'world', 'python']

Log

from htutil import log
from htutil.log import p

A powerful logger, support var name output.

a = 3
p(a)
p(a-1)

b = bob()
p(b.a)

The output is

a = 3;2020-12-19 16:43:49;/Users/t117503445/Project/htutil/htutil/log.py:55
a-1 = 2;2020-12-19 16:43:49;/Users/t117503445/Project/htutil/htutil/log.py:56
b.a = 3;2020-12-19 16:43:49;/Users/t117503445/Project/htutil/htutil/log.py:58

You could change the output format.

config(format = '${var_name} = ${value} ### ${time} ### ${file_name}:${line_number}')
p(a)

then the output is

a = 3 ### 2020-12-19 16:46:56 ### /Users/t117503445/Project/htutil/htutil/log.py:60

The default format is ${var_name} = ${value};${time};${file_name}:${line_number}

If you want to save the log output to file, you could use callback.

def callback_example(string: str):
    file.append_all_text('1.log',string)

register_p_callback(callback_example)
a = 3
p(a)

lambda is also a good choice.

register_p_callback(lambda string:file.append_all_text('1.log',string))
a = 3
p(a)

cache

cache def result by pickle file.

from htutil import cache
@cache.file_cache
def get_1():
    time.sleep(3)
    return 1

counter

a simple counter based on dict.

    c = Counter()
    c.add('1')
    c.add('1')
    c.add('2')

    print(c.to_dict()) # {'1': 2, '2': 1}

    c.dump() # same as print(c.to_dict())

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

htutil-0.1.7.tar.gz (16.3 kB view hashes)

Uploaded Source

Built Distribution

htutil-0.1.7-py3-none-any.whl (17.6 kB view hashes)

Uploaded Python 3

Supported by

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