How to find a document by using specific datetime? - python

This code runs perfectly:
dt = datetime.datetime(2018, 7, 15, 13, 24, 1, 962)
for doc in db.wing_model.find({'sampling_time': {'$gte': dt}}):
print doc
But when I try to find a document with specific datetime like below, it returns nothing.
dt = datetime.datetime(2018, 7, 15, 13, 24, 1, 962)
for doc in db.wing_model.find({'sampling_time': dt}):
print doc
Here's my document looks like:
{
"_id" : ObjectId("5b4ae88100285b236134e08e"),
"value" : [
{
"MQ3_ALCOHOL" : 0,
"MQ5_H2" : 0.39,
"MQ138_PROPANE" : 4.4,
"MQ135_ALCOHOL" : 1.25
}
],
"enose_id" : "node1",
"sampling_time" : ISODate("2018-07-15T13:24:01.962Z")
}

Related

Bucketing for histogram in MongoDB

Syntax issue on line #25. Can someone please help me to spot the mistake not sure if the problem is in the code before this line.
File SyntaxError: invalid syntax, line 25
} #25
^
The line with the syntax is highlighted by #25. Thank you in advance :)
import pandas as pd
def length_vs_references(articles):
res = {"1-5" : 0, "6-10" : 0, "11-15" : 0, "16-20" : 0, "21-25" : 0, "25-30" : 0, ">30" :0}
n = {"1-5" : 0, "6-10" : 0, "11-15" : 0, "16-20" : 0, "21-25" : 0, "25-30" : 0, ">30" :0}
cursor = articles.aggregate([
{'$match': {'$and' : [{'references': {'$exists': False}
}, {'$ne':['$page_end', '']}, {'$ne':['$page_start', '']} ]}},
{'$project': {'len_refernces': {"$size": '$references'},
'pages': {'$subtract': [{"$toInt": 'page_end'},
{"$toInt" : 'page_start'}]}}},
{'$bucket' : {
'$groupBy': '$pages',
'boundaries': [ 0, 6, 11, 16, 21, 26, 31, 1000000],
'default': 'Other',
{
'output' : {"average": {"$avg" : '$len_references'}},
}
} #25
}
])
return cursor
print(length_vs_references(articles))
'default': 'Other',
{
'output' : {"average": {"$avg" : '$len_references'}},
}
That is the problem area. You have a sub-dictionary without a key name.
To illustrate the problem more simply, here is an equivalent dictionary:
mydict = {
'some_key': 5,
'other_key': 10,
'yet_another_key': 100,
3,
'final_key': 1000
}
The 3 is an error because it's just a value without a key name. Your code has a sub-dictionary instead of an integer, but it's the same kind of error.

Determining Turn Order

I wanted to try it like this, but I don't want to nest a bunch of for loops or if statements. For some reason I'm not seeing what the actual comparison should be to take the speeds list and populate turn order. Basically just comparing what each individual minion's speed is against the others using the sort function in it's own method and returning a list. Then I want to compare the list values against the key values stored in the minion_collection. I'm not sure if I'm setting myself up for a world of hurt here or not. Still somewhat new to python so I don't know many of the utilities.
minion_collection = {
'minion1' : {
"name" : "zombie", "str" : 10, "def" : 5, "hp" : 25, "maxhp" : 25, "speed" : 15, "spdbar": 0},
'minion2' : {
"name": 'cleric', "str": 4, "def": 5, "hp": 25,"maxhp": 25, "speed": 20, "spdbar": 0},
'minion3' : {
"name" : "professor", "str" : 10, "def" : 5, "hp" : 25, "maxhp" : 25, "speed" : 15, "spdbar": 0},
'minion4' : {
"name": 'hunter', "str": 4, "def": 5, "hp": 25,"maxhp": 25, "speed": 30, "spdbar": 0}
}
def initiative(speeds):
for x in minion_collection:
minion_collection[x]['spdbar'] = 100
order = sort(speeds, reverse = True)
return order
Where I start to determine the turn order
turn = 1
speeds = (fighter1['speed'], fighter2['speed'], pcfighter1['speed'], pcfighter2['speed'])
order = initiative(speeds)
for speed in order:
if fighter1['speed'] == speed:
first = fighter1
second = '???'
third = '???'
last = '???'
In this case you can use sorted()
turn = 1
speeds = (fighter1['speed'], fighter2['speed'], pcfighter1['speed'], pcfighter2['speed'])
order = initiative(speeds)
for speed in sorted(order):
if fighter1['speed'] == speed:
first = fighter1
second = '???'
third = '???'
last = '???'

find time difference between previous and new data in collection using pymongo

