Populate Mongo Database with Json file in Python Script from command Line - python

page = open("npm.json", "r")
parsed = json.loads(page.read())
for i in parsed["dependencies"]:
collection.insert_one(i)
Hi, I am trying to read in a json file and populate my mongoDB with the rows called ependencies.it keeps giving me and error. I have tried insert, insert_one &insert_many to no avail.
The following is the error i get
File "database.py", line 43, in <module>
collection.insert_one(i)
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 684, in insert_one
common.validate_is_document_type("document", document)
File "/usr/local/lib/python3.6/dist-packages/pymongo/common.py", line 453, in validate_is_document_type
"collections.MutableMapping" % (option,))
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 23, in <module>
import apt
File "/usr/lib/python3/dist-packages/apt/__init__.py", line 23, in <module>
import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Original exception was:
Traceback (most recent call last):
File "database.py", line 43, in <module>
collection.insert_one(i)
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 684, in insert_one
common.validate_is_document_type("document", document)
File "/usr/local/lib/python3.6/dist-packages/pymongo/common.py", line 453, in validate_is_document_type
"collections.MutableMapping" % (option,))
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping
The json file is a list of node dependencies. i ran the command npm list on a node project. Can anybody help Thanks in advance

It looks like your data in dependencies is a dictionary. This means when you iterate over it, you will get strings (which are the keys in this case).
An example would be like so:
my_entry = {'a': 1, 'b': 2}
for i in myentry:
print(i)
# will print 'a' then 'b', not 1 then 2
I'm assuming each dependency is a key, and you want to insert the value. So
# dictionary.values() will give you just the values
for v in parsed['dependencies'].values():
if isinstance(v, dict):
collection.insert_one(v)
else:
raise ValueError("object is not dictionary")
Mongo will only take json or bson entries, which are like dictionaries. When you pass keys, you are trying to insert objects of type string, which it doesn't support

Related

Can we convert google-cloud-dialogflow api returned types to json?

