Skip to content

Commit

Permalink
Merge branch 'pk'
Browse files Browse the repository at this point in the history
  • Loading branch information
pbk0 committed Feb 11, 2024
2 parents d03f2dc + a517340 commit 2dd8f38
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 49 deletions.
55 changes: 55 additions & 0 deletions overrides/partials/comments.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{% if page.meta.comments %}
<h2 id="__comments">{{ lang.t("meta.comments") }}</h2>
<!-- Insert generated snippet here -->
<script src="https://giscus.app/client.js"
data-repo="SpikingNeurons/toolcraft"
data-repo-id="MDEwOlJlcG9zaXRvcnk0MDQyMjM4Njc="
data-category="Announcements"
data-category-id="DIC_kwDOGBf3e84B_IOU"
data-mapping="pathname"
data-strict="0"
data-reactions-enabled="1"
data-emit-metadata="0"
data-input-position="bottom"
data-theme="preferred_color_scheme"
data-lang="en"
crossorigin="anonymous"
async>
</script>

<!-- Synchronize Giscus theme with palette -->
<script>
var giscus = document.querySelector("script[src*=giscus]")

// Set palette on initial load
var palette = __md_get("__palette")
if (palette && typeof palette.color === "object") {
var theme = palette.color.scheme === "slate"
? "transparent_dark"
: "light"

// Instruct Giscus to set theme
giscus.setAttribute("data-theme", theme)
}

// Register event handlers after documented loaded
document.addEventListener("DOMContentLoaded", function() {
var ref = document.querySelector("[data-md-component=palette]")
ref.addEventListener("change", function() {
var palette = __md_get("__palette")
if (palette && typeof palette.color === "object") {
var theme = palette.color.scheme === "slate"
? "transparent_dark"
: "light"

// Instruct Giscus to change theme
var frame = document.querySelector(".giscus-frame")
frame.contentWindow.postMessage(
{ giscus: { setConfig: { theme } } },
"https://giscus.app"
)
}
})
})
</script>
{% endif %}
11 changes: 9 additions & 2 deletions toolcraft/error/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ def __init__(self, *, value, to_be_value, msgs: MESSAGES_TYPE):
])


class IsSliceOrListWithin(_CustomException):
class IsIntOrSliceOrListWithin(_CustomException):
def __init__(
self,
*,
value: t.Union[slice, t.List[int]],
value: t.Union[int, slice, t.List[int]],
min_value: int,
max_value: int,
msgs: MESSAGES_TYPE,
Expand Down Expand Up @@ -206,6 +206,13 @@ def __init__(
]
_raise = True
break
elif isinstance(value, int):
if not min_value <= value < max_value:
msgs += [
f"The value {value} is not within range i.e. "
f"between {min_value} and {max_value}",
]
_raise = True
else:
msgs += [
f"Expected a int, slice or list of int instead found "
Expand Down
71 changes: 55 additions & 16 deletions toolcraft/job/__base__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,19 @@ def delete(self):
]
)

def update(self, data: t.Dict[str, t.Any]):
# _LOGGER.info(msg=f"Updating tag {self.path}")
raise e.code.NotYetImplemented(msgs=[f"yet to implement {Tag.update}"])
def update(self, data: t.Dict[str, t.Any], allow_overwrite: bool = False, encoding: str = 'utf-8'):
_LOGGER.info(msg=f"Updating tag {self.path}")
_old_data = {}
if self.path.exists():
_old_data = self.path.read_yaml(encoding=encoding)
for _k in data.keys():
if _k in _old_data.keys():
if not allow_overwrite:
raise e.validation.NotAllowed(
msgs=[f"Cannot overwrite key {_k} in tag ..."]
)
_old_data.update(data)
self.path.write_yaml(_old_data, encoding=encoding)


@dataclasses.dataclass
Expand Down Expand Up @@ -450,23 +460,28 @@ class JobLaunchParameters:
lsf_memory: int = None

def __setattr__(self, key, value):
# test for lsf parameters
if key.startswith("lsf_"):
if not settings.IS_LSF:
raise e.code.CodingError(
msgs=["Do not try to set LSF launch parameters as this is not LSF environment."]
)
# noinspection PyUnresolvedReferences

# check if value is set multiple times
# noinspection PyUnresolvedReferences,DuplicatedCode
_default_value = self.__class__.__dataclass_fields__[key].default
if _default_value == value:
raise e.code.CodingError(
msgs=[f"The launch parameter {key} cannot be set to default value {_default_value}"]
)
else:
if getattr(self, key) == value:
_current_value = getattr(self, key)
# this means incoming value is not default
if _default_value != value:
# this means that current value is already updated
if _current_value != _default_value:
raise e.code.CodingError(
msgs=[f"The launch parameter {key} is already set to {value} and you cannot set it again"]
msgs=[f"The launch parameter {key} is already set to non default value {_current_value}",
f"You cannot update it again with new value {value}"]
)
super().__setattr__(key, value)

# set attr
super().__setattr__(key, value)


