text:
Q: Why ZeroMQ SUBs are missing messages? I built about 12000 subscribers per computer with threading as following
subscriber side:
def client(id):

    context=zmq.Context()
    subscriber=context.socket(zmq.SUB)
    subscriber.connect('ip:port')
    subscriber.setsockopt(zmq.SUBSCRIBE,(id+'e').encode())

    while 1:
        signal=subscriber.recv_multipart()

        write logs...

for i in range(12000):
    threading.Thread(target=client,args=(str(i+j*12000),)).start()

#j is arbitrary unduplicated int

publisher side:
subscriber=zmq.Context().socket(zmq.PUB)
subscriber.bind('tcp://*:port')
while 1:
    for id in client_id:
        subscriber.send_multipart([(id+'e').encode()]+[message])

When I used more than one computer(by using different j) to build subscribers, sometimes some subscribers could not receive message at all.
If I restart subscribers, those who could not receive message become normal. But those who were normal become to unable to receive message.
These problem will not show any errors, only can be found in my logs.
Do excessive connection occur this problem?

A: As the counts of connections / messages / sizes grow larger and larger, some default guesstimates typically cease to suffice. Try to extend some otherwise working defaults on the PUB-side configuration, where the problem seems to start choking ( do not forget that since v3.?+ the subscription-list processing got transferred from the SUB-side(s) to the central PUB-side. That reduces the volumes of data-flow, yet at some (here growing to remarkable amounts) additional costs on the PUB-side ~ RAM-for-buffers + CPU-for-TOPIC-list-filtering...
So, let's start with these steps on the PUB-side :
aSock2SUBs = zmq.Context( _tweak_nIOthreads ).socket( zmq.PUB ) # MORE CPU POWER
aSock2SUBs.setsockopt( zmq.SNDBUF, _tweak_SIZE_with_SO_SNDBUF ) # ROOM IN SNDBUF

And last but not least, PUB-s do silently drop any messages, that do not "fit" under its current HighWaterMark level, so let's tweak this one too :
aSock2SUBs.setsockopt( zmq.SNDHWM, _tweak_HWM_till_no_DROPs )   # TILL NO DROPS

Other  { TCP_* | TOS | RECONNECT_IVL* | BACKLOG | IMMEDIATE | HEARTBEAT_* | ... }-low-level parameter settings may help further to make your herd of 12k+ SUB-s live in peace side by side with other (both friendly & hostile ) traffic and make your application more robust, than if relying just on pre-cooked API-defaults.
Consult both the ZeroMQ API documentation altogether also with the O/S defaults, as many of these ZeroMQ low-level attributes also rely on the O/S actual configuration values.
You shall also be warned, that making 12k+ threads in Python still leaves a purely [SERIAL] code execution, as the Python central GIL-lock ownership (exclusive) avoids (yes, principally avoids) any form of [CONCURRENT] co-execution, as the very ownership of the GIL-lock is exclusive and re-[SERIAL]-ises any amount of threads into a waiting queue and results in a plain sequence of chunks' execution ( By default, Python 2 will switch threads every 100 instructions. Since Python 3.2+, by default, the GIL will be released after 5 milliseconds ( 5,000 [us] ) so that other thread can have a chance to try & also acquire the GIL-lock. You can change these defaults, if the war of 12k+ threads on swapping the ownership of the GIL-lock actually results in "almost-blocking" any and all of the TCP/IP-instrumentation for message buffering, stacking, sending, re-transmit-ing until an in time confirmed reception. One may test it until a bleeding edge, yet choosing some safer ceiling might help if other parameters have been well adjusted for robustness.

Last but not least, enjoy the Zen-of-Zero, the masterpiece of Martin SUSTRIK for distributed-computing, so well crafted for ultimately scalable, almost zero-latency, very comfortable, widely ported signalling & messaging framework.

A: Further to user3666197's answer, you may also have to consider the time taken for all of those clients to connect. The PUBlisher has no idea how many SUBcribers there are supposed to be, and will simply get on with the job of sending out messages to those SUBscribers presently connected, from when the very first connection is made. The PUBlisher socket does not hang on to messages its sent just in case more SUBscribers connect at some undefined time in the future. Once a message has been transferred to 1 or more SUBscribers, it's dropped from the PUBlisher's queue. Also, the connections are not made instantaneously, and 12,000 is quite a few to get through.
It doesn't matter if you start your PUBlisher or SUBscriber program first; your 12,000 connections will be being made over a period of time once both programs are running, this happening asynchronously wrt to your own thread(s). Some SUBscribers will start getting messages whilst others will still be unknown to the PUBlisher. When, finally, all 12,000 connections are made then it will smooth out.


text:
Q: When should I use 'Cheer!', 'Cheers!'? May be it's simple but I'm stuck.
I'm using "Cheers!" for a toast or valediction on any internet forums. When I'm writing "Cheers!" I want say 'Thanks for the communication! Goodbye!' Can I use "Cheer!" for the same? Or does "Cheer!" have a different meaning?

A: Very good question.
I think 'Cheer' on its own does not stand as an expression. You need to have a phrase.

I cheered up when she arrived   To cheer her up, I took her on a long drive

On the other hand, if you want to express your 'joy' the way you explained, in a single word, it's Cheers!
However, you can use multiple 'cheers' to augment your expression! However, it then becomes an idiom.

'Three cheers for the winners'

redpajama_set_name: RedPajamaStackExchange


text:
Q: css, disabled css files how disables CSS file?
I use CMS and in header load style.css file. O don't have permission change index.php file, so i must change only in my file.
Thanks

A: Comment the whole css out. Start with /* and don't put the end piece in the file.
/*

// Here starts your normal css file
body {
    font-family: serif;
    ...
{
...

// Post at the end or not at all
*/

A: Are you saying you don't want to use the stylesheet but can't remove it from the html markup?  If so, just delete everything in the file.  You can comment it all out, but if you're not using it, save the bandwidth and just erase the content.

A: Just replace style.css with an empty style.css file.


text:
Q: Reformatting Mac... what are some files that are automatically saved? Last time I reformatted my Mac, I noticed all my passwords and favorited websites were saved.  Also my voice memos, I think, were also still there.  Everything else was deleted and I did not use iCloud Drive.

A: When you reformat a Mac, nothing is really left over or "automatically saved". After formatting, every file on that volume is gone (in terms of being visible through ordinary applications - be aware that it is possible in some cases to perform recovery of the formatted drive, so it is not a safe way to ensure data is permanently deleted).
When you use the macOS Recovery system to format and re-install macOS, you're offered multiple ways of adding your data back to the formatted drive. This is mainly in the form of (a) restoring from Time Machine backup, (b) migrating data from another computer or (c) adding an iCloud account that could then sync over data from iCloud Drive (not in your case), and various other iCloud services such as Photos, Contacts, Mail, Keychain, etc.


text:
Q: Why I2C_SMBUS_BLOCK_MAX is limited to 32 bytes? I'm trying to configure a SAA6752HS chip (a MPEG-2 encoder) through I2C bus using a Raspberry Pi as a development kit. It was a piece of cake until I had to write at the address 0xC2 of the chip. For this task, I have to use an I2C command that expects a payload of size 189 bytes. So then I stumbled upon a 32 bytes limitation inside the I2C driver, defined by I2C_SMBUS_BLOCK_MAX, in /usr/include/linux/i2c.h. It is not possible to force different values of max limit. Everything around I2C lib end up into the function i2c_smbus_access and any request with more then 32 bytes makes ioctl returns -1. I have no idea how to debug it so far.
static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
                                     int size, union i2c_smbus_data *data)
{
        struct i2c_smbus_ioctl_data args;

        args.read_write = read_write;
        args.command = command;
        args.size = size;
        args.data = data;
        return ioctl(file,I2C_SMBUS,&args);
}

I can't understand why there is such limitation, considering that there are devices that require more than 32 bytes of payload data to work (SAA6752HS is such an example).
Are there a way to overcome such limitation without rewrite a new driver?
Thank you in advance.

A: Here's the documentation for the Linux i2c interface: https://www.kernel.org/doc/Documentation/i2c/dev-interface
At the simplest level you can use ioctl(I2C_SLAVE) to set the slave address and the write system call to write the command.  Something like:
i2c_write(int file, int address, int subaddress, int size, char *data) {
    char buf[size + 1];               // note: variable length array
    ioctl(file, I2C_SLAVE, address);  // real code would need to check for an error
    buf[0] = subaddress;              // need to send everything in one call to write
    memcpy(buf + 1, data, size);      // so copy subaddress and data to a buffer
    write(file, buf, size + 1);
}

A: if write command is returning -1 make use open fd using
int fd = open("/dev/i2c-1", O_RDWR)
not
int fd = open("/dev/i2c-1", I2C_RDWR)


text:
Q: What Hikari settings do I am studying about spring and db, I found this part on a spring system settings, what the this Hikari configurations really do?
I know what somethings do, but not about all.
spring.datasource.hikari.connection-test-query=SELECT 1 FROM DUAL
spring.datasource.hikari.connection-timeout=600000
spring.datasource.hikari.maximum-pool-size=500
spring.datasource.hikari.max-lifetime=1800000
spring.datasource.hikari.minimum-idle=20
spring.datasource.hikari.validation-timeout=3000
spring.datasource.hikari.idle-timeout=60000

A: I made this error yesterday.
The properties are set, when you omit hikari. So you have to set those instead:
spring.datasource.connection-test-query=SELECT 1 FROM DUAL
spring.datasource.connection-timeout=600000
spring.datasource.maximum-pool-size=500
spring.datasource.max-lifetime=1800000
spring.datasource.minimum-idle=20
spring.datasource.validation-timeout=3000
spring.datasource.idle-timeout=60000

And are you sure, that you want to use a pool size of 500? That is quiet a lot.


text:
Q: Spring Boot Multiple Datasources set autocommit to false for all I have two datasources in a Spring Boot application. I want to set the autocommit to false, however it's only setting one connection to false, at least per the logging the logging for the Hikari connection pool. Somehow Spring takes the datasource wraps it into this Hikari connection but leaves me scratching my head as to which one it did that for.
I'd like to know how, in my application.properties file, I can set the autocommit to all of my datasources. Bonus points to tell me how to name the connection pool that the datasource gets wrapped into.
Below is the application.properties:
spring.datasource.url=${BLUECOST_DATASOURCE_URL}
spring.datasource.username=${BLUECOST_DATASOURCE_USERNAME}
spring.datasource.password=${BLUECOST_DATASOURCE_PASSWORD}
spring.datasource.driver-class-name=${BLUECOST_DATASOURCE_DRIVER}
spring.datasource.auto-commit=false

spring.batch-datasource.url=${BATCH_DATASOURCE_URL}
spring.batch-datasource.username=${BATCH_DATASOURCE_USERNAME}
spring.batch-datasource.password=${BATCH_DATASOURCE_PASSWORD}
spring.batch-datasource.driver-class-name=${BATCH_DATASOURCE_DRIVER}
spring.batch-datasource.jpa
spring.batch-datasource.auto-commit=false

spring.jpa.database-platform=DB2
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.DB2Dialect
spring.jpa.properties.hibernate.default_schema=${cost.rules.schema}

