Integer variables not storing in flatbuffers using python - python

I am seeing a strange issue using flatbuffers on python. I am trying to store a variable value which is integer, however, when I generate the JSON from the resulting .bin file the key / integer value will not be there.
However, if I simply pass an integer it works fine?
Do I need to do some kind of cast or generate an integer type in the same way as I do for strings?
Here is an example of the code I am running:
varInt = 1
SomeClass.SomeClassStart(my_builder)
SomeClass.SomeClassAddMyValue(my_builder, varInt)
SomeClass.SomeClassAddMyOtherValue(my_builder, 2)
some_class_buffer = SomeClass.SomeClassEnd(my_builder)
which produces json of :
some_class:{
my_other_value: 2
}
I don't understand why one way would work and one wouldn't? I don't have much experience with python and flatbuffers are completely new to me so not sure if theres some nuance that I'm unaware of?
Any help is appreciated.

I found the issue with this. It turns out that I had to set the builder to have the forceDefaults variable set to true.
my_builder.forceDefaults = True

Related

Dtype of type str but output of read_csv in Shell still of type int?

I'm fairly new in python so it happens that on certain problems I'm getting really stuck from time to time. I have already checked the web not finding a proper solution.
I'm importing a CSV file having one column defined as str with
test_df=pd.read_csv(('Mappe41.csv'), dtype={'Payment Transaction ID':'str'} ...
Once I'm running the code the output on python shell and subsequently in Excel shows still the data as int. Any idea why? Here below the output:
.
Thanks for any help...
It should be string instead of str. See pandas.DataFrame.dtypes
test_df=pd.read_csv(('Mappe41.csv'), dtype={'Payment Transaction ID':'string'}

How to turn 'string' into 'BuiltInCategory' in Revit Python Shell when reading excel file

I am trying to import data from Excel to Revit Python Shell in order to verify if some parameters exist in the Revit file for selected categories of objects. But I am having some problems in the first 'for' loop (look into the first column of the excel sheet and get the categories).
The first step to achieving what I wanted would be to get all the elements of the categories that I want to analyze. I've tried a lot of things, but I always end up in this error shown in the picture (got string instead of built-in category). I searched for a method to transform a string into builtincategories, but I did not find anything.
Does anyone know how to deal with it? Is there a way to transform a string into built-in category or is there another solution for this?
Thank you!
picture - screenshot
This question is more a C# than a Revit API one. Search for 'c# enum string convert', which turns up, e.g., convert a string to an enum in C#. In this case:
BuiltInCategory bic = BuiltInCategory.Parse( "some_string" );
Looks like the problem is with these lines:
fec = FilteredElementCollector(doc)
cat = fec.OfCategory(i.Value2)
In this instance, i.Value2 is a string, but fec.OfCategory is wanting a BuiltInCategory as you've commented earlier.
Jeremy's answer will convert a string to a BuiltInCategory (thanks Jeremy, I had no idea you could do that!) like this:
bic = BuiltInCategory.Parse(BuiltInCategory, "OST_PlumbingFixtures")
So in your example it would be:
fec.OfCategory(BuiltInCategory.Parse(BuiltInCategory, i.Value2.split('.')[1]))

Python lossy conversion from float to int

I am trying to figure out a way to write 21*(10^21) in Python. I've tried two approaches. The first one is simply by doing the following:
>>> print(21*(10**21))
21000000000000000000000
This works just fine. However, the problem I'm trying to solve requires me to get to this number by iteration, i.e, by building up from 1*(10^1) all the way to 21*(10^21). So I tried the following:
>>> temp = 20*(10**20)
>>> print(21*temp*10/20)
2.1e+22
Now, I want the entire number to show up, and not in the 'e' form, so I converted it to int. But this prints the wrong answer:
>>> print(int(21*temp*10/20))
20999999999999997902848
I know that integers don't have a limit in Python 3 (which is what I'm using) so this baffles me. I thought this may be because the /20 part causes conversion to float, but the number 21*(10^21) falls within the limits of float, so converting back to int shouldn't be a problem.
I've tried searching for this error online with no luck. Any help would be appreciated.

not able to change object to float in pandas dataframe

just started learning python. trying to change a columns data type from object to float to take out the mean. I have tried to change [] to () and even the "". I dont know whether it makes a difference or not. Please help me figure out what the issue is. thanks!!
My code:
df["normalized-losses"]=df["normalized-losses"].astype(float)
error which i see: attached as imageenter image description here
Use:
df['normalized-losses'] = df['normalized-losses'][~(df['normalized-losses'] == '?' )].astype(float)
Using df.normalized-losses leads to interpreter evaluating df.normalized which doesn't exist. The statement you have written executes (df.normalized) - (losses.astype(float)).There appears to be a question mark in your data which can't be converted to float.The above statement converts to float only those rows which don't contain a question mark and drops the rest.If you don't want to drop the columns you can replace them with 0 using:
df['normalized-losses'] = df['normalized-losses'].replace('?', 0.0)
df['normalized-losses'] = df['normalized-losses'].astype(float)
Welcome to Stack Overflow, and good luck on your Python journey! An important part of coding is learning how to interpret error messages. In this case, the traceback is quite helpful - it is telling you that you cannot call normalized after df, since a dataframe does not have a method of this name.
Of course you weren't trying to call something called normalized, but rather the normalized-losses column. The way to do this is as you already did once - df["normalized-losses"].
As to your main problem - if even one of your values can't be converted to a float, the columnwide operation will fail. This is very common. You need to first eliminate all of the non-numerical items in the column, one way to find them is with df[~df['normalized_losses'].str.isnumeric()].
The "df.normalized-losses" does not signify anything to python in this case. you can replace it with df["normalized-losses"]. Usually, if you try
df["normalized-losses"]=df["normalized-losses"].astype(float)
This should work. What this does is, it takes normalized-losses column from dataframe, converts it to float, and reassigns it to normalized column in the same dataframe. But sometimes it might need some data processing before you try the above statement.
You can't use - in an attribute or variable name. Perhaps you mean normalized_losses?

set null to None in Python, is it a good practice?

Very often when I deal with input/output that are generated from different languages, I often have to deal with the null values when I get those input/output into Python. As we all know None is the keyword for Python instead of null, so it often creates error when, say, the null value is in a list. Some libraries already deal with this situation, but some don't. I'm just wondering if it's a good practice to simply set
null = None
Honestly I haven't seen this done before, but I don't know if I'm just not paying attention. Is there any bad side effect for this? Is this even recommended?
EDIT: The current piece I'm working on does contain eval. I didn't it was such an evil thing lol. Someone wrote this a long time ago. I don't know how eval gave impression back then. nonetheless, I think it's a better idea to change the eval part.
Essentially the part that creates problem is when the code tries to eval a list that has null in it.
To be exact, the input here is a list of list that's wrapped in a string.
EDIT2: don't just downvote. I'm simply asking a question
This is not a good idea. It indicates some fuzziness in your program, a blurring of the line between code and input.
Any code you write should use None, not a variable set to None. Having a null constant wouldn't help because you could simply write None.
If you're getting input from the user or from another program, that input would be text. It'd be stored in a string variable as "null". It wouldn't be a raw identifier null. It would be a very poor design if user input could somehow refer to variables in your source code. That would be leaking implementation details.
If you find yourself using eval or equivalent, go directly to jail, do not pass GO, do not collect $200.
You're losing the advantages of None. Try this:
None = "Hello"
It doesn't work (SyntaxError) because assignment to None is prohibited. Creating an extra variable, null, introduces the possibility that null is not None at some future point in your program.
Just use None. None is guaranteed.
I'm imagining you could receive input as a string 'null'. However, this would be a string and not a key word. You'd be free to parse and deal with it as you saw fit. The only way I can imagine using null = None would do anything is if you were trying to evaluate strings as python code like
null = None
`eval('null')`
I'd consider this bad and sloppy coding practice.
I'm working with a response as a dictionary from a GET request from an API and received null and true as values of the dictionary. I took your null = None and it fixed my issue
I also set true = True
return {"cancel_url": null, "initiator_id": null, "initiator_type": null, "is_visible_to_user": true}
NameError: name 'null' is not defined

Categories