text:
Q: what should be the approach for pdf generation along with mvc and webapi? My scenario is I have to make available download link for web, android mobile app and tablet app. where as android and tablet app developers accessing my dot net web API. my web application is in MVC. so what should be approach i should use for this scenario? How can I response as PDF file from my web API ? it is possible?
 Edited 1:
    byte[] urlContents = response1.Content.ReadAsByteArrayAsync().Result;
                string s = GetString(urlContents);
                iTextSharp.text.Document document = new iTextSharp.text.Document();
                MemoryStream stream = new MemoryStream();

                try
                {
                    PdfWriter pdfWriter = PdfWriter.GetInstance(document, stream);
                    pdfWriter.CloseStream = false;

                    document.Open();
                    document.Add(new Paragraph(s));
                }
 return File(stream, "application/pdf", "DownloadName.pdf");.
---------------------------------------------------------------------
    Edit 2
    var document = new iTextSharp.text.Document(PageSize.A4);
                document.SetMargins(20.0F, 20.0F, document.TopMargin + 20, document.BottomMargin);

                var output = new MemoryStream();
                var writer = PdfWriter.GetInstance(document, output);
                document.Open();

                StringReader strRdr = new StringReader(htmlString);
                var parsedHtmlElements = HTMLWorker.ParseToList(strRdr, null);
                foreach (var htmlElement in parsedHtmlElements)
                    document.Add(htmlElement as IElement);

                writer.Flush();
                output.Seek(0, SeekOrigin.Begin);
                //document.Close();
                return File(output, "application/pdf","somefilename");

A: To return a PDF from a ASP.net WebAPI project (Or any file for that matter) you can return the bytes via byte array in a HttpResponseMessage while setting the ContentDisposition header to "attachment".
For example:
    //Web API Controller GET
    public HttpResponseMessage Get()
    {
            //Do something here to generate your pdf
            byte[] fileToReturn = pdf.GenerateImagePDF();

            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new ByteArrayContent(fileToReturn);
            response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = "SomeFileName";

            return response;
    }

    // MVC Controller Action - Reading the PDF from WebAPI and returning to browser
    public ActionResult SomeAction() {
        HttpResponseMessage response = httpClient.GetAsync([BuildYourURLtoWebAPIHere]).Result;
        Task<Stream> streamTask = response.Content.ReadAsStreamAsync();
        Stream stream = streamTask.Result; //blocks until Task is completed
        byte[] bytes;
        using (BinaryReader br = new BinaryReader(stream))
        {
            bytes = br.ReadBytes((int)stream.Length);
        }
         return File(bytes, "application/pdf");
   }

If your question was really about generating the actual PDF, then you should search for the many free or commercial PDF libraries.


text:
Q: Tensorflow probability MCMC with progress bar I am trying to sample from a custom distribution using tfp's No-U-Turn sampler (in jax). I want to show a progress bar, so I tried to draw the samples in a loop, each time initializing the chain with the samples from the last iteration. But I seem to do it wrong, as I get many duplicate samples. (Also, a side question, what are the differences between the parallel_iteraions in the NoUTurnSampler and the sample_chain function, and how does it differ from initializing the state with a batch dimension > 1?)
Here is my code so far :
@partial(jit, static_argnums=(2,3,4,5))
def run_chain(key, initial_state, logpdf, ndim, dt, parallel_iterations):

    kernel = NoUTurnSampler(logpdf, dt, parallel_iterations=parallel_iterations)
    samples, log_probs = sample_chain(num_results=1,
                                  current_state=initial_state,
                                  kernel=kernel,
                                  trace_fn=lambda _, results: results.target_log_prob,
                                  num_burnin_steps=burning_samples,
                                  num_steps_between_results=0,
                                  parallel_iterations=parallel_iterations,
                                  seed=key)

    return samples.reshape(-1, ndim)

# Initial_samples
samples = (random.uniform(key, (n_samples, ndim,)) - 1 / 2)
all_samples = []
for _ in tqdm.trange(int(np.ceil(n_samples / parallel_chains))):
    samples = run_chain(split_key, samples, logpdf_vec, ndim, dt, parallel_chains)
    rng_key, split_key = jax.random.split(rng_key)
    all_samples.append(samples)

samples = np.concatenate(all_samples) # Seems to have many duplicate samples

redpajama_set_name: RedPajamaStackExchange


text:
Q: Adding some HTML to a div with jQuery I have this HTML

and depending on the viewport width, I'd like to add either a small or a large image to the div, so that it will look like this:

Here's some jQuery I found somewhere on the web which seems to do exactly what I want. The only part where I'm stuck now is the img src="....jpg" thing. How can I translate this into jQuery?
jQuery(document).ready(function() {
    $(window).resize(function() {
        var window_width = $(document).width();
        if (window_width <= 1024) {

            img src = "small.jpg"

        } else {

            img src = "large.jpg"

        }
    });
});

Thanks!

A: If you are creating a
$(window).resize(function() {
    var window_width = $(document).width();
    if (window_width <= 1024) {
        $(".picture").html("");
    } else {
        $(".picture").html("");
    }
});

or if you just want to assign a source,
$(window).resize(function() {
    var window_width = $(document).width();
    if (window_width <= 1024) {
        $(".picture").find("img").attr("src", "small.jpg");
    } else {
        $(".picture").find("img").attr("src", "large.jpg");
    }
});

A: You can check if div has img element updated its src else create new img element and append it.
jQuery(document).ready(function() {
    $(window).resize(function() {
        var window_width = $(document).width();
        var imgsrc = "large.jpg"
        if (window_width <= 1024) {
            imgsrc = "small.jpg"
        }
        //Picture element
        var picture = $('.picture');

        //Picture has img element update its src else append new img tag
        if (picture.find('img').length) {
            picture.find('img').attr('src', imgsrc );
        } else {
            picture.append('')
        }
    });
});

A: Try this code snippet:
jQuery(document).ready(function () {
    $(window).resize(function () {
        var window_width = $(document).width();
        var src = (window_width <= 1024) ? "small.jpg" : "large.jpg";
        $('.image').remove(); //Remove in order to avoid multiple images to be added.
        $('.picture').prepend($('', {src: src, class: 'image'}));
    });
});

A: Like this :
$('.picture').find('img').attr('src','small.jpg');

A: You'll need to update the attributes.
In jQuery, you'll do something like this to update the src:
$('') // create a new img node
  .attr('src', 'newsrc')  // set the src to your desired source
  .load(function(){
     this.appendTo('.picture');  // once the source has finished loading, append it to the

   });

For more information on this, check out this other Stack Overflow post:
Changing the image source using jQuery

A: In jquery you can use append
$('.picture').append('');

A: Cache both images up front in jQuery objects constructed with image elements pointing to their source.
Then, use your conditional statement to store the string for source and reference it to place the cached image element on the page.
jQuery(document).ready(function() {
    //cache images
    var imgSet = {
        "small.jpg" : $(''),
        "large.jpg" : $('')
    };
    $(window).resize(function() {
        var window_width = $(document).width();
        var imgsrc;
        if (window_width <= 1024) {
            imgsrc="small.jpg";
        }else {
            imgsrc="large.jpg"
        }
        //use cached image element
        $('.picture').empty().append(imgSet[imgsrc]);
   });
});


text:
Q: Estimate transaction log size for single transaction Today I have tried to execute an update operation on 50M+ rows, something like:
UPDATE [table] SET [Column] += 1;

after 30min of waiting I have seen that the log has started to filling itself, after another 30min the entire transaction rolled-back due to the log being full.
DB was in the simple recovery mode but this is meaningless as this was done as a single transaction. SQL Server on it's own couldn't predict that it will use the entire log size (lets say that it was locked size for this matter), but my question to you my dear experienced DBA's - could you predict that size? Is there any way to calculate such an estimation?

UPDATE:

Yes, I understand the process, I know I can batch, I know how to make it performing well also. My real question is how big the log size is going to be?
How would you calculate update on a simple table like:
CREATE TABLE [TABL] ([ID] int identity(1,1) PRIMARY KEY CLUSTERED, [X] int not null);

INSERT INTO [TABL] VALUES(1)
GO 1000

UPDATE [TABL]
SET [X] = 2;

On my test box, the update part of the operation was 6 pages large = 48KB.
When I repeated the same for 10k rows I received 144 pages of transaction log.
For 100k rows it was 1314 pages, for 1M it was 13970 pages. This shows that we can concider this as linear function (on the we have extra pages which has to be there anyway no matter did we update anything or not --> 2-3 pages).
Going back to the begeining I know that I could run a single batch on 5% of my total operation, check the page chenge content (as blogged by Paul Randal):
    DECLARE @Extent_ID              INT;
    DECLARE @Size_Total             BIGINT = 0;
    DECLARE @File_ID                INT;
    DECLARE @File_Size_Pages        INT;

    DECLARE @Log_Page_ID            INT;
    DECLARE @Log_Total              BIGINT = 0;
    DECLARE @Log_Total_Changed      BIGINT;
    DECLARE @DBCC_PAGE_String_Log   VARCHAR (200);

    DECLARE [files] CURSOR FOR

        SELECT [file_id], [size]
        FROM master.sys.master_files
        WHERE [type_desc] = N'ROWS'
        AND [state_desc] = N'ONLINE'
        AND [database_id] = DB_ID(@DB_Name);

    OPEN files;

    FETCH NEXT FROM [files] INTO @File_ID, @File_Size_Pages;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @Extent_ID = 0;
        SET @Size_Total += @File_Size_Pages / 8;    --| Convert size in pages to to extents

        WHILE (@Extent_ID < @File_Size_Pages)
        BEGIN

             SELECT @Log_Page_ID = @Extent_ID + 7;

             SELECT @DBCC_PAGE_String_Log = 'DBCC PAGE ([' + @DB_Name + '], ' + CAST (@File_ID AS VARCHAR) + ', ' + CAST (@Log_Page_ID AS VARCHAR) + ', 3) WITH TABLERESULTS, NO_INFOMSGS';

             TRUNCATE TABLE [msdb].[dbo].[DBCC_Page_Check];
             INSERT INTO [msdb].[dbo].[DBCC_Page_Check] EXEC (@DBCC_PAGE_String_Log);

             SELECT @Log_Total_Changed = SUM ([msdb].[dbo].[Extent_Change_Checks] ([Field]))
             FROM [msdb].[dbo].[DBCC_Page_Check]
             WHERE [VALUE] = '    MIN_LOGGED'
             AND [ParentObject] LIKE 'ML_MAP%';

             SET @Log_Total += @Log_Total_Changed;

             SET @Extent_ID += 511232;
      END
