I was trying to make an excel sheet but it seems that it's not working - python-3.6

import openpyxl
import pathlib
a = pathlib.Path("G:/myCourse_mhanned/19. SUPERHERO LEVEL Automate Excel Tasks with Python 3/1.1 Employees.xlsx.xlsx")
workbook = openpyxl.load_workbook(a)
print(workbook.sheetnames)
print(workbook.active)
sheet = workbook["EmployeeData"]
workbook.create_sheet("test_file")
workbook.save(a)
I expected to ad a new sheet but otherwise it gave me this permission error:
['EmployeeData', 'Salaries', 'Skills']
Traceback (most recent call last):
File "C:/Users/BMRL/PycharmProjects/pytrial/anuhabd.py", line 10, in
workbook.save(a)
File "C:\Users\BMRL\PycharmProjects\pytrial\venv\lib\site-packages\openpyxl\workbook\workbook.py", line 397, in save
save_workbook(self, filename)
File "C:\Users\BMRL\PycharmProjects\pytrial\venv\lib\site-packages\openpyxl\writer\excel.py", line 292, in save_workbook
archive = ZipFile(filename, 'w', ZIP_DEFLATED, allowZip64=True)
File "C:\Users\BMRL\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 1204, in init
self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'G:\myCourse_mhanned\19. SUPERHERO LEVEL Automate Excel Tasks with Python 3\1.1 Employees.xlsx.xlsx'

Use backslash instead of forward one that's due to windows paths that uses backslash \

Related

Error when exporting from BigQuery to MySQL

