FAQ
Hi

I was wondering whether anyone could help me, I'm pretty new to python
coming from a PHP background and I'm having a few products in getting my
head round how to write the factory pattern within python.

I'm currently looking to try to return values from a db and load up the
relevant objects, values returned are product type (I,S) and product code
(123).

At the moment I've adapted some code I've found illustrating the factory
method but ideally I would like to use the type to load up the relevant
object.

Another issue I've found is that I don't seem to be able to access to the
price attribute of each of the object. I'm sure these are very
straightforward issues however I seem to have tied myself in knots over this
and could do with a fresh set of 'pythonic' eyes to help me out.

registry = {}

class MetaBase(type):
def __init__(cls, name, bases, dict):
registry[name] = cls

class Product(object):
__metaclass__ = MetaBase

class Item(Product):
def __init__(self, *args, **kw):
self.price = 1

class Set(Product):
def __init__(self, *args, **kw):
self.price = 2

def factory(kind, *args, **kw):
return registry[kind](*args, **kw)


item = registry['Item']
print item.price

--
View this message in context: http://www.nabble.com/Factory-pattern-again-tf4156186.html#a11825158
Sent from the Python - python-list mailing list archive at Nabble.com.

Search Discussions

  • Bruno Desthuilliers at Jul 27, 2007 at 10:00 am

    Mike Howarth a ?crit :
    Hi

    I was wondering whether anyone could help me, I'm pretty new to python
    coming from a PHP background and I'm having a few products in getting my
    head round how to write the factory pattern within python.

    I'm currently looking to try to return values from a db and load up the
    relevant objects, values returned are product type (I,S) and product code
    (123).

    At the moment I've adapted some code I've found illustrating the factory
    method but ideally I would like to use the type to load up the relevant
    object.

    Another issue I've found is that I don't seem to be able to access to the
    price attribute of each of the object. I'm sure these are very
    straightforward issues however I seem to have tied myself in knots over this
    and could do with a fresh set of 'pythonic' eyes to help me out.

    registry = {}

    class MetaBase(type):
    def __init__(cls, name, bases, dict):
    registry[name] = cls

    class Product(object):
    __metaclass__ = MetaBase

    class Item(Product):
    def __init__(self, *args, **kw):
    self.price = 1

    class Set(Product):
    def __init__(self, *args, **kw):
    self.price = 2

    def factory(kind, *args, **kw):
    return registry[kind](*args, **kw)


    item = registry['Item']
    This returns the Item *class*, not an instance of... So the following:
    print item.price
    cannot work, since price is an instance attribute, not a class attribute.

    What you want is:

    item = factory('Item')
    print item.price

    HTH
  • Mike Howarth at Jul 27, 2007 at 12:54 pm
    Thanks that makes absolute sense.

    I was sure it was something simple, thanks for your time.


    Bruno Desthuilliers-5 wrote:
    Mike Howarth a ?crit :
    Hi

    I was wondering whether anyone could help me, I'm pretty new to python
    coming from a PHP background and I'm having a few products in getting my
    head round how to write the factory pattern within python.

    I'm currently looking to try to return values from a db and load up the
    relevant objects, values returned are product type (I,S) and product code
    (123).

    At the moment I've adapted some code I've found illustrating the factory
    method but ideally I would like to use the type to load up the relevant
    object.

    Another issue I've found is that I don't seem to be able to access to the
    price attribute of each of the object. I'm sure these are very
    straightforward issues however I seem to have tied myself in knots over
    this
    and could do with a fresh set of 'pythonic' eyes to help me out.

    registry = {}

    class MetaBase(type):
    def __init__(cls, name, bases, dict):
    registry[name] = cls

    class Product(object):
    __metaclass__ = MetaBase

    class Item(Product):
    def __init__(self, *args, **kw):
    self.price = 1

    class Set(Product):
    def __init__(self, *args, **kw):
    self.price = 2

    def factory(kind, *args, **kw):
    return registry[kind](*args, **kw)


    item = registry['Item']
    This returns the Item *class*, not an instance of... So the following:
    print item.price
    cannot work, since price is an instance attribute, not a class attribute.

    What you want is:

    item = factory('Item')
    print item.price

    HTH
    --
    http://mail.python.org/mailman/listinfo/python-list
    --
    View this message in context: http://www.nabble.com/Factory-pattern-again-tf4156186.html#a11828597
    Sent from the Python - python-list mailing list archive at Nabble.com.
  • Mike Howarth at Jul 27, 2007 at 3:26 pm
    Having overcome my first hurdle with the factory pattern, I've now hit
    another stumbling block

    At the moment I'm trying to return around 2000 records from a db and load up
    the relevant product object, what I've found is this is running extremely
    slowly (> 20mins), the db is normalized and indexes exist.

    Can anyone advise on how best I could speed this up?

    def getWebRangeProducts(self):

    if self.c.connected:

    cu = self.c.cursor #create the cursor

    sql = "SELECT type, code FROM products"

    cu.execute(sql)

    if cu.rowcount > 0:

    rows = cu.fetchall()

    for row in rows:

    self.loadProduct(row[0].strip(),row[1].strip())


    return self.products


    def loadProduct(self,type,product_code):

    print type + ":" + product_code

    if type == 'I':

    try:
    product = factory('Item', product_code)
    self.products.append(product)
    except:
    print 'problem occured:' + type + ':' + product_code

    elif type == 'S':

    try:
    item_set = factory('Set', product_code)
    item_set.getItems()
    self.products.extend(item_set.getItems())
    except:
    print 'problem occured:' + type + ':' + product_code

    else:
    pass







    Bruno Desthuilliers-5 wrote:
    Mike Howarth a ?crit :
    Hi

    I was wondering whether anyone could help me, I'm pretty new to python
    coming from a PHP background and I'm having a few products in getting my
    head round how to write the factory pattern within python.

    I'm currently looking to try to return values from a db and load up the
    relevant objects, values returned are product type (I,S) and product code
    (123).

    At the moment I've adapted some code I've found illustrating the factory
    method but ideally I would like to use the type to load up the relevant
    object.

    Another issue I've found is that I don't seem to be able to access to the
    price attribute of each of the object. I'm sure these are very
    straightforward issues however I seem to have tied myself in knots over
    this
    and could do with a fresh set of 'pythonic' eyes to help me out.

    registry = {}

    class MetaBase(type):
    def __init__(cls, name, bases, dict):
    registry[name] = cls

    class Product(object):
    __metaclass__ = MetaBase

    class Item(Product):
    def __init__(self, *args, **kw):
    self.price = 1

    class Set(Product):
    def __init__(self, *args, **kw):
    self.price = 2

    def factory(kind, *args, **kw):
    return registry[kind](*args, **kw)


    item = registry['Item']
    This returns the Item *class*, not an instance of... So the following:
    print item.price
    cannot work, since price is an instance attribute, not a class attribute.

    What you want is:

    item = factory('Item')
    print item.price

    HTH
    --
    http://mail.python.org/mailman/listinfo/python-list
    --
    View this message in context: http://www.nabble.com/Factory-pattern-again-tf4156186.html#a11831500
    Sent from the Python - python-list mailing list archive at Nabble.com.
  • Steve Holden at Jul 27, 2007 at 5:00 pm

    Mike Howarth wrote:
    Having overcome my first hurdle with the factory pattern, I've now hit
    another stumbling block

    At the moment I'm trying to return around 2000 records from a db and load up
    the relevant product object, what I've found is this is running extremely
    slowly (> 20mins), the db is normalized and indexes exist.

    Can anyone advise on how best I could speed this up?

    def getWebRangeProducts(self):
    if self.c.connected:
    cu = self.c.cursor #create the cursor
    sql = "SELECT type, code FROM products"
    cu.execute(sql)
    if cu.rowcount > 0:
    rows = cu.fetchall()
    for row in rows:
    self.loadProduct(row[0].strip(),row[1].strip())
    return self.products

    def loadProduct(self,type,product_code):
    print type + ":" + product_code
    if type == 'I':
    try:
    product = factory('Item', product_code)
    self.products.append(product)
    except:
    print 'problem occured:' + type + ':' + product_code
    elif type == 'S':
    try:
    item_set = factory('Set', product_code)
    item_set.getItems()
    self.products.extend(item_set.getItems())
    except:
    print 'problem occured:' + type + ':' + product_code
    else:
    pass
    There's definitely *something* wrong - this shouldn't take 20 seconds,
    let alone 20 minutes.

    How frequently do you see the

    print type + ":" + product_code

    statement producing output? In other words, is the slowness distributed
    across the task, or is there a delay at the beginning or end? Are the
    Set and Item classes exactly as shown in your last post?

    regards
    Steve

    PS: Please put your answer at the bottom, not the top!
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------
  • Cunheise at Jul 27, 2007 at 6:04 pm
    first you need find the bottleneck of your script db or function
    if the bottleneck is db
    1. which db do you use do you optimize the db from read
    2. the sql you write do not use any index "maybe select code, type
    from products where type = 'I' or type = 'S' will help" and you need
    create index on type field maybe you need set limit 2000 to sql depend
    on your requirement
    On Jul 27, 11:26 pm, Mike Howarth wrote:
    Having overcome my first hurdle with the factory pattern, I've now hit
    another stumbling block

    At the moment I'm trying to return around 2000 records from a db and load up
    the relevant product object, what I've found is this is running extremely
    slowly (> 20mins), the db is normalized and indexes exist.

    Can anyone advise on how best I could speed this up?

    def getWebRangeProducts(self):

    if self.c.connected:

    cu = self.c.cursor #create the cursor

    sql = "SELECT type, code FROM products"

    cu.execute(sql)

    if cu.rowcount > 0:

    rows = cu.fetchall()

    for row in rows:

    self.loadProduct(row[0].strip(),row[1].strip())

    return self.products

    def loadProduct(self,type,product_code):

    print type + ":" + product_code

    if type == 'I':

    try:
    product = factory('Item', product_code)
    self.products.append(product)
    except:
    print 'problem occured:' + type + ':' + product_code

    elif type == 'S':

    try:
    item_set = factory('Set', product_code)
    item_set.getItems()
    self.products.extend(item_set.getItems())
    except:
    print 'problem occured:' + type + ':' + product_code

    else:
    pass



    Bruno Desthuilliers-5 wrote:
    Mike Howarth a ?crit :
    Hi
    I was wondering whether anyone could help me, I'm pretty new to python
    coming from a PHP background and I'm having a few products in getting my
    head round how to write the factory pattern within python.
    I'm currently looking to try to return values from a db and load up the
    relevant objects, values returned are product type (I,S) and product code
    (123).
    At the moment I've adapted some code I've found illustrating the factory
    method but ideally I would like to use the type to load up the relevant
    object.
    Another issue I've found is that I don't seem to be able to access to the
    price attribute of each of the object. I'm sure these are very
    straightforward issues however I seem to have tied myself in knots over
    this
    and could do with a fresh set of 'pythonic' eyes to help me out.
    registry = {}
    class MetaBase(type):
    def __init__(cls, name, bases, dict):
    registry[name] = cls
    class Product(object):
    __metaclass__ = MetaBase
    class Item(Product):
    def __init__(self, *args, **kw):
    self.price = 1
    class Set(Product):
    def __init__(self, *args, **kw):
    self.price = 2
    def factory(kind, *args, **kw):
    return registry[kind](*args, **kw)
    item = registry['Item']
    This returns the Item *class*, not an instance of... So the following:
    print item.price
    cannot work, since price is an instance attribute, not a class attribute.
    What you want is:
    item = factory('Item')
    print item.price
    HTH
    --
    http://mail.python.org/mailman/listinfo/python-list
    --
    View this message in context:http://www.nabble.com/Factory-pattern-again-tf4156186.html#a11831500
    Sent from the Python - python-list mailing list archive at Nabble.com.
  • Steve Holden at Jul 28, 2007 at 12:26 am

    cunheise at yahoo.com.cn wrote:
    first you need find the bottleneck of your script db or function
    if the bottleneck is db
    1. which db do you use do you optimize the db from read
    2. the sql you write do not use any index "maybe select code, type
    from products where type = 'I' or type = 'S' will help" and you need
    create index on type field maybe you need set limit 2000 to sql depend
    on your requirement
    Whether a relational database is indexed or not the SQL to retrieve
    information from it will be exactly the same - the whole point if
    indexing is that it represents a change to the physical schema that
    improves retrieval efficiency without altering the logical schema.

    Mike stated quite clearly "the db is normalized and indexes exist".

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC/Ltd http://www.holdenweb.com
    Skype: holdenweb http://del.icio.us/steve.holden
    --------------- Asciimercial ------------------
    Get on the web: Blog, lens and tag the Internet
    Many services currently offer free registration
    ----------- Thank You for Reading -------------

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJul 27, '07 at 8:42a
activeJul 28, '07 at 12:26a
posts7
users4
websitepython.org

People

Translate

site design / logo © 2022 Grokbase