我最近开始学习Python,但我找不到如何实现多行注释.大多数语言都有块注释符号
/*
*/
Run Code Online (Sandbox Code Playgroud)
我在Python中试过这个,但它会抛出一个错误,所以这可能不是正确的方法.Python实际上是否具有多行注释功能?
在osPython 的模块中,有没有办法找到目录是否存在,例如:
>>> os.direxists(os.path.join(os.getcwd()), 'new_folder')) # in pseudocode
True/False
Run Code Online (Sandbox Code Playgroud) 我正在使用virtualenv,我需要安装"psycopg2".
我做了以下事情:
pip install http://pypi.python.org/packages/source/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
Run Code Online (Sandbox Code Playgroud)
我有以下消息:
Downloading/unpacking http://pypi.python.org/packages/source/p/psycopg2/psycopg2
-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
Downloading psycopg2-2.4.tar.gz (607Kb): 607Kb downloaded
Running setup.py egg_info for package from http://pypi.python.org/packages/sou
rce/p/psycopg2/psycopg2-2.4.tar.gz#md5=24f4368e2cfdc1a2b03282ddda814160
Error: pg_config executable not found.
Please add the directory containing pg_config to the PATH
or specify the full executable path with the option:
python setup.py build_ext --pg-config /path/to/pg_config build ...
or with the pg_config option in 'setup.cfg'.
Complete output from command python setup.py egg_info:
running egg_info
creating pip-egg-info\psycopg2.egg-info
writing pip-egg-info\psycopg2.egg-info\PKG-INFO
writing top-level names to pip-egg-info\psycopg2.egg-info\top_level.txt
writing …Run Code Online (Sandbox Code Playgroud) 是什么区别include,并extend在用例图?
我的Python软件包有一个setup.py可以在Ubuntu Trusty上本地构建并在一个新的Vagrant Ubuntu Trusty VM上构建,当我像这样配置它时:
sudo apt-get install python python-dev --force-yes --assume-yes --fix-broken
curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python2.7
sudo -H pip install setuptools wheel virtualenv --upgrade
Run Code Online (Sandbox Code Playgroud)
但是当我在Travis CI Trusty Beta VM上做同样的事情时:
- sudo apt-get install python python-dev --force-yes --assume-yes --fix-broken
- curl --silent --show-error --retry 5 https://bootstrap.pypa.io/get-pip.py | sudo python2.7
- sudo -H pip install setuptools wheel virtualenv --upgrade
Run Code Online (Sandbox Code Playgroud)
我明白了:
python2.7 setup.py bdist_wheel
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
or: setup.py --help …Run Code Online (Sandbox Code Playgroud) 我在pandas DataFrame中有一个字段,它以字符串格式导入.它应该是一个日期时间变量.如何将其转换为日期时间列,然后根据日期进行过滤.
例:
我一直试图找出如何表示一个最大整数,我已经阅读使用"sys.maxint".但是,在Python 3中,当我调用它时,我得到:
AttributeError: module 'object' has no attribute 'maxint'
Run Code Online (Sandbox Code Playgroud) 采用以下格式:
3D:F2:C9:A6:B3:4F
Run Code Online (Sandbox Code Playgroud)
要么:
3D-F2-C9-A6-B3-4F
Run Code Online (Sandbox Code Playgroud) 我一直在玩Python的哈希函数.对于小整数,它hash(n) == n总是出现.然而,这并没有扩展到大数:
>>> hash(2**100) == 2**100
False
Run Code Online (Sandbox Code Playgroud)
我并不感到惊讶,我理解哈希需要一个有限范围的值.这个范围是多少?
我尝试使用二进制搜索来找到最小的数字hash(n) != n
>>> import codejamhelpers # pip install codejamhelpers
>>> help(codejamhelpers.binary_search)
Help on function binary_search in module codejamhelpers.binary_search:
binary_search(f, t)
Given an increasing function :math:`f`, find the greatest non-negative integer :math:`n` such that :math:`f(n) \le t`. If :math:`f(n) > t` for all :math:`n \ge 0`, return None.
>>> f = lambda n: int(hash(n) != n)
>>> n = codejamhelpers.binary_search(f, 0)
>>> hash(n)
2305843009213693950 …Run Code Online (Sandbox Code Playgroud) 在python 3中,我可以创建参数并返回类型注释.例:
class Graph:
def __init__(self, V: int, E: int, edges: list):
pass
@classmethod
def fromfile(cls, readobj: type(sys.stdin)):
pass
def V(self) -> int:
pass
def E(self) -> int:
pass
Run Code Online (Sandbox Code Playgroud)
问题是我无法使用当前类(Graph)的返回类型进行注释,该类尚未定义.例:
class Graph:
def reverse(self) -> Graph:
pass
Run Code Online (Sandbox Code Playgroud)
此代码有错误
def reverse(self) -> Graph:
NameError: name 'Graph' is not defined
Run Code Online (Sandbox Code Playgroud)
这些注释对于记录和允许IDE识别参数和返回类型=>启用自动完成非常有用
UPD:所以我提出的是要么是不可能的要么是需要一些我不喜欢的黑客攻击,所以我决定使用def reverse (self) -> 'Graph':
哪种文档是可以理解的,尽管违反了规则.缺点是它不适用于IDE自动完成.
python ×8
python-3.x ×4
python-2.7 ×2
annotations ×1
class ×1
comments ×1
datetime ×1
directory ×1
hash ×1
pandas ×1
pip ×1
psycopg2 ×1
pypi ×1
regex ×1
setup.py ×1
travis-ci ×1
uml ×1
use-case ×1
virtualenv ×1