I having trouble converting dialogflow types such as ListIntentsResponse, EntityType to json. I have researched a lot into this. Converting every entry one by one is a headache thats why I want a workaround.
I have tried using google.protobuf.json_format methods. But it doesnt works. says UNknown field : DESCRIPTOR
from google.protobuf.json_format import *
client = dialogflow.IntentsClient()
request = dialogflow.ListIntentsRequest(
parent=f'projects/{DIALOGFLOW_PROJECT_ID}/agent'
)
response = client.list_intents(request)
# print(response)
print(MessageToJson(response ,descriptor_pool=None))```
**Error==>>>**
Traceback (most recent call last):
File "c:\Users\1150-Bilal\Desktop\chatbot\intents.py", line 12, in <module>
intentlist()
File "c:\Users\1150-Bilal\Desktop\chatbot\intents.py", line 10, in intentlist
print(MessageToJson(response ,descriptor_pool=None))
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\google\protobuf\json_format.py", line 130, in MessageToJson
return printer.ToJsonString(message, indent, sort_keys, ensure_ascii)
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\google\protobuf\json_format.py", line 197, in ToJsonString
js = self._MessageToJsonObject(message)
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\google\protobuf\json_format.py", line 203, in _MessageToJsonObject
message_descriptor = message.DESCRIPTOR
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\google\cloud\dialogflow_v2\services\intents\pagers.py", line 74, in __getattr__
return getattr(self._response, name)
File "C:\Users\1150-Bilal\Desktop\chatbot\botenv\lib\site-packages\proto\message.py", line 747, in __getattr__
raise AttributeError(
AttributeError: Unknown field for ListIntentsResponse: DESCRIPTOR

Unable to Import Spacy or Download Models

I recently tried to install the spaCy module for Python 3.7. The installation looks like it runs successfully (shows no errors), but when I try to import spaCy, or when I try to install spaCy models, I get the error below. I'm totally lost; please help!
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\mjrei\AppData\Local\Programs\Python\Python37\lib\site-packages\spacy\__init__.py", line 14, in <module>
from . import pipeline # noqa: F401
File "C:\Users\mjrei\AppData\Local\Programs\Python\Python37\lib\site-packages\spacy\pipeline\__init__.py", line 1, in <module>
from .attributeruler import AttributeRuler
File "C:\Users\mjrei\AppData\Local\Programs\Python\Python37\lib\site-packages\spacy\pipeline\attributeruler.py", line 6, in <module>
from .pipe import Pipe
File "spacy\pipeline\pipe.pyx", line 1, in init spacy.pipeline.pipe
File "spacy\strings.pyx", line 15, in init spacy.strings
File "C:\Users\mjrei\AppData\Local\Programs\Python\Python37\lib\site-packages\spacy\util.py", line 1635, in <module>
default_error_handler: Callable[[str, "Pipe", List["Doc"], Exception], NoReturn],
File "C:\Users\mjrei\AppData\Local\Programs\Python\Python37\lib\typing.py", line 755, in __getitem__
return self.__getitem_inner__(params)
File "C:\Users\mjrei\AppData\Local\Programs\Python\Python37\lib\typing.py", line 251, in inner
return func(*args, **kwds)
File "C:\Users\mjrei\AppData\Local\Programs\Python\Python37\lib\typing.py", line 774, in __getitem_inner__
result = _type_check(result, msg)
File "C:\Users\mjrei\AppData\Local\Programs\Python\Python37\lib\typing.py", line 135, in _type_check
raise TypeError(f"Plain {arg} is not valid as type argument")
TypeError: Plain typing.NoReturn is not valid as type argument

Using pwntools in mac

I want to use pwntools(which is python module) in my mac. However I get error instead of pwntools. I tried all step in https://github.com/Gallopsled/pwntools/issues. But I get
Traceback (most recent call last):
File "/Users/Knight/PycharmProjects/untitled/gmail.py", line 1, in <module>
import pwn
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwn/__init__.py", line 2, in <module>
from .toplevel import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwn/toplevel.py", line 20, in <module>
import pwnlib
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwnlib/__init__.py", line 40, in <module>
importlib.import_module('.%s' % module, 'pwnlib')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwnlib/args.py", line 10, in <module>
from . import term
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwnlib/term/__init__.py", line 3, in <module>
from . import completer
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwnlib/term/completer.py", line 4, in <module>
from . import readline
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwnlib/term/readline.py", line 4, in <module>
from . import text
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwnlib/term/text.py", line 115, in <module>
sys.modules[__name__] = Module()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwnlib/term/text.py", line 26, in __init__
self.num_colors = termcap.get('colors', default = 8)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pwnlib/term/termcap.py", line 25, in get
s = curses.tigetstr(cap)
_curses.error: must call (at least) setupterm() first
I can't understand why I can't use it.
Help me please
You must set the following environment variables as indicated here
export TERM=linux
export TERMINFO=/etc/terminfo
Setupterm could not find terminal, in Python program using curses

How to add chain id in pdb

By using biopython library, I would like to add chains ids in my pdb file.
I'm using
p = PDBParser()
structure=p.get_structure('mypdb',mypdb.pdb)
model=structure[0]
model.child_list=["A","B"]
But I got this error:
Traceback (most recent call last):
File "../../principal_axis_v3.py", line 319, in <module>
main()
File "../../principal_axis_v3.py", line 310, in main
protA=read_PDB(struct,ch1,s1,e1)
File "../../principal_axis_v3.py", line 104, in read_PDB
chain=model[ch]
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/PDB/Entity.py", line 38, in __getitem__
return self.child_dict[id]
KeyError: 'A'
I tried to changes the keys in th child.dict, butI got another error:
Traceback (most recent call last):
File "../../principal_axis_v3.py", line 319, in <module>
main()
File "../../principal_axis_v3.py", line 310, in main
protA=read_PDB(struct,ch1,s1,e1)
File "../../principal_axis_v3.py", line 102, in read_PDB
model.child_dict.keys=["A","B"]
AttributeError: 'dict' object attribute 'keys' is read-only
How can I add chains ids ?
Your error is that child_list is not a list with chain IDs, but of Chain objects (Bio.PDB.Chain.Chain). You have to create Chain objects and then add them to the structure. A lame example:
from Bio.PDB.Chain import Chain
my_chain = Chain("C")
model.add(my_chain)
Now you can access the model child_dict:
>>> model.child_dict
{'A': <Chain id=A>, 'C': <Chain id=C>}
>>> model.child_dict["C"]
<Chain id=C>

Unable to load the package 'enthought.mayavi.mlab'

I'm trying to load the package enthought.mayavi.mlab in python's spider enviorment:
import enthought.mayavi.mlab as mlb
And I get the following error message:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 487, in runfile
execfile(filename, namespace)
File "C:\Users\Eytan\Desktop\Mah\PyBrainTraining.py", line 2, in <module>
import enthought.mayavi.mlab as mlb
File "C:\Python27\lib\site-packages\enthought\mayavi\mlab.py", line 3, in <module>
from mayavi.mlab import *
File "C:\Python27\lib\site-packages\mayavi\mlab.py", line 27, in <module>
from mayavi.tools.camera import view, roll, yaw, pitch, move
File "C:\Python27\lib\site-packages\mayavi\tools\camera.py", line 25, in <module>
from engine_manager import get_engine
File "C:\Python27\lib\site-packages\mayavi\tools\engine_manager.py", line 12, in <module>
from mayavi.preferences.api import preference_manager
File "C:\Python27\lib\site-packages\mayavi\preferences\api.py", line 4, in <module>
from preference_manager import preference_manager
File "C:\Python27\lib\site-packages\mayavi\preferences\preference_manager.py", line 29, in <module>
from traitsui.api import View, Group, Item
File "C:\Python27\lib\site-packages\traitsui\api.py", line 35, in <module>
from .editors.api import (ArrayEditor, BooleanEditor, ButtonEditor,
File "C:\Python27\lib\site-packages\traitsui\editors\__init__.py", line 22, in <module>
from .api import (toolkit, ArrayEditor, BooleanEditor, ButtonEditor,
File "C:\Python27\lib\site-packages\traitsui\editors\api.py", line 10, in <module>
from .code_editor import CodeEditor
File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 36, in <module>
class ToolkitEditorFactory ( EditorFactory ):
File "C:\Python27\lib\site-packages\traitsui\editors\code_editor.py", line 48, in ToolkitEditorFactory
mark_color = Color( 0xECE9D8 )
File "C:\Python27\lib\site-packages\traits\traits.py", line 487, in __call__
return self.maker_function( *args, **metadata )
File "C:\Python27\lib\site-packages\traits\traits.py", line 1183, in Color
return ColorTrait( *args, **metadata )
File "C:\Python27\lib\site-packages\traitsui\toolkit_traits.py", line 7, in ColorTrait
return toolkit().color_trait( *args, **traits )
File "C:\Python27\lib\site-packages\traitsui\toolkit.py", line 109, in toolkit
_toolkit = _import_toolkit(ETSConfig.toolkit)
File "C:\Python27\lib\site-packages\traitsui\toolkit.py", line 51, in _import_toolkit
return __import__( name, globals=globals(), level=1 ).toolkit
File "C:\Python27\lib\site-packages\traitsui\qt4\__init__.py", line 18, in <module>
import pyface.qt
File "C:\Python27\lib\site-packages\pyface\qt\__init__.py", line 35, in <module>
prepare_pyqt4()
File "C:\Python27\lib\site-packages\pyface\qt\__init__.py", line 17, in prepare_pyqt4
sip.setapi('QString', 2)
ValueError: API 'QString' has already been set to version 1
anyone knows how to fix it?
Edit: If it will help, I see think that the problem is explained in the last column:
ValueError: API 'QString' has already been set to version 1
Edit: I tried changing API selection for QString and QVariant objects to Api #1,#2. didn't help.
Tried either to set the "Ignore API change errors" to true, didn't help.
Tried EST_TOOLKIT from 'qt4' to 'wx'.
It might be because spider itself uses the QT library and has already initialized it.
According to this http://pythonhosted.org/spyder/installation.html you could use PySide instead of PyQt4 (but i don't know if that's a configuation option or a fallback).

Categories

Resources