Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #91942

Re: What sort of data structure to use?

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'item.': 0.07; 'performs': 0.07; 'predefined': 0.07; 'iterate': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'files.': 0.13; 'def': 0.14; 'files)': 0.16; 'folder,': 0.16; 'organise': 0.16; 'pairs': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'set,': 0.16; 'wrote:': 0.16; 'sets': 0.23; 'import': 0.24; 'written': 0.24; 'sort': 0.25; 'header:User-Agent:1': 0.26; 'question': 0.26; 'header:X -Complaints-To:1': 0.26; 'yield': 0.27; 'path,': 0.29; "skip:' 10": 0.30; 'folder': 0.31; 'skip:g 30': 0.31; "i'd": 0.31; 'certain': 0.31; 'skip:[ 10': 0.32; 'skip:_ 10': 0.32; 'structure': 0.32; 'class': 0.33; 'subject:use': 0.33; 'utility': 0.33; 'subject:?': 0.34; 'file': 0.34; 'add': 0.34; 'that,': 0.34; 'could': 0.35; 'to:addr:python-list': 0.35; 'list:': 0.35; 'path': 0.35; 'really': 0.35; 'list': 0.35; 'list,': 0.36; 'there': 0.36; 'subject:: ': 0.37; 'skip:v 20': 0.38; 'received:org': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'data': 0.40; 'received:de': 0.40; 'some': 0.40; 'simple': 0.61; 'more': 0.62; 'here': 0.66; 'outline': 0.66; 'sets,': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: What sort of data structure to use?
Date Wed, 03 Jun 2015 12:59:13 +0200
Organization None
References <41302A7145AC054FA7A96CFD03835A0A0B97DECA@EX10MBX02.EU.NEC.COM>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p57bd97df.dip0.t-ipconnect.de
User-Agent KNode/4.13.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.20+
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.100.1433329169.13271.python-list@python.org> (permalink)
Lines 63
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1433329169 news.xs4all.nl 2901 [2001:888:2000:d::a6]:37905
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:91942

Show key headers only | View raw


David Aldrich wrote:

> Hi
> 
> I have written a Python utility that performs a certain activity on some
> predefined sets of files.  Here is the outline of what I have written:
> 
> # File Set A
> pathA = 'pathA'
> fileListA = ['fileA1.txt', 'fileA2.txt']
> 
> # File Set B
> pathB = 'pathB'
> fileListB = ['fileB1.txt', 'fileB2.txt', 'fileB3.txt']
> 
> myFunc1(pathA, fileListA)
> myFunc2(pathA, fileListA)
> 
> myFunc1(pathB, fileListB)
> myFunc2(pathB, fileListB)
> 
> I want to add more file sets, so I really want to add the sets to a list
> and iterate over the list, calling myFunc1 & myFunc2 for each item.
> 
> My question is: what sort of data structure could I use to organise this,
> given that I want to associate a set of files with each path and that, for
> each set, there is an arbitrary number of files?

I'd start simple and put (path, files) pairs into a list:

path_files_pairs = [
    ("pathA", ["fileA1.txt", "fileA2.txt", ...]),
    ("pathB", ["fileB1.txt", ...]),
]

for path, files in path_files_pairs:
    func1(path, files)
    func2(path, files)

You can always add complications later:

import glob
import os

class VirtualFileset:
    def __init__(self, folder, pattern):
        self.folder = folder
        self.pattern = pattern
    def __iter__(self):
        yield self.folder
        yield glob.glob(os.path.join(self.folder, self.pattern))

path_files_pairs = [
    ("pathA", ["fileA1.txt", "fileA2.txt", ...]),
    ("pathB", ["fileB1.txt", ...]),
    VirtualFileset("pathC", "*.py"), # all python files in directory "pathC"
]

for path, files in path_files_pairs:
    func1(path, files)
    func2(path, files)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: What sort of data structure to use? Peter Otten <__peter__@web.de> - 2015-06-03 12:59 +0200

csiph-web