I am in a bit of a jam in deciding how to structure my class. What I have is a baseball player class and necessary attributes of:
Player ID (a key from a DB)
Last name
First name
Team
Position
Opponent
about 10 or 11 stats (historical)
about 10 or 11 stats (projected)
pitcher matchup
weather
... and a few more
Some things to break this down a little:
1) put stats in dictionaries
2) make a team class that can hold general info common for all players on the team like weather and pitcher match up.
But, I still have 10 attributes after this.
Seeing this (Class with too many parameters: better design strategy?) has given me a couple ideas but don't know if they're ideal.
1) Use a dictionary - But then wouldn't be able to use methods to calculate stats (or would have to use separate functions)
2) Use args/kwargs - But from what I can gather, those seem to be for variable amounts of parameters, and all of my parameters will be required.
3) Breaking up into smaller classes - I have broken it up a bit already, but don't know if I can any further.
Is there a better way to build this rather than having a class with a bunch of parameters listed out?
If you think about this from the perspective of database design, it would probably be odd to have a BaseballPlayer object that has the following parameters:
Team
Position
Opponent
about 10 or 11 stats (historical)
about 10 or 11 stats (projected)
pitcher matchup
weather
Because there are certain things associated with a particular BaseballPlayer which remain relatively fixed, such as name, etc., but these other things are fluid and transitory.
If you were designing this as an application with various database tables, then, it's possible that each of the things listed here would represent a separate table, and the BaseballPlayer's relationship with these other tables amount to current and former Team, etc.
Thus, I would probably break up the problem into more classes, including a StatsClass and a Team class (which is probably what an Opponent really is...).
But it all depends what you would like to do. Usually when you are bending over backwards to cram data into a structure or doing the same to get it back out, the design could be reworked to make your job easier.
Related
My django app stores the actions that players do during a game. One of the models is called Event, and it contains a list of all actions by players. It has the following 4 columns: game_id, player_name, action, turn. Turn is the number of the turn in which the action takes place.
Now I want to count how often players behave in certain patterns. For example, I want to know how often a player takes decision A in turn 2 and that same player takes decision B in turn 3. I'm breaking my head over how I can do this. Can someone help?
Note: Ideally the queries should be efficient, bearing in mind that some related queries are following. For example, after the above query I would want to know how often a player takes decision C in turn 4, after doing A and B in turns 2 and 3. (The goal is to predict the likelihood of each action, given the actions in the past.)
I am new to database things and only have a very basic understanding of them.
I need to save historic data of a leaderboard and I am not sure how to do that in a good way.
I will get a list of accountName, characterName and xp.
Options I was thinking of so far:
An extra table for each account where I add their xp as another entry every 10 min (not sure where to put the character name in that option)
A table where I add another table into it every 10 min containing all the data I got for that interval
I am not very sure the first option since there will be about 2000 players I don't know if I want to have 2000 tables (would that be a problem?). But I also don't feel like the second option is a good idea.
It feels like with some basic dimensional modeling techniques you will be able to solve this.
Specifically it sounds like you are in need of a Player Dimension and a Play Fact table...maybe a couple more supporting tables along the way.
It is my pleasure to introduce you to the Guru of Dimensional Modeling (IMHO): Kimball Group - Dimensional Modeling Techniques
My advice - invest a bit of time there, put a few basic dimensional modeling tools in your toolbox, and this build should be quite enjoyable.
In general you want to have a small number of tables, and the number of rows per table doesn't matter so much. That's the case databases are optimized for. Technically you'd want to strive for a structure that implements the Third normal form.
If you wanted to know which account had the most xp, how would you do it? If each account has a separate table, you'd have to query each table. If there's a single table with all the accounts, it's a trivial single query. Expanding that to say the top 15 is likewise a simple single query.
If you had a history table with a snapshot every 10 minutes, that would get pretty big over time but should still be reasonable by database standards. A snapshot every 10 minutes for 2000 characters over 10 years would result in 1,051,920,000 rows, which might be close to the maximum number of rows in a sqlite table. But if you got to that point I think you might be better off splitting the data into multiple databases rather than multiple tables. How far back do you want easily accessible history?
What is a good data type for a unit collection in an rts?
Im contributing to an api that lets you write bots for the strategy game Starcraft2 in Python.
Right now there is a class units that inherits from list. Every frame, a new units object gets created and then selections of these units are made, creating new units objects, for example with a filter for all enemy units or all flying units.
We use these selections to find the closest enemy to control our units, select our units that can attack right now or need a different order and so on.
But this also means we do a lot of filtering by attributes of each unit in every frame which takes a lot of time. The time for inititalizing one units object alone is 2e-5 to 5e-5 sec and we do it millions of times per game which can slow down the bot and tests a lot, in addition to the filtering process with loops over each unit in the units object.
Is there a better datataype for this?
Maybe something that does not need to be recreated every time for each selection in one frame, but just starts with the initial list of all units we get from the protocol buffer and then the selections and filters can be applied without recreating the object? What would be a good way to implement this so that filtering multiple times per frame is not that slow and/or complicated?
This doesn't sound like a ADT problem at all. This sounds like inefficient programming. It is impossible for us to tell you the correct message to construct to achieve what you're going for.
What you should probably be investigating is how to construct a UnitView if you don't actually need to modify the units data. Consider something similar to how dictionaries return views in Python 3. See here for more details.
How would you set up the following class structure, and why?
Class Person represents a Person
Class Room represents a Room
Where I would like to store the Room location of a Person, with the ability to efficiently ask the following two questions:
1) What Room is Person X in? and,
2) Which Persons are in Room Y?
The options I can see are:
Class Person stores the Room location (question 2 would be inefficient),
Class Room stores its Persons (question 1 would be inefficient),
Both of the above (opportunity for data integrity issues) and,
External dictionary holds the data (seems against the spirit of OOP)
External dictionary holds the data (seems against the spirit of OOP)
Or maybe you can implement all approaches at once: you define both associations Person has a Room and Room has many persons, and also you index everything using a proper data structure to access your objects at the light speed!
That is, you can walk across your object graph using object-oriented associations (i.e. composition) and you implement some specific requirements storing your graph in some data structures to fulfill your requirement.
This isn't against the spirit of OOP. It's in favor of good coding practices instead of artificial dogmas.
Not every problem is a good fit for OOP. This is one of them. :-)
What you basically have here is a two-dimensional grid or matrix.
One axis represents the persons, the other represents the room. The restriction is that a person can only be in one room.
A list of (person, room) tuples seems like a good way to represent this in Python. Here's an example (in IPython), with your two queries:
In [1]: locations = [('alfred', 'office'), ('ben', 'storage'), ('charlene', 'office'), ('deborah', 'factory')]
In [2]: [room for person, room in locations if person == 'charlene']
Out[2]: ['office']
In [3]: [person for person, room in locations if room == 'office']
Out[3]: ['alfred', 'charlene']
The queries are list comprehensions so they are pretty fast.
Unless your list of locations is huge, I don't think performance will be a big problem.
Note that this solution will work well with empty rooms and persons that are not present.
In [4]: [room for person, room in locations if person == 'zeke']
Out[4]: []
In [5]: [person for person, room in locations if room == 'maintenance']
Out[5]: []
You will have to take care not to insert the same person twice.
Definately an external structure. That is the spirit of OOP. Classes represent objects. Neither a person is a property of a room, nor a room a property of a person, so there is no reason putting one in another's class. Instead, the dictionary represents a relationship between them, so it should be a new entity.
A simple format would be a dictionary where each key is a room and the value is a list of the people inside it. However, you could probably create a class around it if you need more complex functionality
Currently, I have two tables, Exercise and WorkoutPlan. These will have a many to many relationship.
Exercise
- ID
- Name
...
WorkoutPlan
- ID
- Name
- Exercises (Many to Many with Exercise through WorkoutPlanExercise)
...
In this Many to Many relationship table, I need to store information about a number of sets, such as there min_rest, max_rest, min_repetitions and max_repetitions.
Where I'm stuck is, is trying to figure out the best solution to do this. My first solution is to have another table (WorkoutPlanExerciseSet) that has a many to one relationship with the many to many table (WorkoutPlanExercise), as shown below.
WorkoutPlanExercise
- ID
- ExerciseID
- WorkoutPlanID
- Sets (One to Many with WorkoutPlanExerciseSet)
WorkoutPlanExerciseSet
- WorkoutPlanExerciseID
- MinRepititions
- MaxRepititions
- MinRest
- MaxRest
My second solution is to store all the information about the exercise set, in a single row in the many to many relationship table (WorkoutPlanExercise). For example:
WorkoutPlanExercise
ID ExerciseID WorkoutPlanID Sets Repititions Rest
1 1 1 3 10-12, 10-10, 12-12 90-120, 60-90, 30-30
To note, both the rest time and number of repetitions, can be a range or a single number. For the second solution, I think I would create a custom Django Form Field.
Which is better? Is the former bad database design? Is the latter bad application design?
If it makes any difference, I wish to be able to easily display the information in a user friendly manner, such as:
Example Workout Plan
Exercise Sets Repetitions Rest
Pull Ups 3 10 - 12 90 - 120
8 - 10 30
6 - 8 30
I guess second. Read about custom through field here.
UPDATE: see comments.
UPDATE 2, 3:
Actually, both are very nice. It depends on how you want to process data, stored in Repetitions and Rest fields. If you want to do heavy manipulations and calculations with data, e. g. calculate total rest time for WorkoutPlan or total number of repetitions for Exercise, then using former approach will be slightly easier.
UPDATE 4:
Storing data of the same kind as CSV in one field is bad idea. You will have a lot of fun if you have to change schema in the future. Use first approach. Also link1, link2.