Related
[{'_id': '5ebe39e41e1729d90de',
'modelId': '5ebe3536c289711579',
'lastAt': datetime.datetime(2020, 5, 15, 6, 42, 44, 79000),
'proId': '5ebe3536c2897115793dccfb',
'genId': '5ebe355ac2897115793dcd04',
'count':'ab'},
{'_id': '5ebe3a0d94fcb800fa474310',
'modelId': '5ebe3536c289711579',
'proId': '5ebe3536c2897115793d',
'genId': '5ebe355ac2897115793',
'lastAt': datetime.datetime(2020, 5, 15, 6, 43, 25, 105000),'count':'cd'}]
i've count in the above collection documents which is in encrypted form, how can i extract the counts for each day for a week and total count for a week and for a month.
Note: need to extract the sum of the counts
new = {('E_1E_2', 1.0235591568933329): [datetime.datetime(2019, 6, 25, 4, 38, 47)],
('E_1E_2', 0.9023917716540885): [datetime.datetime(2019, 6, 25, 4, 38, 51)],
('E_12E_2', 1.4927612857980848): [datetime.datetime(2019, 6, 25, 3, 18, 42)],
('E12_1E_2', 1.0235591568933329): [datetime.datetime(2019, 6, 25, 3, 30, 17)],
('E_1E_2', 0.9023917716540885): [datetime.datetime(2019, 6, 25, 3, 13, 51)],
('E_1E_2', 1.4927612857980848): [datetime.datetime(2019, 6, 25, 3, 8, 42)]}
This is how my data input looks like. I need to display the time taken for the students to complete the task in hourly basis. The dictionary keys consist of the student pair doing the task and the distance between the pair. The value consists of the timestamp at which they were close to each other.
tasktime=0
prev=datetime.datetime(1,1,1)
for i,j in new.items():
#print(k[0],k[1])
print(i[0],"-------------------------------------------------")
for k,l in new.items():
if i[0]==k[0]:
if k[1] < 1 and len(l)==1 and j[0].hour==l[0].hour:
print("distance=======",k[1])
print(l[0].hour,i[0],l)
if (k[0],l[0]) not in sample.keys():
sample[k[0],l[0].hour]=0
if prev==datetime.datetime(1,1,1):
prev=l[0]
else:
diff=(l[0]-prev).seconds
tasktime=tasktime+diff
prev=l[0]
elif loitime!=0 and k[1] > 1:
print(k[0],"loitered for ",tasktime/60,"minutes")
print("timestamp ++++++++++++++++",l[0])
I get the time they took for the task for the whole data and not on hourly basis.
I have a list of hours starting from (0 is midnight).
hour = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
I want to generate a sequence of 3 consecutive hours randomly. Example:
[3,6]
or
[15, 18]
or
[23,2]
and so on. random.sample does not achieve what I want!
import random
hourSequence = sorted(random.sample(range(1,24), 2))
Any suggestions?
Doesn't exactly sure what you want, but probably
import random
s = random.randint(0, 23)
r = [s, (s+3)%24]
r
Out[14]: [16, 19]
Note: None of the other answers take in to consideration the possible sequence [23,0,1]
Please notice the following using itertools from python lib:
from itertools import islice, cycle
from random import choice
hours = list(range(24)) # List w/ 24h
hours_cycle = cycle(hours) # Transform the list in to a cycle
select_init = islice(hours_cycle, choice(hours), None) # Select a iterator on a random position
# Get the next 3 values for the iterator
select_range = []
for i in range(3):
select_range.append(next(select_init))
print(select_range)
This will print sequences of three values on your hours list in a circular way, which will also include on your results for example the [23,0,1].
You can try this:
import random
hour = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
index = random.randint(0,len(hour)-2)
l = [hour[index],hour[index+3]]
print(l)
You can get a random number from the array you already created hour and take the element that is 3 places afterward:
import random
def random_sequence_endpoints(l, span):
i = random.choice(range(len(l)))
return [hour[i], hour[(i+span) % len(l)]]
hour = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]
result = random_sequence_endpoints(hour, 3)
This will work not only for the above hours list example but for any other list contain any other elements.
I want to create a mapping of the everyday of the week with its datetime object. So my dictionary should be having keys as "Monday", "Tuesday", .. (so on) so that I can get a datetime object for every day on the next(!) week.
At the moment I have a dictionary with these values:
DAYS_DATETIME_RELATIONS = {
"today": datetime.datetime.now(),
"tomorrow": datetime.datetime.now() + datetime.timedelta(days=1),
"after_tomorrow": datetime.datetime.now() + datetime.timedelta(days=2)
}
Unfortunately I cannot find any algorithmic solution for this and hope anyone of you could help me.
This can be achieved by using 2 dictionaries in the following manner:
import calendar
import datetime
days = {i: calendar.day_name[i-1] for i in range(7)}
today = datetime.datetime.now()
# using i % 7 so an arbitrary range can be used, for example
# range(7, 15) to get the week after the next week
next_week = {days[i % 7]: (today + datetime.timedelta(days=i)).date()
for i in range(7)}
print(next_week)
# {'Tuesday': datetime.date(2018, 1, 9), 'Sunday': datetime.date(2018, 1, 7),
# 'Monday': datetime.date(2018, 1, 8), 'Thursday': datetime.date(2018, 1, 11),
# 'Wednesday': datetime.date(2018, 1, 10), 'Friday': datetime.date(2018, 1, 12),
# 'Saturday': datetime.date(2018, 1, 13)}
print(next_week['Saturday'])
# 2018-01-13
Here is another way to solve your question using datetime and timedelta from datetime module:
from datetime import datetime, timedelta
def generate_dict_relation(_time, _days=0):
keys = {'Yesterday': -1, 'Today': 0, 'Tomorow': 1, 'After_tomorrow': 2}
if not _days:
return {key: _time + timedelta(days=keys.get(key, 0)) for key in keys}
else:
return {(_time + timedelta(days=_days+k)).strftime('%A'): _time + timedelta(days=_days+k)
for k in range(0, 7)}
_date_now = datetime.now()
DAYS_DATETIME_RELATIONS = {}
# get dates: yesterday, today, tomorrow and after tomorrow
DAYS_DATETIME_RELATIONS.update(generate_dict_relation(_date_now, 0))
# get dates after 7 days = 1 week
DAYS_DATETIME_RELATIONS.update(generate_dict_relation(_date_now, 7))
next_tuesday = DAYS_DATETIME_RELATIONS.get('Tuesday')
next_monday = DAYS_DATETIME_RELATIONS.get('Monday')
yesterday = DAYS_DATETIME_RELATIONS.get('Yesterday')
print('{0:%d/%m/%Y %H:%M:%S:%s} \t {1}'.format(next_tuesday, repr(next_tuesday)))
print('{0:%d/%m/%Y %H:%M:%S:%s} \t {1}'.format(next_monday, repr(next_monday)))
print('{0:%d/%m/%Y %H:%M:%S:%s} \t {1}'.format(yesterday, repr(yesterday)))
Output:
16/01/2018 10:56:26:1516096586 datetime.datetime(2018, 1, 16, 10, 56, 26, 659949)
15/01/2018 10:56:26:1516010186 datetime.datetime(2018, 1, 15, 10, 56, 26, 659949)
06/01/2018 10:56:26:1515232586 datetime.datetime(2018, 1, 6, 10, 56, 26, 659949)
One very generic way will be to create a custom iterator to return you the continuos datetime objects as:
from datetime import datetime, timedelta
class RepetetiveDate(object):
def __init__(self, day_range=7, datetime_obj=datetime.now(), jump_days=1):
self.day_range = day_range
self.day_counter = 0
self.datetime_obj = datetime_obj
self.jump_days = jump_days
self.time_deltadiff = timedelta(days=self.jump_days)
def __iter__(self):
return self
# If you are on Python 2.7
# define this function as `next(self)`
def __next__(self):
if self.day_counter >= self.day_range:
raise StopIteration
if self.day_counter != 0: # don't update for the first iteration
self.datetime_obj += self.time_deltadiff
self.day_counter += 1
return self.datetime_obj
Here, this iterator returns continuos datetime object starting from the datetime object you'll initially pass (default starts from current date).
It is using 3 optional params which you may customize as per your need:
day_range: Maximum allowed iteration for the RepetetiveDate iterator. Default value is 7.
jump_days: Integer value for jumping the number of days for the datetime object in next iteration. That means, if jump_days is equal to "2", will return datetime objects of every alternate date. To get the datetime objects of past, pass this value as negative. Default value is 1.
datetime_obj: Accepts the datetime from which date you want to start your iteration. Default value is current date.
If you are new to iterators, take a look at:
What exactly are Python's iterator, iterable, and iteration protocols?
Difference between Python's Generators and Iterators
Sample Run for upcoming dates:
>>> x = RepetetiveDate()
>>> next(x)
datetime.datetime(2018, 1, 8, 15, 55, 39, 124654)
>>> next(x)
datetime.datetime(2018, 1, 9, 15, 55, 39, 124654)
>>> next(x)
datetime.datetime(2018, 1, 10, 15, 55, 39, 124654)
Sample Run for previous dates:
>>> x = RepetetiveDate(jump_days=-1)
>>> next(x)
datetime.datetime(2018, 1, 6, 15, 55, 39, 124654)
>>> next(x)
datetime.datetime(2018, 1, 5, 15, 55, 39, 124654)
>>> next(x)
datetime.datetime(2018, 1, 4, 15, 55, 39, 124654)
How to get your desired dictionary?
Using this, you may create your dictionary using the dict comprehension as:
Dictionary of all days of week
>>> {d.strftime("%A"): d for d in RepetetiveDate(day_range=7)}
{
'Monday': datetime.datetime(2018, 1, 8, 15, 23, 16, 926364),
'Tuesday': datetime.datetime(2018, 1, 9, 15, 23, 16, 926364),
'Wednesday': datetime.datetime(2018, 1, 10, 15, 23, 16, 926364),
'Thursday': datetime.datetime(2018, 1, 11, 15, 23, 16, 926364),
'Friday': datetime.datetime(2018, 1, 12, 15, 23, 16, 926364),
'Saturday': datetime.datetime(2018, 1, 13, 15, 23, 16, 926364),
'Sunday': datetime.datetime(2018, 1, 14, 15, 23, 16, 926364)
}
Here I am using d.strftime("%A") to extract day name from the datetime object.
List of current days for next 4 weeks
>>> [d for d in RepetetiveDate(jump_days=7, day_range=4))]
[
datetime.datetime(2018, 1, 7, 16, 17, 45, 45005),
datetime.datetime(2018, 1, 14, 16, 17, 45, 45005),
datetime.datetime(2018, 1, 21, 16, 17, 45, 45005),
datetime.datetime(2018, 1, 28, 16, 17, 45, 45005)
]
One very clean way to implement this is using rrule from the dateutil library. For example:
>>> from dateutil.rrule import rrule, DAILY
>>> from datetime import datetime
>>> start_date = datetime.now()
>>> {d.strftime("%A"): d for d in rrule(freq=DAILY, count=7, dtstart=start_date)}
which will return your desired dict object:
{
'Sunday': datetime.datetime(2018, 1, 7, 17, 2, 30),
'Monday': datetime.datetime(2018, 1, 8, 17, 2, 30),
'Tuesday': datetime.datetime(2018, 1, 9, 17, 2, 30),
'Wednesday': datetime.datetime(2018, 1, 10, 17, 2, 30),
'Thursday': datetime.datetime(2018, 1, 11, 17, 2, 30),
'Friday': datetime.datetime(2018, 1, 12, 17, 2, 30),
'Saturday': datetime.datetime(2018, 1, 13, 17, 2, 30)
}
(Special thanks to Jon Clements for telling me about rrule)
I want to get rid of the first "pts", and 2 as the key of loc and pts and imsize, loc, pts, imsize are the key of their values.
This is my list:
test = [{'pts': u"""{"2": {"loc": [11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32], "pts": [[
238.12358231315642, 253.66068427891196], [458.64277680287756,
241.96368624623565], [697.01528407931528, 227.18853083653903],
[958.16615594570135, 201.82451404989325], [1246.281686434784,
203.42515588594387], [1548.4572965158027, 241.5523054071067],
[1892.7592776185272, 342.33495115591734], [2254.5289081772476,
445.98514873992008], [2656.9656149224697, 571.79649071207928],
[2971.1562661999892, 867.70244034080304], [3068.3911866286853,
1286.0266095582174], [2929.8340389691793, 1672.0031179683222],
[2613.8132245402226, 1903.4008185146297], [2238.0791358590532,
1946.1114436655755], [1891.3179056028878, 1862.0534199001079],
[1575.3878471688531, 1818.865481764926], [1287.8256402921395,
1766.8583248583836], [1026.4040596301347, 1702.4873909751091],
[783.93932060128668, 1640.5323348318664], [560.42180223554942,
1588.6583330557301], [354.57960965335764, 1540.1880782707833],
[164.40489058630092, 1498.9624158157519]], "imsize": [3264, 2448]},
"43": {"loc": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
"pts": [[986.9522062723704, 697.0146815449186], [1178.2569415526625,
664.0692929800059], [1360.425560676298, 662.1313289467757],
[1526.8136155293448, 681.7878212838245], [1683.2349982114938,
697.2915335496658], [1827.4748926847676, 710.8572817822769],
[1962.0249669918903, 720.2702499436805], [2086.054665118621,
725.8072900386238], [2203.7167671361667, 730.7906261240727],
[2313.903865025539, 730.7906261240728], [2417.1696627962324,
733.2822941667973], [2513.2373084434994, 760.4137906320195],
[2603.7679139958227, 795.2971432301624], [2689.5920354674445,
829.0730878093167], [2769.3254128346284, 857.0351402887804],
[2840.4763780546505, 917.1120253189156], [2882.55788277622,
1023.4231951418275]], "imsize": [3264, 2448]},
"47": {"loc": [34, 35, 36], "pts": [[1393.0609259457722,
1700.979369842461], [1193.0180580859501, 1746.2349694566501],
[957.55776444111029, 1801.984621155289]],
"imsize": [3264, 2448]}}"""}]
I tried this:
test = test[0]
a = test[0].pts
print test
print a #not print a
If you want to change the value of points into a dict that you can parse try:
points = eval(test[0]['pts'])
this will make points equal to:
{'47': {'loc': [34, 35, 36],
'pts': [[1393.0609259457722, 1700.979369842461], [1193.01805808595, 1746.23496945665], [957.5577644411103, 1801.984621155289]],
'imsize': [3264, 2448]},
'2': {'loc': [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32],
'pts': [[238.12358231315642, 253.66068427891196], [458.64277680287756, 241.96368624623565], [697.0152840793153, 227.18853083653903], [958.1661559457013, 201.82451404989325], [1246.281686434784, 203.42515588594387], [1548.4572965158027, 241.5523054071067], [1892.7592776185272, 342.33495115591734], [2254.5289081772476, 445.9851487399201], [2656.9656149224697, 571.7964907120793], [2971.156266199989, 867.702440340803], [3068.3911866286853, 1286.0266095582174], [2929.8340389691793, 1672.0031179683222], [2613.8132245402226, 1903.4008185146297], [2238.079135859053, 1946.1114436655755], [1891.3179056028878, 1862.0534199001079], [1575.387847168853, 1818.865481764926], [1287.8256402921395, 1766.8583248583836], [1026.4040596301347, 1702.487390975109], [783.9393206012867, 1640.5323348318664], [560.4218022355494, 1588.65833305573], [354.57960965335764, 1540.1880782707833], [164.40489058630092, 1498.962415815752]],
'imsize': [3264, 2448]},
'43': {'loc': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
'pts': [[986.9522062723704, 697.0146815449186], [1178.2569415526625, 664.0692929800059],[1360.425560676298, 662.1313289467757], [1526.8136155293448, 681.7878212838245],[1683.2349982114938, 697.2915335496658], [1827.4748926847676, 710.8572817822769],[1962.0249669918903, 720.2702499436805], [2086.054665118621, 725.8072900386238],[2203.7167671361667, 730.7906261240727], [2313.903865025539, 730.7906261240728],[2417.1696627962324, 733.2822941667973], [2513.2373084434994, 760.4137906320195],[2603.7679139958227, 795.2971432301624], [2689.5920354674445, 829.0730878093167],[2769.3254128346284, 857.0351402887804], [2840.4763780546505, 917.1120253189156],[2882.55788277622, 1023.4231951418275]],
'imsize': [3264, 2448]}
}
You can then get each of those dicts by the keys points['47'],points['2'], or points['43'].
Instead try
print test[0]['pts']
as you need to use the key here.
I think it's a little unclear what you're asking.
It looks like you have a list of dictionaries, and you only care about the first element of the list, whose key is pts. Then you want to do something with the dictionary key. The dictionary key appears ti be a JSON string, so you'll need to decode it. Try the following, to get started:
from pprint import pprint
import json
test = [{'pts': u"""{"2": {"loc": [11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32], "pts": [[
238.12358231315642, 253.66068427891196], [458.64277680287756,
241.96368624623565], [697.01528407931528, 227.18853083653903],
[958.16615594570135, 201.82451404989325], [1246.281686434784,
203.42515588594387], [1548.4572965158027, 241.5523054071067],
[1892.7592776185272, 342.33495115591734], [2254.5289081772476,
445.98514873992008], [2656.9656149224697, 571.79649071207928],
[2971.1562661999892, 867.70244034080304], [3068.3911866286853,
1286.0266095582174], [2929.8340389691793, 1672.0031179683222],
[2613.8132245402226, 1903.4008185146297], [2238.0791358590532,
1946.1114436655755], [1891.3179056028878, 1862.0534199001079],
[1575.3878471688531, 1818.865481764926], [1287.8256402921395,
1766.8583248583836], [1026.4040596301347, 1702.4873909751091],
[783.93932060128668, 1640.5323348318664], [560.42180223554942,
1588.6583330557301], [354.57960965335764, 1540.1880782707833],
[164.40489058630092, 1498.9624158157519]], "imsize": [3264, 2448]},
"43": {"loc": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17],
"pts": [[986.9522062723704, 697.0146815449186], [1178.2569415526625,
664.0692929800059], [1360.425560676298, 662.1313289467757],
[1526.8136155293448, 681.7878212838245], [1683.2349982114938,
697.2915335496658], [1827.4748926847676, 710.8572817822769],
[1962.0249669918903, 720.2702499436805], [2086.054665118621,
725.8072900386238], [2203.7167671361667, 730.7906261240727],
[2313.903865025539, 730.7906261240728], [2417.1696627962324,
733.2822941667973], [2513.2373084434994, 760.4137906320195],
[2603.7679139958227, 795.2971432301624], [2689.5920354674445,
829.0730878093167], [2769.3254128346284, 857.0351402887804],
[2840.4763780546505, 917.1120253189156], [2882.55788277622,
1023.4231951418275]], "imsize": [3264, 2448]},
"47": {"loc": [34, 35, 36], "pts": [[1393.0609259457722,
1700.979369842461], [1193.0180580859501, 1746.2349694566501],
[957.55776444111029, 1801.984621155289]],
"imsize": [3264, 2448]}}"""}]
# Print the original input (it's a list of dictionaries that map keys to JSON strings)
pprint(test)
pts = json.loads(test[0]['pts'])
# Print the decoded JSON.
pprint(pts)