说,我有一个整数PK的客户.在我的网络应用中,我希望为他们提供个人资料页面.我不想要像/ profile/10375 /这样的网址(例如,我不希望别人知道我有多少客户).我也不想要像/ profile/acme_corp /这样的slug基本网址.
将唯一整数转换为唯一随机短字符串的好方法是什么?(例如,早期的Reddit曾经有过这种类型的url,但它是从十进制到基数36的转换,跳过一些id).但这对我来说也没用,因为很容易用这个方案猜测数据库中的实体数量.
我不能使用UUID等,因为它们会使网址过大.
def foo(**args):
for k, v in args.items():
print type(k), type(v)
for k, v in args.items():
k = v
print k
print type(k)
foo(a = 10)
foo(**{'a':10})
Run Code Online (Sandbox Code Playgroud)
给我
<type 'str'> <type 'int'>
10
<type 'int'>
<type 'str'> <type 'int'>
10
<type 'int'>
Run Code Online (Sandbox Code Playgroud)
所以我很困惑,我怎么能这样做,因为k是一个字符串,所以我不能分配给它?
我显然做不到
In [35]: 'a' = 10
------------------------------------------------------------
File "<ipython console>", line 1
SyntaxError: can't assign to literal (<ipython console>, line 1)
Run Code Online (Sandbox Code Playgroud) 许多框架将其配置文件保留为与程序其余部分不同的语言.例如,Appengine以yaml格式保持配置.比较,DJango settings.py是一个python模块.我可以看到很多不利之处.
如果它与程序的其他部分使用相同的语言,我可以
在配置文件中做有趣的事情.
MEDIA_DIR = os.path.join(os.path.dir(__file__), 'media')
#Or whaever the correct cals are, you get the idea.
Run Code Online (Sandbox Code Playgroud)
import conf等我可以看到它的优点,如果它是像C/C++等重量级语言,但对于python,为什么它有意义.这似乎就是在不增加任何好处的情况下夺走权力.
我想用 Python 获取网页的屏幕截图。为此,我使用http://github.com/AdamN/python-webkit2png/。
newArgs = ["xvfb-run", "--server-args=-screen 0, 640x480x24", sys.argv[0]]
for i in range(1, len(sys.argv)):
if sys.argv[i] not in ["-x", "--xvfb"]:
newArgs.append(sys.argv[i])
logging.debug("Executing %s" % " ".join(newArgs))
os.execvp(newArgs[0], newArgs)
Run Code Online (Sandbox Code Playgroud)
基本上使用正确的参数调用 xvfb-run 。但man xvfb说:
Note that the demo X clients used in the above examples will not exit on their own, so they will have to be killed before xvfb-run will exit.
因此,这意味着如果整个事情处于循环状态,该脚本将<????>(为了获取多个屏幕截图),除非 X 服务器被终止。我怎样才能做到这一点?
Pylint 经常给我这样的错误。
shabda@jazzy ~/uswaretech_uswaretechsite> pylint -e utpages/forms.py
No config file found, using default configuration
Exception RuntimeError: 'maximum recursion depth exceeded while calling a Python object' in <type 'exceptions.RuntimeError'> ignored
Run Code Online (Sandbox Code Playgroud)
logalib 邮件列表在http://lists.logilab.org/pipermail/python-projects/2009-March/001789.html上讨论了它,但它是一个旧线程,所以有人发现 Pylint 出现这样的错误,并发现代码中的根本原因是什么?
我有一个python模块.
我想在运行时为它填充一些值,我该怎么做.
例如.我有一个清单,
['A','B','C']
我正在创建具有这些名称的类,并希望它们可用,就像我正常创建它们一样
for el in ['A', 'B', 'C']:
type(el, (object,), {})
Run Code Online (Sandbox Code Playgroud) 我有一个字符串 x = "http://query.yahooapis.com/v1/public/yql?q=select%20owner%2Curls%20from%20flickr.photos.info%20where%20photo_id%3D'%s'&format=json"
如果我这样做x % 10失败,因为有%20f等等被视为格式字符串,所以我必须做一个字符串conactination.我怎样才能在这里使用普通的字符串替换.