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
Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
SQLAlchemy

Adbot
ADBOT LOVES YOU

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
Argh! I glossed over it because I was explicitly not looking for a large ORM, but it seems like SQLAlchemy Core is exactly what I'm looking for. Thanks.

duck monster
Dec 15, 2004

Misogynist posted:

Is there a thin database abstraction layer for Python that basically just takes user-supplied connection strings using whatever database driver a user feels like without having to do a bunch of manual legwork to import and instantiate some arbitrary class name in my own code? I don't want an ORM.

I was always fond of ADOdb on PHP

So this is the python implementation:

http://phplens.com/lens/adodb/adodb-py-docs.htm

e: Whats your issue with using an ORM by the way? Performance, "impedence", or learning curve?

duck monster fucked around with this message at 09:10 on Nov 30, 2012

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

duck monster posted:

I was always fond of ADOdb on PHP

So this is the python implementation:

http://phplens.com/lens/adodb/adodb-py-docs.htm

e: Whats your issue with using an ORM by the way? Performance, "impedence", or learning curve?
This script literally needs to run one database query. But that one query might be run against any of a variety of databases the user runs, so being able to dump a connection string in a config file is a nice thing to have.

net work error
Feb 26, 2011

Python noob here. I had a project in mind and it requires doing some reading from a large xml file so the question I have is the following: is using the open() function the best option to open and search this file or are there other, smarter ways of doing this?

Movac
Oct 31, 2012

net work error posted:

Python noob here. I had a project in mind and it requires doing some reading from a large xml file so the question I have is the following: is using the open() function the best option to open and search this file or are there other, smarter ways of doing this?

The library you want to use is lxml. E.g.

code:
from lxml import etree
finances = etree.parse("finances.xml") # load and parse finances.xml, giving you an ElementTree object
payments = finances.findall("payment") # a list of every <payment> element in the document

Movac fucked around with this message at 18:15 on Dec 1, 2012

Modern Pragmatist
Aug 20, 2008
I would like to add coverage analysis to a library I'm working on and I'd love to add 'coverage' as a custom setup.py command (python setup.py coverage). I was wondering about the best way to do this.

I was thinking about adding a coverage subdirectory to the test directory. After googling around a little bit though, it seems that a lot of people include the code to perform the coverage analysis right in setup.py. Is this the best way to do this?

I'm thinking something like the following for my setup.py:

Python code:
class Coverage(setuptools.Command):
    description = "Run coverage analysis"
    user_options = []

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

    def run(self):
        import coverage
        cov = coverage.coverage(config_file='.coveragerc')
        cov.start()

        # Is it really a good idea to import the module? It seems necessary if you go this route
        import module_name
        tests = module_name.test.run_tests.MyTestLoader().loadTestsFromNames()

        t = TextTestRunner()
        t.run(tests)

        cov.stop()
        cov.save()
        cov.report()

setup(
        # All other options here
        cmdclass = {'coverage': Coverage}
        test_loader = 'module_name.run_tests:TestLoader',
        )

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
I need a tool for yet another project that, given a reply email containing a quoted part, will extract out the portion of the message that's just the reply. Does anyone know anything that would fit the bill?

Edit: Nice! https://github.com/zapier/email-reply-parser

Vulture Culture fucked around with this message at 17:14 on Dec 1, 2012

Lurchington
Jan 2, 2003

Forums Dragoon

Modern Pragmatist posted:

I would like to add coverage analysis to a library I'm working on and I'd love to add 'coverage' as a custom setup.py command (python setup.py coverage). I was wondering about the best way to do this.

If what you're asking is the best way to support coverage reports from unit tests, it's hard to beat just using nose with the bundled coverage plugin:

https://nose.readthedocs.org/en/latest/plugins/cover.html

simple as making nose runner the test suite and adding some items in a config file

Popete
Oct 6, 2009

This will make sure you don't suggest to the KDz
That he should grow greens instead of crushing on MCs

