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
Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
I'm guessing that Python was still processing and wouldn't give you a ">>> " prompt until it was done. You asked it to calculate some pretty big numbers.

You also can't "undo" anything in the interactive interpreter; as I recall Ctrl-z is "end of file" on Windows and sends a job into the background on Linux. Ctrl-c is what you use to interrupt some operation (which is implemented as raising the KeyboardInterrupt exception).

Adbot
ADBOT LOVES YOU

OnceIWasAnOstrich
Jul 22, 2006

Bigup DJ posted:

What's happening?

You are making Python try to calculate a number with over 10^30 digits (plus a few somewhat smaller but still ridiculously large numbers). When I run your code I get a MemoryError on Windows.

peepsalot
Apr 24, 2007

        PEEP THIS...
           BITCH!

Bigup DJ posted:

So I'm learning Python and I was fiddling with list comprehensions:

Python code:
c = [x**2**(y-1)**2 for x in range(11) for y in range(11)]
When I tried that all I got was empty white lines until I restarted the shell. Even after I undid it with ctrl+z, none of my input returned anything but white lines. What's happening?

Because you are trying to calculate a list of retardedly large numbers. why are you doing a power of a power of a power

edit: oops didn't see this page

anyways the upper bound in that one would be 10^(2^(9^2))
The resulting number would have 2417851639229258349412353 decimal digits.

peepsalot fucked around with this message at 02:34 on Apr 4, 2013

Bigup DJ
Nov 8, 2012
Ok, thanks everyone! Does python have a way to test whether or not one equation equals another equation? eg. Something like [x**y = x**x if x = y] returning "True" or "False".

evensevenone
May 12, 2001
Glass is a solid.
What on earth does that even mean? An equation doesn't equal anything.

Malcolm XML
Aug 8, 2009

I always knew it would end like this.

Bigup DJ posted:

Ok, thanks everyone! Does python have a way to test whether or not one equation equals another equation? eg. Something like [x**y = x**x if x = y] returning "True" or "False".

You're looking for a proof assistant of some kind. Unfortunately, it's probably undecidable depending on what x and y are quantified over. Look into SMT solvers, z3 has python bindings.

But no, python doesn't have it "built in".

Bigup DJ
Nov 8, 2012
Thanks! Sorry if my questions aren't making sense, I'm new to complicated maths and coding in general.

Experto Crede
Aug 19, 2008

Keep on Truckin'
I'm trying to make a simple script which takes an IP address via raw_input, then splits it and then does something based on the last octet.

Thus far I've got:

code:
ip = raw_input('Enter IP address: ')
octet = ip.split('.');
print octet
which outputs the separated octets, but how do I run an if command which only uses a rule based on the last octet?

EDIT: Never mind, I sussed it. octet[-1] did it for me :D

Experto Crede fucked around with this message at 23:24 on Apr 4, 2013

BigRedDot
Mar 6, 2008

Hi guys, I just wanted to throw out that the videos of tutorials and talks from our latest PyData conference are up on vimeo now:

http://vimeo.com/pydata/videos

I think the keynotes are still missing, but everything else (about 30 videos, including my talk! but which one is it?) is available There are great talks and tutorials on tools: numpy, matplotlib, pandas, scikit-learn, as well as a variety of specific data analysis examples involving python. Hope you might find something interesting or useful!

ShoulderDaemon
Oct 9, 2003
support goon fund
Taco Defender

Experto Crede posted:

I'm trying to make a simple script which takes an IP address via raw_input, then splits it and then does something based on the last octet.

Thus far I've got:

code:
ip = raw_input('Enter IP address: ')
octet = ip.split('.');
print octet
which outputs the separated octets, but how do I run an if command which only uses a rule based on the last octet?

EDIT: Never mind, I sussed it. octet[-1] did it for me :D

When doing anything involving user input and IP addresses, please note that all of the following are perfectly legal IP addresses: (for the same host, even!)
  • 172.16.1.50
  • 172.16.306
  • 172.1048882
  • 2886730034
So if you're writing something which will be exposed to users other than yourself, please be nice and normalize your IP addresses before parsing them, because some of us really like being able to specify a gateway as 10.1 and get annoyed when perfectly legal IP addresses are rejected by stupid programs.

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

Better yet, just find an IP parsing module someone else wrote and import it.

OnceIWasAnOstrich
Jul 22, 2006

BeefofAges posted:

Better yet, just find an IP parsing module someone else wrote and import it.

Python code:
import socket

try:
    octet = socket.inet_ntoa(socket.inet_aton(ip_string)).split('.')[-1]
except socket.error:
    print "Invalid IP Address"

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

