如何从另一个py文件导入类?

时间:2018-10-14 11:15:01

标签: python

我正在使用开源库。他们添加了我想要的新功能,但是我还不能升级该库。

我下载了相关的.py文件,并在我的代码中将其引用为:

原文:

from airflow.contrib.operators.bigquery_check_operator import BigQueryCheckOperator

新功能:

from /home/.../airflow/MyOperators/bigquery_check_operator.py import BigQueryCheckOperator as MyBigQueryCheckOperator

但是,这不起作用。它给出了无效的语法错误。

基本上我想将此文件引用为MyBigQueryCheckOperator,而原始文件仍为BigQueryCheckOperator

我在做什么错了?

4 个答案:

答案 0 :(得分:2)

这是无效的语法。您不能在python导入路径中使用/或文件扩展名。

from /home/.../airflow/MyOperators/bigquery_check_operator.py import BigQueryCheckOperator as MyBigQueryCheckOperator

您应将文件移至相对目录,然后使用常规的python导入语法将其导入。

from newversion.bigquery_check_operator import BigQueryCheckOperator as MyBigQueryCheckOperator

答案 1 :(得分:1)

您可以通过将软件包的路径附加到sys.path中来临时添加软件包的路径。
尝试:

import sys
sys.path.append('/home/.../airflow/MyOperators/')
from bigquery_check_operator import BigQueryCheckOperator

相反。

答案 2 :(得分:-1)

公共类Home扩展了AppCompatActivity {

EditText txt_name,txt_password;
Button btn_login,btn_register;
DBHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    dbHelper=new DBHelper(this);

    txt_name=(EditText)findViewById(R.id.txt_name);
    txt_password=(EditText)findViewById(R.id.txt_password);

    btn_login=(Button) findViewById(R.id.btn_login);
    btn_register=(Button) findViewById(R.id.btn_register);

}

public void buttonClick(View view){

    switch (view.getId()){

        case R.id.btn_login:
            List<User> userList=dbHelper.readAllInfo();
            int id=0;
            for (int i=0;i<userList.size();i++){
                User user = userList.get(i);
                if(user.getUserName().equals(txt_name.getText().toString()) && user.getPassword().equals(txt_password.getText().toString())){
                    id=user.getId();
                }
            }
            if(id>0){
                Toast.makeText(this,"Login Successful!!!",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,EditProfile.class);
                intent.putExtra("ID",id);
                startActivity(intent);
                this.finish();
                break;
            }
            else
                Toast.makeText(this,"Login Unsuccessful!!!",Toast.LENGTH_SHORT).show();

            break;


        case R.id.btn_register:
            Intent intent = new Intent(this,ProfileManagement.class);
            startActivity(intent);
            break;
    }
}

}

公共最终课程UserProfile {

private UserProfile(){}

public static class Users implements BaseColumns{

    public final static String TABLE_NAME="UserInfo";
    public final static String COLUMN_NAME="userName";
    public final static String COLUMN_PASSWORD="password";
    public final static String COLUMN_DOB="dateOfBirth";
    public final static String COLUMN_GENDER="gender";
}

}

公共类用户{

private int id;
private String userName;
private String password;
private String dob;
private String gender;

public User() {
}

public User(int id, String userName, String password, String dob, String gender) {
    this.id = id;
    this.userName = userName;
    this.password = password;
    this.dob = dob;
    this.gender = gender;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getDob() {
    return dob;
}

public void setDob(String dob) {
    this.dob = dob;
}

public String getGender() {
    return gender;
}

public void setGender(String gender) {
    this.gender = gender;
}

}

公共类ProfileManagement扩展了AppCompatActivity {

EditText txt_name,txt_password,txt_date;
RadioGroup rb_group;
RadioButton rb_male,rb_female;
Button btn_update;
DBHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile_management);

    dbHelper=new DBHelper(this);

    txt_name=(EditText)findViewById(R.id.txt_name);
    txt_password=(EditText)findViewById(R.id.txt_password);
    txt_date=(EditText)findViewById(R.id.txt_date);

    rb_group=(RadioGroup)findViewById(R.id.rb_group);
    rb_male=(RadioButton)findViewById(R.id.rb_male);
    rb_female=(RadioButton)findViewById(R.id.rb_female);

    btn_update=(Button) findViewById(R.id.btn_update);

}