Logging snippet:
2021-09-11 23:38:07,431 INFO  [https-jsse-nio-8448-exec-7]: com.ibm.cio.sprint.cost.rules.resource.SoftLayerController Received getAsyncUploadSoftLayerFile request job instance id=33 job execution id=33
2021-09-11 23:38:07,432 INFO  [https-jsse-nio-8448-exec-7]: com.ibm.cio.sprint.cost.rules.application.SoftLayerJobService Looking for status of job instance id 33 job execution id
2021-09-11 23:38:07,432 DEBUG [https-jsse-nio-8448-exec-7]: org.springframework.jdbc.core.JdbcTemplate Executing prepared SQL query
2021-09-11 23:38:07,433 DEBUG [https-jsse-nio-8448-exec-7]: org.springframework.jdbc.core.JdbcTemplate Executing prepared SQL statement [SELECT JOB_INSTANCE_ID, JOB_NAME, JOB_KEY, VERSION from BLUECOST.BATCH_JOB_INSTANCE where JOB_INSTANCE_ID = ?]
2021-09-11 23:38:07,433 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig HikariPool-2 - configuration:
2021-09-11 23:38:07,434 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig allowPoolSuspension.............false
2021-09-11 23:38:07,435 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig autoCommit......................true
2021-09-11 23:38:07,435 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig catalog.........................none
2021-09-11 23:38:07,435 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig connectionInitSql...............none
2021-09-11 23:38:07,435 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig connectionTestQuery.............none
2021-09-11 23:38:07,436 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig connectionTimeout...............30000
2021-09-11 23:38:07,436 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig dataSource......................none
2021-09-11 23:38:07,436 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig dataSourceClassName.............none
2021-09-11 23:38:07,437 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig dataSourceJNDI..................none
2021-09-11 23:38:07,437 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig dataSourceProperties............{password=<masked>}
2021-09-11 23:38:07,437 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig driverClassName................."com.ibm.db2.jcc.DB2Driver"
2021-09-11 23:38:07,437 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig healthCheckProperties...........{}
2021-09-11 23:38:07,438 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig healthCheckRegistry.............none
2021-09-11 23:38:07,438 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig idleTimeout.....................600000
2021-09-11 23:38:07,438 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig initializationFailTimeout.......1
2021-09-11 23:38:07,439 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig isolateInternalQueries..........false
2021-09-11 23:38:07,439 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig jdbcUrl.........................jdbc:db2://<omitted>;
2021-09-11 23:38:07,440 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig leakDetectionThreshold..........0
2021-09-11 23:38:07,440 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig maxLifetime.....................1800000
2021-09-11 23:38:07,440 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig maximumPoolSize.................10
2021-09-11 23:38:07,441 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig metricRegistry..................none
2021-09-11 23:38:07,441 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig metricsTrackerFactory...........none
2021-09-11 23:38:07,442 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig minimumIdle.....................10
2021-09-11 23:38:07,442 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig password........................<masked>
2021-09-11 23:38:07,443 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig poolName........................"HikariPool-2"
2021-09-11 23:38:07,443 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig readOnly........................false
2021-09-11 23:38:07,444 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig registerMbeans..................false
2021-09-11 23:38:07,444 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig scheduledExecutor...............none
2021-09-11 23:38:07,445 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig schema..........................none
2021-09-11 23:38:07,445 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig threadFactory...................internal
2021-09-11 23:38:07,445 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig transactionIsolation............default
2021-09-11 23:38:07,446 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig username........................"90529100"
2021-09-11 23:38:07,446 DEBUG [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariConfig validationTimeout...............5000
2021-09-11 23:38:07,446 INFO  [https-jsse-nio-8448-exec-7]: com.zaxxer.hikari.HikariDataSource HikariPool-2 - Starting...

I tried setting default-auto-commit to false, but Hikari still built an autocommit to true
I looked on SO at question:
Spring Boot 2 disable Autocommit Hikari with multiple datasources, and I tried some of the suggestions mentioned, except this one:
"...long story short make sure that you add .type(HikariDataSource.class) before you build your class."  I don't know what they meant by that. Is that some application.properties change or some Spring annotation change?
Thanks,
Woodsman


text:
Q: Get wrong point.plotY position in Highcharts / Highstock I always get wrong point.plotY position when I click RangeSelector in Highstock.
Please see this fiddle.
I try to catch range change event in setExtremes: function (e). When I click 6m in rangeselector, in firebug console I print both last point object and point.plotY
You can see that the value of PlotY in the point object and the plotY is different. I also draw
a path start from  [chart.plotLeft + chart.plotWidth, chart.plotTop + point.plotY], it seems it does not match the last point of the series.
xAxis: {
                events: {

                    setExtremes: function (e) {
                        console.log('change');
                        $("#test").remove();

                        $(chart.series).each(function (i, serie) {
                            point = serie.data[serie.data.length - 1];
                            if (point) {
                                console.log(point);
                                console.log(point.plotY);
                                chart.renderer.path(['M', chart.plotLeft + chart.plotWidth, chart.plotTop + point.plotY, 'l', 1, 1])
                                    .attr({
                                    'stroke-width': 10,
                                    stroke: 'red',
                                    id: 'test'
                                })
                                    .add();
                            }

                        });
                    }

                }
            },

A: I changed your code to use xAxis.events.afterSetExtremes instead of xAxis.events.setExtremes Also since you probably are referring to the last point in the view port, changed serie.data to serie.points
$(chart.series).each(function (i, serie) {
    point = serie.points[serie.points.length - 1];
    if (point) {
        console.log(point);
        console.log(point.plotY);
        chart.renderer.circle(chart.plotLeft + chart.plotWidth, chart.plotTop + point.plotY, 5)
            .attr({
            'stroke-width': 5,
            stroke: 'red',
            id: 'test'
        }).add();
    }
});

@jsFiddle


text:
Q: Excel VBA conditional formatting - Removing formatting for 0 value I have a set of values in a column, 1 NA 0 1 0 NA and 0.  I run a successful script on the column to highlight the top value in dark green, and the top 50% of values (excluding the top value) in light green.
Range("K4:K10").Select
Selection.FormatConditions.AddTop10
With Selection.FormatConditions(1)
   .TopBottom = xlTop10Top
   .Rank = 50
   .Percent = True
End With
With Selection.FormatConditions(1).Interior
   .Color = 13434777
End With
Selection.FormatConditions.AddTop10

Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1)
   .TopBottom = xlTop10Top
   .Rank = 1
End With
With Selection.FormatConditions(1).Interior
   .Color = 3394611
End With

That works, but what I want to now do (and this will be added to all columns, most of which have much higher numbers) is remove highlighting from any cell with the value of 0.  I tried the below code:
    Dim wsrng As Range
    For Each wsrng In Range("K4:K10")
        If wsrng.Value = 0 Then
            Selection.FormatConditions.Delete
        End If
    Next wsrng

But it is removing the highlighting from every cell in the column regardless of value.  Any ideas?

A: Change selection to wsrng.
Dim wsrng As Range
    For Each wsrng In Range("K4:K10")
        If wsrng.Value = 0 Then
            wsrng.FormatConditions.Delete
        End If
    Next wsrng


text:
Q: Ogr2ogr: ERROR 1: Unable to find driver `PostgreSQL' I need to import geojson files into a local PostgreSQL9.6 database on OSX system.
ogr2ogr -f 'PostgreSQL' PG:"dbname=crossmap user=duccioa" "multigraph.geojson" -nln paris.multilayer

Ogr2ogr gives this error:
ERROR 1: Unable to find driver `PostgreSQL'

My GDAL2.1 library have been installed using kyngchaos binary.
I saw there are other posts on the topic but none of them resolved my problem. One issue is that I cannot figure out how to unistall the library (for example to reinstallit with HomeBrew).
The import used to work, I don't understand what went wrong at one point. The only thing I can think of is the installation of the latest version of QGIS 2.18.

A: Please refer to the answer in here https://github.com/OSGeo/homebrew-osgeo4mac/issues/1291#issuecomment-574170548


text:
Q: Why is my function iterating through rows instead of columns? I've written the following function
compareColumns <- function(x) {

  if (is.numeric(x)) {
    paste("range", x, "=", range(x))
  } else if (is.integer(x)) {
    paste("table", x, "=", table(x))
  } else if (is.logical(x)) {
    paste("table", x, "=", table(x))
  } else if (is.character(x) & length(unique(x)) <= 15) {
    paste("table", x, "=", table (x))
  } else {
    paste("count unique", x, "=", length(unique(x)))
  }

}

I want the output to be the one I would get if I were doing all of those operations manually to each column such as
apply(df, 2, length)

outputs:
Column1                   Column2                   Column3             Column4
2115                      2115                      2115                2115

or
length(df$Column1)

outputs
2115

instead I get
apply(df, 2, compareColumns)

outputs:
Column1
   [1,] "count unique row1 = 212"
   [2,] "count unique row2 = 212"
   [3,] "count unique row3 = 212"
   [4,] "count unique row4 = 212"
   [5,] "count unique row5 = 212"
   [6,] "count unique row6 = 212"
   [7,] "count unique row7 = 212"

Where am I going wrong? Why won't my function apply to columns only?

A: Use lapply instead of apply as apply converts to matrix and thus can have only a single type - i.e. if there is a single column that is character, it converts the whole into character class and thus got the same output with apply - i.e. it couldn't satisfy any of the conditions and thus goes into the else condition
lapply(df, compareColumns)

Also, as we are pasteing with x i.e. the column values, the length of the output will be the same as the number of rows

Check with a reproducible example
data(iris)
apply(iris, 2, compareColumns)
lapply(iris, compareColumns)


text:
Q: Django AJAX returns undefined instead of the variables So I have a simple Django script which I've found online for an AJAX function that runs a Python script and gets the output via stdout.
views.py
from django.shortcuts import render

def index(request):
    return render(request,'homepage/page.html')

homepage/page.html
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">
        <title>test</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

        <script type="text/javascript">
            $(function()
            {
                $('#clickme').click(function(){
                    alert('Im going to start processing');
                    $.ajax({
                        url: "static/homepage/js/external_func.py",
                        type: "POST",
                        datatype:"json",
                        data: {'key':'value','key2':'value2'},
                        success: function(response){
                            console.log(response.keys);
                            console.log(response.message);
                        }
                    });
                });
            });
        </script>

    </head>
    <body>
        <button id="clickme"> click me </button>
    </body>
</html>

So you can see my url is linked to external_func.py which runs after the button is clicked. The script then returns a json.
external_func.py
import sys
import json
import cgi

fs = cgi.FieldStorage()

sys.stdout.write("Content-Type: application/json")

sys.stdout.write("\n")
sys.stdout.write("\n")

result = {}
result['success'] = True
result['message'] = "The command Completed Successfully"
result['keys'] = ",".join(fs.keys())

d = {}
for k in fs.keys():
    d[k] = fs.getvalue(k)

result['data'] = d

sys.stdout.write(json.dumps(result, indent=1))
sys.stdout.write("\n")

