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
Python: date, time formatting
2,487,109
<p>I need to generate a local timestamp in a form of YYYYMMDDHHmmSSOHH'mm'. That OHH'mm' is one of +, -, Z and then there are hourhs and minutes followed by '.</p> <p>Please, how do I get such a timestamp, denoting both local time zone and possible daylight saving?</p>
14
2010-03-21T12:52:18Z
2,487,117
<p><a href="http://docs.python.org/library/time.html#time.strftime">time.strftime</a> will do for that,</p> <p>And in linux, <code>%z</code> will just give you -HHMM format if environment variable is properly set.</p> <pre><code>&gt;&gt;&gt; os.environ['TZ'] = 'EST' &gt;&gt;&gt; time.strftime('%x %X %z') '03/21/10 08...
7
2010-03-21T12:55:30Z
[ "python", "datetime" ]
Python: date, time formatting
2,487,109
<p>I need to generate a local timestamp in a form of YYYYMMDDHHmmSSOHH'mm'. That OHH'mm' is one of +, -, Z and then there are hourhs and minutes followed by '.</p> <p>Please, how do I get such a timestamp, denoting both local time zone and possible daylight saving?</p>
14
2010-03-21T12:52:18Z
2,487,161
<pre><code>import time localtime = time.localtime() timeString = time.strftime("%Y%m%d%H%M%S", localtime) # is DST in effect? timezone = -(time.altzone if localtime.tm_isdst else time.timezone) timeString += "Z" if timezone == 0 else "+" if timezone &gt; 0 else "-" timeString += time.strftime("%H'%M'", time.gmt...
23
2010-03-21T13:12:45Z
[ "python", "datetime" ]
Hidden line removal in JavaScript or Python?
2,487,235
<p>I have the following task:</p> <ul> <li><p>Input:</p> <p>A 3D scene comprised of a set of cuboids. Could be broken down to a set of triangles.</p> <p>A description of a camera: position, direction, focal length.</p></li> <li><p>Output: 2D wire frame projection of the scene as a set of lines. Important: Hidden lin...
1
2010-03-21T13:32:06Z
2,488,209
<p>Maybe the <a href="http://www.python.org/workshops/1996-06/papers/ZaneHTML/" rel="nofollow">Python Nacrisse interface</a> is useful?</p>
0
2010-03-21T18:31:00Z
[ "javascript", "python", "3d", "hidden", "lines" ]
Using ManagementClass.Getinstances() from IronPython
2,487,475
<p>I have an IronPython script that looks for current running processes using WMI. The code looks like this:</p> <pre><code>import clr clr.AddReference('System.Management') from System.Management import ManagementClass from System import Array mc = ManagementClass('Win32_Processes') procs = mc.GetInstances() </code></...
1
2010-03-21T14:50:40Z
2,487,985
<p>I think the only problem is that 'Win32_Processes' is a typo for <strong>'Win32_Process'</strong>. This seems to work:</p> <pre><code>&gt;&gt;&gt; mc = ManagementClass('Win32_Process') &gt;&gt;&gt; procs = mc.GetInstances() &gt;&gt;&gt; for p in procs: ... print p['Name'] ... System Idle Process System smss.e...
1
2010-03-21T17:30:01Z
[ "python", "wmi", "ironpython" ]
django image upload forms
2,487,521
<p>I am having problems with django forms and image uploads. I have googled, read the documentations and even questions ere, but cant figure out the issue. Here are my files</p> <p>my models</p> <pre><code>class UserProfile(User): """user with app settings. """ DESIGNATION_CHOICES=( ('ADM', 'Administrator...
2
2010-03-21T15:10:29Z
2,488,357
<p>the issue was actually that this line </p> <pre><code>&lt;form method="post" id="form" action="" enctype="multipart/form-data" class="infotabs accfrm"&gt; </code></pre> <p>appeared as </p> <pre><code>&lt;form method="post" id="form" action="" enctype="multipart/form -data" class="infotabs accfrm"&gt; </code></pre...
3
2010-03-21T19:11:34Z
[ "python", "django", "django-forms" ]
how to make python load dylib on osx
2,488,016
<p>Trying to load a shared lib out of the current '.' dir in a unit test on osx.</p> <p>What works on Linux and Netbsd there is a symlink <code>_mymodule.so --&gt; ../.libs/libmymodule.so</code></p> <p>but on osx, python's <code>import mymodule</code> won't find</p> <pre><code>_mymodule.dylib --&gt; ../.libs/libmymo...
6
2010-03-21T17:42:05Z
2,582,504
<p>Just use *.so as your module extensions in OS X too. I have a vague memory of not being able to load .dylib's and it turning out to be an issue with python itself. . . but I can't find the mailing list post now.</p> <p>However, rest assured you're following standard practice by using *.so's even on OS X. The only *...
10
2010-04-06T03:38:43Z
[ "python", "osx", "dylib" ]
Djangobb problem
2,488,142
<p>I've installed Djangobb app on my server (Debian, mod_python) by cloning original source. The only things I've changed is database options in settings.py. All needed components are installed - syncdb query was executed right.</p> <p>But, when I'm trying to enter on my forum, it returns me error:</p> <blockquote> ...
4
2010-03-21T18:17:18Z
2,488,505
<p>There are two obvious reasons to why this might happen:</p> <ol> <li><code>djangobb_forum</code> is not on your Python path</li> <li>There is no <code>__init__.py</code> in the <code>djangobb_forum</code> folder</li> </ol> <p>If the code says <code>from djangobb_forum import ...</code> then you need to have the pa...
2
2010-03-21T20:02:40Z
[ "python", "django", "mod-python" ]
Pypcap for mac on python 2.6?
2,488,185
<p>How do you end up running pypcap for python 2.6 on a mac? It seems that there hasn't been any new releases since 2.5 or am I just looking in the wrong places?</p> <p>I seem to be unable to install the 2.5 binary with the following error: You cannot install pcap 1.1 on this volume. pcap requires System Python 2.5 t...
1
2010-03-21T18:25:28Z
2,488,246
<p>Python 2.5 code should run fine unaltered on Python 2.6 (you'll just occasionaly get a <code>DeprecationWarning</code> for features which are changing in Python 3.x).</p>
1
2010-03-21T18:43:05Z
[ "python", "pcap", "packet-sniffers" ]
Python: Inheritance of a class attribute (list)
2,488,306
<p>inheriting a class attribute from a super class and later changing the value for the subclass works fine:</p> <pre><code>class Unit(object): value = 10 class Archer(Unit): pass print Unit.value print Archer.value Archer.value = 5 print Unit.value print Archer.value </code></pre> <p>leads to the output:...
4
2010-03-21T19:00:55Z
2,488,362
<p>It is not a matter of shallow or deep copies, it is a matter of references and assignments.</p> <p>It the first case <code>Unit.value</code> and <code>Archer.value</code> are two variables which reference the same value. When you do <code>Archer.value = 5</code>, you are assigning a new reference to Acher.value.</...
9
2010-03-21T19:13:49Z
[ "python", "inheritance", "list", "deep-copy", "class-attributes" ]
Python: Inheritance of a class attribute (list)
2,488,306
<p>inheriting a class attribute from a super class and later changing the value for the subclass works fine:</p> <pre><code>class Unit(object): value = 10 class Archer(Unit): pass print Unit.value print Archer.value Archer.value = 5 print Unit.value print Archer.value </code></pre> <p>leads to the output:...
4
2010-03-21T19:00:55Z
2,488,367
<p>You coud copy the list in the definition of Archer:</p> <pre><code>class Archer(Unit): listvalue = Unit.listvalue[:] </code></pre>
0
2010-03-21T19:14:33Z
[ "python", "inheritance", "list", "deep-copy", "class-attributes" ]
Python: Inheritance of a class attribute (list)
2,488,306
<p>inheriting a class attribute from a super class and later changing the value for the subclass works fine:</p> <pre><code>class Unit(object): value = 10 class Archer(Unit): pass print Unit.value print Archer.value Archer.value = 5 print Unit.value print Archer.value </code></pre> <p>leads to the output:...
4
2010-03-21T19:00:55Z
2,488,610
<p>Michael's answer is nice and simple, but if you wish to avoid having to add that line to each Unit subclass - maybe you have a bunch of other lists like that one, a metaclass is an easy way to solve the problem</p> <pre><code>class UnitMeta(type): def __init__(self, *args): super(UnitMeta, self).__init_...
6
2010-03-21T20:34:17Z
[ "python", "inheritance", "list", "deep-copy", "class-attributes" ]
Framework for Implementing REST web service in Django
2,488,325
<p>I'm looking to implement a RESTful interface for a Django application. It is primarily a data-service application - the interface will be (at this point) read-only. </p> <p>The question is which Django toolsets / frameworks make the most sense for this task. </p> <p>I see Django-rest and Django-piston. There's...
21
2010-03-21T19:06:36Z
2,488,508
<p><a href="http://stackoverflow.com/questions/212941/using-django-rest-interface">http://stackoverflow.com/questions/212941/using-django-rest-interface</a></p> <p>Still true.</p> <p>It's quite trivial to roll your own. Each REST URI maps to a view function. Each REST method (GET, POST, PUT, DELETE) is a simple con...
4
2010-03-21T20:03:35Z
[ "python", "django", "web-services", "rest" ]
Framework for Implementing REST web service in Django
2,488,325
<p>I'm looking to implement a RESTful interface for a Django application. It is primarily a data-service application - the interface will be (at this point) read-only. </p> <p>The question is which Django toolsets / frameworks make the most sense for this task. </p> <p>I see Django-rest and Django-piston. There's...
21
2010-03-21T19:06:36Z
2,488,734
<blockquote> <p>NOTE: Since this post was written, <code>django-piston</code> is no longer actively maintained. As others have mentioned, look into <code>tastypie</code> or <code>django-rest-framework</code>.</p> </blockquote> <p>Indeed, you can roll your own, but there's a lot of boilerplate involved.</p> <p>...
17
2010-03-21T21:12:25Z
[ "python", "django", "web-services", "rest" ]
Framework for Implementing REST web service in Django
2,488,325
<p>I'm looking to implement a RESTful interface for a Django application. It is primarily a data-service application - the interface will be (at this point) read-only. </p> <p>The question is which Django toolsets / frameworks make the most sense for this task. </p> <p>I see Django-rest and Django-piston. There's...
21
2010-03-21T19:06:36Z
6,535,255
<p>One way is to roll your own, or use django-piston which is excellent. But the problem I have with piston is that is kind a made to be attached to a existing django-project to add an API. It is not so much made for building a resource oriented API with support for formats including HTML.</p> <p>The way I see the use...
4
2011-06-30T13:10:36Z
[ "python", "django", "web-services", "rest" ]
Framework for Implementing REST web service in Django
2,488,325
<p>I'm looking to implement a RESTful interface for a Django application. It is primarily a data-service application - the interface will be (at this point) read-only. </p> <p>The question is which Django toolsets / frameworks make the most sense for this task. </p> <p>I see Django-rest and Django-piston. There's...
21
2010-03-21T19:06:36Z
7,411,725
<p>And since this question still rated pretty highly in my searches on Google, I'll add this alternative to the mix: <a href="http://django-rest-framework.org/">http://django-rest-framework.org/</a></p> <p>My initial impression is that it does a very good job of embodying the RESTful API design principles described he...
8
2011-09-14T05:37:22Z
[ "python", "django", "web-services", "rest" ]
Framework for Implementing REST web service in Django
2,488,325
<p>I'm looking to implement a RESTful interface for a Django application. It is primarily a data-service application - the interface will be (at this point) read-only. </p> <p>The question is which Django toolsets / frameworks make the most sense for this task. </p> <p>I see Django-rest and Django-piston. There's...
21
2010-03-21T19:06:36Z
8,649,537
<p><a href="http://readthedocs.org/docs/django-tastypie/en/latest/" rel="nofollow">tastypie</a> is also an option, I've just tried it and it seemed painless until now. I'm playing with a dummy app that exposes an API to a backbone.js client and I've hit no brick walls with this library. <a href="http://joshbohde.com/bl...
2
2011-12-27T21:34:46Z
[ "python", "django", "web-services", "rest" ]
ping ip's / hostname in app engine
2,488,430
<p>I'm trying to ping an ip address and telnet to some port from an app hosted on app engine. I've tried using <a href="http://svn.pylucid.net/pylucid/CodeSnippets/ping.py" rel="nofollow">http://svn.pylucid.net/pylucid/CodeSnippets/ping.py</a> but for some reason it failing </p> <p>Thanks</p>
0
2010-03-21T19:36:23Z
2,488,551
<blockquote> <p>Note that ICMP messages can only be sent from processes running as root.</p> </blockquote> <p>Your app on GAE is not running as root. Why bother pinging when you want to telnet anyway?</p>
0
2010-03-21T20:16:33Z
[ "python", "google-app-engine" ]
ping ip's / hostname in app engine
2,488,430
<p>I'm trying to ping an ip address and telnet to some port from an app hosted on app engine. I've tried using <a href="http://svn.pylucid.net/pylucid/CodeSnippets/ping.py" rel="nofollow">http://svn.pylucid.net/pylucid/CodeSnippets/ping.py</a> but for some reason it failing </p> <p>Thanks</p>
0
2010-03-21T19:36:23Z
2,488,674
<p>GAE does not support ICMP nor Telnet.</p> <p>For communication with outside hosts at the moment only mail/xmpp/http(s) work through the specific API calls. </p>
2
2010-03-21T20:56:14Z
[ "python", "google-app-engine" ]
Newbie Python programmer tangling with Lists
2,488,447
<p>Here's what I've got so far:</p> <pre><code># A. match_ends # Given a list of strings, return the count of the number of # strings where the string length is 2 or more and the first # and last chars of the string are the same. # Note: python does not have a ++ operator, but += works. def match_ends(words): counte...
0
2010-03-21T19:40:58Z
2,488,460
<p>You should do:</p> <pre><code>counter += 1 </code></pre> <p>instead of</p> <pre><code>counter += counter </code></pre> <p>which stays at 0 for all ages.</p>
2
2010-03-21T19:43:42Z
[ "python", "list" ]
Newbie Python programmer tangling with Lists
2,488,447
<p>Here's what I've got so far:</p> <pre><code># A. match_ends # Given a list of strings, return the count of the number of # strings where the string length is 2 or more and the first # and last chars of the string are the same. # Note: python does not have a ++ operator, but += works. def match_ends(words): counte...
0
2010-03-21T19:40:58Z
2,488,471
<pre><code>counter += 1 </code></pre> <p>you add 0 to 0</p>
0
2010-03-21T19:47:52Z
[ "python", "list" ]
How to increment variable names/Is this a bad idea
2,488,457
<p>In Python, if I were to have a user input the number X, and then the program enters a for loop in which the user inputs X values, is there a way/is it a bad idea to have variable names automatically increment?</p> <p>ie: </p> <pre><code>user inputs '6' value_1 = ... value_2 = ... value_3 = ... value_4 = ....
2
2010-03-21T19:43:35Z
2,488,467
<p>You should append all the values to a list, that allows you to easily iterate through the values later as well as not littering your namespace with useless variables and magic.</p>
11
2010-03-21T19:46:22Z
[ "python", "variables", "loops", "for-loop", "increment" ]
How to increment variable names/Is this a bad idea
2,488,457
<p>In Python, if I were to have a user input the number X, and then the program enters a for loop in which the user inputs X values, is there a way/is it a bad idea to have variable names automatically increment?</p> <p>ie: </p> <pre><code>user inputs '6' value_1 = ... value_2 = ... value_3 = ... value_4 = ....
2
2010-03-21T19:43:35Z
2,488,598
<blockquote> <p>should I be using a completely different method such as appending all the new values onto a list?</p> </blockquote> <p>Yes, you can use a <a href="http://docs.python.org/library/stdtypes.html#sequence-types-str-unicode-list-tuple-buffer-xrange" rel="nofollow">list</a>. You can also use a <a href...
8
2010-03-21T20:29:04Z
[ "python", "variables", "loops", "for-loop", "increment" ]
How to increment variable names/Is this a bad idea
2,488,457
<p>In Python, if I were to have a user input the number X, and then the program enters a for loop in which the user inputs X values, is there a way/is it a bad idea to have variable names automatically increment?</p> <p>ie: </p> <pre><code>user inputs '6' value_1 = ... value_2 = ... value_3 = ... value_4 = ....
2
2010-03-21T19:43:35Z
2,491,738
<p>While using a list or dictionary is of course the right approach here, it is possible to create named variables, using exec(). You'll miss out on all the convenient operations that lists and dictionaries provide (think of len(), inserting, removing, sorting, and so on), but it's possible:</p> <pre><code>count = raw...
4
2010-03-22T11:21:28Z
[ "python", "variables", "loops", "for-loop", "increment" ]
How to increment variable names/Is this a bad idea
2,488,457
<p>In Python, if I were to have a user input the number X, and then the program enters a for loop in which the user inputs X values, is there a way/is it a bad idea to have variable names automatically increment?</p> <p>ie: </p> <pre><code>user inputs '6' value_1 = ... value_2 = ... value_3 = ... value_4 = ....
2
2010-03-21T19:43:35Z
31,082,215
<p>Maybe this will help you out.</p> <pre><code>filenamee = 'Value_' counter1 = 1 counter2 = 2 list1 = [] list2 = range(30) #random number, given len needed for x in list2: counter1 = str(counter1) full_name = (filenamee+counter1) list1.append(full_name) counter1 = counter2 counter2+=1 for x in...
1
2015-06-26T21:11:03Z
[ "python", "variables", "loops", "for-loop", "increment" ]
What exactly are tuples in Python?
2,488,522
<p>I'm following a couple of Pythone exercises and I'm stumped at this one.</p> <pre><code># C. sort_last # Given a list of non-empty tuples, return a list sorted in increasing # order by the last element in each tuple. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use...
5
2010-03-21T20:07:57Z
2,488,535
<p>The tuple is the simplest of Python's sequence types. You can think about it as an immutable (read-only) list:</p> <pre><code>&gt;&gt;&gt; t = (1, 2, 3) &gt;&gt;&gt; print t[0] 1 &gt;&gt;&gt; t[0] = 2 TypeError: tuple object does not support item assignment </code></pre> <p>Tuples can be turned into new lists by j...
14
2010-03-21T20:12:22Z
[ "python", "list", "tuples" ]
What exactly are tuples in Python?
2,488,522
<p>I'm following a couple of Pythone exercises and I'm stumped at this one.</p> <pre><code># C. sort_last # Given a list of non-empty tuples, return a list sorted in increasing # order by the last element in each tuple. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use...
5
2010-03-21T20:07:57Z
2,488,541
<p>A tuple and a list is very similar. The main difference (as a user) is that a tuple is immutable (can't be modified)</p> <p>In your example:</p> <pre><code>[(2, 2), (1, 3), (3, 4, 5), (1, 7)] </code></pre> <p>This is a list of tuples [...] is the list (2,2) is a tuple</p>
3
2010-03-21T20:13:20Z
[ "python", "list", "tuples" ]
What exactly are tuples in Python?
2,488,522
<p>I'm following a couple of Pythone exercises and I'm stumped at this one.</p> <pre><code># C. sort_last # Given a list of non-empty tuples, return a list sorted in increasing # order by the last element in each tuple. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use...
5
2010-03-21T20:07:57Z
2,488,665
<ul> <li><a href="http://www.python.org/doc/faq/general/#why-are-there-separate-tuple-and-list-data-types">Why are there separate tuple and list data types?</a> (Python FAQ)</li> <li><a href="http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/">Python Tuples are Not Just Constant Lists</a></li...
11
2010-03-21T20:53:16Z
[ "python", "list", "tuples" ]
What exactly are tuples in Python?
2,488,522
<p>I'm following a couple of Pythone exercises and I'm stumped at this one.</p> <pre><code># C. sort_last # Given a list of non-empty tuples, return a list sorted in increasing # order by the last element in each tuple. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use...
5
2010-03-21T20:07:57Z
2,491,793
<p>Tuples are used to group related variables together. It's often more convenient to use a tuple rather than writing yet another single-use class. Granted, accessing their content by index is more obscure than a named member variable, but it's always possible to use 'tuple unpacking':</p> <pre><code>def returnTuple(a...
0
2010-03-22T11:29:57Z
[ "python", "list", "tuples" ]
What exactly are tuples in Python?
2,488,522
<p>I'm following a couple of Pythone exercises and I'm stumped at this one.</p> <pre><code># C. sort_last # Given a list of non-empty tuples, return a list sorted in increasing # order by the last element in each tuple. # e.g. [(1, 7), (1, 3), (3, 4, 5), (2, 2)] yields # [(2, 2), (1, 3), (3, 4, 5), (1, 7)] # Hint: use...
5
2010-03-21T20:07:57Z
3,416,326
<p>The best summary of the differences between lists and tuples I have read is this:</p> <blockquote> <p>One common summary of these more interesting, if subtle, differences is that tuples are <em>heterogeneous</em> and lists are <em>homogeneous</em>. In other words: Tuples (generally) are sequences of <strong>diffe...
1
2010-08-05T15:27:45Z
[ "python", "list", "tuples" ]
Trouble with this Python newbie exercise. Using Lists and finding if two adjacent elements are the same
2,488,651
<p>Here's what I got:</p> <pre><code># D. Given a list of numbers, return a list where # all adjacent == elements have been reduced to a single element, # so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or # modify the passed in list. def remove_adjacent(nums): for number in nums: numberHolder = num...
0
2010-03-21T20:48:42Z
2,488,659
<p>Compare the current number with the previous number. If it's not the same then append it to a new list. Then save it so that the next loop can use it.</p>
0
2010-03-21T20:50:37Z
[ "python", "list" ]
Trouble with this Python newbie exercise. Using Lists and finding if two adjacent elements are the same
2,488,651
<p>Here's what I got:</p> <pre><code># D. Given a list of numbers, return a list where # all adjacent == elements have been reduced to a single element, # so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or # modify the passed in list. def remove_adjacent(nums): for number in nums: numberHolder = num...
0
2010-03-21T20:48:42Z
2,488,704
<pre><code>&gt;&gt;&gt; import itertools &gt;&gt;&gt; [i[0] for i in itertools.groupby([1,2,2,3,3,3,2,2])] [1, 2, 3, 2] </code></pre> <p>Or:</p> <pre><code>&gt;&gt;&gt; def f(l): ... r = [] ... last = None ... for i in l: ... if i != last: ... r.append(i) ... last = i ... ...
5
2010-03-21T21:02:51Z
[ "python", "list" ]
Trouble with this Python newbie exercise. Using Lists and finding if two adjacent elements are the same
2,488,651
<p>Here's what I got:</p> <pre><code># D. Given a list of numbers, return a list where # all adjacent == elements have been reduced to a single element, # so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or # modify the passed in list. def remove_adjacent(nums): for number in nums: numberHolder = num...
0
2010-03-21T20:48:42Z
2,488,708
<p>try this:</p> <pre><code>def remove_adjacent(nums): removed_list = [] numberHolder = None for number in nums: if number != numberHolder: removed_list.append(number) numberHolder = number return removed_list </code></pre>
1
2010-03-21T21:04:13Z
[ "python", "list" ]
Trouble with this Python newbie exercise. Using Lists and finding if two adjacent elements are the same
2,488,651
<p>Here's what I got:</p> <pre><code># D. Given a list of numbers, return a list where # all adjacent == elements have been reduced to a single element, # so [1, 2, 2, 3] returns [1, 2, 3]. You may create a new list or # modify the passed in list. def remove_adjacent(nums): for number in nums: numberHolder = num...
0
2010-03-21T20:48:42Z
2,489,003
<p>This is another solution based on filter(). It's not just for adyacent and equal elements but for equal elements:</p> <pre><code>def remove_repeated_items(collection): uniques = [] def not_already_added(item): if item in uniques: return False else: uniques.append(item) r...
0
2010-03-21T22:26:03Z
[ "python", "list" ]
Python performance: iteration and operations on nested lists
2,488,654
<p><strong>Problem</strong> Hey folks. I'm looking for some advice on python performance. Some background on my problem:</p> <p>Given: </p> <ol> <li>A <code>(x,y)</code> mesh of nodes each with a value <code>(0...255)</code> starting at 0</li> <li>A list of <code>N</code> input coordinates each at a specified loca...
4
2010-03-21T20:49:03Z
2,488,937
<p>You can create your own Python module in C, and control the performance as you want: <a href="http://docs.python.org/extending/" rel="nofollow">http://docs.python.org/extending/</a></p>
0
2010-03-21T22:07:19Z
[ "performance", "python" ]
Python performance: iteration and operations on nested lists
2,488,654
<p><strong>Problem</strong> Hey folks. I'm looking for some advice on python performance. Some background on my problem:</p> <p>Given: </p> <ol> <li>A <code>(x,y)</code> mesh of nodes each with a value <code>(0...255)</code> starting at 0</li> <li>A list of <code>N</code> input coordinates each at a specified loca...
4
2010-03-21T20:49:03Z
2,489,024
<p><strong>1</strong>. A (smaller) speedup could definitely be the initialization of your <code>rows</code>...</p> <p>Replace</p> <pre><code>rows = [] for i in range(x): rows.append([0 for i in xrange(y)]) </code></pre> <p>with</p> <pre><code>rows = [[0] * y for i in xrange(x)] </code></pre> <p><strong>2</stro...
2
2010-03-21T22:33:20Z
[ "performance", "python" ]
Python performance: iteration and operations on nested lists
2,488,654
<p><strong>Problem</strong> Hey folks. I'm looking for some advice on python performance. Some background on my problem:</p> <p>Given: </p> <ol> <li>A <code>(x,y)</code> mesh of nodes each with a value <code>(0...255)</code> starting at 0</li> <li>A list of <code>N</code> input coordinates each at a specified loca...
4
2010-03-21T20:49:03Z
2,489,094
<p>in your f3 rewrite, g can be simplified. (Can also be applied to f4)</p> <p>You have the following code inside a for loop.</p> <pre><code>l = max(0, topleft[1]) r = min(topleft[1]+(75*2), 1024) </code></pre> <p>However, it appears that those values never change inside the for loop. So calculate them once, outside...
1
2010-03-21T22:54:18Z
[ "performance", "python" ]
Python performance: iteration and operations on nested lists
2,488,654
<p><strong>Problem</strong> Hey folks. I'm looking for some advice on python performance. Some background on my problem:</p> <p>Given: </p> <ol> <li>A <code>(x,y)</code> mesh of nodes each with a value <code>(0...255)</code> starting at 0</li> <li>A list of <code>N</code> input coordinates each at a specified loca...
4
2010-03-21T20:49:03Z
2,489,215
<p>Based on your f3 version I played with the code. As l and r are constants you can avoid to compute them in g1 loop. Also using new <strong>ternary if</strong> instead of min and max seems to be consistently faster. Also simplified expression with topleft. On my system it appears to be about 20% faster using with the...
1
2010-03-21T23:24:53Z
[ "performance", "python" ]
Python performance: iteration and operations on nested lists
2,488,654
<p><strong>Problem</strong> Hey folks. I'm looking for some advice on python performance. Some background on my problem:</p> <p>Given: </p> <ol> <li>A <code>(x,y)</code> mesh of nodes each with a value <code>(0...255)</code> starting at 0</li> <li>A list of <code>N</code> input coordinates each at a specified loca...
4
2010-03-21T20:49:03Z
2,489,399
<p>On my (slow-ish;-) first-day Macbook Air, 1.6GHz Core 2 Duo, system Python 2.5 on MacOSX 10.5, after saving your code in <code>op.py</code> I see the following timings:</p> <pre><code>$ python -mtimeit -s'import op' 'op.f1()' 10 loops, best of 3: 5.58 sec per loop $ python -mtimeit -s'import op' 'op.f2()' 10 loops,...
2
2010-03-22T00:32:58Z
[ "performance", "python" ]
python parallel computing: split keyspace to give each node a range to work on
2,488,670
<p>My question is rather complicated for me to explain, as i'm not really good at maths, but i'll try to be as clear as possible.</p> <p>I'm trying to code a cluster in python, which will generate words given a charset (i.e. with lowercase: aaaa, aaab, aaac, ..., zzzz) and make various operations on them. I'm searchi...
1
2010-03-21T20:55:22Z
2,488,699
<p>Any way that you could compute a small integer from the string would be fine for clustering. For example, compute a hash with md5, and look at a byte of it:</p> <pre><code>import hashlib s = "aaac" num_nodes = 5 # or whatever m = hashlib.md5(s) node = ord(m.digest()[0]) % num_nodes print node # prints 2 </code>...
1
2010-03-21T21:01:05Z
[ "python", "algorithm", "cluster-analysis", "character-encoding" ]
python parallel computing: split keyspace to give each node a range to work on
2,488,670
<p>My question is rather complicated for me to explain, as i'm not really good at maths, but i'll try to be as clear as possible.</p> <p>I'm trying to code a cluster in python, which will generate words given a charset (i.e. with lowercase: aaaa, aaab, aaac, ..., zzzz) and make various operations on them. I'm searchi...
1
2010-03-21T20:55:22Z
2,489,898
<p>You should be able to treat your words as numerals in a strange base. For example, let's say you have a..z as your charset (26 characters), 4 character strings, and you want to distribute among equally 10 machines. Then there are a total of 26^4 strings, so each machine gets 26^4/10 strings. The first machine wil...
1
2010-03-22T03:33:33Z
[ "python", "algorithm", "cluster-analysis", "character-encoding" ]
django admin - adding fields on the fly
2,488,710
<p>Basically I am writing a simple shopping cart. Each item can have multiple prices. (i.e. shirts where each size is priced differently). I would like to have a single price field in my admin panel, where when the first price is entered, an additional price field pops up. However I am kind of at a loss as to how to do...
2
2010-03-21T21:04:25Z
2,488,726
<p>You probably want <a href="http://docs.djangoproject.com/en/1.1/ref/contrib/admin/#inlinemodeladmin-objects" rel="nofollow">inlines</a> and some javascript.</p>
0
2010-03-21T21:09:08Z
[ "python", "django", "django-admin", "shopping-cart", "django-nonrel" ]
django admin - adding fields on the fly
2,488,710
<p>Basically I am writing a simple shopping cart. Each item can have multiple prices. (i.e. shirts where each size is priced differently). I would like to have a single price field in my admin panel, where when the first price is entered, an additional price field pops up. However I am kind of at a loss as to how to do...
2
2010-03-21T21:04:25Z
2,488,729
<p>Sounds like you want two related models - Item and Option. Item would contain the name of the item, and Option would contain the option - eg size - and the price of that option. You would then set up your admin to use an inline form for Option.</p>
1
2010-03-21T21:09:42Z
[ "python", "django", "django-admin", "shopping-cart", "django-nonrel" ]
django admin - adding fields on the fly
2,488,710
<p>Basically I am writing a simple shopping cart. Each item can have multiple prices. (i.e. shirts where each size is priced differently). I would like to have a single price field in my admin panel, where when the first price is entered, an additional price field pops up. However I am kind of at a loss as to how to do...
2
2010-03-21T21:04:25Z
2,488,825
<p>You might try checking out the Satchmo codebase. It has something similar that may provide some inspiration. <a href="http://satchmoproject.com" rel="nofollow">http://satchmoproject.com</a></p>
0
2010-03-21T21:37:59Z
[ "python", "django", "django-admin", "shopping-cart", "django-nonrel" ]
pyopengl: Could it replace c++?
2,488,730
<p>I'm starting a computer graphics course, and I have to choose a language.</p> <p>Choices are between C++ and Python. I have no problem with C++, python is a work in progress. So i was thinking to go down the python road, using pyopengl for graphics part.</p> <p>I have heard though, that performance is an issue.</p...
9
2010-03-21T21:10:42Z
2,488,759
<p>Python is a dynamic language that get interpreted and compiled on runtime and as such cannot have better performance then C++ - take a look at <a href="http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/" rel="nofollow">this post</a> for comparison between several pro...
0
2010-03-21T21:18:32Z
[ "python", "graphics", "3d", "pyopengl" ]
pyopengl: Could it replace c++?
2,488,730
<p>I'm starting a computer graphics course, and I have to choose a language.</p> <p>Choices are between C++ and Python. I have no problem with C++, python is a work in progress. So i was thinking to go down the python road, using pyopengl for graphics part.</p> <p>I have heard though, that performance is an issue.</p...
9
2010-03-21T21:10:42Z
2,488,775
<p>Python is an awesome language, but it's not the right tool for graphics. And if you want to do anything remotely advanced you'll have to use unpythonic libraries and will end up with ugly C code written in Python.</p>
2
2010-03-21T21:24:17Z
[ "python", "graphics", "3d", "pyopengl" ]
pyopengl: Could it replace c++?
2,488,730
<p>I'm starting a computer graphics course, and I have to choose a language.</p> <p>Choices are between C++ and Python. I have no problem with C++, python is a work in progress. So i was thinking to go down the python road, using pyopengl for graphics part.</p> <p>I have heard though, that performance is an issue.</p...
9
2010-03-21T21:10:42Z
2,489,409
<p>It depends a LOT on the contents of your computer graphics course. If you are doing anything like the introductory course I've taught in the past, it's basically spinning cubes and spheres, some texture mapping and some vertex animation, and that's about it. In this case, Python would be perfectly adequate, assuming...
24
2010-03-22T00:36:01Z
[ "python", "graphics", "3d", "pyopengl" ]
pyopengl: Could it replace c++?
2,488,730
<p>I'm starting a computer graphics course, and I have to choose a language.</p> <p>Choices are between C++ and Python. I have no problem with C++, python is a work in progress. So i was thinking to go down the python road, using pyopengl for graphics part.</p> <p>I have heard though, that performance is an issue.</p...
9
2010-03-21T21:10:42Z
2,704,785
<p>Here's my personal experience:</p> <p>When I first heard about PyOpenGL, I was absolutely <em>thrilled</em>. OpenGL in my favourite language? Deal! So I started learning 3D graphics programming by myself.</p> <p>I went through several tutorials and books like <a href="http://nehe.gamedev.net/" rel="nofollow">NeHe<...
3
2010-04-24T14:25:34Z
[ "python", "graphics", "3d", "pyopengl" ]
pyopengl: Could it replace c++?
2,488,730
<p>I'm starting a computer graphics course, and I have to choose a language.</p> <p>Choices are between C++ and Python. I have no problem with C++, python is a work in progress. So i was thinking to go down the python road, using pyopengl for graphics part.</p> <p>I have heard though, that performance is an issue.</p...
9
2010-03-21T21:10:42Z
4,446,826
<p>Python is the way to go. Since all opengl programming is uploading data to the video card RAM, then using opengl to operate on it, the speed limitations in python are moot. Also it makes the hard things in C++ easy ie opening files, images, sounds etc.</p> <p>As for the person above implementing octrees, there is n...
5
2010-12-15T04:55:54Z
[ "python", "graphics", "3d", "pyopengl" ]
Read more numbers in a line in Python
2,488,769
<p>Let say i want to read the integers a, b and c from stdin (in one line, do not need to press return after each number). In c++, i would just do:</p> <p>cin >> a >>b >> c;</p> <p>How to do this in Python ?</p>
0
2010-03-21T21:23:01Z
2,488,786
<p>for string</p> <pre><code>a,b,c=raw_input().split() </code></pre> <p>for int</p> <pre><code>a,b,c=map(int,raw_input().split()) </code></pre>
3
2010-03-21T21:27:41Z
[ "python", "user-input" ]
Read more numbers in a line in Python
2,488,769
<p>Let say i want to read the integers a, b and c from stdin (in one line, do not need to press return after each number). In c++, i would just do:</p> <p>cin >> a >>b >> c;</p> <p>How to do this in Python ?</p>
0
2010-03-21T21:23:01Z
2,488,790
<pre><code>values = raw_input() # 1 3 15 a, b, c = values.split() </code></pre> <p><code>a</code> will be <code>'1'</code>, <code>b</code> will be <code>'3'</code> and <code>c</code> will be <code>'15'</code>.</p> <hr> <p>If you want to be extra short and get ints try this:</p> <pre><code>a, b, c = [int(_) for _ in...
3
2010-03-21T21:28:32Z
[ "python", "user-input" ]
How to format contour lines from Matplotlib
2,488,800
<p>I am working on using Matplotlib to produce plots of implicit equations (eg. y^x=x^y). With many thanks to the help I have already received I have got quite far with it. I have used a contour line to produce the plot. My remaining problem is with formatting the contour line eg width, color and especially zorder, whe...
3
2010-03-21T21:32:06Z
2,509,157
<p>This is rather hackish but...</p> <p>Apparently in the current release Matplotlib does not support zorder on contours. This support, however, <a href="http://matplotlib.svn.sourceforge.net/viewvc/matplotlib?view=rev&amp;revision=8098" rel="nofollow">was recently added to the trunk</a>.</p> <p>So, the right way to...
3
2010-03-24T15:53:14Z
[ "python", "matplotlib", "sympy" ]
How can I merge two lists and sort them working in 'linear' time?
2,488,889
<p>I have this, and it works:</p> <pre><code># E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, li...
1
2010-03-21T21:55:15Z
2,488,909
<p><a href="http://en.wikipedia.org/wiki/Time_complexity#Linear_time" rel="nofollow"><em>Linear time</em></a> means that the runtime of the program is proportional to the length of the input. In this case the input consists of two lists. If the lists are twice as long, then the program will run approximately twice as l...
3
2010-03-21T22:00:03Z
[ "python", "list", "merge" ]
How can I merge two lists and sort them working in 'linear' time?
2,488,889
<p>I have this, and it works:</p> <pre><code># E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, li...
1
2010-03-21T21:55:15Z
2,488,911
<p><a href="http://en.wikipedia.org/wiki/Time_complexity#Linear_time" rel="nofollow">'Linear time'</a> means that time is an <a href="http://en.wikipedia.org/wiki/Big_O_notation" rel="nofollow">O(n)</a> function, where n - the number of items input (items in the lists).<br> f(n) = O(n) means that that there exist const...
-1
2010-03-21T22:00:38Z
[ "python", "list", "merge" ]
How can I merge two lists and sort them working in 'linear' time?
2,488,889
<p>I have this, and it works:</p> <pre><code># E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, li...
1
2010-03-21T21:55:15Z
2,488,931
<p>Linear time means O(n) complexity. You can read something about algorithmn comlexity and big-O notation here: <a href="http://en.wikipedia.org/wiki/Big_O_notation" rel="nofollow">http://en.wikipedia.org/wiki/Big_O_notation</a> . You should try to combine those lists not after getting them in the finalList, try to me...
0
2010-03-21T22:04:22Z
[ "python", "list", "merge" ]
How can I merge two lists and sort them working in 'linear' time?
2,488,889
<p>I have this, and it works:</p> <pre><code># E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, li...
1
2010-03-21T21:55:15Z
2,488,935
<p>Linear means O(n) in <a href="http://en.wikipedia.org/wiki/Big_O_notation" rel="nofollow">Big O notation</a>, while your code uses a <code>sort()</code> which is most likely <code>O(nlogn)</code>.</p> <p>The question is asking for the <a href="http://en.wikipedia.org/wiki/Merge_algorithm" rel="nofollow">standard me...
4
2010-03-21T22:06:47Z
[ "python", "list", "merge" ]
How can I merge two lists and sort them working in 'linear' time?
2,488,889
<p>I have this, and it works:</p> <pre><code># E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, li...
1
2010-03-21T21:55:15Z
2,488,980
<p>Linear time means that the time taken is bounded by some undefined constant times (in this context) the number of items in the two lists you want to merge. Your approach doesn't achieve this - it takes O(n log n) time.</p> <p>When specifying how long an algorithm takes in terms of the problem size, we ignore detail...
6
2010-03-21T22:20:34Z
[ "python", "list", "merge" ]
How can I merge two lists and sort them working in 'linear' time?
2,488,889
<p>I have this, and it works:</p> <pre><code># E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, li...
1
2010-03-21T21:55:15Z
2,489,119
<p>This thread contains various implementations of a linear-time merge algorithm. Note that for practical purposes, you would use <code>heapq.merge</code>.</p>
2
2010-03-21T22:59:37Z
[ "python", "list", "merge" ]
How can I merge two lists and sort them working in 'linear' time?
2,488,889
<p>I have this, and it works:</p> <pre><code># E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, li...
1
2010-03-21T21:55:15Z
2,489,163
<p>If you build the result in reverse sorted order, you can use <code>pop()</code> and still be O(N)<br> <code>pop()</code> from the right end of the list does not require shifting the elements, so is O(1)<br> Reversing the list before we return it is O(N) </p> <pre><code>&gt;&gt;&gt; def merge(l, r): ... result =...
3
2010-03-21T23:08:56Z
[ "python", "list", "merge" ]
How can I merge two lists and sort them working in 'linear' time?
2,488,889
<p>I have this, and it works:</p> <pre><code># E. Given two lists sorted in increasing order, create and return a merged # list of all the elements in sorted order. You may modify the passed in lists. # Ideally, the solution should work in "linear" time, making a single # pass of both lists. def linear_merge(list1, li...
1
2010-03-21T21:55:15Z
34,379,946
<p>A simpler version which will require equal sized lists:</p> <pre><code>def merge_sort(L1, L2): res = [] for i in range(len(L1)): if(L1[i]&lt;L2[i]): first = L1[i] secound = L2[i] else: first = L2[i] secound = L1[i] res.extend([first,secound]) ...
0
2015-12-20T10:19:34Z
[ "python", "list", "merge" ]
How App Engine application can get a list of instance developers
2,488,910
<p>How Google App instance can get the list of developers (like in Administration > Developers).</p> <p>Hard-coding developer's email is a bad idea because nothing lasts forever.</p> <p>I would like to get a solution in python (because I don't know java).</p>
0
2010-03-21T22:00:19Z
2,492,600
<p>There's no way to do this programmatically, currently. If you only need to email all the admins, however, you can use <a href="http://code.google.com/appengine/docs/python/mail/functions.html" rel="nofollow">send_mail_to_admins</a>.</p>
2
2010-03-22T13:38:31Z
[ "python", "google-app-engine" ]
Nose Tests - File Uploads
2,488,978
<p>How would one go about testing a Pylons controller (using Nose Tests) that takes a file upload as a POST parameter? </p>
3
2010-03-21T22:20:15Z
2,500,903
<p>Like this:</p> <pre><code>class TestUploadController(TestController): // .... def test_upload_files(self): """ Check that upload of text file works. """ files = [("Filedata", "filename.txt", "contents of the file")] res = self.app.post("/my/upload/path", upload_files = files) </code...
4
2010-03-23T14:59:28Z
[ "python", "unit-testing", "pylons", "nosetests" ]
Where can I find a high quality music library for python that plays OGG, ACC and/or MP3 which isn't pygame?
2,489,041
<p>I've tried the sound module in Pygame, and I haven't been satisfied with the results. I need to play music and sounds in my game. What can I use that works with OSX, Linux and Windows, and plays sound well? Unfortunately, pyMedia doesn't compile on OSX.</p>
1
2010-03-21T22:37:54Z
2,489,065
<p><strong><a href="http://www.pyglet.org/" rel="nofollow">pyglet</a></strong> provides an object-oriented programming interface for developing games and other visually-rich applications for Windows, Mac OS X and Linux. Some of the features of pyglet are:</p> <p><strong>No external dependencies or installation require...
3
2010-03-21T22:45:06Z
[ "python", "audio", "music" ]
Where can I find a high quality music library for python that plays OGG, ACC and/or MP3 which isn't pygame?
2,489,041
<p>I've tried the sound module in Pygame, and I haven't been satisfied with the results. I need to play music and sounds in my game. What can I use that works with OSX, Linux and Windows, and plays sound well? Unfortunately, pyMedia doesn't compile on OSX.</p>
1
2010-03-21T22:37:54Z
13,324,044
<p>I have recently developed a high-level Python module which uses FFmpeg for decoding (i.e. it should support almost all existing file formats) and PortAudio for output: <a href="https://github.com/albertz/music-player/blob/master/ffmpeg.c" rel="nofollow"><code>ffmpeg</code></a></p> <p>A simple demo player is <a href...
1
2012-11-10T16:59:23Z
[ "python", "audio", "music" ]
python, wrapping class returning the average of the wrapped members
2,489,133
<p>The title isn't very clear but I'll try to explain. </p> <p>Having this class:</p> <pre><code>class Wrapped(object): def method_a(self): # do some operations return n def method_b(self): # also do some operations return n </code></pre> <p>I want to have a class that perf...
0
2010-03-21T23:02:25Z
2,489,173
<p>You can to this (call an 'arbitary' method on an object) with <a href="http://docs.python.org/library/functions.html#getattr" rel="nofollow">getattr</a>: </p> <p>Consider this as example:</p> <pre><code># this fetches you references to the 'method_a' methods for objects in self.ws funcs = [getattr(wrapped_obj, "me...
1
2010-03-21T23:10:57Z
[ "python" ]
python, wrapping class returning the average of the wrapped members
2,489,133
<p>The title isn't very clear but I'll try to explain. </p> <p>Having this class:</p> <pre><code>class Wrapped(object): def method_a(self): # do some operations return n def method_b(self): # also do some operations return n </code></pre> <p>I want to have a class that perf...
0
2010-03-21T23:02:25Z
2,489,178
<p>You could do this using the special <a href="http://docs.python.org/reference/datamodel.html#object.__getattr__" rel="nofollow"><strong>getattr</strong></a> method.</p>
0
2010-03-21T23:12:15Z
[ "python" ]
python, wrapping class returning the average of the wrapped members
2,489,133
<p>The title isn't very clear but I'll try to explain. </p> <p>Having this class:</p> <pre><code>class Wrapped(object): def method_a(self): # do some operations return n def method_b(self): # also do some operations return n </code></pre> <p>I want to have a class that perf...
0
2010-03-21T23:02:25Z
2,489,225
<p>You could do this by implementing a <a href="http://docs.python.org/reference/datamodel.html#object.__getattr__" rel="nofollow"><code>__getattr__</code></a> method in your <code>Wrapper</code> which returns a function which forwards the call to all wrapped objects. Here's an example of a basic implementation simply ...
1
2010-03-21T23:29:24Z
[ "python" ]
python, wrapping class returning the average of the wrapped members
2,489,133
<p>The title isn't very clear but I'll try to explain. </p> <p>Having this class:</p> <pre><code>class Wrapped(object): def method_a(self): # do some operations return n def method_b(self): # also do some operations return n </code></pre> <p>I want to have a class that perf...
0
2010-03-21T23:02:25Z
2,489,606
<p>While quite doable, it's just a little bit tricky because the getting of a method (or other attribute) and the calling thereof are <em>separate</em> operations. Here's a solution:</p> <pre><code>class Wrapper(object): def __init__(self): self.ws = [Wrapped(1),Wrapped(2),Wrapped(3)] def __getattr__...
3
2010-03-22T01:57:02Z
[ "python" ]
My method is being recognized within my own program. Newbie mistake probably
2,489,273
<p>Here's my code:</p> <pre><code>sentenceToTranslate = raw_input("Please write in the sentence you want to translate: ") words = sentenceToTranslate.split(" ") for word in words: if isVowel(word[0]): print "TEST" def isVowel(letter): if letter.lower() == "a" or letter.lower() == "e" or letter.lower...
1
2010-03-21T23:47:19Z
2,489,284
<p>The function hasn't been defined yet, so the name 'isVowel' doesn't have a function bound to it: that only happens at the time when you say "def".</p> <p>Solution: move your code below the <code>def isVowel(letter)</code>.</p>
5
2010-03-21T23:51:05Z
[ "python", "function" ]
My method is being recognized within my own program. Newbie mistake probably
2,489,273
<p>Here's my code:</p> <pre><code>sentenceToTranslate = raw_input("Please write in the sentence you want to translate: ") words = sentenceToTranslate.split(" ") for word in words: if isVowel(word[0]): print "TEST" def isVowel(letter): if letter.lower() == "a" or letter.lower() == "e" or letter.lower...
1
2010-03-21T23:47:19Z
2,489,287
<p>Python executes all the statements that you put in your module when it encounters them, from top to bottom. What happens is that you try to call isVowel before you have defined that function. The python interpreter has not reached that code yet. </p> <p>If you put the definition of isVowel at the top of your file y...
1
2010-03-21T23:51:33Z
[ "python", "function" ]
My method is being recognized within my own program. Newbie mistake probably
2,489,273
<p>Here's my code:</p> <pre><code>sentenceToTranslate = raw_input("Please write in the sentence you want to translate: ") words = sentenceToTranslate.split(" ") for word in words: if isVowel(word[0]): print "TEST" def isVowel(letter): if letter.lower() == "a" or letter.lower() == "e" or letter.lower...
1
2010-03-21T23:47:19Z
2,489,288
<blockquote> <p>Put the <code>def isVowel</code> definition at top.</p> </blockquote> <p>You defined the function after it had already been called. Since, the source code is <em>not compiled</em> and interpreted at run-time, it does not check for this error.</p> <p>Also, you can redefine the function as</p> <pre><...
2
2010-03-21T23:51:52Z
[ "python", "function" ]
My method is being recognized within my own program. Newbie mistake probably
2,489,273
<p>Here's my code:</p> <pre><code>sentenceToTranslate = raw_input("Please write in the sentence you want to translate: ") words = sentenceToTranslate.split(" ") for word in words: if isVowel(word[0]): print "TEST" def isVowel(letter): if letter.lower() == "a" or letter.lower() == "e" or letter.lower...
1
2010-03-21T23:47:19Z
2,489,315
<p>I think you need to define the function first, then call it after it has been defined. </p> <pre><code>def isVowel(letter): if letter.lower() == "a" or letter.lower() == "e" or letter.lower() == "i" or letter.lower() == "o" or letter.lower() == "u": return True else: return False sentenceTo...
0
2010-03-22T00:02:38Z
[ "python", "function" ]
My method is being recognized within my own program. Newbie mistake probably
2,489,273
<p>Here's my code:</p> <pre><code>sentenceToTranslate = raw_input("Please write in the sentence you want to translate: ") words = sentenceToTranslate.split(" ") for word in words: if isVowel(word[0]): print "TEST" def isVowel(letter): if letter.lower() == "a" or letter.lower() == "e" or letter.lower...
1
2010-03-21T23:47:19Z
2,490,056
<p>Here are a couple of other points to make for a newbie Pythoner:</p> <p>1) What is the point of writing this?</p> <pre><code>def isVowel(): if boolean-expression: return True else: return False </code></pre> <p>You already have the True/False-ness captured in the value of the boolean expre...
0
2010-03-22T04:41:35Z
[ "python", "function" ]
My method is being recognized within my own program. Newbie mistake probably
2,489,273
<p>Here's my code:</p> <pre><code>sentenceToTranslate = raw_input("Please write in the sentence you want to translate: ") words = sentenceToTranslate.split(" ") for word in words: if isVowel(word[0]): print "TEST" def isVowel(letter): if letter.lower() == "a" or letter.lower() == "e" or letter.lower...
1
2010-03-21T23:47:19Z
2,490,349
<p>At the risk of being downvoted again, here is some further insight into these "define before use" requirements.</p> <p>Note that the body of a function is not processed for the existence of symbols in until that function gets called. So you could write code like this:</p> <pre><code>def A(): # calls B - even ...
1
2010-03-22T06:28:05Z
[ "python", "function" ]
Will Python 3 ever catch on?
2,489,299
<p>I have been learning a bit of Python 2 and Python 3 and it seems like Python 2 is overall better than Python 3. So that's where my question comes in. Are there any good reasons to actually switch over to python 3?</p>
22
2010-03-21T23:54:47Z
2,489,312
<p>Python 3 is going to be the new standard going forward. </p> <p>As no major sweeping changes are planned to Python 3 anytime soon, more people will eventually be moving to it. So... although there are many Python 2 applications around now, eventually many of these applications will be migrated up. There is even at...
7
2010-03-22T00:01:08Z
[ "python", "python-3.x" ]
Will Python 3 ever catch on?
2,489,299
<p>I have been learning a bit of Python 2 and Python 3 and it seems like Python 2 is overall better than Python 3. So that's where my question comes in. Are there any good reasons to actually switch over to python 3?</p>
22
2010-03-21T23:54:47Z
2,489,395
<p>The main thing holding Python 3.x back right now is the lack of third-party libraries. I'll be converting my code as soon as SciPy gets ported.</p>
6
2010-03-22T00:31:13Z
[ "python", "python-3.x" ]
Will Python 3 ever catch on?
2,489,299
<p>I have been learning a bit of Python 2 and Python 3 and it seems like Python 2 is overall better than Python 3. So that's where my question comes in. Are there any good reasons to actually switch over to python 3?</p>
22
2010-03-21T23:54:47Z
2,489,398
<p>On the whole, and even in most details, Python3 is better than Python2.</p> <p>The only area where <strong>Python 3 is lagging</strong> is <strong>with regards to 3rd party libraries</strong>.<br> What makes Python great is not only its intrinsic characteristics as a language and its rather extensive standard libr...
31
2010-03-22T00:32:53Z
[ "python", "python-3.x" ]
Will Python 3 ever catch on?
2,489,299
<p>I have been learning a bit of Python 2 and Python 3 and it seems like Python 2 is overall better than Python 3. So that's where my question comes in. Are there any good reasons to actually switch over to python 3?</p>
22
2010-03-21T23:54:47Z
2,489,451
<p>As other answers mention, the only real (and crucial) current advantage of Python 2 over Python 3 is that the former already has a huge wealth of third-party extensions (and auxiliary tools, such as IDEs and the like), which the latter is only gradually moving towards. This is a situation that's gradually improving...
19
2010-03-22T00:56:13Z
[ "python", "python-3.x" ]
Will Python 3 ever catch on?
2,489,299
<p>I have been learning a bit of Python 2 and Python 3 and it seems like Python 2 is overall better than Python 3. So that's where my question comes in. Are there any good reasons to actually switch over to python 3?</p>
22
2010-03-21T23:54:47Z
2,490,394
<p>More iterators (in things like <code>dict.keys()</code>) will be a big boost for web applications.</p> <p>The core team will put more work into the new version. New books might focus on python 3 (see Dive into Python), but the real work is still done in python 2.</p> <p>Sooner or later, the big libraries (numpy, w...
0
2010-03-22T06:40:36Z
[ "python", "python-3.x" ]
Django objects.get()
2,489,311
<p>What if there is nothing that matches the get? Then it returns in an error.</p> <p>How do I say: get if there is, otherwise, return nothing.</p>
2
2010-03-22T00:00:59Z
2,489,316
<p>Use <code>try/except</code> or <a href="http://docs.djangoproject.com/en/dev/topics/http/shortcuts/#get-object-or-404" rel="nofollow"><code>get_object_or_404</code></a></p>
2
2010-03-22T00:02:53Z
[ "python", "django" ]
Django objects.get()
2,489,311
<p>What if there is nothing that matches the get? Then it returns in an error.</p> <p>How do I say: get if there is, otherwise, return nothing.</p>
2
2010-03-22T00:00:59Z
2,489,516
<p>You could create a shortcut like this (based on get_object_or_404):</p> <pre><code>from django.shortcuts import _get_queryset def get_object_or_none(klass, *args, **kwargs): queryset = _get_queryset(klass) try: return queryset.get(*args, **kwargs) except queryset.model.DoesNotExist: return None </cod...
6
2010-03-22T01:19:45Z
[ "python", "django" ]
Django objects.get()
2,489,311
<p>What if there is nothing that matches the get? Then it returns in an error.</p> <p>How do I say: get if there is, otherwise, return nothing.</p>
2
2010-03-22T00:00:59Z
18,449,018
<p>or you can try django annoying</p> <pre><code>pip install django-annoying </code></pre> <p>then import get_object_or_None(klass, *args, **kwargs)</p> <pre><code>from annoying.functions import get_object_or_None get_object_or_None(User, email='test@test.com') </code></pre> <p>Here is the project <a href="https://...
1
2013-08-26T16:49:32Z
[ "python", "django" ]
Issue reading packets from a pcap file. dpkt module. What gives?
2,489,336
<p>I am running the following test script to try to read packets from a sample <code>.pcap</code> file I have downloaded. It won't seem to run. I have all of the modules, but no examples seem to be running.</p> <pre><code>import socket import dpkt import sys pcapReader = dpkt.pcap.Reader(file("test1.pcap", "rb")) fo...
0
2010-03-22T00:09:10Z
2,492,711
<p>Well you seem to be short of assistance ... I don't know a pcap from a kneecap, so all I can do is try to help you help yourself. Suggestions:</p> <p>(1) Have you had a look at line 105 of pcap.py? I guess that the "KeyError: 138" means that it is trying to access a dictionary, but the dictionary doesn't have 138 (...
-1
2010-03-22T13:54:08Z
[ "python", "packet", "pcap", "packet-mangling" ]
Issue reading packets from a pcap file. dpkt module. What gives?
2,489,336
<p>I am running the following test script to try to read packets from a sample <code>.pcap</code> file I have downloaded. It won't seem to run. I have all of the modules, but no examples seem to be running.</p> <pre><code>import socket import dpkt import sys pcapReader = dpkt.pcap.Reader(file("test1.pcap", "rb")) fo...
0
2010-03-22T00:09:10Z
4,951,016
<p>Do <pre><code> pcapReader = dpkt.pcap.Reader(open('test1.pcap')) </pre></code></p> <p>Instead of: <pre><code> pcapReader = dpkt.pcap.Reader(file("test1.pcap", "rb")) </pre></code></p>
1
2011-02-09T21:59:01Z
[ "python", "packet", "pcap", "packet-mangling" ]
Issue reading packets from a pcap file. dpkt module. What gives?
2,489,336
<p>I am running the following test script to try to read packets from a sample <code>.pcap</code> file I have downloaded. It won't seem to run. I have all of the modules, but no examples seem to be running.</p> <pre><code>import socket import dpkt import sys pcapReader = dpkt.pcap.Reader(file("test1.pcap", "rb")) fo...
0
2010-03-22T00:09:10Z
10,653,050
<p>Line 105 of dpkt.pcap module is using the pcap file's link type to access a dictionary of link type mappings:</p> <pre><code> self.dloff = dltoff[self.__fh.linktype] </code></pre> <p>The dltoff dictionary is defined at the top of the module and it does not contain the key 138, hence the exception you are se...
1
2012-05-18T12:53:49Z
[ "python", "packet", "pcap", "packet-mangling" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
2,489,442
<pre><code>&gt;&gt;&gt; def f(x): ... x = x ** 0.5 ... return int(x) == x ... &gt;&gt;&gt; for i in range(10): ... print i, f(i) ... 0 True 1 True 2 False 3 False 4 True 5 False 6 False 7 False 8 False 9 True </code></pre>
-1
2010-03-22T00:52:39Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
2,489,474
<p>You could binary-search for the rounded square root. Square the result to see if it matches the original value.</p> <p>You're probably better off with FogleBirds answer - though beware, as floating point arithmetic is approximate, which can throw this approach off. You could in principle get a false positive from a...
1
2010-03-22T01:06:13Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
2,489,514
<p>I'm not sure of the Python, but you could do something like:</p> <pre><code>function isSquare(x) = x == floor(sqrt(x) + 0.5)^2 </code></pre> <p>That is, take a number, find the square root, round it to the nearest integer, square it, and test if it's the same as the original number. (<code>floor</code> and adding ...
-1
2010-03-22T01:18:08Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
2,489,519
<p>The problem with relying on any floating point computation (<code>math.sqrt(x)</code>, or <code>x**0.5</code>) is that you can't really be sure it's exact (for sufficiently large integers <code>x</code>, it won't be, and might even overflow). Fortunately (if one's in no hurry;-) there are many pure integer approache...
59
2010-03-22T01:20:45Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
2,489,540
<p>Use newton's method to quickly zero in on the nearest integer square root, then square it and see if it's your number. See <a href="http://en.wikipedia.org/wiki/Isqrt" rel="nofollow">isqrt</a>.</p>
15
2010-03-22T01:26:43Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
2,489,580
<p>Since you can never depend on exact comparisons when dealing with floating point computations (such as these ways of calculating the square root), a less error-prone implementation would be</p> <pre><code>import math def is_square(integer): root = math.sqrt(integer) if int(root + 0.5) ** 2 == integer: ...
7
2010-03-22T01:39:50Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
2,489,814
<ol> <li>Decide how long the number will be.</li> <li>take a delta 0.000000000000.......000001</li> <li>see if the (sqrt(x))^2 - x is greater / equal /smaller than delta and decide based on the delta error.</li> </ol>
0
2010-03-22T03:06:29Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
2,497,133
<p>This response doesn't pertain to your stated question, but to an implicit question I see in the code you posted, ie, "how to check if something is an integer?"</p> <p>The first answer you'll generally get to that question is "Don't!" And it's true that in Python, typechecking is usually not the right thing to do.</...
0
2010-03-23T02:17:13Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
5,750,280
<p>I'm new to Stack Overflow, and did a quick skim to find a solution. I just posted a slight variation on some of the examples above on another thread (<a href="http://stackoverflow.com/questions/1547196/finding-perfect-square/5750159#5750159">Finding perfect squares</a>) and thought I'd include a slight variation of ...
4
2011-04-21T22:04:49Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
12,128,407
<p>I have a slight improvement on the original solution using the Babylonian approach. Instead of using a set to store every previously generated approximation, only the most recent two approximations are stored and checked against the current approximation. This saves the huge amount of time wasted checking through...
-1
2012-08-26T07:32:57Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
37,197,669
<p>By my understanding, shouldn't this:</p> <pre><code>int(n**0.5)**2 == n </code></pre> <p>be adequate enough to check if n is a perfect square for a reasonably sized n?</p> <p>I agree with @Alex Martelli that if you want to check ridiculously large cases, you should use a Babylonian algorithm approach, but this is...
1
2016-05-12T21:25:03Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
37,204,305
<p>Maybe I be wrong, but this check is more simple</p> <pre><code>from math import sqrt def check_perfect_square(num): res = int(sqrt(num)) if num % res == 0: return True else: return False </code></pre>
-1
2016-05-13T07:52:29Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
37,565,406
<pre><code>import math if (math.sqrt(number)-int(math.sqrt(number))): print "it's not a perfect square" </code></pre> <p>A perfect square is a number that can be expressed as the product of two equal integers. <code>math.sqrt(number)</code> return a <code>float</code>. <code>int(math.sqrt(number))</code> casts the...
5
2016-06-01T09:58:33Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
37,956,770
<p>There is a VERY easy way to do this. Find how many factors the number has (including one and itself). If it has an odd amount of factors, it is a square.</p> <pre><code>def getFactors(n): '''Code for counting factors of n.''' result = 1 # not forgetting to count the number itself for i in range(1, n // ...
-1
2016-06-22T00:38:12Z
[ "python", "math", "square" ]
How could I check if a number is a perfect square?
2,489,435
<p>I'm pretty stumped with this one guys. I'm trying to toy with Python (as you can see with my previous questions) so I'd really love some help here. :P </p> <p>Speeds is of no concern for now, just working.</p>
35
2010-03-22T00:48:51Z
37,957,172
<p>This can be solved using <a href="https://docs.python.org/3/library/decimal.html" rel="nofollow">the <code>decimal</code> module</a> to get arbitrary precision square roots and easy checks for "exactness":</p> <pre><code>import math from decimal import localcontext, Context, Inexact def is_perfect_square(x): #...
0
2016-06-22T01:38:08Z
[ "python", "math", "square" ]
Python, datetime "time gap" percentage
2,489,513
<p>Assume I have these datatime variables:</p> <pre><code>start_time, end_time, current_time </code></pre> <p>I would like to know how much time left as percentage by checking <code>current_time</code> and the time delta between <code>start_time</code> and the <code>end_time</code></p> <p>IE: Assume the interval is ...
3
2010-03-22T01:17:59Z
2,489,527
<p>Here's a hackish workaround: compute the total number of microseconds between the two values by using the <code>days</code>, <code>seconds</code>, and <code>microseconds</code> fields. Then divide by the total number of microseconds in the interval.</p>
3
2010-03-22T01:23:08Z
[ "python", "datetime", "timedelta" ]