----------------------------------------------------------------------------------------------------------------------------

        FETCH NEXT FROM [files] INTO @File_ID, @File_Size_Pages;
    END;

    DROP TABLE [msdb].[dbo].[DBCC_Page_Check];
    CLOSE [files];
    DEALLOCATE [files];

but this still requires to make a change (even with rollback) before we could estimate anything at all. So again - is there anyone who knows how to estimate it better than the way above - up front?

A:
Is there any way to calculate such an estimation?

No. You cannot predict that. You should always do your updates or deletes in batches.

A: This should only be used for educational purposes. See Kin's comment about potential risk of runnig fn_dblog
This will probably not answer your question as there is no easy answer but it will (hopefully) put you in the right direction to understand what happens in the SQL Server Transaction Log and why it's not always easy to estimate its size.
Create a new TEST database in FULL recovery model and LOG growth to 1MB (so we have a good growth resolution) and run the following:
USE [TEST]
GO

/* set recovery model to full so we retain transactions in the log */
ALTER DATABASE [TEST] SET RECOVERY FULL WITH NO_WAIT
GO

/* prepare enviroment */
IF OBJECT_ID('tempdb..#logspace') IS NOT NULL
    DROP TABLE #logspace

IF OBJECT_ID('tempdb..#dblog') IS NOT NULL
    DROP TABLE #dblog

IF OBJECT_ID('dbo.test') IS NOT NULL
    DROP TABLE dbo.test
GO

CREATE TABLE #logspace (
    [Database Name] sysname,
    [Log Size (MB)] float,
    [Log Space Used (%)] float,
    [Status] bit
    )

/* shrink the log to 1MB */
DBCC SHRINKFILE (N'TEST_log' , 1)
GO

/* first log dump, this shold have just few records
CAREFUL fn_dblog is not documented! */
select *, step=1
into #dblog
from fn_dblog(null, null)

/* record log size, it will have 1MB */
INSERT INTO #logspace
EXEC ('DBCC SQLPERF(LOGSPACE)')
GO

/* dump some data into our TEST database.
   the below will create ~21MB table with 98692 rows */
begin transaction
    SELECT *, tcol=0
    into dbo.test
    from sys.messages

    /* get log size again, wonder how much data will be in the log
       if we have just created 21MB table? should be around 21MB +
       some extra stuff */
    INSERT INTO #logspace
    EXEC ('DBCC SQLPERF(LOGSPACE)')
    GO

    /* second dump of the log after table creation */
    insert into #dblog
    select *, step=2
    from fn_dblog(null, null)
    GO
commit transaction

/* switching database in SIMPLE mode will clear down the log.
   this is just me being lazy but it will prove the point */
ALTER DATABASE [TEST] SET RECOVERY SIMPLE WITH NO_WAIT
GO

/* shrink log again  to 1MB */
DBCC SHRINKFILE (N'TEST_log' , 1)
GO

/* 3rd dump of the log after switching to SMIPLE model.
   log should be empty-ish */
insert into #dblog
select *, step=3
from fn_dblog(null, null)

/* record log size, shold be 1MB */
INSERT INTO #logspace
EXEC ('DBCC SQLPERF(LOGSPACE)')
GO

/* now run update statement */
begin transaction
    UPDATE dbo.test
    SET tcol+=1

    /* record log size right after the update statement */
    INSERT INTO #logspace
    EXEC ('DBCC SQLPERF(LOGSPACE)')
    GO

    /* fourth dump of the log after the update statement */
    insert into #dblog
    select *, step=4
    from fn_dblog(null, null)
    GO
commit transaction

Now let's analyse what happened:

It tells me the following:

*

*The initial size of the log was 1MB

*The SELECT INTO statement recorded 24MB of information in the transaction log. This is close enough to the table size of 21MB.

*The log was shrunk back to 1MB

*The subsequent UPDATE recorded 46MB of data in the transaction log which is almost double the table size!

Now let's check what transactions we have recorded. Let's focus on step 4 - the UPDATE statement:
Running the following:
select *
from #dblog
where step = 4
and AllocUnitName = 'dbo.test'

Gives us exactly the number of updates rows:

However, step 4 contains a few more rows not directly related to dbo.test:
select count(*)
from #dblog
where step = 4

Gives us 98871
Now dump the #dblog table to physical so we can check the size of step 4
select *
into dbo.dblog_step4
from #dblog
where step = 4

which is 87MB of data! -- nowhere close to the 46MB of the log size.

Does this answer your question? probably not, does it help to understand what happens in the transaction log? I hope so. Does it make it clear why it's not easy to estimate the impact on the tlog? I hope so..
The best approach is to test test test test test test.....estimate UPDATE/DELETE/INSERT impact in a non-production environment and depending on results batch up the transaction or adjust production log size.


text:
Q: What cause so different results using background-images with different size? I've this code:

@import url(https://fonts.googleapis.com/css?family=Open+Sans:700,300);
.all-cards {
  margin: 0;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}

.card {
  position: relative;
  width: 256px;
  height: 384px;
  margin-left: 32px;
  margin-bottom: 32px;
  cursor: pointer;
  overflow: hidden;
  box-shadow: 1px 1px 4px 0 rgba(0, 0, 0, 0.05), 2px 4px 32px 0 rgba(0, 0, 0, 0.1), 0 0 64px 0 rgba(0, 0, 0, 0.2);
  font-family: 'Open Sans', Helvetica, sans-serif;
  font-weight: 300;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  transition: transform 300ms ease;
}

.card .card-image {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 70px;
  left: 0;
  background-position: center;
  background-size: cover;
  transition: bottom 300ms ease;
}

.card .card-content {
  position: absolute;
  z-index: 10;
  padding: 0.5rem;
  height: 100%;
  background: white;
  transform: translateY(314px);
  transition: transform 300ms ease;
}

.card .card-content .card-header {
  font-size: 1.5rem;
  font-weight: 700;
}

.card .card-content .card-meta {
  font-size: 0.8rem;
  color: #888;
  margin-bottom: 0.5rem;
}

.card .card-content .card-description {
  opacity: 0;
  transition: opacity 500ms ease 100ms;
}

.card:hover {
  transform: translateY(-16px);
}

.card:hover .card-image {
  bottom: 280px;
}

.card:hover .card-content {
  transform: translateY(104px);
}

.card:hover .card-content .card-description {
  opacity: 1;
}
<!DOCTYPE html>

  <meta charset="UTF-8">
  <title>Hover Cards</title>

  <link href="card.css" rel="stylesheet" type="text/css" media="all" />

          Cool Title

          By Mubanga

          Lorem ipsum, dolor sit amet consectetur adipisicing elit. Corporis maiores expedita exercitationem rerum, voluptate laudantium nostrum ratione aliquam adipisci, vitae soluta repellat cupiditate, inventore ipsa distinctio quidem assumenda voluptates eligendi.

          Cool Title

          By Mubanga

          Lorem ipsum, dolor sit amet consectetur adipisicing elit. Corporis maiores expedita exercitationem rerum, voluptate laudantium nostrum ratione aliquam adipisci, vitae soluta repellat cupiditate, inventore ipsa distinctio quidem assumenda voluptates eligendi.

          Cool Title

          By Mubanga

          Lorem ipsum, dolor sit amet consectetur adipisicing elit. Corporis maiores expedita exercitationem rerum, voluptate laudantium nostrum ratione aliquam adipisci, vitae soluta repellat cupiditate, inventore ipsa distinctio quidem assumenda voluptates eligendi.

If I change background-image:
background-image: url(http://www.example.com/image.jpg)

With a non fixed size image, codes not work anymore, overlapping div "card".
I don't understand why.
jsfiddle


text:
Q: Reactor Kafka: Exactly Once Processing Sample I've read many articles where there are many different configurations to achieve exactly once processing.
Here is my producer config:
final Map<String, Object> props = Maps.newConcurrentMap();

props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, JsonSerializer.class);

props.put(ProducerConfig.ENABLE_IDEMPOTENCE_CONFIG, "true");
props.put(ProducerConfig.ACKS_CONFIG, "all");
props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "producer-tx-1");

Here is my consumer config:
final Map<String, Object> props = Maps.newHashMap();

props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ConsumerConfig.GROUP_ID_CONFIG, consumerGroupId);
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, JsonDeserializer.class);

props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG, "false");
props.put(ConsumerConfig.ISOLATION_LEVEL_CONFIG, "read_committed");

I read this [sample scenarios][1]
And I try to follow but i'm getting some issues:
Here is my producer code:
    @Override
public Mono<SenderResult<Void>> buy(Message msg) {
    final ReactiveKafkaProducerTemplate kafkaProducerTemplate = kafkaConfig.getKafkaProducerTemplate();
    return kafkaProducerTemplate.transactionManager().begin().then(kafkaProducerTemplate.send(mytopic, msg));

}

My Consumer code:
@Override
public void run(ApplicationArguments arg0) throws Exception {
    final ReactiveKafkaProducerTemplate kafkaProducerTemplate = kafkaConfig.getKafkaProducerTemplate();
    final ReactiveKafkaConsumerTemplate kafkaConsumerTemplate = kafkaConfig.getKafkaConsumerTemplate(mytopic, Message.class);

    final Flux<ConsumerRecord<String, Message>> flux = kafkaConsumerTemplate.receiveExactlyOnce(kafkaProducerTemplate.transactionManager())
            .concatMap(receiverRecordFlux -> receiverRecordFlux );

    flux.subscribe(record -> {
        final Message message = record.value();

        System.out.printf("received message: timestamp=%s key=%d value=%s\n",
                dateFormat.format(new Date(record.timestamp())),
                record.key(),
                message);
 transactionService.processAndSendToNextTopic(message)
                .doOnSuccess(aVoid -> kafkaProducerTemplate.transactionManager().commit())
                .subscribe();

    });
}

I'm always getting following error when trying to produce and consume message:
Invalid transition attempted from state IN_TRANSACTION to state IN_TRANSACTION

