473,654 Members | 3,062 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

head for grouped data - looking for best practice

Old, very old informatical problem: I want to "print" grouped data with
head information, that is:
eingabe=[
("Stuttgart","7 0197","Fernseht urm","20"),
("Stuttgart","7 0197","Brotmuse um","123"),
("Stuttgart","7 0197","Porsche" ,"123123"),
("Leipzig","014 91","Messe","91 822"),
("Leipzig","014 91","Schabidu", "9181231"),
]

shall give: ( Braces are not important...)

'Stuttgart', '70197'
--data-- ('Fernsehturm', '20')
--data-- ('Brotmuseum', '123')
--data-- ('Porsche', '123123')
Leipzig', '01491'
--data-- ('Messe', '91822')
--data-- ('Schabidu', '9181231')

my first approach was:
from itertools import groupby
from operator import itemgetter

for key, bereich in groupby(eingabe ,itemgetter(0)) :
print "Area:",key
headnotprinted= True
for data in bereich:
if headnotprinted:
headnotprinted= False
print "additional head info", data[1]
print "--data--", data[2:]

leading to:

Area: Stuttgart
additional head info 70197
--data-- ('Fernsehturm', '20')
--data-- ('Brotmuseum', '123')
--data-- ('Porsche', '123123')
Area: Leipzig
additional head info 01491
--data-- ('Messe', '91822')
--data-- ('Schabidu', '9181231')
which is quite what I expected. But ...
if headnotprinted:
headnotprinted= False
print "additional head info", data[1]

REALLY looks patched, not programmed.

my second try was:
def getdoublekey(ro w):
return row[0:2]

for key, bereich in groupby(eingabe ,getdoublekey):
print "Area:",key
for data in bereich:
print "--data--", data[2:]

which indeed leeds to the expected result, while looking less "hacky" ..
on the other hand side, that "getdoublek ey" ist not very flexible; when
doing the same with 3 Columns forming the head information, I have to
define the next function...

