Skip to content

Commit 793582c

Browse files
committed
Add files
1 parent f8e8cf5 commit 793582c

File tree

220 files changed

+15805
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

220 files changed

+15805
-0
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Python for Quantum Chemistry: A Full Stack Programming Guide
2+
3+
Welcome to the repository for the book *Python for Quantum Chemistry: A Full Stack Programming Guide*. This repository contains all the code examples and supplementary materials in the book.
4+
5+
To run the code examples, make sure to install the required libraries:
6+
```
7+
pip install -r requirements.txt
8+
```
9+
Please note that some chapters may have specific dependencies. You can install these dependencies by using the provided `requirements.txt` file in each chapter.
10+
11+
We welcome your feedback and encourage you to use the issue board to post comments, ask questions, or report any issues you encounter in the book.

code-examples/__init__.py

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from . import module_b
2+
3+
def factory():
4+
return module_b.B()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from . import module_a
2+
3+
def inspect():
4+
return dir(module_a)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from . import module_b
2+
3+
class A:
4+
pass
5+
6+
def factory():
7+
return module_b.B()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from . import module_a
2+
3+
class B(module_a.A):
4+
pass

code-examples/chap01/pyldd/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Tool for inspecting Python pacakge dependencies
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package:
2+
name: pyldd
3+
version: 0.1.0
4+
5+
source:
6+
path: ..
7+
8+
build:
9+
script: pip install --no-deps .
10+
11+
requirements:
12+
host:
13+
- python
14+
run:
15+
- python
16+
- click

code-examples/chap01/pyldd/pyldd/__init__.py

Whitespace-only changes.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/usr/bin/env python
2+
3+
'''
4+
Find and print all dependent libraries required by a package.
5+
'''
6+
7+
import os
8+
import importlib
9+
import ast
10+
import argparse
11+
import pathlib
12+
13+
def get_all_imports(path):
14+
raw_imports = set()
15+
for root, dirs, files in os.walk(path):
16+
for file_name in files:
17+
if os.path.splitext(file_name)[1] != ".py":
18+
continue
19+
file_name = os.path.join(root, file_name)
20+
with open(file_name, 'r') as f:
21+
contents = f.read()
22+
try:
23+
tree = ast.parse(contents)
24+
for node in ast.walk(tree):
25+
if isinstance(node, ast.Import):
26+
for subnode in node.names:
27+
raw_imports.add(subnode.name)
28+
elif isinstance(node, ast.ImportFrom):
29+
raw_imports.add(node.module)
30+
except Exception as exc:
31+
print(f'Failed to parse file: {file_name}')
32+
return list(raw_imports)
33+
34+
def main():
35+
parser = argparse.ArgumentParser(prog='pyldd', description=__doc__)
36+
parser.add_argument('path', type=pathlib.Path)
37+
parser.add_argument('--depth', type=int, default=0, help='The maximum depth of sub-packages to inspect')
38+
args = parser.parse_args()
39+
40+
imports = get_all_imports(args.path)
41+
imports = [imp for imp in imports if imp.count('.') <= args.depth]
42+
for name in set(imports):
43+
try:
44+
mod = importlib.import_module(name)
45+
print(mod)
46+
except Exception:
47+
print(f'Module {name} not found')
48+
49+
50+
if __name__ == '__main__':
51+
main()
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/env python
2+
3+
from os.path import dirname
4+
from typing import List
5+
import importlib
6+
import click
7+
from pipreqs import pipreqs
8+
9+
def dep_modules(path: str) -> List[str]:
10+
imports = pipreqs.get_all_imports(path)
11+
modules = {}
12+
for name in imports:
13+
try:
14+
mod = importlib.import_module(name)
15+
modules[name] = mod
16+
except Exception as e:
17+
print(repr(e))
18+
return modules
19+
20+
@click.command(name='pyldd')
21+
@click.option('--depth', default=1, type=int, help="Recursively solve dependencies")
22+
@click.option('--with-version', is_flag=True, help="Also print version of each library")
23+
@click.argument('path', type=click.Path(exists=True, dir_okay=True))
24+
def main(path, depth=1, with_version=False):
25+
'''
26+
Find and print all dependent libraries reuired by a package.
27+
'''
28+
imports = pipreqs.get_all_imports(path)
29+
paths = [path]
30+
sub_paths = []
31+
modules = {}
32+
for _ in range(depth):
33+
deps = {}
34+
for path in paths:
35+
deps.update(dep_modules(path))
36+
paths = [dirname(v.__file__) for v in deps.values()]
37+
modules.update(deps)
38+
39+
for name, mod in modules.items():
40+
if with_version:
41+
try:
42+
print(f'{mod} version={mod.__version__}')
43+
except AttributeError:
44+
print(mod)
45+
else:
46+
print(mod)
47+
48+
if __name__ == '__main__':
49+
main()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#!/usr/bin/env python
2+
3+
'''
4+
Find and print all dependent libraries required by a package.
5+
'''
6+
7+
import os
8+
import importlib
9+
import ast
10+
import click
11+
12+
def get_all_imports(path):
13+
raw_imports = set()
14+
for root, dirs, files in os.walk(path):
15+
for file_name in files:
16+
if os.path.splitext(file_name)[1] != ".py":
17+
continue
18+
file_name = os.path.join(root, file_name)
19+
with open(file_name, 'r') as f:
20+
contents = f.read()
21+
try:
22+
tree = ast.parse(contents)
23+
for node in ast.walk(tree):
24+
if isinstance(node, ast.Import):
25+
for subnode in node.names:
26+
raw_imports.add(subnode.name)
27+
elif isinstance(node, ast.ImportFrom):
28+
raw_imports.add(node.module)
29+
except Exception as exc:
30+
print(f'Failed to parse file: {file_name}')
31+
return list(raw_imports)
32+
33+
@click.command(name='pyldd', help=__doc__)
34+
@click.option('--depth', type=int, default=0, help='The maximum depth of sub-packages to inspect')
35+
@click.argument('path', type=click.Path(exists=True, dir_okay=True))
36+
def main(path, depth):
37+
imports = get_all_imports(path)
38+
imports = [imp for imp in imports if imp.count('.') <= depth]
39+
for name in set(imports):
40+
try:
41+
mod = importlib.import_module(name)
42+
print(mod)
43+
except Exception:
44+
print(f'Module {name} not found')
45+
46+
47+
if __name__ == '__main__':
48+
main()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env python
2+
3+
import importlib
4+
import click
5+
from pipreqs import pipreqs
6+
7+
@click.command(name='pyldd')
8+
@click.option('--with-version', is_flag=True, help="Also print version of each library")
9+
@click.argument('path', type=click.Path(exists=True, dir_okay=True))
10+
def main(path, with_version=False):
11+
'''
12+
Find and print all dependent libraries reuired by a package.
13+
'''
14+
imports = pipreqs.get_all_imports(path)
15+
for name in imports:
16+
try:
17+
mod = importlib.import_module(name)
18+
except Exception:
19+
print(f'Failed to inspect module {name}')
20+
if with_version:
21+
try:
22+
print(f'{mod} version={mod.__version__}')
23+
except AttributeError:
24+
print(mod)
25+
else:
26+
print(mod)
27+
28+
29+
if __name__ == '__main__':
30+
main()
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
import importlib
4+
import click
5+
from pipreqs import pipreqs
6+
7+
@click.command(name='pyldd')
8+
@click.option('--with-version', is_flag=True, help="Also print version of each library")
9+
@click.argument('path', type=click.Path(exists=True, dir_okay=True))
10+
def main(path, with_version=False):
11+
'''
12+
Find and print all dependent libraries reuired by a package.
13+
'''
14+
imports = pipreqs.get_all_imports(path)
15+
for name in imports:
16+
mod = importlib.import_module(name)
17+
if with_version:
18+
try:
19+
print(f'{mod} version={mod.__version__}')
20+
except AttributeError:
21+
print(mod)
22+
else:
23+
print(mod)
24+
25+
if __name__ == '__main__':
26+
main()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[project]
2+
name = "pyldd"
3+
version = "0.1.0"
4+
description = "Tool for inspecting Python pacakge dependencies"
5+
readme = "README.md"
6+
dependencies = [
7+
"pipreqs>=0.4",
8+
"click"
9+
]
10+
11+
[project.scripts]
12+
pyldd = "pyldd.pyldd:main"