public void PMbuttonClick(View view){

    User user= new User();
    user.setUserName(txt_name.getText().toString());
    user.setPassword(txt_password.getText().toString());
    user.setDob(txt_date.getText().toString());
    if(rb_group.getCheckedRadioButtonId()==R.id.rb_male)
        user.setGender("Male");
    else
        user.setGender("Female");

    long newId=dbHelper.addInfo(user);

    if(newId>0) {
        Toast.makeText(this, "Successfully Added", Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(this,Home.class);
        startActivity(intent);
        finish();
    }

    else
        Toast.makeText(this,"Unsuccessful Insertion!!!",Toast.LENGTH_SHORT).show();



}

}

公共类EditProfile扩展了AppCompatActivity {

EditText txt_name,txt_password,txt_date;
RadioButton rb_male,rb_female;
Button btn_edit,btn_delete,btn_search;
DBHelper dbHelper;
int id;
User user;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_edit_profile);

    Intent intent=getIntent();
    id=intent.getIntExtra("ID",-1);

    dbHelper=new DBHelper(this);
    user=new User();

    txt_name=(EditText)findViewById(R.id.txt_name);
    txt_password=(EditText)findViewById(R.id.txt_password);
    txt_date=(EditText)findViewById(R.id.txt_date);

    rb_male=(RadioButton)findViewById(R.id.rb_male);
    rb_female=(RadioButton)findViewById(R.id.rb_female);

    btn_search=(Button) findViewById(R.id.btn_search);
    btn_delete=(Button) findViewById(R.id.btn_delete);
    btn_edit=(Button) findViewById(R.id.btn_edit);
}

public void EPbuttonClick(View view){

    switch (view.getId()){

        case R.id.btn_search:
            user=dbHelper.readAllInfo(id);
            txt_name.setText(user.getUserName());
            txt_password.setText(user.getPassword());
            txt_date.setText(user.getDob());
            if(user.getGender().equals("Male"))
               rb_male.setChecked(true);
            else
                rb_female.setChecked(true);
            break;


        case R.id.btn_delete:
            if(dbHelper.deleteInfo(id)){
                Toast.makeText(this,"Successfully Deleted",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(this,Home.class);
                startActivity(intent);
            }

            break;

        case R.id.btn_edit:
            user.setId(id);
            user.setUserName(txt_name.getText().toString());
            user.setPassword(txt_password.getText().toString());
            user.setDob(txt_date.getText().toString());
            if(rb_male.isChecked())
                user.setGender("Male");
            else
                user.setGender("Female");

            if(dbHelper.updateInfo(user))
                Toast.makeText(this,"Successfully Updated",Toast.LENGTH_SHORT).show();
            else
                Toast.makeText(this,"Update Unsuccessful",Toast.LENGTH_SHORT).show();

            break;
    }
}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent intent = new Intent(this,Home.class);
    startActivity(intent);
}

}

公共类DBHelper扩展了SQLiteOpenHelper {

public static final String DATABASE="user.db";

public DBHelper(Context context) {
    super(context, DATABASE, null, 1);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String query="CREATE TABLE "+ UserProfile.Users.TABLE_NAME +" ( "+
            UserProfile.Users._ID +" INTEGER PRIMARY KEY, "+
            UserProfile.Users.COLUMN_NAME +" TEXT, "+
            UserProfile.Users.COLUMN_PASSWORD +" TEXT, "+
            UserProfile.Users.COLUMN_DOB +" TEXT, "+
            UserProfile.Users.COLUMN_GENDER +" TEXT);";

    sqLiteDatabase.execSQL(query);
}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ UserProfile.Users.TABLE_NAME);
    onCreate(sqLiteDatabase);
}

public long addInfo(User user){

    SQLiteDatabase db=getWritableDatabase();

    ContentValues values=new ContentValues();
    values.put(UserProfile.Users.COLUMN_NAME,user.getUserName());
    values.put(UserProfile.Users.COLUMN_PASSWORD,user.getPassword());
    values.put(UserProfile.Users.COLUMN_DOB,user.getDob());
    values.put(UserProfile.Users.COLUMN_GENDER,user.getGender());

    long id=db.insert(UserProfile.Users.TABLE_NAME,null,values);

    return id;
}

