Trouble sorting a list of objects by attributes
Hello again, I've found myself stumped when trying to organize this list of objects. The objects in question are timesheets which i'd like to sort by four attributes: class TimeSheet: department = string engagement = string date = datetime.date stare_hour = datetime.time My ultimate goal is to have a list of this timesheet objects which are first sorted by departments, then within each department block of the list, have it organized by projects. Within each project block i finally want them sorted chronologically by date and time. To sort the strings i tried: timesheets.sort(key=operator.attrgetter('string')) which is not doing anything to my list unfortunately; it leaves it untouched. Giving up on that i tried to sort the dates or times using: def sorter_time(a,b): if a.engagement == engagement: # I only want sorting done within each engagement block return cmp(a.start, b.start) return 0 timesheets.sort(sorter_time) But this also did nothing to my list, and at this point i'm incredibly confused. From what i know of the involved functions, i would think that these lines of code (while they wouldn't do exactly what i want yet) would at least do something to my lists. Any explanations, or suggestions for a different approach would be greatly appreciated. My brain might explode if i continue. -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > Robocop: > > >then within each department block of the list, have it organized by > >projects.< > > I don't know what does it means. > > > timesheets.sort(key=operator.attrgetter('string')) > > Try something like: > timesheets.sort(key=attrgetter("department", "engagement", "date", > "stare_hour")) > > > My brain might explode if i continue. > > Relax. > > Bye, > bearophile Projects was meant to be engagements. All these suggestions are great, let me see what i can make happen right now. Thanks all! -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > Robocop: > > >then within each department block of the list, have it organized by > >projects.< > > I don't know what does it means. > > > timesheets.sort(key=operator.attrgetter('string')) > > Try something like: > timesheets.sort(key=attrgetter("department", "engagement", "date", > "stare_hour")) > > > My brain might explode if i continue. > > Relax. > > Bye, > bearophile UH OH GUYS! line 110, in sorter timesheets.sort(key=attrgetter("department", "engagement", "date","start")) TypeError: attrgetter expected 1 arguments, got 4 -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 2:17 pm, Robocop wrote: > On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > > > > > Robocop: > > > >then within each department block of the list, have it organized by > > >projects.< > > > I don't know what does it means. > > > > timesheets.sort(key=operator.attrgetter('string')) > > > Try something like: > > timesheets.sort(key=attrgetter("department", "engagement", "date", > > "stare_hour")) > > > > My brain might explode if i continue. > > > Relax. > > > Bye, > > bearophile > > UH OH GUYS! > > line 110, in sorter > timesheets.sort(key=attrgetter("department", "engagement", > "date","start")) > TypeError: attrgetter expected 1 arguments, got 4 I think there may have been a misunderstanding. I was already using attrgetter, my problem is that it doesn't appear to be sorting by the argument i give it. How does sort work with strings? How about with datetime.time or datetime.date? So far i can get it sorting strictly by the datetime objects, but i need all of this sorting done within the constraints imposed by doing sorts via department and engagements. Any ideas? -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 2:20 pm, Robocop wrote: > On Feb 6, 2:17 pm, Robocop wrote: > > > > > On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > > > > Robocop: > > > > >then within each department block of the list, have it organized by > > > >projects.< > > > > I don't know what does it means. > > > > > timesheets.sort(key=operator.attrgetter('string')) > > > > Try something like: > > > timesheets.sort(key=attrgetter("department", "engagement", "date", > > > "stare_hour")) > > > > > My brain might explode if i continue. > > > > Relax. > > > > Bye, > > > bearophile > > > UH OH GUYS! > > > line 110, in sorter > > timesheets.sort(key=attrgetter("department", "engagement", > > "date","start")) > > TypeError: attrgetter expected 1 arguments, got 4 > > I think there may have been a misunderstanding. I was already using > attrgetter, my problem is that it doesn't appear to be sorting by the > argument i give it. How does sort work with strings? How about with > datetime.time or datetime.date? > > So far i can get it sorting strictly by the datetime objects, but i > need all of this sorting done within the constraints imposed by doing > sorts via department and engagements. > > Any ideas? I'm stuck with python 2.4 right now:( -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 2:34 pm, Robocop wrote: > On Feb 6, 2:20 pm, Robocop wrote: > > > > > On Feb 6, 2:17 pm, Robocop wrote: > > > > On Feb 6, 1:03 pm, bearophileh...@lycos.com wrote: > > > > > Robocop: > > > > > >then within each department block of the list, have it organized by > > > > >projects.< > > > > > I don't know what does it means. > > > > > > timesheets.sort(key=operator.attrgetter('string')) > > > > > Try something like: > > > > timesheets.sort(key=attrgetter("department", "engagement", "date", > > > > "stare_hour")) > > > > > > My brain might explode if i continue. > > > > > Relax. > > > > > Bye, > > > > bearophile > > > > UH OH GUYS! > > > > line 110, in sorter > > > timesheets.sort(key=attrgetter("department", "engagement", > > > "date","start")) > > > TypeError: attrgetter expected 1 arguments, got 4 > > > I think there may have been a misunderstanding. I was already using > > attrgetter, my problem is that it doesn't appear to be sorting by the > > argument i give it. How does sort work with strings? How about with > > datetime.time or datetime.date? > > > So far i can get it sorting strictly by the datetime objects, but i > > need all of this sorting done within the constraints imposed by doing > > sorts via department and engagements. > > > Any ideas? > > I'm stuck with python 2.4 right now:( I've got a python 2.4 fix though! timesheets.sort(key= lambda i:(i.department,i.engagement,i.start)) Thanks for the help and time! -- http://mail.python.org/mailman/listinfo/python-list
Re: Trouble sorting a list of objects by attributes
On Feb 6, 2:41 pm, Stephen Hansen wrote: > > I think there may have been a misunderstanding. I was already using > > attrgetter, my problem is that it doesn't appear to be sorting by the > > argument i give it. How does sort work with strings? How about with > > datetime.time or datetime.date? > > You were using the attrgetter, but it looks like you weren't doing it > correctly: > > timesheets.sort(key=operator.attrgetter('string')) > > You don't specify a type, be it string or date or anything: you > specify the /name/ of the attribute to get. It'll handle sorting by > any data type that has an order to it without you having to worry > about it at all. Strings, ints, dates, times. > > It should be: > > timesheets.sort(key=operator.attrgetter("department")) > > If you want to sort by the value of the "department" attribute, which > happens to be a string. > If you want to sort by the "date" attribute, which happens to be a > datetime.date, you do: > timesheets.sort(key=operator.attrgetter("date")) > > Where date is not a datatype a la datetime.date, but the name of an > attribute on your TimeSheet instances. > > --S Yeah this i totally understand. In my code i was using attrgetter correctly, in the above answer i used string to represent the datatype of the attribute, not what i actually put in there. More my laziness than anything else. The problem boiled down attrgetter taking multiple arguments in 2.5, but only one in 2.4. Thanks for the input though. -- http://mail.python.org/mailman/listinfo/python-list
Text parsing via regex
I'm having a little text parsing problem that i think would be really quick to troubleshoot for someone more versed in python and Regexes. I need to write a simple script that parses some arbitrarily long string every 50 characters, and does not parse text in the middle of words (but ultimately every parsed string should be 50 characters, so adding in white spaces is necessary). So i immediately came up with something along the lines of: string = "a bunch of nonsense that could be really long, or really short depending on the situation" r = re.compile(r".{50}") m = r.match(string) then i started to realize that i didn't know how to do exactly what i wanted. At this point i wanted to find a way to simply use something like: parsed_1, parsed_2,...parsed_n = m.groups() However i'm having several problems. I know that playskool regular expression i wrote above will only parse every 50 characters, and will blindly cut words in half if the parsed string doesn't end with a whitespace. I'm relatively new to regexes and i don't know how to have it take that into account, or even what type of logic i would need to fill in the extra whitespaces to make the string the proper length when avoiding cutting words up. So that's problem #1. Problem #2 is that because the string is of arbitrary length, i never know how many parsed strings i'll have, and thus do not immediately know how many variables need to be created to accompany them. It's easy enough with each pass of the function to find how many i will have by doing: mag = len(string) upper_lim = mag/50 + 1 But i'm not sure how to declare and set them to my parsed strings. Now problem #1 isn't as pressing, i can technically get away with cutting up the words, i'd just prefer not to. The most pressing problem right now is #2. Any help, or suggestions would be great, anything to get me thinking differently is helpful. -- http://mail.python.org/mailman/listinfo/python-list
Re: Text parsing via regex
Wow! Thanks for all the input, it looks like that textwrapper will work great for my needs. And thanks for the regex help everyone. Also, i was thinking of using a list, but i haven't used them much in python. Is there anything in python that is equivalent to pushback in c++ for vectors? As in, could i just initialize a list, and then pushback values into it as i need them? Thanks again! -- http://mail.python.org/mailman/listinfo/python-list
list organization question
I have a list of objects, each object having two relevant attributes: date and id. I'd like not only organize by id, but also by date. I.e. i would like to parse my list into smaller lists such that each new mini-list has a unique date, but consists of only objects with a specific id. Are there any handy imports i could use to accomplish something like this? I'm relatively new to python and as such don't know all of the preloaded functions at my disposal. Thanks for any help in advance, the community here is always ridiculously helpful. -- http://mail.python.org/mailman/listinfo/python-list
Re: list organization question
I'm currently trying something along the lines of a sort.compare, but as i'm never sure how many mini-lists i'll end up with, i'm not sure how exactly to begin. Maybe something like a C vector, i.e. a list of pointers to other lists? Or more specifically, compare dates in my list, push that into some empty dates[], then do something along the lines of for looping over dates to create subset lists, and nesting some more compares within these lists to further sort the data by id. Sound crazy or plausible? -- http://mail.python.org/mailman/listinfo/python-list
Re: list organization question
On Dec 11, 3:31 pm, Arnaud Delobelle wrote: > Robocop writes: > > I have a list of objects, each object having two relevant attributes: > > date and id. I'd like not only organize by id, but also by date. > > I.e. i would like to parse my list into smaller lists such that each > > new mini-list has a unique date, but consists of only objects with a > > specific id. Are there any handy imports i could use to accomplish > > something like this? I'm relatively new to python and as such don't > > know all of the preloaded functions at my disposal. Thanks for any > > help in advance, the community here is always ridiculously helpful. > > Look at itertools.groupby. > > E.g. > > from itertools import groupby > > data = [ my objects ] > > def getdate(x): return x.date > > by_date = {} > for date, objs in groupby(sorted(data, key=getdate), getdate): > by_date[date] = list(objs) > # list(objs) is the list of all objects in data whose date is date > > I'm sorry if this is a bit terse... > > -- > Arnaud This looks to be exactly what i'm looking for! I will try and implement this immediately, thank you all for the suggestions. -- http://mail.python.org/mailman/listinfo/python-list
Script can't find input file despite being in the same directory
I have a simple little script that reads in postscript code, appends it, then writes it to a new postscript file. Everything worked fine a month ago, but after rearranging my directory tree a bit my script fails to find the base postscript file. The line in question is: for line in fileinput.input(['base.ps']): output.write(line) I'm kind of at a loss as the script is executing in the same directory as base.ps, yet it can't find it. I'm relatively new to python scripting, so i'm expecting it's just something i haven't learned about python that is causing the problem. Any suggestions would be greatly appreciated. -- http://mail.python.org/mailman/listinfo/python-list
Re: Script can't find input file despite being in the same directory
On Oct 17, 10:27 am, "Chris Rebert" <[EMAIL PROTECTED]> wrote: > On Fri, Oct 17, 2008 at 10:07 AM, Robocop <[EMAIL PROTECTED]> wrote: > > I have a simple little script that reads in postscript code, appends > > it, then writes it to a new postscript file. Everything worked fine a > > month ago, but after rearranging my directory tree a bit my script > > fails to find the base postscript file. > > > The line in question is: > > > for line in fileinput.input(['base.ps']): > > output.write(line) > > What directory is output if you insert the lines: > > from os import getcwd > print "CWD:", getcwd() > > just before the line is question? > > Cheers, > Chris > -- > Follow the path of the Iguana...http://rebertia.com > > > > > I'm kind of at a loss as the script is executing in the same directory > > as base.ps, yet it can't find it. I'm relatively new to python > > scripting, so i'm expecting it's just something i haven't learned > > about python that is causing the problem. Any suggestions would be > > greatly appreciated. > > -- > >http://mail.python.org/mailman/listinfo/python-list > > The output is /home/bruce/DEVadhc/attendance which is the directory i expected, and the directory that contains base.ps -- http://mail.python.org/mailman/listinfo/python-list
Re: Script can't find input file despite being in the same directory
On Oct 17, 10:27 am, "Chris Rebert" <[EMAIL PROTECTED]> wrote: > On Fri, Oct 17, 2008 at 10:07 AM, Robocop <[EMAIL PROTECTED]> wrote: > > I have a simple little script that reads in postscript code, appends > > it, then writes it to a new postscript file. Everything worked fine a > > month ago, but after rearranging my directory tree a bit my script > > fails to find the base postscript file. > > > The line in question is: > > > for line in fileinput.input(['base.ps']): > > output.write(line) > > What directory is output if you insert the lines: > > from os import getcwd > print "CWD:", getcwd() > > just before the line is question? > > Cheers, > Chris > -- > Follow the path of the Iguana...http://rebertia.com > > > > > I'm kind of at a loss as the script is executing in the same directory > > as base.ps, yet it can't find it. I'm relatively new to python > > scripting, so i'm expecting it's just something i haven't learned > > about python that is causing the problem. Any suggestions would be > > greatly appreciated. > > -- > >http://mail.python.org/mailman/listinfo/python-list > > Also i forgot this pertinent line: output = open("/home/bruce/attend/ media/ps/barcodes.ps", "w") -- http://mail.python.org/mailman/listinfo/python-list
Re: Script can't find input file despite being in the same directory
I'm kind of an idiot, i just realized the problem. Sorry for wasting your time, and thanks for the help! -- http://mail.python.org/mailman/listinfo/python-list
quick newbie syntax question
Is it possible to do something like this syntactically: year = '2008' month = '09' limit = '31' for i in range(1,limit): temp = Table.objects.filter(date = year'-'month'-'i)
Re: quick newbie syntax question
oops! Sorry about that, i should have just copied my code directly. I actually did specify an int in range: > > year = '2008' > > month = '09' > > limit = '31' > > for i in range(1,int(limit)): The code is currently failing due to the syntax in the filter, particularly the section "date = year'-'month'-'i" -- http://mail.python.org/mailman/listinfo/python-list
Re: quick newbie syntax question
date = "%s-%s-%s" % (year, month, i) is exactly what i'd like to do. The Table object will just be a mysql table, and the filter function will yield a list filtered for those dates. For my purposes the limit variable will not be static, depending on which day of the month it is i will only want it to iterate up to that date in the month (i use 31 here as an example as i would want it to iterate through the 30th of september). Thanks for the input! On Oct 20, 1:21 pm, Larry Bates <[EMAIL PROTECTED]> wrote: > Robocop wrote: > > oops! Sorry about that, i should have just copied my code directly. > > I actually did specify an int in range: > >>> year = '2008' > >>> month = '09' > >>> limit = '31' > >>> for i in range(1,int(limit)): > > > The code is currently failing due to the syntax in the filter, > > particularly the section "date = year'-'month'-'i" > > I believe you want something more like > > date = "%s-%s-%s" % (year, month, i) > > but then again I can't quite figure out what is is that you are wanting to do > (Note: September doesn't have 31 days). > > Where did Table object come from and what does the table.objects.filter method > do and what are the appropriate arguments to that method? If it was "my" > method, and I wanted to use it this way, I would think about changing it so > that > it accepted a filtering function and returned only those objects that passed > the > filtering function: > > def myFilter(obj): > return (obj.date >= '2008-09-01' and obj.date <= '2008-09-30') > > class objects(object): > def __init__(self): > self.objects = [] > > def filter(filterFunc): > return [x for x in self.objects if filterFunc(x)] > > Then > > temp = Table.objects.filter(myFilter) > > temp is a list of objects you can act on. > > -Larry -- http://mail.python.org/mailman/listinfo/python-list
Python barcode decoding
Does anyone know of any decent (open source or commercial) python barcode recognition tools or libraries. I need to read barcodes from pdfs or images, so it will involve some OCR algorithm. I also only need to read the code 93 symbology, so it doesn't have to be very fancy. The most important thing to me is that it outputs in some python friendly way, or ideally that it is written in python. Any tips would be great! -- http://mail.python.org/mailman/listinfo/python-list
Re: Python barcode decoding
On Oct 24, 1:24 pm, Mike Driscoll <[EMAIL PROTECTED]> wrote: > On Oct 24, 12:05 pm, Robocop <[EMAIL PROTECTED]> wrote: > > > Does anyone know of any decent (open source or commercial) python > >barcoderecognition tools or libraries. I need to read barcodes from > > pdfs or images, so it will involve some OCR algorithm. I also only > > need to read the code 93 symbology, so it doesn't have to be very > > fancy. The most important thing to me is that it outputs in some > > python friendly way, or ideally that it is written in python. Any > > tips would be great! > > The closest thing I've heard of is the bar code stuff in reportlab. > This post talks a little about it: > > http://mail.python.org/pipermail/python-list/2000-February/024893.html > > And here's the official Reportlab site > link:http://www.reportlab.org/downloads.html > > Mike Thanks for the tip!! -- http://mail.python.org/mailman/listinfo/python-list
Re: Python barcode decoding
On Oct 24, 1:24 pm, Mike Driscoll <[EMAIL PROTECTED]> wrote: > On Oct 24, 12:05 pm,Robocop<[EMAIL PROTECTED]> wrote: > > > Does anyone know of any decent (open source or commercial) python > > barcode recognition tools or libraries. I need to read barcodes from > > pdfs or images, so it will involve some OCR algorithm. I also only > > need to read the code 93 symbology, so it doesn't have to be very > > fancy. The most important thing to me is that it outputs in some > > python friendly way, or ideally that it is written in python. Any > > tips would be great! > > The closest thing I've heard of is the bar code stuff in reportlab. > This post talks a little about it: > > http://mail.python.org/pipermail/python-list/2000-February/024893.html > > And here's the official Reportlab site > link:http://www.reportlab.org/downloads.html > > Mike I appreciate the effort, but that looks to be for generation of barcodes if i'm not mistaken. What i'm looking for is a barcode DECODER, not an ENCODER (i'm currently using the purepostscript barcode script for generation, very slick stuff), so i'm looking for something along the lines of some barcode specific OCR libraries. -- http://mail.python.org/mailman/listinfo/python-list
possible newline problems
I'm running some stupid little script that's supposed to alert me if some fuse link exists. All i do is read in /proc/mounts and look to match the fuse mount command in question, i'm doing this: output = open("/www/htdocs/hatProductAdd/add/output.txt", "a") for line in fileinput.input(['/proc/mounts']): output.write(line) if line == '[EMAIL PROTECTED]:/usr/home/sites/www.website.com/ web/PICTURES/django /www/htdocs/hatProductAdd/media/images/PICTURES/ django fuse rw,nosuid,nodev,user_id=0,group_id=0,max_read=65536 0 0': print "it's mounted" else: print "it's not mounted" For some reason though, it never properly matches the fuse line, even if it's there. When i straight up print the lines as they're read i see "/proc/mounts", but every line has an extra newline between them. Should i be trying to mtch the newline character as well? Any help would be great! -- http://mail.python.org/mailman/listinfo/python-list
Python based barcode readers
Does anyone know of any python based barcode readers? I'm looking for something (commercial or open source) that will use some OCR algorithm to read barcodes from an image or ps/pdf file, and ideally will be something along the lines of a callable python script. I have some pretty simple needs, i'm only trying to read code 93 barcodes, so i don't need it to be able to identify multiple symbologies or anything. Any suggestions would be greatly appreciated. Thanks! -- http://mail.python.org/mailman/listinfo/python-list
Re: Python based barcode readers
I also forgot to mention that it need not be nearly as robust as something like Jailhelper 2.0, I will not really need to compensate for noise and irregular conditions. All of my barcodes will be scanned in a predictable, and consistent environment (i.e. a scanner), so all i need is some stupid little script that will read the an image in and decode it. -- http://mail.python.org/mailman/listinfo/python-list