Comments (5)
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.
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.
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.
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.
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)
- furl doesn't type-hint itself as Text HOT 3
- url parsing still accepts semicolon as separator HOT 5
- Add typing support HOT 6
- Hi, if the URL is like '127.0.0.1:8080/a/b', the result is '8080/a/b', which seems incorrect HOT 2
- How handle javascript:void(0) HOT 2
- Feature: Addition of `is_domain` flag
- Maximum recursion depth exceeded error for url with large list HOT 3
- Adding README to PyPi HOT 2
- '127.0.0.1:8329' parsed wrong in Python 3.9+ HOT 3
- Add SECURITY.md HOT 3
- Feature request: Convert to unescaped string HOT 2
- .join() and .path.normalize() incorrectly interpret base paths ending /. or /..
- furl.path object is NOT of type: pathlib.Path HOT 1
- Feature requests: get url relative to another url
- Setting multiple query params for the same key and resultant url format
- Seems to be a bug when paring multiple '@'s ?
- Use the `ipaddress` module to detect valid IPV6 HOT 1
- a method to set an individual query parameter without removing the rest or keeping the original value
- Removes "git@" from git remote URL HOT 1
- URL fragment percent-encoding
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from furl.