public boolean updateInfo(User user){

    SQLiteDatabase db=getReadableDatabase();

    ContentValues values=new ContentValues();
    values.put(UserProfile.Users.COLUMN_NAME,user.getUserName());
    values.put(UserProfile.Users.COLUMN_PASSWORD,user.getPassword());
    values.put(UserProfile.Users.COLUMN_DOB,user.getDob());
    values.put(UserProfile.Users.COLUMN_GENDER,user.getGender());

    String selection= UserProfile.Users._ID+" = ?";
    String[] selectionArgs={Integer.toString(user.getId())};

    int count=db.update(UserProfile.Users.TABLE_NAME,values,selection,selectionArgs);

    if (count>0)
        return true;
    else
        return false;
}

public List<User> readAllInfo(){

    SQLiteDatabase db=getReadableDatabase();

    String[] projection={UserProfile.Users._ID,
                        UserProfile.Users.COLUMN_NAME,
                        UserProfile.Users.COLUMN_PASSWORD,
                        UserProfile.Users.COLUMN_DOB,
                        UserProfile.Users.COLUMN_GENDER};

    String order= UserProfile.Users._ID+" ASC";

    Cursor cursor = db.query(UserProfile.Users.TABLE_NAME,projection,null,null,null,null,order);

    List<User> list = new ArrayList();
    User user=new User();

    while(cursor.moveToNext()){
        user.setId(cursor.getInt(cursor.getColumnIndex(UserProfile.Users._ID)));
        user.setUserName(cursor.getString(cursor.getColumnIndex(UserProfile.Users.COLUMN_NAME)));
        user.setPassword(cursor.getString(cursor.getColumnIndex(UserProfile.Users.COLUMN_PASSWORD)));
        user.setDob(cursor.getString(cursor.getColumnIndex(UserProfile.Users.COLUMN_DOB)));
        user.setGender(cursor.getString(cursor.getColumnIndex(UserProfile.Users.COLUMN_GENDER)));

        list.add(user);
    }

    return list;
}

public User readAllInfo(int id){

    SQLiteDatabase db=getReadableDatabase();

    String[] projection={UserProfile.Users._ID,
            UserProfile.Users.COLUMN_NAME,
            UserProfile.Users.COLUMN_PASSWORD,
            UserProfile.Users.COLUMN_DOB,
            UserProfile.Users.COLUMN_GENDER};

    String selection= UserProfile.Users._ID+" = ?";
    String[] selectionArgs={Integer.toString(id)};

    String order= UserProfile.Users._ID+" ASC";

    Cursor cursor = db.query(UserProfile.Users.TABLE_NAME,projection,selection,selectionArgs,null,null,order);

    User user=new User();

    while(cursor.moveToNext()){
        user.setId(cursor.getInt(cursor.getColumnIndex(UserProfile.Users._ID)));
        user.setUserName(cursor.getString(cursor.getColumnIndex(UserProfile.Users.COLUMN_NAME)));
        user.setPassword(cursor.getString(cursor.getColumnIndex(UserProfile.Users.COLUMN_PASSWORD)));
        user.setDob(cursor.getString(cursor.getColumnIndex(UserProfile.Users.COLUMN_DOB)));
        user.setGender(cursor.getString(cursor.getColumnIndex(UserProfile.Users.COLUMN_GENDER)));
    }

    return user;
}

public boolean deleteInfo(int id){
    SQLiteDatabase db=getReadableDatabase();

    String selection= UserProfile.Users._ID+" = ?";
    String[] selectionArgs={Integer.toString(id)};

    int count=db.delete(UserProfile.Users.TABLE_NAME,selection,selectionArgs);

    if(count>0)
        return true;
    else
        return false;
}

}

答案 3 :(得分:-1)

public boolean deleteInfo(int id) {
    SQLiteDatabase db=getReadableDatabase();

    String selection= UserProfile.Users._ID+" = ?";
    String[] selectionArgs={Integer.toString(id)};

    int count=db.delete(UserProfile.Users.TABLE_NAME,selection,selectionArgs);

    if(count>0)
        return true;
    else
        return false;
}
相关问题