Best Python code snippet using localstack_python
extract_currency_history_csv.py
Source:extract_currency_history_csv.py  
...771                or (self.what == "1234" and (myString in "1234")) \772                or (self.what == "CURR"):773            if ((self.getLength() + len(myString)) <= self.limit):774                super(JTextFieldLimitYN, self).insertString(myOffset, myString, myAttr)                         # noqa775def fix_delimiter( theDelimiter ):776    try:777        if sys.version_info.major >= 3: return theDelimiter778        if sys.version_info.major <  2: return str(theDelimiter)779        if sys.version_info.minor >  7: return theDelimiter780        if sys.version_info.minor <  7: return str(theDelimiter)781        if sys.version_info.micro >= 2: return theDelimiter782    except:783        pass784    return str( theDelimiter )785def get_StuWareSoftSystems_parameters_from_file():786    global debug, myParameters, lPickle_version_warning, version_build, _resetParameters                            # noqa787    myPrint("D", "In ", inspect.currentframe().f_code.co_name, "()" )788    if _resetParameters:789        myPrint("B", "User has specified to reset parameters... keeping defaults and skipping pickle()")790        myParameters = {}791        return792    myFile = "StuWareSoftSystems.dict"793    old_dict_filename = os.path.join("..", myFile)794    # Pickle was originally encrypted, no need, migrating to unencrypted795    migratedFilename = os.path.join(moneydance_data.getRootFolder().getAbsolutePath(),myFile)796    myPrint("DB", "Now checking for parameter file:", migratedFilename)797    if os.path.exists( migratedFilename ):798        myPrint("DB", "loading parameters from non-encrypted Pickle file:", migratedFilename)799        myPrint("DB", "Parameter file", migratedFilename, "exists..")800        # Open the file801        try:802            istr = FileInputStream(migratedFilename)803            load_file = FileUtil.wrap(istr)804            # noinspection PyTypeChecker805            myParameters = pickle.load(load_file)806            load_file.close()807        except FileNotFoundException:808            myPrint("B", "Error: failed to find parameter file...")809            myParameters = None810        except EOFError:811            myPrint("B", "Error: reached EOF on parameter file....")812            myParameters = None813        except:814            myPrint("B","Error opening Pickle File (will try encrypted version) - Unexpected error ", sys.exc_info()[0])815            myPrint("B","Error opening Pickle File (will try encrypted version) - Unexpected error ", sys.exc_info()[1])816            myPrint("B","Error opening Pickle File (will try encrypted version) - Line Number: ", sys.exc_info()[2].tb_lineno)817            # OK, so perhaps from older version - encrypted, try to read818            try:819                local_storage = moneydance.getCurrentAccountBook().getLocalStorage()820                istr = local_storage.openFileForReading(old_dict_filename)821                load_file = FileUtil.wrap(istr)822                # noinspection PyTypeChecker823                myParameters = pickle.load(load_file)824                load_file.close()825                myPrint("B","Success loading Encrypted Pickle file - will migrate to non encrypted")826                lPickle_version_warning = True827            except:828                myPrint("B","Opening Encrypted Pickle File - Unexpected error ", sys.exc_info()[0])829                myPrint("B","Opening Encrypted Pickle File - Unexpected error ", sys.exc_info()[1])830                myPrint("B","Error opening Pickle File - Line Number: ", sys.exc_info()[2].tb_lineno)831                myPrint("B", "Error: Pickle.load() failed.... Is this a restored dataset? Will ignore saved parameters, and create a new file...")832                myParameters = None833        if myParameters is None:834            myParameters = {}835            myPrint("DB","Parameters did not load, will keep defaults..")836        else:837            myPrint("DB","Parameters successfully loaded from file...")838    else:839        myPrint("J", "Parameter Pickle file does not exist - will use default and create new file..")840        myPrint("D", "Parameter Pickle file does not exist - will use default and create new file..")841        myParameters = {}842    if not myParameters: return843    myPrint("DB","myParameters read from file contains...:")844    for key in sorted(myParameters.keys()):845        myPrint("DB","...variable:", key, myParameters[key])846    if myParameters.get("debug") is not None: debug = myParameters.get("debug")847    if myParameters.get("lUseMacFileChooser") is not None:848        myPrint("B", "Detected old lUseMacFileChooser parameter/variable... Will delete it...")849        myParameters.pop("lUseMacFileChooser", None)  # Old variable - not used - delete from parameter file850    myPrint("DB","Parameter file loaded if present and myParameters{} dictionary set.....")851    # Now load into memory!852    load_StuWareSoftSystems_parameters_into_memory()853    return854def save_StuWareSoftSystems_parameters_to_file():855    global debug, myParameters, lPickle_version_warning, version_build856    myPrint("D", "In ", inspect.currentframe().f_code.co_name, "()" )857    if myParameters is None: myParameters = {}858    # Don't forget, any parameters loaded earlier will be preserved; just add changed variables....859    myParameters["__Author"] = "Stuart Beesley - (c) StuWareSoftSystems"860    myParameters["debug"] = debug861    dump_StuWareSoftSystems_parameters_from_memory()862    myFile = "StuWareSoftSystems.dict"863    # Pickle was originally encrypted, no need, migrating to unencrypted864    migratedFilename = os.path.join(moneydance_data.getRootFolder().getAbsolutePath(),myFile)865    myPrint("DB","Will try to save parameter file:", migratedFilename)866    ostr = FileOutputStream(migratedFilename)867    myPrint("DB", "about to Pickle.dump and save parameters to unencrypted file:", migratedFilename)868    try:869        save_file = FileUtil.wrap(ostr)870        # noinspection PyTypeChecker871        pickle.dump(myParameters, save_file)872        save_file.close()873        myPrint("DB","myParameters now contains...:")874        for key in sorted(myParameters.keys()):875            myPrint("DB","...variable:", key, myParameters[key])876    except:877        myPrint("B", "Error - failed to create/write parameter file.. Ignoring and continuing.....")878        dump_sys_error_to_md_console_and_errorlog()879        return880    myPrint("DB","Parameter file written and parameters saved to disk.....")881    return882# END COMMON DEFINITIONS ###############################################################################################883# END COMMON DEFINITIONS ###############################################################################################884# END COMMON DEFINITIONS ###############################################################################################885# >>> CUSTOMISE & DO THIS FOR EACH SCRIPT886def load_StuWareSoftSystems_parameters_into_memory():887    global debug, myParameters, lPickle_version_warning, version_build888    # >>> THESE ARE THIS SCRIPT's PARAMETERS TO LOAD889    global __extract_currency_history_csv890    global lStripASCII, csvDelimiter, scriptpath, userdateformat891    global lSimplify_ECH, userdateStart_ECH, userdateEnd_ECH, hideHiddenCurrencies_ECH892    global lWriteBOMToExportFile_SWSS                                                                                  # noqa893    myPrint("D", "In ", inspect.currentframe().f_code.co_name, "()" )894    myPrint("DB", "Loading variables into memory...")895    if myParameters is None: myParameters = {}896    if myParameters.get("__extract_currency_history_csv") is not None: __extract_currency_history_csv = myParameters.get("__extract_currency_history_csv")897    if myParameters.get("userdateformat") is not None: userdateformat = myParameters.get("userdateformat")898    if myParameters.get("lStripASCII") is not None: lStripASCII = myParameters.get("lStripASCII")899    if myParameters.get("csvDelimiter") is not None: csvDelimiter = myParameters.get("csvDelimiter")900    if myParameters.get("lSimplify_ECH") is not None: lSimplify_ECH = myParameters.get("lSimplify_ECH")901    if myParameters.get("userdateStart_ECH") is not None: userdateStart_ECH = myParameters.get("userdateStart_ECH")902    if myParameters.get("userdateEnd_ECH") is not None: userdateEnd_ECH = myParameters.get("userdateEnd_ECH")903    if myParameters.get("hideHiddenCurrencies_ECH") is not None: hideHiddenCurrencies_ECH = myParameters.get("hideHiddenCurrencies_ECH")904    if myParameters.get("lWriteBOMToExportFile_SWSS") is not None: lWriteBOMToExportFile_SWSS = myParameters.get("lWriteBOMToExportFile_SWSS")                                                                                  # noqa905    if myParameters.get("scriptpath") is not None:906        scriptpath = myParameters.get("scriptpath")907        if not os.path.isdir(scriptpath):908            myPrint("DB", "Warning: loaded parameter scriptpath does not appear to be a valid directory:", scriptpath, "will ignore")909            scriptpath = ""910    myPrint("DB","myParameters{} set into memory (as variables).....")911    return912# >>> CUSTOMISE & DO THIS FOR EACH SCRIPT913def dump_StuWareSoftSystems_parameters_from_memory():914    global debug, myParameters, lPickle_version_warning, version_build915    # >>> THESE ARE THIS SCRIPT's PARAMETERS TO SAVE916    global __extract_currency_history_csv917    global lStripASCII, csvDelimiter, scriptpath918    global lDisplayOnly, userdateformat919    global lSimplify_ECH, userdateStart_ECH, userdateEnd_ECH, hideHiddenCurrencies_ECH920    global lWriteBOMToExportFile_SWSS                                                                                  # noqa921    myPrint("D", "In ", inspect.currentframe().f_code.co_name, "()" )922    # NOTE: Parameters were loaded earlier on... Preserve existing, and update any used ones...923    # (i.e. other StuWareSoftSystems programs might be sharing the same file)924    if myParameters is None: myParameters = {}925    myParameters["__extract_currency_history_csv"] = version_build926    myParameters["userdateformat"] = userdateformat927    myParameters["lStripASCII"] = lStripASCII928    myParameters["csvDelimiter"] = csvDelimiter929    myParameters["lSimplify_ECH"] = lSimplify_ECH930    myParameters["userdateStart_ECH"] = userdateStart_ECH931    myParameters["userdateEnd_ECH"] = userdateEnd_ECH932    myParameters["hideHiddenCurrencies_ECH"] = hideHiddenCurrencies_ECH933    myParameters["lWriteBOMToExportFile_SWSS"] = lWriteBOMToExportFile_SWSS934    if not lDisplayOnly and scriptpath != "" and os.path.isdir(scriptpath):935        myParameters["scriptpath"] = scriptpath936    myPrint("DB","variables dumped from memory back into myParameters{}.....")937    return938get_StuWareSoftSystems_parameters_from_file()939myPrint("DB", "DEBUG IS ON..")940# END ALL CODE COPY HERE ###############################################################################################941moneydance_ui.firstMainFrame.setStatus(">> StuWareSoftSystems - %s launching......." %(myScriptName),0)942# Create fake JFrame() so that all popups have correct Moneydance Icons etc943extract_currency_history_csv_fake_frame_ = JFrame()944if (not Platform.isMac()):945    moneydance_ui.getImages()946    extract_currency_history_csv_fake_frame_.setIconImage(MDImages.getImage(moneydance_ui.getMain().getSourceInformation().getIconResource()))947extract_currency_history_csv_fake_frame_.setUndecorated(True)948extract_currency_history_csv_fake_frame_.setVisible(False)949extract_currency_history_csv_fake_frame_.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE)950csvfilename = None951if decimalCharSep != "." and csvDelimiter == ",": csvDelimiter = ";"  # Override for EU countries or where decimal point is actually a comma...952myPrint("DB", "Decimal point:", decimalCharSep, "Grouping Separator", groupingCharSep, "CSV Delimiter set to:", csvDelimiter)953sdf = SimpleDateFormat("dd/MM/yyyy")954dateStrings=["dd/mm/yyyy", "mm/dd/yyyy", "yyyy/mm/dd", "yyyymmdd"]955# 1=dd/mm/yyyy, 2=mm/dd/yyyy, 3=yyyy/mm/dd, 4=yyyymmdd956label1 = JLabel("Select Output Date Format (default yyyy/mm/dd):")957user_dateformat = JComboBox(dateStrings)958if userdateformat == "%d/%m/%Y": user_dateformat.setSelectedItem("dd/mm/yyyy")959elif userdateformat == "%m/%d/%Y": user_dateformat.setSelectedItem("mm/dd/yyyy")960elif userdateformat == "%Y%m%d": user_dateformat.setSelectedItem("yyyymmdd")961else: user_dateformat.setSelectedItem("yyyy/mm/dd")962labelDateStart = JLabel("Date range start (enter as yyyy/mm/dd):")963user_selectDateStart = JDateField(CustomDateFormat("ymd"),15)   # Use MD API function (not std Python)964user_selectDateStart.setDateInt(userdateStart_ECH)965labelDateEnd = JLabel("Date range end (enter as yyyy/mm/dd):")966user_selectDateEnd = JDateField(CustomDateFormat("ymd"),15)   # Use MD API function (not std Python)967user_selectDateEnd.setDateInt(userdateEnd_ECH)968# user_selectDateEnd.gotoToday()969labelSimplify = JLabel("Simplify extract?")970user_selectSimplify = JCheckBox("", lSimplify_ECH)971labelHideHiddenCurrencies = JLabel("Hide Hidden Currencies?")972user_selectHideHiddenCurrencies = JCheckBox("", hideHiddenCurrencies_ECH)973label2 = JLabel("Strip non ASCII characters from CSV export?")974user_selectStripASCII = JCheckBox("", lStripASCII)975delimStrings = [";","|",","]976label3 = JLabel("Change CSV Export Delimiter from default to: ';|,'")977user_selectDELIMITER = JComboBox(delimStrings)978user_selectDELIMITER.setSelectedItem(csvDelimiter)979labelBOM = JLabel("Write BOM (Byte Order Mark) to file (helps Excel open files)?")980user_selectBOM = JCheckBox("", lWriteBOMToExportFile_SWSS)981label4 = JLabel("Turn DEBUG Verbose messages on?")982user_selectDEBUG = JCheckBox("", debug)983userFilters = JPanel(GridLayout(0, 2))984userFilters.add(label1)985userFilters.add(user_dateformat)986userFilters.add(labelDateStart)987userFilters.add(user_selectDateStart)988userFilters.add(labelDateEnd)989userFilters.add(user_selectDateEnd)990userFilters.add(labelSimplify)991userFilters.add(user_selectSimplify)992userFilters.add(labelHideHiddenCurrencies)993userFilters.add(user_selectHideHiddenCurrencies)994userFilters.add(label2)995userFilters.add(user_selectStripASCII)996userFilters.add(label3)997userFilters.add(user_selectDELIMITER)998userFilters.add(labelBOM)999userFilters.add(user_selectBOM)1000userFilters.add(label4)1001userFilters.add(user_selectDEBUG)1002lExit = False1003lDisplayOnly = False1004options = ["Abort", "CSV Export"]1005while True:1006    userAction = (JOptionPane.showOptionDialog(extract_currency_history_csv_fake_frame_, userFilters, "%s(build: %s) Set Script Parameters...."%(myScriptName,version_build),1007                                         JOptionPane.OK_CANCEL_OPTION,1008                                         JOptionPane.QUESTION_MESSAGE,1009                                         moneydance_ui.getIcon("/com/moneydance/apps/md/view/gui/glyphs/appicon_64.png"),1010                                         options, options[1]))1011    if userAction != 1:1012        myPrint("B", "User Cancelled Parameter selection.. Will abort..")1013        myPopupInformationBox(extract_currency_history_csv_fake_frame_, "User Cancelled Parameter selection.. Will abort..", "PARAMETERS")1014        lDisplayOnly = False1015        lExit = True1016        break1017    if user_selectDateStart.getDateInt() <= user_selectDateEnd.getDateInt() \1018            and user_selectDateEnd.getDateInt() >= user_selectDateStart.getDateInt():1019        break   # Valid date range1020    myPrint("P","Error - date range incorrect, please try again...")1021    user_selectDateStart.setForeground(Color.RED)1022    user_selectDateEnd.setForeground(Color.RED)1023    continue   # Loop1024if not lExit:1025    myPrint("DB", "Parameters Captured",1026        "User Date Format:", user_dateformat.getSelectedItem(),1027        "Simplify:", user_selectSimplify.isSelected(),1028        "Hide Hidden Currencies:", user_selectHideHiddenCurrencies.isSelected(),1029        "Start date:", user_selectDateStart.getDateInt(),1030        "End date:", user_selectDateEnd.getDateInt(),1031        "Strip ASCII:", user_selectStripASCII.isSelected(),1032        "Write BOM to file:", user_selectBOM.isSelected(),1033        "Verbose Debug Messages: ", user_selectDEBUG.isSelected(),1034        "CSV File Delimiter:", user_selectDELIMITER.getSelectedItem())1035    # endif1036    if user_dateformat.getSelectedItem() == "dd/mm/yyyy": userdateformat = "%d/%m/%Y"1037    elif user_dateformat.getSelectedItem() == "mm/dd/yyyy": userdateformat = "%m/%d/%Y"1038    elif user_dateformat.getSelectedItem() == "yyyy/mm/dd": userdateformat = "%Y/%m/%d"1039    elif user_dateformat.getSelectedItem() == "yyyymmdd": userdateformat = "%Y%m%d"1040    else:1041        # PROBLEM /  default1042        userdateformat = "%Y/%m/%d"1043    lSimplify_ECH = user_selectSimplify.isSelected()1044    hideHiddenCurrencies_ECH = user_selectHideHiddenCurrencies.isSelected()1045    userdateStart_ECH = user_selectDateStart.getDateInt()1046    userdateEnd_ECH = user_selectDateEnd.getDateInt()1047    lStripASCII = user_selectStripASCII.isSelected()1048    csvDelimiter = user_selectDELIMITER.getSelectedItem()1049    if csvDelimiter == "" or (not (csvDelimiter in ";|,")):1050        myPrint("B", "Invalid Delimiter:", csvDelimiter, "selected. Overriding with:','")1051        csvDelimiter = ","1052    if decimalCharSep == csvDelimiter:1053        myPrint("B", "WARNING: The CSV file delimiter:", csvDelimiter, "cannot be the same as your decimal point character:", decimalCharSep, " - Proceeding without file export!!")1054        lDisplayOnly = True1055        myPopupInformationBox(None, "ERROR - The CSV file delimiter: %s ""cannot be the same as your decimal point character: %s. "1056                                    "Proceeding without file export (i.e. I will do nothing)!!" %(csvDelimiter, decimalCharSep),1057                                    "INVALID FILE DELIMITER", theMessageType=JOptionPane.ERROR_MESSAGE)1058    lWriteBOMToExportFile_SWSS = user_selectBOM.isSelected()1059    debug = user_selectDEBUG.isSelected()1060    myPrint("DB", "DEBUG turned ON")1061    myPrint("B","User Parameters...")1062    if lSimplify_ECH:1063        myPrint("B","Simplifying extract")1064    else:1065        myPrint("B","Providing a detailed extract")1066    myPrint("B","user date format....:", userdateformat)1067    myPrint("B", "Selected start date:", userdateStart_ECH)1068    myPrint("B", "Selected end date:", userdateEnd_ECH)1069    if hideHiddenCurrencies_ECH:1070        myPrint("B", "Hiding hidden currencies...")1071    # Now get the export filename1072    csvfilename = None1073    if not lDisplayOnly:  # i.e. we have asked for a file export - so get the filename1074        if lStripASCII:1075            myPrint("B","Will strip non-ASCII characters - e.g. Currency symbols from output file...", " Using Delimiter:", csvDelimiter)1076        else:1077            myPrint("B","Non-ASCII characters will not be stripped from file: ", " Using Delimiter:", csvDelimiter)1078        if lWriteBOMToExportFile_SWSS:1079            myPrint("B", "Script will add a BOM (Byte Order Mark) to front of the extracted file...")1080        else:1081            myPrint("B", "No BOM (Byte Order Mark) will be added to the extracted file...")1082        def grabTheFile():1083            global debug, lDisplayOnly, csvfilename, lIamAMac, scriptpath, myScriptName1084            myPrint("D", "In ", inspect.currentframe().f_code.co_name, "()")1085            if scriptpath == "" or scriptpath is None:  # No parameter saved / loaded from disk1086                scriptpath = myDir()1087            myPrint("DB", "Default file export output path is....:", scriptpath)1088            csvfilename = ""1089            if lIamAMac:1090                myPrint("DB", "MacOS X detected: Therefore I will run FileDialog with no extension filters to get filename....")1091                # jFileChooser hangs on Mac when using file extension filters, also looks rubbish. So using Mac(ish)GUI1092                System.setProperty("com.apple.macos.use-file-dialog-packages", "true")  # In theory prevents access to app file structure (but doesnt seem to work)1093                System.setProperty("apple.awt.fileDialogForDirectories", "false")1094            filename = FileDialog(extract_currency_history_csv_fake_frame_, "Select/Create CSV file for extract (CANCEL=NO EXPORT)")1095            filename.setMultipleMode(False)1096            filename.setMode(FileDialog.SAVE)1097            filename.setFile(extract_filename)1098            if (scriptpath is not None and scriptpath != ""): filename.setDirectory(scriptpath)1099            # Copied from MD code... File filters only work on non Macs (or Macs below certain versions)1100            if (not Platform.isOSX() or not Platform.isOSXVersionAtLeast("10.13")):1101                extfilter = ExtFilenameFilter("csv")1102                filename.setFilenameFilter(extfilter)  # I'm not actually sure this works...?1103            filename.setVisible(True)1104            csvfilename = filename.getFile()1105            if (csvfilename is None) or csvfilename == "":1106                lDisplayOnly = True1107                csvfilename = None1108                myPrint("B", "User chose to cancel or no file selected >>  So no Extract will be performed... ")1109                myPopupInformationBox(extract_currency_history_csv_fake_frame_,"User chose to cancel or no file selected >>  So no Extract will be performed... ","FILE SELECTION")1110            elif str(csvfilename).endswith(".moneydance"):1111                myPrint("B", "User selected file:", csvfilename)1112                myPrint("B", "Sorry - User chose to use .moneydance extension - I will not allow it!... So no Extract will be performed...")1113                myPopupInformationBox(extract_currency_history_csv_fake_frame_,"Sorry - User chose to use .moneydance extension - I will not allow it!... So no Extract will be performed...","FILE SELECTION")1114                lDisplayOnly = True1115                csvfilename = None1116            elif ".moneydance" in filename.getDirectory():1117                myPrint("B", "User selected file:", filename.getDirectory(), csvfilename)1118                myPrint("B", "Sorry - FileDialog() User chose to save file in .moneydance location. NOT Good practice so I will not allow it!... So no Extract will be performed...")1119                myPopupInformationBox(extract_currency_history_csv_fake_frame_,"Sorry - FileDialog() User chose to save file in .moneydance location. NOT Good practice so I will not allow it!... So no Extract will be performed...","FILE SELECTION")1120                lDisplayOnly = True1121                csvfilename = None1122            else:1123                csvfilename = os.path.join(filename.getDirectory(), filename.getFile())1124                scriptpath = str(filename.getDirectory())1125            if not lDisplayOnly:1126                if os.path.exists(csvfilename) and os.path.isfile(csvfilename):1127                    myPrint("D", "WARNING: file exists,but assuming user said OK to overwrite..")1128            if not lDisplayOnly:1129                if check_file_writable(csvfilename):1130                    if lStripASCII:1131                        myPrint("B", 'Will extract to file: ', csvfilename, "(NOTE: Should drop non utf8 characters...)")1132                    else:1133                        myPrint("B", 'Will extract to file: ', csvfilename, "...")1134                    scriptpath = os.path.dirname(csvfilename)1135                else:1136                    myPrint("B", "Sorry - I just checked and you do not have permissions to create this file:", csvfilename)1137                    myPopupInformationBox(extract_currency_history_csv_fake_frame_,"Sorry - I just checked and you do not have permissions to create this file: %s" %csvfilename,"FILE SELECTION")1138                    csvfilename=""1139                    lDisplayOnly = True1140            return1141        # enddef1142        if not lDisplayOnly: grabTheFile()1143    else:1144        pass1145    # endif1146    if csvfilename is None:1147        lDisplayOnly = True1148        myPrint("B", "No Export will be performed")1149    if not lDisplayOnly:1150        save_StuWareSoftSystems_parameters_to_file()1151        myPrint("P", "\nScript running to extract your currency rate history....")1152        myPrint("P", "-------------------------------------------------------------------")1153        if moneydance_data is None:1154            myPrint("B", "MD Data file is empty - no data to scan - aborting...")1155            myPopupInformationBox(extract_currency_history_csv_fake_frame_,"MD Data file is empty - no data to scan - aborting...","EMPTY DATASET")1156            extract_currency_history_csv_fake_frame_.dispose()1157            raise(Exception("MD Data file is empty - no data to scan - aborting..."))1158        header = ["CurrencyName",1159                  "CurrencyID",1160                  "isBase",1161                  "DecimalPlaces",1162                  "Prefix",1163                  "Suffix",1164                  "CurrentRateR2B",1165                  "CurrentRateB2R",1166                  "Snap_Date",1167                  "Snap_DailyRateR2B",1168                  "Snap_DailyRateB2R"]1169        def list_currency_rate_history():1170            global hideHiddenCurrencies_ECH, lSimplify_ECH, userdateStart_ECH, userdateEnd_ECH1171            curr_table=[]1172            currencies = moneydance.getCurrentAccountBook().getCurrencies()1173            baseCurr = currencies.getBaseType()1174            myPrint("P","\nIterating the currency table...")1175            for curr in currencies:1176                # noinspection PyUnresolvedReferences1177                if curr.getCurrencyType() != CurrencyType.Type.CURRENCY: continue   # Skip if not on a Currency record (i.e. a Security)1178                if hideHiddenCurrencies_ECH and curr.getHideInUI(): continue   # Skip if hidden in MD1179                myPrint("P","Currency: %s %s" %(curr, curr.getPrefix()) )1180                currSnapshots = curr.getSnapshots()1181                if not lSimplify_ECH and not len(currSnapshots) and curr == baseCurr:1182                    row = []1183                    row.append((curr.getName()))1184                    row.append((curr.getIDString()))1185                    row.append(curr == baseCurr)1186                    row.append(curr.getDecimalPlaces())1187                    row.append((curr.getPrefix()))1188                    row.append((curr.getSuffix()))1189                    row.append(1)1190                    row.append(1)1191                    row.append(None)1192                    row.append(None)1193                    row.append(None)1194                    curr_table.append(row)1195                # noinspection PyUnusedLocal1196                dpc = curr.getDecimalPlaces()1197                dpc = 8   # Override to 8dpc1198                for currSnapshot in currSnapshots:1199                    if currSnapshot.getDateInt() < userdateStart_ECH \1200                            or currSnapshot.getDateInt() > userdateEnd_ECH:1201                        continue   # Skip if out of date range1202                    row = []1203                    row.append((curr.getName()))1204                    row.append((curr.getIDString()))1205                    row.append(curr == baseCurr)1206                    row.append(curr.getDecimalPlaces())1207                    row.append((curr.getPrefix()))1208                    row.append((curr.getSuffix()))1209                    row.append(round(float(curr.getParameter("rate", None)),dpc))1210                    row.append(round(1/float(curr.getParameter("rate", None)),dpc))1211                    # I don't print relative currency as it's supposed to always be None or = Base..1212                    row.append(currSnapshot.getDateInt())1213                    row.append(round(float(currSnapshot.getRate()),dpc))1214                    row.append(round(1/float(currSnapshot.getRate()),dpc))1215                    curr_table.append(row)1216            return curr_table1217        currencyTable = list_currency_rate_history()1218        def ExportDataToFile(theTable, header):                                                                 # noqa1219            global debug, csvfilename, decimalCharSep, groupingCharSep, csvDelimiter, version_build, myScriptName1220            global sdf, userdateformat, lGlobalErrorDetected1221            global lWriteBOMToExportFile_SWSS1222            myPrint("D", "In ", inspect.currentframe().f_code.co_name, "()")1223            _CURRNAME = 01224            _CURRID = 11225            _SYMB =41226            _SNAPDATE = 81227            # NOTE - You can add sep=; to beginning of file to tell Excel what delimiter you are using1228            if True:1229                theTable = sorted(theTable, key=lambda x: (str(x[_CURRNAME]).upper(),x[_SNAPDATE]))1230            myPrint("P", "Now pre-processing the file to convert integer dates to 'formatted' dates....")1231            for row in theTable:1232                try:1233                    if row[_SNAPDATE]:1234                        dateasdate = datetime.datetime.strptime(str(row[_SNAPDATE]),"%Y%m%d")  # Convert to Date field1235                        dateoutput = dateasdate.strftime(userdateformat)1236                        row[_SNAPDATE] = dateoutput1237                except:1238                    myPrint("B","Error on row below with curr:", row[_CURRNAME], "snap date:", row[_SNAPDATE])1239                    myPrint("B",row)1240                    continue1241                if lStripASCII:1242                    for col in range(0, len(row)):1243                        row[col] = fixFormatsStr(row[col])1244            theTable.insert(0,header)  # Insert Column Headings at top of list. A bit rough and ready, not great coding, but a short list...!1245            # Write the theTable to a file1246            myPrint("B", "Opening file and writing ", len(theTable), " records")1247            try:1248                # CSV Writer will take care of special characters / delimiters within fields by wrapping in quotes that Excel will decode1249                # with open(csvfilename,"wb") as csvfile:  # PY2.7 has no newline parameter so opening in binary; just use "w" and newline='' in PY3.01250                with open(csvfilename,"wb") as csvfile:  # PY2.7 has no newline parameter so opening in binary; just use "w" and newline='' in PY3.01251                    if lWriteBOMToExportFile_SWSS:1252                        csvfile.write(codecs.BOM_UTF8)   # This 'helps' Excel open file with double-click as UTF-81253                    writer = csv.writer(csvfile, dialect='excel', quoting=csv.QUOTE_MINIMAL, delimiter=fix_delimiter(csvDelimiter))1254                    if csvDelimiter != ",":1255                        writer.writerow(["sep=",""])  # Tells Excel to open file with the alternative delimiter (it will add the delimiter to this line)1256                    if not lSimplify_ECH:1257                        for i in range(0, len(theTable)):1258                            try:1259                                writer.writerow( theTable[i] )1260                            except:1261                                myPrint("B","Error writing row %s to file... Older Jython version?" %i)1262                                myPrint("B","Row: ",theTable[i])1263                                myPrint("B","Will attempt coding back to str()..... Let's see if this fails?!")1264                                for _col in range(0, len(theTable[i])):1265                                    theTable[i][_col] = fix_delimiter(theTable[i][_col])1266                                writer.writerow( theTable[i] )1267                        # NEXT1268                        today = Calendar.getInstance()1269                        writer.writerow([""])1270                        writer.writerow(["StuWareSoftSystems - " + myScriptName + "(build: "1271                                         + version_build1272                                         + ")  Moneydance Python Script - Date of Extract: "1273                                         + str(sdf.format(today.getTime()))])1274                        writer.writerow([""])1275                        writer.writerow(["Dataset path/name: %s" %(moneydance_data.getRootFolder()) ])1276                        writer.writerow([""])1277                        writer.writerow(["User Parameters..."])1278                        writer.writerow(["Simplify Extract...........: %s" %(lSimplify_ECH)])1279                        writer.writerow(["Hiding Hidden Currencies...: %s" %(hideHiddenCurrencies_ECH)])1280                        writer.writerow(["Date format................: %s" %(userdateformat)])1281                        writer.writerow(["Date Range Selected........: "+str(userdateStart_ECH) + " to " +str(userdateEnd_ECH)])1282                    else:1283                        # Simplify is for my tester 'buddy' DerekKent23 - it's actually an MS Money Import format1284                        lCurr = None1285                        for row in theTable[1:]:1286                            # Write the table, but swap in the raw numbers (rather than formatted number strings)1287                            if row[_CURRNAME] != lCurr:1288                                if lCurr: writer.writerow("")1289                                lCurr = row[_CURRNAME]1290                                writer.writerow( [fix_delimiter(row[ _CURRNAME])1291                                                  +" - "+fix_delimiter(row[_CURRID])1292                                                  +" - "+fix_delimiter(row[_SYMB])1293                                                  +fix_delimiter(row[_SYMB+1])] )1294                                writer.writerow(["Date","Base to Rate","Rate to Base"])1295                            writer.writerow([row[_SNAPDATE],1296                                            row[_SNAPDATE+1],1297                                            row[_SNAPDATE+2]])1298                        # NEXT1299                myPrint("B", "CSV file " + csvfilename + " created, records written, and file closed..")1300            except IOError, e:1301                lGlobalErrorDetected = True1302                myPrint("B", "Oh no - File IO Error!", e)1303                myPrint("B", "Path:", csvfilename)1304                myPrint("B", "!!! ERROR - No file written - sorry! (was file open, permissions etc?)".upper())1305                dump_sys_error_to_md_console_and_errorlog()1306                myPopupInformationBox(extract_currency_history_csv_fake_frame_,"Sorry - error writing to export file!", "FILE EXTRACT")1307        # enddef...fix_connect_example.py
Source:fix_connect_example.py  
1import time2import quickfix3import quickfix444def messageToString(message):5    fix_delimiter = chr(1)6    return message.toString().replace(fix_delimiter, "|")7def securityListRequest(sessionID):8    # https://www.onixs.biz/fix-dictionary/4.4/msgtype_x_120.html9    message = quickfix44.SecurityListRequest()10    message.setField(quickfix.SecurityReqID("TEST"))11    message.setField(quickfix.SecurityListRequestType(4))  # All securities12    return message13def marketDataRequest(sessionID):14    # https://www.onixs.biz/fix-dictionary/4.4/msgtype_v_86.html15    message = quickfix44.MarketDataRequest()16    header = message.getHeader()17    target = sessionID.getTargetCompID().getString()18    fix_version = "FIX.4.4"19    header.setField(quickfix.BeginString(fix_version))20    header.setField(quickfix.SenderCompID(target))21    header.setField(quickfix.TargetCompID("CRYPTOCOMPARE"))22    header.setField(quickfix.MsgType("V"))  # V for market data request23    message.setField(quickfix.MDReqID("TEST"))24    message.setField(quickfix.SubscriptionRequestType("0"))  # snapshots25    message.setField(quickfix.MarketDepth(1))  # Top of book26    # http://www.quickfixengine.org/quickfix/doc/html/repeating_groups.html27    group_md = quickfix44.MarketDataRequest().NoMDEntryTypes()28    group_md.setField(quickfix.MDEntryType("0"))  # Top of book29    message.addGroup(group_md)30    group_sym = quickfix44.MarketDataRequest().NoRelatedSym()31    group_sym.setField(quickfix.Symbol("coinbase~btc~usd"))32    message.addGroup(group_sym)33    return message34class Application(quickfix.Application):35    # http://www.quickfixengine.org/quickfix/doc/html/application.html36    def onCreate(self, sessionID):37        print("onCreate:")38        self.session_id = sessionID39        target = sessionID.getTargetCompID().getString()40        sender = sessionID.getSenderCompID().getString()41        print(f"target: {target}")42        print(f"sender: {sender[:5]}...{sender[-5:]}")43        return44    def onLogon(self, sessionID):45        self.sessionID = sessionID46        print("onLogon:", f"Session ID: {self.sessionID}")47        security_list_msg = securityListRequest(sessionID)48        print("security list message:", messageToString(security_list_msg))49        message = marketDataRequest(sessionID)50        print("market data requests:", messageToString(message))51        quickfix.Session.sendToTarget(security_list_msg, sessionID)52        quickfix.Session.sendToTarget(message, sessionID)53        return54    def onLogout(self, sessionID):55        print("onLogout..")56        return57    def toAdmin(self, message, sessionID):58        print("toAdmin:", messageToString(message), '\n')59        return60    def toApp(self, message, sessionID):61        print("toApp:", messageToString(message), '\n')62        return63    def fromAdmin(self, message, sessionID):64        print("fromAdmin:", messageToString(message), '\n')65        return66    def fromApp(self, message, sessionID):67        msg = messageToString(message)68        msg_type = message.getHeader().getField(quickfix.MsgType())69        if msg_type.getString() == 'y':  # https://www.onixs.biz/fix-dictionary/4.4/msgtype_y_121.html70            if "coinbase~btc~usd" in msg.lower():71                print("fromApp: Requested security data available", '\n')72                return73        print("fromApp:", msg, '\n')74        return75    def keepAlive(self):76        while True:77            time.sleep(60)78if __name__ == "__main__":79    settings = quickfix.SessionSettings("CCFIX.ini")80    app = Application()81    storeFactory = quickfix.FileStoreFactory(settings)82    logFactory = quickfix.FileLogFactory(settings)83    initiator = quickfix.SocketInitiator(app,84                                         storeFactory,85                                         settings,86                                         logFactory)87    initiator.start()...fix_delimiter.py
Source:fix_delimiter.py  
...34from push_movies import get_dataset_files5from fetch_movie_json import write_to_json_file67def fix_delimiter(old: str, new: str):8    for movie_json in get_dataset_files():9        for key in movie_json:10            if type(movie_json[key]) == str and key in ('genre', 'director', 'writer', 'stars', 'languages'):11                movie_json[key] = movie_json[key].replace(old, new)12        print("Replaced " + movie_json['movieTitle'])13        write_to_json_file(movie_json['movieTitle'], movie_json)14    15def main():16    fix_delimiter(',', ';')1718if __name__ == '__main__':
...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