sys.stdout.close()

However, when I run the server and clicked on the button, the console shows undefined for both values, meaning response.keys and response.message is undefined.

Now, when I instead switch the code to console.log(response) in homepage/page.html. The console prints out the entire external_func.py code in text.
I couldn't find a solution online. It seems like people rarely calls a Python script in an AJAX request, I see a lot of forum posts about AJAX calling for a php code instead.
EDIT1:
I have to clarify one thing. This is just a small section of my project which I want to run some test on. In my actual project, I will have a function in python that takes a long time to compute, hence I prefer to have a webpage partially rendered with a waiting icon while the function processes. The output from the function will then be displayed on a webpage.

A: You have a django app, and yet you are using CGI for this function? Why? Why not simply make the function another django view? Serving your response with django is much superior to CGI, unless that function significantly bloats or slows down your django. It is as easy as this:
from django.http import JsonResponse
def func(request):
    result = ...
    return JsonResponse(result)

If you really want to separate this into a CGI script, the most likely reason you are failing to get a response is your web server not being configured to process the CGI request. (Your Developer Tools Network tab is a great help for diagnosing exactly what kind of response you got.) For security reasons CGI is not enabled by default. You need to tell Apache (or whatever web server you are using) that CGI should be enabled for that directory, and that it should be associated with .py files.


text:
Q: Where can I find jsonlds (context) in order to create Products in Platform of Trust? Product API documentation has examples for creating data products.
There are parameters dataContext and parameterContext with dummy values as https://example.com/v1/Context/Data.
Where can I find some real context jsonIds for these?
I have tried https://api-sandbox.oftrust.net/contexts/v1/ but dataProducts is an empty array there in response.
Are these context specific for translators?

A: The values for the contexts are not actually used or checked, so you need to deploy the translator and give the URL to that (and whatever the other mandatory arguments are). We encourage you to consult this multi-connector git repo to see how context of output is configured there to be returned by connector.
DataProducts contexts started to be defined in standards v2 . They are continuously extending based on business needs.
dataContext should have as value the OutputContext

*

*E.g.: https://standards.oftrust.net/v2/Context/DataProductOutput/Sensor/
parameterContext should have as value the ParametersContext

*

*E.g.: https://standards.oftrust.net/v2/Context/DataProductParameters/Sensor/


text:
Q: Calling a variable stored in a literal notation object I have this literal notation object (took out the irrelevant parts):
var work = {
"display": function displayWork(){
    for(i in work.jobs){
        $('#workExperience').append(HTMLworkStart);

        var formattedEmployer = HTMLworkEmployer.replace('%data%', work.jobs[i].employer);
        var formattedTitle = HTMLworkTitle.replace('%data%', work.jobs[i].title);
        var formattedEmployerTitle = formattedEmployer + formattedTitle;
        var formattedWorkDates = HTMLworkDates.replace('%data%', work.jobs[i].dates);
        var formattedWorkLocation = HTMLworkLocation.replace('%data%', work.jobs[i].location);
        var formattedWorkDescription = HTMLworkDescription.replace('%data%', work.jobs[i].description);

        $('.work-entry:last').append(formattedEmployerTitle);
        $('.work-entry:last').append(formattedWorkDates);
        $('.work-entry:last').append(formattedWorkLocation);
        $('.work-entry:last').append(formattedWorkDescription);
    }
}
};

I have tried calling it with:
work.display.displayWork();
displayWork();
$(work.display).displayWork();

None of these have worked. How do I go about calling this function?
If i place the function outside of the literal notation object than I can call it just fine with:
displayWork();

A: Like this:
work.display();

The work object's display property refers to the function.
Because your function has been created with a function expression, the name displayWork is only usable from within the function, so if the function doesn't need to call itself (recursively) you could choose to omit this name and just say display : function() { ... }.

A: The correct way is the following:
work.display();

since the display is a property of the object we could use the dot notation to access it and the () to evaluate the function that it points to.

A: The correct way is:
work.display();
But the object HTMLworkStart could be missing if it is not in the global scope. So you should set it as argument:
 var work = {
    "display": function (HTMLworkStart){
    ...
    }

Then you have to call
work.display(HTMLworkStart);


text:
Q: tidyverse issue with 4.2.1 update, what to do? I am a beginner in R -
i just updated to 4.2.1. i kept getting the error below. ive tried uninstalling R and Rtools, reinstalled them both. also reinstalled all of my packages.
but i am still getting errors when i call up tidyverse.
when trying with:
library(pacman)
pacman::p_load(dplyr, tidyverse, ggplot2, tidyr, knitr, here, lubridate,
Rcpp, googlesheets4, faraway, mosaic, av, plotly,
gifski, gganimate, png, janitor, skimr, swirl, ggbeeswarm,
spsurvey, ggpubr, broom, climwin, plyr, gridExtra,
reshape2, gapminder, magick, caret, plotmo, mgcv,gamclass,tidymv)
i get:
[package 'tidyverse' successfully unpacked and MD5 sums checked
The downloaded binary packages are in
    C:\Users\erin bell\AppData\Local\Temp\RtmpgvKuST\downloaded_packages
tidyverse installed
Warning message:
In pacman::p_load(dplyr, tidyverse, ggplot2, tidyr, knitr, here,  :
  Failed to install/load:
tidyverse

]2

when trying with just: library(tidyverse),
i get:
[> library(tidyverse)
Error: package or namespace load failed for 'tidyverse' in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
 namespace 'cli' 3.1.0 is already loaded, but >= 3.3.0 is required

]3

thank you for any help.

A: Weirdly (or not?) enough installing package backports fixed that for me.


text:
Q: PCI device (64bits) configuration Memory access I am using a 64 bit FPGA connected by PCI. I am using the next function:
unsigned long pci_resource_[start|len|end|flags](struct pci_dev *pdev, int bar);

However, I don´t get the right result because these function are for 32bit PCI devices.
Are there others functions which work with 64 bits? You can see the information that I got from lspci
0002:01:00.0 Memory controller: Xilinx Corporation Device 7022
    Subsystem: Xilinx Corporation Device 0007
    Flags: bus master, fast devsel, latency 0
    Memory at <ignored> (64-bit, prefetchable)
    Memory at c40000000 (64-bit, prefetchable) [size=64K]
    Capabilities: [40] Power Management version 3
    Capabilities: [48] MSI: Enable- Count=1/8 Maskable- 64bit+
    Capabilities: [60] Express Endpoint, MSI 00
    Capabilities: [100] Device Serial Number 00-00-00-00-00-00-00-00

A: I am not sure which Linux version you are talking about. But in the current Linux tree (4.5), pci_resource_start is defined as:
#define pci_resource_start(dev, bar)    ((dev)->resource[(bar)].start)

and the embedding structure is
/* Linux/include/linux/pci.h */
struct resource resource[DEVICE_COUNT_RESOURCE]; /* I/O and memory regions + expansion ROMs */

/* Linux/include/linux/ioport.h */
struct resource {
    resource_size_t start;
    resource_size_t end;
    const char *name;
    unsigned long flags;
    struct resource *parent, *sibling, *child;
};

/* Linux/include/linux/types.h */
typedef phys_addr_t resource_size_t;

/* Linux/include/linux/types.h */
#ifdef CONFIG_PHYS_ADDR_T_64BIT
typedef u64 phys_addr_t;
#else
typedef u32 phys_addr_t;
#endif

So if you are on a 64bit system and Linux is compiled with CONFIG_PHYS_ADDR_T_64BIT then you are good.


text:
Q: SSL SYSCALL error: Operation timed out | pgAdmin 4, Postgres I am facing constant time-outs if I leave my session idle for more than 60 seconds or so and then try to query.
pgAdmin4, Version 5.7 (4280.88)
PostgreSQL 13.4

ERROR: SSL SYSCALL error: Operation timed out

Both of these are set to 0.
SHOW statement_timeout;
SHOW idle_in_transaction_session_timeout;

Can anyone tell me how to fix this?
I have to re-connect the session to get it to work, and this way all the temp tables are lost.

A: That looks like a misconfigured network component that drops the connection when it is idle.
The typical way to combat that is to configure TCP keepalive, either on the operating system or on the PostgreSQL server using the tcp_keepalives_idle parameter or with the libpq client connection parameter keepalives_idle (although I think that you cannot use connection strings with pgAdmin).


text:
Q: simple model using tensorflow 2 api gives huge loss and acc values import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler

df = pd.DataFrame({'A': np.array([100, 105.4, 108.3, 111.1, 113, 114.7, 120, 125, 129]),
                   'B': np.array([11, 11.8, 12.3, 12.8, 13.1,13.6, 13.9, 14.4, 15]),
                   'C': np.array([55, 56.3, 57, 58, 59.5, 60.4, 61, 61.5, 62]),
                   'Target': np.array([4000, 4200.34, 4700, 5300, 5800, 6400, 6800, 7200, 7500])})

df.head()

X_train = df.iloc[:, :3]
y_train = df.iloc[:, 3]

scaler = StandardScaler()
scaler.fit(X_train)
X_train_std = scaler.transform(X_train)

features = {'A': X_train_std[:, 0],
            'B': X_train_std[:, 1],
            'C': X_train_std[:, 2]}

labels = y_train

batch_size = 1
def train_input_fn(features, labels, batch_size):
    train_dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
    train_dataset = train_dataset.shuffle(1).batch(batch_size)
    return train_dataset

def pack_features_vector(features, labels):
    '''Pack the features into a single array'''
    features = tf.stack(list(features.values()), axis=1)
    return features, labels

train_dataset = train_input_fn(features, labels, batch_size).map(pack_features_vector)

class Model(tf.keras.Model):

    def __init__(self):
        super().__init__()

        self.l1 = tf.keras.layers.Dense(8, activation='relu')
        self.l2 = tf.keras.layers.Dense(4, activation='relu')
        self.out = tf.keras.layers.Dense(1)

    def call(self, x):
        x = self.l1(x)
        x = self.l2(x)
        return self.out(x)

learning_rate = 0.1
optimizer = tf.keras.optimizers.RMSprop(learning_rate)
loss_object = tf.keras.losses.MeanSquaredError()

train_loss_res = []
train_acc_res = []
epochs = 100
model = Model()

for epoch in range(epochs):
    epoch_loss_avg = tf.keras.metrics.Mean()
    epoch_acc = tf.keras.metrics.MeanAbsoluteError()

    for x,y in train_dataset:
        with tf.GradientTape() as tape:
            y_ = model(x)
            loss = loss_object(y, y_)
        gradients = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))

        epoch_loss_avg(loss)
        epoch_acc(y, y_)

    train_loss_res.append(epoch_loss_avg.result())
    train_acc_res.append(epoch_acc.result())

    if epoch % 10 == 0:
        print("Epoch {:03d} Loss: {:.3f}, Acc: {:.3%}".format(epoch, epoch_loss_avg.result(), epoch_acc.result()))

