Top.Mail.Ru
? ?

Entries by tag: python

How to simplify such code?


 1 def generate_time_string(t):

 2   d = datetime.datetime.today().utcnow() - t

 3   if d.days < 0:

 4     sec = - (d.days * 24 * 60 * 60 + d.seconds)

 5   else:

 6     sec = d.days * 24 * 60 * 60 + d.seconds

 7   min = sec / 60

 8   hour = min / 60

 9   day = hour / 24

10   if day > 0:

11     if d.days < 0:

12       return gettext.ngettext("%d day in the future", \

13         "%d days in the future", day) % day

14     else:

15       return gettext.ngettext("%d day ago", \

16         "%d days ago", day) % day

17   elif hour > 0:

18     if d.days < 0:

19       return gettext.ngettext("%d hour in the future", \

20         "%d hours in the future", hour) % hour

21     else:

22       return gettext.ngettext("%d hour ago", \

23         "%d hours ago", hour) % hour

24   elif min > 0:

25     if d.days < 0:

26       return gettext.ngettext("%d minute in the future", \

27         "%d minutes in the future", min) % min

28     else:

29       return gettext.ngettext("%d minute ago", \

30         "%d minutes ago", min) % min

31   else:

32     if d.days < 0:

33       return gettext.ngettext("%d second in the future", \

34         "%d seconds in the future", sec) % sec

35     else:

36       return gettext.ngettext("%d second ago", \

37         "%d seconds ago", sec) % sec

Tags:

python 3 的 print 啊...

原来 python 2.5

[yuan@mstar ~]$ python
Python 2.5.1 (r251:54863, Aug 28 2007, 09:58:30)
[GCC 4.1.2 20070821 (Red Hat 4.1.2-19)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print)
  File "<stdin>", line 1
    type(print)
             ^
SyntaxError: invalid syntax
>>> help(print)
  File "<stdin>", line 1
    help(print)
             ^
SyntaxError: invalid syntax
>>> print "abc"
abc
>>> print

>>>

现在呢

[yuan@mstar ~]$ python3.0
Python 3.0a1 (py3k, Sep  2 2007, 00:35:47)
[GCC 4.1.2 20070821 (Red Hat 4.1.2-19)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print)
<type 'builtin_function_or_method'>
>>> help(print)

>>> print "abc"
  File "<stdin>", line 1
    print "abc"
              ^
SyntaxError: invalid syntax
>>> print
<built-in function print>
>>> print("abc")
abc
>>>


又,从 http://ivazquez.livejournal.com/2436.html 这里搞到的 srpm,稍微改一点,刚装上。 看 pep 3099 了, http://www.python.org/dev/peps/pep-3099/ 还有 pep 3000 http://www.python.org/dev/peps/pep-3000/ ,觉得个人喜好真是够让人热血

不知道什么时候会有 alternatives 切换的 python,不过很有可能不会那么搞,而只是这样简单的打个包了事,反正针对这个分支的代码还少。

Tags:

碰到点 python 代码

yum 的 fastestmirror 插件报错
repo.urls = fastestmirror[str(repo)]

看 repo.urls 的定义,在 yumRepo.py 里面
466     def _geturls(self):
467         if not self._urls:
468             self._baseurlSetup()
469         return self._urls
470 
471     urls = property(lambda self: self._geturls())

根据这段代码,fastestmirror 插件中直接设置 urls 的作法就行不通了,而设置 _urls 又很别扭。 property() 是新增加的吗,是不是说在访问属性时才会计算的,因为实际上是定义了一个函数。怎么看它的类型呢?

update:
查了一会儿文档,原来在很早的 2.2 有了 new class 就有了 property,果然危险,对这个东西了解太少了。

property 只有一个参数时是 readonly (getter),第二个参数是 setter

顺便又看了下 python 2.5 的 changes,提到了 generator:
about Generators and coroutines
http://www.sidhe.org/~dan/blog/archives/000178.html

记得以前看过一篇这样的文章,讲 perl 和 LISP 中的 continuations,死活看不懂。

总之是不停地慨叹 python 文档之多,文档质量之高。就连随便搜索的 blog 文章的一小段代码也是那样,简单而能说明问题。

fedora extras 中 perl 的 modules 远比 python modules 多,为什么呢?

今天很有心情看代码,一天看的东西比前面一年都多。感谢这个假期。我希望自己能坚持一下。 :(

Tags:

Latest Month

November 2025
S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829
30      

Syndicate

RSS Atom

Comments

  • bbbush
    4 Jan 2025, 00:05
    LiveJournal is celebrating New Year!
    Time to recall this year's moments in the holiday photochallenge. Get 5 random photos uploaded to the blog in 2024 and share them with the audience!
    Have an…
  • bbbush
    5 Nov 2024, 02:54
    let me appreciate the flexible work time policy and manager's understanding, too.
  • bbbush
    1 Sep 2023, 16:35
    Heart Palpitations https://www.pbmchealth.org/news-events/blog/why-does-my-heart-beat-fast-when-i-lay-down Gastrocardiac Syndrome…
  • bbbush
    19 Mar 2023, 06:07
    烧钱
  • bbbush
    6 Dec 2022, 05:04
    the last night before final class was spent working on some code stuff — for work. Read Chinese translations instead.. cannot understand anyway. It is less confusing read the original text, but it…
Powered by LiveJournal.com
Designed by Tiffany Chow
Image