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 |
|---|---|---|---|---|---|---|---|---|---|
Sorting dictionary keys by values in a list? | 2,257,101 | <p>I have a dictionary and a list. The values of the keys match those of the list, I'm just trying to find out how to sort the values in the dictionary by the values in the list.</p>
<pre><code>>>> l = [1, 2, 37, 32, 4, 3]
>>> d = {
32: 'Megumi',
1: 'Ai',
2: 'Risa',
3: 'Eri',
4:... | 2 | 2010-02-13T09:41:06Z | 2,257,113 | <p>You can't sort a dictionary because a dictionary is not ordered.</p>
<p>What you can do instead is:</p>
<ul>
<li>Get all the key-value pairs out of the dictionary, sort them and put them into a list or</li>
<li>What you are already doing: keep a sorted list of the keys and use the dictionary when you need the valu... | 1 | 2010-02-13T09:46:13Z | [
"python",
"list",
"sorting",
"dictionary"
] |
Sorting dictionary keys by values in a list? | 2,257,101 | <p>I have a dictionary and a list. The values of the keys match those of the list, I'm just trying to find out how to sort the values in the dictionary by the values in the list.</p>
<pre><code>>>> l = [1, 2, 37, 32, 4, 3]
>>> d = {
32: 'Megumi',
1: 'Ai',
2: 'Risa',
3: 'Eri',
4:... | 2 | 2010-02-13T09:41:06Z | 2,257,118 | <p>Don't shadow the builtins <code>dict</code> and <code>list</code></p>
<pre><code>>>> L = [1, 2, 37, 32, 4, 3]
>>> D = {
... 32: 'Megumi',
... 1: 'Ai',
... 2: 'Risa',
... 3: 'Eri',
... 4: 'Sayumi',
... 37: 'Mai'
... }
# Seems roundabout to use sorted here
# This causes an i... | 5 | 2010-02-13T09:51:11Z | [
"python",
"list",
"sorting",
"dictionary"
] |
Sorting dictionary keys by values in a list? | 2,257,101 | <p>I have a dictionary and a list. The values of the keys match those of the list, I'm just trying to find out how to sort the values in the dictionary by the values in the list.</p>
<pre><code>>>> l = [1, 2, 37, 32, 4, 3]
>>> d = {
32: 'Megumi',
1: 'Ai',
2: 'Risa',
3: 'Eri',
4:... | 2 | 2010-02-13T09:41:06Z | 2,257,122 | <p>Sorted dict is in fact a list of 2-tuples, because in Python 2.x there're no ordered dictionaty built-in. You almost got the solution, just add a value lookup after sorting keys:</p>
<pre><code>[(k,dict[k]) for k in sorted(dict.keys(), key=list.index)]
</code></pre>
<p>But this fails when a key is not in <code>li... | 0 | 2010-02-13T09:52:54Z | [
"python",
"list",
"sorting",
"dictionary"
] |
Sorting dictionary keys by values in a list? | 2,257,101 | <p>I have a dictionary and a list. The values of the keys match those of the list, I'm just trying to find out how to sort the values in the dictionary by the values in the list.</p>
<pre><code>>>> l = [1, 2, 37, 32, 4, 3]
>>> d = {
32: 'Megumi',
1: 'Ai',
2: 'Risa',
3: 'Eri',
4:... | 2 | 2010-02-13T09:41:06Z | 2,257,148 | <p>You shouldn't call you variables dict and list, because then, you cant use the build-in methods any more. I have renamed them in this example.</p>
<pre><code>>>> l = [1, 2, 37, 32, 4]
>>> d = dict = {
... 32: 'Megumi',
... 1: 'Ai',
... 2: 'Risa',
... 3: 'Eri',
... 4: 'Sayumi'... | 4 | 2010-02-13T10:11:27Z | [
"python",
"list",
"sorting",
"dictionary"
] |
Sorting dictionary keys by values in a list? | 2,257,101 | <p>I have a dictionary and a list. The values of the keys match those of the list, I'm just trying to find out how to sort the values in the dictionary by the values in the list.</p>
<pre><code>>>> l = [1, 2, 37, 32, 4, 3]
>>> d = {
32: 'Megumi',
1: 'Ai',
2: 'Risa',
3: 'Eri',
4:... | 2 | 2010-02-13T09:41:06Z | 2,257,246 | <p>In Python 3.1, you could use the <a href="http://docs.python.org/3.1/library/collections.html?highlight=ordereddict#collections.OrderedDict" rel="nofollow">OrderedDict</a> class:</p>
<pre><code>from collections import OrderedDict
l = [1, 2, 37, 32, 4]
d = {
32: 'Megumi',
1: 'Ai',
2: 'Risa',
3: 'Er... | 0 | 2010-02-13T11:03:00Z | [
"python",
"list",
"sorting",
"dictionary"
] |
Extending a PIL decoder | 2,257,318 | <p>I have a file which contains a single image of a specific format at a
specific offset. I can already get a file-like for the embedded image
which supports <code>read()</code>, <code>seek()</code>, and <code>tell()</code>. I want to take advantage
of an existing PIL decoder to handle the embedded image, but be able t... | 9 | 2010-02-13T11:33:30Z | 2,258,223 | <p>The relevant chapter of the docs is <a href="http://effbot.org/imagingbook/decoder.htm" rel="nofollow">this one</a> and I think it's fairly clear: if for example you want to decode image files in the new <code>.zap</code>-format, you write a <code>ZapImagePlugin.py</code> module which must perform a couple things:</... | 7 | 2010-02-13T16:18:14Z | [
"python",
"python-imaging-library"
] |
Extending a PIL decoder | 2,257,318 | <p>I have a file which contains a single image of a specific format at a
specific offset. I can already get a file-like for the embedded image
which supports <code>read()</code>, <code>seek()</code>, and <code>tell()</code>. I want to take advantage
of an existing PIL decoder to handle the embedded image, but be able t... | 9 | 2010-02-13T11:33:30Z | 2,258,992 | <p>What I did to solve this was to derive from the <code>ImageFile.ImageFile</code> child belonging to the embedded format instead of <code>ImageFile.ImageFile</code> directly. Then in <code>_open()</code> I replaced <code>self.fp</code> with the file-like to the embedded image, and called the parent's <code>_open()</c... | 3 | 2010-02-13T20:47:52Z | [
"python",
"python-imaging-library"
] |
How do I do os.getpid() in C++? | 2,257,415 | <p>newb here. I am trying to make a c++ program that will read from a named pipe created by python. My problem is, the named pipe created by python uses os.getpid() as part of the pipe name. when i try calling the pipe from c++, i use getpid(). i am not getting the same value from c++. is there a method equivalent in c... | 0 | 2010-02-13T12:09:32Z | 2,257,420 | <p>The standard library does not give you anything other than files. You will need to use some other OS specific API.</p>
| 0 | 2010-02-13T12:11:22Z | [
"c++",
"python"
] |
How do I do os.getpid() in C++? | 2,257,415 | <p>newb here. I am trying to make a c++ program that will read from a named pipe created by python. My problem is, the named pipe created by python uses os.getpid() as part of the pipe name. when i try calling the pipe from c++, i use getpid(). i am not getting the same value from c++. is there a method equivalent in c... | 0 | 2010-02-13T12:09:32Z | 2,257,422 | <p>You don't get same proccess IDs because your python program and c++ programs are run in different proccesses thus having different process IDs. So generally use a different logic to name your fifo files.</p>
| 4 | 2010-02-13T12:12:20Z | [
"c++",
"python"
] |
How do I do os.getpid() in C++? | 2,257,415 | <p>newb here. I am trying to make a c++ program that will read from a named pipe created by python. My problem is, the named pipe created by python uses os.getpid() as part of the pipe name. when i try calling the pipe from c++, i use getpid(). i am not getting the same value from c++. is there a method equivalent in c... | 0 | 2010-02-13T12:09:32Z | 2,257,426 | <p>You <strong>won't</strong> get the same value if you're running as a separate process as each process has their own process ID. Find some other way to identify the pipe.</p>
| 2 | 2010-02-13T12:12:56Z | [
"c++",
"python"
] |
How do I do os.getpid() in C++? | 2,257,415 | <p>newb here. I am trying to make a c++ program that will read from a named pipe created by python. My problem is, the named pipe created by python uses os.getpid() as part of the pipe name. when i try calling the pipe from c++, i use getpid(). i am not getting the same value from c++. is there a method equivalent in c... | 0 | 2010-02-13T12:09:32Z | 2,257,431 | <p>You cannot easily retrieve the Python interpreter's PID from your C++ program.</p>
<p>Either assign the named pipe a constant name, or if you really <em>need</em> multiple pipes of the same Python program, create a temporary file to which the Python programs write their PIDs (use file locking!) - then you can read ... | 0 | 2010-02-13T12:16:18Z | [
"c++",
"python"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 2,257,449 | <p><strong>Answer in one line:</strong></p>
<pre><code>''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
</code></pre>
<p><strong>A more secure version; see <a href="http://stackoverflow.com/a/23728630/2213647">http://stackoverflow.com/a/23728630/2213647</a>:</strong></p>
<pre><code>''... | 1,438 | 2010-02-13T12:26:22Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 2,257,492 | <p>Taking the answer from Ignacio, this works with Python 2.6:</p>
<pre><code>import random
import string
N=6
print ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
</code></pre>
<p>Example output:</p>
<blockquote>
<p>JQUBT2</p>
</blockquote>
| 13 | 2010-02-13T12:35:24Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 2,257,522 | <p>A simpler, faster but slightly less random way is to use <code>random.sample</code> instead of choosing each letter separately, If n-repetitions are allowed, enlarge your random basis by n times e.g.</p>
<pre><code>import random
import string
char_set = string.ascii_uppercase + string.digits
print ''.join(random.s... | 34 | 2010-02-13T12:44:48Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 9,236,278 | <p>I thought no one had answered this yet lol! But hey, here's my own go at it:</p>
<pre><code>import random
def random_alphanumeric(limit):
#ascii alphabet of all alphanumerals
r = (range(48, 58) + range(65, 91) + range(97, 123))
random.shuffle(r)
return reduce(lambda i, s: i + chr(s), r[:random.rand... | 8 | 2012-02-10T23:27:19Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 9,649,316 | <p>I'd do it this way:</p>
<pre><code>import random
from string import digits, ascii_uppercase
legals = digits + ascii_uppercase
def rand_string(length, char_set=legals):
output = ''
for _ in range(length): output += random.choice(char_set)
return output
</code></pre>
<p>Or just:</p>
<pre><code>def ra... | 3 | 2012-03-10T19:07:21Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 12,399,465 | <p>If you need a random string rather than a <strong>pseudo random</strong> one, you should use <a href="http://docs.python.org/2/library/os.html#os.urandom"><code>os.urandom</code></a> as the source</p>
<pre><code>from os import urandom
from itertools import islice, imap, repeat
import string
def rand_string(length=... | 8 | 2012-09-13T04:32:14Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 14,211,277 | <p>This method is slightly faster, and slightly more annoying, than the random.choice() method Ignacio posted.</p>
<p>It takes advantage of the nature of pseudo-random algorithms, and banks on bitwise and and shift being faster than generating a new random number for each character.</p>
<pre><code># must be length 32... | 6 | 2013-01-08T08:51:54Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 17,299,898 | <p>Consider having them unique too. Give it a try:
<a href="https://github.com/stochastic-technologies/shortuuid" rel="nofollow">shortuuid</a></p>
| 4 | 2013-06-25T14:22:29Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 17,323,913 | <h1>Simply use Python's builtin uuid:</h1>
<p>If UUIDs are okay for your purposes, use the built-in <a href="http://docs.python.org/2/library/uuid.html">uuid</a> package.</p>
<h2>One Line Solution:</h2>
<p><code>import uuid; str(uuid.uuid4().get_hex().upper()[0:6])</code></p>
<h2>In Depth Version:</h2>
<p>Example:... | 85 | 2013-06-26T15:11:13Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 20,688,431 | <p>Based on another Stack Overflow answer, <em><a href="http://stackoverflow.com/a/2782859/399891">Most lightweight way to create a random string and a random hexadecimal number</a></em>, a better version than the accepted answer would be:</p>
<pre><code>('%06x' % random.randrange(16**6)).upper()
</code></pre>
<... | 6 | 2013-12-19T17:51:09Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 21,447,678 | <pre><code>>>> import random
>>> str = []
>>> chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'
>>> num = int(raw_input('How long do you want the string to be? '))
How long do you want the string to be? 10
>>> for k in range(1, num+1):
... str.app... | 1 | 2014-01-30T04:13:41Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 23,728,630 | <p>This Stack Overflow quesion is the current top Google result for "random string Python". The current top answer is:</p>
<pre><code>''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
</code></pre>
<p>This is an excellent method, but the <a href="http://en.wikipedia.org/wiki/Pseudorando... | 268 | 2014-05-19T01:41:56Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 25,505,105 | <p>A faster, easier and more flexible way to do this is to use the <code>strgen</code> module (<code>pip install StringGenerator</code>).</p>
<p>Generate a 6-character random string with upper case letters and digits:</p>
<pre><code>>>> from strgen import StringGenerator as SG
>>> SG("[\u\d]{6}").re... | 13 | 2014-08-26T11:47:33Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 26,078,203 | <p>A simple one:</p>
<pre><code>import string
import random
character = string.lowercase + string.uppercase + string.digits + string.punctuation
char_len = len(character)
# you can specify your password length here
pass_len = random.randint(10,20)
password = ''
for x in range(pass_len):
password = password + chara... | 1 | 2014-09-27T19:12:09Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 28,283,899 | <p>For those of you who enjoy functional python:</p>
<pre><code>from itertools import imap, starmap, islice, repeat
from functools import partial
from string import letters, digits, join
from random import choice
join_chars = partial(join, sep='')
identity = lambda o: o
def irand_seqs(symbols=join_chars((letters, di... | 2 | 2015-02-02T18:02:12Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 30,038,250 | <p>import string
import random</p>
<p>the following logic still generates 6 character random sample </p>
<pre><code>>>> print ''.join(random.sample((string.ascii_uppercase+string.digits),6))
JT7K3Q
</code></pre>
<p>No need to multiply by 6</p>
<pre><code>>>> print ''.join(random.sample((string.as... | 1 | 2015-05-04T19:19:22Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 31,305,957 | <p>You can use the code </p>
<pre><code>var chars = "ABC123";
var random = new Random();
var result = new string(
Enumerable.Repeat(chars, 7) //Change 7 to any number of characters you want in your outcome
.Select(s => s[random.Next(s.Length)])
... | -3 | 2015-07-09T00:06:39Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 34,017,605 | <pre><code>import uuid
str = uuid.uuid4().hex
</code></pre>
<p><code>str</code> is a random value like <code>'cea8b32e00934aaea8c005a35d85a5c0'</code></p>
<pre><code>uppercase_str = str.upper()
</code></pre>
<p><code>uppercase_str</code> is <code>'CEA8B32E00934AAEA8C005A35D85A5C0'</code></p>
| 13 | 2015-12-01T10:04:48Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 36,784,492 | <p>(1) This will give you all caps and numbers:</p>
<pre><code>import string, random
passkey=''
for x in range(8):
if random.choice([1,2]) == 1:
passkey += passkey.join(random.choice(string.ascii_uppercase))
else:
passkey += passkey.join(random.choice(string.digits))
print passkey
</code></pre... | 1 | 2016-04-22T03:50:49Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 37,161,128 | <pre><code>import string
from random import *
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(choice(characters) for x in range(randint(8, 16)))
print password
</code></pre>
| 0 | 2016-05-11T11:22:02Z | [
"python",
"string",
"random"
] |
Random string generation with upper case letters and digits in Python | 2,257,441 | <p>I want to generate a string of size N.</p>
<p>It should be made up of numbers and uppercase English letters such as:</p>
<ul>
<li>6U1S75</li>
<li>4Z4UKK</li>
<li>U911K4</li>
</ul>
<p>How can I achieve this in a <a href="https://en.wikipedia.org/wiki/Python_%28programming_language%29#Features_and_philosophy">pytho... | 674 | 2010-02-13T12:23:58Z | 37,892,414 | <p>Use Numpy's random.choice() function</p>
<pre><code>import numpy as np
import string
if __name__ == '__main__':
a = np.random.choice(list(string.ascii_uppercase + string.digits), length)
print(''.join(a))
</code></pre>
<p>Documentation is here <a href="http://docs.scipy.org/doc/nu... | 0 | 2016-06-18T01:22:33Z | [
"python",
"string",
"random"
] |
How to parse a file line by line, char by char in Python? | 2,257,665 | <p>How do you read a character by character from a source file in python until end of line and how do you check for end of line in python so that you can then start reading from the next line and finally how do we check for the end of file condition to finish the read in the entire file.
Thank You:).</p>
| 1 | 2010-02-13T13:25:07Z | 2,257,689 | <p>You can simply iterate over each line in Python. Use the universal end-of-line mode if you want Python to care about Windows/UNIX/Mac line ends automatically:</p>
<pre><code>with open("mytextfile.txt", "rtU") as f:
for line in f:
# Now you have one line of text in the variable "line" and can
# iterate ove... | 8 | 2010-02-13T13:29:46Z | [
"python"
] |
How to parse a file line by line, char by char in Python? | 2,257,665 | <p>How do you read a character by character from a source file in python until end of line and how do you check for end of line in python so that you can then start reading from the next line and finally how do we check for the end of file condition to finish the read in the entire file.
Thank You:).</p>
| 1 | 2010-02-13T13:25:07Z | 2,257,691 | <p>You don't have to worry about line and file endings, just do</p>
<pre><code>file = open('yourfile', 'r')
for line in file.readlines():
for c in line:
# do sth.
</code></pre>
| 1 | 2010-02-13T13:30:43Z | [
"python"
] |
Search and replace characters in a file with Python | 2,257,731 | <p>I am trying to do transliteration where I need to replace every source character in English from a file with its equivalent from a dictionary I am using in the source code corresponding to another language in Unicode format. I am now able to read character by character from a file in English how do I search for its ... | 4 | 2010-02-13T13:46:32Z | 2,257,747 | <p>Note: Updated after clarifications from questioner. Please read the comments from the OP attached to this answer.</p>
<p>Something like this:</p>
<pre><code>for syllable in input_text.split_into_syllables():
output_file.write(d[syllable])
</code></pre>
<p>Here <code>output_file</code> is a file object, open f... | 0 | 2010-02-13T13:50:51Z | [
"python"
] |
Search and replace characters in a file with Python | 2,257,731 | <p>I am trying to do transliteration where I need to replace every source character in English from a file with its equivalent from a dictionary I am using in the source code corresponding to another language in Unicode format. I am now able to read character by character from a file in English how do I search for its ... | 4 | 2010-02-13T13:46:32Z | 2,258,307 | <p>The <a href="http://docs.python.org/library/stdtypes.html#str.translate" rel="nofollow">translate</a> method of Unicode objects is the simplest and fastest way to perform the transliteration you require. (I assume you're using Unicode, not plain byte strings which would make it impossible to have characters such as... | 3 | 2010-02-13T16:51:21Z | [
"python"
] |
installing pygobject on mac for Python 2.6 | 2,257,771 | <p>Does anyone know how to install PyGObject on Mac OSX for Python 2.6. The current distribution available on darwinports is using Python2.4. I want a package using Python2.6</p>
<p>Alternatively, has anyone tried installing it from sources on a Mac? </p>
| 4 | 2010-02-13T13:59:50Z | 2,257,775 | <p>ah found it : <a href="http://py26-gobject.darwinports.com/" rel="nofollow">http://py26-gobject.darwinports.com/</a></p>
<p>nevermind!</p>
| -1 | 2010-02-13T14:01:19Z | [
"python",
"osx",
"pygobject"
] |
installing pygobject on mac for Python 2.6 | 2,257,771 | <p>Does anyone know how to install PyGObject on Mac OSX for Python 2.6. The current distribution available on darwinports is using Python2.4. I want a package using Python2.6</p>
<p>Alternatively, has anyone tried installing it from sources on a Mac? </p>
| 4 | 2010-02-13T13:59:50Z | 2,924,615 | <p>If you have <a href="http://www.macports.org/">Macports</a> you can simply do </p>
<pre><code>port install py26-gobject
</code></pre>
<p>With <a href="http://brew.sh/">Homebrew</a> it would be:</p>
<pre><code>brew install pygobject
</code></pre>
| 8 | 2010-05-27T20:24:30Z | [
"python",
"osx",
"pygobject"
] |
Python - to check if a char is in dictionary and if not to deal with it | 2,257,799 | <p>I am going about transliteration from one source language(input file) to a target language(target file) so I am checking for equivalent mappings in a dictionary in my source code, certain characters in the source code don't have an equivalent mapping like comma(,) and all other such special symbols. How do I check i... | 2 | 2010-02-13T14:09:11Z | 2,257,814 | <p>I think you want something like this:</p>
<pre><code>tokenMapping = {"&&" : "and"}
for token in source file: # <-- pseudocode
translatedToken = tokenMapping[token] if token in tokenMapping else "transliteration unknown"
</code></pre>
<p>If there's a translation in the dictionary (e.g. "&&" ... | 1 | 2010-02-13T14:15:07Z | [
"python",
"transliteration"
] |
Python - to check if a char is in dictionary and if not to deal with it | 2,257,799 | <p>I am going about transliteration from one source language(input file) to a target language(target file) so I am checking for equivalent mappings in a dictionary in my source code, certain characters in the source code don't have an equivalent mapping like comma(,) and all other such special symbols. How do I check i... | 2 | 2010-02-13T14:09:11Z | 2,257,877 | <pre><code>dictx = {}
for itm in my_source :
dictx[itm] = dictx.get(itm, 0) + 1
</code></pre>
<p>I didn't completely understand the details of your question, but here's the simplest example i could think of that illustrates the pattern i think you are after. </p>
<p>The 'get' method i believe is what you want. It... | 0 | 2010-02-13T14:37:29Z | [
"python",
"transliteration"
] |
Python - to check if a char is in dictionary and if not to deal with it | 2,257,799 | <p>I am going about transliteration from one source language(input file) to a target language(target file) so I am checking for equivalent mappings in a dictionary in my source code, certain characters in the source code don't have an equivalent mapping like comma(,) and all other such special symbols. How do I check i... | 2 | 2010-02-13T14:09:11Z | 2,257,912 | <p>My recommendation, given that <code>rules</code> is a mapping of the characters to their transliterated equivalents:</p>
<pre><code>results = []
for char in source_text:
results.append(rules.get(char, char))
return ''.join(results) # turns the list back into a string
</code></pre>
<p>A dict's <code>get</cod... | 3 | 2010-02-13T14:49:36Z | [
"python",
"transliteration"
] |
Python - to check if a char is in dictionary and if not to deal with it | 2,257,799 | <p>I am going about transliteration from one source language(input file) to a target language(target file) so I am checking for equivalent mappings in a dictionary in my source code, certain characters in the source code don't have an equivalent mapping like comma(,) and all other such special symbols. How do I check i... | 2 | 2010-02-13T14:09:11Z | 2,258,152 | <p>This seems pretty straightforward. If your dictionary is char to char, then you would do something like</p>
<pre><code>outstr = ''
for ch in instr:
if ch in mydict:
outstr += mydict[ch]
else:
outstr += ch
</code></pre>
<p>Here, instr is your input string and mydict contains your mapping of ... | 0 | 2010-02-13T15:56:50Z | [
"python",
"transliteration"
] |
Python - to check if a char is in dictionary and if not to deal with it | 2,257,799 | <p>I am going about transliteration from one source language(input file) to a target language(target file) so I am checking for equivalent mappings in a dictionary in my source code, certain characters in the source code don't have an equivalent mapping like comma(,) and all other such special symbols. How do I check i... | 2 | 2010-02-13T14:09:11Z | 2,258,329 | <p>If you use the <code>translate</code> method of Unicode objects, as I recommended in answer to another question of yours, everything's done automatically for you exactly as you desire: each Unicode character <code>c</code> whose codepoints (<code>ord(c)</code>) is not in the transliteration dictionary is simply pass... | 3 | 2010-02-13T16:58:40Z | [
"python",
"transliteration"
] |
Modify entity data either transactionally or not (depending on the need) | 2,257,946 | <p><strong>What is the best way to keep code modular and decoupled but avoid entering a transaction twice?</strong></p>
<p>Entities often have class methods to load, modify, and store data. Often, this must be transactional to be consistent with child/sibling/cousin entities. Here is the pattern:</p>
<pre><code>class... | 2 | 2010-02-13T14:59:14Z | 2,257,967 | <p>The pattern I use is to have a parameter indicating whether transactional behavior is required.</p>
<pre><code>class OtherEntity(db.Model):
# ... some properties
@classmethod
def update_descendants(cls, ancestor, with_transaction=True):
if with_transaction:
return db.run_in_transaction(cls.update_descendants... | 2 | 2010-02-13T15:02:40Z | [
"python",
"google-app-engine",
"transactions"
] |
Modify entity data either transactionally or not (depending on the need) | 2,257,946 | <p><strong>What is the best way to keep code modular and decoupled but avoid entering a transaction twice?</strong></p>
<p>Entities often have class methods to load, modify, and store data. Often, this must be transactional to be consistent with child/sibling/cousin entities. Here is the pattern:</p>
<pre><code>class... | 2 | 2010-02-13T14:59:14Z | 2,258,336 | <p>I would suggest making the transaction functions top-level class methods. Then, you can call them directly or with <code>db.run_in_transaction</code>, as appropriate.</p>
| 1 | 2010-02-13T17:01:56Z | [
"python",
"google-app-engine",
"transactions"
] |
Why do I get "expected an indented block" when I try to run my Python script? | 2,257,947 | <p>I have an error which says "expected an indented block"
Could you please guide me on how to deal with this error. Thank you:) </p>
<p>Code example:</p>
<pre><code>for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch)
</code></pre>
| 6 | 2010-02-13T14:59:16Z | 2,257,957 | <p>Python uses indentation (spaces/tabs in front of your code lines) to indicate where a block of code starts and ends, relative to what python statement precedes it.</p>
<p>So, taking PHP for example:</p>
<pre><code>if ($blah == 'foo')
{
// this is line 1 of my code block
// this is line 2
}
</code></pre>
<p>Wo... | 2 | 2010-02-13T15:01:09Z | [
"python",
"indentation"
] |
Why do I get "expected an indented block" when I try to run my Python script? | 2,257,947 | <p>I have an error which says "expected an indented block"
Could you please guide me on how to deal with this error. Thank you:) </p>
<p>Code example:</p>
<pre><code>for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch)
</code></pre>
| 6 | 2010-02-13T14:59:16Z | 2,257,965 | <p>Python is a language that relies heavily on indentation to decide program structure unlike C and some other languages that use braces for this purpose.</p>
<p>When you have a statement like:</p>
<pre><code>if true:
pass
</code></pre>
<p>it will complain because there's no indented statement for the <code>if</code... | 11 | 2010-02-13T15:02:29Z | [
"python",
"indentation"
] |
Why do I get "expected an indented block" when I try to run my Python script? | 2,257,947 | <p>I have an error which says "expected an indented block"
Could you please guide me on how to deal with this error. Thank you:) </p>
<p>Code example:</p>
<pre><code>for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch)
</code></pre>
| 6 | 2010-02-13T14:59:16Z | 2,257,966 | <p>You are probably mixing tabs with spaces. It <em>looks</em> indented but it really isn't.</p>
<hr>
<p>Your code gives me a different error:</p>
<pre><code>for ch in f: \
( translatedToken = english_hindi_dict[ch] ) \
if (ch in english_hindi_dic... | 14 | 2010-02-13T15:02:31Z | [
"python",
"indentation"
] |
Why do I get "expected an indented block" when I try to run my Python script? | 2,257,947 | <p>I have an error which says "expected an indented block"
Could you please guide me on how to deal with this error. Thank you:) </p>
<p>Code example:</p>
<pre><code>for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch)
</code></pre>
| 6 | 2010-02-13T14:59:16Z | 2,257,969 | <p>Python determines blocks by indentation, not by characters <code>{</code> and <code>}</code> like C/C++/Java/PHP/... does or by <code>if</code>/<code>endif</code> or <code>begin</code>/<code>end</code> pairs found in some other languages. So you have to be careful about indentation - also mixing tabs and spaces is n... | 1 | 2010-02-13T15:03:01Z | [
"python",
"indentation"
] |
Why do I get "expected an indented block" when I try to run my Python script? | 2,257,947 | <p>I have an error which says "expected an indented block"
Could you please guide me on how to deal with this error. Thank you:) </p>
<p>Code example:</p>
<pre><code>for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch)
</code></pre>
| 6 | 2010-02-13T14:59:16Z | 2,257,982 | <p><em>Editing answer to match the code example.</em></p>
<pre><code>for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch)
</code></pre>
<p>is just not valid Python.</p>
<p>First, readability count. Your code is hard to read and so, is hard to debug. Wh... | 13 | 2010-02-13T15:08:02Z | [
"python",
"indentation"
] |
Why do I get "expected an indented block" when I try to run my Python script? | 2,257,947 | <p>I have an error which says "expected an indented block"
Could you please guide me on how to deal with this error. Thank you:) </p>
<p>Code example:</p>
<pre><code>for ch in f: ( translatedToken = english_hindi_dict[ch] ) if (ch in english_hindi_dict) else (translatedToken = ch)
</code></pre>
| 6 | 2010-02-13T14:59:16Z | 2,258,143 | <p><code>IndentationError</code>s can be caused by a lot if different things. Off the top of my head:</p>
<ul>
<li><p>Forgetting to indent at all.</p>
<p>Python uses indents to delimit syntactic blocks. For example:</p>
<pre><code>if will_is_awesome:
print "You're right"
else:
print "You lie!"
</code></pre>
... | 3 | 2010-02-13T15:56:13Z | [
"python",
"indentation"
] |
SQLAlchemy getting column data types of query results | 2,258,072 | <pre><code>from sqlalchemy import create_engine, MetaData, ForeignKey
engine = create_engine("mysql://user:passwd@localhost/shema", echo=False)
meta = MetaData(engine, True)
conn = engine.connect()
tb_list = meta.tables["tb_list"]
tb_data = meta.tables["tb_data"]
tb_list.c.i_data.append_foreign_key( ForeignKey(tb_da... | 8 | 2010-02-13T15:36:24Z | 2,259,980 | <p>you'd say:</p>
<pre><code>types = [col.type for col in q.columns]
</code></pre>
<p>the (compiled) statement is on the result too if you feel like digging:</p>
<pre><code>types = [col.type for col in res.context.compiled.statement.columns]
</code></pre>
<p>if you want the DBAPI version of the types, which is a li... | 6 | 2010-02-14T02:54:11Z | [
"python",
"sqlalchemy"
] |
How do I implement a simple cross platform Python daemon? | 2,258,079 | <p>I would like to have my Python program run in the background as a daemon, on either Windows or Unix. I see that the <a href="http://pypi.python.org/pypi/python-daemon/">python-daemon package</a> is for Unix only; is there an alternative for cross platform? If possible, I would like to keep the code as simple as I ca... | 19 | 2010-02-13T15:38:12Z | 2,258,107 | <p>In Windows it's called a "service" and you could implement it pretty easily e.g. with the win32serviceutil module, part of <a href="http://sourceforge.net/projects/pywin32/">pywin32</a>. Unfortunately the two "mental models" -- service vs daemon -- are very different in detail, even though they serve similar purpos... | 10 | 2010-02-13T15:44:37Z | [
"python",
"cross-platform",
"daemon"
] |
How do I implement a simple cross platform Python daemon? | 2,258,079 | <p>I would like to have my Python program run in the background as a daemon, on either Windows or Unix. I see that the <a href="http://pypi.python.org/pypi/python-daemon/">python-daemon package</a> is for Unix only; is there an alternative for cross platform? If possible, I would like to keep the code as simple as I ca... | 19 | 2010-02-13T15:38:12Z | 2,258,108 | <p>The reason it's unix only is that <a href="http://en.wikipedia.org/wiki/Daemon_%28computer_software%29" rel="nofollow">daemons</a> are a Unix specific concept i.e a background process initiated by the os and usually running as a child of the root PID .<br>
Windows has no direct equivalent of a unix daemon, the close... | 0 | 2010-02-13T15:44:57Z | [
"python",
"cross-platform",
"daemon"
] |
How do I implement a simple cross platform Python daemon? | 2,258,079 | <p>I would like to have my Python program run in the background as a daemon, on either Windows or Unix. I see that the <a href="http://pypi.python.org/pypi/python-daemon/">python-daemon package</a> is for Unix only; is there an alternative for cross platform? If possible, I would like to keep the code as simple as I ca... | 19 | 2010-02-13T15:38:12Z | 2,258,110 | <p>In general the concept of a daemon is Unix specific, in particular expected behaviour with respect to file creation masks, process hierarchy, and signal handling.</p>
<p>You may find <a href="http://www.python.org/dev/peps/pep-3143/" rel="nofollow">PEP 3143</a> useful wherein a proposed continuation of <a href="htt... | 2 | 2010-02-13T15:45:21Z | [
"python",
"cross-platform",
"daemon"
] |
How do I implement a simple cross platform Python daemon? | 2,258,079 | <p>I would like to have my Python program run in the background as a daemon, on either Windows or Unix. I see that the <a href="http://pypi.python.org/pypi/python-daemon/">python-daemon package</a> is for Unix only; is there an alternative for cross platform? If possible, I would like to keep the code as simple as I ca... | 19 | 2010-02-13T15:38:12Z | 2,258,111 | <p>Two options come to mind:</p>
<ol>
<li><p><strong>Port your program</strong> into <a href="http://mail.python.org/pipermail/python-win32/2006-April/004518.html" rel="nofollow">a windows service</a>. You can probably share much of your code between the two implementations.</p></li>
<li><p>Does your program really us... | 4 | 2010-02-13T15:45:26Z | [
"python",
"cross-platform",
"daemon"
] |
How do I implement a simple cross platform Python daemon? | 2,258,079 | <p>I would like to have my Python program run in the background as a daemon, on either Windows or Unix. I see that the <a href="http://pypi.python.org/pypi/python-daemon/">python-daemon package</a> is for Unix only; is there an alternative for cross platform? If possible, I would like to keep the code as simple as I ca... | 19 | 2010-02-13T15:38:12Z | 39,715,350 | <p>This question is 6 years old, but I had the same problem, and the existing answers weren't cross-platform enough for my use case. Though Windows services are often used in similar ways as Unix daemons, at the end of the day they differ substantially, and "the devil's in the details". Long story short, I set out to t... | 1 | 2016-09-27T03:04:15Z | [
"python",
"cross-platform",
"daemon"
] |
How to execute a VS2008 command from Python and grab its output? | 2,258,094 | <p>I wish to run </p>
<pre><code>tf changeset 12345
</code></pre>
<p>Using the Visual Studio 2008 Command tool. It is located in: <code>"c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\"</code> and the command that gets launched is: <code>%comspec% /k ""c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcva... | 4 | 2010-02-13T15:41:06Z | 2,258,173 | <p>Have a look at this code I used <a href="http://stackoverflow.com/questions/2255126/acessing-netstat-like-data-with-net/2255159#2255159">here</a> to do the job that you're looking for. This is written in C#, and is a flexible class, you just need to change the command, look in <code>ps.FileName</code> and <code>ps.A... | 2 | 2010-02-13T16:06:22Z | [
"python",
"visual-studio-2008",
"shellexecute"
] |
How to execute a VS2008 command from Python and grab its output? | 2,258,094 | <p>I wish to run </p>
<pre><code>tf changeset 12345
</code></pre>
<p>Using the Visual Studio 2008 Command tool. It is located in: <code>"c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\"</code> and the command that gets launched is: <code>%comspec% /k ""c:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\vcva... | 4 | 2010-02-13T15:41:06Z | 2,287,027 | <pre><code>process = subprocess.Popen(['tf', 'changeset', '12345'], cwd='c:/somedir', env={'SOMEENVVAR': 'SOMEVALUE', ...}, stdout=subprocess.PIPE)
for line in process.stdout:
print line
process.terminate()
</code></pre>
| 3 | 2010-02-18T07:56:36Z | [
"python",
"visual-studio-2008",
"shellexecute"
] |
How to copy data from one Tkinter Text widget to another? | 2,258,097 | <pre><code>from Tkinter import *
root = Tk()
root.title("Whois Tool")
text = Text()
text1 = Text()
text1.config(width=15, height=1)
text1.pack()
def button1():
text.insert(END, text1)
b = Button(root, text="Enter", width=10, height=2, command=button1)
b.pack()
scrollbar = Scrollbar(root)
scrollbar.pack(side=R... | 4 | 2010-02-13T15:41:58Z | 2,258,137 | <p>You are trying to insert a <code>Text</code> reference at the end of another <code>Text</code> widget (does not make much sense), but what you actually want to do is to copy the contents of a <code>Text</code> widget to another:</p>
<pre><code>def button1():
text.insert(INSERT, text1.get("1.0", "end-1c"))
</cod... | 4 | 2010-02-13T15:54:31Z | [
"python",
"tkinter"
] |
How to force an ImportError on development machine? (pwd module) | 2,258,100 | <p>I'm trying to use a third-party lib (docutils) on Google App Engine and have a problem with this code (in docutils):</p>
<pre><code>try:
import pwd
do stuff
except ImportError:
do other stuff
</code></pre>
<p>I want the import to fail, as it will on the actual GAE server, but the problem is that it doe... | 3 | 2010-02-13T15:43:39Z | 2,258,122 | <p>In your testing framework, before you cause docutils to be imported, you can perform this setup task:</p>
<pre><code>import __builtin__
self.savimport = __builtin__.__import__
def myimport(name, *a):
if name=='pwd': raise ImportError
return self.savimport(name, *a)
__builtin__.__import__ = myimport
</code></pre... | 4 | 2010-02-13T15:49:21Z | [
"python",
"google-app-engine",
"ubuntu",
"import",
"pwd"
] |
How to force an ImportError on development machine? (pwd module) | 2,258,100 | <p>I'm trying to use a third-party lib (docutils) on Google App Engine and have a problem with this code (in docutils):</p>
<pre><code>try:
import pwd
do stuff
except ImportError:
do other stuff
</code></pre>
<p>I want the import to fail, as it will on the actual GAE server, but the problem is that it doe... | 3 | 2010-02-13T15:43:39Z | 2,258,968 | <p>Even easier than messing with <code>__import__</code> is just inserting <code>None</code> in the sys.modules dict:</p>
<pre><code>>>> import sys
>>> sys.modules['pwd'] = None
>>> import pwd
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: N... | 6 | 2010-02-13T20:41:31Z | [
"python",
"google-app-engine",
"ubuntu",
"import",
"pwd"
] |
Avoid race condition when asserting file permissions in Python | 2,258,257 | <p>An application wants to parse and "execute" a file, and wants to assert the file is executable for security reasons.</p>
<p>A moments thought and you realize this initial code has a race condition that makes the security scheme ineffective:</p>
<pre><code>import os
class ExecutionError (Exception):
pass
def ... | 1 | 2010-02-13T16:29:09Z | 2,258,319 | <p>You cannot entirely solve this race condition -- e.g., in the version where you first open, then check permissions, it's <em>possible</em> that the permissions get changed just after you've opened the file and just before you've changed the permissions.</p>
<p>If you can atomically move the file to a directory wher... | 2 | 2010-02-13T16:56:08Z | [
"python",
"security",
"posix",
"race-condition"
] |
Avoid race condition when asserting file permissions in Python | 2,258,257 | <p>An application wants to parse and "execute" a file, and wants to assert the file is executable for security reasons.</p>
<p>A moments thought and you realize this initial code has a race condition that makes the security scheme ineffective:</p>
<pre><code>import os
class ExecutionError (Exception):
pass
def ... | 1 | 2010-02-13T16:29:09Z | 2,258,362 | <p>The executability is attached to the file you open, there is nothing stopping several files from pointing to the inode containing the data you wish to read. In other words, the same data may be readable from a non-executable file elsewhere in the same filesystem. Furthermore, even after opening the file, you can't p... | 3 | 2010-02-13T17:11:21Z | [
"python",
"security",
"posix",
"race-condition"
] |
Avoid race condition when asserting file permissions in Python | 2,258,257 | <p>An application wants to parse and "execute" a file, and wants to assert the file is executable for security reasons.</p>
<p>A moments thought and you realize this initial code has a race condition that makes the security scheme ineffective:</p>
<pre><code>import os
class ExecutionError (Exception):
pass
def ... | 1 | 2010-02-13T16:29:09Z | 2,258,384 | <p>The best you can do is :</p>
<ul>
<li>save the permission.</li>
<li>change it to your own unique user (something with the program name) and forbid others to run it.</li>
<li>make you checks (on the saved permission if needed).</li>
<li>run your process.</li>
<li>set back the permission to the saved ones.</li>
</ul>... | 0 | 2010-02-13T17:18:18Z | [
"python",
"security",
"posix",
"race-condition"
] |
Avoid race condition when asserting file permissions in Python | 2,258,257 | <p>An application wants to parse and "execute" a file, and wants to assert the file is executable for security reasons.</p>
<p>A moments thought and you realize this initial code has a race condition that makes the security scheme ineffective:</p>
<pre><code>import os
class ExecutionError (Exception):
pass
def ... | 1 | 2010-02-13T16:29:09Z | 2,258,804 | <p>You should change the files ownership such that an attacker cannot access it "chown root:root file_name". Do a "chmod 700 file_name" so that no other accounts can read/write/execute the file. This avoids the problem of a TOCTOU all together and this is how people prevent files from being modified by an attacker w... | 0 | 2010-02-13T19:52:22Z | [
"python",
"security",
"posix",
"race-condition"
] |
Avoid race condition when asserting file permissions in Python | 2,258,257 | <p>An application wants to parse and "execute" a file, and wants to assert the file is executable for security reasons.</p>
<p>A moments thought and you realize this initial code has a race condition that makes the security scheme ineffective:</p>
<pre><code>import os
class ExecutionError (Exception):
pass
def ... | 1 | 2010-02-13T16:29:09Z | 2,273,374 | <p>Another way to do it is to change the file name to something unexpected, or even copy the entire file - if it's not too big - to a temp dir (encrypted if necessary), make you checks, then rename / copy the file back.</p>
<p>Of course it's a very heavy process.</p>
<p>But you end up with that because the system has... | 0 | 2010-02-16T14:00:23Z | [
"python",
"security",
"posix",
"race-condition"
] |
How do I handle Python XML-RPC output and exceptions? | 2,258,514 | <p>I have created a simple <a href="http://docs.python.org/library/xmlrpclib.html" rel="nofollow">Python XML-RPC</a> implementation, largely based on the examples.</p>
<p>However, it sends output like this:</p>
<pre><code>foo.bar.com - - [13/Feb/2010 17:55:47] "POST /RPC2 HTTP/1.0" 200 -
</code></pre>
<p>... to the ... | 3 | 2010-02-13T18:05:09Z | 2,258,570 | <p>I guess you're using the <code>SimpleXMLRPCServer</code> class from the examples. In that case, simply provide the parameter <code>logRequests</code> when creating it:</p>
<pre><code>server = SimpleXMLRPCServer(("localhost", 8000), logRequests = False)
</code></pre>
<p>That will suppress request logging.</p>
<p>A... | 4 | 2010-02-13T18:23:13Z | [
"python",
"xml-rpc"
] |
How do I handle Python XML-RPC output and exceptions? | 2,258,514 | <p>I have created a simple <a href="http://docs.python.org/library/xmlrpclib.html" rel="nofollow">Python XML-RPC</a> implementation, largely based on the examples.</p>
<p>However, it sends output like this:</p>
<pre><code>foo.bar.com - - [13/Feb/2010 17:55:47] "POST /RPC2 HTTP/1.0" 200 -
</code></pre>
<p>... to the ... | 3 | 2010-02-13T18:05:09Z | 2,258,701 | <p>This is really a shell redirection problem, not a python problem.
When you say</p>
<pre><code>python foobar 2>&1 >> foobar.log
</code></pre>
<p>You are <em>first</em> redirecting stderr to stdout, and then <em>second</em> redirecting stdout to foobar.log.
stderr does not get automatically redirected ... | 0 | 2010-02-13T19:15:30Z | [
"python",
"xml-rpc"
] |
Using TinyMCE for sites with a dark background | 2,258,678 | <p>I'm using django-tinymce in my Django website. Through the admin interface one can edit a SimplePage object which has a <code>tinymce.models.HTMLField</code>. The website visitor will then see the html rendered in the content area of the page.</p>
<p>Problem is, the website itself has a dark background, and the Tin... | 2 | 2010-02-13T19:07:59Z | 2,258,743 | <p>Usually in a Rich Text Editor, you would specify a style sheet (the original page's style sheet or a variation of it) as the style sheet for the editor. That way, all the basic text characteristics (size, family, colour and background colour) are really What You See Is What You Get.</p>
<p>I think <a href="http://w... | 0 | 2010-02-13T19:31:32Z | [
"python",
"html",
"django",
"tinymce",
"django-tinymce"
] |
Using TinyMCE for sites with a dark background | 2,258,678 | <p>I'm using django-tinymce in my Django website. Through the admin interface one can edit a SimplePage object which has a <code>tinymce.models.HTMLField</code>. The website visitor will then see the html rendered in the content area of the page.</p>
<p>Problem is, the website itself has a dark background, and the Tin... | 2 | 2010-02-13T19:07:59Z | 2,258,744 | <p>You can customize the CSS of the editable area with the <code>content_css</code> setting, see: <a href="http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/content_css" rel="nofollow">http://wiki.moxiecode.com/index.php/TinyMCE:Configuration/content_css</a></p>
<p>This also works with django-tinymce, simply a... | 1 | 2010-02-13T19:31:55Z | [
"python",
"html",
"django",
"tinymce",
"django-tinymce"
] |
More than 1 docstrings for a single module/function etc.? | 2,258,696 | <p>I'm using python 3.1.</p>
<p>Is it possible to create more than 1 docstring for a single module or function?
I'm creating a program, and I'm intending to have multiple docstrings with a category for each. I intend to give other people the program so they can use it, and to make things easy for programmers and non-p... | 4 | 2010-02-13T19:13:55Z | 2,258,713 | <p>You can replace the <code>__doc__</code> attribute if you want to have more than one possible docstring, but please consider making the initial docstring flexible enough for all types.</p>
| 0 | 2010-02-13T19:21:06Z | [
"python",
"docstring"
] |
More than 1 docstrings for a single module/function etc.? | 2,258,696 | <p>I'm using python 3.1.</p>
<p>Is it possible to create more than 1 docstring for a single module or function?
I'm creating a program, and I'm intending to have multiple docstrings with a category for each. I intend to give other people the program so they can use it, and to make things easy for programmers and non-p... | 4 | 2010-02-13T19:13:55Z | 2,258,721 | <p>Module is a collection of classes/functions/modules. So its docstring gives the intro about what it contains.</p>
<p>Class docstring tells what the class is about and its methods docstrings tell what the methods so. A class serves one purpose and a methods does a single thing so they should have single docstring.</... | 0 | 2010-02-13T19:23:30Z | [
"python",
"docstring"
] |
More than 1 docstrings for a single module/function etc.? | 2,258,696 | <p>I'm using python 3.1.</p>
<p>Is it possible to create more than 1 docstring for a single module or function?
I'm creating a program, and I'm intending to have multiple docstrings with a category for each. I intend to give other people the program so they can use it, and to make things easy for programmers and non-p... | 4 | 2010-02-13T19:13:55Z | 2,258,772 | <p>Instead of using a function, you could use a class with <code>usage</code> and <code>extra</code> attributes defined. For example,</p>
<pre><code>class Foo(object):
'''Here is the function's official docstring'''
usage='All about the usage'
extra='How another part works'
def __call__(self):
... | 2 | 2010-02-13T19:43:24Z | [
"python",
"docstring"
] |
More than 1 docstrings for a single module/function etc.? | 2,258,696 | <p>I'm using python 3.1.</p>
<p>Is it possible to create more than 1 docstring for a single module or function?
I'm creating a program, and I'm intending to have multiple docstrings with a category for each. I intend to give other people the program so they can use it, and to make things easy for programmers and non-p... | 4 | 2010-02-13T19:13:55Z | 2,258,826 | <p>I don't recommend trying to do something complicated with the docstrings. Best to keep the docstrings simple, and do something else if you want to make a bunch of different documentation options available.</p>
<p>If you really want to do what you described, I suggest you use tags to delimit sections within docstri... | 4 | 2010-02-13T19:57:32Z | [
"python",
"docstring"
] |
More than 1 docstrings for a single module/function etc.? | 2,258,696 | <p>I'm using python 3.1.</p>
<p>Is it possible to create more than 1 docstring for a single module or function?
I'm creating a program, and I'm intending to have multiple docstrings with a category for each. I intend to give other people the program so they can use it, and to make things easy for programmers and non-p... | 4 | 2010-02-13T19:13:55Z | 2,258,836 | <p>You want to consider using <a href="http://programmingbits.pythonblogs.com/27_programmingbits/archive/50_function_decorators.html" rel="nofollow">decorators</a> to cleanly do what ~unutbu is proposing for functions: adding a separate field per each. For example:</p>
<pre><code>def human_desc(description):
def a... | 3 | 2010-02-13T20:00:24Z | [
"python",
"docstring"
] |
Prevent a console app from closing when not invoked from an existing terminal? | 2,258,771 | <p>There are many variants on this kind of question. However I am specifically after a way to prevent a console application in Python from closing when it is not invoked from a terminal (or other console, as it may be called on Windows). An example where this could occur is double clicking a <code>.py</code> file from ... | 7 | 2010-02-13T19:43:13Z | 2,258,980 | <p>On Unix, <code>sys.stdin.isatty()</code> reliably tells you whether standard input is coming from a terminal-like device (or is otherwise redirected), and similarly for the same method on <code>sys.stdout</code> and <code>sys.stderr</code> -- so you can use those calls to determine whether the application is being e... | 3 | 2010-02-13T20:43:52Z | [
"python",
"console",
"persistence",
"terminal",
"console-application"
] |
Prevent a console app from closing when not invoked from an existing terminal? | 2,258,771 | <p>There are many variants on this kind of question. However I am specifically after a way to prevent a console application in Python from closing when it is not invoked from a terminal (or other console, as it may be called on Windows). An example where this could occur is double clicking a <code>.py</code> file from ... | 7 | 2010-02-13T19:43:13Z | 2,261,219 | <p>First, an attempt to disuade you from clever hacks. It's perfectly appropriate to have a seperate shortcut designed to be run from Explorer that does slightly different things (like holding the console open) from the script to be used from the commandline. As Alex has already pointed out, this is not an issue on nix... | 6 | 2010-02-14T13:38:38Z | [
"python",
"console",
"persistence",
"terminal",
"console-application"
] |
Google Wave gadget configure / set properties | 2,258,985 | <p>How can you configure or mutate a Google Wave gadget after creating one in Python? The following code will load the gadget via XML:</p>
<pre><code>from waveapi import document
gadget = document.Gadget('http://domain.com/gadget.xml')
</code></pre>
<p>The <a href="http://wave-robot-python-client.googlecode.com/svn/t... | 1 | 2010-02-13T20:44:17Z | 2,261,171 | <p><code>Gadget</code> is derived from <code>Element</code> which states:</p>
<blockquote>
<p>Although a Robot can query the properties of an element it can only interact with the specific types that the element represents.</p>
</blockquote>
<p>The API does mention SubmitDelta() and get() , which seem like wrappers... | 1 | 2010-02-14T13:23:32Z | [
"python",
"google-wave"
] |
How to add an HTML class to a Django form's help_text? | 2,259,137 | <p>I have a simple form</p>
<pre><code>class MyForm(forms.Form):
...
fieldname = forms.CharField(help_text="Some help text")
</code></pre>
<p>I can then display this form with django's handy <code>{{ form.as_ul }}</code> if I like, now I need to stylise the help_text and I have no idea how. Django doesn't ap... | 9 | 2010-02-13T21:33:00Z | 2,259,619 | <p>I guess there is no other way, otherwise there probably wouldn't be a need for this ticket: <a href="http://code.djangoproject.com/ticket/8426" rel="nofollow">http://code.djangoproject.com/ticket/8426</a>.</p>
<p>You could apply the patch that is provided there.</p>
| 2 | 2010-02-14T00:00:57Z | [
"python",
"django"
] |
How to add an HTML class to a Django form's help_text? | 2,259,137 | <p>I have a simple form</p>
<pre><code>class MyForm(forms.Form):
...
fieldname = forms.CharField(help_text="Some help text")
</code></pre>
<p>I can then display this form with django's handy <code>{{ form.as_ul }}</code> if I like, now I need to stylise the help_text and I have no idea how. Django doesn't ap... | 9 | 2010-02-13T21:33:00Z | 2,259,632 | <p>There's only that much you can customize in UI from form options. The more flexible way to approach a problem is to create your own form template then and reuse it instead of <code>{{ form.as_something }}</code>. Read these topics from Django documentation:</p>
<ul>
<li><a href="https://docs.djangoproject.com/en/... | 7 | 2010-02-14T00:07:25Z | [
"python",
"django"
] |
How to add an HTML class to a Django form's help_text? | 2,259,137 | <p>I have a simple form</p>
<pre><code>class MyForm(forms.Form):
...
fieldname = forms.CharField(help_text="Some help text")
</code></pre>
<p>I can then display this form with django's handy <code>{{ form.as_ul }}</code> if I like, now I need to stylise the help_text and I have no idea how. Django doesn't ap... | 9 | 2010-02-13T21:33:00Z | 13,999,496 | <p>A more convenient way to do this is to:</p>
<pre><code>from django.utils.safestring import mark_safe
</code></pre>
<p>and then:</p>
<pre><code>field = models.TextField(help_text=mark_safe("some<br>html"))
</code></pre>
| 10 | 2012-12-22T02:16:50Z | [
"python",
"django"
] |
python: inserting a variable value into a variable name | 2,259,224 | <p>I would like to insert the value of a variable into the name of another variable in python. In a shell script this would be something like: </p>
<pre><code>for n in `more list`
do
var_$n = some_calculation
done
</code></pre>
<p>but I can't see how to do a similar thing in python. Is there a way or should ... | 1 | 2010-02-13T22:00:32Z | 2,259,238 | <p>a dict is one way to maintain an associative array.</p>
<pre><code>dict = {}
dict[str] = calc();
</code></pre>
| 0 | 2010-02-13T22:04:13Z | [
"python",
"variables"
] |
python: inserting a variable value into a variable name | 2,259,224 | <p>I would like to insert the value of a variable into the name of another variable in python. In a shell script this would be something like: </p>
<pre><code>for n in `more list`
do
var_$n = some_calculation
done
</code></pre>
<p>but I can't see how to do a similar thing in python. Is there a way or should ... | 1 | 2010-02-13T22:00:32Z | 2,259,244 | <p>Don't do it! Having variable names that change depending on the value of a variable leads to unnecessary complications. A cleaner way is to use a dictionary:</p>
<pre><code>vr={}
for n in alist:
vr[n]=some_calculation()
</code></pre>
| 6 | 2010-02-13T22:06:06Z | [
"python",
"variables"
] |
python: inserting a variable value into a variable name | 2,259,224 | <p>I would like to insert the value of a variable into the name of another variable in python. In a shell script this would be something like: </p>
<pre><code>for n in `more list`
do
var_$n = some_calculation
done
</code></pre>
<p>but I can't see how to do a similar thing in python. Is there a way or should ... | 1 | 2010-02-13T22:00:32Z | 2,259,247 | <p>Maybe not perfect, but two possible solutions:</p>
<pre>
>>> name = "var_1"
>>> locals()[name] = 5
>>> print var_1
5
>>> exec(name + "= 6")
>>> print var_1
6
</pre>
| -1 | 2010-02-13T22:06:42Z | [
"python",
"variables"
] |
python: inserting a variable value into a variable name | 2,259,224 | <p>I would like to insert the value of a variable into the name of another variable in python. In a shell script this would be something like: </p>
<pre><code>for n in `more list`
do
var_$n = some_calculation
done
</code></pre>
<p>but I can't see how to do a similar thing in python. Is there a way or should ... | 1 | 2010-02-13T22:00:32Z | 2,259,918 | <pre><code>for n in more_list:
globals()["var_"+str(n)]=some_calculation
</code></pre>
| 0 | 2010-02-14T02:24:08Z | [
"python",
"variables"
] |
How are booleans formatted in Strings in Python? | 2,259,228 | <p>I see I can't do:</p>
<pre><code>"%b %b" % (True, False)
</code></pre>
<p>in Python. I guessed <code>%b</code> for b(oolean). Is there something like this?</p>
| 81 | 2010-02-13T22:02:11Z | 2,259,245 | <p>If you want <code>True False</code> use:</p>
<pre><code>"%s %s" % (True, False)
</code></pre>
<p>because <code>str(True)</code> is <code>'True'</code> and <code>str(False)</code> is <code>'False'</code>.</p>
<p>or if you want <code>1 0</code> use:</p>
<pre><code>"%i %i" % (True, False)
</code></pre>
<p>because ... | 39 | 2010-02-13T22:06:22Z | [
"python",
"boolean",
"string-formatting"
] |
How are booleans formatted in Strings in Python? | 2,259,228 | <p>I see I can't do:</p>
<pre><code>"%b %b" % (True, False)
</code></pre>
<p>in Python. I guessed <code>%b</code> for b(oolean). Is there something like this?</p>
| 81 | 2010-02-13T22:02:11Z | 2,259,250 | <pre><code>>>> print "%r, %r" % (True, False)
True, False
</code></pre>
<p>This is not specific to boolean values - <code>%r</code> calls the <code>__repr__</code> method on the argument. <code>%s</code> (for <code>str</code>) should also work.</p>
| 119 | 2010-02-13T22:07:31Z | [
"python",
"boolean",
"string-formatting"
] |
How are booleans formatted in Strings in Python? | 2,259,228 | <p>I see I can't do:</p>
<pre><code>"%b %b" % (True, False)
</code></pre>
<p>in Python. I guessed <code>%b</code> for b(oolean). Is there something like this?</p>
| 81 | 2010-02-13T22:02:11Z | 27,902,667 | <p>You may also use the <a href="https://docs.python.org/3.3/library/string.html#string-formatting" rel="nofollow">Formatter class</a> of string</p>
<pre><code>print "{0} {1}".format(True, False);
print "{0:} {1:}".format(True, False);
print "{0:d} {1:d}".format(True, False);
print "{0:f} {1:f}".format(True, False);
p... | 3 | 2015-01-12T13:04:34Z | [
"python",
"boolean",
"string-formatting"
] |
AttributeError: 'module' object has no attribute 'printable' | 2,259,246 | <p>I have this problem.
Any idea? </p>
<pre><code>from OpenGL.raw.GLUT import *
File "/usr/lib/python2.6/site-packages/OpenGL/raw/GLUT/__init__.py", line 6, in <module>
from OpenGL.raw.GLUT.constants import *
File "/usr/lib/python2.6/site-packages/OpenGL/raw/GLUT/constants.py", line 7, in <... | 3 | 2010-02-13T22:06:34Z | 2,259,252 | <p>You have a file called <code>string.py</code> in the current directory. It's interfering with the standard library module of the same name. Rename the .py file, and remove the <code>string.pyc</code> file that was created.</p>
| 4 | 2010-02-13T22:07:50Z | [
"python",
"attributes"
] |
AttributeError: 'module' object has no attribute 'printable' | 2,259,246 | <p>I have this problem.
Any idea? </p>
<pre><code>from OpenGL.raw.GLUT import *
File "/usr/lib/python2.6/site-packages/OpenGL/raw/GLUT/__init__.py", line 6, in <module>
from OpenGL.raw.GLUT.constants import *
File "/usr/lib/python2.6/site-packages/OpenGL/raw/GLUT/constants.py", line 7, in <... | 3 | 2010-02-13T22:06:34Z | 32,460,947 | <p>I met the exact the same issue. But in a different context.</p>
<p>I setup a Ubuntu virtual machine with <code>VirtualBox</code>. I write some python things on a folder shared between the VM and the Windows host. (The shared folder is a feature of VirtualBox)</p>
<p>The reason for my doom is:</p>
<blockquote>
<... | 0 | 2015-09-08T14:51:35Z | [
"python",
"attributes"
] |
Pickle or json? | 2,259,270 | <p>I need to save to disk a little <code>dict</code> object which keys are <code>str</code>s and values are <code>int</code>s <strong>and then recover it</strong>. Something like this:</p>
<pre><code>{'juanjo': 2, 'pedro':99, 'other': 333}
</code></pre>
<p>What is the best option and why? Serialize it with <code>pick... | 49 | 2010-02-13T22:12:56Z | 2,259,313 | <p>If you do not have any interoperability requirements (e.g. you are just going to use the data with Python) and a binary format is fine, go with <a href="http://python.org/doc/2.5/lib/module-cPickle.html" rel="nofollow">cPickle</a> which gives you really fast Python object serialization.</p>
<p>If you want interoper... | 34 | 2010-02-13T22:22:51Z | [
"python",
"json",
"pickle"
] |
Pickle or json? | 2,259,270 | <p>I need to save to disk a little <code>dict</code> object which keys are <code>str</code>s and values are <code>int</code>s <strong>and then recover it</strong>. Something like this:</p>
<pre><code>{'juanjo': 2, 'pedro':99, 'other': 333}
</code></pre>
<p>What is the best option and why? Serialize it with <code>pick... | 49 | 2010-02-13T22:12:56Z | 2,259,351 | <p>I prefer JSON over pickle for my serialization. Unpickling can run arbitrary code, and using <code>pickle</code> to transfer data between programs or store data between sessions is a security hole. JSON does not introduce a security hole and is standardized, so the data can be accessed by programs in different langu... | 52 | 2010-02-13T22:33:07Z | [
"python",
"json",
"pickle"
] |
Pickle or json? | 2,259,270 | <p>I need to save to disk a little <code>dict</code> object which keys are <code>str</code>s and values are <code>int</code>s <strong>and then recover it</strong>. Something like this:</p>
<pre><code>{'juanjo': 2, 'pedro':99, 'other': 333}
</code></pre>
<p>What is the best option and why? Serialize it with <code>pick... | 49 | 2010-02-13T22:12:56Z | 2,259,424 | <p>JSON or pickle? How about JSON <em>and</em> pickle! You can use <code>jsonpickle</code>. It easy to use and the file on disk is readable because it's JSON.</p>
<p><a href="http://jsonpickle.github.com/" rel="nofollow">http://jsonpickle.github.com/</a></p>
| 8 | 2010-02-13T22:55:10Z | [
"python",
"json",
"pickle"
] |
Pickle or json? | 2,259,270 | <p>I need to save to disk a little <code>dict</code> object which keys are <code>str</code>s and values are <code>int</code>s <strong>and then recover it</strong>. Something like this:</p>
<pre><code>{'juanjo': 2, 'pedro':99, 'other': 333}
</code></pre>
<p>What is the best option and why? Serialize it with <code>pick... | 49 | 2010-02-13T22:12:56Z | 6,215,929 | <p>You might also find this interesting, with some charts to compare: <a href="http://kovshenin.com/archives/pickle-vs-json-which-is-faster/">http://kovshenin.com/archives/pickle-vs-json-which-is-faster/</a></p>
| 28 | 2011-06-02T14:30:41Z | [
"python",
"json",
"pickle"
] |
Pickle or json? | 2,259,270 | <p>I need to save to disk a little <code>dict</code> object which keys are <code>str</code>s and values are <code>int</code>s <strong>and then recover it</strong>. Something like this:</p>
<pre><code>{'juanjo': 2, 'pedro':99, 'other': 333}
</code></pre>
<p>What is the best option and why? Serialize it with <code>pick... | 49 | 2010-02-13T22:12:56Z | 24,430,695 | <p>Personally, I generally prefer JSON because the data is <strong>human-readable</strong>. Definitely, if you need to serialize something that JSON won't take, than use pickle.</p>
<p>But for most data storage, you won't need to serialize anything weird and JSON is much easier and always allows you to pop it open in ... | 3 | 2014-06-26T12:35:06Z | [
"python",
"json",
"pickle"
] |
Pickle or json? | 2,259,270 | <p>I need to save to disk a little <code>dict</code> object which keys are <code>str</code>s and values are <code>int</code>s <strong>and then recover it</strong>. Something like this:</p>
<pre><code>{'juanjo': 2, 'pedro':99, 'other': 333}
</code></pre>
<p>What is the best option and why? Serialize it with <code>pick... | 49 | 2010-02-13T22:12:56Z | 39,607,169 | <p>If you are primarily concerned with speed and space, use cPickle because cPickle is faster than JSON.</p>
<p>If you are more concerned with interoperability, security, and/or human readability, then use JSON.</p>
<hr>
<p>The tests results referenced in other answers were recorded in 2010, and the updated tests in... | 1 | 2016-09-21T03:42:36Z | [
"python",
"json",
"pickle"
] |
How to execute some code when a file is modified using python? | 2,259,336 | <p>I want to execute one function each time a file gets written with new data (gets modified) and I'm using Python.</p>
<p>How can I do it?</p>
| 0 | 2010-02-13T22:29:10Z | 2,259,357 | <p>I'm assuming that it's not necessarily your Python program that's modifying the file. I don't know if there's an OS-independent method. On Linux you can use <a href="http://trac.dbzteam.org/pyinotify" rel="nofollow">pyinotify</a>.</p>
| 2 | 2010-02-13T22:35:21Z | [
"python",
"file",
"function",
"execute"
] |
How to execute some code when a file is modified using python? | 2,259,336 | <p>I want to execute one function each time a file gets written with new data (gets modified) and I'm using Python.</p>
<p>How can I do it?</p>
| 0 | 2010-02-13T22:29:10Z | 2,259,371 | <p>If you want to monitor for changes in the file system using Python, see <a href="http://www.stepthreeprofit.com/2008/06/cross-platform-monitoring-of-filesystem.html">this article</a> for pointers to libraries which can help you achieve this on different platforms.</p>
<p>Short summary of available libraries for dif... | 5 | 2010-02-13T22:37:41Z | [
"python",
"file",
"function",
"execute"
] |
In PyObjC how do you get a sheet to end after using runModalForWindow_? | 2,259,377 | <p>I have a secondary window (a sheet) for a dialog controlled by a secondard WindowController. For some reason, the actions never get called in NSObject subclass after the sheet is displayed. I have confirmed and re-linked the actions. The code runs to runModalForWindow_ but then never receives the ok or cancel action... | 1 | 2010-02-13T22:39:30Z | 2,261,025 | <p>Your lines </p>
<pre><code> @objc.IBAction
def okSelected(self, sender):
</code></pre>
<p>should be</p>
<pre><code> @objc.IBAction
def okSelected_(self, sender):
</code></pre>
<p>etc. Remember, every colon in an Objective-C selector becomes an _ in Python!</p>
| 2 | 2010-02-14T12:27:29Z | [
"python",
"cocoa",
"pyobjc"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.