oh-my-batch 0.2.0__tar.gz → 0.2.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,22 +1,57 @@
1
+ Metadata-Version: 2.1
2
+ Name: oh-my-batch
3
+ Version: 0.2.2
4
+ Summary:
5
+ License: GPL
6
+ Author: weihong.xu
7
+ Author-email: xuweihong.cn@gmail.com
8
+ Requires-Python: >=3.8,<4.0
9
+ Classifier: License :: Other/Proprietary License
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3.8
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Requires-Dist: fire (>=0.7.0,<0.8.0)
18
+ Description-Content-Type: text/markdown
19
+
1
20
  # oh-my-batch
2
21
 
3
22
  [![PyPI version](https://badge.fury.io/py/oh-my-batch.svg)](https://badge.fury.io/py/oh-my-batch)
4
23
  [![PyPI - Downloads](https://img.shields.io/pypi/dm/oh-my-batch)](https://pypi.org/project/oh-my-batch/)
5
24
  [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/oh-my-batch)](https://pypi.org/project/oh-my-batch/)
6
25
 
7
- A simple tool to manipulate batch tasks designed for scientific computing community.
26
+ A toolkit to manipulate batch tasks with command line. Designed for scientific computing community.
8
27
 
9
28
  ## Features
10
29
  * `omb combo`: generate folders/files from different combinations of parameters
11
30
  * `omb batch`: generate batch scripts from multiple working directories
12
31
  * `omb job`: track the state of job in job schedular
32
+ * `omb misc`: miscellaneous commands
13
33
 
14
34
  ## Install
15
35
  ```bash
16
36
  pip install oh-my-batch
17
37
  ```
18
38
 
39
+ ## Shell tips
40
+ `oh-my-batch` is intended to help you implement computational workflows with shell scripts.
41
+ To make the best use of `oh-my-batch`, you need to know some shell tips.
42
+
43
+ * [Retry commands until success in shell script](https://stackoverflow.com/a/79191004/3099733)
44
+ * [Run multiple line shell script with ssh](https://stackoverflow.com/a/32082912/3099733)
45
+
19
46
  ## Use cases
47
+ ### Load functions in shell script
48
+ You can load useful functions from `oh-my-batch` this way:
49
+
50
+ ```bash
51
+ omb misc export-shell-func > omb-func.sh && source omb-func.sh && rm omb-func.sh
52
+ ```
53
+
54
+ This will load some functions to your shell script, for example, `checkpoint`.
20
55
 
21
56
  ### Generate files from different combinations of parameters
22
57
 
@@ -34,8 +69,8 @@ touch tmp/1.data tmp/2.data tmp/3.data
34
69
 
35
70
  # prepare a lammps input file template
36
71
  cat > tmp/in.lmp.tmp <<EOF
37
- read_data $DATA_FILE
38
- velocity all create $TEMP $RANDOM
72
+ read_data @DATA_FILE
73
+ velocity all create @TEMP @RANDOM
39
74
  run 1000
40
75
  EOF
41
76
 
@@ -109,3 +144,4 @@ and wait for the jobs to finish. If the job fails, it will retry for at most 3 t
109
144
  The `--recovery` option will save the job information to `lammps-jobs.json` file.
110
145
  If `omb job` is interrupted, you can rerun the exact same command to recover the job status,
111
146
  so that you don't need to resubmit the jobs that are still running or completed.
147
+
@@ -1,40 +1,38 @@
1
- Metadata-Version: 2.1
2
- Name: oh-my-batch
3
- Version: 0.2.0
4
- Summary:
5
- License: GPL
6
- Author: weihong.xu
7
- Author-email: xuweihong.cn@gmail.com
8
- Requires-Python: >=3.8,<4.0
9
- Classifier: License :: Other/Proprietary License
10
- Classifier: Programming Language :: Python :: 3
11
- Classifier: Programming Language :: Python :: 3.8
12
- Classifier: Programming Language :: Python :: 3.9
13
- Classifier: Programming Language :: Python :: 3.10
14
- Classifier: Programming Language :: Python :: 3.11
15
- Classifier: Programming Language :: Python :: 3.12
16
- Requires-Dist: fire (>=0.7.0,<0.8.0)
17
- Description-Content-Type: text/markdown
18
-
19
1
  # oh-my-batch
20
2
 
21
3
  [![PyPI version](https://badge.fury.io/py/oh-my-batch.svg)](https://badge.fury.io/py/oh-my-batch)
22
4
  [![PyPI - Downloads](https://img.shields.io/pypi/dm/oh-my-batch)](https://pypi.org/project/oh-my-batch/)
23
5
  [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/oh-my-batch)](https://pypi.org/project/oh-my-batch/)
24
6
 
25
- A simple tool to manipulate batch tasks designed for scientific computing community.
7
+ A toolkit to manipulate batch tasks with command line. Designed for scientific computing community.
26
8
 
27
9
  ## Features
28
10
  * `omb combo`: generate folders/files from different combinations of parameters
29
11
  * `omb batch`: generate batch scripts from multiple working directories
30
12
  * `omb job`: track the state of job in job schedular
13
+ * `omb misc`: miscellaneous commands
31
14
 
32
15
  ## Install
33
16
  ```bash
34
17
  pip install oh-my-batch
35
18
  ```
36
19
 
20
+ ## Shell tips
21
+ `oh-my-batch` is intended to help you implement computational workflows with shell scripts.
22
+ To make the best use of `oh-my-batch`, you need to know some shell tips.
23
+
24
+ * [Retry commands until success in shell script](https://stackoverflow.com/a/79191004/3099733)
25
+ * [Run multiple line shell script with ssh](https://stackoverflow.com/a/32082912/3099733)
26
+
37
27
  ## Use cases
28
+ ### Load functions in shell script
29
+ You can load useful functions from `oh-my-batch` this way:
30
+
31
+ ```bash
32
+ omb misc export-shell-func > omb-func.sh && source omb-func.sh && rm omb-func.sh
33
+ ```
34
+
35
+ This will load some functions to your shell script, for example, `checkpoint`.
38
36
 
39
37
  ### Generate files from different combinations of parameters
40
38
 
@@ -52,8 +50,8 @@ touch tmp/1.data tmp/2.data tmp/3.data
52
50
 
53
51
  # prepare a lammps input file template
54
52
  cat > tmp/in.lmp.tmp <<EOF
55
- read_data $DATA_FILE
56
- velocity all create $TEMP $RANDOM
53
+ read_data @DATA_FILE
54
+ velocity all create @TEMP @RANDOM
57
55
  run 1000
58
56
  EOF
59
57
 
@@ -127,4 +125,3 @@ and wait for the jobs to finish. If the job fails, it will retry for at most 3 t
127
125
  The `--recovery` option will save the job information to `lammps-jobs.json` file.
128
126
  If `omb job` is interrupted, you can rerun the exact same command to recover the job status,
129
127
  so that you don't need to resubmit the jobs that are still running or completed.
130
-
@@ -1,6 +1,7 @@
1
+ GREEN='\033[0;32m'
2
+ NC='\033[0m'
1
3
 
2
4
  checkpoint() {
3
- # Usage: checkpoint <flag_file> <command> [arg1] [arg2] ...
4
5
  local flag_file="$1"
5
6
  shift # Remove the first argument so $@ contains only the command and its arguments
6
7
  if [ -f "$flag_file" ]; then
@@ -10,6 +11,7 @@ checkpoint() {
10
11
  local exit_code=$?
11
12
  if [ $exit_code -eq 0 ]; then
12
13
  local current_time=$(date '+%Y-%m-%d %H:%M:%S')
14
+ mkdir -p "$(dirname "$flag_file")"
13
15
  printf 'Command succeeded at %s\n' "$current_time" > "$flag_file"
14
16
  echo "Created flag file '$flag_file' with timestamp: $current_time"
15
17
  else
@@ -18,3 +20,16 @@ checkpoint() {
18
20
  fi
19
21
  fi
20
22
  }
23
+
24
+ echo -e "${GREEN}Functiion: checkpoint${NC}"
25
+ cat <<EOF
26
+ Usage:
27
+ checkpoint <flag_file> <command> [arg1] [arg2] ...
28
+
29
+ Set a flag file to indicate the completion of a command,
30
+ so that it will not be executed again next time.
31
+
32
+ Example:
33
+ checkpoint lmp.done lmp -in in.lmp
34
+
35
+ EOF
@@ -23,6 +23,9 @@ class OhMyBatch:
23
23
  def job(self):
24
24
  return JobCli()
25
25
 
26
+ def misc(self):
27
+ from .misc import Misc
28
+ return Misc()
26
29
 
27
30
  def main():
28
31
  fire.Fire(OhMyBatch)
@@ -1,6 +1,7 @@
1
1
  from itertools import product
2
2
  from string import Template
3
3
  import random
4
+ import json
4
5
  import os
5
6
 
6
7
  from .util import expand_globs, mode_translate
@@ -67,7 +68,7 @@ class ComboMaker:
67
68
  self.add_var(key, *args, broadcast=broadcast)
68
69
  return self
69
70
 
70
- def add_files(self, key: str, *path: str, broadcast=False, abs=False):
71
+ def add_files(self, key: str, *path: str, broadcast=False, abs=False, raise_invalid=False):
71
72
  """
72
73
  Add a variable with files by glob pattern
73
74
  For example, suppose there are 3 files named 1.txt, 2.txt, 3.txt in data directory,
@@ -78,8 +79,9 @@ class ComboMaker:
78
79
  :param path: Path to files, can include glob pattern
79
80
  :param broadcast: If True, values are broadcasted, otherwise they are producted when making combos
80
81
  :param abs: If True, path will be turned into absolute path
82
+ :param raise_invalid: If True, will raise error if no file found for a glob pattern
81
83
  """
82
- args = expand_globs(path, raise_invalid=True)
84
+ args = expand_globs(path, raise_invalid=raise_invalid)
83
85
  if not args:
84
86
  raise ValueError(f"No files found for {path}")
85
87
  if abs:
@@ -87,7 +89,8 @@ class ComboMaker:
87
89
  self.add_var(key, *args, broadcast=broadcast)
88
90
  return self
89
91
 
90
- def add_files_as_one(self, key: str, path: str, broadcast=False, sep=' ', abs=False):
92
+ def add_files_as_one(self, key: str, *path: str, broadcast=False, format=None,
93
+ sep=' ', abs=False, raise_invalid=False):
91
94
  """
92
95
  Add a variable with files by glob pattern as one string
93
96
  Unlike add_files, this function joins the files with a delimiter.
@@ -98,15 +101,25 @@ class ComboMaker:
98
101
  :param key: Variable name
99
102
  :param path: Path to files, can include glob pattern
100
103
  :param broadcast: If True, values are broadcasted, otherwise they are producted when making combos
104
+ :param format: the way to format the files, can be None, 'json-list','json-item'
101
105
  :param sep: Separator to join files
102
106
  :param abs: If True, path will be turned into absolute path
107
+ :param raise_invalid: If True, will raise error if no file found for a glob pattern
103
108
  """
104
- args = expand_globs(path, raise_invalid=True)
109
+ args = expand_globs(path, raise_invalid=raise_invalid)
105
110
  if not args:
106
111
  raise ValueError(f"No files found for {path}")
107
112
  if abs:
108
113
  args = [os.path.abspath(p) for p in args]
109
- self.add_var(key, sep.join(args), broadcast=broadcast)
114
+ if format is None:
115
+ value = sep.join(args)
116
+ elif format == 'json-list':
117
+ value = json.dumps(args)
118
+ elif format == 'json-item':
119
+ value = json.dumps(args).strip('[]')
120
+ else:
121
+ raise ValueError(f"Invalid format: {format}")
122
+ self.add_var(key, value, broadcast=broadcast)
110
123
  return self
111
124
 
112
125
  def add_var(self, key: str, *args, broadcast=False):
@@ -148,19 +161,20 @@ class ComboMaker:
148
161
  raise ValueError(f"Variable {key} not found")
149
162
  return self
150
163
 
151
- def make_files(self, template: str, dest: str, delimiter='$', mode=None):
164
+ def make_files(self, template: str, dest: str, delimiter='@', mode=None):
152
165
  """
153
166
  Make files from template
154
167
  The template file can include variables with delimiter.
155
- For example, if delimiter is '$', then the template file can include $var1, $var2, ...
168
+ For example, if delimiter is '@', then the template file can include @var1, @var2, ...
156
169
 
157
170
  The destination can also include variables in string format style.
158
- For example, if dest is 'output/{i}.txt', then files are saved as output/0.txt, output/1.txt, ...
171
+ For example, if dest is 'output/{i}-{TEMP}.txt',
172
+ then files are saved as output/0-300K.txt, output/1-400K.txt, ...
159
173
 
160
174
  :param template: Path to template file
161
175
  :param dest: Path pattern to destination file
162
- :param delimiter: Delimiter for variables in template, default is '$',
163
- can be changed to other character, e.g $$, @, ...
176
+ :param delimiter: Delimiter for variables in template, default is '@', as '$' is popular in shell scripts
177
+ can be changed to other character, e.g $, $$, ...
164
178
  """
165
179
  _delimiter = delimiter
166
180
 
@@ -0,0 +1,14 @@
1
+ from .assets import get_asset
2
+
3
+ class Misc:
4
+
5
+ def export_shell_func(self):
6
+ """
7
+ Export shell functions for batch scripts
8
+
9
+ For example, you can load them to you shell environment with the following command:
10
+ omb misc export-shell-func > omb-func.sh && source omb-func.sh
11
+ """
12
+ shell_func = get_asset('functions.sh')
13
+ with open(shell_func, 'r', encoding='utf-8') as f:
14
+ print(f.read())
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "oh-my-batch"
3
- version = "0.2.0"
3
+ version = "0.2.2"
4
4
  description = ""
5
5
  authors = ["weihong.xu <xuweihong.cn@gmail.com>"]
6
6
  license = "GPL"
@@ -11,7 +11,6 @@ packages = [{include = "oh_my_batch"}]
11
11
  python = "^3.8"
12
12
  fire = "^0.7.0"
13
13
 
14
-
15
14
  [build-system]
16
15
  requires = ["poetry-core"]
17
16
  build-backend = "poetry.core.masonry.api"
File without changes