Changeset - 6127b170f21c
[Not reviewed]
default
0 2 0
Laman - 7 years ago 2018-03-27 13:20:43

populationStrength, indexy na tabulkách
2 files changed with 24 insertions and 3 deletions:
0 comments (0 inline, 0 general)
egd.py
Show inline comments
 
@@ -46,47 +46,50 @@ def loadPlayers(filename,enc="iso8859_2"
 
		if match is None:
 
			print("ignored:",line,file=sys.stderr)
 
			continue
 
		(pin,name,cc,cl,rank,tcode,wc,gc,r1,r2)=match.groups()
 
		record=dict(zip(
 
			["pin","name","countryCode","club","rank","tournamentCode","winCount","gameCount","rating1","rating2"],
 
			(int(pin),name,cc,cl,rank,tcode,int(wc),int(gc),int(r1),int(r2))
 
		))
 
		players.append(record)
 
	return players
 

	
 

	
 
def setupDB():
 
	cursor=connection.cursor()
 

	
 
	cursor.execute("""create table tournaments (code text primary key, datum text, class text, round_count integer, player_count integer, country_code text, description text)""")
 
	cursor.execute("""create table players (pin integer primary key, name text)""")
 
	cursor.execute("""create table players_performance (pin integer references players(pin), tournament_code text references tournaments(code), country_code text, club text, win_count integer, game_count integer, rating_before real, rating_after real, primary key (pin,tournament_code))""")
 
	cursor.execute("""
 
		create view players_performance_tournaments (pin,p_country_code,rating_before,rating_after,t_country_code,datum) as
 
			select pin,pp.country_code,rating_before,rating_after,ts.country_code,datum
 
				from players_performance as pp join tournaments as ts on pp.tournament_code=ts.code
 
	""")
 

	
 
	cursor.execute("""create index pp_country_ind on players_performance (country_code)""")
 
	cursor.execute("""create index t_datum_ind on tournaments (datum)""")
 

	
 
	connection.commit()
 

	
 

	
 
def fillDB(tournaments,players):
 
	cursor=connection.cursor()
 

	
 
	cursor.execute("""delete from players_performance""")
 
	cursor.execute("""delete from players""")
 
	cursor.execute("""delete from tournaments""")
 

	
 
	cursor.executemany(
 
		"""insert into tournaments (code,datum,class,round_count,player_count,country_code,description) values (:code,:date,:class,:roundCount,:playerCount,:countryCode,:description)""",
 
		tournaments.values()
 
	)
 

	
 
	cursor.executemany("""insert or ignore into players (pin,name) values (:pin,:name)""", players)
 

	
 
	cursor.executemany(
 
		"""insert into players_performance (pin,tournament_code,country_code,club,win_count,game_count,rating_before,rating_after) values (:pin,:tournamentCode,:countryCode,:club,:winCount,:gameCount,:rating1,:rating2)""",
 
		players
 
	)
 

	
 
	connection.commit()
queries.py
Show inline comments
 
@@ -38,35 +38,53 @@ def yearByCountry(year):
 
			order by country_code asc;
 
		"""
 
	res=cursor.execute(query,(date1,date2))
 
	print("kód země\tpočet aktivních hráčů")
 
	for rec in res:
 
		print("\t".join([str(x) for x in rec]))
 

	
 

	
 
def timeRank(year,country):
 
	date1=yearStart(year)
 
	date2=yearStart(year+1)
 
	query="""
 
		select ppt.pin,date(?)-min_datum-1,min_datum,ppt.rating_after as rating
 
			from
 
				(select pin,min(datum) as min_datum,max(datum) as max_datum from players_performance_tournaments where datum<date(?) group by pin having max_datum>=date(?)) as subq
 
				join players_performance_tournaments as ppt on subq.pin=ppt.pin and subq.max_datum=ppt.datum
 
			where ppt.p_country_code=?"""
 

	
 
	res=cursor.execute(query,(date2,date2,date1,country))
 
	print("pin\tcelé roky hraní\tprvní turnaj\trating")
 
	for rec in res:
 
		print("\t".join([str(x) for x in rec]))
 

	
 

	
 
def populationStrength():
 
	pass
 
def populationStrength(year,country):
 
	date1=yearStart(year)
 
	date2=yearStart(year+1)
 
	query="""
 
		select round((rating_after-50)/100)*100 as rating, count(ppt.pin)
 
			from players_performance_tournaments as ppt
 
				join (select pin,max(datum) as max_datum from players_performance_tournaments where datum>=date(?) and datum<date(?) and p_country_code=? group by pin) as subq on ppt.pin=subq.pin and ppt.datum=subq.max_datum
 
			group by rating
 
			order by rating desc
 
	"""
 

	
 
	res=cursor.execute(query,(date1,date2,country))
 
	print("rating\tpočet hráčů s takovým a vyšším ratingem")
 
	for rec in res:
 
		print("\t".join([str(x) for x in rec]))
 

	
 

	
 
def populationAge():
 
	pass
 

	
 

	
 
def tournamentAttendance():
 
	pass
 

	
 

	
 
# playerYear("Kotowski Jaroslav",2017)
 
# yearByCountry(2017)
 
timeRank(2017,"CZ")
 
# timeRank(2017,"CZ")
 
# populationStrength(2017,"CZ")
0 comments (0 inline, 0 general)