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
Introducing Python
19,654
<p>The company I used to work with has two developers working fulltime, and a handful of freelancers. They're in the process of hiring a new lead developer to try to bring order and management to the development.</p> <p>But, currently, one of the developers has seen the light of Django (the company has only developed ...
5
2008-08-21T11:48:03Z
19,715
<p>I recently introduced Python to my company, which does consulting work for the Post Office. I did this by waiting until there was a project for which I would be the only programmer, then getting permission to do this new project in Python. I then did another small project in Python with similarly impressive result...
13
2008-08-21T12:25:21Z
[ "php", "python" ]
Introducing Python
19,654
<p>The company I used to work with has two developers working fulltime, and a handful of freelancers. They're in the process of hiring a new lead developer to try to bring order and management to the development.</p> <p>But, currently, one of the developers has seen the light of Django (the company has only developed ...
5
2008-08-21T11:48:03Z
19,943
<p>I love Python and Django, and use both to develop the our core webapps.</p> <p>That said, it's hard to make a business case for switching at this point. Specifically:</p> <ul> <li>Any new platform is risky compared to staying with the tried and true</li> <li>You'll have the developer fragmentation you mentioned</l...
0
2008-08-21T13:47:40Z
[ "php", "python" ]
Introducing Python
19,654
<p>The company I used to work with has two developers working fulltime, and a handful of freelancers. They're in the process of hiring a new lead developer to try to bring order and management to the development.</p> <p>But, currently, one of the developers has seen the light of Django (the company has only developed ...
5
2008-08-21T11:48:03Z
19,968
<p>It's really all about schedules. To me the break should be with a specific project. If you decide your direction is Django then start new projects with that. Before you start a new project with a new language/framework, either make sure that you have scheduled time to get up to speed in this new direction, or get...
1
2008-08-21T13:57:41Z
[ "php", "python" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
20,065
<p>Use <code>optparse</code> which comes with the standard library. For example:</p> <pre><code>#!/usr/bin/env python import optparse def main(): p = optparse.OptionParser() p.add_option('--person', '-p', default="world") options, arguments = p.parse_args() print 'Hello %s' % options.person if __name__ == '_...
13
2008-08-21T14:25:19Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
20,069
<p>Pretty much everybody is using <a href="http://python.active-venture.com/lib/module-getopt.html" rel="nofollow">getopt</a></p> <p>Here is the example code for the doc :</p> <pre><code>import getopt, sys def main(): try: opts, args = getopt.getopt(sys.argv[1:], "ho:v", ["help", "output="]) except g...
14
2008-08-21T14:26:57Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
20,169
<p>Just in case you might need to, this may help if you need to <strong>grab</strong> unicode arguments on Win32 (2K, XP etc):</p> <pre><code> from ctypes import * def wmain(argc, argv): print argc for i in argv: print i return 0 def startup(): size = c_int() ptr = windll.shell32.CommandL...
4
2008-08-21T14:59:54Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
20,222
<p>I prefer optparse to getopt. It's very declarative: you tell it the names of the options and the effects they should have (e.g., setting a boolean field), and it hands you back a dictionary populated according to your specifications.</p> <p><a href="http://docs.python.org/lib/module-optparse.html" rel="nofollow">ht...
2
2008-08-21T15:22:20Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
26,910
<p><strong>This answer suggests <code>optparse</code> which is appropriate for older Python versions. For Python 2.7 and above, <code>argparse</code> replaces <code>optparse</code>. See <a href="http://stackoverflow.com/questions/3217673/why-use-argparse-rather-than-optparse">this answer</a> for more information.</stro...
85
2008-08-25T21:11:03Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
30,973
<p>I think the best way for larger projects is optparse, but if you are looking for an easy way, maybe <a href="http://werkzeug.pocoo.org/documentation/script" rel="nofollow">http://werkzeug.pocoo.org/documentation/script</a> is something for you.</p> <pre><code>from werkzeug import script # actions go here def actio...
2
2008-08-27T19:27:06Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
979,871
<p>The new hip way is <code>argparse</code> for <a href="http://argparse.googlecode.com/svn/trunk/doc/argparse-vs-optparse.html">these</a> reasons. argparse > optparse > getopt</p> <p><strong>update:</strong> As of py2.7 <a href="http://docs.python.org/library/argparse.html">argparse</a> is part of the standard libra...
32
2009-06-11T07:54:53Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
11,271,779
<p><a href="https://github.com/muromec/consoleargs" rel="nofollow">consoleargs</a> deserves to be mentioned here. It is very easy to use. Check it out:</p> <pre><code>from consoleargs import command @command def main(url, name=None): """ :param url: Remote URL :param name: File name """ print """Downloadin...
1
2012-06-30T05:43:02Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
16,377,263
<p>Since 2012 Python has a very easy, powerful and really <em>cool</em> module for argument parsing called <a href="https://github.com/docopt/docopt" rel="nofollow">docopt</a>. It works with Python 2.6 to 3.5 and needs no installation (just copy it). Here is an example taken from it's documentation: </p> <pre><code>""...
37
2013-05-04T17:51:38Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
27,069,329
<p>I prefer <a href="http://click.pocoo.org/" rel="nofollow">Click</a>. It abstracts managing options and allows "(...) creating beautiful command line interfaces in a composable way with as little code as necessary".</p> <p>Here's example usage:</p> <pre><code>import click @click.command() @click.option('--count', ...
5
2014-11-21T20:00:04Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
30,493,366
<p>Other answers do mention that <code>argparse</code> is the way to go for new Python, but do not give usage examples. For completeness, here is a short summary of how to use argparse:</p> <p><strong>1) Initialize</strong></p> <pre><code>import argparse # Instantiate the parser parser = argparse.ArgumentParser(desc...
27
2015-05-27T21:21:26Z
[ "python", "command-line", "command-line-arguments" ]
What's the best way to grab/parse command line arguments passed to a Python script?
20,063
<p>What's the <strong>easiest, tersest, and most flexible</strong> method or library for parsing Python command line arguments?</p>
84
2008-08-21T14:24:41Z
36,516,929
<p>Here's a method, not a library, which seems to work for me.</p> <p>The goals here are to be terse, each argument parsed by a single line, the args line up for readability, the code is simple and doesn't depend on any special modules (only os + sys), warns about missing or unknown arguments gracefully, use a simple ...
-1
2016-04-09T12:45:12Z
[ "python", "command-line", "command-line-arguments" ]
Find broken symlinks with Python
20,794
<p>If I call <code>os.stat()</code> on a broken <code>symlink</code>, python throws an <code>OSError</code> exception. This makes it useful for finding them. However, there are a few other reasons that <code>os.stat()</code> might throw a similar exception. Is there a more precise way of detecting broken <code>symlinks...
13
2008-08-21T19:00:52Z
20,843
<p>Can I mention testing for hardlinks without python? /bin/test has the FILE1 -ef FILE2 condition that is true when files share an inode.</p> <p>Therefore, something like <code>find . -type f -exec test \{} -ef /path/to/file \; -print</code> works for hard link testing to a specific file.</p> <p>Which brings me to r...
3
2008-08-21T19:13:46Z
[ "python", "linux", "symlink" ]
Find broken symlinks with Python
20,794
<p>If I call <code>os.stat()</code> on a broken <code>symlink</code>, python throws an <code>OSError</code> exception. This makes it useful for finding them. However, there are a few other reasons that <code>os.stat()</code> might throw a similar exception. Is there a more precise way of detecting broken <code>symlinks...
13
2008-08-21T19:00:52Z
20,845
<p>I'm not a python guy but it looks like os.readlink()? The logic I would use in perl is to use readlink() to find the target and the use stat() to test to see if the target exists.</p> <p>Edit: I banged out some perl that demos readlink. I believe perl's stat and readlink and python's os.stat() and os.readlink()ar...
1
2008-08-21T19:14:01Z
[ "python", "linux", "symlink" ]
Find broken symlinks with Python
20,794
<p>If I call <code>os.stat()</code> on a broken <code>symlink</code>, python throws an <code>OSError</code> exception. This makes it useful for finding them. However, there are a few other reasons that <code>os.stat()</code> might throw a similar exception. Is there a more precise way of detecting broken <code>symlinks...
13
2008-08-21T19:00:52Z
20,848
<p><a href="https://docs.python.org/2/library/os.html#os.lstat" rel="nofollow">os.lstat()</a> may be helpful. If lstat() succeeds and stat() fails, then it's probably a broken link.</p>
11
2008-08-21T19:15:33Z
[ "python", "linux", "symlink" ]
Find broken symlinks with Python
20,794
<p>If I call <code>os.stat()</code> on a broken <code>symlink</code>, python throws an <code>OSError</code> exception. This makes it useful for finding them. However, there are a few other reasons that <code>os.stat()</code> might throw a similar exception. Is there a more precise way of detecting broken <code>symlinks...
13
2008-08-21T19:00:52Z
20,859
<p><a href="http://docs.python.org/lib/module-os.path.html" rel="nofollow">os.path</a></p> <p>You may try using realpath() to get what the symlink points to, then trying to determine if it's a valid file using is file.</p> <p>(I'm not able to try that out at the moment, so you'll have to play around with it and see w...
2
2008-08-21T19:19:24Z
[ "python", "linux", "symlink" ]
Find broken symlinks with Python
20,794
<p>If I call <code>os.stat()</code> on a broken <code>symlink</code>, python throws an <code>OSError</code> exception. This makes it useful for finding them. However, there are a few other reasons that <code>os.stat()</code> might throw a similar exception. Is there a more precise way of detecting broken <code>symlinks...
13
2008-08-21T19:00:52Z
26,957
<p>A common Python saying is that it's easier to ask forgiveness than permission. While I'm not a fan of this statement in real life, it does apply in a lot of cases. Usually you want to avoid code that chains two system calls on the same file, because you never know what will happen to the file in between your two c...
18
2008-08-25T21:32:20Z
[ "python", "linux", "symlink" ]
Find broken symlinks with Python
20,794
<p>If I call <code>os.stat()</code> on a broken <code>symlink</code>, python throws an <code>OSError</code> exception. This makes it useful for finding them. However, there are a few other reasons that <code>os.stat()</code> might throw a similar exception. Is there a more precise way of detecting broken <code>symlinks...
13
2008-08-21T19:00:52Z
31,102,280
<p>This is not atomic but it works.</p> <p><code>os.path.islink(filename) and not os.path.exists(filename)</code></p> <p>Indeed by <a href="https://docs.python.org/2/library/os.path.html" rel="nofollow">RTFM</a> (reading the fantastic manual) we see</p> <blockquote> <p>os.path.exists(path)</p> <p>Return True...
4
2015-06-28T16:49:13Z
[ "python", "linux", "symlink" ]
updating an auto_now DateTimeField in a parent model w/ Django
20,927
<p>I've got two models: Message and Attachment. Each attachment is attached to a specific message, using a ForeignKey on the Attachment model. Both models have an auto_now DateTimeField called updated. I'm trying to make it so that when any attachment is saved, it also sets the updated field on the associated message t...
6
2008-08-21T19:41:57Z
20,983
<p>You would also need to then save the message. Then it that should work.</p>
5
2008-08-21T19:57:36Z
[ "python", "database", "django", "orm" ]
updating an auto_now DateTimeField in a parent model w/ Django
20,927
<p>I've got two models: Message and Attachment. Each attachment is attached to a specific message, using a ForeignKey on the Attachment model. Both models have an auto_now DateTimeField called updated. I'm trying to make it so that when any attachment is saved, it also sets the updated field on the associated message t...
6
2008-08-21T19:41:57Z
72,359
<p>DateTime fields with auto_now are automatically updated upon calling <code>save()</code>, so you do not need to update them manually. Django will do this work for you.</p>
1
2008-09-16T13:44:51Z
[ "python", "database", "django", "orm" ]
updating an auto_now DateTimeField in a parent model w/ Django
20,927
<p>I've got two models: Message and Attachment. Each attachment is attached to a specific message, using a ForeignKey on the Attachment model. Both models have an auto_now DateTimeField called updated. I'm trying to make it so that when any attachment is saved, it also sets the updated field on the associated message t...
6
2008-08-21T19:41:57Z
33,449,486
<p>Proper version to work is: (attention to last line <code>self.message.save()</code>)</p> <pre><code>class Message(models.Model): updated = models.DateTimeField(auto_now = True) ... class Attachment(models.Model): updated = models.DateTimeField(auto_now = True) message = models.ForeignKey(Message) ...
0
2015-10-31T08:11:30Z
[ "python", "database", "django", "orm" ]
Specifying a mySQL ENUM in a Django model
21,454
<p>How do I go about specifying and using an ENUM in a Django model?</p>
65
2008-08-21T23:48:08Z
21,468
<p>From the <a href="https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.Field.choices" rel="nofollow">Django documentation</a>:</p> <pre><code>MAYBECHOICE = ( ('y', 'Yes'), ('n', 'No'), ('u', 'Unknown'), ) </code></pre> <p>And you define a charfield in your model :</p> <pre><code>m...
75
2008-08-21T23:54:52Z
[ "python", "mysql", "django", "django-models", "enums" ]
Specifying a mySQL ENUM in a Django model
21,454
<p>How do I go about specifying and using an ENUM in a Django model?</p>
65
2008-08-21T23:48:08Z
33,932
<p>Using the <code>choices</code> parameter won't use the ENUM db type; it will just create a VARCHAR or INTEGER, depending on whether you use <code>choices</code> with a CharField or IntegerField. Generally, this is just fine. If it's important to you that the ENUM type is used at the database level, you have three ...
26
2008-08-29T03:57:14Z
[ "python", "mysql", "django", "django-models", "enums" ]
Specifying a mySQL ENUM in a Django model
21,454
<p>How do I go about specifying and using an ENUM in a Django model?</p>
65
2008-08-21T23:48:08Z
334,932
<p>If you really want to use your databases ENUM type:</p> <ol> <li>Use Django 1.x</li> <li>Recognize your application will only work on some databases.</li> <li>Puzzle through this documentation page:<a href="http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#howto-custom-model-fields">http://docs.django...
6
2008-12-02T18:21:16Z
[ "python", "mysql", "django", "django-models", "enums" ]
Specifying a mySQL ENUM in a Django model
21,454
<p>How do I go about specifying and using an ENUM in a Django model?</p>
65
2008-08-21T23:48:08Z
1,530,858
<pre><code>from django.db import models class EnumField(models.Field): """ A field class that maps to MySQL's ENUM type. Usage: class Card(models.Model): suit = EnumField(values=('Clubs', 'Diamonds', 'Spades', 'Hearts')) c = Card() c.suit = 'Clubs' c.save() """ def __init...
31
2009-10-07T10:47:46Z
[ "python", "mysql", "django", "django-models", "enums" ]
Specifying a mySQL ENUM in a Django model
21,454
<p>How do I go about specifying and using an ENUM in a Django model?</p>
65
2008-08-21T23:48:08Z
13,089,465
<p><a href="http://www.b-list.org/weblog/2007/nov/02/handle-choices-right-way/">http://www.b-list.org/weblog/2007/nov/02/handle-choices-right-way/</a></p> <blockquote> <pre><code>class Entry(models.Model): LIVE_STATUS = 1 DRAFT_STATUS = 2 HIDDEN_STATUS = 3 STATUS_CHOICES = ( (LIVE_STATUS, 'Live...
6
2012-10-26T15:03:50Z
[ "python", "mysql", "django", "django-models", "enums" ]
Specifying a mySQL ENUM in a Django model
21,454
<p>How do I go about specifying and using an ENUM in a Django model?</p>
65
2008-08-21T23:48:08Z
19,040,441
<p>Setting <code>choices</code> on the field will allow some validation on the Django end, but it <em>won't</em> define any form of an enumerated type on the database end.</p> <p>As others have mentioned, the solution is to specify <a href="https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#django.db.mod...
6
2013-09-26T23:38:42Z
[ "python", "mysql", "django", "django-models", "enums" ]
Specifying a mySQL ENUM in a Django model
21,454
<p>How do I go about specifying and using an ENUM in a Django model?</p>
65
2008-08-21T23:48:08Z
22,155,357
<p>A the top of your models.py file, add this line after you do your imports:</p> <pre><code> enum = lambda *l: [(s,_(s)) for s in l] </code></pre>
0
2014-03-03T19:18:56Z
[ "python", "mysql", "django", "django-models", "enums" ]
Specifying a mySQL ENUM in a Django model
21,454
<p>How do I go about specifying and using an ENUM in a Django model?</p>
65
2008-08-21T23:48:08Z
28,408,589
<p>There're currently two github projects based on adding these, though I've not looked into exactly how they're implemented:</p> <ol> <li><a href="https://github.com/5monkeys/django-enumfield" rel="nofollow">Django-EnumField</a>:<br> Provides an enumeration Django model field (using IntegerField) with reusable enums ...
3
2015-02-09T11:23:31Z
[ "python", "mysql", "django", "django-models", "enums" ]
Why Java and Python garbage collection methods are different?
21,934
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p> <p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p> <p>Why does Java choose this strategy and what is the benefit from thi...
35
2008-08-22T07:35:26Z
21,937
<p>I think the article "<a href="http://www.ibm.com/developerworks/java/library/j-jtp10283/">Java theory and practice: A brief history of garbage collection</a>" from IBM should help explain some of the questions you have.</p>
8
2008-08-22T07:40:12Z
[ "java", "python", "garbage-collection" ]
Why Java and Python garbage collection methods are different?
21,934
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p> <p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p> <p>Why does Java choose this strategy and what is the benefit from thi...
35
2008-08-22T07:35:26Z
21,964
<p>There are drawbacks of using reference counting. One of the most mentioned is circular references: Suppose A references B, B references C and C references B. If A were to drop its reference to B, both B and C will still have a reference count of 1 and won't be deleted with traditional reference counting. CPython (re...
40
2008-08-22T09:10:06Z
[ "java", "python", "garbage-collection" ]
Why Java and Python garbage collection methods are different?
21,934
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p> <p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p> <p>Why does Java choose this strategy and what is the benefit from thi...
35
2008-08-22T07:35:26Z
22,219
<p>Darren Thomas gives a good answer. However, one big difference between the Java and Python approaches is that with reference counting in the common case (no circular references) objects are cleaned up immediately rather than at some indeterminate later date.</p> <p>For example, I can write sloppy, non-portable cod...
11
2008-08-22T12:40:03Z
[ "java", "python", "garbage-collection" ]
Why Java and Python garbage collection methods are different?
21,934
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p> <p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p> <p>Why does Java choose this strategy and what is the benefit from thi...
35
2008-08-22T07:35:26Z
23,703
<p>The latest Sun Java VM actually have multiple GC algorithms which you can tweak. The Java VM specifications intentionally omitted specifying actual GC behaviour to allow different (and multiple) GC algorithms for different VMs.</p> <p>For example, for all the people who dislike the "stop-the-world" approach of the...
2
2008-08-22T22:58:36Z
[ "java", "python", "garbage-collection" ]
Why Java and Python garbage collection methods are different?
21,934
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p> <p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p> <p>Why does Java choose this strategy and what is the benefit from thi...
35
2008-08-22T07:35:26Z
46,780
<p>Reference counting is particularly difficult to do efficiently in a multi-threaded environment. I don't know how you'd even start to do it without getting into hardware assisted transactions or similar (currently) unusual atomic instructions.</p> <p>Reference counting is easy to implement. JVMs have had a lot of mo...
2
2008-09-05T20:03:19Z
[ "java", "python", "garbage-collection" ]
Why Java and Python garbage collection methods are different?
21,934
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p> <p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p> <p>Why does Java choose this strategy and what is the benefit from thi...
35
2008-08-22T07:35:26Z
74,327
<p>Garbage collection is faster (more time efficient) than reference counting, if you have enough memory. For example, a copying gc traverses the "live" objects and copies them to a new space, and can reclaim all the "dead" objects in one step by marking a whole memory region. This is very efficient, <em>if</em> you ha...
5
2008-09-16T16:38:56Z
[ "java", "python", "garbage-collection" ]
Why Java and Python garbage collection methods are different?
21,934
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p> <p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p> <p>Why does Java choose this strategy and what is the benefit from thi...
35
2008-08-22T07:35:26Z
196,487
<p>Actually reference counting and the strategies used by the Sun JVM are all different types of garbage collection algorithms.</p> <p>There are two broad approaches for tracking down dead objects: tracing and reference counting. In tracing the GC starts from the "roots" - things like stack references, and traces all ...
22
2008-10-13T01:42:10Z
[ "java", "python", "garbage-collection" ]
Why Java and Python garbage collection methods are different?
21,934
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p> <p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p> <p>Why does Java choose this strategy and what is the benefit from thi...
35
2008-08-22T07:35:26Z
1,604,520
<p>Late in the game, but I think one significant rationale for RC in python is its simplicity. See this <a href="http://mail.python.org/pipermail/python-list/2005-October/921938.html" rel="nofollow">email by Alex Martelli</a>, for example.</p> <p>(I could not find a link outside google cache, the email date from 13th ...
1
2009-10-22T01:11:46Z
[ "java", "python", "garbage-collection" ]
Why Java and Python garbage collection methods are different?
21,934
<p>Python uses the reference count method to handle object life time. So an object that has no more use will be immediately destroyed.</p> <p>But, in Java, the GC(garbage collector) destroys objects which are no longer used at a specific time.</p> <p>Why does Java choose this strategy and what is the benefit from thi...
35
2008-08-22T07:35:26Z
7,826,363
<p>One big disadvantage of Java's tracing GC is that from time to time it will "stop the world" and freeze the application for a relatively long time to do a full GC. If the heap is big and the the object tree complex, it will freeze for a few seconds. Also each full GC visits the whole object tree over and over again...
2
2011-10-19T18:40:07Z
[ "java", "python", "garbage-collection" ]
Why does this python date/time conversion seem wrong?
21,961
<pre><code>&gt;&gt;&gt; import time &gt;&gt;&gt; time.strptime("01-31-2009", "%m-%d-%Y") (2009, 1, 31, 0, 0, 0, 5, 31, -1) &gt;&gt;&gt; time.mktime((2009, 1, 31, 0, 0, 0, 5, 31, -1)) 1233378000.0 &gt;&gt;&gt; 60*60*24 # seconds in a day 86400 &gt;&gt;&gt; 1233378000.0 / 86400 14275.208333333334 </code></pre> <p>time.m...
3
2008-08-22T09:06:43Z
21,973
<pre><code>mktime(...) mktime(tuple) -&gt; floating point number Convert a time tuple in local time to seconds since the Epoch. </code></pre> <p>local time... fancy that.</p> <p>The time tuple:</p> <pre><code>The other representation is a tuple of 9 integers giving local time. The tuple items are: year (f...
1
2008-08-22T09:21:55Z
[ "python", "datetime" ]
Why does this python date/time conversion seem wrong?
21,961
<pre><code>&gt;&gt;&gt; import time &gt;&gt;&gt; time.strptime("01-31-2009", "%m-%d-%Y") (2009, 1, 31, 0, 0, 0, 5, 31, -1) &gt;&gt;&gt; time.mktime((2009, 1, 31, 0, 0, 0, 5, 31, -1)) 1233378000.0 &gt;&gt;&gt; 60*60*24 # seconds in a day 86400 &gt;&gt;&gt; 1233378000.0 / 86400 14275.208333333334 </code></pre> <p>time.m...
3
2008-08-22T09:06:43Z
21,974
<p>Interesting. I don't know, but I did try this:</p> <pre><code>&gt;&gt;&gt; now = time.mktime((2008, 8, 22, 11 ,17, -1, -1, -1, -1)) &gt;&gt;&gt; tomorrow = time.mktime((2008, 8, 23, 11 ,17, -1, -1, -1, -1)) &gt;&gt;&gt; tomorrow - now 86400.0 </code></pre> <p>which is what you expected. My guess? Maybe some time c...
0
2008-08-22T09:22:08Z
[ "python", "datetime" ]
Why does this python date/time conversion seem wrong?
21,961
<pre><code>&gt;&gt;&gt; import time &gt;&gt;&gt; time.strptime("01-31-2009", "%m-%d-%Y") (2009, 1, 31, 0, 0, 0, 5, 31, -1) &gt;&gt;&gt; time.mktime((2009, 1, 31, 0, 0, 0, 5, 31, -1)) 1233378000.0 &gt;&gt;&gt; 60*60*24 # seconds in a day 86400 &gt;&gt;&gt; 1233378000.0 / 86400 14275.208333333334 </code></pre> <p>time.m...
3
2008-08-22T09:06:43Z
21,975
<p>Short answer: Because of timezones.</p> <p>The Epoch is in UTC.</p> <p>For example, I'm on IST (Irish Stsandard Time) or GMT+1. time.mktime() is relative to my timezone, so on my system this refers to</p> <pre><code>&gt;&gt;&gt; time.mktime((2009, 1, 31, 0, 0, 0, 5, 31, -1)) 1233360000.0 </code></pre> <p>Because...
7
2008-08-22T09:24:25Z
[ "python", "datetime" ]
Why does this python date/time conversion seem wrong?
21,961
<pre><code>&gt;&gt;&gt; import time &gt;&gt;&gt; time.strptime("01-31-2009", "%m-%d-%Y") (2009, 1, 31, 0, 0, 0, 5, 31, -1) &gt;&gt;&gt; time.mktime((2009, 1, 31, 0, 0, 0, 5, 31, -1)) 1233378000.0 &gt;&gt;&gt; 60*60*24 # seconds in a day 86400 &gt;&gt;&gt; 1233378000.0 / 86400 14275.208333333334 </code></pre> <p>time.m...
3
2008-08-22T09:06:43Z
22,021
<p>Phil's answer really solved it, but I'll elaborate a little more. Since the epoch is in UTC, if I want to compare other times to the epoch, I need to interpret them as UTC as well.</p> <pre><code>&gt;&gt;&gt; calendar.timegm((2009, 1, 31, 0, 0, 0, 5, 31, -1)) 1233360000 &gt;&gt;&gt; 1233360000 / (60*60*24) 14275 </...
1
2008-08-22T10:12:25Z
[ "python", "datetime" ]
How do content discovery engines, like Zemanta and Open Calais work?
22,059
<p>I was wondering how as semantic service like Open Calais figures out the names of companies, or people, tech concepts, keywords, etc. from a piece of text. Is it because they have a large database that they match the text against? </p> <p>How would a service like Zemanta know what images to suggest to a piece of te...
4
2008-08-22T10:51:19Z
23,041
<p>Open Calais probably use language parsing technology and language statics to guess which words or phrases are Names, Places, Companies, etc. Then, it is just another step to do some kind of search for those entities and return meta data.</p> <p>Zementa probably does something similar, but matches the phrases agains...
0
2008-08-22T17:58:23Z
[ "python", "ruby", "semantics", "zemanta" ]
How do content discovery engines, like Zemanta and Open Calais work?
22,059
<p>I was wondering how as semantic service like Open Calais figures out the names of companies, or people, tech concepts, keywords, etc. from a piece of text. Is it because they have a large database that they match the text against? </p> <p>How would a service like Zemanta know what images to suggest to a piece of te...
4
2008-08-22T10:51:19Z
35,667
<p>I'm not familiar with the specific services listed, but the field of natural language processing has developed a number of techniques that enable this sort of information extraction from general text. As Sean stated, once you have candidate terms, it's not to difficult to search for those terms with some of the oth...
7
2008-08-30T03:56:57Z
[ "python", "ruby", "semantics", "zemanta" ]
How do content discovery engines, like Zemanta and Open Calais work?
22,059
<p>I was wondering how as semantic service like Open Calais figures out the names of companies, or people, tech concepts, keywords, etc. from a piece of text. Is it because they have a large database that they match the text against? </p> <p>How would a service like Zemanta know what images to suggest to a piece of te...
4
2008-08-22T10:51:19Z
821,663
<p>Michal Finkelstein from OpenCalais here.</p> <p>First, thanks for your interest. I'll reply here but I also encourage you to read more on OpenCalais forums; there's a lot of information there including - but not limited to: <a href="http://opencalais.com/tagging-information">http://opencalais.com/tagging-informatio...
9
2009-05-04T19:45:20Z
[ "python", "ruby", "semantics", "zemanta" ]
Unicode vs UTF-8 confusion in Python / Django?
22,149
<p>I stumbled over this passage in the <a href="http://www.djangoproject.com/documentation/tutorial01/">Django tutorial</a>:</p> <blockquote> <p>Django models have a default <strong>str</strong>() method that calls <strong>unicode</strong>() and converts the result to a UTF-8 bytestring. This means that unicode(p) w...
25
2008-08-22T12:01:53Z
22,157
<p>Python stores Unicode as UTF-16. str() will return the UTF-8 representation of the UTF-16 string.</p>
0
2008-08-22T12:03:47Z
[ "python", "django", "unicode" ]
Unicode vs UTF-8 confusion in Python / Django?
22,149
<p>I stumbled over this passage in the <a href="http://www.djangoproject.com/documentation/tutorial01/">Django tutorial</a>:</p> <blockquote> <p>Django models have a default <strong>str</strong>() method that calls <strong>unicode</strong>() and converts the result to a UTF-8 bytestring. This means that unicode(p) w...
25
2008-08-22T12:01:53Z
22,168
<p>From <a href="http://en.wikipedia.org/wiki/UTF-8" rel="nofollow">Wikipedia on UTF-8</a>: </p> <blockquote> UTF-8 (8-bit UCS/Unicode Transformation Format) is a <strong>variable-length character encoding for Unicode. It is able to represent any character in the Unicode standard</strong>, yet the initial encoding of ...
-1
2008-08-22T12:10:14Z
[ "python", "django", "unicode" ]
Unicode vs UTF-8 confusion in Python / Django?
22,149
<p>I stumbled over this passage in the <a href="http://www.djangoproject.com/documentation/tutorial01/">Django tutorial</a>:</p> <blockquote> <p>Django models have a default <strong>str</strong>() method that calls <strong>unicode</strong>() and converts the result to a UTF-8 bytestring. This means that unicode(p) w...
25
2008-08-22T12:01:53Z
22,206
<blockquote> <p>so what is a "Unicode string" in Python?</p> </blockquote> <p>Python 'knows' that your string is Unicode. Hence if you do regex on it, it will know which is character and which is not etc, which is really helpful. If you did a strlen it will also give the correct result. As an example if you did st...
-2
2008-08-22T12:32:04Z
[ "python", "django", "unicode" ]
Unicode vs UTF-8 confusion in Python / Django?
22,149
<p>I stumbled over this passage in the <a href="http://www.djangoproject.com/documentation/tutorial01/">Django tutorial</a>:</p> <blockquote> <p>Django models have a default <strong>str</strong>() method that calls <strong>unicode</strong>() and converts the result to a UTF-8 bytestring. This means that unicode(p) w...
25
2008-08-22T12:01:53Z
22,208
<p>Meanwhile, I did a refined research to verify what the internal representation in Python is, and also what its limits are. "<a href="http://www.cmlenz.net/archives/2008/07/the-truth-about-unicode-in-python">The Truth About Unicode In Python</a>" is a very good article which cites directly from the Python developers....
8
2008-08-22T12:32:59Z
[ "python", "django", "unicode" ]
Unicode vs UTF-8 confusion in Python / Django?
22,149
<p>I stumbled over this passage in the <a href="http://www.djangoproject.com/documentation/tutorial01/">Django tutorial</a>:</p> <blockquote> <p>Django models have a default <strong>str</strong>() method that calls <strong>unicode</strong>() and converts the result to a UTF-8 bytestring. This means that unicode(p) w...
25
2008-08-22T12:01:53Z
522,827
<blockquote> <p>what is a "Unicode string" in Python? Does that mean UCS-2?</p> </blockquote> <p>Unicode strings in Python are stored internally either as UCS-2 (fixed-length 16-bit representation, almost the same as UTF-16) or UCS-4/UTF-32 (fixed-length 32-bit representation). It's a compile-time option; on Windows...
45
2009-02-07T00:54:09Z
[ "python", "django", "unicode" ]
Format numbers to strings in Python
22,617
<p>I need to find out how to format numbers as strings. My code is here:</p> <pre><code>return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm </code></pre> <p>Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of ...
68
2008-08-22T15:10:46Z
22,624
<p>Formatting in Python is done via the <a href="http://docs.python.org/2/library/stdtypes.html#string-formatting">string formatting (<code>%</code>) operator</a>:</p> <pre><code>"%02d:%02d:%02d" % (hours, minutes, seconds) </code></pre> <p>/Edit: There's also <a href="https://docs.python.org/2/library/time.html#time...
103
2008-08-22T15:12:41Z
[ "python", "string-formatting" ]
Format numbers to strings in Python
22,617
<p>I need to find out how to format numbers as strings. My code is here:</p> <pre><code>return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm </code></pre> <p>Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of ...
68
2008-08-22T15:10:46Z
22,630
<p>You can use C style string formatting:</p> <pre><code>"%d:%d:d" % (hours, minutes, seconds) </code></pre> <p>See here, especially: <a href="https://web.archive.org/web/20120415173443/http://diveintopython3.ep.io/strings.html" rel="nofollow">https://web.archive.org/web/20120415173443/http://diveintopython3.ep.io/st...
3
2008-08-22T15:13:45Z
[ "python", "string-formatting" ]
Format numbers to strings in Python
22,617
<p>I need to find out how to format numbers as strings. My code is here:</p> <pre><code>return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm </code></pre> <p>Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of ...
68
2008-08-22T15:10:46Z
24,962
<p><em>str()</em> in python on an integer will <strong>not</strong> print any decimal places.</p> <p>If you have a float that you want to ignore the decimal part, then you can use str(int(floatValue)).</p> <p>Perhaps the following code will demonstrate:</p> <pre><code>&gt;&gt;&gt; str(5) '5' &gt;&gt;&gt; int(8.7) 8 ...
0
2008-08-24T10:32:49Z
[ "python", "string-formatting" ]
Format numbers to strings in Python
22,617
<p>I need to find out how to format numbers as strings. My code is here:</p> <pre><code>return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm </code></pre> <p>Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of ...
68
2008-08-22T15:10:46Z
2,550,630
<p>Starting in Python 2.6, there is an alternative: the <code>str.format()</code> method. Here are some examples using the existing string format operator (<code>%</code>):</p> <pre><code>&gt;&gt;&gt; "Name: %s, age: %d" % ('John', 35) 'Name: John, age: 35' &gt;&gt;&gt; i = 45 &gt;&gt;&gt; 'dec: %d/oct: %#o/hex: %#...
74
2010-03-31T05:13:20Z
[ "python", "string-formatting" ]
Format numbers to strings in Python
22,617
<p>I need to find out how to format numbers as strings. My code is here:</p> <pre><code>return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm </code></pre> <p>Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of ...
68
2008-08-22T15:10:46Z
3,336,493
<p>If you have a value that includes a decimal, but the decimal value is negligible (ie: 100.0) and try to int that, you will get an error. It seems silly, but calling float first fixes this.</p> <p>str(int(float([variable])))</p>
0
2010-07-26T15:46:31Z
[ "python", "string-formatting" ]
Format numbers to strings in Python
22,617
<p>I need to find out how to format numbers as strings. My code is here:</p> <pre><code>return str(hours)+":"+str(minutes)+":"+str(seconds)+" "+ampm </code></pre> <p>Hours and minutes are integers, and seconds is a float. the str() function will convert all of these numbers to the tenths (0.1) place. So instead of ...
68
2008-08-22T15:10:46Z
26,250,207
<p>You can use following to achieve desired functionality</p> <pre><code>"%d:%d:d" % (hours, minutes, seconds) </code></pre>
1
2014-10-08T06:06:44Z
[ "python", "string-formatting" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
22,682
<p>In Python 2, use urllib2 which comes with the standard library.</p> <pre><code>import urllib2 response = urllib2.urlopen('http://www.example.com/') html = response.read() </code></pre> <p>This is the most basic way to use the library, minus any error handling. You can also do more complex stuff such as changing h...
286
2008-08-22T15:38:22Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
22,721
<pre class="lang-py prettyprint-override"><code>import urllib2 mp3file = urllib2.urlopen("http://www.example.com/songs/mp3.mp3") with open('test.mp3','wb') as output: output.write(mp3file.read()) </code></pre> <p>The <code>wb</code> in <code>open('test.mp3','wb')</code> opens a file (and erases any existing file) in...
118
2008-08-22T15:58:17Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
22,723
<p>I agree with Corey, urllib2 is more complete than <a href="http://docs.python.org/lib/module-urllib.html">urllib</a> and should likely be the module used if you want to do more complex things, but to make the answers more complete, urllib is a simpler module if you want just the basics:</p> <pre><code>import urllib...
9
2008-08-22T15:58:52Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
22,776
<p>One more, using <a href="http://docs.python.org/2/library/urllib.html#urllib.urlretrieve"><code>urlretrieve</code></a>:</p> <pre><code>import urllib urllib.urlretrieve ("http://www.example.com/songs/mp3.mp3", "mp3.mp3") </code></pre> <p>(for Python 3+ use 'import urllib.request' and urllib.request.urlretrieve)</p>...
759
2008-08-22T16:19:09Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
10,744,565
<p>In 2012, use the <a href="http://docs.python-requests.org/en/latest/index.html">python requests library</a></p> <pre><code>&gt;&gt;&gt; import requests &gt;&gt;&gt; &gt;&gt;&gt; url = "http://download.thinkbroadband.com/10MB.zip" &gt;&gt;&gt; r = requests.get(url) &gt;&gt;&gt; print len(r.content) 10485760 </code>...
224
2012-05-24T20:08:29Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
16,518,224
<p>An improved version of the PabloG code for Python 2/3:</p> <pre><code>from __future__ import ( division, absolute_import, print_function, unicode_literals ) import sys, os, tempfile, logging if sys.version_info &gt;= (3,): import urllib.request as urllib2 import urllib.parse as urlparse else: import u...
14
2013-05-13T08:59:44Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
19,011,916
<p>Wrote <a href="https://pypi.python.org/pypi/wget">wget</a> library in pure Python just for this purpose. It is pumped up <code>urlretrieve</code> with <a href="https://bitbucket.org/techtonik/python-wget/src/6859e7b4aba37cef57616111be890fb59631bc4c/wget.py?at=default#cl-330">these features</a> as of version 2.0.</p>...
12
2013-09-25T17:55:16Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
19,352,848
<p>This may be a little late, But I saw pabloG's code and couldn't help adding a os.system('cls') to make it look AWESOME! Check it out : </p> <pre><code> import urllib2,os url = "http://download.thinkbroadband.com/10MB.zip" file_name = url.split('/')[-1] u = urllib2.urlopen(url) f = open(file_nam...
2
2013-10-14T02:54:01Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
20,218,209
<p>Source code can be:</p> <pre><code>import urllib sock = urllib.urlopen("http://diveintopython.org/") htmlSource = sock.read() sock.close() print htmlSource </code></pre>
1
2013-11-26T13:21:01Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
21,363,808
<p>You can get the progress feedback with urlretrieve as well:</p> <pre><code>def report(blocknr, blocksize, size): current = blocknr*blocksize sys.stdout.write("\r{0:.2f}%".format(100.0*current/size)) def downloadFile(url): print "\n",url fname = url.split('/')[-1] print fname urllib.urlretri...
6
2014-01-26T13:12:54Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
29,256,384
<p>use wget module:</p> <pre><code>import wget wget.download('url') </code></pre>
6
2015-03-25T12:59:25Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
31,857,152
<p>Here's how to do it in Python 3 using the standard library:</p> <ul> <li><p><a href="https://docs.python.org/3.0/library/urllib.request.html#urllib.request.urlopen"><code>urllib.request.urlopen</code></a></p> <pre><code>import urllib.request response = urllib.request.urlopen('http://www.example.com/') html = respo...
14
2015-08-06T13:30:31Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
33,816,517
<p>If you have wget installed, you can use parallel_sync.</p> <p>pip install parallel_sync</p> <pre><code>from parallel_sync import wget urls = ['http://something.png', 'http://somthing.tar.gz', 'http://somthing.zip'] wget.download('/tmp', urls) # or a single file: wget.download('/tmp', urls[0], filenames='x.zip', ex...
2
2015-11-19T23:48:06Z
[ "python" ]
How do I download a file over HTTP using Python?
22,676
<p>I have a small utility that I use to download a MP3 from a website on a schedule and then builds/updates a podcast XML file which I've obviously added to iTunes.</p> <p>The text processing that creates/updates the XML file is written in Python. I use wget inside a Windows <code>.bat</code> file to download the actu...
492
2008-08-22T15:34:13Z
39,573,536
<p>Following are the most commonly used calls for downloading files in python:</p> <ol> <li><p><code>urllib.urlretrieve ('url_to_file', file_name)</code></p></li> <li><p><code>urllib2.urlopen('url_to_file')</code></p></li> <li><p><code>requests.get(url)</code></p></li> <li><p><code>wget.download('url', file_name)</cod...
2
2016-09-19T12:45:10Z
[ "python" ]
What's the best way to duplicate fork() in windows?
23,397
<p>How do I implement some logic that will allow me to reproduce on Windows the functionality that I have on Linux with the <code>fork()</code> system call, using Python?</p> <p>I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking o...
20
2008-08-22T20:27:46Z
23,414
<p>Possibly a version of spawn() for python? <a href="http://en.wikipedia.org/wiki/Spawn_(operating_system)" rel="nofollow">http://en.wikipedia.org/wiki/Spawn_(operating_system)</a></p>
0
2008-08-22T20:34:00Z
[ "python", "windows", "process", "subprocess", "fork" ]
What's the best way to duplicate fork() in windows?
23,397
<p>How do I implement some logic that will allow me to reproduce on Windows the functionality that I have on Linux with the <code>fork()</code> system call, using Python?</p> <p>I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking o...
20
2008-08-22T20:27:46Z
23,422
<p>Have a look at the process management functions in the <a href="http://python.org/doc/2.5/lib/os-process.html" rel="nofollow">os module</a>. There are function for starting new processes in many different ways, both synchronously and asynchronously.</p> <p>I should note also that Windows doesn't provide functionali...
4
2008-08-22T20:38:18Z
[ "python", "windows", "process", "subprocess", "fork" ]
What's the best way to duplicate fork() in windows?
23,397
<p>How do I implement some logic that will allow me to reproduce on Windows the functionality that I have on Linux with the <code>fork()</code> system call, using Python?</p> <p>I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking o...
20
2008-08-22T20:27:46Z
23,510
<p>In addition to the process management code in the os module that Greg pointed out, you should also take a look at the threading module: <a href="https://docs.python.org/library/threading.html" rel="nofollow">https://docs.python.org/library/threading.html</a></p> <pre><code>from threading import Thread def separate...
3
2008-08-22T21:16:07Z
[ "python", "windows", "process", "subprocess", "fork" ]
What's the best way to duplicate fork() in windows?
23,397
<p>How do I implement some logic that will allow me to reproduce on Windows the functionality that I have on Linux with the <code>fork()</code> system call, using Python?</p> <p>I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking o...
20
2008-08-22T20:27:46Z
25,117
<p>You might also like using the processing module (<a href="http://pypi.python.org/pypi/processing" rel="nofollow">http://pypi.python.org/pypi/processing</a>). It has lot's of functionality for writing parallel systems with the same API as the threading module...</p>
2
2008-08-24T15:38:21Z
[ "python", "windows", "process", "subprocess", "fork" ]
What's the best way to duplicate fork() in windows?
23,397
<p>How do I implement some logic that will allow me to reproduce on Windows the functionality that I have on Linux with the <code>fork()</code> system call, using Python?</p> <p>I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking o...
20
2008-08-22T20:27:46Z
52,191
<p>The Threading example from Eli will run the thread, but not do any of the work after that line. </p> <p>I'm going to look into the processing module and the subprocess module. I think the com method I'm running needs to be in another process, not just in another thread.</p>
2
2008-09-09T15:37:18Z
[ "python", "windows", "process", "subprocess", "fork" ]
What's the best way to duplicate fork() in windows?
23,397
<p>How do I implement some logic that will allow me to reproduce on Windows the functionality that I have on Linux with the <code>fork()</code> system call, using Python?</p> <p>I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking o...
20
2008-08-22T20:27:46Z
170,387
<p><code>fork()</code> <em>has</em> in fact been duplicated in Windows, under <a href="https://en.wikipedia.org/wiki/Cygwin" rel="nofollow">Cygwin</a>, but it's pretty hairy.</p> <blockquote> <p>The fork call in Cygwin is particularly interesting because it does not map well on top of the Win32 API. This makes it ve...
10
2008-10-04T14:10:36Z
[ "python", "windows", "process", "subprocess", "fork" ]
What's the best way to duplicate fork() in windows?
23,397
<p>How do I implement some logic that will allow me to reproduce on Windows the functionality that I have on Linux with the <code>fork()</code> system call, using Python?</p> <p>I'm specifically trying to execute a method on the SAPI Com component, while continuing the other logic in the main thread without blocking o...
20
2008-08-22T20:27:46Z
6,020,663
<p>Use the python <a href="http://docs.python.org/library/multiprocessing.html">multiprocessing module</a> which will work everywhere.</p> <p>Here is a <a href="http://www.ibm.com/developerworks/aix/library/au-multiprocessing/">IBM developerWords article</a> that shows how to convert from os.fork() to the multiprocess...
15
2011-05-16T16:51:18Z
[ "python", "windows", "process", "subprocess", "fork" ]
How can I graph the Lines of Code history for git repo?
23,907
<p>Basically I want to get the number of lines-of-code in the repository after each commit.</p> <p>The only (really crappy) ways I have found is to use git filter-branch to run "wc -l *", and a script that run git reset --hard on each commit, then ran wc -l</p> <p>To make it a bit clearer, when the tool is run, it wo...
34
2008-08-23T03:00:46Z
24,190
<p>The first thing that jumps to mind is the possibility of your git history having a nonlinear history. You might have difficulty determining a sensible sequence of commits.</p> <p>Having said that, it seems like you could keep a log of commit ids and the corresponding lines of code in that commit. In a post-commit h...
4
2008-08-23T12:39:52Z
[ "python", "ruby", "git", "lines-of-code", "sloc" ]
How can I graph the Lines of Code history for git repo?
23,907
<p>Basically I want to get the number of lines-of-code in the repository after each commit.</p> <p>The only (really crappy) ways I have found is to use git filter-branch to run "wc -l *", and a script that run git reset --hard on each commit, then ran wc -l</p> <p>To make it a bit clearer, when the tool is run, it wo...
34
2008-08-23T03:00:46Z
35,664
<p>You may get both added and removed lines with git log, like:</p> <pre><code>git log --shortstat --reverse --pretty=oneline </code></pre> <p>From this, you can write a similar script to the one you did using this info. In python:</p> <pre><code>#!/usr/bin/python """ Display the per-commit size of the current git ...
22
2008-08-30T03:55:23Z
[ "python", "ruby", "git", "lines-of-code", "sloc" ]
How can I graph the Lines of Code history for git repo?
23,907
<p>Basically I want to get the number of lines-of-code in the repository after each commit.</p> <p>The only (really crappy) ways I have found is to use git filter-branch to run "wc -l *", and a script that run git reset --hard on each commit, then ran wc -l</p> <p>To make it a bit clearer, when the tool is run, it wo...
34
2008-08-23T03:00:46Z
2,854,506
<p>You might also consider <a href="http://gitstats.sourceforge.net/">gitstats</a>, which generates this graph as an html file. </p>
22
2010-05-18T04:09:01Z
[ "python", "ruby", "git", "lines-of-code", "sloc" ]
How can I graph the Lines of Code history for git repo?
23,907
<p>Basically I want to get the number of lines-of-code in the repository after each commit.</p> <p>The only (really crappy) ways I have found is to use git filter-branch to run "wc -l *", and a script that run git reset --hard on each commit, then ran wc -l</p> <p>To make it a bit clearer, when the tool is run, it wo...
34
2008-08-23T03:00:46Z
3,180,919
<p><a href="http://github.com/ITikhonov/git-loc">http://github.com/ITikhonov/git-loc</a> worked right out of the box for me.</p>
9
2010-07-05T16:18:32Z
[ "python", "ruby", "git", "lines-of-code", "sloc" ]
Python code generator for Visual Studio?
24,193
<p>I had an idea, if I add a python .py file to my C# project, and tag the file with a custom generator that would execute the python file, and treat the output as the result of the code generation, ie. put it into a C# file, that would allow me to do quite a lot of code generation as part of the build process.</p> <p...
3
2008-08-23T12:41:43Z
24,199
<p>I recall that in previous versions of VS, there was a way to add custom build steps to the build process. I used that a lot to do exactly the kind of automated code generation you describe.</p> <p>I imagine the custom build step feature is still there in 2008.</p>
2
2008-08-23T12:49:12Z
[ "python", "visual-studio-2008", "code-generation" ]
Python code generator for Visual Studio?
24,193
<p>I had an idea, if I add a python .py file to my C# project, and tag the file with a custom generator that would execute the python file, and treat the output as the result of the code generation, ie. put it into a C# file, that would allow me to do quite a lot of code generation as part of the build process.</p> <p...
3
2008-08-23T12:41:43Z
24,236
<p>I don't understand what you are trying to do here. Are you trying to execute a Python script that generates a C# file and then compile that with the project? Or are you trying to compile a Python script to C#?</p>
1
2008-08-23T13:42:39Z
[ "python", "visual-studio-2008", "code-generation" ]
Python code generator for Visual Studio?
24,193
<p>I had an idea, if I add a python .py file to my C# project, and tag the file with a custom generator that would execute the python file, and treat the output as the result of the code generation, ie. put it into a C# file, that would allow me to do quite a lot of code generation as part of the build process.</p> <p...
3
2008-08-23T12:41:43Z
24,248
<p>OK, I see. Well, as far as I know there isn't any code generator for Python. There is a good introduction on how to roll your own <a href="http://www.drewnoakes.com/snippets/WritingACustomCodeGeneratorToolForVisualStudio/" rel="nofollow">here</a>.</p> <p>Actually, that's quite an under-used part of the environment,...
1
2008-08-23T14:07:27Z
[ "python", "visual-studio-2008", "code-generation" ]
Python code generator for Visual Studio?
24,193
<p>I had an idea, if I add a python .py file to my C# project, and tag the file with a custom generator that would execute the python file, and treat the output as the result of the code generation, ie. put it into a C# file, that would allow me to do quite a lot of code generation as part of the build process.</p> <p...
3
2008-08-23T12:41:43Z
222,427
<p>I think <a href="http://nedbatchelder.com/code/cog/" rel="nofollow">Cog</a> does what you want.</p>
3
2008-10-21T16:03:42Z
[ "python", "visual-studio-2008", "code-generation" ]
Python code generator for Visual Studio?
24,193
<p>I had an idea, if I add a python .py file to my C# project, and tag the file with a custom generator that would execute the python file, and treat the output as the result of the code generation, ie. put it into a C# file, that would allow me to do quite a lot of code generation as part of the build process.</p> <p...
3
2008-08-23T12:41:43Z
223,387
<p>I dug through my old bookmarks (I love Del.icio.us!) and found this article: <a href="http://blogs.acceleration.net/ryan/articles/577.aspx" rel="nofollow">Code Generation with Python, Cog, and Nant</a>. Keep in mind that anything you can do in NAnt can probably be done in MSBuild as well. This should be enough to ge...
1
2008-10-21T20:30:33Z
[ "python", "visual-studio-2008", "code-generation" ]
How to capture Python interpreter's and/or CMD.EXE's output from a Python script?
24,931
<ol> <li>Is it possible to capture Python interpreter's output from a Python script?</li> <li>Is it possible to capture Windows CMD's output from a Python script?</li> </ol> <p>If so, which librar(y|ies) should I look into?</p>
7
2008-08-24T08:46:57Z
24,939
<p>In which context are you asking?</p> <p>Are you trying to capture the output from a program you start on the command line?</p> <p>if so, then this is how to execute it:</p> <pre><code>somescript.py | your-capture-program-here </code></pre> <p>and to read the output, just read from standard input.</p> <p>If, on ...
0
2008-08-24T09:05:20Z
[ "python", "windows", "cmd" ]
How to capture Python interpreter's and/or CMD.EXE's output from a Python script?
24,931
<ol> <li>Is it possible to capture Python interpreter's output from a Python script?</li> <li>Is it possible to capture Windows CMD's output from a Python script?</li> </ol> <p>If so, which librar(y|ies) should I look into?</p>
7
2008-08-24T08:46:57Z
24,942
<p>You want <a href="http://docs.python.org/lib/module-subprocess.html" rel="nofollow">subprocess</a>. Look specifically at Popen in 17.1.1 and communicate in 17.1.2.</p>
1
2008-08-24T09:27:54Z
[ "python", "windows", "cmd" ]
How to capture Python interpreter's and/or CMD.EXE's output from a Python script?
24,931
<ol> <li>Is it possible to capture Python interpreter's output from a Python script?</li> <li>Is it possible to capture Windows CMD's output from a Python script?</li> </ol> <p>If so, which librar(y|ies) should I look into?</p>
7
2008-08-24T08:46:57Z
24,949
<p>If you are talking about the python interpreter or CMD.exe that is the 'parent' of your script then no, it isn't possible. In every POSIX-like system (now you're running Windows, it seems, and that might have some quirk I don't know about, YMMV) each process has three streams, standard input, standard output and sta...
9
2008-08-24T09:39:08Z
[ "python", "windows", "cmd" ]
How to capture Python interpreter's and/or CMD.EXE's output from a Python script?
24,931
<ol> <li>Is it possible to capture Python interpreter's output from a Python script?</li> <li>Is it possible to capture Windows CMD's output from a Python script?</li> </ol> <p>If so, which librar(y|ies) should I look into?</p>
7
2008-08-24T08:46:57Z
29,169
<p>Actually, you definitely can, and it's beautiful, ugly, and crazy at the same time!</p> <p>You can replace sys.stdout and sys.stderr with StringIO objects that collect the output.</p> <p>Here's an example, save it as evil.py:</p> <pre><code>import sys import StringIO s = StringIO.StringIO() sys.stdout = s prin...
3
2008-08-26T23:16:16Z
[ "python", "windows", "cmd" ]
How to capture Python interpreter's and/or CMD.EXE's output from a Python script?
24,931
<ol> <li>Is it possible to capture Python interpreter's output from a Python script?</li> <li>Is it possible to capture Windows CMD's output from a Python script?</li> </ol> <p>If so, which librar(y|ies) should I look into?</p>
7
2008-08-24T08:46:57Z
3,378,965
<p>I think I can point you to a good answer for the first part of your question. </p> <blockquote> <p><em>1.&nbsp;&nbsp;Is it possible to capture Python interpreter's output from a Python script?</em></p> </blockquote> <p>The answer is "<em>yes</em>", and personally I like the following lifted from the examples i...
4
2010-07-31T16:40:30Z
[ "python", "windows", "cmd" ]
Binary buffer in Python
25,116
<p>In Python you can use <a href="https://docs.python.org/library/struct.html">StringIO</a> for a file-like buffer for character data. <a href="https://docs.python.org/library/mmap.html">Memory-mapped file</a> basically does similar thing for binary data, but it requires a file that is used as the basis. Does Python ha...
41
2008-08-24T15:37:50Z
25,123
<p>Look at the struct package: <a href="https://docs.python.org/library/struct.html" rel="nofollow">https://docs.python.org/library/struct.html</a>, it allows you to interpret strings as packed binary data.</p> <p>Not sure if this will completely answer your question but you can use struct.unpack() to convert binary d...
3
2008-08-24T15:50:54Z
[ "python", "binary", "io", "buffer" ]
Binary buffer in Python
25,116
<p>In Python you can use <a href="https://docs.python.org/library/struct.html">StringIO</a> for a file-like buffer for character data. <a href="https://docs.python.org/library/mmap.html">Memory-mapped file</a> basically does similar thing for binary data, but it requires a file that is used as the basis. Does Python ha...
41
2008-08-24T15:37:50Z
25,180
<p>As long as you don't try to put any unicode data into your <code>StringIO</code> and you are careful NOT to use <code>cStringIO</code> you should be fine.</p> <p>According to the <a href="https://docs.python.org/library/stringio.html" rel="nofollow">StringIO</a> documentation, as long as you keep to either unicode ...
24
2008-08-24T16:52:29Z
[ "python", "binary", "io", "buffer" ]
Binary buffer in Python
25,116
<p>In Python you can use <a href="https://docs.python.org/library/struct.html">StringIO</a> for a file-like buffer for character data. <a href="https://docs.python.org/library/mmap.html">Memory-mapped file</a> basically does similar thing for binary data, but it requires a file that is used as the basis. Does Python ha...
41
2008-08-24T15:37:50Z
7,357,938
<p>You are probably looking for <a href="http://docs.python.org/release/3.1.3/library/io.html#binary-i-o">io.BytesIO</a> class. It works exactly like StringIO except that it supports binary data:</p> <pre><code>from io import BytesIO bio = BytesIO(b"some initial binary data: \x00\x01") </code></pre> <p>StringIO will ...
55
2011-09-09T06:34:34Z
[ "python", "binary", "io", "buffer" ]
pyGame within a pyGTK application
25,661
<p>What is the best way to use PyGame (SDL) within a PyGTK application?</p> <p>I'm searching for a method that allows me to have a drawing area in the GTK window and at the same time being able to manage both GTK and SDL events.</p>
7
2008-08-25T04:36:48Z
25,761
<p>You may be interested in <a href="http://www.daa.com.au/pipermail/pygtk/2006-September/012888.html" rel="nofollow">this message thread</a>. Looks like they recommend against it.</p>
1
2008-08-25T08:08:46Z
[ "python", "gtk", "pygtk", "sdl", "pygame" ]
pyGame within a pyGTK application
25,661
<p>What is the best way to use PyGame (SDL) within a PyGTK application?</p> <p>I'm searching for a method that allows me to have a drawing area in the GTK window and at the same time being able to manage both GTK and SDL events.</p>
7
2008-08-25T04:36:48Z
28,935
<p>I've never attempted it myself, but hearing plenty about other people who've tried, it's not a road you want to go down.</p> <p>There is the alternative of putting the gui in pygame itself. There are plenty of gui toolkits built specifically for pygame that you could use. Most of them are rather unfinished, but the...
7
2008-08-26T19:36:24Z
[ "python", "gtk", "pygtk", "sdl", "pygame" ]
pyGame within a pyGTK application
25,661
<p>What is the best way to use PyGame (SDL) within a PyGTK application?</p> <p>I'm searching for a method that allows me to have a drawing area in the GTK window and at the same time being able to manage both GTK and SDL events.</p>
7
2008-08-25T04:36:48Z
31,950
<p>PyGame works much better when it can manage its own window, or even better, use the whole screen. GTK has flexible enough widgets to allow creation of a drawing area. </p> <p><a href="http://faq.pygtk.org/index.py?req=show&amp;file=faq23.042.htp" rel="nofollow">This page</a> may help, though, if you want to try it....
1
2008-08-28T10:04:23Z
[ "python", "gtk", "pygtk", "sdl", "pygame" ]