Extract Date from UUID - python

I have a list of UUIDs and I'm trying to extract datetime from the code below I found online but it shows error.
Is there any other way or any changes in this code to extract it?
import uuid
import time_uuid
my_uuid = uuid.UUID('{7a3febe0-3fcc-3a28-bdab-c29438888d12}')
ts = time_uuid.TimeUUID(bytes=my_uuid.bytes).get_timestamp()
Error: No module named time_uuid
Expected: Datetime

Related

Nutritionix API only returns first 10 results?

I'm trying to extract nutritional information using the Nutritionix API/database in Python. I was able to get a successful query and placed it into a pandas dataframe. However, I'm a bit confused though because the resulting json claims that there are several thousand 'hits' for my query but at most 10 are ever returned. For instance, when I query for Garbanzo, the json file says that there are 513 total_hits, but only 10 are actually returned. Does anyone know what is causing this? The code I'm using is below.
import requests
import json
import pandas as pd
from nutritionix import Nutritionix
nix_apikey = ''
nix_appid = ''
nix = Nutritionix(app_id = nix_appid, api_key = nix_apikey)
results = nix.search('Garbanzo').json()
df = pd.json_normalize(results, record_path = ['hits'])
I'm not including the my api_key or app_id for obvious reasons. Here's a link to the Nutritionix API: https://github.com/leetrout/python-nutritionix
Thanks for any suggestions!

Reading from mongodb and converting in dataframe

Hi I am trying to read data from MongoDB and trying to convert it into data frame but getting an error that says "("Could not convert ObjectId('620d3f43ae93743dbb6f6846') with type ObjectId: did not recognize Python value type when inferring an Arrow data type", 'Conversion failed for column _id with type object')".
my code looks like this:
import streamlit as st
import pymongo
import streamlit as st
from pymongo import MongoClient
import pandas as pd
import json
import string
import io
import re
import time
import csv
from pandas import Timestamp
import certifi
import datetime
from pandas.io.json import json_normalize
client=pymongo.MongoClient()
connection = MongoClient("mongodb+srv://*****:****#cluster0.t4iwt.mongodb.net/testdb?retryWrites=true&w=majority",tlsCAFile=certifi.where())
db=connection["testdb"]
collection=db["test"]
cursor = collection.find()
entries=list(cursor)
entries[:]
df=pd.DataFrame(entries)
st.write(df)
Change type of _id to str.
df = pd.DataFrame(entries)
df = df.astype({"_id": str})
st.write(df)

Retrieve the last n days from MongoDB file using Python

I have a code below which connects to a MongoDB database and selects the specified JSON file, flattens it and exports it as a CSV.
So my problem is some of the JSON files in the MongoDB databases are absolutely huge with thousands of rows, so what I am trying to do is filter table down so that I only bring in data from the last 7 days.
from pymongo import MongoClient
import pandas as pd
import os, uuid, sys
import collections
from azure.storage.filedatalake import DataLakeServiceClient
from azure.core._match_conditions import MatchConditions
from azure.storage.filedatalake._models import ContentSettings
from pandas import json_normalize
mongo_client = MongoClient("connstring")
db = mongo_client.nhdb
table = db.Report
document = table.find()
mongo_docs = list(document)
mongo_docs = json_normalize(mongo_docs)
mongo_docs.to_csv("Report.csv", sep = ",", index=False)
Any help will be much appreciated.
Note: I know a way to do it in Azure Data Factory using the expression below, however, I am not sure how to go about it in Python
{"createdDatetime":{$gt: ISODate("#{adddays(utcnow(),-7)}")}}
In python create a datetime object to use as a filter; for example this shows the last 7 days:
from datetime import datetime, timedelta
document = table.find({'createdDatetime': {'$gt': datetime.utcnow() - timedelta(days=7)}})

SQL with regular expressions in python

I am running this query in python. I do get the error "Invalid string literal" in the regular expression part. I know this regex is, not sure what syntax is missing here. Any help would be appreciated.
import pandas as pd
import numpy as np
import datetime
from time import gmtime, strftime
import smtplib
import sys
from IPython.core.display import HTML
PROJECT = 'server'
queryString = '''
SELECT
mn as mName,
dt as DateTime,
ip as LocalIPAddress,
REGEXP_EXTRACT(path, 'ActName:([\s\S\w\W]*?)ActDomain:') AS ActName,
FROM Agent.Logs
'''
You're missing an r
REGEXP_EXTRACT(path, r'ActName:([\s\S\w\W]*?)ActDomain:') AS ActName

AttributeError: 'datetime' module has no attribute 'strptime'

Here is my Transaction class:
class Transaction(object):
def __init__(self, company, num, price, date, is_buy):
self.company = company
self.num = num
self.price = price
self.date = datetime.strptime(date, "%Y-%m-%d")
self.is_buy = is_buy
And when I'm trying to run the date function:
tr = Transaction('AAPL', 600, '2013-10-25')
print tr.date
I'm getting the following error:
self.date = datetime.strptime(self.d, "%Y-%m-%d")
AttributeError: 'module' object has no attribute 'strptime'
How can I fix that?
If I had to guess, you did this:
import datetime
at the top of your code. This means that you have to do this:
datetime.datetime.strptime(date, "%Y-%m-%d")
to access the strptime method. Or, you could change the import statement to this:
from datetime import datetime
and access it as you are.
The people who made the datetime module also named their class datetime:
#module class method
datetime.datetime.strptime(date, "%Y-%m-%d")
Use the correct call: strptime is a classmethod of the datetime.datetime class, it's not a function in the datetime module.
self.date = datetime.datetime.strptime(self.d, "%Y-%m-%d")
As mentioned by Jon Clements in the comments, some people do from datetime import datetime, which would bind the datetime name to the datetime class, and make your initial code work.
To identify which case you're facing (in the future), look at your import statements
import datetime: that's the module (that's what you have right now).
from datetime import datetime: that's the class.
I got the same problem and it is not the solution that you told. So I changed the "from datetime import datetime" to "import datetime". After that with
the help of "datetime.datetime" I can get the whole modules correctly. I guess this is the correct answer to that question.
Values ​​may differ depending on usage.
import datetime
date = datetime.datetime.now()
date.strftime('%Y-%m-%d') # date variable type is datetime
The value of the date variable must be a string::
date = '2021-09-06'
datetime.datetime.strptime(date, "%Y-%m-%d")
str(datetime.datetime.strptime(date, "%Y-%m-%d")) # show differently
The solutions mentioned by the others are correct. But for me, it was a problem with another library importing datetime module for me and overriding the datetime class I was importing.
an example with tsai library:
from datetime import datetime
from tsai.all import *
This will give you the error: 'module' object has no attribute 'strptime'.
In this case, just flip the order of imports or just don't import everything (even if the documentation does that) :
from tsai.all import *
from datetime import datetime

Categories