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
Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
*ahem* http://docs.python.org/library/functions.html#reversed

Adbot
ADBOT LOVES YOU

Jonnty
Aug 2, 2007

The enemy has become a flaming star!

UberJumper posted:

Is there anyway to make a python list iterator to go backwards?

Basically i have this

code:
    class IterTest(object):
        def __init__(self, data):
            self.data = data
            self.__iter = None
    
        def all(self):
            self.__iter = iter(self.data)
            for each in self.__iter:
                mtd = getattr(self, type(each).__name__)
                mtd(each)
    
        def str(self, item):
            print item
    
            next = self.__iter.next()
            while isinstance(next, int):
                print next
                next = self.__iter.next()
    
        def int(self, item):
            print "Crap i skipped C"
    
    if __name__ == '__main__':
        test = IterTest(['a', 1, 2,3,'c', 17])
        test.all()
Running this code results in the output:

a
1
2
3
Crap i skipped C

I know why it gives me the output, however is there a way i can step backwards in the str() method, by one step?

reversed()?

e: bleh

Scaevolus
Apr 16, 2007


He's asking for something like ungetc for iterators.

king_kilr
May 25, 2007
Buffer your iterator.

Haystack
Jan 23, 2005





outlier posted:

So, anyone played with repoze.bfg and has an opinion to share? I'd ignored the project for a long time because of its association with Zope ("Zope - for when you've got just too much free time") but a recent visit to it has reignited my interest:

* Fully unit tested, fully documented
* Actually breaks out some of the Zope architecture into genuinely standalone components
* Runs on UNIX, Windows, Jython and Google App Engine
* Persistence-agnostic: SQLAlchemy, ZODB, CouchDB, etc

I've been playing around with BFG for the last couple of months. Thus far I've only made piddling little test pages with it, but it's been a pleasure to work with so far. I like it better than Turbogears, Django, or Pylons.

Pros:
  • Everything you mentioned. The documentation is particularly nice.
  • It's drat lightweight. URL mapping, security, and template support are all it really does.
  • Traversal rocks.
  • The default authorization scheme is pretty neat. Basically you attach group/user privileges to members of your traversal object tree.
  • Swapping parts out is easy and well documented.
  • The primary developer is very active, always hangs out on IRC, and is really responsive.
  • The source code is (usually) nice and easy to read and understand.
  • Plays nicely with Pylons middleware.
  • Plenty of support for unit tests.

Cons:
  • From what I can tell, the community is tiny, bordering on non-existent.
  • The whole "model/view/context" terminology can get kind of confusing.
  • The BFG homepage is nothing to write home about.
  • There's no traversal + SQLalchemy tutorial.
  • Not much documentation on using middleware.
  • I personally do not like Chameleon, the default template language. I ended up writing my own bindings in order to use Mako. Which was, to be fair, really easy.
  • The default authorization scheme uses MD5 to hash it's session cookies.
  • Have fun finding a host.

Disclaimer: I'm not really a terribly well experienced web developer or programmer, so take my opinion with an appropriate level of caution.

hink
Feb 19, 2006
code:
#!/usr/bin/env python

class ReverseIter:
    def __init__(self, i):
        self.buff = []
        self.last = []
        self.it = iter(i)
    def __iter__(self):
        return self
    def next(self):
        if self.last:
            return self.last.pop()
        else:
            n = self.it.next()
            self.buff.append(n)
            return n
    def rewind(self):
        self.last.append(self.buff.pop())

quote:

>>> from reviter import ReverseIter
>>> rit = ReverseIter([1,2,3,4])
>>> rit.next()
1
>>> rit.next()
2
>>> rit.rewind()
>>> rit.next()
2
>>> rit.next()
3
>>>

could probably name the variables better but w/e

hink fucked around with this message at 04:56 on May 6, 2010

unixbeard
Dec 29, 2004

code:
class IterTest(object):
        def __init__(self, data):
                self.data = data

        def __gen(self):
                self.__x = 0
                while self.__x < len(self.data):
                        yield self.data[self.__x]
                        self.__x += 1

        def all(self):
                self.__iter = self.__gen()
                for each in self.__iter:
                        mtd = getattr(self, type(each).__name__)
                        mtd(each)

        def str(self, item):
                print item

                try:
                        next = self.__iter.next()
                        while isinstance(next, int):
                                print next
                                next = self.__iter.next()
                        self.__x -= 1
                except StopIteration:
                        return


        def int(self, item):
                print "Crap i skipped C"

if __name__ == '__main__':
        test = IterTest(['a', 1, 2,3,'c', 17])
        test.all()