A: See the javadocs for receiveExactlyOnce
/**
 * Returns a {@link Flux} of consumer record batches that may be used for exactly once
 * delivery semantics. A new transaction is started for each inner Flux and it is the
 * responsibility of the consuming application to commit or abort the transaction
 * using {@link TransactionManager#commit()} or {@link TransactionManager#abort()}
 * after processing the Flux.

begin() has already been called so you don't need to call it.
@Override
public Flux<Flux<ConsumerRecord<K, V>>> receiveExactlyOnce(TransactionManager transactionManager) {
    this.ackMode = AckMode.EXACTLY_ONCE;
    Flux<ConsumerRecords<K, V>> flux = withDoOnRequest(createConsumerFlux());
    return  flux.map(consumerRecords -> transactionManager.begin()
                             .then(Mono.fromCallable(() -> awaitingTransaction.getAndSet(true)))
                             .thenMany(transactionalRecords(transactionManager, consumerRecords)))
                             .publishOn(transactionManager.scheduler());
}


text:
Q: dict attribute 'type' to select Subclass of dataclass I have the following class
@dataclass_json
@dataclass
class Source:
    type: str =None
    label: str =None
    path: str = None

and the two subclasses:
@dataclass_json
@dataclass
class Csv(Source):
    csv_path: str=None
    delimiter: str=';'

and
@dataclass_json
@dataclass
class Parquet(Source):
    parquet_path: str=None

Given now the dictionary:
parquet={type: 'Parquet', label: 'events', path: '/.../test.parquet', parquet_path: '../../result.parquet'}

csv={type: 'Csv', label: 'events', path: '/.../test.csv', csv_path: '../../result.csv', delimiter:','}

Now I would like to do something like
Source().from_dict(csv)

and that the output will be the class Csv or Parquet. I understand that if you initiate the class source you just "upload" the parameters with the method "from dict", but is there any posibility in doing this by some type of inheritence without using a "Constructor" which makes a if-else if-else over all possible 'types'?
Pureconfig, a Scala Library, creates different case classes when the attribute 'type' has the name of the desired subclass. In Python this is possible?

A: You can build a helper that picks and instantiates the appropriate subclass.
def from_data(data: dict, tp: type):
    """Create the subtype of ``tp`` for the given ``data``"""
    subtype = [
        stp for stp in tp.__subclasses__()  # look through all subclasses...
        if stp.__name__ == data['type']     # ...and select by type name
    ][0]
    return subtype(**data)  # instantiate the subtype

This can be called with your data and the base class from which to select:
>>> from_data(
...     {'type': 'Csv', 'label': 'events', 'path': '/.../test.csv', 'csv_path': '../../result.csv', 'delimiter':','},
...     Source,
... )
Csv(type='Csv', label='events', path='/.../test.csv', csv_path='../../result.csv', delimiter=',')

If you need to run this often, it is worth building a dict to optimise the subtype lookup. A simple means is to add a method to your base class, and store the lookup there:
@dataclass_json
@dataclass
class Source:
    type: str =None
    label: str =None
    path: str = None

    @classmethod
    def from_data(cls, data: dict):
        if not hasattr(cls, '_lookup'):
            cls._lookup = {stp.__name__: stp for stp in cls.__subclasses__()}
        return cls._lookup[data["type"]](**data)

This can be called directly on the base class:
>>> Source.from_data({'type': 'Csv', 'label': 'events', 'path': '/.../test.csv', 'csv_path': '../../result.csv', 'delimiter':','})
Csv(type='Csv', label='events', path='/.../test.csv', csv_path='../../result.csv', delimiter=',')

A: This is a variation on my answer to this question.
@dataclass_json
@dataclass
class Source:
    type: str = None
    label: str = None
    path: str = None

    def __new__(cls, type=None, **kwargs):
        for subclass in cls.__subclasses__():
            if subclass.__name__ == type:
                break
        else:
            subclass = cls
        instance = super(Source, subclass).__new__(subclass)
        return instance

assert type(Source(**csv)) == Csv
assert type(Source(**parquet)) == Parquet
assert Csv(**csv) == Source(**csv)
assert Parquet(**parquet) == Source(**parquet)

You asked and I am happy to oblige.  However, I'm questioning whether this is really what you need.  I think it might be overkill for your situation.  I originally figured this trick out so I could instantiate directly from data when...

*

*my data was heterogeneous and I didn't know ahead of time which subclass was appropriate for each datum,

*I didn't have control over the data, and

*figuring out which subclass to use required some processing of the data, processing which I felt belonged inside the class (for logical reasons as well as to avoid polluting the scope in which the instantiating took place).

If those conditions apply to your situation, then I think this is a worth-while approach.  If not, the added complexity of mucking with __new__ -- a moderately advanced maneuver -- might not outweigh the savings in complexity in the code used to instantiate.  There are probably simpler alternatives.
For example, it appears as though you already know which subclass you need; it's one of the fields in the data.  If you put it there, presumably whatever logic you wrote to do so could be used to instantiate the appropriate subclass right then and there, bypassing the need for my solution.  Alternatively, instead of storing the name of the subclass as a string, store the subclass itself.  Then you could do this: data['type'](**data)
It also occurs to me that maybe you don't need inheritance at all.  Do Csv and Parquet store the same type of data, differing only in which file format they read it from?  Then maybe you just need one class with from_csv and from_parquet methods.  Alternatively, if one of the parameters is a filename, it would be easy to figure out which type of file parsing you need based on the filename extension.  Normally I'd put this in __init__, but since you're using dataclass, I guess this would happen in __post_init__.

A: Do you need this behavior?
from dataclasses import dataclass
from typing import Optional, Union, List

from validated_dc import ValidatedDC

@dataclass
class Source(ValidatedDC):
    label: Optional[str] = None
    path: Optional[str] = None

@dataclass
class Csv(Source):
    csv_path: Optional[str] = None
    delimiter: str = ';'

@dataclass
class Parquet(Source):
    parquet_path: Optional[str] = None

@dataclass
class InputData(ValidatedDC):
    data: List[Union[Parquet, Csv]]

# Let's say you got a json-string and loaded it:
data = [
    {
        'label': 'events', 'path': '/.../test.parquet',
        'parquet_path': '../../result.parquet'
    },
    {
        'label': 'events', 'path': '/.../test.csv',
        'csv_path': '../../result.csv', 'delimiter': ','
    }

]

input_data = InputData(data=data)

for item in input_data.data:
    print(item)

# Parquet(label='events', path='/.../test.parquet', parquet_path='../../result.parquet')
# Csv(label='events', path='/.../test.csv', csv_path='../../result.csv', delimiter=',')

validated_dc: https://github.com/EvgeniyBurdin/validated_dc


text:
Q: List sort based on another list I have two generic list objects, in which one contains ids and ordering, and the other a bunch of ids with each id in the second list having an id reference to the first list, for example;
public class OptionType
{
    public int ID { get; set; }
    public int Ordering { get; set; }
}

public class Option
{
    public int ID { get; set; }
    public int Type_ID { get; set; }
}

Obviously I can do a simple sort on a list of OptionTypes by doing
types_list.OrderBy(x => x.Ordering);

Question is though, how could I go about ordering an 'options_list' by utilising the 'Type_ID' on the object which would relate to the ordering of the types_list. As in something like (obviously this isn't valid - but hopefully you will get the idea!)
options_list.OrderBy(x => x.Type_ID == types_list.OrderBy(e => e.Ordering));

A: You should be able to use a join to produce your desired output. Example using query syntax.
var orderedOptions = from option in options_list
                     join type in types_list
                     on option.Type_ID equals type.ID
                     orderby type.Ordering
                     select option;

A: List.FindIndex() is your friend when listA is small and already sorted:
var orderedB = listB.OrderBy(b => listA.FindIndex(a => a.id == b.id));

Working example: https://dotnetfiddle.net/CpLeFU
As @goodeye pointed out in the comments, performance will be a nightmare on larger lists. Use the accepted answer in that case.

A: I like Lambda syntax, so I came up with this equivalent. I can see how query syntax is cleaner for joins.
var orderedOptions = options_list
    .Join(
        types_list,
        option => option.Type_ID,
        type => type.ID,
        (option, type) => new { Option = option, Type = type })
    .OrderBy(x => x.Type.Ordering)
    .Select(x => x.Option);

For a slight reduction (of what, I'm not sure), this creates the new object with just the Ordering property, instead of the entire Type class. Not much different here, but I had a large class with the sort data, and only need the sorting property. Don't know if it mattered, but it was clearer to read.
var orderedOptions = options_list
    .Join(
        types_list,
        option => option.Type_ID,
        type => type.ID,
        (option, type) => new { Option = option, Ordering = type.Ordering })
    .OrderBy(x => x.Ordering)
    .Select(x => x.Option);

It looks like the query syntax lets you order within the initial query, while lambda requires ordering after the join creates the new object. Maybe they're really doing the same thing under the covers though: creating the joined object, to be sorted then selected.

A: You can always use a more structured way by implementing the IComparer interface with:
public class OptionComparer : IComparer<Option>
{
    // <Id, Sort>
    private readonly Dictionary<int, int> _orderedOptionTypeIds;

    public OptionComparer(IEnumerable<OptionType> optionTypes)
    {
        _orderedOptionTypeIds = optionTypes
            .OrderBy(x => x.Ordering) //If its not sorted already
            .ToDictionary(x => x.ID, x => x.Ordering);
    }

    public int Compare(Option x, Option y)
    {
        int xIndex, yIndex;

        if (!_orderedOptionTypeIds.TryGetValue(x.Type_ID, out xIndex))
        {
            xIndex = int.MaxValue;
        }

        if (!_orderedOptionTypeIds.TryGetValue(y.Type_ID, out yIndex))
        {
            yIndex = int.MaxValue;
        }

        return xIndex.CompareTo(yIndex);
    }
}

Then the usage may be:
IEnumerable<OptionType> optionTypes = Get(...);

options_list.OrderBy(x => x, new OptionComparer(optionTypes));


text:
Q: Bootstrap CSS inherit height from sibling div I am a backend guy trying to do some minimum css and I am struggling with this problem, I have this html code:

Price Table

        <select id="pricing-level">
            {% for in_pricing_level in pricing_levels %}
                <option value="{% url 'catalogue:table' product_type in_pricing_level.0 %}" {% if in_pricing_level.0 == pricing_level %}selected{% endif %}>{{ in_pricing_level.1 }}</option>
            {% endfor %}
        </select>

Problem:
The dropdown box is wrapped to a new line, which results in

What I have tried is put them in to 2 columns:

Price Table

>
        <select id="pricing-level">
            {% for in_pricing_level in pricing_levels %}
                <option value="{% url 'catalogue:table' product_type in_pricing_level.0 %}" {% if in_pricing_level.0 == pricing_level %}selected{% endif %}>{{ in_pricing_level.1 }}</option>
            {% endfor %}
        </select>

But this results in dropdown box having minimum height and cannot be vertically center aligned with "Price Table"

While I can manually put in some padding/margin, is there a builtin way(ie without js) to make the dropdown div inherit height from the "Price Table" div?

A: Is this what you looking for?

Price Table

        <select id="pricing-level" >
         {% for in_pricing_level in pricing_levels %}
                <option value="{% url 'catalogue:table' product_type in_pricing_level.0 %}" {% if in_pricing_level.0 == pricing_level %}selected{% endif %}>{{ in_pricing_level.1 }}</option>
            {% endfor %}
        </select>

A: Wrap the "Price Table" div and the dropdown menu in a .col and vertically align it using:
.retail-menu {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}


text:
Q: NSDocumentController closeAllDocumentsWithDelegate does not call canCloseDocumentWithDelegate of my NSPersistentDocument subclass According to Apple's docs, closeAllDocumentsWithDelegate (from NSDocumentController) should call canCloseDocumentWithDelegate of NSDocument for all open documents if you quit an application.
In my NSPersistentDocument-based app I need to override canCloseDocumentWithDelegate in order to warn the user in case a certain server functionality is still running when the document closes. This has nothing to do with any data changes.
This works when the user closes a single document; I can present a sheet with the warning and let the user cancel the closing process.
However, my version of canCloseDocumentWithDelegate does not get called when the app is quit. What could be the reason for that?

A: According to Apple Developer Technical Support this is a known issue. I finally cut the automatic wiring of the app's quit menu item and handle it all by myself. I needed to make the server functionality information of the document available from the outside (in this example the state of optionButton) and added this function to the AppDelegate:
- (IBAction)terminateGracefully:(id)sender {
    BOOL optionOn = FALSE;
    for (Document *doc in NSApp.orderedDocuments) {
        if (doc.optionButton.state == NSControlStateValueOn) {
            optionOn = TRUE;
        }
    }

    if (optionOn) {
        NSAlert *alert = [[NSAlert alloc] init];
        [alert setMessageText:@"Checkbox in some window is on"];
        [alert setInformativeText:@"Something is going on. If you close the app now, it will stop. Close anyway?\n"];
        [alert setAlertStyle:NSAlertStyleCritical];
        [alert addButtonWithTitle:@"Don't close"];
        [alert addButtonWithTitle:@"Close anyway"];

        NSModalResponse resp = [alert runModal];

        if (resp == NSAlertSecondButtonReturn) {
            // We really want to close
            [NSApp terminate:sender];
        }

    } else {
        [NSApp terminate:sender];
    }
}

Then I bound the app's quit menu item to terminateGracefully:


text:
Q: XML to csv conversion using python xml library Sorry if this question is already asked.
I have below XML file for which i want to convert it into CSV or excel.
Here i want to extract NodeName and its child DestIPAddress under IpRoutelist. and Value under Custom/Name tag
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <EnbConfigGetResponse xmlns="http://Airspan.Netspan.WebServices">
            <EnbConfigGetResult>
                <ErrorCode>OK</ErrorCode>
                <NodeResult>
                    <NodeResultCode>OK</NodeResultCode>
                    <NodeName>IMUMB0899</NodeName>
                    <NodeDetail>
                        <Custom>
                            <Name>Circle</Name>
                            <Value>MU</Value>
                        </Custom>
                        <Custom>
                            <Name>GW VLAN 601</Name>
                            <Value>2405:200:101::</Value>
                        </Custom>
                        <Custom>
                            <Name>GW VLAN 602</Name>
                            <Value>2405:200:104::</Value>
                        </Custom>
                    </NodeDetail>
                    <EnbConfig>
                        <Name>IMUMB0899</Name>
                        <Hardware>1000 SFP</Hardware>
                        <Description>TT</Description>
                        <Site>DND</Site>
                        <Region>DND</Region>
                        <Altitude>0</Altitude>
                        <NbifEventAlarmForwarding>Enabled</NbifEventAlarmForwarding>
                        <ENodeBType>Macro</ENodeBType>
                        <ENodeBID>397063</ENodeBID>
                        <M1SubnetMask>120</M1SubnetMask>
                        <IpRouteList>
                            <IpRoute>
                                <DestIpAddress>172.172.6.20</DestIpAddress>
                                <IpSubnetMask>255.255.255.255</IpSubnetMask>
                                <GatewayIpAddress>172.21.200.1</GatewayIpAddress>
                            </IpRoute>
                            <IpRoute>
                                <DestIpAddress>2405:20:1::</DestIpAddress>
                                <IpSubnetMask>40</IpSubnetMask>
                                <GatewayIpAddress>2405:20:101:4:7:2:61:1</GatewayIpAddress>
                            </IpRoute>
                        </IpRouteList>
                <NodeResult>
     </EnbConfigGetResult>
        </EnbConfigGetResponse>
    </soap:Body>
</soap:Envelope>

I tried below code which extract the Name and IProute, but when i try to merge tham only one IP route i get against NodeName but two are available.
from bs4 import BeautifulSoup
import pandas as pd
import lxml
import xml.etree.cElementTree
import openpyxl
import inspect
import os

sites = "xml"

with open(sites, "r",encoding='unicode_escape') as f:
    xml_data = f.read()

soup = BeautifulSoup(xml_data, "xml")

tag1 = input("Enter tagname1:")
tag2 = input("Enter tagname2:")

data = []
dd = []

for td in soup.find_all(tag1):
    data.append({"NodeName": td.text})

for iproute in soup.find_all(tag2):
    dd.append({"IpRoute": iproute.text})

df1 = pd.DataFrame(data)
df2 = pd.DataFrame(dd)

df = pd.merge(df1,df2,left_index=True, right_index=True)

df.to_excel(sites + '.xlsx', sheet_name='Detail', index = False)
print("*************Done*************")

Expected Output:

A: Another method.
from simplified_scrapy import SimplifiedDoc, utils, req
# xml = utils.getFileContent('file.xml')
xml = ''' Your xml string'''

doc = SimplifiedDoc(xml)
lstNodeResult = doc.selects('NodeResult')
data = [['NodeName','DestIpAddress','GatewayIpAddress','value1','value2','value3']]
for result in lstNodeResult:
    lstCustom = result.selects('NodeDetail>Custom')
    if lstCustom:
        lstCustom = lstCustom.Value.text
    NodeName = result.NodeName.text
    lstIpRoute = result.IpRoutes
    for IpRoute in lstIpRoute:
        row = [NodeName,IpRoute.DestIpAddress.text,IpRoute.GatewayIpAddress.text]
        if lstCustom: row.extend(lstCustom)
        data.append(row)
# print (data)
utils.save2csv('test.csv',data)

# Or
data = {
    'NodeName':lstNodeResult.NodeName.text,
    'DestIpAddress':lstNodeResult.select('IpRoute>DestIpAddress>text()'),
    'GatewayIpAddress':lstNodeResult.select('IpRoute>GatewayIpAddress>text()')
}
# print (data)

Result:
.......
I-MU-NVMB-OSC-0900-SMC004,2405:200:310:5a::,2405:200:101:500:7:2:602:3503
I-MU-NVMB-OSC-0900-SMC004,2405:200:310:1::,2405:200:101:500:7:2:602:3503
I-MU-NVMB-ISC-0181-SWC0002,2405:200:310:1::,2405:200:101:500:7:2:602:5d03,MU,2405:200:101:500:7:2:601:5d03,2405:200:101:500:7:2:602:5d03
I-MU-NVMB-ISC-0181-SWC0002,2405:200:311:2::,2405:200:101:500:7:2:602:5d03,MU,2405:200:101:500:7:2:601:5d03,2405:200:101:500:7:2:602:5d03
I-MU-NVMB-ISC-0181-SWC0002,2405:200:310:a152::,2405:200:101:500:7:2:602:5d03,MU,2405:200:101:500:7:2:601:5d03,2405:200:101:500:7:2:602:5d03
.......

Here are more examples: https://github.com/yiyedata/simplified-scrapy-demo/tree/master/doc_examples
Delete blank lines.
def delEmptyRows(name, encoding="utf-8"):
    lines = utils.getFileLines(name, encoding=encoding)
    lines = [line for line in lines if line.strip()!='']
    utils.saveFile(name, "".join(lines), encoding=encoding)
# use
delEmptyRows('test.csv')

Method of obtaining IBridge2RfStatsRow data.
from simplified_scrapy import SimplifiedDoc, utils, req
xml = req.get('https://pastebin.com/raw/SWhDM1zq')

rows = []
doc = SimplifiedDoc(xml)
lstIBridge2RfStatsRow = doc.selects('IBridge2RfStatsRow').children
# Add header
header = []
for col in lstIBridge2RfStatsRow[0]:
    header.append(col.tag)
rows.append(header)

# Generate line
for row in lstIBridge2RfStatsRow:
    rows.append([col.text for col in row])

utils.save2csv('IBridge2RfStatsRow.csv',rows)

A: There are certain things you have missed to consider in your code - like you are just doing a soup.find_all('NodeName') - which gives all nodenames present in the xml and soup.find_all('DestIPAddress') gives all destipaddresses present in the xml - how will you map them if you do it that way ?
Try this code, I have made outer loop to go through each noderesult - and inner loop to go through each destipaddress , i am assuming that nodename will be single for each noderesult hence using that same name for every destipaddress found in a particular noderesult :
tag1 = "NodeName"
tag2 = "DestIpAddress"

tag1_list = []
tag2_list = []

for node_res in soup.find_all('NodeResult'):

  for tg2 in node_res.find_all(tag2):
      tag1_list.append(soup.find(tag1).text)
      tag2_list.append(tg2.text)

print(tag1_list)
print(tag2_list)

df = pd.DataFrame({
  'NodeNames': tag1_list,
  'IpRoute': tag2_list
})

print(df)

DF-output :
   NodeNames       IpRoute
0  IMUMB0899  172.172.6.20
1  IMUMB0899   2405:20:1::

You can add the write-excel line in the end .

A: Since you are dealing with xml and importing lxml, you might as well use it with xpath.
Note that the xml in your question is invalid (even after the editing) and this answer assumes the example was properly edited to be valid.
from lxml import etree
import pandas as pd

soap = """[your xml above, fixed]"""
doc = etree.XML(soap.encode())
columns = ['NodeName','DesIPAdd','GIPAdd','Value1','Value2','Value3']
ns = {'xx' : "http://Airspan.Netspan.WebServices"}
nodes = doc.xpath('//xx:NodeResult', namespaces=ns)
rows = []
for node in nodes:
    sub_nodes = node.xpath('//xx:EnbConfig/xx:IpRouteList/xx:IpRoute', namespaces=ns)
    for sub_node in sub_nodes:
        row = []
        row.append(node.xpath('//xx:NodeName/text()', namespaces=ns)[0])
        row.append(sub_node.xpath('.//xx:DestIpAddress/text()', namespaces=ns)[0])
        row.append(sub_node.xpath('.//xx:GatewayIpAddress/text()', namespaces=ns)[0])
        row.extend(node.xpath('//xx:NodeDetail/xx:Custom/xx:Value/text()', namespaces=ns))
        rows.append(row)
pd.DataFrame(rows,columns=columns)

Output:
      NodeName    DesIPAdd      GIPAdd              Value1  Value2      Value3
0   IMUMB0899   172.172.6.20    172.21.200.1           MU   2405:200:101::  2405:200:104::
1   IMUMB0899   2405:20:1::     2405:20:101:4:7:2:61:1  MU  2405:200:101::  2405:200:104::


text:
Q: Java - Read text file by chunks I want to read a log file in different chunks to make it multi threaded. The application is going to run in a serverside environment with multiple hard disks.
After reading into chunks the app is going to process line per line of every chunk.
I've accomplished the reading of every file line line with a bufferedreader and I can make chunks of my file with RandomAccessFile in combination with MappedByteBuffer, but combining these two isn't easy.
The problem is that the chunk is just cutting into the last line of my chunk. I never have the whole last line of my block so processing this last log-line is impossible. I'm trying to find a way to cut my file into variable-length chunks respecting the end of the lines.
Does anyone have a code for doing this?

A: You could find offsets in the file that are at line boundaries before you start processing the chunks. Start with the offset by dividing the file size by the chunk number and seek until you find a line boundary. Then feed those offsets into your multi-threaded file processor. Here's a complete example that uses the number of available processors for the number of chunks:
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class ReadFileByChunks {
    public static void main(String[] args) throws IOException {
        int chunks = Runtime.getRuntime().availableProcessors();
        long[] offsets = new long[chunks];
        File file = new File("your.file");

        // determine line boundaries for number of chunks
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        for (int i = 1; i < chunks; i++) {
            raf.seek(i * file.length() / chunks);

            while (true) {
                int read = raf.read();
                if (read == '\n' || read == -1) {
                    break;
                }
            }

            offsets[i] = raf.getFilePointer();
        }
        raf.close();

        // process each chunk using a thread for each one
        ExecutorService service = Executors.newFixedThreadPool(chunks);
        for (int i = 0; i < chunks; i++) {
            long start = offsets[i];
            long end = i < chunks - 1 ? offsets[i + 1] : file.length();
            service.execute(new FileProcessor(file, start, end));
        }
        service.shutdown();
    }

    static class FileProcessor implements Runnable {
        private final File file;
        private final long start;
        private final long end;

        public FileProcessor(File file, long start, long end) {
            this.file = file;
            this.start = start;
            this.end = end;
        }

        public void run() {
            try {
                RandomAccessFile raf = new RandomAccessFile(file, "r");
                raf.seek(start);

                while (raf.getFilePointer() < end) {
                    String line = raf.readLine();
                    if (line == null) {
                        continue;
                    }

                    // do what you need per line here
                    System.out.println(line);
                }

                raf.close();
            } catch (IOException e) {
                // deal with exception
            }
        }
    }
}

A: You need to let your chunks overlap. If no lines are longer than a block, then a one block overlap is enough.
Are you sure you need a multithreaded version?
Is the performance of gnu grep not good enough?
The implementation of gnu grep has solved the problem with lines that cross the chunk border. If you aren't bothered with the GNU License you can probably borrow ideas and code from there. It is a very efficient single-threaded implementation.


text:
Q: SQLiteDatabase db = this.getWritableDatabase() throws error when broadcast received I'm trying to retrieve some table entries from a database when a broadcast is received (in this case its Connectivity Change).
I've traced the error to the
SQLiteDatabase db = this.getWritableDatabase(); line in the getAllData() method
Here's the code:
sendMessageFromDatabase:
    import android.content.Context;
    import android.database.Cursor;
    import android.os.Handler;
    import android.util.Log;

    import com.loopj.android.http.AsyncHttpClient;
    import com.loopj.android.http.JsonHttpResponseHandler;
    import com.loopj.android.http.RequestParams;
    import com.pusher.client.Pusher;
    import com.pusher.client.PusherOptions;
    import com.pusher.client.channel.Channel;
    import com.pusher.client.channel.SubscriptionEventListener;

    import org.json.JSONObject;

    import java.util.ArrayList;

    import cz.msebera.android.httpclient.Header;

    import static android.content.ContentValues.TAG;

    public class sendMessageFromDatabase {

        private static final String TAG = "SendingMFD DB";

        private final Handler handler;

        final String MESSAGES_ENDPOINT = "http://fragmentstanvir.azurewebsites.net";

        DataBaseHelper myDB;
        Context context;

        public sendMessageFromDatabase(Handler handler) {

            this.handler = handler;
            RequestParams rp = new RequestParams();
            SendMessageFromDBMethod(rp);
        }

        public void SendMessageFromDBMethod(RequestParams param) {

            Log.d(TAG, "Method started");

            AsyncHttpClient client = new AsyncHttpClient();

            //pusher
            PusherOptions options = new PusherOptions();
            options.setCluster("ap2");
            Pusher pusher = new Pusher("87ded5c4cb1e22c46dd4", options);

            Channel channel = pusher.subscribe("my-channel");

            channel.bind("my-event", new SubscriptionEventListener() {
                @Override
                public void onEvent(String channelName, String eventName, final String data) {
                    System.out.println(data);
                }
            });

            pusher.connect();

            RequestParams postParams = new RequestParams();

            // Database
            Log.d(TAG, "DATABASE OPERATIONS STARTED");

            myDB = new DataBaseHelper(context);

            Cursor res = myDB.getAllData();

            if (res.getCount() == 0) {
                Log.d(TAG, "No data found");
                return;

            }

            final ArrayList<String> messageList = new ArrayList();

            while (res.moveToNext()) {
                messageList.add(res.getString(0));
            }

            Log.d(TAG, "BUFFER LENGTH :: " + messageList.size());

            for (int i = 0; i < messageList.size(); i++) {

                client.post(MESSAGES_ENDPOINT + "/messages" + messageList.get(i).toString(), postParams, new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                Log.d(TAG, "fuck the log");
                            }
                        });
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                        //Toast.makeText(, "Something went wrong :(", Toast.LENGTH_LONG).show();
                    }
                });

            }

            return;

        }

    }

DataBaseHelper.java::
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

    public static final String DATABASE_NAME = "messages.db";
    public static final String TABLE_NAME = "kalol";
    public static final String COL_1 = "params";
    SQLiteDatabase db;
    private Context mContext;

    public DataBaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
        mContext = context;
        db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_NAME + " (PARAMS TEXT UNIQUE) ");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String params){
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1,params);
        long result = db.insert(TABLE_NAME,null, contentValues);
        if (result == -1)
            return false;
        else
            return true;
    }

    public Cursor getAllData(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor res = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
        return res;
    }

}

Logcat :
04-06 00:17:00.999 22916-22916/com.tanvirsingh.fragmentsdemo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.tanvirsingh.fragmentsdemo, PID: 22916
                                                                               Theme: themes:{default=overlay:system, iconPack:com.baranovgroup.nstyle, fontPkg:com.baranovgroup.nstyle, com.android.systemui=overlay:com.baranovgroup.nstyle, com.android.systemui.navbar=overlay:system}
                                                                               java.lang.RuntimeException: Unable to start receiver com.tanvirsingh.fragmentsdemo.NetworkChangeReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                at android.app.ActivityThread.handleReceiver(ActivityThread.java:2778)
                                                                                at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5471)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
                                                                                at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
                                                                                at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                at com.tanvirsingh.fragmentsdemo.DataBaseHelper.<init>(DataBaseHelper.java:20)
                                                                                at com.tanvirsingh.fragmentsdemo.sendMessageFromDatabase.SendMessageFromDBMethod(sendMessageFromDatabase.java:73)
                                                                                at com.tanvirsingh.fragmentsdemo.sendMessageFromDatabase.<init>(sendMessageFromDatabase.java:40)
                                                                                at com.tanvirsingh.fragmentsdemo.NetworkChangeReceiver.onReceive(NetworkChangeReceiver.java:43)
                                                                                at android.app.ActivityThread.handleReceiver(ActivityThread.java:2771)
                                                                                at android.app.ActivityThread.-wrap14(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5471)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                                at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)


text:
Q: Why different results for "not in" in postgresql? I would like to know how many serial numbers appear in table 2 but not in table 1. When I run:
SELECT count(DISTINCT sn)
FROM table2
WHERE sn NOT IN (table1.sn)

I get 0.
I am certain there exist serial numbers in table2 not in table1, luckily when I run:
SELECT count(DISTINCT sn)
FROM table2
WHERE sn NOT IN (
SELECT DISTINCT table1.sn
FROM(
table1 INNER JOIN table2 ON(table1.sn=table2.sn)
)
)

I get a large number of sn's.
This is not logically possible!
It means there are serial numbers in table 2 not in the join, but there are no serial numbers in table 2 not in table 1.
I tried casting the results from the query & sub query to text and that didn't work.


text:
Q: Is DateTimeOffset.UtcNow.DateTime equivalent to DateTime.UtcNow? I need to upgrade some piece of code from statically calling DateTime.UtcNow to calling a time provider service which returns, basically, DateTimeOffset.UtcNow. To further convert this DateTimeOffset instance to DateTime, there is the DateTime property. Alternatively, it seems that there is an implicit conversion from DateTimeOffset to DateTime.
I'm a bit concerned that there might be some edge cases that I can't see right now where these two would not be equivalent. Are there?

A: Internally it looks like this:
  public static DateTimeOffset UtcNow {
        get {
            return new DateTimeOffset(DateTime.UtcNow);
        }
  }

  public DateTime DateTime {
        get {
            return ClockDateTime;
        }
    }
   private DateTime ClockDateTime {
        get {
            return new DateTime((m_dateTime + Offset).Ticks, DateTimeKind.Unspecified);
        }
    }

  public DateTime UtcDateTime {
        [Pure]
        get {
            Contract.Ensures(Contract.Result<DateTime>().Kind == DateTimeKind.Utc);
            return DateTime.SpecifyKind(m_dateTime, DateTimeKind.Utc);
        }
    }

So it looks like you could end up getting a DateTime that was based on UTC but then gets converted to Unspecified.

A: If you look at the value of DateTimeOffset.UtcNow.DateTime.Kind you will see the it is Unspecified. Unspecified kinds are treated as local times by the framework. The kind of DateTime.UtcNow is Utc, so there will be differences when timezone conversions are applied to and from the local timezone.
The work around is to use the DateTimeOffset.UtcNow.UtcDateTime which has the Utc kind specified.

A: From the .NET reference source:
public static DateTimeOffset UtcNow {
    get {
        return new DateTimeOffset(DateTime.UtcNow);
    }
}

So, yes, it looks like it's equivalent to DateTime.UtcNow.


text:
Q: How to solve the problem of PayPal gateway has rejected request? I am using Magento 1.9 and I have set up PayPal express checkout as payment method but I am getting an error like this:

PayPal gateway has rejected request. Callback URL is wrong type; you
  must use the HTTPS (#11813: Callback URL is wrong type).

So, can any one help me in this problem ?

A: If you dont have an SSL certificate you can set "transfer shipping options" to NO and "Enable SSL verification" to NO then it should work ok.


text:
Q: ¿Otra forma de seleccionar el máximo de una columna sin el MAX()? SQL Server Saludos a la comunidad.
Tengo una duda sobre sacar el máximo de una columna, pero de otra forma, yo he intentado la siguiente forma:
USE bdprueba
GO

SELECT MAX(M1.TotalDeber) MaxTotalDeber, M1.DocIdentidad, M1.NomCliente
FROM MOCK_DATA M1 JOIN MOCK_DATA M2 ON M1.DocIdentidad = M2.DocIdentidad
GROUP BY M1.DocIdentidad, M1.NomCliente

Y sí me resulta, el resultado es el siguiente:

Ahora bien, procedo a explicar:

*

*Tengo una tabla que es la siguiente:

*

*Requiero el máximo valor de la columna TotalDeber por cada cliente; como ven, una forma sencilla de hacerlo es cómo la consulta que coloqué; sin embargo aquí viene mi problema.

*Antes dejar en claro que esta tabla es una simulación de datos de una consulta más grande que tengo. La columna TotalDeber proviene en realidad de otra tabla y para su cálculo la consulta original es más o menos así:
SELECT tabla1.Identidad, tabla1.CodIdentidad,
   SUM(SELECT total FROM otraTabla WHERE condicion='1') TotalDeber
FROM tabla1 JOIN tabla2 ON tabla1.identidad = tabla2.identidad
GROUP BY tabla1.Identidad, tabla1.CodIdentidad

*

*Como se puede ver el TotalDeber proviene en realidad del uso de SUM() y por tanto no me permitiría hacer uso del MAX() y ese es mi verdadero problema, por que requiero si o sí el máximo de esa columna por cliente y que el resultado sea tal cual este: 1
Entonces, mi problema y consulta es: ¿Qué otra forma existe de sacar el máximo TotalDeber por columna sin la posibilidad de usar el MAX()? Y sin usar bucles, porque ya he probado y eso incrementa muchísimo el tiempo de ejecución.
Gracias de antemano a todos los que respondan.

A: En ese caso puedes convertir la primera consulta en una subconsulta y ahí usar el MAX
SELECT Identidad, CodIdentidad, MAX(TotalDeber) as TotalDeber
FROM (
         SELECT tabla1.Identidad,
                tabla1.CodIdentidad,
                SUM(SELECT total FROM otraTabla WHERE condicion = '1') TotalDeber
         FROM tabla1
                  JOIN tabla2 ON tabla1.identidad = tabla2.identidad
         GROUP BY tabla1.Identidad, tabla1.CodIdentidad
     ) t
GROUP BY Identidad, CodIdentidad


text:
Q: NSRegularExpression (iOS) A have a little problem with parsing HTML into string. I have a big string and I need delete characters between "script" tag. Something like this :
    <script...>Some text here</script>

So I need to delete "Some text here". I think it would be great to use NSRegularExpression. Can anybody help me ? Thanks a lot.

A: While you will generally be advised to not parse HTML with regular expressions (see my all-time favorite S.O. answer), you can approximate it with something like:
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"<script.*?>(.*?)</script>"
                                                                       options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators
                                                                         error:&error];

[regex enumerateMatchesInString:string
                        options:0
                          range:NSMakeRange(0, [string length])
                     usingBlock:^(NSTextCheckingResult *result, NSMatchingFlags flags, BOOL *stop) {

                         NSLog(@"%@", [string substringWithRange:[result rangeAtIndex:1]]);
                     }];


text:
Q: Azure Queue Trigger Functions Not picking the queued message The queue function is working fine in the developer machine. But once published to Azure, after a day or two, the function is not picking the queue message. the queue message is displayed in the Storage Account queue and not dequeued.
When we are using the Run command in monitor then it is picking the queue message and processing successfully.
For your Information: Not access issues, all application and id and secrets are correctly configured, all hosted in the same tenant, No exceptions generated.

A: It's been a long time since I have a similar problem, but what worked for me when some function went to sleep was:

*

*Regenerate the connection string.

*Remove the resource (function) and create a new one.

I don't really know why sometimes some functions goes idle and then it no longer trigger.


text:
Q: oh-my-zsh history completion I have recently switched to zsh, using robbyrussell's oh-my-zsh. Before that i used bash with a lot of custom stuff and i am only missing one thing because zsh is trying to be 'too smart':
If i type git commit and then ↑ zsh goes through all recent git commands. What i really want though, is going through all commands that start with git commit (not just git).
How can i achieve this behavior in (oh-my-)zsh?

A: I have found the solution to my problem in the ZSH documentation. Oh-my-zsh seems to map the ↑ and ↓ Keys to something like
bindkey '\e[A' history-search-backward
bindkey '\e[B' history-search-forward

Which yields the exact behavior I described above. The ZSH Documentation describes the behavior of history-search-backward as

Search backward in the history for a line beginning with the first word in the buffer.

What I wanted instead was the following mapping, which I inserted into my ~/.zshrc:
bindkey '\e[A' history-beginning-search-backward
bindkey '\e[B' history-beginning-search-forward

The behavior of history-beginning-search-backward is as follows:

Search forward in the history for a line beginning with the current line up to the cursor. This leaves the cursor in its original position.

Also, if \e[A doesn't work for the up or down arrows, press <ctrl-v><KEY (e.g., up arrow)> in another terminal which gives ^[OA. Then you can use this instead of \e[A. The process is described here: http://zshwiki.org/home/zle/bindkeys

A: I wanted the same behaviour for zsh with oh-my-zsh installed and found plugin history-substring-search.
I achieved the same behaviour described above by adding the plugin to my ~/.zshrc:

plugins=(git brew npm history-substring-search)

I guess this plugin did not exist back when this question was raised. Just an alternate way to achieve the same thing.


text:
Q: Not implemented Error for reports with Charts (Graphs) on Azure I installed Crystal Reports on Windows Azure, and most reports work just fine.  However, none of the reports with charts work.  I get Operation not yet implemented error from my web page.
Any help greatly appreciated.
Thanks.

A: If you can install all the needed SAP, I understood that from the thread you mentioned, on a server on premises you could try one of the following options:

*

*Start up a webrole, rdp into it and install what's needed

*Create a startup task that installs the needed material. See this for example.

*Make use of the new IaaS capabilities in Azure. Create a new server from one of the images in the gallery or upload a sys prepped image with the stuff you need on it to Azure.


text:
Q: @testSetup method and System.CalloutException: You have uncommitted work pending Apologies ahead of time for the lengthy post, but I'm receiving the following error when attempting to run the test method below. The error occurs when the upsertProject method is called.
System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out

Test Method
@isTest static void upsertErrorTest() {
    // retrieve project to pass to upsert method
    Project__c p = [SELECT Id, Name FROM Project__c LIMIT 1];
    system.assertEquals('Test Project', p.Name);

    // retrieve replicon project data in apex wrapper
    Replicon.Project project = Replicon.getProjectById(p.Id);

    // instantiate payload to use in mock http request
    String errorPayload = '[insert json payload here]';

    Test.startTest();

    // instantiate the http service mock
    RepliconServiceUpsertMock mock = new RepliconServiceUpsertMock(200, 'Success', errorPayload, null);
    Test.setMock(HttpCalloutMock.class, mock);

    RepliconService service = new RepliconService();
    HttpResponse response = service.upsertProject(project, p.Id);

    // assertions here

    Test.stopTest();
}

Http Callout Method
public HttpResponse upsertProject(Replicon.Project project, String projectId) {
    // serialize the project object for insertion into replicon
    String requestBody = project.toString();

    // instantiate the request to send to replicon
    HttpRequest req = this.getRequest('ImportService1.svc/PutProject4');
    req.setBody(requestBody);

    // send the request to replicon to create a new project
    Http h = new Http();
    HttpResponse response = h.send(req);
    system.debug('[upsertProject] Response:' + response.getBody());

    return response;
}

Test data is created via an @testSetup method in the test class:
@testSetup static void setupData() {
    Account a = new Account();
    a.Name = 'Test Account';
    insert a;

    Opportunity o = new Opportunity();
    o.AccountId = a.Id;
    o.Name = 'Test Opp';
    o.StageName = 'First stage';
    o.CloseDate = system.today();
    o.Amount = 55000.00;
    insert o;

    Project__c p = new Project__c();
    p.Account__c = a.Id;
    p.Opportunity__c = o.Id;
    p.Name = 'Test Project';
    insert p;
}

Mock Class
@isTest
public class RepliconServiceUpsertMock implements HttpCalloutMock {

    protected Integer code;
    protected String status;
    protected String body;
    protected Map<String, String> responseHeaders;

    public RepliconServiceUpsertMock(Integer code, String status, String body, Map<String, String> responseHeaders) {
        this.code = code;
        this.status = status;
        this.body = body;
        this.responseHeaders = responseHeaders;
    }

    public HTTPResponse respond(HTTPRequest req) {

        HttpResponse res = new HttpResponse();
        for(String key : this.responseHeaders.keySet()) {
            res.setHeader(key, this.responseHeaders.get(key));
        }
        res.setBody(this.body);
        res.setStatusCode(this.code);
        res.setStatus(this.status);
        return res;
    }

}

The documentation on using Http callouts with Mocks states:

By default, callouts aren't allowed after DML operations in the same
  transaction because DML operations result in pending uncommitted work
  that prevents callouts from executing. Sometimes, you might want to
  insert test data in your test method using DML before making a
  callout. To enable this, enclose the portion of your code that
  performs the callout within Test.startTest and Test.stopTest
  statements. The Test.startTest statement must appear before the
  Test.setMock statement. Also, the calls to DML operations must not be
  part of the Test.startTest/Test.stopTest block.
DML operations that occur after mock callouts are allowed and don't
  require any changes in test methods.

My code is definitely following the order of execution described above. There is also no other processes (e.g. future, scheduled apex, etc) being generated from related triggers. So I'm perplexed as to why I'm receiving this error.
Additionally, I have attempted to move the test data creation into the test method itself and do away with the @testSetup method, but that produced the same results.
Any idea why this is happening?

A: I ran into something similar to what you're describing here. I had all data setup in a testSetup method and committed no DML's in between Test.startTest() and Test.stopTest(), just querying a record and passing that to the data to a web service.
I also confirmed that there were no fresh DML statements by adding a Limits.getDMLStatements() right before the call out, it returned 0. I have scoured the boards and this is the best example that's similar to the behavior I have run into.
I'm certain it's a bug on the Salesforce side, and unfortunately those can take a while to resolve and I need to have this deployed! I came up with a workaround, and I wanted to share it since it may save you (if you're still having the issue) or someone else a lot of misery!
In my mock class that I was calling during the test, I had my sample JSON responses defined as static strings. So I did a tweaked version of a response built in to the call out:
//This is one of the callout functions that I'm using to hit a REST API
global HttpResponse getAllTemplates() {

    HttpRequest req = setupBaseRequest(null);
    req.setEndpoint(baseEndPoint+'/templates');
    req.setMethod('GET');

    HttpResponse res;
    if (Test.isRunningTest()) {
        res = new HttpResponse();
        res.setBody(MyMockClass.templateExampleJSON);
        res.setStatusCode(200);
        res.setStatus('OK');
    }
    if (!Test.isRunningTest()) res = new Http().send(req);
    return res;
}

Here is my mock class (example only):
@isTest
global class MyMockClass implements HttpCalloutMock {
    global static String templateExampleJSON = '{"templateName":"TestName","TemplateId":"12345"}';

    global HTTPResponse respond(HTTPRequest req) {
    HttpResponse res = new HttpResponse();
    res.setStatusCode(200);
    res.setStatus('OK');

    if (req.getEndPoint().contains('/templates')) {
        res.setBody(templateExampleJSON);
    } else {
        res.setStatusCode(400);
        res.setStatus('Bad Request');
    }

    return res;
}

Doing it this way allows my test class to fire correctly. It also allows the actual response to go out when it's not testing. I feel like it's a little bit of a hack; however, it accomplishes my end goal of testing the functionality.

A: I had a very similar issue but I was able to fix it by adding Test.startTest and stopTest before and after the web service call, as follows:
Test.startTest();

    Test.setMock(WebServiceMock.class, new SRM_MetadataApiMocks.DescribeMetadataResponse());

    List<String> metadataTypes = SRM_SelectMetadataController.getMetadataTypes('callout:fakeNamedCredential');

    Test.stopTest();

A: There were several Salesforce issues related to

System.CalloutException: You have uncommitted work pending. Please
  commit or rollback before calling out

Besides of the issues mentioned, additional issues that I have encountered with, were:

*

*The problem with @future(Callout=true) methods.
First time I reported this was at February 12, 2015  Spring 15 Release (v.33)

*There was a known issue, which was claimed to be fixed in the next year, however, it wasn't really fixed. I have opened another Salesforce support case, they told me the work id of it was "W-3171561" but they never provided a list to known issue and I was promised to be notified once this issue is resolved, however, I was never notified on this issue resolution.

*Later I suggested a workaround for this long-running issue to downgrade the apex class version to 28.

*As of Summer 2019 release (version 46) I can't reproduce the future callout issue anymore. However, I have found another issue, the reasons for which are not clear to me. It is somehow related to user trigger and batch or queueable jobs. I have opened another Salesforce support case for this. Let's see how this goes. I have posted about this issue today on my blog

*The issue with user insert and future seems to be fixed for future jobs but not for queueable jobs.
Conclusion.
There is some constant long-running ever-lasting bug related to uncommitted work pending.
It is hard to find and reproduce it. Every time when Salesforce makes some change to this implementation, it becomes harder to debug and to troubleshoot it, but the core issue is still present.
Some workaround might be applicable for this issue.

*

*Try to downgrade the class version to 28.0 or older where issue wasn't introduced yet (the version should be older than Spring 15).

*Try to move the setup code into method annotated with @testSetup annotation. It is not clear why, however, in many cases this helps to workaround this issue


text:
Q: Static Array of objects of one class inside another class/ Constructors I have a question related to the default constructor in C++.
Following are the classes and the code:
class Name
{
  public:
         Name(string name);
  private:
         m_name;
}

class DataBase
{
 public:
      DataBase();
      void addToDB(string name);
      Name* getPtrToName(string name);
 private:
      Name m_Name[10];
      int NoOfEntries;
 }

Now I am trying to create an object of class DataBase and add new entries to the database.
 /*
 * Name.cpp
 */
