使类属性不区分大小写? [英] Making class attributes non-case-sensitive?

查看:83
本文介绍了使类属性不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我在一个应用程序中工作(制作了很多包装器),但

应用程序不区分大小写。例如,键入obj.name,

obj.Name,甚至object.naMe都可以(只要应用程序是关于
)。问题是,如果有人输错,他们可能会因意外调用原始属性

而不是包装版本而得到一个意外错误。有没有人有这个简单的解决方案?


我可以通过制作''别名'来防范某些情况:

class AClass(object):

def name(self):

print" hello"


Name =名字


....但这并不能防止打字错误,它会变得更加复杂

多字属性名称,它让我的由于所有的拼写版本都显示出来(我担心我的

文档很清楚,但没有停止与拼写错误相关的错误),因此epydocs会因为读取而错误地阅读



我想过使用我的包装'__getattr__和__setattr __,但是我

我担心每个委托属性调用的开销

运行搜索并比较(< paramName> .lower()基于比较?)。


任何想法或优先顺序?

干杯,

- Rafe

Hi,

I''m working within an application (making a lot of wrappers), but the
application is not case sensitive. For example, Typing obj.name,
obj.Name, or even object.naMe is all fine (as far as the app is
concerned). The problem is, If someone makes a typo, they may get an
unexpected error due accidentally calling the original attribute
instead of the wrapped version. Does anyone have a simple solution for
this?

I can protect against some cases just by making an ''alias'':
class AClass(object):
def name(self):
print "hello"

Name = name

....but this doesn''t protect against typos, it gets more complicated
with multi-word attribute names, and it makes my epydocs confusing to
read since all spelling versions are shown (I AM concerned about my
docs being clear, but not as much as stopping typo related errors).

I thought about using my wrapper''s __getattr__ and __setattr__, but I
I am concerned about the overhead of every delegated attribute call
running a search and compare (<paramName>.lower() based compare?).

Any ideas or precedence?
Cheers,

- Rafe

推荐答案

Rafe写道:
Rafe wrote:




我在一个应用程序中工作(制作了很多包装),但是

应用程序不区分大小写。例如,键入obj.name,

obj.Name,甚至object.naMe都可以(只要应用程序是关于
)。问题是,如果有人输错,他们可能会因意外调用原始属性

而不是包装版本而得到一个意外错误。有没有人有这个简单的解决方案?


我可以通过制作''别名'来防范某些情况:

class AClass(object):

def name(self):

print" hello"


Name =名字


...但这并不能防止打字错误,它会变得更加复杂

多字属性名称,它使我的epydocs由于所有的拼写版本都显示出来(我担心我的

文档很清楚,但没有停止与拼写错误相关的错误),因此会因此而感到困惑




我想过使用我的包装器'__getattr__和__setattr __,但是我

我担心每个委托属性调用的开销

运行搜索并比较(< paramName> .lower()基于比较?)。


任何想法或优先顺序?
Hi,

I''m working within an application (making a lot of wrappers), but the
application is not case sensitive. For example, Typing obj.name,
obj.Name, or even object.naMe is all fine (as far as the app is
concerned). The problem is, If someone makes a typo, they may get an
unexpected error due accidentally calling the original attribute
instead of the wrapped version. Does anyone have a simple solution for
this?

I can protect against some cases just by making an ''alias'':
class AClass(object):
def name(self):
print "hello"

Name = name

...but this doesn''t protect against typos, it gets more complicated
with multi-word attribute names, and it makes my epydocs confusing to
read since all spelling versions are shown (I AM concerned about my
docs being clear, but not as much as stopping typo related errors).

I thought about using my wrapper''s __getattr__ and __setattr__, but I
I am concerned about the overhead of every delegated attribute call
running a search and compare (<paramName>.lower() based compare?).

Any ideas or precedence?



想法?不要这样做......


说真的:那些代码来自哪里,谁在打字呢?如果它是python,那么
然后让人们遵循python的规则。如果它是某种类型的homebrewn

语言,你映射到python,调整映射器强制执行小写,并使所有属性小写的




Diez

Ideas? Don''t do that...

Seriously: where does that code come from, who''s typing it? If it is python,
then make people follow python''s rules. If it is some sort of homebrewn
language you map to python, adapt the mapper to enforce lower-case and make
all your properties lower case.

Diez


这样我就不会劫持自己的线程,问题是''​​如何包装

