Re: Python in Blender. Writing information to a file.

2019-08-09 Thread Cameron Simpson

On 09Aug2019 20:53, Paul St George  wrote:

I almost understand.
Are you saying I should change the first line of code to something 
like:


|outstream = with open(path to my file,'w') # this is invalid syntax|

and then delete the
outstream.close()


No, you should do what Peter wrote:

with open("/path/to/file", "w") as outstream:
  print(my_stuff, file=outstream)


Got it! I hadn't taken Peter's advice as code. I thought (well anyway now I 
have it). So thanks to Peter, Cameron and
Rhodri.


No worries. You should also got and look up "context manager" in the 
Python docs.


It is a general mechanism where you can write:

 with something as name:
   suite of code

where "something" is an object which implements a context manager.  
Before "suite of code" starts Python calls "something.__enter__" and 
when the interpreter leaves the suite Python calls "something.__exit__".


Notably, it will call __exit__ no matter how you leave the suite: as 
usual, by falling off the end, or early with an exception or a break or 
a continue or a return. The __exit__ function always gets called.


In the case Peter's example, "something" is "open()": the file object 
you get back from open() implements a context manager. Its __enter__ 
function does nothing, but its __exit__ function closes the file.  
Reliably and immediately.


Cheers, Cameron Simpson  (formerly c...@zip.com.au)

I swear to god, officer, I'm fixing this bridge. Just go divert traffic.
--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Python in Blender. Writing information to a file.

2019-08-09 Thread Paul St George


On 09/08/2019 15:59, Rhodri James wrote:

On 09/08/2019 14:54, Paul St George wrote:


On 09/08/2019 04:09, Cameron Simpson wrote:

On 08Aug2019 22:42, Paul St George  wrote:

On 08/08/2019 10:18, Peter Otten wrote:

The print() function has a keyword-only file argument. So:
with open(..., "w") as outstream:
    print("Focal length:", bpy.context.object.data.lens, 
file=outstream)

[...]



That worked perfectly.

outstream = open(path to my file,'w')
print(
whatever I want to print
file=outstream
)
outstream.close()


I just wanted to point out Peter's use of the open context manager:

   with open(..., "w") as outstream:
 print stuff ...

You'll notice that it does not overtly close the file. The file 
object returned from open() is a context manager, the "with" 
statement arranges to close the file when your programme exits the 
with suite.


Importantly, the close will happen even if the code inside raises an 
exception, which in your "open..print..close" sequence would not 
reach the close call.


So we recommend the "with" form of open() above.

There are circumstances where it isn't useful, but they are very rare.

Cheers,
Cameron Simpson 


I almost understand.

Are you saying I should change the first line of code to something like:

|outstream = with open(path to my file,'w') # this is invalid syntax|

and then delete the

outstream.close()


No, you should do what Peter wrote:

with open("/path/to/file", "w") as outstream:
  print(my_stuff, file=outstream)


Got it! I hadn't taken Peter's advice as code. I thought (well anyway now I 
have it). So thanks to Peter, Cameron and
Rhodri.

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python in Blender. Writing information to a file.

2019-08-09 Thread Rhodri James

On 09/08/2019 14:54, Paul St George wrote:


On 09/08/2019 04:09, Cameron Simpson wrote:

On 08Aug2019 22:42, Paul St George  wrote:

On 08/08/2019 10:18, Peter Otten wrote:

The print() function has a keyword-only file argument. So:
with open(..., "w") as outstream:
    print("Focal length:", bpy.context.object.data.lens, 
file=outstream)

[...]



That worked perfectly.

outstream = open(path to my file,'w')
print(
whatever I want to print
file=outstream
)
outstream.close()


I just wanted to point out Peter's use of the open context manager:

   with open(..., "w") as outstream:
 print stuff ...

You'll notice that it does not overtly close the file. The file object 
returned from open() is a context manager, the "with" statement 
arranges to close the file when your programme exits the with suite.


