Code Monkey home page Code Monkey logo

Comments (5)

gruns avatar gruns commented on August 25, 2024

f.args is an ordered multivalue dictionary (https://github.com/gruns/orderedmultidict).

For ease of use and interoperability with other Python libraries, orderedmultidict
retains method parity with Python's dict. That's why only one value is returned with
f.args['Subject'] -- Python's dictionary only supports one value per key.

To access the other Subject values, use orderedmultidict's multivalue methods, like
getlist() and setlist().

>>> from furl import furl
>>> f = 'http://localhost:8080/foo?Subject=aa&Subject=bb'
>>> f.args.allitems()
[('Subject', 'aa'), ('Subject', 'bb')]
>>> f.args.getlist('Subject')
['aa', 'bb']
>>> f.args.setlist('Subject', ['uno', 'due', 'tre'])
>>> f.url
'http://localhost:8080/foo?Subject=uno&Subject=due&Subject=tre'

See orderedmultidict's documentation here:
https://github.com/gruns/orderedmultidict/blob/master/API.md.

Does that answer your question?

from furl.

keul avatar keul commented on August 25, 2024

Thanks for the explanation, in fact I overtaken this limitation iterating onto f.args.allitems() (good to know about other API like setlist).

Still, this library can be pure gold in URL manipulation (which is always a mess) and I'm wondering if this issue can be fixed keeping high-level API: lot of web frameworks use repeated parameters on URLs.
A good behavior can be to store items with the same parameter as a list of values, something like:

f = furl('http://www.google.com/?one=1&one=2')
f.args['one'] = [1, 2]

(maybe this is not possible due to internal limitation).

Apart of that: really cool library. Thanks.

from furl.

gruns avatar gruns commented on August 25, 2024
f.args['one'] = [1, 2]

is weak API design because lists and strings behave similarly, forcing users to
check the type of the values in f.args.

Imagine you want to get the length of the parameter &one=. The natural way is

>>> print len(f.args['one'])

But this is ambiguous.

>>> fs = furl('http://google.com/?one=abc')
>>> fl = furl'http://google.com/?one=a&one=b&one=c')
>>> print len(fs.args['one'])
3
>>> print len(fl.args['one'])
3

To defeat this ambiguity, furl users would be forced to repeatedly test whether
f.args values are strings or lists

>>> fl = furl'http://google.com/?one=a&one=b&one=c')
>>> if isinstance(f.args['one'], basestring):
...   # Handle string.
... else:
...   # Handle list.

That's cumbersome and confusing.

That said, I'm all for improving orderedmultidict's API. It's loquacious. But
allowing f.args values to be lists bleeds ambiguity into the API.

Does that answer your question?

from furl.

keul avatar keul commented on August 25, 2024

Yeah, probably a list with strings inside is not the best.

Maybe the args property should return a custom object instead of a simple string, something like...

>>> f = furl('http://google.com/?one=a&one=b&one=c')
>>> f.args
<Params something>
>>> len(f.args)
1
>>> len(f.args['one'])
3
>>> f.args['one'][0].value
'a'

...but this will probably add too much complexity to the library (and probably 90% of the users are happy with the current features set).

Thanks for the support! 🍻

from furl.

gruns avatar gruns commented on August 25, 2024

To be clear, orderedmultidict (https://github.com/gruns/orderedmultidict) is
exactly that 'custom object'.

See https://github.com/gruns/orderedmultidict/blob/master/API.md.

Thank you again Luca. Don't hesitate to let me know if there's anything else I
can do for you.

from furl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.