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 listlengths 1 5 |
|---|---|---|---|---|---|---|---|---|---|
Can't find my PYTHONPATH | 859,594 | <p>I'm trying to change my PYTHONPATH. I've tried to change it in "My Computer" etc, but it doesn't exist there. I searched in the registry in some places, and even ran a whole search for the word 'PYTHONPATH', but to no avail.</p>
<p>However, it Python I can easily see it exists. So where is it?</p>
| 9 | 2009-05-13T18:28:59Z | 3,079,003 | <p>MacOS 10.5.8, Python 2.6, Eclipse+Pydev 1.5.7</p>
<ol>
<li><p>Python installation's site-package is, for example:
<pre><code>/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages</pre></code></p></li>
<li><p>create symlinks YOUR LIBRARY inside into site-package, for example:
<pre><code>cd /L... | 1 | 2010-06-20T11:09:30Z | [
"python",
"windows",
"path"
] |
Can't find my PYTHONPATH | 859,594 | <p>I'm trying to change my PYTHONPATH. I've tried to change it in "My Computer" etc, but it doesn't exist there. I searched in the registry in some places, and even ran a whole search for the word 'PYTHONPATH', but to no avail.</p>
<p>However, it Python I can easily see it exists. So where is it?</p>
| 9 | 2009-05-13T18:28:59Z | 6,400,643 | <p>And, as with all good things in life, you can find it in the documentation:
<a href="http://docs.python.org/install/index.html#modifying-python-s-search-path" rel="nofollow">http://docs.python.org/install/index.html#modifying-python-s-search-path</a></p>
| 1 | 2011-06-19T04:57:21Z | [
"python",
"windows",
"path"
] |
whoami in python | 860,140 | <p>What is the best way to find out the user that a python process is running under?</p>
<p>I could do this:</p>
<pre><code>name = os.popen('whoami').read()
</code></pre>
<p>But that has to start a whole new process.</p>
<pre><code>os.environ["USER"]
</code></pre>
<p>works sometimes, but sometimes that environmen... | 24 | 2009-05-13T20:09:24Z | 860,154 | <p>This should work under Unix.</p>
<pre><code>import os
print os.getuid() # numeric uid
import pwd
print pwd.getpwuid(os.getuid()) # full /etc/passwd info
</code></pre>
| 18 | 2009-05-13T20:12:10Z | [
"python",
"posix"
] |
whoami in python | 860,140 | <p>What is the best way to find out the user that a python process is running under?</p>
<p>I could do this:</p>
<pre><code>name = os.popen('whoami').read()
</code></pre>
<p>But that has to start a whole new process.</p>
<pre><code>os.environ["USER"]
</code></pre>
<p>works sometimes, but sometimes that environmen... | 24 | 2009-05-13T20:09:24Z | 860,156 | <pre><code>import getpass
print getpass.getuser()
</code></pre>
<p>See the documentation of the <a href="http://docs.python.org/library/getpass.html">getpass</a> module.</p>
<blockquote>
<p>getpass.getuser()</p>
<p>Return the âlogin nameâ of the user. Availability: Unix, Windows.</p>
<p>This function ... | 50 | 2009-05-13T20:12:15Z | [
"python",
"posix"
] |
django and mod_wsgi having database connection issues | 860,169 | <p>I've noticed that whenever I enable the database settings on my django project (starting to notice a trend in my questions?) it gives me an internal server error. Setting the database settings to be blank makes the error go away. Here are the apache error logs that it outputs.</p>
<pre><code>mod_wsgi (pid=770): Exc... | 2 | 2009-05-13T20:13:16Z | 861,806 | <p>You need to set the <code>PYTHON_EGG_CACHE</code> environment variable. Apache/mod_wsgi is trying to extract the egg into a directory that Apache doesn't have write access to....or that doesn't exist.</p>
<p>It's explained in the <a href="http://docs.djangoproject.com/en/dev/howto/deployment/modpython/#using-eggs-... | 8 | 2009-05-14T06:15:16Z | [
"python",
"django",
"mod-wsgi"
] |
Emacs function to add symbol to __all__ in Python mode? | 860,357 | <p>Is there an existing Emacs function that adds the symbol currently under the point to <code>__all__</code> when editing Python code?</p>
<p>E.g., say the cursor was on the first <code>o</code> in <code>foo</code>:</p>
<pre><code># v---- cursor is on that 'o'
def foo():
return 42
</code></pre>
<p>If you did... | 5 | 2009-05-13T20:50:43Z | 860,569 | <p>Not being a python programmer, I'm not sure this covers all the cases, but works for me in a simple case. It'll add the symbol to the array if the array exists, and create <code>__all__</code> if it doesn't exist. <em>Note: it does not parse the array to avoid double insertion.</em></p>
<pre><code>(defun python-a... | 10 | 2009-05-13T21:40:04Z | [
"python",
"emacs"
] |
GUI app spawned from a LocalSystem Service (via CreateProcessAsUser) does not have focus | 860,428 | <p>I have created a service which display a sort of splash screen on the desktop of a specific user and only when that user is logged in (kiosk user).</p>
<p>That splash screen, once entered a valid code, will tell that to the service and the service goes to sleep for an x amount of time (depending of the code).</p>
... | 1 | 2009-05-13T21:04:40Z | 860,711 | <p>Have you tried launching another processes than your own from the service to see if it gets focus? Like notepad and see if it steals focus from your browser? If so perhaps its the program that can take back the focus when it starts.</p>
<p>I otherwise beilive it's the wShowWindow attribute from the STARTUPINFO stru... | 2 | 2009-05-13T22:18:23Z | [
"python",
"windows-services",
"tkinter",
"pywin32"
] |
GUI app spawned from a LocalSystem Service (via CreateProcessAsUser) does not have focus | 860,428 | <p>I have created a service which display a sort of splash screen on the desktop of a specific user and only when that user is logged in (kiosk user).</p>
<p>That splash screen, once entered a valid code, will tell that to the service and the service goes to sleep for an x amount of time (depending of the code).</p>
... | 1 | 2009-05-13T21:04:40Z | 892,142 | <p>For various very legitimated reasons, Microsoft would rather not see a service launching an app and stealing focus, however I found the following work around to still accomplish what I want.</p>
<p>The original intend is to have a kiosk like application hindered by a pass code like splash screen, which upon enterin... | 0 | 2009-05-21T09:44:09Z | [
"python",
"windows-services",
"tkinter",
"pywin32"
] |
Lay out import pathing in Python, straight and simple? | 860,672 | <p>If a group of Python developers wants to put their shared code somewhere, in a hierarchical structure, what's the structure, and what's the related "import" syntax?</p>
<p>Does java-style reference work in Python also? I.e., do directories correspond to dots? </p>
<p>What is standard setup for an internal-use-on... | 2 | 2009-05-13T22:06:46Z | 860,721 | <blockquote>
<p>If a group of Python developers wants to put their shared code somewhere, in a hierarchical structure, what's the structure, and what's the related "import" syntax?</p>
</blockquote>
<p>You put it in your C:\python26\Lib\site-packages\ directory under your own folder.</p>
<p>Inside that folder you s... | 2 | 2009-05-13T22:20:40Z | [
"python",
"import"
] |
Lay out import pathing in Python, straight and simple? | 860,672 | <p>If a group of Python developers wants to put their shared code somewhere, in a hierarchical structure, what's the structure, and what's the related "import" syntax?</p>
<p>Does java-style reference work in Python also? I.e., do directories correspond to dots? </p>
<p>What is standard setup for an internal-use-on... | 2 | 2009-05-13T22:06:46Z | 860,876 | <p>What we do.</p>
<p><strong>Development</strong></p>
<ul>
<li><p>c:\someroot\project\thing__init__.py # makes thing a package</p></li>
<li><p>c:\someroot\project\thing\foo.py</p></li>
<li><p>c:\someroot\project\thing\bar.py</p></li>
</ul>
<p>Our "environment" (set in a variety of ways</p>
<pre><code>SET PYTHONPAT... | 6 | 2009-05-13T23:13:53Z | [
"python",
"import"
] |
How use __setattr__ & __getattr__ for map INI values? | 860,744 | <p>I want to map a INI file as a python object. So if the file have:</p>
<pre><code>[UserOptions]
SampleFile = sample.txt
SamplePort = 80
SampleInt = 1
Sample = Aja
SampleDate = 10/02/2008
</code></pre>
<p>Then I want:</p>
<pre><code>c = Configuration('sample.ini')
c.UserOptions.SamplePort = 90
</code></pre>
<p>I'... | 4 | 2009-05-13T22:25:58Z | 860,852 | <p>You are trying to get the sections on request. But it is much easier to iterate over sections and options and add them as attribute in <code>__init__</code>. I edited my example to support setattr as well. You problem is explained <a href="http://docs.python.org/reference/datamodel.html#object.%5F%5Fsetattr%5F%5F" r... | 4 | 2009-05-13T23:04:38Z | [
"python",
"configuration-files"
] |
How use __setattr__ & __getattr__ for map INI values? | 860,744 | <p>I want to map a INI file as a python object. So if the file have:</p>
<pre><code>[UserOptions]
SampleFile = sample.txt
SamplePort = 80
SampleInt = 1
Sample = Aja
SampleDate = 10/02/2008
</code></pre>
<p>Then I want:</p>
<pre><code>c = Configuration('sample.ini')
c.UserOptions.SamplePort = 90
</code></pre>
<p>I'... | 4 | 2009-05-13T22:25:58Z | 860,867 | <p>Your problem is in <code>Section.__init__</code>. When you set <code>self.__name = name</code> it calls your <code>__setattr__</code> method, doesn't find the key in <code>__dict__</code> so it goes to</p>
<pre><code> self.__parser.set('UserOptions',item, value)
</code></pre>
<p>So now it needs <code>self.__parser... | 4 | 2009-05-13T23:11:08Z | [
"python",
"configuration-files"
] |
Is python automagically parallelizing IO- and CPU- or memory-bound sections? | 860,893 | <p>This is a follow-up questions on a <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous one</a>.</p>
<p>Consider this code, which is less toyish than the one in the <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous question... | 3 | 2009-05-13T23:21:09Z | 860,951 | <p>How big are the lines in your file? If they're not very long (anything under about 1K probably qualifies) then you're likely seeing performance gains because of input buffering.</p>
| 1 | 2009-05-13T23:37:27Z | [
"python",
"linux",
"performance",
"text-files"
] |
Is python automagically parallelizing IO- and CPU- or memory-bound sections? | 860,893 | <p>This is a follow-up questions on a <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous one</a>.</p>
<p>Consider this code, which is less toyish than the one in the <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous question... | 3 | 2009-05-13T23:21:09Z | 860,988 | <p>Why do you think list.append() would be a slower operation? It is extremely fast, considering the internal pointer arrays used by lists to hold references to the objects in them are allocated in increasingly large blocks, so that every append does not actually re-allocate the array, and most can simply increment the... | 1 | 2009-05-13T23:52:30Z | [
"python",
"linux",
"performance",
"text-files"
] |
Is python automagically parallelizing IO- and CPU- or memory-bound sections? | 860,893 | <p>This is a follow-up questions on a <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous one</a>.</p>
<p>Consider this code, which is less toyish than the one in the <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous question... | 3 | 2009-05-13T23:21:09Z | 861,004 | <blockquote>
<p>Obviously data.append() is happening in parallel with the IO.</p>
</blockquote>
<p>I'm afraid not. It <em>is</em> possible to parallelize IO and computation in Python, but it doesn't happen magically.</p>
<p>One thing you could do is use <code>posix_fadvise(2)</code> to give the OS a hint that you ... | 8 | 2009-05-13T23:58:13Z | [
"python",
"linux",
"performance",
"text-files"
] |
Is python automagically parallelizing IO- and CPU- or memory-bound sections? | 860,893 | <p>This is a follow-up questions on a <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous one</a>.</p>
<p>Consider this code, which is less toyish than the one in the <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous question... | 3 | 2009-05-13T23:21:09Z | 861,013 | <p>I don't see any evidence that "data.append() is happening in parallel with the IO." Like Benji, I don't think this is automatic in the way you think. You showed that doing data.append(line[-1]) takes about the same amount of time as lc = lc + 1 (essentially no time at all, compared to the IO and line splitting). ... | 1 | 2009-05-14T00:02:32Z | [
"python",
"linux",
"performance",
"text-files"
] |
Is python automagically parallelizing IO- and CPU- or memory-bound sections? | 860,893 | <p>This is a follow-up questions on a <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous one</a>.</p>
<p>Consider this code, which is less toyish than the one in the <a href="http://stackoverflow.com/questions/849058/is-it-possible-to-speed-up-python-io">previous question... | 3 | 2009-05-13T23:21:09Z | 861,513 | <p>If your run times are varying by that amount for the second example, I'd suspect your method of timing or outside influences (other processes / system load) to be skewing the times to the point where they don't give any reliable information.</p>
| 1 | 2009-05-14T04:11:56Z | [
"python",
"linux",
"performance",
"text-files"
] |
How to do this join query in Django | 860,941 | <p>In Django, I have two models:</p>
<pre><code>class Product(models.Model):
name = models.CharField(max_length = 50)
categories = models.ManyToManyField(Category)
class ProductRank(models.Model):
product = models.ForeignKey(Product)
rank = models.IntegerField(default = 0)
</code></pre>
<p>I put the ... | 8 | 2009-05-13T23:34:24Z | 860,985 | <p>Add a call to the QuerySet's select_related() method, though I'm not positive that grabs references in both directions, it is the most likely answer.</p>
| 2 | 2009-05-13T23:50:08Z | [
"python",
"sql",
"django"
] |
How to do this join query in Django | 860,941 | <p>In Django, I have two models:</p>
<pre><code>class Product(models.Model):
name = models.CharField(max_length = 50)
categories = models.ManyToManyField(Category)
class ProductRank(models.Model):
product = models.ForeignKey(Product)
rank = models.IntegerField(default = 0)
</code></pre>
<p>I put the ... | 8 | 2009-05-13T23:34:24Z | 860,997 | <p>This can be done in Django, but you will need to restructure your models a little bit differently:</p>
<pre><code>class Product(models.Model):
name = models.CharField(max_length=50)
product_rank = models.OneToOneField('ProductRank')
class ProductRank(models.Model):
rank = models.IntegerField(default=0)... | 10 | 2009-05-13T23:55:05Z | [
"python",
"sql",
"django"
] |
How to do this join query in Django | 860,941 | <p>In Django, I have two models:</p>
<pre><code>class Product(models.Model):
name = models.CharField(max_length = 50)
categories = models.ManyToManyField(Category)
class ProductRank(models.Model):
product = models.ForeignKey(Product)
rank = models.IntegerField(default = 0)
</code></pre>
<p>I put the ... | 8 | 2009-05-13T23:34:24Z | 861,156 | <p>I haven't checked but:</p>
<pre><code>products = Product.objects.filter(categories__pk=1).select_related()
</code></pre>
<p>Should grab every instance.</p>
| 4 | 2009-05-14T01:20:40Z | [
"python",
"sql",
"django"
] |
A class method which behaves differently when called as an instance method? | 861,055 | <p>I'm wondering if it's possible to make a method which behaves differently when called as a class method than when called as an instance method.</p>
<p>For example, as a skills-improvement project, I'm writing a <code>Matrix</code> class (yes, I know there are perfectly good matrix classes already out there). I've c... | 24 | 2009-05-14T00:26:24Z | 861,092 | <p>I think the larger problem is that you are overloading the name 'bar' on class 'Foo', something python doesn't allow. The second definition of 'bar' clobbers the first definition of 'bar'. </p>
<p>Try to think of unique names for your classmethod and instance method. i.e.</p>
<pre><code>@classmethod
def create(c... | 3 | 2009-05-14T00:44:27Z | [
"python",
"class",
"methods"
] |
A class method which behaves differently when called as an instance method? | 861,055 | <p>I'm wondering if it's possible to make a method which behaves differently when called as a class method than when called as an instance method.</p>
<p>For example, as a skills-improvement project, I'm writing a <code>Matrix</code> class (yes, I know there are perfectly good matrix classes already out there). I've c... | 24 | 2009-05-14T00:26:24Z | 861,109 | <p>Questionably useful Python hacks are my forte.</p>
<pre><code>from types import *
class Foo(object):
def __init__(self):
self.bar = methodize(bar, self)
self.baz = 999
@classmethod
def bar(cls, baz):
return 2 * baz
def methodize(func, instance):
return MethodType(func, in... | 35 | 2009-05-14T00:56:35Z | [
"python",
"class",
"methods"
] |
A class method which behaves differently when called as an instance method? | 861,055 | <p>I'm wondering if it's possible to make a method which behaves differently when called as a class method than when called as an instance method.</p>
<p>For example, as a skills-improvement project, I'm writing a <code>Matrix</code> class (yes, I know there are perfectly good matrix classes already out there). I've c... | 24 | 2009-05-14T00:26:24Z | 861,119 | <p><strong>[edited: use attribute to be a more direct answer; see the helpful comment by John Fouhy]</strong></p>
<p>You can use a descriptor to do what you want:</p>
<pre><code>class cls_or_inst_method(object):
def __init__(self, class_method, instance_method):
self.class_method = class_method
se... | 6 | 2009-05-14T01:00:08Z | [
"python",
"class",
"methods"
] |
A class method which behaves differently when called as an instance method? | 861,055 | <p>I'm wondering if it's possible to make a method which behaves differently when called as a class method than when called as an instance method.</p>
<p>For example, as a skills-improvement project, I'm writing a <code>Matrix</code> class (yes, I know there are perfectly good matrix classes already out there). I've c... | 24 | 2009-05-14T00:26:24Z | 861,137 | <p>@Unknown What's the difference between your's and this:</p>
<pre><code>class Foo(object):
def _bar(self, baz):
print "_bar, baz:", baz
def __init__(self, bar):
self.bar = self._bar
self.baz = bar
@classmethod
def bar(cls, baz):
print "bar, baz:", baz
In [1]: impor... | 7 | 2009-05-14T01:14:02Z | [
"python",
"class",
"methods"
] |
A class method which behaves differently when called as an instance method? | 861,055 | <p>I'm wondering if it's possible to make a method which behaves differently when called as a class method than when called as an instance method.</p>
<p>For example, as a skills-improvement project, I'm writing a <code>Matrix</code> class (yes, I know there are perfectly good matrix classes already out there). I've c... | 24 | 2009-05-14T00:26:24Z | 861,177 | <p>You can reassign your identity method in <strong>init</strong> with short lambda function:</p>
<pre><code>class Matrix(object):
def __init__(self):
self.identity = lambda s=self:s.__class__.identity(s)
#...whatever initialization code you have...
self.size = 10
@classmethod... | 2 | 2009-05-14T01:32:30Z | [
"python",
"class",
"methods"
] |
How do capture groups work? (wrt python regular expressions) | 861,060 | <p>While using regex to help solve a problem in the <a href="http://www.pythonchallenge.com/" rel="nofollow">Python Challenge</a>, I came across some behaviour that confused me.</p>
<p>from <a href="http://docs.python.org/library/re.html#regular-expression-syntax" rel="nofollow">here</a>:</p>
<p>(...) Matches whateve... | 1 | 2009-05-14T00:27:52Z | 861,070 | <p>Because you only have one capturing group, but it's "run" repeatedly, the new matches are repeatedly entered into the "storage space" for that group. In other words, the <code>1</code>s were lost when they were "overwritten" by subsequent <code>1</code>s and eventually the <code>2</code>.</p>
| 9 | 2009-05-14T00:31:22Z | [
"python",
"regex"
] |
How do capture groups work? (wrt python regular expressions) | 861,060 | <p>While using regex to help solve a problem in the <a href="http://www.pythonchallenge.com/" rel="nofollow">Python Challenge</a>, I came across some behaviour that confused me.</p>
<p>from <a href="http://docs.python.org/library/re.html#regular-expression-syntax" rel="nofollow">here</a>:</p>
<p>(...) Matches whateve... | 1 | 2009-05-14T00:27:52Z | 861,076 | <p>You are repeating the group itself by appending '+' after ')', I do not know the implementation details but it matches 7 times, and returns only the last match.</p>
<p>In the first one, you are matching 7 digits, and making it a group.</p>
| 1 | 2009-05-14T00:33:45Z | [
"python",
"regex"
] |
Ordering a list of dictionaries in python | 861,190 | <p>I've got a python list of dictionaries:</p>
<pre><code>mylist = [
{'id':0, 'weight':10, 'factor':1, 'meta':'ABC'},
{'id':1, 'weight':5, 'factor':1, 'meta':'ABC'},
{'id':2, 'weight':5, 'factor':2, 'meta':'ABC'},
{'id':3, 'weight':1, 'factor':1, 'meta':'ABC'}
]
</code></pre>
<p>Whats the most efficient/cleanest way ... | 7 | 2009-05-14T01:41:03Z | 861,218 | <p>Something along the lines of the following ought to work:</p>
<pre><code>def cmp_dict(x, y):
weight_diff = y['weight'] - x['weight']
if weight_diff == 0:
return y['factor'] - x['factor']
else:
return weight_diff
myList.sort(cmp_dict)
</code></pre>
| 1 | 2009-05-14T01:52:03Z | [
"python",
"list",
"dictionary",
"order"
] |
Ordering a list of dictionaries in python | 861,190 | <p>I've got a python list of dictionaries:</p>
<pre><code>mylist = [
{'id':0, 'weight':10, 'factor':1, 'meta':'ABC'},
{'id':1, 'weight':5, 'factor':1, 'meta':'ABC'},
{'id':2, 'weight':5, 'factor':2, 'meta':'ABC'},
{'id':3, 'weight':1, 'factor':1, 'meta':'ABC'}
]
</code></pre>
<p>Whats the most efficient/cleanest way ... | 7 | 2009-05-14T01:41:03Z | 861,238 | <pre><code>mylist.sort(key=lambda d: (d['weight'], d['factor']))
</code></pre>
<p>or</p>
<pre><code>import operator
mylist.sort(key=operator.itemgetter('weight', 'factor'))
</code></pre>
| 23 | 2009-05-14T01:59:25Z | [
"python",
"list",
"dictionary",
"order"
] |
Ordering a list of dictionaries in python | 861,190 | <p>I've got a python list of dictionaries:</p>
<pre><code>mylist = [
{'id':0, 'weight':10, 'factor':1, 'meta':'ABC'},
{'id':1, 'weight':5, 'factor':1, 'meta':'ABC'},
{'id':2, 'weight':5, 'factor':2, 'meta':'ABC'},
{'id':3, 'weight':1, 'factor':1, 'meta':'ABC'}
]
</code></pre>
<p>Whats the most efficient/cleanest way ... | 7 | 2009-05-14T01:41:03Z | 861,355 | <p>I accepted dF's answer for the inspiration, but here is what I ultimately settled on for my scenario:</p>
<pre><code>@staticmethod
def ordered_list(mylist):
def sort_func(d):
return (d['weight'], d['factor'])
mylist.sort(key=sort_func)
</code></pre>
| 1 | 2009-05-14T03:00:47Z | [
"python",
"list",
"dictionary",
"order"
] |
Ordering a list of dictionaries in python | 861,190 | <p>I've got a python list of dictionaries:</p>
<pre><code>mylist = [
{'id':0, 'weight':10, 'factor':1, 'meta':'ABC'},
{'id':1, 'weight':5, 'factor':1, 'meta':'ABC'},
{'id':2, 'weight':5, 'factor':2, 'meta':'ABC'},
{'id':3, 'weight':1, 'factor':1, 'meta':'ABC'}
]
</code></pre>
<p>Whats the most efficient/cleanest way ... | 7 | 2009-05-14T01:41:03Z | 863,820 | <pre><code>decoratedlist = [(item[weight], item) for item in mylist]
decoratedlist.sort()
results = [item for (key, item) in decoratedlist]
</code></pre>
| -1 | 2009-05-14T14:57:51Z | [
"python",
"list",
"dictionary",
"order"
] |
Use of cycle in django | 861,855 | <p>I have a webpage where I am looping,and using cycle inside the loop.</p>
<pre><code>{% for o in something %}
{% for c in o %}
<div class="{% cycle 'white' 'black'%}"></div>
{% endfor %}
</code></pre>
<p>Now, this means everytime inside the loop, first div tag gets white.But,what I want is to alternate... | 5 | 2009-05-14T06:33:33Z | 861,952 | <p>Something like this might work (untested):</p>
<pre><code>{% for o in something %}
{% for c in o %}
{% ifchanged forloop.parent.counter %}
<div class="{% cycle 'white' 'black' %}"></div>
{% else %}
<div class="{% cycle 'black' 'white' %}"></div>
{% endifchanged %}
{% endfor %}
... | 0 | 2009-05-14T07:10:21Z | [
"python",
"django",
"django-templates"
] |
Use of cycle in django | 861,855 | <p>I have a webpage where I am looping,and using cycle inside the loop.</p>
<pre><code>{% for o in something %}
{% for c in o %}
<div class="{% cycle 'white' 'black'%}"></div>
{% endfor %}
</code></pre>
<p>Now, this means everytime inside the loop, first div tag gets white.But,what I want is to alternate... | 5 | 2009-05-14T06:33:33Z | 3,765,829 | <p>There is an accept <a href="http://code.djangoproject.com/ticket/5908" rel="nofollow">bug</a> open about this issue. You may want to try the proposed change to see if it works for you.</p>
<p>If you do not want to try it, or it does not work, give this a shot:</p>
<pre><code>{% cycle 'white' 'black' as divcolors %... | 4 | 2010-09-22T02:03:26Z | [
"python",
"django",
"django-templates"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 861,869 | <p>The pypy project offers sandboxing features, see <a href="http://codespeak.net/pypy/dist/pypy/doc/sandbox.html" rel="nofollow">http://codespeak.net/pypy/dist/pypy/doc/sandbox.html</a> .</p>
| 3 | 2009-05-14T06:41:01Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 861,875 | <p>No there is no production ready subset of Python that is "safe". Python has had a few sand box modules which were deprecated due to deficiencies.</p>
<p>Your best bet is to either create your own parser, or isolate the python process with syscall hooks and a jailed account.</p>
<p>Some people might point you to Py... | 3 | 2009-05-14T06:42:57Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 861,947 | <p>AFAIK, some attempts were made in standard python library, but they were not successful. See <a href="http://docs.python.org/library/restricted.html" rel="nofollow">Restricted Execution</a> for details.</p>
<blockquote>
<p>Warning</p>
<p>In Python 2.3 these modules have been
disabled due to various known a... | 1 | 2009-05-14T07:09:14Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 861,961 | <p>Here are a couple of links to give you an idea on what you're up against:</p>
<ul>
<li><a href="https://wiki.python.org/moin/Asking%20for%20Help/How%20can%20I%20run%20an%20untrusted%20Python%20script%20safely%20%28i.e.%20Sandbox%29" rel="nofollow">How can I run an untrusted Python script safely (i.e. Sandbox)</a></... | 8 | 2009-05-14T07:12:56Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 861,969 | <blockquote>
<p>I'd be wary about releasing such
applications into the wild for fear of
someone either accidentally, or
maliciously, adding destructive code
to the file.</p>
</blockquote>
<p>Your native code that's "in the wild" is equally vulnerable to this attack; that it is in machine code is just a speed... | 1 | 2009-05-14T07:15:18Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 862,025 | <p>You might try IronPython on Silverlight/Moonlight, as <a href="http://www.trypython.org/" rel="nofollow">these guys</a> impressively seem to be doing. There is a lot of great information on these types of IronPython applications from the Resolver One developers <a href="http://www.voidspace.org.uk/ironpython/silver... | 1 | 2009-05-14T07:35:37Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 862,030 | <p>I don't really know much about exactly what security capabilities you get within the Java Virtual Machine or .NET runtimes, but you might want to consider if running your python code with <a href="http://www.jython.org/Project/" rel="nofollow">Jython</a> or <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName... | 1 | 2009-05-14T07:37:09Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 866,612 | <p>For some discussion on issues previously met with the <code>rexec</code> module:</p>
<ul>
<li><a href="http://mail.python.org/pipermail/python-dev/2002-December/031160.html" rel="nofollow">http://mail.python.org/pipermail/python-dev/2002-December/031160.html</a></li>
<li><a href="http://mail.python.org/pipermail/py... | 0 | 2009-05-15T01:03:10Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 1,000,418 | <p>It's a little hard to understand what you're trying to do -- not enough details.</p>
<p>Are you hosting the native app and allowing the users to write plugins? Consider using an OS-level solution by running the Python application as a separate runtime process inside a jail/chroot/similar, and communicating over soc... | 1 | 2009-06-16T09:27:19Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 1,036,090 | <p>tinypy (<a href="http://tinypy.org" rel="nofollow">tinypy.org</a>) was made to be a small, embed-able Python subset written in the style of Lua. And as lua has a manner to create a sandbox, I estimate that tinypy could be hacked along the same vein. Since tinypy's code base is so small, it's pretty easy to learn a... | 2 | 2009-06-24T01:49:19Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 1,134,995 | <p>The <a href="http://pythononachip.org" rel="nofollow">PyMite VM</a> fits the bill if all you need to do is set simple variables, loops, conditionals and functions. PyMite is tiny, written in C, uses a static memory pool and can be embedded. It has an extremely limited set of builtin functions that is easy to confi... | 3 | 2009-07-16T01:30:08Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Is there a "safe" subset of Python for use as an embedded scripting language? | 861,864 | <p>In the many Python applications I've created, I often create simple modules containing nothing but constants to be used as config files. Additionally, because the config file is actually a Python code file, I can add simple logic for changing variables depending on a debug level, etc.</p>
<p>While this works great... | 7 | 2009-05-14T06:37:43Z | 1,135,067 | <p>This sounds like what you want: <a href="http://lwn.net/Articles/321872/" rel="nofollow">Reviving Python restricted mode</a>.</p>
<p>The Python interpreter has a built-in "restricted" mode, enabled by changing the <code>__builtins__</code> magic variable. The article <a href="http://tav.espians.com/paving-the-way-t... | 0 | 2009-07-16T02:00:45Z | [
"python",
"security",
"scripting",
"scripting-language",
"embedded-language"
] |
Get a unique list/tuple element given a condition in python | 862,158 | <p>How do I get a tuple/list element given a condition in python?
This occurs pretty often and I am looking for a nice-few-lines-pythonic way of doing this.</p>
<p>here could be an example:</p>
<p>Consider a tuple containing 2D points coordinates like this:</p>
<pre><code>points = [[x1, y1],[x2, y2],[x3, y3], ...]
<... | 2 | 2009-05-14T08:18:22Z | 862,191 | <pre><code>min(points, key=lambda x: dist(pointToCompare2, x))
</code></pre>
<p><a href="http://docs.python.org/library/functions.html#min">min is a built-in function</a>.</p>
| 11 | 2009-05-14T08:27:07Z | [
"python",
"list",
"tuples",
"condition"
] |
Get a unique list/tuple element given a condition in python | 862,158 | <p>How do I get a tuple/list element given a condition in python?
This occurs pretty often and I am looking for a nice-few-lines-pythonic way of doing this.</p>
<p>here could be an example:</p>
<p>Consider a tuple containing 2D points coordinates like this:</p>
<pre><code>points = [[x1, y1],[x2, y2],[x3, y3], ...]
<... | 2 | 2009-05-14T08:18:22Z | 17,235,561 | <pre><code>v = [1,3,2,4,7,3,3,4,5,11]
def get_num_uniques(v):
count = []
dup = []
tmp = []
for i in v:
if i not in count:
count.append(i)
else:
dup.append(i)
count.remove(i)
for j in count:
if j not in dup:
tmp.append(j)
return tmp
#Call the function
print get_num_... | 0 | 2013-06-21T12:44:11Z | [
"python",
"list",
"tuples",
"condition"
] |
How to download a file using python in a 'smarter' way? | 862,173 | <p>I need to download several files via http in Python.</p>
<p>The most obvious way to do it is just using urllib2:</p>
<pre><code>import urllib2
u = urllib2.urlopen('http://server.com/file.html')
localFile = open('file.html', 'w')
localFile.write(u.read())
localFile.close()
</code></pre>
<p>But I'll have to deal wi... | 63 | 2009-05-14T08:21:42Z | 862,198 | <p>Download scripts like that tend to push a header telling the user-agent what to name the file:</p>
<pre><code>Content-Disposition: attachment; filename="the filename.ext"
</code></pre>
<p>If you can grab that header, you can get the proper filename.</p>
<p>There's <a href="http://stackoverflow.com/questions/16300... | 38 | 2009-05-14T08:28:43Z | [
"python",
"http",
"download"
] |
How to download a file using python in a 'smarter' way? | 862,173 | <p>I need to download several files via http in Python.</p>
<p>The most obvious way to do it is just using urllib2:</p>
<pre><code>import urllib2
u = urllib2.urlopen('http://server.com/file.html')
localFile = open('file.html', 'w')
localFile.write(u.read())
localFile.close()
</code></pre>
<p>But I'll have to deal wi... | 63 | 2009-05-14T08:21:42Z | 863,017 | <p>Based on comments and @Oli's anwser, I made a solution like this:</p>
<pre><code>from os.path import basename
from urlparse import urlsplit
def url2name(url):
return basename(urlsplit(url)[2])
def download(url, localFileName = None):
localName = url2name(url)
req = urllib2.Request(url)
r = urllib2... | 33 | 2009-05-14T12:23:12Z | [
"python",
"http",
"download"
] |
How to download a file using python in a 'smarter' way? | 862,173 | <p>I need to download several files via http in Python.</p>
<p>The most obvious way to do it is just using urllib2:</p>
<pre><code>import urllib2
u = urllib2.urlopen('http://server.com/file.html')
localFile = open('file.html', 'w')
localFile.write(u.read())
localFile.close()
</code></pre>
<p>But I'll have to deal wi... | 63 | 2009-05-14T08:21:42Z | 2,067,142 | <p>Combining much of the above, here is a more pythonic solution:</p>
<pre><code>import urllib2
import shutil
import urlparse
import os
def download(url, fileName=None):
def getFileName(url,openUrl):
if 'Content-Disposition' in openUrl.info():
# If the response has Content-Disposition, try to ... | 22 | 2010-01-14T19:54:30Z | [
"python",
"http",
"download"
] |
How to download a file using python in a 'smarter' way? | 862,173 | <p>I need to download several files via http in Python.</p>
<p>The most obvious way to do it is just using urllib2:</p>
<pre><code>import urllib2
u = urllib2.urlopen('http://server.com/file.html')
localFile = open('file.html', 'w')
localFile.write(u.read())
localFile.close()
</code></pre>
<p>But I'll have to deal wi... | 63 | 2009-05-14T08:21:42Z | 2,503,668 | <p><strong>2 Kender</strong>:</p>
<pre><code>if localName[0] == '"' or localName[0] == "'":
localName = localName[1:-1]
</code></pre>
<p>it is not safe -- web server can pass wrong formatted name as ["file.ext] or [file.ext'] or even be empty and <strong>localName[0]</strong> will raise exception.
Correct code ca... | 1 | 2010-03-23T21:12:58Z | [
"python",
"http",
"download"
] |
How to download a file using python in a 'smarter' way? | 862,173 | <p>I need to download several files via http in Python.</p>
<p>The most obvious way to do it is just using urllib2:</p>
<pre><code>import urllib2
u = urllib2.urlopen('http://server.com/file.html')
localFile = open('file.html', 'w')
localFile.write(u.read())
localFile.close()
</code></pre>
<p>But I'll have to deal wi... | 63 | 2009-05-14T08:21:42Z | 39,573,403 | <p>Using <code>wget</code>:</p>
<pre><code>custom_file_name = "/custom/path/custom_name.ext"
wget.download(url, custom_file_name)
</code></pre>
<p>Using urlretrieve: </p>
<pre><code>urllib.urlretrieve(url, custom_file_name)
</code></pre>
<p>urlretrieve also creates the directory structure if not exists. </p>
| 0 | 2016-09-19T12:37:58Z | [
"python",
"http",
"download"
] |
Txt file parse to get a list of .o file names | 862,203 | <p>I have a txt file like :</p>
<p>test.txt </p>
<pre><code>Symbols from __ctype_tab.o:
Name Value Class Type Size Line Section
__ctype |00000000| D | OBJECT|00000004| |.data
__ctype_tab |00000000| r | OBJECT|00000101| |.... | 0 | 2009-05-14T08:30:49Z | 862,401 | <p>I would use regular expressions with capture groups for the different kinds of interesting lines in your file; I'd go through the file line by line, and as I found an interesting line (i.e. matched the regex), I'd process the captured data from the regex appropriately.</p>
<p>After having built up dictionaries etc.... | 1 | 2009-05-14T09:28:52Z | [
"python",
"parsing"
] |
Txt file parse to get a list of .o file names | 862,203 | <p>I have a txt file like :</p>
<p>test.txt </p>
<pre><code>Symbols from __ctype_tab.o:
Name Value Class Type Size Line Section
__ctype |00000000| D | OBJECT|00000004| |.data
__ctype_tab |00000000| r | OBJECT|00000101| |.... | 0 | 2009-05-14T08:30:49Z | 862,616 | <p>What do you think the following does?</p>
<pre><code> if '.o' in line: # line containg .o
currObj=line.split()[-1][0:-1]
if '|' not in line: # line containg |
pass
else: # other lines
</code></pre>
<p>Does it find lines with '.o' or '|' or other?</p>
<p>No. Actually, it doesn... | 1 | 2009-05-14T10:28:12Z | [
"python",
"parsing"
] |
Txt file parse to get a list of .o file names | 862,203 | <p>I have a txt file like :</p>
<p>test.txt </p>
<pre><code>Symbols from __ctype_tab.o:
Name Value Class Type Size Line Section
__ctype |00000000| D | OBJECT|00000004| |.data
__ctype_tab |00000000| r | OBJECT|00000101| |.... | 0 | 2009-05-14T08:30:49Z | 863,018 | <p>What about this code? It is based on your two dictionaries. Just call <code>find_dep_for_func(notype_funcname)</code>.</p>
<pre><code>def find_ofile(funcname):
"""This will find .o file for given function."""
for ofile, fns in funcDict.iteritems():
if funcname in fns:
return ofile ... | 1 | 2009-05-14T12:23:14Z | [
"python",
"parsing"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 862,450 | <p>use <a href="http://unixhelp.ed.ac.uk/CGI/man-cgi?find" rel="nofollow"><code>find</code></a>.</p>
<p>Your command will look something like:</p>
<pre><code>find $directory \( -prune 'some pattern' \) -delete
</code></pre>
| 0 | 2009-05-14T09:43:04Z | [
"python",
"linux",
"bash"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 862,561 | <p>You could do something based on <a href="http://docs.python.org/library/os.html" rel="nofollow">Python's os.walk function</a>:</p>
<pre><code>import os
for root, dirs, files in os.walk(top, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.p... | 2 | 2009-05-14T10:14:26Z | [
"python",
"linux",
"bash"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 862,589 | <p>Everything "except" is why we have if-statements; and why os.walk's list of directories is a mutable list.</p>
<pre><code>for path, dirs, files in os.walk( 'root' ):
if 'coke' in dirs:
dirs.remove('coke')
dirs.remove('pepsi')
</code></pre>
| 3 | 2009-05-14T10:20:06Z | [
"python",
"linux",
"bash"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 862,622 | <p><code>find</code>'s <code>-prune</code> comes to mind, but it's a pain to get it to work for specific paths (<code>icecream/cupcake/</code>) rather than specific directories (<code>cupcake/</code>).</p>
<p>Personally, I'd just use <code>cpio</code> and hard-link (to avoid having to copy them) the files in the direc... | 3 | 2009-05-14T10:30:37Z | [
"python",
"linux",
"bash"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 862,666 | <p>Move the stuff you want to keep elsewhere, then delete what's left.</p>
| 2 | 2009-05-14T10:45:50Z | [
"python",
"linux",
"bash"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 862,680 | <p>A oneliner to solve the problem:</p>
<p><del>find . |grep -v "test/icecream/cupcake/"| grep -v "test/mtndew/livewire/"|xargs rm -r</del></p>
<p>Removed since it does not work</p>
<p>this might get you into trouble if have file names with space in them, and it might keep more files then you want if there are other... | 0 | 2009-05-14T10:50:51Z | [
"python",
"linux",
"bash"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 863,294 | <p>This command will leave only the desired files in their original directories:</p>
<pre><code>find test \( ! -path "test/mtndew/livewire/*" ! -path "test/icecream/cupcake/*" \) -delete
</code></pre>
<p>No need for cpio. It works on Ubuntu, Debian 5, and Mac OS X.</p>
<p>On Linux, it will report that it cannot dele... | 4 | 2009-05-14T13:18:39Z | [
"python",
"linux",
"bash"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 863,762 | <p>It works for me with find using two steps: first delete the files allowed, then their empty directories! </p>
<pre><code>find -x -E ~/Desktop/test -not \( -type d -regex '.*/(cupcake|livewire)/*.*' -prune \) -print0 | xargs -0 ls -1 -dG
# delete the files first
# Mac OS X 10.4
find -x -E ~/Desktop/test -not \( ... | 0 | 2009-05-14T14:49:48Z | [
"python",
"linux",
"bash"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 866,793 | <p>Like others I have used os.walk and os.path.join to build the list of files to delete, with fnmatch.fnmatch to select files that must be included or excluded:</p>
<pre><code>#-------------------------------#
# make list of files to display #
#-------------------------------#
displayList = []
for imageDir in args :
... | 0 | 2009-05-15T02:52:00Z | [
"python",
"linux",
"bash"
] |
Delete all files/directories except two specific directories | 862,388 | <p>So, there seems to be a few questions asking about removing files/directories matching certain cases, but I'm looking for the exact opposite: Delete EVERYTHING in a folder that DOESN'T match my provided examples.</p>
<p>For example, here is an example directory tree:</p>
<pre><code>.
|-- coke
| |-- diet
| |--... | 4 | 2009-05-14T09:25:37Z | 3,583,448 | <pre><code>find /path/to/test/ -depth -mindepth 1 \
! -path "/path/to/test/icecream/cupcake/*" \
! -path "/path/to/test/icecream/cupcake" \
! -path "/path/to/test/icecream" \
! -path "/path/to/test/mtndew/livewire/*" \
! -path "/path/to/test/mtndew/livewire" \
! -path "/path/to/test/mtndew"
-delete -print
</code></pre... | 1 | 2010-08-27T10:53:07Z | [
"python",
"linux",
"bash"
] |
Is it possible to have multiple statements in a python lambda expression? | 862,412 | <p>I am a python newbie trying to achieve the following:</p>
<p>I have a list of lists:</p>
<pre><code>lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]
</code></pre>
<p>I want map lst into another list containing only the second smallest number from each sublist. So the result should be:</p>
<pre><code>... | 45 | 2009-05-14T09:31:34Z | 862,430 | <p>Use <a href="http://docs.python.org/library/functions.html#sorted">sorted function</a>, like this:</p>
<pre><code>map(lambda x: sorted(x)[1],lst)
</code></pre>
| 5 | 2009-05-14T09:37:14Z | [
"python"
] |
Is it possible to have multiple statements in a python lambda expression? | 862,412 | <p>I am a python newbie trying to achieve the following:</p>
<p>I have a list of lists:</p>
<pre><code>lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]
</code></pre>
<p>I want map lst into another list containing only the second smallest number from each sublist. So the result should be:</p>
<pre><code>... | 45 | 2009-05-14T09:31:34Z | 862,457 | <p>Or if you want to avoid lambda and have a generator instead of a list:</p>
<p>(sorted(col)[1] for col in lst)</p>
| 3 | 2009-05-14T09:44:08Z | [
"python"
] |
Is it possible to have multiple statements in a python lambda expression? | 862,412 | <p>I am a python newbie trying to achieve the following:</p>
<p>I have a list of lists:</p>
<pre><code>lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]
</code></pre>
<p>I want map lst into another list containing only the second smallest number from each sublist. So the result should be:</p>
<pre><code>... | 45 | 2009-05-14T09:31:34Z | 862,498 | <p>There are several different answers I can give here, from your specific question to more general concerns. so from most specific to most general:</p>
<p><strong>Q.</strong> Can you put multiple statements in a lambda?</p>
<p><strong>A.</strong> No. But you don't actually need to use a lambda. You can put the sta... | 67 | 2009-05-14T09:55:37Z | [
"python"
] |
Is it possible to have multiple statements in a python lambda expression? | 862,412 | <p>I am a python newbie trying to achieve the following:</p>
<p>I have a list of lists:</p>
<pre><code>lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]
</code></pre>
<p>I want map lst into another list containing only the second smallest number from each sublist. So the result should be:</p>
<pre><code>... | 45 | 2009-05-14T09:31:34Z | 6,205,270 | <p>Using begin() from here: <a href="http://www.reddit.com/r/Python/comments/hms4z/ask_pyreddit_if_you_were_making_your_own/c1wycci" rel="nofollow">http://www.reddit.com/r/Python/comments/hms4z/ask_pyreddit_if_you_were_making_your_own/c1wycci</a></p>
<pre><code>Python 3.2 (r32:88445, Mar 25 2011, 19:28:28)
[GCC 4.5.2... | 2 | 2011-06-01T17:27:51Z | [
"python"
] |
Is it possible to have multiple statements in a python lambda expression? | 862,412 | <p>I am a python newbie trying to achieve the following:</p>
<p>I have a list of lists:</p>
<pre><code>lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]
</code></pre>
<p>I want map lst into another list containing only the second smallest number from each sublist. So the result should be:</p>
<pre><code>... | 45 | 2009-05-14T09:31:34Z | 17,604,249 | <p>Time traveler here. If you generally want to have multiple statements within a lambda, you can pass other lambdas as arguments to that lambda.</p>
<pre><code>(lambda x, f: list((y[1] for y in f(x))))(lst, lambda x: (sorted(y) for y in x))
</code></pre>
<p>You can't actually have multiple statements, but you can si... | 15 | 2013-07-11T21:59:36Z | [
"python"
] |
Is it possible to have multiple statements in a python lambda expression? | 862,412 | <p>I am a python newbie trying to achieve the following:</p>
<p>I have a list of lists:</p>
<pre><code>lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]
</code></pre>
<p>I want map lst into another list containing only the second smallest number from each sublist. So the result should be:</p>
<pre><code>... | 45 | 2009-05-14T09:31:34Z | 18,528,195 | <p>Putting the statements in a list may simulate multiple statements:</p>
<p>E.g.:</p>
<pre><code>lambda x: [f1(x), f2(x), f3(x), x+1]
</code></pre>
| 35 | 2013-08-30T08:20:18Z | [
"python"
] |
Is it possible to have multiple statements in a python lambda expression? | 862,412 | <p>I am a python newbie trying to achieve the following:</p>
<p>I have a list of lists:</p>
<pre><code>lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]
</code></pre>
<p>I want map lst into another list containing only the second smallest number from each sublist. So the result should be:</p>
<pre><code>... | 45 | 2009-05-14T09:31:34Z | 18,696,789 | <p>You can do it in O(n) time using min and index instead of using sort or heapq.</p>
<p>First create new list of everything except the min value of the original list:</p>
<pre><code>new_list = lst[:lst.index(min(lst))] + lst[lst.index(min(lst))+1:]
</code></pre>
<p>Then take the min value of the new list:</p>
<pre... | 1 | 2013-09-09T11:03:17Z | [
"python"
] |
Is it possible to have multiple statements in a python lambda expression? | 862,412 | <p>I am a python newbie trying to achieve the following:</p>
<p>I have a list of lists:</p>
<pre><code>lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]
</code></pre>
<p>I want map lst into another list containing only the second smallest number from each sublist. So the result should be:</p>
<pre><code>... | 45 | 2009-05-14T09:31:34Z | 33,296,719 | <p>This is exactly what the <code>bind</code> function in a <a href="http://www.dustingetz.com/2012/04/07/dustins-awesome-monad-tutorial-for-humans-in-python.html" rel="nofollow">Monad</a> is used for. </p>
<p>With the <code>bind</code> function you can combine multiple lambda's into one lambda, each lambda representi... | 1 | 2015-10-23T07:13:45Z | [
"python"
] |
Is it possible to have multiple statements in a python lambda expression? | 862,412 | <p>I am a python newbie trying to achieve the following:</p>
<p>I have a list of lists:</p>
<pre><code>lst = [[567,345,234],[253,465,756, 2345],[333,777,111, 555]]
</code></pre>
<p>I want map lst into another list containing only the second smallest number from each sublist. So the result should be:</p>
<pre><code>... | 45 | 2009-05-14T09:31:34Z | 40,133,069 | <p>You can in fact have multiple statements in a lambda expression in python. It is not entirely trivial but in your example, the following works:</p>
<pre><code>map(lambda x: x.sort() or x[1],lst)
</code></pre>
<p>You have to make sure that each statement does not return anything or if it does wrap it in (.. and Fal... | 0 | 2016-10-19T13:40:44Z | [
"python"
] |
Extracting bitmap from a file | 862,487 | <p><br />
given a somewhat complex file of unknown specification that among other things contains an uncompressed bitmap file (.BMP), how would you extract it in Python?<br />
Scan for the "BM" tag and see if the following bytes "resemble" a BMP header?</p>
| 2 | 2009-05-14T09:50:43Z | 862,493 | <p>I'd use the <a href="http://www.pythonware.com/products/pil/" rel="nofollow">Python Imaging Library</a> PIL and have it a go at the data. If it can parse it, then it's a valid image. When it throws an exception, then it isn't.</p>
<p>You need to search for the begining of the image; if you're lucky, the image reade... | 4 | 2009-05-14T09:53:26Z | [
"python",
"heuristics"
] |
Extracting bitmap from a file | 862,487 | <p><br />
given a somewhat complex file of unknown specification that among other things contains an uncompressed bitmap file (.BMP), how would you extract it in Python?<br />
Scan for the "BM" tag and see if the following bytes "resemble" a BMP header?</p>
| 2 | 2009-05-14T09:50:43Z | 862,588 | <p>Yes, about the only thing you can do is search through the file for the 'BM' marker, pull out the following data into a <a href="http://msdn.microsoft.com/en-us/library/dd183374%28VS.85%29.aspx" rel="nofollow">BITMAPFILEHEADER</a> and corresponding BITMAPINFO, and see if the values in it look valid (i.e. that the di... | 4 | 2009-05-14T10:19:51Z | [
"python",
"heuristics"
] |
Pointers in Python on variables with None value | 862,652 | <p>I have a method that creates a new node in a tree - either left or right. If the value is lower than my current value it is inserted on the left, otherwise on the right side.</p>
<p>I want to refactor this code, so that I first see on which side I have to insert my element, and then insert it. Before I implemented ... | 0 | 2009-05-14T10:42:40Z | 862,714 | <p>In Python variables are names not locations. For example:</p>
<pre><code>>>> a = 1
>>> b = a
>>> a = 2
>>> print b
1
</code></pre>
<p>In your code you're simply rebinding the name <code>child</code> to a different value (your new node) and that has no affect on the previously b... | 2 | 2009-05-14T10:59:14Z | [
"python",
"reference"
] |
Pointers in Python on variables with None value | 862,652 | <p>I have a method that creates a new node in a tree - either left or right. If the value is lower than my current value it is inserted on the left, otherwise on the right side.</p>
<p>I want to refactor this code, so that I first see on which side I have to insert my element, and then insert it. Before I implemented ... | 0 | 2009-05-14T10:42:40Z | 862,724 | <p>Hallo ;-)</p>
<p><code>self.left</code> or <code>self.right</code> don't receive the value because you assign to <code>child</code> which just holds a copy of the destination value and no reference to it.</p>
<p>You want to have a pointer- This doesn't exist directly in Python.</p>
<p>You could express this using... | 0 | 2009-05-14T11:01:27Z | [
"python",
"reference"
] |
Pointers in Python on variables with None value | 862,652 | <p>I have a method that creates a new node in a tree - either left or right. If the value is lower than my current value it is inserted on the left, otherwise on the right side.</p>
<p>I want to refactor this code, so that I first see on which side I have to insert my element, and then insert it. Before I implemented ... | 0 | 2009-05-14T10:42:40Z | 862,740 | <p>Why are you using a tree?</p>
<p>I would use a dictionary:</p>
<p>Initialization:</p>
<blockquote>
<p>tree = {}</p>
</blockquote>
<p>Adding new node:</p>
<blockquote>
<p>tree[sortByValue] = secondValue</p>
</blockquote>
<p>Extracting things</p>
<blockquote>
<p>print tree[sortByValue]</p>
</blockquote>
... | 0 | 2009-05-14T11:04:23Z | [
"python",
"reference"
] |
How would you write a @debuggable decorator in python? | 862,807 | <p>When debugging, I like to print out all the inputs and outputs of a function (I know I need a better IDE, but humour me, this could be used for error reporting). So, I'd ideally like to have:</p>
<pre><code>@debuggable
def myfunc(argA,argB,argC):
return argB+1
</code></pre>
<p>and use a global variable to swit... | 7 | 2009-05-14T11:26:37Z | 862,899 | <p>Use a debugger. Seriously. Decorating every function you want to keep track is a bad idea.</p>
<p>Python <a href="http://docs.python.org/library/pdb.html">has a debugger included</a>, so you don't need a good IDE.</p>
<p>If you don't want to use a debugger, you can use the <a href="http://docs.python.org/library/s... | 19 | 2009-05-14T11:52:07Z | [
"python",
"decorator"
] |
How would you write a @debuggable decorator in python? | 862,807 | <p>When debugging, I like to print out all the inputs and outputs of a function (I know I need a better IDE, but humour me, this could be used for error reporting). So, I'd ideally like to have:</p>
<pre><code>@debuggable
def myfunc(argA,argB,argC):
return argB+1
</code></pre>
<p>and use a global variable to swit... | 7 | 2009-05-14T11:26:37Z | 862,915 | <p>I agree with nosklo using a debugger is much better than writing your own. I'll post an improvement to your code. But I still think you should follow nosklo's advice.</p>
<p>Use decorator classes to make your debugger neater:</p>
<pre><code>class Debugger(object):
enabled = False
def __init__(self, func):
... | 4 | 2009-05-14T11:56:27Z | [
"python",
"decorator"
] |
How would you write a @debuggable decorator in python? | 862,807 | <p>When debugging, I like to print out all the inputs and outputs of a function (I know I need a better IDE, but humour me, this could be used for error reporting). So, I'd ideally like to have:</p>
<pre><code>@debuggable
def myfunc(argA,argB,argC):
return argB+1
</code></pre>
<p>and use a global variable to swit... | 7 | 2009-05-14T11:26:37Z | 862,963 | <p>I second what nosklo said.</p>
<p>Another thing to notice is that your function is a bit dangerous:</p>
<pre><code>b = myfunc(1,3)
</code></pre>
<p>In this case, "b" is <code>None</code>, because the decorated function doesn't return anything.</p>
| 0 | 2009-05-14T12:09:28Z | [
"python",
"decorator"
] |
How would you write a @debuggable decorator in python? | 862,807 | <p>When debugging, I like to print out all the inputs and outputs of a function (I know I need a better IDE, but humour me, this could be used for error reporting). So, I'd ideally like to have:</p>
<pre><code>@debuggable
def myfunc(argA,argB,argC):
return argB+1
</code></pre>
<p>and use a global variable to swit... | 7 | 2009-05-14T11:26:37Z | 863,036 | <p>I think what you're after isn't really a debugging decorator, but more of a logging decorator.</p>
<p>It might make sense to use <a href="http://docs.python.org/library/logging.html">Python's logging module</a> so you can have more fine grained control over the logging itself. For example you would be able to outp... | 6 | 2009-05-14T12:27:37Z | [
"python",
"decorator"
] |
How would you write a @debuggable decorator in python? | 862,807 | <p>When debugging, I like to print out all the inputs and outputs of a function (I know I need a better IDE, but humour me, this could be used for error reporting). So, I'd ideally like to have:</p>
<pre><code>@debuggable
def myfunc(argA,argB,argC):
return argB+1
</code></pre>
<p>and use a global variable to swit... | 7 | 2009-05-14T11:26:37Z | 864,558 | <p>There's a fairly long blog post on the subject of tracing decorators at <a href="http://wordaligned.org/articles/echo" rel="nofollow">Word Aligned</a>.</p>
| 1 | 2009-05-14T17:08:04Z | [
"python",
"decorator"
] |
url method of ImageField returns a non-Url value - Django | 862,860 | <p>I'm developing using Django on Windows. I have a model with an imagefield, and use a form to fill it. Images get uploaded without problem. The problem occurs when I attempt to show an uploaded image inside a template by coding this:</p>
<pre><code><img src ='{{object.image.url}}'/>
</code></pre>
<p>(object i... | 1 | 2009-05-14T11:40:47Z | 6,240,232 | <p>I had the same problem. At model declaration, I changed "upload_to" argument value from absolute path to relative, this fixes the problem.</p>
| 2 | 2011-06-05T00:25:10Z | [
"python",
"django",
"url",
"django-models"
] |
Python: adding namespaces in lxml | 863,183 | <p>I'm trying to specify a namespace using <em>lxml</em> similar to this example (taken from <a href="http://www.maconstateit.net/tutorials/XML/XML03/xml03-01.htm" rel="nofollow">here</a>):</p>
<pre class="lang-xml prettyprint-override"><code><TreeInventory xsi:noNamespaceSchemaLocation="Trees.xsd" xmlns:xsi="http:... | 6 | 2009-05-14T12:58:32Z | 863,834 | <p>In some more steps, for clarity:</p>
<pre><code>>>> NS = 'http://www.w3.org/2001/XMLSchema-instance'
</code></pre>
<p>As far as I can see, it is the attribute <code>noNameSpaceSchemaLocation</code> that you want namespaced, not the <code>TreeInventory</code> element. So:</p>
<pre><code>>>> locat... | 8 | 2009-05-14T14:59:28Z | [
"python",
"lxml",
"xml-namespaces"
] |
Python Module by Path | 863,234 | <p>I am writing a minimal replacement for mod_python's publisher.py</p>
<p>The basic premise is that it is loading modules based on a URL scheme:</p>
<pre><code>/foo/bar/a/b/c/d
</code></pre>
<p>Whereby /foo/ might be a directory and 'bar' is a method ExposedBar in a publishable class in /foo/index.py. Likewise /foo... | 1 | 2009-05-14T13:07:27Z | 863,324 | <blockquote>
<p>Can I load a module by it's absolute
path into a module object? Without
modification of sys.path. I can't find
any docs on <code>__import__</code> or new.module()
for this.</p>
</blockquote>
<pre><code>import imp
import os
def module_from_path(path):
filename = os.path.basename(path)
... | 2 | 2009-05-14T13:23:56Z | [
"python",
"mod-python"
] |
Python and Collective Intelligence | 863,253 | <p>I'm currently reading a great book called 'Programming Collective Intelligence' by Toby Segaran (which i highly recommend)</p>
<p>The code examples are all written in Python, and as I have already learnt one new language this year (graduating from VB.net to C#) i'm not keen to jump on another learning curve.</p>
<... | 1 | 2009-05-14T13:11:53Z | 863,292 | <p>One challenge you'll find is that not only are the algorithms implemented in Python, but the book makes extensive use of Python libraries like BeautifulSoup, Numpy, PIL, and others (see appendix A).</p>
<p>I doubt there are any specifics of the algorithms that you couldn't port to another language, but you'll have ... | 19 | 2009-05-14T13:18:10Z | [
"python",
"collective-intelligence"
] |
Python and Collective Intelligence | 863,253 | <p>I'm currently reading a great book called 'Programming Collective Intelligence' by Toby Segaran (which i highly recommend)</p>
<p>The code examples are all written in Python, and as I have already learnt one new language this year (graduating from VB.net to C#) i'm not keen to jump on another learning curve.</p>
<... | 1 | 2009-05-14T13:11:53Z | 863,328 | <p>Obligatory XKCD: <a href="http://xkcd.com/353/" rel="nofollow">http://xkcd.com/353/</a></p>
<p>I know you explicitly say you don't want to learn Python (this year), but translating the Python examples to C# will definitely be a much steeper curve. Just <a href="http://www.diveintopython.org/" rel="nofollow">dive in... | 1 | 2009-05-14T13:24:33Z | [
"python",
"collective-intelligence"
] |
Python and Collective Intelligence | 863,253 | <p>I'm currently reading a great book called 'Programming Collective Intelligence' by Toby Segaran (which i highly recommend)</p>
<p>The code examples are all written in Python, and as I have already learnt one new language this year (graduating from VB.net to C#) i'm not keen to jump on another learning curve.</p>
<... | 1 | 2009-05-14T13:11:53Z | 863,331 | <p>You can do the same things in all Turing-complete languages. <a href="http://thedailywtf.com/articles/stupid-coding-tricks-the-tsql-madlebrot.aspx" rel="nofollow">Here is an example</a> for rendering a Mandelbrot fractal in SQL. The example shows: Even if you can use any language, the effort will be different.</p>
... | 2 | 2009-05-14T13:24:45Z | [
"python",
"collective-intelligence"
] |
Python and Collective Intelligence | 863,253 | <p>I'm currently reading a great book called 'Programming Collective Intelligence' by Toby Segaran (which i highly recommend)</p>
<p>The code examples are all written in Python, and as I have already learnt one new language this year (graduating from VB.net to C#) i'm not keen to jump on another learning curve.</p>
<... | 1 | 2009-05-14T13:11:53Z | 863,568 | <p>The book is about algorithms, not the details of programming, and the language of choice is just to make the examples concrete. As the author says, "The code examples in this book are written in Python... but I provide explanations of all the algorithms so that programmers of other languages can follow." (p. xv)</p... | 1 | 2009-05-14T14:14:51Z | [
"python",
"collective-intelligence"
] |
Python and Collective Intelligence | 863,253 | <p>I'm currently reading a great book called 'Programming Collective Intelligence' by Toby Segaran (which i highly recommend)</p>
<p>The code examples are all written in Python, and as I have already learnt one new language this year (graduating from VB.net to C#) i'm not keen to jump on another learning curve.</p>
<... | 1 | 2009-05-14T13:11:53Z | 863,626 | <p>Python seems to be to AI programming what LISP was for for many decades. Russel/Norvig's famous book <a href="http://aima.cs.berkeley.edu/" rel="nofollow">AI: A Modern Approach</a> also provides lots of <a href="http://aima.cs.berkeley.edu/python/readme.html" rel="nofollow">examples in Python</a>.</p>
| 0 | 2009-05-14T14:23:12Z | [
"python",
"collective-intelligence"
] |
Python and Collective Intelligence | 863,253 | <p>I'm currently reading a great book called 'Programming Collective Intelligence' by Toby Segaran (which i highly recommend)</p>
<p>The code examples are all written in Python, and as I have already learnt one new language this year (graduating from VB.net to C#) i'm not keen to jump on another learning curve.</p>
<... | 1 | 2009-05-14T13:11:53Z | 986,107 | <p>I suggest translating them to C#. I have been porting chapter 2 "Recommendations" to VB.Net. Along the way I'm learning Python as a side-effect. Toby does some amazing things with Python lists.</p>
<p>Dealing with the the extra Python libraries is another story. Ndelicious is a close match to pyDelicious, but it is... | 2 | 2009-06-12T11:04:40Z | [
"python",
"collective-intelligence"
] |
Which PEP governs the ordering of dict.values()? | 863,446 | <p>When you call dict.values() the order of the returned items is dependent on the has value of the keys. This seems to be very consistent in all versions of cPython, however the python manual for dict <a href="http://docs.python.org/library/stdtypes.html#dict.items" rel="nofollow">simply states that the ordering is "a... | 1 | 2009-05-14T13:53:06Z | 863,464 | <p>From <a href="http://docs.python.org/library/stdtypes.html" rel="nofollow">http://docs.python.org/library/stdtypes.html</a>:</p>
<blockquote>
<p>Keys and values are listed in an
arbitrary order which is non-random,
varies across Python implementations,
and depends on the dictionaryâs
history of insertio... | 7 | 2009-05-14T13:56:48Z | [
"python",
"dictionary"
] |
Which PEP governs the ordering of dict.values()? | 863,446 | <p>When you call dict.values() the order of the returned items is dependent on the has value of the keys. This seems to be very consistent in all versions of cPython, however the python manual for dict <a href="http://docs.python.org/library/stdtypes.html#dict.items" rel="nofollow">simply states that the ordering is "a... | 1 | 2009-05-14T13:53:06Z | 863,488 | <p>"arbitrary" is not the same as "accidental". </p>
<p>But it is the same as "undocumented". Since the dictionary is based on hashes, you can't -- really -- guarantee ordering based on the hashing algorithm and collisions which occur.</p>
<p>To guarantee an order, you use the <code>sorted</code> function. </p>
<... | 2 | 2009-05-14T13:59:32Z | [
"python",
"dictionary"
] |
Which PEP governs the ordering of dict.values()? | 863,446 | <p>When you call dict.values() the order of the returned items is dependent on the has value of the keys. This seems to be very consistent in all versions of cPython, however the python manual for dict <a href="http://docs.python.org/library/stdtypes.html#dict.items" rel="nofollow">simply states that the ordering is "a... | 1 | 2009-05-14T13:53:06Z | 863,494 | <p>I suppose <a href="http://www.python.org/dev/peps/pep-3106/" rel="nofollow">PEP-3106</a> is as close as it gets:</p>
<blockquote>
<p>The specification implies that the order in which items are returned by
.keys(), .values() and .items() is the
same (just as it was in Python 2.x),
because the order is all de... | 6 | 2009-05-14T14:00:30Z | [
"python",
"dictionary"
] |
How to generate XML documents with namespaces in Python | 863,774 | <p>I'm trying to generate an XML document with namespaces, currently with Python's xml.dom.minidom:</p>
<pre><code>import xml.dom.minidom
doc = xml.dom.minidom.Document()
el = doc.createElementNS('http://example.net/ns', 'el')
doc.appendChild(el)
print(doc.toprettyxml())
</code></pre>
<p>The namespace is saved (<code... | 22 | 2009-05-14T14:51:01Z | 864,030 | <p><code>createElementNS()</code> is defined as:</p>
<pre class="lang-py prettyprint-override"><code>def createElementNS(self, namespaceURI, qualifiedName):
prefix, localName = _nssplit(qualifiedName)
e = Element(qualifiedName, namespaceURI, prefix)
e.ownerDocument = self
return e
</code></pre>
<p>soâ... | 19 | 2009-05-14T15:26:47Z | [
"python",
"xml",
"dom",
"namespaces"
] |
How to generate XML documents with namespaces in Python | 863,774 | <p>I'm trying to generate an XML document with namespaces, currently with Python's xml.dom.minidom:</p>
<pre><code>import xml.dom.minidom
doc = xml.dom.minidom.Document()
el = doc.createElementNS('http://example.net/ns', 'el')
doc.appendChild(el)
print(doc.toprettyxml())
</code></pre>
<p>The namespace is saved (<code... | 22 | 2009-05-14T14:51:01Z | 866,412 | <p>This feature is already proposed; a patch is <a href="http://bugs.python.org/issue1621421">slumbering in the Python bug database</a>. See Tomalak's answer (in short: Manually add the <code>xmlns</code> attribute) for a workaround.</p>
| 5 | 2009-05-14T23:39:29Z | [
"python",
"xml",
"dom",
"namespaces"
] |
A data-structure for 1:1 mappings in python? | 863,935 | <p>I have a problem which requires a reversable 1:1 mapping of keys to values. </p>
<p>That means sometimes I want to find the value given a key, but at other times I want to find the key given the value. Both keys and values are guaranteed unique. </p>
<pre><code>x = D[y]
y == D.inverse[x]
</code></pre>
<p>The obvi... | 24 | 2009-05-14T15:14:18Z | 863,974 | <blockquote>
<p>The other alternative is to make a new class which unites two dictionaries, one for each > kind of lookup. That would most likely use up twice as much memory as a single dict.</p>
</blockquote>
<p>Not really, since they would just be holding two references to the same data. In my mind, this is not a ... | 5 | 2009-05-14T15:19:29Z | [
"python",
"data-structures"
] |
A data-structure for 1:1 mappings in python? | 863,935 | <p>I have a problem which requires a reversable 1:1 mapping of keys to values. </p>
<p>That means sometimes I want to find the value given a key, but at other times I want to find the key given the value. Both keys and values are guaranteed unique. </p>
<pre><code>x = D[y]
y == D.inverse[x]
</code></pre>
<p>The obvi... | 24 | 2009-05-14T15:14:18Z | 863,983 | <p>Assuming that you have a key with which you look up a more complex mutable object, just make the key a property of that object. It does seem you might be better off thinking about the data model a bit.</p>
| 1 | 2009-05-14T15:20:47Z | [
"python",
"data-structures"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.