Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Beginner question!

Reply
Thread Tools

Beginner question!

 
 
SMALLp
Guest
Posts: n/a
 
      12-21-2007
Hy! I have error something like this

TypeError: unbound method insert() must be called with insertData
instance as first argument (got str instance instead)

CODE:

File1.py
sql.insertData.insert("files", data)

sql.py

class insertData:
def insert(self, dataTable, data):
conn = self.openConnection.openConnection()
cursor = conn.cursor()
sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
file_path_local, file_path_FTP, curent_location, FTP_valid_time,
uploaded, last_modified, last_verified, file_type, file_category) VLAUES
"+data
cursor.execute(sql)
conn.Close()


Help and advice neaded!
 
Reply With Quote
 
 
 
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      12-21-2007
On Dec 21, 9:11 am, SMALLp <po...@email.t-com.hr> wrote:
> Hy! I have error something like this
>
> TypeError: unbound method insert() must be called with insertData
> instance as first argument (got str instance instead)
>
> CODE:
>
> File1.py
> sql.insertData.insert("files", data)
>
> sql.py
>
> class insertData:
> def insert(self, dataTable, data):
> conn = self.openConnection.openConnection()
> cursor = conn.cursor()
> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
> "+data
> cursor.execute(sql)
> conn.Close()
>
> Help and advice neaded!


I think you need to post the real traceback or the real code since
your error message doesn't look like it has anything to do with the
code above. At least, I do not see a method named "insert".

Which database module are you using?

Mike
 
Reply With Quote
 
 
 
 
SMALLp
Guest
Posts: n/a
 
      12-21-2007
wrote:
> On Dec 21, 9:11 am, SMALLp <po...@email.t-com.hr> wrote:
>> Hy! I have error something like this
>>
>> TypeError: unbound method insert() must be called with insertData
>> instance as first argument (got str instance instead)
>>
>> CODE:
>>
>> File1.py
>> sql.insertData.insert("files", data)
>>
>> sql.py
>>
>> class insertData:
>> def insert(self, dataTable, data):
>> conn = self.openConnection.openConnection()
>> cursor = conn.cursor()
>> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
>> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
>> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
>> "+data
>> cursor.execute(sql)
>> conn.Close()
>>
>> Help and advice neaded!

>
> I think you need to post the real traceback or the real code since
> your error message doesn't look like it has anything to do with the
> code above. At least, I do not see a method named "insert".
>
> Which database module are you using?
>
> Mike


Traceback (most recent call last):
File "/home/pofuk/MzMFIleShare/sharePanel.py", line 130, in share
self.scanDirsAndFiles(dirPath)
File "/home/pofuk/MzMFIleShare/sharePanel.py", line 158, in
scanDirsAndFiles
sql.insertData.insert("files", data)
TypeError: unbound method insert() must be called with insertData
instance as first argument (got str instance instead)



share.py

import wx
import os
import sql
import login
class sharePanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
sizer = wx.BoxSizer(wx.VERTICAL)

nb = wx.Notebook(self, -1,style=wx.NB_TOP)

self.sheet1 = p1(nb, id)
self.sheet2 = p2(nb, id)
self.sheet3 = p3(nb, id)

nb.AddPage(self.sheet1, 'SEt1')
nb.AddPage(self.sheet2, 'Sheet2')
nb.AddPage(self.sheet3, 'Sheet3')

self.sheet1.SetFocus()

sizer.Add(nb,0, wx.EXPAND | wx.TOP, 20)



self.SetSizerAndFit(sizer)

