六月 28, 2011
» building 'sqlalchemy.cprocessors' extension failed on Windows

在 Windows 上 pip install SQLAlchemy 出現 

Traceback (most recent call last):

  File “<string>”, line 1, in <module>

  File “.\build\SQLAlchemy\setup.py”, line 287, in <module>

    “Retrying the build without the C extension now.”

  File “.\build\SQLAlchemy\setup.py”, line 90, in status_msgs

    print(msg)

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-6: ordinal not in range(128)

的繞道方式:

用編輯器打開 build\SQLAlchemy\setup.py

把這四行註解起來:

    # Extension(‘sqlalchemy.cprocessors’,

    #       sources=[‘lib/sqlalchemy/cextension/processors.c’]),

    # Extension(‘sqlalchemy.cresultproxy’,

    #       sources=[‘lib/sqlalchemy/cextension/resultproxy.c’])

再試著裝一次

pip install SQLAlchemy

即可能解決。

這樣不編 C extension 會影響 SQLAlchemy 在 Windows 上的效能,但我想通常不會認真要用這個 Windows 當作正式發佈的平台。不幸如此?保佑了。

6/29 更新: 

編不起來可能是環境變數 Path 出了問題,可以檢查一下有沒有被什麼套件(類似 Zenxxx Server 之類) 亂加了雙引號。

» building 'sqlalchemy.cprocessors' extension failed on Windows

在 Windows 上 pip install SQLAlchemy 出現 

Traceback (most recent call last):

  File "<string>", line 1, in <module>

  File ".\build\SQLAlchemy\setup.py", line 287, in <module>

    “Retrying the build without the C extension now."

  File ".\build\SQLAlchemy\setup.py", line 90, in status_msgs

    print(msg)

UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-6: ordinal not in range(128)

的繞道方式:

用編輯器打開 build\SQLAlchemy\setup.py

把這四行註解起來:

    # Extension(‘sqlalchemy.cprocessors’,

    #       sources=[‘lib/sqlalchemy/cextension/processors.c’]),

    # Extension(‘sqlalchemy.cresultproxy’,

    #       sources=[‘lib/sqlalchemy/cextension/resultproxy.c’])

再試著裝一次

pip install SQLAlchemy

即可能解決。

這樣不編 C extension 會影響 SQLAlchemy 在 Windows 上的效能,但我想通常不會認真要用這個 Windows 當作正式發佈的平台。不幸如此?保佑了。

6/29 更新: 

編不起來可能是環境變數 Path 出了問題,可以檢查一下有沒有被什麼套件(類似 Zenxxx Server 之類) 亂加了雙引號。

六月 7, 2008
» Pylons 0.9.7 內建支援 SQLAchemy 組態設定

在 Pylons 0.9.7 中已內建支援自動產生對 SQLAchemy 的組態設定,情況大概是這樣:

(virpylons)[k]@XO % PYTHONPATH='' paster create -t pylons sqlarmy
Selected and implied templates:
Pylons#pylons Pylons application template

Variables:
egg: sqlarmy
package: sqlarmy
project: sqlarmy
Enter sqlalchemy (True/False: Include SQLAlchemy 0.4 configuration) [False]: _

嗯,好不容易。其自動化的部分大概如 Working with databases and SQLAlchemy 這一段所示,這是目前在 Pylons 0.9.6 所需要DIY的部份。

附註,因為我設了自己的 PYTHONPATH,如果沒把它清空 (PYTHONPATH=''),跑 paster 指令時裝的 pylons 會在我個人預設的套件位置找到 0.9.6 版,而非我用 go-pylons.py 產生 virtualenv 中的 0.9.7 版,讓我困擾了一小下,怎麼沒有 sqlalchemy 的化學反應?XD

二月 19, 2008
» 用 Dia 生 SqlAchemy 的雛形

讀了 Thinker: Dia 除了畫圖 一文,我也在(上一個)case裡用Dia來畫資料庫的草圖。而那時的資料庫是用Sqlalchemy,因此我改寫了Thinker的版本,以輸出Sqlalchemy的草圖為目標。但是,或因我程式碼寫的不好,或因Sqlalchemy的架構需要留心,生出來的程式碼你得知道在幹麻才能用,好吧,廢言。程式如下:


# This is a dia plug-in, you can put it in $HOME/.dia/python directory to
# use Export as SQLAlchemy create_all prototype.
import dia

class Table(object):
def __init__(self, name):
super(Table, self).__init__()
self.name = name
self.attrs = []

def add_attr(self, name, type):
self.attrs.append((name, type))

def expend_type(self, att_types):
types_arg = types_kwarg = ''
att_types = att_types.split(' ')
for t_ori in att_types:
t = t_ori.lower()
print t
if t == 'int':
types_arg += ', types.Integer'
elif t == 'text':
types_arg += ', types.Unicode'
elif t == 'unique':
types_arg += ', unique=True'
elif t == 'primarykey':
types_kwarg += ', primary_key=True'
elif t == 'notnull':
types_kwarg += ', nullable=False'
else:
types_arg += ', ' + t_ori # return the original attr value
types_arg += types_kwarg # add keywords attributes in the end
return types_arg

def __str__(self):
table_name = self.name.lower()
attr_strs = [ "Column('%s'%s)" % (name, self.expend_type(type))
for name, type in self.attrs ]
return "%s = Table( '%s', meta,\n %s\n)" % (
table_name, table_name, ',\n '.join(attr_strs))

class Klass(object):
def __init__(self, name):
super(Klass, self).__init__()
self.name = name
self.attrs = []

def add_attr(self, attrname):
self.attrs.append(attrname)

def __str__(self):
klass_name = self.name
table_name = self.name.lower()
attr_names = [ ", %s" % (name) for name in self.attrs
if name != ('id') ]
attr_strs = [ "self.%s = %s" % (name, name) for name in self.attrs
if name != ('id') ]
return "class %s(object):\n def __init__(self%s):\n %s" % (
klass_name, ''.join(attr_names),
'\n '.join(attr_strs))

class DiaSqla(object):
def __init__(self):
self.tables = []
self.klasses = []
self.cnames = []
self.filename = ''

def begin_render(self, data, filename):
self.filename = filename
first_layer = data.layers[0]
for obj in first_layer.objects:
if obj.type.name != 'UML - Class':
continue
cname = obj.properties['name'].value
attrs = obj.properties['attributes'].value
a_table = Table(cname)
a_klass = Klass(cname)
for attr in attrs:
n, v = attr[:2]
a_klass.add_attr(n)
a_table.add_attr(n, v)
self.tables.append(a_table)
self.klasses.append(a_klass)
self.cnames.append(cname)

def end_render(self):
fo = file(self.filename, 'w+')
fo.write('# Generated by DiaSqla exporter for SQLAlchemy\n\n')
for a_table in self.tables:
fo.write(str(a_table) + '\n\n')
for a_klass in self.klasses:
fo.write(str(a_klass) + '\n\n')
for cname in self.cnames:
fo.write('mapper('+ cname +', '+ cname.lower() +')\n')
fo.close()

dia.register_export('PyDia SQLAlchemy', 'sqla', DiaSqla())
把像這樣的圖:

輸出為:

# Generated by DiaSqla exporter for SQLAlchemy

model = Table( 'model', meta,
Column('id', types.Integer, primary_key=True),
Column('name', types.Unicode, unique=True, nullable=False),
Column('category_id', types.Integer, ForeignKey('category.id')),
Column('price', types.Integer)
)

stock = Table( 'stock', meta,
Column('model_id', types.Integer, ForeignKey('model.id'), primary_key=True),
Column('location_id', types.Integer, ForeignKey('node.id'), primary_key=True),
Column('available', types.Integer),
Column('reserved', types.Integer)
)

node = Table( 'node', meta,
Column('id', types.Integer, primary_key=True),
Column('name', types.Unicode, unique=True, nullable=False),
Column('address', types.Unicode),
Column('phone', types.Unicode),
Column('email', types.Unicode)
)

category = Table( 'category', meta,
Column('id', types.Integer, primary_key=True),
Column('parent_id', types.Integer),
Column('name', types.Unicode, unique=True, nullable=False)
)

class Model(object):
def __init__(self, name, category_id, price):
self.name = name
self.category_id = category_id
self.price = price

class Stock(object):
def __init__(self, model_id, location_id, available, reserved):
self.model_id = model_id
self.location_id = location_id
self.available = available
self.reserved = reserved

class Node(object):
def __init__(self, name, address, phone, email):
self.name = name
self.address = address
self.phone = phone
self.email = email

class Category(object):
def __init__(self, parent_id, name):
self.parent_id = parent_id
self.name = name

mapper(Model, model)
mapper(Stock, stock)
mapper(Node, node)
mapper(Category, category)
PS: 程式碼在這裡的CSS中,只有難看而已,如有意請貼到您最愛的編輯器上品賞(最好別是剛吃完飯,我可提醒了)。

biggo.com.tw

A Django site.