python

How print every line of a python script as its being executed (including the console)?

浪子不回头ぞ 提交于 2021-02-20 17:55:23
问题 I'd like to print every line of python script as it's being executed, as well as the log from the console as every line is being executed. For example, for this script: import time print 'hello' time.sleep(3) print 'goodbye' I'd like it to generate the following in the console: line 1: import time line 2: print 'hello' hello line 3: time.sleep(3) line 4: print 'goodbye' goodbye See below for my attempt import subprocess as subp python_string = """ import sys import inspect class SetTrace

How print every line of a python script as its being executed (including the console)?

浪子不回头ぞ 提交于 2021-02-20 17:55:02
问题 I'd like to print every line of python script as it's being executed, as well as the log from the console as every line is being executed. For example, for this script: import time print 'hello' time.sleep(3) print 'goodbye' I'd like it to generate the following in the console: line 1: import time line 2: print 'hello' hello line 3: time.sleep(3) line 4: print 'goodbye' goodbye See below for my attempt import subprocess as subp python_string = """ import sys import inspect class SetTrace

How to detect screen rotation on Android in Kivy?

♀尐吖头ヾ 提交于 2021-02-20 17:54:26
问题 I have been searching for a Kivy solution to capture the Android device rotation from one orientation to another. I have tried both of the window methods below but neither executes the on_rotate or rotate_screen routines when I rotate the device. I see there is an onConfigurationChanged event in java but I can't find the same event handling for Kivy. Window.on_rotate(self.on_rotate) Window.bind(on_rotate=self.rotate_screen) What I do get in the logcat is the following messages indicating the

How print every line of a python script as its being executed (including the console)?

旧城冷巷雨未停 提交于 2021-02-20 17:54:14
问题 I'd like to print every line of python script as it's being executed, as well as the log from the console as every line is being executed. For example, for this script: import time print 'hello' time.sleep(3) print 'goodbye' I'd like it to generate the following in the console: line 1: import time line 2: print 'hello' hello line 3: time.sleep(3) line 4: print 'goodbye' goodbye See below for my attempt import subprocess as subp python_string = """ import sys import inspect class SetTrace

How to detect screen rotation on Android in Kivy?

ぃ、小莉子 提交于 2021-02-20 17:53:23
问题 I have been searching for a Kivy solution to capture the Android device rotation from one orientation to another. I have tried both of the window methods below but neither executes the on_rotate or rotate_screen routines when I rotate the device. I see there is an onConfigurationChanged event in java but I can't find the same event handling for Kivy. Window.on_rotate(self.on_rotate) Window.bind(on_rotate=self.rotate_screen) What I do get in the logcat is the following messages indicating the

Subclassing logging.Formatter Changes Default Behavior of logging.Formatter

对着背影说爱祢 提交于 2021-02-20 17:51:17
问题 I added two handlers with different formatters to my logger. The first one requires subclassing logging.Formatter to do custom formatting. The default formatter will suffice for the second handler. Let's say the first formatter simply removes newline characters from the message. The following script illustrates what seems like strange behavior: import logging logger = logging.getLogger(__name__) class CustomFormatter(logging.Formatter): def __init__(self, *args, **kwargs): super().__init__(

Linux-部署-Django

拟墨画扇 提交于 2021-02-20 17:21:27
Linux-部署-Django-项目过程与问题总结 优才网 2017-04-12 18:00 本篇主要用于记录部署 Django 项目所有踩过的坑。 最近学习 Django 框架开发,将项目部署到 Linux 服务器上时遇到了很多坑,在此整理一下以作备忘,同时希望对有需要的同学有所帮助。 从 0 开始,在 Linux 上部署 Django 项目共有如下几步: 1. 安装 Python3.5 2. 安装 Apache2.4 3. 安装 wsgi 4. 配置数据库和静态文件 5. 配置 Apache 6. 处理权限问题 下面一步步来进行详细介绍: 安装 Python3.5 1.安装编译环境 2. 下载编译 Python3.5 源码包 prefix 指明了 Python3.5 的安装目录,后面的 --enable-shared LDFLAGS="-Wl,-rpath=/usr/local/python3" 选项保证了可以正常使用共享库,否则在编译安装后可能会出现以下错误: 我自己在完成后执行 Python3.5 的命令时就遇到了。查找了很多的解决方案,上面这种在编译时进行LDFLAGS配置应该是最简洁有效的方式。 另外这个错误在使用wsgi时也可能会遇到,其官方文档还特地针对该问题作了【介绍】(http://modwsgi.readthedocs.io/en/develop/user

pythonic风格代码有什么好处?附12个代码实例

淺唱寂寞╮ 提交于 2021-02-20 17:20:09
pythonic是开发者们在写python代码过程中总结的编程习惯,崇尚优雅、明确、简单。就好比中文笔画,有先后顺序,最符合文字书写的习惯。 因为是习惯,不是江湖规则,所以你大可不必遵守pythonic,但如果你想成为python高手,最好是养成这个习惯。 对比其他语言我们能直观看出pythonic风格的特点,比如写一个简单循环。 在Java里这样的: for index in (index; index < items.length ; index++) { item = items[index]; ... now do something } 尝试用python来写循环,则非常简洁易懂: for item in items: item.perform_action() 想要更加pythonic,用生成器表达式来写循环: (item.some_attribute for item in items) 这样的写法其实已经接近自然语言,一眼能看出代码意思。 如果你在Python IDE中输入 import python ,则会看到下面一首诗: 美胜于丑,简胜于繁,这就是Python哲学。 有一本书《effctive python》里面讲到蛮多pythonic的写法,下面列出一些常见的代码。 「1、用列表推导式来取代map、filter」 map

Optimizing python for loops

☆樱花仙子☆ 提交于 2021-02-20 17:09:49
问题 Here are two programs that naively calculate the number of prime numbers <= n. One is in Python and the other is in Java. public class prime{ public static void main(String args[]){ int n = Integer.parseInt(args[0]); int nps = 0; boolean isp; for(int i = 1; i <= n; i++){ isp = true; for(int k = 2; k < i; k++){ if( (i*1.0 / k) == (i/k) ) isp = false; } if(isp){nps++;} } System.out.println(nps); } } `#!/usr/bin/python` import sys n = int(sys.argv[1]) nps = 0 for i in range(1,n+1): isp = True

Optimizing python for loops

馋奶兔 提交于 2021-02-20 17:08:51
问题 Here are two programs that naively calculate the number of prime numbers <= n. One is in Python and the other is in Java. public class prime{ public static void main(String args[]){ int n = Integer.parseInt(args[0]); int nps = 0; boolean isp; for(int i = 1; i <= n; i++){ isp = true; for(int k = 2; k < i; k++){ if( (i*1.0 / k) == (i/k) ) isp = false; } if(isp){nps++;} } System.out.println(nps); } } `#!/usr/bin/python` import sys n = int(sys.argv[1]) nps = 0 for i in range(1,n+1): isp = True