def getTopGrowthIdListOfDayList(dayList): allList = [] day1 = dayList[0] day2 = dayList[-1] idList = getAllIdList() parser = BaseParser.BaseParser(dayList[0]) for id in idList: path = Tools.getPriceDirPath() + '/' + str(id) res = open(path, 'r').read() endPrice1 = parser.getEndPriceOfDay(res, day1) endPrice2 = parser.getEndPriceOfDay(res, day2) # print day1,day2 # print endPrice1,endPrice2 if 0 == endPrice1: rate = -1 else: rate = round((endPrice2 - endPrice1) / endPrice1, 5) allList.append((id, rate)) # 排序 allList = sorted(allList, key=lambda i: i[1]) l = len(allList) # print allList[0] # print allList[-1] #allList = allList[l-topNum:] allList = allList[traceDays:traceDays * 2] return allList
def test_make_string_as_dict(self): """testing of the correct transformation of string to dict""" actual_result = BaseParser( test_data.SUB_DF_OUTPUT).make_dict_from_string() self.assertIsNotNone(actual_result) self.assertIsInstance(actual_result, dict) self.assertDictEqual(actual_result, test_data.EXPECTED_BaseExecutor_OK)
def trace(id, day): parser = BaseParser.BaseParser(day) priceFile = Tools.getPriceDirPath() + '/' + str(id) res = open(priceFile, 'r').read() ret = {} ret['id'] = id ret['name'] = Tools.getNameById(id) ret['day'] = day ret['inPrice'] = parser.getEndPriceOfDay(res, day) # 买入价为当日收盘价 ret['islp'] = parser.getMinPriceOfDay(res, day) # 初始止损价为买入当日最低价 ret['risk'] = round(ret['islp'] - ret['inPrice'], 5) ret['riskRate'] = round(ret['risk'] / ret['inPrice'], 5) #if ret['riskRate'] < -0.03: #if ret['riskRate'] < -0.1: if ret['riskRate'] < -0.04: # 初始止损不超过n%(平均持股时间才4天) <----------------- return False dayList = parser.getNextTradingDayList(day, 30) inDayMaxPrice = parser.getMaxPriceOfDay(res, day) inDayMinPrice = parser.getMinPriceOfDay(res, day) holdDays = 0 inTracking = False #slp = round(ret['islp'],1)-0.1 # 去尾 slp = ret['islp'] for d in dayList: # 从第2天开始 holdDays += 1 endPrice = parser.getEndPriceOfDay(res, d) minPrice = parser.getMinPriceOfDay(res, d) if minPrice < slp: # 触发止损 要有大盘配合 ret['outPrice'] = slp break if not inTracking: # 处于启动期 if minPrice > inDayMaxPrice: # 进入跟踪止损的信号<----------------- #if minPrice > ret['inPrice']: #if minPrice > inDayMinPrice: inTracking = True #slp = round(minPrice,1)-0.1 # 上调止损价 #slp = ret['inPrice'] slp = minPrice else: # 处于跟踪期 dayList = parser.getPastTradingDayList( d, 1) # n日止损 <----------------- newIntervalMinPrice = getMinPriceOfDays(id, dayList) if newIntervalMinPrice > slp: #slp = round(newIntervalMinPrice,1)-0.1 slp = newIntervalMinPrice ret['holdDays'] = holdDays ret['profit'] = round(ret['outPrice'] - ret['inPrice'], 5) ret['profitRate'] = str( round(ret['profit'] / ret['inPrice'], 5) * 100.0) + '%' ret['growthRate'] = round(ret['profit'] / ret['inPrice'], 5) return ret
def getMinPriceOfDays(id, dayList): parser = BaseParser.BaseParser(dayList[-1]) priceFile = Tools.getPriceDirPath() + '/' + str(id) res = open(priceFile, 'r').read() minPrice = 999999 for d in dayList: price = parser.getMinPriceOfDay(res, d) if price < minPrice: minPrice = price return minPrice
def parse_recipe_new(url): raw_html = get_webpage(url) if raw_html: html = BeautifulSoup(raw_html, 'html.parser') name = BaseParser(HEADING_TAGS, get_name_segments(url)).parse(html) instructions = TitledListSectionParser( ['directions', 'instructions', 'steps']).parse(html) ingredients = TitledListSectionParser(['ingredients', 'materials']).parse(html) return { 'name': name, 'instructions': instructions, 'ingredients': ingredients, }
def getStrongRate(parseDay, indexRate, stockList, traceDays): totalNum = len(stockList) strongNum = 0 parser = BaseParser.BaseParser(parseDay) for item in stockList: id = item[0] growthRate = getGrowthRate(parseDay, traceDays, id) if growthRate == -99999: totalNum -= 1 if growthRate > indexRate: strongNum += 1 # print 'totalNum:'+str(totalNum) # print 'strongNum:'+str(strongNum) rate = round(strongNum * 1.0 / totalNum, 5) * 100.0 return rate
def compare(compareTo, compareList): ret = {} parseDay = time.strftime('%Y-%m-%d', time.localtime(time.time())) parser = BaseParser.BaseParser(parseDay) dayList = parser.getPastTradingDayList(parseDay, compareDays) for code in compareList: for day in dayList: res1 = getRes(compareTo, parseDay) endPrice1 = parser.getEndPriceOfDay(res1, day) res2 = getRes(code, parseDay) endPrice2 = parser.getEndPriceOfDay(res2, day) rate = round(endPrice2 / endPrice1, 5) * 100.0 if not ret.has_key(code): ret[code] = [(day, rate)] else: ret[code].append((day, rate)) return ret
def compute(code,parseDay): res = getRes(code,parseDay) parser = BaseParser.BaseParser(parseDay) dayList = parser.getPastTradingDayList(parseDay,4) print dayList e1 = parser.getEndPriceOfDay(res,dayList[0]) e2 = parser.getEndPriceOfDay(res,dayList[1]) e3 = parser.getEndPriceOfDay(res,dayList[2]) e4 = parser.getEndPriceOfDay(res,dayList[3]) e5 = e4 * 1.1 print e1,e2,e3,e4,e5 if 0 == e1*e2*e3*e4: print 'End Price Error !' else: ma3 = (e3+e4+e5)/3.0 ma5 = (e1+e2+e3+e4+e5)/5.0 print 'MA3 = ' + str(ma3)+',MA5 = ' + str(ma5)
def getAllRightIdList(dayList): allList = [] day1 = dayList[0] day2 = dayList[-1] idList = getAllIdList() for id in idList: path = Tools.getPriceDirPath() + '/' + str(id) res = open(path, 'r').read() parser = BaseParser.BaseParser(dayList[0]) endPrice1 = parser.getEndPriceOfDay(res, day1) endPrice2 = parser.getEndPriceOfDay(res, day2) if 0 == endPrice1 or 0 == endPrice2: rate = -1 continue else: rate = round((endPrice2 - endPrice1) / endPrice1, 5) allList.append((id, rate)) return allList
def computeMaxMa(code, parseDay): res = getRes(code, parseDay) parser = BaseParser.BaseParser(parseDay) dayList = parser.getPastTradingDayList(parseDay, 4) e1 = parser.getEndPriceOfDay(res, dayList[0]) e2 = parser.getEndPriceOfDay(res, dayList[1]) e3 = parser.getEndPriceOfDay(res, dayList[2]) e4 = parser.getEndPriceOfDay(res, dayList[3]) e5 = e4 * 1.1 if 0 == e1 * e2 * e3 * e4: print 'End Price Error !' else: ma5 = round((e1 + e2 + e3 + e4 + e5) / 5.0, 4) ma3 = round((e3 + e4 + e5) / 3.0, 4) gr5 = round((ma5 - e4) / e4, 5) gr3 = round((ma3 - e4) / e4, 5) print '涨停5日线:MA5 = ' + str(ma5) + ',GR5 = ' + str(gr5 * 100.0) + '%' print '涨停3日线:MA3 = ' + str(ma3) + ',GR5 = ' + str(gr3 * 100.0) + '%'
def getGrowthRate(parseDay, days, id): dayList = BaseParser.BaseParser.getPastTradingDayList(parseDay, 2) dayList = BaseParser.BaseParser.getNextTradingDayList( dayList[0], traceDays + 1) path = Tools.getPriceDirPath() + '/' + str(id) res = open(path, 'r').read() day1 = dayList[0] day2 = dayList[-1] parser = BaseParser.BaseParser(parseDay) endPrice1 = parser.getEndPriceOfDay(res, day1) endPrice2 = parser.getEndPriceOfDay(res, day2) # print day1,day2 # print endPrice1,endPrice2 if 0 == endPrice1: rate = -99999 else: rate = round((endPrice2 - endPrice1) / endPrice1, 5) * 100.0 # print rate return rate
def computeReboundMa(code, parseDay): res = getRes(code, parseDay) parser = BaseParser.BaseParser(parseDay) dayList = parser.getPastTradingDayList(parseDay, 4) e1 = parser.getEndPriceOfDay(res, dayList[0]) e2 = parser.getEndPriceOfDay(res, dayList[1]) e3 = parser.getEndPriceOfDay(res, dayList[2]) e4 = parser.getEndPriceOfDay(res, dayList[3]) s4 = e1 + e2 + e3 + e4 if 0 == e1 * e2 * e3 * e4: print 'End Price Error !' else: gr5 = s4 / (4 * e4) - 1 e5 = (1 + gr5) * e4 # b = (1 + gr5 + 0.000) * e4 gr5 = round(gr5, 4) print '回踩5日线:MA5 = ' + str(e5) + ',GR5 = ' + str(gr5 * 100.0) + '%' gr3 = (e3 + e4) / (2 * e4) - 1 e = (1 + gr3) * e4 # b = (1 + gr3 + 0.000) * e4 gr3 = round(gr3, 4) print '回踩3日线:MA3 = ' + str(e) + ',GR3 = ' + str(gr3 * 100.0) + '%'
def getRes(code, parseDay): parser = BaseParser.BaseParser(parseDay) priceFile = Tools.getPriceDirPath() + '/' + str(code) res = open(priceFile, 'r').read() return res
def setUp(self): self.values = optparse.Values() self.values.ensure_value("regex", None) self.values.ensure_value("role", True) self.parser = BaseParser(self.values)
class BaseParserTest(unittest.TestCase): def setUp(self): self.values = optparse.Values() self.values.ensure_value("regex", None) self.values.ensure_value("role", True) self.parser = BaseParser(self.values) def testParseVersionNumberNoEpoch(self): ver = self.parser.parseVersionNumber("1.0-1") self.assertEqual(None, ver.epoch) self.assertEqual("1.0", ver.upstream_version) self.assertEqual("1", ver.debian_version) def testParseVersionNumberEpoch(self): ver = self.parser.parseVersionNumber("1:1.0+svn20080909-1.1") self.assertEqual("1", ver.epoch) self.assertEqual("1.0+svn20080909", ver.upstream_version) self.assertEqual("1.1", ver.debian_version) def testParseVersionNumberWrongVersionNumber(self): self.assertRaises(ParserError, self.parser.parseVersionNumber, "_") def testParseConstraints(self): input = "exim4 (>> 0.5.4-5) | mail-transport-agent, mutt" ors = self.parser.parseConstraints(input) self.assertEqual(2, len(ors.orconstraints)) self.assertEqual(2, len(ors.get(0).constraints)) self.assertEqual(UnversionedBinaryPackage("exim4"), ors.get(0).get(0).package) self.assertEqual(">>", ors.get(0).get(0).operator) self.assertEqual(UnversionedBinaryPackage("mail-transport-agent"), ors.get(0).get(1).package) self.assertEqual(UnversionedBinaryPackage("mutt"), ors.get(1).get(0).package) input = "c1a | c1b, c2,c3, c4, c5" ors = self.parser.parseConstraints(input) self.assertEqual(5, len(ors.orconstraints)) self.assertEqual(2, len(ors.get(0).constraints)) def testParseOrConstraint(self): input = "exim4+r (>> 0.5.4-5) | mail-transport-agent | swaml.4" ord = self.parser.parseOrConstraint(input) self.assertEqual(3, len(ord.constraints)) self.assertEqual(UnversionedBinaryPackage("exim4+r"), ord.get(0).package) self.assertEqual(">>", ord.get(0).operator) self.assertEqual(UnversionedBinaryPackage("mail-transport-agent"), ord.get(1).package) self.assertEqual(UnversionedBinaryPackage("swaml.4"), ord.get(2).package) input = "c1| c2|c3 | c4" ord = self.parser.parseOrConstraint(input) self.assertEqual(4, len(ord.constraints)) def testParseConstraintSimple(self): input = "libgnutls13" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage(input), d.package) self.assertEqual(None, d.version) def testParseConstraintWithVersionAndOperator(self): input = "libidn11 (>= 0.5.18)" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage("libidn11"), d.package) self.assertEqual(">=", d.operator) self.assertEqual(None, d.version.epoch) self.assertEqual("0.5.18", d.version.upstream_version) self.assertEqual(None, d.version.debian_version) def testParseConstraintAllOperators(self): input = "pkg1 (<< 1)" d = self.parser.parseConstraint(input) self.assertEqual("<<", d.operator) input = "pkg1 (<= 1)" d = self.parser.parseConstraint(input) self.assertEqual("<=", d.operator) input = "pkg1 (= 1)" d = self.parser.parseConstraint(input) self.assertEqual("=", d.operator) input = "pkg1 (>= 1)" d = self.parser.parseConstraint(input) self.assertEqual(">=", d.operator) input = "pkg1 (>> 1)" d = self.parser.parseConstraint(input) self.assertEqual(">>", d.operator) def testParseConstraintGuardExcept(self): input = "libasound2-dev [!kfreebsd-i386 !kfreebsd-amd64]" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage("libasound2-dev"), d.package) self.assertEqual(None, d.operator) self.assertEqual(2, len(d.exceptin)) self.assertEqual(0, len(d.onlyin)) expectedlist = [ Architecture("kfreebsd-i386"), Architecture("kfreebsd-amd64") ] self.assertEqual(expectedlist, d.exceptin) def testParseConstraintGuardOnly(self): input = "libasound2-dev [kfreebsd-i386]" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage("libasound2-dev"), d.package) self.assertEqual(None, d.operator) self.assertEqual(0, len(d.exceptin)) self.assertEqual(1, len(d.onlyin)) expectedlist = [Architecture("kfreebsd-i386")] self.assertEqual(expectedlist, d.onlyin) def testParseFullConstraint(self): input = "libidn11 (>= 0.5.18) [!kfreebsd-i386 !kfreebsd-amd64]" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage("libidn11"), d.package) self.assertEqual(">=", d.operator) self.assertEqual(None, d.version.epoch) self.assertEqual("0.5.18", d.version.upstream_version) self.assertEqual(None, d.version.debian_version) self.assertEqual(2, len(d.exceptin)) self.assertEqual(0, len(d.onlyin)) expectedlist = [ Architecture("kfreebsd-i386"), Architecture("kfreebsd-amd64") ] self.assertEqual(expectedlist, d.exceptin) def testParseMalformedConstraint(self): input = "()" self.assertRaises(ParserError, self.parser.parseConstraint, input) input = "(>= 6.8)" self.assertRaises(ParserError, self.parser.parseConstraint, input) input = "[]" self.assertRaises(ParserError, self.parser.parseConstraint, input) input = "[powerpc]" self.assertRaises(ParserError, self.parser.parseConstraint, input) input = "(>= 5.6) [powerpc]" self.assertRaises(ParserError, self.parser.parseConstraint, input) def testParseTags(self): input = "implemented-in::lisp" tags = self.parser.parseTags(input) expected = [Tag("implemented-in", "lisp")] self.assertEqual(1, len(tags)) self.assertEqual(expected, tags) input = "implemented-in::lisp, hardware::storage" tags = self.parser.parseTags(input) expected = [Tag("implemented-in", "lisp"), Tag("hardware", "storage")] self.assertEqual(2, len(tags)) self.assertEqual(expected, tags) input = "hardware::storage:cd" tags = self.parser.parseTags(input) expected = [Tag("hardware", "storage:cd")] self.assertEqual(1, len(tags)) self.assertEqual(expected, tags) input = "hardware::{laptop}" tags = self.parser.parseTags(input) expected = [Tag("hardware", "laptop")] self.assertEqual(1, len(tags)) self.assertEqual(expected, tags) input = "hardware::{laptop,power:apm}, implemented-in::lisp" expected = [Tag("hardware", "laptop"),\ Tag("hardware", "power:apm"), Tag("implemented-in", "lisp")] tags = self.parser.parseTags(input) self.assertEqual(3, len(tags)) self.assertEqual(expected, tags) #input = "facet::t1, facet::t2,facet::t3" #tags = self.parser.parseTags(input) #self.assertEqual(3,len(tags)) input = "::" self.assertRaises(ParserError, self.parser.parseTags, input) input = "hardware::" self.assertRaises(ParserError, self.parser.parseTags, input) input = "::lang:c" self.assertRaises(ParserError, self.parser.parseTags, input) input = "hardware::, ::lang:c" self.assertRaises(ParserError, self.parser.parseTags, input) def testParseSection(self): input = {'Section': "utils"} self.assertEqual(Section("utils"), self.parser.parseSection(input)) def testParseSectionMissingField(self): input = {} self.assertEqual(None, self.parser.parseSection(input)) def testParsePriority(self): input = {'Priority': "optional"} self.assertEqual(PriorityBox.get("optional"),\ self.parser.parsePriority(input)) def testParsePriorityMissingField(self): input = {} self.assertEqual(None, self.parser.parsePriority(input)) def testParseContributorGuessRoleOn(self): input = "Name Surname <*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual("Name Surname", contributor.name) self.assertEqual("*****@*****.**", contributor.email) self.assertEqual(Human, contributor.__class__) input = "Name Surname (Some notes) <*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual("Name Surname (Some notes)", contributor.name) self.assertEqual("*****@*****.**", contributor.email) self.assertEqual(Human, contributor.__class__) input = "<*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual(None, contributor.name) self.assertEqual("*****@*****.**", contributor.email) self.assertEqual(Human, contributor.__class__) input = "Name Surname" self.assertRaises(ParserError, self.parser.parseContributor, input) def testParseContributorGuessRoleOff(self): values = optparse.Values() # Unable to reset a boolean value! values.ensure_value('role', False) self.parser.opts = values input = "Name Surname <*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual(Contributor, contributor.__class__) input = "<*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual(None, contributor.name) self.assertEqual("*****@*****.**", contributor.email) self.assertEqual(Contributor, contributor.__class__) def testParseContributors(self): input = "Name Surname <*****@*****.**>" contributors = self.parser.parseContributors(input) self.assertEqual(1, len(contributors)) input = "S P <*****@*****.**>, T <*****@*****.**>,K R R <*****@*****.**>" contributors = self.parser.parseContributors(input) self.assertEqual(3, len(contributors)) def testParseHomepage(self): input = {'Homepage': "http://example.org"} self.assertEquals("http://example.org", self.parser.parseHomepage(input)) def testParseArea(self): input = 'pool/main/f/foo' self.assertEquals(AreaBox.get("main"), self.parser.parseArea(input)) input = 'pool/non-free/f/foo' self.assertEquals(AreaBox.get("non-free"), self.parser.parseArea(input)) input = 'pool/contrib/f/foo' self.assertEquals(AreaBox.get("contrib"), self.parser.parseArea(input)) input = 'pool/failarea/f/foo' self.assertRaises(ParserError, self.parser.parseArea, input) input = 'dists/slink/main/source/games' self.assertEquals(AreaBox.get("main"), self.parser.parseArea(input)) input = 'dists/bo/non-free/source/x11' self.assertEquals(AreaBox.get("non-free"), self.parser.parseArea(input)) input = 'dists/potato/contrib/source/games' self.assertEquals(AreaBox.get("contrib"), self.parser.parseArea(input)) input = 'dists/potato/failarea/source/games' self.assertRaises(ParserError, self.parser.parseArea, input) def testParseVcs(self): input = {'Vcs-Browser':'http://example.com', \ 'Vcs-Git':'git://git.example.com'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual("http://example.com", repo.browser) self.assertEqual("git://git.example.com", repo.uri) input = {'X-Vcs-Browser':'http://example.com', \ 'X-Vcs-Git':'git://git.example.com'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual("http://example.com", repo.browser) self.assertEqual("git://git.example.com", repo.uri) input = {'Vcs-Browser':'http://example.com', \ 'Vcs-Svn':'svn://svn.example.com/repos/test'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual("http://example.com", repo.browser) self.assertEqual("svn://svn.example.com/repos/test", repo.uri) input = {'Vcs-Git': 'git://git.example.com'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual(None, repo.browser) self.assertEqual("git://git.example.com", repo.uri) input = {'Vcs-Browser': 'http://example.com'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual("http://example.com", repo.browser) self.assertEqual(None, repo.uri) input = {} repo = self.parser.parseVcs(input) self.assertEqual(None, repo) def testParseURI(self): uri1 = "http://example.com" self.assertEqual(uri1, self.parser.parseURI(uri1)) uri2 = "ftp://example.com" self.assertEqual(uri2, self.parser.parseURI(uri2)) uri3 = "example.com" self.assertEqual(uri1, self.parser.parseURI(uri3))
try: res = getRes(id) if not isUpwardLimit(res, parseDay): continue upwardLimitNum = getUpwardLimitNum(res, parseDay) ret.append((id, upwardLimitNum)) except Exception, e: pass # print repr(e) return ret parser = None if __name__ == '__main__': data = {} print 'Dashboard' (parseDay) = getParams() print 'parseDay:' + parseDay parser = BaseParser.BaseParser(parseDay) idList = BaseSpider.BaseSpider.getIdList() ret = getContinuousUpwardLimitData(idList) data['upwardLimitNUm'] = ret print data
def get_all_links(content, page): parser = BaseParser() url_set = parser.parse_url(content, page) links = [u for u in url_set] # this is stupid return links
# trace =========================================================================== selectedNum = 0 riseNum = 0 declineNum = 0 drawNum = 0 totalRise = 0.0 avgRise = 0.0 for parseDay, idList in selectedList.items(): try: dayList = BaseParser.BaseParser.getNextTradingDayList( parseDay, traceNum) traceDay = dayList[-1] for id in idList: selectedNum += 1 growthRate = BaseParser.BaseParser(parseDay).getGrowthRate( id, parseDay, traceDay) totalRise += growthRate if growthRate > 0: riseNum += 1 elif growthRate == 0: drawNum += 1 elif growthRate < 0: declineNum += 1 except Exception, e: pass #print repr(e) avgRise = round(totalRise / selectedNum, 3) print 'selectedNum: ' + str(selectedNum) print 'riseNum: ' + str(riseNum) print 'declineNum: ' + str(declineNum)
def setUp(self): self.values = optparse.Values() self.values.ensure_value("regex", None) self.values.ensure_value("role", True) self.parser = BaseParser(self.values)
class BaseParserTest(unittest.TestCase): def setUp(self): self.values = optparse.Values() self.values.ensure_value("regex", None) self.values.ensure_value("role", True) self.parser = BaseParser(self.values) def testParseVersionNumberNoEpoch(self): ver = self.parser.parseVersionNumber("1.0-1") self.assertEqual(None, ver.epoch) self.assertEqual("1.0", ver.upstream_version) self.assertEqual("1", ver.debian_version) def testParseVersionNumberEpoch(self): ver = self.parser.parseVersionNumber("1:1.0+svn20080909-1.1") self.assertEqual("1", ver.epoch) self.assertEqual("1.0+svn20080909", ver.upstream_version) self.assertEqual("1.1", ver.debian_version) def testParseVersionNumberWrongVersionNumber(self): self.assertRaises(ParserError, self.parser.parseVersionNumber, "_") def testParseConstraints(self): input = "exim4 (>> 0.5.4-5) | mail-transport-agent, mutt" ors = self.parser.parseConstraints(input) self.assertEqual(2, len(ors.orconstraints)) self.assertEqual(2, len(ors.get(0).constraints)) self.assertEqual(UnversionedBinaryPackage("exim4"), ors.get(0).get(0).package) self.assertEqual(">>", ors.get(0).get(0).operator) self.assertEqual(UnversionedBinaryPackage("mail-transport-agent"), ors.get(0).get(1).package) self.assertEqual(UnversionedBinaryPackage("mutt"), ors.get(1).get(0).package) input = "c1a | c1b, c2,c3, c4, c5" ors = self.parser.parseConstraints(input) self.assertEqual(5, len(ors.orconstraints)) self.assertEqual(2, len(ors.get(0).constraints)) def testParseOrConstraint(self): input = "exim4+r (>> 0.5.4-5) | mail-transport-agent | swaml.4" ord = self.parser.parseOrConstraint(input) self.assertEqual(3, len(ord.constraints)) self.assertEqual(UnversionedBinaryPackage("exim4+r"), ord.get(0).package) self.assertEqual(">>", ord.get(0).operator) self.assertEqual(UnversionedBinaryPackage("mail-transport-agent"), ord.get(1).package) self.assertEqual(UnversionedBinaryPackage("swaml.4"), ord.get(2).package) input = "c1| c2|c3 | c4" ord = self.parser.parseOrConstraint(input) self.assertEqual(4, len(ord.constraints)) def testParseConstraintSimple(self): input = "libgnutls13" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage(input), d.package) self.assertEqual(None, d.version) def testParseConstraintWithVersionAndOperator(self): input = "libidn11 (>= 0.5.18)" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage("libidn11"), d.package) self.assertEqual(">=", d.operator) self.assertEqual(None, d.version.epoch) self.assertEqual("0.5.18", d.version.upstream_version) self.assertEqual(None, d.version.debian_version) def testParseConstraintAllOperators(self): input = "pkg1 (<< 1)" d = self.parser.parseConstraint(input) self.assertEqual("<<", d.operator) input = "pkg1 (<= 1)" d = self.parser.parseConstraint(input) self.assertEqual("<=", d.operator) input = "pkg1 (= 1)" d = self.parser.parseConstraint(input) self.assertEqual("=", d.operator) input = "pkg1 (>= 1)" d = self.parser.parseConstraint(input) self.assertEqual(">=", d.operator) input = "pkg1 (>> 1)" d = self.parser.parseConstraint(input) self.assertEqual(">>", d.operator) def testParseConstraintGuardExcept(self): input = "libasound2-dev [!kfreebsd-i386 !kfreebsd-amd64]" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage("libasound2-dev"), d.package) self.assertEqual(None, d.operator) self.assertEqual(2, len(d.exceptin)) self.assertEqual(0, len(d.onlyin)) expectedlist = [Architecture("kfreebsd-i386"), Architecture("kfreebsd-amd64")] self.assertEqual(expectedlist, d.exceptin) def testParseConstraintGuardOnly(self): input = "libasound2-dev [kfreebsd-i386]" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage("libasound2-dev"), d.package) self.assertEqual(None, d.operator) self.assertEqual(0, len(d.exceptin)) self.assertEqual(1, len(d.onlyin)) expectedlist = [Architecture("kfreebsd-i386")] self.assertEqual(expectedlist, d.onlyin) def testParseFullConstraint(self): input = "libidn11 (>= 0.5.18) [!kfreebsd-i386 !kfreebsd-amd64]" d = self.parser.parseConstraint(input) self.assertEqual(UnversionedBinaryPackage("libidn11"), d.package) self.assertEqual(">=", d.operator) self.assertEqual(None, d.version.epoch) self.assertEqual("0.5.18", d.version.upstream_version) self.assertEqual(None, d.version.debian_version) self.assertEqual(2, len(d.exceptin)) self.assertEqual(0, len(d.onlyin)) expectedlist = [Architecture("kfreebsd-i386"), Architecture("kfreebsd-amd64")] self.assertEqual(expectedlist, d.exceptin) def testParseMalformedConstraint(self): input = "()" self.assertRaises(ParserError, self.parser.parseConstraint, input) input = "(>= 6.8)" self.assertRaises(ParserError, self.parser.parseConstraint, input) input = "[]" self.assertRaises(ParserError, self.parser.parseConstraint, input) input = "[powerpc]" self.assertRaises(ParserError, self.parser.parseConstraint, input) input = "(>= 5.6) [powerpc]" self.assertRaises(ParserError, self.parser.parseConstraint, input) def testParseTags(self): input = "implemented-in::lisp" tags = self.parser.parseTags(input) expected = [Tag("implemented-in", "lisp")] self.assertEqual(1,len(tags)) self.assertEqual(expected, tags) input = "implemented-in::lisp, hardware::storage" tags = self.parser.parseTags(input) expected = [Tag("implemented-in", "lisp"), Tag("hardware", "storage")] self.assertEqual(2,len(tags)) self.assertEqual(expected, tags) input = "hardware::storage:cd" tags = self.parser.parseTags(input) expected = [Tag("hardware", "storage:cd")] self.assertEqual(1,len(tags)) self.assertEqual(expected, tags) input = "hardware::{laptop}" tags = self.parser.parseTags(input) expected = [Tag("hardware", "laptop")] self.assertEqual(1,len(tags)) self.assertEqual(expected, tags) input = "hardware::{laptop,power:apm}, implemented-in::lisp" expected = [Tag("hardware", "laptop"),\ Tag("hardware", "power:apm"), Tag("implemented-in", "lisp")] tags = self.parser.parseTags(input) self.assertEqual(3,len(tags)) self.assertEqual(expected, tags) #input = "facet::t1, facet::t2,facet::t3" #tags = self.parser.parseTags(input) #self.assertEqual(3,len(tags)) input = "::" self.assertRaises(ParserError, self.parser.parseTags, input) input = "hardware::" self.assertRaises(ParserError, self.parser.parseTags, input) input = "::lang:c" self.assertRaises(ParserError, self.parser.parseTags, input) input = "hardware::, ::lang:c" self.assertRaises(ParserError, self.parser.parseTags, input) def testParseSection(self): input = {'Section': "utils"} self.assertEqual(Section("utils"), self.parser.parseSection(input)) def testParseSectionMissingField(self): input = {} self.assertEqual(None, self.parser.parseSection(input)) def testParsePriority(self): input = {'Priority': "optional"} self.assertEqual(PriorityBox.get("optional"),\ self.parser.parsePriority(input)) def testParsePriorityMissingField(self): input = {} self.assertEqual(None, self.parser.parsePriority(input)) def testParseContributorGuessRoleOn(self): input = "Name Surname <*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual("Name Surname", contributor.name) self.assertEqual("*****@*****.**", contributor.email) self.assertEqual(Human, contributor.__class__) input = "Name Surname (Some notes) <*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual("Name Surname (Some notes)", contributor.name) self.assertEqual("*****@*****.**", contributor.email) self.assertEqual(Human, contributor.__class__) input = "<*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual(None, contributor.name) self.assertEqual("*****@*****.**", contributor.email) self.assertEqual(Human, contributor.__class__) input = "Name Surname" self.assertRaises(ParserError, self.parser.parseContributor, input) def testParseContributorGuessRoleOff(self): values = optparse.Values() # Unable to reset a boolean value! values.ensure_value('role', False) self.parser.opts = values input = "Name Surname <*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual(Contributor, contributor.__class__) input = "<*****@*****.**>" contributor = self.parser.parseContributor(input) self.assertEqual(None, contributor.name) self.assertEqual("*****@*****.**", contributor.email) self.assertEqual(Contributor, contributor.__class__) def testParseContributors(self): input = "Name Surname <*****@*****.**>" contributors = self.parser.parseContributors(input) self.assertEqual(1, len(contributors)) input = "S P <*****@*****.**>, T <*****@*****.**>,K R R <*****@*****.**>" contributors = self.parser.parseContributors(input) self.assertEqual(3, len(contributors)) def testParseHomepage(self): input = {'Homepage': "http://example.org"} self.assertEquals("http://example.org", self.parser.parseHomepage(input)) def testParseArea(self): input = 'pool/main/f/foo' self.assertEquals(AreaBox.get("main"), self.parser.parseArea(input)) input = 'pool/non-free/f/foo' self.assertEquals(AreaBox.get("non-free"), self.parser.parseArea(input)) input = 'pool/contrib/f/foo' self.assertEquals(AreaBox.get("contrib"), self.parser.parseArea(input)) input = 'pool/failarea/f/foo' self.assertRaises(ParserError, self.parser.parseArea, input) input = 'dists/slink/main/source/games' self.assertEquals(AreaBox.get("main"), self.parser.parseArea(input)) input = 'dists/bo/non-free/source/x11' self.assertEquals(AreaBox.get("non-free"), self.parser.parseArea(input)) input = 'dists/potato/contrib/source/games' self.assertEquals(AreaBox.get("contrib"), self.parser.parseArea(input)) input = 'dists/potato/failarea/source/games' self.assertRaises(ParserError, self.parser.parseArea, input) def testParseVcs(self): input = {'Vcs-Browser':'http://example.com', \ 'Vcs-Git':'git://git.example.com'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual("http://example.com", repo.browser) self.assertEqual("git://git.example.com", repo.uri) input = {'X-Vcs-Browser':'http://example.com', \ 'X-Vcs-Git':'git://git.example.com'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual("http://example.com", repo.browser) self.assertEqual("git://git.example.com", repo.uri) input = {'Vcs-Browser':'http://example.com', \ 'Vcs-Svn':'svn://svn.example.com/repos/test'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual("http://example.com", repo.browser) self.assertEqual("svn://svn.example.com/repos/test", repo.uri) input = {'Vcs-Git':'git://git.example.com'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual(None, repo.browser) self.assertEqual("git://git.example.com", repo.uri) input = {'Vcs-Browser':'http://example.com'} repo = self.parser.parseVcs(input) self.assertNotEqual(None, repo) self.assertEqual("http://example.com", repo.browser) self.assertEqual(None, repo.uri) input = {} repo = self.parser.parseVcs(input) self.assertEqual(None, repo) def testParseURI(self): uri1 = "http://example.com" self.assertEqual(uri1, self.parser.parseURI(uri1)) uri2 = "ftp://example.com" self.assertEqual(uri2, self.parser.parseURI(uri2)) uri3 = "example.com" self.assertEqual(uri1, self.parser.parseURI(uri3))
def report(parseDay, parsers, isOpen=False, traceDay=''): sel = '' s = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />' s += ''' <style> a:link { color:blue; } a:visited { color:blue; } a:hover { color:red; } a:active { color:yellow; } font-size:0.8em; td{ font-size:0.8em; } </style> ''' s += '</head><body>' focusTotal = 0 focusRise = 0 nums = {} riseNums = {} count = {} # for font size tb = '<tr valign="top" align="center">' dirEnterList = Tools.getEnterListDirPath() for parser, tag in parsers.items(): riseNums[parser] = 0 nums[parser] = 0 try: f = dirEnterList + '/' + parseDay + '-' + parser + '.sel' l = open(f, 'r').read().split(',') nums[parser] = len(l) if len(l[0]) < 1: # 可能解析出来是空字符 nums[parser] = 0 tb += '<td style="border:1px solid #eee;"><br/>' if len(l) > 100: # 太多,无意义 tb += '失真(选股过多)' tb += '<br/><br/>' continue for id in l: try: if count.has_key(id): count[id] += 1 else: count[id] = 1 name = Tools.getNameById(id) link = getLinkById(id) div = '<div $_style_$>' # trace color = 'black' traceDayGrowthRate = 0 if len(traceDay) == 10: traceDayGrowthRate = BaseParser.BaseParser( parseDay).getGrowthRate(id, parseDay, traceDay) if not riseNums.has_key(parser): riseNums[parser] = 0 if traceDayGrowthRate > 0: color = 'red' riseNums[parser] += 1 elif traceDayGrowthRate < 0: color = 'green' div += '<font size=1 color="' + color + '">' + str( round(traceDayGrowthRate, 3)) + ' %</font></br>' countNum = '' if count[id] > 1: countNum = '<font size=1>' + NUM_ICON[ count[id]] + '</font>' # id div += '<a href="' + link + '" target="_blank">' div += '<font size=' + str( count[id]) + '>' + str(id) + '</font>' div += '</a></br>' # name div += '<font size=' + str( count[id] ) + ' color="' + color + '"><b>' + name + ' ' + countNum + '</b></font>' isFocus = True # RGB isRgb = BaseParser.BaseParser(parseDay).isRgb(id, parseDay) if isRgb: div += '<br/>' div += '<font size=2 color="red"><b>↗</b></font>' div += '<font size=2 color="green"><b>↗</b></font>' div += '<font size=2 color="blue"><b>↗</b></font>' else: isFocus = False # CR basicInfo = BaseParser.BaseParser.getBasicInfoById(id) changeRate = float(basicInfo[37]) if changeRate >= 3.0: div += '<br/><font size=1 color="orange">换手率: ' + str( changeRate) + '% </font>' else: isFocus = False # Focus if isFocus: div = div.replace( '$_style_$', 'style="border:1px dashed #F00;border-radius:10px;width:80%;";' ) sel += str(id) + ',' focusTotal += 1 if len(traceDay) == 10: if traceDayGrowthRate > 0: focusRise += 1 div += '</div><br/><br/>' tb += div except Exception, e: pass #print repr(e) tb += '</td>' except Exception, e: pass