Skip to content

Commit 428cbcb

Browse files
committed
Added functions from domdfcoding/Python_Modules
1 parent 51e593a commit 428cbcb

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

domdf_python_tools/paths.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,66 @@ def relpath(path, relative_to=None):
113113
return os.path.relpath(os.path.abspath(path))
114114
else:
115115
return os.path.abspath(path)
116+
117+
118+
119+
def delete(filename):
120+
"""
121+
Delete the file in the current directory
122+
123+
# TODO: make this the file in the given directory, by default the current directory
124+
125+
:param filename:
126+
127+
:return:
128+
"""
129+
130+
os.remove(os.path.join(os.getcwd(), filename))
131+
132+
133+
def write(var, filename):
134+
"""
135+
Write a variable to file in the current directory
136+
137+
# TODO: make this the file in the given directory, by default the current directory
138+
139+
:param var:
140+
:param filename:
141+
142+
:return:
143+
"""
144+
145+
with open(os.path.join(os.getcwd(), filename), 'w') as f:
146+
f.write(var)
147+
148+
149+
def read(filename):
150+
"""
151+
Read a file in the current directory; Untested
152+
153+
# TODO: make this the file in the given directory, by default the current directory
154+
155+
:param filename:
156+
157+
:return:
158+
"""
159+
160+
with open(os.path.join(os.getcwd(), filename)) as f:
161+
return f.read()
162+
163+
164+
def append(var, filename):
165+
"""
166+
Append `var` to the file `filename` in the current directory; Untested
167+
168+
# TODO: make this the file in the given directory, by default the current directory
169+
170+
:param var:
171+
:param filename:
172+
173+
:return:
174+
"""
175+
176+
with open(os.path.join(os.getcwd(), filename), 'a') as f:
177+
f.write(var)
178+

domdf_python_tools/terminal.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#
2929

3030
import os
31+
import sys
3132
import shlex
3233
import struct
3334
import platform
@@ -72,6 +73,46 @@ def entry(text_to_print):
7273
elif pyversion == 2:
7374
return raw_input(text_to_print)
7475

76+
77+
def interrupt():
78+
"""
79+
Print what to do to abort the script; dynamic depending on OS
80+
Useful when you have a long-running script that you might want t interrupt part way through
81+
"""
82+
83+
print('(Press Ctrl-%s to quit at any time.)' % 'C' if os.name == 'nt' else 'D')
84+
85+
86+
def overtype(*objects, sep=' ', end='', file=sys.stdout, flush=False):
87+
"""
88+
Print `objects` to the text stream `file`, starting with "\r", separated by `sep` and followed by `end`.
89+
`sep`, `end`, `file` and `flush`, if present, must be given as keyword arguments
90+
91+
All non-keyword arguments are converted to strings like ``str()`` does and written to the stream,
92+
separated by `sep` and followed by `end`.
93+
94+
If no objects are given, ``overwrite()` will just write "\r".
95+
96+
Based on the Python print() docs: https://docs.python.org/3/library/functions.html#print
97+
98+
# This does not currently work in the PyCharm console, at least on Windows
99+
100+
:param objects:
101+
:param sep: String to separate the objects with, by default " "
102+
:type sep: str
103+
:param end: String to end with, by default nothing
104+
:type end: str
105+
:param file: An object with a ``write(string)`` method; default ``sys.stdout``
106+
:param flush: If true, the stream is forcibly flushed.
107+
:type flush: bool
108+
:return:
109+
"""
110+
111+
object0 = f"\r{objects[0]}"
112+
objects = (object0, *objects[1:])
113+
print(*objects, sep=sep, end=end, file=file, flush=flush)
114+
115+
75116
def get_terminal_size():
76117
"""
77118
Get width and height of console

domdf_python_tools/utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,18 @@ def list2str(the_list, sep=","):
139139
:rtype: str
140140
"""
141141

142-
return sep.join([str(x) for x in the_list])
142+
return sep.join([str(x) for x in the_list])
143+
144+
145+
def splitLen(string,n):
146+
"""
147+
Split a string every x characters
148+
149+
:param string:
150+
:param n:
151+
152+
:return:
153+
"""
154+
155+
return [string[i:i+n] for i in range(0,len(string),n)]
156+

0 commit comments

Comments
 (0)