Trying to set the datatype to decimal. I get an error that pyarrow doesn't have the attribute decimal
>>> import pyarrow
>>> pyarrow.decimal(8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'pyarrow' has no attribute 'decimal'
Found that pa.decimal128(18) works
Related
I would like to get familiar with classes and attributes and defined the following minimal example in the Python console:
class test:
def __init__(self, name):
self.name = name
I initiated a class instance:
>>> first_test = test('Linus')
>>> first_test.name
'Linus'
Works fine. I also tried getattr() to get the value of the attribute:
>>> getattr(first_test, 'name')
'Linus'
No problem. Next, I tried packing getattr() into a function:
def return_name(instance, attribute):
return getattr(instance, attribute)
Again, all fine:
>>> return_name(first_test, 'name')
'Linus'
Now, I wanted to try the same thing with the instance.attribute syntax:
def return_name(instance, attribute):
return instance.attribute
But for some reason, this fails:
>>> return_name(first_test, name)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'name' is not defined
Another try, this time passing the attribute name as a string:
>>> return_name(first_test, 'name')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in return_name
AttributeError: 'test' object has no attribute 'attribute'
Fail again?
What is the problem here?
My cmd:
python manage.py shell
from user.models import UserInfo
from user.serializers import UserInfoSerializer
import io
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
u=UserInfo.objects.all()[0]
s=UserInfoSerializer(u)
j=JSONRenderer().render(s.data)
o=io.BytesIO(j)
d=JSONParser().parse(o)
s1=UserInfoSerializer(data=d)
s1.is_valid()
But the issue here is that s1.is_valid() always comes out to be False and I can't save s1 as a serializer.
I'm getting this error:
>>> d
{'username': 'user001', 'password': 'pass001', 'email': 'user001#example.com', 'contact': 9876543210}
>>> s001=UserInfoSerializer(data=d)
>>> s001.is_valid()
False
>>> s001.validated_data()
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: 'dict' object is not callable
>>> s001.validated_data
{}
>>> s001.data
{'username': 'user001', 'password': 'pass001', 'email': 'user001#example.com', 'contact': 9876543210}
>>> s001.save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/imharjyotbagga/PycharmProjects/DRF/venv/lib/python3.7/site-packages/rest_framework/serializers.py", line 182, in save
'You cannot call `.save()` on a serializer with invalid data.'
AssertionError: You cannot call `.save()` on a serializer with invalid data.
>>>
So how can I go about this!?
You'd need to show .errors attribute to even try to diagnose the problem. My blind guess would be you messed up your password field. But you won't get any definitive answer unless you produce the code of your serializer, model, and the .errors of your serializer would also be really helpful.
json.dumps gives an error if both json and jsondatetime are imported. The error is the following:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
TypeError: encode() missing 1 required positional argument: 'o'
But I just import JSON, then json.dumps work fine. I don't know how to deal with this. I need jsondatetime as well
This works::
import json
json.dumps({'DbName': 'DB','Hostname': '10.0.0.6','DbUsername':'SYSTEM'})
'{"Hostname": "10.0.0.6","DbName": "DB", "DbUsername": "SYSTEM"}'
This does not work::
import jsondatetime
import json
json.dumps({'DbName': 'DB', 'Hostname': '10.0.0.6', 'DbUsername': 'SYSTEM'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
TypeError: encode() missing 1 required positional argument: 'o'
jsondatetime is a drop-in replacement for json. You should only have
import jsondatetime as json
From the documentation:
JSON-datetime is a very simple wrapper around Python simplejson loads method. It decodes datetime values contained in JSON strings.
I have been trying to upload a Pandas dataframe to a JSON object in Cloud Storage using Cloud Function. Follwing is my code -
def upload_blob(bucket_name, source_file_name, destination_blob_name):
"""Uploads a file to the bucket."""
storage_client = storage.Client()
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_file(source_file_name)
print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))
final_file = pd.concat([df, df_second], axis=0)
final_file.to_json('/tmp/abc.json')
with open('/tmp/abc.json', 'r') as file_obj:
upload_blob('test-bucket',file_obj,'abc.json')
I am getting the following error in line - blob.upload_from_file(source_file_name)
Deployment failure:
Function failed on loading user code. Error message: Code in file main.py
can't be loaded.
Detailed stack trace: Traceback (most recent call last):
File "/env/local/lib/python3.7/site-
packages/google/cloud/functions/worker.py", line 305, in
check_or_load_user_function
_function_handler.load_user_function()
File "/env/local/lib/python3.7/site-
packages/google/cloud/functions/worker.py", line 184, in load_user_function
spec.loader.exec_module(main)
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/user_code/main.py", line 6, in <module>
import datalab.storage as gcs
File "/env/local/lib/python3.7/site-packages/datalab/storage/__init__.py",
line 16, in <module>
from ._bucket import Bucket, Buckets
File "/env/local/lib/python3.7/site-packages/datalab/storage/_bucket.py",
line 21, in <module>
import datalab.context
File "/env/local/lib/python3.7/site-packages/datalab/context/__init__.py",
line 15, in <module>
from ._context import Context
File "/env/local/lib/python3.7/site-packages/datalab/context/_context.py",
line 20, in <module>
from . import _project
File "/env/local/lib/python3.7/site-packages/datalab/context/_project.py",
line 18, in <module>
import datalab.utils
File "/env/local/lib/python3.7/site-packages/datalab/utils/__init__.py",
line 15
from ._async import async, async_function, async_method
^
SyntaxError: invalid syntax
What possibly is the error?
You are passing a string to blob.upload_from_file(), but this method requires a file object. You probably want to use blob.upload_from_filename() instead. Check the sample in the GCP docs.
Alternatively, you could get the file object, and keep using blob.upload_from_file(), but it's unnecessary extra lines.
with open('/tmp/abc.json', 'r') as file_obj:
upload_blob('test-bucket', file_obj, 'abc.json')
Use a bucket object instead of string
something like upload_blob(conn.get_bucket(mybucket),'/tmp/abc.json','abc.json')}
I am using Python 2.6.6, and trying to generate a ordered_dict from json string. I could understand that I could use object_pairs_hook of json Decoder/loads, but unfortunately it's not supported in 2.6.6. Is there any way out?
e.g.
template_s = '{ "aa": {"_type": "T1"}, "bb": {"_type": "T11"}}'
json.loads(template_s, object_pairs_hook=OrderedDict)
>>> json.loads(json_str, object_pairs_hook=OrderedDict)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/json/__init__.py", line 318, in loads
return cls(encoding=encoding, **kw).decode(s)
TypeError: __init__() got an unexpected keyword argument 'object_pairs_hook'
Thanks
I was able to do the same with simplejson
import simplejson as json
json.loads(config_str, object_pairs_hook=json.OrderedDict)