Hi i am creating ics file for calendar invitation, as far as i see that if i dont put time zone some emails may become conflict, i have checked working ics files from zoom and i see that they put
BEGIN:VTIMEZONE
TZID:Turkey Standard Time
BEGIN:STANDARD
DTSTART:16010101T000000
TZOFFSETFROM:+0300
TZOFFSETTO:+0300
END:STANDARD
END:VTIMEZONE
now i translated this to code like
timezone = Timezone()
timezone.add('TZID','Turkey Standard Time')
timezoneStandard = TimezoneStandard()
timezoneStandard.add('TZOFFSETFROM', timedelta(hours=3))
timezoneStandard.add('TZOFFSETTO', timedelta(hours=3))
timezoneStandard.add('DTSTART','16010101T000000') <==== PROBLEM IS HERE
timezone.add_component(timezoneStandard)
cal.add_component(timezone)
But i dont know to translate 16010101T000000 , i am getting error You must use datetime, date, timedelta
This is ISO-8601, you can parse this for example with .isoparse [readthedocs] from the dateutil package [readthedocs].
You can install this, for example in your local environment with:
pip3 install dateutils
Then you can parse this with:
>>> from dateutil.parser import isoparse
>>> isoparse('16010101T000000')
datetime.datetime(1601, 1, 1, 0, 0)
So you can use this in your code with:
timezoneStandard.add('DTSTART', isoparse('16010101T000000'))
Related
I have a python script which moves file from local dir to a gs:// using os.system. I need to pass today's date to the filename in the gcs bucket.
Here is the script:
#!/usr/bin/python
import time
import requests
import csv
import json
import os
from datetime import date
#current_date = date.today()
def uploadfile2GCSraw():
current_date = date.today()
os. system('gsutil cp /u/y/XXXX/abcd.json gs://XXXX/XX/XX/CRE_DT=current_date')
Im very new to python, when i run the above, the file is created as cre_dt=current_date, as is. its not taking the date from date.today(). Can someone help? Thanks
When you have current_date on that final line, it's going to literally be the string current_date.
Try using an f-string, like this:
os.system(f"gsutil cp /u/y/XXXX/abcd.json gs://XXXX/XX/XX/CRE_DT={current_date}")
For older python 2, use this syntax:
os.system("gsutil cp /u/y/XXXX/abcd.json gs://XXXX/XX/XX/CRE_DT=%s"%(date.today()))
(And then upgrade to Python 3.)
Should do what you want.
Is there any way to save excel(.xls) file with current date ?
I am using Python 2.7
I tried with following code, but it did not work. I am using import datetime Library.
import datetime
wb.save('Name of File' + datetime.datetime.now() + '.xls')
you can use
'file1'+now.strftime("%Y%m%d%H%M")+'.xls'
this thing to generate the file name with current time stamp and save the file in excel.
Fairly new to coding and have been browsing this site for a while, not clued up enough to give anything back yet though.
I'm trying to calculate the Hurst exponent using this code originally from QuantStart but modified to import data from Yahoo. Daily Hurst Exponent
When running in Powershell I return these errors:
C:\Program Files\Anaconda2\lib\site-packages\pandas\io\data.py:35: FutureWarning:
The pandas.io.data module is moved to a separate package (pandas-datareader) and will be removed from pandas in a future
version.
After installing the pandas-datareader package (https://github.com/pydata/pandas-datareader), you can change the import
from pandas.io import data, wb to from pandas_datareader import data, wb.
FutureWarning)
When changing from pandas.io import data, wb to from pandas_datareader import data, wb:
Traceback (most recent call last):
File "hurst.py", line 23, in
aapl = DataReader("AAPL", "yahoo", datetime(2012,1,1), datetime(2015,9,18))
NameError: name 'DataReader' is not defined
Please can someone help and guide me in what changes I'm missing to get the script to run properly.
Thanks,
James
from pandas_datareader.data import DataReader
...
ts1 = DataReader(symbol, "yahoo", start_date, end_date)
See the usage in the documentation
for pandas datareader:
from pandas_datareader import data
import datetime
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2013, 1, 27)
f = data.DataReader("F", 'yahoo', start, end)
So i'm getting to grips with Django, or trying to. I have some code that isn't dependent on being called by the webpage - it's designed to populate the database with information. Eventually it will be set up as a cron job to run overnight. This is the first crack at it, which is to do an initial population (once I have that working, I'll move to an add structure, where only new records are pushed.) I'm using Python 2.7, Django 1.5 and Sqlite3. When I run this code, I get
Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
That seems fairly obvious, but I've spent a couple of hours now trying to work out how to adjust that setting. How do I call / open a connection / whatever the right terminology is here? I have a number of functions like this that will be scheduled jobs, and this has been frustrating me all afternoon.
import urllib2
import csv
import requests
from django.db import models
from gmbl.models import Match
master_data_file = urllib2.urlopen("http://www.football-data.co.uk/mmz4281/1213/E0.csv", "GET")
data = list(tuple(rec) for rec in csv.reader(master_data_file, delimiter=','))
for row in data:
current_match = Match(matchdate=row[1],
hometeam=row[2],
awayteam = row [3],
homegoals = row [4],
awaygoals = row[5],
homeshots = row[10],
awayshots = row[11],
homeshotsontarget = row[12],
awayshotsontarget = row[13],
homecorners = row[16],
awaycorners = row[17])
current_match.save()
I had originally started out with http://django-csv-importer.readthedocs.org/en/latest/ but I had the same error, and the documentation doesn't make much sense trying to debug it. When I tried calling settings.configure in the function, it said it didn't exist; presumably I had to import it, but couldn't make that work.
Make sure Django, and your project are in PYTHONPATH then you can do:
import urllib2
import csv
import requests
from django.core.management import setup_environ
from django.db import models
from yoursite import settings
setup_environ(settings)
from gmbl.models import Match
master_data_file = urllib2.urlopen("http://www.football-data.co.uk/mmz4281/1213/E0.csv", "GET")
data = list(tuple(rec) for rec in csv.reader(master_data_file, delimiter=','))
# ... your code ...
Reference: http://www.b-list.org/weblog/2007/sep/22/standalone-django-scripts/
Hope it helps!
Okay simple question (I think).
I have a DateTime field (auto_add_now) and when output to a template
{{ edited|date:"DATETIME_FORMAT" }}
I get the expected result of "Sept. 16, 2012, 12:01 p.m."
But unfortunately things are slightly more complicated since I am using Backbone.js and need to pass the datetime with JSON, and since it is only used for display purposes I decided to pass it as a nice locale formatted string. So I dug into the code and found what the template tag uses and this is what I setup.
from django.utils.formats import date_format
return {
'created': date_format(self.created, 'DATETIME_FORMAT'),
}
But that ends up with this "Sept. 16, 2012, 5:01 p.m."
I have a feeling it has to do with the following on the template tag
#register.filter(expects_localtime=True, is_safe=False)
I also tried this but ended up with the same results
from django.utils import timezone
tz = timezone.get_current_timezone()
logger.info(tz)
logger.info(self.edited)
logger.info(format(self.edited, 'DATETIME_FORMAT'))
logger.info(self.edited.replace(tzinfo=tz))
logger.info(format(self.edited.replace(tzinfo=tz), 'DATETIME_FORMAT'))
Which gave me this
INFO: America/Chicago
INFO: 2012-09-16 17:01:52.921276+00:00
INFO: Sept. 16, 2012, 5:01 p.m.
INFO: 2012-09-16 17:01:52.921276-06:00
INFO: Sept. 16, 2012, 5:01 p.m.
So yeah, I must be missing something, and I have been up and down the django documentation and cannot find anything that could point me to what I am doing wrong. Thanks for any help.
I figured it out. And sadly it was in the Django Timezones documentation that I thought I had exhausted. Localize Usage timezone.localtime()
from django.utils.formats import date_format
from django.utils import timezone
date_format(timezone.localtime(page.created), 'DATETIME_FORMAT')
Maybe the following will help you.
>>> obj = MyModel.objects.get(...)
>>> data = {"date_format": obj.edited}
>>> from django.core.serializers.json import DjangoJSONEncoder
>>> data = json.dumps(data, cls=DjangoJSONEncoder)
>>> data
'{"date_format": "2012-09-16T21:45:46Z"}'
Send the json formatted data from your view:
E.g return HttpResponse(data, mimetype='application/json').
And then at your client side code you can convert the date_format to the local timezone with:
(Assuming response is the JSON parsed object)
var d = new Date(Date.parse(response.date_format));
// Sun Sep 16 2012 22:45:46 GMT+0100 (BST)