ColwynGulliford / distgen

Particle distribution generator
https://colwyngulliford.github.io/distgen/
Apache License 2.0
12 stars 8 forks source link

open file / resource warning #2

Closed nneveu closed 3 years ago

nneveu commented 3 years ago

When running distgen within a unit test, I get this warning:

../Users/nneveu/github/distgen/distgen/generator.py:58: ResourceWarning: unclosed file <_io.TextIOWrapper name='../slac/paper_test/tdist.yaml' mode='r' encoding='UTF-8'> input = yaml.safe_load(open(input))

I know this is just a warning, but may be worth closing the file to be safe? Here's the specific code triggering the warning:

        dist = Generator(input=self.distgen_file)#, verbose=True)
        dist.input['r_dist']['max_r']['value'] = radius #mm based on scaling in call script 
        dist.run()
        particles = dist.particles
        particles.write_opal('opal_emitted.txt', dist_type= 'emitted')
ColwynGulliford commented 3 years ago

Thank you for pointing his out. All open statements are now use the 'with' keyword, which should handle file closing.