Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
epswing
Nov 4, 2003

Soiled Meat
On the threading module page, why is it
code:
self.value = value = self.value + 1
and not
code:
self.value = self.value + 1
?

Adbot
ADBOT LOVES YOU

epswing
Nov 4, 2003

Soiled Meat
Ok so maybe I'm going blind but I don't see any global variables. I see two class definitions, a counter object and a for loop.

epswing
Nov 4, 2003

Soiled Meat
Ok, let me ask this, if
code:
self.value = value = self.value + 1
is changed to
code:
self.value = self.value + 1
and
code:
return value
is changed to
code:
return self.value
am I looking at exactly the same program? Output seems to be the same. Why would they throw in this value variable?

epswing
Nov 4, 2003

Soiled Meat
Any nose users: I'm trying to import a module at runtime with __import__, I can make calls to the imported module (see m.hello()), but nose complains saying "OSError: No such file" when nose.runmodule() is executed.

code:
--- tests.py:
import sys, nose

module_path, sep, module_name = sys.argv[1].replace('/', '.').rpartition('.')
m = getattr(__import__(module_path, locals(), globals(), [module_name], -1), module_name)

def some_test():
	assert True == True
 
if __name__ == '__main__':
	print m.hello()
	nose.runmodule()

--- subdir/to/module.py:
def hello():
	return "Hello! The import worked!"

$ python tests.py subdir/to/module
Hello! The import worked!
Traceback (most recent call last):
  File "/var/lib/python-support/python2.5/nose/failure.py", line 29, in runTest
    raise self.exc_class(self.exc_val)
OSError: No such file /home/myname/subdir/to/module
e: No error is produced if nose.runmodule() is commented out.

epswing fucked around with this message at 21:41 on Mar 28, 2009

epswing
Nov 4, 2003

Soiled Meat

Habnabit posted:

Hi. :v:

Anyway, I stepped through nose.runmodule() with pdb to find out what was going on. The issue is that you have something in the argv; run the script passing no arguments and it'll work just fine. Nose is examining the argv and doing something with it.

Thanks for taking a look, I appreciate it. :)

Edit: Ahaha I just got the ":v:" The nose guys might say something, if anyone's interested http://code.google.com/p/python-nose/issues/detail?id=247

