Python module 'modules' has no attribute 'millis' - python

Completely new to Python, I have installed the latest stock version, updated PIP and tried to run a script been given to import data from API (person provided says works for them), when tried to run I have installed each library as required.
import json
import pandas as pd
from pandas.io.json import json_normalize
import requests
import modules
from requests_oauthlib import OAuth1
from datetime import datetime
this is main header, I am now getting error AttributeError: module 'modules' has no attribute 'millis' on following line
payload = {'interactive': 'true',
"ended": 'true',
"start": {"from": modules.millis(2019,4,21,0,0), # CHANGE HERE THE DATE
"to": modules.millis(2019,4,24,23,59)}, # CHANGE HERE THE DATA
'skillIds':['1286977632']
}
Any ideas.

sorry guys I have found the fix, it was in a separate file I was not given at the time
# returns the elapsed milliseconds since the start of the program
def millis(year,month,day,hour,minute):
dt = datetime(year,month,day,hour, minute) - start_time
ms = (dt.days * 24 * 60 * 60 + dt.seconds) * 1000 + dt.microseconds / 1000.0
return round(ms)

Related

ModuleNotFoundError in python code - cannot find custom class in console app

I create a python console app that includes imports of a custom class I'm using. Everytime I run my app I get the error ModuleNotFoundError: "No module named 'DataServices'.
Can you help?
Provided below is my folder structure:
ETL
Baseball
Baseball_DataImport.py
DataServices
DataService.py
ConfigServices.py
PageDataMode.py
SportType.py
Here is the import section from the Baseball_DataImport.py file. This is the file when I run I get the error:
from bs4 import BeautifulSoup
import scrapy
import requests
import BaseballEntity
import mechanize
import re
from time import sleep
import logging
import time
import datetime
from functools import wraps
import json
import DataServices.DataService - Error occurs here
Here is my DataService.py file:
import pymongo
import json
import ConfigServices
import PageDataModel
#from SportType import SportType
class DataServices(object):
AppConfig: object
def __init__(self):
AppConfig = ConfigServices.ConfigService()
#print(AppConfig)
#def GetPagingDataBySport(self,Sport:SportType):
def GetPagingDataBySport(self):
#if Sport == SportType.BASEBALL:
pagingData = []
pagingData.append(PageDataModel.PageDataModel("", 2002, 2))
pagingData.append(PageDataModel.PageDataModel("", 2003, 2))
return pagingData
It might seem that your structure is:
Baseball
Baseball_DataImport.py
Dataservices
Dataservice.py
Maybe you need to do from Dataservices.Dataservice import DataServices
Edit:
I created the folder structure, and the method I showed you works:
Here's the implementation
Dataservice.py only contains:
class DataServices():
pass
Did you try copieing the Dataservice.py into the Projectfolder with the main.py?

python script slow after base64 module import

I am trying to generate keys using os.urandom() and base64 methods. Please see the below code. gen_keys() itself may not be very slow, but
the script overall run time is very slow. For example, gen_keys() takes
about 0.85 sec where as the overall script run time is is 2 minutes 6 seconds. I suspect this is some thing to do with module imports. Although I need all of the modules from my script.
Any thoughts on the real issue? Thanks.
I am using python3.4
#!/usr/bin/env python3
import netifaces
import os
import subprocess
import shlex
import requests
import time
import json
import psycopg2
import base64
def gen_keys():
start_time = time.time()
a_tok = os.urandom(40)
a_key = base64.urlsafe_b64encode(a_tok).rstrip(b'=').decode('ascii')
s_tok = os.urandom(64)
s_key = base64.urlsafe_b64encode(s_tok).rstrip(b'=').decode('ascii')
print("a_key: ", a_key)
print("s_key: ", s_key)
end_time = time.time()
print("time taken: ", end_time-start_time)
def Main():
gen_keys()
if __name__ == '__main__':
Main()
$~: time ./keys.py
a_key: 52R_5u4I1aZENTsCl-fuuHU1P4v0l-urw-_5_jCL9ctPYXGz8oFnsQ
s_key: HhJgnywrfgfplVjvtOciZAZ8E3IfeG64RCAMgW71Z8Tg112J11OHewgg0r4CWjK_SJRzYzfnN-igLJLRi1CkeA
time taken: 0.8523025512695312
real 2m6.536s
user 0m0.287s
sys 0m7.007s
$~:

The managed object is not valid - Dynamo Crashes

