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
Threep
Apr 1, 2006

It's kind of a long story.
Convince me to learn Python!

I somehow skipped by Python back when it was still in its youth and only recently decided that maybe I ought to give it a look.

Trouble is that I've now picked up Ruby and while it would've been easy to give up PHP or Perl in favour of Python it's a bit harder when I've already got a language I actually like and not just tolerate.

However, it seems to me that Python's maturity and much better support would be beneficial, so since this is the Python thread I'd like to hear someone with experience in both tell me why they've chosen Python.

I'm using Ruby both for Linux scripting/utilities and for web development.

Adbot
ADBOT LOVES YOU

Threep
Apr 1, 2006

It's kind of a long story.

ShizCakes posted:

I'm trying this now. I don't get the concept of how this works. (the hello world example just puts out "not found"?). God, I swear I am not this retarded usually - I just wish there was something that explained the underlying concepts simply, or a guide to switching from php, or something.
Explain what you've done/not done and what the actual error is, it's hard to help you when we don't know what isn't found.

Anyway, the idea is, with a traditional CGI script, and also with PHP, you call the script directly from the url - http://url/hello.py or http://url/showthread.php

The way web.py and django and similar frameworks are supposed to be used, your script gets called with every url requested from the server - http://url/hello or http://forums.somethingawful.com/thread/2675400-python-information-and-short-questions-megathread. If you need static files served from the server, you make those exceptions (like /static/ in the lighttpd config on the web.py site)

After that you're free to use Python concepts to decide how to deal with the URL - for example web.py uses its url rules to decide what class to will render the page. In the Hello World example on the web.py front page, http://url/hello is a valid url and everything else is a 404.

This is a good excuse to link this.

Threep fucked around with this message at 15:47 on Aug 7, 2009

Threep
Apr 1, 2006

It's kind of a long story.
code:
class index:
        def GET(self, path):
                return path
I've never actually used web.py myself (spot the error in my previous reply), but try this to see what it matches against.

Also, if it actually was /test.py you should do this:
code:
urls = ( r'/test\.py', 'index' )
since . is gonna match any character else.

Threep
Apr 1, 2006

It's kind of a long story.
I'm working on something that involves downloading a queue from a small number of different connections (say, 5 or so) to different files

What's the best way to go about this? I've looked at:

  • Python threads - because I doubt the GIL will hurt much when I'm mostly bandwidth-bound anyway.
  • multiprocessing module's queues - I'm not sure the best way to go about detecting a finished job though

And for the sake of learning - if I was downloading from 50/100 connections and/or to one file, what would the answer be?

Threep
Apr 1, 2006

It's kind of a long story.
I don't think I've ever seen "well formed" output from scientific software. I have a friend working on her Ph.D. in I think it's biochem and she's spent so much time writing Perl and whatnot to deal with the output that she's considering a career in programming now.

Most scientific software is, well, Dwarf Fortress to put it succinctly.

Threep
Apr 1, 2006

It's kind of a long story.
if foo/if not foo work.

I think it's because len(foo) returns 0 when it's empty and empty sequences evaluate to false. Someone correct me otherwise.

Ninja edit: Actually it probably isn't since you're not supposed to call len(foo) because it fetches the whole QuerySet but the logic was still sound

Threep
Apr 1, 2006

It's kind of a long story.
What's a good way to strip out XML tags but not their content? I'm trying to convert Tomboy/Gnote notes to another format (iPhone notes.db) and I need to remove the tags from the text itself.

I can do it with regexes of course, but that seems so crude and volatile. lxml has strip_tags, but it's broken in the version in the Ubuntu repos.

Threep
Apr 1, 2006

It's kind of a long story.

Avenging Dentist posted:

Then get a different version?
Would prefer a solution that works on Ubuntu without messing with custom packages (pip is also way out of date) since it would make support a pain otherwise.

I figured it was a common task and there'd be an easy solution.

Threep
Apr 1, 2006

It's kind of a long story.

Habnabit posted:

u''.join(your_lxml_etree.xpath('//text()'))
Fantastic, thanks.

Threep
Apr 1, 2006

It's kind of a long story.

Mido posted:

a game program that embeds python (I'm aware of how looked down upon this is)
You'll have to explain this because I've been looking into doing the same and I've heard nothing of this sort, not that I've looked. All I know is that Civ4 and EVE both do it.

quote:

Something I have going for me is Asynchronous resource loading (C/C++ threads), and one thing I'm going to need is the ability to load precompiled .pyc files into memory and have python ingest them at some point. What I'm not sure of, is how to feed python a buffer of data that is supposedly a .pyc

I haven't torn into the source code yet and my answers are definitely in there, I just figured there might be some magic Python function on the C side of things that will just magically work for eating precompiled scripts.
I'm pretty sure it's PyImport_ExecCodeModule called directly on the pyc data

Threep
Apr 1, 2006

It's kind of a long story.

Shaocaholica posted:

Given a volume label or drive letter in windows, is there any way to pull the model name of the physical drive?

Ex. 'FreeAgent Go'

I have a tool that copies files from the network to a portable hard drive and re-paths and re-names them and it also builds a log file of all the files. In that log file I want to put the actual model of the portable drive for tracking purposes. I've looked through the docs for pywin and none of the modules seem to have access to that data. I could grab the output of some command and parse it for the drive model but I don't know which native windows commands would even do that.
I haven't done any Win32 API from Python but this looks like a good starting point:
wmi module
Win32_DiskDrive

edit:
code:
import wmi
c = wmi.WMI()
for logical in c.Win32_LogicalDisk(DeviceID='H:'):
    for part in logical.associators(wmi_result_class='Win32_DiskPartition'):
        for phys in part.associators(wmi_result_class='Win32_DiskDrive'):
            print phys.Model
Now someone post the non-horrible way to do it.

Threep fucked around with this message at 22:45 on Jul 15, 2010

Threep
Apr 1, 2006

It's kind of a long story.

inveratulo posted:

Can anyone do it better?
Here's a horrible list comprehension:

code:
import os
import re
import shutil

[shutil.move(src, m.group(1)) for src, m in [(src, re.match('(\D+)-', src)) for src in os.listdir('.')] if m]
or more readable:
code:
for src, m in [(src, re.match('(\D+)-', src)) for src in os.listdir('.')]:
    if m:
        shutil.move(src, m.group(1))
You don't really need to compile regexes for efficiency as they're automatically cached.

Threep fucked around with this message at 15:45 on Aug 9, 2010

Threep
Apr 1, 2006

It's kind of a long story.
The default list is created when the function is parsed, but b only points to it when you don't specify another value. It's the whole "b is a reference to the list, not the list itself" deal.

This is a common way to solve it:

code:
def monkey(a, b=None):
    if b is None:
        b = []
    b.append(a)
    return b

Threep
Apr 1, 2006

It's kind of a long story.

Kolodny posted:

code:
>>> a = ['bbb','ccc','axx','xzz','xaa']
>>> i=0
>>> for s in a:
...   if s[0]!='x':
...     del a[i]
...   i = i+1
...
>>> a
['ccc', 'xzz', 'xaa']
Does anyone know why a[0]='ccc' ?
First off, use enumerate for a construct like that:
code:
>>> a = ['bbb','ccc','axx','xzz','xaa']
>>> for i, s in enumerate(a):
...   if s[0]!='x':
...     del a[i]
...
>>> a
['ccc', 'xzz', 'xaa']
Secondly and more importantly, don't delete from a list while iterating it. It makes weird things happen, as you saw. Here's a better solution:
code:
>>> a = ['bbb','ccc','axx','xzz','xaa']
>>> [s for s in a if s[0] != 'x']
['bbb', 'ccc', 'axx']

Threep
Apr 1, 2006

It's kind of a long story.

Sylink posted:

Has anyone here used mod_python?
I hate to sound like a broken record, but are you sure you should be using mod_python? I've yet to find a use case where it's preferable to mod_wsgi, and it's the source of so many problems.

Threep
Apr 1, 2006

It's kind of a long story.

Sylink posted:

I'm just playing with it.

I'll be abandoning python for this project anyway. The web applications are weird as gently caress compared to the standard php/mysql.
In that case, first of all, mod_python is a red herring. I made that mistake too, if you're coming from mod_php it sounds like the obvious choice.

Most Python web frameworks are MVC-based like the big PHP frameworks, but there's some that can be used like simple PHP pages, although I can't think of any names off hand.

The reason for that is that, trust me on this, if you spend the 10 mins necessary to learn the basics of Django. As mentioned above, you can use the built in webserver and get started with playing around with it immediately. And once you do want it installed, mod_wsgi is much easier to get running on Apache than mod_python.

I came from PHP and terrible web programming practices and Django seemed strange as gently caress, but it takes very little time working with it to realize how much better it is.

Threep
Apr 1, 2006

It's kind of a long story.

MelonWheels posted:

I'm playing around with a rogue-like game. I noticed that Nethack has a neat GUI versions so I want to see if I can change the interface into that. I'm still learning wxPython, but using GIFs with Tkinter makes it incredibly slow. Since wxWidgets is in C, I'm wondering if I can skip the Python with an extension to make things faster. I got the idea here: http://www.linuxjournal.com/article/3776 (end of third paragraph after the bullet list)
Have a look at PyGame instead. Making games in a GUI toolkit is a bad idea because they're rarely built for graphics performance, even if it's just a simple tile engine.

Threep
Apr 1, 2006

It's kind of a long story.

MelonWheels posted:

Yeah, I knew that cramming GIFs into Tkinter was a bad idea, but it was too appealing not to try. I prefer complicating things, which must be pretty selfish. I think PyGame looks too easy.
In that case, make your tile engine by feeding the gifs into PIL, then you just have one big image for Tkinter to render.

Threep
Apr 1, 2006

It's kind of a long story.

Haystack posted:

I really got a lot out of Think Python when I was starting out. Although, I preferred the HTML version of the book over the pdf.

You should be aware of Python's handful of gotchas. Pay particular attention to Number 3 and number 6. Number 6 WILL get you at some point.
You posted the same link twice.

Threep
Apr 1, 2006

It's kind of a long story.

Stabby McDamage posted:

1. What is the easiest way to turn a generator into a list? I was playing with itertools on the command line, and of course when I do "product('abc')", all I see is "<itertools.product object at 0x1e21140>". I've been wrapping these generators with "[x for x in <whatever>]", but I'm thinking there's got to be some shorthand for that.
You're probably going to slap yourself now: list(product('abc'))

Threep
Apr 1, 2006

It's kind of a long story.
I'm not sure exactly what you're after but maybe this'll put you in the right direction:

code:
>>> list1 = [1, 2]
>>> [float(x * 2) for x in list1]
[2.0, 4.0]
>>> [x * 1.5 for x in list1]
[1.5, 3.0]
>>> map(lambda(x): x * 1.5, list1)
[1.5, 3.0]
If what you're trying to do is call a function with every element in the list and return a list of the results, map is definitely what you're after:
code:
>>> list1 = [1, 2]
>>> def dostuff(x):
...   return x * 1.5
...
>>> map(dostuff, list1)
[1.5, 3.0]

Threep
Apr 1, 2006

It's kind of a long story.

TasteMyHouse posted:

number is now a 4 char long string. I could write low-level byte twiddly bullshit using ord() and bitwise shifts, ORs, etc to take these 4 bytes and create a single int out of them... but this is Python, not C! Surely there's an easier way?
I don't know if there's a better way but:
code:
struct.unpack('I', file.readline(4))

Adbot
ADBOT LOVES YOU

Threep
Apr 1, 2006

It's kind of a long story.

Socracheese posted:

There isn't any way to control the directory structure since I have to use a cgi file, right?

like, say this code is at somesite.com/cgi-bin/python/index.cgi, and I'd like to be able to access a config menu at somesite.com/cgi-bin/python/config. Am I correct in thinking I can't do that? Would I have to do something like somesite.com/cgi-bin/python/index.cgi?=config ? Edit: or somesite.com/cgi-bin/python/?=config ?
This is something that would have to be configured on the webserver. If you have access to mod_rewrite you can do it fancily.

If not the standard way to do it would be somesite.com/cgi-bin/python/index.cgi?config or the more correct and extensible somesite.com/cgi-bin/python/index.cgi?page=config

  • Locked thread