fairly intermediate programmer but Python beginner here. I've been working on a game for a while and I restructured all of my classes yesterday. Where I was initially using only compositional data structure, I'm now using a mix of both. My issues come when I want to spawn the player. Here's the relevant code.
class Object(object):
def __init__(self, **kwargs):
DeaultValues={'x':0, 'y':0, 'name':None, 'texture':None, 'blocks':False, 'ObjectID':None, 'Fighter':None, 'Corpse':None, 'Skill':None, 'ai':None}
for key,value in DeaultValues.items():
try:
vars(self)[key] = kwargs[key]
except ValueError:
vars(self)[key] = value
except KeyError:
vars(self)[key] = value
self.x = kwargs['x']
self.y = kwargs['y']
self.name=kwargs['name']
self.blocks=kwargs['blocks']
self.ObjectID=self.AttachID()
self.texture = kwargs['texture']
#This section binds an actors compenents to itself
self.Corpse = kwargs['Corpse']
if self.Corpse:
self.Corpse.owner = self
self.Skill=kwargs['Skill']
if self.Skill:
self.Skill.owner = self
self.Fighter = kwargs['Fighter']
if self.Fighter:
self.Fighter.owner = self
self.ai = kwargs['ai']
if self.ai:
self.ai.owner = self
class HighActor(Object):
def __init__(self, **kwargs):
super(HighActor, self).__init__(**kwargs)
class Player(HighActor):
def __init__(self, Level=1, Xp=0, PreviousLevel=0, PreviousLevelThreshold=100, LevelThreshold=500, **kwargs):
super(Player, self).__init__(**kwargs)
self.LevelThreshold = LevelThreshold
self.PreviousLevelThreshold=PreviousLevelThreshold
self.PreviousLevel=PreviousLevel
self.Level = Level
self.Xp = Xp
def SpawnPlayer():
global player
FighterComponent = Fighter(MaxHp=100, Hp=100, IsHasted=[False, False], death_function=None)
CorpseComponent = Corpse()
SkillComponent = HighActorSkill()
player=Player(name="player", x=None, y=None, texture="player.png", blocks=True, ObjectID=None, Fighter=FighterComponent, Corpse=CorpseComponent, Skill=SkillComponent, ai=None)
The above code works just fine, however its not really inheriting anything. To get the player object to not error I had to add to add all of the attributes of the base object class to the Player initialization. If I remove any of the values that are set to none in the player=Player() statement I get value errors or key errors. I tried to correct this by having a dict of default values that looped through all kwargs the init was given and if they had no value, set them to the default found. This worked until I got to any of the components. So in the case of not specifying ai=none, I got key errors. I would really love to have my code in such a format that if I do not specify a value for any of the base object class attributes the default values would be passed in, but if I do specify a value, that gets passed up to the base class. My ideal end result would be to have my player instancing look like this:
def SpawnPlayer():
global player
FighterComponent = Fighter(MaxHp=100, Hp=100, IsHasted=[False, False], death_function=None)
CorpseComponent = Corpse()
SkillComponent = HighActorSkill()
player=Player(name="player", texture="player.png", blocks=True, Fighter=FighterComponent, Corpse=CorpseComponent, Skill=SkillComponent)
I have a suspicion that my inheritance isn't working 100% because I get errors if I leave out ObjectID even though that should be assigned since in the init of the bass class its set equal to getid(self). So I'm either having issues with my inheritance (I'm really struggling with Super), or the signatures of my objects, and I'm not quite sure what my problem is, and more importantly why. I'm not opposed to changing the codes signature dramatically, as I'm still writing the engine so nothing is reliant on this yet. What should be done?
I think your class structure should be different. Each class should only have the attributes it needs, add new ones as you build up the inheritance, e.g.:
class Object(object):
def __init__(self, x=0, y=0, name=None, **kwargs):
self.x = x
self.y = y
self.name = name
class HighActor(Object):
def __init__(self, corpse=None, skill=None, **kwargs):
super(HighActor, self).__init__(**kwargs)
self.corpse = corpse
self.skill = skill
class Player(HighActor):
def __init__(self, level=1, xp=0, **kwargs):
super(Player, self).__init__(**kwargs)
self.level = level
self.xp = xp
At each level you specify the attributes - all Objects should have x, y and name, all HighActors should also have corpse and skill, etc. Now you can specify arguments to supply to any of the three levels of the hierarchy, or leave them out to get defaults:
player = Player(name="player one", skill=100, xp=12)
You may have things that don't fit into this inheritance scheme - it is OK to have more than one separate set of inheritance relationships in your model, don't force it!
This works because the **kwargs at the end of each __init__ "mops up" any keyword arguments that method isn't expecting into a dictionary kwargs, and can then pass them all to the next level. When you do so, super(...).__init__(**kwargs), this unpacks the dictionary back into keyword arguments, and any that aren't present will take the specified default value.
Related
Accessing class methods in templates, this works but was wondering if their was a better way?
someclass
class Something():
somevar = None
def __init__(self, somevar):
self.somevar = somevar
def set_somevar(self, newvar):
self.somevar = newvar
def set_weird_and_somevar(self, weird, somevar):
self.weird = weird
self.somevar = somevar
def get_tag(self, tag):
templateTag
from django import template
register = template.Library()
#register.filter
def class_get_method(value, method):
f = method.split('.')
method = f[0]
del f[0]
p = getattr(value, method)
if f:
return p(*f)
else:
return p()
in template, lets say content is a class instance
{{content|class_get_method:'set_weird_and_somevar.wenday_adams.nothervar'}}
class Something():
somevar = None
def __init__(self, somevar):
self.somevar = somevar
Yikes!
Don't do that.
The Turing machine you describe has well-defined semantics.
But python engineers don't write such code,
because it leads to maintenance headaches.
Python is all about namespaces.
There is a global namespace, which Something is in.
There is a class namespace which,
ever since the Something class was defined (at parse time)
has a somevar attribute with value None.
Later on, at run time, we create a pair of objects
with self.somevar values of 1 and 2.
But the class attribute is still None.
This is perfectly well defined.
The machine won't become confused.
But you or future maintenance engineers very likely will.
Choose a different name for the class attribute, please.
Reference it as Something.somevar, or as cls.somevar
from within a #classmethod.
Notice that class attribute somevar can be initialized
as a mutable data structure, such as a dict.
And then both classmethods and ordinary methods can mutate it.
So I have this flask app I'm making and I need some help with a variable access.
Most of the time, when you define a form in flask, you'll do the following :
class MyForm(Form):
my_field = StringField('I'm a field')
my_submit = SubmitField('Go!')
And when the time comes where you need the form, you'll declare an instance of that class with form = MyForm()
Up to here, it's all good, However :
If you want say, a SelectField (Dropdown) where the choices depend on the answers of a previous form, you need to be able to give the new form those choices. This is what I'm trying to achieve, but I can't get a variable to keep its contents.
Here is my Form code (Above the page code):
class DataMappingForm(Form):
dm_choices = #I need this array !
DMpatient_id = SelectField(u'Select Patient ID Column',
choices=dm_choices, validators=[Required()])
...
Here is my Page code :
#app.route('/upload', methods=['GET','POST'])
def upload():
uform = SomeOtherForm()
if uform.is_submitted() and uform.data['Usubmit']:
#Do stuff from previous form
# and declare array_of_choices
dmform = DataMappingForm() #Needs array_of_choices to work
...
What I've tried so far :
session['dm_choices'] gives me a working outside of request context error
global variables, get reset for some reason
overloading the __init__ of Form by adding the array but i can't access it in the parts above the __init__ function.
I should mention, this all needs to be on the same page.
Is there a way to pass this array_of_choices to my DataMappingForm class ?
EDIT This is what it looked like when I trid the __init__ overload:
class DataMappingForm(Form):
def __init__(self, dm_choices, *args, **kwargs):
self.dm_choices = dm_choices
Form.__init__(self, *args, **kwargs)
DMpatient_id = SelectField(u'Select Patient ID Column',
choices=dm_choices, validators=[Required()])
#I've tried putting it above or below, I get 'dm_choices is not defined'
I've Got it ! Thanks to #synonym for pointing me in the right direction with your last link.
All you need to do is declare a function in which the class is defined. You then pass the variable to the function, and it will be accessible within the class.
Finally, make the function return the form object.
Example :
def makeMyForm(myArray):
def class MyForm(Form):
my_select_field = SelectField(u'I'm a select field', choices=myArray)
my_submit = SubmitField(u'Go!')
return MyForm()
And to make the form, you use :
form = makeMyForm(theArrayYouWant)
And VoilĂ !
Note : As I've had the problem before, I'll mention that the Array is composed of tuples :
myArray = [('value','What you see'),('value2','What you see again')]
If you want to dynamically change the choices of a SelectField the following should work:
class DataMappingForm(Form):
def __init__(self, choices)
self.DMpatient_id.choices = choices
DMpatient_id = SelectField(u'Select Patient ID Column') #note that choices is absent
If you want fully dynamic fields you can create the class dynamically in a function. From the WTForms Documentation:
def my_view():
class F(MyBaseForm):
pass
F.username = StringField('username')
for name in iterate_some_model_dynamically():
setattr(F, name, StringField(name.title()))
form = F(request.POST, ...)
# do view stuff
In that case you can customize the form as much as you want. Of course in the case you only want to customize the choices the first approach should be enough.
Below is my pipeline and it seems that I can't pass the parameters to my models by using the ModelTransformer class, which I take it from the link (http://zacstewart.com/2014/08/05/pipelines-of-featureunions-of-pipelines.html)
The error message makes sense to me, but I don't know how to fix this. Any idea how to fix this? Thanks.
# define a pipeline
pipeline = Pipeline([
('vect', DictVectorizer(sparse=False)),
('scale', preprocessing.MinMaxScaler()),
('ess', FeatureUnion(n_jobs=-1,
transformer_list=[
('rfc', ModelTransformer(RandomForestClassifier(n_jobs=-1, random_state=1, n_estimators=100))),
('svc', ModelTransformer(SVC(random_state=1))),],
transformer_weights=None)),
('es', EnsembleClassifier1()),
])
# define the parameters for the pipeline
parameters = {
'ess__rfc__n_estimators': (100, 200),
}
# ModelTransformer class. It takes it from the link
(http://zacstewart.com/2014/08/05/pipelines-of-featureunions-of-pipelines.html)
class ModelTransformer(TransformerMixin):
def __init__(self, model):
self.model = model
def fit(self, *args, **kwargs):
self.model.fit(*args, **kwargs)
return self
def transform(self, X, **transform_params):
return DataFrame(self.model.predict(X))
grid_search = GridSearchCV(pipeline, parameters, n_jobs=-1, verbose=1, refit=True)
Error Message:
ValueError: Invalid parameter n_estimators for estimator ModelTransformer.
GridSearchCV has a special naming convention for nested objects. In your case ess__rfc__n_estimators stands for ess.rfc.n_estimators, and, according to the definition of the pipeline, it points to the property n_estimators of
ModelTransformer(RandomForestClassifier(n_jobs=-1, random_state=1, n_estimators=100)))
Obviously, ModelTransformer instances don't have such property.
The fix is easy: in order to access underlying object of ModelTransformer one needs to use model field. So, grid parameters become
parameters = {
'ess__rfc__model__n_estimators': (100, 200),
}
P.S. it's not the only problem with your code. In order to use multiple jobs in GridSearchCV, you need to make all objects you're using copy-able. This is achieved by implementing methods get_params and set_params, you can borrow them from BaseEstimator mixin.
I have a form class that looks something like this:
class RegisterForm(Form):
username = Field(model_field='username', filters=validators.minlength(3))
You'll notice that username is a class variable. I believe this means that Field will be constructed once the first time the RegisterForm is used (after apache is restarted). It will not be re-constructed between page reloads (unless a 2nd WSGI instance (?) is spawned, but we won't get into that). I noticed this because some of the values I've set in Field.__init__ are not being reset.
However, Form.__init__ does seem to be called each page reload. I guess that's because of the way I'm using it? I'm actually constructing it like form = RegisterForm(request) at each page request.
So... supposing I don't want the [class variables in] RegisterForm to be "cached" and have the Fields re-initialized at each request... how would I do that? (without modifying the syntax of RegisterForm; you can do whatever inside the base class, Form)
You could update the class variable each instantiation:
class ContactForm(forms.Form):
username = Field(model_field='username', filters=validators.minlength(3))
def __init__(self, *args, **kwargs):
super(ContactForm, self).__init__(*args, **kwargs)
ContactForm.username = Field(model_field='username', filters=validators.minlength(3))
You could define the class within a function. That way it gets constructed each time the function is called.
def gotclass(data):
class InnerClass(object):
someattr = DoSomethingWith(data)
return InnerClass
MyNewClass = gotclass(42)
someobj = MyNewClass()
Here is the field declaration in a form:
max_number = forms.ChoiceField(widget = forms.Select(),
choices = ([('1','1'), ('2','2'),('3','3'), ]), initial='3', required = True,)
I would like to set the initial value to be 3 and this doesn't seem to work. I have played about with the param, quotes/no quotes, etc... but no change.
Could anyone give me a definitive answer if it is possible? And/or the necessary tweak in my code snippet?
I am using Django 1.0
Try setting the initial value when you instantiate the form:
form = MyForm(initial={'max_number': '3'})
This doesn't touch on the immediate question at hand, but this Q/A comes up for searches related to trying to assign the selected value to a ChoiceField.
If you have already called super().__init__ in your Form class, you should update the form.initial dictionary, not the field.initial property. If you study form.initial (e.g. print self.initial after the call to super().__init__), it will contain values for all the fields. Having a value of None in that dict will override the field.initial value.
e.g.
class MyForm(forms.Form):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
# assign a (computed, I assume) default value to the choice field
self.initial['choices_field_name'] = 'default value'
# you should NOT do this:
self.fields['choices_field_name'].initial = 'default value'
You can also do the following. in your form class def:
max_number = forms.ChoiceField(widget = forms.Select(),
choices = ([('1','1'), ('2','2'),('3','3'), ]), initial='3', required = True,)
then when calling the form in your view you can dynamically set both initial choices and choice list.
yourFormInstance = YourFormClass()
yourFormInstance.fields['max_number'].choices = [(1,1),(2,2),(3,3)]
yourFormInstance.fields['max_number'].initial = [1]
Note: the initial values has to be a list and the choices has to be 2-tuples, in my example above i have a list of 2-tuples. Hope this helps.
I ran into this problem as well, and figured out that the problem is in the browser. When you refresh the browser is re-populating the form with the same values as before, ignoring the checked field. If you view source, you'll see the checked value is correct. Or put your cursor in your browser's URL field and hit enter. That will re-load the form from scratch.
Both Tom and Burton's answers work for me eventually, but I had a little trouble figuring out how to apply them to a ModelChoiceField.
The only trick to it is that the choices are stored as tuples of (<model's ID>, <model's unicode repr>), so if you want to set the initial model selection, you pass the model's ID as the initial value, not the object itself or it's name or anything else. Then it's as simple as:
form = EmployeeForm(initial={'manager': manager_employee_id})
Alternatively the initial argument can be ignored in place of an extra line with:
form.fields['manager'].initial = manager_employee_id
Dave - any luck finding a solution to the browser problem? Is there a way to force a refresh?
As for the original problem, try the following when initializing the form:
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.base_fields['MyChoiceField'].initial = initial_value
To be sure I need to see how you're rendering the form. The initial value is only used in a unbound form, if it's bound and a value for that field is not included nothing will be selected.