im sure if you talked a little more about what you were doing there is a better way

Stabby McDamage
Dec 11, 2005

Doctor Rope
Is there a more compact/sensical way of expressing "every element of list 'a' except the next-to-last one?". Right now I have:
code:
a[:-2]+[a[-1]]
EDIT: Another question. Is there a canonical way to express "find first" without finding all? Something like the following, except more compact:
code:
def find_first_index(function,array):
	for i in xrange(len(array)):
		if function(array[i]): return i

Stabby McDamage fucked around with this message at 20:22 on May 6, 2010

tef
May 30, 2004

-> some l-system crap ->
code:

>>> next(i for i,e in enumerate(['a','b','c']) if e =='c')
2

Sock on a Fish
Jul 17, 2004

What if that thing I said?
I'm almost done reading Clean Code and have realized what a dunce I've been in not writing tests for my code. However, a huge chunk of my work involves writing clients for web services that may or may not be guaranteed to be available during an arbitrary test run. How am I supposed to write a test fixture for those?

Google turns up a lot for testing out the services themselves, but not so much for clients.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
Let the test fail and only do something about it if it fails a few times?

Sock on a Fish
Jul 17, 2004

What if that thing I said?

Avenging Dentist posted:

Let the test fail and only do something about it if it fails a few times?

So it's not recommended to write a dummy web service that does what you think the real one should be doing? That's what I was thinking I'd have to end up doing.

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
If you really wanted you could, but there's something to be said about the philosophy of "worse is better".

Benji the Blade
Jun 22, 2004
Plate of shrimp.

Sock on a Fish posted:

So it's not recommended to write a dummy web service that does what you think the real one should be doing? That's what I was thinking I'd have to end up doing.

Yes, that's basically what you need to do. Mock up the web service enough to test the client code.

hink
Feb 19, 2006

Stabby McDamage posted:

...questions...

you could probably use nicer looking list comprehensions in both these cases but they are going to be much slower

hink fucked around with this message at 00:35 on May 7, 2010

Sock on a Fish
Jul 17, 2004

What if that thing I said?
Dang.

Another question on testing, I usually declare a global logging instance, but I'm betting that's terrible, since my tests break when they find that there is no such logger available.

After I wrote the code that I'm mocking up tests for I discovered that logging creates sort of singletons, so that a call to logging.getLogger('mylogger') will grab an instance of 'mylogger' that has already been created. I think I don't even need to ask, should I abandon the global logger?

NoDamage
Dec 2, 2000

Haystack posted:

I've been playing around with BFG for the last couple of months. Thus far I've only made piddling little test pages with it, but it's been a pleasure to work with so far. I like it better than Turbogears, Django, or Pylons.

Pros:
  • Traversal rocks.
Does 'traversal' couple a url scheme to the underlying implementation of the application? This seems like something that I would generally want to avoid.

Haystack
Jan 23, 2005





NoDamage posted:

Does 'traversal' couple a url scheme to the underlying implementation of the application? This seems like something that I would generally want to avoid.

It can, but it doesn't have to. The "typical" use for traversal is for it to move over an object graph where each node in the graph represents part of a hierarchical data model (often using Zope Object DB to handle persistence). However, the system is extremely flexible. If you like, the object graph can be completely free of application logic, and simply map URLs to view code.

Moreover, you don't have to use traversal if you don't like it. BFG has a fully featured alternate URL mapping system roughly identical to the "Routes" middleware that Pylons uses.

Haystack fucked around with this message at 17:28 on May 7, 2010

nonathlon
Jul 9, 2004
And yet, somehow, now it's my fault ...

Haystack posted:

I've been playing around with BFG for the last couple of months. Thus far I've only made piddling little test pages with it, but it's been a pleasure to work with so far. I like it better than Turbogears, Django, or Pylons.

Awesome - just what I was looking for. Thanks!

Stabby McDamage
Dec 11, 2005

Doctor Rope
I want to take a string as a command line argument and use it to look up a global function by name. Something like:
code:
def prefix_awesome(): 
  print "Running awesome!!"

fname = sys.argv[1]
func = get_function_by_name("prefix_%s" % fname)
func()
$ myscript.py awesome
Running awesome!!


So would do I replace get_function_by_name() with? Don't worry about the security implications of this -- it's just a tool for my personal use.

EDIT: Found my answer:
code:
globals()["prefix_%s" % fname]
vvvvvv Thanks for the replies...I actually found the answer before I refreshed the thread! I didn't expect it to be so easy.

EDIT2: I can even filter the functions to find just those that start with "prefix_". Man, this is awesome. Way better than Perl's name handling.

