error when serializing calendar event - python

I am trying to modify calendars using the python caldav library (version 0.5.0) on python 2.7. I am using the following code snippet to add an exdate to recurring events
for event in results:
vevent = event.instance.vevent
if vevent.status.value != "CANCELLED":
if (vevent.summary.value, vevent.dtstart.value) in cancelled:
ev = calendar.event_by_uid(vevent.uid.value)
ev.instance.vevent.add("exdate").value = vevent.dtstart.value
print ev.instance.vevent.serialize()
Although the vobject library claims support for datetime objects the last line fails with:
Traceback (most recent call last):
File "./caldavTest.py", line 31, in <module>
print ev.instance.vevent.serialize()
File "/usr/lib/python2.7/site-packages/vobject/base.py", line 254, in serialize
return behavior.serialize(self, buf, lineLength, validate)
File "/usr/lib/python2.7/site-packages/vobject/behavior.py", line 166, in serialize
out = base.defaultSerialize(transformed, buf, lineLength)
File "/usr/lib/python2.7/site-packages/vobject/base.py", line 1007, in defaultSerialize
child.serialize(outbuf, lineLength, validate=False)
File "/usr/lib/python2.7/site-packages/vobject/base.py", line 254, in serialize
return behavior.serialize(self, buf, lineLength, validate)
File "/usr/lib/python2.7/site-packages/vobject/behavior.py", line 160, in serialize
transformed = obj.transformFromNative()
File "/usr/lib/python2.7/site-packages/vobject/base.py", line 226, in transformFromNative
raise NativeError(msg, lineNumber)
vobject.base.NativeError: "In transformFromNative, unhandled exception on line None <type 'exceptions.TypeError'>: 'datetime.datetime' object has no attribute '__getitem__'"
Any ideas?

Related

Error shows up when using df.to_parquet("filename")

I want to save the data set as a parquet file, called power.parquet, and I use df.to_parquet(<filename>). But it gives me this errer "ValueError: Error converting column "Global_reactive_power" to bytes using encoding UTF8. Original error: bad argument type for built-in operation" And I installed the fastparquet package.
from fastparquet import write, ParquetFile
dat.to_parquet("power.parquet")
df_parquet = ParquetFile("power.parquet").to_pandas()
df_parquet.head() # Test your final value
`*Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.9/site-packages/fastparquet/writer.py", line 259, in convert
out = array_encode_utf8(data)
File "fastparquet/speedups.pyx", line 50, in fastparquet.speedups.array_encode_utf8
TypeError: bad argument type for built-in operation
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/var/folders/4f/bm2th1p56tz4rq_zffc8g3940000gn/T/ipykernel_85477/3080656655.py", line 1, in <module>
dat.to_parquet("power.parquet", compression="GZIP")
File "/opt/anaconda3/lib/python3.9/site-packages/dask/dataframe/core.py", line 4560, in to_parquet
return to_parquet(self, path, *args, **kwargs)
File "/opt/anaconda3/lib/python3.9/site-packages/dask/dataframe/io/parquet/core.py", line 732, in to_parquet
return compute_as_if_collection(
File "/opt/anaconda3/lib/python3.9/site-packages/dask/base.py", line 315, in compute_as_if_collection
return schedule(dsk2, keys, **kwargs)
File "/opt/anaconda3/lib/python3.9/site-packages/dask/threaded.py", line 79, in get
results = get_async(
File "/opt/anaconda3/lib/python3.9/site-packages/dask/local.py", line 507, in get_async
raise_exception(exc, tb)
File "/opt/anaconda3/lib/python3.9/site-packages/dask/local.py", line 315, in reraise
raise exc
File "/opt/anaconda3/lib/python3.9/site-packages/dask/local.py", line 220, in execute_task
result = _execute_task(task, data)
File "/opt/anaconda3/lib/python3.9/site-packages/dask/core.py", line 119, in _execute_task
return func(*(_execute_task(a, cache) for a in args))
File "/opt/anaconda3/lib/python3.9/site-packages/dask/utils.py", line 35, in apply
return func(*args, **kwargs)
File "/opt/anaconda3/lib/python3.9/site-packages/dask/dataframe/io/parquet/fastparquet.py", line 1167, in write_partition
rg = make_part_file(
File "/opt/anaconda3/lib/python3.9/site-packages/fastparquet/writer.py", line 716, in make_part_file
rg = make_row_group(f, data, schema, compression=compression,
File "/opt/anaconda3/lib/python3.9/site-packages/fastparquet/writer.py", line 701, in make_row_group
chunk = write_column(f, coldata, column,
File "/opt/anaconda3/lib/python3.9/site-packages/fastparquet/writer.py", line 554, in write_column
repetition_data, definition_data, encode[encoding](data, selement), 8 * b'\x00'
File "/opt/anaconda3/lib/python3.9/site-packages/fastparquet/writer.py", line 354, in encode_plain
out = convert(data, se)
File "/opt/anaconda3/lib/python3.9/site-packages/fastparquet/writer.py", line 284, in convert
raise ValueError('Error converting column "%s" to bytes using '
ValueError: Error converting column "Global_reactive_power" to bytes using encoding UTF8. Original error: bad argument type for built-in operation
*
I tried by adding object_coding = "bytes".I want to solve this problem.

How do I reformat this json for database import?

I have these "json" files that I like to insert into my mongodb database.
An example of one is:
http://s.live.ksmobile.net/cheetahlive/de/ff/15201023827214369775/15201023827214369775.json
The problem is, that it is formated like this:
{ "channelType":"TEMPGROUP", ... } # line 1
{ "channelType":"TEMPGROUP", ... } # line 2
So instead of inserting it as 1 document in the DB, it insert every single line as 1 entry. That ends up with what should be 3 documents from 3 "json" files in the database become 1189 documents in the database instead.
How can I insert the whole content of the ".json" into one document?
My code is:
replay_url = "http://live.ksmobile.net/live/getreplayvideos?"
userid = 969730808384462848
url2 = replay_url + urllib.parse.urlencode({'userid': userid}) + '&page_size=1000'
raw_replay_data = requests.get(url2).json()
for i in raw_replay_data['data']['video_info']:
url3 = i['msgfile']
raw_message_data = urllib.request.urlopen(url3)
for line in raw_message_data:
json_data = json.loads(line)
messages.insert_one(json_data)
print(json_data)
Update to give more information to answer
messages.insert(json_data) gives this error:
Traceback (most recent call last):
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/collection.py", line 633, in _insert
blk.execute(concern, session=session)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/bulk.py", line 432, in execute
return self.execute_command(generator, write_concern, session)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/bulk.py", line 329, in execute_command
raise BulkWriteError(full_result)
pymongo.errors.BulkWriteError: batch op errors occurred
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/import_messages_dev.py", line 43, in <module>
messages.insert(json_data)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/collection.py", line 2941, in insert
check_keys, manipulate, write_concern)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/collection.py", line 635, in _insert
_raise_last_error(bwe.details)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/helpers.py", line 220, in _raise_last_error
_raise_last_write_error(write_errors)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/helpers.py", line 188, in _raise_last_write_error
raise DuplicateKeyError(error.get("errmsg"), 11000, error)
pymongo.errors.DuplicateKeyError: E11000 duplicate key error index: liveme.messages.$_id_ dup key: { : ObjectId('5aa2fc6f5d60126499060949') }
messages.insert_one(json_data) gives me this error:
Traceback (most recent call last):
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/import_messages_dev.py", line 43, in <module>
messages.insert_one(json_data)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/collection.py", line 676, in insert_one
common.validate_is_document_type("document", document)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/common.py", line 434, 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
messages.insert_many(json_data) gives me this error:
Traceback (most recent call last):
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/import_messages_dev.py", line 43, in <module>
messages.insert_many(json_data)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/collection.py", line 742, in insert_many
blk.execute(self.write_concern.document, session=session)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/bulk.py", line 432, in execute
return self.execute_command(generator, write_concern, session)
File "/media/anon/06bcf743-8b4d-409f-addc-520fc4e19299/PycharmProjects/LiveMe/venv1/lib/python3.6/site-packages/pymongo/bulk.py", line 329, in execute_command
raise BulkWriteError(full_result)
pymongo.errors.BulkWriteError: batch op errors occurred
messages.insert and messages.insert_many both insert 1 line and throw the error.
These files obviously do not contain properly formatted json - rather they contain a separate object on each line.
To turn them into valid json, you probably want a list of objects, i.e.:
[{ "channelType":"TEMPGROUP", ... },
{ "channelType":"TEMPGROUP", ... }]
You can achieve this by doing:
for i in raw_replay_data['data']['video_info']:
url3 = i['msgfile']
raw_message_data = urllib.request.urlopen(url3)
json_data = []
for line in raw_message_data:
json_data.append(json.loads(line))
messages.insert_one(json_data)
print(json_data)

PYTSK - Unable to retrieve file type

I am trying to acquire all files from a windows VHD.
When calling .info or .info.meta.type on a file entry it doesn't always work and throws an error. Is there any way to retrieve these files?
filesystemObject = pytsk3.FS_Info(imagehandle, offset=(partition.start * metric.block_size))
except:
print "Partition has no supported file system"
continue
print "File System Type Dectected ", filesystemObject.info.ftype
directoryObject = filesystemObject.open_dir(path=dirPath)
for entryObject in directoryObject:
if (not hasattr(entryObject, "info") or
not hasattr(entryObject.info, "name") or
not hasattr(entryObject.info.name, "name") or
entryObject.info.name.name in [".", ".."]):
continue
try:
f_type = entryObject.info.meta.type
except:
print "Cannot retrieve type of", entryObject.info.name.name
metric.total_files += 1
metric.failed_files += 1
continue
Traceback:
> Traceback (most recent call last): File "/Users/paulledwith/PycharmProjects/FYP/Server.py", line 352, in <module>
directoryRecurse(directoryObject, []) File "/Users/paulledwith/PycharmProjects/FYP/Server.py", line 185, in directoryRecurse
directoryRecurse(sub_directory, parentPath) File "/Users/paulledwith/PycharmProjects/FYP/Server.py", line 185, in directoryRecurse
directoryRecurse(sub_directory, parentPath) File "/Users/paulledwith/PycharmProjects/FYP/Server.py", line 185, in directoryRecurse
directoryRecurse(sub_directory, parentPath) File "/Users/paulledwith/PycharmProjects/FYP/Server.py", line 172, in directoryRecurse
print entryObject.info.meta.size AttributeError: 'NoneType' object has no attribute 'size'
This does retrieve about 90% of the files on the 5gb VHD I am working on but 90% is as good as 0.

neomodel giving Attribute error on save

I am running the following config for neo4j:
neo4j - 3.0.0
py2neo - 2.0.8
neomodel - 2.0.2
Finally, the code I try to run is:
class User(neomodel.StructuredNode):
user_id = neomodel.IntegerProperty(unique_index=True, required=True)
name = neomodel.StringProperty()
phone_number = neomodel.StringProperty()
user = User(user_id=6, name='Sourabh Dev', phone_number='9711237840').save()
I don't understand, why I keep getting this strange error. Am I doing something wrong here or should I use py2neo instead of neomodel?
My traceback is:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/signals.py", line 25, in hooked
val = fn(self, *args, **kwargs)
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/core.py", line 159, in save
self._id = self.create(self.__properties__)[0]._id
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/core.py", line 289, in create
results = db.cypher_query(query, params)
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/util.py", line 213, in cypher_query
results = self._execute_query(query, params)
File "/usr/local/lib/python3.4/site-packages/neomodel-2.0.2-py3.4.egg/neomodel/util.py", line 207, in _execute_query
results = self.session.cypher.execute(query, create_params=params)
File "/usr/local/lib/python3.4/site-packages/py2neo/cypher/core.py", line 136, in execute
results = tx.commit()
File "/usr/local/lib/python3.4/site-packages/py2neo/cypher/core.py", line 333, in commit
return self.post(self.__commit or self.__begin_commit)
File "/usr/local/lib/python3.4/site-packages/py2neo/cypher/core.py", line 288, in post
raise self.error_class.hydrate(error)
File "/usr/local/lib/python3.4/site-packages/py2neo/cypher/error/core.py", line 54, in hydrate
error_cls = getattr(error_module, title)
AttributeError: 'module' object has no attribute 'TypeError'
Two points here:
Existing Neomodel doesn't support the Neo4j 3.0
Cypher syntax has changed in 3.0, so the error raises.
In 2.x , MATCH n RETURN n
In 3.0 , MATCH (n) RETURN n
Node n is enclosed in braces.

TypeError: <function context_today at 0xaaa7064> is not JSON serializable

I need to return date to the date field when i change my division
Here is my Python code
tea_worker_ids = self.pool.get('bpl.worker').search(cr, uid, [('bpl_division_id', '=', division_id), ('default_work', '=', 'tea')])
for record in self.pool.get('bpl.worker').browse(cr, uid, tea_worker_ids):
tea_list_data.append({'worker_id': record.id, 'worker_emp_no': record.emp_no, 'is_selected':True,'date': (fields.date.context_today)})#
tea_v['selected_tea_workers'] = tea_list_data
this is the error
2013-05-13 06:22:28,810 15393 ERROR ABC werkzeug: Error on request:
Traceback (most recent call last):
File "/usr/share/pyshared/werkzeug/serving.py", line 159, in run_wsgi
execute(app)
File "/usr/share/pyshared/werkzeug/serving.py", line 146, in execute
application_iter = app(environ, start_response)
File "/usr/share/pyshared/simplejson/__init__.py", line 286, in dumps
return _default_encoder.encode(obj)
File "/usr/share/pyshared/simplejson/encoder.py", line 226, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/share/pyshared/simplejson/encoder.py", line 296, in iterencode
return _iterencode(o, 0)
File "/usr/share/pyshared/simplejson/encoder.py", line 202, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <function context_today at 0xaaa7064> is not JSON serializable
please help me to sort this issue,
EDITED
when it changed to fields.date.context_today()
then error comes as below
File "/home/bellvantage/Documents/openerp-7.0/openerp-7/openerp/addons/bpl/bpl.py", line 1059, in on_change_division
workers_list.append({'worker_id': record.id,'start_date': fields.date.context_today()})
TypeError: context_today() takes at least 3 arguments (0 given)
Looks like context_today is a method, not a value. You need to call it:
tea_list_data.append({'worker_id': record.id, 'worker_emp_no': record.emp_no,
'is_selected':True,'date': (fields.date.context_today())})
its worked with
workers_list.append({'worker_id': record.id, 'start_date': time.strftime('%Y-%m-%d'), 'selected':True})
Here fields.date.context_today is a method, so you need to pass argument, so it will be look like,
tea_list_data.append({'worker_id': record.id, 'worker_emp_no': record.emp_no,
'is_selected':True,
'date': (fields.date.context_today(self,cr,uid,context=context))})

Categories

Resources