Of course there's something in argv, I want to load an argv-specified module at runtime, that's the point :colbert: So...what can I do here. Maybe clean out argv (if it's modifiable?) before it hits nose.runmodule()?

Thanks again.

epswing fucked around with this message at 16:18 on Mar 29, 2009

epswing
Nov 4, 2003

Soiled Meat
When I catch SomeException and I want to print the exception message, is it

code:
except SomeException, e:
    print "error: {0}".format(e)
or

code:
except SomeException as e:
    print "error: {0}".format(e)
I see some examples with "as" and some with "," (comma).

What's the difference?
When should I use which?
Also, should it be format(str(e)) rather than just format(e)?

epswing
Nov 4, 2003

Soiled Meat
What if I want to use the catch-all

code:
try:
    blah()
except:
    pass
    # even though an Exception was raised
    # I have no exception variable to work with

epswing
Nov 4, 2003

Soiled Meat
I'm sending mail and catching an SMTPException, I was asking about a bare except clause out of curiosity.

I would think it's useful when using some 3rd party library that could throw who knows what?

epswing
Nov 4, 2003

Soiled Meat

TasteMyHouse posted:

IMO you should avoid situations where you have to use spaces for alignment anyway.

"Avoid situations"?

I'm hoping you wouldn't prefer this

code:
urlpatterns = patterns('homesite.views',
    
    url(r'^$', 'home', name='homesite_home'),
    url(r'^contact/$', 'contact', name='homesite_contact'),
    url(r'^about/$', 'about', name='homesite_about'),
    url(r'^privacy/$', 'privacy', name='homesite_privacy'),
    url(r'^agreement/$', 'agreement', name='homesite_agreement'),
)
over this


code:
urlpatterns = patterns('homesite.views',
    
    url(r'^$',            'home',       name='homesite_home'),
    url(r'^contact/$',    'contact',    name='homesite_contact'),
    url(r'^about/$',      'about',      name='homesite_about'),
    url(r'^privacy/$',    'privacy',    name='homesite_privacy'),
    url(r'^agreement/$',  'agreement',  name='homesite_agreement'),
)

epswing
Nov 4, 2003

Soiled Meat

Janin posted:

The second is a maintenance nightmare, because every time someone adds a URL/view name/etc that's longer than the previous alignment, you have to shift everything over (and thus gently caress up the diff history).

By "maintenance nightmare" you mean "oops, I need to add/subtract a space from all these other lines"? Whew, what a nightmare :rolleyes:

I look at code far more often than diffs of code.

Edit

The more I think about this, the more :laffo: it is. Do you know what a maintenance nightmare is? Have you ever worked on code that was/is actually in production?

MY
DIFF
HISTORY
:negative:

epswing fucked around with this message at 05:52 on Sep 30, 2011

epswing
Nov 4, 2003

Soiled Meat
Is creating a zipfile with shutil.make_archive as useless as I think?

I wrote a simple backup script, zip a directory, and getting

code:
Traceback (most recent call last):
  File "backup.py", line 24, in <module>
    shutil.make_archive(zipfile, 'zip', source_dir)
  File "C:\Python27\lib\shutil.py", line 547, in make_archive
    filename = func(base_name, base_dir, **kwargs)
  File "C:\Python27\lib\shutil.py", line 454, in _make_zipfile
    zip.write(path, path)
  File "C:\Python27\lib\zipfile.py", line 1045, in write
    zinfo = ZipInfo(arcname, date_time)
  File "C:\Python27\lib\zipfile.py", line 295, in __init__
    raise ValueError('ZIP does not support timestamps before 1980')
ValueError: ZIP does not support timestamps before 1980
So shutil.make_archive will just fail without recourse if one day a file with a timestamp < 1980 is in the directory I'm zipping? Meaning any python file that uses shutil.make_archive is a nice little time bomb?

(I'm just going to use tar instead. I'm still mad though.)

epswing
Nov 4, 2003

Soiled Meat
I have a couple Django sites running on Linode, and they have excellent step by step tutorials for Django and practically everything else too. I'm no server admin but I managed to set up apache, postgres, and django without much trouble, just following their tutorials.

epswing
Nov 4, 2003

Soiled Meat

the posted:

And they wonder why no one uses Linux. Jesus Christ, I am going to throw this laptop out of the window.

I hope you do a few years of python development in linux, so you can look back at this statement and laugh at yourself.

It's perfectly sane, you're just unfamiliar with it and throwing a fit because it doesn't behave the way you think it should.

epswing
Nov 4, 2003

Soiled Meat
generator.py:
code:
import time

print 'one', time.ctime()
time.sleep(1)
print 'two', time.ctime()
time.sleep(1)
print 'thr', time.ctime()
time.sleep(1)
filter.py:
code:
import sys, time

for line in sys.stdin.readlines():
    print line.strip().upper(), time.ctime()
What I get:

pre:
C:\Users\Documents>python generator.py
one Tue Mar 31 10:39:51 2015
two Tue Mar 31 10:39:52 2015
thr Tue Mar 31 10:39:53 2015

C:\Users\Documents>python generator.py | python filter.py
ONE TUE MAR 31 10:40:02 2015 Tue Mar 31 10:40:05 2015
TWO TUE MAR 31 10:40:03 2015 Tue Mar 31 10:40:05 2015
THR TUE MAR 31 10:40:04 2015 Tue Mar 31 10:40:05 2015
Seems like filter.py is waiting for generator.py to terminate, and then reads all lines at once. I want filter.py to process lines as they arrive.

What I want:

pre:
C:\Users\Documents>python generator.py
one Tue Mar 31 10:39:51 2015
two Tue Mar 31 10:39:52 2015
thr Tue Mar 31 10:39:53 2015

C:\Users\Documents>python generator.py | python filter.py
ONE TUE MAR 31 10:40:02 2015 Tue Mar 31 10:40:02 2015
TWO TUE MAR 31 10:40:03 2015 Tue Mar 31 10:40:03 2015
THR TUE MAR 31 10:40:04 2015 Tue Mar 31 10:40:04 2015
What am I misunderstanding about stdin/stdout?

epswing
Nov 4, 2003

Soiled Meat

Hammerite posted:

Haven't tried it, don't know much about this sort of stuff, but is there some way of "flushing" the output of generator.py using the sys.stdout file-like object? It does have a flush() method.

I don't actually have control over generator.py, it's another program spitting out log files.

For the sake of argument, I added sys.stdout.flush() after every print call, and there was no change in behavior.

Adbot
ADBOT LOVES YOU

epswing
Nov 4, 2003

Soiled Meat
I seem to be getting "Forbidden 403, CSRF cookie not set" on IE, FIreFox, and Chrome, when running the local dev server. I'm getting the error when submitting the standard django admin site login form. I don't think it's likely that all of a sudden none of the browsers on my machine are accepting cookies. What else could cause this? I can see the csrfmiddlewaretoken hidden input in the html source.

Edit: Django 1.7.7

Edit: vvv Whoops! Thank you.

epswing fucked around with this message at 17:33 on May 28, 2015

  • Locked thread