Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6007667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:42:48+00:00 2026-05-23T01:42:48+00:00

I found this snippet: def timesince(dt, default=just now): now = datetime.utcnow() diff = now

  • 0

I found this snippet:

def timesince(dt, default="just now"):
        now = datetime.utcnow()
        diff = now - dt
        periods = (
                (diff.days / 365, "year", "years"),
        (diff.days / 30, "month", "months"),
        (diff.days / 7, "week", "weeks"),
        (diff.days, "day", "days"),
        (diff.seconds / 3600, "hour", "hours"),
        (diff.seconds / 60, "minute", "minutes"),
        (diff.seconds, "second", "seconds"),
    )
    for period, singular, plural in periods:
            if period:
                        return "%d %s ago" % (period, singular if period == 1 else plural)
    return default

and want to use it in an output when doing a query to my database in Google Appegine.
My database looks like so:

class Service(db.Model):
    name = db.StringProperty(multiline=False)
    urla = db.StringProperty(multiline=False)
    urlb = db.StringProperty(multiline=False)
    urlc = db.StringProperty(multiline=False)
    timestampcreated = db.DateTimeProperty(auto_now_add=True)
    timestamplastupdate = db.DateTimeProperty(auto_now=True)

In the mainpage of the webapp requesthandler I want to do:

            elif self.request.get('type') == 'list':
                    q = db.GqlQuery('SELECT * FROM Service')
                    count = q.count()
                    if count == 0:
                            self.response.out.write('Success: No services registered, your database is empty.')
                    else: 
                            results = q.fetch(1000)
                            for result in results:
                                    resultcreated = timesince(result.timestampcreated)
                                    resultupdated = timesince(result.timestamplastupdate)
                                    self.response.out.write(result.name + '\nCreated:' + resultcreated + '\nLast Updated:' + resultupdated + '\n\n')

What am I doing wrong? I’m having troubles with formatting my code using the snippet.

Which one of these should I do?

this?

def timesince:
class Service
class Mainpage
  def get(self):

or this?

class Service
class Mainpage
  def timesince:
  def get(self):

I’m not too familiar with Python and would appreciate any input on how to fix this. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T01:42:49+00:00Added an answer on May 23, 2026 at 1:42 am

    I’m not totally clear what the problem you’re having is, so bear with me. A Traceback would be helpful. 🙂

    timesince() doesn’t require any member variables, so I don’t think it should be inside one of the classes. If I were in your situation, I would probably put timesince in its own file and then import that module in the file where Mainpage is defined.

    If you’re putting them all in the same file, make sure that your spacing is consistent and you don’t have any tabs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just found this out, so i am answering my own question :) Use a
Hey all, I am completely lost on this one. I found a code snippet
I found this snippet online and the purpose is to commify any number including
I found this tease of a code snippet: http://codepaste.net/m6xsdi slickGrid.AddColumn( Column.ForId(riga) .Named(Riga) .ForField(id) .WithEditorFunction(TextCellEditor)
I have this snippet of Scala code: def prologList(l: List[ScalaObject], sep: String) = [
I found this snippet of code here that allows you to log into a
I found this nifty code snippet on several sites, allowing me to analyze the
Reading this question I found this as (note the quotation marks) code to solve
I found this open-source library that I want to use in my Java application.
I found this link http://artis.imag.fr/~Xavier.Decoret/resources/glsl-mode/ , but there isn't a lot of description around

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.