Wow, those are crappy function names.

QuarkJets
Sep 8, 2008

BeefofAges posted:

Wow, those are crappy function names.

It's pretty similar to the old atoi and atof functions that C has

karms
Jan 22, 2006

by Nyc_Tattoo
Yam Slacker

QuarkJets posted:

It's pretty similar to the old atoi and atof functions that C has

This is how php names its functions. Do you really want to draw that comparison?

Hed
Mar 31, 2004

Fun Shoe
Ipaddress was included in 3.3 and it is awesome. I haven't checked some of those address variants, though.

Risket
Apr 3, 2004
Lipstick Apathy
I'm in the process of learning Python to replace my work's batch file scripts with something more powerful.

I use the subprocess module for calling external programs and grabbing their stdout and stderr return codes. Some of the example code that I've found had things like this:

code:
from subprocess import call

import subprocess as sp
Why would you want to import only one part of subprocess, such as call or popen? Why not import the whole thing and just use those commands?

Also, why would you want to shorten subprocess? So you have less to type in very large scripts? Doesn't that get confusing when you have a 500 line script and you can't remember what the hell you shortened subprocess to?

Risket
Apr 3, 2004
Lipstick Apathy
Dammit, Edit not Quote

Dren
Jan 5, 2001

Pillbug

Risket posted:

I'm in the process of learning Python to replace my work's batch file scripts with something more powerful.

I use the subprocess module for calling external programs and grabbing their stdout and stderr return codes. Some of the example code that I've found had things like this:

code:
from subprocess import call

import subprocess as sp
Why would you want to import only one part of subprocess, such as call or popen? Why not import the whole thing and just use those commands?

Also, why would you want to shorten subprocess? So you have less to type in very large scripts? Doesn't that get confusing when you have a 500 line script and you can't remember what the hell you shortened subprocess to?

You might want to use the from statement to reduce the amount of typing or to prevent python from doing a lookup on the module every time it goes to access subprocess.call(). There actually is a performance penalty there.

In the interest of readability I lean toward "don't rename stuff on import and mostly don't use import ... from". It's easier for other people to know what your symbols are if you fully qualify them. If you feel that you must import ... from, consider doing it right above the code that needs the symbol instead of at the top of the file so that others don't have to look far to find the meaning of the symbol.

accipter
Sep 12, 2003

Risket posted:

I'm in the process of learning Python to replace my work's batch file scripts with something more powerful.

I use the subprocess module for calling external programs and grabbing their stdout and stderr return codes. Some of the example code that I've found had things like this:

code:
from subprocess import call

import subprocess as sp
Why would you want to import only one part of subprocess, such as call or popen? Why not import the whole thing and just use those commands?

Also, why would you want to shorten subprocess? So you have less to type in very large scripts? Doesn't that get confusing when you have a 500 line script and you can't remember what the hell you shortened subprocess to?

I usually like to import the complete module because that way I don't have to worry about potential naming conflicts, but sometimes it is nice to shorten names because they are long, and used frequently. For example, import matplotlib.pyplot as plt is a convention for importing the pyplot module of matplotlib. There is also import numpy as np. Here the shortening is used because there might be a number of numpy methods called on a single line.

emoji
Jun 4, 2004

Risket posted:

I'm in the process of learning Python to replace my work's batch file scripts with something more powerful.

I use the subprocess module for calling external programs and grabbing their stdout and stderr return codes. Some of the example code that I've found had things like this:

code:
from subprocess import call

import subprocess as sp
Why would you want to import only one part of subprocess, such as call or popen? Why not import the whole thing and just use those commands?

Also, why would you want to shorten subprocess? So you have less to type in very large scripts? Doesn't that get confusing when you have a 500 line script and you can't remember what the hell you shortened subprocess to?

read this http://bytebaker.com/2008/07/30/python-namespaces/

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES

Bigup DJ posted:

Thanks! Sorry if my questions aren't making sense, I'm new to complicated maths and coding in general.

Just a quick guess, but reading your questions, you may wanna look into whether your school has a site license for Mathematica. Mathematica is great for solving some hard questions, especially when you need a analytical solution. As with everything, don't trust the black box, and make sure you're getting the correct answer though.


accipter posted:

I usually like to import the complete module because that way I don't have to worry about potential naming conflicts, but sometimes it is nice to shorten names because they are long, and used frequently. For example, import matplotlib.pyplot as plt is a convention for importing the pyplot module of matplotlib. There is also import numpy as np. Here the shortening is used because there might be a number of numpy methods called on a single line.

