我收到一个NameError:全局名称' valid_username'没有定义哪个让我困惑。为什么会这样?我在实际调用之前声明了valid_username函数,这是什么问题?
这里是代码完整代码:
import webapp2
import cgi
import re
class WelcomeHandler(webapp2.RequestHandler):
def get(self):
self.response.out.write("Welcome, ")
class MainPage(webapp2.RequestHandler):
def write_form(self, username="", username_error="", password_error="", verify_password_error="", email="",
email_error=""):
self.response.out.write(form % {
"username": username, "username_error": username_error,
"password_error": password_error,
"verify_password_error": verify_password_error,
"email": email, "email_error": email_error})
def checkEscaped(text):
return cgi.escape(text, quote="True")
def valid_username(username):
USER_RE = re.compile(r"^[a-zA-Z0-9_-]{3,20}$")
return USER_RE.match(checkEscaped(username))
def valid_password(password):
PASSWORD_RE = re.compile(r"^.{3,20}$")
return PASSWORD_RE.match(checkEscaped(password))
def valid_email(email):
if len(email) > 0:
EMAIL_RE = re.compile(r"^[\S]+@[\S]+\.[\S]+$")
return EMAIL_RE.match(checkEscaped(email))
else:
return True
def password_match(password, confirmPassword):
return password == confirmPassword
def get(self):
self.write_form()
def post(self):
input_username = self.request.get('username')
input_password = self.request.get('password')
input_verify_password = self.request.get('verify_password')
input_email = self.request.get('email')
username_error = ""
is_valid_name = valid_username(input_username)
if not is_valid_name:
username_error = "That's not a valid username."
password_error = ""
is_valid_password = valid_password(input_password)
if not is_valid_password:
password_error = "That wasn't a valid password."
verify_password_error = ""
is_valid_verify_password = password_match(input_password, input_verify_password)
if not is_valid_verify_password:
verify_password_error = "the passwords don't match"
email_error = ""
is_valid_email = valid_email(input_email)
if not is_valid_email:
email_error = "that's not a valid email"
if not (is_valid_name and is_valid_password and is_valid_verify_password and is_valid_email):
self.write_form(name, username_error, password_error, verify_password_error, email, email_error)
else:
self.redirect("/welcome")
app = webapp2.WSGIApplication([
('/', MainPage), ('/welcome', WelcomeHandler)], debug=True)
答案 0 :(得分:3)
您需要通过package com.reimaginedgaming.blur.jwin.events;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import net.minecraft.entity.Entity;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ChunkCoordinates;
import net.minecraftforge.event.entity.player.PlayerSleepInBedEvent;
import cpw.mods.fml.common.asm.transformers.deobf.FMLDeobfuscatingRemapper;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
public class BedSleepingFix {
private static Field sleeping;
private static Field sleeptimer;
private static Method setsize;
private static Method func_71013_b;
public static void setupFields() {
Class bed = EntityPlayer.class;
Class entity = Entity.class;
try {
sleeping = getField("sleeping",bed);
}
catch(Exception e){
System.out.println("Ran into error:\n"+e.getLocalizedMessage()+"\n between lines 34 and 37.");
}
try {
sleeptimer = getField("sleepTimer",bed);
}
catch(Exception e){
System.out.println("Ran into error:\n"+e.getLocalizedMessage()+"\n between lines 34 and 37.");
}
try {
setsize = getMethod("setSize",entity, float.class, float.class);
}
catch(Exception e){
System.out.println("Ran into error:\n"+e.getLocalizedMessage()+"\n between lines 34 and 37.");
}
try {
func_71013_b = getMethod("func_71013_b",bed, int.class);
} catch (Exception e) {
System.out.println("Ran into error:\n"+e.getLocalizedMessage()+"between lines 23 and 29.");
}
try {
func_71013_b.setAccessible(true);
}
catch(Exception e){
System.out.println("Ran into error:\n"+e.getLocalizedMessage()+"\n between lines 34 and 37.");
}
try {
setsize.setAccessible(true);
}
catch(Exception e){
System.out.println("Ran into error:\n"+e.getLocalizedMessage()+"\n between lines 34 and 37.");
}
try{
sleeping.setAccessible(true);
}
catch(Exception e){
System.out.println("Ran into error:\n"+e.getLocalizedMessage()+"\n between lines 34 and 37.");
}
try{
sleeptimer.setAccessible(true);
}
catch(Exception e){
System.out.println("Ran into error:\n"+e.getLocalizedMessage()+"\n between lines 34 and 37.");
}
}
public static Field getField(String methodName, Class classy){
for(Field i:classy.getDeclaredFields()){
if(FMLDeobfuscatingRemapper.INSTANCE.isRemappedClass(classy.getName())){
if(FMLDeobfuscatingRemapper.INSTANCE.unmap(i.getName())==methodName){
System.out.println("Found Field "+methodName+" as "+i.getName());
return i;
}
} else {
if(i.getName()==methodName){
System.out.println("Found Field "+methodName+" as "+methodName);
return i;
}
}
}
return null;
}
public static Method getMethod(String methodName, Class classy, Class... parameters){
for(Method i:classy.getDeclaredMethods()){
if(FMLDeobfuscatingRemapper.INSTANCE.isRemappedClass(classy.getName())){
if(FMLDeobfuscatingRemapper.INSTANCE.unmap(i.getName())==methodName){
if(arraysAreEqual(i.getParameterTypes(),parameters))
System.out.println("Found Method "+methodName+" as "+i.getName());
return i;
}
} else {
if(i.getName()==methodName){
System.out.println("Found Method "+methodName+" as "+methodName);
return i;
}
}
}
return null;
}
public static boolean arraysAreEqual(Object[] a, Object[] b){
if(a.length!=b.length)return false;
int check = 0;
for(int i = 0;i<=a.length;i++){
if(a[i]!=b[i]){
return false;
}
check = i;
}
if(b[check]!=null)return false;
return true;
}
@SubscribeEvent
public void sleepfix(PlayerSleepInBedEvent e) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException{
if(e.entity.worldObj.isRemote)return;
if (!e.entity.worldObj.provider.isSurfaceWorld())return;
if (e.entity.worldObj.isDaytime())return;
int d0 = 8;
int d1 = 8;
int d2 = 8;
List list = e.entity.worldObj.getEntitiesWithinAABB(EntityMob.class, AxisAlignedBB.getBoundingBox((double)e.x - d0, (double)e.y - d1, (double)e.z - d0, (double)e.x + d0, (double)e.y + d1, (double)e.z + d0));
EntityPlayer this1 = e.entityPlayer;
if (!list.isEmpty())
{
boolean lineOfSight = false;
for(Object i:list){
EntityMob em = (EntityMob) i;
if(em.canEntityBeSeen(this1)){
lineOfSight = true;
System.out.println("A "+em.getClass().getName()+" can see the player.");
}
if(em.getBlockPathWeight((int)this1.posX, (int)this1.posY, (int)this1.posZ)<.2){
lineOfSight = false;
System.out.println("A "+em.getClass().getName()+" can reach the player.");
}
}
if(lineOfSight){
System.out.println("Deemed unsafe by JWIN");
e.result = EntityPlayer.EnumStatus.NOT_SAFE;
return;
}
}
if (this1.isRiding())
{
this1.mountEntity((Entity)null);
}
if(setsize!=null)
setsize.invoke(this1, 0.2F, 0.2F);
this1.yOffset = 0.2F;
if(!this1.worldObj.isDaytime()){
if (this1.worldObj.blockExists(e.x, e.y, e.z))
{
int l = this1.worldObj.getBlock(e.x, e.y, e.z).getBedDirection(this1.worldObj, e.x, e.y, e.z);
float f1 = 0.5F;
float f = 0.5F;
switch (l)
{
case 0:
f = 0.9F;
break;
case 1:
f1 = 0.1F;
break;
case 2:
f = 0.1F;
break;
case 3:
f1 = 0.9F;
}
if(func_71013_b!=null)
func_71013_b.invoke(this1, l);
this1.setPosition((double)((float)e.x + f1), (double)((float)e.y + 0.9375F), (double)((float)e.z + f));
}
else
{
this1.setPosition((double)((float)e.x + 0.5F), (double)((float)e.y + 0.9375F), (double)((float)e.z + 0.5F));
}
System.out.println("Deemed safe by JWIN");
e.result = EntityPlayer.EnumStatus.OK;
System.out.println(e.result);
if(sleeping!=null)
sleeping.setBoolean(this1,true);
if(sleeptimer!=null)
sleeptimer.setInt(this1, 0);
this1.playerLocation = new ChunkCoordinates(e.x, e.y, e.z);
this1.motionX = this1.motionZ = this1.motionY = 0.0D;
if(!this1.worldObj.isRemote)
this1.worldObj.updateAllPlayersSleepingFlag();
}
return;
}
}
对象引用实例上的方法:
self
你在其他地方也有同样的问题; is_valid_password = self.valid_password(input_password)
应为checkEscaped()
,self.checkEscaped()
应为password_match()
等。
方法不是全局变量或本地变量。
接下来,您的方法也应接受 self.password_match()
引用;您自己的每个方法都缺少必需的参数:
self
当然,您可以将这四种方法 out 移动到类中,并实际上将它们作为全局函数而不是方法。在这种情况下,请再次取消def checkEscaped(self, text):
# ...
def valid_username(self, username):
# ...
def valid_password(self, password):
# ...
def valid_email(self, email):
# ...
def password_match(self, password, confirmPassword):
# ...
参数。