Psycopg2.errors.UndefinedColumn: column "{random thing}" does not exist -- using PostgreSQL - python

I'm very new to the whole programming thing but and ive ran into a pretty annoying problem.
This is a small slice of some code I had to do for school and I've ran into a problem where it keeps telling me the variables I'm trying to put into the {} brackets are not columns, when they shouldn't even be seen as columns in the first place. They are variables linked to some buttons and input things via Tkinter, I just want them to be placed into the columns.
cursor.execute("INSERT INTO reviews(name,messae,date,time,location,messaeg_id) VALUES ({},{},{},{},{},{})".format(naam, review, datumm, tijdd, locatie, review_id))
I've tried quite a few things like putting '' and ""s basically everywhere you could think of (I think), tried it with only one column and value, which just gave me a different error:
(psycopg2.errors.SyntaxError: syntax error at or near "VALUE")
If I kept it as VALUES instead of VALUE it gave me the same error as the main one this question is about.
Also "tried" some solutions I found on this site but since I'm still very new, I found it hard to adapt those solutions over to my code, hence why I've resorted to asking a question myself for once lol.
BTW it's pretty late here at the time of writing this and English is not my first language so please excuse any grammatical errors and what not lol

Related

How to set cell formula using odfpy

I keep getting Err:508 or #Name? (Err:525) when opening a spreadsheet created with odfpy and putting a formula in a cell with the following code:
tc = TableCell( valuetype="string", formula=calc, value=0 )
In the spreadsheet, the formula looks fine, and any edit to it together with reverting the edit, so there is no net change to the formula, makes it think it has changed, re-evaluates it, and it works fine. But not until it is tweaked. What am I missing?
Here's the formula I'm using, in case that is relevant:
=TEXT(NOW()+0*LEN(CONCAT(A2:F999));"YYYY.MM.DD.HH.MM.SS")
(purpose, to timestamp the most recent edit to a range of cells). I note that at the time the formula is inserted in row 1, that other rows haven't been inserted yet, but a few are in subsequent steps. But I wouldn't think any attempt to evaluate the range would occur until loaded into LibreOffice, so that doesn't seem a likely cause of the error.
I already am using ; not , in the function parameters, which seems to be the most successful answer for other people that encounter this error, and I'm using the English install, which is the other seeming issue some have with copy/pasting formulas. But still no joy, and not much that is relevant shows up in searching.
Well, this is weird, but probably documented somewhere.
The most helpful thing that I was able to find by searching was "Solution 4" at this link. While that comment was not easy to find, because it was very generic, and didn't turn up in searches for "formula", it did provide a debug technique which enabled me to compare the formula that was getting inserted initially, as in my question, with what was there after the editing "tweak". "CONCAT" has to be prefixed with "COM.MICROSOFT.". Not at all obvious to the first-time formula insertion attempt.

Asking for help breaking down a piece of code --- Head First Python 2nd Edition (11/9/2022) pg 102