The following error in Dynamo is bugging me since many hours.
Warning:
IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File " < string > ", line 33, in < module>
Exception: The managed object is not valid.
I’m not sure why the error has occur would appreciate if someone share its solution with me. Thank you
import clr
clr.AddReference('ProtoGeometry')
import Autodesk.DesignScript.Geometry
# Import Element wrapper extension methods
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# Import geometry conversion extension methods
clr.ImportExtensions(Revit.GeometryConversion)
# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
# Import RevitAPI
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
import System
from System import Array
from System.Collections.Generic import *
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)
The above are my standard imports while working with Dynamo/Revit API. It has been working for me for a while.
In all honesty I wish I had an "actual" answer why your script is not working. The only thing that is different than mine, is order in which you reference certain things. That should have no effect on their validity. It's Dynamo though, and it's very capricious.

Datetime and Time can't be used in the same program?

So I'm writing a program with
import datetime
import time
I'm using time to record the time it takes the program to run, and I need to check the date so if the file is more than a certain age, don't process it.
I keep getting this error when trying to use these two classes
Traceback (most recent call last):
File "<stdin>", line 563, in <module>
File "<stdin>", line 498, in main
AttributeError: type object 'datetime.time' has no attribute 'time'
shell returned 1
Is it not possible to use both time and datetime in one program?
Some of the code:
import PyPDF2
import re
import os
#Time testing
import time
#Using this to check if address is in proper format and to clean it up
import usaddress
#testing this one out
import datetime
from dateutil.parser import *
from dateutil.tz import *
from datetime import *
#Timer starts
start_time = time.time() #Error is referring to this line, line 498
#Opens 3 different files
#For file in folder parse it to text
#Writes some things to file
#Gets the date from the file
if date != None:
fileDate = parse(date).year
now = datetime.now()
print now.year, now.month, now.day
#Ends the timer and prints the time to the console
print("--- %s seconds ---" % round(time.time() - start_time, 2))
Here is your problem:
import datetime
from dateutil.parser import *
from dateutil.tz import *
from datetime import * # <<<< problems
First you are importing datetime and then you are importing everything from datetime.
Be explicit, and only import what you need.
from datetime import datetime
Then you can use it as datetime.now or whatever methods you may need.
As a rule of thumb, never import *. It causes exactly these sorts of issues.
The problems is:
from datetime import *
because it imports time from datetime. It's always better to import only what you need. But if you also really need this method you could do (for example):
from datetime import time as dt
That's why import * is dangerous...

PyCharm (1.5.4) and Pandas 0.6.0 - ImportError: No module named data

I am using PyCharm (1.5.4) as my python IDE on MacOS 10.6.4. I am tinkering with some code to manipulate stock price data. As part of that I want to import price data from yahoo by using the DataReader function that comes with Pandas 0.6.0. The code is as follow:
http://www.statalgo.com/2011/09/08/pandas-getting-financial-data-from-yahoo-fred-etc/
from pandas import ols, DataFrame
from pandas.stats.moments import rolling_std
from pandas.io.data import DataReader
import datetime
sp500 = DataReader("^GSPC", "yahoo", start=datetime.datetime(1990, 1, 1))
sp500_returns = sp500["adj clos"].shift(-250)/sp500["adj clos"] - 1
gdp = DataReader("GDP", "fred", start=datetime.datetime(1990, 1, 1))["value"]
gdp_returns = (gdp/gdp.shift(1) - 1)
gdp_std = rolling_std(gdp_returns, 10)
gdp_standard = gdp_returns / gdp_std
gdp_on_sp = ols(y=sp500_returns, x=DataFrame({"gdp": gdp_standard}))
sp500.plot()
gdp.plot()
When I run the code I get the following error:
Traceback (most recent call last):
File "/Users/MyName/PycharmProjects/test/mytest", line 3, in <module>
from pandas.io.data import DataReader
ImportError: No module named data
I see that PyCharm does not know how to unresolve the reference 'data'.
My python paths are set as follows:
import sys
from pprint import pprint as pp
pp(sys.path)
['/private/var/folders/st/stQUFIfOG28bmpY9dCspTk+++TI/-Tmp-',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/scikits.statsmodels-0.3.1-py2.7.egg',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python27.zip',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/plat-darwin',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/plat-mac',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/plat-mac/lib-scriptpackages',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/lib-tk',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/lib-old',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/lib-dynload',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages',
'/Library/Frameworks/EPD64.framework/Versions/7.1/lib/python2.7/site-packages/PIL']
What is puzzling is that PyCharm can resolve pandas.stats.moments but can't resolve pandas.io.data. I checked that both directories have the __init__.py file (the files are blank).
At this point I am not sure how to move forward. Greatly appreciate the help.
UPDATE:
$ cat __egginst__.txt
# egginst metadata
egg_name = 'pandas-0.3.0-3.egg'
prefix = '/Library/Frameworks/EPD64.framework/Versions/7.1'
installed_size = 1454562
rel_files = [
'EGG-INFO/pandas/__egginst__.txt',
'lib/python2.7/site-packages/pandas-0.3.0-3.egg-info',
Seems like deleting PyCharm's python interpreter configuration and re-configuring solved the problem. Strange... but fixed

Categories

Resources