In [29]: (c -d).days Out[29]: 1 In [30]: a = datetime(2012, 9, 23) In [31]: a + timedelta(months=1) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-31-8ba71fa4f0e5> in <module>() ----> 1 a + timedelta(months=1) TypeError: 'months' is an invalid keyword argument for this function In [33]: from dateutil.relativedelta import relativedelta In [34]: a + relativedelta(months=+1) Out[34]: datetime.datetime(2012, 10, 23, 0, 0) In [35]: a + relativedelta(months=+4) Out[35]: datetime.datetime(2013, 1, 23, 0, 0) In [36]: b = datetime(2012, 12, 21) In [37]: d = b - a In [38]: d Out[38]: datetime.timedelta(89) In [39]: d = relativedelta(b, a) In [40]: d
def aportaciones(self): if self.proj=="commons": dbname="commonswiki_p" elif self.proj=="meta": dbname="metawiki_p" elif wikipro[self.proj]=="wikipedia": dbname=self.lang+"wiki_p" elif wikipro[self.proj]=="wikibooks": dbname=self.lang+"wikibooks_p" elif wikipro[self.proj]=="wikiquote": dbname=self.lang+"wikiquote_p" elif wikipro[self.proj]=="wikinews": dbname=self.lang+"wikinews_p" elif wikipro[self.proj]=="wikisource": dbname=self.lang+"wikisource_p" elif wikipro[self.proj]=="wikiversity": dbname=self.lang+"wikiversity_p" elif wikipro[self.proj]=="wiktionary": dbname=self.lang+"wiktionary_p" else: #print "[net.py #222] Proyecto erroneo" return user=self.user user=urllib.quote(user.encode('utf-8','replace')) t=pageText('http://tools.wikimedia.de/~interiot/cgi-bin/count_edits?user='******'&dbname='+dbname) #t=pageText('http://tools.wikimedia.de/~kate/cgi-bin/count_edits?user='******'utf-8'))+'&dbname='+dbname) general=ur"(?us)Username</th><td colspan='2'>(?P<user>.*?)<.*?Total edits</th><td colspan='2'>(?P<total>.*?)<.*?User groups</a><td colspan=2>(?P<flags>.*?)\s*<.*?Image uploads</th><td colspan='2'>(?P<images>.*?) \(.*?Distinct pages edited</th><td colspan='2'>(?P<distinct>.*?)<.*?Deleted edits</th><td colspan='2'>(?P<delete1>.*?)\s*<.*?First edit</th><td colspan='2'>(?P<first>.*?)<.*?" namespaces=ur"(?us)<tr class='nsn'><td class='nsname'><.*?class='nedits'.*?namespace=(?P<ns>\d*).*?>(?P<cuantas>\d*)<" logging=ur"(?us)<tr class='nsn'><td class='nsname'><.*?(?P<action>Delete|Restore|Block|Unblock|Protect|Unprotect|Move)</a></td><td class='nedits'><a.*?>(?P<cuantas>\d*)</a>" mg=re.search(general,t) mn=re.findall(namespaces,t) ml=re.findall(logging,t) apor = u"" nick = u"%s:%s:User:%s" % (str(self.proj),str(self.lang),self.user) if not self.quiet else "the user" if mg: #apor=u"%s:%s:User:%s has made " % (str(self.proj),str(self.lang),str(urllib.quote(self.user.encode('utf-8')))) apor=u"%s has made " % nick apor+=u"%s edits. " % (mg.group('total')) apor+=u"%s in different pages. " % (mg.group('distinct')) for i in mn: if int(i[0])==0: apor+=u"%s in articles. " % (str(i[1])) break mf=re.search(ur"(?P<ano>\d{4})/(?P<mes>\d{2})/(?P<dia>\d{2}) (?P<hora>\d{2}):(?P<min>\d{2}):(?P<sec>\d{2})", mg.group('first')) if mf: firstedit_date=datetime(int(mf.group("ano")),int(mf.group("mes")),int(mf.group("dia")),int(mf.group("hora")),int(mf.group("min")),int(mf.group("sec"))) diff = relativedelta(datetime.now(),firstedit_date) diff={u"years": diff.years, "months": diff.months, u"days": diff.days} sq=[] sorted=[u"years","months",u"days"] for txt in sorted: if diff[txt] > 1: sq.append("%d %s" % (diff[txt], txt)) elif diff[txt] == 1: sing={u"years": u"year", "months": "month", u"days": u"day"} sq.append("%d %s" % (diff[txt], sing[txt])) if len(sq)>1: txt=", ".join(sq[:-1]) txt+=" and %s" % sq[-1] apor+=txt elif sq: apor+=sq[0] apor+=u" old (%s/%s/%s %s:%s:%s). " % (mf.group("dia"),mf.group("mes"), mf.group("ano"), mf.group("hora"),mf.group("min"),mf.group("sec")) if int(mg.group('images'))>0: apor+=u"%s uploaded images. " % (mg.group('images')) if mg.group('flags'): if user.lower() in self.flags: flags=self.flags[user.lower()] else: flags=mg.group('flags') apor+=u"Flags: %s. " % flags apor+=u"%s deleted contributions. " % (mg.group('delete1')) for i in ml: if i[0]=='Delete': apor+=u"%s deletions. " % (str(i[1])) break else: pass #print "No" if not self.quiet: if specialwikipro.has_key(wikipro[self.proj]): #Si es un proyecto especial apor+=u"\u000315".encode('utf-8')+"(http://"+specialwikipro[wikipro[self.proj]] else: apor+=u"\u000315".encode('utf-8')+"(http://"+str(self.lang)+"."+wikipro[self.proj]+".org" apor+=u"/wiki/Special:Contributions/"+user+")" self.conexion.privmsg(self.chan,apor.encode('utf-8'))
def razlika(ime): now = datetime.now() a = relativedelta(now,ime) return a