I’m doing a database insert script in pycassa. I want to set up a public static class that defines some variables that will get used a lot by other functions later on. Heres what I have…
class ks_refs():
pool = ConnectionPool('TweetsKS')
user_name_cf = self.cf_connect('UserName')
user_tweet_cf = self.cf_connect('UserTweet')
def cf_connect(column_family):
cf = pycassa.ColumnFamily(self.pool, column_family)
return cf
I haven’t even tried to run this yet because I’m sure it wont work. You can see I want this static variable ‘pool’ first, and then set up user_name_cf and user_tweet_cf (and some more later) using the cf_connect method which needs ‘pool’ to work.
I know I could put that method outside the class, or I could have this non-static and make an instance of it, but I want to try this because this is what I really want (before I was just using globals but I think a static class holding all this is the best idea)
I think you want to have a class method instead:
Now you can refer to the pool defined on your class with ease.
Your
user_name_cfanduser_tweet_cf‘attributes’ will not work, however. You can add these after having created the class definition:where they are then module-level constants, or you can add them to the class as attributes after the fact: