title
stringlengths
10
172
question_id
int64
469
40.1M
question_body
stringlengths
22
48.2k
question_score
int64
-44
5.52k
question_date
stringlengths
20
20
answer_id
int64
497
40.1M
answer_body
stringlengths
18
33.9k
answer_score
int64
-38
8.38k
answer_date
stringlengths
20
20
tags
list
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,287
<p>You could read chunks of 1,000 bytes or so from the end of the file into a buffer until you have 10 lines.</p>
1
2008-11-03T23:06:54Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,312
<p>I think reading the last 2 KB or so of the file should make sure you get 10 lines, and shouldn't be too much of a resource hog.</p> <pre><code>file_handle = open("somefile") file_size = file_handle.tell() file_handle.seek(max(file_size - 2*1024, 0)) # this will get rid of trailing newlines, unlike readlines() last...
7
2008-11-03T23:21:29Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,324
<p>Personally I'd be tempted to break out to the shell and call tail -n10 to load the file. But then I'm not really a Python programmer ;)</p>
0
2008-11-03T23:27:11Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,339
<p>I think I remember adapting the code from <a href="http://manugarg.blogspot.com/2007/04/tailing-in-python.html" rel="nofollow">this blog post from Manu Garg</a> when I had to do something similar.</p>
2
2008-11-03T23:35:00Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,352
<pre><code># Tail from __future__ import with_statement find_str = "FIREFOX" # String to find fname = "g:/autoIt/ActiveWin.log_2" # File to check with open(fname, "r") as f: f.seek (0, 2) # Seek @ EOF fsize = f.tell() # Get Size f.seek (max (fsize-1024, 0), 0) # Set...
27
2008-11-03T23:40:34Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,359
<p>First, a function that returns a list:</p> <pre><code>def lastNLines(file, N=10, chunksize=1024): lines = None file.seek(0,2) # go to eof size = file.tell() for pos in xrange(chunksize,size-1,chunksize): # read a chunk file.seek(pos,2) chunk = file.read(chunksize) if ...
0
2008-11-03T23:43:06Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,394
<p>If you are running Python on a POSIX system, you can use 'tail -10' to retrieve the last few lines. This may be faster than writing your own Python code to get the last 10 lines. Rather than opening the file directly, open a pipe from the command 'tail -10 filename'. If you are certain of the log output though (for ...
8
2008-11-04T00:04:13Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,407
<p>If you're on a unix box, <code>os.popen("tail -10 " + filepath).readlines()</code> will probably be the fastest way. Otherwise, it depends on how robust you want it to be. The methods proposed so far will all fall down, one way or another. For robustness and speed in the most common case you probably want someth...
1
2008-11-04T00:08:58Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,433
<p>Here's an answer like MizardX's, but without its apparent problem of taking quadratic time in the worst case from rescanning the working string repeatedly for newlines as chunks are added.</p> <p>Compared to the activestate solution (which also seems to be quadratic), this doesn't blow up given an empty file, and d...
29
2008-11-04T00:19:26Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,648
<p>I ran into that problem, parsing the last hour of LARGE syslog files, and used this function from activestate's recipe site... (<a href="http://code.activestate.com/recipes/439045/" rel="nofollow">http://code.activestate.com/recipes/439045/</a>)</p> <pre><code>!/usr/bin/env python # -*-mode: python; coding: iso-885...
2
2008-11-04T02:37:04Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
260,973
<p>Here is a version using <code>mmap</code> that seems pretty efficient. The big plus is that <code>mmap</code> will automatically handle the file to memory paging requirements for you.</p> <pre><code>import os from mmap import mmap def lastn(filename, n): # open the file and mmap it f = open(filename, 'r+')...
5
2008-11-04T05:21:41Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
262,921
<p>This solution will read the file only once, but using 2 file object pointers to be able obtain the last N lines of file without re-reading it:</p> <pre><code>def getLastLines (path, n): # return the las N lines from the file indicated in path fp = open(path) for i in range(n): line = fp.readlin...
0
2008-11-04T18:50:40Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
262,988
<p>You could also count the lines as you reverse through the file, instead of guessing at a byte offset. </p> <pre><code>lines = 0 chunk_size = 1024 f = file('filename') f.seek(0, 2) f.seek(f.tell() - chunk_size) while True: s = f.read(chunk_size) lines += s.count('\n') if lines &gt; NUM_OF_LINES: ...
1
2008-11-04T19:03:35Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
2,436,554
<p>Maybe this might be useful:</p> <pre><code>import os.path path = 'path_to_file' os.system('tail -n1 ' + path) </code></pre>
0
2010-03-12T23:30:58Z
[ "python", "file", "search" ]
Most efficient way to search the last x lines of a file in python
260,273
<p>I have a file and I don't know how big it's going to be (it could be quite large, but the size will vary greatly). I want to search the last 10 lines or so to see if any of them match a string. I need to do this as quickly and efficiently as possible and was wondering if there's anything better than:</p> <pre><co...
29
2008-11-03T23:01:50Z
17,841,165
<p>I took mhawke's suggestion to use <code>mmap</code> and wrote a version that uses <code>rfind</code>:</p> <pre><code>from mmap import mmap import sys def reverse_file(f): mm = mmap(f.fileno(), 0) nl = mm.size() - 1 prev_nl = mm.size() while nl &gt; -1: nl = mm.rfind('\n', 0, nl) yie...
1
2013-07-24T17:43:52Z
[ "python", "file", "search" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
260,770
<p>You can find information about Python audio here: <a href="http://wiki.python.org/moin/Audio/">http://wiki.python.org/moin/Audio/</a></p> <p>It doesn't look like it can play .mp3 files without external libraries. You could either convert your .mp3 file to a .wav or other format, or use a library like <a href="http:...
11
2008-11-04T03:27:13Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
260,901
<p>Your best bet is probably to use <a href="http://www.pygame.org" rel="nofollow">pygame/SDL</a>. It's an external library, but it has great support across platforms.</p> <pre><code>pygame.mixer.init() pygame.mixer.music.load("file.mp3") pygame.mixer.music.play() </code></pre> <p>You can find more specific documenta...
19
2008-11-04T04:40:50Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
260,924
<p>You can't do this without a nonstandard library.</p> <p>for windows users who end up in this thread, try <a href="http://python.net/crew/mhammond/win32/" rel="nofollow">pythonwin</a>. <a href="http://www.pygame.org/docs/ref/mixer.html" rel="nofollow">PyGame</a> has some sound support. For hardware accelerated gam...
1
2008-11-04T04:52:24Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
262,084
<p><a href="http://pyglet.org/">Pyglet</a> has the ability to play back audio through an external library called <a href="http://code.google.com/p/avbin">AVbin</a>. Pyglet is a ctypes wrapper around native system calls on each platform it supports. Unfortunately, I don't think anything in the standard library will play...
5
2008-11-04T14:37:24Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
507,464
<p>If you need portable Python audio library try <a href="http://people.csail.mit.edu/hubert/pyaudio/">PyAudio</a>. It certainly has a mac port.</p> <p>As for mp3 files: it's certainly doable in "raw" Python, only I'm afraid you'd have to code everything yourself :). If you can afford some external library I've found ...
6
2009-02-03T15:08:52Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
14,018,627
<p>You can see this: <a href="http://www.speech.kth.se/snack/" rel="nofollow">http://www.speech.kth.se/snack/</a></p> <pre><code>s = Sound() s.read('sound.wav') s.play() </code></pre>
4
2012-12-24T07:48:54Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
20,746,883
<p>In <a href="http://pydub.com" rel="nofollow">pydub</a> we've recently <a href="https://github.com/jiaaro/pydub/blob/master/pydub/playback.py" rel="nofollow">opted to use ffplay (via subprocess)</a> from the ffmpeg suite of tools, which internally uses SDL.</p> <p>It works for our purposes – mainly just making it ...
8
2013-12-23T15:54:08Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
22,689,253
<p>If you're on OSX, you can use the "os" module or "subprocess" etc. to call the OSX "play" command. From the OSX shell, it looks like </p> <p>play "bah.wav"</p> <p>It starts to play in about a half-second on my machine.</p>
0
2014-03-27T13:33:35Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
28,248,604
<p>Also on OSX - from <a href="http://stackoverflow.com/questions/3498313/how-to-trigger-from-python-playing-of-a-wav-or-mp3-audio-file-on-a-mac">SO</a>, using OSX's <a href="https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/afplay.1.html" rel="nofollow">afplay</a> command:</p> <pre>...
2
2015-01-31T05:45:03Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
30,141,027
<p>VLC has some nice python bindings here, for me this worked better than pyglet, at least on Mac OS:</p> <p><a href="https://wiki.videolan.org/Python_bindings" rel="nofollow">https://wiki.videolan.org/Python_bindings</a></p> <p>But it does rely on the VLC application, unfortunately</p>
1
2015-05-09T14:22:11Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
34,179,010
<p>Sorry for the late reply, but I think this is a good place to advertise my library ...</p> <p>AFAIK, the standard library has only one module for playing audio: <a href="https://docs.python.org/3/library/ossaudiodev.html" rel="nofollow">ossaudiodev</a>. Sadly, this only works on Linux and FreeBSD.</p> <p>UPDATE: T...
3
2015-12-09T12:37:18Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
34,568,298
<p>It is possible to play audio in OS X without any 3rd party libraries using an analogue of the following code. The raw audio data can be input with wave_wave.writeframes. This code extracts 4 seconds of audio from the input file.</p> <pre><code>import wave import io from AppKit import NSSound wave_output = io.Byte...
1
2016-01-02T16:53:43Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
34,984,200
<p><a href="http://stackoverflow.com/a/34568298/901641">Aaron's answer</a> appears to be about 10x more complicated than necessary. Just do this if you only need an answer that works on OS X:</p> <pre><code>from AppKit import NSSound sound = NSSound.alloc() sound.initWithContentsOfFile_byReference_('/path/to/file.wav...
1
2016-01-25T02:24:09Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
36,115,093
<p>Simply You can do it with the help of cvlc- I did it in this way:</p> <pre><code>import os os.popen2("cvlc /home/maulo/selfProject/task.mp3 --play-and-exit") </code></pre> <p>/home/maulo/selfProject/task.mp3. This is the location of my mp3 file. with the help of "--play-and-exit" you will be able to play again th...
0
2016-03-20T14:41:10Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
36,284,043
<p>Take a look at <a href="http://simpleaudio.readthedocs.org/en/latest/index.html" rel="nofollow">Simpleaudio</a>, which is a relatively recent and lightweight library for this purpose:</p> <pre><code>&gt; pip install simpleaudio </code></pre> <p>Then:</p> <pre><code>import simpleaudio as sa wave_obj = sa.WaveObje...
1
2016-03-29T12:17:17Z
[ "python", "audio" ]
Play audio with Python
260,738
<p>How can I play audio (it would be like a 1 second sound) from a Python script?</p> <p>It would be best if it was platform independent, but firstly it needs to work on a Mac.</p> <p>I know I could just execute the <code>afplay file.mp3</code> command from within Python, but is it possible to do it in raw Python? I ...
63
2008-11-04T03:11:03Z
37,501,920
<p>Throwing in another library into the bucket:</p> <p><a href="https://github.com/bastibe/PySoundCard" rel="nofollow">https://github.com/bastibe/PySoundCard</a></p> <p>PySoundCard uses PortAudio for playback which is available on many platforms. In addition, it recognizes "professional" sound devices with lots of ch...
0
2016-05-28T17:30:18Z
[ "python", "audio" ]
How to build Python C extension modules with autotools
261,500
<p>Most of the documentation available for building Python extension modules uses distutils, but I would like to achieve this by using the appropriate python autoconf &amp; automake macros instead.</p> <p>I'd like to know if there is an open source project out there that does exactly this. Most of the ones I've found ...
7
2008-11-04T10:46:43Z
261,839
<p>All PyGTK extensions use autotools, so if the PyGTK aspects don't kill the whole thing for you, it might be worth having a look at the <a href="http://svn.gnome.org/viewvc/pygtk/trunk/" rel="nofollow">PyGTK source code</a>. Additionally, <a href="http://bazaar.launchpad.net/~poppler-python/poppler-python/trunk/files...
4
2008-11-04T13:10:44Z
[ "c++", "python", "c", "autotools" ]
How to build Python C extension modules with autotools
261,500
<p>Most of the documentation available for building Python extension modules uses distutils, but I would like to achieve this by using the appropriate python autoconf &amp; automake macros instead.</p> <p>I'd like to know if there is an open source project out there that does exactly this. Most of the ones I've found ...
7
2008-11-04T10:46:43Z
13,711,218
<p>Supposing that you have a project with a directory called <code>src</code>, so let's follow the follow steps to get a python extension built and packaged using autotools:</p> <h2>Create the Makefile.am files</h2> <p>First, you need to create one Makefile.am in the root of your project, basically (but not exclusive...
11
2012-12-04T20:22:22Z
[ "c++", "python", "c", "autotools" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
261,645
<p>Python, being a byte-code-compiled interpreted language, is very difficult to lock down. Even if you use a exe-packager like <a href="http://py2exe.org">py2exe</a>, the layout of the executable is well-known, and the Python byte-codes are well understood.</p> <p>Usually in cases like this, you have to make a trade...
277
2008-11-04T12:00:34Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
261,704
<p>You should take a look at how the guys at getdropbox.com do it for their client software, including Linux. It's quite tricky to crack and requires some quite creative disassembly to get past the protection mechanisms.</p>
5
2008-11-04T12:20:21Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
261,719
<p>Is your employer aware that he can "steal" back any ideas that other people get from your code? I mean, if they can read your work, so can you theirs. Maybe looking at how you can benefit from the situation would yield a better return of your investment than fearing how much you could lose.</p> <p>[EDIT] Answer to ...
30
2008-11-04T12:27:52Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
261,723
<p>I have looked at software protection in general for my own projects and the general philosophy is that complete protection is impossible. The only thing that you can hope to achieve is to add protection to a level that would cost your customer more to bypass than it would to purchase another license.</p> <p>With t...
4
2008-11-04T12:28:38Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
261,727
<p>"Is there a good way to handle this problem?" No. Nothing can be protected against reverse engineering. Even the firmware on DVD machines has been reverse engineered and <a href="http://en.wikipedia.org/wiki/AACS%5Fencryption%5Fkey%5Fcontroversy">AACS Encryption key</a> exposed. And that's in spite of the DMCA m...
395
2008-11-04T12:29:24Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
261,728
<p>In some circumstances, it may be possible to move (all, or at least a key part) of the software into a web service that your organization hosts.</p> <p>That way, the license checks can be performed in the safety of your own server room.</p>
12
2008-11-04T12:29:48Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
261,797
<p>Depending in who the client is, a simple protection mechanism, combined with a sensible license agreement will be <em>far</em> more effective than any complex licensing/encryption/obfuscation system.</p> <p>The best solution would be selling the code as a service, say by hosting the service, or offering support - a...
8
2008-11-04T12:53:25Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
261,808
<p>What about signing your code with standard encryption schemes by hashing and signing important files and checking it with public key methods?</p> <p>In this way you can issue license file with a public key for each customer.</p> <p>Additional you can use an python obfuscator like <a href="http://www.lysator.liu.se...
7
2008-11-04T12:59:04Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
261,817
<h1>Python is not the tool you need</h1> <p>You must use the right tool to do the right thing, and Python was not designed to be obfuscated. It's the contrary; everything is open or easy to reveal or modify in Python because that's the language's philosophy.</p> <p>If you want something you can't see through, look fo...
289
2008-11-04T13:03:06Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
262,895
<p>The best you can do with Python is to obscure things.</p> <ul> <li>Strip out all docstrings</li> <li>Distribute only the .pyc compiled files.</li> <li>freeze it</li> <li>Obscure your constants inside a class/module so that help(config) doesn't show everything</li> </ul> <p>You may be able to add some additional ob...
5
2008-11-04T18:45:18Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
262,937
<p>Do not rely on obfuscation. As You have correctly concluded, it offers very limited protection. UPDATE: Here is a <a href="https://www.usenix.org/system/files/conference/woot13/woot13-kholia.pdf">link to paper</a> which reverse engineered obfuscated python code in Dropbox. The approach - opcode remapping is a good b...
19
2008-11-04T18:53:11Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
263,314
<p>The reliable only way to protect code is to run it on a server you control and provide your clients with a client which interfaces with that server.</p>
7
2008-11-04T20:27:05Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
264,450
<p>Though there's no perfect solution, the following can be done:</p> <ol> <li>Move some critical piece of startup code into a native library.</li> <li>Enforce the license check in the native library.</li> </ol> <p>If the call to the native code were to be removed, the program wouldn't start anyway. If it's not remov...
13
2008-11-05T06:10:26Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
267,875
<p>I understand that you want your customers to use the power of python but do not want expose the source code.</p> <p>Here are my suggestions:</p> <p>(a) Write the critical pieces of the code as C or C++ libraries and then use <a href="http://www.riverbankcomputing.co.uk/software/sip/intro">SIP</a> or <a href="http:...
52
2008-11-06T07:41:59Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
554,565
<p>Shipping .pyc files has its problems - they are not compatible with any other python version than the python version they were created with, which means you must know which python version is running on the systems the product will run on. That's a very limiting factor.</p>
11
2009-02-16T21:09:33Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
827,080
<p>Another attempt to make your code harder to steal is to use jython and then use <a href="http://proguard.sourceforge.net/">java obfuscator</a>. </p> <p>This should work pretty well as jythonc translate python code to java and then java is compiled to bytecode. So ounce you obfuscate the classes it will be really ha...
8
2009-05-05T21:53:15Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
2,987,179
<p>Idea of having time restricted license and check for it in locally installed program will not work. Even with perfect obfuscation, license check can be removed. However if you check license on remote system and run significant part of the program on your closed remote system, you will be able to protect your IP.</p>...
4
2010-06-07T05:07:06Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
7,347,168
<h2>Compile python and distribute binaries!</h2> <p><strong>Sensible idea:</strong> </p> <p>Use <a href="http://cython.org/">Cython</a> (or something similar) to compile python to C code, then distribute your app as python binary libraries (pyd) instead.</p> <p>That way, no Python (byte) code is left and you've done...
83
2011-09-08T11:14:04Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
9,547,169
<p>using cxfreeze ( py2exe for linux ) will do the job.</p> <p><a href="http://cx-freeze.sourceforge.net/" rel="nofollow">http://cx-freeze.sourceforge.net/</a></p> <p>it is available in ubuntu repositories</p>
3
2012-03-03T15:13:13Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
10,593,293
<p><a href="http://www.bitboost.com" rel="nofollow">www.bitboost.com</a> offers a python obfuscator. Full disclosure: the author is a friend of mine.</p>
4
2012-05-15T02:00:40Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
11,315,793
<p>I think there is one more method to protect your Python code; part of the Obfuscation method. I beleive there was a game like Mount and Blade or something that changed and recompiled their own python interpreter (the original interpreter which i believe is open source) and just changed the OP codes in the OP code ta...
8
2012-07-03T17:07:27Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
16,695,056
<p>It is possible to have the py2exe byte-code in a crypted resource for a C launcher that loads and executes it in memory. Some ideas <a href="http://evilzone.org/hacking-and-security/encrypting-programs-how-does-that-work/10/?wap2" rel="nofollow">here</a> and <a href="http://bubblews.com/news/430170-crypter-small-gui...
3
2013-05-22T14:53:50Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
How do I protect Python code?
261,638
<p>I am developing a piece of software in Python that will be distributed to my employer's customers. My employer wants to limit the usage of the software with a time restricted license file.</p> <p>If we distribute the .py files or even .pyc files it will be easy to (decompile and) remove the code that checks the lic...
408
2008-11-04T11:57:27Z
30,121,460
<p>Have you had a look at <a href="https://liftoff.github.io/pyminifier/">pyminifier</a>? It does Minify, obfuscate, and compress Python code. The example code looks pretty nasty for casual reverse engineering.</p> <pre><code>$ pyminifier --nonlatin --replacement-length=50 /tmp/tumult.py #!/usr/bin/env python3 ﺭ异í...
9
2015-05-08T10:21:58Z
[ "python", "licensing", "obfuscation", "copy-protection" ]
Converting a List of Tuples into a Dict in Python
261,655
<p>I have a list of tuples like this:</p> <pre><code>[ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ] </code></pre> <p>I want to iterate through this keying by the first item, so for example I could print something like this:</p> <pre><code>a 1 2 3 b 1 2 c 1 </code></pre> <p>How would I go about doin...
29
2008-11-04T12:03:34Z
261,665
<pre><code>l = [ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ] d = {} for x, y in l: d.setdefault(x, []).append(y) print d </code></pre> <p>produces:</p> <pre><code>{'a': [1, 2, 3], 'c': [1], 'b': [1, 2]} </code></pre>
27
2008-11-04T12:07:21Z
[ "python", "data-structures", "iteration" ]
Converting a List of Tuples into a Dict in Python
261,655
<p>I have a list of tuples like this:</p> <pre><code>[ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ] </code></pre> <p>I want to iterate through this keying by the first item, so for example I could print something like this:</p> <pre><code>a 1 2 3 b 1 2 c 1 </code></pre> <p>How would I go about doin...
29
2008-11-04T12:03:34Z
261,677
<p>Slightly simpler...</p> <pre><code>&gt;&gt;&gt; from collections import defaultdict &gt;&gt;&gt; fq= defaultdict( list ) &gt;&gt;&gt; for n,v in myList: fq[n].append(v) &gt;&gt;&gt; fq defaultdict(&lt;type 'list'&gt;, {'a': [1, 2, 3], 'c': [1], 'b': [1, 2]}) </code></pre>
23
2008-11-04T12:11:52Z
[ "python", "data-structures", "iteration" ]
Converting a List of Tuples into a Dict in Python
261,655
<p>I have a list of tuples like this:</p> <pre><code>[ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ] </code></pre> <p>I want to iterate through this keying by the first item, so for example I could print something like this:</p> <pre><code>a 1 2 3 b 1 2 c 1 </code></pre> <p>How would I go about doin...
29
2008-11-04T12:03:34Z
261,766
<p>A solution using groupby</p> <pre><code> &gt;&gt;&gt; from itertools import groupby &gt;&gt;&gt; l = [('a',1), ('a', 2),('a', 3),('b', 1),('b', 2),('c', 1),] &gt;&gt;&gt; [(label, [v for l,v in value]) for (label, value) in groupby(l, lambda x:x[0])] [('a', [1, 2, 3]), ('b', [1, 2]), ('c', [1])] </co...
8
2008-11-04T12:42:32Z
[ "python", "data-structures", "iteration" ]
Converting a List of Tuples into a Dict in Python
261,655
<p>I have a list of tuples like this:</p> <pre><code>[ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ] </code></pre> <p>I want to iterate through this keying by the first item, so for example I could print something like this:</p> <pre><code>a 1 2 3 b 1 2 c 1 </code></pre> <p>How would I go about doin...
29
2008-11-04T12:03:34Z
262,530
<p>I would just do the basic</p> <pre> answer = {} for key, value in list_of_tuples: if key in answer: answer[key].append(value) else: answer[key] = [value] </pre> <p>If it's this short, why use anything complicated. Of course if you don't mind using setdefault that's okay too.</p>
1
2008-11-04T17:06:28Z
[ "python", "data-structures", "iteration" ]
Converting a List of Tuples into a Dict in Python
261,655
<p>I have a list of tuples like this:</p> <pre><code>[ ('a', 1), ('a', 2), ('a', 3), ('b', 1), ('b', 2), ('c', 1), ] </code></pre> <p>I want to iterate through this keying by the first item, so for example I could print something like this:</p> <pre><code>a 1 2 3 b 1 2 c 1 </code></pre> <p>How would I go about doin...
29
2008-11-04T12:03:34Z
1,694,768
<h3>Print list of tuples grouping by the first item</h3> <p>This answer is based on <a href="http://stackoverflow.com/questions/261655/converting-a-list-of-tuples-into-a-dict-in-python/261766#261766">the @gommen one</a>.</p> <pre><code>#!/usr/bin/env python from itertools import groupby from operator import itemget...
1
2009-11-07T23:11:33Z
[ "python", "data-structures", "iteration" ]
How do I safely decode a degrees symbol in a wxPython app?
262,249
<p>I have a debug app I've been writing which receives data from a C-based process via UDP. One of the strings sent to me contains a <code>°</code> character - <a href="http://en.wikipedia.org/wiki/Degree_symbol" rel="nofollow">Unicode U+00B0</a> (which incidentally breaks the StackOverflow search function!). When my ...
0
2008-11-04T16:07:01Z
262,433
<p>I can't say mych about wxPython itself, but I am guessing that it is trying to convert the text to Unicode before displaying it, If you have a string like <code>'123\xB0'</code> and try to convert it to Unicode with teh default encoding (ASCII) then it will throw <code>UnicodeDecodeError</code>. You can probably fix...
1
2008-11-04T16:48:22Z
[ "python", "unicode", "wxpython", "decode", "textctrl" ]
How do I safely decode a degrees symbol in a wxPython app?
262,249
<p>I have a debug app I've been writing which receives data from a C-based process via UDP. One of the strings sent to me contains a <code>°</code> character - <a href="http://en.wikipedia.org/wiki/Degree_symbol" rel="nofollow">Unicode U+00B0</a> (which incidentally breaks the StackOverflow search function!). When my ...
0
2008-11-04T16:07:01Z
263,330
<p>pdc got it right, the following works fine (but fails without the <code>decode</code>):</p> <pre><code>#!/usr/bin/env python # -*- coding: utf-8 -*- import wx app = wx.PySimpleApp() app.TopWindow = wx.Frame(None) field = wx.TextCtrl(app.TopWindow) field.Value += '°'.decode('ISO8859-1') app.TopWindow.Show() app.Ma...
2
2008-11-04T20:34:13Z
[ "python", "unicode", "wxpython", "decode", "textctrl" ]
How do I safely decode a degrees symbol in a wxPython app?
262,249
<p>I have a debug app I've been writing which receives data from a C-based process via UDP. One of the strings sent to me contains a <code>°</code> character - <a href="http://en.wikipedia.org/wiki/Degree_symbol" rel="nofollow">Unicode U+00B0</a> (which incidentally breaks the StackOverflow search function!). When my ...
0
2008-11-04T16:07:01Z
13,496,753
<p>Things may have been different back when this was asked, but my thoughts for anyone who stumbles on this:</p> <p>The issue is wxPython is trying to convert TO unicode, and lacking charset information it tries to use ASCII, which is invalid. If you know your data is utf-8, tell it so and it'll just work.</p> <pre>...
0
2012-11-21T15:42:08Z
[ "python", "unicode", "wxpython", "decode", "textctrl" ]
making a programme run indefinitely in python
262,460
<p>Is there any way to make a function (the ones I'm thinking of are in the style of the simple ones I've made which generate the fibonnacci sequence from 0 to a point, and all the primes between two points) run indefinitely. E.g. until I press a certain key or until a time has passed, rather than until a number reac...
0
2008-11-04T16:53:45Z
262,474
<p>As in almost all languages:</p> <pre><code>while True: # check what you want and eventually break print nextValue() </code></pre> <p>The second part of your question is more interesting:</p> <blockquote> <p>Also, if it is based on time then is there anyway I could just extend the time and start it going fro...
0
2008-11-04T16:55:50Z
[ "python", "time", "key", "indefinite" ]
making a programme run indefinitely in python
262,460
<p>Is there any way to make a function (the ones I'm thinking of are in the style of the simple ones I've made which generate the fibonnacci sequence from 0 to a point, and all the primes between two points) run indefinitely. E.g. until I press a certain key or until a time has passed, rather than until a number reac...
0
2008-11-04T16:53:45Z
262,490
<p>The simplest way is just to write a program with an infinite loop, and then hit control-C to stop it. Without more description it's hard to know if this works for you.</p> <p>If you do it time-based, you don't need a generator. You can just have it pause for user input, something like a "Continue? [y/n]", read from...
1
2008-11-04T16:58:14Z
[ "python", "time", "key", "indefinite" ]
making a programme run indefinitely in python
262,460
<p>Is there any way to make a function (the ones I'm thinking of are in the style of the simple ones I've made which generate the fibonnacci sequence from 0 to a point, and all the primes between two points) run indefinitely. E.g. until I press a certain key or until a time has passed, rather than until a number reac...
0
2008-11-04T16:53:45Z
262,520
<p>If you use a child thread to run the function while the main thread waits for character input it should work. Just remember to have something that stops the child thread (in the example below the global runthread)</p> <p>For example:</p> <pre><code>import threading, time runthread = 1 def myfun(): while runthr...
0
2008-11-04T17:04:28Z
[ "python", "time", "key", "indefinite" ]
making a programme run indefinitely in python
262,460
<p>Is there any way to make a function (the ones I'm thinking of are in the style of the simple ones I've made which generate the fibonnacci sequence from 0 to a point, and all the primes between two points) run indefinitely. E.g. until I press a certain key or until a time has passed, rather than until a number reac...
0
2008-11-04T16:53:45Z
262,542
<p>If you really want your function to run and still wants user (or system) input, you have two solutions:</p> <ol> <li>multi-thread</li> <li>multi-process</li> </ol> <p>It will depend on how fine the interaction. If you just want to interrupt the function and don't care about the exit, then multi-process is fine.</p...
0
2008-11-04T17:10:49Z
[ "python", "time", "key", "indefinite" ]
making a programme run indefinitely in python
262,460
<p>Is there any way to make a function (the ones I'm thinking of are in the style of the simple ones I've made which generate the fibonnacci sequence from 0 to a point, and all the primes between two points) run indefinitely. E.g. until I press a certain key or until a time has passed, rather than until a number reac...
0
2008-11-04T16:53:45Z
262,852
<p>If you want to exit based on time, you can use the signal module's alarm(time) function, and the catch the SIGALRM - here's an example <a href="http://docs.python.org/library/signal.html#example" rel="nofollow">http://docs.python.org/library/signal.html#example</a></p> <p>You can let the user interrupt the program ...
0
2008-11-04T18:29:31Z
[ "python", "time", "key", "indefinite" ]
making a programme run indefinitely in python
262,460
<p>Is there any way to make a function (the ones I'm thinking of are in the style of the simple ones I've made which generate the fibonnacci sequence from 0 to a point, and all the primes between two points) run indefinitely. E.g. until I press a certain key or until a time has passed, rather than until a number reac...
0
2008-11-04T16:53:45Z
263,876
<p>You could do something like this to generate fibonnacci numbers for 1 second then stop.</p> <pre><code>fibonnacci = [1,1] stoptime = time.time() + 1 # set stop time to 1 second in the future while time.time() &lt; stoptime: fibonnacci.append(fibonnacci[-1]+fibonnacci[-2]) print "Generated %s numbers, the last on...
-1
2008-11-04T23:27:14Z
[ "python", "time", "key", "indefinite" ]
making a programme run indefinitely in python
262,460
<p>Is there any way to make a function (the ones I'm thinking of are in the style of the simple ones I've made which generate the fibonnacci sequence from 0 to a point, and all the primes between two points) run indefinitely. E.g. until I press a certain key or until a time has passed, rather than until a number reac...
0
2008-11-04T16:53:45Z
264,614
<p>You could use a generator for this:</p> <pre><code>def finished(): "Define your exit condition here" return ... def count(i=0): while not finished(): yield i i += 1 for i in count(): print i </code></pre> <p>If you want to change the exit condition you could pass a value back into...
0
2008-11-05T08:33:49Z
[ "python", "time", "key", "indefinite" ]
Is it possible to change the Environment of a parent process in python?
263,005
<p>In Linux When I invoke python from the shell it replicates its environment, and starts the python process. Therefore if I do something like the following:</p> <pre><code>import os os.environ["FOO"] = "A_Value" </code></pre> <p>When the python process returns, FOO, assuming it was undefined originally, will still b...
11
2008-11-04T19:08:16Z
263,022
<p>It's not possible, for any child process, to change the environment of the parent process. The best you can do is to output shell statements to stdout that you then source, or write it to a file that you source in the parent.</p>
11
2008-11-04T19:12:44Z
[ "python", "linux", "environment" ]
Is it possible to change the Environment of a parent process in python?
263,005
<p>In Linux When I invoke python from the shell it replicates its environment, and starts the python process. Therefore if I do something like the following:</p> <pre><code>import os os.environ["FOO"] = "A_Value" </code></pre> <p>When the python process returns, FOO, assuming it was undefined originally, will still b...
11
2008-11-04T19:08:16Z
263,068
<p>No process can change its parent process (or any other existing process' environment).</p> <p>You can, however, create a new environment by creating a new interactive shell with the modified environment.</p> <p>You have to spawn a new copy of the shell that uses the upgraded environment and has access to the exist...
9
2008-11-04T19:27:27Z
[ "python", "linux", "environment" ]
Is it possible to change the Environment of a parent process in python?
263,005
<p>In Linux When I invoke python from the shell it replicates its environment, and starts the python process. Therefore if I do something like the following:</p> <pre><code>import os os.environ["FOO"] = "A_Value" </code></pre> <p>When the python process returns, FOO, assuming it was undefined originally, will still b...
11
2008-11-04T19:08:16Z
263,162
<p>I would use the bash eval statement, and have the python script output the shell code</p> <p>child.py:</p> <pre><code>#!/usr/bin/env python print 'FOO="A_Value"' </code></pre> <p>parent.sh</p> <pre><code>#!/bin/bash eval `./child.py` </code></pre>
7
2008-11-04T19:41:03Z
[ "python", "linux", "environment" ]
Creating a python win32 service
263,296
<p>I am currently trying to create a win32 service using pywin32. My main point of reference has been this tutorial:</p> <p><a href="http://code.activestate.com/recipes/551780/">http://code.activestate.com/recipes/551780/</a></p> <p>What i don't understand is the initialization process, since the Daemon is never init...
11
2008-11-04T20:23:17Z
264,871
<p>I've never used these APIs, but digging through the code, it looks like the class passed in is used to register the name of the class in the registry, so you can't do any initialization of your own. But there's a method called GetServiceCustomOption that may help:</p> <p><a href="http://mail.python.org/pipermail/p...
5
2008-11-05T11:18:14Z
[ "python", "winapi", "pywin32" ]
Creating a python win32 service
263,296
<p>I am currently trying to create a win32 service using pywin32. My main point of reference has been this tutorial:</p> <p><a href="http://code.activestate.com/recipes/551780/">http://code.activestate.com/recipes/551780/</a></p> <p>What i don't understand is the initialization process, since the Daemon is never init...
11
2008-11-04T20:23:17Z
900,775
<p>I just create a simple "how to" where the program is in one module and the service is in another place, it uses py2exe to create the win32 service, which I believe is the best you can do for your users that don't want to mess with the python interpreter or other dependencies.</p> <p>You can check my tutorial here: ...
9
2009-05-23T03:20:19Z
[ "python", "winapi", "pywin32" ]
Merging/adding lists in Python
263,457
<p>I'm pretty sure there should be a more Pythonic way of doing this - but I can't think of one: How can I merge a two-dimensional list into a one-dimensional list? Sort of like zip/map but with more than two iterators.</p> <p>Example - I have the following list:</p> <pre><code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9...
33
2008-11-04T21:06:36Z
263,465
<pre><code>[sum(a) for a in zip(*array)] </code></pre>
68
2008-11-04T21:16:39Z
[ "python", "list" ]
Merging/adding lists in Python
263,457
<p>I'm pretty sure there should be a more Pythonic way of doing this - but I can't think of one: How can I merge a two-dimensional list into a one-dimensional list? Sort of like zip/map but with more than two iterators.</p> <p>Example - I have the following list:</p> <pre><code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9...
33
2008-11-04T21:06:36Z
263,523
<p>[sum(value) for value in zip(*array)] is pretty standard.</p> <p>This might help you understand it:</p> <pre><code>In [1]: array=[[1, 2, 3], [4, 5, 6], [7, 8, 9]] In [2]: array Out[2]: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] In [3]: *array ------------------------------------------------------------ File "&lt;ipyth...
62
2008-11-04T21:33:47Z
[ "python", "list" ]
Merging/adding lists in Python
263,457
<p>I'm pretty sure there should be a more Pythonic way of doing this - but I can't think of one: How can I merge a two-dimensional list into a one-dimensional list? Sort of like zip/map but with more than two iterators.</p> <p>Example - I have the following list:</p> <pre><code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9...
33
2008-11-04T21:06:36Z
265,472
<p>If you're doing a lot of this kind of thing, you want to learn about <a href="http://scipy.org/"><code>scipy</code>.</a></p> <pre><code>&gt;&gt;&gt; import scipy &gt;&gt;&gt; sum(scipy.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) array([12, 15, 18]) </code></pre> <p>All array sizes are checked for you automatically. ...
8
2008-11-05T15:24:08Z
[ "python", "list" ]
Merging/adding lists in Python
263,457
<p>I'm pretty sure there should be a more Pythonic way of doing this - but I can't think of one: How can I merge a two-dimensional list into a one-dimensional list? Sort of like zip/map but with more than two iterators.</p> <p>Example - I have the following list:</p> <pre><code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9...
33
2008-11-04T21:06:36Z
265,495
<p>An alternative way:</p> <pre><code>map(sum, zip(*array)) </code></pre>
13
2008-11-05T15:31:04Z
[ "python", "list" ]
Merging/adding lists in Python
263,457
<p>I'm pretty sure there should be a more Pythonic way of doing this - but I can't think of one: How can I merge a two-dimensional list into a one-dimensional list? Sort of like zip/map but with more than two iterators.</p> <p>Example - I have the following list:</p> <pre><code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9...
33
2008-11-04T21:06:36Z
5,113,719
<p>Agree with fivebells, but you could also use Numpy, which is a smaller (quicker import) and more generic implementation of array-like stuff. (actually, it is a dependency of scipy). These are great tools which, as have been said, are a 'must use' if you deal with this kind of manipulations.</p>
3
2011-02-25T04:45:31Z
[ "python", "list" ]
Merging/adding lists in Python
263,457
<p>I'm pretty sure there should be a more Pythonic way of doing this - but I can't think of one: How can I merge a two-dimensional list into a one-dimensional list? Sort of like zip/map but with more than two iterators.</p> <p>Example - I have the following list:</p> <pre><code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9...
33
2008-11-04T21:06:36Z
13,240,341
<p>Late to the game, and it's not as good of an answer as some of the others, but I thought it was kind of cute:</p> <pre><code>map(lambda *x:sum(x),*array) </code></pre> <p>it's too bad that <code>sum(1,2,3)</code> doesn't work. If it did, we could eliminate the silly <code>lambda</code> in there, but I suppose tha...
2
2012-11-05T21:04:10Z
[ "python", "list" ]
Merging/adding lists in Python
263,457
<p>I'm pretty sure there should be a more Pythonic way of doing this - but I can't think of one: How can I merge a two-dimensional list into a one-dimensional list? Sort of like zip/map but with more than two iterators.</p> <p>Example - I have the following list:</p> <pre><code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9...
33
2008-11-04T21:06:36Z
27,910,105
<p>It's not concise but it does work for adding lists of any size i.e. the list lengths do not match.</p> <pre><code>def add_vectors(v1, v2): """ takes two lists of ints of any size and returns a third list equal to the sum of their elements. """ vsum = [] v1len = len(v1) v2len = len(v2) ...
0
2015-01-12T20:21:00Z
[ "python", "list" ]
Merging/adding lists in Python
263,457
<p>I'm pretty sure there should be a more Pythonic way of doing this - but I can't think of one: How can I merge a two-dimensional list into a one-dimensional list? Sort of like zip/map but with more than two iterators.</p> <p>Example - I have the following list:</p> <pre><code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9...
33
2008-11-04T21:06:36Z
29,433,541
<blockquote> <p>[sum(a) for a in zip(*array)]</p> </blockquote> <p>I like that. I needed something related for interleaving objects in to a list of items, came up with something similar but more concise for even length lists:</p> <pre><code>sum(zip(*array),()) </code></pre> <p>for example, interleaving two lists:...
0
2015-04-03T13:55:00Z
[ "python", "list" ]
Merging/adding lists in Python
263,457
<p>I'm pretty sure there should be a more Pythonic way of doing this - but I can't think of one: How can I merge a two-dimensional list into a one-dimensional list? Sort of like zip/map but with more than two iterators.</p> <p>Example - I have the following list:</p> <pre><code>array = [[1, 2, 3], [4, 5, 6], [7, 8, 9...
33
2008-11-04T21:06:36Z
29,439,639
<p>You can simply do this:</p> <pre><code>print [sum(x) for x in zip(*array)] </code></pre> <p>If you wish to iterate through lists in this fashion, you can use <code>chain</code> of the <code>itertools</code> module:</p> <pre><code>from itertools import chain for x in array.chain.from_iterable(zip(*array)): pr...
0
2015-04-03T21:02:37Z
[ "python", "list" ]
We have a graphical designer, now they want a text based designer. Suggestions?
263,550
<p>I'm sorry I could not think of a better title.</p> <p>The problem is the following:</p> <p>For our customer we have created (as part of a larger application) a graphical designer which they can use to build "scenario's".</p> <p>These scenario's consist of "Composites" which in turn consist of "Commands". These co...
4
2008-11-04T21:39:36Z
263,583
<p>From what i think i've understood you have two options</p> <p>you could either use an XML style "markup" to let them define entities and their groupings, but that may not be best.</p> <p>Your alternatives are yes, yoou could embedd a language, but do you really need to, wouldnt that be overkill, and how can you co...
4
2008-11-04T21:47:19Z
[ "c#", "python", "scripting", "parsing", "embedding" ]
We have a graphical designer, now they want a text based designer. Suggestions?
263,550
<p>I'm sorry I could not think of a better title.</p> <p>The problem is the following:</p> <p>For our customer we have created (as part of a larger application) a graphical designer which they can use to build "scenario's".</p> <p>These scenario's consist of "Composites" which in turn consist of "Commands". These co...
4
2008-11-04T21:39:36Z
263,664
<p>This looks like a perfect scenario for a simple DSL. See <a href="http://msdn.microsoft.com/en-us/library/bb126235(VS.80).aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/bb126235(VS.80).aspx</a> for some information.</p> <p>You could also use a scripting language such as lua.Net. </p>
2
2008-11-04T22:08:12Z
[ "c#", "python", "scripting", "parsing", "embedding" ]
We have a graphical designer, now they want a text based designer. Suggestions?
263,550
<p>I'm sorry I could not think of a better title.</p> <p>The problem is the following:</p> <p>For our customer we have created (as part of a larger application) a graphical designer which they can use to build "scenario's".</p> <p>These scenario's consist of "Composites" which in turn consist of "Commands". These co...
4
2008-11-04T21:39:36Z
263,675
<p>If you really just want a dirt simple language, you want a 'recursive descent parser'.</p> <p>For example, a language like this:</p> <pre><code>SCENARIO MyScenario DELAY 1 COUNT 1 ADD 1 DIRECT_POWER 23, False, 150 WAIT 3 ... END_SCENARIO </code></pre> <p>You might have a grammar like:</p> <pre><code>scenario :: ...
2
2008-11-04T22:11:58Z
[ "c#", "python", "scripting", "parsing", "embedding" ]
We have a graphical designer, now they want a text based designer. Suggestions?
263,550
<p>I'm sorry I could not think of a better title.</p> <p>The problem is the following:</p> <p>For our customer we have created (as part of a larger application) a graphical designer which they can use to build "scenario's".</p> <p>These scenario's consist of "Composites" which in turn consist of "Commands". These co...
4
2008-11-04T21:39:36Z
263,727
<p>Here's a Pythonic solution for building a DSL that you can use to compile and create byte code arrays.</p> <ol> <li><p>Write a simple module that makes your C# structures available to Python. The goal is to define each C# class that users are allowed to work with (Composites or Commands or whatever) as a Python cl...
1
2008-11-04T22:31:45Z
[ "c#", "python", "scripting", "parsing", "embedding" ]
We have a graphical designer, now they want a text based designer. Suggestions?
263,550
<p>I'm sorry I could not think of a better title.</p> <p>The problem is the following:</p> <p>For our customer we have created (as part of a larger application) a graphical designer which they can use to build "scenario's".</p> <p>These scenario's consist of "Composites" which in turn consist of "Commands". These co...
4
2008-11-04T21:39:36Z
263,742
<p>To add to S.Lott's comment, here's how you <a href="http://www.redmountainsw.com/wordpress/archives/embedding-ironpython-c-calling-python-script" rel="nofollow">eval a Python script from C# </a></p>
1
2008-11-04T22:36:41Z
[ "c#", "python", "scripting", "parsing", "embedding" ]
We have a graphical designer, now they want a text based designer. Suggestions?
263,550
<p>I'm sorry I could not think of a better title.</p> <p>The problem is the following:</p> <p>For our customer we have created (as part of a larger application) a graphical designer which they can use to build "scenario's".</p> <p>These scenario's consist of "Composites" which in turn consist of "Commands". These co...
4
2008-11-04T21:39:36Z
263,931
<p>While it might be great fun to create this mini-language and code it all up, the real questions you need to ask are:</p> <ol> <li>What is the business case for adding this feature / facility?</li> <li>Who is going to pay for this feature?</li> <li>Who is going to "sign off" on this feature if you build it?</li> </o...
0
2008-11-04T23:57:34Z
[ "c#", "python", "scripting", "parsing", "embedding" ]
What is a Ruby equivalent for Python's "zip" builtin?
263,623
<p>Is there any Ruby equivalent for Python's builtin <code>zip</code> function? If not, what is a concise way of doing the same thing?</p> <p>A bit of context: this came up when I was trying to find a clean way of doing a check involving two arrays. If I had <code>zip</code>, I could have written something like:</p> ...
14
2008-11-04T21:58:29Z
263,652
<p>Ruby has a zip function:</p> <pre><code>[1,2].zip([3,4]) =&gt; [[1,3],[2,4]] </code></pre> <p>so your code example is actually:</p> <pre><code>a.zip(b).all? {|pair| pair[0] === pair[1]} </code></pre> <p>or perhaps more succinctly:</p> <pre><code>a.zip(b).all? {|a,b| a === b } </code></pre>
21
2008-11-04T22:05:46Z
[ "python", "ruby", "translation" ]
What is a Ruby equivalent for Python's "zip" builtin?
263,623
<p>Is there any Ruby equivalent for Python's builtin <code>zip</code> function? If not, what is a concise way of doing the same thing?</p> <p>A bit of context: this came up when I was trying to find a clean way of doing a check involving two arrays. If I had <code>zip</code>, I could have written something like:</p> ...
14
2008-11-04T21:58:29Z
263,670
<p>Could you not do:</p> <pre><code>a.eql?(b) </code></pre> <p>Edited to add an example:</p> <pre><code>a = %w[a b c] b = %w[1 2 3] c = ['a', 'b', 'c'] a.eql?(b) # =&gt; false a.eql?(c) # =&gt; true a.eql?(c.reverse) # =&gt; false </code></pre>
0
2008-11-04T22:09:07Z
[ "python", "ruby", "translation" ]
What is a Ruby equivalent for Python's "zip" builtin?
263,623
<p>Is there any Ruby equivalent for Python's builtin <code>zip</code> function? If not, what is a concise way of doing the same thing?</p> <p>A bit of context: this came up when I was trying to find a clean way of doing a check involving two arrays. If I had <code>zip</code>, I could have written something like:</p> ...
14
2008-11-04T21:58:29Z
266,848
<p>This is from the ruby spec:</p> <pre><code>it "returns true if other has the same length and each pair of corresponding elements are eql" do a = [1, 2, 3, 4] b = [1, 2, 3, 4] a.should eql(b) [].should eql([]) end </code></pre> <p>So you should it should work for the example you mentioned.</p> <p>I...
-2
2008-11-05T21:49:58Z
[ "python", "ruby", "translation" ]
How can I ask for root password but perform the action at a later time?
263,773
<p>I have a python script that I would like to add a "Shutdown when done" feature to.</p> <p>I know I can use gksudo (when the user clicks on "shutdown when done") to ask the user for root privileges but how can I use those privileges at a later time (when the script is actually finished).</p> <p>I have thought about...
7
2008-11-04T22:47:39Z
263,804
<p>gksudo should have a timeout, I believe it's from the time you last executed a gksudo command.</p> <p>So I think I'd just throw out a "gksudo echo meh" or something every minute. Should reset the timer and keep you active until you reboot.</p>
3
2008-11-04T23:02:05Z
[ "python", "linux", "ubuntu" ]
How can I ask for root password but perform the action at a later time?
263,773
<p>I have a python script that I would like to add a "Shutdown when done" feature to.</p> <p>I know I can use gksudo (when the user clicks on "shutdown when done") to ask the user for root privileges but how can I use those privileges at a later time (when the script is actually finished).</p> <p>I have thought about...
7
2008-11-04T22:47:39Z
263,851
<p>Escalate priority, spawn (<code>fork (2)</code>) a separate process that will <code>wait (2)</code>, and drop priority in the main process.</p>
1
2008-11-04T23:18:05Z
[ "python", "linux", "ubuntu" ]
How can I ask for root password but perform the action at a later time?
263,773
<p>I have a python script that I would like to add a "Shutdown when done" feature to.</p> <p>I know I can use gksudo (when the user clicks on "shutdown when done") to ask the user for root privileges but how can I use those privileges at a later time (when the script is actually finished).</p> <p>I have thought about...
7
2008-11-04T22:47:39Z
263,859
<p>Instead of <code>chmod u+s</code>ing the shutdown command, allowing passwordless sudo access to that command would be better..</p> <p>As for allowing shutdown at the end of the script, I suppose you could run the entire script with sudo, then drop privileges to the initial user at the start of the script?</p>
4
2008-11-04T23:21:50Z
[ "python", "linux", "ubuntu" ]
launching vs2008 build from python
263,820
<p>The first batch file launches a command prompt, i need the second command to be in the ccontext of the first. how can I do this in python?</p> <p>As is, it launches the batch, and blocks until the batch (with its command prompt context) terminates, and then executes <code>devenv</code> without the necessary contex...
1
2008-11-04T23:08:03Z
263,856
<p>You could append the devenv command onto the end of the original batch file like so:</p> <pre><code>'%comspec% /k "...vcvarsall.bat" x86 &amp;&amp; devenv asdf.sln /rebuild ...' </code></pre> <p>(obviously I have shortened the commands for simplicity's sake)</p>
2
2008-11-04T23:21:24Z
[ "python", "windows", "visual-studio-2008", "build-automation" ]
launching vs2008 build from python
263,820
<p>The first batch file launches a command prompt, i need the second command to be in the ccontext of the first. how can I do this in python?</p> <p>As is, it launches the batch, and blocks until the batch (with its command prompt context) terminates, and then executes <code>devenv</code> without the necessary contex...
1
2008-11-04T23:08:03Z
263,881
<p>I these situations I use script that does it all. That way you can chain as much as you want. Sometimes I will generate the script on the fly.</p> <pre><code>compileit.cmd call C:\Program Files\Microsoft Visual Studio 9.0\VC\vcvarsall.bat devenv $1.sln /rebuild Debug /Out last-build.txt </code></pre>
2
2008-11-04T23:29:38Z
[ "python", "windows", "visual-studio-2008", "build-automation" ]
launching vs2008 build from python
263,820
<p>The first batch file launches a command prompt, i need the second command to be in the ccontext of the first. how can I do this in python?</p> <p>As is, it launches the batch, and blocks until the batch (with its command prompt context) terminates, and then executes <code>devenv</code> without the necessary contex...
1
2008-11-04T23:08:03Z
288,734
<p>I run my Python script from a batch file that sets the variables :-)</p> <pre><code>call ...\vcvarsall.bat c:\python26\python.exe myscript.py </code></pre> <p>But Brett's solution sounds better.</p>
2
2008-11-13T23:24:48Z
[ "python", "windows", "visual-studio-2008", "build-automation" ]