https://prnt.sc/B4pFd_w5reM0
<<< photo of page
https://prnt.sc/bfCN6MN3P9DM
<<< screenshot of code
favorite_languages = {
'jen': 'python',
'sarah': 'c',
'edward': 'ruby',
'phil': 'python',
}
friends = ['phil', 'sarah']
for name in favorite_languages.keys():
print(f"Hi {name.title()}.")
if name in friends:
language = favorite_languages[name].title()
print(f"\t{name.title()}, I see you love {language}!")
I am not sure why [name] is in brackets instead of language. We are interested in the language they prioritize and not who the person is (for the value 'language' in specific at least). So I am wondering why the brackets have 'name' inside them, and not 'language'.
Also.... could someone breakdown what is happening in this code? I think I am just lost in general.
First we have a dictionary called favorite_languages
first column are keys, second column are values
going down, we have a list called friends, I think the words inside are called values.
for name in favorite_languages.keys():
this line of code says tells the editor? right? that the keys (first column), each of the keys will be categorized as a 'name'. correct? the keys in the dictionary (named favorite_languages).
print(f"Hi {name.title()}.")
line of code says we will print a message. Hi (with a name pulled from the dictonary with the first letter capitalized by the (.title() command) Just not sure how to describe the f... all I know is that it is needed... and that it exists.
if name in friends:
as the names from the dictonary get pulled, this line of code tells the editor to check if the name pulled is identical to one of the values in the list 'friends'.
language = favorite_lanugages[name].title()
this is where I am stuck. the name pulled from the dictionary, is the same as the value in the list.... so we tell the editor to put the name back into the dictonary... we find the value that is next to it? and then that value gets its first letter capitalized and becomes known as the value 'language'? or is it a variable? and not a value...
print(f"\t{name.title()}, I see you love {language}!")
coming back full circle. We are going to print on a new line. The message starts with the name we pulled from the dictionary that is also identical to a value found in the list. The name has it's first letter capitalized. Text is added ', I see you love '. Then we add the value we created earlier called 'lanugage' and add an exclaimation point after it. Close the quotation marks and close the parenthesis.
What did I miss? Is my thought process right or wrong? Am I on the right track?
You are asking several questions at once, so I will answer them one at a time.
I will preface my answer by saying this:
Python is a programming language. A programming language consists of instructions that are provided to a computer program. The computer program simply reads one instruction at a time, and performs some work according to the contents of the instruction. Therefore, programming languages have strict rules about every operation that they support. The programming language provides a set of operations that it supports, and we build programs by combining those operations.
Moreover, programs need to be converted from text into a version of those instructions that the computer understands. Therefore the text must also follow a specific and clearly-defined syntax. The meaning of something like [] is and must be unambiguous, otherwise it would be impossible for the computer to interpret our programs.
The important conclusion here is that computer programs must be expressed in a specific way, and that every piece of text has a specific purpose and meaning. Python does not and cannot ever understand what your intentions are. Programming consists of taking your ideas and translating them into the specific operations and syntax provided by the programming language.
I write this because you seem to be taking an interpretive approach to reading code, and you seem to be lacking the fundamental knowledge required to understand it properly. You seem to be trying to guess at the meaning of code by broadly matching it up to how it looks in English. As nice as it is that Python looks like plain English text, it is not plain English text. It is a programming language like any other, and its rules are as strict as any other. If you don't know what something means, resist the temptation to guess at what it means.
I strongly suggest finding a structured learning resource, such as an online course or a book. It will guide you through all of the concepts, syntax elements, and data types that you will need to learn programming. You must learn what these things are. You will never be able to read or write code by interpreting and guessing based on visual patterns.
I am not sure why [name] is in brackets instead of language. We are interested in the language they prioritize and not who the person is (for the value 'language' in specific at least). So I am wondering why the brackets have 'name' inside them, and not 'language'.
It is in brackets because the names are the keys of the dictionary. The favorite_languages is a lookup table from names to languages. It is not a lookup table from languages to names.
In general, a dictionary is a one-way lookup table. The things on the left of : are the "keys", and the things on the right are the "values". The keys must be unique, but the values can be non-unique. The [] syntax performs lookups from keys to values only.
What you are "interested in" is relevant only insofar as it is the goal of your program. You do not get to choose what [] means, based on what you are interested in. Its meaning is built into Python and cannot be changed without modifying Python itself. You must use the tools that you are given, and cannot use tools that do not exist.
first we have a dictionary called favorite_languages first column are keys, second column are values
It's not helpful to think of a dictionary as "columns", like a spreadsheet. Think of it as a lookup table. That's why it's called a dictionary. The keys of the dictionary are like words in a traditional dictionary, and the values are like definitions.
It is important to have a clear understanding of what dictionaries do. Otherwise you won't understand when or how to use them, and you won't understand code that uses them. That might be what is happening here.
going down, we have a list called friends, I think the words inside are called values.
This is a somewhat different usage of the term "value".
Outside the context of dictionaries, "value" does not have a strict technical meaning. The word is usually used to distinguish values, which are tangible things, from variables, which are placeholders for things and do not mean anything on their own.
So when we talk about the values in a list, we are just talking about the things inside the list. People also use the word elements to mean the same thing.
editor? right?
I assume you are talking about an "editor" as in a "text editor" or "code editor". No. That's just a program that you use to edit text. It does not run your code. Some code editors like VS Code have built-in features that help you write code. But your code never "tells" your text editor anything. The code exists to be interpreted by Python. Anything else is just an attempt to help you write the code.
for name in favorite_languages.keys(): this line of code says tells the editor? right? that the keys (first column), each of the keys will be categorized as a 'name'. correct? the keys in the dictionary (named favorite_languages).
No. This code does not categorize anything.
The .keys() method accesses the keys of a dictionary (as explained above). This loops over favorite_languages.keys() and assigns the looped values to name.
The syntax for x in things: loops over the elements of things. It assigns the first element to x, then runs the code in the block, then assigns the second element to x, then runs the code in the block, etc. Loops are an essential tool in programming, and it is very important that you spend time on understanding how they work and how to use them.
print(f"Hi {name.title()}.")
Your understanding seems correct.
name is the key from favorite_languages.keys(), because of for name in favorite_languages.keys():. We also happen to know that this corresponds to people's names.
if name in friends:
Correct. name in friends is an expression that returns True if and only if the value of name is equal to one of the elements in friends.
The if then executes the code inside the block if and only if the result is True (or is equivalent to True in a specific sense that you don't need to worry about right now).
language = favorite_languages[name].title()
This code does not put anything back into the dictionary. This code does not operate by finding values "next to" anything.
favorite_languages is a dictionary, i.e. a lookup table. This code uses the value of name to look up a language in favorite_languages.
Finally, it assigns the result to the language variable. From the perspective of Python, this variable is completely unrelated to the favorite_languages. It is a completely separate placeholder.
The fact that they both say "languages" is relevant to you, the reader and programmer, but not relevant to Python.
print(f"\t{name.title()}, I see you love {language}!")
\t is a tab characdter, not a new line.
Otherwise yes, your interpretation is correct.

C-Grammar in Antlr4 raises error `extraneous input 'int'` on `int main()`

(For a better overview I will link my code, so that there is not too much space used unnecessarily)
Summary:
I am currently facing a weird issue when using the Grammar (partly modified file I made) made for C in Antlr4. It raises errors when encountering type/function definitions, such as int main() or int x. I am confused why that is though (partly due to my lack of experience or knowledge), since the rules seem to not contain an issue.
Still, when running the Python-generated code, it logs an error saying:
extraneous input 'int' expecting {'__extension__', '__builtin_va_arg', '__builtin_offsetof', '_Generic', '(', Identifier, Constant, StringLiteral}
Debugging the code I found that the entire declaration is classified as a primaryExpression, even though it should be an assignmentExpression. So it seems there might be an issue inside the grammar file causing it to identify it incorrectly, or my implementation (my file utilising the generated code) contains a weird bug causing this to happen.
If anyone has a clue what it might be or what I could try to fix the issue, I would greatly appreciate that ^^
Edit: Additional Info
Here the base version: link. The changes in my version are minimal and I only added a new type and specifier, meaning it should not interfere with the lexing and correctly identifying it. (Changes can be viewed here: link)
I found my issue and it was based on my misunderstanding from how antlr4 dealt with this and how the grammar file was structured. (I realised that after reading #sepp2k comment).
I called the wrong rule, which in this case was primaryExpression. This rule was not the actual entry rule, meaning that calling it causes Antlr4 to only call one of the rules needed and therefore parse the entire string wrongly and not recognise anything. I fixed the issue and found that compilationUnit was the actual entry, meaning that now everything is parsed fine.
(Also I realised that my question was rather loosely formed and didn't contain enough information, so sorry about that, but luckily I found the issue rather quickly after realising what was actually going on)

Elasticsearch is missing data when querying after insert

I have a test that does the following:
create an index on ES using pyes
insert mapping
insert data
query the data to check if it fits the expected result
However when I run this test, sometimes it would find no result, and sometimes it would find something but some data is missing. The interesting thing is this only happens when I run the test automatically. If I type in the code ling-by-line, everything works as expected. I've tested it 3 times manually and it works without failure.
Sometimes I've even received this message:
NoServerAvailable: list index out of range
It seems like the index was not created at all
I've pinged my ES address and everything looks correct. There was no other error msg found anywhere.
I thought it would be because I was trying to get the data too fast after the insert, as documented here: Elastic Search No server available, list index out of range
(check first comment in the accepted answer)
However, even if I make it delay for 4 seconds or so, this problem still happens. What concerns me is the fact that sometimes only SOME data is missing, which is really strange. I thought it would be either finding it all or missing it all.
Anyone got similar experience that can shine some light on this issue? Thanks.
Elasticsearch is Near Realtime(NRT). It might take upto 1 second to make your recently indexed document be visible/available for search.
To make your recently indexed document available instantly for searching, you can append ?refresh=wait_for at the end of the index document command. For e.g.,
POST index/type?refresh=wait_for
{
"field1":"test",
"field2":2,
"field3":"testing"
}
?refresh=wait_for will forcibly refresh your index to make the recently indexed document available for search. Refer ?refresh.

How to delete almost duplicate files

Edit 2:
Solved, see my answer waaaaaaay below.
Edit:
After banging my head a few times, I almost did it.
Here's my (not cleaned up, you can tell I was troubleshooting a bunch of stuff) code:
http://pastebin.com/ve4Qkj2K
And here's the problem: It works sometimes and other times not so much. For example, it will work perfectly with some files, then leave one of the longest codes instead of the shortest one, and for others it will delete maybe 2 out of 5 duplicates, leaving 3 behind. If it just performed reliably, I might be able to fix it, but I don't understand the seemingly random behavior. Any ideas?
Original Post:
Just so you know, I'm just beginning with python, and I'm using python 3.3
So here's my problem:
Let's say I have a folder with about 5,000 files in it. Some of these files have very similar names, but different contents and possible different extensions. After a readable name, there is a code, always with a "(" or a "[" (no quotes) before it. The name and code are separated by a space. For example:
something (TZA).blah
something [TZZ].another
hello (YTYRRFEW).extension
something (YJTR).another_ext
I'm trying to only get one of the something's.something, and delete the others. Another fact which may be important is that there are usually more than one code, such as "something (THTG) (FTGRR) [GTGEES!#!].yet_another_random_extension", all separated by spaces. Although it doesn't matter 100%, it would be best to save the one with the least codes.
I made some (very very short) code to get a list of all files:
import glob
files=[]
files=glob.glob("*")
but after this I'm pretty much lost. Any help would be appreciated, even if it's just pointing me in the right direction!
I would suggest creating separate array of bare file names and check the condition if any element exists in any other place by taking array with all indices excluding the current checked in loop iteration.
The
if str_fragment in name
condition simply finds any string fragment in any string-type name. It can be useful as well.
I got it! The version I ended up with works (99%) perfectly. Although it needs to make multiply passes, reading,analyzing, and deleting over 2 thousand files took about 2 seconds on my pitiful, slow notebook. My final version is here:
http://pastebin.com/i7SE1mh6
The only tiny bug is that if the final item in the list has a duplicate, it will leave it there (and no more than 2). That's very simple to manually correct so I didn't bother to fix it (ain't nobody got time fo that and all).
Hope sometime in the future this could actually help somebody other than me.
I didn't get too many answers here, but it WAS a pretty unusual problem, so thanks anyway. See ya.

Categories