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
hey mom its 420
May 12, 2007

Check out http://www.rmunn.com/sqlalchemy-tutorial/tutorial.html where it says "Transactions". I think that might be what you're looking for.

Adbot
ADBOT LOVES YOU

hey mom its 420
May 12, 2007

Hey m0nk3yz, congratz on the PEP, I read it and it looks real good, I hope it gets accepted!

hey mom its 420
May 12, 2007

Also, it might do you good to use the named string formatting for more readability.
code:
"Hello my name is %(name)s and I'm %(age)d years old" % {
   'name':'Bonus',
   'age':12,
}

hey mom its 420
May 12, 2007

code:
def indices(c, st):
    return [i for k, i in zip(list(st), itertools.count()) if k==c]

hey mom its 420
May 12, 2007

From the commandline, try navigating to the
code:
neat
directory and type in
code:
python setup.py install
at the terminal. I'm not sure if that's the procedure for that particular package, but that's how it usually goes.

hey mom its 420
May 12, 2007

I found that Java-ish static methods are best translated to module top level functions in Python.

hey mom its 420
May 12, 2007

AWESOME!!!!!

hey mom its 420
May 12, 2007

What qualifies a duplicate entry? What's that column where it's mostly zeroes but one 36 and a 3? Is that the counter?

hey mom its 420
May 12, 2007

Yeah. Also watch that you don't fall into an infinite loop within the __getattr__ or __setattr__ methods. Manipulate the __dict__ directly.

hey mom its 420
May 12, 2007

Milde: Ah, yeah, right, I always consfuse get/setattr with get/setattribute. Those are really awkward names.

HatfulOfHollow: try timestamp = oracle.Timestamp(*date). You can also call keyword arguments in that way by using ** with any dict-like object.

e: beaten :pwn:

hey mom its 420
May 12, 2007

http://docs.python.org/tut/node6.html#SECTION006700000000000000000

It's really simple, when defining functions, *args have to come after any other arguments and **kwargs have to come after any *args.

Also, quick demo:
code:
>>> def meow(arg1, *args, **kwargs):
...   print arg1
...   print args
...   print kwargs
...
>>> meow("one", "two", "three", heh="feh", meh="teh")
one
('two', 'three')
{'heh': 'feh', 'meh': 'teh'}
>>>

hey mom its 420
May 12, 2007

Yeah there is.
It seems like the best solution here would be Django. It's a web framework for Python. It features a very good templating engine, database abstraction and is generally very easy to use. I'd go so far as to say it's one of the most well planned out and best programming tools today. There's a Django thread here in CoC, so I'd suggest starting there.

hey mom its 420
May 12, 2007

You can try
code:
for i, e in enumerate(k):
  k[i] = 0
But a more elegant solution would be to just map 0 to the list
code:
[0 for e in k]
Of course this copies a list and doesn't modify it in place. But if you're worried about copying large datasets, you shouldn't be using lists in the first place but generators and then map stuff to generators using generator expressions or functions from the itertools module.

hey mom its 420
May 12, 2007

:cool:

hey mom its 420
May 12, 2007

Watch out .... in your first example, you're modifying the arguments in place and then returning a copy. So if someone passes a list to that function and tries to use the same list later on, they'll find it's been mysteriously changed.
Why not just do
code:
def cleanse_table(table):
  return [[cleanse_cell(cell) for cell in row] for row in table]
Also, you're spelling cleanse wrong :science:
Also, it's generally not cool to just use catch-all except clauses because then they also catch KeyboardInterrupt exceptions and whatnot.

hey mom its 420 fucked around with this message at 00:35 on Jul 1, 2008

hey mom its 420
May 12, 2007

Sure, it's section 5.1.4 here http://docs.python.org/tut/node7.html