I am using simple data and a simple model.
The results that I am receiving are:
Epoch 000 Loss: 32666856.000, Acc: 561536.963%
Epoch 010 Loss: 342012.625, Acc: 37158.075%
Epoch 020 Loss: 328074.844, Acc: 35578.772%
Epoch 030 Loss: 189751.594, Acc: 27069.794%
Epoch 040 Loss: 273142.312, Acc: 29358.673%
Epoch 050 Loss: 424036.625, Acc: 44175.562%
Epoch 060 Loss: 43667.957, Acc: 14025.812%
Epoch 070 Loss: 96341.156, Acc: 19105.350%
Epoch 080 Loss: 39308.691, Acc: 16228.386%
Epoch 090 Loss: 46950.699, Acc: 17407.053%

I can't find out what the problem is.
I tried other setup (more units, different metrics, loss) but the result is the same.

A: The loss is so high because it is the square of the difference between the predicted y and true y. I noticed that you are using absolute error for your loss metric, but you are passing the squared loss into it.

*

*Your average y is approximately 5766

*The square root of your final MSE (i.e. the actual difference) is approximately 216

*216 is about 3%

*If you train for 1000 epochs that will drop to less than 1%

There are two main problems with the accuracy. The metric used will not output a percentage, it is outputting the absolute value of the loss. The second problem is that the string format token {:.3%} is expecting a float between 0 and 1 which is then multiplied by 100 to display as a percentage.
See the following code in which I've divided the average MAE by the average of the y value.(And subtracted it from 100 so that it represents the "accuracy". This math only works when loss is not greater that the average y).
import tensorflow as tf
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler

df = pd.DataFrame({'A': np.array([100, 105.4, 108.3, 111.1, 113, 114.7, 120, 125, 129]),
                   'B': np.array([11, 11.8, 12.3, 12.8, 13.1,13.6, 13.9, 14.4, 15]),
                   'C': np.array([55, 56.3, 57, 58, 59.5, 60.4, 61, 61.5, 62]),
                   'Target': np.array([4000, 4200.34, 4700, 5300, 5800, 6400, 6800, 7200, 7500])})

df.head()

X_train = df.iloc[:, :3]
y_train = df.iloc[:, 3]

scaler = StandardScaler()
scaler.fit(X_train)
X_train_std = scaler.transform(X_train)

features = {'A': X_train_std[:, 0],
            'B': X_train_std[:, 1],
            'C': X_train_std[:, 2]}

labels = y_train

batch_size = 1
def train_input_fn(features, labels, batch_size):
    train_dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
    train_dataset = train_dataset.shuffle(1).batch(batch_size)
    return train_dataset

def pack_features_vector(features, labels):
    '''Pack the features into a single array'''
    features = tf.stack(list(features.values()), axis=1)
    return features, labels

train_dataset = train_input_fn(features, labels, batch_size).map(pack_features_vector)

class Model(tf.keras.Model):

    def __init__(self):
        super().__init__()

        self.l1 = tf.keras.layers.Dense(8, activation='relu')
        self.l2 = tf.keras.layers.Dense(4, activation='relu')
        self.out = tf.keras.layers.Dense(1)

    def call(self, x):
        x = self.l1(x)
        x = self.l2(x)
        return self.out(x)

learning_rate = 0.1
optimizer = tf.keras.optimizers.RMSprop(learning_rate)
loss_object = tf.keras.losses.MeanSquaredError()

train_loss_res = []
train_acc_res = []
epochs = 1000
model = Model()

for epoch in range(epochs):
    epoch_loss_avg = tf.keras.metrics.Mean()
    epoch_acc = tf.keras.metrics.MeanAbsoluteError()
    y_avg = tf.metrics.Mean()
    for x,y in train_dataset:
        with tf.GradientTape() as tape:
            y_ = model(x)
            loss = loss_object(y, y_)
        gradients = tape.gradient(loss, model.trainable_variables)
        optimizer.apply_gradients(zip(gradients, model.trainable_variables))

        epoch_loss_avg(loss)
        epoch_acc(y, y_)
        y_avg(y)

    train_loss_res.append(epoch_loss_avg.result())
    train_acc_res.append(epoch_acc.result())

    if epoch % 10 == 0:
        print("Epoch {:03d} Squared Loss: {:.3f}, Acc: {:.3f}%".format(epoch, epoch_loss_avg.result(),100-(epoch_acc.result()/y_avg.result())))


text:
Q: Where to put a ViewModel factory in my ASP.NET MVC app? I have a fairly complicated ViewModel associated with a product listing form, which has a lot of custom default values depending on the currently logged in user.  I'm finding that populating it in my controller is quite a chore.  So I'm thinking of creating a factory class for it, but I'm wondering where in the project structure it would be a best fit.  In the Controllers folder?

A: I'd move it out into a separate Common or Core folder, or Common or Core project for that matter.  If you can decouple the logic that's not specific to MVC, you'll probably be better off in the long run.


text:
Q: Considering the condition of Rabin-Miller's prime recognition, should the case $r = 1$ also be tested? In my textbook for cryptography the lemma for this prime recognition is stated as following:

_Let $n \in \mathbb N$, $n = 2^sd + 1$, $d$ is odd. Denote, for $1 \leq x \lt n$, by $C(x)$ the condition:
 $\quad$ $\quad$ $\quad$ $x^d \not\equiv 1 \pmod n$ and ${x}^{2^rd} \not\equiv -1 \pmod n$ for all $1 \lt r \lt s$.
If $C(x)$ holds for some $1 \leq x \leq n$, then $n$ is not a prime. If $n$ is not a prime, then $C(x)$ holds for at least half of $x$ between $1$ and $n$.

But from what I have seen on Wikipedia, the case where r = 1 should be tested. And in this post, in the answer, the case r = 1 is also included. Therefore, in the condition it should be "for all 1 $\leq$ r $\lt$ s", shouldn't it?

A: Consider $n=5$. Then the $C(x)$ from your textbook translates into $x\not\equiv 1\pmod{n}$, obviously wrong. With $r=1$ (but not $r=0$) included, an additional requirement is $x^2\not\equiv-1$, satisfied by $x=4$ and claiming $5$ "not a prime" again.
The correct condition is "for $0\leqslant r<s$".


text:
Q: Buseman function on manifolds with $Ric \ge - \left( {n - 1} \right)$ It's well known that if M is a Riemannian manifold with $Ric \ge 0$ and contains a line $\gamma $. Set ${\gamma _ + } = \gamma \left| {_{[0, + \infty )}} \right.$, ${\gamma _ - } = \gamma \left| {_{[ - \infty ,0)}} \right.$, then for any point $x \in M$, we can construct a ray ${\sigma _ + }$(${\sigma _ - }$ ) as the limit of geodesic $x{\gamma _ + }(t)$($x{\gamma _ - }(t)$).
We can proof that: 1, ${\sigma _ + }$ and ${\sigma _ - }$ forms a line. 2, The Busemann function(about ${\gamma _ + }$ and ${\gamma _ - }$ respectively) ${b_ + }$, ${b_ - }$ has the property ${b_ + } + {b_ - } = 0$.
For a Riemannian manifold with $Ric \ge  - \left( {n - 1} \right)$ and contains a line,can we get 1 and 2? Maybe the simplest example is the hyperbolic space but I don't know how to compute.

A: Yes, the hyperbolic plane is a counter example. If you take a geodesic asymptotic to points $p, q$ at infinity, then the Busemann functions $b_p, b_q$ diverge to infinity at any ideal point $s$ different from $p, q$. Therefore, you cannot
have $b_p$ equal the negative of $b_q$.


text:
Q: Convert VARIANT to...? Note:

*

*Attempting to invoke a method of an interface, of which the return type is _variant_t
Code:
_variant_t resultsDataString;

_bstr_t simObjectNames;

simObjectNames = SysAllocString (L"TEST example 3");

resultsDataString = pis8->GetSimObject (simObjectNames);

inline function illustrated below, contained in .tli FILE:
inline _variant_t IS8Simulation::GetSimObject ( _bstr_t Name ) {
    VARIANT _result;
    VariantInit(&_result);
     HRESULT _hr = get_SimObject(Name, &_result);
   if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _variant_t(_result, false);
}

Note:

*

*resultsDataString is of struct tagVARIANT:

*

*VARTYPE vt is 9 (unsigned short)

*IDispatch IDispatch interface pointer

Question

*

*How can I then convert or extract the value?

*Possible using VariantChangeType?

*Another way?

Edit:
Note:

*

*Looking to transform the following, Visual Basic code to Visual C++

*MFC or ATL, if need be

*Ideally, pure C++
Visual basic equivalent:
Public example1, example2 As SIMUL8.S8SimObject
Dim numberOfexamples As Variant
Dim resultString As Variant

Set example1 = MySimul8.SimObject("Example 1")
Set example2 = MySimul8.SimObject("Example 2")

numberOfexamples = example1.CountContents + example2.CountContents

resultString = CStr(numberOfexamples) & "*"

A: Take a look at the MSDN docs for _variant_t
There is a ChangeType method as well as Extractors.
Edit: Once you have an IDispatch pointer, you need to use QueryInterface to get a specific object type with members and methods, or use IDispatch::Invoke. You can look at the MSDN Docs for IDispatch. Either way, you need to know about the object type that is being returned by the IS8Simulation::GetSimObject call.
Edit #2: Based on your VB code update, you want to use the same kind of code for C++, i.e. use the S8SimObject type in its C++ form (look in the generated .tlh file for _COM_SMARTPTR_TYPEDEF). Then you can directly do the same thing with CountContents. Otherwise, you would need to look up the CountContents DISPID with IDispatch::GetIDsOfNames and then call invoke.

A: It appears you are using C++ as a COM client by relying on the VC++ compiler's built-in COM support. To make coding the client "easier" you've used #import to generate C++ wrapper classes that attempt to hide all the COM details from you - or at least make the COM details simpler. So you're not using the COM SDK directly, but are using a client-side framework (I think of it like a light-weight COM-only framework akin to ATL or MFC).
Your example code, however, seems to be mixing the direct low-level COM SDK (VARIANTs, BSTR, SysAllocString) with the #import COM framework (_variant_t, _bstr_t, XXXXPtr). COM from C++ is complicated at first - so in a perfect world I would suggest getting to know the basics of COM before going too far forward.
However, if you just want something to work I would guess this is #import-style-of-COM-clients version of the VB code you provided:
_variant_t example1Var;
_variant_t example1Var;
SIMUL8::S8SimObjectQIPtr example1; // I'm guessing at this type-name from the VB code
SIMUL8::S8SimObjectQIPtr example2;

example1Var = pis8->GetSimObject(_bstr_t(L"Example 1"));
example2Var = pis8->GetSimObject(_bstr_t(L"Example 2"));
if (example1Var.vt == VT_DISPATCH && example2Var.vt == VT_DISPATCH)
{
   // **UPDATE** to try to spoon feed the QI ptr...
   example1 = IDispatchPtr((IDispatch*)example1Var);
   example2 = IDispatchPtr((IDispatch*)example2Var);
   // Does this screw-up reference counting?

   int numberOfexamples = example1->CountContents + example2->CountContents;

}

UPDATE:
Documentation on #import This makes using COM from C++ much easier, but is yet one other thing to learn...

