V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  lrxiao  ›  全部回复第 9 页 / 共 16 页
回复总数  307
1  2  3  4  5  6  7  8  9  10 ... 16  
2017-11-21 12:18:28 +08:00
回复了 winglight2016 创建的主题 Python 请教一个 Python 列表解析的问题
如果你修改__getitem__是可以用的
2017-11-21 12:18:05 +08:00
回复了 winglight2016 创建的主题 Python 请教一个 Python 列表解析的问题
233 其实这都是 python 对象。。
相当于传了一个
(slice(None, None, None), 2)的 tuple 给__getitem__
但是:单独不是 py 对象 不像...
2017-11-20 13:07:46 +08:00
回复了 sticnarf 创建的主题 分享发现 分享一个病毒的 GitHub Repo
So funny hhhhh
2017-11-15 10:01:55 +08:00
回复了 billgreen1 创建的主题 Python 如何批量下载文件?
我觉得 lz 是想扫人家网站上的 archive..
2017-11-15 09:55:00 +08:00
回复了 KOSKERS 创建的主题 Linux 新版 Firefox 57 速度在 Linux 上太快了。。。。。
这几天好多这种反馈..感想:该学 Rust 了
2017-11-15 09:48:29 +08:00
回复了 workwonder 创建的主题 Python Python 's builtin min/max is evil
@workwonder 那也是你自己的情况。。然后就 evil。。
2017-11-15 09:47:28 +08:00
回复了 workwonder 创建的主题 Python Python 's builtin min/max is evil
@billgreen1 我知道。。我在想为什么 numpy 这么干 大概是早发现 早报错的思路。。
2017-11-15 09:28:42 +08:00
回复了 billgreen1 创建的主题 Python 如何批量下载文件?
最后一个参数就是如果换个 base url。。
2017-11-15 09:28:07 +08:00
回复了 billgreen1 创建的主题 Python 如何批量下载文件?
https://gist.github.com/Airtnp/f6d4ce2ee116dab9ffdfbd3f11c3e762

以前写过一个玩具。。。用法就是
python PDF_downloader.py url folder [如果文件不用 url/...格式]
2017-11-15 09:22:32 +08:00
回复了 workwonder 创建的主题 Python Python 's builtin min/max is evil
@billgreen1 因为 IEEE754 说了因为 NaN 本身没有序 除非-NaN +NaN 比较 不知道 numpy 什么意思
2017-11-15 01:45:33 +08:00
回复了 workwonder 创建的主题 Python Python 's builtin min/max is evil
ignore None 还叫 安 全 替 代 ...
2017-11-10 00:38:01 +08:00
回复了 Lxxyx 创建的主题 C 关于指针的疑惑, int **p[10], p 是数组还是指针?
这问题居然能引来地球。。
2017-11-08 00:20:20 +08:00
回复了 vtoexsir 创建的主题 Python python3 正则不支持 Unicode properties,有第三方正则模块支持吗?
regex+1
2017-11-07 13:00:41 +08:00
回复了 vtoexsir 创建的主题 Python Python 怎么获取函数参数的字面量?
emmm 解决了传入 attr 访问

import sys
import dis
import types
from opcode import *
import inspect

# ref: https://nedbatchelder.com/blog/200804/wicked_hack_python_bytecode_tracing.html
def hack_line_numbers(f):
....""" Replace a code object's line number information to claim that every
........byte of the bytecode is a new line. Returns a new code object.
........Also recurses to hack the line numbers in nested code objects.
...."""
....code = f.__code__
....n_bytes = len(code.co_code)
....new_lnotab = "\x01\x01" * (n_bytes-1)
....new_consts = []
....for const in code.co_consts:
........if type(const) == types.CodeType:
............new_consts.append(hack_line_numbers(const))
........else:
............new_consts.append(const)
....new_code = types.CodeType(
........code.co_argcount, code.co_kwonlyargcount, code.co_nlocals, code.co_stacksize, code.co_flags,
........code.co_code, tuple(new_consts), code.co_names, code.co_varnames,
........code.co_filename, code.co_name, 0, str.encode(new_lnotab), code.co_freevars, code.co_cellvars
........)
....f.__code__ = new_code
....f.__is_lineno_hacked__ = True
....return f

def get_variable_name_easy(**kwargs):
....for arg_name in kwargs:
........return kwargs[arg_name], arg_name

def get_variable_name_simple(var):
....loc = sys._getframe(1).f_locals
....names = []
....for k, v in loc.items():
........if v == var:
............names.append(k)
....return names

# Don't work with REPL, nothing named after f_globals['<module>']
# Need to redirect a frame
# If not handled with hacked lineno,
# We must use 1-level nested no-argument function
# which directly ref to ordered variable
def get_variable_name(var):
....last_frame = sys._getframe(1)
....last_code = last_frame.f_code
....last_func_name = last_code.co_name
....last_func = None
....if last_func_name in last_frame.f_globals.keys():
........last_func = last_frame.f_globals[last_func_name]
....elif last_func_name in last_frame.f_locals.keys():
........last_func = last_frame.f_globals[last_func_name]
....else:
........# nested support
........if last_func_name in last_frame.f_back.f_globals.keys():
............last_func = last_frame.f_back.f_globals[last_func_name]
........elif last_func_name in last_frame.f_back.f_locals.keys():
............last_func = last_frame.f_back.f_locals[last_func_name]
....is_lineno_hacked = False;
....if not last_func:
........print("Holy crap. Assume we have hacked our lineno")
........is_lineno_hacked = True
....elif '__is_lineno_hacked__' in last_func.__dict__.keys():
........is_lineno_hacked = True
....if is_lineno_hacked:
........last_code_arr = bytearray(last_code.co_code)
........call_lineno = last_frame.f_lineno
........# last_code_arr[last_frame.f_lineno] = opmap['CALL_FUNCTION']
........attr_name = []
........pos_code = 2
........pos_off = 1
........load_var_op = last_code_arr[call_lineno - pos_code]
........load_var_pos = last_code_arr[call_lineno - pos_off]
........while load_var_op == opmap['LOAD_ATTR']:
............attr_name.append(last_code.co_names[load_var_pos])
............load_var_op = last_code_arr[call_lineno - pos_code]
............load_var_pos = last_code_arr[call_lineno - pos_off]
............pos_code += 2
............pos_off += 2
........if load_var_op == opmap['LOAD_FAST']:
............attr_name.append(last_code.co_varnames[load_var_pos])
............return '.'.join(attr_name)
........elif load_var_op == opmap['LOAD_GLOBAL'] or load_var_op == opmap['LOAD_NAME']:
............attr_name.append(last_code.co_names[load_var_pos])
............return '.'.join(attr_name)
........elif load_var_op == opmap['LOAD_DEREF']:
............attr_name.append(last_code.co_freevars[load_var_pos])
............return '.'.join(attr_name)

........print("I don't know, maybe just consts")
........return None
....else:
........last_func = hack_line_numbers(last_func)
........sys._getframe(0).f_locals[last_func_name] = last_func
........return last_func()
........# sys._getframe(0).f_back = last_frame.f_back
........# last_frame.clear()

def do_nothing():
....pass

@hack_line_numbers
def f():
....a_var = 'str'
....print(get_variable_name(a_var)) # a_var


def g():
....ar = 1
....arrrrrgggghhhhhh = 1
....do_nothing()
....do_nothing()
....do_nothing()
....do_nothing()
....name = None
....def nested_get_varname():
........return get_variable_name(arrrrrgggghhhhhh)
....name = nested_get_varname()
....print(name) # arrrrrgggghhhhh

class A:
....pass

def h():
....test = A()
....test.t = A()
....test.t.t = 1
....return get_variable_name(test.t.t) # test.t.t

def u():
....return get_variable_name(1)

def test_all():
....f()
....g()
....print(h())
....print(u())
2017-11-07 11:50:45 +08:00
回复了 vtoexsir 创建的主题 Python Python 怎么获取函数参数的字面量?
emmm 原来可以 kwargs 23333
2017-11-07 11:49:51 +08:00
回复了 vtoexsir 创建的主题 Python Python 怎么获取函数参数的字面量?
好的 我又丧病的 hack 了

import.sys
import.dis
import.types
from.opcode.import.*

