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