Re: easy question, how to double a variable
On Sep 22, 9:57 am, Grant Edwards wrote: > > No, no, no. The plan is to do his homework for him so that > he's incompetent when he graduates and won't be competition for > the rest of us who did do our homework. Don't forget the Peter principal --- we might end up working for him! Btw, I can't believe nobody provided the simplest literal solution: def twice(i): return i, i -- http://mail.python.org/mailman/listinfo/python-list
Re: Compute working days
How about: from datetime import date, timedelta # Define the weekday mnemonics to match the date.weekday function (MON, TUE, WED, THU, FRI, SAT, SUN) = range(7) def workdays(start_date, end_date, whichdays=(MON,TUE,WED,THU,FRI)): ''' Calculate the number of working days between two dates inclusive (start_date <= end_date). The actual working days can be set with the optional whichdays parameter (default is MON-FRI) ''' delta_days = (end_date - start_date).days + 1 full_weeks, extra_days = divmod(delta_days, 7) # num_workdays = how many days/week you work * total # of weeks num_workdays = (full_weeks + 1) * len(whichdays) # subtract out any working days that fall in the 'shortened week' for d in range(1, 8 - extra_days): if (end_date + timedelta(d)).weekday() in whichdays: num_workdays -= 1 return num_workdays -- http://mail.python.org/mailman/listinfo/python-list
Re: How to add months to a date (datetime object)?
On Mar 15, 2:00 pm, tinn...@isbd.co.uk wrote: > No, it's perfectly possible applying simple logic. My reminder > program manages it perfectly well. I have, for example, two sets of > three monthly reminders. > > One starts on Jan 19th and repeats three monthly, that means Jan > 19th, April 19th, July 19th and October 19th. Very simple. > > The other is a little more difficult, it starts on December 31st > and repeats three monthly. So that one is on December 31st, March > 31st, June 30th and September 30th. > > The calendar program manages it perfectly well using what seems to me > fairly obvious logic, what I want is to be able to do the same in > Python. The example you give does have fairly obvious logic. But how does it handle Feb 28th, 2009 + 3 months? To me, there are two "obvious" answers: May 28th, 2009 or May 31st, 2009. The question is intent; is Feb 28th an arbitrary day of the month, or is it the last day of the month, which for something like a monthly bill reminder is more likely? -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 18, 1:30 pm, Kottiyath wrote: > When we say readability counts over complexity, how do we define what > level of complexity is ok? > For example: > Say I have dict a = {'a': 2, 'c': 4, 'b': 3} > I want to increment the values by 1 for all keys in the dictionary. > So, should we do:>>> for key in a: > > ... a[key] = a[key] + 1 > or is it Ok to have code like: > dict(map(lambda key: (key, a[key] + 1), a)) > > How do we decide whether a level of complexity is Ok or not? This isn't just a question of readability; the two expressions are entirely different. The second expression creates a whole new dictionary, which might not have been obvious to you given the overall complexity of the expression. The first expression is simple, clear, and other than maybe changing "a[key] = a[key] + 1" to "a[key] += 1" is pretty much hard to improve on. If the number of lines matters to you (it shouldn't, be opinions vary), then you could always write: >>> for k in a: a[k] += 1 Which is shorter and far easier to read than the dict/map/lambda expression. And probably what you really intended! -- http://mail.python.org/mailman/listinfo/python-list
Re: strip char from list of strings
On May 18, 3:30 pm, Laurent Luce wrote: > I have the following list: > > [ 'test\n', test2\n', 'test3\n' ] > > I want to remove the '\n' from each string in place, what is the most > efficient way to do that ? > > Regards, > > Laurent Do you _really_ need to do this in place? If not, the simplest answer is probably: >>> x = ['test\n', test2\n', 'test3\n'] >>> x = [s.rstrip('\n') for s in x] And if what you really want to do is strip off all trailing whitespace (tabs, spaces, and newlines), then: >>> x = [s.rstrip() for s in x] A quick test of 1,000,000 strings of length 27 took less than 0.2 seconds on my PC. Efficiency isn't really much of an issue for most data sets. -- http://mail.python.org/mailman/listinfo/python-list
Re: performance problem with time.strptime()
On Jul 2, 7:30 am, Nils Rüttershoff wrote: > Rec = > re.compile(r"^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\s-\s\d+\s\[(\d{2}/\w+/\d{4}:\d{2}:\d{2}:\d{2})\s\+\d{4}\].*") > Line = '1.2.3.4 - 4459 [02/Jul/2009:01:50:26 +0200] "GET /foo HTTP/1.0" 200 - > "-" "www.example.org" "-" "-" "-"' I'm not sure how much it will help but if you are only using the regex to get the date/time group element, it might be faster to replace the regex with: >>> date_string = Line.split()[3][1:-1] -- http://mail.python.org/mailman/listinfo/python-list
Re: PEP368 and pixeliterators
On Jul 2, 4:32 am, Joachim Strömbergson wrote: > But, wouldn't it be more Pythonic and simpler to have an iterator that > iterates over all pixels in an image? Starting with upper left corner > and moving left-right and (line by line) to lower right. This would > change the code above to: Unless I'm totally misreading the PEP, the author does provide both iterators. Quoting the PEP: Non-planar images offer the following additional methods: pixels() -> iterator[pixel] Returns an iterator that iterates over all the pixels in the image, starting from the top line and scanning each line from left to right. See below for a description of the pixel objects. __iter__() -> iterator[line] Returns an iterator that iterates over all the lines in the image, from top to bottom. See below for a description of the line objects. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python Equivalent for dd & fold
On Jul 16, 10:12 am, seldan24 wrote: > On Jul 15, 1:48 pm, Emile van Sebille wrote: > > > > > On 7/15/2009 10:23 AM MRAB said... > > > >> On Jul 15, 12:47 pm, Michiel Overtoom wrote: > > >>> seldan24 wrote: > > what can I use as the equivalent for the Unix 'fold' command? > > >>> def fold(s,len): > > >>> while s: > > >>> print s[:len] > > >>> s=s[len:] > > > > > > You might still need to tweak the above code as regards how line endings > > > are handled. > > > You might also want to tweak it if the strings are _really_ long to > > simply slice out the substrings as opposed to reassigning the balance to > > a newly created s on each iteration. > > > Emile > > Thanks for all of the help. I'm almost there. I have it working now, > but the 'fold' piece is very slow. When I use the 'fold' command in > shell it is almost instantaneous. I was able to do the EBCDIC->ASCII > conversion usng the decode method in the built-in str type. I didn't > have to import the codecs module. I just decoded the data to cp037 > which works fine. > > So now, I'm left with a large file, consisting of one extremely long > line of ASCII data that needs to be sliced up into 35 character > lines. I did the following, which works but takes a very long time: > > f = open(ascii_file, 'w') > while ascii_data: > f.write(ascii_data[:len]) > ascii_data = ascii_data[len:] > f.close() > > I know that Emile suggested that I can slice out the substrings rather > than do the gradual trimming of the string variable as is being done > by moving around the length. So, I'm going to give that a try... I'm > a bit confused by what that means, am guessing that slice can break up > a string based on characters; will research. Thanks for the help thus > far. I'll post again when all is working fine. The problem is that it creates a new string every time you iterate through the "ascii_data = ascii_data[len:]". I believe Emile was suggesting that you just keep moving the starting index through the same string, something like (warning - untested code!): >>> i = 0 >>> str_len = len(ascii_data) >>> while i < str_len: >>> j = min(i + length, str_len) >>> print ascii_data[i:j] >>> i = j -- http://mail.python.org/mailman/listinfo/python-list
Re: Suggestions for Python MapReduce?
On Jul 22, 5:27 am, Phillip B Oldham wrote: > I understand that there are a number of MapReduce frameworks/tools > that play nicely with Python (Disco, Dumbo/Hadoop), however these have > "large" dependencies (Erlang/Java). Are there any MapReduce frameworks/ > tools which are either pure-Python, or play nicely with Python but > don't require the Java/Erlang runtime environments? I can't answer your question, but I would like to better understand the problem you are trying to solve. The Apache Hadoop/MapReduce java application isn't really that "large" by modern standards, although it is generally run with large heap sizes for performance (-Xmx1024m or larger for the mapred.child.java.opts parameter). MapReduce is designed to do extremely fast parallel data set processing on terabytes of data over hundreds of physical nodes; what advantage would a pure Python approach have here? -- http://mail.python.org/mailman/listinfo/python-list