Grimey Drawer
So I'm trying to learn Python well making my own simple pong game using Pyglet, so far it's going alright but I'm hung up adding variables to a subclass in pyglet.

This is my subclass "player" which I want to extend Sprite.

Code posted:


pre:
import math
import pyglet
class player(pyglet.sprite.Sprite):

    def __init__(self, *args, **kwargs):
              [tab]super(player, self).__init__(*args, **kwargs)
        
              self.velocity_x, self.velocity_y = 0.0, 0.0


This is where I am calling it in a separate file which imports pyglet as well as player. The commented-out code is how I was originally calling a new Sprite object, above that is how I want to call a new player object which is a subset of Sprite.

Code posted:


player_1 = player(img=player1_image, x=20,y=400, batch=main_batch)
"""player_1 = pyglet.sprite.Sprite(img=player1_image, x=20,y=400, batch=main_batch)"""


This is the error I get when I try to run the main class which constructs player_1.

Error posted:


pre:
Traceback (most recent call last):
  File "C:\Users\Peter\Desktop\python programs\pong\version1\pong.py", line 29, in <module>
    player_1 = player(img=player1_image, x=20,y=400, batch=main_batch)
TypeError: 'module' object is not callable

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
You did "import player", not "from player import player"

Masa
Jun 20, 2003
Generic Newbie
What's the best way to copy text to the clipboard in a program using PyQt? I've tried using QApplication.clipboard, but it only works while the program that copied the text is still running.

Titan Coeus
Jul 30, 2007

check out my horn

Masa posted:

What's the best way to copy text to the clipboard in a program using PyQt? I've tried using QApplication.clipboard, but it only works while the program that copied the text is still running.

What system is this on? That sounds like an X11 problem.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Masa posted:

What's the best way to copy text to the clipboard in a program using PyQt? I've tried using QApplication.clipboard, but it only works while the program that copied the text is still running.

Most desktop environments have a clipboard manager.

Popete
Oct 6, 2009

This will make sure you don't suggest to the KDz
That he should grow greens instead of crushing on MCs

Grimey Drawer

Suspicious Dish posted:

You did "import player", not "from player import player"

Thank you!

FoiledAgain
May 6, 2007

What's a good way of dealing with phylogenetic trees in Python? Some initial googling makes the available tools seem very biologist-centered (for obvious reasons). I'm completely new to this area (phylogeny, not Python), and not a biologist at all. I have some data from a simulation on language change that I'd like to be able to display as a tree showing which languages descend from which. It looks like I need to make sure my data is formatted in a particular way, and then choose a Python module that will visualize that. Any suggestions?

Lysidas
Jul 26, 2002

John Diefenbaker is a madman who thinks he's John Diefenbaker.
Pillbug
I mostly use BioPython for its Seq and SeqIO packages, but I've heard very good things about its Phylo package.

net work error
Feb 26, 2011

Movac posted:

The library you want to use is lxml. E.g.


This is perfect for what I was looking for, thanks. It seems like it also works better than minidom which I was originally going to try out.

edit:tables

net work error fucked around with this message at 23:30 on Dec 3, 2012

OnceIWasAnOstrich
Jul 22, 2006

FoiledAgain posted:

What's a good way of dealing with phylogenetic trees in Python? Some initial googling makes the available tools seem very biologist-centered (for obvious reasons). I'm completely new to this area (phylogeny, not Python), and not a biologist at all. I have some data from a simulation on language change that I'd like to be able to display as a tree showing which languages descend from which. It looks like I need to make sure my data is formatted in a particular way, and then choose a Python module that will visualize that. Any suggestions?

Try ETE. It has has some nice visualization and manipulation tools. If the manipulation is too biology-centry try DendryPy.

KaiserBen
Aug 11, 2007
I'm pretty sure this is an insanely dumb question, but it's been irritating the hell out of me for the last hour. How do I take this input from a file:
08 20 30 54 60
23 45 42 42 50
. . .

and read it into a 2d array of integers?

