<legend id='Kdhle'><style id='Kdhle'><dir id='Kdhle'><q id='Kdhle'></q></dir></style></legend>

    <i id='Kdhle'><tr id='Kdhle'><dt id='Kdhle'><q id='Kdhle'><span id='Kdhle'><b id='Kdhle'><form id='Kdhle'><ins id='Kdhle'></ins><ul id='Kdhle'></ul><sub id='Kdhle'></sub></form><legend id='Kdhle'></legend><bdo id='Kdhle'><pre id='Kdhle'><center id='Kdhle'></center></pre></bdo></b><th id='Kdhle'></th></span></q></dt></tr></i><div id='Kdhle'><tfoot id='Kdhle'></tfoot><dl id='Kdhle'><fieldset id='Kdhle'></fieldset></dl></div>
    • <bdo id='Kdhle'></bdo><ul id='Kdhle'></ul>

  • <small id='Kdhle'></small><noframes id='Kdhle'>

  • <tfoot id='Kdhle'></tfoot>

        IPython 将变量加载到工作区:你能想到比这更好的解决方案吗?

        IPython loading variables to workspace: can you think of a better solution than this?(IPython 将变量加载到工作区:你能想到比这更好的解决方案吗?)

            <bdo id='z01AX'></bdo><ul id='z01AX'></ul>

            <i id='z01AX'><tr id='z01AX'><dt id='z01AX'><q id='z01AX'><span id='z01AX'><b id='z01AX'><form id='z01AX'><ins id='z01AX'></ins><ul id='z01AX'></ul><sub id='z01AX'></sub></form><legend id='z01AX'></legend><bdo id='z01AX'><pre id='z01AX'><center id='z01AX'></center></pre></bdo></b><th id='z01AX'></th></span></q></dt></tr></i><div id='z01AX'><tfoot id='z01AX'></tfoot><dl id='z01AX'><fieldset id='z01AX'></fieldset></dl></div>

              1. <tfoot id='z01AX'></tfoot>

                <small id='z01AX'></small><noframes id='z01AX'>

                  <tbody id='z01AX'></tbody>
                <legend id='z01AX'><style id='z01AX'><dir id='z01AX'><q id='z01AX'></q></dir></style></legend>
                  本文介绍了IPython 将变量加载到工作区:你能想到比这更好的解决方案吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  I'm migrating from MATLAB to ipython and before taking the leap I'm going through my minimal workflow to make sure every operation I perform daily on MATLAB for data crunching is available on ipython.

                  I'm currently stuck on the very basic task of saving and loading numpy arrays via a one-line command, such as MATLAB's:

                  >>> save('myresults.mat','a','b','c')
                  >>> load('myresults.mat')
                  

                  In particular, what I like about MATLAB's load command is that not only it reads the data file but it loads the variables into the workspace, nothing else is needed to start working with them. Note that this is not the case with, for instance, numpy.load(), which requires another line to be able to assign the loaded values to the workspace variables. [ See: IPython: how to automagically load npz file and assign values to variables? ]

                  Based on the answers and comments to that question, I came up with this dirty-bad-engineering-ugly-coding-but-working solution. I know it's not pretty, and I would like to know if you can come up with the correct version of this [1].

                  I put this into iocustom.py:

                  def load(filename):
                      ip = get_ipython()
                      ip.ex("import numpy as np")
                      ip.ex("locals().update(np.load('" + filename + "'))") 
                  

                  so that I can run, from the ipython session:

                  import iocustom
                  load('myresults.npz')
                  

                  and the variables are dumped to the workspace.

                  I find it hard to believe there's nothing built-in equivalent to this, and it's even harder to think that that 3-line function is the optimal solution. I would be very grateful if you could please suggest a more correct way of doing this.

                  Please keep in mind that:

                  • I'm looking for a solution which would also work inside a script and a function.
                  • I know there's "pickle" but I refuse to use more than one line of code for something as mundane as a simple 'save' and/or 'load' command.
                  • I know there's "savemat" and "loadmat" available from scipy, but I would like to migrate completely, i.e., do not work with mat files but with numpy arrays.

                  Thanks in advance for all your help.

                  [1] BTW: how do people working with ipython save and load a set of numpy arrays easily? After hours of googling I cannot seem to find a simple and straightforward solution for this daily task.

                  解决方案

                  If I save this as load_on_run.py:

                  import argparse
                  import numpy as np
                  if __name__=='__main__':
                      parser = argparse.ArgumentParser()
                      parser.add_argument('-l','--list', help='list variables', action='store_true')
                      parser.add_argument('filename')
                      __args = parser.parse_args()
                      data = np.load(__args.filename)
                      locals().update(data)
                      del parser, data, argparse, np
                      if __args.list:
                          print([k for k in locals() if not k.startswith('__')])
                      del __args
                  

                  And then in ipython I can invoke it with %run:

                  In [384]: %run load_on_run testarrays.npz -l
                  ['array2', 'array3', 'array4', 'array1']
                  In [385]: array3
                  Out[385]: array([-10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2,  -1])
                  

                  It neatly loads the arrays from the file into the ipython workspace.

                  I'm taking advantage of the fact that magic %run runs a script, leaving all functions and variables defined by it in the main namespace. I haven't looked into how it does this.

                  The script just takes a few arguments, loads the file (so far only .npz), and uses the locals().update trick to put its variables into the local namespace. Then I clear out the unnecessary variables and modules, leaving only the newly loaded ones.

                  I could probably define an alias for %run load_on_run.

                  I can also imagine a script along these lines that lets you load variables with an import: from <script> import *.

                  这篇关于IPython 将变量加载到工作区:你能想到比这更好的解决方案吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Command line with variables in subprocess(子进程中带有变量的命令行)
                  Pandas - group by column and transform the data to numpy array(Pandas - 按列分组并将数据转换为 numpy 数组)
                  Reassign variable to original value (defined prior to the loop) at start of each iteration in loop(在循环中每次迭代开始时将变量重新分配给原始值(在循环之前定义))
                  Iterate over 36 million items in a list of tuples in python efficiently and faster(在 python 的元组列表中有效且更快地迭代超过 3600 万个项目)
                  shallow iteration with nditer(使用 nditer 进行浅迭代)
                  Assigning a variable NaN in python without numpy(在没有numpy的python中分配一个变量NaN)

                  <i id='2Nfpp'><tr id='2Nfpp'><dt id='2Nfpp'><q id='2Nfpp'><span id='2Nfpp'><b id='2Nfpp'><form id='2Nfpp'><ins id='2Nfpp'></ins><ul id='2Nfpp'></ul><sub id='2Nfpp'></sub></form><legend id='2Nfpp'></legend><bdo id='2Nfpp'><pre id='2Nfpp'><center id='2Nfpp'></center></pre></bdo></b><th id='2Nfpp'></th></span></q></dt></tr></i><div id='2Nfpp'><tfoot id='2Nfpp'></tfoot><dl id='2Nfpp'><fieldset id='2Nfpp'></fieldset></dl></div>

                  <small id='2Nfpp'></small><noframes id='2Nfpp'>

                  <tfoot id='2Nfpp'></tfoot><legend id='2Nfpp'><style id='2Nfpp'><dir id='2Nfpp'><q id='2Nfpp'></q></dir></style></legend>
                    <bdo id='2Nfpp'></bdo><ul id='2Nfpp'></ul>

                          <tbody id='2Nfpp'></tbody>