PyYaml - overwrite yaml file and save it

Visiting the UK as unmarried couple

Flux received by a negative charge

why `nmap 192.168.1.97` returns less services than `nmap 127.0.0.1`?

Could the E-bike drivetrain wear down till needing replacement after 400 km?

Create all possible words using a set or letters

Can I use my Chinese passport to enter China after I acquired another citizenship?

When quoting, must I also copy hyphens used to divide words that continue on the next line?

Drawing a topological "handle" with Tikz

What (else) happened July 1st 1858 in London?

Should I stop contributing to retirement accounts?

Extending the spectral theorem for bounded self adjoint operators to bounded normal operators

Engineer refusing to file/disclose patents

Do the concepts of IP address and network interface not belong to the same layer?

A social experiment. What is the worst that can happen?

How does the reference system of the Majjhima Nikaya work?

Customize circled numbers

Global amount of publications over time

Can someone explain how this makes sense electrically?

Why we can't differentiate a polynomial equation as many times as we wish?

How do I repair my stair bannister?

Can we have a perfect cadence in a minor key?

Folder comparison

What is the gram­mat­i­cal term for “‑ed” words like these?

We have a love-hate relationship



PyYaml - overwrite yaml file and save it














0












$begingroup$


what I am trying is to load a yaml file and update its values by argparse and save the resulted yaml in another path.



the output of yaml.save_load is:



import os
import argparse
import yaml
import functools

parser = argparse.ArgumentParser()
parser.add_argument(
"--config",
nargs="?",
type=str,
default="p.yml",
help="Configuration file to use",
)
parser.add_argument('--learning_rate', type=float, help='learning rate amount')

args = parser.parse_args()


def dumps(func):
@functools.wraps(func)
def wrapper(self, *args, **kwargs):
ret = func(self, *args, **kwargs)
with open(self.filename, "w") as f:
yaml.safe_dump(self, f)
return ret
return wrapper

class Config(dict):
def __init__(self, filename):
self.filename = filename

if os.path.isfile(filename):
print(filename)
with open(filename) as f:
# use super here to avoid unnecessary write
super(Config, self).update(yaml.safe_load(f) or )

__setitem__ = dumps(dict.__setitem__)
__delitem__ = dumps(dict.__delitem__)
update = dumps(dict.update)



cfg = Config(args.config)
print(cfg)
cfg.update('training': 'optimizer': 'lr': args.learning_rate)
print(cfg)


the original yaml file has the following configurations:



> data:
dataset: pascal
train_split: train
val_split: val
img_rows: 'same'
img_cols: 'same'
path: SkyScapes/
sbd_path: /private/home/meetshah/datasets/VOC/benchmark_RELEASE/
training:
train_iters: 300000
batch_size: 1
val_interval: 1000
n_workers: 16
print_interval: 50
optimizer:
name: 'sgd'
lr: 1.0e-10
weight_decay: 0.0005
momentum: 0.99
loss:
name: 'cross_entropy'
size_average: False
lr_schedule:
resume: fcn8s_pascal_best_model.pkl


and I am going to update lr parameter. However, I am facing with the following error:



> 'data': 'dataset': 'pascal', 'img_cols': 'same', 'img_rows': 'same', 'sbd_path': '/private/home/meetshah/datasets/VOC/benchmark_RELEASE/', 'train_split': 'train', 'val_split': 'val', 'path': 'SkyScapes/', 'training': 'resume': 'fcn8s_pascal_best_model.pkl', 'train_iters': 300000, 'lr_schedule': None, 'optimizer': 'name': 'sgd', 'weight_decay': 0.0005, 'lr': 1e-10, 'momentum': 0.99, 'print_interval': 50, 'n_workers': 16, 'batch_size': 1, 'loss': 'name': 'cross_entropy', 'size_average': False, 'val_interval': 1000
Traceback (most recent call last):
File "test.py", line 70, in <module>
cfg.update('training': 'optimizer': 'lr': args.learning_rate)
File "test.py", line 48, in wrapper
yaml.safe_dump(self, f)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 306, in safe_dump
return dump_all([data], stream, Dumper=SafeDumper, **kwds)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 278, in dump_all
dumper.represent(data)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 27, in represent
node = self.represent_data(data)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 58, in represent_data
node = self.yaml_representers[None](self, data)
File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 231, in represent_undefined
raise RepresenterError("cannot represent an object", data)
yaml.representer.RepresenterError: ('cannot represent an object', 'data': 'dataset': 'pascal', 'img_cols': 'same', 'img_rows': 'same', 'sbd_path': '/private/home/meetshah/datasets/VOC/benchmark_RELEASE/', 'train_split': 'train', 'val_split': 'val', 'path': 'SkyScapes/', 'training': 'optimizer': 'lr': 2.0)