gettriplekey(ro w):
return (row[1], row[0], ---yadda yadda yadda

so, what is the best recommended practice for this usual problem within
Python?

Harald

Jul 18 '05 #1
4 1711
> which indeed leeds to the expected result, while looking less "hacky" ..
on the other hand side, that "getdoublek ey" ist not very flexible; when
doing the same with 3 Columns forming the head information, I have to
define the next function...


Make getdoublekey something like this (untested):

def get_key(f=0,t=1 ):
def _get_key(list_v alue):
return list_value[f:t]
return _get_key

Then use it like this:

for key, bereich in groupby(eingabe ,get_key(t=key_ size)):
****print*"Area :",key
****for*data*in *bereich:
********print*"--data--",*data[key_size:]

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Harald Massa wrote:
def getdoublekey(ro w):
return*row[0:2]

for key, bereich in groupby(eingabe ,getdoublekey):
print*"Area:",k ey
for*data*in*ber eich:
print*"--data--",*data[2:]

which indeed leeds to the expected result, while looking less "hacky" ..
on the other hand side, that "getdoublek ey" ist not very flexible; when
doing the same with 3 Columns forming the head information, I have to
define the next function...


Function creation is cheap and easily understood by someone reading your
code -- so you may already have the best solution. If Raymond Hettingers
recent suggestion on python-dev makes it into Python 2.5,
itemgetter()/attrgettter() could grow support for the extraction of
multiple attributes/items.

Anyway, here is a generalized getter factory that tries to handle all the
common cases in an intuitive way. E. g. you can create itemgetters using
the [] notation:
extract[::3](range(5)) [0, 3] extract[3](range(5)) 3 extract[0,3,4](range(5)) (0, 3, 4) import os
extract.path(os )

<module 'posixpath' from '/somewhere/posixpath.pyc'>

Peter

import itertools
import operator

def tuple_itemgette r(*keys):
"""Create a function that extracts a tuple of items from an
indexable object.
"""
# helper for extract
getters = map(operator.it emgetter, keys)
def get(obj):
return tuple(get(obj) for get in getters)
return get

def tuple_attrgette r(*names):
"""Create a function that extracts a tuple of attributes from an object.
"""
# helper for extract
getters = map(operator.at trgetter, names)
def get(obj):
return tuple(get(obj) for get in getters)
return get

class extract(object) :
"""Present unified access to the creation of
attribute and item getters.
"""
def __getitem__(sel f, index):
if isinstance(inde x, tuple):
return tuple_itemgette r(*index)
return operator.itemge tter(index)
def __getattribute_ _(self, name):
return operator.attrge tter(name)
def __call__(self, *names):
return tuple_attrgette r(*names)

extract = extract() # we only ever need one instance

if __name__ == "__main__":
# the demo is an anglo-german hotchpotch, really:
eingabe=[
("Stuttgart","7 0197","Fernseht urm","20"),
("Stuttgart","7 0197","Brotmuse um","123"),
("Stuttgart","7 0197","Porsche" ,"123123"),
("Leipzig","014 91","Messe","91 822"),
("Leipzig","014 91","Schabidu", "9181231"),
]
class Site(object):
def __init__(self, stadt, plz, name, nummer):
self.stadt = stadt
self.plz = plz
self.name = name
self.nummer = nummer
def __str__(self):
return "Site(stadt =%r, plz=%r, name=%r, nummer=%r)" % (
self.stadt, self.plz, self.name, self.nummer)
__repr__ = __str__

def show(iterable, groupkey):
print "-" * 20
for group, items in itertools.group by(iterable, groupkey):
print group
for item in items:
print "\t", item

show(eingabe, extract[1])
show(eingabe, extract[0, 1, 0:2])
show(eingabe, extract[0:2])
show((Site(*e) for e in eingabe), extract("stadt" , "plz"))
show((Site(*e) for e in eingabe), extract.stadt)

Jul 18 '05 #3
Harald Massa wrote:
def getdoublekey(ro w):
return row[0:2]

for key, bereich in groupby(eingabe ,getdoublekey):
print "Area:",key
for data in bereich:
print "--data--", data[2:]


Why don't you just pass a slice to itemgetter?

py> eingabe=[
.... ("Stuttgart","7 0197","Fernseht urm","20"),
.... ("Stuttgart","7 0197","Brotmuse um","123"),
.... ("Stuttgart","7 0197","Porsche" ,"123123"),
.... ("Leipzig","014 91","Messe","91 822"),
.... ("Leipzig","014 91","Schabidu", "9181231"),
.... ]
py> from itertools import groupby
py> from operator import itemgetter
py> for key, bereich in groupby(eingabe , itemgetter(slic e(0, 2))):
.... print "Area:", key
.... for data in bereich:
.... print "--data--", data[2:]
....
Area: ('Stuttgart', '70197')
--data-- ('Fernsehturm', '20')
--data-- ('Brotmuseum', '123')
--data-- ('Porsche', '123123')
Area: ('Leipzig', '01491')
--data-- ('Messe', '91822')
--data-- ('Schabidu', '9181231')

STeVe
Jul 18 '05 #4
Steve,

Why don't you just pass a slice to itemgetter?

py> for key, bereich in groupby(eingabe , itemgetter(slic e(0, 2))):
WHOW, that is great! that makes it really simple, just have to structure
the SQL to make a real "cut first, serve first" structure.

Thanks to all who helped!

also the "function factory function" bei Dietz was very helpfull; and
Peters classes looked really impressive!

Thanks again... now my code will be even clearer.

Harald
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
3018
by: D Witherspoon | last post by:
I am developing a Windows Forms application in VB.NET that will use .NET remoting to access the data tier classes. A very simple way I have come up with is by creating typed (.xsd) datasets. For example dsParts.xsd and including that in the data tier. I then will create a class that looks like this Public Class CPart Inherits dsParts
41
4685
by: laimis | last post by:
Hey guys, I just recently got introduced to data mappers (DTO mapper). So now I have a SqlHelper being used by DTOMapper and then business layer is using DTOMapper when it needs to persist object to database or load them back. Everything is working nicely so far. My question is, is it OK practice to use DTOMapper rfom the presentation layer? For instance, if I want to present in HTML format the list of entries in my database, should I...
3
2655
by: Marc Gravell | last post by:
Kind of an open question on best-practice for smart-client design. I'd really appreciate anyones views (preferably with reasoning, but I'll take what I get...). Or if anybody has any useful links on the subject? (and yes, I have already googled it at length, but still no strong decision) ============= After a long stint of pure-desktop / pure-server applications, I'm currently working on a number of smart-client projects in C# using...
4
4852
by: Paul Aspinall | last post by:
Hi I've previously constucted my .NET 1.1 data access, via a Data Access Layer (DAL), that was all in code. Now that I have access to the DataSet control in .NET 2.0, I'm looking for opinions or pointers into how to implement a DAL under .NET 2.0. Is it best to use controls or code?? Are there any best practice code samples around? Thanks
1
1537
by: Paul Aspinall | last post by:
Hi I've previously constucted my .NET 1.1 data access, via a Data Access Layer (DAL), that was all in code. Now that I have access to the DataSet control in .NET 2.0, I'm looking for opinions or pointers into how to implement a DAL under .NET 2.0. Is it best to use controls or code?? Are there any best practice code samples around? Thanks
3
4957
by: Furty | last post by:
Hi, I'm looking for the best practice for creating a generic data validation implementation for my data bound business objects. I currently have a business object base class implementing the following interfaces: IEditableObject, ICloneable, INotifyPropertyChanged, and IDataErrorInfo More specifically, my IDataErrorInfo implementation is like so:
3
2081
by: Rick | last post by:
I have a base class that inherits from System.Web.UI.Page that I need to include the ability to read the inheriting class/page meta tags. I can't find any methods in the System.Web.UI.Page members that provide access to do this. What is the practice to read meta information from the head section of an ASP.NET page? I am using VS.NET 1.1 C# (VB is fine).
13
3101
by: Alan Silver | last post by:
Hello, MSDN (amongst other places) is full of helpful advice on ways to do data access, but they all seem geared to wards enterprise applications. Maybe I'm in a minority, but I don't have those sorts of clients. Mine are all small businesses whose sites will never reach those sorts of scales. I deal with businesses whose sites get maybe a few hundred visitors per day (some not even that much) and get no more than ten orders per day....
3
4022
by: at_the_gonq | last post by:
Hello, I am hoping to get some guidance on the following scenerio: I have a password protected site where users have various permissions. Are sessions the best way of storing the user's id? And if so, on load of a page should I be hitting the database for their permissions (based on the session stored user id), or should everything I need be stored in session variables to save the trip to the database? I have also wondered about...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8707
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8482
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7306
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.