Name::Name(string name) //overloaded constructor
{
   m_name = name;
}

/*
* DataBase.cpp
*/
DataBase::addToDB(string name) // add entries to the database
{
    Name newEntryToDB(name);
    m_Name[NoOfEntries] = newEntryToDB;
    NoOfEntries++;
 }

DataBase::DataBase() // gives an error stating no matching call for function Name::Name()
{
  NoOfEntries = 0;
}

The error "no matching call for function Name::Name() "
Now I understand that I can simply define a default constructor in the Name.cpp and resolve the compilation error.
But isn't the default constructor invoked by the compiler automatically?
This should possibly avoid the trigger of the error.
Is there any other way to resolve this error apart from defining the default constructor in Name.cpp?

A:
But isn't the default constructor invoked by the compiler automatically?

No it is not.  As soon as you provide your own constructor the compiler will no longer provide a default constructor.  You will either have to make one or you could just use
Name() = default;

In the class declaration in the header file to declare a default constructor.
Alternatively you could switch to using a std::vector which will allow you to have an "array" but allow you to add to it one object at a time.


text:
Q: Can you use a varialben to build a variable in Gitlab CI? Is it possible in a gitlab CICD pipline to build a variable dynamically with a variable?
Sample:
i have a variable in gitlab "TEST_MASTER".
script:
- echo "$TEST_"$CI_COMMIT_BRANCH""