对象不区分大小写''。


我遇到这个问题的原因是什么?...应用程序的API是

通过COM访问,所以我不知道我能做什么,只能对我得到的东西做出反应。 API是在应用程序(Softimage | XSI - 一个用于高端视觉效果的3个领先3D应用程序中的一个b $ b)由

Microsoft拥有的时候编写的。我不确定它是微软的标准版还是仅仅是这个应用程序实现的方式(也许是因为用户不足以使用VBscript中的
脚本编写而不是这种情况敏感)。


XSI允许通过COM使用多种语言,甚至可以在

软件中使用(有内置的代码编辑器)。在早期,

VBScript是最常用的脚本语言,而在任何事情上,使用C ++完成了更多的硬核(当然,C实现是大概
敏感 - 据我所知。然后JScript成为最常见的
常见的,现在Python被认为是标准的。


无论如何,标准做法是使用混合大小写,所以我需要

坚持它,因为我创建的结果框架需要直观地使用(我的最终用户仍在编写代码。它是一个用于
一个API我猜...)


我不认为*我需要过多担心性能因为我是

没有做任何严肃的处理,这更多是关于约定

执行和质量控制而不是数字运算。我可能

尝试写一些由包装器执行的泛型

__getattr__和__setattr__,但我希望有一些漂亮的

解决方法,也许是装饰者的形式?再次......

任何想法?

干杯,


- Rafe

10月13日,4:15 * pm,Diez B. Roggisch, < de ... @nospam.web.dewrote:
Just so I don''t hijack my own thread, the issue is ''how to wrap an
object which is not case sensitive''.

The reason I am stuck dealing with this?... The application''s API is
accessed through COM, so I don''t know if I can do anything but react
to what I get. The API was written while the app (Softimage|XSI - one
of 3 leading 3D applications for high-end visual effects) was owned by
Microsoft. I''m not sure if it is standard for Microsoft or just the
way this app was implemented (perhaps because under-users were
scripting in VBscript which is not case sensitive).

XSI allows many languages to be used via COM, even from within the
software (there are built-in code editors). In the early days,
VBScript was the most common scripting language used while anything
more hard-core was done in C++ (of course the C implementation is case
sensitive - well as far as I know). Then JScript became the most
common, now Python is considered standard.

Anyway, the standard practice is to use mixed-case, so I need to
adhere to it as the resulting framework I am creating needs to be
intuitive to use (my end-user is still writing code. It''s an API for
an API I guess...)

I don''t *think* I need to worry too much about performance because I''m
not doing any serious processing, this is more about convention
enforcement and quality control rather than number crunching. I might
try to write something generic which gets executed by the wrappers
__getattr__ and __setattr__, but I was hoping for some nifty
workaround, maybe in the form of a decorator or something? Again...
any ideas?
Cheers,

- Rafe
On Oct 13, 4:15*pm, "Diez B. Roggisch" <de...@nospam.web.dewrote:

Rafe写道:
Rafe wrote:


Hi,


我在一个应用程序中工作(制作了很多包装器),但

应用程序并非如此敏感。例如,键入obj.name,

obj.Name,甚至object.naMe都可以(只要应用程序是关于
)。问题是,如果有人输错,他们可能会因意外调用原始属性

而不是包装版本而得到一个意外错误。有没有人有一个简单的解决方案

这个?
I''m working within an application (making a lot of wrappers), but the
application is not case sensitive. For example, Typing obj.name,
obj.Name, or even object.naMe is all fine (as far as the app is
concerned). The problem is, If someone makes a typo, they may get an
unexpected error due accidentally calling the original attribute
instead of the wrapped version. Does anyone have a simple solution for
this?


我可以通过制作''alias''来防范某些情况:

class AClass(object):

* * def name(self):

* * * * print" hello"
I can protect against some cases just by making an ''alias'':
class AClass(object):
* * def name(self):
* * * * print "hello"


* * Name = name
* * Name = name


...但是这个没有防止打字错误,它会变得更加复杂

多字属性名称,这使得我的epydocs因为所有拼写版本都显示而难以读取
我担心我的

文档很清楚,但没有停止与错误相关的错误)。
...but this doesn''t protect against typos, it gets more complicated
with multi-word attribute names, and it makes my epydocs confusing to
read since all spelling versions are shown (I AM concerned about my
docs being clear, but not as much as stopping typo related errors).


我想过使用我的包装''__getattr__和__setattr__,但是我

