Skip to content

add replace() method #134

@robertour

Description

@robertour

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions