Skip to content

Commit 01dab6e

Browse files
authored
Merge branch 'master' into python-315-sampling-profiler
2 parents 968b336 + 9d69c03 commit 01dab6e

16 files changed

Lines changed: 903 additions & 0 deletions

python-mixins-course/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Understanding Mixin Classes in Python
2+
3+
This folder contains sample code for the Real Python video course [Understanding Mixin Classes in Python](https://realpython.com/courses/understanding-mixin-classes-in-python/).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Person:
2+
def __init__(self, name, age):
3+
self.name = name
4+
self.age = age
5+
6+
def as_dict(self):
7+
return {
8+
"name": self.name,
9+
"age": self.age,
10+
}
11+
12+
13+
class Car:
14+
def __init__(self, make, model):
15+
self.make = make
16+
self.model = model
17+
18+
def as_dict(self):
19+
return {
20+
"make": self.make,
21+
"model": self.model,
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class AsDictMixin:
2+
def as_dict(self):
3+
return vars(self)
4+
5+
6+
class Person(AsDictMixin):
7+
def __init__(self, name, age):
8+
self.name = name
9+
self.age = age
10+
11+
12+
john = Person("John", 42)
13+
print(john.as_dict())
14+
15+
16+
class Car(AsDictMixin):
17+
def __init__(self, make, model):
18+
self.make = make
19+
self.model = model
20+
21+
22+
toyota = Car("Toyota", "Prius")
23+
print(toyota.as_dict())

python-mixins-course/sequence.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Sequence(ABC):
5+
@abstractmethod
6+
def __getitem__(self, index):
7+
pass
8+
9+
@abstractmethod
10+
def __len__(self):
11+
pass
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Sequence(ABC):
5+
@abstractmethod
6+
def __getitem__(self, index):
7+
pass
8+
9+
@abstractmethod
10+
def __len__(self):
11+
pass
12+
13+
def __iter__(self):
14+
index = 0
15+
while index < len(self):
16+
yield self[index]
17+
index += 1
18+
19+
20+
class MyRange(Sequence):
21+
def __init__(self, stop):
22+
self.stop = stop # Assume that `stop` is >= 0.
23+
24+
def __getitem__(self, index):
25+
if 0 <= index < self.stop:
26+
return index
27+
raise IndexError
28+
29+
def __len__(self):
30+
return self.stop
31+
32+
33+
r = MyRange(10)
34+
for number in r:
35+
print(number)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Sequence(ABC):
5+
@abstractmethod
6+
def __getitem__(self, index):
7+
pass
8+
9+
@abstractmethod
10+
def __len__(self):
11+
pass
12+
13+
def __iter__(self):
14+
index = 0
15+
while index < len(self):
16+
yield self[index]
17+
index += 1
18+
19+
20+
class MyRange(Sequence):
21+
def __init__(self, stop):
22+
self.stop = stop # Assume that `stop` is >= 0.
23+
24+
def __getitem__(self, index):
25+
if 0 <= index < self.stop:
26+
return index
27+
raise IndexError
28+
29+
def __len__(self):
30+
return self.stop
31+
32+
33+
r = MyRange(10)
34+
for number in r:
35+
print(number)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class IterMixin:
5+
# __iter__ works IF the class inheriting the mixin
6+
# already provides __getitem__ and __len__.
7+
def __iter__(self):
8+
print("Hey from inside IterMixin.__iter__.")
9+
index = 0
10+
while index < len(self):
11+
yield self[index]
12+
index += 1
13+
14+
15+
class Sequence(IterMixin, ABC):
16+
@abstractmethod
17+
def __getitem__(self, index):
18+
pass
19+
20+
@abstractmethod
21+
def __len__(self):
22+
pass
23+
24+
25+
class MyRange(Sequence):
26+
def __init__(self, stop):
27+
self.stop = stop # Assume that `stop` is >= 0.
28+
29+
def __getitem__(self, index):
30+
if 0 <= index < self.stop:
31+
return index
32+
raise IndexError
33+
34+
def __len__(self):
35+
return self.stop
36+
37+
38+
r = MyRange(10)
39+
for number in r:
40+
print(number)

python-pointblank/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Validating Data With Pointblank in Python
2+
3+
Supporting code and sample data for the Real Python tutorial [Validating Data With Pointblank in Python](https://realpython.com/python-pointblank/).
4+
5+
## Requirements
6+
7+
The Python scripts use PEP 723 dependency metadata and run with [uv](https://docs.astral.sh/uv/):
8+
9+
```console
10+
$ uv run pointblank_quickstart.py
11+
$ uv run pointblank_thresholds.py
12+
$ uv run pointblank_atoms.py
13+
```
14+
15+
The command-line examples can run without a project environment:
16+
17+
```console
18+
$ uv run --no-project --with 'pointblank[pl]' -- pb scan pointblank_atoms.csv
19+
$ uv run --no-project --with 'pointblank[pl]' -- pb missing pointblank_atoms.csv
20+
$ uvx --from 'pointblank[pl]' pb run pointblank_atoms.yaml --output-html pointblank_report.html
21+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
atom_id,symbol,x,y,z,fx,fy,fz
2+
0,Cu,1.0,0.5,0.1,0.1,0.0,0.0
3+
1,Pt,2.1,1.5,0.2,-0.2,0.1,-0.1
4+
2,Cu,3.2,2.5,0.3,0.3,-0.1,0.1
5+
3,Pt,4.3,3.5,0.4,-0.1,0.0,0.0
6+
4,Cu,5.4,4.5,0.5,0.2,0.1,-0.1
7+
5,Pt,6.5,5.5,0.6,-0.3,-0.1,0.1
8+
6,Cu,7.6,6.5,0.7,0.1,0.0,0.0
9+
7,Pt,8.7,7.5,0.8,-0.2,0.1,-0.1
10+
8,Cu,9.8,8.5,0.9,0.3,-0.1,0.1
11+
9,Pt,10.9,9.5,1.0,-0.1,0.0,0.0
12+
10,Zz,0.5,0.5,0.1,0.0,0.0,0.0
13+
11,Cu,,1.5,0.2,0.0,0.0,0.0
14+
12,Pt,12.1,2.5,0.3,1500.0,0.0,0.0
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# /// script
2+
# requires-python = ">=3.10"
3+
# dependencies = [
4+
# "pointblank[pl]",
5+
# ]
6+
# ///
7+
8+
import polars as pl
9+
import pointblank as pb
10+
11+
VALID_ELEMENTS = ["Cu", "Pt"]
12+
13+
14+
def main() -> None:
15+
atoms = pl.read_csv("pointblank_atoms.csv")
16+
17+
validation = (
18+
pb.Validate(
19+
data=atoms,
20+
tbl_name="atoms",
21+
label="Atom data validation",
22+
thresholds=pb.Thresholds(warning=0.02, error=0.05, critical=0.07),
23+
)
24+
.col_vals_in_set(columns="symbol", set=VALID_ELEMENTS)
25+
.col_vals_not_null(columns=["x", "y", "z"])
26+
.col_vals_between(columns=["x", "y", "z"], left=0, right=20)
27+
.col_vals_between(columns=["fx", "fy", "fz"], left=-1000, right=1000)
28+
.interrogate()
29+
)
30+
31+
clean = validation.get_sundered_data(type="pass")
32+
dirty = validation.get_sundered_data(type="fail")
33+
34+
print(f"Clean rows: {len(clean)}")
35+
print(clean.select(["atom_id", "symbol", "x", "fx"]))
36+
print(f"\nDirty rows: {len(dirty)}")
37+
print(dirty.select(["atom_id", "symbol", "x", "fx"]))
38+
39+
40+
if __name__ == "__main__":
41+
main()

0 commit comments

Comments
 (0)