A: You don't have to convert the _variant_t. It contains an IDispatch interface pointer, so you can get it out like this:
if (v.vt == VT_DISPATCH)
{
    IDispatchPtr pDispatch = v.pdispVal;
    // Do something with pDispatch.
    // e.g. assign it to a strongly-typed interface pointer.
}

A: There is a concept of "value" for an object (DISPID_VALUE).
Here is a codeproject.com article on resolving VARIANTs that might help.


text:
Q: Run time operators in C What is the definition of compile time and run time operator in C ? I know sizeof() is a compile time operator in C but which are the run time operators in C ?

A: For C; various optimisations (e.g. constant propagation and constant folding) mean that every operator can potentially be done at compile time (if the situation permits it).
For a simple example, consider this function:
int foo(int a, int b) {
    return a+b;
}

This looks like the addition needs to be done at run time.
Now consider this code:
int x = foo(1, 2);

Now the compiler can "inline" the function, propagate the constants, and do the addition at compile time. You'd end up with int x = 3; (plus a potentially redundant copy of the foo() function which is capable of doing the addition at run time).
There are also cases where an optimisation can't be done at compile time but can be done during linking (with LTO/Link Time Optimisation); where the operator isn't evaluated at compile time or run time. A simple example would be if code in another object file ("compilation unit") did int x = foo(1, 2);.
Also, in general, the opposite is also true - nothing guarantees that an operator will be evaluated at compile time when it is possible; so you can't say that an operator is always "compile time". For a simple example of this, consider string concatenation (e.g. char *myString = "Hello " "World!";) - it would be legal for the compiler to generate code that does the concatenation at run time (even though it's hard to think of a reason why a compiler would want to).

A: Compile time operators -> Calculated during compilation
Run time operators -> During execution
Example:
INPUT A,B
C = A + B
Here + is run time operator as it depends on values you input.


text:
Q: Excel - Comparing multiple columns to see if results are identical I would like to compare three separate columns in an excel spreadsheet, across thousands of rows.
If any value appears in column A multiple times (say the word hello in column A rows 1 and 4, and the word bye in column A and rows 3 & 5, I would like to check the corresponding values in column B for those rows (ie rows 1&4 and 3&5).
If the values in column B for rows 1&4 are say 15 & 15, and the values for rows 3&5 are 20 & 20 , then I want to check column C.
Now we know rows 1&4 and 3&5 have the same corresponding values in column A & B, I would like to check the corresponding values in column C. If these are different then I would like to perform a specific calculation. If they are the same values in Column C, then I want to ignore these rows.
I am sorry this is very unclear, as I cannot paste an image to show what I mean.  I can email you an example if it helps.
This is way beyond me and my excel skills and I do not know where to start. Any help would be appreciated. I am hoping I don't need to write a Macro.
Thanks in advance!

A: So, to resummarize your question as I understand it:
Column A holds string values (text). There are some duplicates here.
Column B holds number values. When a duplicate occurs in column A, the data in column B may or may not be identical as for the other duplicate entries.
Column C holds values (you did not define what type of values, but I assume these are number values). Sometimes, duplicates in column A hold the same values in column B, and also the same values in column C. In this case, we can ignore the row as all the duplicates agree. Sometimes, duplicates in column A hold different values in column B. In this case, we can also ignore the values. Finally, sometimes duplicates in column A hold the same values in column B, but different values in column C. For these specific values, we want to perform some other type of calculation (which you did not specify).
Put the following in column D, starting at row 2 (assuming a header on row 1), which is the starting point of the formula we will build.
=IFERROR(VLOOKUP(A$1:B1,A2,2,0)=B2,"")

This says: Look at column A, starting always at row 1, and going until 1 row above the current row. Check for a match of the text in the current row. If it finds a match there, pull the result from column B. Does that result match column B in the current row? If it matches it will say TRUE; if it doesnt match it will say FALSE. If there are no duplicates yet in column A it will say "".
Now add a new check - if the above formula is TRUE [ie: there is a duplicate in column A, and the result in column B matches], then we want to check the results from column C:
=IFERROR(IF(VLOOKUP(A$1:B1,A2,2,0)=B2,VLOOKUP(A$1:C1,A2,3,0)=C2,""),"")

This will now return TRUE if the values in column C match for that duplicate in column A (which is only checked if the values in column B match too). Finally, add in your "special calculation", like so:
=IFERROR(IF(VLOOKUP(A$1:B1,A2,2,0)=B2,IF(VLOOKUP(A$1:C1,A2,3,0)=C2,"",C2+1),""),"")

Where I have C2+1, this is where you will perform your special calculation. This will only be recorded by Excel if: there is a duplicate in column A, that duplicate has a matching value in column B, and that duplicate has an unmatched value in column C.


text:
Q: What is the difference between a dollar sign and a dollar sign followed by a period in JQuery? In jQuery, what is the difference between $  and $.? Please provide a practical explanation if possible.

A: $ is a reference (or synonym, if you want an analogy) to the global jQuery object. $.<method> calls a static method of the jQuery object, where $(<selector>) creates a new instance of the jQuery object.

A: In the context of jQuery, $ refers to the global jQuery object.
$. by itself is invalid JavaScript. As $ is an object, methods on this object are called like on any other object: $.methodname().
Maybe it becomes clearer by substituting $ with jQuery: jQuery.methodname().

A: I presume you are asking about the syntactic difference between $('#selector'); and $.parseJSON(str);.
The former is an invocation of a function called $ and the latter invokes it's method called parseJSON. Yup, functions can have methods - it is possible because Javascript functions are also objects. Figuratively speaking:
<script type="text/javascript">

function $(str) {
   alert(str);
}

$.parseJSON = function () {
   alert('Parsing JSON');
}

$('Hi');
$.parseJSON('{}');

</script>

A: From http://en.wikipedia.org/wiki/JQuery:

the $ function, which is a factory
  method for the jQuery object. These
  functions, often called commands, are
  chainable; they each return a jQuery
  object
$.-prefixed functions. These are
  utility functions which do not work on
  the jQuery object per se.
Typically, access to and manipulation
  of multiple DOM nodes begins with the
  $ function being called with a CSS
  selector string, which results in a
  jQuery object referencing matching
  elements in the HTML page. This node
  set can be manipulated by calling
  instance methods on the jQuery object,
  or on the nodes themselves. For
  example:
$("div.test").add("p.quote").addClass("blue").slideDown("slow");

This line finds the union of all div
  tags with class attribute test and all
  p tags with CSS class attribute quote,
  adds the class attribute blue to each
  matched element, and then slides them
  down with an animation. The $ and add
  functions affect the matched set,
  while the addClass and slideDown
  affect the referenced nodes.
The methods prefixed with $. are
  convenience methods or affect global
  properties and behaviour. For example,
  the following is an example of the map
  function called each in jQuery:
$.each([1,2,3], function(){
document.write(this + 1); });

This writes the number 234 to the
  document.

A: $ - dollar sign is also same as Jquery usage. But I preferred to use dollar sign because I like the way dollar sign is.


text:
Q: Can't generate facebook access token I need to generate the Facebook access token for a project.
Code:
import requests
import json

url = 'https://graph.facebook.com/oauth/access_token'
payload = {
'grant_type': 'client_credentials',
'client_id': 'clientid',
'client_secret': 'secret'
}
response = requests.post(url, json=payload)
print (response.json()['access_token'])

What I'm getting as output is my id|type, after research I found that it is due to the 'client_credential' request. I need to generate access tokens for a project (I want to use them to find likes etc). Any help would be appreciated, it's my first time attempting a project like this.

A: use [facebook-sdk.][1]
import facebook

    app_id = 'YOUR_APP_ID'
    app_secret = 'YOUR_APP_SECRET'

    graph = facebook.GraphAPI()

    # exactly what you're after ;-)
    access_token = graph.get_app_access_token(app_id, app_secret)


text:
Q: Is there a way, in the web console, to sort files in a google storage bucket according to date, size, type, etc Can't find any reference to this. Seems like a basic capability of any file storage in general.

A: Alternative is using the command line for this(gcloud/gsutil)
Assuming you have gcloud already installed... do next:
Using terminal(check/set) GCP project you are logged in:
$> gcloud config list - to check if you are using the proper GCP project.
$> gcloud config set project <your_project_id> - to set required project
Get the total bucket/folder size:
gsutil du -sah gs://bucket_name/folder1

List objects sorted by date ascending:
gsutil ls -l gs://bucket_name/folder1/folder2 | sort -k 2

List objects sorted by size descending:
gsutil du -ah gs://bucket_name/folder1/folder2/* | sort -k 2

List unique objects by prefix(for example object name pattern: twitter_2020-09-03-03-01-10.csv, facebook_2020-09-03-03-01-11.csv):
gsutil ls gs://bucket_name/folder_name | sed 's/_.*//' | uniq

Here (in the last example) if you have multiple twitter, facebook files in that folder the result will be:

gs://bucket_name/folder_name/twitter
gs://bucket_name/folder_name/facebook

A: Google Cloud added the ability to do this, but it's a bit hidden. You want to click on the dropdown right above the directory listing where it says Filter by name prefix only and then you can select Sort and filter. After that you should be able to click on a column to sort by that column.
Google Cloud warns that this will make the UI slower.

A: Currently, there is no way to sort the files or folders inside a bucket on the Cloud Console.
You can always do by coding a sorting routine:
// Imports the Google Cloud client library
const {Storage} = require('@google-cloud/storage');

// Your Google Cloud Platform project ID
const projectId = 'YOUR PROJECT';

// Creates a client
const storage = new Storage({
  projectId: projectId,
});

// The name for the new bucket
const bucketName = 'YOUR-BUCKET';
const bucket = storage.bucket(bucketName);
bucket.getFiles(null, (err,data) => {
    data.sort((a, b) => {
        if (a.metadata.updated > b.metadata.updated) {
            return 1;
        }
        if (a.metadata.updated < b.metadata.updated) {
            return -1;
        }
        return 0;
    });
    for (file of data) {
        console.log(` ${file.metadata.name} - ${file.metadata.updated}`);
    }
});

There is a Feature request but seems that is not updated.
https://issuetracker.google.com/issues/119209458


text:
Q: Attempting to bind a dynamic method on a dynamically-created assembly causes a RuntimeBinderException I have a handy utility method which takes code and spits out an in-memory assembly. (It uses CSharpCodeProvider, although I don't think that should matter.) This assembly works like any other with reflection, but when used with the dynamic keyword, it seems to fail with a RuntimeBinderException:

'object' does not contain a definition for 'Sound'

Example:
var assembly = createAssembly("class Dog { public string Sound() { return \"woof\"; } }");
var type = assembly.GetType("Dog");
Object dog = Activator.CreateInstance(type);

var method = type.GetMethod("Sound");
var test1Result = method.Invoke(dog, null); //This returns "woof", as you'd expect

dynamic dog2 = dog;
String test2Result = dog2.Sound(); //This throws a RuntimeBinderException

Does anyone know the reason why the DLR is not able to handle this? Is there anything that could be done to fix this scenario?
EDIT:
createAssembly method:
Disclaimer: some of this stuff contains extension methods, custom types, etc. It should be self-explanatory though.
private Assembly createAssembly(String source, IEnumerable<String> assembliesToReference = null)
{
    //Create compiler
    var codeProvider = new CSharpCodeProvider();

    //Set compiler parameters
    var compilerParameters = new CompilerParameters
    {
        GenerateInMemory = true,
        GenerateExecutable = false,
        CompilerOptions = "/optimize",
    };

    //Get the name of the current assembly and everything it references
    if (assembliesToReference == null)
    {
        var executingAssembly = Assembly.GetExecutingAssembly();
        assembliesToReference = executingAssembly
            .AsEnumerable()
            .Concat(
                executingAssembly
                    .GetReferencedAssemblies()
                    .Select(a => Assembly.Load(a))
            )
            .Select(a => a.Location);
     }//End if

    compilerParameters.ReferencedAssemblies.AddRange(assembliesToReference.ToArray());

    //Compile code
    var compilerResults = codeProvider.CompileAssemblyFromSource(compilerParameters, source);

    //Throw errors
    if (compilerResults.Errors.Count != 0)
    {
        throw new CompilationException(compilerResults.Errors);
    }

    return compilerResults.CompiledAssembly;
}

A: Make your class public.
var assembly = createAssembly("public class Dog { public string Sound() ...
                               ^

That solves the problem on my machine.

A: This could be a workarround.
//instead of this
//dynamic dog2 = dog;

//try this
dynamic dog2 = DynWrapper(dog);
String test2Result = dog2.Sound();//Now this works.

public class DynWrapper : DynamicObject {
        private readonly object _target;

        public DynWrapper(object target) {
            _target = target;
        }

        public override bool TryInvokeMember(InvokeMemberBinder binder, object[] args, out object result) {
            //for the sake of simplicity I'm not taking arguments into account,
            //of course you should in a real app.
            var mi = _target.GetType().GetMethod(binder.Name);
            if (mi != null) {
                result = mi.Invoke(_target, null);
                return true;
            }
            return base.TryInvokeMember(binder, args, out result);
        }
    }

PS: I try to post this as a comment (since it's a workarround not an answer) but couldn't do that beacuse it's to long....


text:
Q: Is there any implemented SubscribableChannel/EventDrivenConsumer out there to be used in a Spring Integration Kafka consumer? All I have found so far are PollableChannel/PollableConsumer implementations, e.g.:
https://github.com/spring-projects/spring-integration-samples/blob/master/basic/kafka
Do I still need to keep searching?
Does it make sense at all an event driven channel for a Kafka consumer?

A: See the spring-integration-kafka Chapter in the Spring for Apache Kafka Documentation.

Spring Integration Kafka is now based on the Spring for Apache Kafka project. It provides the following components:
Outbound Channel Adapter
Message-driven Channel Adapter
Inbound Channel Adapter
Outbound Gateway
Inbound Gateway
Channels Backed by Kafka Topics


text:
Q: Is there any way to automatically generate har files? I'm trying to automate page load time testing. I'm imagining running a cron job every couple hours that will load various pages from my website, retrieve the associated har files, and process the data. The part I can't figure out is how to programatically generate har files ... the only way I know of now is manually through chrome developer tools. Does anyone know if this can be done, and if so how?

A: We just did this recently with Firebug and NetExport:
http://www.softwareishard.com/blog/netexport/
You can configure NetExport to automatically export HAR files to a specified directory (compression optional). This has been useful for us in getting samples of resource loading data for particular web apps.

A: Have a look at the BrowserMob Proxy or run a private instance of WebPageTest and use WPT Monitor with it

A: PhantomJS (headless Webkit-browser on Node.js) can export .har files directly.
This way you should be able to build a tool that returns a .har-file for a given URL...

A: Using the combination of Selenium Server, Browsermob-Proxy and Chromedriver (if you wish to use Chrome to do your tests) in a language binding of your choice (java, python, c#) is the best way to programatically generate an HAR file, imho.
I have written this little python tool that uses Browsermob proxy to generate HAR file headlessly using Chrome or Firefox. It also generates NavigationTimingAPI data that lets you get to the tiny details of page load time.

A: If you can't run your own instance as Mr. Davies suggested you could use the REST API.
Marcel Duran has written a WebPageTest API wrapper for nodejs. Which can return a HAR from the resulting test. https://github.com/marcelduran/webpagetest-api

A: Har file reader API also available which will help you to parse the har file basically JSON format . You can also get the details of each http/https request/response,etc for preparing metrics.


text:
Q: How do you efficiently manage texture memory in OpenGLES on Android? I am loading textures into OpenGLES on Android and maintaining a reference to the generated id in a HashMap.
At a given point in time not all textures that have been loaded will be in use, but may be used at a later point in time, therefore if the device has enough free memory I'd like to keep the textures loaded.
However, if the device starts running low on memory I would like to delete any textures that are not in use since they can always be reloaded later if they're required.
I've tried a few methodologies for handling this scenario.
1. Respond to system memory warnings
If the application receives a memory warning, then it will identify which textures are not in use and schedule for those textures to be deleted.
This method did work reasonably well.
2. Use Soft References
In this approach the application would maintain a List<SoftReference<Texture>> where the Texture class is a wrapper around the id that was returned when a given texture was loaded into OpenGLES.
If a given Texture is not in use at a given point in time then only a SoftReference would exist to this Texture and thus if the garbage collector deemed it necessary it could reclaim this memory and the finalize method on the Texture class would schedule for this texture to be deleted.
This approach seemed ideal based upon the description of SoftReference in the Java documentation, since they would only be reclaimed when more memory was required.

Soft reference objects, which are cleared at the discretion of the garbage collector in response to memory demand. Soft references are most often used to implement memory-sensitive caches.

However, the Android implementation of SoftReference does not work like this: since Android 9 as the garbage collector is more aggressive and the soft references are reclaimed almost immediately regardless of whether the device is low on memory.
3. Use LruCache
The Android documentation advises against using SoftReference in a cache implementation and to use a LruCache instead. However, the LruCache poses some drawbacks.
Firstly, you have to specifying the maximum size of the cache. It isn't obvious what to set the cache size to: ideally it'd just automatically be set as high as possible while still being a good citizen. If it's set too small, it might be constantly reloading textures unnecessarily.
Secondly, a Texture may be removed from the cache which is currently in use and thus may be deleted from OpenGLES and then display as a missing texture to the user.
Is there a better way to maintain a cache of textures in OpenGL and be responsive to low memory scenarios (besides just deleting textures upon memory warnings)?

A: Talking about best practices, large caches (that can take all the memory) with OpenGL ids for textures are rarely used. Actually it's worth to have only a few textures to render in the scene at one moment. More textures you have in the scene, more texture switches you need to do in frame. It costs. Texture atlases were developed many years ago to reduce number of texture switches. However, atlases still can take a lot of memory.
With development of hardware capabilities and user expectations, texture atlases evolved into virtual/mega/sparse textures + texture data streaming for the cases of high memory usage. The idea is to use virtual memory approach and load/unload blocks of one very large texture in real-time. It has some drawbacks, good discussion about it can be found here. LRU cache can be built on top of it to tell what blocks are required at the moment.
Of course, engines can preload many textures (sparse or usual ones) and unload it after usage, e.g. as a part of dynamic loading of open world in a game. These textures aren't used for rendering simultaneously, and no one waits until they take all the memory to start unloading. The eviction algorithm here is highly dependant on particular app, even though maximum cache size in Mb is widely used practice here.


text:
Q: python folium polygons map color categories I have a dataframe in Python containing zipcodes (4 digits), pologyons, and would like to create maps with colored zipcodes based on a column value. I tried this with the geopandas and folium packages but it seems to be not working.
The shapefigures I downloaded from this website https://www.cbs.nl/nl-nl/dossier/nederland-regionaal/geografische-data/gegevens-per-postcode .
The shapefile seems to work when I use this code:
file_path = r'C:\xxxx\CBS_pc4_2020_v1.shp'
gdf_zipcodes = gpd.read_file(file_path)
fig, ax = plt.subplots(figsize=(8,8))
gdf_zipcodes.plot(ax=ax, facecolor='none', edgecolor='gray')
plt.show()

Currently, this is (part of) my dataframe
zipcodes =

Zipcode
Var
Location
Allocation

9941
o
Polygon
7023

7023
o
Polygon
7023

6321
o
Multipolygon
4020

I also have a location dataframe containing zipcodes and specific lon & latitudes like this:
loc_df =

Zipcode
lat
lon

9941
xxx
yyy

7231
xxx
yyy

What does work is creating a map with these specific coordinates, this is the code I used:
map = folium.Map(location =[loc_df['lat'].mean(),loc_df['lon'].mean()], zoom_start=12)
step = cm.StepColormap(colors, vmin = 0, vmax = 9999, index = [], caption = 'step')
color_pallete = sns.color_palette()
color_pallete = sns.color_palette("Set2", 99999)
color_pallete = color_pallete.as_hex()
for index, row in results_1.iterrows():
    if row['Var'] == 'o':
        folium.Marker([row['lat'], row['lon']], popup = row['Variable Name']).add_to(map)
    if row['Var'] == 'x':
        c = color_pallete[int(row['Allocation'])]
        folium.CircleMarker([row['lat'], row['lon']],radius=5, color = c).add_to(map)
    else:
        continue
    folium.CircleMarker(
        location=[row['lon'], row['lat']],
        radius=12,
        weight=2,
        fill=True,
        fill_color=colors[int(row['cluster'])],
        color=colors[int(row['cluster'])]
    ).add_to(map)
map.save('map1.html')

Then, I thought this code would work to use the pologyons instead of specific lat and lon values to create the map but unfortunately it does not:
for _, r in zipcodes.iterrows():
    # Without simplifying the representation of each borough,
    # the map might not be displayed
    sim_geo = gpd.GeoSeries(r['geometry']).simplify(tolerance=0.001)
    geo_j = sim_geo.to_json()
    geo_j = folium.GeoJson(data=geo_j,
                           style_function=lambda x: {'fillColor': 'blue'})
    folium.Popup(r['Location']).add_to(geo_j)
    geo_j.add_to(map)

In the end I would like to color based on the allocation of the dataframe and create markers such as in the first folium map but for now I would really like to visualize the polygons (and hopefully I can find out how I create the colors after that).


text:
Q: Better way to parse nullable double value from sql reader Is there a better way to parse SqlDecimal from SqlDataReader to double than this?
class test {
    public double? Qte { get; set; }
}

SqlDataReader reader = cmd.executeReader();
reader.Read();

test t = new test() {
    Qte = (reader["DL_Qte"] == DBNull.Value) ? (double?)null : double.Parse(reader["DL_Qte"].ToString())
}

A: Here's the way I'd do it:
double d = reader.GetFieldValue<double>(reader.GetOrdinal("DL_Qte"));

GetFieldValue<T> is only available since .NET 4.5, so you can also do this:
double d = reader.GetDouble(reader.GetOrdinal("DL_Qte"));

EDIT:
To address the issue with nullable and DB null you're having, you could do something like this:
int columnIndex = reader.GetOrdinal("DL_Qte");

double? d = !reader.IsDBNull(columnIndex) ?
    reader.GetFieldValue<double>(columnIndex) : new double?();

A: If it's a decimal or money in database, why don't you store it as decimal in your model? You should also not parse a decimal to string and then to double, use the right method:
class Test {
    public decimal? Qte { get; set; }
}

...
int columnIndex = reader.GetOrdinal("DL_Qte");
decimal? qte = null;
if(!reader.IsDBNull(columnIndex))
    qte = reader.GetDecimal(columnIndex);  // <----- !!!
Test t = new Test { Qte = qte };


text:
Q: Problems making REST POST requests to SP-Hosted App with Postman I am facing problems when making REST calls of type POST to my Sharepoint Hosted app with Postman in Chrome. GET type calls work fine.
For example, I want to make the following call on my app to get the changes on a list (and yes, this is a POST call weird enough):
https://mysite-AppGUID.sharepoint.com/sites/developer2/MySharepointApp/_api/Lists/getbytitle('lstRollen')/getChanges

Response is the following:
{
"error": {
    "code": "-2130575251, Microsoft.SharePoint.SPException",
    "message": {
        "lang": "en-US",
        "value": "The security validation for this page is invalid and might be corrupted. Please use your web browser's Back button to try your operation again."
    }
}

Googling learned me that I have to add a X-RequestDigest header with the value obtained from the POST call:
../_api/contextinfo
Indeed I get a value there as expected but when I add it to the headers, it still does not work. Am I missing something here? Really need this to work!!!!


text:
Q: How IDLE prints a character not supported by sys.stdout.encoding (python 3.7) I am trying to print the heart (♥) character in an idle shell on windows (Python 3.7 64-bit). The sys.stdout.encoding is 'cp1255'. As this character cannot be encoded into 'cp1255', a UnicodeEncodeError should be thrown. However, for some reason, printing works. How does it do this?
>>> import sys
>>> sys.stdout.encoding
'cp1255'
>>> u"\u2665".encode('cp1255')
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    u"\u2665".encode('cp1255')
  File "C:\Users\kobip\AppData\Local\Programs\Python\Python37\lib\encodings\cp1255.py", line 12, in encode
    return codecs.charmap_encode(input,errors,encoding_table)
UnicodeEncodeError: 'charmap' codec can't encode character '\u2665' in position 0: character maps to <undefined>
>>> print (u"\u2665")
♥

My motivation in asking the question is that I want to know, before I try to print, whether the console supports some unicode characters. I can do a try and catch every time I print the risky characters, or I can perform an initial attempt of printing these symbols and determine the behavior of the program based on its success, but I prefer a more elegant solution if possible (without the delay of trying an catching errors all the time, and without printing a wierd sentence to stdout). Currently I use:
sys.stdout.encoding.lower().startswith('utf') to chekc console support,
but, as shown here, some consoles (including mine) can print characters not supported by sys.stdout.encoding.


text:
Q: reduce an Object based on DATE and TIME + calculate a VALUE
I have an object and I want to reduce the date and time together and calculate a number.
At the moment I'm trying the following code (but the 'time' value is missing)...

const myObject = [
    {
        "date": "2023-01-01",
        "time": "18:00",
        "somevalue": 2
    },
    {
        "date": "2023-01-01",
        "time": "18:00",
        "somevalue": 5
    },
    {
        "date": "2023-01-02",
        "time": "19:00",
        "somevalue": 10
    },
    {
        "date": "2023-01-03",
        "time": "06:00",
        "somevalue": 13
    }
];

const all = myObject.reduce( (prev, value) => (
  { ...prev, [value.date]: (prev[value.date] || 0) + value.somevalue } ), {});

// to array
console.log(
  Object.entries(all).map( ([key, value]) => ( {date: key, somevalue: value} ) )
);

My question is: How can I add the "time" value. Like this:
[
    {
        "date": "2023-01-01",
        "time": "18:00",
        "somevalue": 7
    },
    {
        "date": "2023-01-02",
        "time": "19:00",
        "somevalue": 10
    },
    {
        "date": "2023-01-03",
        "time": "06:00",
        "somevalue": 13
    }
]

A: Maybe something like this?

const myObject = [ { date: "2023-01-01", time: "18:00", somevalue: 2 }, { date: "2023-01-01", time: "18:00", somevalue: 5 }, { date: "2023-01-02", time: "19:00", somevalue: 10 }, { date: "2023-01-03", time: "06:00", somevalue: 13 }];

const output = myObject.reduce((arr, acc) => {
  const index = arr.findIndex(({ date, time }) => date === acc.date && time === acc.time); //find duplicated elements by date
  if (index > -1) { // if you have duplicated element
    arr[index] = {
      ...arr[index], // merge objects
      somevalue: arr[index].somevalue + acc.somevalue // add value
    };
  } else {
    arr = [...arr, acc]; //if no duplicates values, just add to array.
  }
  return arr;
}, []);

console.log(output)

I use reduce to merge duplicated elements by "date" and add the someValue.

A: This would also work

const myObject = [ { date: "2023-01-01", time: "18:00", somevalue: 2, }, { date: "2023-01-01", time: "18:00", somevalue: 5, }, { date: "2023-01-02", time: "19:00", somevalue: 10, }, { date: "2023-01-03", time: "06:00", somevalue: 13, }, ];

const output = Object.entries(
  myObject.reduce((prev, { date, time, somevalue }) => {
    // create a key like <date>$<time> and add up
    // somevalue when key is discovered again
    prev[`${date}$${time}`] =
      prev[`${date}$${time}`] != undefined
        ? prev[`${date}$${time}`] + somevalue
        : somevalue;
    return prev;
  }, {})
).map(([key, somevalue]) => {
  // split the key using $ and get date and time
  // return created object
  const [date, time] = key.split("$");
  return { date, time, somevalue };
});

console.log(output);


text:
Q: How to access nested documents using Restangular I'm beginning to use restangular for the first time and struggling to access nested documents. If I have a Mongoose schema like so:
var mongoose = require('mongoose');

module.exports = function Schemas() {
    this.schema = mongoose.Schema;

this.EmployeeSchema = new this.schema({
    'firstname': {
        type: String,
        required: true,
        trim: true
    },
    'lastname': {
        type: String,
        required: true,
        trim: true
    },
    'email': {
        type: String,
        required: true,
        trim: true,
        index: true,
        validate: /\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}\b/
    },
    'departmentId': {
        type: this.schema.ObjectId,
        trim: true,
        required: true
    },
    'enddate': {
        type: String,
        trim: true
    },
    'active': {
        type: Boolean,
        "default": true
    }
});
this.EmployeeSchemaModel = mongoose.model('employees', this.EmployeeSchema);

this.DepartmentSchema = new this.schema({
    'name': {
        type: String,
        required: true,
        trim: true,
        index: {
            unique: true
        }
    },
    'employees': [this.EmployeeSchema]
});
    this.DepartmentSchemaModel = mongoose.model('departments', this.DepartmentSchema);
    mongoose.connect('mongodb://localhost:8120/staff');
};

To access and employee within a department, I thought I could simply do the following:
        var getEmployeeById = function(departmentId, empId) {

             Restangular.one('departments',departmentId).one('employees', empId).get().then(function (emp){
            return emp;
        });

Although I can retrieve a department, and when I do, it has a nested employees array, correctly populated with the employees I expect. I can't actually retrieve an individual employee via its id as I intended. I end up with a 404.
What am I doing wrong?

A: I'm the creator of Restangular.
I don't know 100% how Mongose works with Express or with what you're exposing the API but the idea is that you should have some nested Restful Resources as well as this "nesting" in your mongo documents.
So, you should have some Rest API that given a request to /departments/123 returns the department 123 and that given /departments/123/employees/456 returns the employee 456 of the department 123. If you have a Rest API for that, then, you're going to get the rigth response with Restangular :D
Hope this works for you!


text:
Q: Leap year friendly year/month/day select option tags with PHP embedded I'm trying to write codes with PHP to enable leap year friendly select option tags. For example, you pick a year, then it checks if it is a leap year or not, then it shows a pull down menu (select option tags) of days preceded by another select option tags of 12 months. PHP codes are embedded in HTML. Here is my failed attempt below (I'm new to PHP):
<form method="post">
<select name="year">
 <option value="" selected>Pick a year</option><!--Default-->
 <option value="2016">2016</option>
 <option value="2017">2017</option>
 <option value="2018">2018</option>
 <option value="2019">2019</option>
 <option value="2020">2020</option>
</select>
<select name="month">
 <option value="" selected>Pick a month</option><!--Default-->
 <!--Show all 12 months-->
 <?php for( $i = 1; $i <= 12; $i++ ): ?>
 <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
 <?php endfor; ?>
</select>
<select name="day">
 <option value="" selected>Pick a day</option><!--Default-->
 <!--Show dates depending on the conditions below:-->
 <?php
 if ( $month == 1 || 3 || 5 || 7 || 8 || 10 || 12 )
 {
    //Show dates until 31
    for ( $i = 1; $i <= 31; $i++ )
    {
 ?>
 <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
 <?php
    }
 }
 elseif ( $month == 2 )
 {
    //Leap year
    if ( $year != "" && $year % 4 == 0 && $year % 100 == 0 && $year % 400 == 0 )
    {
        //Show dates until 29
        for ( $i = 1; $i <= 29; $i++ )
        {
            ?>
            <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
            <?php
        }
    }
    //Regular year (Non-leap year)
    else
    {
        //Show dates until 28
        for ( $i = 1; $i <= 28; $i++ )
        {
            ?>
            <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
            <?php
        }
     }
}
elseif ( $month == 4 || 6 || 9 || 11 )
{
    //Show dates until 30
    for ( $i = 1; $i <= 30; $i++ )
    {
        ?>
        <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
        <?php
    }
 }
?>
</select>
<input type="submit" value="submit">
</form>

Notes:
I understand that the above is the incomplete code and it doesn't work. Besides, I don't know how to define $year or $month or $day from the select tags. Should I do it like this? If so, where should I put it?
<?php
if( $_SERVER["REQUEST_METHOD"] == "POST" )
{
    $year = $_POST['year'];
    $month = $_POST['month'];
    $day = $_POST['day'];

    //The rest of the php code?
}
?>

Preferably, I do not want to show years by using for loops as in months and days. I would like to do it manually this time (sorry, but if you suggest I should use the for loop, I appreciate your advise).
I wrote $year =! "" in the leap year February if block because I want to evade the empty default values from being counted.
I warmly welcome your suggestions and corrections, please help me out :)
Thanks in advance!

A: As far as I know about leap year:
/**
 * In the Gregorian calendar 3 criteria must be taken into account to identify leap years:
 *  1. The year is evenly divisible by 4;
 *  2. If the year can be evenly divided by 100, it is NOT a leap year, unless;
 *  3. The year is also evenly divisible by 400. Then it is a leap year.
 *
 * @param $year integer
 * @return bool
 */
private static function isLeapYear($year)
{
    // the rule was applied after 1582
    if ($year > 1582) {
        if (($year % 400 == 0 && $year % 100 != 0) || ($year % 4 == 0))
        {
            //"It is a leap year"
            return true;
        }
        else
        {
            return false;
            //"It is not a leap year"
        }
    }
    return false;
}

A: This won't work:
( $month == 1 || 3 || 5 || 7 || 8 || 10 || 12 )

This will only check that $month == 1, then the other conditions will be evaluated to true because the integers are evaluated to true (and PHP can't compare a variable to booleans with this code).
It looks like you're trying to get the number of days per month. This can be done easily with DateTime::Format() that use the date() format:
t returns the Number of days in the given month:
$dt = new Datetime('2016-02-15');
print $dt->format('t');
// display "29"

Example.
L returns the 1 if a year is leap, 0 otherwise:
$dt = new Datetime('2016-02-15');
print $dt->format('L');
// display "1"
$dt = new Datetime('2015-02-15');
print $dt->format('L');
// display "0"

Example.
You should probably don't care about the inputs and display all the years, the 12 months and 31 days per month and check the date with checkdate() once the data have been submitted.


text:
Q: React createRef() does not trigger on Netlify Deployment I am implementing an invisible google recaptcha using the react-google-recaptcha package. Everything works fine locally, all is trigerred and works smoothly but when deployed on Netlify, when you click the button the flow stops on the recaptchaRef.current.execute().
These are the main parts of the code:

const recaptchaRef = React.createRef();

class App extends Component {
  triggerRecaptcha = () => {
    recaptchaRef.current.execute();
  }

  handleChange = () => {
    // verify recaptcha
  }

  render() {
    return (
      <React.Fragment>
        <ReCAPTCHA
          ref={recaptchaRef}
          sitekey={KEY}
          onChange={this.handleChange}
          size="invisible"
        />
        <button type="button" onClick={this.triggerRecaptcha}>Proceed</button>
      </React.Fragment>
    )
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>

Basically locally the handleChange will be trigerred. On Netlify it is not, and after debugging it seems that the issue on the deployed version is recaptchaRef.current.execute();
Any one has any clue what is happening?
Thanks

A: This involved Recaptcha admin console not being configured correctly. Sorry for time lost and thanks @UjinT34


text:
Q: MyBatis Dynamic Recursive WHERE clause I would like to expose to my frontend the ability to search using logical expressions that are specified at runtime (with restrictions to ensure performance, but that's another topic). That is, I would like to run queries with WHERE clauses like
WHERE ((email like 'test') AND (user_id=1 OR user_id>=1000)) OR (trans_id <100 AND session_id> 500)

without knowing the tree structure of the logic up front (desire #1). I would also like to use the #{} mechanism (which substitutes "?" in the SQL string and builds a list of parameters to pass to a PreparedStatement) to encapsulate user-provided values (desire #2). Finally, I would like to use XML query construction rather than SQLBuilder construction because I am working with a significant amount of code based on the former (desire #3).
Unfortunately, even though the task which I want Mybatis to perform is well within the bounds of possibility (it's just recursion over a small hierarchy of java objects!) the limitations of mybatis seem to conspire to prevent me from achieving #1, #2, and #3 simultaneously. Hopefully I'm just missing something. Here is what I have tried:
Strategy A: Limit user queries to "OR of ANDs" structure (abandon #1). Those of you familiar with DeMorgan's laws will note that any logical tree can be put into this form fairly easily -- however, the DeMorganated statements cannot then be fed to MyBatis cleanly because the leaf statements (the things which get ANDed together -- e.g. user_id=1) do not have a fixed number of #{} arguments and cannot therefore be the subject of a foreach. This is the strategy taken in MyBatis - how to create w dynamic WHERE Clause
Strategy B: Just insert the recursively constructed WHERE with ${} (abandon #2). I must instead sanitize my own user input (slightly concerning) and translate it into strings which will no doubt introduce subtle bugs where the string conversion and subsequent parsing by the SQL server behaves differently than the '?' mechanism which passes objects out-of-band to the JDBC driver (highly concerning).
Strategy C: Convert all my code to SQLBuilder construction (abandon #3). I don't like this approach because SQLBuilder doesn't seem particularly mature -- in particular, there's an open issue speculating about strategies for adding parenthesis support, which I would no doubt need. https://github.com/mybatis/mybatis-3/issues/362
Strategy D: Build the WHERE clause recursively, manually inserting ? marks and keeping track of arguments to pass to a PreparedStatement. Manually chunk this string into blocks containing one ? each and then iterate over them from the XML. This will work but is painfully ugly.
<foreach item="block" index="i" collection="oneParamBlocks" open="" separator="" close="">${block.pfx}#{block.param}${block.sfx}</foreach>

Strategy E (wishful thinking): there's a mechanism I am not aware of to perform ${whereClause} substitution where the whereClause can contain a few "?" characters and the corresponding objects are specified by packaging them with the string somehow (e.g. getWhereClause() returns an object containing both the string "WHERE user_name=? AND user_email=?" and an array of parameters {"Bob","bob@example.com"}).

This is where mybatis parses #{}: https://github.com/mybatis/mybatis-3/blob/2d079fdd397f8a801679f701188be6e05ee9fdf5/src/main/java/org/apache/ibatis/builder/SqlSourceBuilder.java
This is where mybatis parses ${}: https://github.com/mybatis/mybatis-3/blob/2d079fdd397f8a801679f701188be6e05ee9fdf5/src/main/java/org/apache/ibatis/parsing/PropertyParser.java
If none of you can find something I've missed I'll probably just code up something to let me implement Strategy E and send a pull request.

A: It turned out to be possible to "cleanly" convert the logic tree to a token list after all. Key observation: the only filters which directly contain unsanitized data requiring a filter-dependent number of #{} substitutions are leaf filters. Interior filters (AND and OR) can be implemented by a generic token type which drops strings into the query. This is what we wound up using:
<foreach item="tok" collection="filter.tokenRepresentation" open="" separator="" close=""><choose>
        <when test="tok.mybatisChooseType == 'str'">${tok.hardcodedGlueStr}</when>

        <when test="tok.mybatisChooseType == "<"">${tok.filter.validatedColumnName}<#{tok.filter.unsanitizedLiteral}</when>
        <when test="tok.mybatisChooseType == ">"">${tok.filter.validatedColumnName}>#{tok.filter.unsanitizedLiteral}</when>
        ...
 </choose></foreach>

The tokenRepresentation is constructed by straightforward depth first search. The lack of spacing between choose and foreach prevents two newlines from being inserted between every token.


text:
Q: Output Form To A TextArea And Copy The TextArea Output With Labels To ClipBoard Hope I could get some help from here which will help to complete my project.
Using JavaScript and HTML only, I would like to be able to fill out a form with a few fields, once we click the submit button, it should be able to output this complete form with the filled in content to a textarea box or even a div container and then be able to copy that output div container or textarea.
I tried to fiddle around on it on jsfiddle but am no luck on this, it keeps throwing some error. Here is the JSFiddle I was trying to play with.
function createIt() {
    var Name = document.thisForm.name.value;
    var Info = document.thisForm.info.value;

    alert(Name + Info);

    outputValue = +Name + Info;
    document.thisForm.outputArea.value = outputValue;
}

A: I have corrected your code a bit.
for example: placed document.getElementById("Name") instead of what you wrote.
I have placed a working example in JSBIN , click here


text:
Q: Is there a keystroke combination like "WIN + Left-arrow" In Linux (openSUSE)? Is there a keystroke combination like "WIN + Left-arrow" in Linux (openSUSE), which moves the active window to the right half of the current screen?
Edit:
The solution I found is:

*

*Download "ctrlwm" from http://gtk-apps.org/content/show.php/ctrlwm?content=114565

*Open the Control Panel of openSUSE, click on "Regional and Accessiblity", and click on "Keyboard shortcuts"

*Add a shortcut with "meta + left" and point it to ctrlwm with the additional parameters -z -u

A: This is an older question, so the solution I'm about to describe may not have been available back when this was first asked.
In OpenSUSE 12.3 (KDE), one can do the following:

*

*Alt+F2, type "systemsettings" and hit Enter.

*Click on "Shortcuts and Gestures", select "Global Keyboard Shortcuts" from the pane at the left, and select "KWin" (the KDE default window manager) from the "KDE component" drop-down menu.

*Scroll down until you see in the "Action" column the actions "Quick Tile Window to the Left [/Right]"
These are the actions you want to map to the Meta+Left / Meta+Right keybindings -- just click on the actions, input the mappings, and enjoy.

A: It depends on the window manager you're using, not your flavor of Linux. I'm assuming you use KDE on OpenSuse.
If you're running compiz, I understand it's quite easy: http://ubuntuforums.org/showthread.php?t=1325473
Also, check this out: http://gtk-apps.org/content/show.php/ctrlwm?content=114565


text:
Q: How can I change this "number" var so it displays a binary number not a "System Tostring"? Sorry for the spaghettiOs!
I am trying to do the Adventofcode tasks and this one got me stuck for a bit. I am a newbie into programming but trying my best to finish this task so probably I am doing everything by force.
Can u guys help me or point into a right direction?
    static void Main()
    {
        string path = "plik.txt";
        string[] diagnostics = File.ReadAllLines(path);

        int diagSize = diagnostics.Length;
        string[] number = new string[12];

        //  int result = Convert.ToInt32(diagnostics[i], 2);
        for (int j = 0; j < 12; j++)
        {
            int ones = 0;
            int zeroes = 0;
            for (int i = 0; i < diagSize; i++)
            {
                string temp = diagnostics[i];

                if (temp[j] == '1')
                {
                    ones++;
                }
                else zeroes++;
            }
            if(ones > zeroes)
            {
                number[j] = "1";
            }
            else
            {
                number[j] = "0";
            }
            Console.WriteLine(number[j]);
        }
        Console.WriteLine(number);
    }

A: As suggested on the first comment you received, you should first produce a string from the array you previously built, like this:
string result = String.Join("", number);

Now you can easily print that string or just convert it to decimal if you need it as a number to further process.


text:
Q: Django создать модель ForeignKey Имею модель Post для блога:
сlass Post(models.Model):
..............
..............

    author = models.ForeignKey(User,
                               related_name="blog_posts",
                               on_delete=models.CASCADE)

Хочу подключить фильтр по тегам, написал такую модель:
class Tag(models.Model):
    tag_name = models.CharField(max_length=20,
                            blank=True)

    def __str__(self):
        return self.tag_name

Добавил в Post:
tag = models.ForeignKey(Tag, related_name="blog_tag",
                        on_delete=models.CASCADE)

при makemigrations выдает ошибку:
You are trying to add a non-nullable field 'tag' to post without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit, and let me add a default in models.py

И собственно вопрос как сделать field 'tag' nullable? Уже добавил пару постов в блог, их можно и удалить и создавать посты заново , но интересно как решить это проблему не удаляя постов?

A: У Вас поле tag не может иметь значение null. Варианты решения Django-умняшка Вам написал:

*

*или установите одноразовое значение (выбираете 1 и вводите желаемое значение, затем пишите yes и нажимаете Enter),

*или добавьте к полю дефолтное значение default='MY_TAG':

Еще есть вариант добавить к полю null=True, а затем выполнять makemigrations
В документации есть примеры.
Удалять ничего не нужно.