Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property buckets in Python

Tags:

python

dynamic

Ok, serious noob question here, so I'm looking for a very general answer. I'm new to Python, and the language seems straightforward enough, but I'm having a problem when I approach a problem. Coming from static typed languages, my first instinct is to make some objects so I can pass buckets of relevant data around while keeping my logic terse and simple.

But is this a thing in Python? Since the language is dynamically typed, is there any point in making an object to pass around? Why would I define fields in advance if I can just set them on the fly? And if I don't have fields, why would I bother with an object at all? Couldn't I use a dictionary and it's basically the same?

So I guess I'm asking, if I need to separate my data access code from my logic, is a property bucket a good idea? Or do people not do this in Python and just use dictionaries?

EDIT:

Here's a basic idea of what I'm trying to accomplish:

I have an http api that returns a json string. I want to hit that url with urllib2, bring it back as a string, and use json.loads to turn it into a dict object. Then I want to separate the mysql code into another class. Obviously I don't want to pass 20 values around, and since I already have a dict with all my values, what would be the benefit to creating a dynamic object and adding each value from the dict? I'm still going to have to know all the names in the data access class right? I'm not really talking about performance, but more about design.

like image 687
LoveMeSomeCode Avatar asked Jan 05 '13 03:01

LoveMeSomeCode


People also ask

What are buckets in Python?

buckets is a python library which can be used to manage data that it will organize to keep the most relevant/recent data and deprioritize older data. A bucket in this instance is a place holder for data. And with Buckets you can manage multiple bucket lists. A bucket list is a fixed size list of buckets.

What is bucket Type?

Bucket types allow groups of buckets to share configuration details and for Riak users to manage bucket properties more efficiently than in the older configuration system based on bucket properties. Important note on cluster downgrades.

Which Python function is used to create a new bucket in Riak?

Bucket Type creation and activation is only supported via the riak-admin bucket-type command-line tool.


2 Answers

Without knowing your concrete requirements, I'd say that dictionaries are probably better, if you want to pass around data. They are built-in, it's fast, simple and every programmer understands them, in contrast to something like value objects, where the developer will have to look up the specific attributes.

Examples:

  • at WSGI (Web Server Gateway Interface) core is an environments dictionary
  • Werkzeug's MultiDict is used to hold request arguments
  • flask's config is just a dict
  • ...

Also relevant in this context: Jack Diederich's Stop writing classes (YT):

Classes are great but they are also overused. This talk will describe examples of class overuse taken from real world code and refactor the unnecessary classes, exceptions, and modules out of them.

like image 50
miku Avatar answered Sep 21 '22 02:09

miku


For read-only data, named tuple is a good choice, you can access value by field name and it's more memory-efficient than a dictionary. In my opinion, data that get passed around should be made read-only.

Edit: For your use case, I think you should just pass the dictionary down the pipelines. Creating a class just for data storage and provide no other service just further complicate the code with little benefit.

like image 21
Kien Truong Avatar answered Sep 22 '22 02:09

Kien Truong