Importantly, the close will happen even if the code inside raises an 
exception, which in your "open..print..close" sequence would not reach 
the close call.


So we recommend the "with" form of open() above.

There are circumstances where it isn't useful, but they are very rare.

Cheers,
Cameron Simpson 


I almost understand.

Are you saying I should change the first line of code to something like:

|outstream = with open(path to my file,'w') # this is invalid syntax|

and then delete the

outstream.close()


No, you should do what Peter wrote:

with open("/path/to/file", "w") as outstream:
  print(my_stuff, file=outstream)

--
Rhodri James *-* Kynesim Ltd
--
https://mail.python.org/mailman/listinfo/python-list


Re: Python in Blender. Writing information to a file.

2019-08-09 Thread Paul St George


On 09/08/2019 04:09, Cameron Simpson wrote:

On 08Aug2019 22:42, Paul St George  wrote:

On 08/08/2019 10:18, Peter Otten wrote:

The print() function has a keyword-only file argument. So:
with open(..., "w") as outstream:
    print("Focal length:", bpy.context.object.data.lens, 
file=outstream)

[...]



That worked perfectly.

outstream = open(path to my file,'w')
print(
whatever I want to print
file=outstream
)
outstream.close()


I just wanted to point out Peter's use of the open context manager:

   with open(..., "w") as outstream:
 print stuff ...

You'll notice that it does not overtly close the file. The file object 
returned from open() is a context manager, the "with" statement 
arranges to close the file when your programme exits the with suite.


Importantly, the close will happen even if the code inside raises an 
exception, which in your "open..print..close" sequence would not reach 
the close call.


So we recommend the "with" form of open() above.

There are circumstances where it isn't useful, but they are very rare.

Cheers,
Cameron Simpson 


I almost understand.

Are you saying I should change the first line of code to something like:

|outstream = with open(path to my file,'w') # this is invalid syntax|

and then delete the

outstream.close()

--
https://mail.python.org/mailman/listinfo/python-list


Re: Python in Blender. Writing information to a file.

2019-08-08 Thread Cameron Simpson

On 08Aug2019 22:42, Paul St George  wrote:

On 08/08/2019 10:18, Peter Otten wrote:

The print() function has a keyword-only file argument. So:
with open(..., "w") as outstream:
print("Focal length:", bpy.context.object.data.lens, file=outstream)

[...]



That worked perfectly.

outstream = open(path to my file,'w')
print(
whatever I want to print
file=outstream
)
outstream.close()


I just wanted to point out Peter's use of the open context manager:

   with open(..., "w") as outstream:
 print stuff ...

You'll notice that it does not overtly close the file. The file object 
returned from open() is a context manager, the "with" statement arranges 
to close the file when your programme exits the with suite.


Importantly, the close will happen even if the code inside raises an 
exception, which in your "open..print..close" sequence would not reach 
the close call.


So we recommend the "with" form of open() above.

There are circumstances where it isn't useful, but they are very rare.

Cheers,
Cameron Simpson 
--
https://mail.python.org/mailman/listinfo/python-list


Re: Re: Python in Blender. Writing information to a file.

2019-08-08 Thread Paul St George


On 08/08/2019 10:18, Peter Otten wrote:

Paul St George wrote:


I am using Python 3.5 within Blender. I want to collect values of the
current settings and then write all the results to a file.

I can see the settings and the values in the Python console by doing
this for each of the settings
|
|

|print(“Focal length:”,bpy.context.object.data.lens)|

---Focal length: 35.0


or I can do many at a time like this:

