BigRoy / pather

Manage file system structure using patterns. Parse, format and list your paths.
GNU Lesser General Public License v3.0
47 stars 7 forks source link

Positional Arguments #2

Open mottosso opened 8 years ago

mottosso commented 8 years ago

These two return the same results.

>>> ls("project/assets/{item}/{task}")
>>> ls("project/assets/{task}/{item}")

So why have keywords in there at all?

>>> ls("project/assets/{}/{}")

And if I'm only including a single key in data, wouldn't I only have to pass that key to the pattern?

>>> ls("project/assets/{}/{item}", data={"item": "value"})   

On that note, couldn't we do positional arguments?

ls("project/assets/{0}/{1}", data=["value"])  # Search first
ls("project/assets/{0}/{1}", data=[None, "value"]) Search second
BigRoy commented 8 years ago

This would make the arguments a bit more complex, basically what you're saying is to support the full range of formatting just like normal string.format() does. I think that's good, though note that it would take args and kwargs.

So it would have to become something like:

ls("project/assets/{item}/{0}", data_args=["value"], data_kwargs={"item": "ben"})

Maybe for simplicity keep it out and allow this:

ls("project/assets/{item}/{0}", data={0: "zero", "item": "ben"})

Or..

ls("project/assets/{item}/{0}", data={"0": "zero", "item": "ben"})
BigRoy commented 8 years ago

The tricky thing here is that it becomes "ugly" once you start to mix *args and **kwargs in one set of data. This is because there's no way to differentiate them from each other when passed to data since it's in a dictionary. And as such you'd have to separate them as data into two different inputs or a 2-tuple.

As per your examples it's solely doing it on a list (which doesn't act as kwargs) and won't allow adding in any **kwargs. It's something that could easily be added of course. I'm only in doubt whether that's enough to make it useful (if no additional kwargs can be added).

Also see my samples above.

Another way to do it could be to use a small utility method, for example:

args2kwargs(*args):
    return dict((str(i), v) for i, v in enumerate(args))