Giter Site home page Giter Site logo

kwlist's Introduction

KeywordList

kwlist provides a KeywordList class for associating string keywords with multiple values in a consistent order.

This is useful for specifying multiple values for arguments as in HTTP headers or query parameters.

Inspired by Elixir's Keyword module.

Examples

Basic usage:

>>> from kwlist import KeywordList

>>> a = KeywordList(foo=1, bar=2)
>>> b = KeywordList(foo=3, baz=4)
>>> c = a + b
>>> c
KeywordList[foo: 1, bar: 2, foo: 3, baz: 4]
>>> a == [("foo", 1), ("bar", 2)]
True
>>> "bar" in b
False
>>> c["foo"]
1
>>> list(c.get_all("foo"))
[1, 3]

A KeywordList can be built by passing tuples of keyword-value pairs or keyword arguments or both:

>>> a = KeywordList(("foo", 1), ("bar", 2))
>>> b = KeywordList(foo=3, baz=4)
>>> a + b == KeywordList(("foo", 1), ("foo", 3), bar=2, baz=4)
True

Note that keyword argument ordering is only preserved in Python 3.6+ (see PEP 468).

All methods of the KeywordList return new objects without mutating the object invoked:

>>> original = KeywordList(a=2)
>>> original.update('a', lambda n: n + 1)
KeywordList[a: 3]
>>> original.delete('a')
KeywordList[]
>>> original.put('a', None)
KeywordList[a: None]
>>> original
KeywordList[a: 2]

This property simplifies arbitrary and complex operator chaining:

>>> original = KeywordList()
>>> original.append([("foo", 1), ("foo", 2), ("bar", 3)]).transform(lambda x: str(x ** 2)).split("foo")
KeywordList[foo: '1', foo: '4'], KeywordList[bar: '9']

Installing

Ideally in a virtual environment, run:

git clone https://github.com/sseg/kwlist.git && pip install kwlist/

Similar Projects

If you're looking for something closer to Python's dict API you might like one of these implementations:

License

BSD licensed. See the LICENSE file for details.

kwlist's People

Contributors

sseg avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.