code-examples/chap01/pyldd/setup.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from setuptools import setup, find_packages
2+
3+
setup(
4+
name='pyldd',
5+
packages=find_packages(),
6+
entry_points={
7+
"console_scripts": [
8+
'pyldd = pyldd.cli:main'
9+
]
10+
}
11+
)

code-examples/chap01/setup_py/example_A.cpp

Whitespace-only changes.

code-examples/chap01/setup_py/example_B.cpp

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from setuptools import setup, Extension
2+
3+
ext1 = Extension(
4+
'example.simple_cpp',
5+
sources=['example_A.cpp', 'example_B.cpp'],
6+
language='c++',
7+
libraries=[':libblas.so.3.9.0'],
8+
#/usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
9+
library_dirs=['/usr/lib/x86_64-linux-gnu/blas/'],
10+
)
11+
setup(ext_modules=[ext1])
12+

code-examples/chap02/mpfft.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import numpy as np
2+
import mpmath as mp
3+
mp.mp.dps = 30
4+
5+
def mpfftfreq(n):
6+
if n % 2 == 0:
7+
p1 = mp.arange(0, n//2)
8+
p2 = mp.arange(-n//2, 0)
9+
else:
10+
p1 = mp.arange(0, (n+1)//2)
11+
p2 = mp.arange(-(n-1)//2, 0)
12+
return np.append(p1, p2) / n
13+
14+
mpexp = np.vectorize(mp.exp)
15+
16+
def mpfft(a):
17+
n = a.shape[-1]
18+
kx = 2j*mp.pi * mpfftfreq(n)
19+
fac = mpexp(-np.arange(n)[:,None] * kx)
20+
return a.dot(fac)
21+
22+
def mpifft(a):
23+
n = a.shape[-1]
24+
kx = 2j*mp.pi * mpfftfreq(n)
25+
fac = mpexp(np.arange(n)[:,None] * kx)
26+
return a.dot(fac) / n
27+
28+
if __name__ == '__main__':
29+
a = np.random.rand(7)
30+
print(abs(np.fft.fft(a) - mpfft(a)).max())
31+
print(abs(a - mpifft(mpfft(a))).max())
32+
33+
a = np.random.rand(3,6)
34+
print(abs(np.fft.fft(a) - mpfft(a)).max())
35+
print(abs(a - mpifft(mpfft(a))).max())

0 commit comments

Comments
 (0)