Storing a big amount of path names

MRAB python at mrabarnett.plus.com
Thu Feb 11 22:13:17 EST 2016


On 2016-02-12 00:31, Paulo da Silva wrote:
> Hi!
>
> What is the best (shortest memory usage) way to store lots of pathnames
> in memory where:
>
> 1. Path names are pathname=(dirname,filename)
> 2. There many different dirnames but much less than pathnames
> 3. dirnames have in general many chars
>
> The idea is to share the common dirnames.
>
> More realistically not only the pathnames are stored but objects each
> object being a MyFile containing
> self.name - <base name>
> getPathname(self) - <full pathname>
> other stuff
>
> class MyFile:
>
>    __allfiles=[]
>
>    def __init__(self,dirname,filename):
>      self.dirname=dirname  # But I want to share this with other files
>      self.name=filename
>      MyFile.__allfiles.append(self)
>      ...
>
>    def getPathname(self):
>      return os.path.join(self.dirname,self.name)
>
>    ...
>
Apart from all of the other answers that have been given:

 >>> p1 = 'foo/bar'
 >>> p2 = 'foo/bar'
 >>> id(p1), id(p2)
(982008930176, 982008930120)
 >>> d = {}
 >>> id(d.setdefault(p1, p1))
982008930176
 >>> id(d.setdefault(p2, p2))
982008930176

The dict maps equal strings (dirnames) to the same string, so you won't 
have multiple copies.



More information about the Python-list mailing list