class pageMenu(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.SetBackgroundColour("WHITE")

sizer = wx.BoxSizer(wx.HORIZONTAL)
self.links = wx.StaticText(self, -1, "<<< 1 2 3 4 5 6 7 8 9
10 >>>")
sizer.Add(self.links)

class p1(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, size=(500, 300))
self.SetBackgroundColour("WHITE")

sizer = wx.BoxSizer(wx.VERTICAL)

self.pMenu = pageMenu(self, id)
sizer.Add(self.pMenu,0, wx.LEFT | wx.TOP | wx.RIGHT, 40)

fileList = myFilesList(self, id)
sizer.Add(fileList, 0, wx.LEFT | wx.RIGHT, 10)

shareBox = sharePanelBox(self, id)
sizer.Add(shareBox,0, wx.EXPAND | wx.TOP | wx.LEFT, 20)

sizer.Fit(self)
self.SetSizerAndFit(sizer)

class p2(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, size=(200, 200))
self.SetBackgroundColour("RED")

class p3(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id, size=(200, 100))
self.SetBackgroundColour("YELLOW")


class myFilesList(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.SetBackgroundColour("WHITE")

vsizer = wx.BoxSizer(wx.VERTICAL)
data = {'fileName':'My filename',
'filePath':'/home/pofuk/Documents/myfile', 'fileSize':'41223 MB'}


for i in range(10):
d = [myFilesListItem(self, id, data)]
vsizer.Add(d[0],1, wx.EXPAND)

self.SetSizerAndFit(vsizer)



class myFilesListItem(wx.Panel):
def __init__(self, parent, id, data):
wx.Panel.__init__(self, parent, id)
self.SetBackgroundColour("WHITE")

hsizer = wx.BoxSizer(wx.HORIZONTAL)

self.fileName = wx.TextCtrl(self, -1, data['fileName'])
hsizer.Add(self.fileName,1, wx.EXPAND)
self.filePath = wx.StaticText(self, -1, data['filePath'])
hsizer.Add(self.filePath,1, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 10)
self.fileSize = wx.StaticText(self, -1, data['fileSize'])
hsizer.Add(self.fileSize, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT, 30)

self.delete = wx.Button(self, -1, "Delete")
hsizer.Add(self.delete, 0, wx.LEFT, 20)

self.SetSizerAndFit(hsizer)

def setData(text):
self.fileName.Value = text


class sharePanelBox(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.SetBackgroundColour("WHITE")
sizer = wx.FlexGridSizer(1,3)

self.files = wx.CheckBox(self, -1, 'Share Files')
self.folders = wx.CheckBox(self, -1, 'Share Folders')
shareButton = wx.Button(self, -1, 'Share')

sizer.Add(self.files,0, wx.ALIGN_CENTER_VERTICAL)
sizer.Add(self.folders,0, wx.ALIGN_CENTER_VERTICAL)
sizer.Add(shareButton, 0, wx.LEFT, 20)

self.Bind(wx.EVT_BUTTON, self.share, id=shareButton.GetId())
self.SetSizerAndFit(sizer)

def share(self, event):
if self.files.IsChecked() and self.folders.IsChecked():
dialog = wx.DirDialog(self)
dialog.ShowModal()
dirPath = dialog.GetPath()
print dirPath
self.scanDirsAndFiles(dirPath)
if self.files.IsChecked() and not self.folders.IsChecked():
dialog = wx.FileDialog(self, style=wx.OPEN)
dialog.ShowModal()
if not self.files.IsChecked() and self.folders.IsChecked():
dialog = wx.DirDialog(self)
dialog.ShowModal()

def scanDirsAndFiles(self, dirPath):
for item in os.listdir(dirPath):
if os.path.isdir(os.path.join(dirPath, item)):
scanDirsAndFiles(os.path.join(dirPath, item))
if os.path.isfile(os.path.join(dirPath, item)):
user_name = login.getUserName()
fileName = item
fileSize = os.path.getsize(os.path.join(dirPath, item))
filePathLocal = os.path.join(dirPath, item)
filePathFTP = ""
currentLocation = "Local"
FTP_valid_time = 7
uploaded = ""
lastModified = "NOW()"
lastVerified = "NOW()"
fileType = "file"
fileCategory = "Ostalo"

data = [fileName, fileSize, filePathLocal, filePathFTP,
currentLocation, FTP_valid_time, uploaded, lastModified, lastVerified,
fileType, fileCategory]

sql.insertData.insert("files", data)




sql.py


connectionString = login.initialize()

class openConnection:
def openConnection(self):
conn = mysql.connect(host=connectionString["host"],
user=connectionString["user"], passwd=connectionString["passwd"],
db=connectionString["db"])
return conn


class getData:


def select(self, dataTable, limitOffset, count):
conn = self.openConnection.openConnection()
cursor = conn.cursor()
sql = "SELECT * FROM " + dataTable + " LIMIT " + limitOffset +","+ count
print sql
cursor.execute(sql)
result = []
for i in range(int(count)):
result.append(cursor.fetchone())

conn.close()
return result
def select(self, dataTable, limitOffset, count, filterCol, filterValue):
conn = self.openConnection()
cursor = conn.cursor()
sql = "SELECT FROM " + dataTable + " WHERE
"+filterCol+"="+filerValue+" LIMIT "+limitOffset+","+count
print sql
cursor.execute(sql)
result = []
for i in range(int(count)):
result.append(cursor.fetchone())
conn.close()

class insertData:
def insert(self, dataTable, data):
conn = self.openConnection.openConnection()
cursor = conn.cursor()
sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
file_path_local, file_path_FTP, curent_location, FTP_valid_time,
uploaded, last_modified, last_verified, file_type, file_category) VLAUES
"+data
cursor.execute(sql)
conn.Close()
 
Reply With Quote
 
Chris Mellon
Guest
Posts: n/a
 
      12-21-2007
On Dec 21, 2007 9:11 AM, SMALLp <> wrote:
> Hy! I have error something like this
>
> TypeError: unbound method insert() must be called with insertData
> instance as first argument (got str instance instead)
>
> CODE:
>
> File1.py
> sql.insertData.insert("files", data)
>
> sql.py
>
> class insertData:
> def insert(self, dataTable, data):
> conn = self.openConnection.openConnection()
> cursor = conn.cursor()
> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
> "+data
> cursor.execute(sql)
> conn.Close()
>
>
> Help and advice neaded!
> --


You are unclear on the distinction between classes and instances of
those classes. Following Python convention and naming your classes in
CapsCase (InsertData not insertData) would help. I recommend taking 2
giant steps backward and working through the python tutorial and dive
into python, then coming back to this project.
 
Reply With Quote
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      12-21-2007

> Traceback (most recent call last):
> File "/home/pofuk/MzMFIleShare/sharePanel.py", line 130, in share
> self.scanDirsAndFiles(dirPath)
> File "/home/pofuk/MzMFIleShare/sharePanel.py", line 158, in
> scanDirsAndFiles
> sql.insertData.insert("files", data)
> TypeError: unbound method insert() must be called with insertData
> instance as first argument (got str instance instead)
>
> share.py



<snip>


> def scanDirsAndFiles(self, dirPath):
> for item in os.listdir(dirPath):
> if os.path.isdir(os.path.join(dirPath, item)):
> scanDirsAndFiles(os.path.join(dirPath, item))
> if os.path.isfile(os.path.join(dirPath, item)):
> user_name = login.getUserName()
> fileName = item
> fileSize = os.path.getsize(os.path.join(dirPath, item))
> filePathLocal = os.path.join(dirPath, item)
> filePathFTP = ""
> currentLocation = "Local"
> FTP_valid_time = 7
> uploaded = ""
> lastModified = "NOW()"
> lastVerified = "NOW()"
> fileType = "file"
> fileCategory = "Ostalo"
>
> data = [fileName, fileSize, filePathLocal, filePathFTP,
> currentLocation, FTP_valid_time, uploaded, lastModified, lastVerified,
> fileType, fileCategory]
>
> sql.insertData.insert("files", data)
>



<snip>


> class insertData:
> def insert(self, dataTable, data):
> conn = self.openConnection.openConnection()
> cursor = conn.cursor()
> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
> "+data
> cursor.execute(sql)
> conn.Close()


It doesn't look like you are instantiating the insertData class. You
would need to do something like:

# untested
foo = insertData()
foo.insert("files", data)


But I agree with Chris. You really do need to go through a tutorial on
using classes and following Python naming conventions. Dive Into
Python and some of the other online resources are very helpful.

This is something that I have trouble with myself since wxPython uses
CamelCase for classes and methods/functions and and most
recommendations for plain Python seem to only want CamelCase for
classes and something like myFunct or myMethod for the other objects.

Mike
 
Reply With Quote
 
SMALLp
Guest
Posts: n/a
 
      12-21-2007
wrote:
>> Traceback (most recent call last):
>> File "/home/pofuk/MzMFIleShare/sharePanel.py", line 130, in share
>> self.scanDirsAndFiles(dirPath)
>> File "/home/pofuk/MzMFIleShare/sharePanel.py", line 158, in
>> scanDirsAndFiles
>> sql.insertData.insert("files", data)
>> TypeError: unbound method insert() must be called with insertData
>> instance as first argument (got str instance instead)
>>
>> share.py

>
>
> <snip>
>
>
>> def scanDirsAndFiles(self, dirPath):
>> for item in os.listdir(dirPath):
>> if os.path.isdir(os.path.join(dirPath, item)):
>> scanDirsAndFiles(os.path.join(dirPath, item))
>> if os.path.isfile(os.path.join(dirPath, item)):
>> user_name = login.getUserName()
>> fileName = item
>> fileSize = os.path.getsize(os.path.join(dirPath, item))
>> filePathLocal = os.path.join(dirPath, item)
>> filePathFTP = ""
>> currentLocation = "Local"
>> FTP_valid_time = 7
>> uploaded = ""
>> lastModified = "NOW()"
>> lastVerified = "NOW()"
>> fileType = "file"
>> fileCategory = "Ostalo"
>>
>> data = [fileName, fileSize, filePathLocal, filePathFTP,
>> currentLocation, FTP_valid_time, uploaded, lastModified, lastVerified,
>> fileType, fileCategory]
>>
>> sql.insertData.insert("files", data)
>>

>
>
> <snip>
>
>
>> class insertData:
>> def insert(self, dataTable, data):
>> conn = self.openConnection.openConnection()
>> cursor = conn.cursor()
>> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
>> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
>> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
>> "+data
>> cursor.execute(sql)
>> conn.Close()

>
> It doesn't look like you are instantiating the insertData class. You
> would need to do something like:
>
> # untested
> foo = insertData()
> foo.insert("files", data)
>
>
> But I agree with Chris. You really do need to go through a tutorial on
> using classes and following Python naming conventions. Dive Into
> Python and some of the other online resources are very helpful.
>
> This is something that I have trouble with myself since wxPython uses
> CamelCase for classes and methods/functions and and most
> recommendations for plain Python seem to only want CamelCase for
> classes and something like myFunct or myMethod for the other objects.
>
> Mike

Thanks! I solved the problem. And I thing i understand now.
 
Reply With Quote
 
Carsten Haese
Guest
Posts: n/a
 
      12-21-2007
On Fri, 2007-12-21 at 18:06 +0100, SMALLp wrote:
> >> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
> >> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
> >> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
> >> "+data
> >> cursor.execute(sql)


> Thanks! I solved the problem. And I thing i understand now.


You may have solved your initial problem, but the above snippet raises
two red flags:

1) Why is the table name coming from a variable? This implies to me that
you a working with a collection of tables with different names that all
have the same column names. If that is the case, that smells of really
bad database design. If at all possible, those tables should be merged
into one table that has an additional column (or set of columns) for
distinguishing which "fragment" each row is in.

2) Sticking literal values into an SQL query string is a bad idea. You
should learn about parametrized queries, e.g. here:
http://informixdb.blogspot.com/2007/...in-blanks.html

Hope this helps,

--
Carsten Haese
http://informixdb.sourceforge.net


 
Reply With Quote
 
SMALLp
Guest
Posts: n/a
 
      12-21-2007
Carsten Haese wrote:
> On Fri, 2007-12-21 at 18:06 +0100, SMALLp wrote:
>>>> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
>>>> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
>>>> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
>>>> "+data
>>>> cursor.execute(sql)

>
>> Thanks! I solved the problem. And I thing i understand now.

>
> You may have solved your initial problem, but the above snippet raises
> two red flags:
>
> 1) Why is the table name coming from a variable? This implies to me that
> you a working with a collection of tables with different names that all
> have the same column names. If that is the case, that smells of really
> bad database design. If at all possible, those tables should be merged
> into one table that has an additional column (or set of columns) for
> distinguishing which "fragment" each row is in.
>
> 2) Sticking literal values into an SQL query string is a bad idea. You
> should learn about parametrized queries, e.g. here:
> http://informixdb.blogspot.com/2007/...in-blanks.html
>
> Hope this helps,
>

