Start New Discussion within our Software Development Community

I am learning to create a database with python. I am using this line of code to populate my table T.insert(2, 'Jones', 'Jack', 15, False, 0.0)

And the table is created as follow:
T = SDBTable('id=ui', 'last', 'first', 'age=i', 'married=b', 'salary=f')

I have a class SDBTable and in it there are several methods I have to implement. The first one being def __init__(self, *fields):
in which I should create a table with parameters defining valid fields/columns in
the table. There is at least one parameter. (Field should specifiers have a name and an option attribute)

I have been searching the web for example of defining the fields for a table in Python but i cannot find any. If anyone can provide help I would really appreciate it.

I tried something like this in defining my fields. Is this at all correct?
self.uid=id
self.last=last
self.first=first
self.age=age
self.married=married
self.salary=salary

there is a test for this method written as:
for f in fields:
# test the format and determine name, unique, and type
# ...
self.__fields.append('field_name')
self.__specs.append((True, str))
which I am suppose to test if the field is unique, the name of it, and what type it is.

Please help me understand. I am a beginner to learning Python.

Thank you

Are you asking about passing keyword/argument lists to functions/classes?

Here's a very basic example to illustrate, let me know if this is what you're asking:

>>> class VarKeyArg(object):
...     def __init__(self, *args, **kwargs):
...         self.key1 = kwargs.get('key1')
...         self.key2 = kwargs.get('key2')
...         self.extra = args[:]
...     
>>> c1 = VarKeyArg()
>>> c1.extra
()
>>> c1.key1
>>> c2 = VarKeyArg(1,2,3,key1='First',key2='Second')
>>> c2.extra
(1, 2, 3)
>>> c2.key1
'First'
>>> c2.key2
'Second'
>>>
This article has been dead for over six months. Start a new discussion instead.