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 |
|---|---|---|---|---|---|---|---|---|---|
Fill list with objects and sort (Newbie) | 1,920,315 | <p>I'm new to Python, so please forgive me when using wrong terms :)</p>
<p>I'd like to have a list of several "objects", each of them having the same numeric attributes (A, B, C). This list should then be sorted by the value of attribute A.</p>
<p>In Java I'd define a Class with my attributes as members, implement S... | 0 | 2009-12-17T08:50:49Z | 1,920,365 | <pre><code>List = [(3,1,1),(1,2,4),(2,5,1),(1,2,3)]
sorted(List)
</code></pre>
<p>Update: The heart of the answer is actually the <code>sorted</code> built-in. I just put it on two lines to allow for incremental inspection that (a) the data structure chosen is a list of tuples, and (b) the sorting is done by <code>sor... | 2 | 2009-12-17T09:01:41Z | [
"python",
"list",
"sorting",
"object"
] |
Fill list with objects and sort (Newbie) | 1,920,315 | <p>I'm new to Python, so please forgive me when using wrong terms :)</p>
<p>I'd like to have a list of several "objects", each of them having the same numeric attributes (A, B, C). This list should then be sorted by the value of attribute A.</p>
<p>In Java I'd define a Class with my attributes as members, implement S... | 0 | 2009-12-17T08:50:49Z | 1,920,370 | <pre><code>class myclass(object):
def __init__(self, a, b, c):
self.a = a
self.b = b
self.c = c
def __repr__(self):
return "(a=%s, b=%s, c=%s)" % (self.a, self.b, self.c)
>>> obj1 = myclass(1, 2, 3)
>>> obj2 = myclass(1, 2, 4)
>>> obj3 = myclass(2, 5,... | 6 | 2009-12-17T09:03:27Z | [
"python",
"list",
"sorting",
"object"
] |
Fill list with objects and sort (Newbie) | 1,920,315 | <p>I'm new to Python, so please forgive me when using wrong terms :)</p>
<p>I'd like to have a list of several "objects", each of them having the same numeric attributes (A, B, C). This list should then be sorted by the value of attribute A.</p>
<p>In Java I'd define a Class with my attributes as members, implement S... | 0 | 2009-12-17T08:50:49Z | 1,920,371 | <p>You can give a comparison function as first argument of the <code>sort</code> of a list See the code below:</p>
<pre><code>class Foo:
def __init__(self, a, b=0, c=0):
self.a = a
self.b = b
self.c = c
def __repr__(self):
return "%d %d %d" % (self.a, self.b, self.c)
foos = ... | 2 | 2009-12-17T09:03:27Z | [
"python",
"list",
"sorting",
"object"
] |
Fill list with objects and sort (Newbie) | 1,920,315 | <p>I'm new to Python, so please forgive me when using wrong terms :)</p>
<p>I'd like to have a list of several "objects", each of them having the same numeric attributes (A, B, C). This list should then be sorted by the value of attribute A.</p>
<p>In Java I'd define a Class with my attributes as members, implement S... | 0 | 2009-12-17T08:50:49Z | 1,920,429 | <p>Consider using a <code>namedtuple</code> to create your objects. (Have a look at <a href="http://stackoverflow.com/questions/1831218/is-there-a-tuple-data-structure-in-python">is-there-a-tuple-data-structure-in-python</a>.)</p>
<blockquote>
<p><a href="http://docs.python.org/library/collections.html#collections.n... | 3 | 2009-12-17T09:19:39Z | [
"python",
"list",
"sorting",
"object"
] |
Partial evaluation with pyparsing | 1,920,588 | <p>I need to be able to take a formula that uses the OpenDocument formula syntax, parse it into syntax that Python can understand, but without evaluating the variables, and then be able to evaluate the formula many times with changing valuables for the variables.
Formulas can be user input, so pyparsing allows me to bo... | 3 | 2009-12-17T09:49:49Z | 1,921,795 | <p>1) Yes, it is possible to pickle the results from parsing your expression, and save that to a database. Then you can just fetch and unpickle the expression, rather than reparse the original again. </p>
<p>2) You can do a quick-and-dirty pass at this just using the compile and eval built-ins, as in the following i... | 4 | 2009-12-17T13:31:06Z | [
"python",
"parsing",
"evaluation",
"pyparsing"
] |
Python, Ruby, Haskell - Do they provide true multithreading? | 1,920,805 | <p>We are planning to write a highly concurrent application in any of the Very-High Level programming languages.</p>
<p>1) Do Python, Ruby, or Haskell support true multithreading? </p>
<p>2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there... | 16 | 2009-12-17T10:33:03Z | 1,920,818 | <p>The current version of Ruby 1.9(YARV- C based version) has native threads but has the problem of GIL. As I know Python also has the problem of GIL. </p>
<p>However both Jython and JRuby(mature Java implementations of both Ruby and Python) provide native multithreading, no green threads and no GIL. </p>
<p>Don't kn... | 7 | 2009-12-17T10:35:12Z | [
"python",
"ruby",
"concurrency",
"haskell",
"multithreading"
] |
Python, Ruby, Haskell - Do they provide true multithreading? | 1,920,805 | <p>We are planning to write a highly concurrent application in any of the Very-High Level programming languages.</p>
<p>1) Do Python, Ruby, or Haskell support true multithreading? </p>
<p>2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there... | 16 | 2009-12-17T10:33:03Z | 1,920,829 | <p>Haskell is thread-capable, in addition you get <a href="http://en.wikipedia.org/wiki/Purely%5Ffunctional" rel="nofollow">pure</a> functional language - no <a href="http://en.wikipedia.org/wiki/Side%5Feffect%5F%28computer%5Fscience%29" rel="nofollow">side effects</a> </p>
| 1 | 2009-12-17T10:36:17Z | [
"python",
"ruby",
"concurrency",
"haskell",
"multithreading"
] |
Python, Ruby, Haskell - Do they provide true multithreading? | 1,920,805 | <p>We are planning to write a highly concurrent application in any of the Very-High Level programming languages.</p>
<p>1) Do Python, Ruby, or Haskell support true multithreading? </p>
<p>2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there... | 16 | 2009-12-17T10:33:03Z | 1,920,843 | <p>For real concurrency, you probably want to try Erlang.</p>
| 1 | 2009-12-17T10:39:43Z | [
"python",
"ruby",
"concurrency",
"haskell",
"multithreading"
] |
Python, Ruby, Haskell - Do they provide true multithreading? | 1,920,805 | <p>We are planning to write a highly concurrent application in any of the Very-High Level programming languages.</p>
<p>1) Do Python, Ruby, or Haskell support true multithreading? </p>
<p>2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there... | 16 | 2009-12-17T10:33:03Z | 1,920,979 | <p>The GHC compiler will run your program on multiple OS threads (and thus multiple cores) if you compile with the <code>-threaded</code> option and then pass <code>+RTS -N<x> -RTS</code> at runtime, where <code><x></code> = the number of OS threads you want.</p>
| 16 | 2009-12-17T11:00:29Z | [
"python",
"ruby",
"concurrency",
"haskell",
"multithreading"
] |
Python, Ruby, Haskell - Do they provide true multithreading? | 1,920,805 | <p>We are planning to write a highly concurrent application in any of the Very-High Level programming languages.</p>
<p>1) Do Python, Ruby, or Haskell support true multithreading? </p>
<p>2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there... | 16 | 2009-12-17T10:33:03Z | 1,921,244 | <p>Haskell is suitable for anything.
python has <code>processing</code> module, which (I think - not sure) helps to avoid GIL problems. (so it suitable for anything too). </p>
<p>But my opinion - best way you can do is to select highest level possible language with static type system for big and huge things. Today thi... | -2 | 2009-12-17T11:53:47Z | [
"python",
"ruby",
"concurrency",
"haskell",
"multithreading"
] |
Python, Ruby, Haskell - Do they provide true multithreading? | 1,920,805 | <p>We are planning to write a highly concurrent application in any of the Very-High Level programming languages.</p>
<p>1) Do Python, Ruby, or Haskell support true multithreading? </p>
<p>2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there... | 16 | 2009-12-17T10:33:03Z | 1,921,624 | <p>I second the choice of Erlang. Erlang can support distributed highly concurrent programming out of the box. Does not matter whether you callit "multi-threading" or "multi-processing". Two important elements to consider are the level of concurrency and the fact that Erlang processes <strong>do not share</strong> stat... | 1 | 2009-12-17T12:59:41Z | [
"python",
"ruby",
"concurrency",
"haskell",
"multithreading"
] |
Python, Ruby, Haskell - Do they provide true multithreading? | 1,920,805 | <p>We are planning to write a highly concurrent application in any of the Very-High Level programming languages.</p>
<p>1) Do Python, Ruby, or Haskell support true multithreading? </p>
<p>2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there... | 16 | 2009-12-17T10:33:03Z | 1,921,825 | <blockquote>
<p>1) Do Python, Ruby, or Haskell support true multithreading?</p>
</blockquote>
<p>This has nothing to do with the language. It is a question of the hardware (if the machine only has 1 CPU, it is simply physically impossible to execute two instructions at the same time), the Operating System (again, if... | 32 | 2009-12-17T13:35:17Z | [
"python",
"ruby",
"concurrency",
"haskell",
"multithreading"
] |
Python, Ruby, Haskell - Do they provide true multithreading? | 1,920,805 | <p>We are planning to write a highly concurrent application in any of the Very-High Level programming languages.</p>
<p>1) Do Python, Ruby, or Haskell support true multithreading? </p>
<p>2) If a program contains threads, will a Virtual Machine automatically assign work to multiple cores (or to physical CPUs if there... | 16 | 2009-12-17T10:33:03Z | 1,925,211 | <p>The Haskell implementation, GHC, supports multiple mechanisms for parallel execution on shared memory multicore. These mechanisms are described in "<a href="http://www.haskell.org/~simonmar/bib/multicore-ghc-09_abstract.html" rel="nofollow">Runtime Support for Multicore Haskell</a>".</p>
<p>Concretely, the Haskell ... | 22 | 2009-12-17T23:04:44Z | [
"python",
"ruby",
"concurrency",
"haskell",
"multithreading"
] |
bulkloader.py --dump without authentication | 1,920,936 | <p>Is there some way or using the bulkloader.py dump and restore functionality without authentication?
I have tried using:</p>
<pre><code>- url: /remote_api
script: $PYTHON_LIB/google/appengine/ext/remote_api/handler.py
</code></pre>
<p>without the login-parameter, but login still seems to be required.
I still get<... | 0 | 2009-12-17T10:53:47Z | 1,922,287 | <p>remote_api, which the bulkloader uses, is written to deliberately require authentication, even if you omit the relevant clause in app.yaml. You can override it if you really want, but it's an incredibly bad idea - it would allow any anonymous user to do practically anything they liked to your app!</p>
| 1 | 2009-12-17T14:56:47Z | [
"python",
"google-app-engine",
"backup"
] |
Linux group scheduling for user not being applied to setuid-ed process | 1,920,952 | <p>On the 2.6.28-11 Linux kernel, I am using setpriority to bias the amount of cpu time different user processes receive. User 1 is to receive double the CPU power of user 2. Using Linux's setpriority, I have assigned user 2 a lower priority (higher in terms of nice values). When I run the exact same program via the sh... | 1 | 2009-12-17T10:56:27Z | 1,924,824 | <p>Right, this is the designed behavior, even if it's not what you want. You can update your own priority to match what you should have inherited, if you were started normally:</p>
<pre><code>/* C */
#include <sys/resource.h>
int proc_prio = getpriority(PRIO_PROCESS, getpid()),
pgrp_prio = getpriority(PRIO_... | 2 | 2009-12-17T21:48:16Z | [
"python",
"linux",
"scheduling",
"scheduler",
"setuid"
] |
How can I run a python script on windows? | 1,920,997 | <p>Can anyone please tell me an IDE for running python programs? Is it possible to run the program through command line?</p>
| -3 | 2009-12-17T11:04:45Z | 1,921,019 | <p>Take a look at <a href="http://www.activestate.com/activepython/" rel="nofollow">ActiveState's ActivePython</a>. It's quite a nice implementation of Python on Windows. Another way is using <a href="http://www.cygwin.com" rel="nofollow">Cygwin's Python port</a>. These two are Python <em>implementations</em>. I don't ... | 3 | 2009-12-17T11:07:24Z | [
"python",
"ide"
] |
How can I run a python script on windows? | 1,920,997 | <p>Can anyone please tell me an IDE for running python programs? Is it possible to run the program through command line?</p>
| -3 | 2009-12-17T11:04:45Z | 1,921,032 | <p>IDE for running scripts? You can have any IDE you like, but if you need only to run python scripts you go like this:</p>
<p>python.exe pythonScript.py</p>
| 3 | 2009-12-17T11:09:24Z | [
"python",
"ide"
] |
How can I run a python script on windows? | 1,920,997 | <p>Can anyone please tell me an IDE for running python programs? Is it possible to run the program through command line?</p>
| -3 | 2009-12-17T11:04:45Z | 1,921,033 | <p>I like the <a href="http://www.easyeclipse.org/site/distributions/index.html" rel="nofollow">EasyEclipse for python</a> distribution. You'd need to have python and java installed of course.</p>
| 0 | 2009-12-17T11:09:59Z | [
"python",
"ide"
] |
How can I run a python script on windows? | 1,920,997 | <p>Can anyone please tell me an IDE for running python programs? Is it possible to run the program through command line?</p>
| -3 | 2009-12-17T11:04:45Z | 1,921,097 | <p>PyDev and Komodo Edit are 2 nice Python IDE on Windows.</p>
<p>I also like the SciTE text editor very much.</p>
<p>These 3 solutions make possible to run Python scripts</p>
| 0 | 2009-12-17T11:24:44Z | [
"python",
"ide"
] |
How can I run a python script on windows? | 1,920,997 | <p>Can anyone please tell me an IDE for running python programs? Is it possible to run the program through command line?</p>
| -3 | 2009-12-17T11:04:45Z | 33,467,368 | <p>I tried to run a Python script with multiprocessing on windows. <a href="http://stackoverflow.com/questions/5549190/is-shared-readonly-data-copied-to-different-processes-for-python-multiprocessing/5550156#5550156">see this tutorial</a></p>
<p>It does not work on Windows, but on raspian it went very well. Thus I kne... | 0 | 2015-11-01T21:21:18Z | [
"python",
"ide"
] |
Python many-to-one mapping (creating equivalence classes) | 1,921,027 | <p>I have a project of converting one database to another. One of the original database columns defines the row's category. This column should be mapped to a new category in the new database.</p>
<p>For example, let's assume the original categories are:<code>parrot, spam, cheese_shop, Cleese, Gilliam, Palin</code></p>... | 9 | 2009-12-17T11:08:36Z | 1,921,079 | <pre><code>>>> monty={ ('parrot','spam','cheese_shop'): 'sketch',
('Cleese', 'Gilliam', 'Palin') : 'actors'}
>>> item=lambda x:[z for y,z in monty.items() if x in y][0]
>>>
>>> item("parrot")
'sketch'
>>> item("Cleese")
'actors'
</code></pre>
<p>But let me tell yo... | 1 | 2009-12-17T11:20:08Z | [
"python",
"many-to-one",
"equivalence-classes"
] |
Python many-to-one mapping (creating equivalence classes) | 1,921,027 | <p>I have a project of converting one database to another. One of the original database columns defines the row's category. This column should be mapped to a new category in the new database.</p>
<p>For example, let's assume the original categories are:<code>parrot, spam, cheese_shop, Cleese, Gilliam, Palin</code></p>... | 9 | 2009-12-17T11:08:36Z | 1,921,116 | <p>You could override dict's indexer, but perhaps the following simpler solution would be better:</p>
<pre><code>>>> assoc_list = ( (('parrot','spam','cheese_shop'), 'sketch'), (('Cleese', 'Gilliam', 'Palin'), 'actors') )
>>> equiv_dict = dict()
>>> for keys, value in assoc_list:
for key... | 4 | 2009-12-17T11:28:47Z | [
"python",
"many-to-one",
"equivalence-classes"
] |
Python many-to-one mapping (creating equivalence classes) | 1,921,027 | <p>I have a project of converting one database to another. One of the original database columns defines the row's category. This column should be mapped to a new category in the new database.</p>
<p>For example, let's assume the original categories are:<code>parrot, spam, cheese_shop, Cleese, Gilliam, Palin</code></p>... | 9 | 2009-12-17T11:08:36Z | 1,921,146 | <p>It seems to me that you have two concerns. First, how do you express your mapping originally, that is, how do you type the mapping into your new_mapping.py file. Second, how does the mapping work during the re-mapping process. There's no reason for these two representations to be the same. </p>
<p>Start with the... | 9 | 2009-12-17T11:32:48Z | [
"python",
"many-to-one",
"equivalence-classes"
] |
Python many-to-one mapping (creating equivalence classes) | 1,921,027 | <p>I have a project of converting one database to another. One of the original database columns defines the row's category. This column should be mapped to a new category in the new database.</p>
<p>For example, let's assume the original categories are:<code>parrot, spam, cheese_shop, Cleese, Gilliam, Palin</code></p>... | 9 | 2009-12-17T11:08:36Z | 38,548,202 | <p>If you want to have multiple keys pointing to the same value, i.e. </p>
<p><code>m_dictionary{('k1', 'k2', 'k3', 'k4'):1, ('k5', 'k6'):2}</code> and access them as,</p>
<pre><code>`print(m_dictionary['k1'])` ==> `1`.
</code></pre>
<p>Check this multi dictionary python module <code>multi_key_dict</code>. Instal... | 0 | 2016-07-24T02:27:45Z | [
"python",
"many-to-one",
"equivalence-classes"
] |
The best solution for distribution website? | 1,921,559 | <p>Ok, I have a question from a <strong>"client"</strong> perspective. Let's say we are talking about website designed for distribution: products + their logistics info. </p>
<p>Definitely less than a 2k rows, rarely changed but often accessed. Typical row with several columns will have to consist of a picture so it m... | 0 | 2009-12-17T12:49:45Z | 1,921,584 | <p>I would not comment about Django & Python. But a more simpler way to store images would be to store just the "path" (location in the directory) in the tables, and load the path in your application/framework.</p>
| 0 | 2009-12-17T12:54:05Z | [
"python",
"django",
"postgresql"
] |
The best solution for distribution website? | 1,921,559 | <p>Ok, I have a question from a <strong>"client"</strong> perspective. Let's say we are talking about website designed for distribution: products + their logistics info. </p>
<p>Definitely less than a 2k rows, rarely changed but often accessed. Typical row with several columns will have to consist of a picture so it m... | 0 | 2009-12-17T12:49:45Z | 1,921,646 | <p><strong>Website</strong> For small webservices you can use micro-frameworks like <a href="http://www.sinatrarb.com/" rel="nofollow">http://www.sinatrarb.com/</a> (which is written in ruby); you write simple and useful apps in a few dozens or hundreds of lines. Plug your database in via some object-relational mapper ... | 0 | 2009-12-17T13:02:59Z | [
"python",
"django",
"postgresql"
] |
The best solution for distribution website? | 1,921,559 | <p>Ok, I have a question from a <strong>"client"</strong> perspective. Let's say we are talking about website designed for distribution: products + their logistics info. </p>
<p>Definitely less than a 2k rows, rarely changed but often accessed. Typical row with several columns will have to consist of a picture so it m... | 0 | 2009-12-17T12:49:45Z | 1,922,486 | <p>"What i need to know is whether the porposed solution is suitable for such a small project and could not be easily replaced by less complicated languages/frameworks/dmbses like PHP with MySQL etc.
"</p>
<p>Yes. It's suitable.</p>
<p>No. Nothing is "less complicated" than Django. PHP language may appear less c... | 2 | 2009-12-17T15:25:37Z | [
"python",
"django",
"postgresql"
] |
XML edit attributes | 1,921,601 | <p>I want to edit the attributes of an element in an XML file.</p>
<p>The file looks like </p>
<pre><code><Parameter name="Spec 2 Circumference/Length" type="real" mode="both">
<Value>0.0</Value>
<Result>0.0</Result>
</Parameter>
</code></pre>
<p>I want to replace the va... | 1 | 2009-12-17T12:56:19Z | 1,921,849 | <p>An example using <a href="http://effbot.org/zone/element.htm" rel="nofollow">ElementTree</a>. It will replace the <code>Value</code> elements text with some string; the procedure for the <code>Result</code> element is analogue and omitted here:</p>
<pre><code>#!/usr/bin/env python
xml = """
<Parameter name="Spe... | 1 | 2009-12-17T13:37:58Z | [
"python",
"xml"
] |
How do I get names of subdirectories of a directory purely in python? | 1,921,623 | <p>I dont want to use commands or any similar module that uses unix shell.
Thanks in advance..</p>
| 1 | 2009-12-17T12:59:41Z | 1,921,666 | <p>Use <code>os.walk()</code>:</p>
<pre><code>import os, os.path
def walk_directories(src):
for root, dirs, files in os.walk(src):
for dir in dirs:
print os.path.join(root, dir)
walk_directories(r'c:\temp')
</code></pre>
| 2 | 2009-12-17T13:05:41Z | [
"python",
"subdirectories"
] |
How do I get names of subdirectories of a directory purely in python? | 1,921,623 | <p>I dont want to use commands or any similar module that uses unix shell.
Thanks in advance..</p>
| 1 | 2009-12-17T12:59:41Z | 1,921,739 | <p>If you want to do this recursively, going down a tree visiting all the directories, then you can use os.walk like this:</p>
<pre><code> for root, directories, files in os.walk("c:\\"):
doSomething
</code></pre>
<p>If you only want the subdirectories you can either call walk once:</p>
<pre><code> directories... | 0 | 2009-12-17T13:19:36Z | [
"python",
"subdirectories"
] |
Python - Should one start a new project directly in Python 3.x? | 1,921,742 | <p>What Python version can you please recommend for a long-term (years) project? Should one use 2.6+ or 3.x is already stable? (only standard libraries are required)</p>
<p>UPDATE: according to the answers below, Python 3.x still has critical bugs. Please also see <a href="http://bugs.python.org/" rel="nofollow">Pytho... | 5 | 2009-12-17T13:20:06Z | 1,921,758 | <p>If you are starting your project now, and all the third party libraries you want to use are already ported to Python 3, and your target operating system (I really mean linux distribution) packages Python 3, then sure.</p>
<p>Go ahead, use Python 3.</p>
| 0 | 2009-12-17T13:23:36Z | [
"python"
] |
Python - Should one start a new project directly in Python 3.x? | 1,921,742 | <p>What Python version can you please recommend for a long-term (years) project? Should one use 2.6+ or 3.x is already stable? (only standard libraries are required)</p>
<p>UPDATE: according to the answers below, Python 3.x still has critical bugs. Please also see <a href="http://bugs.python.org/" rel="nofollow">Pytho... | 5 | 2009-12-17T13:20:06Z | 1,921,760 | <p>There's nothing wrong with Python 3, but many libraries haven't been ported yet. For example, from <a href="http://www.pygame.org/wiki/FrequentlyAskedQuestions" rel="nofollow">PyGame FAQ</a>:</p>
<blockquote>
<p>Python 3 support is incomplete and still in the development stage</p>
</blockquote>
<p>And from <a hr... | 4 | 2009-12-17T13:23:49Z | [
"python"
] |
Python - Should one start a new project directly in Python 3.x? | 1,921,742 | <p>What Python version can you please recommend for a long-term (years) project? Should one use 2.6+ or 3.x is already stable? (only standard libraries are required)</p>
<p>UPDATE: according to the answers below, Python 3.x still has critical bugs. Please also see <a href="http://bugs.python.org/" rel="nofollow">Pytho... | 5 | 2009-12-17T13:20:06Z | 1,921,764 | <p>I think it boils down to how closely you can control the environment in which your application will run. If you are producing a piece of software in which your customer will 'install' python to run your software, then you might as well use the latest release (Python 3.x). If you are targeting existing python install... | 2 | 2009-12-17T13:24:55Z | [
"python"
] |
Python - Should one start a new project directly in Python 3.x? | 1,921,742 | <p>What Python version can you please recommend for a long-term (years) project? Should one use 2.6+ or 3.x is already stable? (only standard libraries are required)</p>
<p>UPDATE: according to the answers below, Python 3.x still has critical bugs. Please also see <a href="http://bugs.python.org/" rel="nofollow">Pytho... | 5 | 2009-12-17T13:20:06Z | 1,921,897 | <p>The good thing about Python3 is that <a href="http://docs.python.org/library/2to3.html" rel="nofollow">there is a automatic way to port your programs</a> from Python2. But the program has to meet certain criteria. </p>
<p>I think the best approach for now is to write Python2 code and test it in Python3 test mode (r... | 4 | 2009-12-17T13:45:40Z | [
"python"
] |
Python - Should one start a new project directly in Python 3.x? | 1,921,742 | <p>What Python version can you please recommend for a long-term (years) project? Should one use 2.6+ or 3.x is already stable? (only standard libraries are required)</p>
<p>UPDATE: according to the answers below, Python 3.x still has critical bugs. Please also see <a href="http://bugs.python.org/" rel="nofollow">Pytho... | 5 | 2009-12-17T13:20:06Z | 1,924,353 | <p><a href="http://stackoverflow.com/questions/1921742/python-should-one-start-a-new-project-directly-in-python-3-x/1921897#1921897">THC4k mentions 2to3</a>, and I just wanted to point out that there's also a <a href="http://pypi.python.org/pypi/3to2" rel="nofollow">3to2 package</a>. I've never used either package, b... | 1 | 2009-12-17T20:30:08Z | [
"python"
] |
Python - Should one start a new project directly in Python 3.x? | 1,921,742 | <p>What Python version can you please recommend for a long-term (years) project? Should one use 2.6+ or 3.x is already stable? (only standard libraries are required)</p>
<p>UPDATE: according to the answers below, Python 3.x still has critical bugs. Please also see <a href="http://bugs.python.org/" rel="nofollow">Pytho... | 5 | 2009-12-17T13:20:06Z | 17,330,887 | <p><strong>This is why you should use Python 3.x:</strong></p>
<p>Python 2.x:</p>
<pre><code>>>>True = False
>>>True
False
</code></pre>
<p>Python 3.x:</p>
<pre><code>>>> True = False
File "<stdin>", line 1
SyntaxError: assignment to keyword
</code></pre>
<p>Source: <a href="http... | 8 | 2013-06-26T21:36:18Z | [
"python"
] |
Grep and Python | 1,921,894 | <p>I need a way of searching a file using grep via a regular expression 'RE' from the unix command line. For example when i type in the command line:</p>
<blockquote>
<p>python pythonfile.py 'RE'
'file-to-be-searched'</p>
</blockquote>
<p>I need the regualr expression 'RE' to be searched in the file and print out... | 33 | 2009-12-17T13:45:00Z | 1,921,908 | <ol>
<li>use <code>sys.argv</code> to get the command-line parameters</li>
<li>use <code>open()</code>, <code>read()</code> to manipulate file</li>
<li>use the <a href="http://docs.python.org/library/re.html" rel="nofollow">Python re module</a> to match lines</li>
</ol>
| 2 | 2009-12-17T13:47:35Z | [
"python",
"grep"
] |
Grep and Python | 1,921,894 | <p>I need a way of searching a file using grep via a regular expression 'RE' from the unix command line. For example when i type in the command line:</p>
<blockquote>
<p>python pythonfile.py 'RE'
'file-to-be-searched'</p>
</blockquote>
<p>I need the regualr expression 'RE' to be searched in the file and print out... | 33 | 2009-12-17T13:45:00Z | 1,921,932 | <p>The natural question is why not just use grep?! But assuming you can't ...</p>
<pre><code> import re
import sys
file = open(sys.argv[2], "r")
for line in file:
if re.search(sys.argv[1], line):
print line,
</code></pre>
<p>Things to note:</p>
<ul>
<li><code>search</code> instead of <code... | 46 | 2009-12-17T13:51:26Z | [
"python",
"grep"
] |
Grep and Python | 1,921,894 | <p>I need a way of searching a file using grep via a regular expression 'RE' from the unix command line. For example when i type in the command line:</p>
<blockquote>
<p>python pythonfile.py 'RE'
'file-to-be-searched'</p>
</blockquote>
<p>I need the regualr expression 'RE' to be searched in the file and print out... | 33 | 2009-12-17T13:45:00Z | 1,921,934 | <p>Adapted from a <a href="http://casa.colorado.edu/~ginsbura/pygrep.htm" rel="nofollow">grep in python</a>.</p>
<p>Accepts a list of filenames via <code>[2:]</code>, does no exception handling:</p>
<pre><code>#!/usr/bin/env python
import re, sys, os
for f in filter(os.path.isfile, sys.argv[2:]):
for line in ope... | 5 | 2009-12-17T13:51:43Z | [
"python",
"grep"
] |
Grep and Python | 1,921,894 | <p>I need a way of searching a file using grep via a regular expression 'RE' from the unix command line. For example when i type in the command line:</p>
<blockquote>
<p>python pythonfile.py 'RE'
'file-to-be-searched'</p>
</blockquote>
<p>I need the regualr expression 'RE' to be searched in the file and print out... | 33 | 2009-12-17T13:45:00Z | 17,040,831 | <p>You might be interested in <a href="http://pyvideo.org/video/686/the-pyed-piper-a-modern-python-alternative-to-aw" rel="nofollow">pyp</a>. Citing my other <a href="http://stackoverflow.com/a/12915952/95735">answer</a>: </p>
<blockquote>
<p>"The Pyed Piper", or pyp, is a linux command line text manipulation
too... | 2 | 2013-06-11T09:32:41Z | [
"python",
"grep"
] |
Grep and Python | 1,921,894 | <p>I need a way of searching a file using grep via a regular expression 'RE' from the unix command line. For example when i type in the command line:</p>
<blockquote>
<p>python pythonfile.py 'RE'
'file-to-be-searched'</p>
</blockquote>
<p>I need the regualr expression 'RE' to be searched in the file and print out... | 33 | 2009-12-17T13:45:00Z | 25,181,706 | <p>This would use more memory but might be faster in some cases:</p>
<pre><code>#!/usr/bin/env python
# file: grep.py
import re, sys
def grep(s,pattern):
return '\n'.join(re.findall(r'^.*%s.*?$'%pattern,s,flags=re.M))
if __name__ == '__main__':
print (grep(sys.stdin.read(),sys.argv[1]))
</code></pre>
<p>It ... | 4 | 2014-08-07T11:46:37Z | [
"python",
"grep"
] |
Parsing HTML in python - lxml or BeautifulSoup? Which of these is better for what kinds of purposes? | 1,922,032 | <p>From what I can make out, the two main HTML parsing libraries in Python are lxml and BeautifulSoup. I've chosen BeautifulSoup for a project I'm working on, but I chose it for no particular reason other than finding the syntax a bit easier to learn and understand. But I see a lot of people seem to favour lxml and I'v... | 44 | 2009-12-17T14:08:59Z | 1,922,064 | <p><s>For starters, BeautifulSoup is no longer actively maintained, and <a href="http://www.crummy.com/software/BeautifulSoup/3.1-problems.html">the author even recommends alternatives</a> such as lxml.</s></p>
<p>Quoting from the linked page:</p>
<blockquote>
<p>Version 3.1.0 of Beautiful Soup does
significantly... | 23 | 2009-12-17T14:13:22Z | [
"python",
"beautifulsoup",
"html-parsing",
"lxml"
] |
Parsing HTML in python - lxml or BeautifulSoup? Which of these is better for what kinds of purposes? | 1,922,032 | <p>From what I can make out, the two main HTML parsing libraries in Python are lxml and BeautifulSoup. I've chosen BeautifulSoup for a project I'm working on, but I chose it for no particular reason other than finding the syntax a bit easier to learn and understand. But I see a lot of people seem to favour lxml and I'v... | 44 | 2009-12-17T14:08:59Z | 1,922,096 | <p>I've used lxml with great success for parsing HTML. It seems to do a good job of handling "soupy" HTML, too. I'd highly recommend it.</p>
<p>Here's a quick test I had lying around to try handling of some ugly HTML:</p>
<pre><code>import unittest
from StringIO import StringIO
from lxml import etree
class TestLxm... | 5 | 2009-12-17T14:19:18Z | [
"python",
"beautifulsoup",
"html-parsing",
"lxml"
] |
Parsing HTML in python - lxml or BeautifulSoup? Which of these is better for what kinds of purposes? | 1,922,032 | <p>From what I can make out, the two main HTML parsing libraries in Python are lxml and BeautifulSoup. I've chosen BeautifulSoup for a project I'm working on, but I chose it for no particular reason other than finding the syntax a bit easier to learn and understand. But I see a lot of people seem to favour lxml and I'v... | 44 | 2009-12-17T14:08:59Z | 1,922,130 | <p>Don't use BeautifulSoup, use
<a href="http://lxml.de/elementsoup.html" rel="nofollow">lxml.soupparser</a> then you're sitting on top of the power of lxml and can use the good bits of BeautifulSoup which is to deal with really broken and crappy HTML. </p>
| 10 | 2009-12-17T14:24:12Z | [
"python",
"beautifulsoup",
"html-parsing",
"lxml"
] |
Parsing HTML in python - lxml or BeautifulSoup? Which of these is better for what kinds of purposes? | 1,922,032 | <p>From what I can make out, the two main HTML parsing libraries in Python are lxml and BeautifulSoup. I've chosen BeautifulSoup for a project I'm working on, but I chose it for no particular reason other than finding the syntax a bit easier to learn and understand. But I see a lot of people seem to favour lxml and I'v... | 44 | 2009-12-17T14:08:59Z | 1,923,767 | <p><code>Pyquery</code> provides the jQuery selector interface to Python (using lxml under the hood).</p>
<p><a href="http://pypi.python.org/pypi/pyquery" rel="nofollow">http://pypi.python.org/pypi/pyquery</a></p>
<p>It's really awesome, I don't use anything else anymore.</p>
| 25 | 2009-12-17T18:48:09Z | [
"python",
"beautifulsoup",
"html-parsing",
"lxml"
] |
Parsing HTML in python - lxml or BeautifulSoup? Which of these is better for what kinds of purposes? | 1,922,032 | <p>From what I can make out, the two main HTML parsing libraries in Python are lxml and BeautifulSoup. I've chosen BeautifulSoup for a project I'm working on, but I chose it for no particular reason other than finding the syntax a bit easier to learn and understand. But I see a lot of people seem to favour lxml and I'v... | 44 | 2009-12-17T14:08:59Z | 13,778,004 | <p>A somewhat outdated speed comparison can be found <a href="http://blog.ianbicking.org/2008/03/30/python-html-parser-performance/" rel="nofollow">here</a>, which clearly recommends lxml, as the speed differences seem drastic.</p>
| 0 | 2012-12-08T13:42:45Z | [
"python",
"beautifulsoup",
"html-parsing",
"lxml"
] |
Parsing HTML in python - lxml or BeautifulSoup? Which of these is better for what kinds of purposes? | 1,922,032 | <p>From what I can make out, the two main HTML parsing libraries in Python are lxml and BeautifulSoup. I've chosen BeautifulSoup for a project I'm working on, but I chose it for no particular reason other than finding the syntax a bit easier to learn and understand. But I see a lot of people seem to favour lxml and I'v... | 44 | 2009-12-17T14:08:59Z | 19,549,530 | <p>In summary, <code>lxml</code> is positioned as a lightning-fast production-quality html and xml parser that, by the way, also includes a <code>soupparser</code> module to fall back on BeautifulSoup's functionality. <code>BeautifulSoup</code> is a one-person project, designed to save you time to quickly extract data ... | 9 | 2013-10-23T18:25:30Z | [
"python",
"beautifulsoup",
"html-parsing",
"lxml"
] |
Parsing HTML in python - lxml or BeautifulSoup? Which of these is better for what kinds of purposes? | 1,922,032 | <p>From what I can make out, the two main HTML parsing libraries in Python are lxml and BeautifulSoup. I've chosen BeautifulSoup for a project I'm working on, but I chose it for no particular reason other than finding the syntax a bit easier to learn and understand. But I see a lot of people seem to favour lxml and I'v... | 44 | 2009-12-17T14:08:59Z | 36,112,383 | <p>For sure i would use EHP. It is faster than lxml, much more elegant and simpler to use.</p>
<p>Check out. <a href="https://github.com/iogf/ehp" rel="nofollow">https://github.com/iogf/ehp</a></p>
<pre><code><body ><em > foo <font color="red" ></font></em></body>
from ehp impor... | 0 | 2016-03-20T10:03:18Z | [
"python",
"beautifulsoup",
"html-parsing",
"lxml"
] |
Indexing over the results returned by selenium | 1,922,211 | <p>I try to index over results returned by an xpath. For example:</p>
<pre><code>xpath = '//a[@id="someID"]'
</code></pre>
<p>can return a few results. I want to get a list of them. I thought that doing:</p>
<pre><code>numOfResults = sel.get_xpath_count(xpath)
l = []
for i in range(1,numOfResults+1):
l.append(sel... | 3 | 2009-12-17T14:40:06Z | 1,922,660 | <p>In Selenium you normally do it without the extra brackets so your loop would look like the following</p>
<pre><code>numOfResults = sel.get_xpath_count(xpath)
l = []
for i in range(1,numOfResults+1):
l.append(sel.get_text('%s[%d]'%(xpath, i)))
</code></pre>
<p>And that will produce a valid XPath in Selenium like... | 0 | 2009-12-17T15:47:48Z | [
"python",
"xpath",
"selenium"
] |
Indexing over the results returned by selenium | 1,922,211 | <p>I try to index over results returned by an xpath. For example:</p>
<pre><code>xpath = '//a[@id="someID"]'
</code></pre>
<p>can return a few results. I want to get a list of them. I thought that doing:</p>
<pre><code>numOfResults = sel.get_xpath_count(xpath)
l = []
for i in range(1,numOfResults+1):
l.append(sel... | 3 | 2009-12-17T14:40:06Z | 1,927,491 | <p>Can you try the xpath <code>/html/descendant::a[@id="someID"]</code> You can replace the <code>/html</code> with something else that is an ancestor of your links like <code>id('content')</code>. You should then be able to locate individual links using <code>[1]</code>, <code>[2]</code> etc.</p>
<p>From the XPath TR... | 2 | 2009-12-18T10:49:17Z | [
"python",
"xpath",
"selenium"
] |
Indexing over the results returned by selenium | 1,922,211 | <p>I try to index over results returned by an xpath. For example:</p>
<pre><code>xpath = '//a[@id="someID"]'
</code></pre>
<p>can return a few results. I want to get a list of them. I thought that doing:</p>
<pre><code>numOfResults = sel.get_xpath_count(xpath)
l = []
for i in range(1,numOfResults+1):
l.append(sel... | 3 | 2009-12-17T14:40:06Z | 2,661,900 | <p>The answer is that you need to tell selenium that you're using xpath:</p>
<pre><code>numOfResults = sel.get_xpath_count(xpath)
l = []
for i in range(1,numOfResults+1):
l.append(sel.get_text(xpath='(%s)[%d]'%(xpath, i)))
</code></pre>
| 1 | 2010-04-18T10:28:26Z | [
"python",
"xpath",
"selenium"
] |
Python: when to use pty.fork() versus os.fork() | 1,922,254 | <p>I'm uncertain whether to use <code>pty.fork()</code> or <code>os.fork()</code> when spawning external background processes from my app. (Such as chess engines)</p>
<p>I want the spawned processes to die if the parent is killed, as with spawning apps in a terminal.</p>
<p>What are the ups and downs between the two ... | 13 | 2009-12-17T14:47:41Z | 1,922,469 | <p>In the past I've always used the <code>subprocess</code> module for this. It provides a good api for communicating with subprocesses.</p>
<p>You can use <code>call(*popenargs, **kwargs)</code> for blocking execution of them, and I believe using the <code>Popen</code> class can handle async execution.</p>
<p>Check... | 3 | 2009-12-17T15:22:40Z | [
"python",
"linux",
"fork",
"kill",
"pty"
] |
Python: when to use pty.fork() versus os.fork() | 1,922,254 | <p>I'm uncertain whether to use <code>pty.fork()</code> or <code>os.fork()</code> when spawning external background processes from my app. (Such as chess engines)</p>
<p>I want the spawned processes to die if the parent is killed, as with spawning apps in a terminal.</p>
<p>What are the ups and downs between the two ... | 13 | 2009-12-17T14:47:41Z | 1,923,358 | <p>The child process created with <code>os.fork()</code> inherits stdin/stdout/stderr from parent process, while the child created with <code>pty.fork()</code> is connected to new pseudo terminal. You need the later when you write a program like xterm: <code>pty.fork()</code> in parent process returns a descriptor to ... | 10 | 2009-12-17T17:36:24Z | [
"python",
"linux",
"fork",
"kill",
"pty"
] |
Python: when to use pty.fork() versus os.fork() | 1,922,254 | <p>I'm uncertain whether to use <code>pty.fork()</code> or <code>os.fork()</code> when spawning external background processes from my app. (Such as chess engines)</p>
<p>I want the spawned processes to die if the parent is killed, as with spawning apps in a terminal.</p>
<p>What are the ups and downs between the two ... | 13 | 2009-12-17T14:47:41Z | 11,804,899 | <p>Pseudotermials are necessary for some applications that really expect a terminal. An interactive shell is one of these examples but there are many other. The pty.fork option is not there as another os.fork but as a specific API to use a pseudoterminal.</p>
| 1 | 2012-08-04T00:25:45Z | [
"python",
"linux",
"fork",
"kill",
"pty"
] |
nested regular expressions in python | 1,922,261 | <p>In perl I can do this:</p>
<pre><code>$number = qr/ zero | one | two | three | four | five | six | seven | eight | nine /ix;
$foo = qr/ quantity: \s* $number /ix;
</code></pre>
<p>My actual regular expression is many lines and does two-digit and ordinal numbers (e.g., "twenty-two", "forty-fourth" and "twelve are a... | 2 | 2009-12-17T14:49:16Z | 1,922,324 | <p>In python, you build regular expressions by passing a string to re.compile.
You can "nest" regular expression by just doing regular string manipulation:</p>
<pre><code>#!/usr/bin/env python
import re
number = 'zero | one | two | three | four | five | six | seven | eight | nine'
foo = re.compile(' quantity: \s* (%s... | 6 | 2009-12-17T15:03:04Z | [
"python",
"nested",
"expression"
] |
nested regular expressions in python | 1,922,261 | <p>In perl I can do this:</p>
<pre><code>$number = qr/ zero | one | two | three | four | five | six | seven | eight | nine /ix;
$foo = qr/ quantity: \s* $number /ix;
</code></pre>
<p>My actual regular expression is many lines and does two-digit and ordinal numbers (e.g., "twenty-two", "forty-fourth" and "twelve are a... | 2 | 2009-12-17T14:49:16Z | 1,922,342 | <p>This is probably not quite the same. But you could do this:</p>
<pre><code>import re
number = "(?:zero | one | two | three | four | five | six | seven | eight | nine)"
foo = "quantity: \s* " + number
bar = re.compile(foo, re.I | re.X)
</code></pre>
| 1 | 2009-12-17T15:06:04Z | [
"python",
"nested",
"expression"
] |
Testing subpackage modules in Python 3 | 1,922,291 | <p>I have been experimenting with various uses of hierarchies like this and the differences between absolute and relative imports, and can't figure out how to do routine things with the package, subpackages, and modules without simply putting everything on sys.path. I have a two-level package hierarchy:</p>
<pre>
... | 1 | 2009-12-17T14:57:46Z | 1,977,497 | <p>I don't see what this has to do with relative imports. Relative imports allow exactly the same things as absolute imports, only using a different notation.</p>
| -1 | 2009-12-29T23:09:35Z | [
"python",
"unit-testing",
"import",
"python-3.x",
"relative"
] |
Testing subpackage modules in Python 3 | 1,922,291 | <p>I have been experimenting with various uses of hierarchies like this and the differences between absolute and relative imports, and can't figure out how to do routine things with the package, subpackages, and modules without simply putting everything on sys.path. I have a two-level package hierarchy:</p>
<pre>
... | 1 | 2009-12-17T14:57:46Z | 9,245,003 | <p>I'm struggling with the same sort of problems, and the only feasible way I can think of is to give up or alter your second point.
The reason - as far as I understand it is the following: You will want to only use relative imports in the subpackages and submodules. This means that the top-level package will have to b... | 0 | 2012-02-11T23:31:02Z | [
"python",
"unit-testing",
"import",
"python-3.x",
"relative"
] |
Good Python Libraries for iPod Synchronization | 1,922,371 | <p>I am making a music application, and want to be able to synchronize with the iPod from my application. Has anyone had any experience in doing this with python, and if so what would you recommend?</p>
| 5 | 2009-12-17T15:09:29Z | 1,930,317 | <p>I haven't used it, but <a href="http://www.gtkpod.org/libgpod/" rel="nofollow">libgpod</a> does appear to have python bindings.</p>
| 2 | 2009-12-18T19:47:27Z | [
"python",
"ipod"
] |
Good Python Libraries for iPod Synchronization | 1,922,371 | <p>I am making a music application, and want to be able to synchronize with the iPod from my application. Has anyone had any experience in doing this with python, and if so what would you recommend?</p>
| 5 | 2009-12-17T15:09:29Z | 14,781,334 | <p>i made this program to you.
it will Work with more devices</p>
<p><a href="https://sites.google.com/site/yousaywemakeit/programs/musicparser" rel="nofollow">https://sites.google.com/site/yousaywemakeit/programs/musicparser</a></p>
| 0 | 2013-02-08T21:05:27Z | [
"python",
"ipod"
] |
Parsing a datafile in python (2.5.2) | 1,922,426 | <p>I have a message definition file that looks like this</p>
<pre><code>struct1
{
field="name" type="string" ignore="false";
field="id" type="int" enums=" 0="val1" 1="val2" ";
}
struct2
{
field = "object" type="struct1";
...
}
</code></pre>
<p>How can I parse this into a dictionary with keys 'struct1, stru... | 1 | 2009-12-17T15:17:10Z | 1,922,452 | <p>Use can use <a href="http://www.json.org/" rel="nofollow">json</a> as file format, it supports (in python lingo) dictionaries and lists. Since json support is native only for python 2.6 and higher, you'll need this library: <a href="http://pypi.python.org/pypi/simplejson/2.0.9" rel="nofollow">http://pypi.python.org/... | 4 | 2009-12-17T15:20:47Z | [
"python",
"parsing",
"dictionary"
] |
Parsing a datafile in python (2.5.2) | 1,922,426 | <p>I have a message definition file that looks like this</p>
<pre><code>struct1
{
field="name" type="string" ignore="false";
field="id" type="int" enums=" 0="val1" 1="val2" ";
}
struct2
{
field = "object" type="struct1";
...
}
</code></pre>
<p>How can I parse this into a dictionary with keys 'struct1, stru... | 1 | 2009-12-17T15:17:10Z | 1,922,454 | <p>Use <a href="http://www.yaml.org/" rel="nofollow">YAML</a> instead. There is <a href="http://pyyaml.org/" rel="nofollow">PyYAML</a> library for python. It is heavily used by Google AppEngine. </p>
<p>This is just a friendly suggestion :-)</p>
<p>Example ( Mapping Scalars to Sequences ):</p>
<pre><code>american:
... | 4 | 2009-12-17T15:20:53Z | [
"python",
"parsing",
"dictionary"
] |
Parsing a datafile in python (2.5.2) | 1,922,426 | <p>I have a message definition file that looks like this</p>
<pre><code>struct1
{
field="name" type="string" ignore="false";
field="id" type="int" enums=" 0="val1" 1="val2" ";
}
struct2
{
field = "object" type="struct1";
...
}
</code></pre>
<p>How can I parse this into a dictionary with keys 'struct1, stru... | 1 | 2009-12-17T15:17:10Z | 1,922,458 | <p>Might I recommend <a href="http://www.yaml.org/" rel="nofollow">YAML</a>? IMHO the syntax is more readable for data entry, and then you don't have to write and maintain a parser. Eschew XML -- it is good for marking up text, but not good for data entry since the text isn't human readable with all the duplicate tag... | 4 | 2009-12-17T15:21:13Z | [
"python",
"parsing",
"dictionary"
] |
Parsing a datafile in python (2.5.2) | 1,922,426 | <p>I have a message definition file that looks like this</p>
<pre><code>struct1
{
field="name" type="string" ignore="false";
field="id" type="int" enums=" 0="val1" 1="val2" ";
}
struct2
{
field = "object" type="struct1";
...
}
</code></pre>
<p>How can I parse this into a dictionary with keys 'struct1, stru... | 1 | 2009-12-17T15:17:10Z | 1,922,463 | <p>Since you are at liberty to change the file format, you could change it to any of several formats that have Python libraries to read and write. For example, JSON, YAML, XML, or even the built-in <a href="http://docs.python.org/library/configparser.html" rel="nofollow">ConfigParser</a>.</p>
<pre><code>[struct1]
fiel... | 0 | 2009-12-17T15:21:55Z | [
"python",
"parsing",
"dictionary"
] |
Parsing a datafile in python (2.5.2) | 1,922,426 | <p>I have a message definition file that looks like this</p>
<pre><code>struct1
{
field="name" type="string" ignore="false";
field="id" type="int" enums=" 0="val1" 1="val2" ";
}
struct2
{
field = "object" type="struct1";
...
}
</code></pre>
<p>How can I parse this into a dictionary with keys 'struct1, stru... | 1 | 2009-12-17T15:17:10Z | 1,922,500 | <p>Pyparsing is a nice easy to use library. That what I would use. </p>
<p><a href="http://pyparsing.wikispaces.com/" rel="nofollow">http://pyparsing.wikispaces.com/</a></p>
| 1 | 2009-12-17T15:27:19Z | [
"python",
"parsing",
"dictionary"
] |
Parsing a datafile in python (2.5.2) | 1,922,426 | <p>I have a message definition file that looks like this</p>
<pre><code>struct1
{
field="name" type="string" ignore="false";
field="id" type="int" enums=" 0="val1" 1="val2" ";
}
struct2
{
field = "object" type="struct1";
...
}
</code></pre>
<p>How can I parse this into a dictionary with keys 'struct1, stru... | 1 | 2009-12-17T15:17:10Z | 1,922,505 | <p>I would simply use Python for the message definition file format.</p>
<p>Let your message definition file be a plain Python file:</p>
<pre><code># file messages.py
messages = dict(
struct1=[
dict(field="name", type="string", ignore=False),
dict(field="id", type="int", enums={0: "val1", 1: "val2... | 2 | 2009-12-17T15:27:42Z | [
"python",
"parsing",
"dictionary"
] |
Huge amount of time sending data with suds and proxy | 1,922,538 | <p>I have the following code to send data through a proxy using suds:</p>
<pre><code> import suds
t = suds.transport.http.HttpTransport()
proxy = urllib2.ProxyHandler({'http': 'http://192.168.3.217:3128'})
opener = urllib2.build_opener(proxy)
t.urlopener = opener
ws = suds.client.Client('http://xxxxxxx/web.asmx?W... | 2 | 2009-12-17T15:31:24Z | 2,837,907 | <p>A sniffer output (e.g. from wireshark) could be very helpful to understand this one.</p>
| 0 | 2010-05-14T22:00:27Z | [
"python",
"proxy",
"suds"
] |
MYSQLDB python module | 1,922,623 | <p>I am using MySQLdb module of python on FC11 machine. Here, i have an issue. I have the following implementation for one of our requirement:</p>
<ol>
<li>connect to mysqldb and get DB handle,open a cursor, execute a delete statement,commit and then close the cursor.</li>
<li>Again using the DB handle above, iam perf... | 0 | 2009-12-17T15:42:43Z | 1,922,710 | <p>With no code, I can only make a guess: try not closing the cursor until you are done with that connection. I think that calling cursor() again after calling cursor.close() will just give you a reference to the same cursor, which can no longer be used for queries.</p>
<p>I am not 100% sure if that is the intended ... | 0 | 2009-12-17T15:53:08Z | [
"python",
"mysql"
] |
MYSQLDB python module | 1,922,623 | <p>I am using MySQLdb module of python on FC11 machine. Here, i have an issue. I have the following implementation for one of our requirement:</p>
<ol>
<li>connect to mysqldb and get DB handle,open a cursor, execute a delete statement,commit and then close the cursor.</li>
<li>Again using the DB handle above, iam perf... | 0 | 2009-12-17T15:42:43Z | 1,924,766 | <p>It sounds as though the first cursor is being returned back to the second step.</p>
| 0 | 2009-12-17T21:37:50Z | [
"python",
"mysql"
] |
MYSQLDB python module | 1,922,623 | <p>I am using MySQLdb module of python on FC11 machine. Here, i have an issue. I have the following implementation for one of our requirement:</p>
<ol>
<li>connect to mysqldb and get DB handle,open a cursor, execute a delete statement,commit and then close the cursor.</li>
<li>Again using the DB handle above, iam perf... | 0 | 2009-12-17T15:42:43Z | 1,945,554 | <p>did u try </p>
<pre><code>records = cur.fetchall()
</code></pre>
<p>?</p>
| 0 | 2009-12-22T10:43:17Z | [
"python",
"mysql"
] |
How to create an Incremental loading webpage | 1,922,673 | <p>I'm writing a page dealing with a large amount of data. It would last forever until my resultant page loaded (nearly infinite) because the data returned is so large. Therefore, I need to implement an incrementally loading page like one at thie url: </p>
<p><a href="http://docs.python.org/" rel="nofollow">http://doc... | 4 | 2009-12-17T15:49:06Z | 1,922,700 | <p>I think you'll have to use ajax for that kind of effect.</p>
<p>Have a look at this in the <a href="http://docs.python.org/%5Fstatic/searchtools.js" rel="nofollow">Python JavaScript</a>:</p>
<pre><code>performSearch : function(query) {
// create the required interface elements
this.out = $('#search-results');
this... | 1 | 2009-12-17T15:52:09Z | [
"jquery",
"python"
] |
How to create an Incremental loading webpage | 1,922,673 | <p>I'm writing a page dealing with a large amount of data. It would last forever until my resultant page loaded (nearly infinite) because the data returned is so large. Therefore, I need to implement an incrementally loading page like one at thie url: </p>
<p><a href="http://docs.python.org/" rel="nofollow">http://doc... | 4 | 2009-12-17T15:49:06Z | 1,922,709 | <p>Sounds like it's AJAX time. When a person submits a query, send an AJAX request to the server to get some results. Display some "loading" message until you have a response. Set a Javascript timer in the page such that it refreshes the results output every few seconds until perhaps some flag is set to where it kno... | 1 | 2009-12-17T15:53:02Z | [
"jquery",
"python"
] |
How to create an Incremental loading webpage | 1,922,673 | <p>I'm writing a page dealing with a large amount of data. It would last forever until my resultant page loaded (nearly infinite) because the data returned is so large. Therefore, I need to implement an incrementally loading page like one at thie url: </p>
<p><a href="http://docs.python.org/" rel="nofollow">http://doc... | 4 | 2009-12-17T15:49:06Z | 1,922,719 | <p>You don't specify a language or an idea of what kind of data you are displaying or how it's accessed, so this leaves a lot of questions to be answered. Do you want to fetch more data when they scroll to the bottom of the page? Is this just a really slow search?</p>
<p>Either way, there are basically two ways to go ... | 1 | 2009-12-17T15:54:23Z | [
"jquery",
"python"
] |
How to create an Incremental loading webpage | 1,922,673 | <p>I'm writing a page dealing with a large amount of data. It would last forever until my resultant page loaded (nearly infinite) because the data returned is so large. Therefore, I need to implement an incrementally loading page like one at thie url: </p>
<p><a href="http://docs.python.org/" rel="nofollow">http://doc... | 4 | 2009-12-17T15:49:06Z | 1,922,726 | <p>The way to do this is using Javascript to retreive the data after the page has loaded, this is commonly known as "AJAX".</p>
<p>If you let us know what platform you are using someone will be able to give you more specific information - there are a lot of toolkits out there that help you out here, you don't have to ... | 1 | 2009-12-17T15:55:02Z | [
"jquery",
"python"
] |
How to create an Incremental loading webpage | 1,922,673 | <p>I'm writing a page dealing with a large amount of data. It would last forever until my resultant page loaded (nearly infinite) because the data returned is so large. Therefore, I need to implement an incrementally loading page like one at thie url: </p>
<p><a href="http://docs.python.org/" rel="nofollow">http://doc... | 4 | 2009-12-17T15:49:06Z | 1,922,728 | <p>There are various possible ways to do so, but the basic trick is to have the search program return results to the wire before finishing. This is something usually done by explicitly calling a flush() call or equivalent every so many results.</p>
<p>Now, to present them you can either</p>
<ul>
<li><p>Use AJAX: Retu... | 3 | 2009-12-17T15:55:11Z | [
"jquery",
"python"
] |
How to create an Incremental loading webpage | 1,922,673 | <p>I'm writing a page dealing with a large amount of data. It would last forever until my resultant page loaded (nearly infinite) because the data returned is so large. Therefore, I need to implement an incrementally loading page like one at thie url: </p>
<p><a href="http://docs.python.org/" rel="nofollow">http://doc... | 4 | 2009-12-17T15:49:06Z | 1,922,837 | <p>I would suggest <a href="http://www.jquery.com" rel="nofollow">jQuery</a> if you're going to try using Javascript/AJAX. If you've never had any experience with JS, but have some time to read through their getting started guide, this is (IMO) a great place/time to get started. It doesn't take much work at all to get ... | 1 | 2009-12-17T16:12:48Z | [
"jquery",
"python"
] |
Python: obtain & manipulate (as integers) bit patterns of floats | 1,922,771 | <p>In Python V.2.5.4, I have a float, and I'd like to obtain and manipulate (as an integer) the bit pattern of that float.</p>
<p>For example, suppose I have</p>
<pre><code>x = 173.3125
</code></pre>
<p>In IEEE 754 format, x's bit pattern (in hexadecimal) is <code>432D5000</code> .</p>
<p>How can I obtain & man... | 7 | 2009-12-17T16:02:13Z | 1,922,791 | <p>I am not too well versed on this topic, but have you tried the <a href="http://docs.python.org/library/ctypes.html" rel="nofollow"><code>ctypes</code></a> module?</p>
| 0 | 2009-12-17T16:05:15Z | [
"python",
"floating-point",
"bit-manipulation"
] |
Python: obtain & manipulate (as integers) bit patterns of floats | 1,922,771 | <p>In Python V.2.5.4, I have a float, and I'd like to obtain and manipulate (as an integer) the bit pattern of that float.</p>
<p>For example, suppose I have</p>
<pre><code>x = 173.3125
</code></pre>
<p>In IEEE 754 format, x's bit pattern (in hexadecimal) is <code>432D5000</code> .</p>
<p>How can I obtain & man... | 7 | 2009-12-17T16:02:13Z | 1,922,829 | <p>The problem is that a Python float object might not be a IEEE 754, because it is an object (in fact they are, but internally they could hold whichever representation is more convenient)...</p>
<p>As leo said, you can do a type cast with ctypes, so you are enforcing a particular representation (in this case, single ... | 8 | 2009-12-17T16:11:20Z | [
"python",
"floating-point",
"bit-manipulation"
] |
Python: obtain & manipulate (as integers) bit patterns of floats | 1,922,771 | <p>In Python V.2.5.4, I have a float, and I'd like to obtain and manipulate (as an integer) the bit pattern of that float.</p>
<p>For example, suppose I have</p>
<pre><code>x = 173.3125
</code></pre>
<p>In IEEE 754 format, x's bit pattern (in hexadecimal) is <code>432D5000</code> .</p>
<p>How can I obtain & man... | 7 | 2009-12-17T16:02:13Z | 1,922,871 | <p>You can get the string you want (apparently implying a big-endian, 32-bit representation; Python internally uses the native endianity and 64-bits for floats) with the <code>struct</code> module:</p>
<pre><code>>>> import struct
>>> x = 173.125
>>> s = struct.pack('>f', x)
>>> ... | 11 | 2009-12-17T16:17:41Z | [
"python",
"floating-point",
"bit-manipulation"
] |
Python: obtain & manipulate (as integers) bit patterns of floats | 1,922,771 | <p>In Python V.2.5.4, I have a float, and I'd like to obtain and manipulate (as an integer) the bit pattern of that float.</p>
<p>For example, suppose I have</p>
<pre><code>x = 173.3125
</code></pre>
<p>In IEEE 754 format, x's bit pattern (in hexadecimal) is <code>432D5000</code> .</p>
<p>How can I obtain & man... | 7 | 2009-12-17T16:02:13Z | 1,922,890 | <p>Use <code>struct</code> or <code>xdrlib</code> module:</p>
<pre><code>>>> import struct
>>> x = 173.3125
>>> rep = struct.pack('>f', x)
>>> numeric = struct.unpack('>I', rep)[0]
>>> '%x' %numeric
'432d5000'
</code></pre>
<p>Now you can work with <code>numeric</cod... | 2 | 2009-12-17T16:20:42Z | [
"python",
"floating-point",
"bit-manipulation"
] |
Python: obtain & manipulate (as integers) bit patterns of floats | 1,922,771 | <p>In Python V.2.5.4, I have a float, and I'd like to obtain and manipulate (as an integer) the bit pattern of that float.</p>
<p>For example, suppose I have</p>
<pre><code>x = 173.3125
</code></pre>
<p>In IEEE 754 format, x's bit pattern (in hexadecimal) is <code>432D5000</code> .</p>
<p>How can I obtain & man... | 7 | 2009-12-17T16:02:13Z | 31,324,264 | <p>For reference, it is also possible to use numpy and view.</p>
<pre><code>import numpy
def fextract( f ):
bits = numpy.asarray( f, dtype=numpy.float64 ).view( numpy.int64 )
if not bits & 0x7fffffffffffffff: # f == +/-0
return 0, 0
sign = numpy.sign(bits)
exponent = ( (bits>>52) & 0x7ff ) -... | 1 | 2015-07-09T17:21:20Z | [
"python",
"floating-point",
"bit-manipulation"
] |
Boolean Query | 1,922,849 | <p>if:</p>
<pre><code>x = 0
b = x==0
</code></pre>
<p>and i print be it would print 'true'</p>
<p>but if i did:</p>
<blockquote>
<p>x = 0<br>
b = x ==3</p>
</blockquote>
<p>and i printed be it would be false.
In stead of it printing false how would i take the boolean value 'b' to print what text i wanted?</p>
... | -2 | 2009-12-17T16:14:21Z | 1,922,863 | <p>Something like this you mean?</p>
<pre><code>x = 0
if x != 3:
print "x does not equal 3"
</code></pre>
| 6 | 2009-12-17T16:16:31Z | [
"python",
"syntax",
"boolean"
] |
Boolean Query | 1,922,849 | <p>if:</p>
<pre><code>x = 0
b = x==0
</code></pre>
<p>and i print be it would print 'true'</p>
<p>but if i did:</p>
<blockquote>
<p>x = 0<br>
b = x ==3</p>
</blockquote>
<p>and i printed be it would be false.
In stead of it printing false how would i take the boolean value 'b' to print what text i wanted?</p>
... | -2 | 2009-12-17T16:14:21Z | 1,922,875 | <pre><code>>>> x = 0
>>> if not x == 3: print 'x does not equal 3'
x does not equal 3
</code></pre>
<p>lte me explain further:</p>
<pre><code>>>> list = [-1, 1, 2, 3]
>>> if not all(n > 0 for n in list): print 'a value is not greater than zero'
a value is not greater than zero
... | 2 | 2009-12-17T16:17:52Z | [
"python",
"syntax",
"boolean"
] |
Boolean Query | 1,922,849 | <p>if:</p>
<pre><code>x = 0
b = x==0
</code></pre>
<p>and i print be it would print 'true'</p>
<p>but if i did:</p>
<blockquote>
<p>x = 0<br>
b = x ==3</p>
</blockquote>
<p>and i printed be it would be false.
In stead of it printing false how would i take the boolean value 'b' to print what text i wanted?</p>
... | -2 | 2009-12-17T16:14:21Z | 1,922,903 | <p>An <code>if</code> statement as other answers suggest is a possibility (and you could add an <code>else</code> clause to print something specific in each case). More direct is an <code>if/else</code> <em>operator</em>:</p>
<pre><code>print('equality' if b else 'diversity')
</code></pre>
<p>You could also use index... | 4 | 2009-12-17T16:23:03Z | [
"python",
"syntax",
"boolean"
] |
Boolean Query | 1,922,849 | <p>if:</p>
<pre><code>x = 0
b = x==0
</code></pre>
<p>and i print be it would print 'true'</p>
<p>but if i did:</p>
<blockquote>
<p>x = 0<br>
b = x ==3</p>
</blockquote>
<p>and i printed be it would be false.
In stead of it printing false how would i take the boolean value 'b' to print what text i wanted?</p>
... | -2 | 2009-12-17T16:14:21Z | 1,922,913 | <pre><code>a = lambda b :("not true","true")[b == 3]
print a(3)
</code></pre>
<p>will do it for you if you want to put it in a lambda</p>
| 2 | 2009-12-17T16:25:20Z | [
"python",
"syntax",
"boolean"
] |
Boolean Query | 1,922,849 | <p>if:</p>
<pre><code>x = 0
b = x==0
</code></pre>
<p>and i print be it would print 'true'</p>
<p>but if i did:</p>
<blockquote>
<p>x = 0<br>
b = x ==3</p>
</blockquote>
<p>and i printed be it would be false.
In stead of it printing false how would i take the boolean value 'b' to print what text i wanted?</p>
... | -2 | 2009-12-17T16:14:21Z | 1,922,981 | <p>You will need to do the printing yourself, as everyone suggested here.</p>
<p>It's worthy to note that some languages (e.g. Scala, Ruby, Groovy) have language features that enable you to write:</p>
<pre><code>x should be(3)
</code></pre>
<p>And that will report:</p>
<pre><code>0 should be 3 but is not.
</code></... | 0 | 2009-12-17T16:35:42Z | [
"python",
"syntax",
"boolean"
] |
Boolean Query | 1,922,849 | <p>if:</p>
<pre><code>x = 0
b = x==0
</code></pre>
<p>and i print be it would print 'true'</p>
<p>but if i did:</p>
<blockquote>
<p>x = 0<br>
b = x ==3</p>
</blockquote>
<p>and i printed be it would be false.
In stead of it printing false how would i take the boolean value 'b' to print what text i wanted?</p>
... | -2 | 2009-12-17T16:14:21Z | 1,922,994 | <p>Remove the quotes around True:</p>
<pre><code>bool = all(n > 0 for n in list)
if bool != True:
print 'a value is not greater than zero'
</code></pre>
<p>or, you can also check for False:</p>
<pre><code>bool = all(n > 0 for n in list)
if bool == False:
print 'a value is not greater than zero'
</c... | 3 | 2009-12-17T16:37:27Z | [
"python",
"syntax",
"boolean"
] |
Boolean Query | 1,922,849 | <p>if:</p>
<pre><code>x = 0
b = x==0
</code></pre>
<p>and i print be it would print 'true'</p>
<p>but if i did:</p>
<blockquote>
<p>x = 0<br>
b = x ==3</p>
</blockquote>
<p>and i printed be it would be false.
In stead of it printing false how would i take the boolean value 'b' to print what text i wanted?</p>
... | -2 | 2009-12-17T16:14:21Z | 1,923,103 | <p>I think perhaps the following will help alleviate some of your confusion...</p>
<pre><code>>>> 0==0
True
>>> 'True'
'True'
>>> (0==0) == 'True'
False
>>> (0==0) == True
True
>>>
</code></pre>
| 6 | 2009-12-17T16:53:30Z | [
"python",
"syntax",
"boolean"
] |
Boolean Query | 1,922,849 | <p>if:</p>
<pre><code>x = 0
b = x==0
</code></pre>
<p>and i print be it would print 'true'</p>
<p>but if i did:</p>
<blockquote>
<p>x = 0<br>
b = x ==3</p>
</blockquote>
<p>and i printed be it would be false.
In stead of it printing false how would i take the boolean value 'b' to print what text i wanted?</p>
... | -2 | 2009-12-17T16:14:21Z | 1,923,976 | <pre><code>>>> 'True' is not True
True
</code></pre>
<p>'True' is a string</p>
<p>True is a boolean</p>
<p>They have nothing to do with each other, except coincidentally. The string value happens to have the same letters as the boolean literal. But that's just a coincidence.</p>
| 0 | 2009-12-17T19:22:24Z | [
"python",
"syntax",
"boolean"
] |
Convert string / character to integer in python | 1,923,054 | <p>I want to convert a single character of a string into an integer, add 2 to it, and then convert it back to a string. Hence, A becomes C, K becomes M, etc.</p>
| 7 | 2009-12-17T16:46:29Z | 1,923,074 | <p>Try <code>ord()</code>, should do the trick :)</p>
| 1 | 2009-12-17T16:48:57Z | [
"python",
"string",
"encryption",
"integer"
] |
Convert string / character to integer in python | 1,923,054 | <p>I want to convert a single character of a string into an integer, add 2 to it, and then convert it back to a string. Hence, A becomes C, K becomes M, etc.</p>
| 7 | 2009-12-17T16:46:29Z | 1,923,076 | <p>This is done through the chr and ord functions. Eg; <code>chr(ord(ch)+2)</code> does what you want. These are fully described <a href="http://docs.python.org/library/functions.html">here</a>.</p>
| 13 | 2009-12-17T16:49:06Z | [
"python",
"string",
"encryption",
"integer"
] |
Convert string / character to integer in python | 1,923,054 | <p>I want to convert a single character of a string into an integer, add 2 to it, and then convert it back to a string. Hence, A becomes C, K becomes M, etc.</p>
| 7 | 2009-12-17T16:46:29Z | 1,923,077 | <p><a href="http://docs.python.org/library/functions.html" rel="nofollow">http://docs.python.org/library/functions.html</a></p>
<p>ord(c)</p>
<p>Given a string of length one, return an integer representing the Unicode code point of the character when the argument is a unicode object, or the value of the byte when the... | 2 | 2009-12-17T16:49:50Z | [
"python",
"string",
"encryption",
"integer"
] |
Convert string / character to integer in python | 1,923,054 | <p>I want to convert a single character of a string into an integer, add 2 to it, and then convert it back to a string. Hence, A becomes C, K becomes M, etc.</p>
| 7 | 2009-12-17T16:46:29Z | 1,923,085 | <p>This sounds a lot like homework, so I'll give you a couple of pieces and let you fill in the rest.</p>
<p>To access a single character of string s, its s[x] where x is an integer index. Indices start at 0.</p>
<p>To get the integer value of a character it is ord(c) where c is the character. To cast an integer ba... | 11 | 2009-12-17T16:50:56Z | [
"python",
"string",
"encryption",
"integer"
] |
Convert string / character to integer in python | 1,923,054 | <p>I want to convert a single character of a string into an integer, add 2 to it, and then convert it back to a string. Hence, A becomes C, K becomes M, etc.</p>
| 7 | 2009-12-17T16:46:29Z | 1,923,090 | <p>Normally, Just ord and add 2 and chr back, (Y, Z will give you unexpected result ("[","\")</p>
<pre><code>>>> chr(ord("A")+2)
'C'
</code></pre>
<p>If you want to change Y, Z to A, B, you could do like this.</p>
<pre><code>>>> chr((ord("A")-0x41+2)%26+0x41)
'C'
>>> chr((ord("Y")-0x41+2)%... | 1 | 2009-12-17T16:51:27Z | [
"python",
"string",
"encryption",
"integer"
] |
Convert string / character to integer in python | 1,923,054 | <p>I want to convert a single character of a string into an integer, add 2 to it, and then convert it back to a string. Hence, A becomes C, K becomes M, etc.</p>
| 7 | 2009-12-17T16:46:29Z | 1,923,093 | <p>For a whole string this would be:</p>
<pre><code>>>> s = "Anne"
>>> ''.join([chr(ord(i)+2) for i in s])
'Cppg'
</code></pre>
<p>It's diffucult for 'Y', 'Z' ...</p>
<pre><code>>>> s = "Zappa"
>>> ''.join([chr(ord(i)+2) for i in s])
'\\crrc'
</code></pre>
<p>Functions: <a href... | 0 | 2009-12-17T16:51:57Z | [
"python",
"string",
"encryption",
"integer"
] |
Convert string / character to integer in python | 1,923,054 | <p>I want to convert a single character of a string into an integer, add 2 to it, and then convert it back to a string. Hence, A becomes C, K becomes M, etc.</p>
| 7 | 2009-12-17T16:46:29Z | 1,932,007 | <p>"ord" is only part of the solution. The puzzle you mentioned there rotates, so that "X"+3 rotates to "A". The most famous of these is rot-13, which rotates 13 characters. Applying rot-13 twice (rotating 26 characters) brings the text back to itself.</p>
<p>The easiest way to handle this is with a translation table.... | 1 | 2009-12-19T04:59:21Z | [
"python",
"string",
"encryption",
"integer"
] |
monolithic inheritance vs modular member based OOP design | 1,923,101 | <p>I'm having a hard time making a design decision</p>
<p>I have a class in python, that processing form data, this data is very similar to other form data, and so I'm refactoring it into it's own object so it can be reused by the other classes.</p>
<p>The delima is weather to make this formprocessor a member of the ... | 3 | 2009-12-17T16:52:56Z | 1,923,175 | <p>Definitely go for the modular approach. Some of the advantages of taking the modular approach are:</p>
<ul>
<li>It makes your code more readable, i.e. it's more clear what the PageHandler and FormProcessor do</li>
<li>It makes it easier and more effective to write <a href="http://en.wikipedia.org/wiki/Unit%5Ftestin... | 7 | 2009-12-17T17:05:59Z | [
"python",
"oop"
] |
Get class object __dict__ without special attributes | 1,923,130 | <p>For getting all the defined class attributes I try to go with</p>
<pre><code>TheClass.__dict__
</code></pre>
<p>but that also gives me the special attributes. Is there a way to get only the self-defined attributes or do I have to "clean" the dict myself?</p>
| 0 | 2009-12-17T16:58:38Z | 1,923,195 | <p>You can't clean the <code>__dict__</code>:</p>
<pre><code>AttributeError: attribute '__dict__' of 'type' objects is not writable
</code></pre>
<p>You could rely on <a href="http://www.python.org/dev/peps/pep-0008/" rel="nofollow">naming conventions</a>:</p>
<pre><code>class A(object):
def __init__(self, arg):... | 3 | 2009-12-17T17:09:15Z | [
"python",
"oop",
"new-style-class"
] |
Get class object __dict__ without special attributes | 1,923,130 | <p>For getting all the defined class attributes I try to go with</p>
<pre><code>TheClass.__dict__
</code></pre>
<p>but that also gives me the special attributes. Is there a way to get only the self-defined attributes or do I have to "clean" the dict myself?</p>
| 0 | 2009-12-17T16:58:38Z | 1,923,226 | <p>I don't think there's anything simple, and why would there be? There's no language-enforced difference between magic and user-defined attributes.</p>
<p>MYYN's solution won't work if you have a user-defined attribute starting with '__'. However, it does suggest a solution based on conventions: if you want to intros... | 2 | 2009-12-17T17:14:57Z | [
"python",
"oop",
"new-style-class"
] |
Get class object __dict__ without special attributes | 1,923,130 | <p>For getting all the defined class attributes I try to go with</p>
<pre><code>TheClass.__dict__
</code></pre>
<p>but that also gives me the special attributes. Is there a way to get only the self-defined attributes or do I have to "clean" the dict myself?</p>
| 0 | 2009-12-17T16:58:38Z | 1,923,843 | <p>Another solution:</p>
<pre><code>class _BaseA(object):
_intern = object.__dict__.keys()
class A(_BaseA):
myattribute = 1
print filter(lambda x: x not in A._intern+['__module__'], A.__dict__.keys())
</code></pre>
<p>I don't think this is terribly robust and there might still be a better way.</p>
<p>This ... | 4 | 2009-12-17T18:58:48Z | [
"python",
"oop",
"new-style-class"
] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.