Skip to content

Commit

Permalink
Use importlib instead of janky use of eval.
Browse files Browse the repository at this point in the history
Apparently needed for Python 3.13 in any case.

Fix #24.
  • Loading branch information
brettviren committed Nov 19, 2024
1 parent 1b4a713 commit f9cdfc0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 14 deletions.
8 changes: 3 additions & 5 deletions python/gegede/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from collections import OrderedDict

from .util import list_match
from .util import list_match, make_class

class Builder(object):
'''
Expand Down Expand Up @@ -88,10 +88,8 @@ def add_builder(self, name, klass):
print ('Builder already registered: "%s"' % name)
return

if (isinstance(klass, type(""))):
mod,name = klass.rsplit('.',1) # this seems dirty
exec ('import %s' % mod)
klass = eval(klass)
if isinstance(klass, str):
klass = make_class(klass)
builder = klass(name)
self.builders[name] = builder
return
Expand Down
12 changes: 6 additions & 6 deletions python/gegede/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os
import re
from collections import OrderedDict
from .util import make_class

def parse(filenames):
'''Parse configuration files.
Expand Down Expand Up @@ -40,6 +41,7 @@ def cfg2pod(cfg):
pod[secname] = secdat
return pod


interp_reobj = re.compile(r'{([\w:]+)}')
def interpolate_value(value, secname, sections):
def getter(match):
Expand All @@ -53,6 +55,7 @@ def getter(match):
ret = re.subn(interp_reobj, getter, value)
return ret[0]


def interpolate(sections):
'''Perform string interpolation values <sections>.
Expand All @@ -74,15 +77,12 @@ def interpolate(sections):
if changed:
continue
break



def make_class(fqclass):
mod, cls = fqclass.rsplit('.',1)
exec('import %s' % mod) # better way?
return eval(fqclass)

def make_value(v, **kwds):
'''
Return value of string.
'''
from . import Quantity
return eval(v, globals(), dict(Q=Quantity, **kwds))

Expand Down
7 changes: 4 additions & 3 deletions python/gegede/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
'''

from gegede.util import make_module

class Exporter(object):
'''
Wrapper around an exporter module which helps to enforce the conventions.
'''
def __init__(self, mod):
if isinstance(mod, type("")):
if isinstance(mod, str):
if not '.' in mod:
mod = 'gegede.export.' + mod
print ('Importing: "%s"' % mod)
exec('import %s' % mod)
mod = eval(mod)
mod = make_module(mod)
self.mod = mod

self.obj = None
Expand Down
17 changes: 17 additions & 0 deletions python/gegede/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'''
'''

from importlib import import_module
from gegede import Quantity

def wash_units(obj, lunit='cm', aunit='radian'):
Expand Down Expand Up @@ -71,3 +72,19 @@ def list_match(values, entry = None, deref = lambda x: x):
ret.append(v)
return list(ret)


def make_module(mod):
'''
Return a module object given pkg.mod string.
'''
return import_module(mod)


def make_class(fqclass):
'''
Return a class given pkg.mod.Class string.
'''
mod, cls = fqclass.rsplit('.',1)
mod = import_module(mod)
return getattr(mod, cls)

0 comments on commit f9cdfc0

Please sign in to comment.