share







New contributor




Majid Azimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$
















    0












    $begingroup$


    what I am trying is to load a yaml file and update its values by argparse and save the resulted yaml in another path.



    the output of yaml.save_load is:



    import os
    import argparse
    import yaml
    import functools

    parser = argparse.ArgumentParser()
    parser.add_argument(
    "--config",
    nargs="?",
    type=str,
    default="p.yml",
    help="Configuration file to use",
    )
    parser.add_argument('--learning_rate', type=float, help='learning rate amount')

    args = parser.parse_args()


    def dumps(func):
    @functools.wraps(func)
    def wrapper(self, *args, **kwargs):
    ret = func(self, *args, **kwargs)
    with open(self.filename, "w") as f:
    yaml.safe_dump(self, f)
    return ret
    return wrapper

    class Config(dict):
    def __init__(self, filename):
    self.filename = filename

    if os.path.isfile(filename):
    print(filename)
    with open(filename) as f:
    # use super here to avoid unnecessary write
    super(Config, self).update(yaml.safe_load(f) or )

    __setitem__ = dumps(dict.__setitem__)
    __delitem__ = dumps(dict.__delitem__)
    update = dumps(dict.update)



    cfg = Config(args.config)
    print(cfg)
    cfg.update('training': 'optimizer': 'lr': args.learning_rate)
    print(cfg)


    the original yaml file has the following configurations:



    > data:
    dataset: pascal
    train_split: train
    val_split: val
    img_rows: 'same'
    img_cols: 'same'
    path: SkyScapes/
    sbd_path: /private/home/meetshah/datasets/VOC/benchmark_RELEASE/
    training:
    train_iters: 300000
    batch_size: 1
    val_interval: 1000
    n_workers: 16
    print_interval: 50
    optimizer:
    name: 'sgd'
    lr: 1.0e-10
    weight_decay: 0.0005
    momentum: 0.99
    loss:
    name: 'cross_entropy'
    size_average: False
    lr_schedule:
    resume: fcn8s_pascal_best_model.pkl


    and I am going to update lr parameter. However, I am facing with the following error:



    > 'data': 'dataset': 'pascal', 'img_cols': 'same', 'img_rows': 'same', 'sbd_path': '/private/home/meetshah/datasets/VOC/benchmark_RELEASE/', 'train_split': 'train', 'val_split': 'val', 'path': 'SkyScapes/', 'training': 'resume': 'fcn8s_pascal_best_model.pkl', 'train_iters': 300000, 'lr_schedule': None, 'optimizer': 'name': 'sgd', 'weight_decay': 0.0005, 'lr': 1e-10, 'momentum': 0.99, 'print_interval': 50, 'n_workers': 16, 'batch_size': 1, 'loss': 'name': 'cross_entropy', 'size_average': False, 'val_interval': 1000
    Traceback (most recent call last):
    File "test.py", line 70, in <module>
    cfg.update('training': 'optimizer': 'lr': args.learning_rate)
    File "test.py", line 48, in wrapper
    yaml.safe_dump(self, f)
    File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 306, in safe_dump
    return dump_all([data], stream, Dumper=SafeDumper, **kwds)
    File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 278, in dump_all
    dumper.represent(data)
    File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 27, in represent
    node = self.represent_data(data)
    File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 58, in represent_data
    node = self.yaml_representers[None](self, data)
    File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 231, in represent_undefined
    raise RepresenterError("cannot represent an object", data)
    yaml.representer.RepresenterError: ('cannot represent an object', 'data': 'dataset': 'pascal', 'img_cols': 'same', 'img_rows': 'same', 'sbd_path': '/private/home/meetshah/datasets/VOC/benchmark_RELEASE/', 'train_split': 'train', 'val_split': 'val', 'path': 'SkyScapes/', 'training': 'optimizer': 'lr': 2.0)








    share







    New contributor




    Majid Azimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      0












      0








      0





      $begingroup$


      what I am trying is to load a yaml file and update its values by argparse and save the resulted yaml in another path.



      the output of yaml.save_load is:



      import os
      import argparse
      import yaml
      import functools

      parser = argparse.ArgumentParser()
      parser.add_argument(
      "--config",
      nargs="?",
      type=str,
      default="p.yml",
      help="Configuration file to use",
      )
      parser.add_argument('--learning_rate', type=float, help='learning rate amount')

      args = parser.parse_args()


      def dumps(func):
      @functools.wraps(func)
      def wrapper(self, *args, **kwargs):
      ret = func(self, *args, **kwargs)
      with open(self.filename, "w") as f:
      yaml.safe_dump(self, f)
      return ret
      return wrapper

      class Config(dict):
      def __init__(self, filename):
      self.filename = filename

      if os.path.isfile(filename):
      print(filename)
      with open(filename) as f:
      # use super here to avoid unnecessary write
      super(Config, self).update(yaml.safe_load(f) or )

      __setitem__ = dumps(dict.__setitem__)
      __delitem__ = dumps(dict.__delitem__)
      update = dumps(dict.update)



      cfg = Config(args.config)
      print(cfg)
      cfg.update('training': 'optimizer': 'lr': args.learning_rate)
      print(cfg)


      the original yaml file has the following configurations:



      > data:
      dataset: pascal
      train_split: train
      val_split: val
      img_rows: 'same'
      img_cols: 'same'
      path: SkyScapes/
      sbd_path: /private/home/meetshah/datasets/VOC/benchmark_RELEASE/
      training:
      train_iters: 300000
      batch_size: 1
      val_interval: 1000
      n_workers: 16
      print_interval: 50
      optimizer:
      name: 'sgd'
      lr: 1.0e-10
      weight_decay: 0.0005
      momentum: 0.99
      loss:
      name: 'cross_entropy'
      size_average: False
      lr_schedule:
      resume: fcn8s_pascal_best_model.pkl


      and I am going to update lr parameter. However, I am facing with the following error:



      > 'data': 'dataset': 'pascal', 'img_cols': 'same', 'img_rows': 'same', 'sbd_path': '/private/home/meetshah/datasets/VOC/benchmark_RELEASE/', 'train_split': 'train', 'val_split': 'val', 'path': 'SkyScapes/', 'training': 'resume': 'fcn8s_pascal_best_model.pkl', 'train_iters': 300000, 'lr_schedule': None, 'optimizer': 'name': 'sgd', 'weight_decay': 0.0005, 'lr': 1e-10, 'momentum': 0.99, 'print_interval': 50, 'n_workers': 16, 'batch_size': 1, 'loss': 'name': 'cross_entropy', 'size_average': False, 'val_interval': 1000
      Traceback (most recent call last):
      File "test.py", line 70, in <module>
      cfg.update('training': 'optimizer': 'lr': args.learning_rate)
      File "test.py", line 48, in wrapper
      yaml.safe_dump(self, f)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 306, in safe_dump
      return dump_all([data], stream, Dumper=SafeDumper, **kwds)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 278, in dump_all
      dumper.represent(data)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 27, in represent
      node = self.represent_data(data)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 58, in represent_data
      node = self.yaml_representers[None](self, data)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 231, in represent_undefined
      raise RepresenterError("cannot represent an object", data)
      yaml.representer.RepresenterError: ('cannot represent an object', 'data': 'dataset': 'pascal', 'img_cols': 'same', 'img_rows': 'same', 'sbd_path': '/private/home/meetshah/datasets/VOC/benchmark_RELEASE/', 'train_split': 'train', 'val_split': 'val', 'path': 'SkyScapes/', 'training': 'optimizer': 'lr': 2.0)








      share







      New contributor




      Majid Azimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      what I am trying is to load a yaml file and update its values by argparse and save the resulted yaml in another path.



      the output of yaml.save_load is:



      import os
      import argparse
      import yaml
      import functools

      parser = argparse.ArgumentParser()
      parser.add_argument(
      "--config",
      nargs="?",
      type=str,
      default="p.yml",
      help="Configuration file to use",
      )
      parser.add_argument('--learning_rate', type=float, help='learning rate amount')

      args = parser.parse_args()


      def dumps(func):
      @functools.wraps(func)
      def wrapper(self, *args, **kwargs):
      ret = func(self, *args, **kwargs)
      with open(self.filename, "w") as f:
      yaml.safe_dump(self, f)
      return ret
      return wrapper

      class Config(dict):
      def __init__(self, filename):
      self.filename = filename

      if os.path.isfile(filename):
      print(filename)
      with open(filename) as f:
      # use super here to avoid unnecessary write
      super(Config, self).update(yaml.safe_load(f) or )

      __setitem__ = dumps(dict.__setitem__)
      __delitem__ = dumps(dict.__delitem__)
      update = dumps(dict.update)



      cfg = Config(args.config)
      print(cfg)
      cfg.update('training': 'optimizer': 'lr': args.learning_rate)
      print(cfg)


      the original yaml file has the following configurations:



      > data:
      dataset: pascal
      train_split: train
      val_split: val
      img_rows: 'same'
      img_cols: 'same'
      path: SkyScapes/
      sbd_path: /private/home/meetshah/datasets/VOC/benchmark_RELEASE/
      training:
      train_iters: 300000
      batch_size: 1
      val_interval: 1000
      n_workers: 16
      print_interval: 50
      optimizer:
      name: 'sgd'
      lr: 1.0e-10
      weight_decay: 0.0005
      momentum: 0.99
      loss:
      name: 'cross_entropy'
      size_average: False
      lr_schedule:
      resume: fcn8s_pascal_best_model.pkl


      and I am going to update lr parameter. However, I am facing with the following error:



      > 'data': 'dataset': 'pascal', 'img_cols': 'same', 'img_rows': 'same', 'sbd_path': '/private/home/meetshah/datasets/VOC/benchmark_RELEASE/', 'train_split': 'train', 'val_split': 'val', 'path': 'SkyScapes/', 'training': 'resume': 'fcn8s_pascal_best_model.pkl', 'train_iters': 300000, 'lr_schedule': None, 'optimizer': 'name': 'sgd', 'weight_decay': 0.0005, 'lr': 1e-10, 'momentum': 0.99, 'print_interval': 50, 'n_workers': 16, 'batch_size': 1, 'loss': 'name': 'cross_entropy', 'size_average': False, 'val_interval': 1000
      Traceback (most recent call last):
      File "test.py", line 70, in <module>
      cfg.update('training': 'optimizer': 'lr': args.learning_rate)
      File "test.py", line 48, in wrapper
      yaml.safe_dump(self, f)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 306, in safe_dump
      return dump_all([data], stream, Dumper=SafeDumper, **kwds)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/__init__.py", line 278, in dump_all
      dumper.represent(data)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 27, in represent
      node = self.represent_data(data)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 58, in represent_data
      node = self.yaml_representers[None](self, data)
      File "/home/majid/.virtualenvs/pytorch-py3/lib/python3.5/site-packages/yaml/representer.py", line 231, in represent_undefined
      raise RepresenterError("cannot represent an object", data)
      yaml.representer.RepresenterError: ('cannot represent an object', 'data': 'dataset': 'pascal', 'img_cols': 'same', 'img_rows': 'same', 'sbd_path': '/private/home/meetshah/datasets/VOC/benchmark_RELEASE/', 'train_split': 'train', 'val_split': 'val', 'path': 'SkyScapes/', 'training': 'optimizer': 'lr': 2.0)






      python yaml





      share







      New contributor




      Majid Azimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Majid Azimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Majid Azimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 mins ago









      Majid AzimiMajid Azimi

      1011




      1011




      New contributor




      Majid Azimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Majid Azimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Majid Azimi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Majid Azimi is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216129%2fpyyaml-overwrite-yaml-file-and-save-it%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Majid Azimi is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          Majid Azimi is a new contributor. Be nice, and check out our Code of Conduct.












          Majid Azimi is a new contributor. Be nice, and check out our Code of Conduct.











          Majid Azimi is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216129%2fpyyaml-overwrite-yaml-file-and-save-it%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          瀋陽號驅逐艦 目录 接收與服役 配置反潛直升機 武進三型性能升級 歷史 除役 參考資料 外部連結 导航菜单Taiwan Air Power海疆老兵-陽字號驅逐艦沿革World Navies Today: Taiwan (Republic of China)DD-839 USS POWER

          Memorizing the KeyboardThe Norwegian Foreman''If the B…''The Consonant EaterThe Cherry TreeElle Rend Le Coeur Plus AmoureuxFill in the blanks with the number in wordsState of the UnionFind the missing elementsCircuit DiagramWhat's the name of the game show?

          名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局