Skip to content

Miscellaneous

Mika Hämäläinen edited this page Aug 8, 2020 · 6 revisions

file_exists(file, accept_directories=False)

A simple function for checking whether a file exists, use accept_directories=True to accept folders too.

from mikatools import *
file_exists("/home/mikahama/file.txt")
>> True

script_path(join_file=None)

Returns the path where the Python script calling this method is located. Use join_file="file.txt" to append a file to this path.

from mikatools import *
script_path()
>> /home/mikahama/mikatools
script_path("subdir/file.txt")
>> /home/mikahama/mikatools/subdir/file.txt

download_json(url, args={})

Downloads a JSON from a URL. args are passed to requests.get()

from mikatools import *
j = download_json("http://example.com/json.json")
print(j)
>> {"message":"hello"}

download_file(url, path, show_progress=False)

Downloads a file and saves it to a path. Use show_progress=True to show a progress bar.

from mikatools import *
download_file("http://example.com/data.bin", "data.bin")

print_json_help(json_dictionary, indent=0, indent_char=" ")

Prints a JSON formatted help file

from mikatools import *
print_json_help({"key1":"value", "key2":[2,4], "key3":{"key":"value"}},indent=4, indent_char=" ")
>> - key1:
>>    value
>>    - key2:
>>        2, 4
>>    - key3:
>>        - key:
>>            value