Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 28 additions & 16 deletions data/from_list.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Native import
from copy import deepcopy

# Local import
Expand All @@ -6,25 +7,39 @@


class FromList(Utils):
"""
Handle search of duplicate or unique item inside a list

def __init__(self, lst, key=None):
:param list lst: Param to work on
:param str key: Param to work in a list of dict if you need it
"""

def __init__(self, lst: list, key: str = None) -> None:
self.copied_lst = deepcopy(lst)
Utils.__init__(self, self.copied_lst)
if key:
self.create_list(key)
self.get_type()
self.validate_items()

def analyze(self, analyze_type, feedback=True):
def analyze(self, analyze_type: str, feedback: bool = True) -> dict:
"""
Analyze the list to get the items and their indexes
(depending on the analyze_type)

:return: {
"all_index": [INDEX(ES)],
"VALUE_X": [INDEX(ES)],
"VALUE_Y": [INDEX(ES)],
}
:param str analyze_type: Param to determine what type of analyse to do
:param bool feedback: Param to return more or less information
:return: * without feedback::

{ "all_index": [REVERSED_ORDER_INDEX(ES)] }

* with feedback::

{
"all_index": [REVERSED_ORDER_INDEX(ES)],
"VALUE_X": [INDEX(ES)],
"VALUE_Y": [INDEX(ES)],
}
"""

if analyze_type == UNIQUE:
Expand All @@ -34,11 +49,10 @@ def analyze(self, analyze_type, feedback=True):
if analyze_type == CREATE_UNIQUE:
return self.create_unique_index(feedback)

def create_unique(self):
def create_unique(self) -> list:
"""
Delete duplicate item to create a list of unique items

:return: [list]
:return: Unique items (by deleting duplicate items)
"""
indexes = self.create_unique_index()

Expand All @@ -47,21 +61,19 @@ def create_unique(self):

return self.copied_lst

def get_unique(self):
def get_unique(self) -> list:
"""
Get only all unique items

:return: [list]
:return: Only unique items
"""
indexes = self.get_indexes(UNIQUE)

return [self.copied_lst[index] for index in indexes['all_index']]

def get_duplicate(self):
def get_duplicate(self) -> list:
"""
Get only all duplicate items

:return: [list]
:return: Only all duplicate items
"""
indexes = self.get_indexes(DUPLICATE)

Expand Down
82 changes: 43 additions & 39 deletions data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,30 @@


class Utils:
"""
Provides utils function to handle search of duplicate or unique item
inside a list

def __init__(self, lst):
:param list lst: Param to work on.
By default its the same list passed to `FromList`_
"""

def __init__(self, lst: list) -> None:
self.indexes = {}
self.lst = lst
self.item_type = list

def create_list(self, key):
def create_list(self, key: str) -> None:
"""
Create a list of all item of a given key
Ex: obj = [{'id': 1}, {'id': 2}] . key = 'id'
Create a list of all item for a given key

:return: [1, 2]
:param str key: Dict key on which to get data
"""
self.lst = [value.get(key) for value in self.lst]

def get_type(self):
def get_type(self) -> None:
"""
Get the type of the first item

:return: item_type
"""
item = self.lst[0]
if isinstance(item, (int, float)):
Expand All @@ -34,22 +38,19 @@ def get_type(self):
elif isinstance(item, tuple):
self.item_type = tuple

def validate_items(self):
def validate_items(self) -> None:
"""
Check if all items are of the same type

:return:
if not: raise an TypeError
if yes: nothing
:raise TypeError: If items are NOT of the same type
"""
if not all(isinstance(x, self.item_type) for x in self.lst):
raise TypeError(f'An item has a different type than the others')

def create_update_feedback(self, index, value, feedback):
def create_update_feedback(self, index: int, value: any,
feedback: bool) -> None:
"""
Create or update the 'self.indexes' dict to provide feedback

:return: { VALUE: [INDEX(ES)] }
"""
if feedback:
str_value = str(value)
Expand All @@ -58,21 +59,23 @@ def create_update_feedback(self, index, value, feedback):
else:
self.indexes.update({str_value: [index]})

def get_indexes(self, index_type, feedback=False):
def get_indexes(self, index_type: str, feedback: bool = False) -> dict:
"""
Identify unique or duplicate item and return the index of each of them

:return:
without feedback:
{
"all_index": [INDEX(ES)]
}
with feedback:
{
"all_index": [INDEX(ES)],
VALUE_X: [INDEX(ES)],
VALUE_Y: [INDEX(ES)],
}
:param str index_type: Param to know what type of item index we want
:param bool feedback: Param to return more or less information
:return: * without feedback::

{ "all_index": [INDEX(ES)] }

* with feedback::

{
"all_index": [INDEX(ES)],
"VALUE_X": [INDEX(ES)],
"VALUE_Y": [INDEX(ES)],
}
"""
indexes = []
for index, value in enumerate(self.lst):
Expand All @@ -85,22 +88,23 @@ def get_indexes(self, index_type, feedback=False):
self.indexes['all_index'] = indexes
return self.indexes

def create_unique_index(self, feedback=False):
def create_unique_index(self, feedback: bool = False) -> dict:
"""
Identify duplicated item and return the index of each of them except for
the first one (in order to create a list of unique item)

:return:
without feedback:
{
"all_index": [REVERSED_ORDER_INDEX(ES)]
}
with feedback:
{
"all_index": [REVERSED_ORDER_INDEX(ES)],
VALUE_X: [INDEX(ES)],
VALUE_Y: [INDEX(ES)],
}
:param bool feedback: Param to return more or less information
:return: * without feedback::

{ "all_index": [REVERSED_ORDER_INDEX(ES)] }

* with feedback::

{
"all_index": [REVERSED_ORDER_INDEX(ES)],
"VALUE_X": [INDEX(ES)],
"VALUE_Y": [INDEX(ES)],
}
"""
seen_value = []
seen_index = []
Expand Down
11 changes: 6 additions & 5 deletions docs/python-duplicate/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ This document specifies python-duplicate's data package.
FromList
--------

.. py:class:: FromList(lst[, key=None])
.. module:: data.from_list

:param list lst: list on which to work.
:param str key: key on which to work in a list of dict if you need it
.. autoclass:: FromList
:members:

Utils
-----

.. py:class:: Utils(lst)
.. module:: data.utils

:param list lst: list on which to work. By default its the same list passed to `FromList`_
.. autoclass:: Utils
:members: