chtoolbox 0.2.0__tar.gz → 0.2.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: chtoolbox
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  License-File: LICENSE
@@ -0,0 +1 @@
1
+ from . import misc
@@ -0,0 +1,120 @@
1
+ import pandas as pd
2
+
3
+ def clipboard_to_dict():
4
+ """
5
+ Converts clipboard content to a dictionary.
6
+ This function reads the clipboard content into a pandas DataFrame and converts it to a dictionary.
7
+ If the DataFrame has two columns, it converts it to a flat dictionary.
8
+ If the DataFrame has more than two columns, it converts it to a nested dictionary with 'index' orientation.
9
+ Returns:
10
+ dict: A dictionary representation of the clipboard content.
11
+
12
+ Example 1:
13
+
14
+ >>> # Copy a range in excel containing thw following to the clipboard:
15
+ idx col_a col_b col_c
16
+ row_a 1 4 7
17
+ row_b 2 5 8
18
+ row_c 3 6 9
19
+
20
+ >>> clipboard_to_dict()
21
+ {'row_a': {'col_a': 1, 'col_b': 4, 'col_c': 7},
22
+ 'row_b': {'col_a': 2, 'col_b': 5, 'col_c': 8},
23
+ 'row_c': {'col_a': 3, 'col_b': 6, 'col_c': 9}}
24
+
25
+ Example 2:
26
+ >>> # Copy a range in excel containing thw following to the clipboard:
27
+ idx col_a
28
+ row_a 1
29
+ row_b 2
30
+ row_c 3
31
+
32
+ >>> clipboard_to_dict()
33
+ {'row_a': 1, 'row_b': 2, 'row_c': 3}
34
+ """
35
+ # Read the clipboard content into a DataFrame
36
+ df = pd.read_clipboard(header=None)
37
+
38
+ if df.shape[1] == 2:
39
+ # Convert the DataFrame to a flat dictionary
40
+ dictionary = dict(zip(df[0], df[1]))
41
+ else:
42
+ # Check if the first column header is empty
43
+ if pd.isna(df.iloc[0, 0]):
44
+ df.columns = ['idx'] + list(df.iloc[0, 1:])
45
+ df = df[1:]
46
+ else:
47
+ df.columns = df.iloc[0]
48
+ df = df[1:]
49
+
50
+ # Set the first column as the index
51
+ df.set_index('idx', inplace=True)
52
+
53
+ # Convert the DataFrame to a nested dictionary with 'index' orientation
54
+ dictionary = df.to_dict(orient='index')
55
+
56
+ return dictionary
57
+
58
+
59
+ def clipboard_to_list():
60
+ """
61
+ Retrieve data from the system clipboard and convert it to a list.
62
+ This function reads the clipboard content into a pandas DataFrame,
63
+ converts the DataFrame values into a one-dimensional list, and returns the list.
64
+ Returns:
65
+ list: A list containing the clipboard data.
66
+ """
67
+
68
+ # Read clipboard data into a DataFrame
69
+ df = pd.read_clipboard(header=None)
70
+
71
+ # Convert DataFrame values to a one-dimensional list
72
+ data_list = df.values.flatten().tolist()
73
+
74
+ # Print the list
75
+ return data_list
76
+
77
+
78
+ def compare_lists_from_clipboard():
79
+ """
80
+ A function used to select and copy a table from Excel.
81
+ The table is converted into lists (one list per column in the Excel range).
82
+ Then it compares all the lists and finds the unique elements that are not present in all the lists.
83
+ Useful for comparing large amounts of elements from Excel.
84
+ Dictionary containing one list per column in the clipboard (Excel range).
85
+ """
86
+ '''
87
+ En funksjon som brukes ved å merke og kopiere en tabell fra excel.
88
+ Tabellen konverteres til lister (en liste per kolonne i excel rangen)
89
+ Deretter så sammenligner den alle listene og finner de unike elementene som ikke finnes i alle listene
90
+
91
+ Nyttig for å sammenligne store mengder med elementer fra excel
92
+
93
+ Returns
94
+ -------
95
+ list_dict : dict
96
+ Dictionary containing one list per column in clipboard (excel range).
97
+ unique_items : list
98
+ A list of items that are not present in all the lists.
99
+ common_items : list
100
+ A list of items that are present in all the lists.
101
+
102
+ '''
103
+ # Read clipboard data as a DataFrame and convert it to dictionary
104
+ data = pd.read_clipboard(header=None)
105
+ list_dict = {}
106
+ for col in data.columns:
107
+ col_values = data[col].tolist()
108
+ col_values = [value for value in col_values if not pd.isna(value)]
109
+ list_dict[col] = col_values
110
+
111
+ # Find values not present in all lists
112
+ common_items = set.intersection(*(set(values) for values in list_dict.values()))
113
+ unique_items = []
114
+ for values in list_dict.values():
115
+ for value in values:
116
+ if value not in common_items and value not in unique_items:
117
+ unique_items.append(value)
118
+
119
+ # Print the resulting dictionary
120
+ return list_dict, unique_items, list(common_items)
@@ -1,4 +1,4 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: chtoolbox
3
- Version: 0.2.0
3
+ Version: 0.2.1
4
4
  License-File: LICENSE
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name='chtoolbox',
5
- version='0.2.0',
5
+ version='0.2.1',
6
6
  packages=find_packages(),