I need the result from the variable TEST_MASTER, but the part of MASTER must come from the branch variable.

A: That looks like a bash script to me so assuming TEST_MASTER already has a value, you should be able echo it like this:
script:
- myvar=TEST_"$CI_COMMIT_BRANCH"
  echo "${!myvar}"

For more information, check this question and it's answers


text:
Q: Find RMSE from StatsModels OLS Results I playing around with some regression analyses in Python using StatsModels. I am getting a little confused with some terminology and just wanted to clarify.
I have run a regression and get the following results.
 OLS Regression Results
==============================================================================
Dep. Variable:                    TTo   R-squared:                       0.048
Model:                            OLS   Adj. R-squared:                  0.032
Method:                 Least Squares   F-statistic:                     2.933
Date:                Fri, 22 Jun 2018   Prob (F-statistic):             0.0921
Time:                        11:44:53   Log-Likelihood:                -380.18
No. Observations:                  60   AIC:                             764.4
Df Residuals:                      58   BIC:                             768.5
Df Model:                           1
Covariance Type:            nonrobust
===================================================================================
                      coef    std err          t      P>|t|      [0.025      0.975]
-----------------------------------------------------------------------------------
Intercept         472.7183     17.942     26.347      0.000     436.804     508.633
Var1    -1.2158      0.710     -1.713      0.092      -2.637       0.205
==============================================================================
Omnibus:                       25.817   Durbin-Watson:                   0.371
Prob(Omnibus):                  0.000   Jarque-Bera (JB):                4.466
Skew:                           0.107   Prob(JB):                        0.107
Kurtosis:                       1.681   Cond. No.                         25.3
==============================================================================

Now,  I get the R-squared values etc and the significance tests. Where can I find the mean squared error which shows the absolute measure of fit within the summary? Is it even there but hidden in something?
I found that I can use model.resid_mse, which from the documentation stats it provides a residual sum of squares by dividing by the residual degrees of freedom. Is this the same as the average sum of squares? Is this just assuming it's from a sample? am I being simple?
It this is what I am after, then surely to get the RMSE, I can just take the square root of the resid_mse (such as np.sqrt(model.resid_mse)) to find the absolute fit of the model?
Any help to clarify is greatly appreciated.
Regards,
BJR


text:
Q: Execute qml->build execute file from Another qml gui I'm trying to figure out, how to execute qml build run file from another qml gui button, when onclicked event happened, it should execute another qml build exe file, I have tried with QProcess, but I'm unable to figure out QProcessEnvironment for qml build.
QString filepath="/QML TYPES/build-Task-Desktop_Qt_5_15_0_GCC_64bit-Debug/Task";
QProcess process;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.insert("TMPDIR", "/home/ubuntu/Qt/5.15.0/gcc_64/bin/qml"); // Add an environment variable
env.insert("COMPILER", env.value("Compiler") + "/usr/lib/ccache/x86_64-linux-gnu-g++");
process.setProcessEnvironment(env);
process.start(filepath);

