Meanwhile Norwegian trolls created ...

2019-08-17 Thread Abdur-Rahmaan Janhangeer
Was browsing when i came across this hilarious piece of text:

*Once upon a time there was a programming language called C. And it had 2
memory management functions: malloc() and free(). But it was too
complicated. Bjarne Stroustrup decided that C memory management should be
easier. So he invented C++. In addition to malloc() and free(), C++ had
new, delete, destructors, RAII, auto and shared pointers. Guido van Rossum
found that C++ was also not simple enough. He chose another way and
invented Python - a language which doesn’t have even malloc() or free().
Meanwhile Norwegian trolls created the C++ GUI library Qt. It simplifies
memory management by deleting objects automatically when it thinks the
objects are not needed. A man called Phil Thompson was upset that a cool
library like Qt doesn’t exist in the excellent Python language. He combined
them in the PyQt project. But it is not so easy to combine different memory
management paradigms. Let’s see what the pitfalls are. (Text above is a
fairy tale. Text below contains code and technical information)*

source: http://enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html

Yours,

Abdur-Rahmaan Janhangeer
pythonmembers.club  | github

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


Re: absolute path to a file

2019-08-17 Thread Paul St George

On 17/08/2019 01:07, Gregory Ewing wrote:
On Sat, Aug 17, 2019 at 2:27 AM Paul St George 
 wrote:



BUT does not work with
| print('test2:',os.path.realpath(n.image.filepath))|

This returns only
|/image01.tif|


What does n.image.filepath look like on its own? If it starts
with a leading slash, then os.path.realpath will think it's
already an absolute path and leave it alone.

The problem then is why it's getting a leading slash in the
first place. It looks like the result of something naively
trying to append a filename to an empty directory path.


Gregory,
Thank you for this. You ask what n.image.filepath looks like on its own. 
Here (below) are some tests and the results: You are right about the 
leading slashes. I have tried to remove them and then use 
os.path.realpath and os.path.abspath but it is as if the slash is still 
there. Perhaps there is a better way to remove the slash - perhaps by 
getting the name of the file. Then I could find the true path to the 
directory that contains the file, and finally join the two together.


But there must be a simpler way?


 print('Track A:',n.image.filepath)
---Track A: //image01.tif

print('Track B:',n.image.filepath[1:])
---Track B: /image01.tif

 print('Track C:',n.image.filepath[2:])
---Track C: image01.tif

 print('Track D  from Track B:',os.path.realpath(n.image.filepath))
---Track D  from Track B: /image01.tif

 print('Track E  from Track B:',os.path.realpath(n.image.filepath[1:]))
---Track E  from Track B: /image01.tif

 print('Track F from Track C:',os.path.realpath(n.image.filepath[2:]))
---Track F from Track C: /image01.tif

 print('Track G from Track C:',os.path.abspath(n.image.filepath[2:]))
---Track G from Track C: /image01.tif

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


Re: Meanwhile Norwegian trolls created ...

2019-08-17 Thread Pankaj Jangid
Abdur-Rahmaan Janhangeer  writes:

> Was browsing when i came across this hilarious piece of text:
>
> source: http://enki-editor.org/2014/08/23/Pyqt_mem_mgmt.html
>
I haven't searched for it though, but I guess if there is a Qt like
framework in Rust then a Python wrapper around it would behave more
meaningfully.

-- 
Pankaj
Planet Earth. 
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: absolute path to a file

2019-08-17 Thread Manfred Lotz
Hi Paul,
Here an example how I used both functions

https://gitlab.com/snippets/1886520

Hope this helps.

-- 
Manfred




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


Re: absolute path to a file

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

> Can someone please tell me how to get the absolute path to a file? I
> have tried os.path.abspath. In the code below I have a problem in the
> final line (15).
> 
> #
> |import bpy||

Is this blender? If so the "//" prefix starts making sense:

https://docs.blender.org/api/current/bpy.path.html

I'd try bpy.path.abspath() instead of os.pathabspath().


> ||import os||
> ||
> ||texture_list = []||
> ||
> ||with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as
> outstream:||
> ||
> ||
> ||for obj in bpy.context.scene.objects:||
> ||for s in obj.material_slots:||
> ||if s.material and s.material.use_nodes:||
> ||for n in s.material.node_tree.nodes:||
> ||if n.type == 'TEX_IMAGE':||
> ||texture_list += [n.image]||
> ||print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which
> is at', os.path.abspath(n.image.filepath), file=outstream)||
> |#
> 
> This gives me:
> ---Plane uses image01.tif saved at //image01.tif which is at //image01.tif
> ---Plane uses image02.tif saved at //../images/image02.tif which is at
> //images/image02.tif
> 
> But I want an absolute path such as:
> ---Plane uses image01.tif saved at /Users/Lion/Desktop/test8/image01.tif
> ---Plane uses image02.tif saved at /Users/Lion/Desktop/images/image02.tif
> 
> If it is relevant, my files are on a Mac. Hence the escaped forward slash.
> 


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


Re: absolute path to a file

2019-08-17 Thread Paul St George

On 17/08/2019 15:37, Peter Otten wrote:

Paul St George wrote:


Can someone please tell me how to get the absolute path to a file? I
have tried os.path.abspath. In the code below I have a problem in the
final line (15).

#
|import bpy||


Is this blender? If so the "//" prefix starts making sense:

https://docs.blender.org/api/current/bpy.path.html

I'd try bpy.path.abspath() instead of os.pathabspath().



||import os||
||
||texture_list = []||
||
||with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as
outstream:||
||
||
||for obj in bpy.context.scene.objects:||
||for s in obj.material_slots:||
||if s.material and s.material.use_nodes:||
||for n in s.material.node_tree.nodes:||
||if n.type == 'TEX_IMAGE':||
||texture_list += [n.image]||
||print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which
is at', os.path.abspath(n.image.filepath), file=outstream)||
|#

