Python 目标代码和程序代码
由于我仍然是python中的noob程序员,所以我想知道代码结构之间的区别。对象还是程序 下面是我当前处理mysql连接和查询的类的代码Python 目标代码和程序代码,python,object,Python,Object,由于我仍然是python中的noob程序员,所以我想知道代码结构之间的区别。对象还是程序 下面是我当前处理mysql连接和查询的类的代码 import pymysql class Database(object): def __init__(self): self.db = pymysql.connect(host="localhost", # your host, usually localhost
import pymysql
class Database(object):
def __init__(self):
self.db = pymysql.connect(host="localhost", # your host, usually localhost
user="root", # your username
passwd="", # your password
db="mgdeal") # name of the data base
self.cur = self.db.cursor()
def select(self, sql):
cursor = self.db.cursor()
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
return result
def insert (self, sql):
cursor= self.db.cursor()
cursor.execute(sql)
newID = cursor.lastrowid
cursor.close()
return newID
if __name__ == '__main__':
db = Database()
现在我已经发现并理解了这个新的mysql类
class MysqlPython(object):
"""
Python Class for connecting with MySQL server and accelerate development project using MySQL
Extremely easy to learn and use, friendly construction.
"""
__instance = None
__host = None
__user = None
__password = None
__database = None
__session = None
__connection = None
def __new__(cls, *args, **kwargs):
if not cls.__instance or not cls.__database:
cls.__instance = super(MysqlPython, cls).__new__(cls,*args,**kwargs)
return cls.__instance
## End def __new__
def __init__(self, host='localhost', user='root', password='', database=''):
self.__host = host
self.__user = user
self.__password = password
self.__database = database
## End def __init__
def __open(self):
try:
cnx = MySQLdb.connect(self.__host, self.__user, self.__password, self.__database)
self.__connection = cnx
self.__session = cnx.cursor()
except MySQLdb.Error as e:
print "Error %d: %s" % (e.args[0],e.args[1])
def __close(self):
self.__session.close()
self.__connection.close()
def select(self, table, where=None, *args, **kwargs):
result = None
query = 'SELECT '
keys = args
values = tuple(kwargs.values())
l = len(keys) - 1
for i, key in enumerate(keys):
query += "`"+key+"`"
if i < l:
query += ","
## End for keys
query += 'FROM `%s`' % table
if where:
query += " WHERE %s" % where
print(query)
## End if where
我想知道这两个类中哪一个更面向对象?
据我所知,对象代码总是从类生成Instance对象,但对我来说,它更像是一种将函数对象存储在超级函数中的方式…我想知道什么时候我们可以说它是对象还是非对象
同样在我的主程序中,有什么东西必须更改,才能说它是objet代码
谢谢你的帮助
emailFieldID = "Login"
passFieldID = "Password"
loginButtonXpath = "//*[@id='id_1__']"
headerFieldID = "header"
emailFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id(emailFieldID))
passFieldElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_id(passFieldID))
loginButtonElement = WebDriverWait(driver, 10).until(lambda driver: driver.find_element_by_xpath(loginButtonXpath))
# Envoie des credentials
emailFieldElement.clear()
emailFieldElement.send_keys(Username)
passFieldElement.clear()
passFieldElement.send_keys(Password)
loginButtonElement.click()
# Etape 2: Ajout des produit
#Initialisation de l'objet Database
db = Database()
sql = 'SELECT * FROM `table 2` WHERE `categorie` = "Lego"'
listeproduit = db.select(sql)
for record in listeproduit:
skuref = record[0]
print (skuref)
ean = record[1]
titre_prod = record[2]
print (titre_prod)
desc_prod = record[7]
img_prod = record[6]
cat1 = record[11]
cat2 = record[12]
cat3 = record[13]
cat4 = record[14]
marque = record[12]