It's quite simple actually, this is how list comprehensions behave:
code:
>>> foo = [1,2,3,4,5]
>>> [x*2 for x in foo]
[2, 4, 6, 8, 10]
>>> [x/2 for x in foo if x % 2 == 0]
[1, 2]
>>> strings = ['hehe', 'woot', what']
>>> [st.upper() for st in strings]
['HEHE', 'WOOT', 'WHAT']

hey mom its 420
May 12, 2007

I don't think there's a really idiomatic way of doing that. But you can do it recursively in this manner.
- For every element in the list, check if it's a list.
--- If it is a list, call the function recursively on it.
--- If it's not a list, apply whatever transformations you want to it.

hey mom its 420
May 12, 2007

That doesn't say anything positive about mod_python, only something negative about JSP and PHP. If you develop for WSGI, you can deploy your application on any WSGI-compilant server. There's also a WSGI wrapper for mod_python that makes it possible to run WSGI apps on mod_python.

hey mom its 420
May 12, 2007

How about you just write it into a dict in Python syntax and then import the dict?
edit: or just set variables and then import the module. You know, like if you have test_settings.py and then in it you do
code:
ip_numbers = ["192.168.1.1", "132.242.222.111", "192.168.1.100"]
blah = 20420
something = 203.20
and then just do import test_settings and so it's handy to access the values like test_settings.blah

hey mom its 420 fucked around with this message at 22:15 on Jul 7, 2008

hey mom its 420
May 12, 2007

No, __getslice__ is deprecated, don't use that. Make sure that you have a new-style object there. So if you have
code:
class Collection:
  def __init__(self, data)
    self.data = data
  def __getitem__(self, key)
    return self.data.__getitem__(key)
That won't work, because you have to do make it a new-style object. So you'd have to have
code:
class Collection(object):
  # stuff goes here
in Python 2.5, objects are considered old style objects unless they inherit from the object. In py3k, you won't have to do that anymore.

hey mom its 420
May 12, 2007

Your awesomeproject directory should have an __init__.py file, even if it's empty. That makes it a python package.
Doing from something import * is generally always considered a bad idea because you can get all kinds of name clashes. What you could do is mimic what PyGame did — you could define a file called, say, locals.py in your root package. Then in locals.py import everything you need:
code:
from base.neatobject import blah, blah2, blah3
from base.otherobject import foo, foobar, foo
from other.something import bla, bla, bla
and then you just do from awesomeproject.locals import * and everything that's been imported in locals.py will jump into your namespace. That way you can quickly import stuff you need, but the importing is still explicit, only it's tucked away in locals.py.

hey mom its 420 fucked around with this message at 20:49 on Aug 18, 2008

hey mom its 420
May 12, 2007

Yeah, in base/__init__.py do:
code:
from controller import Controller
and then you can do import base and then stuff like base.Controller()

Basically, if you put stuff into a package's __init__.py, it acts like the package was the script. So, for instance, if you have a file foo/__init__.py and in that file you have
code:
a = 5
you can do import foo and then access foo.a to get a 5

hey mom its 420
May 12, 2007

code:
>>> print('hehe', 'hahaha')
('hehe', 'hahaha')
>>> print 'hehe', 'hahaha'
hehe hahaha
edit: ugh, beaten, gently caress you milde

hey mom its 420
May 12, 2007

Check out this video http://video.google.com/videoplay?docid=-6459339159268485356&ei=CEOtSMOwHZCuigKD_qjpDg&q=python+3000+guido at approximately 50 minutes and 30 seconds where Guido talks about turning print into a function, how most people hated it at first, but he gives a nice reasoning as to why it's being done.

hey mom its 420
May 12, 2007

The best thing is though, you won't even have to run a search and replace to change it to logging or whatever. You can just do something like print = Logging.log

hey mom its 420
May 12, 2007

I'll never get why people make their own style guidelines when there's good old PEP 8.

hey mom its 420
May 12, 2007

Just use a generator.
code:
xs = [2,3,4,5,6,7]
xs_modified = (a*2 for a in xs)
then you can iterate over xs_modified and it will only start doing the computations once you iterate over it.

hey mom its 420
May 12, 2007

Use generator expressions
code:
>>> mylist = [(0x20, 40), (0x70, 6), (0x7B, 6), (0x04, 0x01, 3), (0x70, 10)]
>>> matching_tuples = (a for a in mylist if a[0] == 0x70)
>>> for x in matching_tuples: print x
...
(112, 6)
(112, 10)

hey mom its 420
May 12, 2007

Yeah, that's perfect for the next method of generators.

code:
>>> mylist = [(0x20, 40), (0x70, 6), (0x7B, 6), (0x04, 0x01, 3), (0x70, 10)]
>>> matching_tuples = (a for a in mylist if a[0] == 0x70)
>>> matching_tuples.next()
(112, 6)
>>> matching_tuples.next()
(112, 10)
>>> matching_tuples.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

hey mom its 420
May 12, 2007

Also a great way to do it would be to use lazy slicing from the itertools module.
code:
>>> import itertools
>>> mylist = [(0x20, 40), (0x70, 6), (0x7B, 6), (0x04, 0x01, 3), (0x70, 10)]
>>> index_to_search_from = 2
>>> sliced = itertools.islice(iter(mylist), index_to_search_from, None)
>>> matching_tuples = (a for a in sliced if a[0] == 0x70)
>>> matching_tuples.next()
(112, 10)
>>> matching_tuples.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
No copies of the list are made and everything is wonderfully lazy.

hey mom its 420
May 12, 2007

2.6 is out, w00t!

hey mom its 420
May 12, 2007

Use generators! (What a surprise! :v: )
code:
>>> big_list = [{'DxY':17L}, {'DxY':9L}, {'DxY':199L}]
>>> new_list_gen = (a['DxY'] for a in big_list)
Then you can iterate over new_list_gen. No new copies of big_list are created.

hey mom its 420
May 12, 2007

Then you can just implement normal bisection on the list that you have, only instead of doing your_list[x], you do your_list[x]['DxY'].

hey mom its 420
May 12, 2007

One way to do it:
code:
>>> reduce(lambda x,y:x+y, [x for x in "Hey, there!" if x.isalnum()])
'Heythere'
But yeah, use regexen.
code:
>>> import re
>>> re.sub(r'[^a-zA-Z0-9]','','aaa!haha!!!,,,')
'aaahaha'

hey mom its 420 fucked around with this message at 00:32 on Oct 13, 2008

hey mom its 420
May 12, 2007

Ugh, I've been in Haskell land for too long, that reduce was uncalled for.

hey mom its 420
May 12, 2007

code:
>>> foo = [1, "two", "foo", "bar"]
>>> def f(arg1, arg2, arg3, arg4, arg5):
...   print "arg1 is:", arg1
...   print "arg2 is:", arg2
...   print "arg3 is:", arg3
...   print "arg4 is:", arg4
...   print "arg5 is:", arg5
...
>>> f("something", *foo)
arg1 is: something
arg2 is: 1
arg3 is: two
arg4 is: foo
arg5 is: bar
The key is the *.

hey mom its 420
May 12, 2007

Python supports functions as first-class objects. So one way to do it would be this.
code:
>>> def foo(x):
...   print 'haha %s' % x
...
>>> def bar():
...   print 'hoho'
...
>>> my_func_dict = {'the_foo':foo, 'the_bar':bar}
>>> my_func_dict['the_foo']('blah')
haha blah

hey mom its 420
May 12, 2007

Yeah, that's true.

hey mom its 420
May 12, 2007

code:
for field in ["att1", "att2", "att3"]:
  if getattr(object, field) == 0:
    setattr(object, field, "")

Adbot
ADBOT LOVES YOU

hey mom its 420
May 12, 2007

He could, but somehow I feel the Python mantra of explicit is better than implicit should be taken into account here.

  • Locked thread