Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add replace() method #134

Open
robertour opened this issue Feb 8, 2021 · 0 comments
Open

add replace() method #134

robertour opened this issue Feb 8, 2021 · 0 comments

Comments

@robertour
Copy link

It would be great if there would be a replace method, so that:

>> furl(`http:\\example.com\?a=1&b=2`).replace({'a':100}).url

would output: http:\\example.com\?a=100&b=2

A remove and add would change the order of things:

>> furl(`http:\\example.com\?a=1&b=2`).remove('a').add({'a':100}).url

would output http:\\example.com\?b=2&a=100, instead

Although http:\\example.com\?a=100&b=2 is equivalent to http:\\example.com\?b=2&a=100 according to the the standards, in practice, it is useful to keep order when you end with millions of URLs and you want to keep things organized for data processing. I currently have a pandas dataframe (df) in which I use apply to modify a subset of rows with apply:

df.loc[criteria, ['url', 'value']].apply(lambda x: furl(x).remove('a').add('{a: 100}'), axis=1)

However, this would change the "order" of the URL in the subset of criteria, as compared to all the rest.

so, I created the following function to circumvent the problem:

from furl import furl

def replace_param(url, param, value):
    _f = furl(url)
    _f.args[param] = value
    return _f.url

# small test
replace_param("http:\\example.com\?a=1&b=2", 'a', 100)

# pandas usage
df.loc[criteria, ['url', 'value']].apply(lambda x: furl(x).replace_param('a', 100), axis=1)

I know the internals of furl could easily support the replace function, so this should be fairly easy to implement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant