Read and process multiple json files in python - python

I'm trying to process many json files with user input.
If there are three indentical data ["a","a","a"] in GeneListA, I suppose it is going to run the code three times with
jsonurl = "http://abc.def/a/format=json"
However, I got the error :
Traceback (most recent call last):
File "/Users/me/miniconda3/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/me/miniconda3/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/me/miniconda3/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/me/Desktop/Project/gsea/analysis/views.py", line 49, in result
for gene in data[0][gidA]:
KeyError: 'NC_000913\r\nNC_000913\r\nNC_000913'
And here is my code:
AGOAnnos = []
for genefromlist in GeneListA:
jsonurl = "http://abc.def/"+genefromlist+"/format=json"
print(jsonurl)
with urllib.request.urlopen(jsonurl) as url:
data = json.loads(url.read().decode())
for gene in data[0][gidA]:
for anno in data[0][gidA][gene]:
if type(anno) is dict:
GOAnno = re.search(r'GO:\d+',anno["ID"])
if GOAnno:
AGOAnnos.append(GOAnno.group())
elif type(anno) is str:
GOAnno = re.search(r'GO:\d+',anno)
if GOAnno:
AGOAnnos.append(GOAnno.group())

I can't find what are the values of variables (gidA / gene) in your code.
The error raised because the in the data response there is no key with gidA

Related

Django-Channels 2 not persisting session data set in `connect`

channels==2.1.2 | channels-redis==2.2.1 | daphne==2.2.0 | Django==1.11.6
I've upgraded to Channels 2 specifically for the ability to access and modify the session from within a consumer (and access it in a view), but that doesn't seem to be working. Basically, I want to identify AnonymousUsers and send them messages (each his own, not all of them together).
Here's my routing.py file:
application = ProtocolTypeRouter({
# (http->django views is added by default)
'websocket': AuthMiddlewareStack(
URLRouter(
core.routing.websocket_urlpatterns
)
),
})
Here's my consumers.py file:
class ChatConsumer(WebsocketConsumer):
def connect(self):
if self.scope['user'].is_authenticated:
user_id = str(self.scope['user'].id)
self.scope['session']['user_identifier']= user_id
self.group_name = user_id
else:
user_id = str(self.scope['user']) + str(uuid.uuid4())
self.scope['session']['user_identifier'] = user_id
self.group_name = user_id
self.scope['session'].save()
print(f" in consumer: {self.scope['session']['user_identifier']}")
# Join room group
async_to_sync(self.channel_layer.group_add)(
self.group_name,
self.channel_name
)
self.accept()
And in the view where I need to use send, I'm trying to get the group_name (user_identifier) from the session in case of an AnonymousUser:
def get_spotify_link(request):
if request.user.is_authenticated:
user_identifier = str(request.user.id)
else:
user_identifier = request.session['user_identifier']
print(f"in get_spotify_link: {user_identifier}")
However, I'm intermittently (well, 99% of time) getting KeyError: 'user_identifier'.
Internal Server Error: /get_spotify_link/
Traceback (most recent call last):
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/myusername/PycharmProjects/artist_notify/core/views.py", line 442, in get_spotify_link
user_identifier = request.session['user_identifier']
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 57, in __getitem__
return self._session[key]
KeyError: 'user_identifier'
[2018/07/08 20:54:03] WebSocket HANDSHAKING / [127.0.0.1:65478]
in consumer: AnonymousUser8883761c-f673-413e-82e2-413d1cbb17e3
[2018/07/08 20:54:03] WebSocket CONNECT / [127.0.0.1:65478]
Internal Server Error: /get_spotify_link/
Traceback (most recent call last):
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/myusername/PycharmProjects/artist_notify/core/views.py", line 442, in get_spotify_link
user_identifier = request.session['user_identifier']
File "/Users/myusername/.virtualenvs/bap_dev/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 57, in __getitem__
return self._session[key]
KeyError: 'user_identifier'

TypeError: ufunc 'ndtri' not supported for the input types

How can I fix the following error?
I am trying to calucalte normsINV value.. But facing issue when i use it in my code for application
Internal Server Error: /reports/rschighlevel/
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/auth/decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/pulsedev2/prod/pulse.corp.uber.internal/ats/reports/views.py", line 167, in rschighlevel
print norm.ppf(a)
File "/usr/lib/python2.7/dist-packages/scipy/stats/distributions.py", line 1514, in ppf
place(output, cond, self._ppf(*goodargs) * scale + loc)
File "/usr/lib/python2.7/dist-packages/scipy/stats/distributions.py", line 2187, in _ppf
return _norm_ppf(q)
File "/usr/lib/python2.7/dist-packages/scipy/stats/distributions.py", line 2133, in _norm_ppf
return special.ndtri(q)
TypeError: ufunc 'ndtri' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe'
Here's my code:
from scipy.stats import norm
from scipy.special import ndtri
import pdb
sigma = []
for i in usigma:
a = (1-i[6])
print type(a)
print norm.ppf(a) - Error Line
sigma.append([i[0],i[1],i[2],i[3],i[4],i[5],i[6],i[7], a])
print sigma
I use Python 2.7 and Django.
error message

Getting error while reading the file content using Python and Django

I am trying to read the file content using URL and return that the content as response in html format but getting the following error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/lampp/htdocs/rework/Nuclear/RFI/secure/plant/views.py", line 217, in view_reactor
pers = User.objects.get(pk=request.session['id'])
File "/usr/local/lib/python2.7/dist-packages/django/contrib/sessions/backends/base.py", line 57, in __getitem__
return self._session[key]
KeyError: u'id'
I am providing my code below:
site = 'http://127.0.0.1:8000/'
my_list = []
my_list.extend(['home', 'view_reactor'])
if request.GET.get('file') is not None and request.GET.get('file') != '':
file = request.GET.get('file')
if file in my_list:
full_path = site+file
response = urllib.urlopen(full_path)
lines = response.readlines()
return HttpResponse(content=lines, content_type="text/html")
else:
return render(request, 'plant/home.html', {'count': 1})
else:
return render(request, 'plant/home.html', {'count': 1})
def view_reactor(request):
""" This function for to get serch screen. """
pers = User.objects.get(pk=request.session['id'])
root = []
user_name = pers.uname
count = 1
root.append(
{'username': user_name,
'count': count
})
return render(request, 'plant/view_reactor.html',
{'user': root, 'count': 1})
Here I am passing the the value in query string like this http://127.0.0.1:8000/createfile/?file=view_reactor and finally I need the response of http://127.0.0.1:8000/view_reactor page in html format. But in my code I am getting this error.
The error is saying that there is no id key in your session dictionary. In the following line:
pers = User.objects.get(pk=request.session['id'])
And to me it looks redundant to get a user like this. You should be able to get a user simply by doing this:
pers = request.user
Provided that you have auth middleware installed.
Second option (not tested though):
pers = User.objects.get(pk=request.session['_auth_user_id'])

Getting ValueError: while using encrypt and decrypt using Django and Python

I am getting the following error while trying to encrypt my string type value.
Error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/opt/lampp/htdocs/d30/carClinic_vulnerable/bookingservice/views.py", line 141, in signsave
obj = AES.new('this is a carkey123', AES.MODE_CBC, 'This is an IV456')
File "/usr/lib/python2.7/dist-packages/Crypto/Cipher/AES.py", line 94, in new
return AESCipher(key, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/Crypto/Cipher/AES.py", line 59, in __init__
blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
File "/usr/lib/python2.7/dist-packages/Crypto/Cipher/blockalgo.py", line 141, in __init__
self._cipher = factory.new(key, *args, **kwargs)
ValueError: AES key must be either 16, 24, or 32 bytes long
Here is my code:
name = request.POST.get('uname')
obj = AES.new('this is a carkey123', AES.MODE_CBC, 'This is an IV456')
enpass = obj.encrypt(name)
The answer is in plain letters in the error message:
"AES key must be either 16, 24, or 32 bytes long".
Your key ('this is a carkey123') is 19 bytes long, use a key that's either 16, 24 or 32 bytes long instead.

LDAP (2, 'No such file or directory') error using DJANGO

I am implementing a Django app, I am trying to fetch data from an LDAP server.
Here is my code in views.py.I have supplied all the necessary information below, but yet I am facing (2, 'No such file or directory') error. What Am I missing here?
LDAP_USERNAME = ""
LDAP_PASSWORD = ""
LDAP_BASEDN = ""
try:
l = ldap.initialize('')
l.protocol_version = ldap.VERSION2
l.simple_bind_s(LDAP_USERNAME, LDAP_PASSWORD)
search_filter = "(cn=*)"
search_attribute = None
search_scope = ldap.SCOPE_SUBTREE
ldap_result_id = l.search(LDAP_BASEDN, search_scope, search_filter, search_attribute)
result_set = []
while 1:
result_type, result_data = l.result(ldap_result_id, 0)
if (result_data == []):
break
else:
if result_type == ldap.RES_SEARCH_ENTRY:
result_set.append(result_data)
print result_set
except ldap.LDAPError, e:
print e
Error Traceback:
Traceback (most recent call last):
File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/core/handlers/base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Library/Python/2.7/site-packages/Django-1.11.2-py2.7.egg/django/core/handlers/base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/////views.py", line 43, in index
l = ldap.initialize('xyz')
File "/Library/Python/2.7/site-packages/ldap/functions.py", line 94, in initialize
return LDAPObject(uri,trace_level,trace_file,trace_stack_limit)
File "/Library/Python/2.7/site-packages/ldap/ldapobject.py", line 79, in __init__
self._l = ldap.functions._ldap_function_call(ldap._ldap_module_lock,_ldap.initialize,uri)
File "/Library/Python/2.7/site-packages/ldap/functions.py", line 66, in _ldap_function_call
result = func(*args,**kwargs)
LDAPError: (2, 'No such file or directory')
ldap.initialize() requires a proper URL for the LDAP source. I had a same issue and LDAPError: (0, 'Error') and LDAPError: (2, 'No such file or directory') with flask helped me.
Try ldap.initialize(<protocol>://<host>:<port>). default protocol would be ldap and the default port is 389.

Categories