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 |
|---|---|---|---|---|---|---|---|---|---|
Can you give a Django app a verbose name for use throughout the admin? | 612,372 | <p>In the same way that you can give fields and models verbose names that appear in the Django admin, can you give an app a custom name?</p>
| 95 | 2009-03-04T20:51:03Z | 4,998,073 | <p>I'm using <a href="https://bitbucket.org/izi/django-admin-tools/" rel="nofollow">django-admin-tools</a> for that.</p>
| 3 | 2011-02-14T22:33:21Z | [
"python",
"django"
] |
Can you give a Django app a verbose name for use throughout the admin? | 612,372 | <p>In the same way that you can give fields and models verbose names that appear in the Django admin, can you give an app a custom name?</p>
| 95 | 2009-03-04T20:51:03Z | 5,819,110 | <p>If you have more than one model in the app just create a model with the Meta information and create subclasses of that class for all your models.</p>
<pre><code>class MyAppModel(models.Model):
class Meta:
app_label = 'My App Label'
abstract = True
class Category(MyAppModel):
name = models.... | 30 | 2011-04-28T13:08:37Z | [
"python",
"django"
] |
Can you give a Django app a verbose name for use throughout the admin? | 612,372 | <p>In the same way that you can give fields and models verbose names that appear in the Django admin, can you give an app a custom name?</p>
| 95 | 2009-03-04T20:51:03Z | 6,312,798 | <p>For Django 1.4 (not yet released, but trunk is pretty stable), you can use the following method. It relies on the fact that AdminSite now returns a TemplateResponse, which you can alter before it is rendered.</p>
<p>Here, we do a small bit of monkey patching to insert our behaviour, which can be avoided if you use ... | 7 | 2011-06-10T22:47:20Z | [
"python",
"django"
] |
Can you give a Django app a verbose name for use throughout the admin? | 612,372 | <p>In the same way that you can give fields and models verbose names that appear in the Django admin, can you give an app a custom name?</p>
| 95 | 2009-03-04T20:51:03Z | 22,150,764 | <p>If you already have existing tables using the old app name, and you don't want to migrate them, then just set the app_label on a proxy of the original model.</p>
<pre><code>class MyOldModel(models.Model):
pass
class MyNewModel(MyOldModel):
class Meta:
proxy = True
app_label = 'New APP name'... | 2 | 2014-03-03T15:32:11Z | [
"python",
"django"
] |
Can you give a Django app a verbose name for use throughout the admin? | 612,372 | <p>In the same way that you can give fields and models verbose names that appear in the Django admin, can you give an app a custom name?</p>
| 95 | 2009-03-04T20:51:03Z | 25,786,187 | <p>There is a hack that can be done that does not require any migrations.
Taken from Ionel's blog and credit goes to him: <a href="http://blog.ionelmc.ro/2011/06/24/custom-app-names-in-the-django-admin/" rel="nofollow">http://blog.ionelmc.ro/2011/06/24/custom-app-names-in-the-django-admin/</a></p>
<p>There is also a t... | 3 | 2014-09-11T11:24:43Z | [
"python",
"django"
] |
Can you give a Django app a verbose name for use throughout the admin? | 612,372 | <p>In the same way that you can give fields and models verbose names that appear in the Django admin, can you give an app a custom name?</p>
| 95 | 2009-03-04T20:51:03Z | 26,988,256 | <p>first you need to create a apps.py file like this on your appfolder.</p>
<pre><code># appName/apps.py
# -*- coding: utf-8 -*-
from django.apps import AppConfig
class AppNameConfig(AppConfig):
name = 'appName'
verbose_name = "app Custom Name"
</code></pre>
<p>to load this AppConfig subclass b... | 5 | 2014-11-18T06:48:03Z | [
"python",
"django"
] |
Can you give a Django app a verbose name for use throughout the admin? | 612,372 | <p>In the same way that you can give fields and models verbose names that appear in the Django admin, can you give an app a custom name?</p>
| 95 | 2009-03-04T20:51:03Z | 27,254,978 | <p>As stated by rhunwicks' comment to OP, this is now possible out of the box since Django 1.7</p>
<p>Taken from the <a href="https://docs.djangoproject.com/en/1.7/ref/applications/#for-application-authors">docs</a>:</p>
<pre class="lang-py prettyprint-override"><code># in yourapp/apps.py
from django.apps import Ap... | 19 | 2014-12-02T17:05:26Z | [
"python",
"django"
] |
Can you give a Django app a verbose name for use throughout the admin? | 612,372 | <p>In the same way that you can give fields and models verbose names that appear in the Django admin, can you give an app a custom name?</p>
| 95 | 2009-03-04T20:51:03Z | 27,784,446 | <p>The following plug-and-play piece of code works perfectly since <code>Django 1.7</code>. All you have to do is copy the below code in the <code>__init__.py</code> file of the specific app and change the <code>VERBOSE_APP_NAME</code> parameter.</p>
<pre><code>from os import path
from django.apps import AppConfig
VE... | 0 | 2015-01-05T17:09:34Z | [
"python",
"django"
] |
Problem using super(python 2.5.2) | 612,468 | <p>I'm writing a plugin system for my program and I can't get past one thing:</p>
<pre><code>class ThingLoader(object):
'''
Loader class
'''
def loadPlugins(self):
'''
Get all the plugins from plugins folder
'''
from diones.thingpad.plugin.IntrospectionHelper import loadClasses
... | 1 | 2009-03-04T21:18:13Z | 612,668 | <p>âsuperâ is a builtin. Unless you went out of your way to delete builtins, you shouldn't ever see âglobal name 'super' is not definedâ.</p>
<p>I'm looking at your <a href="http://logdiones.blogspot.com/">user web link</a> where there is a dump of IntrospectionHelper. It's very hard to read without the indent... | 20 | 2009-03-04T22:02:24Z | [
"python",
"introspection",
"python-datamodel"
] |
Problem using super(python 2.5.2) | 612,468 | <p>I'm writing a plugin system for my program and I can't get past one thing:</p>
<pre><code>class ThingLoader(object):
'''
Loader class
'''
def loadPlugins(self):
'''
Get all the plugins from plugins folder
'''
from diones.thingpad.plugin.IntrospectionHelper import loadClasses
... | 1 | 2009-03-04T21:18:13Z | 612,676 | <p>Unless you're running a version of Python earlier than 2.2 (pretty unlikely), <code>super()</code> is definitely a <a href="http://docs.python.org/library/functions.html" rel="nofollow">built-in function</a> (available in every scope, and without importing anything).</p>
<p>May be worth checking your version of Pyt... | 0 | 2009-03-04T22:05:07Z | [
"python",
"introspection",
"python-datamodel"
] |
Code samples for Django + SWFUpload? | 612,734 | <p>Does anyone have any simple code samples for Django + <a href="http://swfupload.org/" rel="nofollow">SWFUpload</a>? I have it working perfectly in my PHP application but Django is giving me headaches.</p>
| 5 | 2009-03-04T22:21:07Z | 612,898 | <p>Unfortunately I can't give you any very detailed code samples, but I have quite a bit of experience with working with SWFUpload + Django (for a photo sharing site I work on). Anyway, here are a few pointers that will hopefully help you on your quest for DjSWF happiness :)</p>
<ol>
<li><p>You'll want to use the cook... | 17 | 2009-03-04T23:05:31Z | [
"python",
"django",
"swfupload"
] |
Code samples for Django + SWFUpload? | 612,734 | <p>Does anyone have any simple code samples for Django + <a href="http://swfupload.org/" rel="nofollow">SWFUpload</a>? I have it working perfectly in my PHP application but Django is giving me headaches.</p>
| 5 | 2009-03-04T22:21:07Z | 1,318,520 | <p>Django version of the samples for SWFUpload:</p>
<p><a href="http://github.com/naltimari/django-swfupload-samples/tree/master" rel="nofollow">http://github.com/naltimari/django-swfupload-samples/tree/master</a></p>
<p>So long uploadify. Great idea but it is just buggy, especially on Windows.</p>
| 3 | 2009-08-23T13:06:52Z | [
"python",
"django",
"swfupload"
] |
Code samples for Django + SWFUpload? | 612,734 | <p>Does anyone have any simple code samples for Django + <a href="http://swfupload.org/" rel="nofollow">SWFUpload</a>? I have it working perfectly in my PHP application but Django is giving me headaches.</p>
| 5 | 2009-03-04T22:21:07Z | 24,508,069 | <p>The following is my Django-specific implementation for fixing this issue (i.e. my uploads were failing in Firefox with a 302 Redirect).</p>
<p>In my initial view that generates the page with the uploader on it, I looked at the cookies and found sessionid</p>
<pre><code>ipdb> self.request.COOKIES
{'csrftoken': '... | 0 | 2014-07-01T10:32:48Z | [
"python",
"django",
"swfupload"
] |
Python one-liner to print every file in the current directory | 613,007 | <p>How can I make the following one liner print every file through Python?</p>
<pre><code>python -c "import sys;print '>>',sys.argv[1:]" | dir *.*
</code></pre>
<p>Specifically would like to know how to pipe into a python -c.
DOS or Cygwin responses accepted.</p>
| 1 | 2009-03-04T23:45:09Z | 613,012 | <pre><code>python -c "import os; print os.listdir('.')"
</code></pre>
<p>If you want to apply some formatting like you have in your question,</p>
<pre><code>python -c "import os; print '\n'.join(['>>%s' % x for x in os.listdir('.')])"
</code></pre>
<p>If you want to use a pipe, use <code>xargs</code>:</p>
<pr... | 6 | 2009-03-04T23:47:33Z | [
"python",
"cygwin",
"cmd",
"pipe"
] |
Python one-liner to print every file in the current directory | 613,007 | <p>How can I make the following one liner print every file through Python?</p>
<pre><code>python -c "import sys;print '>>',sys.argv[1:]" | dir *.*
</code></pre>
<p>Specifically would like to know how to pipe into a python -c.
DOS or Cygwin responses accepted.</p>
| 1 | 2009-03-04T23:45:09Z | 613,072 | <p>You can read data piped <strong>into</strong> a Python script by reading sys.stdin. For example:</p>
<pre><code>ls -al | python -c "import sys; print sys.stdin.readlines()"
</code></pre>
<p>It is not entirely clear what you want to do (maybe I am stupid). The confusion comes from your example which is piping data ... | 4 | 2009-03-05T00:06:43Z | [
"python",
"cygwin",
"cmd",
"pipe"
] |
Python one-liner to print every file in the current directory | 613,007 | <p>How can I make the following one liner print every file through Python?</p>
<pre><code>python -c "import sys;print '>>',sys.argv[1:]" | dir *.*
</code></pre>
<p>Specifically would like to know how to pipe into a python -c.
DOS or Cygwin responses accepted.</p>
| 1 | 2009-03-04T23:45:09Z | 613,099 | <p>If you want to print <em>all</em> files:</p>
<pre><code>find . -type f
</code></pre>
<p>If you want to print only the current directory's files</p>
<pre><code>find . -type f -maxdepth 1
</code></pre>
<p>If you want to include the ">>" before each line</p>
<pre><code>find . -type f -maxdepth 1 | xargs -L 1 echo ... | 4 | 2009-03-05T00:15:53Z | [
"python",
"cygwin",
"cmd",
"pipe"
] |
Python one-liner to print every file in the current directory | 613,007 | <p>How can I make the following one liner print every file through Python?</p>
<pre><code>python -c "import sys;print '>>',sys.argv[1:]" | dir *.*
</code></pre>
<p>Specifically would like to know how to pipe into a python -c.
DOS or Cygwin responses accepted.</p>
| 1 | 2009-03-04T23:45:09Z | 613,134 | <pre><code>ls | python -c "import sys; print sys.stdin.read()"
</code></pre>
<p>just read stdin as normal for pipes</p>
| 3 | 2009-03-05T00:29:16Z | [
"python",
"cygwin",
"cmd",
"pipe"
] |
Python one-liner to print every file in the current directory | 613,007 | <p>How can I make the following one liner print every file through Python?</p>
<pre><code>python -c "import sys;print '>>',sys.argv[1:]" | dir *.*
</code></pre>
<p>Specifically would like to know how to pipe into a python -c.
DOS or Cygwin responses accepted.</p>
| 1 | 2009-03-04T23:45:09Z | 613,171 | <blockquote>
<p>would like to know how to pipe though</p>
</blockquote>
<p>You had the pipe the wrong way round, if you wanted to feed the output of âdirâ into Python, âdirâ would have to be on the left. eg.:</p>
<pre><code>dir "*.*" | python -c "import sys;[sys.stdout.write('>>%s\n' % line) for line ... | 2 | 2009-03-05T00:44:18Z | [
"python",
"cygwin",
"cmd",
"pipe"
] |
Python one-liner to print every file in the current directory | 613,007 | <p>How can I make the following one liner print every file through Python?</p>
<pre><code>python -c "import sys;print '>>',sys.argv[1:]" | dir *.*
</code></pre>
<p>Specifically would like to know how to pipe into a python -c.
DOS or Cygwin responses accepted.</p>
| 1 | 2009-03-04T23:45:09Z | 614,056 | <blockquote>
<p>Specifically would like to know how to pipe into a python -c</p>
</blockquote>
<p>see <a href="http://stackoverflow.com/questions/613007/613134#613134">cobbal's answer</a></p>
<p>piping through a program is transparent from the program's point of view, all the program knows is that it's getting inpu... | 1 | 2009-03-05T09:12:06Z | [
"python",
"cygwin",
"cmd",
"pipe"
] |
Why do I get wrong results for hmac in Python but not Perl? | 613,111 | <p>I'm trying to compute hmac using sha-512.</p>
<p>The Perl code: </p>
<pre><code>use Digest::SHA qw(hmac_sha512_hex);
$key = "\x0b"x20;
$data = "Hi There";
$hash = hmac_sha512_hex($data, $key);
print "$hash\n";
</code></pre>
<p>and gives the correct hash of </p>
<pre><code>87aa7cdea5ef619d4ff0b4241a1d6cb0237... | 2 | 2009-03-05T00:20:15Z | 613,128 | <p>Which version of Python? Strings are Unicode in Python 3. Is this a Unicode issue?</p>
| 0 | 2009-03-05T00:27:08Z | [
"python",
"perl",
"hash",
"hmac"
] |
Why do I get wrong results for hmac in Python but not Perl? | 613,111 | <p>I'm trying to compute hmac using sha-512.</p>
<p>The Perl code: </p>
<pre><code>use Digest::SHA qw(hmac_sha512_hex);
$key = "\x0b"x20;
$data = "Hi There";
$hash = hmac_sha512_hex($data, $key);
print "$hash\n";
</code></pre>
<p>and gives the correct hash of </p>
<pre><code>87aa7cdea5ef619d4ff0b4241a1d6cb0237... | 2 | 2009-03-05T00:20:15Z | 613,137 | <p>I am unable to replicate your results here. In IDLE using Python 2.5:</p>
<pre><code>Python 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information.
...
IDLE 1.2.2
>>> import hashlib, hmac
>>> print hmac... | 1 | 2009-03-05T00:30:30Z | [
"python",
"perl",
"hash",
"hmac"
] |
Why do I get wrong results for hmac in Python but not Perl? | 613,111 | <p>I'm trying to compute hmac using sha-512.</p>
<p>The Perl code: </p>
<pre><code>use Digest::SHA qw(hmac_sha512_hex);
$key = "\x0b"x20;
$data = "Hi There";
$hash = hmac_sha512_hex($data, $key);
print "$hash\n";
</code></pre>
<p>and gives the correct hash of </p>
<pre><code>87aa7cdea5ef619d4ff0b4241a1d6cb0237... | 2 | 2009-03-05T00:20:15Z | 613,189 | <p>Under python 2.5.2 I get the correct hash<br />
I guess the old version was the problem</p>
| 0 | 2009-03-05T00:50:23Z | [
"python",
"perl",
"hash",
"hmac"
] |
Why do I get wrong results for hmac in Python but not Perl? | 613,111 | <p>I'm trying to compute hmac using sha-512.</p>
<p>The Perl code: </p>
<pre><code>use Digest::SHA qw(hmac_sha512_hex);
$key = "\x0b"x20;
$data = "Hi There";
$hash = hmac_sha512_hex($data, $key);
print "$hash\n";
</code></pre>
<p>and gives the correct hash of </p>
<pre><code>87aa7cdea5ef619d4ff0b4241a1d6cb0237... | 2 | 2009-03-05T00:20:15Z | 613,271 | <p>yes indeed -- it seems the Leopard version of python2.5 is the one that is broken. </p>
<p>below run on a Penryn-based MBP...</p>
<pre><code>$ **uname -a**
Darwin lizard-wifi 9.6.0 Darwin Kernel Version 9.6.0: Mon Nov 24 17:37:00 PST 2008; root:xnu-1228.9.59~1/RELEASE_I386 i386
dpc@lizard-wifi:~$ **which python**
... | 9 | 2009-03-05T01:23:29Z | [
"python",
"perl",
"hash",
"hmac"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 613,207 | <p>Technically, dictionaries aren't sequences, and therefore can't be sorted. You can do something like</p>
<pre><code>sorted(a_dictionary.values())
</code></pre>
<p>assuming performance isn't a huge deal.</p>
<p>UPDATE: Thanks to the commenters for pointing out that I made this way too complicated in the beginning.... | 12 | 2009-03-05T00:56:12Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 613,218 | <p>It is not possible to sort a dict, only to get a representation of a dict that is sorted. Dicts are inherently orderless, but other types, such as lists and tuples, are not. So you need a sorted representation, which will be a listâprobably a list of tuples.</p>
<p>For instance,</p>
<pre><code>import operator
x ... | 2,257 | 2009-03-05T00:59:34Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 613,228 | <p>Dicts can't be sorted, but you can build a sorted list from them.</p>
<p>A sorted list of dict values:</p>
<pre><code>sorted(d.values())
</code></pre>
<p>A list of (key, value) pairs, sorted by value:</p>
<pre><code>from operator import itemgetter
sorted(d.items(), key=itemgetter(1))
</code></pre>
| 114 | 2009-03-05T01:05:22Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 613,230 | <p>Pretty much the same as Hank Gay's answer;</p>
<pre>
sorted([(value,key) for (key,value) in mydict.items()])
</pre>
| 34 | 2009-03-05T01:06:04Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 613,326 | <p>You can create an "inverted index", also</p>
<pre><code>from collections import defaultdict
inverse= defaultdict( list )
for k, v in originalDict.items():
inverse[v].append( k )
</code></pre>
<p>Now your inverse has the values; each value has a list of applicable keys.</p>
<pre><code>for k in sorted(inverse):... | 16 | 2009-03-05T01:52:18Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 2,258,273 | <p>You could use:</p>
<p><code>sorted(d.items(), key=lambda x: x[1])</code></p>
<p>This will sort the dictionary by the values of each entry within the dictionary from smallest to largest.</p>
| 365 | 2010-02-13T16:33:51Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 3,177,025 | <p>In recent Python 2.7, we have the new <a href="http://docs.python.org/dev/whatsnew/2.7.html#pep-372-adding-an-ordered-dictionary-to-collections">OrderedDict</a> type, which remembers the order in which the items were added.</p>
<pre><code>>>> d = {"third": 3, "first": 1, "fourth": 4, "second": 2}
>>... | 78 | 2010-07-05T02:50:41Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 3,177,911 | <h2>As simple as: <code>sorted(dict1, key=dict1.get)</code></h2>
<p>Well, it is actually possible to do a "sort by dictionary values". Recently I had to do that in a Code Golf (Stack Overflow question <em><a href="http://stackoverflow.com/questions/3169051/code-golf-word-frequency-chart#3170549">Code golf: Word freque... | 659 | 2010-07-05T08:01:16Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 4,068,769 | <pre><code>from django.utils.datastructures import SortedDict
def sortedDictByKey(self,data):
"""Sorted dictionary order by key"""
sortedDict = SortedDict()
if data:
if isinstance(data, dict):
sortedKey = sorted(data.keys())
for k in sortedKey:
sortedDict[k] ... | 9 | 2010-11-01T12:16:41Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 4,215,710 | <p>I had the same problem, I solved it like this:</p>
<pre><code>WantedOutput = sorted(MyDict, key=lambda x : MyDict[x])
</code></pre>
<p>(people who answer: "It is not possible to sort a dict" did not read the question!!
In fact "I can sort on the keys, but how can I sort based on the values?" clearly means that he... | 20 | 2010-11-18T14:19:57Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 5,227,519 | <p>This is the code:</p>
<pre><code>import operator
origin_list = [
{"name": "foo", "rank": 0, "rofl": 20000},
{"name": "Silly", "rank": 15, "rofl": 1000},
{"name": "Baa", "rank": 300, "rofl": 20},
{"name": "Zoo", "rank": 10, "rofl": 200},
{"name": "Penguin", "rank": -1, "rofl": 10000}
]
print ">... | 12 | 2011-03-08T02:06:25Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 7,237,524 | <p>It can often be very handy to use <b><a href="http://docs.python.org/library/collections.html#collections.namedtuple">namedtuple</a></b>. For example, you have a dictionary of 'name' as keys and 'score' as values and you want to sort on 'score':</p>
<pre><code>import collections
Player = collections.namedtuple('Pla... | 38 | 2011-08-30T00:30:15Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 7,817,348 | <p>Use <strong>ValueSortedDict</strong> from <a href="http://pypi.python.org/pypi/dicts">dicts</a>:</p>
<pre><code>from dicts.sorteddict import ValueSortedDict
d = {1: 2, 3: 4, 4:3, 2:1, 0:0}
sorted_dict = ValueSortedDict(d)
print sorted_dict.items()
[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
</code></pre>
| 5 | 2011-10-19T06:25:41Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 7,947,321 | <p>Iterate through a dict and sort it by its values in descending order:</p>
<pre><code>$ python --version
Python 3.2.2
$ cat sort_dict_by_val_desc.py
dictionary = dict(siis = 1, sana = 2, joka = 3, tuli = 4, aina = 5)
for word in sorted(dictionary, key=dictionary.get, reverse=True):
print(word, dictionary[word])
... | 4 | 2011-10-30T19:42:06Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 8,148,132 | <p>This works in 3.1.x:</p>
<pre><code>import operator
slovar_sorted=sorted(slovar.items(), key=operator.itemgetter(1), reverse=True)
print(slovar_sorted)
</code></pre>
| 4 | 2011-11-16T07:32:01Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 8,992,838 | <p>If your values are integers, and you use Python 2.7 or newer, you can use <a href="http://docs.python.org/py3k/library/collections.html#collections.Counter"><code>collections.Counter</code></a> instead of <code>dict</code>. The <code>most_common</code> method will give you all items, sorted by the value.</p>
| 5 | 2012-01-24T19:28:36Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 11,230,132 | <p>If values are numeric you may also use Counter from collections</p>
<pre><code>from collections import Counter
x={'hello':1,'python':5, 'world':3}
c=Counter(x)
print c.most_common()
>> [('python', 5), ('world', 3), ('hello', 1)]
</code></pre>
| 14 | 2012-06-27T15:43:45Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 13,208,582 | <p>Using Python 3.2:</p>
<pre><code>x = {"b":4, "a":3, "c":1}
for i in sorted(x.values()):
print(list(x.keys())[list(x.values()).index(i)])
</code></pre>
| 1 | 2012-11-03T11:07:21Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 15,310,681 | <p>You can use the <a href="http://docs.python.org/2/library/collections.html#collections.Counter">collections.Counter</a>. Note, this will work for both numeric and non-numeric values.</p>
<pre><code>>>> x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
>>> from collections import Counter
>>> #To sort in rev... | 17 | 2013-03-09T12:30:22Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 15,587,800 | <p>For the sake of completeness, I am posting a solution using <a href="http://docs.python.org/2/library/heapq.html" rel="nofollow">heapq</a>. Note, this method will work for both numeric and non-numeric values</p>
<pre><code>>>> x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
>>> x_items = x.items()
>>> he... | 3 | 2013-03-23T14:19:53Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 16,435,785 | <p>I came up with this one, </p>
<pre><code>import operator
x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
sorted_x = {k[0]:k[1] for k in sorted(x.items(), key=operator.itemgetter(1))}
</code></pre>
<p>For Python 3.x: <code>x.items()</code> replacing <code>iteritems()</code>.</p>
<pre><code>>>> sorted_x
{0: 0, 1: 2, 2... | 2 | 2013-05-08T08:17:55Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 18,375,444 | <p>In Python 2.7, simply do:</p>
<pre class="lang-py prettyprint-override"><code>from collections import OrderedDict
# regular unsorted dictionary
d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
# dictionary sorted by key
OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3)... | 23 | 2013-08-22T08:38:48Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 21,738,569 | <p>This returns the list of key-value pairs in the dictionary, sorted by value from highest to lowest:</p>
<pre><code>sorted(d.items(), key=lambda x: x[1], reverse=True)
</code></pre>
<p>For the dictionary sorted by key, use the following:</p>
<pre><code>sorted(d.items(), reverse=True)
</code></pre>
<p>The return i... | 6 | 2014-02-12T20:10:46Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 21,767,696 | <pre><code>months = {"January": 31, "February": 28, "March": 31, "April": 30, "May": 31,
"June": 30, "July": 31, "August": 31, "September": 30, "October": 31,
"November": 30, "December": 31}
def mykey(t):
""" Customize your sorting logic using this function. The parameter to
this function ... | 2 | 2014-02-13T23:18:20Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 21,784,867 | <pre><code>>>> import collections
>>> x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
>>> sorted_x = collections.OrderedDict(sorted(x.items(), key=lambda t:t[1]))
>>> OrderedDict([(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)])
</code></pre>
<p><code>OrderedDict</code> is subclass of <code>dict</code></p>... | 0 | 2014-02-14T16:44:34Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 22,150,003 | <p>Because of requirements to retain backward compatability with older versions of <a href="http://en.wikipedia.org/wiki/Python_%28programming_language%29" rel="nofollow">Python</a> I think the OrderedDict solution is very unwise. You want something that works with Python 2.7 and older versions.</p>
<p>But the collect... | 0 | 2014-03-03T14:58:30Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 22,903,797 | <p>Why not try this approach. Let us define a dictionary called mydict with the following data:</p>
<pre><code>mydict = {'carl':40,
'alan':2,
'bob':1,
'danny':3}
</code></pre>
<p>If one wanted to sort the dictionary by keys, one could do something like:</p>
<pre><code>for key in sorted(... | 9 | 2014-04-07T04:46:44Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 26,049,456 | <p>You can use a <a href="https://pypi.python.org/pypi/skipdict/1.0">skip dict</a> which is a dictionary that's permanently sorted by value.</p>
<pre><code>>>> data = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
>>> SkipDict(data)
{0: 0.0, 2: 1.0, 1: 2.0, 4: 3.0, 3: 4.0}
</code></pre>
<p>If you use <code>keys()</... | 9 | 2014-09-25T22:56:55Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 27,064,308 | <p>You can use the sorted function of Python</p>
<p><code>sorted(iterable[, cmp[, key[, reverse]]])</code></p>
<p>Thus you can use:</p>
<p><code>sorted(dictionary.items(),key = lambda x :x[1])</code></p>
<p>Visit this link for more information on sorted function: <a href="https://docs.python.org/2/library/functions... | 4 | 2014-11-21T15:04:09Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 30,949,456 | <p>Here is a solution using zip on <a href="https://docs.python.org/2/library/stdtypes.html#dictionary-view-objects" rel="nofollow"><code>d.values()</code> and <code>d.keys()</code></a>. A few lines down this link (on Dictionary view objects) is:</p>
<blockquote>
<p>This allows the creation of (value, key) pairs us... | 3 | 2015-06-20T01:44:58Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 31,689,638 | <p>I've found that the following function performs well compared to other solutions posted, even on large dictionaries.</p>
<pre><code>vsort = lambda d: sorted(d.iteritems(), key=lambda (k, v): v)
</code></pre>
<p>Example:</p>
<pre><code>data = {}
for i in range(10):
data[i] = i if i % 2 else -i
print 'Origina... | -3 | 2015-07-29T01:08:13Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 31,741,215 | <p>Of course, remember, you need to use <code>OrderedDict</code> because regular Python dictionaries don't keep the original order. </p>
<pre><code>from collections import OrderedDict
a = OrderedDict(sorted(originalDict.items(), key = lambda x: x[1]))
</code></pre>
<hr>
<p>If you do not have Python 2.7 or higher, th... | 2 | 2015-07-31T08:08:49Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 34,103,440 | <p><strong>UPDATE: 5 DECEMBER 2015 using Python 3.5</strong></p>
<p>Whilst I found the accepted answer useful, I was also surprised that it hasn't been updated to reference <strong><a href="https://docs.python.org/3/library/collections.html#collections.OrderedDict">OrderedDict</a></strong> from the standard library <s... | 29 | 2015-12-05T09:46:06Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 34,995,529 | <pre><code>>>> e = {1:39, 4:34, 7:110, 2:87}
>>> sortE = sorted(e.items(), key=lambda value: value[1])
>>> print(sortE)
[(4, 34), (1, 39), (2, 87), (7, 110)]
</code></pre>
<p>You can use a lambda function to sort things up by value and store them processed inside a variable, in this case <st... | 13 | 2016-01-25T14:54:43Z | [
"python",
"sorting",
"dictionary"
] |
Sort a Python dictionary by value | 613,183 | <p>I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.</p>
<p>I can sort on the keys, but how can I sort based on the values?</p>
<p>Note: I have read Stack Overflow question <a href="http://stackoverfl... | 1,837 | 2009-03-05T00:49:05Z | 39,424,969 | <h1>As of <a href="https://docs.python.org/3.6/whatsnew/3.6.html" rel="nofollow">Python 3.6</a> the builtin dict will be ordered</h1>
<p>Good news, so the OP's original use case of mapping pairs retrieved from a database with unique string ids as keys and numeric values as values into a builtin python v3.6+ dict, shou... | 1 | 2016-09-10T10:05:36Z | [
"python",
"sorting",
"dictionary"
] |
How does Vista Recycle bin work? | 613,246 | <p>I am trying to write a python module to move files to the 'Recycle Bin' on both Mac and PC. </p>
<p>Is there a way, only from the commandline (and yes, I mean using absloutly no C#/C++/etc) to move a file into the Recycle Bin, and have it appear as a file trashed by drag and drop (or deleted via SHFileOperation, et... | 5 | 2009-03-05T01:12:38Z | 613,258 | <p>You should use the <a href="http://msdn.microsoft.com/en-us/library/bb762164(VS.85).aspx" rel="nofollow"><code>SHFileOperation</code></a> function or, on Vista, the <a href="http://msdn.microsoft.com/en-us/library/bb775771(VS.85).aspx" rel="nofollow"><code>IFileOperation</code></a> interface (as pointed out by gix b... | 5 | 2009-03-05T01:17:39Z | [
"python",
"windows-vista",
"recycle-bin"
] |
How does Vista Recycle bin work? | 613,246 | <p>I am trying to write a python module to move files to the 'Recycle Bin' on both Mac and PC. </p>
<p>Is there a way, only from the commandline (and yes, I mean using absloutly no C#/C++/etc) to move a file into the Recycle Bin, and have it appear as a file trashed by drag and drop (or deleted via SHFileOperation, et... | 5 | 2009-03-05T01:12:38Z | 613,260 | <p>It looks like <a href="http://mail.python.org/pipermail/python-list/2001-June/091800.html" rel="nofollow">this mailing list entry</a> might help you.</p>
| -1 | 2009-03-05T01:18:39Z | [
"python",
"windows-vista",
"recycle-bin"
] |
How does Vista Recycle bin work? | 613,246 | <p>I am trying to write a python module to move files to the 'Recycle Bin' on both Mac and PC. </p>
<p>Is there a way, only from the commandline (and yes, I mean using absloutly no C#/C++/etc) to move a file into the Recycle Bin, and have it appear as a file trashed by drag and drop (or deleted via SHFileOperation, et... | 5 | 2009-03-05T01:12:38Z | 613,267 | <p>Moving files to Windows' Recycle Bin is a Shell operation. Shell operations are run via COM. For older Windows versions there is the <a href="http://msdn.microsoft.com/en-us/library/bb762164%28VS.85%29.aspx" rel="nofollow">SHFileOperation interface</a>. Since Vista there is the new <a href="http://msdn.microsoft.com... | 5 | 2009-03-05T01:20:37Z | [
"python",
"windows-vista",
"recycle-bin"
] |
How does Vista Recycle bin work? | 613,246 | <p>I am trying to write a python module to move files to the 'Recycle Bin' on both Mac and PC. </p>
<p>Is there a way, only from the commandline (and yes, I mean using absloutly no C#/C++/etc) to move a file into the Recycle Bin, and have it appear as a file trashed by drag and drop (or deleted via SHFileOperation, et... | 5 | 2009-03-05T01:12:38Z | 3,039,820 | <p>I have written a <a href="http://pypi.python.org/pypi/Send2Trash/" rel="nofollow">Python library that does precisely that</a>. You might want to check it out.</p>
| 3 | 2010-06-14T18:28:15Z | [
"python",
"windows-vista",
"recycle-bin"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 613,368 | <p>It seems like the ideal of "Pythonic" code serves a lot of the same purpose as <code>use strict</code>.</p>
| -2 | 2009-03-05T02:11:37Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 613,435 | <p>Python does have something that can change script syntax:</p>
<pre><code>from __future__ import print_function
</code></pre>
<p>and various other future-features that have syntax implications. It's just that Python's syntax has been stricter, stabler and more well-defined than historical Perl; the kind of things t... | 10 | 2009-03-05T02:54:53Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 613,439 | <p>I consider the <code>'use strict'</code> in Perl more like a pragma as you hinted at: it changes the behavior of the compiler. </p>
<p>Perl language philosophy is different from python philosophy. As in, you are given more than enough rope to hang yourself repeatedly, in Perl. </p>
<p>Larry Wall is big into lin... | 3 | 2009-03-05T02:57:31Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 613,492 | <p>I don't have a Perl background, but from what I know, there's no feature in python that needs to be disabled in order for your code to be "more reliable", so in that sense, I guess you can say it's unnecessary</p>
| -3 | 2009-03-05T03:31:28Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 613,499 | <p>"the run-time binding philosophy that Python embraces... makes "use strict" behavior unnecessary [and] especially undesirable"</p>
<p>Pretty good summary. Thanks.</p>
<p>That is essentially it. Static analysis tools don't help Python enough to be worthwhile. </p>
<p><hr /></p>
<p><strong>Edit</strong></p>
<p... | 11 | 2009-03-05T03:36:24Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 614,565 | <p>Python has no true lexical scoping, so strict vars wouldn't be very sensible. It has no symbolic references AFAIK, so it has not need for strict refs. It has not barewords, so it has no need for strict vars.</p>
<p>To be honest, it's only lexical scoping I miss. The other two I'd consider warts in Perl.</p>
| 5 | 2009-03-05T12:25:19Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 614,570 | <p>I think there's some confusion as the what "use strict" does, from the comments I'm seeing. It does not turn on compile time type checks (to be like Java). In that sense, Perl progammers are in agreement with python programmers. As S.Lott says above these types of checks don't protect against logic bugs, don't reduc... | 7 | 2009-03-05T12:27:03Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 615,306 | <p>Well, I'm not much of a python programmer, but I'd say that the answer is 'YES'.</p>
<p>Any dynamic language that lets you create a variable with any name at any time, could use a 'strict' pragma.</p>
<p>Strict vars (one of the options for strict in Perl, 'use strict' turns them all on at once) in Perl requires th... | 32 | 2009-03-05T15:37:14Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 616,256 | <p>I've found that I only really care about detecting references to undeclared vars. Eclipse has pylint integration via PyDev and, although pylint is far from perfect, it does a reasonable job at that.</p>
<p>It does kind of go against Python's dynamic nature, and I do have to add #IGNOREs occasionally, when my code ... | 1 | 2009-03-05T19:25:17Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 4,140,155 | <p>This original answer is correct, but does not perhaps explain the situation
in a practical sense.</p>
<blockquote>
<p>There exist static analysis tools for Python, but compile time checks tend to be >diametrically opposed to the run-time binding philosophy that Python embraces.</p>
</blockquote>
<p>What 'use str... | 3 | 2010-11-10T00:28:43Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 6,740,746 | <p>It is very difficult to write large programs without 'use strict' in Perl.
Without 'use strict', if you use a variable again, and misspell it by leaving a letter out, the program still runs. And without test cases to check your results, you can never find such errors. It can be very time-consuming to find why you ar... | 1 | 2011-07-18T23:44:44Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Is there a need for a "use strict" Python compiler? | 613,364 | <p>There exist <a href="http://stackoverflow.com/questions/35470/are-there-any-static-analysis-tools-for-python">static analysis tools for Python</a>, but compile time checks tend to be diametrically opposed to the <a href="http://python-history.blogspot.com/2009/01/introduction-and-overview.html">run-time binding phil... | 28 | 2009-03-05T02:08:25Z | 25,151,630 | <p>Perl is a unrestrained language as they said :) . So you can use the variable before announced ; Eg : If you use a var name "is_array" but type in "is_arrby" ,the compiler won't report error without "use strict" . So when coding long program in perl ,better use "use strict" statement. Of course, less than 50 lines f... | 0 | 2014-08-06T03:10:24Z | [
"python",
"perl",
"compiler-construction",
"static-analysis",
"use-strict"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 613,473 | <p>Whenever I wanted to do this, I would just scrape <a href="http://whatismyip.org" rel="nofollow">whatismyip.org</a>. When you go to the site, it gives you your plain text public IP. Plain and simple.</p>
<p>Just have your script access that site and read the IP.</p>
<p>I don't know if you were implying this in you... | 8 | 2009-03-05T03:20:57Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 613,477 | <p>Your simplest way may be to ask some server on the outside of your network.</p>
<p>One thing to keep in mind is that different destinations may see a different address for you. The router may be multihomed. And really that's just where problems begin.</p>
| 3 | 2009-03-05T03:22:34Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 613,482 | <p>I have made a program that connects to <a href="http://automation.whatismyip.com/n09230945.asp" rel="nofollow">http://automation.whatismyip.com/n09230945.asp</a>
it is is written in D an getting someone else to tell you what they see your ip as is probably the most reliable way:</p>
<pre><code>/*
Get my IP addr... | 17 | 2009-03-05T03:26:16Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 613,518 | <p>If the network has an UpNp server running on the gateway you are able to talk to the gateway and ask it for your outside IP address.</p>
| 3 | 2009-03-05T03:52:25Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 613,565 | <p>Targeting www.whatsmyip.org is rude. They plea not to do that on the page.</p>
<p>Only a system on the same level of NAT as your target will see the same IP.
For instance, your application may be behind multiple layers of NAT (this happens more as you move away from the US, where the glut of IPs are).</p>
<p>STUN ... | 10 | 2009-03-05T04:30:19Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 613,600 | <p>This may be the easiest way. Parse the output of the following commands:</p>
<ol>
<li>run a traceroute to find a router that is less than 3 hops out from your machine.</li>
<li>run ping with the option to record the source route and parse the output. The first IP address in the recorded route is your public one.</l... | 20 | 2009-03-05T04:47:07Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 628,180 | <p>As mentioned by several people, <a href="http://en.wikipedia.org/wiki/STUN" rel="nofollow">STUN</a> is indeed the proper solution.</p>
<ul>
<li>a list of <a href="http://www.voip-info.org/wiki-STUN" rel="nofollow">public STUN servers</a></li>
<li>a free software <a href="http://sourceforge.net/projects/stun/" rel="... | 3 | 2009-03-09T21:54:33Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 5,199,330 | <p>another cheeky way:
if your router has got the option to update it's web IP on <a href="http://dyn.com/dns/" rel="nofollow">DynDNS</a>, you can get your own IP with something like:</p>
<pre><code>IP=`resolveip -s myalias.dyndns-home.com`
</code></pre>
| 1 | 2011-03-04T21:13:42Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 16,698,596 | <p><strong>EDIT</strong>: curlmyip.com is no longer available. (thanks maxywb)</p>
<p><strong>Original Post</strong>:</p>
<p>As of writing this post, curlmyip.com works. From the command line:</p>
<pre><code>curl curlmyip.com
</code></pre>
<p>It's a third-party website, which may or may not be available a couple ye... | 8 | 2013-05-22T17:57:52Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 22,075,729 | <p>To get your external ip, you could make a dns query to an opendns server with the special hostname "myip.opendns.com":</p>
<pre><code>from subprocess import check_output
ip = check_output(["dig", "+short", "@resolver1.opendns.com",
"myip.opendns.com"]).decode().strip()
</code></pre>
<p>On Windo... | 3 | 2014-02-27T17:22:34Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 33,743,758 | <p>Duck Duck Go gives free access to their API according to their own page here:
<a href="https://duckduckgo.com/api" rel="nofollow">https://duckduckgo.com/api</a></p>
<p>Here's the URL you hit if you want your IP address:
<a href="http://api.duckduckgo.com/?q=my+ip&format=json" rel="nofollow">http://api.duckduckg... | 0 | 2015-11-16T20:10:49Z | [
"python",
"ip-address",
"tcp"
] |
Discovering public IP programatically | 613,471 | <p>I'm behind a router, I need a simple command to discover my public ip (instead of googling what's my ip and clicking one the results)</p>
<p>Are there any standard protocols for this? I've heard about STUN but I don't know how can I use it?</p>
<p>P.S. I'm planning on writing a short python script to do it</p>
| 23 | 2009-03-05T03:18:42Z | 35,317,247 | <p>I like the <a href="https://ipify.org" rel="nofollow">ipify.org</a>:</p>
<ul>
<li>it's free to use (even programmatically and even heavy traffic)</li>
<li>response contains only the IP address without any garbage (no need for parsing)</li>
<li>you can also request response in JSON</li>
<li>works for both IPv4 and I... | 3 | 2016-02-10T14:00:15Z | [
"python",
"ip-address",
"tcp"
] |
Adding twisted code to a pygtk app | 613,777 | <p>I have a simple pygtk app using urllib2, what changes should I make to add working twisted code?</p>
<p>The pbgtk2.py example it's confusing</p>
| 0 | 2009-03-05T06:28:20Z | 614,449 | <p>You switch from using the gtk mainloop to the right <a href="http://twistedmatrix.com/projects/core/documentation/howto/choosing-reactor.html#auto11" rel="nofollow">Twisted reactor</a>. Or you decide to run Twisted in a separate thread using <a href="http://unpythonic.blogspot.com/2008/10/what-twisted-could-learn-fr... | 4 | 2009-03-05T11:48:22Z | [
"python",
"pygtk",
"twisted"
] |
Why is the regular expression returning an error in python? | 614,205 | <p>Am trying the following regular expression in python but it returns an error</p>
<pre><code>import re
...
#read a line from a file to variable line
# loking for the pattern 'WORD' in the line ...
m=re.search('(?<=[WORD])\w+',str(line))
m.group(0)
</code></pre>
<p>i get the following error:</p>
<p><strong>Att... | 2 | 2009-03-05T10:13:47Z | 614,217 | <p>This is happening because the regular expression wasn't matched. Therefore m is None and of course you can't access group[0]. You need to first test that the search was successful, before trying to access group members.</p>
| 4 | 2009-03-05T10:16:21Z | [
"python",
"regex"
] |
Why is the regular expression returning an error in python? | 614,205 | <p>Am trying the following regular expression in python but it returns an error</p>
<pre><code>import re
...
#read a line from a file to variable line
# loking for the pattern 'WORD' in the line ...
m=re.search('(?<=[WORD])\w+',str(line))
m.group(0)
</code></pre>
<p>i get the following error:</p>
<p><strong>Att... | 2 | 2009-03-05T10:13:47Z | 614,220 | <p><a href="http://docs.python.org/library/re.html#re.search" rel="nofollow">re.search documentation says</a>:</p>
<blockquote>
<p>Return <strong>None</strong> if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.</p>
</blockquote>... | 2 | 2009-03-05T10:17:19Z | [
"python",
"regex"
] |
Why is the regular expression returning an error in python? | 614,205 | <p>Am trying the following regular expression in python but it returns an error</p>
<pre><code>import re
...
#read a line from a file to variable line
# loking for the pattern 'WORD' in the line ...
m=re.search('(?<=[WORD])\w+',str(line))
m.group(0)
</code></pre>
<p>i get the following error:</p>
<p><strong>Att... | 2 | 2009-03-05T10:13:47Z | 614,221 | <pre><code>>>> help(re.search)
Help on function search in module re:
search(pattern, string, flags=0)
Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found.
</code></pre>
<p>Look at the last line: <strong>returning None if no match was found</... | 2 | 2009-03-05T10:17:31Z | [
"python",
"regex"
] |
Why is the regular expression returning an error in python? | 614,205 | <p>Am trying the following regular expression in python but it returns an error</p>
<pre><code>import re
...
#read a line from a file to variable line
# loking for the pattern 'WORD' in the line ...
m=re.search('(?<=[WORD])\w+',str(line))
m.group(0)
</code></pre>
<p>i get the following error:</p>
<p><strong>Att... | 2 | 2009-03-05T10:13:47Z | 614,295 | <p>BTW, your regular expression is incorrect.
If you look for 'WORD', it should be just 'WORD'. <code>str</code> is also extraneous. Your code should look like this:</p>
<pre><code>m = re.search('WORD', line)
if m:
print m.group[0]
</code></pre>
<p>Your original regexp will return probably unexpected and undesire... | 1 | 2009-03-05T10:46:45Z | [
"python",
"regex"
] |
Why is the regular expression returning an error in python? | 614,205 | <p>Am trying the following regular expression in python but it returns an error</p>
<pre><code>import re
...
#read a line from a file to variable line
# loking for the pattern 'WORD' in the line ...
m=re.search('(?<=[WORD])\w+',str(line))
m.group(0)
</code></pre>
<p>i get the following error:</p>
<p><strong>Att... | 2 | 2009-03-05T10:13:47Z | 614,643 | <p>Two issues:</p>
<ol>
<li><p>your pattern does not match, therefore <code>m</code> is set to <code>None</code>, and <code>None</code> has no <code>group</code> attribute.</p></li>
<li><p>I believe you meant either:</p>
<pre><code>m= re.search(r"(?<=WORD)\w+", str(line))
</code></pre>
<p>as entered, or</p>
<pre... | 2 | 2009-03-05T12:53:45Z | [
"python",
"regex"
] |
How do i write a regular expression for the following pattern in python? | 614,458 | <p>How do i look for the following pattern using regular expression in python? for the two cases</p>
<p>Am looking for str2 after the "=" sign </p>
<ul>
<li>Case 1: <code>str1=str2</code></li>
<li>Case 2: <code>str1 = str2</code></li>
</ul>
<p>please note there can be a <strong>space or none</strong> between the eit... | 0 | 2009-03-05T11:50:50Z | 614,474 | <p>I think a regex is overkill if you only want to deal with the above two cases. Here's what I'd do-</p>
<pre><code>>>> case1 = "str1=str2"
>>> case2 = "str1 = str2"
>>> case2.split()
['str1', '=', 'str2']
>>> ''.join(case2.split())
'str1=str2'
>>> case1[5:]
'str2'
>>... | 0 | 2009-03-05T11:56:41Z | [
"python",
"regex",
"string"
] |
How do i write a regular expression for the following pattern in python? | 614,458 | <p>How do i look for the following pattern using regular expression in python? for the two cases</p>
<p>Am looking for str2 after the "=" sign </p>
<ul>
<li>Case 1: <code>str1=str2</code></li>
<li>Case 2: <code>str1 = str2</code></li>
</ul>
<p>please note there can be a <strong>space or none</strong> between the eit... | 0 | 2009-03-05T11:50:50Z | 614,477 | <pre><code>re.search(r'=\s*(.*)', 'str = str2').group(1)
</code></pre>
<p>or if you just want a single word:</p>
<pre><code>re.search(r'=\s*(\w+)', 'str = str2').group(1)
</code></pre>
<p>Extended to specific initial string:</p>
<pre><code>re.search(r'\bstr\s*=\s*(\w+)', 'str=str2').group(1)
</code></pre>
<p><code... | 3 | 2009-03-05T11:57:28Z | [
"python",
"regex",
"string"
] |
How do i write a regular expression for the following pattern in python? | 614,458 | <p>How do i look for the following pattern using regular expression in python? for the two cases</p>
<p>Am looking for str2 after the "=" sign </p>
<ul>
<li>Case 1: <code>str1=str2</code></li>
<li>Case 2: <code>str1 = str2</code></li>
</ul>
<p>please note there can be a <strong>space or none</strong> between the eit... | 0 | 2009-03-05T11:50:50Z | 614,508 | <p>if you indeed have only such simple strings to parse you don't need regular expression. you can just partition on <code>=</code> and strip (or even lstrip) last element of a resulting tuple:</p>
<pre><code>>>> case = 'str = str2'
>>> case.partition('=')[2].lstrip()
'str2'
</code></pre>
<p>it'll b... | 8 | 2009-03-05T12:08:53Z | [
"python",
"regex",
"string"
] |
How do i write a regular expression for the following pattern in python? | 614,458 | <p>How do i look for the following pattern using regular expression in python? for the two cases</p>
<p>Am looking for str2 after the "=" sign </p>
<ul>
<li>Case 1: <code>str1=str2</code></li>
<li>Case 2: <code>str1 = str2</code></li>
</ul>
<p>please note there can be a <strong>space or none</strong> between the eit... | 0 | 2009-03-05T11:50:50Z | 614,511 | <p>Expanding on @batbrat's answer, and the other suggestions, you can use <code>re.split()</code> to separate the input string. The pattern can use <code>\s</code> (whitespace) or an explicit space.</p>
<pre><code>>>> import re
>>> c1="str1=str2"
>>> c2="str1 = str2"
>>> re.split(' ... | 0 | 2009-03-05T12:09:23Z | [
"python",
"regex",
"string"
] |
How do i write a regular expression for the following pattern in python? | 614,458 | <p>How do i look for the following pattern using regular expression in python? for the two cases</p>
<p>Am looking for str2 after the "=" sign </p>
<ul>
<li>Case 1: <code>str1=str2</code></li>
<li>Case 2: <code>str1 = str2</code></li>
</ul>
<p>please note there can be a <strong>space or none</strong> between the eit... | 0 | 2009-03-05T11:50:50Z | 614,528 | <p>If your data is fixed then you can do this without using regex. Just split it on '='.
For example:</p>
<pre><code>>>> case1 = "str1=str2"
>>> case2 = "str1 = str2"
>>> str2 = case1.split('=')[1].strip()
>>> str2 = case2.split('=')[1].strip()
</code></pre>
<p>This <code>YOURCASE... | 1 | 2009-03-05T12:14:37Z | [
"python",
"regex",
"string"
] |
How do i write a regular expression for the following pattern in python? | 614,458 | <p>How do i look for the following pattern using regular expression in python? for the two cases</p>
<p>Am looking for str2 after the "=" sign </p>
<ul>
<li>Case 1: <code>str1=str2</code></li>
<li>Case 2: <code>str1 = str2</code></li>
</ul>
<p>please note there can be a <strong>space or none</strong> between the eit... | 0 | 2009-03-05T11:50:50Z | 614,684 | <p>Two cases:</p>
<ul>
<li><p>(case 1) if there is a single space before the '=', then there <strong>must</strong> also be a single space after the '='</p>
<pre><code>m=re.search(r'(?<=\S)(?:\s=\s|=)(\w+)','str = str2')
print m.group(1)
</code></pre></li>
<li><p>(case 2) otherwise,</p>
<pre><code>m=re.search(r'(?... | 0 | 2009-03-05T13:09:22Z | [
"python",
"regex",
"string"
] |
How do i write a regular expression for the following pattern in python? | 614,458 | <p>How do i look for the following pattern using regular expression in python? for the two cases</p>
<p>Am looking for str2 after the "=" sign </p>
<ul>
<li>Case 1: <code>str1=str2</code></li>
<li>Case 2: <code>str1 = str2</code></li>
</ul>
<p>please note there can be a <strong>space or none</strong> between the eit... | 0 | 2009-03-05T11:50:50Z | 618,355 | <p>Related idea: I find using graphical regular expression tool helpful when trying to figure out correct pattern: <a href="http://kodos.sf.net" rel="nofollow">http://kodos.sf.net</a>.</p>
| 0 | 2009-03-06T10:24:49Z | [
"python",
"regex",
"string"
] |
How do i write a regular expression for the following pattern in python? | 614,458 | <p>How do i look for the following pattern using regular expression in python? for the two cases</p>
<p>Am looking for str2 after the "=" sign </p>
<ul>
<li>Case 1: <code>str1=str2</code></li>
<li>Case 2: <code>str1 = str2</code></li>
</ul>
<p>please note there can be a <strong>space or none</strong> between the eit... | 0 | 2009-03-05T11:50:50Z | 629,318 | <p>Simply use split function</p>
| 1 | 2009-03-10T08:25:01Z | [
"python",
"regex",
"string"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.