Skip to content

Commit

Permalink
Added Tutorials 1 and 2 from PandaBlocks-FPGA repo
Browse files Browse the repository at this point in the history
  • Loading branch information
tomtrafford committed Jan 2, 2024
1 parent 7c4ec5e commit d86b62d
Show file tree
Hide file tree
Showing 20 changed files with 3,824 additions and 0 deletions.
Empty file added common/__init__.py
Empty file.
Empty file added common/python/__init__.py
Empty file.
29 changes: 29 additions & 0 deletions common/python/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
try:
# Python2
import ConfigParser as configparser
except ImportError:
# Python3
import configparser

try:
# For type checking only
from typing import TYPE_CHECKING
except ImportError:
TYPE_CHECKING = False

try:
# Python2
str_ = basestring
except NameError:
# Python3
str_ = str

# Taken from six
def add_metaclass(metaclass):
"""Class decorator for creating a class with a metaclass."""
def wrapper(cls):
orig_vars = cls.__dict__.copy()
orig_vars.pop('__dict__', None)
orig_vars.pop('__weakref__', None)
return metaclass(cls.__name__, cls.__bases__, orig_vars)
return wrapper
69 changes: 69 additions & 0 deletions common/python/ini_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import os
from collections import OrderedDict

from .compat import configparser, TYPE_CHECKING, str_

if TYPE_CHECKING:
from typing import Iterable, Tuple, Dict, List, Union


def read_ini(paths):
# type: (Union[List[str], str]) -> configparser.SafeConfigParser
app_ini = configparser.ConfigParser()
read_ok = app_ini.read(paths)
if isinstance(paths, str_):
paths = [paths]
errored = set(paths) - set(read_ok)
assert not errored, "Can't read ini files %s" % [
os.path.abspath(x) for x in sorted(errored)]
return app_ini


def ini_get(ini, section, field, default):
try:
return ini.get(section, field)
except configparser.NoOptionError:
return default


def parse_assigments(line):
# type: (str) -> Dict[str, str]
"""Parse name1=value1, name2=value2 into an OrderedDict"""
ret = OrderedDict()
line = line.strip()
if line:
for assignment in line.split(","):
split = assignment.split("=")
assert len(split) == 2, \
"Expected name=value, got %r" % line
ret[split[0].strip()] = split[1].strip()
return ret


def timing_entries(ini, # type: configparser.SafeConfigParser
section # type: str
):
# type: (...) -> Iterable[Tuple[int, Dict[str, str], Dict[str, str]]]
"""Parse timing lines from ini file, returning timing entries
Args:
ini: The ini files to parse
section: The name of the section to generate lines from
Returns:
A list of timing entries. Each entry is a tuple of (ts, inputs, outputs)
where inputs and outputs are dictionaries mapping the string field name
to its string value
"""
for ts, line in ini.items(section):
ts = int(ts)
split = line.split("->")
assert len(split) in (1, 2), \
"Expected ts1: k1=v1, k2=v2 -> k3=v3, k4=v4, got '%s: %s'" %(
ts, line)
inputs = parse_assigments(split[0])
if len(split) == 2:
outputs = parse_assigments(split[1])
else:
outputs = {}
yield ts, inputs, outputs
Loading

0 comments on commit d86b62d

Please sign in to comment.