7
7
  install_requires=[
8
8
  'numpy',
@@ -1 +0,0 @@
1
- from .misc import *
@@ -1,56 +0,0 @@
1
- import pandas as pd
2
-
3
- def clipboard_to_dict():
4
- """
5
- Converts clipboard content to a dictionary.
6
- This function reads the clipboard content into a pandas DataFrame and converts it to a dictionary.
7
- If the DataFrame has two columns, it converts it to a flat dictionary.
8
- If the DataFrame has more than two columns, it converts it to a nested dictionary with 'index' orientation.
9
- Returns:
10
- dict: A dictionary representation of the clipboard content.
11
-
12
- Example 1:
13
-
14
- >>> # Copy a range in excel containing thw following to the clipboard:
15
- idx col_a col_b col_c
16
- row_a 1 4 7
17
- row_b 2 5 8
18
- row_c 3 6 9
19
-
20
- >>> clipboard_to_dict()
21
- {'row_a': {'col_a': 1, 'col_b': 4, 'col_c': 7},
22
- 'row_b': {'col_a': 2, 'col_b': 5, 'col_c': 8},
23
- 'row_c': {'col_a': 3, 'col_b': 6, 'col_c': 9}}
24
-
25
- Example 2:
26
- >>> # Copy a range in excel containing thw following to the clipboard:
27
- idx col_a
28
- row_a 1
29
- row_b 2
30
- row_c 3
31
-
32
- >>> clipboard_to_dict()
33
- {'row_a': 1, 'row_b': 2, 'row_c': 3}
34
- """
35
- # Read the clipboard content into a DataFrame
36
- df = pd.read_clipboard(header=None)
37
-
38
- if df.shape[1] == 2:
39
- # Convert the DataFrame to a flat dictionary
40
- dictionary = dict(zip(df[0], df[1]))
41
- else:
42
- # Check if the first column header is empty
43
- if pd.isna(df.iloc[0, 0]):
44
- df.columns = ['idx'] + list(df.iloc[0, 1:])
45
- df = df[1:]
46
- else:
47
- df.columns = df.iloc[0]
48
- df = df[1:]
49
-
50
- # Set the first column as the index
51
- df.set_index('idx', inplace=True)
52
-
53
- # Convert the DataFrame to a nested dictionary with 'index' orientation
54
- dictionary = df.to_dict(orient='index')
55
-
56
- return dictionary
File without changes
File without changes
File without changes