def getIBTradeFiles(mode): """ [String] mode => [Iterable] IB trade files """ def tradeFile(file): """ [String] file => [Bool] does the file name contains 'trade' """ return 'trade_steven' in file.split('.') if mode == 'production': return filter( newerThanDB, filter(tradeFile, filter(csvFile, getFiles(getTradeFileDir())))) else: return filter(tradeFile, filter(csvFile, getFiles(getTradeFileDir())))
def mostRecentSqlBackup(): """ return: [String] the file name of the most recent database backup """ return sorted(filter(sqlBackup, getFiles(get_backup_directory())), key=dateFromFilename, reverse=True)[0]
def getHGNHTradeFiles(mode): """ [String] mode => [Iterable] HGNH trade files """ def tradeFile(file): """ [String] file => [Bool] does the file name contains 'trade' """ return file.lower().startswith('trade file') if mode == 'production': return filter( newerThanDB, filter(tradeFile, filter(excelFile, getFiles(getTradeFileDir())))) else: return filter(tradeFile, filter(excelFile, getFiles(getTradeFileDir())))
def _get_with_prefix(self): files=getFiles(self.path, isPath=True) if self.prefix: try: files = filter(lambda x:self.prefix in x , files) except: raise Exception("i don't know ") return files
def getInputFile(): logger.debug('getInputFile(): searching for files in {0}'.format( getInputDirectory())) files = list( filter(lambda f: f.startswith('TD') and f.endswith('.xlsx'), getFiles(getInputDirectory()))) if len(files) == 0: raise ValueError('No input file found') elif len(files) > 1: raise ValueError('Too many input files: {0}'.format(files)) else: return files[0]
def getNearestAccumulateFile(outputDir, portfolio, date): """ [String] outputDir, [String] portfolio, [String] date (yyyy-mm-dd) => [String] file Search for all files in the output dir, then: 1) Find all the accumulate trade files for the portfolio; 2) Find those whose date is less than the date; 3) Find the file with the latest date. 4) Return the file name with full path. """ logger.debug('getNearestAccumulateFile(): start') # [String] fn => [String] date (yyyy-mm-dd) getDateFromFilename = compose( lambda s: datetime.strftime(datetime.strptime(s, '%d%m%Y'), '%Y-%m-%d' ), lambda s: s.split('_')[-1].strip(), lambda fn: fn.split('.')[0]) isAccumulateTradeFile = lambda fn: \ fn.startswith('Equities_BOC_') if portfolio == '11500' \ else fn.startswith('Equities_') and not fn.startswith('Equities_BOC_') if portfolio == '11490' \ else fn.startswith('Equities_A-MC-P_') # 13006 fileOfLatestDate = lambda filesWithDate: max(filesWithDate, key=lambda t: t[0])[1] return compose( lambda fn: join(outputDir, fn), lambda fn: lognContinue( 'getNearestAccumulateFile(): {0}'.format(fn), fn), fileOfLatestDate, partial(filter, lambda t: t[0] < date), partial(map, lambda fn: (getDateFromFilename(fn), fn)), partial(filter, isAccumulateTradeFile), lambda outputDir, _: getFiles(outputDir))(outputDir, date)
def get_files(self): self.files = getFiles(self.path, isPath=True) pass
""" return join(get_backup_directory(), fn) if __name__ == '__main__': import logging.config, os logging.config.fileConfig('logging.config', disable_existing_loggers=False) logger.info('Start clearing') """ for fn in filter(olderThanNdays, map(toFullPath, getFiles(get_backup_directory()))): try: os.remove(fn) logger.debug('removed {0}'.format(fn)) except: logger.exception() """ for fn in map(toFullPath , filter(olderThanNdays , getFiles(get_backup_directory()))): try: print('deleting {0}'.format(fn)) os.remove(fn) except: logger.exception()
return True else: return False def toList(tradeTable): """ [Dict] tradeTable => [List] tuple (month, # trades, # trades portfolio) Where tradeTable is a dictonary like: '2018-01': (5588, 123) '2018-02': (6789, 456) """ return [(key, value[0], value[1]) for key, value in tradeTable.items()] if __name__ == '__main__': import logging.config logging.config.fileConfig('logging.config', disable_existing_loggers=False) """ To use the program, put Bloomberg XML trade files into a folder (specified in the config file), then run: $python trade.py """ writeCsv('result.csv' , sorted(toList(tradeTable(getFiles(getInputDirectory(), True) \ , '40006'))))