Giter VIP home page Giter VIP logo

sk-guritech / ext-list Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 744 KB

ExtList is a Python library that improves code quality by allowing list comprehension operations to be called as methods and handling lists more abstractly than the built-in list. It reduces list comprehensions, improving code readability and searchability.

License: MIT License

Dockerfile 1.33% Python 96.61% Shell 0.59% Makefile 0.62% Batchfile 0.79% CSS 0.06%
list-comprehension python python-library

ext-list's Introduction

ExtList

PyPI version PyPI - Python Version GitHub

This library was created to improve the quality of code through list operations. It allows commonly written list comprehension operations to be called as methods and enables lists to be treated more abstractly than the built-in list.

Using this library reduces the number of times list comprehensions need to be written, resulting in improved overall code readability and searchability.

More information -> Docs: ExtList

Installation

pip install ext-list

Examples

Below are some examples of features of this library.

Note: In the following examples, the Person class is defined as follows.

(class Person)
>>> class Person:
...     def __init__(self, name, age):
...         self.__name = name
...         self.__age = age
...
...     def introduce(self):
...         return f'{self.name} is {self.age} years old.'
...
...     def get_age_n_years_ago(self, n: int) -> int:
...        return self.age - n
...
...     @property
...     def name(self):
...         return self.__name
...
...     @property
...     def age(self):
...         return self.__age
...
...     def __repr__(self):
...         return f'Person(\'{self.name}\', {self.age})'
...
  • Extract values

    >>> person_dicts = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}])
    >>> person_dicts.extract('name')
    ['Alice', 'Bob']
    
    >>> persons = ExtList(Person('Alice', 25), Person('Bob', 30))
    >>> persons.extract(Person.name)
    ['Alice', 'Bob']
    
    >>> persons.extract('name')
    ['Alice', 'Bob']
    
    >>> persons.extract(Person.introduce)
    ['Alice is 25 years old.', 'Bob is 30 years old.']
    
    >>> persons.extract(Person.get_age_n_years_ago, 5)
    [20, 25]
    
  • Get matched objects

    >>> persons = ExtList([{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}])
    >>> persons.equals('name', 'Alice')
    [{'name': 'Alice', 'age': 25}]
    
    >>> persons = ExtList(Person('Alice', 25), Person('Bob', 30))
    >>> persons.equals(Person.name, 'Alice')
    [Person('Alice', 25)]
    
    >>> persons.equals(Person.introduce, 'Bob is 30 years old.')
    [Person('Bob', 30)]
    
  • Convert list to dict

    >>> persons = ExtList([Person('Alice', 25), Person('Bob', 30)])
    >>> persons.to_dict(Person.name)
    {'Alice': Person('Alice', 25), 'Bob': Person('Bob', 30)}
    
  • Convert list to dict with complex keys

    >>> persons = ExtList([Person('Alice', 25), Person('Bob', 30)])
    >>> persons.to_dict_with_complex_keys([Person.name, Person.age])
    {('Alice', 25): Person('Alice', 25),
    ('Bob', 30): Person('Bob', 30)}
    

See the Docs: ExtList for more examples !

requirements

typing_extensions

License

MIT license

Author

Sakaguchi Ryo (@GuriTech)

ext-list's People

Contributors

sk-guritech avatar

Stargazers

 avatar

Watchers

 avatar

ext-list's Issues

methods too slow

The execution time of the method is long. It has been found to be slower by approximately up to 8 times compared to list comprehensions. The cause has been identified as the high cost of function object invocation.

relaxing type restrictions

現在ExtListでは単一の型のみを扱えるように制限していますが、
実用上不都合が出始めているため、制限の緩和を実装します。

implement 'list to sorted dict ' method

# Input
[
    {"name" : "alice", "age" : 25},
    {"name" : "bob", "age" : 30},
    {"name" : "charlie", "age" : 35},
    {"name" : "david", "age" : 30}
]


ExtList(input, key="age")  # key is FunctionType | property | str | Hashable

#Output
{
    25 : [{"name" : "alice", "age" : 25}],
    30 : [{"name" : "bob", "age" : 30}, {"name" : "david", "age" : 30}],
    35 : [{"name" : "charlie", "age" : 35}]
}

implement to_dict_list(..)

a = [{"name": "alice", "age": 20, "country": "JP"}, { "name": "bob", "age": 35, "country": "US"}]
ExtList(a).to_dict_list("name", "age")

[{"name": "alice", "age": 20}, {"name": "bob", "age": 35}]

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.