I am using aggregate and Sum to determine the number of hours I have worked in each month. I have it working, but the "hours" variable always has extra content in it!
I should add, I am a newbie at django and I got most of this code from here (Django beginner: How to query in django ORM to calculate fields based on dates).
My code:
hours = ""
work_data = ""
month_data = ""
for month in range(1,13):
entries_per_month = Mydata.objects.filter(myTimePeriod__month=month).filter(myResource="James")
hours = str(entries_per_month.aggregate(value=Sum('myHoursLogged')))
month_data = month_data + "'" + str(month) + "',"
work_data = work_data + hours + ","
I look at the results of work_data:
work_data
This gives me a result of {'value': Decimal('136.80')},{'value': Decimal('146.40')},
I need it in the format: 136.80, 146.40 (This is the format required by the charting library). I have tried using str() to convert it but it doesnt seem to work in this case.
str is not useful here, because your data is a list of dictionaries. You just need to process that and get the results:
hours = ','.join(str(v['value']) for v in entries_per_month)
Related
I am looking to automate ingesting some data but the format of the time I've scraped for each row is different to what I need.
The data I scrape comes like this
9a.m.–5p.m.
Open 24 hours
It should look like the below:
09:00AM,05:00AM
{empty}
So far I have the below that only works for the second attribute, I'm struggling with the time reformatting and doing it in the same regex.
[str_replace(array("Open 24 Hours"),array(""),{import_element[1]})]
I would appreciate any help.
Thanks
Assuming your data has just hours (not minutes in the input string), this should work:
import re
def process_match(m):
n = m[:-4]
AmPm = m[-4:].replace(".", "").upper()
if len(n) == 1:
res = f"0{n}:00{AmPm}"
else:
res = f"{n}:00{AmPm}"
return res
inpt = "9a.m.–5p.m."
matches = re.findall(r"\d[ap].m.", inpt)
print(f"{process_match(matches[0])},{process_match(matches[1])}")
I am working on a code of exporting values to excel using xlwt and came across the following error
TypeError: can't subtract offset-naive and offset-aware datetimes
Here's my code
for leave in leave_data:
tes = ''
date1 = ''
user_pool = self.env['res.users']
user = user_pool.browse(SUPERUSER_ID)
tz = pytz.timezone(user.partner_id.tz) or pytz.utc
date1 = pytz.utc.localize(datetime.strptime(leave.date_from,
DEFAULT_SERVER_DATETIME_FORMAT)).astimezone(tz)
single_leave_data = []
single_leave_data.append(leave.employee_id.name)
single_leave_data.append(leave.employee_id.x_EmployeeId)
single_leave_data.append(date1)
I am not able to figure out the reason behind this error. I have searched for many solutions, but none seem to work.
i'm using the following var on my script to send output to one
output = "/opt/output"
i want to adjust it to make the output relative to the date current date of trigger the script it should be structured like this
output = "/opt/output/year/month/day"
i'm not sure if i'm using the correct way here i used the following approach
output = "/opt/output/" + today.strftime('%Y%m%d')
any tips here
I recommend you use the full timestamp instead of just using the date:
import os
mydir = os.path.join(output, datetime.datetime.now().strftime('%Y/%m/%d_%H-%M-%S'))
It's recommended do it this way because what'd happen if your script runs more than once a day ? You should at least add a counter or something (if you don't want the full timestamp) which will increment some variable if the folder already exist.
You can read more about os.path.join here
As per creating a folder, you can do it like this:
if not os.path.exists(directory):
os.makedirs(mydir)
i figure it by
today = datetime.datetime.now()
year = today.strftime("%Y")
month=today.strftime("%m")
day=today.strftime("%d")
output = "/opt/output/" + year +"/" + month + "/" + day
thats worked fine to me
I will suggest using os.path.join and os.path.sep:
import os
.
.
.
full_dir = os.path.join(base_dir, today.strftime('%Y{0}%m{0}%d').format(os.path.sep))
today.strftime('%Y%m%d') would print todays date as 20170607. But I guess you want it printed as 2017/06/07. You could explicitly add the slashes and print it something like this?
output = "/opt/output/" + today.year +"/" + today.month + "/" + today.date
I'm trying to create a data-scraping file for a class, and the data I have to scrape requires that I use while loops to get the right data into separate arrays-- i.e. for states, and SAT averages, etc.
However, once I set up the while loops, my regex that cleared the majority of the html tags from the data broke, and I am getting an error that reads:
Attribute Error: 'NoneType' object has no attribute 'groups'
My Code is:
import re, util
from BeautifulSoup import BeautifulStoneSoup
# create a comma-delineated file
delim = ", "
#base url for sat data
base = "http://www.usatoday.com/news/education/2007-08-28-sat-table_N.htm"
#get webpage object for site
soup = util.mysoupopen(base)
#get column headings
colCols = soup.findAll("td", {"class":"vaTextBold"})
#get data
dataCols = soup.findAll("td", {"class":"vaText"})
#append data to cols
for i in range(len(dataCols)):
colCols.append(dataCols[i])
#open a csv file to write the data to
fob=open("sat.csv", 'a')
#initiate the 5 arrays
states = []
participate = []
math = []
read = []
write = []
#split into 5 lists for each row
for i in range(len(colCols)):
if i%5 == 0:
states.append(colCols[i])
i=1
while i<=250:
participate.append(colCols[i])
i = i+5
i=2
while i<=250:
math.append(colCols[i])
i = i+5
i=3
while i<=250:
read.append(colCols[i])
i = i+5
i=4
while i<=250:
write.append(colCols[i])
i = i+5
#write data to the file
for i in range(len(states)):
states = str(states[i])
participate = str(participate[i])
math = str(math[i])
read = str(read[i])
write = str(write[i])
#regex to remove html from data scraped
#remove <td> tags
line = re.search(">(.*)<", states).groups()[0] + delim + re.search(">(.*)<", participate).groups()[0]+ delim + re.search(">(.*)<", math).groups()[0] + delim + re.search(">(.*)<", read).groups()[0] + delim + re.search(">(.*)<", write).groups()[0]
#append data point to the file
fob.write(line)
Any ideas regarding why this error suddenly appeared? The regex was working fine until I tried to split the data into different lists. I have already tried printing the various strings inside the final "for" loop to see if any of them were "None" for the first i value (0), but they were all the string that they were supposed to be.
Any help would be greatly appreciated!
It looks like the regex search is failing on (one of) the strings, so it returns None instead of a MatchObject.
Try the following instead of the very long #remove <td> tags line:
out_list = []
for item in (states, participate, math, read, write):
try:
out_list.append(re.search(">(.*)<", item).groups()[0])
except AttributeError:
print "Regex match failed on", item
sys.exit()
line = delim.join(out_list)
That way, you can find out where your regex is failing.
Also, I suggest you use .group(1) instead of .groups()[0]. The former is more explicit.
Hie all,
I'm trying to get eventlog entries using WMI and WQL.
I can get the right log with the right sourcename of itand so on, but i can make a select query to only get result for the 5 or 10 past minutes.
here is my query:
Here are a few snippets from a script of mine:
Dim dtmStart, dtmEnd, sDate, ...
I actually had an array of dates and I was looking for logon/off/unlock events for the entire day. So I built my complete start and end dates from that.
I won't put in the day month and year but, you could just define it, e.g. sDate = 10100608.
dtmStart = sDate + "000000.000000-420" '0hr on the date in question.
dtmEnd = sDate + "235900.000000-420" ' 23:59 on the date in question
(Note that the UTC offset is in minutes here -420 day light savings time North America.)
Set colEvents = oWMIService.ExecQuery _
("SELECT * FROM Win32_NTLogEvent WHERE Logfile = 'Security' AND " _
& "TimeWritten >= '" & dtmStart & "' AND TimeWritten < '" _
& dtmEnd & "' AND " _
& "(EventCode = '528' OR EventCode = '540' OR EventCode = '538')")
' Query for events during the time range we're looking for.
Mike,
Show me your query. Usually the time format is something like this
20100608100000.000000-300
see this for more details about DateTime formatting for WQL