Yup. I also use import subprocess as subp. Subp is non-standard though, and you should probably use sp if you're planning on giving the code to people outside your group. And I never forget what np, or plt, or sp/subp mean because I use them all the time. Yeah, when you shorten libraries you don't use as much, you can sometimes forget, but that's why you just quickly scroll to the top and find out.

Collections is a good example of a library I use occasionally that I will sometimes forget what I shorten it to.

While were on the topic, I get really frustrated at coding guides that use:
from blah import *
or whatever the analog is. (For example using namespace std in C++ beginner's guides.)

I used to do it when first learning python, and it's a colossal mistake. It bothers me when people encourage others to do it. Namespace pollution is bad enough, but when you don't even know what functions belong to what library, that's downright bad practice. It's the biggest reason why I am so against using pylab. It encourages people to forget what functions are in numpy and what are in pyplot. If you're using matplotlib on a regular basis, chances are the work you do will always need numpy. You'll NOT always need pyplot.

Risket
Apr 3, 2004
Lipstick Apathy

Dren posted:

You might want to use the from statement to reduce the amount of typing or to prevent python from doing a lookup on the module every time it goes to access subprocess.call(). There actually is a performance penalty there.
I'm showing my ignorance here, I didn't realize that the whole module was "brought in", I thought it just told the interpreter where to look for a particular command. Something to keep in mind for my project, as this script will be executed about 14829378 times a day due to the type of testing we're doing. Thanks!
Nice article, thanks!

Smarmy Coworker
May 10, 2008

by XyloJW
edit: again: just kidding it actually appears to be Twisted's fault

Real Question Mk 2:

I replaced their calendar example with something that just generates a page holding "SUCCESS".

If I try to generate a page whose name is something like "1 2 3 4" (and I know for a fact spaces are fine in URLs) it will give me the error:

quote:

No Such Resource

No such child resource.

what the heck?

Smarmy Coworker fucked around with this message at 17:04 on Apr 5, 2013

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
Well, that's obviously not your real code because you wrote print passed instead of print "passed". I really don't know what else it would be — just try having a path component without colons?

evensevenone
May 12, 2001
Glass is a solid.
When you have multiple nested modules "from" makes a lot more sense. I use qt so most of the modules are prefixed with Qt anyway and it's a lot easier to do from pyqt4 import QtCore, QtGui and not end up with constructions like pyqt4.QtCore.QThread everywhere.

I think the real advantage is that when you do it that way it only imports the submodules you asked for and not the whole library, but the interpreter might be smarter than that.

Smarmy Coworker
May 10, 2008

by XyloJW
edit:
For some dumbass reason http://localhost:8880/x works but http://localhost:8880/x/ doesn't.
Thanks Twisted :(



hooray :)

Smarmy Coworker fucked around with this message at 18:18 on Apr 5, 2013

Gaukler
Oct 9, 2012


ARACHNOTRON posted:

edit:
For some dumbass reason http://localhost:8880/x works but http://localhost:8880/x/ doesn't.
Thanks Twisted :(

That's pretty standard for web frameworks that use regex-based pattern routing, though I'm not sure how twisted does it. In Django you'd have to make the route something like '^/fart/?$' which would match with or without a trailing slash.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
twisted.web does not use regex routing, but it's really not the best thing in the world. Twisted is a great networking stack for other protocols, and it's pretty OK at being an HTTP client. It's really not the greatest web server out there. Try one of Pyramid or Flask?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
So, what it's doing is calling getChild('') on the resource that you returned. You can work-around the issue by special-casing the empty string in getChild, or calling self.putChild('', self) in your resource constructor.

EDIT: or just set isLeaf on the leaf resource. I'll contact the Twisted docs guys about this.

Gaukler
Oct 9, 2012


Suspicious Dish posted:

twisted.web does not use regex routing, but it's really not the best thing in the world. Twisted is a great networking stack for other protocols, and it's pretty OK at being an HTTP client. It's really not the greatest web server out there. Try one of Pyramid or Flask?

My unsolicited advice on these after trying them out myself:

Flask is pretty cool, though I haven't found a way to scale into larger things with it elegantly yet. If you have lots of routes, it can be a pain to find your views if you're using the decorators for routing. You'll also end up with circular module dependencies really quickly if you start breaking your app out into modules. There also doesn't seem to be a counterpart to tastypie/django-rest-framework if you want a full REST framework with auth, throttling, and pagination while still being able to define your own routes or customize your endpoints. Flask-Restless works great if you just want to expose SQLAlchemy models via REST, though, and has auth hooks as well as pagination and throttling. Simple REST views are easy to whip up with MethodViews.

Could never get into Pyramid, seemed to have all the cruft of Django without any of the batteries included. It's a bunch of interfaces and you do the wiring yourself, unless there was a page detailing all the standard 'give me a CRUD app with SQLAlchemy' recipes/plugins that I missed.

Smarmy Coworker
May 10, 2008

by XyloJW
Unfortunately we use Twisted at work for everything so I pretty much have to learn twisted.web and stick with it. For anything personal heck no I'm not using it.

Suspicious Dish posted:

So, what it's doing is calling getChild('') on the resource that you returned. You can work-around the issue by special-casing the empty string in getChild, or calling self.putChild('', self) in your resource constructor.

EDIT: or just set isLeaf on the leaf resource. I'll contact the Twisted docs guys about this.

I figured this out when I was trying to understand why my images weren't loading. /img/x was trying to get the x child resource from the other child resource and yeah whatever I just did putChild to set up a static file structure.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
You might be interested in klein, which hooks up Werkzeug routes to twisted.web. The twisted.web simple Site/Resource system isn't really the best thing in the world, but thankfully, it's easily swappable.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Gaukler posted:

Flask is pretty cool, though I haven't found a way to scale into larger things with it elegantly yet. If you have lots of routes, it can be a pain to find your views if you're using the decorators for routing. You'll also end up with circular module dependencies really quickly if you start breaking your app out into modules.
I prefer Rails' approach of a single routes file (with nothing but the routes), but I haven't found finding views in Flask to be a major issue. Is your module structure completely unrelated to your URL structure or something?

I haven't encountered any reason to want to import a Flask blueprint from another blueprint, so I'm not sure how you'd end up with circular dependencies other than putting way too much into them.

Smarmy Coworker
May 10, 2008

by XyloJW

Suspicious Dish posted:

You might be interested in klein, which hooks up Werkzeug routes to twisted.web. The twisted.web simple Site/Resource system isn't really the best thing in the world, but thankfully, it's easily swappable.

This just looks... so... good. Simple, clean. Wow.

I will look into using this :c00l:

Risket
Apr 3, 2004
Lipstick Apathy
I'm trying to capture and display the stdout from subprocess.popen(). This is on Python 3.3 using Windows 7.

Python code:
import subprocess
serial_number = input('Please enter the last 5 non-zero digits of the board being tested:  ')
child_process = subprocess.Popen('setdevice.exe /s /d1 /v0 /n%s /i' % serial_number,
stdout=subprocess.PIPE, 
stderr=subprocess.PIPE)
streamdata = child_process.communicate()[0]
rc = child_process.returncode

print ('The output of SetDevice is:', streamdata)
print ('The return code is:', rc)
The return code sent to rc gives back the expected value, however stream data is displayed like this (the serial number typed is 231):

quote:

The output of SetDevice is: b'Board ID 1\r\nBoard Variant 0\r\nBoardSerial 231\r\nHW Flags 0000'
When the SetDevice command is run from the command prompt the output is:

quote:

Board ID 1
Board Variant 0
BoardSerial 231
HW Flags 0000
Is there an easy way to format the ouput of streamdata? It doesn't have to be displayed in real time, I just need to see the output of the SetDevice command in something that's a little easier to read.

Gaukler
Oct 9, 2012


Plorkyeran posted:

I prefer Rails' approach of a single routes file (with nothing but the routes), but I haven't found finding views in Flask to be a major issue. Is your module structure completely unrelated to your URL structure or something?

I haven't encountered any reason to want to import a Flask blueprint from another blueprint, so I'm not sure how you'd end up with circular dependencies other than putting way too much into them.

You don't HAVE to use the decorator-based routing and could create a routes.py or urls.py (using app.add_url_rule()), which may help that. The circular imports/dependencies issue is mainly just "feels wrong", they even explicitly call out this scenario in the docs as not a bad thing (Bottom of this page), but it feels like there could be a cleaner solution.

Dren
Jan 5, 2001

Pillbug
Risket, http://docs.python.org/3/library/stdtypes.html#binaryseq

Risket
Apr 3, 2004
Lipstick Apathy
That was spot on, thank you.

Adbot
ADBOT LOVES YOU

Dominoes
Sep 20, 2007

Another CSV / memory vs drive write question. If I want to download a CSV from a url, is there a way I can put the file information directly into a variable in memory? I've tried many combinations using list(), read(), csv.reader() etc, but can't find a working solution that skips writing the csv to disk, and reading it.

Code like this works:
Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        with open('drive_file.csv', 'wb') as f:
            f.write(u.read())
        
        with open('drive_file.csv', 'r') as f:
            return list(csv.reader(f))
For example, this seems like it should do what I'm asking, but doesn't.

Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        return list(csv.reader(u.read()))

  • Locked thread