NameError: name 'datetime' is not defined
You need to import the module datetime
first:
>>> import datetime
After that it works:
>>> import datetime
>>> date = datetime.date.today()
>>> date
datetime.date(2013, 11, 12)
Date::today not defined?
The Date
class you are seeing is defined in lib/rubygems/specification.rb for compatibility reasons:
# date.rb can't be loaded for `make install` due to miniruby
# Date is needed for old gems that stored #date as Date instead of Time.
class Date; end
It's an empty class definition and it doesn't provide any methods or functionality.
If starting IRB without RubyGems, that Date class is gone:
$ ruby --disable-gems -S irb
irb(main):001:0> Date
NameError: uninitialized constant Date
Update
The empty Date
class was removed in RubyGems 2.4.0:
- RubyGems no longer defines an empty Date class. Pull Request #948 by Benoit
Daloze.
NameError: name 'datetime' is not defined [while running 'ChangeDataType DistrictAllocationAndListStore-ptransform-570']
You can try to put import datetime within the function.
Trying to mock datetime.date.today(), but not working
There are a few problems.
First of all, the way you're using mock.patch
isn't quite right. When used as a decorator, it replaces the given function/class (in this case, datetime.date.today
) with a Mock
object only within the decorated function. So, only within your today()
will datetime.date.today
be a different function, which doesn't appear to be what you want.
What you really want seems to be more like this:
@mock.patch('datetime.date.today')
def test():
datetime.date.today.return_value = date(2010, 1, 1)
print datetime.date.today()
Unfortunately, this won't work:
>>> test()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.6-universal/egg/mock.py", line 557, in patched
File "build/bdist.macosx-10.6-universal/egg/mock.py", line 620, in __enter__
TypeError: can't set attributes of built-in/extension type 'datetime.date'
This fails because Python built-in types are immutable - see this answer for more details.
In this case, I would subclass datetime.date myself and create the right function:
import datetime
class NewDate(datetime.date):
@classmethod
def today(cls):
return cls(2010, 1, 1)
datetime.date = NewDate
And now you could do:
>>> datetime.date.today()
NewDate(2010, 1, 1)
date.getDate is not a function Typescript
I once had a similar problem when getting dates from a web API. I solved it by creating a new Date object.
So your function call could look like this:
this.addDays(new Date(this.gDetailDS.activeFrom),this.gDetailDS.activeNoDays)
Uncaught ReferenceError: $ is not defined when td is defined
It was a DOM
issue - on my calendar.php
page at the very top, I had:
<?php
include "assets/system/functions.php";
$currentCal = new Calendar("Current");
$nextCal = new Calendar("Next");
$priorCal = new Calendar("Prior");
$wipCal = new Calendar("WIP");
?>
WAY before any of the DOM
were created.
I moved it to the bottom of the page and it works just perfect.
Evaluation error: & not defined for Date objects
In the comments to the question camille (user:5325862) suggested to use case_when
instead of nasted if statements. The following solution worked for me:
shipments_sample %>%
mutate(
# flights %>% filter(between(month, 7, 9))
DaysInMonth = case_when(
StartDate >= floor_date(proratamonth,"month") & StartDate < ceiling_date(proratamonth,"month") ~ as.numeric(ceiling_date(proratamonth,"month") - StartDate),
EndDate >= floor_date(proratamonth,"month") & EndDate < ceiling_date(proratamonth,"month") ~ as.numeric(ceiling_date(EndDate,"day") - floor_date(proratamonth,"month")),
StartDate < floor_date(proratamonth,"month") & EndDate >ceiling_date(proratamonth,"month") ~ as.numeric(ceiling_date(proratamonth,"month") - floor_date(proratamonth,"month")),
TRUE ~ as.numeric(0)
),
Duration = as.numeric(EndDate - StartDate),
PercentageInMonth = DaysInMonth/as.numeric(EndDate - StartDate),
ProRata_Rev_Freight = Rev_Freight * PercentageInMonth,
ProRata_Rev_Demurrage = PercentageInMonth * Rev_Demurrage,
ProRata_Cost_Transport = PercentageInMonth * Cost_Transport
)
Python Django NameError: name 'datetime' is not defined
The problem is that you did not imported the datetime
module (or at least not in a qualified way): you imported the date
attribute.
Use the date
class
You can replace datetime.date
with just date
(and this is the only element you need):
from django.db import models
from django.utils import timezone
from datetime import date
class Item(models.Model):
FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)
TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", default=date.today)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(default = date.today)
on_shopping_list = models.BooleanField()
def __str__(self):
return self.title
Perform an import of the module
Alternatively, you can import the module, and then use datetime.date
:
from django.db import models
from django.utils import timezone
import datetime
class Item(models.Model):
FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)
TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", default=datetime.date.today)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(default = datetime.date.today)
on_shopping_list = models.BooleanField()
def __str__(self):
return self.title
Use auto_now_add
of the DateField
Regardless how we import this, in fact Django has already support for such default: with auto_now_add=True
[Django-doc]: this will add a default value that is equal to today
. Furthermore it will make the field blank=True
, and editable=False
, to prevent it to show up in forms, etc. So it is not completely equivalent, although it is likely what you mean:
# Note: makes the fields blank=True, and editable=False as well.
from django.db import models
from django.utils import timezone
class Item(models.Model):
FREEZER_DRAWERS = (
(1, 'Kitchen: Top tray'),
(2, 'Kitchen: Middle drawer'),
(3, 'Kitchen: Bottom drawer'),
)
TYPES = (
(1, 'Raw meat'),
(2, 'Fruit and veg'),
(3, 'Meal'),
)
author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
title = models.CharField("food", max_length=100)
item_type = models.IntegerField(choices = TYPES, default = 1)
added_date = models.DateField("date added", auto_now_add=True)
where = models.IntegerField(choices = FREEZER_DRAWERS, default = 1)
expires_date = models.DateField(auto_now_add=True)
on_shopping_list = models.BooleanField()
def __str__(self):
return self.title
Related Topics
Passenger Installation with Nginx Fails
Ruby/Rails Image Processing Libraries
Checking Whether the C Compiler Works... No
Why Doesn't This Work If in Ruby Everything Is an Object
Start or Ensure That Delayed Job Runs When an Application/Server Restarts
Rails 3, Check CSV File Encoding Before Import
Refactoring a Large Routes.Rb File in Rails 4
Rails Implementation of Search with Autocomplete
Strictly Convert String to Integer (Or Nil)
Gem Not Found in Ruby Cron Job in Rvm Env
Setting Up Facets in Elasticsearch with Searchkick Gem in Rails 4.1
Why Can't I Install SASS on MAC Os Sierra
How to Format a String with Floats in Ruby Using #{Variable}
How to Expect Some (But Not All) Arguments with Rspec Should_Receive
Firebase Token Error, "The Custom Token Corresponds to a Different Audience."