|print("Plane rotation
X:",bpy.data.objects["Plane"].rotation_euler[0],"\nPlane rotation
Y:",bpy.data.objects["Plane"].rotation_euler[1],"\nPlane rotation
Z:",bpy.data.objects["Plane"].rotation_euler[2])|

---Plane rotation X: 0.0
---Plane rotation Y: 0.0
---Plane rotation Z: 0.0


My question:
How do I write all the results to a file? I have tried file.write but
can only write one argument at a time. Is there a better way to open a
file, write the collected information to it and then close the file?


The print() function has a keyword-only file argument. So:

with open(..., "w") as outstream:
 print("Focal length:", bpy.context.object.data.lens, file=outstream)


|print("Plane rotation
X:",bpy.data.objects["Plane"].rotation_euler[0],"\nPlane rotation
Y:",bpy.data.objects["Plane"].rotation_euler[1],"\nPlane rotation
Z:",bpy.data.objects["Plane"].rotation_euler[2])|

This looks messy to me. I' probably use intermediate variables

x, y, z = bpy.data.objects["Plane"].rotation_euler
print(
 "Plane rotation X:", x,
 "Plane rotation Y:", y,
 "Plane rotation Z:", z,
 file=outstream, sep="\n"
)

or even a loop.



That worked perfectly.

outstream = open(path to my file,'w')
print(
whatever I want to print
file=outstream
)
outstream.close()



--
https://mail.python.org/mailman/listinfo/python-list


Re: Python in Blender. Writing information to a file.

2019-08-08 Thread Peter Otten
Paul St George wrote:

> I am using Python 3.5 within Blender. I want to collect values of the
> current settings and then write all the results to a file.
> 
> I can see the settings and the values in the Python console by doing
> this for each of the settings
> |
> |
> 
> |print(“Focal length:”,bpy.context.object.data.lens)|
> 
> ---Focal length: 35.0
> 
> 
> or I can do many at a time like this:
> 
> |print("Plane rotation
> X:",bpy.data.objects["Plane"].rotation_euler[0],"\nPlane rotation
> Y:",bpy.data.objects["Plane"].rotation_euler[1],"\nPlane rotation
> Z:",bpy.data.objects["Plane"].rotation_euler[2])|
> 
> ---Plane rotation X: 0.0
> ---Plane rotation Y: 0.0
> ---Plane rotation Z: 0.0
> 
> 
> My question:
> How do I write all the results to a file? I have tried file.write but
> can only write one argument at a time. Is there a better way to open a
> file, write the collected information to it and then close the file?
> 

The print() function has a keyword-only file argument. So:

with open(..., "w") as outstream:
print("Focal length:", bpy.context.object.data.lens, file=outstream)

> |print("Plane rotation
> X:",bpy.data.objects["Plane"].rotation_euler[0],"\nPlane rotation
> Y:",bpy.data.objects["Plane"].rotation_euler[1],"\nPlane rotation
> Z:",bpy.data.objects["Plane"].rotation_euler[2])|

This looks messy to me. I' probably use intermediate variables

x, y, z = bpy.data.objects["Plane"].rotation_euler
print(
"Plane rotation X:", x,
"Plane rotation Y:", y,
"Plane rotation Z:", z,
file=outstream, sep="\n"
)

or even a loop.

-- 
https://mail.python.org/mailman/listinfo/python-list


Python in Blender. Writing information to a file.

2019-08-08 Thread Paul St George
I am using Python 3.5 within Blender. I want to collect values of the 
current settings and then write all the results to a file.


I can see the settings and the values in the Python console by doing 
this for each of the settings

|
|

|print(“Focal length:”,bpy.context.object.data.lens)|

---Focal length: 35.0


or I can do many at a time like this:

|print("Plane rotation 
X:",bpy.data.objects["Plane"].rotation_euler[0],"\nPlane rotation 
Y:",bpy.data.objects["Plane"].rotation_euler[1],"\nPlane rotation 
Z:",bpy.data.objects["Plane"].rotation_euler[2])|


---Plane rotation X: 0.0
---Plane rotation Y: 0.0
---Plane rotation Z: 0.0


My question:
How do I write all the results to a file? I have tried file.write but 
can only write one argument at a time. Is there a better way to open a 
file, write the collected information to it and then close the file?


--
https://mail.python.org/mailman/listinfo/python-list