hash table/pscustomobject instead of switch block - hashtable

Is there a way to use hash table or pscustomobject instead of switch block below? hash table seems like a great way to simplify the function.
function Get-Farm
{
[cmdletbinding()]
param (
[parameter (Mandatory = $true)]
[string]$farm)
Process{
switch($farm){
A {
$script:startHostID = 0
$script:endHostID = 0
}
B {
$script:startHostID = 1
$script:endHostID = 12
}
C {
$script:startHostID = 13
$script:endHostID = 24
}
BC {
$script:startHostID = 1
$script:endHostID = 24
}
ALL {
$script:startHostID = 1
$script:endHostID =48
}
}
}

Indeed, hashtable lookup is a better alternative.
Create the lookup table in Begin block and use it in Process.
Also, use ValueFromPipeline in parameter description, otherwise don't use Process.
function Get-Farm
{
[cmdletbinding()]
param (
[parameter(Mandatory, ValueFromPipeline)]
[string]$farm
)
Begin {
$lookup = #{
A = 0,0
B = 1,12
C = 13,24
BC = 1,24
ALL = 1,48
}
}
Process {
if ($values = $lookup[$farm]) {
$script:startHostID = $values[0]
$script:endHostID = $values[1]
}
}
}
I've used the assignment inside if() to condense the code but of course you can write it separately:
$values = $lookup[$farm]
if ($values) {

Related

how to set excel cell to 2 decimal places using phpexcel

while ($input = $data_query->fetch(PDO::FETCH_ASSOC))
{
$row++;
$letter2 = "A";
$row++;
$letter2 = "A";
$objPHPExcel->getActiveSheet()->SetCellValue($letter2++.$row, $input['emp_loan_code']);
$objPHPExcel->getActiveSheet()->SetCellValue($letter2++.$row, $input['date_from']);
$objPHPExcel->getActiveSheet()->SetCellValue($letter2++.$row, $input['date_to']);
if ($input['status'] == 0) {
$input['status'] = 'Pending';
$objPHPExcel->getActiveSheet()->SetCellValue($letter2++.$row, $input['status']);
}
elseif ($input['status'] == 1) {
$input['status'] = 'Paid';
$objPHPExcel->getActiveSheet()->SetCellValue($letter2++.$row, $input['status']);
}
elseif ($input['status'] == 2) {
$input['status'] = 'Cancel';
$objPHPExcel->getActiveSheet()->SetCellValue($letter2++.$row, $input['status']);
}
else{
$input['status'] = 'Request';
$objPHPExcel->getActiveSheet()->SetCellValue($letter2++.$row, $input['status']);
}
$objPHPExcel->getActiveSheet()->SetCellValue($letter2.$row, $input['amount'])->getStyle()->getNumberFormat()->setFormatCode('0.00');
}
i want to set the entire E column to be 2 decimal places and
i already set it to 2 decimal buy the output is still 00.00000
how can i display the output 00.00
thanks
Instead of using this:
$objPHPExcel->getActiveSheet()->SetCellValue($letter2.$row, $input['amount'])->getStyle()->getNumberFormat()->setFormatCode('0.00');
Try this:
$objPHPExcel->getActiveSheet()->SetCellValue($letter2.$row, number_format($input['amount'],2,'.',''));

redux-persist, save only first 10 items using Transform

I'm trying to save only 2 items when persisting through redux-persist .
I'm perplexed why the following code won't work..
(it seems to save more than 2 items)
const myTransform = createTransform(
(inboundState, key) => {
let { openforum_threads } = inboundState
if (!openforum_threads) {
return inboundState
}
let { allIds } = openforum_threads
let STORE_NUM = 2
let storeIds = allIds.slice(0, STORE_NUM)
console.log('saving', storeIds)
let { byId } = openforum_threads
let storeById = {}
storeIds.map((id) => {
storeById[id] = byId[id]
})
openforum_threads = {
...openforum_threads,
allIds: storeIds,
byId: storeById
}
return { ...inboundState, openforum_threads}
},
(outboundState, key) => {
// convert mySet to an Array.
return outboundState
}
)

web api call performance tuning

I am trying to figure find why my api is taking ~5 secs working on 1000 records,is that good or bad?I want to know what tools are there in asp.net webapi to figure out which piece of code or where its consuming time and fine tune the performance
CODE:-
List<LookaheadRunInfo> lookaheadRunsInfo = new List<LookaheadRunInfo>();
List<lookahead_run> lookaheadRunData = new List<lookahead_run>();
if (!filterCriteria.labeled)
{
lookaheadRunData = bitDB.lookahead_run.Where(x => x.lookahead_run_status == null && x.submission_time != null).OrderByDescending(x => x.submission_time).Skip(filterCriteria.PageNumber * filterCriteria.PageSize).Take(filterCriteria.PageSize).ToList();
}
else
{
lookaheadRunData = bitDB.lookahead_run.OrderByDescending(x => x.submission_time).Skip(filterCriteria.PageNumber * filterCriteria.PageSize).Take(filterCriteria.PageSize).ToList();
}
foreach (var lookaheadRunRow in lookaheadRunData)
{
var lookaheadRunInfo = new LookaheadRunInfo();
var lookaheadRunId = lookaheadRunRow.lookahead_run_id;
lookaheadRunInfo.ECJobLink = lookaheadRunRow.ec_job_link;
lookaheadRunInfo.UserSubmitted = lookaheadRunRow.submitted_by;
lookaheadRunInfo.SubmittedTime = lookaheadRunRow.submission_time.ToString();
lookaheadRunInfo.RunStatus = lookaheadRunRow.lookahead_run_status;
var completionTime = lookaheadRunRow.completion_time;
if (completionTime == null)
{
lookaheadRunInfo.ElapsedTime = (DateTime.UtcNow - lookaheadRunRow.submission_time).ToString();
}
else
{
lookaheadRunInfo.ElapsedTime = (lookaheadRunRow.completion_time - lookaheadRunRow.submission_time).ToString();
}
List<String> gerrits = new List<String>();
List<string> lookaheadRunChangeListIds = new List<string>();
if (!filterCriteria.labeled)
{
lookaheadRunChangeListIds = (from lrcl in bitDB.lookahead_run_change_list
join cl in bitDB.change_lists on lrcl.change_list_id equals cl.change_list_id
where lrcl.lookahead_run_id == lookaheadRunId
//and cl.change_list_id not in (select clcl.change_list_id from component_labels_change_lists as clcl)
//where !(from clcl in bitDB.component_labels_change_lists select clcl.change_list_id).Contains(cl.change_list_id)
where !bitDB.component_labels_change_lists.Any(clcl => clcl.change_list_id == cl.change_list_id)
select cl.change_list.ToString()).ToList();
}
else
{
lookaheadRunChangeListIds = (from lrcl in bitDB.lookahead_run_change_list
join cl in bitDB.change_lists on lrcl.change_list_id equals cl.change_list_id
where lrcl.lookahead_run_id == lookaheadRunId
select cl.change_list.ToString()).ToList();
}
//bitDB.Log = Console.Out;
//lookaheadRunInfo.gerrits = gerrits;
lookaheadRunInfo.gerrits = lookaheadRunChangeListIds;
if (lookaheadRunChangeListIds.Count != 0 && filterCriteria.labeled == false)
{
lookaheadRunsInfo.Add(lookaheadRunInfo);
}
else if (filterCriteria.labeled == true)
{
lookaheadRunsInfo.Add(lookaheadRunInfo);
}
}
return lookaheadRunsInfo;

javafx checklistree output: the term inter comma

for (CheckBoxTreeItem<String> treeItem : treeItems) {
if (treeItem.isSelected()) {
if (treeItem.getValue() != null) {
konular = konular + treeItem.getValue();
}
}
}
System.out.println(konular);
hi, this code for checktreeviews ,treelist sample1 sample2...
I have any choice, output: nullSample3Sample4
I want: sample3, sample4
The Java 8 way:
String konular = treeItems
.stream()
.map(TreeItem::getValue)
.filter(value -> value != null)
.collect(Collectors.joining(", "));
Try setting your variable like this:
String konular = "";
I believe it is currently turning null into a string when you add the first new string, and thus you end up with it starting with "null".
To add the comma, you could do something like this:
for (CheckBoxTreeItem<String> treeItem : treeItems) {
if (treeItem.isSelected()) {
if (treeItem.getValue() != null) {
if(konular!=""){
konular = konular + ", ";
}
konular = konular + treeItem.getValue();
}
}
}
I'm sure it's not the cleanest way, but it should do the trick.

Circular References in PHPExcel - infinite loop or wrong result

I am using PHPExcel 1.8.0
I have read the posts regarding circular references, such as this one, but I am still running into issues.
If a spreadsheet contains one circular ref. formula, PHPExcel's calculations do not match MS Excel.
If a spreadsheet contains more than one circular ref, then PHPExcel goes into an infinite loop.
Here are the details of what I've done so far.
Assume a spreadsheet where A1 = B1 and B1 = A1+1, and Excel is set to 100 iterations. Here is my code:
// create reader
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
// load workbook
$objPHPExcel = $objReader->load($this->_path);
// set iterative calculations max count
PHPExcel_Calculation::getInstance($objPHPExcel)->cyclicFormulaCount = 100;
// calculate
$objWorksheet = $objPHPExcel->getSheetByName('Testing');
$data = $objWorksheet->rangeToArray('A1:B1');
echo '<pre>';
print_r ($data);
echo '</pre>';
// release resources
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
MSExcel results in A1 = 99, B1 = 100. My code produces this:
Array
(
[0] => Array
(
[0] => #VALUE!
[1] => #VALUE!
)
)
Further to that, if I add A2 = B2 and B2 = A2+1, and attempt to calculate (A1:B2), it goes into an infinite loop and eventually crashes:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 24 bytes) in C:\xampp\htdocs\cgc\bulldog\application\third_party\PHPExcel\Calculation.php on line 2837
Here is what I've done so far. In _calculateFormulaValue in Calculation.php:
Line 2383: $cellValue = ''; - this is the cause of the #Value! error. I changed that to $cellValue = 0;
Line 2400:
} elseif ($this->_cyclicFormulaCell == '') {
$this->_cyclicFormulaCell = $wsTitle.'!'.$cellID;
This is the cause of the infinite loop. $this->_cyclicFormulaCell does not get re-set to '' after the formula in row 1 is done, so this condition does not work for the formula in row 2.
I fixed this as follows, starting from Line 2389:
if (($wsTitle{0} !== "\x00") && ($this->_cyclicReferenceStack->onStack($wsTitle.'!'.$cellID))) {
if ($this->cyclicFormulaCount <= 0) {
return $this->_raiseFormulaError('Cyclic Reference in Formula');
} elseif (($this->_cyclicFormulaCount >= $this->cyclicFormulaCount) &&
($this->_cyclicFormulaCell == $wsTitle.'!'.$cellID)) {
// Olga - reset for next formula
$this->_cyclicFormulaCell = '';
return $cellValue;
} elseif ($this->_cyclicFormulaCell == $wsTitle.'!'.$cellID) {
++$this->_cyclicFormulaCount;
if ($this->_cyclicFormulaCount >= $this->cyclicFormulaCount) {
// Olga - reset for next formula
$this->_cyclicFormulaCell = '';
return $cellValue;
}
} elseif ($this->_cyclicFormulaCell == '') {
$this->_cyclicFormulaCell = $wsTitle.'!'.$cellID;
if ($this->_cyclicFormulaCount >= $this->cyclicFormulaCount) {
// Olga - reset for next formula
$this->_cyclicFormulaCell = '';
return $cellValue;
}
}
After these fixes, if I run $data = $objWorksheet->rangeToArray('A1:B2');, I get the following result:
Array
(
[0] => Array
(
[0] => 100 // should be 99
[1] => 100
)
[1] => Array
(
[0] => 100 // should be 99
[1] => 100
)
)
As you can see, the results from PHPExcel are not consistent with MS Excel. Why is this happening and how can I get around this?
Ok, I managed to debug this. My spreadsheet was very complicated, lots of circular refs. The most challenging is a scenario where A depends on B, B depends on both A and C, and C depends on B.
I also added a maxChange parameter, so the thing works like Excel. Otherwise it takes too long on my spreadsheet.
Anyway, here is a usage example:
$objPHPExcel = PHPExcel_IOFactory::load($path);
$objCalc = PHPExcel_Calculation::getInstance($objPHPExcel);
$objCalc->cyclicFormulaCount = 100;
$objCalc->maxChange = 0.001;
The two files that were modified are: Calculation.php and CalcEngine/CyclicReferenceStack.php
Here is the code (sorry Mark, I can't afford more time to submit it to git properly).
Calculation.php
add these to class properties:
private $_precedentsStack = array();
public $maxChange = 0;
replace _calculateFormulaValue() function with this:
public function _calculateFormulaValue($formula, $cellID=null, PHPExcel_Cell $pCell = null) {
$this->_debugLog->writeDebugLog("BREAKPOINT: _calculateFormulaValue for $cellID");
// Basic validation that this is indeed a formula
// We simply return the cell value if not
$formula = trim($formula);
if ($formula{0} != '=') return self::_wrapResult($formula);
$formula = ltrim(substr($formula,1));
if (!isset($formula{0})) return self::_wrapResult($formula);
// initialize values
$pCellParent = ($pCell !== NULL) ? $pCell->getWorksheet() : NULL;
$wsTitle = ($pCellParent !== NULL) ? $pCellParent->getTitle() : "\x00Wrk";
$key = $wsTitle.'!'.$cellID;
$data = array(
'i' => 0, // incremented when the entire stack has been calculated
'j' => 0, // flags the formula as having been calculated; can only be 0 or 1
'cellValue' => $pCell->getOldCalculatedValue(), // default value to start with
'precedents' => array(),
'holdValue' => FALSE // set to TRUE when change in value is less then maxChange
);
// add this as precedent
$this->_precedentsStack[] = $key;
// if already been calculated, return cached value
if (($cellID !== NULL) && ( $this->getValueFromCache($wsTitle, $cellID, $cellValue))) {
return $cellValue;
}
$this->_cyclicReferenceStack->getValueByKey($key, $data);
extract($data);
$this->_debugLog->writeDebugLog("iteration # $i");
// if already calculated in this iteration, return the temp cached value
if ($i >= $this->cyclicFormulaCount || $j == 1) {
return $cellValue;
}
// on stack, but has not yet been calculated => return default value
if (($wsTitle{0} !== "\x00") && ($this->_cyclicReferenceStack->onStack($key))) {
if ($this->cyclicFormulaCount <= 0) {
return $this->_raiseFormulaError('Cyclic Reference in Formula');
}
return $cellValue;
}
// calculate value recursively
$this->_cyclicReferenceStack->push($key);
$cellValue = $this->_processTokenStack($this->_parseFormula($formula, $pCell), $cellID, $pCell);
$this->_cyclicReferenceStack->pop();
// everything in precedent stack after the current cell is a precedent
// and every precedent's precedent is a precedent (aka a mouthfull)
while ( $this->_precedentsStack[ count($this->_precedentsStack) - 1 ] != $key ){
$data['precedents'][] = array_pop($this->_precedentsStack);
}
$data['precedents'] = array_unique($data['precedents']);
// check for max change
$oldValue = $this->_extractResult($data['cellValue']);
$newValue = $this->_extractResult($cellValue);
$data['cellValue'] = $cellValue;
$data['holdValue'] = (abs($oldValue - $newValue) < $this->maxChange);
// flag as calculated and save to temp storage
$data['j'] = 1;
$this->_cyclicReferenceStack->setValueByKey($key, $data);
// if this cell is a precedent, trigger a re-calculate
$tempCache = $this->_cyclicReferenceStack->showValues();
foreach ($tempCache as $tempKey => $tempData) {
if ( $tempData['holdValue'] == TRUE && ( in_array($key, $tempData['precedents'])) ) {
$tempData['holdValue'] = FALSE;
}
$this->_cyclicReferenceStack->setValueByKey($tempKey, $tempData);
}
// at the end of the stack, increment the counter and flag formulas for re-calculation
if (count($this->_cyclicReferenceStack->showStack()) == 0) {
$i++;
$this->_precedentsStack = array();
$tempCache = $this->_cyclicReferenceStack->showValues();
foreach ($tempCache as $tempKey => $tempData) {
$tempData['i'] = $i;
if ( ! $tempData['holdValue'] ) $tempData['j'] = 0;
$this->_cyclicReferenceStack->setValueByKey($tempKey, $tempData);
}
$this->_debugLog->writeDebugLog("iteration # $i-1 finished");
}
if ($i < $this->cyclicFormulaCount) {
$cellValue = $this->_calculateFormulaValue($pCell->getValue(), $cellID, $pCell);
} elseif ($cellID !== NULL) {
// all done: move value from temp storage to cache
$this->saveValueToCache($wsTitle, $cellID, $cellValue);
$this->_cyclicReferenceStack->removeValueByKey($key);
}
// Return the calculated value
return $cellValue;
} // function _calculateFormulaValue()
add this helper function:
private function _extractResult($result) {
if (is_array($result)) {
while (is_array($result)) {
$result = array_pop($result);
}
}
return $result;
}
CyclicReferenceStack.php
add a property:
private $_values = array();
add a bunch of functions:
public function setValueByKey($key, $value) {
$this->_values[$key] = $value;
}
public function getValueByKey($key, &$value) {
if (isset($this->_values[$key])) {
$value = $this->_values[$key];
return true;
}
return false;
}
public function removeValueByKey($key) {
if (isset($this->_values[$key])) {
unset($this->_values[$key]);
}
}
public function showValues() {
return $this->_values;
}

Resources