Utility Functions Class Methods
A general purpose class for performing python pipeline functions such as reading/writing to google sheets, postgreSQL databases storing data as pickle or JSON files, with error handling and automated retries.
Also includes more complicated functions such as for merging paid and organic social data using fuzzy matching and regex or storing cumulative data we receieve as a daily incremental total
Source code in veetility/utility_functions.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
__init__(client_name, gspread_auth_dict=None, db_user=None, db_password=None, db_host=None, db_port=None, db_name=None, log_name='utility_functions')
Initialise a google sheets connector and postgreSQL connector for the utility instance
This means you can only connect to one google account and one database per instance of the UtilityFunctions class.
The email address of the google account must be added to the google sheet as a collaborator
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
client_name |
str
|
Used to specify the folder, |
required |
gspread_auth_dict |
dict
|
A dictionary containing google authorisation data |
None
|
db_user |
str
|
The postgreSQL database username |
None
|
db_password |
str
|
The postgreSQL database password |
None
|
db_host |
str
|
The postgreSQL database host url |
None
|
db_port |
str
|
The postgreSQL database port number as a string, usually 5432 |
None
|
db_name |
str
|
The postgreSQL database name |
None
|
Returns:
| Type | Description |
|---|---|
None |
Source code in veetility/utility_functions.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
best_fuzzy_match(list_1, list_2, threshold, json_name)
Takes in two lists of strings and every string in list_1 is fuzzy matched onto every item in list_2 The fuzzy match of a string in list_1 with a string in list_2 with the highest score will count as the match as long as it is above the threshold. The match is then stored as a key value pair in a dictionary
The dictionary of matches will be saved as a pickle file to be used next time the function is run to save having to do searches on a string if we've already found a match in the past
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
list_1 |
list
|
First List of strings, every item will be searched for a fuzzy match in list_2 |
required |
list_2 |
list
|
Second List of strings, every item in list_1 will be fuzzy matched with every item in list 2 and best fuzzy match score wins |
required |
threshold |
integer
|
value between 0 and 100 signifying percentage fuzzy match score at which a match is considered sufficiently close |
required |
json_name |
str
|
Name of json file to store dictionary of matches in |
required |
Returns:
| Name | Type | Description |
|---|---|---|
best_match_dict |
dict
|
Dictionary of matches (with highest fuzzy match score) between strings in list_1 and list_2 key = string in list_1, value = string in list_2 |
Source code in veetility/utility_functions.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 | |
columnnames_to_lowercase(df)
Change the columns in a dataframe into lowercase with spaces replaced by underscores
Source code in veetility/utility_functions.py
918 919 920 921 922 923 | |
convert_cumulative_to_daily(df, metric_list=None, unique_identifier_cols='url', date_row_added_col='date_row_added')
Convert cumulative metrics to daily metrics for a given dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
The dataframe to convert the cumulative metrics to daily metrics |
required |
metric_list |
list
|
The list of metrics to convert. Defaults to ['impressions','comments','clicks', 'link_clicks','likes','saved','shares','video_views']. |
None
|
unique_identifier_cols |
list
|
The list of columns that uniquely identify a post. Defaults to 'url'. |
'url'
|
date_row_added_col |
str
|
The name of the column that contains the date the row was added to the dataframe. Defaults to 'date_row_added'. |
'date_row_added'
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
DataFrame
|
The dataframe with the cumulative metrics converted to daily metrics |
Source code in veetility/utility_functions.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 | |
dupes_some_cols_but_differ_in_others(df, subset_cols, diff_cols, return_mode='only_differing_duplicates', max_value_keep_col=None)
Identifies rows that are duplicates in some columns but differ in others.
This is useful in some cases when you want to find specific types of duplicates caused by specific types of errors. For example social media posts that have the same URL but differ in the number of impressions.
For the "only_differing_duplicates" return_mode, the function will return the rows that are duplicates in the subset_cols but differ in the diff_cols.
For the "max_value" return_mode, the function will return the original and keep only the row with the max value in the max_value_keep_col
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
The dataframe to identify the rows in |
required |
subset_cols |
list
|
The columns to check for duplicates in |
required |
diff_cols |
list
|
The columns to check for differences in |
required |
return_mode |
str
|
The mode to return the duplicates in. Options are 'only_differing_duplicates' or 'max_value'. Defaults to 'only_differing_duplicates' |
'only_differing_duplicates'
|
max_value_keep_col |
str
|
The column to use to determine which row to use to find the max value of and therefore keep the row item with the highest value when return_mode is 'max_value'. Defaults to None. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
df_dupes |
DataFrame
|
Depending on the return mode, either the rows that are duplicates in the subset_cols but differ in the diff_cols or the original dataframe with only the row with the max value in the max_value_keep_col |
Source code in veetility/utility_functions.py
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 | |
get_active_git_branch()
Get the name of the currently active Git branch.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If the active Git branch cannot be found or there's any other error. |
Returns:
| Name | Type | Description |
|---|---|---|
str | The name of the active Git branch. |
Source code in veetility/utility_functions.py
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 | |
identify_match_multi_cols(df_1, df_2, df_1_cols_to_match, df_2_cols_to_match, match_col_name, exclude_values=None)
This function will identify if a row in df_1 is in df_2 based on the columns specified in df_1_cols_to_match and df_2_cols_to_match
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_1 |
pd.DataFrame
|
The first dataframe to identify matches in |
required |
df_2 |
pd.DataFrame
|
The second dataframe to identify matches in |
required |
df_1_cols_to_match |
list
|
The columns in df_1 to look for matches in df_2 |
required |
df_2_cols_to_match |
list
|
The columns in df_2 to look for matches in df_1 |
required |
match_col_name |
str
|
The name of the column to be created in df_1 to indicate if the row is in df_2 |
required |
exclude_values |
list
|
The values to be excluded from the match. Default is ['None', 'none', 'nan', ''] |
None
|
Source code in veetility/utility_functions.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
identify_paid_or_organic(df)
Identify whether a given dataframe contains paid data
Source code in veetility/utility_functions.py
832 833 834 835 836 837 838 839 840 841 | |
match_ads(df_1, df_2, df_1_exact_col, df_2_exact_col, extract_shortcode=False, df_1_fuzzy_col=None, df_2_fuzzy_col=None, is_exact_col_link=True, matched_col_name='boosted', merge=False, cols_to_merge=None, pickle_name='NoStore', fuzz_thresh=80)
Match row items in df_2 onto row items in df_1 based on two sets of columns,using exact and fuzzy matching.
First try to match the row items in df_1 using the first set of columns, if there is no match then try to match the row items in df_2 using the second set of columns and fuzzy matching. For example the first set of columns might be URLs, which tend to be exact matches, and the second set of columns might be post copy, which can have slight variations, for example the post copy from a tracker sheet might be slightly incorrect due to manual entry, therefore fuzzy matching with a threshold of how similar the strings need to be is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_1 |
DataFrame
|
The Dataframe that will be searched to see if any corresponding values in df_2, if merge=True df_2 will be left joined onto df_1 |
required |
df_2 |
DataFrame
|
The Dataframe that if merge = True will be left joined onto df_1 |
required |
df_1_exact_col |
str
|
The Column name from df_1 that will be first attempted to find exact matches |
required |
df_2_exact_col |
str
|
The Column name from df_2 that will be first attempted to find exact matches |
required |
extract_shortcode |
bool
|
Boolean Flag, if True then the exact match will be attempted on the shortcodes of the df_2_exact_col which should be a url |
False
|
df_1_fuzzy_col |
str
|
The Column name from df_1 that will be attempted to fuzzy match if there was no exact match before. |
None
|
df_2_fuzzy_col |
str
|
The Column name from df_2 that will be attempted to fuzzy match if there was no exact match before. |
None
|
is_exact_col_link |
bool
|
Boolean Flag, is the set of columns to be exact matched hyperlinks? If so they will be cleaned to remove utm parameters. |
True
|
matched_col_name |
str
|
String to name the column which will contain boolean values to indicate whether row items in df_1 found a match in df_2. |
'boosted'
|
merge |
bool
|
Boolean Flag, if true then df_2 will be left joined onto df_1. Else df_1 will be left unchanged apart from column indicating whether there is a match. |
False
|
cols_to_merge |
list, str
|
List of strings to merge on if 'merge' = True. |
None
|
pickle_name |
str
|
Name of the dictionary of best matches found by fuzzy matching to be stored as a pickle file. The next time the function is run with the same pickle_name, the pickle file is used to find matches without having to do slow fuzzy matching from scratch. |
'NoStore'
|
fuzz_thresh |
int
|
Fuzzy matching threshold value between 0 and 100 |
80
|
Returns:
| Name | Type | Description |
|---|---|---|
df_1 |
DataFrame
|
The original df_1 with just a column to indicate whether a match has occured if merge = False else df_1 will have df_2 left joined on. |
df_2 |
DataFrame
|
The original df_2 with cleaned columns and 'Match String' column to help quality check why some rows have or haven't matched. |
df_2_no_match |
DataFrame
|
A dataframe of df_2 row items that haven't found a match in df_1. |
Source code in veetility/utility_functions.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
match_shortcode_to_url(shortcode_list, url_list)
Matches a list of shortcodes to a list of urls, creates a dictionary of the matches.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shortcode_list |
list
|
A list of shortcodes to match to urls. |
required |
url_list |
list
|
A list of urls to match to shortcodes. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
url_shortcode_dict |
dict
|
A dictionary of the matches between shortcodes and urls. |
Source code in veetility/utility_functions.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 | |
merge_match_perc(df_1, df_2, left_on=None, right_on=None, on=None, how='left', tag='', ignore_values_df2=None)
Merges two dataframes and prints out the number of matches and the percentage of matches out of the total number of rows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df_1 |
pd.DataFrame
|
The first dataframe to merge |
required |
df_2 |
pd.DataFrame
|
The second dataframe to merge |
required |
left_on |
str
|
The column name to merge on in the first dataframe |
None
|
right_on |
str
|
The column name to merge on in the second dataframe |
None
|
how |
str
|
The type of merge to perform. Defaults to 'left'. |
'left'
|
tag |
str
|
A tag to add to the print statement. Defaults to "". |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
output_df |
pd.DataFrame
|
A pandas dataframe that contains the merged data from the input dataframes. |
Source code in veetility/utility_functions.py
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
pickle_data(data, filename, folder='Pickled Files')
Pickle data and save it to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
Object
|
The data to be pickled. |
required |
filename |
str
|
The name of the file to save the pickled data to. |
required |
folder |
str
|
The folder to save the pickled file to. Defaults to "Pickled Files". |
'Pickled Files'
|
Source code in veetility/utility_functions.py
843 844 845 846 847 848 849 850 851 852 853 | |
prepare_string_matching(string, is_url=False, readable_form=False, ascii_characters='remove', remove_newlines=True)
Removing unnecessary detail, whitespaces and converting to lower case.
Prepare strings for matching say in a merge function by removing unnecessary detail, whitespaces and converting to lower case.
Remove URLs and emojis as sometimes they cannot come through properly in Tracer data
Replace non-ASCII characters with their closest ASCII equivalents
Parameters
-----------------
string : str
The string to be cleaned
is_url : bool
If True then remove URLs and characters after the '?' which are utm parameters
These can be present in some URLs we receive and not others
Returns
----------------
string : str
A cleaned string stripped of whitespace, punctuation, emojis, non-ASCII characters, and URLs.
Source code in veetility/utility_functions.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 | |
read_from_gsheet(workbook_name, sheet_name, clean_date=True, date_col=None, dayfirst=None, yearfirst=None, format=None, errors='raise')
Read data from a google sheet and return it as a dataframe.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workbook_name |
str
|
The name of the google sheet workbook. |
required |
sheet_name |
str
|
The name of the sheet to read data from. |
required |
clean_date |
bool
|
If true, the 'date_col' column will be converted to datetime format. (default: True) |
True
|
date_col |
str
|
The name of the column containing the date values to be cleaned. (default: 'EnterValue') |
None
|
dayfirst |
bool
|
Whether to interpret the first value in an ambiguous 3-integer date (e.g. 01/05/09) as the day (True) or month (False). (default: 'EnterValue') |
None
|
yearfirst |
bool
|
Similar to 'dayfirst', but for the year. (default: 'EnterValue') |
None
|
format |
str
|
The format of the date values. (default: None) |
None
|
errors |
str
|
The behavior when encountering errors in the date format. (default: 'raise') |
'raise'
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
pandas.DataFrame
|
The dataframe containing the data from the google sheets |
Source code in veetility/utility_functions.py
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 | |
read_from_postgresql(table_name, clean_date=True, date_col=None, dayfirst=None, yearfirst=None, format=None, errors='raise')
Reads a table from a PostgreSQL database table using a pscopg2 connection. If fails it waits 10 seconds and tries again.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name |
str
|
The name of the table to read from. |
required |
clean_date |
bool
|
Whether or not to clean the date column. Defaults to True. |
True
|
date_col |
str
|
The column name of the date column to clean. |
None
|
dayfirst |
str
|
The day first format for date parsing. |
None
|
yearfirst |
str
|
The year first format for date parsing. |
None
|
format |
str
|
The format for date parsing. Defaults to None. |
None
|
errors |
str
|
The behavior for date parsing errors. Defaults to 'raise'. |
'raise'
|
Returns:
| Name | Type | Description |
|---|---|---|
df |
pandas.DataFrame
|
The table data in a pandas dataframe. |
Source code in veetility/utility_functions.py
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 | |
read_json(file_name, file_type, folder='JSON Files')
Read a json file and return a Python object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_name |
str
|
The name of the json file to be read. |
required |
file_type |
str
|
The type of the object. It must be 'DataFrame', 'List' or 'Dictionary' |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Object | The object read from json file. |
Source code in veetility/utility_functions.py
896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 | |
store_daily_organic_data(df, output_table_name, num_days_to_store=30, date_col_name='date', dayfirst='EnterValue', yearfirst='EnterValue', format=None, errors='raise', check_created_col=True, created_col='created', refresh_lag=1, cumulative_metric_cols=None, unique_id_cols=None, require_run_after_hour=False, run_after_hour=15)
Converts a post level organic dataframe to a daily level dataframe and stores it in a PostGreSQL table.
Most organic data is stored at the post level and this function converts it to a daily level dataframe and stores it in a PostGreSQL table. It also converts the cumulative metrics to daily difference metrics. The date column is parsed through with the correct format, dayfirst and yearfirst values needing to be specified. If the table already exists then it checks the date_updated column to see if the data has already been updated today. If it has then it doesn't update the table. If it hasn't then it updates the table. If the table doesn't exist then it creates it. If the require_run_after_hour is set to True then it will only run if the current time is after the run_after_hour time which is in 24 hour format but only the hour is used. If check_created_col is set to True then it will only run if the created column is less than the refresh_lag days ago. This is to ensure that the data is up to date before it is stored. This "created" columns appears in databases from tracer and tells us when Tracer last updated the row items. Tracer is a day behind hence the refresh_lag of 1 day. The date_row_added column is added to the dataframe and is the date that the row was added to the data output_table. The date_first_tracked column is added to the dataframe and is the date that a unique post as defined by the unique_id_cols was first tracked The date_diff column is added to the dataframe and is the number of days difference between when the post was first tracked and when that particular row item was added to the data output_table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
The dataframe to be converted to a daily level dataframe and stored in a PostGreSQL table |
required |
output_table_name |
str
|
The name of the table to store the data in |
required |
num_days_to_store |
int
|
The number of days worth of data per post to store in the table. Defaults to 30. |
30
|
date_col_name |
str
|
The name of the date column in the dataframe that will be formatted. Defaults to 'date'. |
'date'
|
dayfirst |
str
|
Whether the day is the first value in the date column. Defaults to "EnterValue". |
'EnterValue'
|
yearfirst |
str
|
Whether the year is the first value in the date column. Defaults to "EnterValue". |
'EnterValue'
|
format |
str
|
The format of the date column. Defaults to None. |
None
|
errors |
str
|
How to handle errors in the date column. Defaults to 'raise'. |
'raise'
|
check_created_col |
bool
|
Whether to check the created column to ensure the data is up to date. Defaults to True. |
True
|
created_col |
str
|
The name of the created column. Defaults to 'created'. |
'created'
|
refresh_lag |
int
|
The number of days to check the created column is less than. Defaults to 1. |
1
|
cumulative_metric_cols |
list
|
The list of cumulative metrics to convert to daily difference metrics. Defaults to ['impressions','reach','video_views','reactions','comments','shares']. |
None
|
unique_id_cols |
list
|
The list of columns that uniquely identify a post. Defaults to None. |
None
|
require_run_after_hour |
bool
|
Whether to only run the function if the current time is after the run_after_hour time. Defaults to False. |
False
|
run_after_hour |
int
|
The hour of the day to run the function after in 24 hour format. Defaults to 15. |
15
|
Returns:
| Name | Type | Description |
|---|---|---|
None | the function writes the data to a postgresql table |
Source code in veetility/utility_functions.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 | |
table_exists(table_name)
Check if a table with the given name exists in the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
table_name |
str
|
name of the table to check for existence. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
bool | True if table exists, False otherwise. |
Source code in veetility/utility_functions.py
590 591 592 593 594 595 596 597 598 599 | |
unpickle_data(filename, folder='Pickled Files')
Load pickled data from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename |
str
|
The name of the file to load the pickled data from. |
required |
folder |
str
|
The folder where the pickled file is located. Defaults to "Pickled Files". |
'Pickled Files'
|
Returns:
| Name | Type | Description |
|---|---|---|
Object | The unpickled data |
Source code in veetility/utility_functions.py
856 857 858 859 860 861 862 863 864 865 | |
write_json(object, file_name, file_type, folder='JSON Files')
Write a Python object to a json file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object |
Object
|
The Python object to be written to a json file. |
required |
file_name |
str
|
The name of the json file to be created. |
required |
file_type |
str
|
The type of the object. It must be 'DataFrame', 'List' or 'Dictionary' |
required |
folder |
str
|
The folder to save the json file to. Defaults to "JSON Files". |
'JSON Files'
|
Source code in veetility/utility_functions.py
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 | |
write_to_gsheet(workbook_name, sheet_name, df, if_exists='replace', sheet_prefix='')
Write a dataframe to a google sheet
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
workbook_name |
str
|
The name of the google sheet workbook. |
required |
sheet_name |
str
|
The name of the sheet to write data to. |
required |
df |
pandas.DataFrame
|
The dataframe to be written to the google sheet. |
required |
if_exists |
str
|
Determines the behavior when the sheet already exists, options are 'replace' or 'append'. (default='replace') |
'replace'
|
sheet_prefix |
str
|
A prefix to be added to the sheet name. (default='') |
''
|
Returns:
| Type | Description |
|---|---|
None |
Source code in veetility/utility_functions.py
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 | |
write_to_postgresql(df, table_name, if_exists='replace')
Writes a dataframe to a PostgreSQL database table using a SQLalchemy engine defined elsewhere. If writing fails it waits 10 seconds then trys again
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df |
DataFrame
|
The Dataframe to send to the PostGreSQL table |
required |
table_name |
str
|
The name of the table to write the dataframe to |
required |
if_exists |
str
|
Either 'replace' or 'append' which describes what to do if a table with that name already exists |
'replace'
|
Returns:
| Name | Type | Description |
|---|---|---|
error_message |
str
|
An error message saying that the connection has failed |
Source code in veetility/utility_functions.py
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |