问 题 main.py layout_format = {} for filename in os.listdir('mod'): if filename.endswith('py'): for line in open(os.path.join('mod',filename)): eval(line) print(layout_format) exit() mod/md.py markdown_template = "# %(title)s\r\nAuthor: %(author)s\r\nCreate Date: %(created_time)s\r\nLast Update: %(updated_time)s\r\n\r\n%(content)s\r\n" def markdown(self): return markdown_template % self layout_format['md']
以下是关于 python3.x 的编程技术问答
问 题 代码比较简单 import multiprocessing import time list1 = [1,2,3] def func(msg): list1[0] = 99 return "done " + msg if __name__ == "__main__": pool = multiprocessing.Pool(processes=4) for i in range(10): msg = "hello %d" % (i) pool.apply_async(func, (msg,)) pool.close() pool.join() print (list1) 因为真实的环境比较多,所以简化了代码,我大概的需求就是list1是一个比较多的数据列表,然后在多线程处理的时候,每次更新其中一项数据... 但是我运行以后,发现list1还是[1,2,3] , 我理想
问 题 texts = [[word for word in document.lower().split()] for document in documents] 我在网址我爱自然语言处理-如何计算两个文档的相似度(二)中看到下面一份代码。 对于>>> texts = [[word for word in document.lower().split()] for document in documents]的含义不是很理解。 >>>documents = ["Shipment of gold damaged in a fire", ... "Delivery of silver arrived in a silver truck", ... "Shipment of gold arrived in a truck"] >>> texts = [[word for word in document.lower().split()] for document in doc
问 题 Python3实现了一个简单的udp server和udp client。host指定为'localhost'时,在同一台机器上是运行正常的。 udpserver.py: from socket import * HOST = 'localhost' PORT = 9999 s = socket(AF_INET,SOCK_DGRAM) s.bind((HOST,PORT)) print('...waiting for message..') while True: data,address = s.recvfrom(1024) print(data,address) s.sendto('this is the UDP server'.encode('utf-8'), address) s.close() udpclient.py: from socket import * HOST='localho
写这段代码的起因是一道很简单的数学题: 数学上的做法如下: 在用笔算得出正确结果(4.5^100)后,想用Python写一段代码进行模拟以验证,代码如下: import random import datetime T=100 ex=0 random.seed(datetime.datetime.now()) while T>0: y=1 count=100 while count>0: x=random.randint(0,9) y*=x count-=1 ex+=y T-=1 ex/=100 print(ex) 然而每次输出的结果都是0,所以。。。Python的随机数生成器应该如何正确使用?
问 题 描述问题 format的用法,代码中的!r !s分别是format的哪部分用法 我查阅文档, 未能找到这部分的说明(https://docs.python.org/3/lib... 使用搜索引擎,也不知如何组织关键词 上下文环境 Python3 重现 拷贝代码 运行之 相关代码 class Pair: def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return 'Pair({0.x!r}, {0.y!r})'.format(self) def __str__(self): return '({0.x!s}, {0.y!s})'.format(self) >>> p = Pair(3, 4) >>> p Pa
问 题 看 Python 3.x 的文档,涉及到实例方法特殊属性的内容,有这么一段话描述 instance method 的 __func__ 属性: When an instance method object is created by retrieving a user-defined function object from a class via one of its instances, its self attribute is the instance, and the method object is said to be bound. The new method’s func attribute is the original function object. When a user-defined method object is created by retrieving another method object from a class or
问 题 win10系统,python 3.5.2, djang 1.10.3 刚刚创建django项目 C:\Users\JD\Desktop\firstsite>python manage.py runserver Traceback (most recent call last): File "manage.py", line 22, in execute_from_command_line(sys.argv) File "C:\Python35\lib\site-packages\django\core\management\__init__.py", line 367, in execute_from_command_line utility.execute() File "C:\Python35\lib\site-packages\django\core\management\__init__.py", line 35
问 题 多线程中 join 的疑问: for t in th: t.setDaemon(True) t.start() for t in th: t.join() 按照上面的代码,假如有10个线程,那么: 第一个 for 循环是10个线程依次启动(这里有理解错误么?) 主要是第二个 for 循环,是否意味着这十个循环必须依次执行?因为: 第一个线程没有执行完的话,会在 t[0].join()这里等着他。 即使第二个现成执行完了,这段代码也会导致一定要t[0].join()之后,再等待t[1].join(),是么?也就是这个部分其实是有依赖的,即使后面n-1个线程都是0ms 执行完,也一定要等 t0执行完之后才能 join()? for t in th: t.setDaemon(True) t.start() for index, t in enumerate(th): t.join() print
问 题 想写个小程序,目的是将GPS写入照片的exif信息中,没有处理图片信息的经验,找了python 3的相关库,貌似pillow可以处理,但粗看了一下官方文档,貌似只提供了read exif的功能,并没有找到修改exif的方法,还请有经验的大神给些指点。 解决方案 花时间自己解决了一下:目前pillow包并没有简便的方式修改 exif 信息:其只提供了save()方法,该方法可以将修改好的 exif 信息重新保存到照片之中(im.save(fp, format, exif=raw_exif)),但是save方法只接收raw_exif的bytes类型的 exif 数据,这就让人十分抓狂,因为pillow的_getexif() 方法返回的是字典格式,即便可以在字典格式中增改了所需要的 exif 信息,也还是需要想办法将修改好的字典转换成raw_exif,可是pillow竟然没有提供这个方法...瞬间无语中。 另一种方法就是通过pillow的Image.info['ex
问 题 举个例子 class A: def __init__(self,value1,value2): self.value1=value1 self.value2=value2 def function(value): #do some thing and get the name of obj.property print(property) tempObj=A() function(tempObj.value1)#这里会print输出value1,无论value1值是多少 function(tempObj.value2)#这里会print输出value2,无论value2值是多少 真实遇到的问题是这样的 在写restfulApi,每次会在逻辑代码前要检查传入数据的正确性,经常会检查数组的长度 然后大概写成这样 def post(self): parsed_data = sel