This gives me:
---Plane uses image01.tif saved at //image01.tif which is at //image01.tif
---Plane uses image02.tif saved at //../images/image02.tif which is at
//images/image02.tif

But I want an absolute path such as:
---Plane uses image01.tif saved at /Users/Lion/Desktop/test8/image01.tif
---Plane uses image02.tif saved at /Users/Lion/Desktop/images/image02.tif

If it is relevant, my files are on a Mac. Hence the escaped forward slash.





Yes, it is Blender and the bpy.path.abspath() works!
And thank you also for the link to the docs. They say:
Returns the absolute path relative to the current blend file using the 
“//” prefix.


So does Blender have its own Python???

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


Re: absolute path to a file

2019-08-17 Thread Paul St George

On 17/08/2019 16:32, Dennis Lee Bieber wrote:

On Sat, 17 Aug 2019 11:51:47 +0200, Paul St George 
declaimed the following:



  print('Track E  from Track B:',os.path.realpath(n.image.filepath[1:]))
---Track E  from Track B: /image01.tif



Just for giggles, what happens if you preface that with a single
period...

print('Track E  from Track B:',os.path.realpath("." +
n.image.filepath[1:]))





print('Track E for giggles from Track B:',os.path.realpath("." + 
n.image.filepath[1:]))


gives:

Track E for giggles from Track B: /images/blah.tif

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


Re: absolute path to a file

2019-08-17 Thread Richard Damon
On 8/17/19 10:21 AM, Paul St George wrote:
> Yes, it is Blender and the bpy.path.abspath() works!
> And thank you also for the link to the docs. They say:
> Returns the absolute path relative to the current blend file using the
> “//” prefix.
>
> So does Blender have its own Python???

I don't think it has its own Python, but it sounds like it sort of
extends the file system, and uses a leading // for something special.
That means files that begin with // need to be processes through the
blender library and not be used directly to the OS.

-- 
Richard Damon

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


Re: absolute path to a file

2019-08-17 Thread Paul St George

On 17/08/2019 15:37, Peter Otten wrote:

Paul St George wrote:


Can someone please tell me how to get the absolute path to a file? I
have tried os.path.abspath. In the code below I have a problem in the
final line (15).

#
|import bpy||


Is this blender? If so the "//" prefix starts making sense:

https://docs.blender.org/api/current/bpy.path.html

I'd try bpy.path.abspath() instead of os.pathabspath().



||import os||
||
||texture_list = []||
||
||with open(os.path.splitext(bpy.data.filepath)[0] + ".txt", "w") as
outstream:||
||
||
||for obj in bpy.context.scene.objects:||
||for s in obj.material_slots:||
||if s.material and s.material.use_nodes:||
||for n in s.material.node_tree.nodes:||
||if n.type == 'TEX_IMAGE':||
||texture_list += [n.image]||
||print(obj.name,'uses',n.image.name,'saved at',n.image.filepath, 'which
is at', os.path.abspath(n.image.filepath), file=outstream)||
|#

This gives me:
---Plane uses image01.tif saved at //image01.tif which is at //image01.tif
---Plane uses image02.tif saved at //../images/image02.tif which is at
//images/image02.tif

But I want an absolute path such as:
---Plane uses image01.tif saved at /Users/Lion/Desktop/test8/image01.tif
---Plane uses image02.tif saved at /Users/Lion/Desktop/images/image02.tif

If it is relevant, my files are on a Mac. Hence the escaped forward slash.




Now following Peter's immensely useful nudge towards bpy.path.abspath(), 
I have learned that Blender uses a special kind of relative path. These 
paths start with ”//” . The double slashes replace the path to the blend 
file’s directory.


This means that py.path.abspath() gives a path that is absolute for most 
of its length then relative towards the end. The good news is that I 
have found a way to tidy this.


os.path.abspath(bpy.path.abspath(p))

This gives a fully absolute file path. The bpy.path.abspath function 
takes care of the ’//’ replacing it with the full path to the file, 
while os.path.abspath takes care of any other unwelcome relative ’../’ 
that might remain.


Thanks to everyone for their help!
Paul

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


Re: absolute path to a file

2019-08-17 Thread Cameron Simpson

On 17Aug2019 11:51, Paul St George  wrote:

print('Track D  from Track B:',os.path.realpath(n.image.filepath))
---Track D  from Track B: /image01.tif

print('Track E  from Track B:',os.path.realpath(n.image.filepath[1:]))
---Track E  from Track B: /image01.tif

print('Track F from Track C:',os.path.realpath(n.image.filepath[2:]))
---Track F from Track C: /image01.tif


I know these may just be cut/paste, but you can shorten this by:

   from os.path import realpath
   print('Track F from Track C:',realpath(n.image.filepath[2:]))

I have 2 other questions:

1: Is image01.tif a real existing file when you ran this code?

realpath kind of expects that - it may do nothing for something which 
doesn't exist. I'd point out that realpath(3), which you get from the 
command "man 3 realpath", says:


   The realpath() function will resolve both absolute and relative paths and
   return the absolute pathname corresponding to file_name.  All
   components of file_name must exist when realpath() is called.

Most of the things in the os module are just thin wrappers for the OS 
supplied functions, which is why the MacOS realpath library function is 
relevant.


2: What is your current working directory when you run this code?

If the underlying OS realpath fails, the Python library function might 
fall back on os.path.join(os.getcwd(), filename).


3: What is the airspeed velocity of an unladen swallow?

https://www.python.org/dev/peps/pep-0234/#rationale

Cheers,
Cameron Simpson  (formerly [email protected])
--
https://mail.python.org/mailman/listinfo/python-list