Jedi fails on autocomplete in sublime text - sublime-text-plugin

Jedi fails on autocomplete in sublime text for some cases:
import os
def x(y):
print(y)
print(x)
f = open('1.txt', mode='r', encoding='utf-8'). # fail on dot
from datetime import d # fail on module import
I've got this error in both cases:
...
File "/Users/rmerkushin/Library/Application Support/Sublime Text 3/Packages/sublime-jedi/jedi/parser/__init__.py", line 267, in parse
root_node = super(ParserWithRecovery, self).parse(self._tokenize(tokenizer))
File "/Users/rmerkushin/Library/Application Support/Sublime Text 3/Packages/sublime-jedi/jedi/parser/__init__.py", line 146, in parse
self.remove_last_newline()
File "/Users/rmerkushin/Library/Application Support/Sublime Text 3/Packages/sublime-jedi/jedi/parser/__init__.py", line 227, in remove_last_newline
assert newline.value.endswith('\n')
AssertionError
How to fix this issue?
P.S.: jedi version - 0.10.0, Sublime Text - 3126 (Python 3.3.6)

Related

IOError: [Errno 22] invalid mode ('rb') or filename

I am a beginner at python and trying to use a very simple shutil module (shutil.copy) to copy databases from multiple folders into a backup folder. I am getting the error below. Any help is appreciated.
# importing os module
import os
#import time module
import time
import datetime
# importing shutil module
import shutil
now = datetime.datetime.now()
timestamp = str(now.strftime("%Y%m%d_%H%M%S"))
source5 = "F:/SHARED/SOP/PRE GO LIVE/TEST CASES & SCENARIOS/MASTER/PRE_GO_LIVE_MASTER.accdb"
dest5 = "F:/SHARED/SOP/SB/Python/Destination/PRE_GO_LIVE_MASTER.accdb_"+timestamp+".accdb"
print("Before copying ")
DB5 = shutil.copy(source5,dest5)
print("After DATABASE has been copied")
Error:
Traceback (most recent call last):
File "C:\Users\sbasava1\Desktop\Python\Final_Attempt.py", line 101, in <module>
DB5 = shutil.copy(source5,dest5)
File "C:\Python27\lib\shutil.py", line 119, in copy
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 22] invalid mode ('rb') or filename:
Check your file path, use double backslashes \\ or a single forward slash / or make your string a raw string r"...".
# Original path - wouldn't work
path = "c:\location\directory"
# Raw string - would work
path = r"c:\location\directory"
# Double slashes - would work
path = "c:\\location\\directory"
# Forward slashes - would work
path = "c:/location/directory"
Consider learning about string literals
If this didn't help leave me a comment, it would also help to see the/part of the code you are working with!
Edit: Running your script didn't give me an issue:
Check if the directory you are trying to create a file in actually exists

Python 2.7 import unicode_literals from __future__ gives UnicodeDecodeError while reading the file with umauts

I have a Python script which read and write a file with german umlauts (äöü) in an input file "myfile.in". I used Python version 2.7. Here a reduced version of my script:
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
if __name__=='__main__':
with open("myfile.in", "r") as f:
lines = f.readlines()
txt = ""
for line in lines:
txt = txt + line
with open("myfile.out", "w") as f:
f.write(txt)
This works fine.
Now I got the requirement from my customer to used the Future statement definitions and I added the following line to my Python script:
from __future__ import unicode_literals
Now I get the following error message:
Traceback (most recent call last):
File "myscript.py", line 9, in <module>
txt = txt + line
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 23: ordinal not in range(128)
How can I resolve this problem.
Thanks for your hints Thomas

Pytube RegexMatchError

I am trying to write script in python where a user is prompted to enter a youtube link on the command line. This link should then be downloaded
from pytube import YouTube
downloadFile = raw_input("Enter your Youtube link: ")
YouTube(downloadFile).streams.first().download()
However when the link is entered on the command line I get the following:
File "dl.py", line 10, in <module>
YouTube(downloadFile).streams.first().download()
File "build/bdist.linux-x86_64/egg/pytube/__main__.py", line 69, in __init__
File "build/bdist.linux-x86_64/egg/pytube/extract.py", line 43, in video_id
File "build/bdist.linux-x86_64/egg/pytube/helpers.py", line 39, in regex_search
pytube.exceptions.RegexMatchError: regex pattern ((?:v=|\/)([0-9A-Za-z_-]{11}).*) had zero matches
I am able to get get it working via python interpeter.
Any suggestions are welcome!
Ok, after a bit of rooting around this seems to do the trick
import sys # import sys
import pytube
link = raw_input('Please enter a url link\n')
yt = pytube.YouTube(link)
stream = yt.streams.first()
finished = stream.download()
print 'Download is complete'
sys.exit()

NLTK python tokenizing a CSV file

I have began to experiment with Python and NLTK.
I am experiencing a lengthy error message which I cannot find a solution to and would appreciate any insights you may have.
import nltk,csv,numpy
from nltk import sent_tokenize, word_tokenize, pos_tag
reader = csv.reader(open('Medium_Edited.csv', 'rU'), delimiter= ",",quotechar='|')
tokenData = nltk.word_tokenize(reader)
I'm running Python 2.7 and the latest nltk package on OSX Yosemite.
These are also two lines of code I attempted with no difference in results:
with open("Medium_Edited.csv", "rU") as csvfile:
tokenData = nltk.word_tokenize(reader)
These are the error messages I see:
Traceback (most recent call last):
File "nltk_text.py", line 11, in <module>
tokenData = nltk.word_tokenize(reader)
File "/Library/Python/2.7/site-packages/nltk/tokenize/__init__.py", line 101, in word_tokenize
return [token for sent in sent_tokenize(text, language)
File "/Library/Python/2.7/site-packages/nltk/tokenize/__init__.py", line 86, in sent_tokenize
return tokenizer.tokenize(text)
File "/Library/Python/2.7/site-packages/nltk/tokenize/punkt.py", line 1226, in tokenize
return list(self.sentences_from_text(text, realign_boundaries))
File "/Library/Python/2.7/site-packages/nltk/tokenize/punkt.py", line 1274, in sentences_from_text
return [text[s:e] for s, e in self.span_tokenize(text, realign_boundaries)]
File "/Library/Python/2.7/site-packages/nltk/tokenize/punkt.py", line 1265, in span_tokenize
return [(sl.start, sl.stop) for sl in slices]
File "/Library/Python/2.7/site-packages/nltk/tokenize/punkt.py", line 1304, in _realign_boundaries
for sl1, sl2 in _pair_iter(slices):
File "/Library/Python/2.7/site-packages/nltk/tokenize/punkt.py", line 310, in _pair_iter
prev = next(it)
File "/Library/Python/2.7/site-packages/nltk/tokenize/punkt.py", line 1278, in _slices_from_text
for match in self._lang_vars.period_context_re().finditer(text):
TypeError: expected string or buffer
Thanks in advance
As you can read in the Python csv documentation, csv.reader "returns a reader object which will iterate over lines in the given csvfile". In other words, if you want to tokenize the text in your csv file, you will have to go through the lines and the fields in those lines:
for line in reader:
for field in line:
tokens = word_tokenize(field)
Also, when you import word_tokenize at the beginning of your script, you should call it as word_tokenize, and not as nltk.word_tokenize. This also means you can drop the import nltk statement.
It is giving error - expected string or buffer because you have forgotten to add str as
tokenData = nltk.word_tokenize(str(reader))

Click on any one of "1 2 3 4 5 ..." on a page by using Selenium in Python (e.g., Splinter):

I have HTML that looks like the three following sample statements:
...
12
13
(I'd presently be on pg. 11.)
I don't know the Py/Selenium/Splinter syntax for selecting one of the page numbers in a list and clicking on it to go to that page. (Also, I need to be able to identify the element in the argument as, for example, 'Page$10' or 'Page$12', as seen in the __doPostBack notation. Maybe just a 'next page', in so many words, would be fine, but I don't even know how to do that.)
Thank you for any help.
UPDATE II: Here's the code I have to work from:
import time
import win32ui
import win32api
import win32con
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from ctypes import *
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://[site]');
UPDATE III:
Traceback (most recent call last):
File "montpa_05.py", line 47, in <module>
continue_link = driver.find_element_by_link_text('4')
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 246, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 680, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 165, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 164, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'no such element\n
(Session info: chrome=28.0.1500.95)\n (Driver info: chromedriver=2.2,platform=
Windows NT 6.1 SP1 x86_64)'
The <a> element is defined as a link. That means that you can select it by link text.
I don't know Python, but the java syntax would be By.linkText(##) where ## is the number you want to click on.