@dataclasses.dataclass
Expand All @@ -490,9 +505,9 @@ def __setattr__(self, key, value):
# ------------------------------------------- 01.01
# check IS_ON_SINGLE_CPU
if key == 'IS_ON_SINGLE_CPU':
if settings.IS_LSF:
if settings.IS_LSF and value:
raise e.code.CodingError(
msgs=["You cannot set IS_ON_SINGLE_CPU on LSF platform"]
msgs=["You cannot set IS_ON_SINGLE_CPU on LSF platform to True"]
)
# ------------------------------------------- 01.02
# check IS_LSF_JOB
Expand Down Expand Up @@ -613,6 +628,30 @@ def is_view_job(self) -> bool:
else:
raise e.code.ShouldNeverHappen(msgs=[f"Check {sys.argv}"])

@property
def is_archive_job(self) -> bool:
if 'archive' in sys.argv:
if sys.argv[1] == 'archive':
return True
else:
raise e.code.ShouldNeverHappen(msgs=[f"Check {sys.argv}"])

@property
def is_unfinished_job(self) -> bool:
if 'unfinished' in sys.argv:
if sys.argv[1] == 'unfinished':
return True
else:
raise e.code.ShouldNeverHappen(msgs=[f"Check {sys.argv}"])

@property
def is_failed_job(self) -> bool:
if 'failed' in sys.argv:
if sys.argv[1] == 'failed':
return True
else:
raise e.code.ShouldNeverHappen(msgs=[f"Check {sys.argv}"])

@property
def is_launched(self) -> bool:
return self.tag_manager.launched.exists()
Expand Down Expand Up @@ -791,10 +830,10 @@ def __init__(
"This is lsf environment and you are trying to run local job"
]
)
elif self.is_view_job:
elif self.is_view_job or self.is_archive_job or self.is_unfinished_job or self.is_failed_job:
...
else:
raise e.code.ShouldNeverHappen(msgs=[])
raise e.code.ShouldNeverHappen(msgs=[f"Check {sys.argv}"])
# ------------------------------------------------------------------ 01.03
# check single cpu
if self.is_on_single_cpu:
Expand Down
1 change: 1 addition & 0 deletions toolcraft/job/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ def archive(
print("*"*30)
print(_ps1_script_file.read_text())
print("*"*30)
subprocess.run(["gedit", _ps1_script_file.as_posix()])


@_APP.command(help="Copies from server to cwd.")
Expand Down
23 changes: 4 additions & 19 deletions toolcraft/storage/file_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,6 @@ def periodic_check_needed(self) -> bool:
def is_outdated(self) -> bool:
return False

# def __getattribute__(self, item: str) -> t.Any:
# """
# Helper method to access the files in group as annotated attributes.
# """
# # todo: if there is t.ClassVar annotation use their name to
# # resolve the file and get() it here ... so that we can get files in
# # FileGroup without file_key
# # a way to detect if field is class var
# # dataclasses._is_classvar('aa', typing)
# ...

def explore(self):
# for open with select
# subprocess.Popen(r'explorer /select,"sadasdfas"')
Expand Down Expand Up @@ -1231,14 +1220,10 @@ def dtype(self) -> t.Dict[str, t.Any]:
@property
@util.CacheResult
def lengths(self) -> t.Dict[str, int]:
if self.is_created:
_shape = self.config.shape
return {
k: _shape[k][0] for k in self.file_keys
}
raise e.code.CodingError(
msgs=["You have not created files yet so you cannot use this property"]
)
_shape = self.shape
return {
k: _shape[k][0] for k in self.file_keys
}

@property
def has_arbitrary_lengths(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion toolcraft/texipy/__base__.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ def write(
if make_pdf:
from . import helper
helper.make_pdf_with_pdflatex(
tex_file=_save_to_file,
main_tex_file=_save_to_file,
pdf_file=_save_to_file.parent /
(_save_to_file.name.split(".")[0] + ".pdf"),
clean=clean,
Expand Down
2 changes: 1 addition & 1 deletion toolcraft/texipy/usepackage.sty
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
\RequirePackage{subcaption} %
\RequirePackage{ulem} %
\RequirePackage{ragged2e} % https://www.overleaf.com/learn/latex/Text_alignment
%\RequirePackage{todonotes}
\RequirePackage{todonotes}

% reguired for mathbb
\RequirePackage{amsmath}
Expand Down
30 changes: 20 additions & 10 deletions toolcraft/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,18 +1215,28 @@ def io_is_dir_empty(_dir: pathlib.Path) -> bool:
return _is_empty


def find_free_port(localhost: bool):
def find_open_port(start_port=49152, end_port=65535, raise_error: bool = True) -> t.Optional[int]:
"""
Inspired from
https://stackoverflow.com/questions/1365265/on-localhost-how-do-i-pick-a-free-port-number
Find an open port on the local machine between the specified range.
Returns the open port number or None if no port is found.
"""
with contextlib.closing(
socket.socket(socket.AF_INET, socket.SOCK_STREAM)
) as _socket:
# use 'localhost' for local machines ... or else I assume that it will scan all network routes
_socket.bind(('localhost' if localhost else '', 0))
_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return _socket.getsockname()[1]
for _port in range(start_port, end_port):
try:
# Try to create a socket using TCP/IP
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# Try to bind to the port
s.bind(("", _port))
# If successful, return this port
return _port
except OSError:
# This will be raised if the port is already in use
pass
# No open port was found
if raise_error:
raise e.validation.NotAllowed(
msgs=["We did not find any open port"]
)
return None


def npy_load(file: "s.Path", memmap: bool = False, shape=None, dtype=None, ) -> np.ndarray:
Expand Down

0 comments on commit 2dd8f38

Please sign in to comment.