code:
my_functions = dict( filter(lambda (k,v): k.startswith('prefix_'), globals().items()) )

Stabby McDamage fucked around with this message at 20:54 on May 7, 2010

Avenging Dentist
Oct 1, 2005

oh my god is that a circular saw that does not go in my mouth aaaaagh
globals()?

king_kilr
May 25, 2007
globals()["some_shit"]

hink
Feb 19, 2006

Stabby McDamage posted:

Way better than Perl

sounds about right

Habnabit
Dec 30, 2007

lift your skinny fists like
antennas in germany.

Stabby McDamage posted:

:words:

Yow there's some things wrong with that code!

dict((k, v) for k, v in D.iteritems() if k.startswith('prefix_')) <- filter+lambda is pretty silly now that python has generator expressions, and .items() is unnecessary because you don't need a list of all of the key/value pairs upfront.

Also it's really easy to make a decorator to add all of your functions to an explicit dict. "Explicit is better than implicit" is a key part of the zen of python, etc.
code:
commands = {}
def command(func):
    commands[func.__name__.partition('prefix_')[2]] = func
    return func

@command
def prefix_awesome(): 
    print "Running awesome!!"

@command
def prefix_whatever():
    pass

commands['awesome']()

Stabby McDamage
Dec 11, 2005

Doctor Rope

Habnabit posted:

Yow there's some things wrong with that code!

dict((k, v) for k, v in D.iteritems() if k.startswith('prefix_')) <- filter+lambda is pretty silly now that python has generator expressions, and .items() is unnecessary because you don't need a list of all of the key/value pairs upfront.

Also it's really easy to make a decorator to add all of your functions to an explicit dict. "Explicit is better than implicit" is a key part of the zen of python, etc.
code:
commands = {}
def command(func):
    commands[func.__name__.partition('prefix_')[2]] = func
    return func

@command
def prefix_awesome(): 
    print "Running awesome!!"

@command
def prefix_whatever():
    pass

commands['awesome']()

Awesome. This will make it a lot better.

UberJumper
May 20, 2007
woop
Quick question what is everyone using for an IDE? I am mainly looking for something with intellisense and a debugger.

In the past i have Tried:
Komodo
- Its awesome, but its too expensive for a university student (like seriously a student gimped license still costs several hundred dollars?)
Pyscripter
- crashes often / intellisense simply does not work for anything complex / debugger doesn't really work well
PyDev
- ohgodihateeclipse.

Has anyone given PyCharm a shot?

slipped
Jul 12, 2001

Stabby McDamage posted:

EDIT2: I can even filter the functions to find just those that start with "prefix_". Man, this is awesome. Way better than Perl's name handling.
Although it is a bit messier to accomplish this with perl, the fact remains that you should probably be doing something a better way if you need to muck with internals. For example, a simple dispatch table would be much more elegant in perl.

BeefofAges
Jun 5, 2004

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

UberJumper posted:

Quick question what is everyone using for an IDE? I am mainly looking for something with intellisense and a debugger.

In the past i have Tried:
Komodo
- Its awesome, but its too expensive for a university student (like seriously a student gimped license still costs several hundred dollars?)
Pyscripter
- crashes often / intellisense simply does not work for anything complex / debugger doesn't really work well
PyDev
- ohgodihateeclipse.

Has anyone given PyCharm a shot?

I've heard good things about Wing IDE, though I have not tried it myself.

Thermopyle
Jul 1, 2003

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

UberJumper posted:

Quick question what is everyone using for an IDE? I am mainly looking for something with intellisense and a debugger.

In the past i have Tried:
Komodo
- Its awesome, but its too expensive for a university student (like seriously a student gimped license still costs several hundred dollars?)
Pyscripter
- crashes often / intellisense simply does not work for anything complex / debugger doesn't really work well
PyDev
- ohgodihateeclipse.

Has anyone given PyCharm a shot?

I've been using Komodo Edit, which I guess is the free, stripped down version of Komodo IDE.

jonypawks
Dec 1, 2003

"Why didn't you tell me you were the real Snake?" -- Ken

UberJumper posted:

Quick question what is everyone using for an IDE? I am mainly looking for something with intellisense and a debugger.

In the past i have Tried:
Komodo
- Its awesome, but its too expensive for a university student (like seriously a student gimped license still costs several hundred dollars?)
Pyscripter
- crashes often / intellisense simply does not work for anything complex / debugger doesn't really work well
PyDev
- ohgodihateeclipse.

Has anyone given PyCharm a shot?

I've been using vim for many years so I'm pretty well locked in there, but I did try out PIDA recently which can embed your preferred text editor into an IDE-like environment. It seemed pretty nice, and I still fire it up when I feel like I need that tree view for a project.