I am trying to export a table from BigQuery to Google Cloud MySQL database.
I found this operator called BigQueryToMySqlOperator (documented here https://airflow.apache.org/docs/apache-airflow-providers-google/stable/_api/airflow/providers/google/cloud/transfers/bigquery_to_mysql/index.html?highlight=bigquerytomysqloperator#module-airflow.providers.google.cloud.transfers.bigquery_to_mysql)
When I deploy the DAG containing this task onto cloud composer, the task always failed with the error
Traceback (most recent call last):
File "/opt/python3.8/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1113, in _run_raw_task
self._prepare_and_execute_task_with_callbacks(context, task)
File "/opt/python3.8/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1287, in _prepare_and_execute_task_with_callbacks
result = self._execute_task(context, task_copy)
File "/opt/python3.8/lib/python3.8/site-packages/airflow/models/taskinstance.py", line 1317, in _execute_task
result = task_copy.execute(context=context)
File "/opt/python3.8/lib/python3.8/site-packages/airflow/providers/google/cloud/transfers/bigquery_to_mysql.py", line 166, in execute
for rows in self._bq_get_data():
File "/opt/python3.8/lib/python3.8/site-packages/airflow/providers/google/cloud/transfers/bigquery_to_mysql.py", line 138, in _bq_get_data
response = cursor.get_tabledata(
File "/opt/python3.8/lib/python3.8/site-packages/airflow/providers/google/cloud/hooks/bigquery.py", line 2508, in get_tabledata
return self.hook.get_tabledata(*args, **kwargs)
File "/opt/python3.8/lib/python3.8/site-packages/airflow/providers/google/cloud/hooks/bigquery.py", line 1284, in get_tabledata
rows = self.list_rows(dataset_id, table_id, max_results, selected_fields, page_token, start_index)
File "/opt/python3.8/lib/python3.8/site-packages/airflow/providers/google/common/hooks/base_google.py", line 412, in inner_wrapper
raise AirflowException(
airflow.exceptions.AirflowException: You must use keyword arguments in this methods rather than positional
I don't really understand why it is throwing out this error. Can anyone help me figuring out what went wrong or how should I export data from BigQuery to MySQL DB? Much thanks for your help!
EDIT: My operator code would basically look like this
transfer_data = BigQueryToMySqlOperator(
task_id='task_id',
dataset_table='origin_bq_table',
mysql_table='dest_table_name',
replace=True,
)
Based on the stacktrace, you are most likely using apache-airflow-providers-google==2.2.0.
airflow.exceptions.AirflowException: You must use keyword arguments in
this methods rather than positional
This error originates from the GoogleBaseHook, which can be traced back the BigQueryToMySqlOperator.
BigQueryToMySqlOperator > BigQueryHook > BigQueryConnection > BigQueryCursor > get_tabledata
The reason why you are getting the AirflowException is because get_tabledata
is called as part of the execute method.
Unforuntately, the test for the operator is not comprehensive since it only checks whether or not the method was called was the correct parameters.
I think this will require a new release of the google provider where the cursor in BigQueryToMySqlOperator calls list_rows with keyword arguments instead of get_tabledata, which calls list_rows with positional arguments.
I have also made a Github Issue in the Airflow repository.

TypeError: string indices must be integers on ArangoDB

Arango module gives a weird error while accessing databases:
from arango import ArangoClient
client = ArangoClient(hosts='http://localhost:8529/')
sys_db = client.db('_system', username='root', password='root')
sys_db.databases()
below is the error:
Traceback (most recent call last): File "", line 1, in
File
"/home/ubuntu/arangovenv/lib/python3.6/site-packages/arango/database.py",
line 699, in databases
return self._execute(request, response_handler) File "/home/ubuntu/arangovenv/lib/python3.6/site-packages/arango/api.py",
line 66, in _execute
return self._executor.execute(request, response_handler) File "/home/ubuntu/arangovenv/lib/python3.6/site-packages/arango/executor.py",
line 82, in execute
return response_handler(resp) File "/home/ubuntu/arangovenv/lib/python3.6/site-packages/arango/database.py",
line 697, in response_handler
return resp.body['result'] TypeError: string indices must be integers
calling database module from "packages/arango/database.py" giving me the same error.
my env:
1) ubuntu 16.4
2) python-arango==5.2.1
any help appreciated.
If you are running it on some server, it may be a server issue. It was in my case at least. I ran the following to clear the proxy and it worked fine.
export http_proxy=''
As I guessed, resp.body is not the data type that you provided. line 697 of database.py is expecting something else. For example:
>>> data = "MyName"
>>> print(data[0])
'M'
>>> print(data['anything'])
TypeError: string indices must be integers
First print command gives the result while seconds command throws the error.
I hope this might solve your problem.

Steps to load a .txt file and convert the strings in it to usable data in a Sage notebook

I am on a Mac with OS 10.11.6, and I'm learning the notebook interface for Sage 7.2. As a start, in a Sage worksheet I created a .txt file containing the string [1, 2, 3] and saved it. I can open the text file directly and verify its contents just by clicking on it, but I can't yet do this in Sage.
I'd like to be able to open it and convert the string to a usable Sage object. I'd appreciate explicit instructions, assuming nothing at all about my Sage background. Thank you.
Note: The procedure to do what I just asked in the Sage documentation under "Saving and Loading Individual Objects" doesn't work in my environment (specs above.) I do A = [1, 2, 3]. Then I do save(A, 'A') and Sage returns a hot link for A.sobj. Then I hit the save-and-quit button. Then I hit "sign out." Then I sign back in and go to the worksheet where I did the steps I just described. I do A = load('A'). This is what Sage says:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "_sage_input_3.py", line 10, in <module>
exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("QSA9IGxvYWQoJ0EnKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
File "", line 1, in <module>
File "/private/var/folders/7n/t9k4hfyn44s2qp7wxt479kn80000gn/T/tmpEa1OkK/___code___.py", line 2, in <module>
exec compile(u"A = load('A')" + '\n', '', 'single')
File "", line 1, in <module>
File "sage/structure/sage_object.pyx", line 1032, in sage.structure.sage_object.load (build/cythonized/sage/structure/sage_object.c:11594)
IOError: [Errno 2] No such file or directory: 'A.sobj'`
I found an answer in Finch's book. First a quote:
“We used a module called os from the Python standard library module to help us write code that can run on multiple platforms. A text file must have a special character to denote the end of each line in the file. Unfortunately, for historical reasons, each family of operating systems (Mac, Windows, and UNIX) uses a different end-of-line character. The os module has a constant called linesep that contains the correct character for the platform that the code is run on. We used the statement import os to make the module available, and accessed the constant using the syntax os.linesep. We also used the function os.path.join to join the path to the file name with the correct character for the current operating system.”
Excerpt From: Craig Finch. “Sage Beginner's Guide.”
Example using a file named "File2.txt" containing a single text character, '1':
import os path='/Users/barrybrent/.sage/sage_notebook.sagenb/home/store/2/21/212/2123/admin/19/data/' fileName='File2.txt' times = [] text_file = open(os.path.join(path, fileName), 'r') line = text_file.readline()
(Comment: is just a character string. To convert it a Sage object useful in computations:)
elements=line.split(',') times.append(float(elements[0].strip()))
(Comment: evaluate:)
times[0]
(Comment: Sage says"1.0". Now can we do arithmetic with times[0]?)
times[0]+1
Sage says "2.0"

How can I overcome this key word error?

enter code here
# -*- coding: utf-8 -*-
import math
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig1=plt.figure()
ax=plt.axes(xlim=(-10,10), ylim=(-10,10))
line,=ax.plot([],[],lw=1)
"""def init ():
line.set_data([],[])
return line,"""
dt=0.001
X=[]
Y=[]
r=float(input("Enter the radius :: "))
w=float(input("Enter angular frequency :: "))
def run(data):
t=0
while w*t<=2*math.pi:
x=r*math.cos(w*t)
y=r*math.sin(w*t)
X.append(x)
Y.append(y)
t=t+dt
line.set_data(X,Y)
return line,
line,=ax.plot(X,Y,lw=2)
FFMpegWriter = animation.writers['ffmpeg']
writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
anim=animation.FuncAnimation(fig1,run,frames=200,interval=20,blit=True)
anim.save('amim.mp4',writer=writer)
The error message shown is ::
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/tathagata/anaconda3/lib/python3.4/site- packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
execfile(filename, namespace)
File "/home/tathagata/anaconda3/lib/python3.4/site- packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile
exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)
File "/home/tathagata/Documents/Python scripts/circleamim.py", line 35, in <module>
FFMpegWriter = animation.writers['ffmpeg']
File "/home/tathagata/anaconda3/lib/python3.4/site-packages/matplotlib/animation.py", line 81, in __getitem__
return self.avail[name]
KeyError: 'ffmpeg'
I use anacoda distribution and SPYDER as my IDE. I have seen the many solutions related to key errors. But the movie wont run. How can I make the movie to run? I hope there are no other logical errors.
First install ffmpeg and add path to ffmpeg
# on windows
plt.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg\\bin\\ffmpeg.exe'
# on linux
plt.rcParams['animation.ffmpeg_path'] = u'/home/username/anaconda/envs/env_name/bin/ffmpeg'
Note for linux users: The path for ffmpeg can be found by simply using which: which ffmpeg
Also instead of
FFMpegWriter = animation.writers['ffmpeg']
writer = FFMpegWriter(fps=15, metadata=dict(artist='Me'), bitrate=1800)
I just used writer = animation.FFMpegWriter()
It seems that ffmpegis not installed on your system. Try the following code:
import matplotlib.animation as animation
print(animation.writers.list())
It will print out a list of all available MovieWriters. If ffmpegis not among it, you need to install it first from the ffmpeg homepage.
If you have Homebrew, literally just run the command
brew install ffmpeg
And Homebrew will take care of the rest (dependencies, etc). If you don't, I would recommend getting Homebrew or something like it (apt-get on Linux is built in, or an alternative on OS X would be Macports)
I have also posed with same problem(keyError: 'ffmpeg') but instead of using anakonda, I used IDLE3. So, first i checked for 'ffmpeg' in terminal it wasn't installed so installed it.
Using: sudo apt install ffmpeg
and when I run my save_animation program, it worked generating animation files in '.mpeg' format.

How do I enable through-the-filesystem diazo editing with plone 4.3

Summary: through-the-filesystem editing not working for my diazo theme. Plone breaks.
Details:
I've created my first live plone site with 4.3.2 and diazo. You can see the live version at borogreen.org. I would like to keep editing the theme forward.
My ubuntu 12.04LTS test server has only plone432 + diazo + dexterity (not used) + Static resource storage 1.0.2 enabled. For test purposes, I'm using the available sunrain theme.
I've placed the sunrain theme manually inside the /resources folder, as suggested per
http://developer.plone.org/reference_manuals/external/plone.app.theming/userguide.html#deploying-and-testing-themes
Trying to enable that theme in the Site Setup | Theming panel | Advanced, I set the path to the theme rules to
/++theme++sunrain/rules.xml
and the absolute path prefix to
/++theme++sunrain/
Plone does not recognize it: no theme gets enabled. The debug mode spits out the following error codes
2014-03-29 00:10:07 ERROR plone.subrequest Error handling subrequest to /++theme++sunrain/rules.xml
Traceback (most recent call last):
File "/home/plone/Plone/buildout-cache/eggs/plone.subrequest-1.6.7-py2.7.egg/plone/subrequest/__init__.py", line 116, in subrequest
traversed = request.traverse(path)
File "/home/plone/Plone/buildout-cache/eggs/Zope2-2.13.21-py2.7.egg/ZPublisher/BaseRequest.py", line 502, in traverse
subobject = self.traverseName(object, entry_name)
File "/home/plone/Plone/buildout-cache/eggs/Zope2-2.13.21-py2.7.egg/ZPublisher/BaseRequest.py", line 326, in traverseName
ob2 = namespaceLookup(ns, nm, ob, self)
File "/home/plone/Plone/buildout-cache/eggs/zope.traversing-3.13.2-py2.7.egg/zope/traversing/namespace.py", line 112, in namespaceLookup
return traverser.traverse(name, ())
File "/home/plone/Plone/buildout-cache/eggs/plone.resource-1.0.2-py2.7.egg/plone/resource/traversal.py", line 27, in traverse
raise NotFound
NotFound
2014-03-29 00:10:07 ERROR plone.transformchain Unexpected error whilst trying to apply transform chain
Traceback (most recent call last):
File "/home/plone/Plone/buildout-cache/eggs/plone.transformchain-1.0.3-py2.7.egg/plone/transformchain/transformer.py", line 48, in __call__
newResult = handler.transformIterable(result, encoding)
File "/home/plone/Plone/buildout-cache/eggs/plone.app.theming-1.1.1-py2.7.egg/plone/app/theming/transform.py", line 170, in transformIterable
transform = self.setupTransform(runtrace=runtrace)
File "/home/plone/Plone/buildout-cache/eggs/plone.app.theming-1.1.1-py2.7.egg/plone/app/theming/transform.py", line 108, in setupTransform
transform = compileThemeTransform(rules, absolutePrefix, readNetwork, parameterExpressions, runtrace=runtrace)
File "/home/plone/Plone/buildout-cache/eggs/plone.app.theming-1.1.1-py2.7.egg/plone/app/theming/utils.py", line 580, in compileThemeTransform
runtrace=runtrace,
File "/home/plone/Plone/buildout-cache/eggs/diazo-1.0.4-py2.7.egg/diazo/compiler.py", line 115, in compile_theme
read_network=read_network,
File "/home/plone/Plone/buildout-cache/eggs/diazo-1.0.4-py2.7.egg/diazo/rules.py", line 195, in process_rules
rules_doc = etree.parse(rules, parser=rules_parser)
File "lxml.etree.pyx", line 2957, in lxml.etree.parse (src/lxml/lxml.etree.c:56299)
File "parser.pxi", line 1526, in lxml.etree._parseDocument (src/lxml/lxml.etree.c:82331)
File "parser.pxi", line 1555, in lxml.etree._parseDocumentFromURL (src/lxml/lxml.etree.c:82624)
File "parser.pxi", line 1455, in lxml.etree._parseDocFromFile (src/lxml/lxml.etree.c:81663)
File "parser.pxi", line 1002, in lxml.etree._BaseParser._parseDocFromFile (src/lxml/lxml.etree.c:78623)
File "parser.pxi", line 569, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:74567)
File "parser.pxi", line 650, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:75458)
File "parser.pxi", line 588, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:74760)
IOError: Error reading file '/++theme++sunrain/rules.xml': failed to load external entity "/++theme++sunrain/rules.xml"
What's wrong here?
ps: of course I can upload the theme as zip file and enable it that way, which works fine. I would really like to edit through-the-filesystem as I can foresee a lot of development in the future.
An up-to-date and working write-up for plone432 how to edit diazo themes through the filesystem using the /resources directory would be the answer, but I have not found that either outside of the plone.app.theming user guide. Help!

Resources