But, Unfortunately above code is not working, so, please help to get out of this issue.
Thanks in advance.


text:
Q: How can I connect QtcpSockets to about 100 servers without a UI hang? How can I connect QtcpSockets to about 100 servers without a UI hang?
When I create 100 QTcpSockets to connect to each server and call the connectToHost() function, the QDialog stuck. Is there a way to run the connectToHost() part as a background job?
As a result, all connections are made, but while trying to connect (call connectToHost() the UI is in 'No Response' state.
// onConnectToICPMC is the slot function connected to the dialog button pressed signal
void WidgetUploadFile::onConnectToICPMC()
{
    // m_tcpClients is a QVector containing a custm QObject Class that manages QTcpSocket
    for(int i = 0; i < m_tcpClients.size(); ++i)
    {
        if(m_tcpClients.at(i)->state() != QAbstractSocket::ConnectedState)
        {
            m_tcpClients.at(i)->connectToServer(); // Call the connectToHost() function of QTcpSocket.
        }
        emit sendConnProgBarUpdate(i+1);
        emit sendCurrentSockLog(m_tcpClients.at(i)->ipAddress(), m_tcpClients.at(i)->state());
    }
}

A dialog that hangs while each QTcpSocket connects to the server.:

After all QTcpSockets try to connect to the server:

A: Time-consuming tasks should not be run on the main thread. In that case there are 2 strategies:

*

*Use of threads.

*Divide into subtasks and execute in parts every T seconds.

In this case I think the second option is the best using QTimeLine.
*.h
private:
   void handleFrameChanged(int i);
   QTimeLine timeLine;

*.cpp
{
    // constructor
    timeLine.setRange(0, m_tcpClients.size()-1);
    connect(&timeLine, &QTimeLine::frameChanged, this, &WidgetUploadFile::handleFrameChanged);
}

void WidgetUploadFile::onConnectToICPMC()
{
    timeLine.start();
}

void WidgetUploadFile::handleFrameChanged(int i){
    auto client = m_tcpClients.at(i);
    if(client->state() != QAbstractSocket::ConnectedState){
        client->connectToServer();
    }
    emit sendConnProgBarUpdate(i + 1);
    emit sendConnProgBarUpdate(client->ipAddress(), client->state());
}


text:
Q: Assign a unique value to a user each time How to generate and assign a unique value (sequence) to a user every time a user logs in? If two users login in at the same time simultaneously, I should be able to avoid assigning same value to both.
And it is not a specific requirement. It can be any operation for which a sequence is required to be generated and assigned. I am trying to understand how the situation can be handled if the operation happens at the same time. I require a solution for Oracle 11g specifically.

A: You named it - use a sequence.
create sequence myseq;

Fetch value from it using
select myseq.nextval from dual;

Assingn it to that user. Though, I'm not sure I understand what that actually is; what would you assign it to, really? I hope you know.

A: Without understanding what exactly what you want to accomplish with the unique value, as you are not explaining it, you might do something like this:
Create a Sequence for creating an incremental sequential number
SQL> create sequence my_seq start with xxx increment by yyy cache zzz;

Where

*

*xxx is the number where the sequence should start

*yyy is the increment value of the sequence ( normally 1 )

*As you plan to use it each time the user logs in, it might be a good idea to cache some values for faster access. zzz represents the number of values of the sequence you want to cache.

Assign the values to each user which logs in
A way to assign this value to a user each time logs in is through a logon trigger and related it to the properties of the session, using the advantages provided by the default context SYS_CONTEXT. Of course, for that you need a DBA user to install the trigger in an admin schema ( as sys or system )
An example to store those values in a custom audit table:
SQL> CREATE TRIGGER AFT_LOG_DBT
     AFTER LOGON ON DATABASE
     DECLARE
     -- declare variables
     v_db_user varchar2(100);
     v_os_user varchar2(100);
     v_seq number;
     BEGIN
         SELECT sys_context('userenv','session_id') , -- the session id
                sys_context('userenv','osuser') , -- the os user
                my_seq.nextval                   -- the sequence next val
         INTO
               v_db_user,
               v_os_user,
               v_seq
         FROM DUAL;
     -- insert those values into an audit table
     insert into your_schema.log_audit ( logon_time , db_user, os_user, seq_val )
     values ( sysdate , v_db_user, v_os_user, v_seq );
     commit;
   END;

As you were not explaining what you wanted to do with the unique sequence value, I tried to provide you with an example of a use case.
I have never gotten duplicate sequence numbers from a sequence generator unless it has wrapped the max value or is set to cycle.


text:
Q: ID Gets changed because out of range value of datatype Big Int I am getting this error in mySQL database.
I have a table where I have take ID column & its data type set BIGINT & values to 100.
Now I am trying to pass this ID (110585777261232376048) under but it gets changed to this (9223372036854775807).
I can't change length of my ID since its auto-generated dynamic value.
How do I change my column properties to make it work correctly?

A: BIGINT unsigned can take a range of 0 to 18,446,744,073,709,551,615. Try setting the column as UNSIGNED BIGINT .. It cannot accept a value beyond this. You need to convert the datatype to DOUBLE if the range exceeds this limit


text:
Q: Optimizing the performance of mass inserts from file SQLite Android I'm currently working with a friend on developing a game that deals with words primarily, so
I have a word list that I entered into a database. The plan is to have individual databases(as in on everyone's phone) be filled up during the installation. I have done every optimization I found on StackOverflow and Google, but it still takes too long, about almost 2 minutes, which is unacceptable for such a simple game.
The wordlist itself consists of around 350000 words, so we have created a file which contains
the same number of inserts(is this the best way to go?), i.e each word has its own line in a text file with a corresponding insert. This is what I currently have:
public int initializeDatabase(Context context) throws IOException {

        int result = 0; //returns querys completed
        SQLiteDatabase db = this.getWritableDatabase();
        //Opening a file for reading
        InputStream insertStream = context.getResources().getAssets().open("allWords.txt");
        BufferedReader insertReader = new BufferedReader(new InputStreamReader(insertStream));

        //Iterating through lines
        String query = "INSERT INTO words(word,tezina) VALUES(?,?)";//? entered in code
        SQLiteStatement statement = db.compileStatement(query);
        db.beginTransaction();//otvaram transakciju
        while(insertReader.ready()){
                statement.clearBindings();
                String toExtract = insertReader.readLine();//uzimam upit
                String word = "";
                int tezina = (int)toExtract.charAt(toExtract.length() - 2);//extract word difficulty
                for(int i = toExtract.length() - 4; i > 0; i--){

                    if(toExtract.charAt(i) == '(') break;
                    else word += toExtract.charAt(i);
                }//extract word along with ' ', from back to save time
                String reverseWord = "";
                for(int j = word.length() - 1; j >= 0; j--){
                    reverseWord = reverseWord + word.charAt(j);
                }//reverse it
                statement.bindString(1, word);//bind parameters
                statement.bindLong(2, tezina);
                statement.execute();
                result++;
        }
        db.setTransactionSuccessful();
        db.endTransaction();//end of transaction
        insertReader.close();
        return result;//return queries completed

    }

350262 inserts, line - by - line, taking about 2 minutes. Any better way of doing all of this?

A: The parsing of the text file is rather inefficient (using String instead of StringBuilder; constructing the unused reverseWord).
However, mobile devices are not fast; you will never be able to parse and insert 350,000 rows without some delay.
You should not construct the database on the device in the first place.
Just ship the complete database file in the assets folder, and copy it to your data directory after installation.


text:
Q: How to change field's answer when I use gem 'rails_admin'? In my project I am using RailsAdmin and have two models, Product & Product_rate.
has_many :product_rates
belongs_to :product

The code in rails_admin.rb is
config.model Product do
  ....
  list do
    field :product_rates
  end

I want show the product_rates' rank in the "field :product_rates". The product_rate model looks like this.
class ProductRate < ActiveRecord::Base
  belongs_to :product
  attr_accessible :rank, :product_id
end

The best results can be displayed the rank sum.
For example: Product XX has ranks what are 0,1,0,2. I want to show rank sum in the product_rates, which would be 3.
Any help is appreciated. Thanks

A: How about you add a total_rank method to your Product model and use that.
class Product < ActiveRecord::Base
  has_many :product_rates

  def total_rank
    product_rates.map(&:rank).inject(:+)
  end
end

Then in your initializers/rails_admin.rb:
config.model Product do
  ....
  list do
    field :total_rank do
      label "Rating"
    end
  end

Maybe that doesn't exactly answer your question. According to the docs maybe you want to try something like this..
 list do
   field :product_rating do
     formatted_value do # used in form views
       value.map(&:rank).inject(:+)  # I'm assuming the value is an array of products
     end
   end
 end


text:
Q: Installing Midnight Commander from sources (no root privileges) I tried to configure
./configure --prefix=/localfolder
glib-2.26.1/
make
make install
but it fails at make stage.
trying to configure
mc-4.6.1/
and make doesn't obviously work.
What are the steps I need to make in order to install midnight comander for my local user in a custom folder?
Make for glib gives me these errors
/usr/bin/msgfmt: found 2 fatal errors
cp: cannot stat `test.mo': No such file or directory
gmake[4]: *** [test.mo] Error 1
gmake[4]: Leaving directory `/remote/folder/mc/glib-2.26.1/gio/tests'
gmake[3]: *** [all-recursive] Error 1
gmake[3]: Leaving directory `/remote/folder/mc/glib-2.26.1/gio'
gmake[2]: *** [all] Error 2
gmake[2]: Leaving directory `/remote/folder/mc/glib-2.26.1/gio'
gmake[1]: *** [all-recursive] Error 1
gmake[1]: Leaving directory `/remote/folder/mc/glib-2.26.1'
gmake: *** [all] Error 2

A: Here's your problem:
./configure --prefix=/localfolder
This will install to a folder underneath the root directory, which is not writable for a normal user.
This is the correct commandline:
./configure --prefix=~/localfolder && make && make install
(Of course, I'm assuming that you don't have write priveliges to /localfolder. The missing file test.mo may be another problem entirely.)


text:
Q: django - multithreading queries I'm using django 1.8 and my logic requires queries in a loop.
I have multiple levels foreign keys:
Trucks->Boxes->Bottles
and thus multiple cascaded for loops.
The code below is over-simplified, but the only way to make it faster seems to be parallelizing the main loop.
I need to speed it up so I used multiprocessing as shown below.
The queries are read only!
This code with sqlite and postgres gets into deadlock.
A similar code on sqlite3 failed with:
DatabaseError: database disk image is malformed

I need to know how to speed this up.
If I'm required to put the model objects in a lock, then how should I do it to avoid too much locking
views.py
from django.shortcuts import render
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from django.http import HttpResponseRedirect

from models import Truck, Box, Bottle

from multiprocessing import Pool
from functools import partial
MULTI_THREAD = True

def get_data_per_bottle(box):
    data = Bottle.objects.filter(box=box).values_list('color')
    data = [x[0] for x in data]
    return data

def get_data_per_box(some_param, truck):
    rows = []
    for bottle in Box.objects.filter(truck=truck):
        rows.append(get_data_per_bottle(bottle))
    return rows

def get_rows(location):
    trucks = Truck.objects.filter(location=location)
    some_param = ''
    data = []
    if MULTI_THREAD:
        pool = Pool(processes=2)
        func = partial(get_data_per_box, some_param)
        data = pool.map(func, trucks)
        pool.close()
        pool.join()
    else:
        for truck in trucks:
            data.append(get_data_per_box(some_param, truck))

    return data

def index(request):
    rows = get_rows('usa')
    return render(request, 'index.html', locals())

models.py
from django.db import models

class Bottle(models.Model):
    color = models.CharField(max_length=30)

class Box(models.Model):
    bottle = models.ForeignKey(Bottle)

class Truck(models.Model):
    box = models.ForeignKey(Box)
    location = models.CharField(max_length=30)