Skip to main content

pyquerylist Build Status

Extension of base Python list that you can query. Queries are chainable and invertable with logical operators (& | ~). Simple operations such as .where(...), to filter, .select(...) to choose fields, and .aggregate(...) to e.g. sum over a field are supported. .groupby(...) can be also used to .count() and .aggregate(...) QueryLists as well.

Installation

pip install pyquerylist

Documentation

Documentation is available at http://markmuetz.github.io/pyquerylist/.

example.py

from dataclasses import dataclass

from pyquerylist import Query as Q, QueryList


@dataclass
class Book:
    name: str
    price_pence: int
    category: str

    def price(self):
        return self.price_pence / 100

    def vat_price(self):
        return self.price() * 1.2


books = QueryList(
    [
        Book('Three crows', 500, 'fantasy'),
        Book('Molly had a little calf', 200, 'child'),
        Book('Time for another', 150, 'bargain'),
        Book('Of stars and mud', 700, 'highbrow'),
        Book('The Fruggalo', 300, 'child'),
        Book('Three women in a canoe', 120, 'classic'),
        Book('Who turns the page', 700, 'mystery'),
        Book('The sword of silver', 300, 'fantasy'),
        Book('Murder at 30000 ft', 150, 'mystery'),
        Book('Once I saw a little mouse', 50, 'child'),
        Book('The Leonardo code', 20, 'bargain'),
        Book('The place we are at', 900, 'highbrow'),
    ]
)


if __name__ == '__main__':
    print(books.count())
    # books can be filtered on one of their fields.
    print(books.where('name=="The Fruggalo"').count())
    # This can also be done using a query.
    print(books.where(Q('name=="The Fruggalo"')).count())
    # Queries can be inverted.
    print(books.where(~Q('name=="The Fruggalo"')).count())
    # books is a list -- can be indexed or sliced:
    print(books[:3])

    # books can be filtered on multiple fields, showing price <= 3.
    # if a field is a function (is callable) - it will be called (i.e. price).
    print(books.where('(category=="fantasy")&(price<=3)'))
    # Equivalent to above using combined queries.
    print(books.where(Q('category=="fantasy"') & Q('price<=3')))

    # These are equivalent, but not considered equal due to different definition.
    print(Q(lambda x: x.price <= 5) != Q('price <= 5'))

    # Queries can be combined using logical operators, and ordered by field(s).
    print(
        books.where(Q('category in ["fantasy", "mystery"]') | Q('price <= 3'))
        .orderby('price', order='descending')
        .select(fields=['category', 'price'])
    )
    # `lambda`s can be used, and fields can be chosen using `.select(...)`.
    print(
        books.where(Q('category == "fantasy"') | Q(lambda x: x.price() * 2 <= 6)).select(fields=['category', 'price'])
    )
    # Select can also take a function/lambda:
    print(books.select(func=lambda b: b.price() * 2))

    # Simple aggregate operations available.
    print(books.aggregate(sum, 'price'))
    print(books.aggregate(sum, ['price', 'vat_price']))

    # Simple group by operator (returns a dict subclass), with group operations count and aggregate.
    print(books.groupby('category')['mystery'])
    print(books.groupby('category').count())
    print(books.groupby('category').aggregate(sum, 'price'))

    # QueryLists can be formatted for tabular display.
    print(books.where('category=="fantasy"').tabulate(['name', 'price']))

Metadata

Release files for pyquerylist 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyquerylist 0.4.0
File Size Uploaded Provenance Actions
pyquerylist-0.4.0.tar.gz 12.8 kB Without provenance Details for pyquerylist-0.4.0.tar.gz

Release files / pyquerylist-0.4.0.tar.gz

Download URL pyquerylist-0.4.0.tar.gz
Size 12.8 kB
Tags Source
SHA-256 checksum
How to use checksums
32cf0c3ba5ac83f5474e074e9ac6260bd795c33f5ae3595cdef9bc2b3f4380af
BLAKE2b-256 checksum
How to use checksums
4a5e128f26755dd235e73cd409146b1e57a766fe2c0c2e8a642dddd056c4750f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.10.4

Security for pyquerylist 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page