Good question. I'm using only one tale and have no idea why i had table
name from variable. But every new knowledge comes handy.

One more question. How does my code looks like. I couldn't find any open
source program written in python to learn from, so i read some tutorials
and I'm not sure about how it looks.
 
Reply With Quote
 
kyosohma@gmail.com
Guest
Posts: n/a
 
      12-21-2007
On Dec 21, 1:44 pm, SMALLp <po...@email.t-com.hr> wrote:
> Carsten Haese wrote:
> > On Fri, 2007-12-21 at 18:06 +0100, SMALLp wrote:
> >>>> sql ="INSERT INTO "+dataTable+" (user_name, file_name, file_size,
> >>>> file_path_local, file_path_FTP, curent_location, FTP_valid_time,
> >>>> uploaded, last_modified, last_verified, file_type, file_category) VLAUES
> >>>> "+data
> >>>> cursor.execute(sql)

>
> >> Thanks! I solved the problem. And I thing i understand now.

>
> > You may have solved your initial problem, but the above snippet raises
> > two red flags:

>
> > 1) Why is the table name coming from a variable? This implies to me that
> > you a working with a collection of tables with different names that all
> > have the same column names. If that is the case, that smells of really
> > bad database design. If at all possible, those tables should be merged
> > into one table that has an additional column (or set of columns) for
> > distinguishing which "fragment" each row is in.

>
> > 2) Sticking literal values into an SQL query string is a bad idea. You
> > should learn about parametrized queries, e.g. here:
> >http://informixdb.blogspot.com/2007/...in-blanks.html

>
> > Hope this helps,

>
> Good question. I'm using only one tale and have no idea why i had table
> name from variable. But every new knowledge comes handy.
>
> One more question. How does my code looks like. I couldn't find any open
> source program written in python to learn from, so i read some tutorials
> and I'm not sure about how it looks.


You couldn't find any programs written in Python? What the!?

Here's a few:

http://cheeseshop.python.org/pypi/UliPad/3.6/
http://spambayes.sourceforge.net/
http://sourceforge.net/softwaremap/t...p?form_cat=178

Mike
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-21-2007
a écrit :
> On Dec 21, 9:11 am, SMALLp <po...@email.t-com.hr> wrote:
>

(snip)
>>class insertData:
>> def insert(self, dataTable, data):

(snip)
>
> I think you need to post the real traceback or the real code since
> your error message doesn't look like it has anything to do with the
> code above. At least, I do not see a method named "insert".


May I suggest a new pair of glasses ?-)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner's Beginner william nelson Ruby 7 04-11-2011 11:23 PM
Beginner can't get a wireless VPN connect? Russell DeMarco Wireless Networking 2 03-18-2005 10:35 PM
Beginner can't connect Laptop to wireless network Russell DeMarco Wireless Networking 2 03-17-2005 03:31 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57