I have list of dictionary data which is inserted in mongodb using pymongo
data =[{
"cam_name" : "cam1",
"stats" : [
{
"total" : 10,
"red" : 5,
"yellow" : 0,
"green" : 5,
"time_stamp" : datetime(2020,6,20,17,52,4,992000),
"image_path" : "image/19-06-2020/cam1/19-06-2020_17-52-16.jpg"
},
{
"total" : 10,
"red" : 5,
"yellow" : 0,
"green" : 5,
"time_stamp" : datetime(2020,6,20,17,52,27,992000),
"image_path" : "image/19-06-2020/cam1/19-06-2020_17-52-25.jpg"
},
{
"total" : 10,
"red" : 5,
"yellow" : 0,
"green" : 5,
"time_stamp" : datetime(2020,6,20,17,52,1,992000),
"image_path" : "image/19-06-2020/cam1/19-06-2020_17-52-25.jpg"
}]
},
{
"cam_name": "cam2",
"stats": [
{
"total": 10,
"red": 5,
"yellow": 0,
"green": 5,
"time_stamp": datetime(2020, 6, 20, 17, 52, 6, 992000),
"image_path": "image/19-06-2020/cam1/19-06-2020_17-52-16.jpg"
},
{
"total": 10,
"red": 5,
"yellow": 0,
"green": 5,
"time_stamp": datetime(2020, 6, 20, 17, 52, 59, 992000),
"image_path": "image/19-06-2020/cam1/19-06-2020_17-52-25.jpg"
},
{
"total": 10,
"red": 5,
"yellow": 0,
"green": 5,
"time_stamp": datetime(2020,6, 20, 17, 52, 4, 992000),
"image_path": "image/19-06-2020/cam1/19-06-2020_17-52-25.jpg"
}]
}
]
And after entering the data I am trying to find the difference between time between each element in list of dictionaries available for key ['stats']['time_stamp']
But I am not able to find out by using the approach of finding difference between the elements available in the list
from pymongo import MongoClient
from datetime import datetime
import json
import pandas as pd
from datetime import timedelta
myclient = MongoClient('localhost', 27017)
master_data = myclient['data_set']
cam_db = master_data['cam_table']
prev = None
for i,c in enumerate(data):
for k in c['stats']:
#print(k['time_stamp'])
if prev==None:
prev = k['time_stamp']
#print(prev)
else:
prev = k['time_stamp'] - prev
print(prev)
output:
0:00:23
2020-06-20 17:52:08.992000
-1 day, 23:59:56
2020-06-20 17:52:31.992000
0:00:00
after that, I am not able to find the perfect approach to use for find out the time difference
Note: I want to check whether time difference between every data available is less than 20 seconds or not
Suggestions will be really helpful

Python and JSON class

