Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
406 views
in Technique[技术] by (71.8m points)

python - Picklable data containers that are dumpable in the current namespace

The documentation suggests the following mechanism to dynamically create data containers in Python:

class Employee:
    pass

john = Employee() # Create an empty employee record

# Fill the fields of the record
john.name = 'John Doe'
john.dept = 'computer lab'
john.salary = 1000

The above allows one to easily group a diverse set of variables within one single identifier (john), without having to type quotes ('') as one would do with a dictionary.

I am looking for a solution that allows me to "dump" the pieces (the attributes) back into the current namespace. There are three ideas/problems that come to mind to address this:

1. Given the identifier above john, how can I programatically get a list of it's attributes?

2. How can I easily dump john's attributes in the current namespace? (i.e. create local variables called name, dept, salary either via shallow or deep copies)

3. The top answer in the following thread describes a way to dump variables from the namespace created by argparse: Importing variables from a namespace object in Python

Perhaps I could use a Namespace object as a data container, as in the above post, and then easily dump those variables with:

locals().update(vars(john))

?

For convenience, below I include a list of threads discussing other approaches for creating data containers in Python, some of which don't seem to be pickable:

Connection with MATLAB workflows:

For reference, MATLAB provides this exact functionality through save and load, and variables can be nested and unnested easily, eliminating the need for quotes/dictionaries for this purpose). The motivation behind this question is to identify mechanisms that support such "pickable workspaces" in Python.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • Given the identifier above john, how can I programatically get a list of it's attributes?

    vars(john)

Technically, this will give you a dictionary mapping. If you only want the list of attributes, then you actually will need vars(john).keys()

  • How can I easily dump john's attributes in the current namespace? (i.e. create local variables called name, dept, salary either via shallow or deep copies)

I'm not sure what you mean here about the shallow or deep copies. If you're talking about simple references, there is no (good) way to do this. If you're in the global (module level) namespace, you can do:

globals().update(vars(john))

If you're using CPython, using locals().update(vars(john)) works (in some places), but the documentation explicitly warns against doing this. The best you can do is some sort of exec loop (yuck!):

d = vars(john)
for k in john:
    exec '{key} = d["{key}"]'.format(key=k)

beware that there is a very good reason why this code is ugly -- mainly -- YOU SHOULDN'T BE DOING SOMETHING LIKE THIS :-P

and when using exec, the usual warnings apply -- Make sure you trust the attributes on john. e.g. setattr(john,'__import__("os").remove("useful_file"); foo',"anything here") would make for a pretty bad day ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...