~lottanzb/lottanzb/trunk-packaging-9.10

« back to all changes in this revision

Viewing changes to lottanzb/backend/hubs/__init__.py

  • Committer: Severin Heiniger
  • Date: 2010-09-03 15:05:11 UTC
  • mfrom: (1212.1.209 trunk)
  • Revision ID: severinheiniger@gmail.com-20100903150511-rhpcpr3td53h4ihm
Sync with main.

Show diffs side-by-side

added added

removed removed

Lines of Context:
174
174
    @property
175
175
    def short(self):
176
176
        """
177
 
        Return a pretty string representation of the `TimeDelta` like '12 hours'
178
 
        or '1 second'. Because a string like '1 hour' is not precise enough,
179
 
        it will also include the number of minutes in that case:
180
 
        '1 hour and 23 minutes'.
 
177
        Return a pretty, localized string representation of the `TimeDelta` like
 
178
        '12 hours' or '1 second'.
 
179
        
 
180
        Because a string like '1 hour' is not precise enough, it must also
 
181
        include information about the value of the next smaller time scale, in
 
182
        this case minutes.
 
183
        
 
184
        Because such values will often be imprecise (especially if they're
 
185
        estimations), the value of the next smaller time scale will be rounded
 
186
        down.
 
187
        
 
188
        This means that instead of '1 hour and 43 minutes', the returned string
 
189
        will be '1 hour and 40 minutes'. For estimations that are likely to
 
190
        change over time, this causes the return values to be changed less
 
191
        frequently.
 
192
        
 
193
        Consider the following example return values:
 
194
        
 
195
        >>> TimeDelta(seconds=46).short
 
196
        '46 seconds'
 
197
        >>> TimeDelta(minutes=2, seconds=3).short
 
198
        '2 minutes'
 
199
        >>> TimeDelta(minutes=2, seconds=33).short
 
200
        '2 minutes and 30 seconds'.
 
201
        >>> TimeDelta(hours=2, minutes=3).short
 
202
        '2 hours'
 
203
        >>> TimeDelta(hours=2, minutes=33).short
 
204
        '2 hours and 30 minutes'.
 
205
        >>> TimeDelta(days=2, minutes=57)
 
206
        '2 days'
 
207
        >>> TimeDelta(days=1, hours=18)
 
208
        '1 day and 18 hours'
181
209
        """
182
210
        
183
211
        # Use 'seconds' as the default major time scale so that if the
197
225
                break
198
226
        
199
227
        if minor_scale is None:
 
228
            # `minor_scale' is None because `major_scale' is 'seconds'.
200
229
            return self._get_simple_string(major_scale)
201
230
        else:
 
231
            minor_scale_value = self._get_partitioned_value(minor_scale)
 
232
            
 
233
            if minor_scale in ("minutes", "seconds"):
 
234
                # Only round minutes and seconds.
 
235
                minor_scale_value = minor_scale_value / 10 * 10
 
236
            
 
237
            if not minor_scale_value:
 
238
                # Don't return something like '1 minute and 0 seconds', but
 
239
                # simply '1 minute'.
 
240
                return self._get_simple_string(major_scale)
 
241
            
 
242
            rounded_time_delta = TimeDelta(**{
 
243
                major_scale: self._get_partitioned_value(major_scale),
 
244
                minor_scale: minor_scale_value
 
245
            })
 
246
            
202
247
            return _("%(major_time_scale)s and %(minor_time_scale)s") % {
203
 
                "major_time_scale": self._get_simple_string(major_scale),
204
 
                "minor_time_scale": self._get_simple_string(minor_scale)
 
248
                "major_time_scale":
 
249
                    rounded_time_delta._get_simple_string(major_scale),
 
250
                "minor_time_scale":
 
251
                    rounded_time_delta._get_simple_string(minor_scale)
205
252
            }
206
253
    
207
254
    def _get_partitioned_value(self, scale):