I've gotten the data read from the file, into a string, but that's where it all goes wrong. I can read the file, and split it into the 2 digit chunks, but I can't seem to organize them into anything meaningful.

I've tried using the list class, with zero luck. Apparently integers don't belong in lists? I see that lists have no type for what's in them, which is throwing me for a loop. How do I get an ordered set of items of a known type (or at least such that I can treat them all as ints)? Preferably a 2D one. The array class seems to do 1D, but not 2D?

I'm new to python, obviously. I could solve this is 30sec in C, but python doesn't appear to have a functional array structure beyond 1D simple arrays, and I'm really used to languages where datatypes actually appear to mean something (eg C/C++, Java, etc).

Thanks in advance.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

KaiserBen posted:

I'm pretty sure this is an insanely dumb question, but it's been irritating the hell out of me for the last hour. How do I take this input from a file:
08 20 30 54 60
23 45 42 42 50
. . .

and read it into a 2d array of integers?

I've gotten the data read from the file, into a string, but that's where it all goes wrong. I can read the file, and split it into the 2 digit chunks, but I can't seem to organize them into anything meaningful.

I've tried using the list class, with zero luck. Apparently integers don't belong in lists? I see that lists have no type for what's in them, which is throwing me for a loop. How do I get an ordered set of items of a known type (or at least such that I can treat them all as ints)? Preferably a 2D one. The array class seems to do 1D, but not 2D?

I'm new to python, obviously. I could solve this is 30sec in C, but python doesn't appear to have a functional array structure beyond 1D simple arrays, and I'm really used to languages where datatypes actually appear to mean something (eg C/C++, Java, etc).

Thanks in advance.


Python code:
>>> data = "08 20 30 54 60\n23 45 42 42 50"
>>> data2 = [x.split() for x in data.split("\n")]
>>> data3 = [[int(x) for x in y] for y in data2]
>>> print data3
[[8, 20, 30, 54, 60], [23, 45, 42, 42, 50]]
>>> data3[1][2]
42
I'm lazy. Is that good enough? Do you understand what happened?

Reformed Pissboy
Nov 6, 2003

To elaborate slightly, lists can contain lists; you can make an AxB 2D array like so (one of many many ways, but this will do):

Python code:
>>> A = 7
>>> B = 5
>>> matrix = []
>>> for r in range(A):
...     row = []
...     for c in range(B):
...             row.append(r*c) # just inserting some int
...     matrix.append(row) # inserting a list!
...
>>> import pprint
>>> pprint.pprint(matrix)
[[0, 0, 0, 0, 0],
 [0, 1, 2, 3, 4],
 [0, 2, 4, 6, 8],
 [0, 3, 6, 9, 12],
 [0, 4, 8, 12, 16],
 [0, 5, 10, 15, 20],
 [0, 6, 12, 18, 24]]
If you're doing scientific or otherwise processor-intense number crunching and you'd like some array classes more similar to how they behave in C (enforced typing, constant sizes, etc), check out NumPy.

KaiserBen
Aug 11, 2007

Thermopyle posted:

Python code:
>>> data = "08 20 30 54 60\n23 45 42 42 50"
>>> data2 = [x.split() for x in data.split("\n")]
>>> data3 = [[int(x) for x in y] for y in data2]
>>> print data3
[[8, 20, 30, 54, 60], [23, 45, 42, 42, 50]]
>>> data3[1][2]
42
I'm lazy. Is that good enough? Do you understand what happened?

Awesome. That works perfectly.

I think I understand, and I think I found why it wasn't working. I had a mistake in my list indices, they were like: grid[x[y]], instead of [x][y]so it was looking for the int to be a list. I swear, I spend more time chasing simple typos than anything else. Is there a good python IDE to spot little poo poo like this? I'm using IDLE right now, and I can't say I'm terribly thrilled.

I ended up with the following:
Python code:
   tmp = tmp.rstrip("\n")
   grid[i] = [int(x) for x in tmp.split(" ")]

to do the removal of the "\n" and the splitting. I'm still not 100% on how lists work (more how they can contain anything and there's no type checking until you try to use a string as an int, and more importantly, why that's a "feature").