#.ref:.https://nedbatchelder.com/blog/200804/wicked_hack_python_bytecode_tracing.html
def.hack_line_numbers(f):
....""".Replace.a.code.object's.line.number.information.to.claim.that.every
........byte.of.the.bytecode.is.a.new.line...Returns.a.new.code.object.
........Also.recurses.to.hack.the.line.numbers.in.nested.code.objects.
...."""
....code.=.f.__code__
....n_bytes.=.len(code.co_code)
....new_lnotab.=."\x01\x01".*.(n_bytes-1)
....new_consts.=.[]
....for.const.in.code.co_consts:
........if.type(const).==.types.CodeType:
............new_consts.append(hack_line_numbers(const))
........else:
............new_consts.append(const)
....new_code.=.types.CodeType(
........code.co_argcount,.code.co_kwonlyargcount,.code.co_nlocals,.code.co_stacksize,.code.co_flags,
........code.co_code,.tuple(new_consts),.code.co_names,.code.co_varnames,
........code.co_filename,.code.co_name,.0,.str.encode(new_lnotab),.code.co_freevars,.code.co_cellvars
........)..
....f.__code__.=.new_code
....return.f

def.get_variable_name_simple(var):
....loc.=.sys._getframe(1).f_locals
....names.=.[]
....for.k,.v.in.loc.items():
........if.v.==.var:
............names.append(k)
....return.name

#.don't.work.with.REPL
#.the.caller.function.should.hack.line.number.to.get.accurate.lineno
def.get_variable_name(var):
....last_frame.=.sys._getframe(1)
....frame.=.sys._getframe(0)
....last_code.=.sys._getframe(1).f_code
....last_code_arr.=.bytearray(last_code.co_code)
....call_lineno.=.last_frame.f_lineno
....#.last_code_arr[last_frame.f_lineno].=.opmap['CALL_FUNCTION']
....load_var_op.=.last_code_arr[call_lineno.-.2]
....load_var_pos.=.last_code_arr[call_lineno.-.1]
....if.load_var_op.==.opmap['LOAD_NAME'].or.load_var_op.==.opmap['LOAD_FAST']:
........return.last_code.co_varnames[load_var_pos]
....if.load_var_op.==.opmap['LOAD_GLOBAL']:
........return.last_code.co_names[load_var_pos]
....print("I.don't.know,.maybe.just.consts")
....return.None

def.do_nothing():
....pass

@hack_line_numbers
def.g():
....ar.=.1
....arrrrrgggghhhhhh.=.1
....do_nothing()
....do_nothing()
....do_nothing()
....do_nothing()
....n.=.get_variable_name(arrrrrgggghhhhhh)
....print(n)

> arrrrrgggghhhhhh

空格全部替换的. 为了缩进。。。。。
2017-11-07 10:51:52 +08:00
回复了 vtoexsir 创建的主题 Python Python 怎么获取函数参数的字面量?
loc = sys._getframe(1).f_locals
2017-11-07 10:51:13 +08:00
回复了 vtoexsir 创建的主题 Python Python 怎么获取函数参数的字面量?
@crazycabbage
用 locals 不太好
不如 loc = sys._getframe(1)
2017-11-06 11:08:59 +08:00
回复了 SimbaPeng 创建的主题 Python 我想知道 Python 的类中到底有没有建立作用域?
这个 a 只是个 local variable 改下 @ifkite 的类

import builtins
import sys


old_bc = builtins.__build_class__

def fake_build_class(cls_func, cls_name):
....print("Build class...")
....print(cls_func.__code__.co_consts)
....print(cls_func.__code__.co_names)
....print(cls_name)
....print("Create class...")
....cls = old_bc(cls_func, cls_name)
....print("End of create class...")
....print(cls.__dict__['a'])

builtins.__build_class__ = fake_build_class

class A:
....a = 3
....def test():
........frame = sys._getframe(1)
........print(frame.f_locals['a'])
....test()

嗯 极其丧病
2017-11-06 10:57:56 +08:00
回复了 SimbaPeng 创建的主题 Python 我想知道 Python 的类中到底有没有建立作用域?
讲 unbound 的都是 py2 选手。。
py3 除非你要 instance 访问才需要 @staticmethod
打印 3 是创建时的事情 (发现我上面写的疯狂笔误。。)
1  2  3  4  5  6  7  8  9  10 ... 16  
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3964 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 25ms · UTC 10:21 · PVG 18:21 · LAX 03:21 · JFK 06:21
Developed with CodeLauncher
♥ Do have faith in what you're doing.