JasperReports: Converting String into array and populating List with it - list

What I have is this String [125, 154, 749, 215, 785, 1556, 3214, 7985]
(string can have anything from 1 to 15 ID's in it and the reason it is a string and not a List is that, its being sent through a URL)
I need to populate a List called campusAndFaculty with it
I am using iReport 5.0.0
I've tried entering this in the campusAndFaculty default value Expression field
Array.asList(($P{campusAndFacultyString}.substring( 1, ($P{campusAndFacultyString}.length() -2 ))).split("\\s*,\\s*"))
But it does not populate the campusAndFaculty List
Any idea how I can populate the List campusAndFaculty using that String ("campusAndFacultyString")?
======================
UPDATE
I have these variables in iReport (5.0.0)
String campusAndFacultyFromBack = "[111, 125, 126, 4587, 1235, 1259]"
String noBrackets = $P{campusAndFacultyFromBack}.substring(1 ($P{campusAndFacultyFromBack}.length() -1 ))
List campusAndFacultyVar = java.util.Arrays.asList(($V{noBrackets}).split("\\s*,\\s*"))
When I print campusAndFacultyVar It returns "[111, 125, 126, 4587, 1235, 1259]"
but when I use it in a Filter I get the "Cannot evaluate the following expression: org_organisation.org_be_id in null"

This works for me:
String something = "[125, 154, 749, 215, 785, 1556, 3214, 7985]";
Arrays.asList((something.substring(1, (something.length() -1 ))).split("\\s*,\\s*"));
Which means you can do this in iReport:
java.util.Arrays.asList(($P{campusAndFacultyString}.substring(1, (something.length() -1 ))).split("\\s*,\\s*"));
Differences with your snippet:
It's Arrays, not Array
You should take 1, not 2 from the length
Fully qualified reference to Arrays class (which may or may not matter depending on how your iReport is configured)

Related

output_ptr Assertion Error in Main - Cairo-lang

I am going through the official Cairo language tutorial. When I get to the build_dict function for the 15-puzzle, I am a bit confused so would like to print out some things to see.
struct Location:
member row : felt
member col : felt
end
...
func finalize_state(dict : DictAccess*, idx) -> (
dict : DictAccess*):
if idx == 0:
return (dict=dict)
end
assert dict.key = idx
assert dict.prev_value = idx - 1
assert dict.new_value = idx - 1
return finalize_state(
dict=dict + DictAccess.SIZE, idx=idx - 1)
end
##### I added the {} along with context in it for output #####
func main{output_ptr : felt*}():
alloc_locals
local loc_tuple : (Location, Location, Location, Location, Location) = (
Location(row=0, col=2),
Location(row=1, col=2),
Location(row=1, col=3),
Location(row=2, col=3),
Location(row=3, col=3),
)
# Get the value of the frame pointer register (fp) so that
# we can use the address of loc_tuple.
let (__fp__, _) = get_fp_and_pc()
# Since the tuple elements are next to each other we can use the
# address of loc_tuple as a pointer to the 5 locations.
verify_location_list(
loc_list=cast(&loc_tuple, Location*), n_steps=4)
##### Here is what I added #####
local locs : Location* = cast(&loc_tuple, Location*)
tempvar loc = [locs]
tempvar row = loc.row
serialize_word(row)
################################
return ()
end
I added the lines for printing the first row in loc_tuple. However, the Cairo compiler is giving me the following errors:
Traceback (most recent call last):
File "/Users/yijiachen/cairo_venv/bin/cairo-compile", line 10, in <module>
sys.exit(main())
File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 397, in main
cairo_compile_common(
File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 121, in cairo_compile_common
assembled_program = assemble_func(
File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 367, in
cairo_assemble_program
check_main_args(program)
File "/Users/yijiachen/cairo_venv/lib/python3.8/site-packages/starkware/cairo/lang/compiler/cairo_compile.py", line 296, in check_main_args
assert main_args == expected_builtin_ptrs, (
AssertionError: Expected main to contain the following arguments (in this order): []. Found: ['output_ptr'].
I have tried with various serialize_word statements and none seem to work. This issue never arose before with other serialize_word statements, including in earlier parts of the tutorial.
Declare %builtins output at the top of your code so that the compiler will know that you use an implicit argument (output_ptr) in your main function and expect it.
The Cairo compiler is able to process implicit arguments only if you declare that you are going to use them. See here.

Why is max function giving on error on list of numbers?

I have a list obtained from a df from a previous function, but it's structured like a normal list:
list1 = [45, 789, 52, 7962, 54, 5, 78]
when I try to call max(list1), i get an error that says: Invalid argument, not a string or column.
I've tried just defining a new list to see if the error came from the gettign of it from a function:
test = [1,2,3,4,5,6]
But max(test) resulted in the same error. Why is this happening?

List index out of range when reading a file

I am opening a file and trying to read the 3rd value on each line. Here is my code
myfile = 'dummy2.pepmasses'
fileObj = open(myfile, 'r')
line = fileObj.readline()
while line:
line = fileObj.readline()
linesplit = line.split()
weight = linesplit[2]
print(weight)
fileObj.close
This is resulting the third value being correctly displayed however there is an index error at the bottom but I'm not sure why as I'm not specifying a range of values to read, but rather just to read everything. I believe the issue is that when I read the file there is a blank [] at the bottom, although there are no blank lines on the actual file so I don't understand what is happening.
Any ideas appreciated, thanks.
The end of my file is
STE50,YCL032W 36 1262.6920 0 0 QQGLHPAIMLR
STE50,YCL032W 37 174.1117 0 0 R
STE50,YCL032W 38 174.1117 0 0 R
STE50,YCL032W 39 2081.8783 0 0 GDFEEVAMMNGSDNVTPGGR
STE50,YCL032W 40 131.0947 0 0 L*
The error generated is
174.1117
2081.8783
131.0947
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/Test/Test.py", line 12, in <module>
weight = linesplit[2]
IndexError: list index out of range
You should check whether linesplit has at least 3 values in it and only print the weight in that case.

XlsxWriter: set_column() with one format for multiple non-continuous columns

I want to write my Pandas dataframe to Excel and apply a format to multiple individual columns (e.g., A and C but not B) using a one-liner as such:
writer = pd.ExcelWriter(filepath, engine='xlsxwriter')
my_format = writer.book.add_format({'num_format': '#'})
writer.sheets['Sheet1'].set_column('A:A,C:C', 15, my_format)
This results in the following error:
File ".../python2.7/site-packages/xlsxwriter/worksheet.py", line 114, in column_wrapper
cell_1, cell_2 = [col + '1' for col in args[0].split(':')]
ValueError: too many values to unpack
It doesn't accept the syntax 'A:A,C:C'. Is it even possible to apply the same formatting without calling set_column() for each column?
If the column ranges are non-contiguous you will have to call set_column() for each range:
writer.sheets['Sheet1'].set_column('A:A', 15, my_format)
writer.sheets['Sheet1'].set_column('C:C', 15, my_format)
Note, to do this programmatically you can also use a numeric range:
for col in (0, 2):
writer.sheets['Sheet1'].set_column(col, col, 15, my_format)
Or you could reference columns like this:
for col in ('X', 'Z'):
writer.sheets['Sheet1'].set_column(col+':'+col, None, my_format)

Merge two annotated results into one dictionary

I have to annotated results:
cred_rec = Disponibilidad.objects.values('mac__mac', 'int_mes').annotate(tramites=Count('fuar'), recibidas=Count('fecha_disp_mac')
cred14 = Disponibilidad.objects.filter(int_disponible__lte=14).values('mac__mac', 'int_mes').annotate(en14=Count('fuar'))
Both have the same keys 'mac__mac' and 'int_mes', what I want is to create a new dictionary with the keys in cred_red, plus en14 from cred14.
I try some answers found here, but I missing something.
Thanks.
EDIT. After some tries and errors, I got this:
for linea in cred_rec:
clave = (linea['mac__mac'], linea['int_mes'])
for linea2 in cred14:
clave2 = (linea2['mac__mac'], linea2['int_mes'])
if clave2 == clave:
linea['en14'] = linea2['en14']
linea['disp'] = float(linea2['en14'])/linea['recibidas']*100
Now, I have to ask for a better solution. Thanks again.
=======
EDIT
This is how the input looks like:
fuar, mac_id, int_mes, int_disponible, int_exitoso, fecha_tramite, fecha_actualiza_pe, fecha_disp_mac
1229012106349,1,7,21,14,2012-07-02 08:33:54.0,2012-07-16 17:33:21.0,2012-07-23 08:01:22.0
1229012106350,1,7,25,17,2012-07-02 09:01:25.0,2012-07-19 17:45:57.0,2012-07-27 17:45:59.0
1229012106351,1,7,21,14,2012-07-02 09:15:12.0,2012-07-16 19:14:35.0,2012-07-23 08:01:22.0
1229012106352,1,7,24,16,2012-07-02 09:25:19.0,2012-07-18 07:52:18.0,2012-07-26 16:04:11.0
... a few thousand lines dropped ...
The fuar is like an order_id; mac__mac is like the site_id, mes is month; int_disponible is the timedelta between fecha_tramite and fecha_disp_mac; int_exitoso is the timedelta between fecha_tramite and fecha_actualiza_pe.
The output is like this:
mac, mes, tramites, cred_rec, cred14, % rec, % en 14
1, 7, 2023, 2006, 1313, 99.1596638655, 65.4536390828
1, 8, 1748, 1182, 1150, 67.6201372998, 97.2927241963
2, 8, 731, 471, 441, 64.4322845417, 93.6305732484
3, 8, 1352, 840, 784, 62.1301775148, 93.3333333333
tramites is the sum of all orders (fuar) in a month
cred_rec cred is our product, in theory for each fuar there is a cred, cred_rec is the sum of all cred produced in a month
cred_14 is the sum of all cred made in 14 days
% rec the relationship between fuar received and cred produced, in %
% en 14 is the relationship between the cred produced and the cred produced in time
I will use this table in a Annotated Time Line chart or a Combo Chart from Google Charts to show the performance of our manufacturation process.
Thanks for your time.
One immediate improvement to the current code you have would be to have the en14 and disp values precalculated and indexed by key. This will reduce the scans on the cred14 list, but will use memory to store the precalculated values.
def line_key(line):
return (line['mac__mac'], line['int_mes'])
cred14_calcs = {}
for line in cred14:
cred14_calcs[line_key(line)] = {
'en14': line['en14'],
'disp': float(line['en14'])/line['recibidas']*100
}
for line in cred_rec:
calc = cred14_calcs.get(line_key(line))
if calc:
line.update(calc)