Code Inspection Warning with pandas.read_csv read from string buffer - pandas

My Python environment uses Pandas 1.4.2. I have the following code that reads from a string buffer:
response: requests.Response = session.get(url="...")
data: pandas.DataFrame = pandas.read_csv(io.StringIO(response.content.decode("utf-8")), skiprows=2)
When I run Code Inspection in PyCharm, I get the following warning:
Expected type 'str | PathLike[str] | ReadCsvBuffer[bytes] | ReadCsvBuffer[str]', got 'StringIO' instead
What change should I make to my code to resolve the issue short of suppressing the warning?

I would ignore this warning.
StringIO is meant to be accepted as a valid data type for the read_csv method ("By file-like object, we refer to objects with a read() method, such as a file handle (e.g. via builtin open function) or StringIO." - https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html) The issue is with Pandas' own type validation (https://github.com/pandas-dev/pandas/blob/v1.4.2/pandas/io/parsers/readers.py#L584-L680) or PyCharm's handling of it.

Related

Can't compile Kotlin Jackson Extensions readValue(File)

I am trying to use Kotlin Jackson extensions to do JSON conversions in my code. But for some reason, I am getting a syntax error when trying to use the readValue(File) function.
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3'
---
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
...
private val objectMapper = jacksonObjectMapper()
...
val factionList: List<Faction> = objectMapper.readValue<List<Faction>>(
File(javaClass.classLoader.getResource("data/factions.json").file))
The error I get from the compiler is:
None of the following functions can be called with the arguments supplied.
readValue(JsonParser!, ResolvedType!)
...
[it lists all the valid function signatures ...]
However, none of the extension functions seem to be showing up in that list. If I click on the function and hit Cmd-B in IntelliJ, I am seeing the readValue(File) method in the extensions code.
I am confused why the function is not being found by the compiler.
You're most likely missing the following import:
import com.fasterxml.jackson.module.kotlin.readValue
Forgot this a few times myself.

How to fix wrong pylance warning for pandas code in my vscode project?

I'm trying to keep my Python project in VSCode free of PyLance warnings. Unfortunately it looks like there's no complete typing information for Pandas objects. Here is a simple example:
This simple working code for creating a pandas Timestamp object:
ts = pd.Timestamp("2019-03-31 00:00:00")
will give me the warning:
Arguments missing for parameters "month", "day"
It looks like that the typing information for the Timestamp object does not know that it supports a string as parameter.
I know that I can turn it off with the # type:ignore comment:
ts = pd.Timestamp("2019-03-31 00:00:00")), # type: ignore
I want to educate my interns to solve all typing warnings, so this is bad practice.
How can I configure these extra typing information and commit it to my project? Can I add my own type stubs to my project?

Trying to deserialize and serialize a table in lua using the serpent library

So I am trying to do a simple serialization of a lua table, and deserialize it back into a table. But for some reason it just fails.
local a = {}
a[0] = {name="presetA"}
local line = serpent.line(a)
local presets, err = loadstring(line)
if (err) then
log("Error")
log(err)
else
log("Success")
log(serpent.block(presets))
end
After running, log(err) shows
[string "{[0] = {name = "presetA"}}"]:1: unexpected symbol near '{'
loadstring loads a Lua chunk from the given string and runs it.
As your serialized table is not a valid Lua expression the interpreter reports the observed error.
Let's serialze an example:
serpent.line({key = "value"})
returns
"{key = "value"} --[[table: 0D80CF40]]"
A table constructor on it's own is not a valid Lua expression.
Try to run that line and you'll Lua will report:
input:1: unexpected symbol near '{'
The output of serpent.line cannot be used as input to loadstring.
Now see the difference if you use serpent.dump instead
"do local _={name="hallo"};return _;end"
This is a valid, executable Lua chunk that will return the serialized table.
Please note the following section from the serpent documentation:
Note that line and block functions return pretty-printed data
structures and if you want to deserialize them, you need to add return
before running them through loadstring. For example:
loadstring('return '..require('mobdebug').line("foo"))() == "foo".
While you can use loadstring or load functions to load serialized
fragments, Serpent also provides load function that adds safety checks
and reports an error if there is any executable code in the fragment...
Please read manuals.

Python ast.literal_eval on dictionary string not working (SyntaxError: invalid syntax)

I am trying to process a dataset with JSON data. However, the data have been written on a file without being parsed. That means that a python dictionary is written in the file as a string instead of a JSON object as a string.
I've found a module (AST) that will do the job to convert the string to a dictionary again using the ast.literal_eval function.
However, I am getting a very strange error in some of the instances:
The code reads from a text file and apply the following to each line:
ast.literal_eval(line.rstrip())
It seems some of the characters are not ok with the AST module.
Need to recall as well that this is not happening with all the dataset, just with some instances.
Any ideas?
Many thanks in advance.
Try exploring the json package. It is cleaner and more standard way of converting strings to dictionary
json.loads(inputStr) // Converts string -> dict
json.dumps(inputJson) // Converts dict -> string
Hope this helps. Cheers!

JPype: how to convert the boolean value from python to java

I want to run java class in my python code and I use the tool JPype.
I have a java method with a boolean argument.
It works in java code but when I call it in python, I get the error message:
RuntimeError: No matching overloads found. at src/native/common/jp_method.cpp:121
I even use the jpype wrapper JBoolean, but it still fails.
For example, the code in java is:
item.myMethod(true);
and I have tried to convert it in python as:
item.myMethod(1)
item.myMethod(True)
item.myMethod(jpype.JBoolean(True))
item.myMethod(jpype.JBoolean(1))
but all of above get the same error message.
Could anyone help me to convert the boolean argument from python to java??
thank you!!
Is the argument of your Java method defined as a boolean or a java.lang.Boolean?
If it is boolean, then all the possibilities you have tried should work (if not there might be something wrong with the way you import the Class in your Python code). However, if it's a java.lang.Boolean, then you have to call your method as such:
item.myMethod(jpype.java.lang.Boolean(True))