Python Library (foundation\FormParameters.py) error - python

The first line i enter in IDLE is
from foundation import FormParameters
And i get a error like this
>>> from foundation import FormParameters
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from foundation import FormParameters
File "C:\Python32\lib\foundation\FormParameters.py", line 19
con_str = StringIO()
^
TabError: inconsistent use of tabs and spaces in indentation
As it is a standard library can i change it? or is there any other way round??

As it is a standard library can i change it?
You can just open the referenced File ("C:\Python32\lib\foundation\FormParameters.py") and edit it - in this case I would suggest replacing tabs with spaces, as tabs and python are an evil combination.
As some others commented, this is NOT a part of the standard library - but even if it were, the same rules would apply - find the file and you're free to edit it (you probably shouldn't though if you intend to run your programs on other machines with an unmodified standard library). For C extensions this pörocess is somewhat more complicated as you would need to compile them.

Related

why is the error thinking Its not string?

path of file is:
"C:\Users\deana\OneDrive\Marlon's files\Programming\Python\PITT\PITT_LIbrary\Lists\test.txt"
lines of code are:
import os
os.chdir("C:/Users/deana/OneDrive/Marlon's files/Programming/Python/PITT/PITT_LIbrary/Lists")
exec(open('test.txt'))
the error is this:
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
exec(open('test.txt'))
TypeError: exec() arg 1 must be a string, bytes or code object
also if I try on one line as such:
exec(open(r"C:/Users/deana/OneDrive/Marlon's files/Programming/Python/PITT/PITT_LIbrary/Lists/test.txt"))
i'ts the same error. (with and without r)
super frustrationg as it reads like i'm not inputting string... but it is string!?!
also I've done this litteraly the same way before, restarted IDLE shell, no difference.
ugh! I always get stupid errors with file paths.
I should have been using os.startfile() to open this.
It was confusing by using .open(). as I was attempting to open in default app.
before, i've used exec.open() to open .py files and guess I got them confused.
exec is just used to open other scripts... need stronger coffee next time.
Try this:
import os
os.chdir("C:/Users/deana/OneDrive/Marlon's files/Programming/Python/PITT/PITT_LIbrary/Lists")
exec(open('test.txt', 'rb'))
You can convert the txt file to bytes by opening it with rb (read bytes).

Using Python, NLTK, to analyse German text

I am a beginner in Python and currently trying to use NLTK to analyze German text (extract the German noun and it's frequency of German text) by following this tutorial: https://datascience.blog.wzb.eu/2016/07/13/accurate-part-of-speech-tagging-of-german-texts-with-nltk/
There are several issues that I faced during the process and I am not able to solve them.
When I follow the website to execute the code below:
import random
tagged_sents = list(corp.tagged_sents())
random.shuffle(tagged_sents)
split_perc = 0.1
split_size = int(len(tagged_sents) * split_perc)
train_sents, test_sents = tagged_sents[split_size:], tagged_sents[:split_size]
and it comes out with this
Traceback (most recent call last):
File "test2.py", line 7, in <module>
tagged_sents = list(corp.tagged_sents())
File "C:\Users\User\anaconda3\lib\site-packages\nltk\corpus\reader\conll.py", line 130, in tagged_sents
return LazyMap(get_tagged_words, self._grids(fileids))
File "C:\Users\User\anaconda3\lib\site-packages\nltk\corpus\reader\conll.py", line 215, in _grids
return concat(
File "C:\Users\User\anaconda3\lib\site-packages\nltk\corpus\reader\util.py", line 433, in concat
raise ValueError("concat() expects at least one object!")
ValueError: concat() expects at least one object!
Then I try to fix by following this solution https://teamtreehouse.com/community/randomshuffle-crashes-when-passed-a-range-somenums-randomshufflerange5250
and alter the
tagged_sents = list(corp.tagged_sents())
to
tagged_sents = list(range(5,250))
And the ValueError didn't come out, I don't know what (5,250) means, although I have read the explanation.
Then I continue to execute the follow step
from ClassifierBasedGermanTagger.ClassifierBasedGermanTagger import ClassifierBasedGermanTagger
tagger = ClassifierBasedGermanTagger(train=train_sents)
And it shows
Traceback (most recent call last):
File "test1.py", line 90, in <module>
from ClassifierBasedGermanTagger.ClassifierBasedGermanTagger import ClassifierBasedGermanTagger
ModuleNotFoundError: No module named 'ClassifierBasedGermanTagger'
I have already downloaded the ClassifierBasedGermanTagger.py and init.py and put them in the folder which link to the VS CODE, don't know if it is correct as the passage said:
'Using his Python class ClassifierBasedGermanTagger (which you can download from the github page) we can create a tagger and train it with the data from the TIGER corpus:'
Please help me to fix these problems, thanks!
First of all, welcome to StackOverflow! Before posting a question, please make sure that you have done your own research and most of the time it solves the problem.
Secondly, range(start, end) is a very basic function in Python to get list of numbers based on the input and I don't think using it like the way you did is going to solve the problem. I would suggest you to use print to see what kind of data is being populated in corp and start debugging from there. Maybe corp is just empty and that's why you don't get any tagged_sents.
For the the import part, it is not clear to me where did you put the ClassifierBasedGermanTagger.py but wherever it is, it is not visible to your code. You can try to put your code (test2.py) and ClassifierBasedGermanTagger.py in the same directory. Read the link below for more details on how to properly import module in Python.
https://docs.python.org/3/reference/import.html

python console to call function

I know this is an easy fix, but could someone tell me how to call a python file in the python Console who have this symbol: -.
Here is my mistake:
>>> import main #no error here
>>> import a1-devoir1
File "<input>", line 1
import a1-devoir1
Syntax Error: invalid syntax
You must name your files so that they only contains letters, underscores, or numbers (but not as the first character). All libraries and modules must follow this.
So rename your .py file to a1_devoir and then try import a1_devoir

How to get past the AttributeError in Gnu Radio version 3.8

Here is the error I am recieving:
Traceback (most recent call last):
File "/home/awilhelmy5/Downloads/qpsk-adaptive-master/gnuradio/qpsk_usrp.py", line 354, in <module>
main()
File "/home/awilhelmy5/Downloads/qpsk-adaptive-master/gnuradio/qpsk_usrp.py", line 332, in main
tb = top_block_cls()
File "/home/awilhelmy5/Downloads/qpsk-adaptive-master/gnuradio/qpsk_usrp.py", line 148, in __init__
self.mapper_preamble_sync_demapper_hard_0 = mapper.preamble_sync_dehard(0, per_bits, mapper.QPSK, [0,1,3,2], 0, 3, False)
AttributeError: module 'mapper' has no attribute 'preamble_sync_dehard'
The very last line of the error is the part that is bugging me. I have tried a multitude of things, such as installing swig version 4.0, tried doing the sudo ldconfig command, tried reversing the order of the commands in the .h file, I changed the yml file name to match the name it had in the xml file and I even started messing around with the target_link
_libraries command. Any help would be greatly appreciated!
Is it possible that your code has an error (e.g. accidental deletion of some characters)? I ask because I was not able to find the text "preamble_sync_dehard" anywhere on the web, but there is a class called gr::mapper::preamble_sync_demapper_hard. Perhaps some person or program did an unwise find-and-replace that intended to remove a prefix mapper_ but ended up removing a substring instead. You can check whether this is the problem: run Python and enter
import mapper
dir(mapper)
This will list all the names in the Python module, one of which (if this is the only problem) will be preamble_sync_demapper_hard, or some related spelling which you can then adjust your qpsk_usrp.py to use.
But if it is not there at all, then you may have a bigger problem. From what I have heard, OOT modules generally have to have their structure updated in order to work with GNU Radio 3.8, and the repository I found doesn't seem to have been touched since 2018 (before 3.8 was released in 2019).

eval lines from a file

I am creating a program that stores options in a txt file, and I tried a few different lines but so far I think an eval is the best one.
I got this info in the file:
colordefondo = "#abcdef"
and I got this eval in the program:
for line in open("filename.txt", "r"):
eval(line)
However, when I try the code it gives me this error:
Traceback (most recent call last):
File "D:\project-sae\Sae.py", line 25, in <module>
eval(line)
File "<string>", line 1
colordefondo = "#abcdef"
^
SyntaxError: invalid syntax
My question is Why? And if anyone knows a better way to load and store the value of several options from a txt file it would be great. Nevertheless my question is about why that eval is failing, my knowledge about how eval works seems wrong and I don't know why, used it several times before, just never from a file.
As the documentation says, 'eval' evaluates an expression. Not a statement. Assignments are statements.
You could use the exec statement. But if you want to exec all of the lines of a file, that's exactly what execfile does. (I'm assuming Python 2.x here; things are a little different in 3.x, but the idea is similar.)
However, this is a bad idea to do in the first place. A better way to load and store the value of several options from a txt file is to use anything other than Python source as your format. JSON, .ini files, .rc files, etc. Python comes with code built-in to handle a variety of such formats. See File Formats and Internet Data Handling for a list of the relevant modules, read through them, and pick the one you want.

Categories