GithubHelp home page GithubHelp logo

picklist's Introduction

日本語はこちら

picklist

Picklist is a user-friendly list to pick element with conditions.

Quick example

# normal way
[person for person in person_list if person.name == "John"][0]

# equivalent with picklist
person_list.pick(name="John")  # easy!

Install

$ python -m pip install git+https://github.com/yoko72/picklist

Example

With following data:

from dataclasses import dataclass
from picklist import PickList


@dataclass
class Person:
    name: str
    age: int


John = Person("John", 35)
Smith = Person("Smith", 22)

persons = PickList([John, Smith])

Let's pick an element whose value of "name" attribute is "John".

persons.pick(name="John")  # == John
# Even .pick is omitted, it works same.

The pick method accepts any keyword arguments(key=value), and returns the element which satisfies all element.key == value conditions.

It's almost equivalent to following ways.

# for loop
John = None
for person in persons:
    if person.name == "John":
        John = person
        break
# comprehension
try:
    John = [person for person in persons if person.name == "John"][0]
except IndexError:  # if list is empty:
    John = None

Comprehension way differs a little from pick method and for loop. It doesn't stop the process even after it finds the object.

Dict as element

Not only objects holding value as attr, but also dictlike object is available. Let's see the example of dict.

John_dict = {"name": "John", "age": 35}
Smith_dict = {"name": "Smith", "age": 22}

plist = PickList([John_dict, Smith_dict])
plist.pick(name="Smith")  # is Smith_dict

get_all

get_all() returns list of all elements satisfying the conditions as picklist.

persons = PickList([Person("Abigail", 35),
                    Person("John", 35),
                    Person("Smith", 22)])

persons_aged_35 = persons.get_all(age=35)  
# == PickList([person for person in persons if person.age==35])

Values of an attribute

If you want names of all persons,

names = persons.names
# ["Abigail", "John", "Smith"]
# persons.get_values("name") also works same.

It's equivalent with:

names = [person.name for person in persons]

Nothing has "names" attribute, but all elements have "name" attribute. If picklist is accessed with undefined attribute with "s" suffix, each element is checked if they have the attribute without "s" suffix. If they have, picklist returns the values of each element as PickList. If not, AttributeError is raised.

This usage comes from the usage of multiple form in English, but it purely checks if it ends with "s" or not. Therefore, incorrect english words are possible like picklist.informations, plist.womans and so on.

You can use get_values method if you don't like such usage, it works same.

Followings are equivalent.

plist.get_values("example")
plist.examples

Complicated conditions:

You can extract with complicated conditions by giving callable as positional argument. The callable must accept one argument, and the element is extracted only when bool(Callable(element)) is True.

example = persons.pick(
    lambda person: 
        person.name.startswith("A")
        and person.weight > 45.0,
    age=27, height=160)

Subclass of list

Picklist inherits from standard class. Each method or operator for the list is available.

picklist's People

Contributors

yoko72 avatar

Watchers

 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.