Captain Capacitor
Jan 21, 2008

The code you say?
I just use TextMate and occasionally PyDev. I don't mind using Eclipse so much myself.

HIERARCHY OF WEEDZ
Aug 1, 2005

I'm using Macports, and I have a virtual environment with no site packages that needs pyyaml installed. In order to install pyyaml it needs to build against libyaml, which was installed in /opt/local/lib by Macports.

Is there a way to specify on the command line an option to distutils that emulates the include_dir option in an ext_module dictionary?

Stabby McDamage
Dec 11, 2005

Doctor Rope

slipped posted:

Although it is a bit messier to accomplish this with perl, the fact remains that you should probably be doing something a better way if you need to muck with internals. For example, a simple dispatch table would be much more elegant in perl.

A dispatch table is what I have now, and I'm sick of adding to it. It's a table full of heuristic query algorithms, as I'm analyzing a unique dataset. When I want to find a new kind of thing, I would write the function, then go to the table and add an entry. I need to do this constantly, so just autogenerating the dispatch table will make things much easier.

xPanda
Feb 6, 2003

Was that me or the door?
Could anyone recommend a package which would allow quick and simple rendering of a list of 3D coordinates as cubes in a 3D cubic grid? So if I have a grid 72 to an edge, and I only have a list of 4000 coordinates within this grid, only those 4000 coordinates are displayed within the greater 72**3 grid?

I'd like to be able to rotate the view and colour the displayed cubes, if possible. I have no experience with 3D stuff, so I don't even know whether this is a big or small task.

Also, why the Eclipse hate? Is it just the whole Java/memory usage thing?

nbv4
Aug 21, 2002

by Duchess Gummybuns

xPanda posted:

Could anyone recommend a package which would allow quick and simple rendering of a list of 3D coordinates as cubes in a 3D cubic grid? So if I have a grid 72 to an edge, and I only have a list of 4000 coordinates within this grid, only those 4000 coordinates are displayed within the greater 72**3 grid?

I'd like to be able to rotate the view and colour the displayed cubes, if possible. I have no experience with 3D stuff, so I don't even know whether this is a big or small task.

Also, why the Eclipse hate? Is it just the whole Java/memory usage thing?

you want matplotlib my friend

http://matplotlib.sourceforge.net/gallery.html scroll down to the bottom for the 3d stuff, then click for the code

tehk
Mar 10, 2006

[-4] Flaw: Heart Broken - Tehk is extremely lonely. The Gay Empire's ultimate weapon finds it hard to have time for love.

UberJumper posted:

Quick question what is everyone using for an IDE? I am mainly looking for something with intellisense and a debugger.

In the past i have Tried:
Komodo
- Its awesome, but its too expensive for a university student (like seriously a student gimped license still costs several hundred dollars?)
Pyscripter
- crashes often / intellisense simply does not work for anything complex / debugger doesn't really work well
PyDev
- ohgodihateeclipse.

Has anyone given PyCharm a shot?

WingIDE is the best python IDE out there right now but it is pricey. Though you can get a free license to work on a free software project. Other then that a nice mix of AutoComplete and pymacs in emacs works well. If you are spending a lot of time working on python and don't have the time to setup a good emacs python environment then Wing is the best way to go(especially if you need emacs keybindings).

king_kilr
May 25, 2007

tehk posted:

WingIDE is the best python IDE out there right now but it is pricey. Though you can get a free license to work on a free software project. Other then that a nice mix of AutoComplete and pymacs in emacs works well. If you are spending a lot of time working on python and don't have the time to setup a good emacs python environment then Wing is the best way to go(especially if you need emacs keybindings).

Wing looks nice (and they give out free licenses at PyCon, yipee), but last time I tried it it was inexplicably slow as hell, swapping like crazy, etc. Right now I'm trying out Komodo edit, although I suspect I'll end up back at Gedit.

Scaevolus
Apr 16, 2007

king_kilr posted:

Wing looks nice (and they give out free licenses at PyCon, yipee), but last time I tried it it was inexplicably slow as hell, swapping like crazy, etc.

Wing IDE is written in Python. :haw:

king_kilr
May 25, 2007

Scaevolus posted:

Wing IDE is written in Python. :haw:

It wasn't CPU bound (which it would be if python was the cause) it was causing disk swapping like crazy, meaning it was IO bound meaning it was doing something hosed up :)

Adbot
ADBOT LOVES YOU

tef
May 30, 2004

-> some l-system crap ->
I can't think of anything in python that would cause thrashing :3:

  • Locked thread