VV I figured there had to be a way to do it, but I just couldn't find it. I may check out NumPy, I'd love to have a proper array class (type checking and all).

Thanks!

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

KaiserBen posted:

I swear, I spend more time chasing simple typos than anything else. Is there a good python IDE to spot little poo poo like this? I'm using IDLE right now, and I can't say I'm terribly thrilled.

Everyone is liking PyCharm right now. Some other options include Sublime Text with the lint package or Komodo Edit.


KaiserBen posted:

I'm still not 100% on how lists work (more how they can contain anything and there's no type checking until you try to use a string as an int, and more importantly, why that's a "feature").

In Python you ask for forgiveness rather than permission. Because Python uses duck typing, you just try to do something with an object and if it supports the relevant methods it'll work. If it doesn't work you catch the exception (aka ask for forgiveness).

http://en.wikipedia.org/wiki/Duck_typing#In_Python

Actually just read the whole duck typing article there and see if that helps you out. Like everything there are pros and cons.

Bunny Cuddlin
Dec 12, 2004
Sublime Text has the benefit of being free (nag screen every 50 saves) and having a decent plugin framework. It does NOT, however, have passable vi/m emulation. It exists but it was clearly not written by someone who uses vim because it's not really usable and to get the ex commands you have to download another package.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

Bunny Cuddlin posted:

Sublime Text has the benefit of being free (nag screen every 50 saves) and having a decent plugin framework. It does NOT, however, have passable vi/m emulation. It exists but it was clearly not written by someone who uses vim because it's not really usable and to get the ex commands you have to download another package.

This is pretty much why I've never ended up moving on from Vim. It's got something for every language, I can use it absolutely anywhere, and I never have to worry about the quality of it's vim emulation. :)

I've tried other editors, but it's never lasted. I just like the way Vim works.

BigRedDot
Mar 6, 2008

I've used pretty much nothing but gvim for 15+ years, and I switched to SublimeText pretty much overnight. No, the vim emulation is not perfect but it is much, much better than anything else (ever tried "vim mode" in Eclipse?) It still needs some work, I hope they add more visual mode support, but ST2 is my new rock.

The Insect Court
Nov 22, 2012

by FactsAreUseless
Anywhere I can get a reasonably up to date list of the pros/cons of the various Python web frameworks? I did some work in Django like two years back, but nothing since then so my knowledge of the state of the various projects is out of date. Optimally I'd like something with the extensibility of Django without all of the cruft. Right now, I'm glancing at web2py but there doesn't seem to be much of a community around it.

The Insect Court fucked around with this message at 10:05 on Dec 4, 2012

The Gripper
Sep 14, 2004
i am winner
For Django without the cruft you're probably best off looking into basic frameworks w/ templating, flask likely being the best candidate followed by Pyramid and after that I don't know what is still current/maintained.

What features do you need for your application? The Python Wiki has a listing that is updated for whatever the latest official release of each framework is, though you'll have to look outside the full-stack listing to get a good idea of what's available.

geera
May 20, 2003

The Insect Court posted:

Anywhere I can get a reasonably up to date list of the pros/cons of the various Python web frameworks? I did some work in Django like two years back, but nothing since then so my knowledge of the state of the various projects is out of date. Optimally I'd like something with the extensibility of Django without all of the cruft. Right now, I'm glancing at web2py but there doesn't seem to be much of a community around it.
Flask is probably what you want. I've messed around with it on a side project or two, and it's pretty nice. There's a good community and library of extensions built up around it also.

Thern
Aug 12, 2006

Say Hello To My Little Friend
As somebody who just recently starting use Sublime Text, I'm really digging it. I'm still not sure if there is a way to get debugging and running unit tests from with the app. So far I still rely on going to the console for that.

Still I've been eyeing PyCharm. If only it wasn't so expensive.

Bunny Cuddlin
Dec 12, 2004

BigRedDot posted:

