I’ve often seen people use Perl data structures in lieu of configuration files; i.e. a lone file containing only:
%config = ( 'color' => 'red', 'numbers' => [5, 8], qr/^spam/ => 'eggs' );
What’s the best way to convert the contents of these files into Python-equivalent data structures, using pure Python? For the time being we can assume that there are no real expressions to evaluate, only structured data.
Not sure what the use case is. Here’s my assumption: you’re going to do a one-time conversion from Perl to Python.
Perl has this
In Python, it would be
So, I’m guessing it’s a bunch of RE’s to replace
%variable = (withvariable = {);with}variable => valuewithvariable : valueqr/.../ =>withre.compile( r'...' ) : valueHowever, Python’s built-in
dictdoesn’t do anything unusual with a regex as a hash key. For that, you’d have to write your own subclass ofdict, and override__getitem__to check REGEX keys separately.Here’s the example of using a Perl-like dict.