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
LOLLERZ
Dec 9, 2003
ASK ME ABOUT SPAMMING THE REPORT FORUM TO PROTECT ~MY WIFE'S~ OKCUPID PERSONALS ANALYSIS SA-MART THREAD. DO IT. ALL THE TIME. CONSTANTLY. IF SHE DOESN'T HAVE THE THREAD, SHE'LL WANT TO TALK TO ME!

marcan posted:

I can see why that would take forever. The code matches all the a?s first, determines the string to be too short, and then starts trying all combinations for which a?s to consider or not (it isn't smart enough to figure out that they are all equivalent). My guess is the runtine is 2^30.
Very astute. There is a better way to match these though.

Adbot
ADBOT LOVES YOU

npe
Oct 15, 2004
Need some django help... I posted this to the django users group but I have a bad feeling it's not going to get any hits, so I may as well see if anyone has any ideas. Anyways:

I've been trying to get django working with a legacy system which I feel could benefit greatly from it, however I've managed to hit a critical snag. Our setup is such that we have tables in multiple schemas, so (for example) there might be one called "app_sys" and another called "app_prod", with tables in each that need to be accessed.

Initially I had been working with 0.96 and managed to get things working by specifying db_table values in class Meta with the schema prefix... for example

code:
class Documents(models.Model):
    documentid = models.IntegerField(primary_key=True, db_column='documentid')
    # ...etc

    class Meta:
        db_table = 'app_prod.documents'
However, I ended up upgrading to the SVN release (to try and see if slicing query results worked, they don't seem to with Oracle in .96) and now I see that table names are quoted. This means my trick doesn't work anymore, because what gets put into the query is

code:
SELECT "APP_PROD.DOCUMENTS"."DOCUMENTID", <snip>
   FROM "APP_PROD.DOCUMENTS" ...etc
Of course with the quotes, Oracle is looking in the user's schema for a table named with the period, which doesn't exist. Naturally, what I was doing was pretty hackish, so I'm not bothered that this particular approach doesn't work (it seemed pretty sketchy anyways), but on the other hand if I can't figure out how to specify the full schema and table name in SOME way, it seems to kill this effort completely.

Does anyone have any ideas for a way around this?

npe fucked around with this message at 18:43 on Dec 6, 2007

No Safe Word
Feb 26, 2005

Put quotes that will hack around it?
code:
        db_table = 'app_prod"."documents'
I mean this is ugly as all get-out, but it might work.

npe
Oct 15, 2004
You're on to me! I actually tried that right after posting about this, and it seems to be working for now, which is letting me continue development in the meantime. But I'm highly embarassed about it... hopefully there's a real solution at some point in the future. :(

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

LOLLERZ posted:

Very astute. There is a better way to match these though.

Non recursive regexes are not a panacea and a lot of things that are commonplace on regular backtracking regex engines would have to be emulated and would most likely would run several orders of magnitude slower.

If you match a{,30}a{30} instead of that one case which benefits the Thompson method, then you'll get about the same result between the methods. In fact, the Thompson method needs to be modified to emulate the backtracking method.

Digital Spaghetti
Jul 8, 2007
I never gave a reach-around to a spider monkey while reciting the Pledge of Alligence.
Another Django question from me:

I have a comment model I am building (yes I know Django has it's own Free comments, but it's poo poo) and what I want to do is have a template tag that displays the comment form for my entries. Here is the model at the moment, before I clean it up:

code:
from django.db import models
from django.db.models import permalink
from django.core import urlresolvers
from django.contrib.auth.models import User
from django.template.defaultfilters import slugify
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

from digitalspaghetti.blog.models import Entry

import datetime

class Comment(models.Model):
    author=models.CharField(max_length=50, default="Anonymous Chump")
    email=models.EmailField()
    website=models.URLField()
    comment=models.TextField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.IntegerField(_('object ID'))
    pub_date=models.DateTimeField('date published')
    is_private=models.BooleanField(default=False)
    is_authorised=models.BooleanField(default=False)
    def __unicode__(self):
        return self.author
    class Admin:
        pass
The problem is I don't know how to use New Forms within a template tag and be able to save the model data. If it was a view, I'd be fine, but since I can't have a save method as it's not a class, just a function I'm stumped :(

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

Digital Spaghetti posted:

(yes I know Django has it's own Free comments, but it's poo poo)

poo poo? Fairly undocumented perhaps, but they have everything, karma, adaptable moderation, akismet integration, what's poo poo about it?

Digital Spaghetti posted:

Well maybe I'm a bit harsh - but what I mean is it isn't really extensible without having to go in a change some core code as far as I can tell - I want to remove the site field, and add a few other fields in so it's more like wordpress, unless you can tell me otherwise? And I don't see Askimet integration, which is what I am looking for but as you say the docs are pretty poor

Err my bad, akismet integration is not out of the box, you can use signals to pretty much make the comment system do whatever you want.

Unfortunately some of django's best things are often the worst documented.

deimos fucked around with this message at 04:13 on Dec 8, 2007

Digital Spaghetti
Jul 8, 2007
I never gave a reach-around to a spider monkey while reciting the Pledge of Alligence.

deimos posted:

poo poo? Fairly undocumented perhaps, but they have everything, karma, adaptable moderation, akismet integration, what's poo poo about it?

Well maybe I'm a bit harsh - but what I mean is it isn't really extensible without having to go in a change some core code as far as I can tell - I want to remove the site field, and add a few other fields in so it's more like wordpress, unless you can tell me otherwise? And I don't see Askimet integration, which is what I am looking for but as you say the docs are pretty poor

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!
There's also http://code.google.com/p/django-comment-utils/

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!
I don't know how old this is, but it references one of my annoyances with python. There's such a thing as TOO much choice.

bitprophet posted:

FWIW when anyone asks "What GUI toolkits should I use with Python?" the answer is almost always "Tkinter, which is kind of old and busted, or wxWidgets, which is the new hotness; and Python bindings exist for most platform-specific GUI related setups out there, such as GTK or Cocoa, if you don't care about cross-platform." Not "See this list of a dozen+ toolkits".

This I know, but I keep having people point out GUI toolkit of the month to me all the time, that's the real annoyance I guess. Or people telling me "hey check out the cool application I made with <obscure toolkit 876213>".

deimos fucked around with this message at 20:12 on Dec 11, 2007

bitprophet
Jul 22, 2004
Taco Defender

deimos posted:

I don't know how old this is, but it references one of my annoyances with python. There's such a thing as TOO much choice.

Eh, that's generic programming/open source for you, has nothing to do with Python itself IMO. Coders have been reinventing the wheel/working on parallel solutions to the same problem, forever.

FWIW when anyone asks "What GUI toolkits should I use with Python?" the answer is almost always "Tkinter, which is kind of old and busted, or wxWidgets, which is the new hotness; and Python bindings exist for most platform-specific GUI related setups out there, such as GTK or Cocoa, if you don't care about cross-platform." Not "See this list of a dozen+ toolkits".

EVGA Longoria
Dec 25, 2005

Let's go exploring!

I figure it's worth pointing out that PyObjC, which allows you to program native Coacoa apps for OSX ,now comes standard with Leopard and XCode, making Mac Python programming a bigger joy.

m0nk3yz
Mar 13, 2002

Behold the power of cheese!

Casao posted:

I figure it's worth pointing out that PyObjC, which allows you to program native Coacoa apps for OSX ,now comes standard with Leopard and XCode, making Mac Python programming a bigger joy.

One quick note, if you are running 2.5.1 on Leopard and want to new pyobjc stuff from subversion to compile, you have to edit Python.framework/Versions/2.5/lib/python2.5/config/Makefile and remove the "-isysroot /Developer/SDKs/MacOSX10.4u.sdk" chunk from it. Otherwise, the new pobjc stuff won't compile.

code:
Python 2.5.1 (r251:54869, Apr 18 2007, 22:08:04) 
[GCC 4.0.1 (Apple Computer, Inc. build 5367)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import WebKit
>>> 

Mulozon Empuri
Jan 23, 2006

The Django Book 1.0 is finally online! :woop:

SmirkingJack
Nov 27, 2002

Mulozon Empuri posted:

The Django Book 1.0 is finally online! :woop:

Wow, that really surprises me. I would have thought that they would have waited until after Christmas at least before releasing it online, to push book sales for the season.

gabensraum
Sep 16, 2003


LOAD "NICE!",8,1
Maybe they want to spur sales by putting it up as a preview.

No Safe Word
Feb 26, 2005

I actually half-expected the book release to coincide with the Django 1.0 release, but I have no idea how far away the 1.0 release is, so that may not have been feasible. And in fact, I'm guessing it's probably a little ways away because it'd be a bad idea to release a book and then immediately obsolete it (granted, it'd still be valid for the 0.96 release, but there's a lot of new cool useful stuff in the 1.0 branch).

bitprophet
Jul 22, 2004
Taco Defender
The book has had problems getting out on time, and so has the 1.0 release, so I'm guessing it was released when it was simply because "oh god we're finally done".

There's a WROX book out that includes Django in its content and was published like a month ago, but it's WROX, I'm told their quality is Not So Great (never read any myself). There are also at least two or three other Django books that will be released over the next ~6 months or so :)

No Safe Word
Feb 26, 2005

Not to derail but the latest stuff I've been doing at work has been possible thanks to the help of some Wrox books :)

And for Python-related stuff to get somewhat back on track: here's this!

Digital Spaghetti
Jul 8, 2007
I never gave a reach-around to a spider monkey while reciting the Pledge of Alligence.

Mulozon Empuri posted:

The Django Book 1.0 is finally online! :woop:

I've made a PDF version of the online book, you can download it from http://digitalspaghetti.me.uk/2007/12/17/django-book-pdf

politicorific
Sep 15, 2007
finally, django documentation out I've been waiting, but for now I'm going to focus on building the basic stuff until I fully understand what all's going on. So here's my SQL question:

I'm using Gadfly to learn how to access databases from python. I'm building a prototype for a website that will end up storing about 3000-5000 rows. A lot of the information is very similar - 10 columns and maybe only 3 records are slightly different. The problem is that those 3 different records are all I can search on. I'm making this too complicated.

Let's say I have 2 records in the database and I do the following:

code:
select * from table
Let's say this gives two results:
code:
(first, result)
(second, result)
now I want to store the result to a variable
code:
for row in cursor.fetchall():
      print row
This ends up randomly assigning either (first, result) or (second, result) to row.

How do I manipulate multiple rows, return the two options, or ask the user for more input?

bitprophet
Jul 22, 2004
Taco Defender
You need to understand SQL more. SQL result sets are explicitly unordered unless you specify an ORDER BY clause. So you should never, ever assume any sort of order in such a result unless you're using ORDER BY.

So change your SELECT statement to something like
code:
SELECT * FROM table ORDER BY colname
and then it will be consistent every time you call it.

politicorific
Sep 15, 2007
okay I guess I should've used my exact code

code:
c = g.cursor()
c.execute("""SELECT
combined,root1,root2,root3,
root4,root5,root6,participle,
tablenumber,intrans,english
FROM englishtable
ORDER BY
combined,root1,root2,root3,
root4,root5,root6,participle,
tablenumber,intrans,english
""")
c.description
print c.pp()
print "remember - right to left encoding is messing with the output"
for row in c.fetchall():
    print row
this returns this:
code:
('\xd7\x90\xd7\x91\xd7\x93', '\xd7\x90', '\xd7\x91', '\xd7\x93', '', '', '', '', '118', 'intrans', 'lose')
('\xd7\x90\xd7\x91\xd7\x93', '\xd7\x90', '\xd7\x91', '\xd7\x93', '', '', '', '', '91', 'trans', 'lose')
then if I do something like
code:
 z = row
print z
This will assign a single cursor selection to row, print it, then go ahead and assign another cursor selection to row and overwrite it.
How do I store multiple rows to different variables so that python/gadfly isn't overwriting them? The problem isn't the select statement, I'm getting the data I want, I just want all of it - it's the fetchall() statement that I'm having a hard time understand

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

politicorific posted:

okay I guess I should've used my exact code

this returns this:
code:
('\xd7\x90\xd7\x91\xd7\x93', '\xd7\x90', '\xd7\x91', '\xd7\x93', '', '', '', '', '118', 'intrans', 'lose')
('\xd7\x90\xd7\x91\xd7\x93', '\xd7\x90', '\xd7\x91', '\xd7\x93', '', '', '', '', '91', 'trans', 'lose')
then if I do something like
code:
 z = row
print z
This will assign a single cursor selection to row, print it, then go ahead and assign another cursor selection to row and overwrite it.
How do I store multiple rows to different variables so that python/gadfly isn't overwriting them? The problem isn't the select statement, I'm getting the data I want, I just want all of it - it's the fetchall() statement that I'm having a hard time understand

you just want all the rows? then just assign it, fetchall is a generator.

code:
>>>rows_as_list = list(c.fetchall())
>>>for x in range(len(rows_as_list)):
...   print x
...
('\xd7\x90\xd7\x91\xd7\x93', '\xd7\x90', '\xd7\x91', '\xd7\x93', '', '', '', '', '118', 'intrans', 'lose')
('\xd7\x90\xd7\x91\xd7\x93', '\xd7\x90', '\xd7\x91', '\xd7\x93', '', '', '', '', '91', 'trans', 'lose')
>>>print x[0]
('\xd7\x90\xd7\x91\xd7\x93', '\xd7\x90', '\xd7\x91', '\xd7\x93', '', '', '', '', '118', 'intrans', 'lose')
>>>print x[0][0]
'\xd7\x90\xd7\x91\xd7\x93'

No Safe Word
Feb 26, 2005

To get this back onto the front page where it belongs and to sort of continue the topic at hand, what do people think of the Python ORMs that are out there? I'm mainly a user of the one built into Django, and I think it's pretty awesome (with a few warts but what doesn't have those?). I know some live and die by SQLAlchemy and there are a few others out there which are pretty heavily used as well. Anybody a whiz with several who wants to provide a good comparison of them?

I will say that when I was doing some ASP.NET (ugh) training last week, the instructor was all high on LINQ and talking about how in .NET 3.5 they had just introduced all this cool stuff and wow delayed execution and chained querysets and ooh so neat, which really blew the mind of some of the other folks in the training but I was like "yeah, some journalism major wrote the same thing for Django" (I don't know that Adrian actually wrote that part, but it sounds funnier ... though I didn't actually say it). Not to say LINQ isn't pretty cool (it is), but I just forget how cool some of the stuff we've already got in our pet frameworks/languages already is.

bitprophet
Jul 22, 2004
Taco Defender
I too am basically defined as a "Django ORM user". I used some SQLObject before I found Django but hardly remember it, and was at an NYCPython meetup where the author of SQLAlchemy gave a little talk about that, looks pretty neat, and can probably do "more" than Django's ORM at the expense of not being quite as concise. However I haven't actually used it yet so hard to say for sure.

I'm currently debugging a PHP project which is basically the purest form of PHP you can find, namely absolutely mind-boggling horrible :psyduck:-inducing shite. Including your standard raw SQL queries up the ying-yang, building them in various ways, with plenty of near-breakage and probably a million potential injection exploits.

Makes me appreciate ORMs and Python all the more :(

1550NM
Aug 31, 2004
Frossen fisk
This thread will be useful.

I got Learning Python two days ago and I'm quite amazed at how easy it have been to grasp the language this far.

I finished the fifth chapter yesterday and decided that I wanted to try to recreate
the creepy checker that Steel Sanitizer posted over in Goon-Made Goodness thread, which took all of 5 minutes, what made it fun was the ease of
growing the the little snippet of code to add input validation, different output if
you're male or female or the partner is older, adding some def xxx just to try that etc etc.

It was just plain fun, and being that my last try at achieving anything with a
programming language was Delphi4 in 1999 it great to be able to paste code into the
IDLE window to quickly test it.

hey mom its 420
May 12, 2007

Here's an interesting one.
Do you guys think there's a way to pass data from decorators to the functions they're decorating without using those functions' call arguments or keyword arguments? I've given this some thought and I think there is no way, but if anyone can prove me wrong that sure would be great. If you could somehow manipulate the function's namespace before calling it or something, hmmm.

Here's an example of what I'm trying to do.
code:
>>> def clown_decorator(func):
...     def inner(*args, **kwargs):
...             return func(clowns=5, *args, **kwargs)
...     return inner
...
>>> @clown_decorator
... def bar(clowns):
...     print "There are %d clowns!" % clowns
...
>>> bar()
There are 5 clowns!
Here I pass the number of clowns via the function's arguments, but I'm wondering if there's a way to do it without having to place the `clowns` argument in the function definition for `bar`.

I'm not trying to solve a particular problem here (although it would come in handy with something I'm working on, but it's fine without it too), I'm just generally wondering if there's a way to do that in the language. My bets are on "no".

hey mom its 420
May 12, 2007

Yay, I found the answer!
Just do
code:
>>> def clown_decorator(func):
...     def inner(*args, **kwargs):
...             func.func_globals['clowns'] = 5
...             return func(*args, **kwargs)
...     return inner
...
Man, Python is awesome.

duck monster
Dec 15, 2004

I now have this strange urge to start filling my code with clown decorators.

duck monster
Dec 15, 2004

Himmit posted:

This thread will be useful.

I got Learning Python two days ago and I'm quite amazed at how easy it have been to grasp the language this far.


Thats one of the biggest joys of python. It really aint hard. I learned python maybe ten years ago , slacking off at work one afternoon doing the tutorial. It was like a huge succession of lightbulbs going off above my head. Then it was straight over to the modules section, and before long I was cranking out all sorts of stuff. Fantastic.

The second joy was learning the fine details of the object orientation. Python is such a good language.

hey mom its 420
May 12, 2007

Haha! Yes, clown coding is the best coding.

Eh, it seems that this func_globals is not gonna work since it pollutes the global namespace and I don't want that.
code:
>>> def foo():
...     print clowns
...
>>> dir()
['__builtins__', '__doc__', '__name__', 'foo']
>>> foo.func_globals['clowns'] = 5
>>> dir()
['__builtins__', '__doc__', '__name__', 'clowns', 'foo']

I wonder if there's a way to just affect a function's local namespace?

deedee megadoodoo
Sep 28, 2000
Two roads diverged in a wood, and I, I took the one to Flavortown, and that has made all the difference.


Bonus posted:

Haha! Yes, clown coding is the best coding.

Eh, it seems that this func_globals is not gonna work since it pollutes the global namespace and I don't want that.
code:
>>> def foo():
...     print clowns
...
>>> dir()
['__builtins__', '__doc__', '__name__', 'foo']
>>> foo.func_globals['clowns'] = 5
>>> dir()
['__builtins__', '__doc__', '__name__', 'clowns', 'foo']

I wonder if there's a way to just affect a function's local namespace?

Is there a reason you can't make 'clowns' a global? Unless I'm misunderstanding some implementation-specific need, a global variable should probably do the trick.

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!
So why don't you want to edit *args or **kwargs?

deimos fucked around with this message at 06:12 on Jan 9, 2008

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Bonus posted:

Eh, it seems that this func_globals is not gonna work since it pollutes the global namespace and I don't want that.

Yeah, func_globals is the set of global variables that the function can see. The closest I can see is to use function attributes:

code:
>>> def foo():
...   return foo.clowns
... 
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in foo
AttributeError: 'function' object has no attribute 'clowns'
>>> foo.clowns = 100
>>> foo()
100
Except it doesn't work in a decorator:

code:
>>> def clown_decorator(func):
...   def inner(*args, **kwargs):
...     func.clowns = 5
...     return func(*args, **kwargs)
...   return inner
... 
>>> @clown_decorator
... def foo():
...   return foo.clowns
... 
>>> foo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in inner
  File "<stdin>", line 2, in foo
AttributeError: 'function' object has no attribute 'clowns'
The reason is that foo is looking itself up by name, but clown_decorator actually sets foo.clowns, then makes a wrapper and renames that to foo, overriding the previous name. Best way to get around that would be to figure out how to make a function refer to itself, but I have no idea.

Of course, you can always just make a class with a __call__ method and get lots of flexibility that way.

Lonely Wolf
Jan 20, 2003

Will hawk false idols for heaps and heaps of dough.
code:
def dec(f):
    def inner(*a,**k):
        return f(*a,**k)
    inner.func_name = f.func_name
    #setting this in f.func_dict does not work for some reason
    inner.func_dict['clowns'] = 1
    return inner

@dec
def foo():
    print foo.clowns

foo()
It prints 1. Booyah.

JoeNotCharles
Mar 3, 2005

Yet beyond each tree there are only more trees.

Lord Uffenham posted:

It prints 1. Booyah.

Wow, I can't believe this worked.

You realize this is a terrible, terrible hack and I feel dirty for helping you out with it, right?

Buffis
Apr 29, 2006

I paid for this
Fallen Rib
I don't really see the point but if I'm understanding this correctly, then this is nicer

code:
def dec(f):
    f.clowns = 1
    return f

@dec 
def f():
    print f.clowns

f() # prints 1

deimos
Nov 30, 2006

Forget it man this bat is whack, it's got poobrain!

Lord Uffenham posted:

code:
def dec(f):
    def inner(*a,**k):
        return f(*a,**k)
    inner.func_name = f.func_name
    #setting this in f.func_dict does not work for some reason
    inner.func_dict['clowns'] = 1
    return inner

@dec
def foo():
    print foo.clowns

foo()
It prints 1. Booyah.

Yeah but what happens if you call foo(clowns=20)?

Adbot
ADBOT LOVES YOU

Buffis
Apr 29, 2006

I paid for this
Fallen Rib

deimos posted:

Yeah but what happens if you call foo(clowns=20)?

He didnt want to be able to do that?

quote:

but I'm wondering if there's a way to do it without having to place the `clowns` argument in the function definition for `bar`.

Buffis fucked around with this message at 17:23 on Jan 9, 2008

  • Locked thread