I've used pretty much nothing but gvim for 15+ years, and I switched to SublimeText pretty much overnight. No, the vim emulation is not perfect but it is much, much better than anything else (ever tried "vim mode" in Eclipse?) It still needs some work, I hope they add more visual mode support, but ST2 is my new rock.

I use sublime text but I turned Vintage off. I couldn't stand doing things like v/(<enter> and ending up with a single letter selected 20 characters away from the cursor or something.

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.
Does logging call flush() on registered log handlers when a program exits, or is that something that needs to be done manually in the application?

BeefofAges
Jun 5, 2004

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

At work I've been using PyDev, and while it's not as nice as PyCharm, it's still pretty good. I can't be the only one who's far too lazy to learn how to use vim.

The Gripper
Sep 14, 2004
i am winner
I really like vim, but I don't think I could use it for large projects locally when I could just have ST2/PyCharm there. I don't develop locally and copy code over if I'm developing for a server though, I'll always use vim there.

Vim really shines when you want an environment you can use anywhere, so I have mine set up for a bunch of different languages with a few handy add-ons and just shell in when I'm on my netbook or traveling.

The autocomplete isn't bad in it either, with the right plugins, though a lot of people picking vim up for the first time tend to miss that it even exists since Ctrl+n/i isn't an obvious shortcut for it (I use supertab so just hitting tab cycles autocomplete and arrow keys select).



(screenshot reminds me I need to fix coloring because for some reason it doesn't color method/function calls like it does with perl :psyduck:).

Also I guess this is a good time to ask, what plugins do you guys use for Python in vim? The autocomplete is *passable* the way I'm using it now but as in the screenshot it brings up a bunch of other dumb stuff like "acted" and "are" when autocompleting list.a<tab> (no idea where they're from), is there anything better?

Civil Twilight
Apr 2, 2011

The Gripper posted:

Also I guess this is a good time to ask, what plugins do you guys use for Python in vim? The autocomplete is *passable* the way I'm using it now but as in the screenshot it brings up a bunch of other dumb stuff like "acted" and "are" when autocompleting list.a<tab> (no idea where they're from), is there anything better?

I don't use the completion stuff a lot, but neocomplcache seems to do a pretty good job of it. Some others I use a lot (not python-specific) include syntastic, CtrlP, and vim-surround.

Edit: oh, and vundle to manage the above.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

The Gripper posted:

Also I guess this is a good time to ask, what plugins do you guys use for Python in vim? The autocomplete is *passable* the way I'm using it now but as in the screenshot it brings up a bunch of other dumb stuff like "acted" and "are" when autocompleting list.a<tab> (no idea where they're from), is there anything better?

For Python, there's ropevim. I don't know if anybody has hooked it up to vim like I hooked it up to emacs, but it's still pretty cool.

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Suspicious Dish posted:

For Python, there's ropevim. I don't know if anybody has hooked it up to vim like I hooked it up to emacs, but it's still pretty cool.

This reminds me that I installed the SublimeRope package for ST2 yesterday and it works real nice in conjunction with SublimeLint.

Civil Twilight
Apr 2, 2011

Also python-mode for vim which includes a bunch of stuff.

Adbot
ADBOT LOVES YOU

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES
Hey guys, I've been p busy lately with non-python stuff, so I've been kinda away!

Quick (stupid) question. I want to make a bunch of lists before going into a loop (that will append data into these lists).

Here's the deal. This looks kinda sloppy to me, so I was wondering if there was an accepted "cleaner" way. The caveat being I want to keep all those list names as is. The reason why is just so that when I look back at this code in months I won't be like, "list[0,3]?? WHAT'S THAT?"

code:
oct_x, nov_x, dec_x, jan_x = [[], [], [], []]
oct_xerr, nov_xerr, dec_xerr, jan_xerr = [[], [], [], []]
oct_y, nov_y, dec_y, jan_y = [[], [], [], []]
oct_yerr, nov_yerr, dec_yerr, jan_yerr = [[], [], [], []]

  • Locked thread