Skip to content

Commit f6ebbd9

Browse files
committed
cleaning up converter notebooks
1 parent 57fd085 commit f6ebbd9

File tree

13 files changed

+207
-0
lines changed

13 files changed

+207
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[project]
2+
name = "myconverters"
3+
description = "TODO"
4+
version='0.1.0'
5+
requires-python = ">=3.9"
6+
dependencies = [
7+
"asdf >=2.8",
8+
]
9+
10+
[build-system]
11+
requires = ["setuptools >=61", "setuptools_scm[toml] >=3.4"]
12+
build-backend = "setuptools.build_meta"
13+
14+
[project.entry-points."asdf.resource_mappings"]
15+
myschema = "myschemas.integration:get_resource_mappings"
16+
17+
[project.entry-points."asdf.extensions"]
18+
myconverters = "myconverters.integration:get_extensions"

notebooks/Your_second_ASDF_converter/src/myconverters/__init__.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from asdf.extension import Converter
2+
from .photo_id import PhotoID
3+
from .traffic_citation import TrafficCitation
4+
5+
6+
class PhotoIDConverter(Converter):
7+
tags = ["asdf://stsci.edu/example-project/tags/photo_id-*"]
8+
types = ["myconverters.photo_id.PhotoID"]
9+
# The above registers the tag that the converter is used for, as well as
10+
# associating the class that the converter is used for.
11+
12+
# This method converts from the Python object to yaml
13+
def to_yaml_tree(self, obj, tags, ctx):
14+
# The yaml conversion expects a dictionary returned
15+
node = {}
16+
node['first_name'] = obj.first_name
17+
node['last_name'] = obj.last_name
18+
node['photo'] = obj.photo
19+
return node
20+
21+
# This method converts from yaml to the Python object
22+
def from_yaml_tree(self, node, tag, ctx):
23+
return PhotoID(node['last_name'],
24+
node['first_name'],
25+
node['photo'])
26+
27+
28+
class TrafficCitationConverter(Converter):
29+
tags = ["asdf://stsci.edu/example-project/tags/traffic_citation-1.0.0"]
30+
types = ["myconverters.traffic_citation.TrafficCitation"]
31+
32+
def to_yaml_tree(self, obj, tags, ctx):
33+
node = {}
34+
node['ociffer'] = obj.ociffer
35+
node['violation'] = obj.violation
36+
node['date'] = obj.date
37+
node['time'] = obj.time
38+
node['photo_id'] = obj.photo_id
39+
return node
40+
41+
def from_yaml_tree(self, node, tag, ctx):
42+
return TrafficCitation(node['ociffer'],
43+
node['violation'],
44+
node['date'],
45+
node['time'],
46+
node['photo_id'])
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from asdf.extension import ManifestExtension
2+
from .converters import (
3+
PhotoIDConverter,
4+
TrafficCitationConverter,)
5+
6+
MY_CONVERTERS = [
7+
PhotoIDConverter(),
8+
TrafficCitationConverter(),
9+
]
10+
11+
MY_EXTENSIONS = [
12+
ManifestExtension.from_uri(
13+
"asdf://stsci.edu/example-project/manifests/allmyschemas-1.0",
14+
converters=MY_CONVERTERS)
15+
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def get_extensions():
2+
from . import extensions
3+
return extensions.MY_EXTENSIONS
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class PhotoID:
2+
"Holds Phot ID information"
3+
4+
def __init__(self, last_name, first_name, image):
5+
"expects a monochromatic numpy array for image"
6+
self.last_name = last_name
7+
self.first_name = first_name
8+
self.photo = image
9+
10+
def name(self):
11+
return self.last_name + ', ' + self.first_name
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from .photo_id import PhotoID
2+
3+
4+
class TrafficCitation:
5+
"Record of a traffic violation"
6+
7+
def __init__(self, ociffer, violation, date, time, photo_id):
8+
self.ociffer = ociffer
9+
self.violation = violation
10+
self.date = date
11+
self.time = time
12+
self.photo_id = photo_id

notebooks/Your_second_ASDF_converter/src/myschemas/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import sys
2+
3+
from asdf.resource import DirectoryResourceMapping
4+
5+
import importlib.resources as importlib_resources
6+
7+
8+
def get_resource_mappings():
9+
"""
10+
Get the resource mapping instances for myschemas
11+
and manifests. This method is registered with the
12+
asdf.resource_mappings entry point.
13+
14+
Returns
15+
-------
16+
list of collections.abc.Mapping
17+
"""
18+
from . import resources
19+
resources_root = importlib_resources.files(resources)
20+
21+
return [
22+
DirectoryResourceMapping(
23+
resources_root / "schemas", "asdf://stsci.edu/example-project/schemas/"),
24+
DirectoryResourceMapping(
25+
resources_root / "manifests", "asdf://stsci.edu/example-project/manifests/"),
26+
]

notebooks/Your_second_ASDF_converter/src/myschemas/resources/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)