I am having trouble with JSON, I can not find suitable information.
Firstly, I managed to make the code to create an XML in python, but not JSON.
I wonder how to transform the xml writer class below in order to convert equivalent to JSON Writer Class.
from lxml import etree
import re
class XmlWriter:
# Function create a xml
def createNode(self,nodeName, parentNode = '', withAttribs = {}, withValue = ''):
if nodeName != '':
nodeName = re.sub(r'/', '_', nodeName)
nodeName = re.sub(r'#','id',nodeName)
if parentNode == '':
# Create a parent node
createdNode = etree.Element(nodeName)
else:
# Create a child node
createdNode = etree.SubElement(parentNode, nodeName)
# Put the Attributs with value
if withAttribs !={}:
for key,value in withAttribs.items():
createdNode.set(key,value)
# Put the text content of the xml node
if withValue != '':
createdNode.text = withValue
else:
pass
return createdNode
# Print the XML for information
def printXML(self,nodeName):
print (etree.tostring(nodeName, pretty_print=True))
# Save the XML on the file
def saveXML(self,nodeName,filename):
if nodeName != '' or filename !='':
with open(filename, "w") as f:
f.write(etree.tostring(nodeName, pretty_print=True))
else:
return False
I try in several research attempting to create an equivalent of a "tree node" for example:
{"menu": {
"id": "file",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
But how to create a class to get a result if you do bellow example:
n1 = CreateNode(name,parent_dependancy,value)...
Thank you very much
JSON is very different from XML, so it’s not possible to get an equivalent “JSON Writer”, as you are using specific XML features (attributes). JSON allows you to replicate an object structure. In respect of Python, JSON simply allows you to encode dicts, lists and simple scalar values (like str, int etc.).
If you have such a structure, it’s very simple to get the JSON document from it:
>>> import json
>>> obj = {
'string-example' : 'foobar',
'int-example' : 42,
'float-example' : 3.14,
'list-example' : [ 0, 1, 1, 2, 3, 5, 8, 13, 21 ],
'tuple-example' : ( 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ),
'dict-example' : { 'key' : 'value', 'key2' : 'value2' } }
>>> json.dumps(obj)
'{"list-example": [0, 1, 1, 2, 3, 5, 8, 13, 21], "int-example": 42, "string-example": "foobar", "tuple-example": [2, 3, 5, 7, 11, 13, 17, 19, 23, 29], "float-example": 3.14, "dict-example": {"key2": "value2", "key": "value"}}'
You can read JSON strings using json.loads in the same way.
You can Pretty Print with JSON dumps. Like this:
print json.dumps( jsonData, indent=2 )
It is subtle but the indent=2 says that you want the data to be pretty printed.

Get child dict values use Mongo Map/Reduce

I have a mongo collection, i want get total value of 'number_of_ad_clicks' by given sitename, timestamp and variant id. Because we have large data so it would be better use map/reduce. Could any guys give me any suggestion?
Here is my collection json format
{ "_id" : ObjectId( "4e3c280ecacbd1333b00f5ff" ),
"timestamp" : "20110805",
"variants" : { "94" : { "number_of_ad_clicks" : 41,
"number_of_search_keywords" : 9,
"total_duration" : 0,
"os" : { "os_2" : 2,
"os_1" : 1,
"os_0" : 0 },
"countries" : { "ge" : 6,
"ca" : 1,
"fr" : 8,
"uk" : 4,
"us" : 6 },
"screen_resolutions" : { "(320, 240)" : 1,
"(640, 480)" : 5,
"(1024, 960)" : 5,
"(1280, 768)" : 5 },
"widgets" : { "widget_1" : 1,
"widget_0" : 0 },
"languages" : { "ua_uk" : 8,
"ca_en" : 2,
"ca_fr" : 2,
"us_en" : 5 },
"search_keywords" : { "search_keyword_8" : 8,
"search_keyword_5" : 5,
"search_keyword_4" : 4,
"search_keyword_7" : 7,
"search_keyword_6" : 6,
"search_keyword_1" : 1,
"search_keyword_3" : 3,
"search_keyword_2" : 2 },
"number_of_pageviews" : 18,
"browsers" : { "browser_4" : 4,
"browser_0" : 0,
"browser_1" : 1,
"browser_2" : 2,
"browser_3" : 3 },
"keywords" : { "keyword_5" : 5,
"keyword_4" : 4,
"keyword_1" : 1,
"keyword_0" : 0,
"keyword_3" : 3,
"keyword_2" : 2 },
"number_of_keyword_clicks" : 83,
"number_of_visits" : 96 } },
"site_name" : "fonter.com",
"number_of_variants" : 1 }
Here is my try. but failed.
He is my try.
m = function() {
emit(this.query, {variants: this.variants});
}
r = function(key , vals) {
var clicks = 0 ;
for(var i = 0; i < vals.length(); i++){
clicks = vals[i]['number_of_ad_clicks'];
}
return clicks;
}
res = db.variant_daily_collection.mapReduce(m, r, {out : "myoutput", "query":{"site_name": 'fonter.com', 'timestamp': '20110805'}})
db.myoutput.find()
could somebody any suggestion?
Thank you very much, i try you solution but nothing return.
I invoke the mapreduce in the following, is there any thing wrong?
res = db.variant_daily_collection.mapReduce(map, reduce, {out : "myoutput", "query":{"site_name": 'facee.com', 'timestamp': '20110809', 'variant_id': '305'}})
db.myoutput.find()
The emit function emits both a key and a value.
If you are used to SQL think of key as your GROUP BY and value as your SUM(), AVG(), etc..
In your case you want to "group by": site_name, timestamp and variant id. It looks like you may have more than one variant, so you will need to loop through the variants, like this:
map = function() {
for(var i in variants){
var key = {};
key.timestamp = this.timestamp;
key.site_name = this.site_name;
key.variant_id = i; // that's the "94" string.
var value = {};
value.clicks = this.variants[i].number_of_ad_clicks;
emit(key, value);
}
}
The reduce function will get an array of values each one like this { clicks: 41 }. The function needs to return one object that looks the same.
So if you get values = [ {clicks:21}, {clicks:10}, {clicks:5} ] you must output {clicks:36}.
So you do something like this:
reduce = function(key , vals) {
var returnValue = { clicks: 0 }; // initializing to zero
for(var i = 0; i < vals.length(); i++){
returnValue.clicks += vals[i].clicks;
}
return returnValue;
}
Note that the value from map has the same shape as the return from reduce.

Categories

Resources