def test_custom_handler_class(self): doc = drill.parse(self.path, handler_class=CustomHandler) self.assertEqual(doc[0].__class__, CustomElement) for e in drill.iterparse(open(self.path, 'rb'), handler_class=CustomHandler): self.assertEqual(e.__class__, CustomElement) e.clear()
def fetch(self): r = requests.get(self.xml_url, timeout=5) doc = drill.parse(r.text) self.last_fetched = timezone.now() self.save(update_fields=('last_fetched', )) return self.weather.create(data=doc.json(), date_fetched=self.last_fetched)
def handle(self, *args, **options): t = int(time.time() * 1000) r = requests.get(SCORESTRIP_URL % t) ss = drill.parse(r.text) year = int(ss.gms['y']) week = int(ss.gms['w']) tz = pytz.timezone('US/Eastern') for g in ss.gms.children('g'): home = get_team(g['hnn'], g['h']) away = get_team(g['vnn'], g['v']) game_date = datetime.datetime.strptime(g['eid'][:8], '%Y%m%d') hour, minute = [int(p) for p in g['t'].split(':', 1)] # The feed does not specify AM/PM, so I just assume most games are PM. Not the case for some situations, # like the games in the UK (9:30 AM EST), but oh well. if hour < 12: hour += 12 game_date = game_date.replace(hour=hour, minute=minute) game, _created = Game.objects.get_or_create(eid=g['eid'], defaults={ 'home_team': home, 'away_team': away, 'year': year, 'week': week, 'game_date': timezone.make_aware(game_date, tz), }) game.home_score = int(g['hs']) game.away_score = int(g['vs']) game.quarter = g['q'] if game.quarter.startswith('F'): game.winner = home if game.home_score > game.away_score else away game.loser = home if game.home_score < game.away_score else away game.save() # Load point spreads root = drill.parse(LINE_URL) for event in root.find('events/event'): game_date = datetime.datetime.strptime(event.event_datetimeGMT.data, '%Y-%m-%d %H:%M') game_date = timezone.make_aware(game_date, pytz.utc) home = away = '' home_spread = away_spread = 0 for p in event.participants: if p.visiting_home_draw.data == 'Home': home = p.participant_name.data.split()[-1].lower() elif p.visiting_home_draw.data == 'Visiting': away = p.participant_name.data.split()[-1].lower() home_spread = decimal.Decimal(event.periods.period.spread.spread_home.data) away_spread = decimal.Decimal(event.periods.period.spread.spread_visiting.data) Game.objects.filter(home_team__name=home, away_team__name=away, game_date=game_date).update(home_spread=home_spread, away_spread=away_spread)
def load_map(filename, world=None): root = drill.parse(filename) if root.tagname.lower() != 'map': raise Exception('Expected "map" root element.') m = Map(**root.attrs) if world: m.load(root, world) return m
def test_json(self): doc = drill.parse(""" <root> <e1>hello</e1> <e2>world</e2> <e2 attr="ignore">this</e2> <e3> <e4> <e5>is a test</e5> </e4> </e3> </root> """) self.assertEqual(doc.json(), {'e1': 'hello', 'e2': ['world', 'this'], 'e3': {'e4': {'e5': 'is a test'}}})
def test_parse(self): # Parse out the drive on Windows, they don't play nice with file:// URLs. drive, path = os.path.splitdrive(self.path) # Load XML document from a URL. drill.parse('file://' + path.replace('\\', '/')) # Load an XML (unicode) string. drill.parse(u('<title>Él Libro</title>')) # Load XML document from a string (will be bytes on Python 3) with open(self.path, 'rb') as f: drill.parse(f.read())
def create_world(window, map_name): world = World(window) root = drill.parse(map_name) process_elements(world, root) return world
def load(self, map_name): root = drill.parse(map_name) process_elements(self, root)
def setUp(self): self.path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'catalog.xml') self.catalog = drill.parse(self.path)