Skip to content

Create time-stamp and load it into args #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/diffpy/labpdfproc/labpdfprocapp.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from argparse import ArgumentParser

from datetime import datetime
from diffpy.labpdfproc.functions import apply_corr, compute_cve
from diffpy.labpdfproc.tools import (
known_sources,
Expand Down Expand Up @@ -90,6 +90,18 @@ def get_args(override_cli_inputs=None):
"For example, facility='NSLS II', 'facility=NSLS II', beamline=28ID-2, "
"'beamline'='28ID-2', 'favorite color'=blue, are all valid key=value items. ",
)
p.add_argument(
"-t"
"--is-test",
action="store_true",
help="If this flag is specified, the app would be run in test mode. No need for general users to specify this "
"flag",
)
p.add_argument(
"-ct",
"--creation_time",
default=datetime.now().strftime("%Y%m%d%H%M%S"),
)
args = p.parse_args(override_cli_inputs)
return args

Expand Down
19 changes: 17 additions & 2 deletions src/diffpy/labpdfproc/tests/test_tools.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import re
from pathlib import Path

import freezegun
from freezegun import freeze_time
from datetime import datetime
import pytest

from diffpy.labpdfproc.labpdfprocapp import get_args
Expand All @@ -11,6 +13,7 @@
set_input_lists,
set_output_directory,
set_wavelength,
load_datetime
)

# Use cases can be found here: https://github.com/diffpy/diffpy.labpdfproc/issues/48
Expand Down Expand Up @@ -162,7 +165,7 @@ def test_set_wavelength(inputs, expected):
params3 = [
(
["--anode-type", "invalid"],
[f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources, }."],
[f"Anode type not recognized. Please rerun specifying an anode_type from {*known_sources,}."],
),
(
["--wavelength", "0"],
Expand Down Expand Up @@ -241,3 +244,15 @@ def test_load_user_metadata_bad(inputs, msg):
actual_args = get_args(cli_inputs)
with pytest.raises(ValueError, match=msg[0]):
actual_args = load_user_metadata(actual_args)


params7 = [(["--creation_time", "2014-02-01 12:34:56"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't want --creation-time as an input. Please delete.

[datetime.strptime("2014-02-01 12:34:56", "%Y-%m-%d %H:%M:%S")])]


@freezegun.freeze_time("2024-02-01 12:34:56")
@pytest.mark.parametrize("inputs, expected", params7)
def test_load_datetime(inputs, expected):
actual_args = ["2.5", "data.xy"] + inputs
actual_args = load_datetime(actual_args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we won't be loading a datetime so this function is not needed.

assert actual_args.creation_time == expected
19 changes: 19 additions & 0 deletions src/diffpy/labpdfproc/tools.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datetime import datetime
from pathlib import Path

WAVELENGTHS = {"Mo": 0.71, "Ag": 0.59, "Cu": 1.54}
Expand Down Expand Up @@ -171,3 +172,21 @@ def load_user_metadata(args):
setattr(args, key, value)
delattr(args, "user_metadata")
return args


def load_datetime(args):
"""
Load datetime into the provided argparse Namespace.

Parameters
----------
args argparse.Namespace
the arguments from the parser

Returns
-------
the updated argparse Namespace with datetime inserted as key-value pairs
"""
curr_time = datetime.now()
setattr(args, "creation_time", curr_time)
return args