我担心开销的问题每个委托属性调用

运行搜索并比较(< paramName> .lower()基于比较?)。
I thought about using my wrapper''s __getattr__ and __setattr__, but I
I am concerned about the overhead of every delegated attribute call
running a search and compare (<paramName>.lower() based compare?).


任何想法或优先顺序?
Any ideas or precedence?



想法?不要这样做......


说真的:那些代码来自哪里,谁在打字呢?如果它是python,那么
然后让人们遵循python的规则。如果它是某种类型的homebrewn

语言,你映射到python,调整映射器强制执行小写,并使所有属性小写的




Diez


Ideas? Don''t do that...

Seriously: where does that code come from, who''s typing it? If it is python,
then make people follow python''s rules. If it is some sort of homebrewn
language you map to python, adapt the mapper to enforce lower-case and make
all your properties lower case.

Diez


Rafe写道:
Rafe wrote:

就这样我不喜欢劫持我自己的线程,问题是''​​如何包装一个不区分大小写的

对象'。


我之所以如此处理这个问题?...应用程序的API是通过COM访问的
,所以我不知道我是否可以做任何事情,但是反应

到我得到了什么。 API是在应用程序(Softimage | XSI - 一个用于高端视觉效果的3个领先3D应用程序中的一个b $ b)由

Microsoft拥有的时候编写的。我不确定它是微软的标准版还是仅仅是这个应用程序实现的方式(也许是因为用户不足以使用VBscript中的
脚本编写而不是这种情况敏感)。


XSI允许通过COM使用多种语言,甚至可以在

软件中使用(有内置的代码编辑器)。在早期,

VBScript是最常用的脚本语言,而在任何事情上,使用C ++完成了更多的硬核(当然,C实现是大概
敏感 - 据我所知。然后JScript成为最常见的
常见的,现在Python被认为是标准的。


无论如何,标准做法是使用混合大小写,所以我需要

坚持它,因为我创建的结果框架需要直观地使用(我的最终用户仍在编写代码。它是一个用于
一个API我猜...)


我不认为*我需要过多担心性能因为我是

没有做任何严肃的处理,这更多是关于约定

执行和质量控制而不是数字运算。我可能

尝试写一些由包装器执行的泛型

__getattr__和__setattr__,但我希望有一些漂亮的

解决方法,也许是装饰者的形式?再次......

任何想法?
Just so I don''t hijack my own thread, the issue is ''how to wrap an
object which is not case sensitive''.

The reason I am stuck dealing with this?... The application''s API is
accessed through COM, so I don''t know if I can do anything but react
to what I get. The API was written while the app (Softimage|XSI - one
of 3 leading 3D applications for high-end visual effects) was owned by
Microsoft. I''m not sure if it is standard for Microsoft or just the
way this app was implemented (perhaps because under-users were
scripting in VBscript which is not case sensitive).

XSI allows many languages to be used via COM, even from within the
software (there are built-in code editors). In the early days,
VBScript was the most common scripting language used while anything
more hard-core was done in C++ (of course the C implementation is case
sensitive - well as far as I know). Then JScript became the most
common, now Python is considered standard.

Anyway, the standard practice is to use mixed-case, so I need to
adhere to it as the resulting framework I am creating needs to be
intuitive to use (my end-user is still writing code. It''s an API for
an API I guess...)

I don''t *think* I need to worry too much about performance because I''m
not doing any serious processing, this is more about convention
enforcement and quality control rather than number crunching. I might
try to write something generic which gets executed by the wrappers
__getattr__ and __setattr__, but I was hoping for some nifty
workaround, maybe in the form of a decorator or something? Again...
any ideas?



我仍​​然没有全面了解 - 无论如何,我推断的是:


- 为你的自己的缘故,你想坚持适当的命名,无论如何

意味着


- 你无法控制将要调用的东西。


然后我会使用__getattr __-方式,加上可能是一个元类(或

后处理),它将所有属性映射到小写变体,

这样查找效率很高。


这有帮助吗?


Diez

I still don''t get the full picture - anyway, what I infer is this:

- for your own sake, you want to stick with "proper" naming, whatever that
means

- you can''t control what will be called.

Then I''d go with the __getattr__-way, plus maybe a meta-class (or
postprocessing) that maps all attributes to a lower-case-variant as well,
so that the lookup is efficient.

Does that help?

Diez


这篇关于使类属性不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