Categories
python

关于python3编码的说明

写代码过程中遇到的编码问题,通过搜索引擎解决了。记录在这里,方便自己以后查阅。
需要注意的几点:
python3中字符串是unicode编码,但并不等同于utf-8编码!
字符串通过编码(encode)转换成字节码,字节码通过解码(decode)成为字符串
直接使用str.decode(“xxxx”)是不行的,直接就报错了 AttributeError: ‘str’ object has no attribute ‘decode’
string != bytes

参考
1. https://www.cnblogs.com/wenyi1992/p/6261716.html
2. https://www.cnblogs.com/Xuuuuuu/articles/9670451.html
3. https://www.cnblogs.com/abclife/p/7445222.html
4. https://www.cnblogs.com/liuyinzhou/p/7911509.html

Categories
python web

用python创建最简单的HTTP服务

python2:

python -m SimpleHTTPServer 8080

python3:

python3 -m http.server 8080

参考:https://docs.python.org/2/library/simplehttpserver.html

 

have a fun!

Categories
python

足彩分析项目

一直以来都在买足彩竞彩,但总是黑多红少。。打算利用业余时间做一个足彩分析项目,用技术手段提高中奖率。。不知道是否可行

Categories
linux mac python web

两个有意思的东东

1. Javascript 3D library – three.js

官网:threejs.org
github地址:https://github.com/mrdoob/three.js/

2. python’s graph tool libary: graph-tool.

下载地址 https://graph-tool.skewed.de/download

Categories
other python

流程自助设计相关资料

最近工作内容需要做一个流程自助表单的小项目,找了些相关的资料,先记在这里:

https://code.djangoproject.com/wiki/GoFlow

https://code.djangoproject.com/wiki/GoFlow/sampleprocess

https://code.djangoproject.com/wiki/data_model

https://code.google.com/p/goflow/

https://code.djangoproject.com/wiki/GoFlow

Categories
python

python里的下划线及私有变量

pep8里关于私有变量的说明:

In addition, the following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention):

  • _single_leading_underscore: weak “internal use” indicator. E.g. from M import * does not import objects whose name starts with an underscore.
  • single_trailing_underscore_: used by convention to avoid conflicts with Python keyword, e.g.

Tkinter.Toplevel(master, class_='ClassName')

  • __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).
  • __double_leading_and_trailing_underscore__: “magic” objects or attributes that live in user-controlled namespaces. E.g. __init__, __import__ or __file__. Never invent such names; only use them as documented.

以下是两道练习题,看看应该输出啥
1.

class Parent(object):
def __init__(self):
self.__private()
self.public()
def __private(self):
print 'Parent.__private()'
def public(self):
print 'Parent.public()'

class Child(Parent):
def __private(self):
print 'Child.__private()'
def public(self):
print 'Child.public()'

if __name__ == "__main__":
child = Child() #会输出啥?

2.

class NameMangling(object):
def __init__(self):
self._NameMangling__private() # 调用一个没有定义的函数
self.public()
def __private(self):
print 'NameMangling.__private()'
def public(self):
print 'NameMangling.public()'

if __name == "__main__":
test = NameMangling()

Categories
python

ImportError: cannot import name WebSocketHandler

wssh 是一个 SSH 到 WebSockets 的Bridge,可以让你通过 HTTP 调用远程服务器的 SHELL 命令。

远程机器启动一个daemon,然后客户端通过浏览器就可以查看到远程机器的屏幕输出。这个东东可以方便地集成到自己的Python Web应用里。

wssh的客户端自带了Python和Javascript的Lib库,如果想在其他语言里面使用也能很方便的改写。

安装wssh的步骤:

1) git clone https://github.com/aluzzardi/wssh.git

2) cd wssh; python setup.py install

3) sudo pip install flask gevent paramiko gevent-websocket

基本就可以了,启动wsshd,报错,ImportError: cannot import name WebSocketHandler

首先google之,试了几个都没解决问题。直接打开出错提示中的对应文件,定位到错误处,发现是:

from geventwebsocket import WebSocketHandler

翻了下gevent-websocket的文档,改成了如下就ok了:

from geventwebsocket.handler import WebSocketHandler